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