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 v2.6.23-rc6 4948 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/module.h> 428#include <linux/types.h> 429#include <linux/string.h> 430#include <linux/kernel.h> 431#include <linux/delay.h> 432#include <linux/timer.h> 433#include <linux/mm.h> 434#include <linux/interrupt.h> 435#include <linux/jiffies.h> 436#include <linux/major.h> 437#include <linux/errno.h> 438#include <linux/genhd.h> 439#include <linux/slab.h> 440#include <linux/pci.h> 441#include <linux/ide.h> 442#include <linux/smp_lock.h> 443#include <linux/completion.h> 444#include <linux/bitops.h> 445#include <linux/mutex.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 u32 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 DEFINE_MUTEX(idetape_ref_mutex); 1015 1016static struct class *idetape_sysfs_class; 1017 1018#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref) 1019 1020#define ide_tape_g(disk) \ 1021 container_of((disk)->private_data, struct ide_tape_obj, driver) 1022 1023static struct ide_tape_obj *ide_tape_get(struct gendisk *disk) 1024{ 1025 struct ide_tape_obj *tape = NULL; 1026 1027 mutex_lock(&idetape_ref_mutex); 1028 tape = ide_tape_g(disk); 1029 if (tape) 1030 kref_get(&tape->kref); 1031 mutex_unlock(&idetape_ref_mutex); 1032 return tape; 1033} 1034 1035static void ide_tape_release(struct kref *); 1036 1037static void ide_tape_put(struct ide_tape_obj *tape) 1038{ 1039 mutex_lock(&idetape_ref_mutex); 1040 kref_put(&tape->kref, ide_tape_release); 1041 mutex_unlock(&idetape_ref_mutex); 1042} 1043 1044/* 1045 * Tape door status 1046 */ 1047#define DOOR_UNLOCKED 0 1048#define DOOR_LOCKED 1 1049#define DOOR_EXPLICITLY_LOCKED 2 1050 1051/* 1052 * Tape flag bits values. 1053 */ 1054#define IDETAPE_IGNORE_DSC 0 1055#define IDETAPE_ADDRESS_VALID 1 /* 0 When the tape position is unknown */ 1056#define IDETAPE_BUSY 2 /* Device already opened */ 1057#define IDETAPE_PIPELINE_ERROR 3 /* Error detected in a pipeline stage */ 1058#define IDETAPE_DETECT_BS 4 /* Attempt to auto-detect the current user block size */ 1059#define IDETAPE_FILEMARK 5 /* Currently on a filemark */ 1060#define IDETAPE_DRQ_INTERRUPT 6 /* DRQ interrupt device */ 1061#define IDETAPE_READ_ERROR 7 1062#define IDETAPE_PIPELINE_ACTIVE 8 /* pipeline active */ 1063/* 0 = no tape is loaded, so we don't rewind after ejecting */ 1064#define IDETAPE_MEDIUM_PRESENT 9 1065 1066/* 1067 * Supported ATAPI tape drives packet commands 1068 */ 1069#define IDETAPE_TEST_UNIT_READY_CMD 0x00 1070#define IDETAPE_REWIND_CMD 0x01 1071#define IDETAPE_REQUEST_SENSE_CMD 0x03 1072#define IDETAPE_READ_CMD 0x08 1073#define IDETAPE_WRITE_CMD 0x0a 1074#define IDETAPE_WRITE_FILEMARK_CMD 0x10 1075#define IDETAPE_SPACE_CMD 0x11 1076#define IDETAPE_INQUIRY_CMD 0x12 1077#define IDETAPE_ERASE_CMD 0x19 1078#define IDETAPE_MODE_SENSE_CMD 0x1a 1079#define IDETAPE_MODE_SELECT_CMD 0x15 1080#define IDETAPE_LOAD_UNLOAD_CMD 0x1b 1081#define IDETAPE_PREVENT_CMD 0x1e 1082#define IDETAPE_LOCATE_CMD 0x2b 1083#define IDETAPE_READ_POSITION_CMD 0x34 1084#define IDETAPE_READ_BUFFER_CMD 0x3c 1085#define IDETAPE_SET_SPEED_CMD 0xbb 1086 1087/* 1088 * Some defines for the READ BUFFER command 1089 */ 1090#define IDETAPE_RETRIEVE_FAULTY_BLOCK 6 1091 1092/* 1093 * Some defines for the SPACE command 1094 */ 1095#define IDETAPE_SPACE_OVER_FILEMARK 1 1096#define IDETAPE_SPACE_TO_EOD 3 1097 1098/* 1099 * Some defines for the LOAD UNLOAD command 1100 */ 1101#define IDETAPE_LU_LOAD_MASK 1 1102#define IDETAPE_LU_RETENSION_MASK 2 1103#define IDETAPE_LU_EOT_MASK 4 1104 1105/* 1106 * Special requests for our block device strategy routine. 1107 * 1108 * In order to service a character device command, we add special 1109 * requests to the tail of our block device request queue and wait 1110 * for their completion. 1111 */ 1112 1113enum { 1114 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */ 1115 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */ 1116 REQ_IDETAPE_READ = (1 << 2), 1117 REQ_IDETAPE_WRITE = (1 << 3), 1118 REQ_IDETAPE_READ_BUFFER = (1 << 4), 1119}; 1120 1121/* 1122 * Error codes which are returned in rq->errors to the higher part 1123 * of the driver. 1124 */ 1125#define IDETAPE_ERROR_GENERAL 101 1126#define IDETAPE_ERROR_FILEMARK 102 1127#define IDETAPE_ERROR_EOD 103 1128 1129/* 1130 * The following is used to format the general configuration word of 1131 * the ATAPI IDENTIFY DEVICE command. 1132 */ 1133struct idetape_id_gcw { 1134 unsigned packet_size :2; /* Packet Size */ 1135 unsigned reserved234 :3; /* Reserved */ 1136 unsigned drq_type :2; /* Command packet DRQ type */ 1137 unsigned removable :1; /* Removable media */ 1138 unsigned device_type :5; /* Device type */ 1139 unsigned reserved13 :1; /* Reserved */ 1140 unsigned protocol :2; /* Protocol type */ 1141}; 1142 1143/* 1144 * INQUIRY packet command - Data Format (From Table 6-8 of QIC-157C) 1145 */ 1146typedef struct { 1147 unsigned device_type :5; /* Peripheral Device Type */ 1148 unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */ 1149 unsigned reserved1_6t0 :7; /* Reserved */ 1150 unsigned rmb :1; /* Removable Medium Bit */ 1151 unsigned ansi_version :3; /* ANSI Version */ 1152 unsigned ecma_version :3; /* ECMA Version */ 1153 unsigned iso_version :2; /* ISO Version */ 1154 unsigned response_format :4; /* Response Data Format */ 1155 unsigned reserved3_45 :2; /* Reserved */ 1156 unsigned reserved3_6 :1; /* TrmIOP - Reserved */ 1157 unsigned reserved3_7 :1; /* AENC - Reserved */ 1158 __u8 additional_length; /* Additional Length (total_length-4) */ 1159 __u8 rsv5, rsv6, rsv7; /* Reserved */ 1160 __u8 vendor_id[8]; /* Vendor Identification */ 1161 __u8 product_id[16]; /* Product Identification */ 1162 __u8 revision_level[4]; /* Revision Level */ 1163 __u8 vendor_specific[20]; /* Vendor Specific - Optional */ 1164 __u8 reserved56t95[40]; /* Reserved - Optional */ 1165 /* Additional information may be returned */ 1166} idetape_inquiry_result_t; 1167 1168/* 1169 * READ POSITION packet command - Data Format (From Table 6-57) 1170 */ 1171typedef struct { 1172 unsigned reserved0_10 :2; /* Reserved */ 1173 unsigned bpu :1; /* Block Position Unknown */ 1174 unsigned reserved0_543 :3; /* Reserved */ 1175 unsigned eop :1; /* End Of Partition */ 1176 unsigned bop :1; /* Beginning Of Partition */ 1177 u8 partition; /* Partition Number */ 1178 u8 reserved2, reserved3; /* Reserved */ 1179 u32 first_block; /* First Block Location */ 1180 u32 last_block; /* Last Block Location (Optional) */ 1181 u8 reserved12; /* Reserved */ 1182 u8 blocks_in_buffer[3]; /* Blocks In Buffer - (Optional) */ 1183 u32 bytes_in_buffer; /* Bytes In Buffer (Optional) */ 1184} idetape_read_position_result_t; 1185 1186/* 1187 * Follows structures which are related to the SELECT SENSE / MODE SENSE 1188 * packet commands. Those packet commands are still not supported 1189 * by ide-tape. 1190 */ 1191#define IDETAPE_BLOCK_DESCRIPTOR 0 1192#define IDETAPE_CAPABILITIES_PAGE 0x2a 1193#define IDETAPE_PARAMTR_PAGE 0x2b /* Onstream DI-x0 only */ 1194#define IDETAPE_BLOCK_SIZE_PAGE 0x30 1195#define IDETAPE_BUFFER_FILLING_PAGE 0x33 1196 1197/* 1198 * Mode Parameter Header for the MODE SENSE packet command 1199 */ 1200typedef struct { 1201 __u8 mode_data_length; /* Length of the following data transfer */ 1202 __u8 medium_type; /* Medium Type */ 1203 __u8 dsp; /* Device Specific Parameter */ 1204 __u8 bdl; /* Block Descriptor Length */ 1205#if 0 1206 /* data transfer page */ 1207 __u8 page_code :6; 1208 __u8 reserved0_6 :1; 1209 __u8 ps :1; /* parameters saveable */ 1210 __u8 page_length; /* page Length == 0x02 */ 1211 __u8 reserved2; 1212 __u8 read32k :1; /* 32k blk size (data only) */ 1213 __u8 read32k5 :1; /* 32.5k blk size (data&AUX) */ 1214 __u8 reserved3_23 :2; 1215 __u8 write32k :1; /* 32k blk size (data only) */ 1216 __u8 write32k5 :1; /* 32.5k blk size (data&AUX) */ 1217 __u8 reserved3_6 :1; 1218 __u8 streaming :1; /* streaming mode enable */ 1219#endif 1220} idetape_mode_parameter_header_t; 1221 1222/* 1223 * Mode Parameter Block Descriptor the MODE SENSE packet command 1224 * 1225 * Support for block descriptors is optional. 1226 */ 1227typedef struct { 1228 __u8 density_code; /* Medium density code */ 1229 __u8 blocks[3]; /* Number of blocks */ 1230 __u8 reserved4; /* Reserved */ 1231 __u8 length[3]; /* Block Length */ 1232} idetape_parameter_block_descriptor_t; 1233 1234/* 1235 * The Data Compression Page, as returned by the MODE SENSE packet command. 1236 */ 1237typedef struct { 1238 unsigned page_code :6; /* Page Code - Should be 0xf */ 1239 unsigned reserved0 :1; /* Reserved */ 1240 unsigned ps :1; 1241 __u8 page_length; /* Page Length - Should be 14 */ 1242 unsigned reserved2 :6; /* Reserved */ 1243 unsigned dcc :1; /* Data Compression Capable */ 1244 unsigned dce :1; /* Data Compression Enable */ 1245 unsigned reserved3 :5; /* Reserved */ 1246 unsigned red :2; /* Report Exception on Decompression */ 1247 unsigned dde :1; /* Data Decompression Enable */ 1248 __u32 ca; /* Compression Algorithm */ 1249 __u32 da; /* Decompression Algorithm */ 1250 __u8 reserved[4]; /* Reserved */ 1251} idetape_data_compression_page_t; 1252 1253/* 1254 * The Medium Partition Page, as returned by the MODE SENSE packet command. 1255 */ 1256typedef struct { 1257 unsigned page_code :6; /* Page Code - Should be 0x11 */ 1258 unsigned reserved1_6 :1; /* Reserved */ 1259 unsigned ps :1; 1260 __u8 page_length; /* Page Length - Should be 6 */ 1261 __u8 map; /* Maximum Additional Partitions - Should be 0 */ 1262 __u8 apd; /* Additional Partitions Defined - Should be 0 */ 1263 unsigned reserved4_012 :3; /* Reserved */ 1264 unsigned psum :2; /* Should be 0 */ 1265 unsigned idp :1; /* Should be 0 */ 1266 unsigned sdp :1; /* Should be 0 */ 1267 unsigned fdp :1; /* Fixed Data Partitions */ 1268 __u8 mfr; /* Medium Format Recognition */ 1269 __u8 reserved[2]; /* Reserved */ 1270} idetape_medium_partition_page_t; 1271 1272/* 1273 * Run time configurable parameters. 1274 */ 1275typedef struct { 1276 int dsc_rw_frequency; 1277 int dsc_media_access_frequency; 1278 int nr_stages; 1279} idetape_config_t; 1280 1281/* 1282 * The variables below are used for the character device interface. 1283 * Additional state variables are defined in our ide_drive_t structure. 1284 */ 1285static struct ide_tape_obj * idetape_devs[MAX_HWIFS * MAX_DRIVES]; 1286 1287#define ide_tape_f(file) ((file)->private_data) 1288 1289static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i) 1290{ 1291 struct ide_tape_obj *tape = NULL; 1292 1293 mutex_lock(&idetape_ref_mutex); 1294 tape = idetape_devs[i]; 1295 if (tape) 1296 kref_get(&tape->kref); 1297 mutex_unlock(&idetape_ref_mutex); 1298 return tape; 1299} 1300 1301/* 1302 * Function declarations 1303 * 1304 */ 1305static int idetape_chrdev_release (struct inode *inode, struct file *filp); 1306static void idetape_write_release (ide_drive_t *drive, unsigned int minor); 1307 1308/* 1309 * Too bad. The drive wants to send us data which we are not ready to accept. 1310 * Just throw it away. 1311 */ 1312static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount) 1313{ 1314 while (bcount--) 1315 (void) HWIF(drive)->INB(IDE_DATA_REG); 1316} 1317 1318static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount) 1319{ 1320 struct idetape_bh *bh = pc->bh; 1321 int count; 1322 1323 while (bcount) { 1324#if IDETAPE_DEBUG_BUGS 1325 if (bh == NULL) { 1326 printk(KERN_ERR "ide-tape: bh == NULL in " 1327 "idetape_input_buffers\n"); 1328 idetape_discard_data(drive, bcount); 1329 return; 1330 } 1331#endif /* IDETAPE_DEBUG_BUGS */ 1332 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), bcount); 1333 HWIF(drive)->atapi_input_bytes(drive, bh->b_data + atomic_read(&bh->b_count), count); 1334 bcount -= count; 1335 atomic_add(count, &bh->b_count); 1336 if (atomic_read(&bh->b_count) == bh->b_size) { 1337 bh = bh->b_reqnext; 1338 if (bh) 1339 atomic_set(&bh->b_count, 0); 1340 } 1341 } 1342 pc->bh = bh; 1343} 1344 1345static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount) 1346{ 1347 struct idetape_bh *bh = pc->bh; 1348 int count; 1349 1350 while (bcount) { 1351#if IDETAPE_DEBUG_BUGS 1352 if (bh == NULL) { 1353 printk(KERN_ERR "ide-tape: bh == NULL in " 1354 "idetape_output_buffers\n"); 1355 return; 1356 } 1357#endif /* IDETAPE_DEBUG_BUGS */ 1358 count = min((unsigned int)pc->b_count, (unsigned int)bcount); 1359 HWIF(drive)->atapi_output_bytes(drive, pc->b_data, count); 1360 bcount -= count; 1361 pc->b_data += count; 1362 pc->b_count -= count; 1363 if (!pc->b_count) { 1364 pc->bh = bh = bh->b_reqnext; 1365 if (bh) { 1366 pc->b_data = bh->b_data; 1367 pc->b_count = atomic_read(&bh->b_count); 1368 } 1369 } 1370 } 1371} 1372 1373static void idetape_update_buffers (idetape_pc_t *pc) 1374{ 1375 struct idetape_bh *bh = pc->bh; 1376 int count; 1377 unsigned int bcount = pc->actually_transferred; 1378 1379 if (test_bit(PC_WRITING, &pc->flags)) 1380 return; 1381 while (bcount) { 1382#if IDETAPE_DEBUG_BUGS 1383 if (bh == NULL) { 1384 printk(KERN_ERR "ide-tape: bh == NULL in " 1385 "idetape_update_buffers\n"); 1386 return; 1387 } 1388#endif /* IDETAPE_DEBUG_BUGS */ 1389 count = min((unsigned int)bh->b_size, (unsigned int)bcount); 1390 atomic_set(&bh->b_count, count); 1391 if (atomic_read(&bh->b_count) == bh->b_size) 1392 bh = bh->b_reqnext; 1393 bcount -= count; 1394 } 1395 pc->bh = bh; 1396} 1397 1398/* 1399 * idetape_next_pc_storage returns a pointer to a place in which we can 1400 * safely store a packet command, even though we intend to leave the 1401 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet 1402 * commands is allocated at initialization time. 1403 */ 1404static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive) 1405{ 1406 idetape_tape_t *tape = drive->driver_data; 1407 1408#if IDETAPE_DEBUG_LOG 1409 if (tape->debug_level >= 5) 1410 printk(KERN_INFO "ide-tape: pc_stack_index=%d\n", 1411 tape->pc_stack_index); 1412#endif /* IDETAPE_DEBUG_LOG */ 1413 if (tape->pc_stack_index == IDETAPE_PC_STACK) 1414 tape->pc_stack_index=0; 1415 return (&tape->pc_stack[tape->pc_stack_index++]); 1416} 1417 1418/* 1419 * idetape_next_rq_storage is used along with idetape_next_pc_storage. 1420 * Since we queue packet commands in the request queue, we need to 1421 * allocate a request, along with the allocation of a packet command. 1422 */ 1423 1424/************************************************************** 1425 * * 1426 * This should get fixed to use kmalloc(.., GFP_ATOMIC) * 1427 * followed later on by kfree(). -ml * 1428 * * 1429 **************************************************************/ 1430 1431static struct request *idetape_next_rq_storage (ide_drive_t *drive) 1432{ 1433 idetape_tape_t *tape = drive->driver_data; 1434 1435#if IDETAPE_DEBUG_LOG 1436 if (tape->debug_level >= 5) 1437 printk(KERN_INFO "ide-tape: rq_stack_index=%d\n", 1438 tape->rq_stack_index); 1439#endif /* IDETAPE_DEBUG_LOG */ 1440 if (tape->rq_stack_index == IDETAPE_PC_STACK) 1441 tape->rq_stack_index=0; 1442 return (&tape->rq_stack[tape->rq_stack_index++]); 1443} 1444 1445/* 1446 * idetape_init_pc initializes a packet command. 1447 */ 1448static void idetape_init_pc (idetape_pc_t *pc) 1449{ 1450 memset(pc->c, 0, 12); 1451 pc->retries = 0; 1452 pc->flags = 0; 1453 pc->request_transfer = 0; 1454 pc->buffer = pc->pc_buffer; 1455 pc->buffer_size = IDETAPE_PC_BUFFER_SIZE; 1456 pc->bh = NULL; 1457 pc->b_data = NULL; 1458} 1459 1460/* 1461 * idetape_analyze_error is called on each failed packet command retry 1462 * to analyze the request sense. We currently do not utilize this 1463 * information. 1464 */ 1465static void idetape_analyze_error (ide_drive_t *drive, idetape_request_sense_result_t *result) 1466{ 1467 idetape_tape_t *tape = drive->driver_data; 1468 idetape_pc_t *pc = tape->failed_pc; 1469 1470 tape->sense = *result; 1471 tape->sense_key = result->sense_key; 1472 tape->asc = result->asc; 1473 tape->ascq = result->ascq; 1474#if IDETAPE_DEBUG_LOG 1475 /* 1476 * Without debugging, we only log an error if we decided to 1477 * give up retrying. 1478 */ 1479 if (tape->debug_level >= 1) 1480 printk(KERN_INFO "ide-tape: pc = %x, sense key = %x, " 1481 "asc = %x, ascq = %x\n", 1482 pc->c[0], result->sense_key, 1483 result->asc, result->ascq); 1484#endif /* IDETAPE_DEBUG_LOG */ 1485 1486 /* 1487 * Correct pc->actually_transferred by asking the tape. 1488 */ 1489 if (test_bit(PC_DMA_ERROR, &pc->flags)) { 1490 pc->actually_transferred = pc->request_transfer - tape->tape_block_size * ntohl(get_unaligned(&result->information)); 1491 idetape_update_buffers(pc); 1492 } 1493 1494 /* 1495 * If error was the result of a zero-length read or write command, 1496 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives 1497 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes. 1498 */ 1499 if ((pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD) 1500 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) { /* length==0 */ 1501 if (result->sense_key == 5) { 1502 /* don't report an error, everything's ok */ 1503 pc->error = 0; 1504 /* don't retry read/write */ 1505 set_bit(PC_ABORT, &pc->flags); 1506 } 1507 } 1508 if (pc->c[0] == IDETAPE_READ_CMD && result->filemark) { 1509 pc->error = IDETAPE_ERROR_FILEMARK; 1510 set_bit(PC_ABORT, &pc->flags); 1511 } 1512 if (pc->c[0] == IDETAPE_WRITE_CMD) { 1513 if (result->eom || 1514 (result->sense_key == 0xd && result->asc == 0x0 && 1515 result->ascq == 0x2)) { 1516 pc->error = IDETAPE_ERROR_EOD; 1517 set_bit(PC_ABORT, &pc->flags); 1518 } 1519 } 1520 if (pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD) { 1521 if (result->sense_key == 8) { 1522 pc->error = IDETAPE_ERROR_EOD; 1523 set_bit(PC_ABORT, &pc->flags); 1524 } 1525 if (!test_bit(PC_ABORT, &pc->flags) && 1526 pc->actually_transferred) 1527 pc->retries = IDETAPE_MAX_PC_RETRIES + 1; 1528 } 1529} 1530 1531/* 1532 * idetape_active_next_stage will declare the next stage as "active". 1533 */ 1534static void idetape_active_next_stage (ide_drive_t *drive) 1535{ 1536 idetape_tape_t *tape = drive->driver_data; 1537 idetape_stage_t *stage = tape->next_stage; 1538 struct request *rq = &stage->rq; 1539 1540#if IDETAPE_DEBUG_LOG 1541 if (tape->debug_level >= 4) 1542 printk(KERN_INFO "ide-tape: Reached idetape_active_next_stage\n"); 1543#endif /* IDETAPE_DEBUG_LOG */ 1544#if IDETAPE_DEBUG_BUGS 1545 if (stage == NULL) { 1546 printk(KERN_ERR "ide-tape: bug: Trying to activate a non existing stage\n"); 1547 return; 1548 } 1549#endif /* IDETAPE_DEBUG_BUGS */ 1550 1551 rq->rq_disk = tape->disk; 1552 rq->buffer = NULL; 1553 rq->special = (void *)stage->bh; 1554 tape->active_data_request = rq; 1555 tape->active_stage = stage; 1556 tape->next_stage = stage->next; 1557} 1558 1559/* 1560 * idetape_increase_max_pipeline_stages is a part of the feedback 1561 * loop which tries to find the optimum number of stages. In the 1562 * feedback loop, we are starting from a minimum maximum number of 1563 * stages, and if we sense that the pipeline is empty, we try to 1564 * increase it, until we reach the user compile time memory limit. 1565 */ 1566static void idetape_increase_max_pipeline_stages (ide_drive_t *drive) 1567{ 1568 idetape_tape_t *tape = drive->driver_data; 1569 int increase = (tape->max_pipeline - tape->min_pipeline) / 10; 1570 1571#if IDETAPE_DEBUG_LOG 1572 if (tape->debug_level >= 4) 1573 printk (KERN_INFO "ide-tape: Reached idetape_increase_max_pipeline_stages\n"); 1574#endif /* IDETAPE_DEBUG_LOG */ 1575 1576 tape->max_stages += max(increase, 1); 1577 tape->max_stages = max(tape->max_stages, tape->min_pipeline); 1578 tape->max_stages = min(tape->max_stages, tape->max_pipeline); 1579} 1580 1581/* 1582 * idetape_kfree_stage calls kfree to completely free a stage, along with 1583 * its related buffers. 1584 */ 1585static void __idetape_kfree_stage (idetape_stage_t *stage) 1586{ 1587 struct idetape_bh *prev_bh, *bh = stage->bh; 1588 int size; 1589 1590 while (bh != NULL) { 1591 if (bh->b_data != NULL) { 1592 size = (int) bh->b_size; 1593 while (size > 0) { 1594 free_page((unsigned long) bh->b_data); 1595 size -= PAGE_SIZE; 1596 bh->b_data += PAGE_SIZE; 1597 } 1598 } 1599 prev_bh = bh; 1600 bh = bh->b_reqnext; 1601 kfree(prev_bh); 1602 } 1603 kfree(stage); 1604} 1605 1606static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage) 1607{ 1608 __idetape_kfree_stage(stage); 1609} 1610 1611/* 1612 * idetape_remove_stage_head removes tape->first_stage from the pipeline. 1613 * The caller should avoid race conditions. 1614 */ 1615static void idetape_remove_stage_head (ide_drive_t *drive) 1616{ 1617 idetape_tape_t *tape = drive->driver_data; 1618 idetape_stage_t *stage; 1619 1620#if IDETAPE_DEBUG_LOG 1621 if (tape->debug_level >= 4) 1622 printk(KERN_INFO "ide-tape: Reached idetape_remove_stage_head\n"); 1623#endif /* IDETAPE_DEBUG_LOG */ 1624#if IDETAPE_DEBUG_BUGS 1625 if (tape->first_stage == NULL) { 1626 printk(KERN_ERR "ide-tape: bug: tape->first_stage is NULL\n"); 1627 return; 1628 } 1629 if (tape->active_stage == tape->first_stage) { 1630 printk(KERN_ERR "ide-tape: bug: Trying to free our active pipeline stage\n"); 1631 return; 1632 } 1633#endif /* IDETAPE_DEBUG_BUGS */ 1634 stage = tape->first_stage; 1635 tape->first_stage = stage->next; 1636 idetape_kfree_stage(tape, stage); 1637 tape->nr_stages--; 1638 if (tape->first_stage == NULL) { 1639 tape->last_stage = NULL; 1640#if IDETAPE_DEBUG_BUGS 1641 if (tape->next_stage != NULL) 1642 printk(KERN_ERR "ide-tape: bug: tape->next_stage != NULL\n"); 1643 if (tape->nr_stages) 1644 printk(KERN_ERR "ide-tape: bug: nr_stages should be 0 now\n"); 1645#endif /* IDETAPE_DEBUG_BUGS */ 1646 } 1647} 1648 1649/* 1650 * This will free all the pipeline stages starting from new_last_stage->next 1651 * to the end of the list, and point tape->last_stage to new_last_stage. 1652 */ 1653static void idetape_abort_pipeline(ide_drive_t *drive, 1654 idetape_stage_t *new_last_stage) 1655{ 1656 idetape_tape_t *tape = drive->driver_data; 1657 idetape_stage_t *stage = new_last_stage->next; 1658 idetape_stage_t *nstage; 1659 1660#if IDETAPE_DEBUG_LOG 1661 if (tape->debug_level >= 4) 1662 printk(KERN_INFO "ide-tape: %s: idetape_abort_pipeline called\n", tape->name); 1663#endif 1664 while (stage) { 1665 nstage = stage->next; 1666 idetape_kfree_stage(tape, stage); 1667 --tape->nr_stages; 1668 --tape->nr_pending_stages; 1669 stage = nstage; 1670 } 1671 if (new_last_stage) 1672 new_last_stage->next = NULL; 1673 tape->last_stage = new_last_stage; 1674 tape->next_stage = NULL; 1675} 1676 1677/* 1678 * idetape_end_request is used to finish servicing a request, and to 1679 * insert a pending pipeline request into the main device queue. 1680 */ 1681static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects) 1682{ 1683 struct request *rq = HWGROUP(drive)->rq; 1684 idetape_tape_t *tape = drive->driver_data; 1685 unsigned long flags; 1686 int error; 1687 int remove_stage = 0; 1688 idetape_stage_t *active_stage; 1689 1690#if IDETAPE_DEBUG_LOG 1691 if (tape->debug_level >= 4) 1692 printk(KERN_INFO "ide-tape: Reached idetape_end_request\n"); 1693#endif /* IDETAPE_DEBUG_LOG */ 1694 1695 switch (uptodate) { 1696 case 0: error = IDETAPE_ERROR_GENERAL; break; 1697 case 1: error = 0; break; 1698 default: error = uptodate; 1699 } 1700 rq->errors = error; 1701 if (error) 1702 tape->failed_pc = NULL; 1703 1704 spin_lock_irqsave(&tape->spinlock, flags); 1705 1706 /* The request was a pipelined data transfer request */ 1707 if (tape->active_data_request == rq) { 1708 active_stage = tape->active_stage; 1709 tape->active_stage = NULL; 1710 tape->active_data_request = NULL; 1711 tape->nr_pending_stages--; 1712 if (rq->cmd[0] & REQ_IDETAPE_WRITE) { 1713 remove_stage = 1; 1714 if (error) { 1715 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags); 1716 if (error == IDETAPE_ERROR_EOD) 1717 idetape_abort_pipeline(drive, active_stage); 1718 } 1719 } else if (rq->cmd[0] & REQ_IDETAPE_READ) { 1720 if (error == IDETAPE_ERROR_EOD) { 1721 set_bit(IDETAPE_PIPELINE_ERROR, &tape->flags); 1722 idetape_abort_pipeline(drive, active_stage); 1723 } 1724 } 1725 if (tape->next_stage != NULL) { 1726 idetape_active_next_stage(drive); 1727 1728 /* 1729 * Insert the next request into the request queue. 1730 */ 1731 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end); 1732 } else if (!error) { 1733 idetape_increase_max_pipeline_stages(drive); 1734 } 1735 } 1736 ide_end_drive_cmd(drive, 0, 0); 1737// blkdev_dequeue_request(rq); 1738// drive->rq = NULL; 1739// end_that_request_last(rq); 1740 1741 if (remove_stage) 1742 idetape_remove_stage_head(drive); 1743 if (tape->active_data_request == NULL) 1744 clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags); 1745 spin_unlock_irqrestore(&tape->spinlock, flags); 1746 return 0; 1747} 1748 1749static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive) 1750{ 1751 idetape_tape_t *tape = drive->driver_data; 1752 1753#if IDETAPE_DEBUG_LOG 1754 if (tape->debug_level >= 4) 1755 printk(KERN_INFO "ide-tape: Reached idetape_request_sense_callback\n"); 1756#endif /* IDETAPE_DEBUG_LOG */ 1757 if (!tape->pc->error) { 1758 idetape_analyze_error(drive, (idetape_request_sense_result_t *) tape->pc->buffer); 1759 idetape_end_request(drive, 1, 0); 1760 } else { 1761 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE itself - Aborting request!\n"); 1762 idetape_end_request(drive, 0, 0); 1763 } 1764 return ide_stopped; 1765} 1766 1767static void idetape_create_request_sense_cmd (idetape_pc_t *pc) 1768{ 1769 idetape_init_pc(pc); 1770 pc->c[0] = IDETAPE_REQUEST_SENSE_CMD; 1771 pc->c[4] = 20; 1772 pc->request_transfer = 20; 1773 pc->callback = &idetape_request_sense_callback; 1774} 1775 1776static void idetape_init_rq(struct request *rq, u8 cmd) 1777{ 1778 memset(rq, 0, sizeof(*rq)); 1779 rq->cmd_type = REQ_TYPE_SPECIAL; 1780 rq->cmd[0] = cmd; 1781} 1782 1783/* 1784 * idetape_queue_pc_head generates a new packet command request in front 1785 * of the request queue, before the current request, so that it will be 1786 * processed immediately, on the next pass through the driver. 1787 * 1788 * idetape_queue_pc_head is called from the request handling part of 1789 * the driver (the "bottom" part). Safe storage for the request should 1790 * be allocated with idetape_next_pc_storage and idetape_next_rq_storage 1791 * before calling idetape_queue_pc_head. 1792 * 1793 * Memory for those requests is pre-allocated at initialization time, and 1794 * is limited to IDETAPE_PC_STACK requests. We assume that we have enough 1795 * space for the maximum possible number of inter-dependent packet commands. 1796 * 1797 * The higher level of the driver - The ioctl handler and the character 1798 * device handling functions should queue request to the lower level part 1799 * and wait for their completion using idetape_queue_pc_tail or 1800 * idetape_queue_rw_tail. 1801 */ 1802static void idetape_queue_pc_head (ide_drive_t *drive, idetape_pc_t *pc,struct request *rq) 1803{ 1804 struct ide_tape_obj *tape = drive->driver_data; 1805 1806 idetape_init_rq(rq, REQ_IDETAPE_PC1); 1807 rq->buffer = (char *) pc; 1808 rq->rq_disk = tape->disk; 1809 (void) ide_do_drive_cmd(drive, rq, ide_preempt); 1810} 1811 1812/* 1813 * idetape_retry_pc is called when an error was detected during the 1814 * last packet command. We queue a request sense packet command in 1815 * the head of the request list. 1816 */ 1817static ide_startstop_t idetape_retry_pc (ide_drive_t *drive) 1818{ 1819 idetape_tape_t *tape = drive->driver_data; 1820 idetape_pc_t *pc; 1821 struct request *rq; 1822 atapi_error_t error; 1823 1824 error.all = HWIF(drive)->INB(IDE_ERROR_REG); 1825 pc = idetape_next_pc_storage(drive); 1826 rq = idetape_next_rq_storage(drive); 1827 idetape_create_request_sense_cmd(pc); 1828 set_bit(IDETAPE_IGNORE_DSC, &tape->flags); 1829 idetape_queue_pc_head(drive, pc, rq); 1830 return ide_stopped; 1831} 1832 1833/* 1834 * idetape_postpone_request postpones the current request so that 1835 * ide.c will be able to service requests from another device on 1836 * the same hwgroup while we are polling for DSC. 1837 */ 1838static void idetape_postpone_request (ide_drive_t *drive) 1839{ 1840 idetape_tape_t *tape = drive->driver_data; 1841 1842#if IDETAPE_DEBUG_LOG 1843 if (tape->debug_level >= 4) 1844 printk(KERN_INFO "ide-tape: idetape_postpone_request\n"); 1845#endif 1846 tape->postponed_rq = HWGROUP(drive)->rq; 1847 ide_stall_queue(drive, tape->dsc_polling_frequency); 1848} 1849 1850/* 1851 * idetape_pc_intr is the usual interrupt handler which will be called 1852 * during a packet command. We will transfer some of the data (as 1853 * requested by the drive) and will re-point interrupt handler to us. 1854 * When data transfer is finished, we will act according to the 1855 * algorithm described before idetape_issue_packet_command. 1856 * 1857 */ 1858static ide_startstop_t idetape_pc_intr (ide_drive_t *drive) 1859{ 1860 ide_hwif_t *hwif = drive->hwif; 1861 idetape_tape_t *tape = drive->driver_data; 1862 atapi_status_t status; 1863 atapi_bcount_t bcount; 1864 atapi_ireason_t ireason; 1865 idetape_pc_t *pc = tape->pc; 1866 1867 unsigned int temp; 1868#if SIMULATE_ERRORS 1869 static int error_sim_count = 0; 1870#endif 1871 1872#if IDETAPE_DEBUG_LOG 1873 if (tape->debug_level >= 4) 1874 printk(KERN_INFO "ide-tape: Reached idetape_pc_intr " 1875 "interrupt handler\n"); 1876#endif /* IDETAPE_DEBUG_LOG */ 1877 1878 /* Clear the interrupt */ 1879 status.all = HWIF(drive)->INB(IDE_STATUS_REG); 1880 1881 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) { 1882 if (HWIF(drive)->ide_dma_end(drive) || status.b.check) { 1883 /* 1884 * A DMA error is sometimes expected. For example, 1885 * if the tape is crossing a filemark during a 1886 * READ command, it will issue an irq and position 1887 * itself before the filemark, so that only a partial 1888 * data transfer will occur (which causes the DMA 1889 * error). In that case, we will later ask the tape 1890 * how much bytes of the original request were 1891 * actually transferred (we can't receive that 1892 * information from the DMA engine on most chipsets). 1893 */ 1894 1895 /* 1896 * On the contrary, a DMA error is never expected; 1897 * it usually indicates a hardware error or abort. 1898 * If the tape crosses a filemark during a READ 1899 * command, it will issue an irq and position itself 1900 * after the filemark (not before). Only a partial 1901 * data transfer will occur, but no DMA error. 1902 * (AS, 19 Apr 2001) 1903 */ 1904 set_bit(PC_DMA_ERROR, &pc->flags); 1905 } else { 1906 pc->actually_transferred = pc->request_transfer; 1907 idetape_update_buffers(pc); 1908 } 1909#if IDETAPE_DEBUG_LOG 1910 if (tape->debug_level >= 4) 1911 printk(KERN_INFO "ide-tape: DMA finished\n"); 1912#endif /* IDETAPE_DEBUG_LOG */ 1913 } 1914 1915 /* No more interrupts */ 1916 if (!status.b.drq) { 1917#if IDETAPE_DEBUG_LOG 1918 if (tape->debug_level >= 2) 1919 printk(KERN_INFO "ide-tape: Packet command completed, %d bytes transferred\n", pc->actually_transferred); 1920#endif /* IDETAPE_DEBUG_LOG */ 1921 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags); 1922 1923 local_irq_enable(); 1924 1925#if SIMULATE_ERRORS 1926 if ((pc->c[0] == IDETAPE_WRITE_CMD || 1927 pc->c[0] == IDETAPE_READ_CMD) && 1928 (++error_sim_count % 100) == 0) { 1929 printk(KERN_INFO "ide-tape: %s: simulating error\n", 1930 tape->name); 1931 status.b.check = 1; 1932 } 1933#endif 1934 if (status.b.check && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) 1935 status.b.check = 0; 1936 if (status.b.check || test_bit(PC_DMA_ERROR, &pc->flags)) { /* Error detected */ 1937#if IDETAPE_DEBUG_LOG 1938 if (tape->debug_level >= 1) 1939 printk(KERN_INFO "ide-tape: %s: I/O error\n", 1940 tape->name); 1941#endif /* IDETAPE_DEBUG_LOG */ 1942 if (pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) { 1943 printk(KERN_ERR "ide-tape: I/O error in request sense command\n"); 1944 return ide_do_reset(drive); 1945 } 1946#if IDETAPE_DEBUG_LOG 1947 if (tape->debug_level >= 1) 1948 printk(KERN_INFO "ide-tape: [cmd %x]: check condition\n", pc->c[0]); 1949#endif 1950 /* Retry operation */ 1951 return idetape_retry_pc(drive); 1952 } 1953 pc->error = 0; 1954 if (test_bit(PC_WAIT_FOR_DSC, &pc->flags) && 1955 !status.b.dsc) { 1956 /* Media access command */ 1957 tape->dsc_polling_start = jiffies; 1958 tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST; 1959 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT; 1960 /* Allow ide.c to handle other requests */ 1961 idetape_postpone_request(drive); 1962 return ide_stopped; 1963 } 1964 if (tape->failed_pc == pc) 1965 tape->failed_pc = NULL; 1966 /* Command finished - Call the callback function */ 1967 return pc->callback(drive); 1968 } 1969 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) { 1970 printk(KERN_ERR "ide-tape: The tape wants to issue more " 1971 "interrupts in DMA mode\n"); 1972 printk(KERN_ERR "ide-tape: DMA disabled, reverting to PIO\n"); 1973 ide_dma_off(drive); 1974 return ide_do_reset(drive); 1975 } 1976 /* Get the number of bytes to transfer on this interrupt. */ 1977 bcount.b.high = hwif->INB(IDE_BCOUNTH_REG); 1978 bcount.b.low = hwif->INB(IDE_BCOUNTL_REG); 1979 1980 ireason.all = hwif->INB(IDE_IREASON_REG); 1981 1982 if (ireason.b.cod) { 1983 printk(KERN_ERR "ide-tape: CoD != 0 in idetape_pc_intr\n"); 1984 return ide_do_reset(drive); 1985 } 1986 if (ireason.b.io == test_bit(PC_WRITING, &pc->flags)) { 1987 /* Hopefully, we will never get here */ 1988 printk(KERN_ERR "ide-tape: We wanted to %s, ", 1989 ireason.b.io ? "Write":"Read"); 1990 printk(KERN_ERR "ide-tape: but the tape wants us to %s !\n", 1991 ireason.b.io ? "Read":"Write"); 1992 return ide_do_reset(drive); 1993 } 1994 if (!test_bit(PC_WRITING, &pc->flags)) { 1995 /* Reading - Check that we have enough space */ 1996 temp = pc->actually_transferred + bcount.all; 1997 if (temp > pc->request_transfer) { 1998 if (temp > pc->buffer_size) { 1999 printk(KERN_ERR "ide-tape: The tape wants to send us more data than expected - discarding data\n"); 2000 idetape_discard_data(drive, bcount.all); 2001 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL); 2002 return ide_started; 2003 } 2004#if IDETAPE_DEBUG_LOG 2005 if (tape->debug_level >= 2) 2006 printk(KERN_NOTICE "ide-tape: The tape wants to send us more data than expected - allowing transfer\n"); 2007#endif /* IDETAPE_DEBUG_LOG */ 2008 } 2009 } 2010 if (test_bit(PC_WRITING, &pc->flags)) { 2011 if (pc->bh != NULL) 2012 idetape_output_buffers(drive, pc, bcount.all); 2013 else 2014 /* Write the current buffer */ 2015 HWIF(drive)->atapi_output_bytes(drive, pc->current_position, bcount.all); 2016 } else { 2017 if (pc->bh != NULL) 2018 idetape_input_buffers(drive, pc, bcount.all); 2019 else 2020 /* Read the current buffer */ 2021 HWIF(drive)->atapi_input_bytes(drive, pc->current_position, bcount.all); 2022 } 2023 /* Update the current position */ 2024 pc->actually_transferred += bcount.all; 2025 pc->current_position += bcount.all; 2026#if IDETAPE_DEBUG_LOG 2027 if (tape->debug_level >= 2) 2028 printk(KERN_INFO "ide-tape: [cmd %x] transferred %d bytes on that interrupt\n", pc->c[0], bcount.all); 2029#endif 2030 /* And set the interrupt handler again */ 2031 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL); 2032 return ide_started; 2033} 2034 2035/* 2036 * Packet Command Interface 2037 * 2038 * The current Packet Command is available in tape->pc, and will not 2039 * change until we finish handling it. Each packet command is associated 2040 * with a callback function that will be called when the command is 2041 * finished. 2042 * 2043 * The handling will be done in three stages: 2044 * 2045 * 1. idetape_issue_packet_command will send the packet command to the 2046 * drive, and will set the interrupt handler to idetape_pc_intr. 2047 * 2048 * 2. On each interrupt, idetape_pc_intr will be called. This step 2049 * will be repeated until the device signals us that no more 2050 * interrupts will be issued. 2051 * 2052 * 3. ATAPI Tape media access commands have immediate status with a 2053 * delayed process. In case of a successful initiation of a 2054 * media access packet command, the DSC bit will be set when the 2055 * actual execution of the command is finished. 2056 * Since the tape drive will not issue an interrupt, we have to 2057 * poll for this event. In this case, we define the request as 2058 * "low priority request" by setting rq_status to 2059 * IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and exit 2060 * the driver. 2061 * 2062 * ide.c will then give higher priority to requests which 2063 * originate from the other device, until will change rq_status 2064 * to RQ_ACTIVE. 2065 * 2066 * 4. When the packet command is finished, it will be checked for errors. 2067 * 2068 * 5. In case an error was found, we queue a request sense packet 2069 * command in front of the request queue and retry the operation 2070 * up to IDETAPE_MAX_PC_RETRIES times. 2071 * 2072 * 6. In case no error was found, or we decided to give up and not 2073 * to retry again, the callback function will be called and then 2074 * we will handle the next request. 2075 * 2076 */ 2077static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive) 2078{ 2079 ide_hwif_t *hwif = drive->hwif; 2080 idetape_tape_t *tape = drive->driver_data; 2081 idetape_pc_t *pc = tape->pc; 2082 atapi_ireason_t ireason; 2083 int retries = 100; 2084 ide_startstop_t startstop; 2085 2086 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) { 2087 printk(KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n"); 2088 return startstop; 2089 } 2090 ireason.all = hwif->INB(IDE_IREASON_REG); 2091 while (retries-- && (!ireason.b.cod || ireason.b.io)) { 2092 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing " 2093 "a packet command, retrying\n"); 2094 udelay(100); 2095 ireason.all = hwif->INB(IDE_IREASON_REG); 2096 if (retries == 0) { 2097 printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while " 2098 "issuing a packet command, ignoring\n"); 2099 ireason.b.cod = 1; 2100 ireason.b.io = 0; 2101 } 2102 } 2103 if (!ireason.b.cod || ireason.b.io) { 2104 printk(KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing " 2105 "a packet command\n"); 2106 return ide_do_reset(drive); 2107 } 2108 /* Set the interrupt routine */ 2109 ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL); 2110#ifdef CONFIG_BLK_DEV_IDEDMA 2111 /* Begin DMA, if necessary */ 2112 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) 2113 hwif->dma_start(drive); 2114#endif 2115 /* Send the actual packet */ 2116 HWIF(drive)->atapi_output_bytes(drive, pc->c, 12); 2117 return ide_started; 2118} 2119 2120static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape_pc_t *pc) 2121{ 2122 ide_hwif_t *hwif = drive->hwif; 2123 idetape_tape_t *tape = drive->driver_data; 2124 atapi_bcount_t bcount; 2125 int dma_ok = 0; 2126 2127#if IDETAPE_DEBUG_BUGS 2128 if (tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD && 2129 pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) { 2130 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - " 2131 "Two request sense in serial were issued\n"); 2132 } 2133#endif /* IDETAPE_DEBUG_BUGS */ 2134 2135 if (tape->failed_pc == NULL && pc->c[0] != IDETAPE_REQUEST_SENSE_CMD) 2136 tape->failed_pc = pc; 2137 /* Set the current packet command */ 2138 tape->pc = pc; 2139 2140 if (pc->retries > IDETAPE_MAX_PC_RETRIES || 2141 test_bit(PC_ABORT, &pc->flags)) { 2142 /* 2143 * We will "abort" retrying a packet command in case 2144 * a legitimate error code was received (crossing a 2145 * filemark, or end of the media, for example). 2146 */ 2147 if (!test_bit(PC_ABORT, &pc->flags)) { 2148 if (!(pc->c[0] == IDETAPE_TEST_UNIT_READY_CMD && 2149 tape->sense_key == 2 && tape->asc == 4 && 2150 (tape->ascq == 1 || tape->ascq == 8))) { 2151 printk(KERN_ERR "ide-tape: %s: I/O error, " 2152 "pc = %2x, key = %2x, " 2153 "asc = %2x, ascq = %2x\n", 2154 tape->name, pc->c[0], 2155 tape->sense_key, tape->asc, 2156 tape->ascq); 2157 } 2158 /* Giving up */ 2159 pc->error = IDETAPE_ERROR_GENERAL; 2160 } 2161 tape->failed_pc = NULL; 2162 return pc->callback(drive); 2163 } 2164#if IDETAPE_DEBUG_LOG 2165 if (tape->debug_level >= 2) 2166 printk(KERN_INFO "ide-tape: Retry number - %d, cmd = %02X\n", pc->retries, pc->c[0]); 2167#endif /* IDETAPE_DEBUG_LOG */ 2168 2169 pc->retries++; 2170 /* We haven't transferred any data yet */ 2171 pc->actually_transferred = 0; 2172 pc->current_position = pc->buffer; 2173 /* Request to transfer the entire buffer at once */ 2174 bcount.all = pc->request_transfer; 2175 2176 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags)) { 2177 printk(KERN_WARNING "ide-tape: DMA disabled, " 2178 "reverting to PIO\n"); 2179 ide_dma_off(drive); 2180 } 2181 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma) 2182 dma_ok = !hwif->dma_setup(drive); 2183 2184 if (IDE_CONTROL_REG) 2185 hwif->OUTB(drive->ctl, IDE_CONTROL_REG); 2186 hwif->OUTB(dma_ok ? 1 : 0, IDE_FEATURE_REG); /* Use PIO/DMA */ 2187 hwif->OUTB(bcount.b.high, IDE_BCOUNTH_REG); 2188 hwif->OUTB(bcount.b.low, IDE_BCOUNTL_REG); 2189 hwif->OUTB(drive->select.all, IDE_SELECT_REG); 2190 if (dma_ok) /* Will begin DMA later */ 2191 set_bit(PC_DMA_IN_PROGRESS, &pc->flags); 2192 if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) { 2193 ide_set_handler(drive, &idetape_transfer_pc, IDETAPE_WAIT_CMD, NULL); 2194 hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG); 2195 return ide_started; 2196 } else { 2197 hwif->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG); 2198 return idetape_transfer_pc(drive); 2199 } 2200} 2201 2202/* 2203 * General packet command callback function. 2204 */ 2205static ide_startstop_t idetape_pc_callback (ide_drive_t *drive) 2206{ 2207 idetape_tape_t *tape = drive->driver_data; 2208 2209#if IDETAPE_DEBUG_LOG 2210 if (tape->debug_level >= 4) 2211 printk(KERN_INFO "ide-tape: Reached idetape_pc_callback\n"); 2212#endif /* IDETAPE_DEBUG_LOG */ 2213 2214 idetape_end_request(drive, tape->pc->error ? 0 : 1, 0); 2215 return ide_stopped; 2216} 2217 2218/* 2219 * A mode sense command is used to "sense" tape parameters. 2220 */ 2221static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, u8 page_code) 2222{ 2223 idetape_init_pc(pc); 2224 pc->c[0] = IDETAPE_MODE_SENSE_CMD; 2225 if (page_code != IDETAPE_BLOCK_DESCRIPTOR) 2226 pc->c[1] = 8; /* DBD = 1 - Don't return block descriptors */ 2227 pc->c[2] = page_code; 2228 /* 2229 * Changed pc->c[3] to 0 (255 will at best return unused info). 2230 * 2231 * For SCSI this byte is defined as subpage instead of high byte 2232 * of length and some IDE drives seem to interpret it this way 2233 * and return an error when 255 is used. 2234 */ 2235 pc->c[3] = 0; 2236 pc->c[4] = 255; /* (We will just discard data in that case) */ 2237 if (page_code == IDETAPE_BLOCK_DESCRIPTOR) 2238 pc->request_transfer = 12; 2239 else if (page_code == IDETAPE_CAPABILITIES_PAGE) 2240 pc->request_transfer = 24; 2241 else 2242 pc->request_transfer = 50; 2243 pc->callback = &idetape_pc_callback; 2244} 2245 2246static void calculate_speeds(ide_drive_t *drive) 2247{ 2248 idetape_tape_t *tape = drive->driver_data; 2249 int full = 125, empty = 75; 2250 2251 if (time_after(jiffies, tape->controlled_pipeline_head_time + 120 * HZ)) { 2252 tape->controlled_previous_pipeline_head = tape->controlled_last_pipeline_head; 2253 tape->controlled_previous_head_time = tape->controlled_pipeline_head_time; 2254 tape->controlled_last_pipeline_head = tape->pipeline_head; 2255 tape->controlled_pipeline_head_time = jiffies; 2256 } 2257 if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ)) 2258 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_last_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_pipeline_head_time); 2259 else if (time_after(jiffies, tape->controlled_previous_head_time)) 2260 tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_previous_head_time); 2261 2262 if (tape->nr_pending_stages < tape->max_stages /*- 1 */) { 2263 /* -1 for read mode error recovery */ 2264 if (time_after(jiffies, tape->uncontrolled_previous_head_time + 10 * HZ)) { 2265 tape->uncontrolled_pipeline_head_time = jiffies; 2266 tape->uncontrolled_pipeline_head_speed = (tape->pipeline_head - tape->uncontrolled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->uncontrolled_previous_head_time); 2267 } 2268 } else { 2269 tape->uncontrolled_previous_head_time = jiffies; 2270 tape->uncontrolled_previous_pipeline_head = tape->pipeline_head; 2271 if (time_after(jiffies, tape->uncontrolled_pipeline_head_time + 30 * HZ)) { 2272 tape->uncontrolled_pipeline_head_time = jiffies; 2273 } 2274 } 2275 tape->pipeline_head_speed = max(tape->uncontrolled_pipeline_head_speed, tape->controlled_pipeline_head_speed); 2276 if (tape->speed_control == 0) { 2277 tape->max_insert_speed = 5000; 2278 } else if (tape->speed_control == 1) { 2279 if (tape->nr_pending_stages >= tape->max_stages / 2) 2280 tape->max_insert_speed = tape->pipeline_head_speed + 2281 (1100 - tape->pipeline_head_speed) * 2 * (tape->nr_pending_stages - tape->max_stages / 2) / tape->max_stages; 2282 else 2283 tape->max_insert_speed = 500 + 2284 (tape->pipeline_head_speed - 500) * 2 * tape->nr_pending_stages / tape->max_stages; 2285 if (tape->nr_pending_stages >= tape->max_stages * 99 / 100) 2286 tape->max_insert_speed = 5000; 2287 } else if (tape->speed_control == 2) { 2288 tape->max_insert_speed = tape->pipeline_head_speed * empty / 100 + 2289 (tape->pipeline_head_speed * full / 100 - tape->pipeline_head_speed * empty / 100) * tape->nr_pending_stages / tape->max_stages; 2290 } else 2291 tape->max_insert_speed = tape->speed_control; 2292 tape->max_insert_speed = max(tape->max_insert_speed, 500); 2293} 2294 2295static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive) 2296{ 2297 idetape_tape_t *tape = drive->driver_data; 2298 idetape_pc_t *pc = tape->pc; 2299 atapi_status_t status; 2300 2301 status.all = HWIF(drive)->INB(IDE_STATUS_REG); 2302 if (status.b.dsc) { 2303 if (status.b.check) { 2304 /* Error detected */ 2305 if (pc->c[0] != IDETAPE_TEST_UNIT_READY_CMD) 2306 printk(KERN_ERR "ide-tape: %s: I/O error, ", 2307 tape->name); 2308 /* Retry operation */ 2309 return idetape_retry_pc(drive); 2310 } 2311 pc->error = 0; 2312 if (tape->failed_pc == pc) 2313 tape->failed_pc = NULL; 2314 } else { 2315 pc->error = IDETAPE_ERROR_GENERAL; 2316 tape->failed_pc = NULL; 2317 } 2318 return pc->callback(drive); 2319} 2320 2321static ide_startstop_t idetape_rw_callback (ide_drive_t *drive) 2322{ 2323 idetape_tape_t *tape = drive->driver_data; 2324 struct request *rq = HWGROUP(drive)->rq; 2325 int blocks = tape->pc->actually_transferred / tape->tape_block_size; 2326 2327 tape->avg_size += blocks * tape->tape_block_size; 2328 tape->insert_size += blocks * tape->tape_block_size; 2329 if (tape->insert_size > 1024 * 1024) 2330 tape->measure_insert_time = 1; 2331 if (tape->measure_insert_time) { 2332 tape->measure_insert_time = 0; 2333 tape->insert_time = jiffies; 2334 tape->insert_size = 0; 2335 } 2336 if (time_after(jiffies, tape->insert_time)) 2337 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time); 2338 if (time_after_eq(jiffies, tape->avg_time + HZ)) { 2339 tape->avg_speed = tape->avg_size * HZ / (jiffies - tape->avg_time) / 1024; 2340 tape->avg_size = 0; 2341 tape->avg_time = jiffies; 2342 } 2343 2344#if IDETAPE_DEBUG_LOG 2345 if (tape->debug_level >= 4) 2346 printk(KERN_INFO "ide-tape: Reached idetape_rw_callback\n"); 2347#endif /* IDETAPE_DEBUG_LOG */ 2348 2349 tape->first_frame_position += blocks; 2350 rq->current_nr_sectors -= blocks; 2351 2352 if (!tape->pc->error) 2353 idetape_end_request(drive, 1, 0); 2354 else 2355 idetape_end_request(drive, tape->pc->error, 0); 2356 return ide_stopped; 2357} 2358 2359static void idetape_create_read_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh) 2360{ 2361 idetape_init_pc(pc); 2362 pc->c[0] = IDETAPE_READ_CMD; 2363 put_unaligned(htonl(length), (unsigned int *) &pc->c[1]); 2364 pc->c[1] = 1; 2365 pc->callback = &idetape_rw_callback; 2366 pc->bh = bh; 2367 atomic_set(&bh->b_count, 0); 2368 pc->buffer = NULL; 2369 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size; 2370 if (pc->request_transfer == tape->stage_size) 2371 set_bit(PC_DMA_RECOMMENDED, &pc->flags); 2372} 2373 2374static void idetape_create_read_buffer_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh) 2375{ 2376 int size = 32768; 2377 struct idetape_bh *p = bh; 2378 2379 idetape_init_pc(pc); 2380 pc->c[0] = IDETAPE_READ_BUFFER_CMD; 2381 pc->c[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK; 2382 pc->c[7] = size >> 8; 2383 pc->c[8] = size & 0xff; 2384 pc->callback = &idetape_pc_callback; 2385 pc->bh = bh; 2386 atomic_set(&bh->b_count, 0); 2387 pc->buffer = NULL; 2388 while (p) { 2389 atomic_set(&p->b_count, 0); 2390 p = p->b_reqnext; 2391 } 2392 pc->request_transfer = pc->buffer_size = size; 2393} 2394 2395static void idetape_create_write_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct idetape_bh *bh) 2396{ 2397 idetape_init_pc(pc); 2398 pc->c[0] = IDETAPE_WRITE_CMD; 2399 put_unaligned(htonl(length), (unsigned int *) &pc->c[1]); 2400 pc->c[1] = 1; 2401 pc->callback = &idetape_rw_callback; 2402 set_bit(PC_WRITING, &pc->flags); 2403 pc->bh = bh; 2404 pc->b_data = bh->b_data; 2405 pc->b_count = atomic_read(&bh->b_count); 2406 pc->buffer = NULL; 2407 pc->request_transfer = pc->buffer_size = length * tape->tape_block_size; 2408 if (pc->request_transfer == tape->stage_size) 2409 set_bit(PC_DMA_RECOMMENDED, &pc->flags); 2410} 2411 2412/* 2413 * idetape_do_request is our request handling function. 2414 */ 2415static ide_startstop_t idetape_do_request(ide_drive_t *drive, 2416 struct request *rq, sector_t block) 2417{ 2418 idetape_tape_t *tape = drive->driver_data; 2419 idetape_pc_t *pc = NULL; 2420 struct request *postponed_rq = tape->postponed_rq; 2421 atapi_status_t status; 2422 2423#if IDETAPE_DEBUG_LOG 2424#if 0 2425 if (tape->debug_level >= 5) 2426 printk(KERN_INFO "ide-tape: %d, " 2427 "dev: %s, cmd: %ld, errors: %d\n", 2428 rq->rq_disk->disk_name, rq->cmd[0], rq->errors); 2429#endif 2430 if (tape->debug_level >= 2) 2431 printk(KERN_INFO "ide-tape: sector: %ld, " 2432 "nr_sectors: %ld, current_nr_sectors: %d\n", 2433 rq->sector, rq->nr_sectors, rq->current_nr_sectors); 2434#endif /* IDETAPE_DEBUG_LOG */ 2435 2436 if (!blk_special_request(rq)) { 2437 /* 2438 * We do not support buffer cache originated requests. 2439 */ 2440 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in " 2441 "request queue (%d)\n", drive->name, rq->cmd_type); 2442 ide_end_request(drive, 0, 0); 2443 return ide_stopped; 2444 } 2445 2446 /* 2447 * Retry a failed packet command 2448 */ 2449 if (tape->failed_pc != NULL && 2450 tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) { 2451 return idetape_issue_packet_command(drive, tape->failed_pc); 2452 } 2453#if IDETAPE_DEBUG_BUGS 2454 if (postponed_rq != NULL) 2455 if (rq != postponed_rq) { 2456 printk(KERN_ERR "ide-tape: ide-tape.c bug - " 2457 "Two DSC requests were queued\n"); 2458 idetape_end_request(drive, 0, 0); 2459 return ide_stopped; 2460 } 2461#endif /* IDETAPE_DEBUG_BUGS */ 2462 2463 tape->postponed_rq = NULL; 2464 2465 /* 2466 * If the tape is still busy, postpone our request and service 2467 * the other device meanwhile. 2468 */ 2469 status.all = HWIF(drive)->INB(IDE_STATUS_REG); 2470 2471 if (!drive->dsc_overlap && !(rq->cmd[0] & REQ_IDETAPE_PC2)) 2472 set_bit(IDETAPE_IGNORE_DSC, &tape->flags); 2473 2474 if (drive->post_reset == 1) { 2475 set_bit(IDETAPE_IGNORE_DSC, &tape->flags); 2476 drive->post_reset = 0; 2477 } 2478 2479 if (tape->tape_still_time > 100 && tape->tape_still_time < 200) 2480 tape->measure_insert_time = 1; 2481 if (time_after(jiffies, tape->insert_time)) 2482 tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time); 2483 calculate_speeds(drive); 2484 if (!test_and_clear_bit(IDETAPE_IGNORE_DSC, &tape->flags) && 2485 !status.b.dsc) { 2486 if (postponed_rq == NULL) { 2487 tape->dsc_polling_start = jiffies; 2488 tape->dsc_polling_frequency = tape->best_dsc_rw_frequency; 2489 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT; 2490 } else if (time_after(jiffies, tape->dsc_timeout)) { 2491 printk(KERN_ERR "ide-tape: %s: DSC timeout\n", 2492 tape->name); 2493 if (rq->cmd[0] & REQ_IDETAPE_PC2) { 2494 idetape_media_access_finished(drive); 2495 return ide_stopped; 2496 } else { 2497 return ide_do_reset(drive); 2498 } 2499 } else if (time_after(jiffies, tape->dsc_polling_start + IDETAPE_DSC_MA_THRESHOLD)) 2500 tape->dsc_polling_frequency = IDETAPE_DSC_MA_SLOW; 2501 idetape_postpone_request(drive); 2502 return ide_stopped; 2503 } 2504 if (rq->cmd[0] & REQ_IDETAPE_READ) { 2505 tape->buffer_head++; 2506#if USE_IOTRACE 2507 IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor); 2508#endif 2509 tape->postpone_cnt = 0; 2510 pc = idetape_next_pc_storage(drive); 2511 idetape_create_read_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special); 2512 goto out; 2513 } 2514 if (rq->cmd[0] & REQ_IDETAPE_WRITE) { 2515 tape->buffer_head++; 2516#if USE_IOTRACE 2517 IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor); 2518#endif 2519 tape->postpone_cnt = 0; 2520 pc = idetape_next_pc_storage(drive); 2521 idetape_create_write_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special); 2522 goto out; 2523 } 2524 if (rq->cmd[0] & REQ_IDETAPE_READ_BUFFER) { 2525 tape->postpone_cnt = 0; 2526 pc = idetape_next_pc_storage(drive); 2527 idetape_create_read_buffer_cmd(tape, pc, rq->current_nr_sectors, (struct idetape_bh *)rq->special); 2528 goto out; 2529 } 2530 if (rq->cmd[0] & REQ_IDETAPE_PC1) { 2531 pc = (idetape_pc_t *) rq->buffer; 2532 rq->cmd[0] &= ~(REQ_IDETAPE_PC1); 2533 rq->cmd[0] |= REQ_IDETAPE_PC2; 2534 goto out; 2535 } 2536 if (rq->cmd[0] & REQ_IDETAPE_PC2) { 2537 idetape_media_access_finished(drive); 2538 return ide_stopped; 2539 } 2540 BUG(); 2541out: 2542 return idetape_issue_packet_command(drive, pc); 2543} 2544 2545/* 2546 * Pipeline related functions 2547 */ 2548static inline int idetape_pipeline_active (idetape_tape_t *tape) 2549{ 2550 int rc1, rc2; 2551 2552 rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags); 2553 rc2 = (tape->active_data_request != NULL); 2554 return rc1; 2555} 2556 2557/* 2558 * idetape_kmalloc_stage uses __get_free_page to allocate a pipeline 2559 * stage, along with all the necessary small buffers which together make 2560 * a buffer of size tape->stage_size (or a bit more). We attempt to 2561 * combine sequential pages as much as possible. 2562 * 2563 * Returns a pointer to the new allocated stage, or NULL if we 2564 * can't (or don't want to) allocate a stage. 2565 * 2566 * Pipeline stages are optional and are used to increase performance. 2567 * If we can't allocate them, we'll manage without them. 2568 */ 2569static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape, int full, int clear) 2570{ 2571 idetape_stage_t *stage; 2572 struct idetape_bh *prev_bh, *bh; 2573 int pages = tape->pages_per_stage; 2574 char *b_data = NULL; 2575 2576 if ((stage = kmalloc(sizeof (idetape_stage_t),GFP_KERNEL)) == NULL) 2577 return NULL; 2578 stage->next = NULL; 2579 2580 bh = stage->bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL); 2581 if (bh == NULL) 2582 goto abort; 2583 bh->b_reqnext = NULL; 2584 if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL) 2585 goto abort; 2586 if (clear) 2587 memset(bh->b_data, 0, PAGE_SIZE); 2588 bh->b_size = PAGE_SIZE; 2589 atomic_set(&bh->b_count, full ? bh->b_size : 0); 2590 2591 while (--pages) { 2592 if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL) 2593 goto abort; 2594 if (clear) 2595 memset(b_data, 0, PAGE_SIZE); 2596 if (bh->b_data == b_data + PAGE_SIZE) { 2597 bh->b_size += PAGE_SIZE; 2598 bh->b_data -= PAGE_SIZE; 2599 if (full) 2600 atomic_add(PAGE_SIZE, &bh->b_count); 2601 continue; 2602 } 2603 if (b_data == bh->b_data + bh->b_size) { 2604 bh->b_size += PAGE_SIZE; 2605 if (full) 2606 atomic_add(PAGE_SIZE, &bh->b_count); 2607 continue; 2608 } 2609 prev_bh = bh; 2610 if ((bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL)) == NULL) { 2611 free_page((unsigned long) b_data); 2612 goto abort; 2613 } 2614 bh->b_reqnext = NULL; 2615 bh->b_data = b_data; 2616 bh->b_size = PAGE_SIZE; 2617 atomic_set(&bh->b_count, full ? bh->b_size : 0); 2618 prev_bh->b_reqnext = bh; 2619 } 2620 bh->b_size -= tape->excess_bh_size; 2621 if (full) 2622 atomic_sub(tape->excess_bh_size, &bh->b_count); 2623 return stage; 2624abort: 2625 __idetape_kfree_stage(stage); 2626 return NULL; 2627} 2628 2629static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape) 2630{ 2631 idetape_stage_t *cache_stage = tape->cache_stage; 2632 2633#if IDETAPE_DEBUG_LOG 2634 if (tape->debug_level >= 4) 2635 printk(KERN_INFO "ide-tape: Reached idetape_kmalloc_stage\n"); 2636#endif /* IDETAPE_DEBUG_LOG */ 2637 2638 if (tape->nr_stages >= tape->max_stages) 2639 return NULL; 2640 if (cache_stage != NULL) { 2641 tape->cache_stage = NULL; 2642 return cache_stage; 2643 } 2644 return __idetape_kmalloc_stage(tape, 0, 0); 2645} 2646 2647static int idetape_copy_stage_from_user (idetape_tape_t *tape, idetape_stage_t *stage, const char __user *buf, int n) 2648{ 2649 struct idetape_bh *bh = tape->bh; 2650 int count; 2651 int ret = 0; 2652 2653 while (n) { 2654#if IDETAPE_DEBUG_BUGS 2655 if (bh == NULL) { 2656 printk(KERN_ERR "ide-tape: bh == NULL in " 2657 "idetape_copy_stage_from_user\n"); 2658 return 1; 2659 } 2660#endif /* IDETAPE_DEBUG_BUGS */ 2661 count = min((unsigned int)(bh->b_size - atomic_read(&bh->b_count)), (unsigned int)n); 2662 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, count)) 2663 ret = 1; 2664 n -= count; 2665 atomic_add(count, &bh->b_count); 2666 buf += count; 2667 if (atomic_read(&bh->b_count) == bh->b_size) { 2668 bh = bh->b_reqnext; 2669 if (bh) 2670 atomic_set(&bh->b_count, 0); 2671 } 2672 } 2673 tape->bh = bh; 2674 return ret; 2675} 2676 2677static int idetape_copy_stage_to_user (idetape_tape_t *tape, char __user *buf, idetape_stage_t *stage, int n) 2678{ 2679 struct idetape_bh *bh = tape->bh; 2680 int count; 2681 int ret = 0; 2682 2683 while (n) { 2684#if IDETAPE_DEBUG_BUGS 2685 if (bh == NULL) { 2686 printk(KERN_ERR "ide-tape: bh == NULL in " 2687 "idetape_copy_stage_to_user\n"); 2688 return 1; 2689 } 2690#endif /* IDETAPE_DEBUG_BUGS */ 2691 count = min(tape->b_count, n); 2692 if (copy_to_user(buf, tape->b_data, count)) 2693 ret = 1; 2694 n -= count; 2695 tape->b_data += count; 2696 tape->b_count -= count; 2697 buf += count; 2698 if (!tape->b_count) { 2699 tape->bh = bh = bh->b_reqnext; 2700 if (bh) { 2701 tape->b_data = bh->b_data; 2702 tape->b_count = atomic_read(&bh->b_count); 2703 } 2704 } 2705 } 2706 return ret; 2707} 2708 2709static void idetape_init_merge_stage (idetape_tape_t *tape) 2710{ 2711 struct idetape_bh *bh = tape->merge_stage->bh; 2712 2713 tape->bh = bh; 2714 if (tape->chrdev_direction == idetape_direction_write) 2715 atomic_set(&bh->b_count, 0); 2716 else { 2717 tape->b_data = bh->b_data; 2718 tape->b_count = atomic_read(&bh->b_count); 2719 } 2720} 2721 2722static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage) 2723{ 2724 struct idetape_bh *tmp; 2725 2726 tmp = stage->bh; 2727 stage->bh = tape->merge_stage->bh; 2728 tape->merge_stage->bh = tmp; 2729 idetape_init_merge_stage(tape); 2730} 2731 2732/* 2733 * idetape_add_stage_tail adds a new stage at the end of the pipeline. 2734 */ 2735static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage) 2736{ 2737 idetape_tape_t *tape = drive->driver_data; 2738 unsigned long flags; 2739 2740#if IDETAPE_DEBUG_LOG 2741 if (tape->debug_level >= 4) 2742 printk (KERN_INFO "ide-tape: Reached idetape_add_stage_tail\n"); 2743#endif /* IDETAPE_DEBUG_LOG */ 2744 spin_lock_irqsave(&tape->spinlock, flags); 2745 stage->next = NULL; 2746 if (tape->last_stage != NULL) 2747 tape->last_stage->next=stage; 2748 else 2749 tape->first_stage = tape->next_stage=stage; 2750 tape->last_stage = stage; 2751 if (tape->next_stage == NULL) 2752 tape->next_stage = tape->last_stage; 2753 tape->nr_stages++; 2754 tape->nr_pending_stages++; 2755 spin_unlock_irqrestore(&tape->spinlock, flags); 2756} 2757 2758/* 2759 * idetape_wait_for_request installs a completion in a pending request 2760 * and sleeps until it is serviced. 2761 * 2762 * The caller should ensure that the request will not be serviced 2763 * before we install the completion (usually by disabling interrupts). 2764 */ 2765static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq) 2766{ 2767 DECLARE_COMPLETION_ONSTACK(wait); 2768 idetape_tape_t *tape = drive->driver_data; 2769 2770#if IDETAPE_DEBUG_BUGS 2771 if (rq == NULL || !blk_special_request(rq)) { 2772 printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid request\n"); 2773 return; 2774 } 2775#endif /* IDETAPE_DEBUG_BUGS */ 2776 rq->end_io_data = &wait; 2777 rq->end_io = blk_end_sync_rq; 2778 spin_unlock_irq(&tape->spinlock); 2779 wait_for_completion(&wait); 2780 /* The stage and its struct request have been deallocated */ 2781 spin_lock_irq(&tape->spinlock); 2782} 2783 2784static ide_startstop_t idetape_read_position_callback (ide_drive_t *drive) 2785{ 2786 idetape_tape_t *tape = drive->driver_data; 2787 idetape_read_position_result_t *result; 2788 2789#if IDETAPE_DEBUG_LOG 2790 if (tape->debug_level >= 4) 2791 printk(KERN_INFO "ide-tape: Reached idetape_read_position_callback\n"); 2792#endif /* IDETAPE_DEBUG_LOG */ 2793 2794 if (!tape->pc->error) { 2795 result = (idetape_read_position_result_t *) tape->pc->buffer; 2796#if IDETAPE_DEBUG_LOG 2797 if (tape->debug_level >= 2) 2798 printk(KERN_INFO "ide-tape: BOP - %s\n",result->bop ? "Yes":"No"); 2799 if (tape->debug_level >= 2) 2800 printk(KERN_INFO "ide-tape: EOP - %s\n",result->eop ? "Yes":"No"); 2801#endif /* IDETAPE_DEBUG_LOG */ 2802 if (result->bpu) { 2803 printk(KERN_INFO "ide-tape: Block location is unknown to the tape\n"); 2804 clear_bit(IDETAPE_ADDRESS_VALID, &tape->flags); 2805 idetape_end_request(drive, 0, 0); 2806 } else { 2807#if IDETAPE_DEBUG_LOG 2808 if (tape->debug_level >= 2) 2809 printk(KERN_INFO "ide-tape: Block Location - %u\n", ntohl(result->first_block)); 2810#endif /* IDETAPE_DEBUG_LOG */ 2811 tape->partition = result->partition; 2812 tape->first_frame_position = ntohl(result->first_block); 2813 tape->last_frame_position = ntohl(result->last_block); 2814 tape->blocks_in_buffer = result->blocks_in_buffer[2]; 2815 set_bit(IDETAPE_ADDRESS_VALID, &tape->flags); 2816 idetape_end_request(drive, 1, 0); 2817 } 2818 } else { 2819 idetape_end_request(drive, 0, 0); 2820 } 2821 return ide_stopped; 2822} 2823 2824/* 2825 * idetape_create_write_filemark_cmd will: 2826 * 2827 * 1. Write a filemark if write_filemark=1. 2828 * 2. Flush the device buffers without writing a filemark 2829 * if write_filemark=0. 2830 * 2831 */ 2832static void idetape_create_write_filemark_cmd (ide_drive_t *drive, idetape_pc_t *pc,int write_filemark) 2833{ 2834 idetape_init_pc(pc); 2835 pc->c[0] = IDETAPE_WRITE_FILEMARK_CMD; 2836 pc->c[4] = write_filemark; 2837 set_bit(PC_WAIT_FOR_DSC, &pc->flags); 2838 pc->callback = &idetape_pc_callback; 2839} 2840 2841static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc) 2842{ 2843 idetape_init_pc(pc); 2844 pc->c[0] = IDETAPE_TEST_UNIT_READY_CMD; 2845 pc->callback = &idetape_pc_callback; 2846} 2847 2848/* 2849 * idetape_queue_pc_tail is based on the following functions: 2850 * 2851 * ide_do_drive_cmd from ide.c 2852 * cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c 2853 * 2854 * We add a special packet command request to the tail of the request 2855 * queue, and wait for it to be serviced. 2856 * 2857 * This is not to be called from within the request handling part 2858 * of the driver ! We allocate here data in the stack, and it is valid 2859 * until the request is finished. This is not the case for the bottom 2860 * part of the driver, where we are always leaving the functions to wait 2861 * for an interrupt or a timer event. 2862 * 2863 * From the bottom part of the driver, we should allocate safe memory 2864 * using idetape_next_pc_storage and idetape_next_rq_storage, and add 2865 * the request to the request list without waiting for it to be serviced ! 2866 * In that case, we usually use idetape_queue_pc_head. 2867 */ 2868static int __idetape_queue_pc_tail (ide_drive_t *drive, idetape_pc_t *pc) 2869{ 2870 struct ide_tape_obj *tape = drive->driver_data; 2871 struct request rq; 2872 2873 idetape_init_rq(&rq, REQ_IDETAPE_PC1); 2874 rq.buffer = (char *) pc; 2875 rq.rq_disk = tape->disk; 2876 return ide_do_drive_cmd(drive, &rq, ide_wait); 2877} 2878 2879static void idetape_create_load_unload_cmd (ide_drive_t *drive, idetape_pc_t *pc,int cmd) 2880{ 2881 idetape_init_pc(pc); 2882 pc->c[0] = IDETAPE_LOAD_UNLOAD_CMD; 2883 pc->c[4] = cmd; 2884 set_bit(PC_WAIT_FOR_DSC, &pc->flags); 2885 pc->callback = &idetape_pc_callback; 2886} 2887 2888static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout) 2889{ 2890 idetape_tape_t *tape = drive->driver_data; 2891 idetape_pc_t pc; 2892 int load_attempted = 0; 2893 2894 /* 2895 * Wait for the tape to become ready 2896 */ 2897 set_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags); 2898 timeout += jiffies; 2899 while (time_before(jiffies, timeout)) { 2900 idetape_create_test_unit_ready_cmd(&pc); 2901 if (!__idetape_queue_pc_tail(drive, &pc)) 2902 return 0; 2903 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2) 2904 || (tape->asc == 0x3A)) { /* no media */ 2905 if (load_attempted) 2906 return -ENOMEDIUM; 2907 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK); 2908 __idetape_queue_pc_tail(drive, &pc); 2909 load_attempted = 1; 2910 /* not about to be ready */ 2911 } else if (!(tape->sense_key == 2 && tape->asc == 4 && 2912 (tape->ascq == 1 || tape->ascq == 8))) 2913 return -EIO; 2914 msleep(100); 2915 } 2916 return -EIO; 2917} 2918 2919static int idetape_queue_pc_tail (ide_drive_t *drive,idetape_pc_t *pc) 2920{ 2921 return __idetape_queue_pc_tail(drive, pc); 2922} 2923 2924static int idetape_flush_tape_buffers (ide_drive_t *drive) 2925{ 2926 idetape_pc_t pc; 2927 int rc; 2928 2929 idetape_create_write_filemark_cmd(drive, &pc, 0); 2930 if ((rc = idetape_queue_pc_tail(drive, &pc))) 2931 return rc; 2932 idetape_wait_ready(drive, 60 * 5 * HZ); 2933 return 0; 2934} 2935 2936static void idetape_create_read_position_cmd (idetape_pc_t *pc) 2937{ 2938 idetape_init_pc(pc); 2939 pc->c[0] = IDETAPE_READ_POSITION_CMD; 2940 pc->request_transfer = 20; 2941 pc->callback = &idetape_read_position_callback; 2942} 2943 2944static int idetape_read_position (ide_drive_t *drive) 2945{ 2946 idetape_tape_t *tape = drive->driver_data; 2947 idetape_pc_t pc; 2948 int position; 2949 2950#if IDETAPE_DEBUG_LOG 2951 if (tape->debug_level >= 4) 2952 printk(KERN_INFO "ide-tape: Reached idetape_read_position\n"); 2953#endif /* IDETAPE_DEBUG_LOG */ 2954 2955 idetape_create_read_position_cmd(&pc); 2956 if (idetape_queue_pc_tail(drive, &pc)) 2957 return -1; 2958 position = tape->first_frame_position; 2959 return position; 2960} 2961 2962static void idetape_create_locate_cmd (ide_drive_t *drive, idetape_pc_t *pc, unsigned int block, u8 partition, int skip) 2963{ 2964 idetape_init_pc(pc); 2965 pc->c[0] = IDETAPE_LOCATE_CMD; 2966 pc->c[1] = 2; 2967 put_unaligned(htonl(block), (unsigned int *) &pc->c[3]); 2968 pc->c[8] = partition; 2969 set_bit(PC_WAIT_FOR_DSC, &pc->flags); 2970 pc->callback = &idetape_pc_callback; 2971} 2972 2973static int idetape_create_prevent_cmd (ide_drive_t *drive, idetape_pc_t *pc, int prevent) 2974{ 2975 idetape_tape_t *tape = drive->driver_data; 2976 2977 if (!tape->capabilities.lock) 2978 return 0; 2979 2980 idetape_init_pc(pc); 2981 pc->c[0] = IDETAPE_PREVENT_CMD; 2982 pc->c[4] = prevent; 2983 pc->callback = &idetape_pc_callback; 2984 return 1; 2985} 2986 2987static int __idetape_discard_read_pipeline (ide_drive_t *drive) 2988{ 2989 idetape_tape_t *tape = drive->driver_data; 2990 unsigned long flags; 2991 int cnt; 2992 2993 if (tape->chrdev_direction != idetape_direction_read) 2994 return 0; 2995 2996 /* Remove merge stage. */ 2997 cnt = tape->merge_stage_size / tape->tape_block_size; 2998 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags)) 2999 ++cnt; /* Filemarks count as 1 sector */ 3000 tape->merge_stage_size = 0; 3001 if (tape->merge_stage != NULL) { 3002 __idetape_kfree_stage(tape->merge_stage); 3003 tape->merge_stage = NULL; 3004 } 3005 3006 /* Clear pipeline flags. */ 3007 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags); 3008 tape->chrdev_direction = idetape_direction_none; 3009 3010 /* Remove pipeline stages. */ 3011 if (tape->first_stage == NULL) 3012 return 0; 3013 3014 spin_lock_irqsave(&tape->spinlock, flags); 3015 tape->next_stage = NULL; 3016 if (idetape_pipeline_active(tape)) 3017 idetape_wait_for_request(drive, tape->active_data_request); 3018 spin_unlock_irqrestore(&tape->spinlock, flags); 3019 3020 while (tape->first_stage != NULL) { 3021 struct request *rq_ptr = &tape->first_stage->rq; 3022 3023 cnt += rq_ptr->nr_sectors - rq_ptr->current_nr_sectors; 3024 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK) 3025 ++cnt; 3026 idetape_remove_stage_head(drive); 3027 } 3028 tape->nr_pending_stages = 0; 3029 tape->max_stages = tape->min_pipeline; 3030 return cnt; 3031} 3032 3033/* 3034 * idetape_position_tape positions the tape to the requested block 3035 * using the LOCATE packet command. A READ POSITION command is then 3036 * issued to check where we are positioned. 3037 * 3038 * Like all higher level operations, we queue the commands at the tail 3039 * of the request queue and wait for their completion. 3040 * 3041 */ 3042static int idetape_position_tape (ide_drive_t *drive, unsigned int block, u8 partition, int skip) 3043{ 3044 idetape_tape_t *tape = drive->driver_data; 3045 int retval; 3046 idetape_pc_t pc; 3047 3048 if (tape->chrdev_direction == idetape_direction_read) 3049 __idetape_discard_read_pipeline(drive); 3050 idetape_wait_ready(drive, 60 * 5 * HZ); 3051 idetape_create_locate_cmd(drive, &pc, block, partition, skip); 3052 retval = idetape_queue_pc_tail(drive, &pc); 3053 if (retval) 3054 return (retval); 3055 3056 idetape_create_read_position_cmd(&pc); 3057 return (idetape_queue_pc_tail(drive, &pc)); 3058} 3059 3060static void idetape_discard_read_pipeline (ide_drive_t *drive, int restore_position) 3061{ 3062 idetape_tape_t *tape = drive->driver_data; 3063 int cnt; 3064 int seek, position; 3065 3066 cnt = __idetape_discard_read_pipeline(drive); 3067 if (restore_position) { 3068 position = idetape_read_position(drive); 3069 seek = position > cnt ? position - cnt : 0; 3070 if (idetape_position_tape(drive, seek, 0, 0)) { 3071 printk(KERN_INFO "ide-tape: %s: position_tape failed in discard_pipeline()\n", tape->name); 3072 return; 3073 } 3074 } 3075} 3076 3077/* 3078 * idetape_queue_rw_tail generates a read/write request for the block 3079 * device interface and wait for it to be serviced. 3080 */ 3081static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, struct idetape_bh *bh) 3082{ 3083 idetape_tape_t *tape = drive->driver_data; 3084 struct request rq; 3085 3086#if IDETAPE_DEBUG_LOG 3087 if (tape->debug_level >= 2) 3088 printk(KERN_INFO "ide-tape: idetape_queue_rw_tail: cmd=%d\n",cmd); 3089#endif /* IDETAPE_DEBUG_LOG */ 3090#if IDETAPE_DEBUG_BUGS 3091 if (idetape_pipeline_active(tape)) { 3092 printk(KERN_ERR "ide-tape: bug: the pipeline is active in idetape_queue_rw_tail\n"); 3093 return (0); 3094 } 3095#endif /* IDETAPE_DEBUG_BUGS */ 3096 3097 idetape_init_rq(&rq, cmd); 3098 rq.rq_disk = tape->disk; 3099 rq.special = (void *)bh; 3100 rq.sector = tape->first_frame_position; 3101 rq.nr_sectors = rq.current_nr_sectors = blocks; 3102 (void) ide_do_drive_cmd(drive, &rq, ide_wait); 3103 3104 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0) 3105 return 0; 3106 3107 if (tape->merge_stage) 3108 idetape_init_merge_stage(tape); 3109 if (rq.errors == IDETAPE_ERROR_GENERAL) 3110 return -EIO; 3111 return (tape->tape_block_size * (blocks-rq.current_nr_sectors)); 3112} 3113 3114/* 3115 * idetape_insert_pipeline_into_queue is used to start servicing the 3116 * pipeline stages, starting from tape->next_stage. 3117 */ 3118static void idetape_insert_pipeline_into_queue (ide_drive_t *drive) 3119{ 3120 idetape_tape_t *tape = drive->driver_data; 3121 3122 if (tape->next_stage == NULL) 3123 return; 3124 if (!idetape_pipeline_active(tape)) { 3125 set_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags); 3126 idetape_active_next_stage(drive); 3127 (void) ide_do_drive_cmd(drive, tape->active_data_request, ide_end); 3128 } 3129} 3130 3131static void idetape_create_inquiry_cmd (idetape_pc_t *pc) 3132{ 3133 idetape_init_pc(pc); 3134 pc->c[0] = IDETAPE_INQUIRY_CMD; 3135 pc->c[4] = pc->request_transfer = 254; 3136 pc->callback = &idetape_pc_callback; 3137} 3138 3139static void idetape_create_rewind_cmd (ide_drive_t *drive, idetape_pc_t *pc) 3140{ 3141 idetape_init_pc(pc); 3142 pc->c[0] = IDETAPE_REWIND_CMD; 3143 set_bit(PC_WAIT_FOR_DSC, &pc->flags); 3144 pc->callback = &idetape_pc_callback; 3145} 3146 3147#if 0 3148static void idetape_create_mode_select_cmd (idetape_pc_t *pc, int length) 3149{ 3150 idetape_init_pc(pc); 3151 set_bit(PC_WRITING, &pc->flags); 3152 pc->c[0] = IDETAPE_MODE_SELECT_CMD; 3153 pc->c[1] = 0x10; 3154 put_unaligned(htons(length), (unsigned short *) &pc->c[3]); 3155 pc->request_transfer = 255; 3156 pc->callback = &idetape_pc_callback; 3157} 3158#endif 3159 3160static void idetape_create_erase_cmd (idetape_pc_t *pc) 3161{ 3162 idetape_init_pc(pc); 3163 pc->c[0] = IDETAPE_ERASE_CMD; 3164 pc->c[1] = 1; 3165 set_bit(PC_WAIT_FOR_DSC, &pc->flags); 3166 pc->callback = &idetape_pc_callback; 3167} 3168 3169static void idetape_create_space_cmd (idetape_pc_t *pc,int count, u8 cmd) 3170{ 3171 idetape_init_pc(pc); 3172 pc->c[0] = IDETAPE_SPACE_CMD; 3173 put_unaligned(htonl(count), (unsigned int *) &pc->c[1]); 3174 pc->c[1] = cmd; 3175 set_bit(PC_WAIT_FOR_DSC, &pc->flags); 3176 pc->callback = &idetape_pc_callback; 3177} 3178 3179static void idetape_wait_first_stage (ide_drive_t *drive) 3180{ 3181 idetape_tape_t *tape = drive->driver_data; 3182 unsigned long flags; 3183 3184 if (tape->first_stage == NULL) 3185 return; 3186 spin_lock_irqsave(&tape->spinlock, flags); 3187 if (tape->active_stage == tape->first_stage) 3188 idetape_wait_for_request(drive, tape->active_data_request); 3189 spin_unlock_irqrestore(&tape->spinlock, flags); 3190} 3191 3192/* 3193 * idetape_add_chrdev_write_request tries to add a character device 3194 * originated write request to our pipeline. In case we don't succeed, 3195 * we revert to non-pipelined operation mode for this request. 3196 * 3197 * 1. Try to allocate a new pipeline stage. 3198 * 2. If we can't, wait for more and more requests to be serviced 3199 * and try again each time. 3200 * 3. If we still can't allocate a stage, fallback to 3201 * non-pipelined operation mode for this request. 3202 */ 3203static int idetape_add_chrdev_write_request (ide_drive_t *drive, int blocks) 3204{ 3205 idetape_tape_t *tape = drive->driver_data; 3206 idetape_stage_t *new_stage; 3207 unsigned long flags; 3208 struct request *rq; 3209 3210#if IDETAPE_DEBUG_LOG 3211 if (tape->debug_level >= 3) 3212 printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_write_request\n"); 3213#endif /* IDETAPE_DEBUG_LOG */ 3214 3215 /* 3216 * Attempt to allocate a new stage. 3217 * Pay special attention to possible race conditions. 3218 */ 3219 while ((new_stage = idetape_kmalloc_stage(tape)) == NULL) { 3220 spin_lock_irqsave(&tape->spinlock, flags); 3221 if (idetape_pipeline_active(tape)) { 3222 idetape_wait_for_request(drive, tape->active_data_request); 3223 spin_unlock_irqrestore(&tape->spinlock, flags); 3224 } else { 3225 spin_unlock_irqrestore(&tape->spinlock, flags); 3226 idetape_insert_pipeline_into_queue(drive); 3227 if (idetape_pipeline_active(tape)) 3228 continue; 3229 /* 3230 * Linux is short on memory. Fallback to 3231 * non-pipelined operation mode for this request. 3232 */ 3233 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh); 3234 } 3235 } 3236 rq = &new_stage->rq; 3237 idetape_init_rq(rq, REQ_IDETAPE_WRITE); 3238 /* Doesn't actually matter - We always assume sequential access */ 3239 rq->sector = tape->first_frame_position; 3240 rq->nr_sectors = rq->current_nr_sectors = blocks; 3241 3242 idetape_switch_buffers(tape, new_stage); 3243 idetape_add_stage_tail(drive, new_stage); 3244 tape->pipeline_head++; 3245#if USE_IOTRACE 3246 IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor); 3247#endif 3248 calculate_speeds(drive); 3249 3250 /* 3251 * Estimate whether the tape has stopped writing by checking 3252 * if our write pipeline is currently empty. If we are not 3253 * writing anymore, wait for the pipeline to be full enough 3254 * (90%) before starting to service requests, so that we will 3255 * be able to keep up with the higher speeds of the tape. 3256 */ 3257 if (!idetape_pipeline_active(tape)) { 3258 if (tape->nr_stages >= tape->max_stages * 9 / 10 || 3259 tape->nr_stages >= tape->max_stages - tape->uncontrolled_pipeline_head_speed * 3 * 1024 / tape->tape_block_size) { 3260 tape->measure_insert_time = 1; 3261 tape->insert_time = jiffies; 3262 tape->insert_size = 0; 3263 tape->insert_speed = 0; 3264 idetape_insert_pipeline_into_queue(drive); 3265 } 3266 } 3267 if (test_and_clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags)) 3268 /* Return a deferred error */ 3269 return -EIO; 3270 return blocks; 3271} 3272 3273/* 3274 * idetape_wait_for_pipeline will wait until all pending pipeline 3275 * requests are serviced. Typically called on device close. 3276 */ 3277static void idetape_wait_for_pipeline (ide_drive_t *drive) 3278{ 3279 idetape_tape_t *tape = drive->driver_data; 3280 unsigned long flags; 3281 3282 while (tape->next_stage || idetape_pipeline_active(tape)) { 3283 idetape_insert_pipeline_into_queue(drive); 3284 spin_lock_irqsave(&tape->spinlock, flags); 3285 if (idetape_pipeline_active(tape)) 3286 idetape_wait_for_request(drive, tape->active_data_request); 3287 spin_unlock_irqrestore(&tape->spinlock, flags); 3288 } 3289} 3290 3291static void idetape_empty_write_pipeline (ide_drive_t *drive) 3292{ 3293 idetape_tape_t *tape = drive->driver_data; 3294 int blocks, min; 3295 struct idetape_bh *bh; 3296 3297#if IDETAPE_DEBUG_BUGS 3298 if (tape->chrdev_direction != idetape_direction_write) { 3299 printk(KERN_ERR "ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n"); 3300 return; 3301 } 3302 if (tape->merge_stage_size > tape->stage_size) { 3303 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n"); 3304 tape->merge_stage_size = tape->stage_size; 3305 } 3306#endif /* IDETAPE_DEBUG_BUGS */ 3307 if (tape->merge_stage_size) { 3308 blocks = tape->merge_stage_size / tape->tape_block_size; 3309 if (tape->merge_stage_size % tape->tape_block_size) { 3310 unsigned int i; 3311 3312 blocks++; 3313 i = tape->tape_block_size - tape->merge_stage_size % tape->tape_block_size; 3314 bh = tape->bh->b_reqnext; 3315 while (bh) { 3316 atomic_set(&bh->b_count, 0); 3317 bh = bh->b_reqnext; 3318 } 3319 bh = tape->bh; 3320 while (i) { 3321 if (bh == NULL) { 3322 3323 printk(KERN_INFO "ide-tape: bug, bh NULL\n"); 3324 break; 3325 } 3326 min = min(i, (unsigned int)(bh->b_size - atomic_read(&bh->b_count))); 3327 memset(bh->b_data + atomic_read(&bh->b_count), 0, min); 3328 atomic_add(min, &bh->b_count); 3329 i -= min; 3330 bh = bh->b_reqnext; 3331 } 3332 } 3333 (void) idetape_add_chrdev_write_request(drive, blocks); 3334 tape->merge_stage_size = 0; 3335 } 3336 idetape_wait_for_pipeline(drive); 3337 if (tape->merge_stage != NULL) { 3338 __idetape_kfree_stage(tape->merge_stage); 3339 tape->merge_stage = NULL; 3340 } 3341 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags); 3342 tape->chrdev_direction = idetape_direction_none; 3343 3344 /* 3345 * On the next backup, perform the feedback loop again. 3346 * (I don't want to keep sense information between backups, 3347 * as some systems are constantly on, and the system load 3348 * can be totally different on the next backup). 3349 */ 3350 tape->max_stages = tape->min_pipeline; 3351#if IDETAPE_DEBUG_BUGS 3352 if (tape->first_stage != NULL || 3353 tape->next_stage != NULL || 3354 tape->last_stage != NULL || 3355 tape->nr_stages != 0) { 3356 printk(KERN_ERR "ide-tape: ide-tape pipeline bug, " 3357 "first_stage %p, next_stage %p, " 3358 "last_stage %p, nr_stages %d\n", 3359 tape->first_stage, tape->next_stage, 3360 tape->last_stage, tape->nr_stages); 3361 } 3362#endif /* IDETAPE_DEBUG_BUGS */ 3363} 3364 3365static void idetape_restart_speed_control (ide_drive_t *drive) 3366{ 3367 idetape_tape_t *tape = drive->driver_data; 3368 3369 tape->restart_speed_control_req = 0; 3370 tape->pipeline_head = 0; 3371 tape->controlled_last_pipeline_head = tape->uncontrolled_last_pipeline_head = 0; 3372 tape->controlled_previous_pipeline_head = tape->uncontrolled_previous_pipeline_head = 0; 3373 tape->pipeline_head_speed = tape->controlled_pipeline_head_speed = 5000; 3374 tape->uncontrolled_pipeline_head_speed = 0; 3375 tape->controlled_pipeline_head_time = tape->uncontrolled_pipeline_head_time = jiffies; 3376 tape->controlled_previous_head_time = tape->uncontrolled_previous_head_time = jiffies; 3377} 3378 3379static int idetape_initiate_read (ide_drive_t *drive, int max_stages) 3380{ 3381 idetape_tape_t *tape = drive->driver_data; 3382 idetape_stage_t *new_stage; 3383 struct request rq; 3384 int bytes_read; 3385 int blocks = tape->capabilities.ctl; 3386 3387 /* Initialize read operation */ 3388 if (tape->chrdev_direction != idetape_direction_read) { 3389 if (tape->chrdev_direction == idetape_direction_write) { 3390 idetape_empty_write_pipeline(drive); 3391 idetape_flush_tape_buffers(drive); 3392 } 3393#if IDETAPE_DEBUG_BUGS 3394 if (tape->merge_stage || tape->merge_stage_size) { 3395 printk (KERN_ERR "ide-tape: merge_stage_size should be 0 now\n"); 3396 tape->merge_stage_size = 0; 3397 } 3398#endif /* IDETAPE_DEBUG_BUGS */ 3399 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL) 3400 return -ENOMEM; 3401 tape->chrdev_direction = idetape_direction_read; 3402 3403 /* 3404 * Issue a read 0 command to ensure that DSC handshake 3405 * is switched from completion mode to buffer available 3406 * mode. 3407 * No point in issuing this if DSC overlap isn't supported, 3408 * some drives (Seagate STT3401A) will return an error. 3409 */ 3410 if (drive->dsc_overlap) { 3411 bytes_read = idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, 0, tape->merge_stage->bh); 3412 if (bytes_read < 0) { 3413 __idetape_kfree_stage(tape->merge_stage); 3414 tape->merge_stage = NULL; 3415 tape->chrdev_direction = idetape_direction_none; 3416 return bytes_read; 3417 } 3418 } 3419 } 3420 if (tape->restart_speed_control_req) 3421 idetape_restart_speed_control(drive); 3422 idetape_init_rq(&rq, REQ_IDETAPE_READ); 3423 rq.sector = tape->first_frame_position; 3424 rq.nr_sectors = rq.current_nr_sectors = blocks; 3425 if (!test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags) && 3426 tape->nr_stages < max_stages) { 3427 new_stage = idetape_kmalloc_stage(tape); 3428 while (new_stage != NULL) { 3429 new_stage->rq = rq; 3430 idetape_add_stage_tail(drive, new_stage); 3431 if (tape->nr_stages >= max_stages) 3432 break; 3433 new_stage = idetape_kmalloc_stage(tape); 3434 } 3435 } 3436 if (!idetape_pipeline_active(tape)) { 3437 if (tape->nr_pending_stages >= 3 * max_stages / 4) { 3438 tape->measure_insert_time = 1; 3439 tape->insert_time = jiffies; 3440 tape->insert_size = 0; 3441 tape->insert_speed = 0; 3442 idetape_insert_pipeline_into_queue(drive); 3443 } 3444 } 3445 return 0; 3446} 3447 3448/* 3449 * idetape_add_chrdev_read_request is called from idetape_chrdev_read 3450 * to service a character device read request and add read-ahead 3451 * requests to our pipeline. 3452 */ 3453static int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks) 3454{ 3455 idetape_tape_t *tape = drive->driver_data; 3456 unsigned long flags; 3457 struct request *rq_ptr; 3458 int bytes_read; 3459 3460#if IDETAPE_DEBUG_LOG 3461 if (tape->debug_level >= 4) 3462 printk(KERN_INFO "ide-tape: Reached idetape_add_chrdev_read_request, %d blocks\n", blocks); 3463#endif /* IDETAPE_DEBUG_LOG */ 3464 3465 /* 3466 * If we are at a filemark, return a read length of 0 3467 */ 3468 if (test_bit(IDETAPE_FILEMARK, &tape->flags)) 3469 return 0; 3470 3471 /* 3472 * Wait for the next block to be available at the head 3473 * of the pipeline 3474 */ 3475 idetape_initiate_read(drive, tape->max_stages); 3476 if (tape->first_stage == NULL) { 3477 if (test_bit(IDETAPE_PIPELINE_ERROR, &tape->flags)) 3478 return 0; 3479 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks, tape->merge_stage->bh); 3480 } 3481 idetape_wait_first_stage(drive); 3482 rq_ptr = &tape->first_stage->rq; 3483 bytes_read = tape->tape_block_size * (rq_ptr->nr_sectors - rq_ptr->current_nr_sectors); 3484 rq_ptr->nr_sectors = rq_ptr->current_nr_sectors = 0; 3485 3486 3487 if (rq_ptr->errors == IDETAPE_ERROR_EOD) 3488 return 0; 3489 else { 3490 idetape_switch_buffers(tape, tape->first_stage); 3491 if (rq_ptr->errors == IDETAPE_ERROR_FILEMARK) 3492 set_bit(IDETAPE_FILEMARK, &tape->flags); 3493 spin_lock_irqsave(&tape->spinlock, flags); 3494 idetape_remove_stage_head(drive); 3495 spin_unlock_irqrestore(&tape->spinlock, flags); 3496 tape->pipeline_head++; 3497#if USE_IOTRACE 3498 IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor); 3499#endif 3500 calculate_speeds(drive); 3501 } 3502#if IDETAPE_DEBUG_BUGS 3503 if (bytes_read > blocks * tape->tape_block_size) { 3504 printk(KERN_ERR "ide-tape: bug: trying to return more bytes than requested\n"); 3505 bytes_read = blocks * tape->tape_block_size; 3506 } 3507#endif /* IDETAPE_DEBUG_BUGS */ 3508 return (bytes_read); 3509} 3510 3511static void idetape_pad_zeros (ide_drive_t *drive, int bcount) 3512{ 3513 idetape_tape_t *tape = drive->driver_data; 3514 struct idetape_bh *bh; 3515 int blocks; 3516 3517 while (bcount) { 3518 unsigned int count; 3519 3520 bh = tape->merge_stage->bh; 3521 count = min(tape->stage_size, bcount); 3522 bcount -= count; 3523 blocks = count / tape->tape_block_size; 3524 while (count) { 3525 atomic_set(&bh->b_count, min(count, (unsigned int)bh->b_size)); 3526 memset(bh->b_data, 0, atomic_read(&bh->b_count)); 3527 count -= atomic_read(&bh->b_count); 3528 bh = bh->b_reqnext; 3529 } 3530 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, tape->merge_stage->bh); 3531 } 3532} 3533 3534static int idetape_pipeline_size (ide_drive_t *drive) 3535{ 3536 idetape_tape_t *tape = drive->driver_data; 3537 idetape_stage_t *stage; 3538 struct request *rq; 3539 int size = 0; 3540 3541 idetape_wait_for_pipeline(drive); 3542 stage = tape->first_stage; 3543 while (stage != NULL) { 3544 rq = &stage->rq; 3545 size += tape->tape_block_size * (rq->nr_sectors-rq->current_nr_sectors); 3546 if (rq->errors == IDETAPE_ERROR_FILEMARK) 3547 size += tape->tape_block_size; 3548 stage = stage->next; 3549 } 3550 size += tape->merge_stage_size; 3551 return size; 3552} 3553 3554/* 3555 * Rewinds the tape to the Beginning Of the current Partition (BOP). 3556 * 3557 * We currently support only one partition. 3558 */ 3559static int idetape_rewind_tape (ide_drive_t *drive) 3560{ 3561 int retval; 3562 idetape_pc_t pc; 3563#if IDETAPE_DEBUG_LOG 3564 idetape_tape_t *tape = drive->driver_data; 3565 if (tape->debug_level >= 2) 3566 printk(KERN_INFO "ide-tape: Reached idetape_rewind_tape\n"); 3567#endif /* IDETAPE_DEBUG_LOG */ 3568 3569 idetape_create_rewind_cmd(drive, &pc); 3570 retval = idetape_queue_pc_tail(drive, &pc); 3571 if (retval) 3572 return retval; 3573 3574 idetape_create_read_position_cmd(&pc); 3575 retval = idetape_queue_pc_tail(drive, &pc); 3576 if (retval) 3577 return retval; 3578 return 0; 3579} 3580 3581/* 3582 * Our special ide-tape ioctl's. 3583 * 3584 * Currently there aren't any ioctl's. 3585 * mtio.h compatible commands should be issued to the character device 3586 * interface. 3587 */ 3588static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, unsigned long arg) 3589{ 3590 idetape_tape_t *tape = drive->driver_data; 3591 idetape_config_t config; 3592 void __user *argp = (void __user *)arg; 3593 3594#if IDETAPE_DEBUG_LOG 3595 if (tape->debug_level >= 4) 3596 printk(KERN_INFO "ide-tape: Reached idetape_blkdev_ioctl\n"); 3597#endif /* IDETAPE_DEBUG_LOG */ 3598 switch (cmd) { 3599 case 0x0340: 3600 if (copy_from_user(&config, argp, sizeof (idetape_config_t))) 3601 return -EFAULT; 3602 tape->best_dsc_rw_frequency = config.dsc_rw_frequency; 3603 tape->max_stages = config.nr_stages; 3604 break; 3605 case 0x0350: 3606 config.dsc_rw_frequency = (int) tape->best_dsc_rw_frequency; 3607 config.nr_stages = tape->max_stages; 3608 if (copy_to_user(argp, &config, sizeof (idetape_config_t))) 3609 return -EFAULT; 3610 break; 3611 default: 3612 return -EIO; 3613 } 3614 return 0; 3615} 3616 3617/* 3618 * idetape_space_over_filemarks is now a bit more complicated than just 3619 * passing the command to the tape since we may have crossed some 3620 * filemarks during our pipelined read-ahead mode. 3621 * 3622 * As a minor side effect, the pipeline enables us to support MTFSFM when 3623 * the filemark is in our internal pipeline even if the tape doesn't 3624 * support spacing over filemarks in the reverse direction. 3625 */ 3626static int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count) 3627{ 3628 idetape_tape_t *tape = drive->driver_data; 3629 idetape_pc_t pc; 3630 unsigned long flags; 3631 int retval,count=0; 3632 3633 if (mt_count == 0) 3634 return 0; 3635 if (MTBSF == mt_op || MTBSFM == mt_op) { 3636 if (!tape->capabilities.sprev) 3637 return -EIO; 3638 mt_count = - mt_count; 3639 } 3640 3641 if (tape->chrdev_direction == idetape_direction_read) { 3642 /* 3643 * We have a read-ahead buffer. Scan it for crossed 3644 * filemarks. 3645 */ 3646 tape->merge_stage_size = 0; 3647 if (test_and_clear_bit(IDETAPE_FILEMARK, &tape->flags)) 3648 ++count; 3649 while (tape->first_stage != NULL) { 3650 if (count == mt_count) { 3651 if (mt_op == MTFSFM) 3652 set_bit(IDETAPE_FILEMARK, &tape->flags); 3653 return 0; 3654 } 3655 spin_lock_irqsave(&tape->spinlock, flags); 3656 if (tape->first_stage == tape->active_stage) { 3657 /* 3658 * We have reached the active stage in the read pipeline. 3659 * There is no point in allowing the drive to continue 3660 * reading any farther, so we stop the pipeline. 3661 * 3662 * This section should be moved to a separate subroutine, 3663 * because a similar function is performed in 3664 * __idetape_discard_read_pipeline(), for example. 3665 */ 3666 tape->next_stage = NULL; 3667 spin_unlock_irqrestore(&tape->spinlock, flags); 3668 idetape_wait_first_stage(drive); 3669 tape->next_stage = tape->first_stage->next; 3670 } else 3671 spin_unlock_irqrestore(&tape->spinlock, flags); 3672 if (tape->first_stage->rq.errors == IDETAPE_ERROR_FILEMARK) 3673 ++count; 3674 idetape_remove_stage_head(drive); 3675 } 3676 idetape_discard_read_pipeline(drive, 0); 3677 } 3678 3679 /* 3680 * The filemark was not found in our internal pipeline. 3681 * Now we can issue the space command. 3682 */ 3683 switch (mt_op) { 3684 case MTFSF: 3685 case MTBSF: 3686 idetape_create_space_cmd(&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK); 3687 return (idetape_queue_pc_tail(drive, &pc)); 3688 case MTFSFM: 3689 case MTBSFM: 3690 if (!tape->capabilities.sprev) 3691 return (-EIO); 3692 retval = idetape_space_over_filemarks(drive, MTFSF, mt_count-count); 3693 if (retval) return (retval); 3694 count = (MTBSFM == mt_op ? 1 : -1); 3695 return (idetape_space_over_filemarks(drive, MTFSF, count)); 3696 default: 3697 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",mt_op); 3698 return (-EIO); 3699 } 3700} 3701 3702 3703/* 3704 * Our character device read / write functions. 3705 * 3706 * The tape is optimized to maximize throughput when it is transferring 3707 * an integral number of the "continuous transfer limit", which is 3708 * a parameter of the specific tape (26 KB on my particular tape). 3709 * (32 kB for Onstream) 3710 * 3711 * As of version 1.3 of the driver, the character device provides an 3712 * abstract continuous view of the media - any mix of block sizes (even 1 3713 * byte) on the same backup/restore procedure is supported. The driver 3714 * will internally convert the requests to the recommended transfer unit, 3715 * so that an unmatch between the user's block size to the recommended 3716 * size will only result in a (slightly) increased driver overhead, but 3717 * will no longer hit performance. 3718 * This is not applicable to Onstream. 3719 */ 3720static ssize_t idetape_chrdev_read (struct file *file, char __user *buf, 3721 size_t count, loff_t *ppos) 3722{ 3723 struct ide_tape_obj *tape = ide_tape_f(file); 3724 ide_drive_t *drive = tape->drive; 3725 ssize_t bytes_read,temp, actually_read = 0, rc; 3726 ssize_t ret = 0; 3727 3728#if IDETAPE_DEBUG_LOG 3729 if (tape->debug_level >= 3) 3730 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_read, count %Zd\n", count); 3731#endif /* IDETAPE_DEBUG_LOG */ 3732 3733 if (tape->chrdev_direction != idetape_direction_read) { 3734 if (test_bit(IDETAPE_DETECT_BS, &tape->flags)) 3735 if (count > tape->tape_block_size && 3736 (count % tape->tape_block_size) == 0) 3737 tape->user_bs_factor = count / tape->tape_block_size; 3738 } 3739 if ((rc = idetape_initiate_read(drive, tape->max_stages)) < 0) 3740 return rc; 3741 if (count == 0) 3742 return (0); 3743 if (tape->merge_stage_size) { 3744 actually_read = min((unsigned int)(tape->merge_stage_size), (unsigned int)count); 3745 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, actually_read)) 3746 ret = -EFAULT; 3747 buf += actually_read; 3748 tape->merge_stage_size -= actually_read; 3749 count -= actually_read; 3750 } 3751 while (count >= tape->stage_size) { 3752 bytes_read = idetape_add_chrdev_read_request(drive, tape->capabilities.ctl); 3753 if (bytes_read <= 0) 3754 goto finish; 3755 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, bytes_read)) 3756 ret = -EFAULT; 3757 buf += bytes_read; 3758 count -= bytes_read; 3759 actually_read += bytes_read; 3760 } 3761 if (count) { 3762 bytes_read = idetape_add_chrdev_read_request(drive, tape->capabilities.ctl); 3763 if (bytes_read <= 0) 3764 goto finish; 3765 temp = min((unsigned long)count, (unsigned long)bytes_read); 3766 if (idetape_copy_stage_to_user(tape, buf, tape->merge_stage, temp)) 3767 ret = -EFAULT; 3768 actually_read += temp; 3769 tape->merge_stage_size = bytes_read-temp; 3770 } 3771finish: 3772 if (!actually_read && test_bit(IDETAPE_FILEMARK, &tape->flags)) { 3773#if IDETAPE_DEBUG_LOG 3774 if (tape->debug_level >= 2) 3775 printk(KERN_INFO "ide-tape: %s: spacing over filemark\n", tape->name); 3776#endif 3777 idetape_space_over_filemarks(drive, MTFSF, 1); 3778 return 0; 3779 } 3780 3781 return (ret) ? ret : actually_read; 3782} 3783 3784static ssize_t idetape_chrdev_write (struct file *file, const char __user *buf, 3785 size_t count, loff_t *ppos) 3786{ 3787 struct ide_tape_obj *tape = ide_tape_f(file); 3788 ide_drive_t *drive = tape->drive; 3789 ssize_t actually_written = 0; 3790 ssize_t ret = 0; 3791 3792 /* The drive is write protected. */ 3793 if (tape->write_prot) 3794 return -EACCES; 3795 3796#if IDETAPE_DEBUG_LOG 3797 if (tape->debug_level >= 3) 3798 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_write, " 3799 "count %Zd\n", count); 3800#endif /* IDETAPE_DEBUG_LOG */ 3801 3802 /* Initialize write operation */ 3803 if (tape->chrdev_direction != idetape_direction_write) { 3804 if (tape->chrdev_direction == idetape_direction_read) 3805 idetape_discard_read_pipeline(drive, 1); 3806#if IDETAPE_DEBUG_BUGS 3807 if (tape->merge_stage || tape->merge_stage_size) { 3808 printk(KERN_ERR "ide-tape: merge_stage_size " 3809 "should be 0 now\n"); 3810 tape->merge_stage_size = 0; 3811 } 3812#endif /* IDETAPE_DEBUG_BUGS */ 3813 if ((tape->merge_stage = __idetape_kmalloc_stage(tape, 0, 0)) == NULL) 3814 return -ENOMEM; 3815 tape->chrdev_direction = idetape_direction_write; 3816 idetape_init_merge_stage(tape); 3817 3818 /* 3819 * Issue a write 0 command to ensure that DSC handshake 3820 * is switched from completion mode to buffer available 3821 * mode. 3822 * No point in issuing this if DSC overlap isn't supported, 3823 * some drives (Seagate STT3401A) will return an error. 3824 */ 3825 if (drive->dsc_overlap) { 3826 ssize_t retval = idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, 0, tape->merge_stage->bh); 3827 if (retval < 0) { 3828 __idetape_kfree_stage(tape->merge_stage); 3829 tape->merge_stage = NULL; 3830 tape->chrdev_direction = idetape_direction_none; 3831 return retval; 3832 } 3833 } 3834 } 3835 if (count == 0) 3836 return (0); 3837 if (tape->restart_speed_control_req) 3838 idetape_restart_speed_control(drive); 3839 if (tape->merge_stage_size) { 3840#if IDETAPE_DEBUG_BUGS 3841 if (tape->merge_stage_size >= tape->stage_size) { 3842 printk(KERN_ERR "ide-tape: bug: merge buffer too big\n"); 3843 tape->merge_stage_size = 0; 3844 } 3845#endif /* IDETAPE_DEBUG_BUGS */ 3846 actually_written = min((unsigned int)(tape->stage_size - tape->merge_stage_size), (unsigned int)count); 3847 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, actually_written)) 3848 ret = -EFAULT; 3849 buf += actually_written; 3850 tape->merge_stage_size += actually_written; 3851 count -= actually_written; 3852 3853 if (tape->merge_stage_size == tape->stage_size) { 3854 ssize_t retval; 3855 tape->merge_stage_size = 0; 3856 retval = idetape_add_chrdev_write_request(drive, tape->capabilities.ctl); 3857 if (retval <= 0) 3858 return (retval); 3859 } 3860 } 3861 while (count >= tape->stage_size) { 3862 ssize_t retval; 3863 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, tape->stage_size)) 3864 ret = -EFAULT; 3865 buf += tape->stage_size; 3866 count -= tape->stage_size; 3867 retval = idetape_add_chrdev_write_request(drive, tape->capabilities.ctl); 3868 actually_written += tape->stage_size; 3869 if (retval <= 0) 3870 return (retval); 3871 } 3872 if (count) { 3873 actually_written += count; 3874 if (idetape_copy_stage_from_user(tape, tape->merge_stage, buf, count)) 3875 ret = -EFAULT; 3876 tape->merge_stage_size += count; 3877 } 3878 return (ret) ? ret : actually_written; 3879} 3880 3881static int idetape_write_filemark (ide_drive_t *drive) 3882{ 3883 idetape_pc_t pc; 3884 3885 /* Write a filemark */ 3886 idetape_create_write_filemark_cmd(drive, &pc, 1); 3887 if (idetape_queue_pc_tail(drive, &pc)) { 3888 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n"); 3889 return -EIO; 3890 } 3891 return 0; 3892} 3893 3894/* 3895 * idetape_mtioctop is called from idetape_chrdev_ioctl when 3896 * the general mtio MTIOCTOP ioctl is requested. 3897 * 3898 * We currently support the following mtio.h operations: 3899 * 3900 * MTFSF - Space over mt_count filemarks in the positive direction. 3901 * The tape is positioned after the last spaced filemark. 3902 * 3903 * MTFSFM - Same as MTFSF, but the tape is positioned before the 3904 * last filemark. 3905 * 3906 * MTBSF - Steps background over mt_count filemarks, tape is 3907 * positioned before the last filemark. 3908 * 3909 * MTBSFM - Like MTBSF, only tape is positioned after the last filemark. 3910 * 3911 * Note: 3912 * 3913 * MTBSF and MTBSFM are not supported when the tape doesn't 3914 * support spacing over filemarks in the reverse direction. 3915 * In this case, MTFSFM is also usually not supported (it is 3916 * supported in the rare case in which we crossed the filemark 3917 * during our read-ahead pipelined operation mode). 3918 * 3919 * MTWEOF - Writes mt_count filemarks. Tape is positioned after 3920 * the last written filemark. 3921 * 3922 * MTREW - Rewinds tape. 3923 * 3924 * MTLOAD - Loads the tape. 3925 * 3926 * MTOFFL - Puts the tape drive "Offline": Rewinds the tape and 3927 * MTUNLOAD prevents further access until the media is replaced. 3928 * 3929 * MTNOP - Flushes tape buffers. 3930 * 3931 * MTRETEN - Retension media. This typically consists of one end 3932 * to end pass on the media. 3933 * 3934 * MTEOM - Moves to the end of recorded data. 3935 * 3936 * MTERASE - Erases tape. 3937 * 3938 * MTSETBLK - Sets the user block size to mt_count bytes. If 3939 * mt_count is 0, we will attempt to autodetect 3940 * the block size. 3941 * 3942 * MTSEEK - Positions the tape in a specific block number, where 3943 * each block is assumed to contain which user_block_size 3944 * bytes. 3945 * 3946 * MTSETPART - Switches to another tape partition. 3947 * 3948 * MTLOCK - Locks the tape door. 3949 * 3950 * MTUNLOCK - Unlocks the tape door. 3951 * 3952 * The following commands are currently not supported: 3953 * 3954 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, 3955 * MTSETDRVBUFFER, MT_ST_BOOLEANS, MT_ST_WRITE_THRESHOLD. 3956 */ 3957static int idetape_mtioctop (ide_drive_t *drive,short mt_op,int mt_count) 3958{ 3959 idetape_tape_t *tape = drive->driver_data; 3960 idetape_pc_t pc; 3961 int i,retval; 3962 3963#if IDETAPE_DEBUG_LOG 3964 if (tape->debug_level >= 1) 3965 printk(KERN_INFO "ide-tape: Handling MTIOCTOP ioctl: " 3966 "mt_op=%d, mt_count=%d\n", mt_op, mt_count); 3967#endif /* IDETAPE_DEBUG_LOG */ 3968 /* 3969 * Commands which need our pipelined read-ahead stages. 3970 */ 3971 switch (mt_op) { 3972 case MTFSF: 3973 case MTFSFM: 3974 case MTBSF: 3975 case MTBSFM: 3976 if (!mt_count) 3977 return (0); 3978 return (idetape_space_over_filemarks(drive,mt_op,mt_count)); 3979 default: 3980 break; 3981 } 3982 switch (mt_op) { 3983 case MTWEOF: 3984 if (tape->write_prot) 3985 return -EACCES; 3986 idetape_discard_read_pipeline(drive, 1); 3987 for (i = 0; i < mt_count; i++) { 3988 retval = idetape_write_filemark(drive); 3989 if (retval) 3990 return retval; 3991 } 3992 return (0); 3993 case MTREW: 3994 idetape_discard_read_pipeline(drive, 0); 3995 if (idetape_rewind_tape(drive)) 3996 return -EIO; 3997 return 0; 3998 case MTLOAD: 3999 idetape_discard_read_pipeline(drive, 0); 4000 idetape_create_load_unload_cmd(drive, &pc, IDETAPE_LU_LOAD_MASK); 4001 return (idetape_queue_pc_tail(drive, &pc)); 4002 case MTUNLOAD: 4003 case MTOFFL: 4004 /* 4005 * If door is locked, attempt to unlock before 4006 * attempting to eject. 4007 */ 4008 if (tape->door_locked) { 4009 if (idetape_create_prevent_cmd(drive, &pc, 0)) 4010 if (!idetape_queue_pc_tail(drive, &pc)) 4011 tape->door_locked = DOOR_UNLOCKED; 4012 } 4013 idetape_discard_read_pipeline(drive, 0); 4014 idetape_create_load_unload_cmd(drive, &pc,!IDETAPE_LU_LOAD_MASK); 4015 retval = idetape_queue_pc_tail(drive, &pc); 4016 if (!retval) 4017 clear_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags); 4018 return retval; 4019 case MTNOP: 4020 idetape_discard_read_pipeline(drive, 0); 4021 return (idetape_flush_tape_buffers(drive)); 4022 case MTRETEN: 4023 idetape_discard_read_pipeline(drive, 0); 4024 idetape_create_load_unload_cmd(drive, &pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK); 4025 return (idetape_queue_pc_tail(drive, &pc)); 4026 case MTEOM: 4027 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD); 4028 return (idetape_queue_pc_tail(drive, &pc)); 4029 case MTERASE: 4030 (void) idetape_rewind_tape(drive); 4031 idetape_create_erase_cmd(&pc); 4032 return (idetape_queue_pc_tail(drive, &pc)); 4033 case MTSETBLK: 4034 if (mt_count) { 4035 if (mt_count < tape->tape_block_size || mt_count % tape->tape_block_size) 4036 return -EIO; 4037 tape->user_bs_factor = mt_count / tape->tape_block_size; 4038 clear_bit(IDETAPE_DETECT_BS, &tape->flags); 4039 } else 4040 set_bit(IDETAPE_DETECT_BS, &tape->flags); 4041 return 0; 4042 case MTSEEK: 4043 idetape_discard_read_pipeline(drive, 0); 4044 return idetape_position_tape(drive, mt_count * tape->user_bs_factor, tape->partition, 0); 4045 case MTSETPART: 4046 idetape_discard_read_pipeline(drive, 0); 4047 return (idetape_position_tape(drive, 0, mt_count, 0)); 4048 case MTFSR: 4049 case MTBSR: 4050 case MTLOCK: 4051 if (!idetape_create_prevent_cmd(drive, &pc, 1)) 4052 return 0; 4053 retval = idetape_queue_pc_tail(drive, &pc); 4054 if (retval) return retval; 4055 tape->door_locked = DOOR_EXPLICITLY_LOCKED; 4056 return 0; 4057 case MTUNLOCK: 4058 if (!idetape_create_prevent_cmd(drive, &pc, 0)) 4059 return 0; 4060 retval = idetape_queue_pc_tail(drive, &pc); 4061 if (retval) return retval; 4062 tape->door_locked = DOOR_UNLOCKED; 4063 return 0; 4064 default: 4065 printk(KERN_ERR "ide-tape: MTIO operation %d not " 4066 "supported\n", mt_op); 4067 return (-EIO); 4068 } 4069} 4070 4071/* 4072 * Our character device ioctls. 4073 * 4074 * General mtio.h magnetic io commands are supported here, and not in 4075 * the corresponding block interface. 4076 * 4077 * The following ioctls are supported: 4078 * 4079 * MTIOCTOP - Refer to idetape_mtioctop for detailed description. 4080 * 4081 * MTIOCGET - The mt_dsreg field in the returned mtget structure 4082 * will be set to (user block size in bytes << 4083 * MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK. 4084 * 4085 * The mt_blkno is set to the current user block number. 4086 * The other mtget fields are not supported. 4087 * 4088 * MTIOCPOS - The current tape "block position" is returned. We 4089 * assume that each block contains user_block_size 4090 * bytes. 4091 * 4092 * Our own ide-tape ioctls are supported on both interfaces. 4093 */ 4094static int idetape_chrdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) 4095{ 4096 struct ide_tape_obj *tape = ide_tape_f(file); 4097 ide_drive_t *drive = tape->drive; 4098 struct mtop mtop; 4099 struct mtget mtget; 4100 struct mtpos mtpos; 4101 int block_offset = 0, position = tape->first_frame_position; 4102 void __user *argp = (void __user *)arg; 4103 4104#if IDETAPE_DEBUG_LOG 4105 if (tape->debug_level >= 3) 4106 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_ioctl, " 4107 "cmd=%u\n", cmd); 4108#endif /* IDETAPE_DEBUG_LOG */ 4109 4110 tape->restart_speed_control_req = 1; 4111 if (tape->chrdev_direction == idetape_direction_write) { 4112 idetape_empty_write_pipeline(drive); 4113 idetape_flush_tape_buffers(drive); 4114 } 4115 if (cmd == MTIOCGET || cmd == MTIOCPOS) { 4116 block_offset = idetape_pipeline_size(drive) / (tape->tape_block_size * tape->user_bs_factor); 4117 if ((position = idetape_read_position(drive)) < 0) 4118 return -EIO; 4119 } 4120 switch (cmd) { 4121 case MTIOCTOP: 4122 if (copy_from_user(&mtop, argp, sizeof (struct mtop))) 4123 return -EFAULT; 4124 return (idetape_mtioctop(drive,mtop.mt_op,mtop.mt_count)); 4125 case MTIOCGET: 4126 memset(&mtget, 0, sizeof (struct mtget)); 4127 mtget.mt_type = MT_ISSCSI2; 4128 mtget.mt_blkno = position / tape->user_bs_factor - block_offset; 4129 mtget.mt_dsreg = ((tape->tape_block_size * tape->user_bs_factor) << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK; 4130 if (tape->drv_write_prot) { 4131 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff); 4132 } 4133 if (copy_to_user(argp, &mtget, sizeof(struct mtget))) 4134 return -EFAULT; 4135 return 0; 4136 case MTIOCPOS: 4137 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset; 4138 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos))) 4139 return -EFAULT; 4140 return 0; 4141 default: 4142 if (tape->chrdev_direction == idetape_direction_read) 4143 idetape_discard_read_pipeline(drive, 1); 4144 return idetape_blkdev_ioctl(drive, cmd, arg); 4145 } 4146} 4147 4148static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive); 4149 4150/* 4151 * Our character device open function. 4152 */ 4153static int idetape_chrdev_open (struct inode *inode, struct file *filp) 4154{ 4155 unsigned int minor = iminor(inode), i = minor & ~0xc0; 4156 ide_drive_t *drive; 4157 idetape_tape_t *tape; 4158 idetape_pc_t pc; 4159 int retval; 4160 4161 /* 4162 * We really want to do nonseekable_open(inode, filp); here, but some 4163 * versions of tar incorrectly call lseek on tapes and bail out if that 4164 * fails. So we disallow pread() and pwrite(), but permit lseeks. 4165 */ 4166 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE); 4167 4168#if IDETAPE_DEBUG_LOG 4169 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_open\n"); 4170#endif /* IDETAPE_DEBUG_LOG */ 4171 4172 if (i >= MAX_HWIFS * MAX_DRIVES) 4173 return -ENXIO; 4174 4175 if (!(tape = ide_tape_chrdev_get(i))) 4176 return -ENXIO; 4177 4178 drive = tape->drive; 4179 4180 filp->private_data = tape; 4181 4182 if (test_and_set_bit(IDETAPE_BUSY, &tape->flags)) { 4183 retval = -EBUSY; 4184 goto out_put_tape; 4185 } 4186 4187 retval = idetape_wait_ready(drive, 60 * HZ); 4188 if (retval) { 4189 clear_bit(IDETAPE_BUSY, &tape->flags); 4190 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name); 4191 goto out_put_tape; 4192 } 4193 4194 idetape_read_position(drive); 4195 if (!test_bit(IDETAPE_ADDRESS_VALID, &tape->flags)) 4196 (void)idetape_rewind_tape(drive); 4197 4198 if (tape->chrdev_direction != idetape_direction_read) 4199 clear_bit(IDETAPE_PIPELINE_ERROR, &tape->flags); 4200 4201 /* Read block size and write protect status from drive. */ 4202 idetape_get_blocksize_from_block_descriptor(drive); 4203 4204 /* Set write protect flag if device is opened as read-only. */ 4205 if ((filp->f_flags & O_ACCMODE) == O_RDONLY) 4206 tape->write_prot = 1; 4207 else 4208 tape->write_prot = tape->drv_write_prot; 4209 4210 /* Make sure drive isn't write protected if user wants to write. */ 4211 if (tape->write_prot) { 4212 if ((filp->f_flags & O_ACCMODE) == O_WRONLY || 4213 (filp->f_flags & O_ACCMODE) == O_RDWR) { 4214 clear_bit(IDETAPE_BUSY, &tape->flags); 4215 retval = -EROFS; 4216 goto out_put_tape; 4217 } 4218 } 4219 4220 /* 4221 * Lock the tape drive door so user can't eject. 4222 */ 4223 if (tape->chrdev_direction == idetape_direction_none) { 4224 if (idetape_create_prevent_cmd(drive, &pc, 1)) { 4225 if (!idetape_queue_pc_tail(drive, &pc)) { 4226 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED) 4227 tape->door_locked = DOOR_LOCKED; 4228 } 4229 } 4230 } 4231 idetape_restart_speed_control(drive); 4232 tape->restart_speed_control_req = 0; 4233 return 0; 4234 4235out_put_tape: 4236 ide_tape_put(tape); 4237 return retval; 4238} 4239 4240static void idetape_write_release (ide_drive_t *drive, unsigned int minor) 4241{ 4242 idetape_tape_t *tape = drive->driver_data; 4243 4244 idetape_empty_write_pipeline(drive); 4245 tape->merge_stage = __idetape_kmalloc_stage(tape, 1, 0); 4246 if (tape->merge_stage != NULL) { 4247 idetape_pad_zeros(drive, tape->tape_block_size * (tape->user_bs_factor - 1)); 4248 __idetape_kfree_stage(tape->merge_stage); 4249 tape->merge_stage = NULL; 4250 } 4251 idetape_write_filemark(drive); 4252 idetape_flush_tape_buffers(drive); 4253 idetape_flush_tape_buffers(drive); 4254} 4255 4256/* 4257 * Our character device release function. 4258 */ 4259static int idetape_chrdev_release (struct inode *inode, struct file *filp) 4260{ 4261 struct ide_tape_obj *tape = ide_tape_f(filp); 4262 ide_drive_t *drive = tape->drive; 4263 idetape_pc_t pc; 4264 unsigned int minor = iminor(inode); 4265 4266 lock_kernel(); 4267 tape = drive->driver_data; 4268#if IDETAPE_DEBUG_LOG 4269 if (tape->debug_level >= 3) 4270 printk(KERN_INFO "ide-tape: Reached idetape_chrdev_release\n"); 4271#endif /* IDETAPE_DEBUG_LOG */ 4272 4273 if (tape->chrdev_direction == idetape_direction_write) 4274 idetape_write_release(drive, minor); 4275 if (tape->chrdev_direction == idetape_direction_read) { 4276 if (minor < 128) 4277 idetape_discard_read_pipeline(drive, 1); 4278 else 4279 idetape_wait_for_pipeline(drive); 4280 } 4281 if (tape->cache_stage != NULL) { 4282 __idetape_kfree_stage(tape->cache_stage); 4283 tape->cache_stage = NULL; 4284 } 4285 if (minor < 128 && test_bit(IDETAPE_MEDIUM_PRESENT, &tape->flags)) 4286 (void) idetape_rewind_tape(drive); 4287 if (tape->chrdev_direction == idetape_direction_none) { 4288 if (tape->door_locked == DOOR_LOCKED) { 4289 if (idetape_create_prevent_cmd(drive, &pc, 0)) { 4290 if (!idetape_queue_pc_tail(drive, &pc)) 4291 tape->door_locked = DOOR_UNLOCKED; 4292 } 4293 } 4294 } 4295 clear_bit(IDETAPE_BUSY, &tape->flags); 4296 ide_tape_put(tape); 4297 unlock_kernel(); 4298 return 0; 4299} 4300 4301/* 4302 * idetape_identify_device is called to check the contents of the 4303 * ATAPI IDENTIFY command results. We return: 4304 * 4305 * 1 If the tape can be supported by us, based on the information 4306 * we have so far. 4307 * 4308 * 0 If this tape driver is not currently supported by us. 4309 */ 4310static int idetape_identify_device (ide_drive_t *drive) 4311{ 4312 struct idetape_id_gcw gcw; 4313 struct hd_driveid *id = drive->id; 4314#if IDETAPE_DEBUG_INFO 4315 unsigned short mask,i; 4316#endif /* IDETAPE_DEBUG_INFO */ 4317 4318 if (drive->id_read == 0) 4319 return 1; 4320 4321 *((unsigned short *) &gcw) = id->config; 4322 4323#if IDETAPE_DEBUG_INFO 4324 printk(KERN_INFO "ide-tape: Dumping ATAPI Identify Device tape parameters\n"); 4325 printk(KERN_INFO "ide-tape: Protocol Type: "); 4326 switch (gcw.protocol) { 4327 case 0: case 1: printk("ATA\n");break; 4328 case 2: printk("ATAPI\n");break; 4329 case 3: printk("Reserved (Unknown to ide-tape)\n");break; 4330 } 4331 printk(KERN_INFO "ide-tape: Device Type: %x - ",gcw.device_type); 4332 switch (gcw.device_type) { 4333 case 0: printk("Direct-access Device\n");break; 4334 case 1: printk("Streaming Tape Device\n");break; 4335 case 2: case 3: case 4: printk("Reserved\n");break; 4336 case 5: printk("CD-ROM Device\n");break; 4337 case 6: printk("Reserved\n"); 4338 case 7: printk("Optical memory Device\n");break; 4339 case 0x1f: printk("Unknown or no Device type\n");break; 4340 default: printk("Reserved\n"); 4341 } 4342 printk(KERN_INFO "ide-tape: Removable: %s",gcw.removable ? "Yes\n":"No\n"); 4343 printk(KERN_INFO "ide-tape: Command Packet DRQ Type: "); 4344 switch (gcw.drq_type) { 4345 case 0: printk("Microprocessor DRQ\n");break; 4346 case 1: printk("Interrupt DRQ\n");break; 4347 case 2: printk("Accelerated DRQ\n");break; 4348 case 3: printk("Reserved\n");break; 4349 } 4350 printk(KERN_INFO "ide-tape: Command Packet Size: "); 4351 switch (gcw.packet_size) { 4352 case 0: printk("12 bytes\n");break; 4353 case 1: printk("16 bytes\n");break; 4354 default: printk("Reserved\n");break; 4355 } 4356 printk(KERN_INFO "ide-tape: Model: %.40s\n",id->model); 4357 printk(KERN_INFO "ide-tape: Firmware Revision: %.8s\n",id->fw_rev); 4358 printk(KERN_INFO "ide-tape: Serial Number: %.20s\n",id->serial_no); 4359 printk(KERN_INFO "ide-tape: Write buffer size: %d bytes\n",id->buf_size*512); 4360 printk(KERN_INFO "ide-tape: DMA: %s",id->capability & 0x01 ? "Yes\n":"No\n"); 4361 printk(KERN_INFO "ide-tape: LBA: %s",id->capability & 0x02 ? "Yes\n":"No\n"); 4362 printk(KERN_INFO "ide-tape: IORDY can be disabled: %s",id->capability & 0x04 ? "Yes\n":"No\n"); 4363 printk(KERN_INFO "ide-tape: IORDY supported: %s",id->capability & 0x08 ? "Yes\n":"Unknown\n"); 4364 printk(KERN_INFO "ide-tape: ATAPI overlap supported: %s",id->capability & 0x20 ? "Yes\n":"No\n"); 4365 printk(KERN_INFO "ide-tape: PIO Cycle Timing Category: %d\n",id->tPIO); 4366 printk(KERN_INFO "ide-tape: DMA Cycle Timing Category: %d\n",id->tDMA); 4367 printk(KERN_INFO "ide-tape: Single Word DMA supported modes: "); 4368 for (i=0,mask=1;i<8;i++,mask=mask << 1) { 4369 if (id->dma_1word & mask) 4370 printk("%d ",i); 4371 if (id->dma_1word & (mask << 8)) 4372 printk("(active) "); 4373 } 4374 printk("\n"); 4375 printk(KERN_INFO "ide-tape: Multi Word DMA supported modes: "); 4376 for (i=0,mask=1;i<8;i++,mask=mask << 1) { 4377 if (id->dma_mword & mask) 4378 printk("%d ",i); 4379 if (id->dma_mword & (mask << 8)) 4380 printk("(active) "); 4381 } 4382 printk("\n"); 4383 if (id->field_valid & 0x0002) { 4384 printk(KERN_INFO "ide-tape: Enhanced PIO Modes: %s\n", 4385 id->eide_pio_modes & 1 ? "Mode 3":"None"); 4386 printk(KERN_INFO "ide-tape: Minimum Multi-word DMA cycle per word: "); 4387 if (id->eide_dma_min == 0) 4388 printk("Not supported\n"); 4389 else 4390 printk("%d ns\n",id->eide_dma_min); 4391 4392 printk(KERN_INFO "ide-tape: Manufacturer\'s Recommended Multi-word cycle: "); 4393 if (id->eide_dma_time == 0) 4394 printk("Not supported\n"); 4395 else 4396 printk("%d ns\n",id->eide_dma_time); 4397 4398 printk(KERN_INFO "ide-tape: Minimum PIO cycle without IORDY: "); 4399 if (id->eide_pio == 0) 4400 printk("Not supported\n"); 4401 else 4402 printk("%d ns\n",id->eide_pio); 4403 4404 printk(KERN_INFO "ide-tape: Minimum PIO cycle with IORDY: "); 4405 if (id->eide_pio_iordy == 0) 4406 printk("Not supported\n"); 4407 else 4408 printk("%d ns\n",id->eide_pio_iordy); 4409 4410 } else 4411 printk(KERN_INFO "ide-tape: According to the device, fields 64-70 are not valid.\n"); 4412#endif /* IDETAPE_DEBUG_INFO */ 4413 4414 /* Check that we can support this device */ 4415 4416 if (gcw.protocol !=2 ) 4417 printk(KERN_ERR "ide-tape: Protocol is not ATAPI\n"); 4418 else if (gcw.device_type != 1) 4419 printk(KERN_ERR "ide-tape: Device type is not set to tape\n"); 4420 else if (!gcw.removable) 4421 printk(KERN_ERR "ide-tape: The removable flag is not set\n"); 4422 else if (gcw.packet_size != 0) { 4423 printk(KERN_ERR "ide-tape: Packet size is not 12 bytes long\n"); 4424 if (gcw.packet_size == 1) 4425 printk(KERN_ERR "ide-tape: Sorry, padding to 16 bytes is still not supported\n"); 4426 } else 4427 return 1; 4428 return 0; 4429} 4430 4431/* 4432 * Use INQUIRY to get the firmware revision 4433 */ 4434static void idetape_get_inquiry_results (ide_drive_t *drive) 4435{ 4436 char *r; 4437 idetape_tape_t *tape = drive->driver_data; 4438 idetape_pc_t pc; 4439 idetape_inquiry_result_t *inquiry; 4440 4441 idetape_create_inquiry_cmd(&pc); 4442 if (idetape_queue_pc_tail(drive, &pc)) { 4443 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n", tape->name); 4444 return; 4445 } 4446 inquiry = (idetape_inquiry_result_t *) pc.buffer; 4447 memcpy(tape->vendor_id, inquiry->vendor_id, 8); 4448 memcpy(tape->product_id, inquiry->product_id, 16); 4449 memcpy(tape->firmware_revision, inquiry->revision_level, 4); 4450 ide_fixstring(tape->vendor_id, 10, 0); 4451 ide_fixstring(tape->product_id, 18, 0); 4452 ide_fixstring(tape->firmware_revision, 6, 0); 4453 r = tape->firmware_revision; 4454 if (*(r + 1) == '.') 4455 tape->firmware_revision_num = (*r - '0') * 100 + (*(r + 2) - '0') * 10 + *(r + 3) - '0'; 4456 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); 4457} 4458 4459/* 4460 * idetape_get_mode_sense_results asks the tape about its various 4461 * parameters. In particular, we will adjust our data transfer buffer 4462 * size to the recommended value as returned by the tape. 4463 */ 4464static void idetape_get_mode_sense_results (ide_drive_t *drive) 4465{ 4466 idetape_tape_t *tape = drive->driver_data; 4467 idetape_pc_t pc; 4468 idetape_mode_parameter_header_t *header; 4469 idetape_capabilities_page_t *capabilities; 4470 4471 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE); 4472 if (idetape_queue_pc_tail(drive, &pc)) { 4473 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming some default values\n"); 4474 tape->tape_block_size = 512; 4475 tape->capabilities.ctl = 52; 4476 tape->capabilities.speed = 450; 4477 tape->capabilities.buffer_size = 6 * 52; 4478 return; 4479 } 4480 header = (idetape_mode_parameter_header_t *) pc.buffer; 4481 capabilities = (idetape_capabilities_page_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t) + header->bdl); 4482 4483 capabilities->max_speed = ntohs(capabilities->max_speed); 4484 capabilities->ctl = ntohs(capabilities->ctl); 4485 capabilities->speed = ntohs(capabilities->speed); 4486 capabilities->buffer_size = ntohs(capabilities->buffer_size); 4487 4488 if (!capabilities->speed) { 4489 printk(KERN_INFO "ide-tape: %s: overriding capabilities->speed (assuming 650KB/sec)\n", drive->name); 4490 capabilities->speed = 650; 4491 } 4492 if (!capabilities->max_speed) { 4493 printk(KERN_INFO "ide-tape: %s: overriding capabilities->max_speed (assuming 650KB/sec)\n", drive->name); 4494 capabilities->max_speed = 650; 4495 } 4496 4497 tape->capabilities = *capabilities; /* Save us a copy */ 4498 if (capabilities->blk512) 4499 tape->tape_block_size = 512; 4500 else if (capabilities->blk1024) 4501 tape->tape_block_size = 1024; 4502 4503#if IDETAPE_DEBUG_INFO 4504 printk(KERN_INFO "ide-tape: Dumping the results of the MODE SENSE packet command\n"); 4505 printk(KERN_INFO "ide-tape: Mode Parameter Header:\n"); 4506 printk(KERN_INFO "ide-tape: Mode Data Length - %d\n",header->mode_data_length); 4507 printk(KERN_INFO "ide-tape: Medium Type - %d\n",header->medium_type); 4508 printk(KERN_INFO "ide-tape: Device Specific Parameter - %d\n",header->dsp); 4509 printk(KERN_INFO "ide-tape: Block Descriptor Length - %d\n",header->bdl); 4510 4511 printk(KERN_INFO "ide-tape: Capabilities and Mechanical Status Page:\n"); 4512 printk(KERN_INFO "ide-tape: Page code - %d\n",capabilities->page_code); 4513 printk(KERN_INFO "ide-tape: Page length - %d\n",capabilities->page_length); 4514 printk(KERN_INFO "ide-tape: Read only - %s\n",capabilities->ro ? "Yes":"No"); 4515 printk(KERN_INFO "ide-tape: Supports reverse space - %s\n",capabilities->sprev ? "Yes":"No"); 4516 printk(KERN_INFO "ide-tape: Supports erase initiated formatting - %s\n",capabilities->efmt ? "Yes":"No"); 4517 printk(KERN_INFO "ide-tape: Supports QFA two Partition format - %s\n",capabilities->qfa ? "Yes":"No"); 4518 printk(KERN_INFO "ide-tape: Supports locking the medium - %s\n",capabilities->lock ? "Yes":"No"); 4519 printk(KERN_INFO "ide-tape: The volume is currently locked - %s\n",capabilities->locked ? "Yes":"No"); 4520 printk(KERN_INFO "ide-tape: The device defaults in the prevent state - %s\n",capabilities->prevent ? "Yes":"No"); 4521 printk(KERN_INFO "ide-tape: Supports ejecting the medium - %s\n",capabilities->eject ? "Yes":"No"); 4522 printk(KERN_INFO "ide-tape: Supports error correction - %s\n",capabilities->ecc ? "Yes":"No"); 4523 printk(KERN_INFO "ide-tape: Supports data compression - %s\n",capabilities->cmprs ? "Yes":"No"); 4524 printk(KERN_INFO "ide-tape: Supports 512 bytes block size - %s\n",capabilities->blk512 ? "Yes":"No"); 4525 printk(KERN_INFO "ide-tape: Supports 1024 bytes block size - %s\n",capabilities->blk1024 ? "Yes":"No"); 4526 printk(KERN_INFO "ide-tape: Supports 32768 bytes block size / Restricted byte count for PIO transfers - %s\n",capabilities->blk32768 ? "Yes":"No"); 4527 printk(KERN_INFO "ide-tape: Maximum supported speed in KBps - %d\n",capabilities->max_speed); 4528 printk(KERN_INFO "ide-tape: Continuous transfer limits in blocks - %d\n",capabilities->ctl); 4529 printk(KERN_INFO "ide-tape: Current speed in KBps - %d\n",capabilities->speed); 4530 printk(KERN_INFO "ide-tape: Buffer size - %d\n",capabilities->buffer_size*512); 4531#endif /* IDETAPE_DEBUG_INFO */ 4532} 4533 4534/* 4535 * ide_get_blocksize_from_block_descriptor does a mode sense page 0 with block descriptor 4536 * and if it succeeds sets the tape block size with the reported value 4537 */ 4538static void idetape_get_blocksize_from_block_descriptor(ide_drive_t *drive) 4539{ 4540 4541 idetape_tape_t *tape = drive->driver_data; 4542 idetape_pc_t pc; 4543 idetape_mode_parameter_header_t *header; 4544 idetape_parameter_block_descriptor_t *block_descrp; 4545 4546 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR); 4547 if (idetape_queue_pc_tail(drive, &pc)) { 4548 printk(KERN_ERR "ide-tape: Can't get block descriptor\n"); 4549 if (tape->tape_block_size == 0) { 4550 printk(KERN_WARNING "ide-tape: Cannot deal with zero block size, assume 32k\n"); 4551 tape->tape_block_size = 32768; 4552 } 4553 return; 4554 } 4555 header = (idetape_mode_parameter_header_t *) pc.buffer; 4556 block_descrp = (idetape_parameter_block_descriptor_t *) (pc.buffer + sizeof(idetape_mode_parameter_header_t)); 4557 tape->tape_block_size =( block_descrp->length[0]<<16) + (block_descrp->length[1]<<8) + block_descrp->length[2]; 4558 tape->drv_write_prot = (header->dsp & 0x80) >> 7; 4559 4560#if IDETAPE_DEBUG_INFO 4561 printk(KERN_INFO "ide-tape: Adjusted block size - %d\n", tape->tape_block_size); 4562#endif /* IDETAPE_DEBUG_INFO */ 4563} 4564 4565#ifdef CONFIG_IDE_PROC_FS 4566static void idetape_add_settings (ide_drive_t *drive) 4567{ 4568 idetape_tape_t *tape = drive->driver_data; 4569 4570/* 4571 * drive setting name read/write data type min max mul_factor div_factor data pointer set function 4572 */ 4573 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff, 1, 2, &tape->capabilities.buffer_size, NULL); 4574 ide_add_setting(drive, "pipeline_min", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->min_pipeline, NULL); 4575 ide_add_setting(drive, "pipeline", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_stages, NULL); 4576 ide_add_setting(drive, "pipeline_max", SETTING_RW, TYPE_INT, 1, 0xffff, tape->stage_size / 1024, 1, &tape->max_pipeline, NULL); 4577 ide_add_setting(drive, "pipeline_used", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_stages, NULL); 4578 ide_add_setting(drive, "pipeline_pending", SETTING_READ, TYPE_INT, 0, 0xffff, tape->stage_size / 1024, 1, &tape->nr_pending_stages, NULL); 4579 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff, 1, 1, &tape->capabilities.speed, NULL); 4580 ide_add_setting(drive, "stage", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1024, &tape->stage_size, NULL); 4581 ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_frequency, NULL); 4582 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL); 4583 ide_add_setting(drive, "pipeline_head_speed_c",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->controlled_pipeline_head_speed, NULL); 4584 ide_add_setting(drive, "pipeline_head_speed_u",SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->uncontrolled_pipeline_head_speed,NULL); 4585 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff, 1, 1, &tape->avg_speed, NULL); 4586 ide_add_setting(drive, "debug_level", SETTING_RW, TYPE_INT, 0, 0xffff, 1, 1, &tape->debug_level, NULL); 4587} 4588#else 4589static inline void idetape_add_settings(ide_drive_t *drive) { ; } 4590#endif 4591 4592/* 4593 * ide_setup is called to: 4594 * 4595 * 1. Initialize our various state variables. 4596 * 2. Ask the tape for its capabilities. 4597 * 3. Allocate a buffer which will be used for data 4598 * transfer. The buffer size is chosen based on 4599 * the recommendation which we received in step (2). 4600 * 4601 * Note that at this point ide.c already assigned us an irq, so that 4602 * we can queue requests here and wait for their completion. 4603 */ 4604static void idetape_setup (ide_drive_t *drive, idetape_tape_t *tape, int minor) 4605{ 4606 unsigned long t1, tmid, tn, t; 4607 int speed; 4608 struct idetape_id_gcw gcw; 4609 int stage_size; 4610 struct sysinfo si; 4611 4612 spin_lock_init(&tape->spinlock); 4613 drive->dsc_overlap = 1; 4614#ifdef CONFIG_BLK_DEV_IDEPCI 4615 if (HWIF(drive)->pci_dev != NULL) { 4616 /* 4617 * These two ide-pci host adapters appear to need DSC overlap disabled. 4618 * This probably needs further analysis. 4619 */ 4620 if ((HWIF(drive)->pci_dev->device == PCI_DEVICE_ID_ARTOP_ATP850UF) || 4621 (HWIF(drive)->pci_dev->device == PCI_DEVICE_ID_TTI_HPT343)) { 4622 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n", tape->name); 4623 drive->dsc_overlap = 0; 4624 } 4625 } 4626#endif /* CONFIG_BLK_DEV_IDEPCI */ 4627 /* Seagate Travan drives do not support DSC overlap. */ 4628 if (strstr(drive->id->model, "Seagate STT3401")) 4629 drive->dsc_overlap = 0; 4630 tape->minor = minor; 4631 tape->name[0] = 'h'; 4632 tape->name[1] = 't'; 4633 tape->name[2] = '0' + minor; 4634 tape->chrdev_direction = idetape_direction_none; 4635 tape->pc = tape->pc_stack; 4636 tape->max_insert_speed = 10000; 4637 tape->speed_control = 1; 4638 *((unsigned short *) &gcw) = drive->id->config; 4639 if (gcw.drq_type == 1) 4640 set_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags); 4641 4642 tape->min_pipeline = tape->max_pipeline = tape->max_stages = 10; 4643 4644 idetape_get_inquiry_results(drive); 4645 idetape_get_mode_sense_results(drive); 4646 idetape_get_blocksize_from_block_descriptor(drive); 4647 tape->user_bs_factor = 1; 4648 tape->stage_size = tape->capabilities.ctl * tape->tape_block_size; 4649 while (tape->stage_size > 0xffff) { 4650 printk(KERN_NOTICE "ide-tape: decreasing stage size\n"); 4651 tape->capabilities.ctl /= 2; 4652 tape->stage_size = tape->capabilities.ctl * tape->tape_block_size; 4653 } 4654 stage_size = tape->stage_size; 4655 tape->pages_per_stage = stage_size / PAGE_SIZE; 4656 if (stage_size % PAGE_SIZE) { 4657 tape->pages_per_stage++; 4658 tape->excess_bh_size = PAGE_SIZE - stage_size % PAGE_SIZE; 4659 } 4660 4661 /* 4662 * Select the "best" DSC read/write polling frequency 4663 * and pipeline size. 4664 */ 4665 speed = max(tape->capabilities.speed, tape->capabilities.max_speed); 4666 4667 tape->max_stages = speed * 1000 * 10 / tape->stage_size; 4668 4669 /* 4670 * Limit memory use for pipeline to 10% of physical memory 4671 */ 4672 si_meminfo(&si); 4673 if (tape->max_stages * tape->stage_size > si.totalram * si.mem_unit / 10) 4674 tape->max_stages = si.totalram * si.mem_unit / (10 * tape->stage_size); 4675 tape->max_stages = min(tape->max_stages, IDETAPE_MAX_PIPELINE_STAGES); 4676 tape->min_pipeline = min(tape->max_stages, IDETAPE_MIN_PIPELINE_STAGES); 4677 tape->max_pipeline = min(tape->max_stages * 2, IDETAPE_MAX_PIPELINE_STAGES); 4678 if (tape->max_stages == 0) 4679 tape->max_stages = tape->min_pipeline = tape->max_pipeline = 1; 4680 4681 t1 = (tape->stage_size * HZ) / (speed * 1000); 4682 tmid = (tape->capabilities.buffer_size * 32 * HZ) / (speed * 125); 4683 tn = (IDETAPE_FIFO_THRESHOLD * tape->stage_size * HZ) / (speed * 1000); 4684 4685 if (tape->max_stages) 4686 t = tn; 4687 else 4688 t = t1; 4689 4690 /* 4691 * Ensure that the number we got makes sense; limit 4692 * it within IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX. 4693 */ 4694 tape->best_dsc_rw_frequency = max_t(unsigned long, min_t(unsigned long, t, IDETAPE_DSC_RW_MAX), IDETAPE_DSC_RW_MIN); 4695 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, " 4696 "%dkB pipeline, %lums tDSC%s\n", 4697 drive->name, tape->name, tape->capabilities.speed, 4698 (tape->capabilities.buffer_size * 512) / tape->stage_size, 4699 tape->stage_size / 1024, 4700 tape->max_stages * tape->stage_size / 1024, 4701 tape->best_dsc_rw_frequency * 1000 / HZ, 4702 drive->using_dma ? ", DMA":""); 4703 4704 idetape_add_settings(drive); 4705} 4706 4707static void ide_tape_remove(ide_drive_t *drive) 4708{ 4709 idetape_tape_t *tape = drive->driver_data; 4710 4711 ide_proc_unregister_driver(drive, tape->driver); 4712 4713 ide_unregister_region(tape->disk); 4714 4715 ide_tape_put(tape); 4716} 4717 4718static void ide_tape_release(struct kref *kref) 4719{ 4720 struct ide_tape_obj *tape = to_ide_tape(kref); 4721 ide_drive_t *drive = tape->drive; 4722 struct gendisk *g = tape->disk; 4723 4724 BUG_ON(tape->first_stage != NULL || tape->merge_stage_size); 4725 4726 drive->dsc_overlap = 0; 4727 drive->driver_data = NULL; 4728 class_device_destroy(idetape_sysfs_class, 4729 MKDEV(IDETAPE_MAJOR, tape->minor)); 4730 class_device_destroy(idetape_sysfs_class, 4731 MKDEV(IDETAPE_MAJOR, tape->minor + 128)); 4732 idetape_devs[tape->minor] = NULL; 4733 g->private_data = NULL; 4734 put_disk(g); 4735 kfree(tape); 4736} 4737 4738#ifdef CONFIG_IDE_PROC_FS 4739static int proc_idetape_read_name 4740 (char *page, char **start, off_t off, int count, int *eof, void *data) 4741{ 4742 ide_drive_t *drive = (ide_drive_t *) data; 4743 idetape_tape_t *tape = drive->driver_data; 4744 char *out = page; 4745 int len; 4746 4747 len = sprintf(out, "%s\n", tape->name); 4748 PROC_IDE_READ_RETURN(page, start, off, count, eof, len); 4749} 4750 4751static ide_proc_entry_t idetape_proc[] = { 4752 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL }, 4753 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL }, 4754 { NULL, 0, NULL, NULL } 4755}; 4756#endif 4757 4758static int ide_tape_probe(ide_drive_t *); 4759 4760static ide_driver_t idetape_driver = { 4761 .gen_driver = { 4762 .owner = THIS_MODULE, 4763 .name = "ide-tape", 4764 .bus = &ide_bus_type, 4765 }, 4766 .probe = ide_tape_probe, 4767 .remove = ide_tape_remove, 4768 .version = IDETAPE_VERSION, 4769 .media = ide_tape, 4770 .supports_dsc_overlap = 1, 4771 .do_request = idetape_do_request, 4772 .end_request = idetape_end_request, 4773 .error = __ide_error, 4774 .abort = __ide_abort, 4775#ifdef CONFIG_IDE_PROC_FS 4776 .proc = idetape_proc, 4777#endif 4778}; 4779 4780/* 4781 * Our character device supporting functions, passed to register_chrdev. 4782 */ 4783static const struct file_operations idetape_fops = { 4784 .owner = THIS_MODULE, 4785 .read = idetape_chrdev_read, 4786 .write = idetape_chrdev_write, 4787 .ioctl = idetape_chrdev_ioctl, 4788 .open = idetape_chrdev_open, 4789 .release = idetape_chrdev_release, 4790}; 4791 4792static int idetape_open(struct inode *inode, struct file *filp) 4793{ 4794 struct gendisk *disk = inode->i_bdev->bd_disk; 4795 struct ide_tape_obj *tape; 4796 4797 if (!(tape = ide_tape_get(disk))) 4798 return -ENXIO; 4799 4800 return 0; 4801} 4802 4803static int idetape_release(struct inode *inode, struct file *filp) 4804{ 4805 struct gendisk *disk = inode->i_bdev->bd_disk; 4806 struct ide_tape_obj *tape = ide_tape_g(disk); 4807 4808 ide_tape_put(tape); 4809 4810 return 0; 4811} 4812 4813static int idetape_ioctl(struct inode *inode, struct file *file, 4814 unsigned int cmd, unsigned long arg) 4815{ 4816 struct block_device *bdev = inode->i_bdev; 4817 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk); 4818 ide_drive_t *drive = tape->drive; 4819 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg); 4820 if (err == -EINVAL) 4821 err = idetape_blkdev_ioctl(drive, cmd, arg); 4822 return err; 4823} 4824 4825static struct block_device_operations idetape_block_ops = { 4826 .owner = THIS_MODULE, 4827 .open = idetape_open, 4828 .release = idetape_release, 4829 .ioctl = idetape_ioctl, 4830}; 4831 4832static int ide_tape_probe(ide_drive_t *drive) 4833{ 4834 idetape_tape_t *tape; 4835 struct gendisk *g; 4836 int minor; 4837 4838 if (!strstr("ide-tape", drive->driver_req)) 4839 goto failed; 4840 if (!drive->present) 4841 goto failed; 4842 if (drive->media != ide_tape) 4843 goto failed; 4844 if (!idetape_identify_device (drive)) { 4845 printk(KERN_ERR "ide-tape: %s: not supported by this version of ide-tape\n", drive->name); 4846 goto failed; 4847 } 4848 if (drive->scsi) { 4849 printk("ide-tape: passing drive %s to ide-scsi emulation.\n", drive->name); 4850 goto failed; 4851 } 4852 if (strstr(drive->id->model, "OnStream DI-")) { 4853 printk(KERN_WARNING "ide-tape: Use drive %s with ide-scsi emulation and osst.\n", drive->name); 4854 printk(KERN_WARNING "ide-tape: OnStream support will be removed soon from ide-tape!\n"); 4855 } 4856 tape = kzalloc(sizeof (idetape_tape_t), GFP_KERNEL); 4857 if (tape == NULL) { 4858 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape structure\n", drive->name); 4859 goto failed; 4860 } 4861 4862 g = alloc_disk(1 << PARTN_BITS); 4863 if (!g) 4864 goto out_free_tape; 4865 4866 ide_init_disk(g, drive); 4867 4868 ide_proc_register_driver(drive, &idetape_driver); 4869 4870 kref_init(&tape->kref); 4871 4872 tape->drive = drive; 4873 tape->driver = &idetape_driver; 4874 tape->disk = g; 4875 4876 g->private_data = &tape->driver; 4877 4878 drive->driver_data = tape; 4879 4880 mutex_lock(&idetape_ref_mutex); 4881 for (minor = 0; idetape_devs[minor]; minor++) 4882 ; 4883 idetape_devs[minor] = tape; 4884 mutex_unlock(&idetape_ref_mutex); 4885 4886 idetape_setup(drive, tape, minor); 4887 4888 class_device_create(idetape_sysfs_class, NULL, 4889 MKDEV(IDETAPE_MAJOR, minor), &drive->gendev, "%s", tape->name); 4890 class_device_create(idetape_sysfs_class, NULL, 4891 MKDEV(IDETAPE_MAJOR, minor + 128), &drive->gendev, "n%s", tape->name); 4892 4893 g->fops = &idetape_block_ops; 4894 ide_register_region(g); 4895 4896 return 0; 4897 4898out_free_tape: 4899 kfree(tape); 4900failed: 4901 return -ENODEV; 4902} 4903 4904MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver"); 4905MODULE_LICENSE("GPL"); 4906 4907static void __exit idetape_exit (void) 4908{ 4909 driver_unregister(&idetape_driver.gen_driver); 4910 class_destroy(idetape_sysfs_class); 4911 unregister_chrdev(IDETAPE_MAJOR, "ht"); 4912} 4913 4914static int __init idetape_init(void) 4915{ 4916 int error = 1; 4917 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape"); 4918 if (IS_ERR(idetape_sysfs_class)) { 4919 idetape_sysfs_class = NULL; 4920 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n"); 4921 error = -EBUSY; 4922 goto out; 4923 } 4924 4925 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) { 4926 printk(KERN_ERR "ide-tape: Failed to register character device interface\n"); 4927 error = -EBUSY; 4928 goto out_free_class; 4929 } 4930 4931 error = driver_register(&idetape_driver.gen_driver); 4932 if (error) 4933 goto out_free_driver; 4934 4935 return 0; 4936 4937out_free_driver: 4938 driver_unregister(&idetape_driver.gen_driver); 4939out_free_class: 4940 class_destroy(idetape_sysfs_class); 4941out: 4942 return error; 4943} 4944 4945MODULE_ALIAS("ide:*m-tape*"); 4946module_init(idetape_init); 4947module_exit(idetape_exit); 4948MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);