blob: 73e41cff37388cac22741140d981f4ef6056e673 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427#include <linux/module.h>
428#include <linux/types.h>
429#include <linux/string.h>
430#include <linux/kernel.h>
431#include <linux/delay.h>
432#include <linux/timer.h>
433#include <linux/mm.h>
434#include <linux/interrupt.h>
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -0800435#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437#include <linux/errno.h>
438#include <linux/genhd.h>
439#include <linux/slab.h>
440#include <linux/pci.h>
441#include <linux/ide.h>
442#include <linux/smp_lock.h>
443#include <linux/completion.h>
444#include <linux/bitops.h>
Arjan van de Vencf8b8972006-03-23 03:00:45 -0800445#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447#include <asm/byteorder.h>
448#include <asm/irq.h>
449#include <asm/uaccess.h>
450#include <asm/io.h>
451#include <asm/unaligned.h>
452
453/*
454 * partition
455 */
456typedef struct os_partition_s {
457 __u8 partition_num;
458 __u8 par_desc_ver;
459 __u16 wrt_pass_cntr;
460 __u32 first_frame_addr;
461 __u32 last_frame_addr;
462 __u32 eod_frame_addr;
463} os_partition_t;
464
465/*
466 * DAT entry
467 */
468typedef struct os_dat_entry_s {
469 __u32 blk_sz;
470 __u16 blk_cnt;
471 __u8 flags;
472 __u8 reserved;
473} os_dat_entry_t;
474
475/*
476 * DAT
477 */
478#define OS_DAT_FLAGS_DATA (0xc)
479#define OS_DAT_FLAGS_MARK (0x1)
480
481typedef struct os_dat_s {
482 __u8 dat_sz;
483 __u8 reserved1;
484 __u8 entry_cnt;
485 __u8 reserved3;
486 os_dat_entry_t dat_list[16];
487} os_dat_t;
488
489#include <linux/mtio.h>
490
491/**************************** Tunable parameters *****************************/
492
493
494/*
495 * Pipelined mode parameters.
496 *
497 * We try to use the minimum number of stages which is enough to
498 * keep the tape constantly streaming. To accomplish that, we implement
499 * a feedback loop around the maximum number of stages:
500 *
501 * We start from MIN maximum stages (we will not even use MIN stages
502 * if we don't need them), increment it by RATE*(MAX-MIN)
503 * whenever we sense that the pipeline is empty, until we reach
504 * the optimum value or until we reach MAX.
505 *
506 * Setting the following parameter to 0 is illegal: the pipelined mode
507 * cannot be disabled (calculate_speeds() divides by tape->max_stages.)
508 */
509#define IDETAPE_MIN_PIPELINE_STAGES 1
510#define IDETAPE_MAX_PIPELINE_STAGES 400
511#define IDETAPE_INCREASE_STAGES_RATE 20
512
513/*
514 * The following are used to debug the driver:
515 *
516 * Setting IDETAPE_DEBUG_INFO to 1 will report device capabilities.
517 * Setting IDETAPE_DEBUG_LOG to 1 will log driver flow control.
518 * Setting IDETAPE_DEBUG_BUGS to 1 will enable self-sanity checks in
519 * some places.
520 *
521 * Setting them to 0 will restore normal operation mode:
522 *
523 * 1. Disable logging normal successful operations.
524 * 2. Disable self-sanity checks.
525 * 3. Errors will still be logged, of course.
526 *
527 * All the #if DEBUG code will be removed some day, when the driver
528 * is verified to be stable enough. This will make it much more
529 * esthetic.
530 */
531#define IDETAPE_DEBUG_INFO 0
532#define IDETAPE_DEBUG_LOG 0
533#define IDETAPE_DEBUG_BUGS 1
534
535/*
536 * After each failed packet command we issue a request sense command
537 * and retry the packet command IDETAPE_MAX_PC_RETRIES times.
538 *
539 * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
540 */
541#define IDETAPE_MAX_PC_RETRIES 3
542
543/*
544 * With each packet command, we allocate a buffer of
545 * IDETAPE_PC_BUFFER_SIZE bytes. This is used for several packet
546 * commands (Not for READ/WRITE commands).
547 */
548#define IDETAPE_PC_BUFFER_SIZE 256
549
550/*
551 * In various places in the driver, we need to allocate storage
552 * for packet commands and requests, which will remain valid while
553 * we leave the driver to wait for an interrupt or a timeout event.
554 */
555#define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
556
557/*
558 * Some drives (for example, Seagate STT3401A Travan) require a very long
559 * timeout, because they don't return an interrupt or clear their busy bit
560 * until after the command completes (even retension commands).
561 */
562#define IDETAPE_WAIT_CMD (900*HZ)
563
564/*
565 * The following parameter is used to select the point in the internal
566 * tape fifo in which we will start to refill the buffer. Decreasing
567 * the following parameter will improve the system's latency and
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200568 * interactive response, while using a high value might improve system
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 * throughput.
570 */
571#define IDETAPE_FIFO_THRESHOLD 2
572
573/*
574 * DSC polling parameters.
575 *
576 * Polling for DSC (a single bit in the status register) is a very
577 * important function in ide-tape. There are two cases in which we
578 * poll for DSC:
579 *
580 * 1. Before a read/write packet command, to ensure that we
581 * can transfer data from/to the tape's data buffers, without
582 * causing an actual media access. In case the tape is not
583 * ready yet, we take out our request from the device
584 * request queue, so that ide.c will service requests from
585 * the other device on the same interface meanwhile.
586 *
587 * 2. After the successful initialization of a "media access
588 * packet command", which is a command which can take a long
589 * time to complete (it can be several seconds or even an hour).
590 *
591 * Again, we postpone our request in the middle to free the bus
592 * for the other device. The polling frequency here should be
593 * lower than the read/write frequency since those media access
594 * commands are slow. We start from a "fast" frequency -
595 * IDETAPE_DSC_MA_FAST (one second), and if we don't receive DSC
596 * after IDETAPE_DSC_MA_THRESHOLD (5 minutes), we switch it to a
597 * lower frequency - IDETAPE_DSC_MA_SLOW (1 minute).
598 *
599 * We also set a timeout for the timer, in case something goes wrong.
600 * The timeout should be longer then the maximum execution time of a
601 * tape operation.
602 */
603
604/*
605 * DSC timings.
606 */
607#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
608#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
609#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
610#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
611#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
612#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
613#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
614
615/*************************** End of tunable parameters ***********************/
616
617/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 * Read/Write error simulation
619 */
620#define SIMULATE_ERRORS 0
621
622/*
623 * For general magnetic tape device compatibility.
624 */
625typedef enum {
626 idetape_direction_none,
627 idetape_direction_read,
628 idetape_direction_write
629} idetape_chrdev_direction_t;
630
631struct idetape_bh {
Stephen Rothwellab057962007-08-01 23:46:44 +0200632 u32 b_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 atomic_t b_count;
634 struct idetape_bh *b_reqnext;
635 char *b_data;
636};
637
638/*
639 * Our view of a packet command.
640 */
641typedef struct idetape_packet_command_s {
642 u8 c[12]; /* Actual packet bytes */
643 int retries; /* On each retry, we increment retries */
644 int error; /* Error code */
645 int request_transfer; /* Bytes to transfer */
646 int actually_transferred; /* Bytes actually transferred */
647 int buffer_size; /* Size of our data buffer */
648 struct idetape_bh *bh;
649 char *b_data;
650 int b_count;
651 u8 *buffer; /* Data buffer */
652 u8 *current_position; /* Pointer into the above buffer */
653 ide_startstop_t (*callback) (ide_drive_t *); /* Called when this packet command is completed */
654 u8 pc_buffer[IDETAPE_PC_BUFFER_SIZE]; /* Temporary buffer */
655 unsigned long flags; /* Status/Action bit flags: long for set_bit */
656} idetape_pc_t;
657
658/*
659 * Packet command flag bits.
660 */
661/* Set when an error is considered normal - We won't retry */
662#define PC_ABORT 0
663/* 1 When polling for DSC on a media access command */
664#define PC_WAIT_FOR_DSC 1
665/* 1 when we prefer to use DMA if possible */
666#define PC_DMA_RECOMMENDED 2
667/* 1 while DMA in progress */
668#define PC_DMA_IN_PROGRESS 3
669/* 1 when encountered problem during DMA */
670#define PC_DMA_ERROR 4
671/* Data direction */
672#define PC_WRITING 5
673
674/*
675 * Capabilities and Mechanical Status Page
676 */
677typedef struct {
678 unsigned page_code :6; /* Page code - Should be 0x2a */
679 __u8 reserved0_6 :1;
680 __u8 ps :1; /* parameters saveable */
681 __u8 page_length; /* Page Length - Should be 0x12 */
682 __u8 reserved2, reserved3;
683 unsigned ro :1; /* Read Only Mode */
684 unsigned reserved4_1234 :4;
685 unsigned sprev :1; /* Supports SPACE in the reverse direction */
686 unsigned reserved4_67 :2;
687 unsigned reserved5_012 :3;
688 unsigned efmt :1; /* Supports ERASE command initiated formatting */
689 unsigned reserved5_4 :1;
690 unsigned qfa :1; /* Supports the QFA two partition formats */
691 unsigned reserved5_67 :2;
692 unsigned lock :1; /* Supports locking the volume */
693 unsigned locked :1; /* The volume is locked */
694 unsigned prevent :1; /* The device defaults in the prevent state after power up */
695 unsigned eject :1; /* The device can eject the volume */
696 __u8 disconnect :1; /* The device can break request > ctl */
697 __u8 reserved6_5 :1;
698 unsigned ecc :1; /* Supports error correction */
699 unsigned cmprs :1; /* Supports data compression */
700 unsigned reserved7_0 :1;
701 unsigned blk512 :1; /* Supports 512 bytes block size */
702 unsigned blk1024 :1; /* Supports 1024 bytes block size */
703 unsigned reserved7_3_6 :4;
704 unsigned blk32768 :1; /* slowb - the device restricts the byte count for PIO */
705 /* transfers for slow buffer memory ??? */
706 /* Also 32768 block size in some cases */
707 __u16 max_speed; /* Maximum speed supported in KBps */
708 __u8 reserved10, reserved11;
709 __u16 ctl; /* Continuous Transfer Limit in blocks */
710 __u16 speed; /* Current Speed, in KBps */
711 __u16 buffer_size; /* Buffer Size, in 512 bytes */
712 __u8 reserved18, reserved19;
713} idetape_capabilities_page_t;
714
715/*
716 * Block Size Page
717 */
718typedef struct {
719 unsigned page_code :6; /* Page code - Should be 0x30 */
720 unsigned reserved1_6 :1;
721 unsigned ps :1;
722 __u8 page_length; /* Page Length - Should be 2 */
723 __u8 reserved2;
724 unsigned play32 :1;
725 unsigned play32_5 :1;
726 unsigned reserved2_23 :2;
727 unsigned record32 :1;
728 unsigned record32_5 :1;
729 unsigned reserved2_6 :1;
730 unsigned one :1;
731} idetape_block_size_page_t;
732
733/*
734 * A pipeline stage.
735 */
736typedef struct idetape_stage_s {
737 struct request rq; /* The corresponding request */
738 struct idetape_bh *bh; /* The data buffers */
739 struct idetape_stage_s *next; /* Pointer to the next stage */
740} idetape_stage_t;
741
742/*
743 * REQUEST SENSE packet command result - Data Format.
744 */
745typedef struct {
746 unsigned error_code :7; /* Current of deferred errors */
747 unsigned valid :1; /* The information field conforms to QIC-157C */
748 __u8 reserved1 :8; /* Segment Number - Reserved */
749 unsigned sense_key :4; /* Sense Key */
750 unsigned reserved2_4 :1; /* Reserved */
751 unsigned ili :1; /* Incorrect Length Indicator */
752 unsigned eom :1; /* End Of Medium */
753 unsigned filemark :1; /* Filemark */
754 __u32 information __attribute__ ((packed));
755 __u8 asl; /* Additional sense length (n-7) */
756 __u32 command_specific; /* Additional command specific information */
757 __u8 asc; /* Additional Sense Code */
758 __u8 ascq; /* Additional Sense Code Qualifier */
759 __u8 replaceable_unit_code; /* Field Replaceable Unit Code */
760 unsigned sk_specific1 :7; /* Sense Key Specific */
761 unsigned sksv :1; /* Sense Key Specific information is valid */
762 __u8 sk_specific2; /* Sense Key Specific */
763 __u8 sk_specific3; /* Sense Key Specific */
764 __u8 pad[2]; /* Padding to 20 bytes */
765} idetape_request_sense_result_t;
766
767
768/*
769 * Most of our global data which we need to save even as we leave the
770 * driver due to an interrupt or a timer event is stored in a variable
771 * of type idetape_tape_t, defined below.
772 */
773typedef struct ide_tape_obj {
774 ide_drive_t *drive;
775 ide_driver_t *driver;
776 struct gendisk *disk;
777 struct kref kref;
778
779 /*
780 * Since a typical character device operation requires more
781 * than one packet command, we provide here enough memory
782 * for the maximum of interconnected packet commands.
783 * The packet commands are stored in the circular array pc_stack.
784 * pc_stack_index points to the last used entry, and warps around
785 * to the start when we get to the last array entry.
786 *
787 * pc points to the current processed packet command.
788 *
789 * failed_pc points to the last failed packet command, or contains
790 * NULL if we do not need to retry any packet command. This is
791 * required since an additional packet command is needed before the
792 * retry, to get detailed information on what went wrong.
793 */
794 /* Current packet command */
795 idetape_pc_t *pc;
796 /* Last failed packet command */
797 idetape_pc_t *failed_pc;
798 /* Packet command stack */
799 idetape_pc_t pc_stack[IDETAPE_PC_STACK];
800 /* Next free packet command storage space */
801 int pc_stack_index;
802 struct request rq_stack[IDETAPE_PC_STACK];
803 /* We implement a circular array */
804 int rq_stack_index;
805
806 /*
807 * DSC polling variables.
808 *
809 * While polling for DSC we use postponed_rq to postpone the
810 * current request so that ide.c will be able to service
811 * pending requests on the other device. Note that at most
812 * we will have only one DSC (usually data transfer) request
813 * in the device request queue. Additional requests can be
814 * queued in our internal pipeline, but they will be visible
815 * to ide.c only one at a time.
816 */
817 struct request *postponed_rq;
818 /* The time in which we started polling for DSC */
819 unsigned long dsc_polling_start;
820 /* Timer used to poll for dsc */
821 struct timer_list dsc_timer;
822 /* Read/Write dsc polling frequency */
823 unsigned long best_dsc_rw_frequency;
824 /* The current polling frequency */
825 unsigned long dsc_polling_frequency;
826 /* Maximum waiting time */
827 unsigned long dsc_timeout;
828
829 /*
830 * Read position information
831 */
832 u8 partition;
833 /* Current block */
834 unsigned int first_frame_position;
835 unsigned int last_frame_position;
836 unsigned int blocks_in_buffer;
837
838 /*
839 * Last error information
840 */
841 u8 sense_key, asc, ascq;
842
843 /*
844 * Character device operation
845 */
846 unsigned int minor;
847 /* device name */
848 char name[4];
849 /* Current character device data transfer direction */
850 idetape_chrdev_direction_t chrdev_direction;
851
852 /*
853 * Device information
854 */
855 /* Usually 512 or 1024 bytes */
856 unsigned short tape_block_size;
857 int user_bs_factor;
858 /* Copy of the tape's Capabilities and Mechanical Page */
859 idetape_capabilities_page_t capabilities;
860
861 /*
862 * Active data transfer request parameters.
863 *
864 * At most, there is only one ide-tape originated data transfer
865 * request in the device request queue. This allows ide.c to
866 * easily service requests from the other device when we
867 * postpone our active request. In the pipelined operation
868 * mode, we use our internal pipeline structure to hold
869 * more data requests.
870 *
871 * The data buffer size is chosen based on the tape's
872 * recommendation.
873 */
874 /* Pointer to the request which is waiting in the device request queue */
875 struct request *active_data_request;
876 /* Data buffer size (chosen based on the tape's recommendation */
877 int stage_size;
878 idetape_stage_t *merge_stage;
879 int merge_stage_size;
880 struct idetape_bh *bh;
881 char *b_data;
882 int b_count;
883
884 /*
885 * Pipeline parameters.
886 *
887 * To accomplish non-pipelined mode, we simply set the following
888 * variables to zero (or NULL, where appropriate).
889 */
890 /* Number of currently used stages */
891 int nr_stages;
892 /* Number of pending stages */
893 int nr_pending_stages;
894 /* We will not allocate more than this number of stages */
895 int max_stages, min_pipeline, max_pipeline;
896 /* The first stage which will be removed from the pipeline */
897 idetape_stage_t *first_stage;
898 /* The currently active stage */
899 idetape_stage_t *active_stage;
900 /* Will be serviced after the currently active request */
901 idetape_stage_t *next_stage;
902 /* New requests will be added to the pipeline here */
903 idetape_stage_t *last_stage;
904 /* Optional free stage which we can use */
905 idetape_stage_t *cache_stage;
906 int pages_per_stage;
907 /* Wasted space in each stage */
908 int excess_bh_size;
909
910 /* Status/Action flags: long for set_bit */
911 unsigned long flags;
912 /* protects the ide-tape queue */
913 spinlock_t spinlock;
914
915 /*
916 * Measures average tape speed
917 */
918 unsigned long avg_time;
919 int avg_size;
920 int avg_speed;
921
922 /* last sense information */
923 idetape_request_sense_result_t sense;
924
925 char vendor_id[10];
926 char product_id[18];
927 char firmware_revision[6];
928 int firmware_revision_num;
929
930 /* the door is currently locked */
931 int door_locked;
932 /* the tape hardware is write protected */
933 char drv_write_prot;
934 /* the tape is write protected (hardware or opened as read-only) */
935 char write_prot;
936
937 /*
938 * Limit the number of times a request can
939 * be postponed, to avoid an infinite postpone
940 * deadlock.
941 */
942 /* request postpone count limit */
943 int postpone_cnt;
944
945 /*
946 * Measures number of frames:
947 *
948 * 1. written/read to/from the driver pipeline (pipeline_head).
949 * 2. written/read to/from the tape buffers (idetape_bh).
950 * 3. written/read by the tape to/from the media (tape_head).
951 */
952 int pipeline_head;
953 int buffer_head;
954 int tape_head;
955 int last_tape_head;
956
957 /*
958 * Speed control at the tape buffers input/output
959 */
960 unsigned long insert_time;
961 int insert_size;
962 int insert_speed;
963 int max_insert_speed;
964 int measure_insert_time;
965
966 /*
967 * Measure tape still time, in milliseconds
968 */
969 unsigned long tape_still_time_begin;
970 int tape_still_time;
971
972 /*
973 * Speed regulation negative feedback loop
974 */
975 int speed_control;
976 int pipeline_head_speed;
977 int controlled_pipeline_head_speed;
978 int uncontrolled_pipeline_head_speed;
979 int controlled_last_pipeline_head;
980 int uncontrolled_last_pipeline_head;
981 unsigned long uncontrolled_pipeline_head_time;
982 unsigned long controlled_pipeline_head_time;
983 int controlled_previous_pipeline_head;
984 int uncontrolled_previous_pipeline_head;
985 unsigned long controlled_previous_head_time;
986 unsigned long uncontrolled_previous_head_time;
987 int restart_speed_control_req;
988
989 /*
990 * Debug_level determines amount of debugging output;
991 * can be changed using /proc/ide/hdx/settings
992 * 0 : almost no debugging output
993 * 1 : 0+output errors only
994 * 2 : 1+output all sensekey/asc
995 * 3 : 2+follow all chrdev related procedures
996 * 4 : 3+follow all procedures
997 * 5 : 4+include pc_stack rq_stack info
998 * 6 : 5+USE_COUNT updates
999 */
1000 int debug_level;
1001} idetape_tape_t;
1002
Arjan van de Vencf8b8972006-03-23 03:00:45 -08001003static DEFINE_MUTEX(idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Will Dysond5dee802005-09-16 02:55:07 -07001005static struct class *idetape_sysfs_class;
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref)
1008
1009#define ide_tape_g(disk) \
1010 container_of((disk)->private_data, struct ide_tape_obj, driver)
1011
1012static struct ide_tape_obj *ide_tape_get(struct gendisk *disk)
1013{
1014 struct ide_tape_obj *tape = NULL;
1015
Arjan van de Vencf8b8972006-03-23 03:00:45 -08001016 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 tape = ide_tape_g(disk);
1018 if (tape)
1019 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -08001020 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return tape;
1022}
1023
1024static void ide_tape_release(struct kref *);
1025
1026static void ide_tape_put(struct ide_tape_obj *tape)
1027{
Arjan van de Vencf8b8972006-03-23 03:00:45 -08001028 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 kref_put(&tape->kref, ide_tape_release);
Arjan van de Vencf8b8972006-03-23 03:00:45 -08001030 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
1033/*
1034 * Tape door status
1035 */
1036#define DOOR_UNLOCKED 0
1037#define DOOR_LOCKED 1
1038#define DOOR_EXPLICITLY_LOCKED 2
1039
1040/*
1041 * Tape flag bits values.
1042 */
1043#define IDETAPE_IGNORE_DSC 0
1044#define IDETAPE_ADDRESS_VALID 1 /* 0 When the tape position is unknown */
1045#define IDETAPE_BUSY 2 /* Device already opened */
1046#define IDETAPE_PIPELINE_ERROR 3 /* Error detected in a pipeline stage */
1047#define IDETAPE_DETECT_BS 4 /* Attempt to auto-detect the current user block size */
1048#define IDETAPE_FILEMARK 5 /* Currently on a filemark */
1049#define IDETAPE_DRQ_INTERRUPT 6 /* DRQ interrupt device */
1050#define IDETAPE_READ_ERROR 7
1051#define IDETAPE_PIPELINE_ACTIVE 8 /* pipeline active */
1052/* 0 = no tape is loaded, so we don't rewind after ejecting */
1053#define IDETAPE_MEDIUM_PRESENT 9
1054
1055/*
1056 * Supported ATAPI tape drives packet commands
1057 */
1058#define IDETAPE_TEST_UNIT_READY_CMD 0x00
1059#define IDETAPE_REWIND_CMD 0x01
1060#define IDETAPE_REQUEST_SENSE_CMD 0x03
1061#define IDETAPE_READ_CMD 0x08
1062#define IDETAPE_WRITE_CMD 0x0a
1063#define IDETAPE_WRITE_FILEMARK_CMD 0x10
1064#define IDETAPE_SPACE_CMD 0x11
1065#define IDETAPE_INQUIRY_CMD 0x12
1066#define IDETAPE_ERASE_CMD 0x19
1067#define IDETAPE_MODE_SENSE_CMD 0x1a
1068#define IDETAPE_MODE_SELECT_CMD 0x15
1069#define IDETAPE_LOAD_UNLOAD_CMD 0x1b
1070#define IDETAPE_PREVENT_CMD 0x1e
1071#define IDETAPE_LOCATE_CMD 0x2b
1072#define IDETAPE_READ_POSITION_CMD 0x34
1073#define IDETAPE_READ_BUFFER_CMD 0x3c
1074#define IDETAPE_SET_SPEED_CMD 0xbb
1075
1076/*
1077 * Some defines for the READ BUFFER command
1078 */
1079#define IDETAPE_RETRIEVE_FAULTY_BLOCK 6
1080
1081/*
1082 * Some defines for the SPACE command
1083 */
1084#define IDETAPE_SPACE_OVER_FILEMARK 1
1085#define IDETAPE_SPACE_TO_EOD 3
1086
1087/*
1088 * Some defines for the LOAD UNLOAD command
1089 */
1090#define IDETAPE_LU_LOAD_MASK 1
1091#define IDETAPE_LU_RETENSION_MASK 2
1092#define IDETAPE_LU_EOT_MASK 4
1093
1094/*
1095 * Special requests for our block device strategy routine.
1096 *
1097 * In order to service a character device command, we add special
1098 * requests to the tail of our block device request queue and wait
1099 * for their completion.
1100 */
1101
1102enum {
1103 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */
1104 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */
1105 REQ_IDETAPE_READ = (1 << 2),
1106 REQ_IDETAPE_WRITE = (1 << 3),
1107 REQ_IDETAPE_READ_BUFFER = (1 << 4),
1108};
1109
1110/*
1111 * Error codes which are returned in rq->errors to the higher part
1112 * of the driver.
1113 */
1114#define IDETAPE_ERROR_GENERAL 101
1115#define IDETAPE_ERROR_FILEMARK 102
1116#define IDETAPE_ERROR_EOD 103
1117
1118/*
1119 * The following is used to format the general configuration word of
1120 * the ATAPI IDENTIFY DEVICE command.
1121 */
1122struct idetape_id_gcw {
1123 unsigned packet_size :2; /* Packet Size */
1124 unsigned reserved234 :3; /* Reserved */
1125 unsigned drq_type :2; /* Command packet DRQ type */
1126 unsigned removable :1; /* Removable media */
1127 unsigned device_type :5; /* Device type */
1128 unsigned reserved13 :1; /* Reserved */
1129 unsigned protocol :2; /* Protocol type */
1130};
1131
1132/*
1133 * INQUIRY packet command - Data Format (From Table 6-8 of QIC-157C)
1134 */
1135typedef struct {
1136 unsigned device_type :5; /* Peripheral Device Type */
1137 unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
1138 unsigned reserved1_6t0 :7; /* Reserved */
1139 unsigned rmb :1; /* Removable Medium Bit */
1140 unsigned ansi_version :3; /* ANSI Version */
1141 unsigned ecma_version :3; /* ECMA Version */
1142 unsigned iso_version :2; /* ISO Version */
1143 unsigned response_format :4; /* Response Data Format */
1144 unsigned reserved3_45 :2; /* Reserved */
1145 unsigned reserved3_6 :1; /* TrmIOP - Reserved */
1146 unsigned reserved3_7 :1; /* AENC - Reserved */
1147 __u8 additional_length; /* Additional Length (total_length-4) */
1148 __u8 rsv5, rsv6, rsv7; /* Reserved */
1149 __u8 vendor_id[8]; /* Vendor Identification */
1150 __u8 product_id[16]; /* Product Identification */
1151 __u8 revision_level[4]; /* Revision Level */
1152 __u8 vendor_specific[20]; /* Vendor Specific - Optional */
1153 __u8 reserved56t95[40]; /* Reserved - Optional */
1154 /* Additional information may be returned */
1155} idetape_inquiry_result_t;
1156
1157/*
1158 * READ POSITION packet command - Data Format (From Table 6-57)
1159 */
1160typedef struct {
1161 unsigned reserved0_10 :2; /* Reserved */
1162 unsigned bpu :1; /* Block Position Unknown */
1163 unsigned reserved0_543 :3; /* Reserved */
1164 unsigned eop :1; /* End Of Partition */
1165 unsigned bop :1; /* Beginning Of Partition */
1166 u8 partition; /* Partition Number */
1167 u8 reserved2, reserved3; /* Reserved */
1168 u32 first_block; /* First Block Location */
1169 u32 last_block; /* Last Block Location (Optional) */
1170 u8 reserved12; /* Reserved */
1171 u8 blocks_in_buffer[3]; /* Blocks In Buffer - (Optional) */
1172 u32 bytes_in_buffer; /* Bytes In Buffer (Optional) */
1173} idetape_read_position_result_t;
1174
1175/*
1176 * Follows structures which are related to the SELECT SENSE / MODE SENSE
1177 * packet commands. Those packet commands are still not supported
1178 * by ide-tape.
1179 */
1180#define IDETAPE_BLOCK_DESCRIPTOR 0
1181#define IDETAPE_CAPABILITIES_PAGE 0x2a
1182#define IDETAPE_PARAMTR_PAGE 0x2b /* Onstream DI-x0 only */
1183#define IDETAPE_BLOCK_SIZE_PAGE 0x30
1184#define IDETAPE_BUFFER_FILLING_PAGE 0x33
1185
1186/*
1187 * Mode Parameter Header for the MODE SENSE packet command
1188 */
1189typedef struct {
1190 __u8 mode_data_length; /* Length of the following data transfer */
1191 __u8 medium_type; /* Medium Type */
1192 __u8 dsp; /* Device Specific Parameter */
1193 __u8 bdl; /* Block Descriptor Length */
1194#if 0
1195 /* data transfer page */
1196 __u8 page_code :6;
1197 __u8 reserved0_6 :1;
1198 __u8 ps :1; /* parameters saveable */
1199 __u8 page_length; /* page Length == 0x02 */
1200 __u8 reserved2;
1201 __u8 read32k :1; /* 32k blk size (data only) */
1202 __u8 read32k5 :1; /* 32.5k blk size (data&AUX) */
1203 __u8 reserved3_23 :2;
1204 __u8 write32k :1; /* 32k blk size (data only) */
1205 __u8 write32k5 :1; /* 32.5k blk size (data&AUX) */
1206 __u8 reserved3_6 :1;
1207 __u8 streaming :1; /* streaming mode enable */
1208#endif
1209} idetape_mode_parameter_header_t;
1210
1211/*
1212 * Mode Parameter Block Descriptor the MODE SENSE packet command
1213 *
1214 * Support for block descriptors is optional.
1215 */
1216typedef struct {
1217 __u8 density_code; /* Medium density code */
1218 __u8 blocks[3]; /* Number of blocks */
1219 __u8 reserved4; /* Reserved */
1220 __u8 length[3]; /* Block Length */
1221} idetape_parameter_block_descriptor_t;
1222
1223/*
1224 * The Data Compression Page, as returned by the MODE SENSE packet command.
1225 */
1226typedef struct {
1227 unsigned page_code :6; /* Page Code - Should be 0xf */
1228 unsigned reserved0 :1; /* Reserved */
1229 unsigned ps :1;
1230 __u8 page_length; /* Page Length - Should be 14 */
1231 unsigned reserved2 :6; /* Reserved */
1232 unsigned dcc :1; /* Data Compression Capable */
1233 unsigned dce :1; /* Data Compression Enable */
1234 unsigned reserved3 :5; /* Reserved */
1235 unsigned red :2; /* Report Exception on Decompression */
1236 unsigned dde :1; /* Data Decompression Enable */
1237 __u32 ca; /* Compression Algorithm */
1238 __u32 da; /* Decompression Algorithm */
1239 __u8 reserved[4]; /* Reserved */
1240} idetape_data_compression_page_t;
1241
1242/*
1243 * The Medium Partition Page, as returned by the MODE SENSE packet command.
1244 */
1245typedef struct {
1246 unsigned page_code :6; /* Page Code - Should be 0x11 */
1247 unsigned reserved1_6 :1; /* Reserved */
1248 unsigned ps :1;
1249 __u8 page_length; /* Page Length - Should be 6 */
1250 __u8 map; /* Maximum Additional Partitions - Should be 0 */
1251 __u8 apd; /* Additional Partitions Defined - Should be 0 */
1252 unsigned reserved4_012 :3; /* Reserved */
1253 unsigned psum :2; /* Should be 0 */
1254 unsigned idp :1; /* Should be 0 */
1255 unsigned sdp :1; /* Should be 0 */
1256 unsigned fdp :1; /* Fixed Data Partitions */
1257 __u8 mfr; /* Medium Format Recognition */
1258 __u8 reserved[2]; /* Reserved */
1259} idetape_medium_partition_page_t;
1260
1261/*
1262 * Run time configurable parameters.
1263 */
1264typedef struct {
1265 int dsc_rw_frequency;
1266 int dsc_media_access_frequency;
1267 int nr_stages;
1268} idetape_config_t;
1269
1270/*
1271 * The variables below are used for the character device interface.
1272 * Additional state variables are defined in our ide_drive_t structure.
1273 */
1274static struct ide_tape_obj * idetape_devs[MAX_HWIFS * MAX_DRIVES];
1275
1276#define ide_tape_f(file) ((file)->private_data)
1277
1278static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
1279{
1280 struct ide_tape_obj *tape = NULL;
1281
Arjan van de Vencf8b8972006-03-23 03:00:45 -08001282 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 tape = idetape_devs[i];
1284 if (tape)
1285 kref_get(&tape->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -08001286 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 return tape;
1288}
1289
1290/*
1291 * Function declarations
1292 *
1293 */
1294static int idetape_chrdev_release (struct inode *inode, struct file *filp);
1295static void idetape_write_release (ide_drive_t *drive, unsigned int minor);
1296
1297/*
1298 * Too bad. The drive wants to send us data which we are not ready to accept.
1299 * Just throw it away.
1300 */
1301static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
1302{
1303 while (bcount--)
1304 (void) HWIF(drive)->INB(IDE_DATA_REG);
1305}
1306
1307static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
1308{
1309 struct idetape_bh *bh = pc->bh;
1310 int count;
1311
1312 while (bcount) {
1313#if IDETAPE_DEBUG_BUGS
1314 if (bh == NULL) {
1315 printk(KERN_ERR "ide-tape: bh == NULL in "
1316 "idetape_input_buffers\n");
1317 idetape_discard_data(drive, bcount);
1318 return;
1319 }
1320#endif /* IDETAPE_DEBUG_BUGS */
1321 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), bcount);
1322 HWIF(drive)->atapi_input_bytes(drive, bh->b_data + atomic_read(&bh->b_count), count);
1323 bcount -= count;
1324 atomic_add(count, &bh->b_count);
1325 if (atomic_read(&bh->b_count) == bh->b_size) {
1326 bh = bh->b_reqnext;
1327 if (bh)
1328 atomic_set(&bh->b_count, 0);
1329 }
1330 }
1331 pc->bh = bh;
1332}
1333
1334static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
1335{
1336 struct idetape_bh *bh = pc->bh;
1337 int count;
1338
1339 while (bcount) {
1340#if IDETAPE_DEBUG_BUGS
1341 if (bh == NULL) {
1342 printk(KERN_ERR "ide-tape: bh == NULL in "
1343 "idetape_output_buffers\n");
1344 return;
1345 }
1346#endif /* IDETAPE_DEBUG_BUGS */
1347 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
1348 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count);
1349 bcount -= count;
1350 pc->b_data += count;
1351 pc->b_count -= count;
1352 if (!pc->b_count) {
1353 pc->bh = bh = bh->b_reqnext;
1354 if (bh) {
1355 pc->b_data = bh->b_data;
1356 pc->b_count = atomic_read(&bh->b_count);
1357 }
1358 }
1359 }
1360}
1361
1362static void idetape_update_buffers (idetape_pc_t *pc)
1363{
1364 struct idetape_bh *bh = pc->bh;
1365 int count;
1366 unsigned int bcount = pc->actually_transferred;
1367
1368 if (test_bit(PC_WRITING, &pc->flags))
1369 return;
1370 while (bcount) {
1371#if IDETAPE_DEBUG_BUGS
1372 if (bh == NULL) {
1373 printk(KERN_ERR "ide-tape: bh == NULL in "
1374 "idetape_update_buffers\n");
1375 return;
1376 }
1377#endif /* IDETAPE_DEBUG_BUGS */
1378 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
1379 atomic_set(&bh->b_count, count);
1380 if (atomic_read(&bh->b_count) == bh->b_size)
1381 bh = bh->b_reqnext;
1382 bcount -= count;
1383 }
1384 pc->bh = bh;
1385}
1386
1387/*
1388 * idetape_next_pc_storage returns a pointer to a place in which we can
1389 * safely store a packet command, even though we intend to leave the
1390 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
1391 * commands is allocated at initialization time.
1392 */
1393static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
1394{
1395 idetape_tape_t *tape = drive->driver_data;
1396
1397#if IDETAPE_DEBUG_LOG
1398 if (tape->debug_level >= 5)
1399 printk(KERN_INFO "ide-tape: pc_stack_index=%d\n",
1400 tape->pc_stack_index);
1401#endif /* IDETAPE_DEBUG_LOG */
1402 if (tape->pc_stack_index == IDETAPE_PC_STACK)
1403 tape->pc_stack_index=0;
1404 return (&tape->pc_stack[tape->pc_stack_index++]);
1405}
1406
1407/*
1408 * idetape_next_rq_storage is used along with idetape_next_pc_storage.
1409 * Since we queue packet commands in the request queue, we need to
1410 * allocate a request, along with the allocation of a packet command.
1411 */
1412
1413/**************************************************************
1414 * *
1415 * This should get fixed to use kmalloc(.., GFP_ATOMIC) *
1416 * followed later on by kfree(). -ml *
1417 * *
1418 **************************************************************/
1419
1420static struct request *idetape_next_rq_storage (ide_drive_t *drive)
1421{
1422 idetape_tape_t *tape = drive->driver_data;
1423
1424#if IDETAPE_DEBUG_LOG
1425 if (tape->debug_level >= 5)
1426 printk(KERN_INFO "ide-tape: rq_stack_index=%d\n",
1427 tape->rq_stack_index);
1428#endif /* IDETAPE_DEBUG_LOG */
1429 if (tape->rq_stack_index == IDETAPE_PC_STACK)
1430 tape->rq_stack_index=0;
1431 return (&tape->rq_stack[tape->rq_stack_index++]);
1432}
1433
1434/*
1435 * idetape_init_pc initializes a packet command.
1436 */
1437static void idetape_init_pc (idetape_pc_t *pc)
1438{
1439 memset(pc->c, 0, 12);
1440 pc->retries = 0;
1441 pc->flags = 0;
1442 pc->request_transfer = 0;
1443 pc->buffer = pc->pc_buffer;
1444 pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
1445 pc->bh = NULL;
1446 pc->b_data = NULL;
1447}
1448
1449/*
1450 * idetape_analyze_error is called on each failed packet command retry
1451 * to analyze the request sense. We currently do not utilize this
1452 * information.
1453 */
1454static void idetape_analyze_error (ide_drive_t *drive, idetape_request_sense_result_t *result)
1455{
1456 idetape_tape_t *tape = drive->driver_data;
1457 idetape_pc_t *pc = tape->failed_pc;
1458
1459 tape->sense = *result;
1460 tape->sense_key = result->sense_key;
1461 tape->asc = result->asc;
1462 tape->ascq = result->ascq;
1463#if IDETAPE_DEBUG_LOG
1464 /*
1465 * Without debugging, we only log an error if we decided to
1466 * give up retrying.
1467 */
1468 if (tape->debug_level >= 1)
1469 printk(KERN_INFO "ide-tape: pc = %x, sense key = %x, "
1470 "asc = %x, ascq = %x\n",
1471 pc->c[0], result->sense_key,
1472 result->asc, result->ascq);
1473#endif /* IDETAPE_DEBUG_LOG */
1474
1475 /*
1476 * Correct pc->actually_transferred by asking the tape.
1477 */
1478 if (test_bit(PC_DMA_ERROR, &pc->flags)) {
1479 pc->actually_transferred = pc->request_transfer - tape->tape_block_size * ntohl(get_unaligned(&result->information));
1480 idetape_update_buffers(pc);
1481 }
1482
1483 /*
1484 * If error was the result of a zero-length read or write command,
1485 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives
1486 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
1487 */
1488 if ((pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD)
1489 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) { /* length==0 */
1490 if (result->sense_key == 5) {
1491 /* don't report an error, everything's ok */
1492 pc->error = 0;
1493 /* don't retry read/write */
1494 set_bit(PC_ABORT, &pc->flags);
1495 }
1496 }
1497 if (pc->c[0] == IDETAPE_READ_CMD && result->filemark) {
1498 pc->error = IDETAPE_ERROR_FILEMARK;
1499 set_bit(PC_ABORT, &pc->flags);
1500 }
1501 if (pc->c[0] == IDETAPE_WRITE_CMD) {
1502 if (result->eom ||
1503 (result->sense_key == 0xd && result->asc == 0x0 &&
1504 result->ascq == 0x2)) {
1505 pc->error = IDETAPE_ERROR_EOD;
1506 set_bit(PC_ABORT, &pc->flags);
1507 }
1508 }
1509 if (pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD) {
1510 if (result->sense_key == 8) {
1511 pc->error = IDETAPE_ERROR_EOD;
1512 set_bit(PC_ABORT, &pc->flags);
1513 }
1514 if (!test_bit(PC_ABORT, &pc->flags) &&
1515 pc->actually_transferred)
1516 pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
1517 }
1518}
1519
1520/*
1521 * idetape_active_next_stage will declare the next stage as "active".
1522 */
1523static void idetape_active_next_stage (ide_drive_t *drive)
1524{
1525 idetape_tape_t *tape = drive->driver_data;
1526 idetape_stage_t *stage = tape->next_stage;
1527 struct request *rq = &stage->rq;
1528
1529#if IDETAPE_DEBUG_LOG
1530 if (tape->debug_level >= 4)
1531 printk(KERN_INFO "ide-tape: Reached idetape_active_next_stage\n");
1532#endif /* IDETAPE_DEBUG_LOG */
1533#if IDETAPE_DEBUG_BUGS
1534 if (stage == NULL) {
1535 printk(KERN_ERR "ide-tape: bug: Trying to activate a non existing stage\n");
1536 return;
1537 }
1538#endif /* IDETAPE_DEBUG_BUGS */
1539
1540 rq->rq_disk = tape->disk;
1541 rq->buffer = NULL;
1542 rq->special = (void *)stage->bh;
1543 tape->active_data_request = rq;
1544 tape->active_stage = stage;
1545 tape->next_stage = stage->next;
1546}
1547
1548/*
1549 * idetape_increase_max_pipeline_stages is a part of the feedback
1550 * loop which tries to find the optimum number of stages. In the
1551 * feedback loop, we are starting from a minimum maximum number of
1552 * stages, and if we sense that the pipeline is empty, we try to
1553 * increase it, until we reach the user compile time memory limit.
1554 */
1555static void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
1556{
1557 idetape_tape_t *tape = drive->driver_data;
1558 int increase = (tape->max_pipeline - tape->min_pipeline) / 10;
1559
1560#if IDETAPE_DEBUG_LOG
1561 if (tape->debug_level >= 4)
1562 printk (KERN_INFO "ide-tape: Reached idetape_increase_max_pipeline_stages\n");
1563#endif /* IDETAPE_DEBUG_LOG */
1564
1565 tape->max_stages += max(increase, 1);
1566 tape->max_stages = max(tape->max_stages, tape->min_pipeline);
1567 tape->max_stages = min(tape->max_stages, tape->max_pipeline);
1568}
1569
1570/*
1571 * idetape_kfree_stage calls kfree to completely free a stage, along with
1572 * its related buffers.
1573 */
1574static void __idetape_kfree_stage (idetape_stage_t *stage)
1575{
1576 struct idetape_bh *prev_bh, *bh = stage->bh;
1577 int size;
1578
1579 while (bh != NULL) {
1580 if (bh->b_data != NULL) {
1581 size = (int) bh->b_size;
1582 while (size > 0) {
1583 free_page((unsigned long) bh->b_data);
1584 size -= PAGE_SIZE;
1585 bh->b_data += PAGE_SIZE;
1586 }
1587 }
1588 prev_bh = bh;
1589 bh = bh->b_reqnext;
1590 kfree(prev_bh);
1591 }
1592 kfree(stage);
1593}
1594
1595static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
1596{
1597 __idetape_kfree_stage(stage);
1598}
1599
1600/*
1601 * idetape_remove_stage_head removes tape->first_stage from the pipeline.
1602 * The caller should avoid race conditions.
1603 */
1604static void idetape_remove_stage_head (ide_drive_t *drive)
1605{
1606 idetape_tape_t *tape = drive->driver_data;
1607 idetape_stage_t *stage;
1608
1609#if IDETAPE_DEBUG_LOG
1610 if (tape->debug_level >= 4)
1611 printk(KERN_INFO "ide-tape: Reached idetape_remove_stage_head\n");
1612#endif /* IDETAPE_DEBUG_LOG */
1613#if IDETAPE_DEBUG_BUGS
1614 if (tape->first_stage == NULL) {
1615 printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n");
1616 return;
1617 }
1618 if (tape->active_stage == tape->first_stage) {
1619 printk(KERN_ERR "ide-tape: bug: Trying to free our active pipeline stage\n");
1620 return;
1621 }
1622#endif /* IDETAPE_DEBUG_BUGS */
1623 stage = tape->first_stage;
1624 tape->first_stage = stage->next;
1625 idetape_kfree_stage(tape, stage);
1626 tape->nr_stages--;
1627 if (tape->first_stage == NULL) {
1628 tape->last_stage = NULL;
1629#if IDETAPE_DEBUG_BUGS
1630 if (tape->next_stage != NULL)
1631 printk(KERN_ERR "ide-tape: bug: tape->next_stage != NULL\n");
1632 if (tape->nr_stages)
1633 printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 now\n");
1634#endif /* IDETAPE_DEBUG_BUGS */
1635 }
1636}
1637
1638/*
1639 * This will free all the pipeline stages starting from new_last_stage->next
1640 * to the end of the list, and point tape->last_stage to new_last_stage.
1641 */
1642static void idetape_abort_pipeline(ide_drive_t *drive,
1643 idetape_stage_t *new_last_stage)
1644{
1645 idetape_tape_t *tape = drive->driver_data;
1646 idetape_stage_t *stage = new_last_stage->next;
1647 idetape_stage_t *nstage;
1648
1649#if IDETAPE_DEBUG_LOG
1650 if (tape->debug_level >= 4)
1651 printk(KERN_INFO "ide-tape: %s: idetape_abort_pipeline called\n", tape->name);
1652#endif
1653 while (stage) {
1654 nstage = stage->next;
1655 idetape_kfree_stage(tape, stage);
1656 --tape->nr_stages;
1657 --tape->nr_pending_stages;
1658 stage = nstage;
1659 }
1660 if (new_last_stage)
1661 new_last_stage->next = NULL;
1662 tape->last_stage = new_last_stage;
1663 tape->next_stage = NULL;
1664}
1665
1666/*
1667 * idetape_end_request is used to finish servicing a request, and to
1668 * insert a pending pipeline request into the main device queue.
1669 */
1670static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects)
1671{
1672 struct request *rq = HWGROUP(drive)->rq;
1673 idetape_tape_t *tape = drive->driver_data;
1674 unsigned long flags;
1675 int error;
1676 int remove_stage = 0;
1677 idetape_stage_t *active_stage;
1678
1679#if IDETAPE_DEBUG_LOG
1680 if (tape->debug_level >= 4)
1681 printk(KERN_INFO "ide-tape: Reached idetape_end_request\n");
1682#endif /* IDETAPE_DEBUG_LOG */
1683
1684 switch (uptodate) {
1685 case 0: error = IDETAPE_ERROR_GENERAL; break;
1686 case 1: error = 0; break;
1687 default: error = uptodate;
1688 }
1689 rq->errors = error;
1690 if (error)
1691 tape->failed_pc = NULL;
1692
Bartlomiej Zolnierkiewicz36872212008-01-26 20:13:10 +01001693 if (!blk_special_request(rq)) {
1694 ide_end_request(drive, uptodate, nr_sects);
1695 return 0;
1696 }
1697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 spin_lock_irqsave(&tape->spinlock, flags);
1699
1700 /* The request was a pipelined data transfer request */
1701 if (tape->active_data_request == rq) {
1702 active_stage = tape->active_stage;
1703 tape->active_stage = NULL;
1704 tape->active_data_request = NULL;
1705 tape->nr_pending_stages--;
1706 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
1707 remove_stage = 1;
1708 if (error) {
1709 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
1710 if (error == IDETAPE_ERROR_EOD)
1711 idetape_abort_pipeline(drive, active_stage);
1712 }
1713 } else if (rq->cmd[0] & REQ_IDETAPE_READ) {
1714 if (error == IDETAPE_ERROR_EOD) {
1715 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
1716 idetape_abort_pipeline(drive, active_stage);
1717 }
1718 }
1719 if (tape->next_stage != NULL) {
1720 idetape_active_next_stage(drive);
1721
1722 /*
1723 * Insert the next request into the request queue.
1724 */
1725 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
1726 } else if (!error) {
1727 idetape_increase_max_pipeline_stages(drive);
1728 }
1729 }
1730 ide_end_drive_cmd(drive, 0, 0);
1731// blkdev_dequeue_request(rq);
1732// drive->rq = NULL;
1733// end_that_request_last(rq);
1734
1735 if (remove_stage)
1736 idetape_remove_stage_head(drive);
1737 if (tape->active_data_request == NULL)
1738 clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
1739 spin_unlock_irqrestore(&tape->spinlock, flags);
1740 return 0;
1741}
1742
1743static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive)
1744{
1745 idetape_tape_t *tape = drive->driver_data;
1746
1747#if IDETAPE_DEBUG_LOG
1748 if (tape->debug_level >= 4)
1749 printk(KERN_INFO "ide-tape: Reached idetape_request_sense_callback\n");
1750#endif /* IDETAPE_DEBUG_LOG */
1751 if (!tape->pc->error) {
1752 idetape_analyze_error(drive, (idetape_request_sense_result_t *) tape->pc->buffer);
1753 idetape_end_request(drive, 1, 0);
1754 } else {
1755 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - Aborting request!\n");
1756 idetape_end_request(drive, 0, 0);
1757 }
1758 return ide_stopped;
1759}
1760
1761static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
1762{
1763 idetape_init_pc(pc);
1764 pc->c[0] = IDETAPE_REQUEST_SENSE_CMD;
1765 pc->c[4] = 20;
1766 pc->request_transfer = 20;
1767 pc->callback = &idetape_request_sense_callback;
1768}
1769
1770static void idetape_init_rq(struct request *rq, u8 cmd)
1771{
1772 memset(rq, 0, sizeof(*rq));
Jens Axboe4aff5e22006-08-10 08:44:47 +02001773 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 rq->cmd[0] = cmd;
1775}
1776
1777/*
1778 * idetape_queue_pc_head generates a new packet command request in front
1779 * of the request queue, before the current request, so that it will be
1780 * processed immediately, on the next pass through the driver.
1781 *
1782 * idetape_queue_pc_head is called from the request handling part of
1783 * the driver (the "bottom" part). Safe storage for the request should
1784 * be allocated with idetape_next_pc_storage and idetape_next_rq_storage
1785 * before calling idetape_queue_pc_head.
1786 *
1787 * Memory for those requests is pre-allocated at initialization time, and
1788 * is limited to IDETAPE_PC_STACK requests. We assume that we have enough
1789 * space for the maximum possible number of inter-dependent packet commands.
1790 *
1791 * The higher level of the driver - The ioctl handler and the character
1792 * device handling functions should queue request to the lower level part
1793 * and wait for their completion using idetape_queue_pc_tail or
1794 * idetape_queue_rw_tail.
1795 */
1796static void idetape_queue_pc_head (ide_drive_t *drive, idetape_pc_t *pc,struct request *rq)
1797{
1798 struct ide_tape_obj *tape = drive->driver_data;
1799
1800 idetape_init_rq(rq, REQ_IDETAPE_PC1);
1801 rq->buffer = (char *) pc;
1802 rq->rq_disk = tape->disk;
1803 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
1804}
1805
1806/*
1807 * idetape_retry_pc is called when an error was detected during the
1808 * last packet command. We queue a request sense packet command in
1809 * the head of the request list.
1810 */
1811static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
1812{
1813 idetape_tape_t *tape = drive->driver_data;
1814 idetape_pc_t *pc;
1815 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Bartlomiej Zolnierkiewicz0e38a662008-01-25 22:17:12 +01001817 (void)drive->hwif->INB(IDE_ERROR_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 pc = idetape_next_pc_storage(drive);
1819 rq = idetape_next_rq_storage(drive);
1820 idetape_create_request_sense_cmd(pc);
1821 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
1822 idetape_queue_pc_head(drive, pc, rq);
1823 return ide_stopped;
1824}
1825
1826/*
1827 * idetape_postpone_request postpones the current request so that
1828 * ide.c will be able to service requests from another device on
1829 * the same hwgroup while we are polling for DSC.
1830 */
1831static void idetape_postpone_request (ide_drive_t *drive)
1832{
1833 idetape_tape_t *tape = drive->driver_data;
1834
1835#if IDETAPE_DEBUG_LOG
1836 if (tape->debug_level >= 4)
1837 printk(KERN_INFO "ide-tape: idetape_postpone_request\n");
1838#endif
1839 tape->postponed_rq = HWGROUP(drive)->rq;
1840 ide_stall_queue(drive, tape->dsc_polling_frequency);
1841}
1842
1843/*
1844 * idetape_pc_intr is the usual interrupt handler which will be called
1845 * during a packet command. We will transfer some of the data (as
1846 * requested by the drive) and will re-point interrupt handler to us.
1847 * When data transfer is finished, we will act according to the
1848 * algorithm described before idetape_issue_packet_command.
1849 *
1850 */
1851static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
1852{
1853 ide_hwif_t *hwif = drive->hwif;
1854 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 idetape_pc_t *pc = tape->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 unsigned int temp;
1857#if SIMULATE_ERRORS
1858 static int error_sim_count = 0;
1859#endif
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001860 u16 bcount;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001861 u8 stat, ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863#if IDETAPE_DEBUG_LOG
1864 if (tape->debug_level >= 4)
1865 printk(KERN_INFO "ide-tape: Reached idetape_pc_intr "
1866 "interrupt handler\n");
1867#endif /* IDETAPE_DEBUG_LOG */
1868
1869 /* Clear the interrupt */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001870 stat = hwif->INB(IDE_STATUS_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001873 if (hwif->ide_dma_end(drive) || (stat & ERR_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 /*
1875 * A DMA error is sometimes expected. For example,
1876 * if the tape is crossing a filemark during a
1877 * READ command, it will issue an irq and position
1878 * itself before the filemark, so that only a partial
1879 * data transfer will occur (which causes the DMA
1880 * error). In that case, we will later ask the tape
1881 * how much bytes of the original request were
1882 * actually transferred (we can't receive that
1883 * information from the DMA engine on most chipsets).
1884 */
1885
1886 /*
1887 * On the contrary, a DMA error is never expected;
1888 * it usually indicates a hardware error or abort.
1889 * If the tape crosses a filemark during a READ
1890 * command, it will issue an irq and position itself
1891 * after the filemark (not before). Only a partial
1892 * data transfer will occur, but no DMA error.
1893 * (AS, 19 Apr 2001)
1894 */
1895 set_bit(PC_DMA_ERROR, &pc->flags);
1896 } else {
1897 pc->actually_transferred = pc->request_transfer;
1898 idetape_update_buffers(pc);
1899 }
1900#if IDETAPE_DEBUG_LOG
1901 if (tape->debug_level >= 4)
1902 printk(KERN_INFO "ide-tape: DMA finished\n");
1903#endif /* IDETAPE_DEBUG_LOG */
1904 }
1905
1906 /* No more interrupts */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001907 if ((stat & DRQ_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908#if IDETAPE_DEBUG_LOG
1909 if (tape->debug_level >= 2)
1910 printk(KERN_INFO "ide-tape: Packet command completed, %d bytes transferred\n", pc->actually_transferred);
1911#endif /* IDETAPE_DEBUG_LOG */
1912 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1913
1914 local_irq_enable();
1915
1916#if SIMULATE_ERRORS
1917 if ((pc->c[0] == IDETAPE_WRITE_CMD ||
1918 pc->c[0] == IDETAPE_READ_CMD) &&
1919 (++error_sim_count % 100) == 0) {
1920 printk(KERN_INFO "ide-tape: %s: simulating error\n",
1921 tape->name);
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001922 stat |= ERR_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 }
1924#endif
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001925 if ((stat & ERR_STAT) && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD)
1926 stat &= ~ERR_STAT;
1927 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
1928 /* Error detected */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929#if IDETAPE_DEBUG_LOG
1930 if (tape->debug_level >= 1)
1931 printk(KERN_INFO "ide-tape: %s: I/O error\n",
1932 tape->name);
1933#endif /* IDETAPE_DEBUG_LOG */
1934 if (pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1935 printk(KERN_ERR "ide-tape: I/O error in request sense command\n");
1936 return ide_do_reset(drive);
1937 }
1938#if IDETAPE_DEBUG_LOG
1939 if (tape->debug_level >= 1)
1940 printk(KERN_INFO "ide-tape: [cmd %x]: check condition\n", pc->c[0]);
1941#endif
1942 /* Retry operation */
1943 return idetape_retry_pc(drive);
1944 }
1945 pc->error = 0;
1946 if (test_bit(PC_WAIT_FOR_DSC, &pc->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01001947 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 /* Media access command */
1949 tape->dsc_polling_start = jiffies;
1950 tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST;
1951 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
1952 /* Allow ide.c to handle other requests */
1953 idetape_postpone_request(drive);
1954 return ide_stopped;
1955 }
1956 if (tape->failed_pc == pc)
1957 tape->failed_pc = NULL;
1958 /* Command finished - Call the callback function */
1959 return pc->callback(drive);
1960 }
1961 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
1962 printk(KERN_ERR "ide-tape: The tape wants to issue more "
1963 "interrupts in DMA mode\n");
1964 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01001965 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 return ide_do_reset(drive);
1967 }
1968 /* Get the number of bytes to transfer on this interrupt. */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001969 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
1970 hwif->INB(IDE_BCOUNTL_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001972 ireason = hwif->INB(IDE_IREASON_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001974 if (ireason & CD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 printk(KERN_ERR "ide-tape: CoD != 0 in idetape_pc_intr\n");
1976 return ide_do_reset(drive);
1977 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001978 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 /* Hopefully, we will never get here */
1980 printk(KERN_ERR "ide-tape: We wanted to %s, ",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001981 (ireason & IO) ? "Write" : "Read");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n",
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01001983 (ireason & IO) ? "Read" : "Write");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 return ide_do_reset(drive);
1985 }
1986 if (!test_bit(PC_WRITING, &pc->flags)) {
1987 /* Reading - Check that we have enough space */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001988 temp = pc->actually_transferred + bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 if (temp > pc->request_transfer) {
1990 if (temp > pc->buffer_size) {
1991 printk(KERN_ERR "ide-tape: The tape wants to send us more data than expected - discarding data\n");
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01001992 idetape_discard_data(drive, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
1994 return ide_started;
1995 }
1996#if IDETAPE_DEBUG_LOG
1997 if (tape->debug_level >= 2)
1998 printk(KERN_NOTICE "ide-tape: The tape wants to send us more data than expected - allowing transfer\n");
1999#endif /* IDETAPE_DEBUG_LOG */
2000 }
2001 }
2002 if (test_bit(PC_WRITING, &pc->flags)) {
2003 if (pc->bh != NULL)
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01002004 idetape_output_buffers(drive, pc, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 else
2006 /* Write the current buffer */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01002007 hwif->atapi_output_bytes(drive, pc->current_position,
2008 bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 } else {
2010 if (pc->bh != NULL)
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01002011 idetape_input_buffers(drive, pc, bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 else
2013 /* Read the current buffer */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01002014 hwif->atapi_input_bytes(drive, pc->current_position,
2015 bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 }
2017 /* Update the current position */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01002018 pc->actually_transferred += bcount;
2019 pc->current_position += bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020#if IDETAPE_DEBUG_LOG
2021 if (tape->debug_level >= 2)
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01002022 printk(KERN_INFO "ide-tape: [cmd %x] transferred %d bytes "
2023 "on that interrupt\n", pc->c[0], bcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024#endif
2025 /* And set the interrupt handler again */
2026 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
2027 return ide_started;
2028}
2029
2030/*
2031 * Packet Command Interface
2032 *
2033 * The current Packet Command is available in tape->pc, and will not
2034 * change until we finish handling it. Each packet command is associated
2035 * with a callback function that will be called when the command is
2036 * finished.
2037 *
2038 * The handling will be done in three stages:
2039 *
2040 * 1. idetape_issue_packet_command will send the packet command to the
2041 * drive, and will set the interrupt handler to idetape_pc_intr.
2042 *
2043 * 2. On each interrupt, idetape_pc_intr will be called. This step
2044 * will be repeated until the device signals us that no more
2045 * interrupts will be issued.
2046 *
2047 * 3. ATAPI Tape media access commands have immediate status with a
2048 * delayed process. In case of a successful initiation of a
2049 * media access packet command, the DSC bit will be set when the
2050 * actual execution of the command is finished.
2051 * Since the tape drive will not issue an interrupt, we have to
2052 * poll for this event. In this case, we define the request as
2053 * "low priority request" by setting rq_status to
2054 * IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and exit
2055 * the driver.
2056 *
2057 * ide.c will then give higher priority to requests which
2058 * originate from the other device, until will change rq_status
2059 * to RQ_ACTIVE.
2060 *
2061 * 4. When the packet command is finished, it will be checked for errors.
2062 *
2063 * 5. In case an error was found, we queue a request sense packet
2064 * command in front of the request queue and retry the operation
2065 * up to IDETAPE_MAX_PC_RETRIES times.
2066 *
2067 * 6. In case no error was found, or we decided to give up and not
2068 * to retry again, the callback function will be called and then
2069 * we will handle the next request.
2070 *
2071 */
2072static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
2073{
2074 ide_hwif_t *hwif = drive->hwif;
2075 idetape_tape_t *tape = drive->driver_data;
2076 idetape_pc_t *pc = tape->pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 int retries = 100;
2078 ide_startstop_t startstop;
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01002079 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
2081 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
2082 printk(KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
2083 return startstop;
2084 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01002085 ireason = hwif->INB(IDE_IREASON_REG);
2086 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing "
2088 "a packet command, retrying\n");
2089 udelay(100);
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01002090 ireason = hwif->INB(IDE_IREASON_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 if (retries == 0) {
2092 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while "
2093 "issuing a packet command, ignoring\n");
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01002094 ireason |= CD;
2095 ireason &= ~IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
2097 }
Bartlomiej Zolnierkiewicz8e7657a2008-01-25 22:17:12 +01002098 if ((ireason & CD) == 0 || (ireason & IO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing "
2100 "a packet command\n");
2101 return ide_do_reset(drive);
2102 }
2103 /* Set the interrupt routine */
2104 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
2105#ifdef CONFIG_BLK_DEV_IDEDMA
2106 /* Begin DMA, if necessary */
2107 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags))
2108 hwif->dma_start(drive);
2109#endif
2110 /* Send the actual packet */
2111 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12);
2112 return ide_started;
2113}
2114
2115static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape_pc_t *pc)
2116{
2117 ide_hwif_t *hwif = drive->hwif;
2118 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 int dma_ok = 0;
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01002120 u16 bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122#if IDETAPE_DEBUG_BUGS
2123 if (tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD &&
2124 pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
2125 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
2126 "Two request sense in serial were issued\n");
2127 }
2128#endif /* IDETAPE_DEBUG_BUGS */
2129
2130 if (tape->failed_pc == NULL && pc->c[0] != IDETAPE_REQUEST_SENSE_CMD)
2131 tape->failed_pc = pc;
2132 /* Set the current packet command */
2133 tape->pc = pc;
2134
2135 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
2136 test_bit(PC_ABORT, &pc->flags)) {
2137 /*
2138 * We will "abort" retrying a packet command in case
2139 * a legitimate error code was received (crossing a
2140 * filemark, or end of the media, for example).
2141 */
2142 if (!test_bit(PC_ABORT, &pc->flags)) {
2143 if (!(pc->c[0] == IDETAPE_TEST_UNIT_READY_CMD &&
2144 tape->sense_key == 2 && tape->asc == 4 &&
2145 (tape->ascq == 1 || tape->ascq == 8))) {
2146 printk(KERN_ERR "ide-tape: %s: I/O error, "
2147 "pc = %2x, key = %2x, "
2148 "asc = %2x, ascq = %2x\n",
2149 tape->name, pc->c[0],
2150 tape->sense_key, tape->asc,
2151 tape->ascq);
2152 }
2153 /* Giving up */
2154 pc->error = IDETAPE_ERROR_GENERAL;
2155 }
2156 tape->failed_pc = NULL;
2157 return pc->callback(drive);
2158 }
2159#if IDETAPE_DEBUG_LOG
2160 if (tape->debug_level >= 2)
2161 printk(KERN_INFO "ide-tape: Retry number - %d, cmd = %02X\n", pc->retries, pc->c[0]);
2162#endif /* IDETAPE_DEBUG_LOG */
2163
2164 pc->retries++;
2165 /* We haven't transferred any data yet */
2166 pc->actually_transferred = 0;
2167 pc->current_position = pc->buffer;
2168 /* Request to transfer the entire buffer at once */
Bartlomiej Zolnierkiewicz790d1232008-01-25 22:17:12 +01002169 bcount = pc->request_transfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
2171 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) {
2172 printk(KERN_WARNING "ide-tape: DMA disabled, "
2173 "reverting to PIO\n");
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +01002174 ide_dma_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 }
2176 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
2177 dma_ok = !hwif->dma_setup(drive);
2178
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +01002179 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
2180 IDE_TFLAG_OUT_DEVICE, bcount, dma_ok);
2181
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 if (dma_ok) /* Will begin DMA later */
2183 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
2184 if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
2185 ide_set_handler(drive, &idetape_transfer_pc, IDETAPE_WAIT_CMD, NULL);
2186 hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
2187 return ide_started;
2188 } else {
2189 hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
2190 return idetape_transfer_pc(drive);
2191 }
2192}
2193
2194/*
2195 * General packet command callback function.
2196 */
2197static ide_startstop_t idetape_pc_callback (ide_drive_t *drive)
2198{
2199 idetape_tape_t *tape = drive->driver_data;
2200
2201#if IDETAPE_DEBUG_LOG
2202 if (tape->debug_level >= 4)
2203 printk(KERN_INFO "ide-tape: Reached idetape_pc_callback\n");
2204#endif /* IDETAPE_DEBUG_LOG */
2205
2206 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0);
2207 return ide_stopped;
2208}
2209
2210/*
2211 * A mode sense command is used to "sense" tape parameters.
2212 */
2213static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, u8 page_code)
2214{
2215 idetape_init_pc(pc);
2216 pc->c[0] = IDETAPE_MODE_SENSE_CMD;
2217 if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
2218 pc->c[1] = 8; /* DBD = 1 - Don't return block descriptors */
2219 pc->c[2] = page_code;
2220 /*
2221 * Changed pc->c[3] to 0 (255 will at best return unused info).
2222 *
2223 * For SCSI this byte is defined as subpage instead of high byte
2224 * of length and some IDE drives seem to interpret it this way
2225 * and return an error when 255 is used.
2226 */
2227 pc->c[3] = 0;
2228 pc->c[4] = 255; /* (We will just discard data in that case) */
2229 if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
2230 pc->request_transfer = 12;
2231 else if (page_code == IDETAPE_CAPABILITIES_PAGE)
2232 pc->request_transfer = 24;
2233 else
2234 pc->request_transfer = 50;
2235 pc->callback = &idetape_pc_callback;
2236}
2237
2238static void calculate_speeds(ide_drive_t *drive)
2239{
2240 idetape_tape_t *tape = drive->driver_data;
2241 int full = 125, empty = 75;
2242
2243 if (time_after(jiffies, tape->controlled_pipeline_head_time + 120 * HZ)) {
2244 tape->controlled_previous_pipeline_head = tape->controlled_last_pipeline_head;
2245 tape->controlled_previous_head_time = tape->controlled_pipeline_head_time;
2246 tape->controlled_last_pipeline_head = tape->pipeline_head;
2247 tape->controlled_pipeline_head_time = jiffies;
2248 }
2249 if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
2250 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_last_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_pipeline_head_time);
2251 else if (time_after(jiffies, tape->controlled_previous_head_time))
2252 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_previous_head_time);
2253
2254 if (tape->nr_pending_stages < tape->max_stages /*- 1 */) {
2255 /* -1 for read mode error recovery */
2256 if (time_after(jiffies, tape->uncontrolled_previous_head_time + 10 * HZ)) {
2257 tape->uncontrolled_pipeline_head_time = jiffies;
2258 tape->uncontrolled_pipeline_head_speed = (tape->pipeline_head - tape->uncontrolled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->uncontrolled_previous_head_time);
2259 }
2260 } else {
2261 tape->uncontrolled_previous_head_time = jiffies;
2262 tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
2263 if (time_after(jiffies, tape->uncontrolled_pipeline_head_time + 30 * HZ)) {
2264 tape->uncontrolled_pipeline_head_time = jiffies;
2265 }
2266 }
2267 tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed, tape->controlled_pipeline_head_speed);
2268 if (tape->speed_control == 0) {
2269 tape->max_insert_speed = 5000;
2270 } else if (tape->speed_control == 1) {
2271 if (tape->nr_pending_stages >= tape->max_stages / 2)
2272 tape->max_insert_speed = tape->pipeline_head_speed +
2273 (1100 - tape->pipeline_head_speed) * 2 * (tape->nr_pending_stages - tape->max_stages / 2) / tape->max_stages;
2274 else
2275 tape->max_insert_speed = 500 +
2276 (tape->pipeline_head_speed - 500) * 2 * tape->nr_pending_stages / tape->max_stages;
2277 if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
2278 tape->max_insert_speed = 5000;
2279 } else if (tape->speed_control == 2) {
2280 tape->max_insert_speed = tape->pipeline_head_speed * empty / 100 +
2281 (tape->pipeline_head_speed * full / 100 - tape->pipeline_head_speed * empty / 100) * tape->nr_pending_stages / tape->max_stages;
2282 } else
2283 tape->max_insert_speed = tape->speed_control;
2284 tape->max_insert_speed = max(tape->max_insert_speed, 500);
2285}
2286
2287static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
2288{
2289 idetape_tape_t *tape = drive->driver_data;
2290 idetape_pc_t *pc = tape->pc;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01002291 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01002293 stat = drive->hwif->INB(IDE_STATUS_REG);
2294 if (stat & SEEK_STAT) {
2295 if (stat & ERR_STAT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 /* Error detected */
2297 if (pc->c[0] != IDETAPE_TEST_UNIT_READY_CMD)
2298 printk(KERN_ERR "ide-tape: %s: I/O error, ",
2299 tape->name);
2300 /* Retry operation */
2301 return idetape_retry_pc(drive);
2302 }
2303 pc->error = 0;
2304 if (tape->failed_pc == pc)
2305 tape->failed_pc = NULL;
2306 } else {
2307 pc->error = IDETAPE_ERROR_GENERAL;
2308 tape->failed_pc = NULL;
2309 }
2310 return pc->callback(drive);
2311}
2312
2313static ide_startstop_t idetape_rw_callback (ide_drive_t *drive)
2314{
2315 idetape_tape_t *tape = drive->driver_data;
2316 struct request *rq = HWGROUP(drive)->rq;
2317 int blocks = tape->pc->actually_transferred / tape->tape_block_size;
2318
2319 tape->avg_size += blocks * tape->tape_block_size;
2320 tape->insert_size += blocks * tape->tape_block_size;
2321 if (tape->insert_size > 1024 * 1024)
2322 tape->measure_insert_time = 1;
2323 if (tape->measure_insert_time) {
2324 tape->measure_insert_time = 0;
2325 tape->insert_time = jiffies;
2326 tape->insert_size = 0;
2327 }
2328 if (time_after(jiffies, tape->insert_time))
2329 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08002330 if (time_after_eq(jiffies, tape->avg_time + HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 tape->avg_speed = tape->avg_size * HZ / (jiffies - tape->avg_time) / 1024;
2332 tape->avg_size = 0;
2333 tape->avg_time = jiffies;
2334 }
2335
2336#if IDETAPE_DEBUG_LOG
2337 if (tape->debug_level >= 4)
2338 printk(KERN_INFO "ide-tape: Reached idetape_rw_callback\n");
2339#endif /* IDETAPE_DEBUG_LOG */
2340
2341 tape->first_frame_position += blocks;
2342 rq->current_nr_sectors -= blocks;
2343
2344 if (!tape->pc->error)
2345 idetape_end_request(drive, 1, 0);
2346 else
2347 idetape_end_request(drive, tape->pc->error, 0);
2348 return ide_stopped;
2349}
2350
2351static void idetape_create_read_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
2352{
2353 idetape_init_pc(pc);
2354 pc->c[0] = IDETAPE_READ_CMD;
2355 put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
2356 pc->c[1] = 1;
2357 pc->callback = &idetape_rw_callback;
2358 pc->bh = bh;
2359 atomic_set(&bh->b_count, 0);
2360 pc->buffer = NULL;
2361 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
2362 if (pc->request_transfer == tape->stage_size)
2363 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
2364}
2365
2366static void idetape_create_read_buffer_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
2367{
2368 int size = 32768;
2369 struct idetape_bh *p = bh;
2370
2371 idetape_init_pc(pc);
2372 pc->c[0] = IDETAPE_READ_BUFFER_CMD;
2373 pc->c[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK;
2374 pc->c[7] = size >> 8;
2375 pc->c[8] = size & 0xff;
2376 pc->callback = &idetape_pc_callback;
2377 pc->bh = bh;
2378 atomic_set(&bh->b_count, 0);
2379 pc->buffer = NULL;
2380 while (p) {
2381 atomic_set(&p->b_count, 0);
2382 p = p->b_reqnext;
2383 }
2384 pc->request_transfer = pc->buffer_size = size;
2385}
2386
2387static void idetape_create_write_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh)
2388{
2389 idetape_init_pc(pc);
2390 pc->c[0] = IDETAPE_WRITE_CMD;
2391 put_unaligned(htonl(length), (unsigned int *) &pc->c[1]);
2392 pc->c[1] = 1;
2393 pc->callback = &idetape_rw_callback;
2394 set_bit(PC_WRITING, &pc->flags);
2395 pc->bh = bh;
2396 pc->b_data = bh->b_data;
2397 pc->b_count = atomic_read(&bh->b_count);
2398 pc->buffer = NULL;
2399 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
2400 if (pc->request_transfer == tape->stage_size)
2401 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
2402}
2403
2404/*
2405 * idetape_do_request is our request handling function.
2406 */
2407static ide_startstop_t idetape_do_request(ide_drive_t *drive,
2408 struct request *rq, sector_t block)
2409{
2410 idetape_tape_t *tape = drive->driver_data;
2411 idetape_pc_t *pc = NULL;
2412 struct request *postponed_rq = tape->postponed_rq;
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01002413 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
2415#if IDETAPE_DEBUG_LOG
2416#if 0
2417 if (tape->debug_level >= 5)
Jens Axboecdd60262006-07-28 09:32:07 +02002418 printk(KERN_INFO "ide-tape: %d, "
2419 "dev: %s, cmd: %ld, errors: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 rq->rq_disk->disk_name, rq->cmd[0], rq->errors);
2421#endif
2422 if (tape->debug_level >= 2)
2423 printk(KERN_INFO "ide-tape: sector: %ld, "
2424 "nr_sectors: %ld, current_nr_sectors: %d\n",
2425 rq->sector, rq->nr_sectors, rq->current_nr_sectors);
2426#endif /* IDETAPE_DEBUG_LOG */
2427
Jens Axboe4aff5e22006-08-10 08:44:47 +02002428 if (!blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 /*
2430 * We do not support buffer cache originated requests.
2431 */
2432 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
Jens Axboe4aff5e22006-08-10 08:44:47 +02002433 "request queue (%d)\n", drive->name, rq->cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 ide_end_request(drive, 0, 0);
2435 return ide_stopped;
2436 }
2437
2438 /*
2439 * Retry a failed packet command
2440 */
2441 if (tape->failed_pc != NULL &&
2442 tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
2443 return idetape_issue_packet_command(drive, tape->failed_pc);
2444 }
2445#if IDETAPE_DEBUG_BUGS
2446 if (postponed_rq != NULL)
2447 if (rq != postponed_rq) {
2448 printk(KERN_ERR "ide-tape: ide-tape.c bug - "
2449 "Two DSC requests were queued\n");
2450 idetape_end_request(drive, 0, 0);
2451 return ide_stopped;
2452 }
2453#endif /* IDETAPE_DEBUG_BUGS */
2454
2455 tape->postponed_rq = NULL;
2456
2457 /*
2458 * If the tape is still busy, postpone our request and service
2459 * the other device meanwhile.
2460 */
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01002461 stat = drive->hwif->INB(IDE_STATUS_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
2463 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2))
2464 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
2465
2466 if (drive->post_reset == 1) {
2467 set_bit(IDETAPE_IGNORE_DSC, &tape->flags);
2468 drive->post_reset = 0;
2469 }
2470
2471 if (tape->tape_still_time > 100 && tape->tape_still_time < 200)
2472 tape->measure_insert_time = 1;
2473 if (time_after(jiffies, tape->insert_time))
2474 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
2475 calculate_speeds(drive);
2476 if (!test_and_clear_bit(IDETAPE_IGNORE_DSC, &tape->flags) &&
Bartlomiej Zolnierkiewicz22c525b2008-01-25 22:17:11 +01002477 (stat & SEEK_STAT) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 if (postponed_rq == NULL) {
2479 tape->dsc_polling_start = jiffies;
2480 tape->dsc_polling_frequency = tape->best_dsc_rw_frequency;
2481 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
2482 } else if (time_after(jiffies, tape->dsc_timeout)) {
2483 printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
2484 tape->name);
2485 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
2486 idetape_media_access_finished(drive);
2487 return ide_stopped;
2488 } else {
2489 return ide_do_reset(drive);
2490 }
Marcelo Feitoza Parisi9bae1ff2006-03-28 01:56:46 -08002491 } else if (time_after(jiffies, tape->dsc_polling_start + IDETAPE_DSC_MA_THRESHOLD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 tape->dsc_polling_frequency = IDETAPE_DSC_MA_SLOW;
2493 idetape_postpone_request(drive);
2494 return ide_stopped;
2495 }
2496 if (rq->cmd[0] & REQ_IDETAPE_READ) {
2497 tape->buffer_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 tape->postpone_cnt = 0;
2499 pc = idetape_next_pc_storage(drive);
2500 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
2501 goto out;
2502 }
2503 if (rq->cmd[0] & REQ_IDETAPE_WRITE) {
2504 tape->buffer_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 tape->postpone_cnt = 0;
2506 pc = idetape_next_pc_storage(drive);
2507 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
2508 goto out;
2509 }
2510 if (rq->cmd[0] & REQ_IDETAPE_READ_BUFFER) {
2511 tape->postpone_cnt = 0;
2512 pc = idetape_next_pc_storage(drive);
2513 idetape_create_read_buffer_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special);
2514 goto out;
2515 }
2516 if (rq->cmd[0] & REQ_IDETAPE_PC1) {
2517 pc = (idetape_pc_t *) rq->buffer;
2518 rq->cmd[0] &= ~(REQ_IDETAPE_PC1);
2519 rq->cmd[0] |= REQ_IDETAPE_PC2;
2520 goto out;
2521 }
2522 if (rq->cmd[0] & REQ_IDETAPE_PC2) {
2523 idetape_media_access_finished(drive);
2524 return ide_stopped;
2525 }
2526 BUG();
2527out:
2528 return idetape_issue_packet_command(drive, pc);
2529}
2530
2531/*
2532 * Pipeline related functions
2533 */
2534static inline int idetape_pipeline_active (idetape_tape_t *tape)
2535{
2536 int rc1, rc2;
2537
2538 rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
2539 rc2 = (tape->active_data_request != NULL);
2540 return rc1;
2541}
2542
2543/*
2544 * idetape_kmalloc_stage uses __get_free_page to allocate a pipeline
2545 * stage, along with all the necessary small buffers which together make
2546 * a buffer of size tape->stage_size (or a bit more). We attempt to
2547 * combine sequential pages as much as possible.
2548 *
2549 * Returns a pointer to the new allocated stage, or NULL if we
2550 * can't (or don't want to) allocate a stage.
2551 *
2552 * Pipeline stages are optional and are used to increase performance.
2553 * If we can't allocate them, we'll manage without them.
2554 */
2555static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape, int full, int clear)
2556{
2557 idetape_stage_t *stage;
2558 struct idetape_bh *prev_bh, *bh;
2559 int pages = tape->pages_per_stage;
2560 char *b_data = NULL;
2561
Robert P. J. Day5cbded52006-12-13 00:35:56 -08002562 if ((stage = kmalloc(sizeof (idetape_stage_t),GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 return NULL;
2564 stage->next = NULL;
2565
Robert P. J. Day5cbded52006-12-13 00:35:56 -08002566 bh = stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 if (bh == NULL)
2568 goto abort;
2569 bh->b_reqnext = NULL;
2570 if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
2571 goto abort;
2572 if (clear)
2573 memset(bh->b_data, 0, PAGE_SIZE);
2574 bh->b_size = PAGE_SIZE;
2575 atomic_set(&bh->b_count, full ? bh->b_size : 0);
2576
2577 while (--pages) {
2578 if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
2579 goto abort;
2580 if (clear)
2581 memset(b_data, 0, PAGE_SIZE);
2582 if (bh->b_data == b_data + PAGE_SIZE) {
2583 bh->b_size += PAGE_SIZE;
2584 bh->b_data -= PAGE_SIZE;
2585 if (full)
2586 atomic_add(PAGE_SIZE, &bh->b_count);
2587 continue;
2588 }
2589 if (b_data == bh->b_data + bh->b_size) {
2590 bh->b_size += PAGE_SIZE;
2591 if (full)
2592 atomic_add(PAGE_SIZE, &bh->b_count);
2593 continue;
2594 }
2595 prev_bh = bh;
Robert P. J. Day5cbded52006-12-13 00:35:56 -08002596 if ((bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 free_page((unsigned long) b_data);
2598 goto abort;
2599 }
2600 bh->b_reqnext = NULL;
2601 bh->b_data = b_data;
2602 bh->b_size = PAGE_SIZE;
2603 atomic_set(&bh->b_count, full ? bh->b_size : 0);
2604 prev_bh->b_reqnext = bh;
2605 }
2606 bh->b_size -= tape->excess_bh_size;
2607 if (full)
2608 atomic_sub(tape->excess_bh_size, &bh->b_count);
2609 return stage;
2610abort:
2611 __idetape_kfree_stage(stage);
2612 return NULL;
2613}
2614
2615static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
2616{
2617 idetape_stage_t *cache_stage = tape->cache_stage;
2618
2619#if IDETAPE_DEBUG_LOG
2620 if (tape->debug_level >= 4)
2621 printk(KERN_INFO "ide-tape: Reached idetape_kmalloc_stage\n");
2622#endif /* IDETAPE_DEBUG_LOG */
2623
2624 if (tape->nr_stages >= tape->max_stages)
2625 return NULL;
2626 if (cache_stage != NULL) {
2627 tape->cache_stage = NULL;
2628 return cache_stage;
2629 }
2630 return __idetape_kmalloc_stage(tape, 0, 0);
2631}
2632
Daniel Walkerdcd96372006-06-25 05:47:37 -07002633static int idetape_copy_stage_from_user (idetape_tape_t *tape, idetape_stage_t *stage, const char __user *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634{
2635 struct idetape_bh *bh = tape->bh;
2636 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002637 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
2639 while (n) {
2640#if IDETAPE_DEBUG_BUGS
2641 if (bh == NULL) {
2642 printk(KERN_ERR "ide-tape: bh == NULL in "
2643 "idetape_copy_stage_from_user\n");
Daniel Walkerdcd96372006-06-25 05:47:37 -07002644 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 }
2646#endif /* IDETAPE_DEBUG_BUGS */
2647 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), (unsigned int)n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07002648 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, count))
2649 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 n -= count;
2651 atomic_add(count, &bh->b_count);
2652 buf += count;
2653 if (atomic_read(&bh->b_count) == bh->b_size) {
2654 bh = bh->b_reqnext;
2655 if (bh)
2656 atomic_set(&bh->b_count, 0);
2657 }
2658 }
2659 tape->bh = bh;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002660 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661}
2662
Daniel Walkerdcd96372006-06-25 05:47:37 -07002663static int idetape_copy_stage_to_user (idetape_tape_t *tape, char __user *buf, idetape_stage_t *stage, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664{
2665 struct idetape_bh *bh = tape->bh;
2666 int count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07002667 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
2669 while (n) {
2670#if IDETAPE_DEBUG_BUGS
2671 if (bh == NULL) {
2672 printk(KERN_ERR "ide-tape: bh == NULL in "
2673 "idetape_copy_stage_to_user\n");
Daniel Walkerdcd96372006-06-25 05:47:37 -07002674 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 }
2676#endif /* IDETAPE_DEBUG_BUGS */
2677 count = min(tape->b_count, n);
Daniel Walkerdcd96372006-06-25 05:47:37 -07002678 if (copy_to_user(buf, tape->b_data, count))
2679 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 n -= count;
2681 tape->b_data += count;
2682 tape->b_count -= count;
2683 buf += count;
2684 if (!tape->b_count) {
2685 tape->bh = bh = bh->b_reqnext;
2686 if (bh) {
2687 tape->b_data = bh->b_data;
2688 tape->b_count = atomic_read(&bh->b_count);
2689 }
2690 }
2691 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07002692 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693}
2694
2695static void idetape_init_merge_stage (idetape_tape_t *tape)
2696{
2697 struct idetape_bh *bh = tape->merge_stage->bh;
2698
2699 tape->bh = bh;
2700 if (tape->chrdev_direction == idetape_direction_write)
2701 atomic_set(&bh->b_count, 0);
2702 else {
2703 tape->b_data = bh->b_data;
2704 tape->b_count = atomic_read(&bh->b_count);
2705 }
2706}
2707
2708static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
2709{
2710 struct idetape_bh *tmp;
2711
2712 tmp = stage->bh;
2713 stage->bh = tape->merge_stage->bh;
2714 tape->merge_stage->bh = tmp;
2715 idetape_init_merge_stage(tape);
2716}
2717
2718/*
2719 * idetape_add_stage_tail adds a new stage at the end of the pipeline.
2720 */
2721static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
2722{
2723 idetape_tape_t *tape = drive->driver_data;
2724 unsigned long flags;
2725
2726#if IDETAPE_DEBUG_LOG
2727 if (tape->debug_level >= 4)
2728 printk (KERN_INFO "ide-tape: Reached idetape_add_stage_tail\n");
2729#endif /* IDETAPE_DEBUG_LOG */
2730 spin_lock_irqsave(&tape->spinlock, flags);
2731 stage->next = NULL;
2732 if (tape->last_stage != NULL)
2733 tape->last_stage->next=stage;
2734 else
2735 tape->first_stage = tape->next_stage=stage;
2736 tape->last_stage = stage;
2737 if (tape->next_stage == NULL)
2738 tape->next_stage = tape->last_stage;
2739 tape->nr_stages++;
2740 tape->nr_pending_stages++;
2741 spin_unlock_irqrestore(&tape->spinlock, flags);
2742}
2743
2744/*
2745 * idetape_wait_for_request installs a completion in a pending request
2746 * and sleeps until it is serviced.
2747 *
2748 * The caller should ensure that the request will not be serviced
2749 * before we install the completion (usually by disabling interrupts).
2750 */
2751static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
2752{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07002753 DECLARE_COMPLETION_ONSTACK(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 idetape_tape_t *tape = drive->driver_data;
2755
2756#if IDETAPE_DEBUG_BUGS
Jens Axboe4aff5e22006-08-10 08:44:47 +02002757 if (rq == NULL || !blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid request\n");
2759 return;
2760 }
2761#endif /* IDETAPE_DEBUG_BUGS */
Jens Axboec00895a2006-09-30 20:29:12 +02002762 rq->end_io_data = &wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 rq->end_io = blk_end_sync_rq;
2764 spin_unlock_irq(&tape->spinlock);
2765 wait_for_completion(&wait);
2766 /* The stage and its struct request have been deallocated */
2767 spin_lock_irq(&tape->spinlock);
2768}
2769
2770static ide_startstop_t idetape_read_position_callback (ide_drive_t *drive)
2771{
2772 idetape_tape_t *tape = drive->driver_data;
2773 idetape_read_position_result_t *result;
2774
2775#if IDETAPE_DEBUG_LOG
2776 if (tape->debug_level >= 4)
2777 printk(KERN_INFO "ide-tape: Reached idetape_read_position_callback\n");
2778#endif /* IDETAPE_DEBUG_LOG */
2779
2780 if (!tape->pc->error) {
2781 result = (idetape_read_position_result_t *) tape->pc->buffer;
2782#if IDETAPE_DEBUG_LOG
2783 if (tape->debug_level >= 2)
2784 printk(KERN_INFO "ide-tape: BOP - %s\n",result->bop ? "Yes":"No");
2785 if (tape->debug_level >= 2)
2786 printk(KERN_INFO "ide-tape: EOP - %s\n",result->eop ? "Yes":"No");
2787#endif /* IDETAPE_DEBUG_LOG */
2788 if (result->bpu) {
2789 printk(KERN_INFO "ide-tape: Block location is unknown to the tape\n");
2790 clear_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
2791 idetape_end_request(drive, 0, 0);
2792 } else {
2793#if IDETAPE_DEBUG_LOG
2794 if (tape->debug_level >= 2)
2795 printk(KERN_INFO "ide-tape: Block Location - %u\n", ntohl(result->first_block));
2796#endif /* IDETAPE_DEBUG_LOG */
2797 tape->partition = result->partition;
2798 tape->first_frame_position = ntohl(result->first_block);
2799 tape->last_frame_position = ntohl(result->last_block);
2800 tape->blocks_in_buffer = result->blocks_in_buffer[2];
2801 set_bit(IDETAPE_ADDRESS_VALID, &tape->flags);
2802 idetape_end_request(drive, 1, 0);
2803 }
2804 } else {
2805 idetape_end_request(drive, 0, 0);
2806 }
2807 return ide_stopped;
2808}
2809
2810/*
2811 * idetape_create_write_filemark_cmd will:
2812 *
2813 * 1. Write a filemark if write_filemark=1.
2814 * 2. Flush the device buffers without writing a filemark
2815 * if write_filemark=0.
2816 *
2817 */
2818static void idetape_create_write_filemark_cmd (ide_drive_t *drive, idetape_pc_t *pc,int write_filemark)
2819{
2820 idetape_init_pc(pc);
2821 pc->c[0] = IDETAPE_WRITE_FILEMARK_CMD;
2822 pc->c[4] = write_filemark;
2823 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2824 pc->callback = &idetape_pc_callback;
2825}
2826
2827static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
2828{
2829 idetape_init_pc(pc);
2830 pc->c[0] = IDETAPE_TEST_UNIT_READY_CMD;
2831 pc->callback = &idetape_pc_callback;
2832}
2833
2834/*
2835 * idetape_queue_pc_tail is based on the following functions:
2836 *
2837 * ide_do_drive_cmd from ide.c
2838 * cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
2839 *
2840 * We add a special packet command request to the tail of the request
2841 * queue, and wait for it to be serviced.
2842 *
2843 * This is not to be called from within the request handling part
2844 * of the driver ! We allocate here data in the stack, and it is valid
2845 * until the request is finished. This is not the case for the bottom
2846 * part of the driver, where we are always leaving the functions to wait
2847 * for an interrupt or a timer event.
2848 *
2849 * From the bottom part of the driver, we should allocate safe memory
2850 * using idetape_next_pc_storage and idetape_next_rq_storage, and add
2851 * the request to the request list without waiting for it to be serviced !
2852 * In that case, we usually use idetape_queue_pc_head.
2853 */
2854static int __idetape_queue_pc_tail (ide_drive_t *drive, idetape_pc_t *pc)
2855{
2856 struct ide_tape_obj *tape = drive->driver_data;
2857 struct request rq;
2858
2859 idetape_init_rq(&rq, REQ_IDETAPE_PC1);
2860 rq.buffer = (char *) pc;
2861 rq.rq_disk = tape->disk;
2862 return ide_do_drive_cmd(drive, &rq, ide_wait);
2863}
2864
2865static void idetape_create_load_unload_cmd (ide_drive_t *drive, idetape_pc_t *pc,int cmd)
2866{
2867 idetape_init_pc(pc);
2868 pc->c[0] = IDETAPE_LOAD_UNLOAD_CMD;
2869 pc->c[4] = cmd;
2870 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2871 pc->callback = &idetape_pc_callback;
2872}
2873
2874static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
2875{
2876 idetape_tape_t *tape = drive->driver_data;
2877 idetape_pc_t pc;
2878 int load_attempted = 0;
2879
2880 /*
2881 * Wait for the tape to become ready
2882 */
2883 set_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
2884 timeout += jiffies;
2885 while (time_before(jiffies, timeout)) {
2886 idetape_create_test_unit_ready_cmd(&pc);
2887 if (!__idetape_queue_pc_tail(drive, &pc))
2888 return 0;
2889 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
2890 || (tape->asc == 0x3A)) { /* no media */
2891 if (load_attempted)
2892 return -ENOMEDIUM;
2893 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
2894 __idetape_queue_pc_tail(drive, &pc);
2895 load_attempted = 1;
2896 /* not about to be ready */
2897 } else if (!(tape->sense_key == 2 && tape->asc == 4 &&
2898 (tape->ascq == 1 || tape->ascq == 8)))
2899 return -EIO;
Nishanth Aravamudan80ce45f2005-09-10 00:27:08 -07002900 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 }
2902 return -EIO;
2903}
2904
2905static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc)
2906{
2907 return __idetape_queue_pc_tail(drive, pc);
2908}
2909
2910static int idetape_flush_tape_buffers (ide_drive_t *drive)
2911{
2912 idetape_pc_t pc;
2913 int rc;
2914
2915 idetape_create_write_filemark_cmd(drive, &pc, 0);
2916 if ((rc = idetape_queue_pc_tail(drive, &pc)))
2917 return rc;
2918 idetape_wait_ready(drive, 60 * 5 * HZ);
2919 return 0;
2920}
2921
2922static void idetape_create_read_position_cmd (idetape_pc_t *pc)
2923{
2924 idetape_init_pc(pc);
2925 pc->c[0] = IDETAPE_READ_POSITION_CMD;
2926 pc->request_transfer = 20;
2927 pc->callback = &idetape_read_position_callback;
2928}
2929
2930static int idetape_read_position (ide_drive_t *drive)
2931{
2932 idetape_tape_t *tape = drive->driver_data;
2933 idetape_pc_t pc;
2934 int position;
2935
2936#if IDETAPE_DEBUG_LOG
2937 if (tape->debug_level >= 4)
2938 printk(KERN_INFO "ide-tape: Reached idetape_read_position\n");
2939#endif /* IDETAPE_DEBUG_LOG */
2940
2941 idetape_create_read_position_cmd(&pc);
2942 if (idetape_queue_pc_tail(drive, &pc))
2943 return -1;
2944 position = tape->first_frame_position;
2945 return position;
2946}
2947
2948static void idetape_create_locate_cmd (ide_drive_t *drive, idetape_pc_t *pc, unsigned int block, u8 partition, int skip)
2949{
2950 idetape_init_pc(pc);
2951 pc->c[0] = IDETAPE_LOCATE_CMD;
2952 pc->c[1] = 2;
2953 put_unaligned(htonl(block), (unsigned int *) &pc->c[3]);
2954 pc->c[8] = partition;
2955 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
2956 pc->callback = &idetape_pc_callback;
2957}
2958
2959static int idetape_create_prevent_cmd (ide_drive_t *drive, idetape_pc_t *pc, int prevent)
2960{
2961 idetape_tape_t *tape = drive->driver_data;
2962
2963 if (!tape->capabilities.lock)
2964 return 0;
2965
2966 idetape_init_pc(pc);
2967 pc->c[0] = IDETAPE_PREVENT_CMD;
2968 pc->c[4] = prevent;
2969 pc->callback = &idetape_pc_callback;
2970 return 1;
2971}
2972
2973static int __idetape_discard_read_pipeline (ide_drive_t *drive)
2974{
2975 idetape_tape_t *tape = drive->driver_data;
2976 unsigned long flags;
2977 int cnt;
2978
2979 if (tape->chrdev_direction != idetape_direction_read)
2980 return 0;
2981
2982 /* Remove merge stage. */
2983 cnt = tape->merge_stage_size / tape->tape_block_size;
2984 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
2985 ++cnt; /* Filemarks count as 1 sector */
2986 tape->merge_stage_size = 0;
2987 if (tape->merge_stage != NULL) {
2988 __idetape_kfree_stage(tape->merge_stage);
2989 tape->merge_stage = NULL;
2990 }
2991
2992 /* Clear pipeline flags. */
2993 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
2994 tape->chrdev_direction = idetape_direction_none;
2995
2996 /* Remove pipeline stages. */
2997 if (tape->first_stage == NULL)
2998 return 0;
2999
3000 spin_lock_irqsave(&tape->spinlock, flags);
3001 tape->next_stage = NULL;
3002 if (idetape_pipeline_active(tape))
3003 idetape_wait_for_request(drive, tape->active_data_request);
3004 spin_unlock_irqrestore(&tape->spinlock, flags);
3005
3006 while (tape->first_stage != NULL) {
3007 struct request *rq_ptr = &tape->first_stage->rq;
3008
3009 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors;
3010 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
3011 ++cnt;
3012 idetape_remove_stage_head(drive);
3013 }
3014 tape->nr_pending_stages = 0;
3015 tape->max_stages = tape->min_pipeline;
3016 return cnt;
3017}
3018
3019/*
3020 * idetape_position_tape positions the tape to the requested block
3021 * using the LOCATE packet command. A READ POSITION command is then
3022 * issued to check where we are positioned.
3023 *
3024 * Like all higher level operations, we queue the commands at the tail
3025 * of the request queue and wait for their completion.
3026 *
3027 */
3028static int idetape_position_tape (ide_drive_t *drive, unsigned int block, u8 partition, int skip)
3029{
3030 idetape_tape_t *tape = drive->driver_data;
3031 int retval;
3032 idetape_pc_t pc;
3033
3034 if (tape->chrdev_direction == idetape_direction_read)
3035 __idetape_discard_read_pipeline(drive);
3036 idetape_wait_ready(drive, 60 * 5 * HZ);
3037 idetape_create_locate_cmd(drive, &pc, block, partition, skip);
3038 retval = idetape_queue_pc_tail(drive, &pc);
3039 if (retval)
3040 return (retval);
3041
3042 idetape_create_read_position_cmd(&pc);
3043 return (idetape_queue_pc_tail(drive, &pc));
3044}
3045
3046static void idetape_discard_read_pipeline (ide_drive_t *drive, int restore_position)
3047{
3048 idetape_tape_t *tape = drive->driver_data;
3049 int cnt;
3050 int seek, position;
3051
3052 cnt = __idetape_discard_read_pipeline(drive);
3053 if (restore_position) {
3054 position = idetape_read_position(drive);
3055 seek = position > cnt ? position - cnt : 0;
3056 if (idetape_position_tape(drive, seek, 0, 0)) {
3057 printk(KERN_INFO "ide-tape: %s: position_tape failed in discard_pipeline()\n", tape->name);
3058 return;
3059 }
3060 }
3061}
3062
3063/*
3064 * idetape_queue_rw_tail generates a read/write request for the block
3065 * device interface and wait for it to be serviced.
3066 */
3067static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, struct idetape_bh *bh)
3068{
3069 idetape_tape_t *tape = drive->driver_data;
3070 struct request rq;
3071
3072#if IDETAPE_DEBUG_LOG
3073 if (tape->debug_level >= 2)
3074 printk(KERN_INFO "ide-tape: idetape_queue_rw_tail: cmd=%d\n",cmd);
3075#endif /* IDETAPE_DEBUG_LOG */
3076#if IDETAPE_DEBUG_BUGS
3077 if (idetape_pipeline_active(tape)) {
3078 printk(KERN_ERR "ide-tape: bug: the pipeline is active in idetape_queue_rw_tail\n");
3079 return (0);
3080 }
3081#endif /* IDETAPE_DEBUG_BUGS */
3082
3083 idetape_init_rq(&rq, cmd);
3084 rq.rq_disk = tape->disk;
3085 rq.special = (void *)bh;
3086 rq.sector = tape->first_frame_position;
3087 rq.nr_sectors = rq.current_nr_sectors = blocks;
3088 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
3089
3090 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0)
3091 return 0;
3092
3093 if (tape->merge_stage)
3094 idetape_init_merge_stage(tape);
3095 if (rq.errors == IDETAPE_ERROR_GENERAL)
3096 return -EIO;
3097 return (tape->tape_block_size * (blocks-rq.current_nr_sectors));
3098}
3099
3100/*
3101 * idetape_insert_pipeline_into_queue is used to start servicing the
3102 * pipeline stages, starting from tape->next_stage.
3103 */
3104static void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
3105{
3106 idetape_tape_t *tape = drive->driver_data;
3107
3108 if (tape->next_stage == NULL)
3109 return;
3110 if (!idetape_pipeline_active(tape)) {
3111 set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
3112 idetape_active_next_stage(drive);
3113 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end);
3114 }
3115}
3116
3117static void idetape_create_inquiry_cmd (idetape_pc_t *pc)
3118{
3119 idetape_init_pc(pc);
3120 pc->c[0] = IDETAPE_INQUIRY_CMD;
3121 pc->c[4] = pc->request_transfer = 254;
3122 pc->callback = &idetape_pc_callback;
3123}
3124
3125static void idetape_create_rewind_cmd (ide_drive_t *drive, idetape_pc_t *pc)
3126{
3127 idetape_init_pc(pc);
3128 pc->c[0] = IDETAPE_REWIND_CMD;
3129 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
3130 pc->callback = &idetape_pc_callback;
3131}
3132
3133#if 0
3134static void idetape_create_mode_select_cmd (idetape_pc_t *pc, int length)
3135{
3136 idetape_init_pc(pc);
3137 set_bit(PC_WRITING, &pc->flags);
3138 pc->c[0] = IDETAPE_MODE_SELECT_CMD;
3139 pc->c[1] = 0x10;
3140 put_unaligned(htons(length), (unsigned short *) &pc->c[3]);
3141 pc->request_transfer = 255;
3142 pc->callback = &idetape_pc_callback;
3143}
3144#endif
3145
3146static void idetape_create_erase_cmd (idetape_pc_t *pc)
3147{
3148 idetape_init_pc(pc);
3149 pc->c[0] = IDETAPE_ERASE_CMD;
3150 pc->c[1] = 1;
3151 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
3152 pc->callback = &idetape_pc_callback;
3153}
3154
3155static void idetape_create_space_cmd (idetape_pc_t *pc,int count, u8 cmd)
3156{
3157 idetape_init_pc(pc);
3158 pc->c[0] = IDETAPE_SPACE_CMD;
3159 put_unaligned(htonl(count), (unsigned int *) &pc->c[1]);
3160 pc->c[1] = cmd;
3161 set_bit(PC_WAIT_FOR_DSC, &pc->flags);
3162 pc->callback = &idetape_pc_callback;
3163}
3164
3165static void idetape_wait_first_stage (ide_drive_t *drive)
3166{
3167 idetape_tape_t *tape = drive->driver_data;
3168 unsigned long flags;
3169
3170 if (tape->first_stage == NULL)
3171 return;
3172 spin_lock_irqsave(&tape->spinlock, flags);
3173 if (tape->active_stage == tape->first_stage)
3174 idetape_wait_for_request(drive, tape->active_data_request);
3175 spin_unlock_irqrestore(&tape->spinlock, flags);
3176}
3177
3178/*
3179 * idetape_add_chrdev_write_request tries to add a character device
3180 * originated write request to our pipeline. In case we don't succeed,
3181 * we revert to non-pipelined operation mode for this request.
3182 *
3183 * 1. Try to allocate a new pipeline stage.
3184 * 2. If we can't, wait for more and more requests to be serviced
3185 * and try again each time.
3186 * 3. If we still can't allocate a stage, fallback to
3187 * non-pipelined operation mode for this request.
3188 */
3189static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks)
3190{
3191 idetape_tape_t *tape = drive->driver_data;
3192 idetape_stage_t *new_stage;
3193 unsigned long flags;
3194 struct request *rq;
3195
3196#if IDETAPE_DEBUG_LOG
3197 if (tape->debug_level >= 3)
3198 printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_write_request\n");
3199#endif /* IDETAPE_DEBUG_LOG */
3200
3201 /*
3202 * Attempt to allocate a new stage.
3203 * Pay special attention to possible race conditions.
3204 */
3205 while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) {
3206 spin_lock_irqsave(&tape->spinlock, flags);
3207 if (idetape_pipeline_active(tape)) {
3208 idetape_wait_for_request(drive, tape->active_data_request);
3209 spin_unlock_irqrestore(&tape->spinlock, flags);
3210 } else {
3211 spin_unlock_irqrestore(&tape->spinlock, flags);
3212 idetape_insert_pipeline_into_queue(drive);
3213 if (idetape_pipeline_active(tape))
3214 continue;
3215 /*
3216 * Linux is short on memory. Fallback to
3217 * non-pipelined operation mode for this request.
3218 */
3219 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
3220 }
3221 }
3222 rq = &new_stage->rq;
3223 idetape_init_rq(rq, REQ_IDETAPE_WRITE);
3224 /* Doesn't actually matter - We always assume sequential access */
3225 rq->sector = tape->first_frame_position;
3226 rq->nr_sectors = rq->current_nr_sectors = blocks;
3227
3228 idetape_switch_buffers(tape, new_stage);
3229 idetape_add_stage_tail(drive, new_stage);
3230 tape->pipeline_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 calculate_speeds(drive);
3232
3233 /*
3234 * Estimate whether the tape has stopped writing by checking
3235 * if our write pipeline is currently empty. If we are not
3236 * writing anymore, wait for the pipeline to be full enough
3237 * (90%) before starting to service requests, so that we will
3238 * be able to keep up with the higher speeds of the tape.
3239 */
3240 if (!idetape_pipeline_active(tape)) {
3241 if (tape->nr_stages >= tape->max_stages * 9 / 10 ||
3242 tape->nr_stages >= tape->max_stages - tape->uncontrolled_pipeline_head_speed * 3 * 1024 / tape->tape_block_size) {
3243 tape->measure_insert_time = 1;
3244 tape->insert_time = jiffies;
3245 tape->insert_size = 0;
3246 tape->insert_speed = 0;
3247 idetape_insert_pipeline_into_queue(drive);
3248 }
3249 }
3250 if (test_and_clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
3251 /* Return a deferred error */
3252 return -EIO;
3253 return blocks;
3254}
3255
3256/*
3257 * idetape_wait_for_pipeline will wait until all pending pipeline
3258 * requests are serviced. Typically called on device close.
3259 */
3260static void idetape_wait_for_pipeline (ide_drive_t *drive)
3261{
3262 idetape_tape_t *tape = drive->driver_data;
3263 unsigned long flags;
3264
3265 while (tape->next_stage || idetape_pipeline_active(tape)) {
3266 idetape_insert_pipeline_into_queue(drive);
3267 spin_lock_irqsave(&tape->spinlock, flags);
3268 if (idetape_pipeline_active(tape))
3269 idetape_wait_for_request(drive, tape->active_data_request);
3270 spin_unlock_irqrestore(&tape->spinlock, flags);
3271 }
3272}
3273
3274static void idetape_empty_write_pipeline (ide_drive_t *drive)
3275{
3276 idetape_tape_t *tape = drive->driver_data;
3277 int blocks, min;
3278 struct idetape_bh *bh;
3279
3280#if IDETAPE_DEBUG_BUGS
3281 if (tape->chrdev_direction != idetape_direction_write) {
3282 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
3283 return;
3284 }
3285 if (tape->merge_stage_size > tape->stage_size) {
3286 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n");
3287 tape->merge_stage_size = tape->stage_size;
3288 }
3289#endif /* IDETAPE_DEBUG_BUGS */
3290 if (tape->merge_stage_size) {
3291 blocks = tape->merge_stage_size / tape->tape_block_size;
3292 if (tape->merge_stage_size % tape->tape_block_size) {
3293 unsigned int i;
3294
3295 blocks++;
3296 i = tape->tape_block_size - tape->merge_stage_size % tape->tape_block_size;
3297 bh = tape->bh->b_reqnext;
3298 while (bh) {
3299 atomic_set(&bh->b_count, 0);
3300 bh = bh->b_reqnext;
3301 }
3302 bh = tape->bh;
3303 while (i) {
3304 if (bh == NULL) {
3305
3306 printk(KERN_INFO "ide-tape: bug, bh NULL\n");
3307 break;
3308 }
3309 min = min(i, (unsigned int)(bh->b_size - atomic_read(&bh->b_count)));
3310 memset(bh->b_data + atomic_read(&bh->b_count), 0, min);
3311 atomic_add(min, &bh->b_count);
3312 i -= min;
3313 bh = bh->b_reqnext;
3314 }
3315 }
3316 (void) idetape_add_chrdev_write_request(drive, blocks);
3317 tape->merge_stage_size = 0;
3318 }
3319 idetape_wait_for_pipeline(drive);
3320 if (tape->merge_stage != NULL) {
3321 __idetape_kfree_stage(tape->merge_stage);
3322 tape->merge_stage = NULL;
3323 }
3324 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
3325 tape->chrdev_direction = idetape_direction_none;
3326
3327 /*
3328 * On the next backup, perform the feedback loop again.
3329 * (I don't want to keep sense information between backups,
3330 * as some systems are constantly on, and the system load
3331 * can be totally different on the next backup).
3332 */
3333 tape->max_stages = tape->min_pipeline;
3334#if IDETAPE_DEBUG_BUGS
3335 if (tape->first_stage != NULL ||
3336 tape->next_stage != NULL ||
3337 tape->last_stage != NULL ||
3338 tape->nr_stages != 0) {
3339 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, "
3340 "first_stage %p, next_stage %p, "
3341 "last_stage %p, nr_stages %d\n",
3342 tape->first_stage, tape->next_stage,
3343 tape->last_stage, tape->nr_stages);
3344 }
3345#endif /* IDETAPE_DEBUG_BUGS */
3346}
3347
3348static void idetape_restart_speed_control (ide_drive_t *drive)
3349{
3350 idetape_tape_t *tape = drive->driver_data;
3351
3352 tape->restart_speed_control_req = 0;
3353 tape->pipeline_head = 0;
3354 tape->controlled_last_pipeline_head = tape->uncontrolled_last_pipeline_head = 0;
3355 tape->controlled_previous_pipeline_head = tape->uncontrolled_previous_pipeline_head = 0;
3356 tape->pipeline_head_speed = tape->controlled_pipeline_head_speed = 5000;
3357 tape->uncontrolled_pipeline_head_speed = 0;
3358 tape->controlled_pipeline_head_time = tape->uncontrolled_pipeline_head_time = jiffies;
3359 tape->controlled_previous_head_time = tape->uncontrolled_previous_head_time = jiffies;
3360}
3361
3362static int idetape_initiate_read (ide_drive_t *drive, int max_stages)
3363{
3364 idetape_tape_t *tape = drive->driver_data;
3365 idetape_stage_t *new_stage;
3366 struct request rq;
3367 int bytes_read;
3368 int blocks = tape->capabilities.ctl;
3369
3370 /* Initialize read operation */
3371 if (tape->chrdev_direction != idetape_direction_read) {
3372 if (tape->chrdev_direction == idetape_direction_write) {
3373 idetape_empty_write_pipeline(drive);
3374 idetape_flush_tape_buffers(drive);
3375 }
3376#if IDETAPE_DEBUG_BUGS
3377 if (tape->merge_stage || tape->merge_stage_size) {
3378 printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n");
3379 tape->merge_stage_size = 0;
3380 }
3381#endif /* IDETAPE_DEBUG_BUGS */
3382 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
3383 return -ENOMEM;
3384 tape->chrdev_direction = idetape_direction_read;
3385
3386 /*
3387 * Issue a read 0 command to ensure that DSC handshake
3388 * is switched from completion mode to buffer available
3389 * mode.
3390 * No point in issuing this if DSC overlap isn't supported,
3391 * some drives (Seagate STT3401A) will return an error.
3392 */
3393 if (drive->dsc_overlap) {
3394 bytes_read = idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, 0, tape->merge_stage->bh);
3395 if (bytes_read < 0) {
3396 __idetape_kfree_stage(tape->merge_stage);
3397 tape->merge_stage = NULL;
3398 tape->chrdev_direction = idetape_direction_none;
3399 return bytes_read;
3400 }
3401 }
3402 }
3403 if (tape->restart_speed_control_req)
3404 idetape_restart_speed_control(drive);
3405 idetape_init_rq(&rq, REQ_IDETAPE_READ);
3406 rq.sector = tape->first_frame_position;
3407 rq.nr_sectors = rq.current_nr_sectors = blocks;
3408 if (!test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags) &&
3409 tape->nr_stages < max_stages) {
3410 new_stage = idetape_kmalloc_stage(tape);
3411 while (new_stage != NULL) {
3412 new_stage->rq = rq;
3413 idetape_add_stage_tail(drive, new_stage);
3414 if (tape->nr_stages >= max_stages)
3415 break;
3416 new_stage = idetape_kmalloc_stage(tape);
3417 }
3418 }
3419 if (!idetape_pipeline_active(tape)) {
3420 if (tape->nr_pending_stages >= 3 * max_stages / 4) {
3421 tape->measure_insert_time = 1;
3422 tape->insert_time = jiffies;
3423 tape->insert_size = 0;
3424 tape->insert_speed = 0;
3425 idetape_insert_pipeline_into_queue(drive);
3426 }
3427 }
3428 return 0;
3429}
3430
3431/*
3432 * idetape_add_chrdev_read_request is called from idetape_chrdev_read
3433 * to service a character device read request and add read-ahead
3434 * requests to our pipeline.
3435 */
3436static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks)
3437{
3438 idetape_tape_t *tape = drive->driver_data;
3439 unsigned long flags;
3440 struct request *rq_ptr;
3441 int bytes_read;
3442
3443#if IDETAPE_DEBUG_LOG
3444 if (tape->debug_level >= 4)
3445 printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_read_request, %d blocks\n", blocks);
3446#endif /* IDETAPE_DEBUG_LOG */
3447
3448 /*
3449 * If we are at a filemark, return a read length of 0
3450 */
3451 if (test_bit(IDETAPE_FILEMARK, &tape->flags))
3452 return 0;
3453
3454 /*
3455 * Wait for the next block to be available at the head
3456 * of the pipeline
3457 */
3458 idetape_initiate_read(drive, tape->max_stages);
3459 if (tape->first_stage == NULL) {
3460 if (test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags))
3461 return 0;
3462 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks, tape->merge_stage->bh);
3463 }
3464 idetape_wait_first_stage(drive);
3465 rq_ptr = &tape->first_stage->rq;
3466 bytes_read = tape->tape_block_size * (rq_ptr->nr_sectors - rq_ptr->current_nr_sectors);
3467 rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0;
3468
3469
3470 if (rq_ptr->errors == IDETAPE_ERROR_EOD)
3471 return 0;
3472 else {
3473 idetape_switch_buffers(tape, tape->first_stage);
3474 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK)
3475 set_bit(IDETAPE_FILEMARK, &tape->flags);
3476 spin_lock_irqsave(&tape->spinlock, flags);
3477 idetape_remove_stage_head(drive);
3478 spin_unlock_irqrestore(&tape->spinlock, flags);
3479 tape->pipeline_head++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 calculate_speeds(drive);
3481 }
3482#if IDETAPE_DEBUG_BUGS
3483 if (bytes_read > blocks * tape->tape_block_size) {
3484 printk(KERN_ERR "ide-tape: bug: trying to return more bytes than requested\n");
3485 bytes_read = blocks * tape->tape_block_size;
3486 }
3487#endif /* IDETAPE_DEBUG_BUGS */
3488 return (bytes_read);
3489}
3490
3491static void idetape_pad_zeros (ide_drive_t *drive, int bcount)
3492{
3493 idetape_tape_t *tape = drive->driver_data;
3494 struct idetape_bh *bh;
3495 int blocks;
3496
3497 while (bcount) {
3498 unsigned int count;
3499
3500 bh = tape->merge_stage->bh;
3501 count = min(tape->stage_size, bcount);
3502 bcount -= count;
3503 blocks = count / tape->tape_block_size;
3504 while (count) {
3505 atomic_set(&bh->b_count, min(count, (unsigned int)bh->b_size));
3506 memset(bh->b_data, 0, atomic_read(&bh->b_count));
3507 count -= atomic_read(&bh->b_count);
3508 bh = bh->b_reqnext;
3509 }
3510 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh);
3511 }
3512}
3513
3514static int idetape_pipeline_size (ide_drive_t *drive)
3515{
3516 idetape_tape_t *tape = drive->driver_data;
3517 idetape_stage_t *stage;
3518 struct request *rq;
3519 int size = 0;
3520
3521 idetape_wait_for_pipeline(drive);
3522 stage = tape->first_stage;
3523 while (stage != NULL) {
3524 rq = &stage->rq;
3525 size += tape->tape_block_size * (rq->nr_sectors-rq->current_nr_sectors);
3526 if (rq->errors == IDETAPE_ERROR_FILEMARK)
3527 size += tape->tape_block_size;
3528 stage = stage->next;
3529 }
3530 size += tape->merge_stage_size;
3531 return size;
3532}
3533
3534/*
3535 * Rewinds the tape to the Beginning Of the current Partition (BOP).
3536 *
3537 * We currently support only one partition.
3538 */
3539static int idetape_rewind_tape (ide_drive_t *drive)
3540{
3541 int retval;
3542 idetape_pc_t pc;
3543#if IDETAPE_DEBUG_LOG
3544 idetape_tape_t *tape = drive->driver_data;
3545 if (tape->debug_level >= 2)
3546 printk(KERN_INFO "ide-tape: Reached idetape_rewind_tape\n");
3547#endif /* IDETAPE_DEBUG_LOG */
3548
3549 idetape_create_rewind_cmd(drive, &pc);
3550 retval = idetape_queue_pc_tail(drive, &pc);
3551 if (retval)
3552 return retval;
3553
3554 idetape_create_read_position_cmd(&pc);
3555 retval = idetape_queue_pc_tail(drive, &pc);
3556 if (retval)
3557 return retval;
3558 return 0;
3559}
3560
3561/*
3562 * Our special ide-tape ioctl's.
3563 *
3564 * Currently there aren't any ioctl's.
3565 * mtio.h compatible commands should be issued to the character device
3566 * interface.
3567 */
3568static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, unsigned long arg)
3569{
3570 idetape_tape_t *tape = drive->driver_data;
3571 idetape_config_t config;
3572 void __user *argp = (void __user *)arg;
3573
3574#if IDETAPE_DEBUG_LOG
3575 if (tape->debug_level >= 4)
3576 printk(KERN_INFO "ide-tape: Reached idetape_blkdev_ioctl\n");
3577#endif /* IDETAPE_DEBUG_LOG */
3578 switch (cmd) {
3579 case 0x0340:
3580 if (copy_from_user(&config, argp, sizeof (idetape_config_t)))
3581 return -EFAULT;
3582 tape->best_dsc_rw_frequency = config.dsc_rw_frequency;
3583 tape->max_stages = config.nr_stages;
3584 break;
3585 case 0x0350:
3586 config.dsc_rw_frequency = (int) tape->best_dsc_rw_frequency;
3587 config.nr_stages = tape->max_stages;
3588 if (copy_to_user(argp, &config, sizeof (idetape_config_t)))
3589 return -EFAULT;
3590 break;
3591 default:
3592 return -EIO;
3593 }
3594 return 0;
3595}
3596
3597/*
3598 * idetape_space_over_filemarks is now a bit more complicated than just
3599 * passing the command to the tape since we may have crossed some
3600 * filemarks during our pipelined read-ahead mode.
3601 *
3602 * As a minor side effect, the pipeline enables us to support MTFSFM when
3603 * the filemark is in our internal pipeline even if the tape doesn't
3604 * support spacing over filemarks in the reverse direction.
3605 */
3606static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
3607{
3608 idetape_tape_t *tape = drive->driver_data;
3609 idetape_pc_t pc;
3610 unsigned long flags;
3611 int retval,count=0;
3612
3613 if (mt_count == 0)
3614 return 0;
3615 if (MTBSF == mt_op || MTBSFM == mt_op) {
3616 if (!tape->capabilities.sprev)
3617 return -EIO;
3618 mt_count = - mt_count;
3619 }
3620
3621 if (tape->chrdev_direction == idetape_direction_read) {
3622 /*
3623 * We have a read-ahead buffer. Scan it for crossed
3624 * filemarks.
3625 */
3626 tape->merge_stage_size = 0;
3627 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags))
3628 ++count;
3629 while (tape->first_stage != NULL) {
3630 if (count == mt_count) {
3631 if (mt_op == MTFSFM)
3632 set_bit(IDETAPE_FILEMARK, &tape->flags);
3633 return 0;
3634 }
3635 spin_lock_irqsave(&tape->spinlock, flags);
3636 if (tape->first_stage == tape->active_stage) {
3637 /*
3638 * We have reached the active stage in the read pipeline.
3639 * There is no point in allowing the drive to continue
3640 * reading any farther, so we stop the pipeline.
3641 *
3642 * This section should be moved to a separate subroutine,
3643 * because a similar function is performed in
3644 * __idetape_discard_read_pipeline(), for example.
3645 */
3646 tape->next_stage = NULL;
3647 spin_unlock_irqrestore(&tape->spinlock, flags);
3648 idetape_wait_first_stage(drive);
3649 tape->next_stage = tape->first_stage->next;
3650 } else
3651 spin_unlock_irqrestore(&tape->spinlock, flags);
3652 if (tape->first_stage->rq.errors == IDETAPE_ERROR_FILEMARK)
3653 ++count;
3654 idetape_remove_stage_head(drive);
3655 }
3656 idetape_discard_read_pipeline(drive, 0);
3657 }
3658
3659 /*
3660 * The filemark was not found in our internal pipeline.
3661 * Now we can issue the space command.
3662 */
3663 switch (mt_op) {
3664 case MTFSF:
3665 case MTBSF:
3666 idetape_create_space_cmd(&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
3667 return (idetape_queue_pc_tail(drive, &pc));
3668 case MTFSFM:
3669 case MTBSFM:
3670 if (!tape->capabilities.sprev)
3671 return (-EIO);
3672 retval = idetape_space_over_filemarks(drive, MTFSF, mt_count-count);
3673 if (retval) return (retval);
3674 count = (MTBSFM == mt_op ? 1 : -1);
3675 return (idetape_space_over_filemarks(drive, MTFSF, count));
3676 default:
3677 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op);
3678 return (-EIO);
3679 }
3680}
3681
3682
3683/*
3684 * Our character device read / write functions.
3685 *
3686 * The tape is optimized to maximize throughput when it is transferring
3687 * an integral number of the "continuous transfer limit", which is
3688 * a parameter of the specific tape (26 KB on my particular tape).
3689 * (32 kB for Onstream)
3690 *
3691 * As of version 1.3 of the driver, the character device provides an
3692 * abstract continuous view of the media - any mix of block sizes (even 1
3693 * byte) on the same backup/restore procedure is supported. The driver
3694 * will internally convert the requests to the recommended transfer unit,
3695 * so that an unmatch between the user's block size to the recommended
3696 * size will only result in a (slightly) increased driver overhead, but
3697 * will no longer hit performance.
3698 * This is not applicable to Onstream.
3699 */
3700static ssize_t idetape_chrdev_read (struct file *file, char __user *buf,
3701 size_t count, loff_t *ppos)
3702{
3703 struct ide_tape_obj *tape = ide_tape_f(file);
3704 ide_drive_t *drive = tape->drive;
3705 ssize_t bytes_read,temp, actually_read = 0, rc;
Daniel Walkerdcd96372006-06-25 05:47:37 -07003706 ssize_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707
3708#if IDETAPE_DEBUG_LOG
3709 if (tape->debug_level >= 3)
3710 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_read, count %Zd\n", count);
3711#endif /* IDETAPE_DEBUG_LOG */
3712
3713 if (tape->chrdev_direction != idetape_direction_read) {
3714 if (test_bit(IDETAPE_DETECT_BS, &tape->flags))
3715 if (count > tape->tape_block_size &&
3716 (count % tape->tape_block_size) == 0)
3717 tape->user_bs_factor = count / tape->tape_block_size;
3718 }
3719 if ((rc = idetape_initiate_read(drive, tape->max_stages)) < 0)
3720 return rc;
3721 if (count == 0)
3722 return (0);
3723 if (tape->merge_stage_size) {
3724 actually_read = min((unsigned int)(tape->merge_stage_size), (unsigned int)count);
Daniel Walkerdcd96372006-06-25 05:47:37 -07003725 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, actually_read))
3726 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 buf += actually_read;
3728 tape->merge_stage_size -= actually_read;
3729 count -= actually_read;
3730 }
3731 while (count >= tape->stage_size) {
3732 bytes_read = idetape_add_chrdev_read_request(drive, tape->capabilities.ctl);
3733 if (bytes_read <= 0)
3734 goto finish;
Daniel Walkerdcd96372006-06-25 05:47:37 -07003735 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, bytes_read))
3736 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 buf += bytes_read;
3738 count -= bytes_read;
3739 actually_read += bytes_read;
3740 }
3741 if (count) {
3742 bytes_read = idetape_add_chrdev_read_request(drive, tape->capabilities.ctl);
3743 if (bytes_read <= 0)
3744 goto finish;
3745 temp = min((unsigned long)count, (unsigned long)bytes_read);
Daniel Walkerdcd96372006-06-25 05:47:37 -07003746 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, temp))
3747 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 actually_read += temp;
3749 tape->merge_stage_size = bytes_read-temp;
3750 }
3751finish:
3752 if (!actually_read && test_bit(IDETAPE_FILEMARK, &tape->flags)) {
3753#if IDETAPE_DEBUG_LOG
3754 if (tape->debug_level >= 2)
3755 printk(KERN_INFO "ide-tape: %s: spacing over filemark\n", tape->name);
3756#endif
3757 idetape_space_over_filemarks(drive, MTFSF, 1);
3758 return 0;
3759 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07003760
3761 return (ret) ? ret : actually_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762}
3763
3764static ssize_t idetape_chrdev_write (struct file *file, const char __user *buf,
3765 size_t count, loff_t *ppos)
3766{
3767 struct ide_tape_obj *tape = ide_tape_f(file);
3768 ide_drive_t *drive = tape->drive;
Daniel Walkerdcd96372006-06-25 05:47:37 -07003769 ssize_t actually_written = 0;
3770 ssize_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
3772 /* The drive is write protected. */
3773 if (tape->write_prot)
3774 return -EACCES;
3775
3776#if IDETAPE_DEBUG_LOG
3777 if (tape->debug_level >= 3)
3778 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_write, "
3779 "count %Zd\n", count);
3780#endif /* IDETAPE_DEBUG_LOG */
3781
3782 /* Initialize write operation */
3783 if (tape->chrdev_direction != idetape_direction_write) {
3784 if (tape->chrdev_direction == idetape_direction_read)
3785 idetape_discard_read_pipeline(drive, 1);
3786#if IDETAPE_DEBUG_BUGS
3787 if (tape->merge_stage || tape->merge_stage_size) {
3788 printk(KERN_ERR "ide-tape: merge_stage_size "
3789 "should be 0 now\n");
3790 tape->merge_stage_size = 0;
3791 }
3792#endif /* IDETAPE_DEBUG_BUGS */
3793 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL)
3794 return -ENOMEM;
3795 tape->chrdev_direction = idetape_direction_write;
3796 idetape_init_merge_stage(tape);
3797
3798 /*
3799 * Issue a write 0 command to ensure that DSC handshake
3800 * is switched from completion mode to buffer available
3801 * mode.
3802 * No point in issuing this if DSC overlap isn't supported,
3803 * some drives (Seagate STT3401A) will return an error.
3804 */
3805 if (drive->dsc_overlap) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07003806 ssize_t retval = idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, 0, tape->merge_stage->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 if (retval < 0) {
3808 __idetape_kfree_stage(tape->merge_stage);
3809 tape->merge_stage = NULL;
3810 tape->chrdev_direction = idetape_direction_none;
3811 return retval;
3812 }
3813 }
3814 }
3815 if (count == 0)
3816 return (0);
3817 if (tape->restart_speed_control_req)
3818 idetape_restart_speed_control(drive);
3819 if (tape->merge_stage_size) {
3820#if IDETAPE_DEBUG_BUGS
3821 if (tape->merge_stage_size >= tape->stage_size) {
3822 printk(KERN_ERR "ide-tape: bug: merge buffer too big\n");
3823 tape->merge_stage_size = 0;
3824 }
3825#endif /* IDETAPE_DEBUG_BUGS */
3826 actually_written = min((unsigned int)(tape->stage_size - tape->merge_stage_size), (unsigned int)count);
Daniel Walkerdcd96372006-06-25 05:47:37 -07003827 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, actually_written))
3828 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 buf += actually_written;
3830 tape->merge_stage_size += actually_written;
3831 count -= actually_written;
3832
3833 if (tape->merge_stage_size == tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07003834 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 tape->merge_stage_size = 0;
3836 retval = idetape_add_chrdev_write_request(drive, tape->capabilities.ctl);
3837 if (retval <= 0)
3838 return (retval);
3839 }
3840 }
3841 while (count >= tape->stage_size) {
Daniel Walkerdcd96372006-06-25 05:47:37 -07003842 ssize_t retval;
3843 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, tape->stage_size))
3844 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 buf += tape->stage_size;
3846 count -= tape->stage_size;
3847 retval = idetape_add_chrdev_write_request(drive, tape->capabilities.ctl);
3848 actually_written += tape->stage_size;
3849 if (retval <= 0)
3850 return (retval);
3851 }
3852 if (count) {
3853 actually_written += count;
Daniel Walkerdcd96372006-06-25 05:47:37 -07003854 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, count))
3855 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 tape->merge_stage_size += count;
3857 }
Daniel Walkerdcd96372006-06-25 05:47:37 -07003858 return (ret) ? ret : actually_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859}
3860
3861static int idetape_write_filemark (ide_drive_t *drive)
3862{
3863 idetape_pc_t pc;
3864
3865 /* Write a filemark */
3866 idetape_create_write_filemark_cmd(drive, &pc, 1);
3867 if (idetape_queue_pc_tail(drive, &pc)) {
3868 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
3869 return -EIO;
3870 }
3871 return 0;
3872}
3873
3874/*
3875 * idetape_mtioctop is called from idetape_chrdev_ioctl when
3876 * the general mtio MTIOCTOP ioctl is requested.
3877 *
3878 * We currently support the following mtio.h operations:
3879 *
3880 * MTFSF - Space over mt_count filemarks in the positive direction.
3881 * The tape is positioned after the last spaced filemark.
3882 *
3883 * MTFSFM - Same as MTFSF, but the tape is positioned before the
3884 * last filemark.
3885 *
3886 * MTBSF - Steps background over mt_count filemarks, tape is
3887 * positioned before the last filemark.
3888 *
3889 * MTBSFM - Like MTBSF, only tape is positioned after the last filemark.
3890 *
3891 * Note:
3892 *
3893 * MTBSF and MTBSFM are not supported when the tape doesn't
3894 * support spacing over filemarks in the reverse direction.
3895 * In this case, MTFSFM is also usually not supported (it is
3896 * supported in the rare case in which we crossed the filemark
3897 * during our read-ahead pipelined operation mode).
3898 *
3899 * MTWEOF - Writes mt_count filemarks. Tape is positioned after
3900 * the last written filemark.
3901 *
3902 * MTREW - Rewinds tape.
3903 *
3904 * MTLOAD - Loads the tape.
3905 *
3906 * MTOFFL - Puts the tape drive "Offline": Rewinds the tape and
3907 * MTUNLOAD prevents further access until the media is replaced.
3908 *
3909 * MTNOP - Flushes tape buffers.
3910 *
3911 * MTRETEN - Retension media. This typically consists of one end
3912 * to end pass on the media.
3913 *
3914 * MTEOM - Moves to the end of recorded data.
3915 *
3916 * MTERASE - Erases tape.
3917 *
3918 * MTSETBLK - Sets the user block size to mt_count bytes. If
3919 * mt_count is 0, we will attempt to autodetect
3920 * the block size.
3921 *
3922 * MTSEEK - Positions the tape in a specific block number, where
3923 * each block is assumed to contain which user_block_size
3924 * bytes.
3925 *
3926 * MTSETPART - Switches to another tape partition.
3927 *
3928 * MTLOCK - Locks the tape door.
3929 *
3930 * MTUNLOCK - Unlocks the tape door.
3931 *
3932 * The following commands are currently not supported:
3933 *
3934 * MTFSS, MTBSS, MTWSM, MTSETDENSITY,
3935 * MTSETDRVBUFFER, MT_ST_BOOLEANS, MT_ST_WRITE_THRESHOLD.
3936 */
3937static int idetape_mtioctop (ide_drive_t *drive,short mt_op,int mt_count)
3938{
3939 idetape_tape_t *tape = drive->driver_data;
3940 idetape_pc_t pc;
3941 int i,retval;
3942
3943#if IDETAPE_DEBUG_LOG
3944 if (tape->debug_level >= 1)
3945 printk(KERN_INFO "ide-tape: Handling MTIOCTOP ioctl: "
3946 "mt_op=%d, mt_count=%d\n", mt_op, mt_count);
3947#endif /* IDETAPE_DEBUG_LOG */
3948 /*
3949 * Commands which need our pipelined read-ahead stages.
3950 */
3951 switch (mt_op) {
3952 case MTFSF:
3953 case MTFSFM:
3954 case MTBSF:
3955 case MTBSFM:
3956 if (!mt_count)
3957 return (0);
3958 return (idetape_space_over_filemarks(drive,mt_op,mt_count));
3959 default:
3960 break;
3961 }
3962 switch (mt_op) {
3963 case MTWEOF:
3964 if (tape->write_prot)
3965 return -EACCES;
3966 idetape_discard_read_pipeline(drive, 1);
3967 for (i = 0; i < mt_count; i++) {
3968 retval = idetape_write_filemark(drive);
3969 if (retval)
3970 return retval;
3971 }
3972 return (0);
3973 case MTREW:
3974 idetape_discard_read_pipeline(drive, 0);
3975 if (idetape_rewind_tape(drive))
3976 return -EIO;
3977 return 0;
3978 case MTLOAD:
3979 idetape_discard_read_pipeline(drive, 0);
3980 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK);
3981 return (idetape_queue_pc_tail(drive, &pc));
3982 case MTUNLOAD:
3983 case MTOFFL:
3984 /*
3985 * If door is locked, attempt to unlock before
3986 * attempting to eject.
3987 */
3988 if (tape->door_locked) {
3989 if (idetape_create_prevent_cmd(drive, &pc, 0))
3990 if (!idetape_queue_pc_tail(drive, &pc))
3991 tape->door_locked = DOOR_UNLOCKED;
3992 }
3993 idetape_discard_read_pipeline(drive, 0);
3994 idetape_create_load_unload_cmd(drive, &pc,!IDETAPE_LU_LOAD_MASK);
3995 retval = idetape_queue_pc_tail(drive, &pc);
3996 if (!retval)
3997 clear_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags);
3998 return retval;
3999 case MTNOP:
4000 idetape_discard_read_pipeline(drive, 0);
4001 return (idetape_flush_tape_buffers(drive));
4002 case MTRETEN:
4003 idetape_discard_read_pipeline(drive, 0);
4004 idetape_create_load_unload_cmd(drive, &pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
4005 return (idetape_queue_pc_tail(drive, &pc));
4006 case MTEOM:
4007 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
4008 return (idetape_queue_pc_tail(drive, &pc));
4009 case MTERASE:
4010 (void) idetape_rewind_tape(drive);
4011 idetape_create_erase_cmd(&pc);
4012 return (idetape_queue_pc_tail(drive, &pc));
4013 case MTSETBLK:
4014 if (mt_count) {
4015 if (mt_count < tape->tape_block_size || mt_count % tape->tape_block_size)
4016 return -EIO;
4017 tape->user_bs_factor = mt_count / tape->tape_block_size;
4018 clear_bit(IDETAPE_DETECT_BS, &tape->flags);
4019 } else
4020 set_bit(IDETAPE_DETECT_BS, &tape->flags);
4021 return 0;
4022 case MTSEEK:
4023 idetape_discard_read_pipeline(drive, 0);
4024 return idetape_position_tape(drive, mt_count * tape->user_bs_factor, tape->partition, 0);
4025 case MTSETPART:
4026 idetape_discard_read_pipeline(drive, 0);
4027 return (idetape_position_tape(drive, 0, mt_count, 0));
4028 case MTFSR:
4029 case MTBSR:
4030 case MTLOCK:
4031 if (!idetape_create_prevent_cmd(drive, &pc, 1))
4032 return 0;
4033 retval = idetape_queue_pc_tail(drive, &pc);
4034 if (retval) return retval;
4035 tape->door_locked = DOOR_EXPLICITLY_LOCKED;
4036 return 0;
4037 case MTUNLOCK:
4038 if (!idetape_create_prevent_cmd(drive, &pc, 0))
4039 return 0;
4040 retval = idetape_queue_pc_tail(drive, &pc);
4041 if (retval) return retval;
4042 tape->door_locked = DOOR_UNLOCKED;
4043 return 0;
4044 default:
4045 printk(KERN_ERR "ide-tape: MTIO operation %d not "
4046 "supported\n", mt_op);
4047 return (-EIO);
4048 }
4049}
4050
4051/*
4052 * Our character device ioctls.
4053 *
4054 * General mtio.h magnetic io commands are supported here, and not in
4055 * the corresponding block interface.
4056 *
4057 * The following ioctls are supported:
4058 *
4059 * MTIOCTOP - Refer to idetape_mtioctop for detailed description.
4060 *
4061 * MTIOCGET - The mt_dsreg field in the returned mtget structure
4062 * will be set to (user block size in bytes <<
4063 * MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK.
4064 *
4065 * The mt_blkno is set to the current user block number.
4066 * The other mtget fields are not supported.
4067 *
4068 * MTIOCPOS - The current tape "block position" is returned. We
4069 * assume that each block contains user_block_size
4070 * bytes.
4071 *
4072 * Our own ide-tape ioctls are supported on both interfaces.
4073 */
4074static int idetape_chrdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
4075{
4076 struct ide_tape_obj *tape = ide_tape_f(file);
4077 ide_drive_t *drive = tape->drive;
4078 struct mtop mtop;
4079 struct mtget mtget;
4080 struct mtpos mtpos;
4081 int block_offset = 0, position = tape->first_frame_position;
4082 void __user *argp = (void __user *)arg;
4083
4084#if IDETAPE_DEBUG_LOG
4085 if (tape->debug_level >= 3)
4086 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_ioctl, "
4087 "cmd=%u\n", cmd);
4088#endif /* IDETAPE_DEBUG_LOG */
4089
4090 tape->restart_speed_control_req = 1;
4091 if (tape->chrdev_direction == idetape_direction_write) {
4092 idetape_empty_write_pipeline(drive);
4093 idetape_flush_tape_buffers(drive);
4094 }
4095 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
4096 block_offset = idetape_pipeline_size(drive) / (tape->tape_block_size * tape->user_bs_factor);
4097 if ((position = idetape_read_position(drive)) < 0)
4098 return -EIO;
4099 }
4100 switch (cmd) {
4101 case MTIOCTOP:
4102 if (copy_from_user(&mtop, argp, sizeof (struct mtop)))
4103 return -EFAULT;
4104 return (idetape_mtioctop(drive,mtop.mt_op,mtop.mt_count));
4105 case MTIOCGET:
4106 memset(&mtget, 0, sizeof (struct mtget));
4107 mtget.mt_type = MT_ISSCSI2;
4108 mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
4109 mtget.mt_dsreg = ((tape->tape_block_size * tape->user_bs_factor) << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
4110 if (tape->drv_write_prot) {
4111 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
4112 }
4113 if (copy_to_user(argp, &mtget, sizeof(struct mtget)))
4114 return -EFAULT;
4115 return 0;
4116 case MTIOCPOS:
4117 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
4118 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos)))
4119 return -EFAULT;
4120 return 0;
4121 default:
4122 if (tape->chrdev_direction == idetape_direction_read)
4123 idetape_discard_read_pipeline(drive, 1);
4124 return idetape_blkdev_ioctl(drive, cmd, arg);
4125 }
4126}
4127
4128static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive);
4129
4130/*
4131 * Our character device open function.
4132 */
4133static int idetape_chrdev_open (struct inode *inode, struct file *filp)
4134{
4135 unsigned int minor = iminor(inode), i = minor & ~0xc0;
4136 ide_drive_t *drive;
4137 idetape_tape_t *tape;
4138 idetape_pc_t pc;
4139 int retval;
4140
4141 /*
4142 * We really want to do nonseekable_open(inode, filp); here, but some
4143 * versions of tar incorrectly call lseek on tapes and bail out if that
4144 * fails. So we disallow pread() and pwrite(), but permit lseeks.
4145 */
4146 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
4147
4148#if IDETAPE_DEBUG_LOG
4149 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_open\n");
4150#endif /* IDETAPE_DEBUG_LOG */
4151
4152 if (i >= MAX_HWIFS * MAX_DRIVES)
4153 return -ENXIO;
4154
4155 if (!(tape = ide_tape_chrdev_get(i)))
4156 return -ENXIO;
4157
4158 drive = tape->drive;
4159
4160 filp->private_data = tape;
4161
4162 if (test_and_set_bit(IDETAPE_BUSY, &tape->flags)) {
4163 retval = -EBUSY;
4164 goto out_put_tape;
4165 }
4166
4167 retval = idetape_wait_ready(drive, 60 * HZ);
4168 if (retval) {
4169 clear_bit(IDETAPE_BUSY, &tape->flags);
4170 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
4171 goto out_put_tape;
4172 }
4173
4174 idetape_read_position(drive);
4175 if (!test_bit(IDETAPE_ADDRESS_VALID, &tape->flags))
4176 (void)idetape_rewind_tape(drive);
4177
4178 if (tape->chrdev_direction != idetape_direction_read)
4179 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags);
4180
4181 /* Read block size and write protect status from drive. */
4182 idetape_get_blocksize_from_block_descriptor(drive);
4183
4184 /* Set write protect flag if device is opened as read-only. */
4185 if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
4186 tape->write_prot = 1;
4187 else
4188 tape->write_prot = tape->drv_write_prot;
4189
4190 /* Make sure drive isn't write protected if user wants to write. */
4191 if (tape->write_prot) {
4192 if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
4193 (filp->f_flags & O_ACCMODE) == O_RDWR) {
4194 clear_bit(IDETAPE_BUSY, &tape->flags);
4195 retval = -EROFS;
4196 goto out_put_tape;
4197 }
4198 }
4199
4200 /*
4201 * Lock the tape drive door so user can't eject.
4202 */
4203 if (tape->chrdev_direction == idetape_direction_none) {
4204 if (idetape_create_prevent_cmd(drive, &pc, 1)) {
4205 if (!idetape_queue_pc_tail(drive, &pc)) {
4206 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
4207 tape->door_locked = DOOR_LOCKED;
4208 }
4209 }
4210 }
4211 idetape_restart_speed_control(drive);
4212 tape->restart_speed_control_req = 0;
4213 return 0;
4214
4215out_put_tape:
4216 ide_tape_put(tape);
4217 return retval;
4218}
4219
4220static void idetape_write_release (ide_drive_t *drive, unsigned int minor)
4221{
4222 idetape_tape_t *tape = drive->driver_data;
4223
4224 idetape_empty_write_pipeline(drive);
4225 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0);
4226 if (tape->merge_stage != NULL) {
4227 idetape_pad_zeros(drive, tape->tape_block_size * (tape->user_bs_factor - 1));
4228 __idetape_kfree_stage(tape->merge_stage);
4229 tape->merge_stage = NULL;
4230 }
4231 idetape_write_filemark(drive);
4232 idetape_flush_tape_buffers(drive);
4233 idetape_flush_tape_buffers(drive);
4234}
4235
4236/*
4237 * Our character device release function.
4238 */
4239static int idetape_chrdev_release (struct inode *inode, struct file *filp)
4240{
4241 struct ide_tape_obj *tape = ide_tape_f(filp);
4242 ide_drive_t *drive = tape->drive;
4243 idetape_pc_t pc;
4244 unsigned int minor = iminor(inode);
4245
4246 lock_kernel();
4247 tape = drive->driver_data;
4248#if IDETAPE_DEBUG_LOG
4249 if (tape->debug_level >= 3)
4250 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_release\n");
4251#endif /* IDETAPE_DEBUG_LOG */
4252
4253 if (tape->chrdev_direction == idetape_direction_write)
4254 idetape_write_release(drive, minor);
4255 if (tape->chrdev_direction == idetape_direction_read) {
4256 if (minor < 128)
4257 idetape_discard_read_pipeline(drive, 1);
4258 else
4259 idetape_wait_for_pipeline(drive);
4260 }
4261 if (tape->cache_stage != NULL) {
4262 __idetape_kfree_stage(tape->cache_stage);
4263 tape->cache_stage = NULL;
4264 }
4265 if (minor < 128 && test_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags))
4266 (void) idetape_rewind_tape(drive);
4267 if (tape->chrdev_direction == idetape_direction_none) {
4268 if (tape->door_locked == DOOR_LOCKED) {
4269 if (idetape_create_prevent_cmd(drive, &pc, 0)) {
4270 if (!idetape_queue_pc_tail(drive, &pc))
4271 tape->door_locked = DOOR_UNLOCKED;
4272 }
4273 }
4274 }
4275 clear_bit(IDETAPE_BUSY, &tape->flags);
4276 ide_tape_put(tape);
4277 unlock_kernel();
4278 return 0;
4279}
4280
4281/*
4282 * idetape_identify_device is called to check the contents of the
4283 * ATAPI IDENTIFY command results. We return:
4284 *
4285 * 1 If the tape can be supported by us, based on the information
4286 * we have so far.
4287 *
4288 * 0 If this tape driver is not currently supported by us.
4289 */
4290static int idetape_identify_device (ide_drive_t *drive)
4291{
4292 struct idetape_id_gcw gcw;
4293 struct hd_driveid *id = drive->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294
4295 if (drive->id_read == 0)
4296 return 1;
4297
4298 *((unsigned short *) &gcw) = id->config;
4299
4300#if IDETAPE_DEBUG_INFO
4301 printk(KERN_INFO "ide-tape: Dumping ATAPI Identify Device tape parameters\n");
4302 printk(KERN_INFO "ide-tape: Protocol Type: ");
4303 switch (gcw.protocol) {
4304 case 0: case 1: printk("ATA\n");break;
4305 case 2: printk("ATAPI\n");break;
4306 case 3: printk("Reserved (Unknown to ide-tape)\n");break;
4307 }
4308 printk(KERN_INFO "ide-tape: Device Type: %x - ",gcw.device_type);
4309 switch (gcw.device_type) {
4310 case 0: printk("Direct-access Device\n");break;
4311 case 1: printk("Streaming Tape Device\n");break;
4312 case 2: case 3: case 4: printk("Reserved\n");break;
4313 case 5: printk("CD-ROM Device\n");break;
4314 case 6: printk("Reserved\n");
4315 case 7: printk("Optical memory Device\n");break;
4316 case 0x1f: printk("Unknown or no Device type\n");break;
4317 default: printk("Reserved\n");
4318 }
4319 printk(KERN_INFO "ide-tape: Removable: %s",gcw.removable ? "Yes\n":"No\n");
4320 printk(KERN_INFO "ide-tape: Command Packet DRQ Type: ");
4321 switch (gcw.drq_type) {
4322 case 0: printk("Microprocessor DRQ\n");break;
4323 case 1: printk("Interrupt DRQ\n");break;
4324 case 2: printk("Accelerated DRQ\n");break;
4325 case 3: printk("Reserved\n");break;
4326 }
4327 printk(KERN_INFO "ide-tape: Command Packet Size: ");
4328 switch (gcw.packet_size) {
4329 case 0: printk("12 bytes\n");break;
4330 case 1: printk("16 bytes\n");break;
4331 default: printk("Reserved\n");break;
4332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333#endif /* IDETAPE_DEBUG_INFO */
4334
4335 /* Check that we can support this device */
4336
4337 if (gcw.protocol !=2 )
4338 printk(KERN_ERR "ide-tape: Protocol is not ATAPI\n");
4339 else if (gcw.device_type != 1)
4340 printk(KERN_ERR "ide-tape: Device type is not set to tape\n");
4341 else if (!gcw.removable)
4342 printk(KERN_ERR "ide-tape: The removable flag is not set\n");
4343 else if (gcw.packet_size != 0) {
4344 printk(KERN_ERR "ide-tape: Packet size is not 12 bytes long\n");
4345 if (gcw.packet_size == 1)
4346 printk(KERN_ERR "ide-tape: Sorry, padding to 16 bytes is still not supported\n");
4347 } else
4348 return 1;
4349 return 0;
4350}
4351
4352/*
4353 * Use INQUIRY to get the firmware revision
4354 */
4355static void idetape_get_inquiry_results (ide_drive_t *drive)
4356{
4357 char *r;
4358 idetape_tape_t *tape = drive->driver_data;
4359 idetape_pc_t pc;
4360 idetape_inquiry_result_t *inquiry;
4361
4362 idetape_create_inquiry_cmd(&pc);
4363 if (idetape_queue_pc_tail(drive, &pc)) {
4364 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n", tape->name);
4365 return;
4366 }
4367 inquiry = (idetape_inquiry_result_t *) pc.buffer;
4368 memcpy(tape->vendor_id, inquiry->vendor_id, 8);
4369 memcpy(tape->product_id, inquiry->product_id, 16);
4370 memcpy(tape->firmware_revision, inquiry->revision_level, 4);
4371 ide_fixstring(tape->vendor_id, 10, 0);
4372 ide_fixstring(tape->product_id, 18, 0);
4373 ide_fixstring(tape->firmware_revision, 6, 0);
4374 r = tape->firmware_revision;
4375 if (*(r + 1) == '.')
4376 tape->firmware_revision_num = (*r - '0') * 100 + (*(r + 2) - '0') * 10 + *(r + 3) - '0';
4377 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);
4378}
4379
4380/*
4381 * idetape_get_mode_sense_results asks the tape about its various
4382 * parameters. In particular, we will adjust our data transfer buffer
4383 * size to the recommended value as returned by the tape.
4384 */
4385static void idetape_get_mode_sense_results (ide_drive_t *drive)
4386{
4387 idetape_tape_t *tape = drive->driver_data;
4388 idetape_pc_t pc;
4389 idetape_mode_parameter_header_t *header;
4390 idetape_capabilities_page_t *capabilities;
4391
4392 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
4393 if (idetape_queue_pc_tail(drive, &pc)) {
4394 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming some default values\n");
4395 tape->tape_block_size = 512;
4396 tape->capabilities.ctl = 52;
4397 tape->capabilities.speed = 450;
4398 tape->capabilities.buffer_size = 6 * 52;
4399 return;
4400 }
4401 header = (idetape_mode_parameter_header_t *) pc.buffer;
4402 capabilities = (idetape_capabilities_page_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t) + header->bdl);
4403
4404 capabilities->max_speed = ntohs(capabilities->max_speed);
4405 capabilities->ctl = ntohs(capabilities->ctl);
4406 capabilities->speed = ntohs(capabilities->speed);
4407 capabilities->buffer_size = ntohs(capabilities->buffer_size);
4408
4409 if (!capabilities->speed) {
4410 printk(KERN_INFO "ide-tape: %s: overriding capabilities->speed (assuming 650KB/sec)\n", drive->name);
4411 capabilities->speed = 650;
4412 }
4413 if (!capabilities->max_speed) {
4414 printk(KERN_INFO "ide-tape: %s: overriding capabilities->max_speed (assuming 650KB/sec)\n", drive->name);
4415 capabilities->max_speed = 650;
4416 }
4417
4418 tape->capabilities = *capabilities; /* Save us a copy */
4419 if (capabilities->blk512)
4420 tape->tape_block_size = 512;
4421 else if (capabilities->blk1024)
4422 tape->tape_block_size = 1024;
4423
4424#if IDETAPE_DEBUG_INFO
4425 printk(KERN_INFO "ide-tape: Dumping the results of the MODE SENSE packet command\n");
4426 printk(KERN_INFO "ide-tape: Mode Parameter Header:\n");
4427 printk(KERN_INFO "ide-tape: Mode Data Length - %d\n",header->mode_data_length);
4428 printk(KERN_INFO "ide-tape: Medium Type - %d\n",header->medium_type);
4429 printk(KERN_INFO "ide-tape: Device Specific Parameter - %d\n",header->dsp);
4430 printk(KERN_INFO "ide-tape: Block Descriptor Length - %d\n",header->bdl);
4431
4432 printk(KERN_INFO "ide-tape: Capabilities and Mechanical Status Page:\n");
4433 printk(KERN_INFO "ide-tape: Page code - %d\n",capabilities->page_code);
4434 printk(KERN_INFO "ide-tape: Page length - %d\n",capabilities->page_length);
4435 printk(KERN_INFO "ide-tape: Read only - %s\n",capabilities->ro ? "Yes":"No");
4436 printk(KERN_INFO "ide-tape: Supports reverse space - %s\n",capabilities->sprev ? "Yes":"No");
4437 printk(KERN_INFO "ide-tape: Supports erase initiated formatting - %s\n",capabilities->efmt ? "Yes":"No");
4438 printk(KERN_INFO "ide-tape: Supports QFA two Partition format - %s\n",capabilities->qfa ? "Yes":"No");
4439 printk(KERN_INFO "ide-tape: Supports locking the medium - %s\n",capabilities->lock ? "Yes":"No");
4440 printk(KERN_INFO "ide-tape: The volume is currently locked - %s\n",capabilities->locked ? "Yes":"No");
4441 printk(KERN_INFO "ide-tape: The device defaults in the prevent state - %s\n",capabilities->prevent ? "Yes":"No");
4442 printk(KERN_INFO "ide-tape: Supports ejecting the medium - %s\n",capabilities->eject ? "Yes":"No");
4443 printk(KERN_INFO "ide-tape: Supports error correction - %s\n",capabilities->ecc ? "Yes":"No");
4444 printk(KERN_INFO "ide-tape: Supports data compression - %s\n",capabilities->cmprs ? "Yes":"No");
4445 printk(KERN_INFO "ide-tape: Supports 512 bytes block size - %s\n",capabilities->blk512 ? "Yes":"No");
4446 printk(KERN_INFO "ide-tape: Supports 1024 bytes block size - %s\n",capabilities->blk1024 ? "Yes":"No");
4447 printk(KERN_INFO "ide-tape: Supports 32768 bytes block size / Restricted byte count for PIO transfers - %s\n",capabilities->blk32768 ? "Yes":"No");
4448 printk(KERN_INFO "ide-tape: Maximum supported speed in KBps - %d\n",capabilities->max_speed);
4449 printk(KERN_INFO "ide-tape: Continuous transfer limits in blocks - %d\n",capabilities->ctl);
4450 printk(KERN_INFO "ide-tape: Current speed in KBps - %d\n",capabilities->speed);
4451 printk(KERN_INFO "ide-tape: Buffer size - %d\n",capabilities->buffer_size*512);
4452#endif /* IDETAPE_DEBUG_INFO */
4453}
4454
4455/*
4456 * ide_get_blocksize_from_block_descriptor does a mode sense page 0 with block descriptor
4457 * and if it succeeds sets the tape block size with the reported value
4458 */
4459static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive)
4460{
4461
4462 idetape_tape_t *tape = drive->driver_data;
4463 idetape_pc_t pc;
4464 idetape_mode_parameter_header_t *header;
4465 idetape_parameter_block_descriptor_t *block_descrp;
4466
4467 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
4468 if (idetape_queue_pc_tail(drive, &pc)) {
4469 printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
4470 if (tape->tape_block_size == 0) {
4471 printk(KERN_WARNING "ide-tape: Cannot deal with zero block size, assume 32k\n");
4472 tape->tape_block_size = 32768;
4473 }
4474 return;
4475 }
4476 header = (idetape_mode_parameter_header_t *) pc.buffer;
4477 block_descrp = (idetape_parameter_block_descriptor_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t));
4478 tape->tape_block_size =( block_descrp->length[0]<<16) + (block_descrp->length[1]<<8) + block_descrp->length[2];
4479 tape->drv_write_prot = (header->dsp & 0x80) >> 7;
4480
4481#if IDETAPE_DEBUG_INFO
4482 printk(KERN_INFO "ide-tape: Adjusted block size - %d\n", tape->tape_block_size);
4483#endif /* IDETAPE_DEBUG_INFO */
4484}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02004485
4486#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487static void idetape_add_settings (ide_drive_t *drive)
4488{
4489 idetape_tape_t *tape = drive->driver_data;
4490
4491/*
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02004492 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 */
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02004494 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff, 1, 2, &tape->capabilities.buffer_size, NULL);
4495 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->min_pipeline, NULL);
4496 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_stages, NULL);
4497 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_pipeline, NULL);
4498 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages, NULL);
4499 ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_pending_stages, NULL);
4500 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff, 1, 1, &tape->capabilities.speed, NULL);
4501 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1024, &tape->stage_size, NULL);
4502 ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_frequency, NULL);
4503 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
4504 ide_add_setting(drive, "pipeline_head_speed_c",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed, NULL);
4505 ide_add_setting(drive, "pipeline_head_speed_u",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->uncontrolled_pipeline_head_speed,NULL);
4506 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->avg_speed, NULL);
4507 ide_add_setting(drive, "debug_level", SETTING_RW, TYPE_INT, 0, 0xffff, 1, 1, &tape->debug_level, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02004509#else
4510static inline void idetape_add_settings(ide_drive_t *drive) { ; }
4511#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512
4513/*
4514 * ide_setup is called to:
4515 *
4516 * 1. Initialize our various state variables.
4517 * 2. Ask the tape for its capabilities.
4518 * 3. Allocate a buffer which will be used for data
4519 * transfer. The buffer size is chosen based on
4520 * the recommendation which we received in step (2).
4521 *
4522 * Note that at this point ide.c already assigned us an irq, so that
4523 * we can queue requests here and wait for their completion.
4524 */
4525static void idetape_setup (ide_drive_t *drive, idetape_tape_t *tape, int minor)
4526{
4527 unsigned long t1, tmid, tn, t;
4528 int speed;
4529 struct idetape_id_gcw gcw;
4530 int stage_size;
4531 struct sysinfo si;
4532
4533 spin_lock_init(&tape->spinlock);
4534 drive->dsc_overlap = 1;
Bartlomiej Zolnierkiewicz4166c192008-02-01 23:09:30 +01004535 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
4536 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
4537 tape->name);
4538 drive->dsc_overlap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 /* Seagate Travan drives do not support DSC overlap. */
4541 if (strstr(drive->id->model, "Seagate STT3401"))
4542 drive->dsc_overlap = 0;
4543 tape->minor = minor;
4544 tape->name[0] = 'h';
4545 tape->name[1] = 't';
4546 tape->name[2] = '0' + minor;
4547 tape->chrdev_direction = idetape_direction_none;
4548 tape->pc = tape->pc_stack;
4549 tape->max_insert_speed = 10000;
4550 tape->speed_control = 1;
4551 *((unsigned short *) &gcw) = drive->id->config;
4552 if (gcw.drq_type == 1)
4553 set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags);
4554
4555 tape->min_pipeline = tape->max_pipeline = tape->max_stages = 10;
4556
4557 idetape_get_inquiry_results(drive);
4558 idetape_get_mode_sense_results(drive);
4559 idetape_get_blocksize_from_block_descriptor(drive);
4560 tape->user_bs_factor = 1;
4561 tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
4562 while (tape->stage_size > 0xffff) {
4563 printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
4564 tape->capabilities.ctl /= 2;
4565 tape->stage_size = tape->capabilities.ctl * tape->tape_block_size;
4566 }
4567 stage_size = tape->stage_size;
4568 tape->pages_per_stage = stage_size / PAGE_SIZE;
4569 if (stage_size % PAGE_SIZE) {
4570 tape->pages_per_stage++;
4571 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE;
4572 }
4573
4574 /*
4575 * Select the "best" DSC read/write polling frequency
4576 * and pipeline size.
4577 */
4578 speed = max(tape->capabilities.speed, tape->capabilities.max_speed);
4579
4580 tape->max_stages = speed * 1000 * 10 / tape->stage_size;
4581
4582 /*
4583 * Limit memory use for pipeline to 10% of physical memory
4584 */
4585 si_meminfo(&si);
4586 if (tape->max_stages * tape->stage_size > si.totalram * si.mem_unit / 10)
4587 tape->max_stages = si.totalram * si.mem_unit / (10 * tape->stage_size);
4588 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES);
4589 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES);
4590 tape->max_pipeline = min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES);
4591 if (tape->max_stages == 0)
4592 tape->max_stages = tape->min_pipeline = tape->max_pipeline = 1;
4593
4594 t1 = (tape->stage_size * HZ) / (speed * 1000);
4595 tmid = (tape->capabilities.buffer_size * 32 * HZ) / (speed * 125);
4596 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000);
4597
4598 if (tape->max_stages)
4599 t = tn;
4600 else
4601 t = t1;
4602
4603 /*
4604 * Ensure that the number we got makes sense; limit
4605 * it within IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
4606 */
4607 tape->best_dsc_rw_frequency = max_t(unsigned long, min_t(unsigned long, t, IDETAPE_DSC_RW_MAX), IDETAPE_DSC_RW_MIN);
4608 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
4609 "%dkB pipeline, %lums tDSC%s\n",
4610 drive->name, tape->name, tape->capabilities.speed,
4611 (tape->capabilities.buffer_size * 512) / tape->stage_size,
4612 tape->stage_size / 1024,
4613 tape->max_stages * tape->stage_size / 1024,
4614 tape->best_dsc_rw_frequency * 1000 / HZ,
4615 drive->using_dma ? ", DMA":"");
4616
4617 idetape_add_settings(drive);
4618}
4619
Russell King4031bbe2006-01-06 11:41:00 +00004620static void ide_tape_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621{
4622 idetape_tape_t *tape = drive->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02004624 ide_proc_unregister_driver(drive, tape->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625
4626 ide_unregister_region(tape->disk);
4627
4628 ide_tape_put(tape);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629}
4630
4631static void ide_tape_release(struct kref *kref)
4632{
4633 struct ide_tape_obj *tape = to_ide_tape(kref);
4634 ide_drive_t *drive = tape->drive;
4635 struct gendisk *g = tape->disk;
4636
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02004637 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size);
4638
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 drive->dsc_overlap = 0;
4640 drive->driver_data = NULL;
Tony Jonesdbc12722007-09-25 02:03:03 +02004641 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
4642 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor + 128));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 idetape_devs[tape->minor] = NULL;
4644 g->private_data = NULL;
4645 put_disk(g);
4646 kfree(tape);
4647}
4648
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02004649#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650static int proc_idetape_read_name
4651 (char *page, char **start, off_t off, int count, int *eof, void *data)
4652{
4653 ide_drive_t *drive = (ide_drive_t *) data;
4654 idetape_tape_t *tape = drive->driver_data;
4655 char *out = page;
4656 int len;
4657
4658 len = sprintf(out, "%s\n", tape->name);
4659 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
4660}
4661
4662static ide_proc_entry_t idetape_proc[] = {
4663 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
4664 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL },
4665 { NULL, 0, NULL, NULL }
4666};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667#endif
4668
Russell King4031bbe2006-01-06 11:41:00 +00004669static int ide_tape_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671static ide_driver_t idetape_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02004672 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01004673 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02004674 .name = "ide-tape",
4675 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02004676 },
Russell King4031bbe2006-01-06 11:41:00 +00004677 .probe = ide_tape_probe,
4678 .remove = ide_tape_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 .version = IDETAPE_VERSION,
4680 .media = ide_tape,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 .supports_dsc_overlap = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 .do_request = idetape_do_request,
4683 .end_request = idetape_end_request,
4684 .error = __ide_error,
4685 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02004686#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 .proc = idetape_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02004688#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689};
4690
4691/*
4692 * Our character device supporting functions, passed to register_chrdev.
4693 */
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08004694static const struct file_operations idetape_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 .owner = THIS_MODULE,
4696 .read = idetape_chrdev_read,
4697 .write = idetape_chrdev_write,
4698 .ioctl = idetape_chrdev_ioctl,
4699 .open = idetape_chrdev_open,
4700 .release = idetape_chrdev_release,
4701};
4702
4703static int idetape_open(struct inode *inode, struct file *filp)
4704{
4705 struct gendisk *disk = inode->i_bdev->bd_disk;
4706 struct ide_tape_obj *tape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707
4708 if (!(tape = ide_tape_get(disk)))
4709 return -ENXIO;
4710
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 return 0;
4712}
4713
4714static int idetape_release(struct inode *inode, struct file *filp)
4715{
4716 struct gendisk *disk = inode->i_bdev->bd_disk;
4717 struct ide_tape_obj *tape = ide_tape_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718
4719 ide_tape_put(tape);
4720
4721 return 0;
4722}
4723
4724static int idetape_ioctl(struct inode *inode, struct file *file,
4725 unsigned int cmd, unsigned long arg)
4726{
4727 struct block_device *bdev = inode->i_bdev;
4728 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk);
4729 ide_drive_t *drive = tape->drive;
4730 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
4731 if (err == -EINVAL)
4732 err = idetape_blkdev_ioctl(drive, cmd, arg);
4733 return err;
4734}
4735
4736static struct block_device_operations idetape_block_ops = {
4737 .owner = THIS_MODULE,
4738 .open = idetape_open,
4739 .release = idetape_release,
4740 .ioctl = idetape_ioctl,
4741};
4742
Russell King4031bbe2006-01-06 11:41:00 +00004743static int ide_tape_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744{
4745 idetape_tape_t *tape;
4746 struct gendisk *g;
4747 int minor;
4748
4749 if (!strstr("ide-tape", drive->driver_req))
4750 goto failed;
4751 if (!drive->present)
4752 goto failed;
4753 if (drive->media != ide_tape)
4754 goto failed;
4755 if (!idetape_identify_device (drive)) {
4756 printk(KERN_ERR "ide-tape: %s: not supported by this version of ide-tape\n", drive->name);
4757 goto failed;
4758 }
4759 if (drive->scsi) {
4760 printk("ide-tape: passing drive %s to ide-scsi emulation.\n", drive->name);
4761 goto failed;
4762 }
4763 if (strstr(drive->id->model, "OnStream DI-")) {
4764 printk(KERN_WARNING "ide-tape: Use drive %s with ide-scsi emulation and osst.\n", drive->name);
4765 printk(KERN_WARNING "ide-tape: OnStream support will be removed soon from ide-tape!\n");
4766 }
Robert P. J. Day5cbded52006-12-13 00:35:56 -08004767 tape = kzalloc(sizeof (idetape_tape_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 if (tape == NULL) {
4769 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape structure\n", drive->name);
4770 goto failed;
4771 }
4772
4773 g = alloc_disk(1 << PARTN_BITS);
4774 if (!g)
4775 goto out_free_tape;
4776
4777 ide_init_disk(g, drive);
4778
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02004779 ide_proc_register_driver(drive, &idetape_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 kref_init(&tape->kref);
4782
4783 tape->drive = drive;
4784 tape->driver = &idetape_driver;
4785 tape->disk = g;
4786
4787 g->private_data = &tape->driver;
4788
4789 drive->driver_data = tape;
4790
Arjan van de Vencf8b8972006-03-23 03:00:45 -08004791 mutex_lock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 for (minor = 0; idetape_devs[minor]; minor++)
4793 ;
4794 idetape_devs[minor] = tape;
Arjan van de Vencf8b8972006-03-23 03:00:45 -08004795 mutex_unlock(&idetape_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796
4797 idetape_setup(drive, tape, minor);
4798
Tony Jonesdbc12722007-09-25 02:03:03 +02004799 device_create(idetape_sysfs_class, &drive->gendev,
4800 MKDEV(IDETAPE_MAJOR, minor), "%s", tape->name);
4801 device_create(idetape_sysfs_class, &drive->gendev,
4802 MKDEV(IDETAPE_MAJOR, minor + 128), "n%s", tape->name);
Will Dysond5dee802005-09-16 02:55:07 -07004803
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 g->fops = &idetape_block_ops;
4805 ide_register_region(g);
4806
4807 return 0;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02004808
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809out_free_tape:
4810 kfree(tape);
4811failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02004812 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813}
4814
4815MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
4816MODULE_LICENSE("GPL");
4817
4818static void __exit idetape_exit (void)
4819{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02004820 driver_unregister(&idetape_driver.gen_driver);
Will Dysond5dee802005-09-16 02:55:07 -07004821 class_destroy(idetape_sysfs_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 unregister_chrdev(IDETAPE_MAJOR, "ht");
4823}
4824
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01004825static int __init idetape_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826{
Will Dysond5dee802005-09-16 02:55:07 -07004827 int error = 1;
4828 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
4829 if (IS_ERR(idetape_sysfs_class)) {
4830 idetape_sysfs_class = NULL;
4831 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
4832 error = -EBUSY;
4833 goto out;
4834 }
4835
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
4837 printk(KERN_ERR "ide-tape: Failed to register character device interface\n");
Will Dysond5dee802005-09-16 02:55:07 -07004838 error = -EBUSY;
4839 goto out_free_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 }
Will Dysond5dee802005-09-16 02:55:07 -07004841
4842 error = driver_register(&idetape_driver.gen_driver);
4843 if (error)
4844 goto out_free_driver;
4845
4846 return 0;
4847
4848out_free_driver:
4849 driver_unregister(&idetape_driver.gen_driver);
4850out_free_class:
4851 class_destroy(idetape_sysfs_class);
4852out:
4853 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854}
4855
Kay Sievers263756e2005-12-12 18:03:44 +01004856MODULE_ALIAS("ide:*m-tape*");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857module_init(idetape_init);
4858module_exit(idetape_exit);
4859MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);