Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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