root/drivers/block/ide-tape.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. idetape_identify_device
  2. idetape_register_chrdev
  3. idetape_setup
  4. idetape_get_mode_sense_results
  5. idetape_issue_packet_command
  6. idetape_pc_intr
  7. idetape_postpone_request
  8. idetape_poll_for_dsc_direct
  9. idetape_poll_for_dsc
  10. idetape_put_back_postponed_request
  11. idetape_media_access_finished
  12. idetape_retry_pc
  13. idetape_pc_callback
  14. idetape_read_callback
  15. idetape_write_callback
  16. idetape_inquiry_callback
  17. idetape_input_data
  18. idetape_output_data
  19. idetape_discard_data
  20. idetape_create_inquiry_cmd
  21. idetape_display_inquiry_result
  22. idetape_create_request_sense_cmd
  23. idetape_request_sense_callback
  24. idetape_analyze_error
  25. idetape_create_test_unit_ready_cmd
  26. idetape_create_locate_cmd
  27. idetape_create_rewind_cmd
  28. idetape_create_mode_sense_cmd
  29. idetape_create_write_filemark_cmd
  30. idetape_create_load_unload_cmd
  31. idetape_create_erase_cmd
  32. idetape_create_read_cmd
  33. idetape_create_space_cmd
  34. idetape_create_write_cmd
  35. idetape_create_read_position_cmd
  36. idetape_read_position_callback
  37. idetape_blkdev_ioctl
  38. idetape_end_request
  39. idetape_do_request
  40. idetape_queue_pc_tail
  41. idetape_queue_pc_head
  42. idetape_wait_for_request
  43. idetape_queue_rw_tail
  44. idetape_add_chrdev_read_request
  45. idetape_add_chrdev_write_request
  46. idetape_discard_read_pipeline
  47. idetape_wait_for_pipeline
  48. idetape_empty_write_pipeline
  49. idetape_zero_packet_command
  50. idetape_swap_short
  51. idetape_swap_long
  52. idetape_next_pc_storage
  53. idetape_next_rq_storage
  54. idetape_blkdev_open
  55. idetape_blkdev_release
  56. idetape_chrdev_read
  57. idetape_chrdev_write
  58. idetape_chrdev_ioctl
  59. idetape_mtioctop
  60. idetape_space_over_filemarks
  61. idetape_chrdev_open
  62. idetape_chrdev_release
  63. idetape_position_tape
  64. idetape_rewind_tape
  65. idetape_flush_tape_buffers
  66. idetape_kmalloc_stage
  67. idetape_kfree_stage
  68. idetape_copy_buffer_from_stage
  69. idetape_copy_buffer_to_stage
  70. idetape_increase_max_pipeline_stages
  71. idetape_add_stage_tail
  72. idetape_remove_stage_head
  73. idetape_insert_pipeline_into_queue
  74. idetape_active_next_stage

   1 /*
   2  * linux/drivers/block/ide-tape.c       Version 1.3 - ALPHA     Feb   9, 1996
   3  *
   4  * Copyright (C) 1995, 1996 Gadi Oxman <tgud@tochnapc2.technion.ac.il>
   5  *
   6  * This driver was constructed as a student project in the software laboratory
   7  * of the faculty of electrical engineering in the Technion - Israel's
   8  * Institute Of Technology, with the guide of Avner Lottem and Dr. Ilana David.
   9  *
  10  * It is hereby placed under the terms of the GNU general public license.
  11  * (See linux/COPYING).
  12  */
  13  
  14 /*
  15  * IDE ATAPI streaming tape driver.
  16  *
  17  * This driver is a part of the Linux ide driver and works in co-operation
  18  * with linux/drivers/block/ide.c.
  19  *
  20  * The driver, in co-operation with ide.c, basically traverses the 
  21  * request-list for the block device interface. The character device
  22  * interface, on the other hand, creates new requests, adds them
  23  * to the request-list of the block device, and waits for their completion.
  24  *
  25  * Pipelined operation mode is now supported on both reads and writes.
  26  *
  27  * The block device major and minor numbers are determined from the
  28  * tape's relative position in the ide interfaces, as explained in ide.c.
  29  *
  30  * The character device interface consists of two devices:
  31  *
  32  * ht0          major=37,minor=0        first IDE tape, rewind on close.
  33  * nht0         major=37,minor=128      first IDE tape, no rewind on close.
  34  *
  35  * Run /usr/src/linux/drivers/block/MAKEDEV.ide to create the above entries.
  36  * We currently support only one ide tape drive.
  37  *
  38  * The general magnetic tape commands compatible interface, as defined by
  39  * include/linux/mtio.h, is accessible through the character device.
  40  *
  41  * General ide driver configuration options, such as the interrupt-unmask
  42  * flag, can be configured by issuing an ioctl to the block device interface,
  43  * as any other ide device.
  44  *
  45  * Our own ide-tape ioctl's can can be issued to either the block device or
  46  * the character device interface.
  47  *
  48  * Maximal throughput with minimal bus load will usually be achieved in the
  49  * following scenario:
  50  *
  51  *      1.      ide-tape is operating in the pipelined operation mode.
  52  *      2.      All character device read/write requests consist of an
  53  *              integral number of the tape's recommended data transfer unit
  54  *              (which is shown on initialization and can be received with
  55  *               an ioctl).
  56  *              As of version 1.3 of the driver, this is no longer as critical
  57  *              as it used to be.
  58  *      3.      No buffering is performed by the user backup program.
  59  *
  60  * Testing was done with a 2 GB CONNER CTMA 4000 IDE ATAPI Streaming Tape Drive.
  61  * 
  62  * Ver 0.1   Nov  1 95   Pre-working code :-)
  63  * Ver 0.2   Nov 23 95   A short backup (few megabytes) and restore procedure
  64  *                        was successful ! (Using tar cvf ... on the block
  65  *                        device interface).
  66  *                       A longer backup resulted in major swapping, bad
  67  *                        overall Linux performance and eventually failed as
  68  *                        we received non serial read-ahead requests from the
  69  *                        buffer cache.
  70  * Ver 0.3   Nov 28 95   Long backups are now possible, thanks to the
  71  *                        character device interface. Linux's responsiveness
  72  *                        and performance doesn't seem to be much affected
  73  *                        from the background backup procedure.
  74  *                       Some general mtio.h magnetic tape operations are
  75  *                        now supported by our character device. As a result,
  76  *                        popular tape utilities are starting to work with
  77  *                        ide tapes :-)
  78  *                       The following configurations were tested:
  79  *                              1. An IDE ATAPI TAPE shares the same interface
  80  *                                 and irq with an IDE ATAPI CDROM.
  81  *                              2. An IDE ATAPI TAPE shares the same interface
  82  *                                 and irq with a normal IDE disk.
  83  *                        Both configurations seemed to work just fine !
  84  *                        However, to be on the safe side, it is meanwhile
  85  *                        recommended to give the IDE TAPE its own interface
  86  *                        and irq.
  87  *                       The one thing which needs to be done here is to
  88  *                        add a "request postpone" feature to ide.c,
  89  *                        so that we won't have to wait for the tape to finish
  90  *                        performing a long media access (DSC) request (such
  91  *                        as a rewind) before we can access the other device
  92  *                        on the same interface. This effect doesn't disturb
  93  *                        normal operation most of the time because read/write
  94  *                        requests are relatively fast, and once we are
  95  *                        performing one tape r/w request, a lot of requests
  96  *                        from the other device can be queued and ide.c will
  97  *                        service all of them after this single tape request.
  98  * Ver 1.0   Dec 11 95   Integrated into Linux 1.3.46 development tree.
  99  *                       On each read / write request, we now ask the drive
 100  *                        if we can transfer a constant number of bytes
 101  *                        (a parameter of the drive) only to its buffers,
 102  *                        without causing actual media access. If we can't,
 103  *                        we just wait until we can by polling the DSC bit.
 104  *                        This ensures that while we are not transferring
 105  *                        more bytes than the constant referred to above, the
 106  *                        interrupt latency will not become too high and
 107  *                        we won't cause an interrupt timeout, as happened
 108  *                        occasionally in the previous version.
 109  *                       While polling for DSC, the current request is
 110  *                        postponed and ide.c is free to handle requests from
 111  *                        the other device. This is handled transparently to
 112  *                        ide.c. The hwgroup locking method which was used
 113  *                        in the previous version was removed.
 114  *                       Use of new general features which are provided by
 115  *                        ide.c for use with atapi devices.
 116  *                        (Programming done by Mark Lord)
 117  *                       Few potential bug fixes (Again, suggested by Mark)
 118  *                       Single character device data transfers are now
 119  *                        not limited in size, as they were before.
 120  *                       We are asking the tape about its recommended
 121  *                        transfer unit and send a larger data transfer
 122  *                        as several transfers of the above size.
 123  *                        For best results, use an integral number of this
 124  *                        basic unit (which is shown during driver
 125  *                        initialization). I will soon add an ioctl to get
 126  *                        this important parameter.
 127  *                       Our data transfer buffer is allocated on startup,
 128  *                        rather than before each data transfer. This should
 129  *                        ensure that we will indeed have a data buffer.
 130  * Ver 1.1   Dec 14 95   Fixed random problems which occured when the tape
 131  *                        shared an interface with another device.
 132  *                        (poll_for_dsc was a complete mess).
 133  *                       Removed some old (non-active) code which had
 134  *                        to do with supporting buffer cache originated
 135  *                        requests.
 136  *                       The block device interface can now be opened, so
 137  *                        that general ide driver features like the unmask
 138  *                        interrupts flag can be selected with an ioctl.
 139  *                        This is the only use of the block device interface.
 140  *                       New fast pipelined operation mode (currently only on
 141  *                        writes). When using the pipelined mode, the
 142  *                        throughput can potentially reach the maximum
 143  *                        tape supported throughput, regardless of the
 144  *                        user backup program. On my tape drive, it sometimes
 145  *                        boosted performance by a factor of 2. Pipelined
 146  *                        mode is enabled by default, but since it has a few
 147  *                        downfalls as well, you may want to disable it.
 148  *                        A short explanation of the pipelined operation mode
 149  *                        is available below.
 150  * Ver 1.2   Jan  1 96   Eliminated pipelined mode race condition.
 151  *                       Added pipeline read mode. As a result, restores
 152  *                        are now as fast as backups.
 153  *                       Optimized shared interface behavior. The new behavior
 154  *                        typically results in better IDE bus efficiency and
 155  *                        higher tape throughput.
 156  *                       Pre-calculation of the expected read/write request
 157  *                        service time, based on the tape's parameters. In
 158  *                        the pipelined operation mode, this allows us to
 159  *                        adjust our polling frequency to a much lower value,
 160  *                        and thus to dramatically reduce our load on Linux,
 161  *                        without any decrease in performance.
 162  *                       Implemented additional mtio.h operations.
 163  *                       The recommended user block size is returned by
 164  *                        the MTIOCGET ioctl.
 165  *                       Additional minor changes.
 166  * Ver 1.3   Feb  9  96  Fixed pipelined read mode bug which prevented the
 167  *                        use of some block sizes during a restore procedure.
 168  *                       The character device interface will now present a
 169  *                        continuous view of the media - any mix of block sizes
 170  *                        during a backup/restore procedure is supported. The
 171  *                        driver will buffer the requests internally and
 172  *                        convert them to the tape's recommended transfer
 173  *                        unit, making performance almost independent of the
 174  *                        chosen user block size.
 175  *                       Some improvements in error recovery.
 176  *                       By cooperating with triton.c, bus mastering DMA can
 177  *                        now sometimes be used with IDE tape drives as well.
 178  *                        Bus mastering DMA has the potential to dramatically
 179  *                        reduce the CPU's overhead when accessing the device,
 180  *                        and can be enabled by using hdparm -d1 on the tape's
 181  *                        block device interface. For more info, read the
 182  *                        comments in triton.c.
 183  *
 184  * We are currently in an *alpha* stage. The driver is not complete and not
 185  * much tested. I would strongly suggest to:
 186  *
 187  *      1. Connect the tape to a separate interface and irq.
 188  *      2. Be truly prepared for a kernel crash and the resulting data loss.
 189  *      3. Don't rely too much on the resulting backups.
 190  *
 191  * Other than that, enjoy !
 192  *
 193  * Here are some words from the first releases of hd.c, which are quoted
 194  * in ide.c and apply here as well:
 195  *
 196  * | Special care is recommended.  Have Fun!
 197  *
 198  */
 199 
 200 /*
 201  * An overview of the pipelined operation mode.
 202  *
 203  * In the pipelined write mode, we will usually just add requests to our
 204  * pipeline and return immediately, before we even start to service them. The
 205  * user program will then have enough time to prepare the next request while
 206  * we are still busy servicing previous requests. In the pipelined read mode,
 207  * the situation is similar - we add read-ahead requests into the pipeline,
 208  * before the user even requested them.
 209  *
 210  * The pipeline can be viewed as a "safety net" which will be activated when
 211  * the system load is high and prevents the user backup program from keeping up
 212  * with the current tape speed. At this point, the pipeline will get
 213  * shorter and shorter but the tape will still be streaming at the same speed.
 214  * Assuming we have enough pipeline stages, the system load will hopefully
 215  * decrease before the pipeline is completely empty, and the backup program
 216  * will be able to "catch up" and refill the pipeline again.
 217  * 
 218  * When using the pipelined mode, it would be best to disable any type of
 219  * buffering done by the user program, as ide-tape already provides all the
 220  * benefits in the kernel, where it can be done in a more efficient way.
 221  * As we will usually not block the user program on a request, the most
 222  * efficient user code will then be a simple read-write-read-... cycle.
 223  * Any additional logic will usually just slow down the backup process.
 224  *
 225  * Using the pipelined mode, I get a constant over 400 KBps throughput,
 226  * which seems to be the maximum throughput supported by my tape.
 227  *
 228  * However, there are some downfalls:
 229  *
 230  *      1.      We use memory (for data buffers) in proportional to the number
 231  *              of pipeline stages (each stage is about 26 KB with my tape).
 232  *      2.      In the pipelined write mode, we cheat and postpone error codes
 233  *              to the user task. In read mode, the actual tape position
 234  *              will be a bit further than the last requested block.
 235  *
 236  * Concerning (1):
 237  *
 238  *      1.      We allocate stages dynamically only when we need them. When
 239  *              we don't need them, we don't consume additional memory. In
 240  *              case we can't allocate stages, we just manage without them
 241  *              (at the expense of decreased throughput) so when Linux is
 242  *              tight in memory, we will not pose additional difficulties.
 243  *
 244  *      2.      The maximum number of stages (which is, in fact, the maximum
 245  *              amount of memory) which we allocate is limited by the compile
 246  *              time parameter IDETAPE_MAX_PIPELINE_STAGES.
 247  *
 248  *      3.      The maximum number of stages is a controlled parameter - We
 249  *              don't start from the user defined maximum number of stages
 250  *              but from the lower IDETAPE_MIN_PIPELINE_STAGES (again, we
 251  *              will not even allocate this amount of stages if the user
 252  *              program can't handle the speed). We then implement a feedback
 253  *              loop which checks if the pipeline is empty, and if it is, we
 254  *              increase the maximum number of stages as necessary until we
 255  *              reach the optimum value which just manages to keep the tape
 256  *              busy with with minimum allocated memory or until we reach
 257  *              IDETAPE_MAX_PIPELINE_STAGES.
 258  *
 259  * Concerning (2):
 260  *
 261  *      In pipelined write mode, ide-tape can not return accurate error codes
 262  *      to the user program since we usually just add the request to the
 263  *      pipeline without waiting for it to be serviced. In case an error
 264  *      occurs, I will report it on the next user request.
 265  *
 266  *      In the pipelined read mode, subsequent read requests or forward
 267  *      filemark spacing will perform correctly, as we preserve all blocks
 268  *      and filemarks which we encountered during our excess read-ahead.
 269  * 
 270  *      For accurate tape positioning and error reporting, disabling
 271  *      pipelined mode might be the best option.
 272  *
 273  * You can enable/disable/tune the pipelined operation mode by adjusting
 274  * the compile time parameters in ide-tape.h.
 275  */
 276 
 277 /*
 278  *      Possible improvements.
 279  *
 280  *      1.      Support for the ATAPI overlap protocol.
 281  *
 282  *              In order to maximize bus throughput, we currently use the DSC
 283  *              overlap method which enables ide.c to service requests from the
 284  *              other device while the tape is busy executing a command. The
 285  *              DSC overlap method involves polling the tape's status register
 286  *              for the DSC bit, and servicing the other device while the tape
 287  *              isn't ready.
 288  *
 289  *              In the current QIC development standard (December 1995),
 290  *              it is recommended that new tape drives will *in addition* 
 291  *              implement the ATAPI overlap protocol, which is used for the
 292  *              same purpose - efficient use of the IDE bus, but is interrupt
 293  *              driven and thus has much less CPU overhead.
 294  *
 295  *              ATAPI overlap is likely to be supported in most new ATAPI
 296  *              devices, including new ATAPI cdroms, and thus provides us
 297  *              a method by which we can achieve higher throughput when
 298  *              sharing a (fast) ATA-2 disk with any (slow) new ATAPI device.
 299  */
 300 
 301 #include <linux/config.h>
 302 #include <linux/hdreg.h>
 303 #include <linux/types.h>
 304 #include <linux/string.h>
 305 #include <linux/kernel.h>
 306 #include <linux/delay.h>
 307 #include <linux/timer.h>
 308 #include <linux/mm.h>
 309 #include <linux/ioport.h>
 310 #include <linux/interrupt.h>
 311 #include <linux/major.h>
 312 #include <linux/blkdev.h>
 313 #include <linux/errno.h>
 314 #include <linux/hdreg.h>
 315 #include <linux/genhd.h>
 316 #include <linux/malloc.h>
 317 
 318 #include <asm/byteorder.h>
 319 #include <asm/irq.h>
 320 #include <asm/segment.h>
 321 #include <asm/io.h>
 322 
 323 /*
 324  *      Main Linux ide driver include file
 325  *
 326  *      Automatically includes our include file - ide-tape.h.
 327  */
 328  
 329 #include "ide.h"                
 330 
 331 /*
 332  *      Supported ATAPI tape drives packet commands
 333  */
 334 
 335 #define IDETAPE_TEST_UNIT_READY_CMD     0x00
 336 #define IDETAPE_REWIND_CMD              0x01
 337 #define IDETAPE_REQUEST_SENSE_CMD       0x03
 338 #define IDETAPE_READ_CMD                0x08
 339 #define IDETAPE_WRITE_CMD               0x0a
 340 #define IDETAPE_WRITE_FILEMARK_CMD      0x10
 341 #define IDETAPE_SPACE_CMD               0x11
 342 #define IDETAPE_INQUIRY_CMD             0x12
 343 #define IDETAPE_ERASE_CMD               0x19
 344 #define IDETAPE_MODE_SENSE_CMD          0x1a
 345 #define IDETAPE_LOAD_UNLOAD_CMD         0x1b
 346 #define IDETAPE_LOCATE_CMD              0x2b
 347 #define IDETAPE_READ_POSITION_CMD       0x34
 348 
 349 /*
 350  *      Some defines for the SPACE command
 351  *
 352  *      (The code field in the SPACE packet command).
 353  */
 354  
 355 #define IDETAPE_SPACE_OVER_FILEMARK     1
 356 #define IDETAPE_SPACE_TO_EOD            3
 357 
 358 /*
 359  *      Some defines for the LOAD UNLOAD command
 360  */
 361  
 362 #define IDETAPE_LU_LOAD_MASK            1
 363 #define IDETAPE_LU_RETENSION_MASK       2
 364 #define IDETAPE_LU_EOT_MASK             4
 365 
 366 /*
 367  *      Our ioctls - We will use 0x034n and 0x035n
 368  *
 369  *      Nothing special meanwhile.
 370  *      mtio.h MTIOCTOP compatible commands are supported on the character
 371  *      device interface.
 372  */
 373 
 374 /*
 375  *      Special requests for our block device strategy routine.
 376  *
 377  *      In order to service a character device command, we add special
 378  *      requests to the tail of our block device request queue and wait
 379  *      for their completion.
 380  *
 381  */
 382 
 383 #define IDETAPE_FIRST_REQUEST                   90
 384 
 385 /*
 386  *      IDETAPE_PACKET_COMMAND_REQUEST_TYPE1 is used to queue a packet command
 387  *      in the request queue. We will wait for DSC before issuing the command
 388  *      if it is still not set. In that case, we will temporary replace the
 389  *      cmd field to type 2 and restore it back to type 1 when we receive DSC
 390  *      and can start with sending the command.
 391  */
 392  
 393 #define IDETAPE_PACKET_COMMAND_REQUEST_TYPE1    90
 394 #define IDETAPE_PACKET_COMMAND_REQUEST_TYPE2    91
 395 
 396 /*
 397  *      IDETAPE_READ_REQUEST and IDETAPE_WRITE_REQUEST are used by our
 398  *      character device interface to request read/write operations from
 399  *      our block device interface.
 400  *
 401  *      In case a read or write request was requested by the buffer cache
 402  *      and not by our character device interface, the cmd field in the
 403  *      request will contain READ and WRITE instead.
 404  *
 405  *      We handle both cases in a similar way. The main difference is that
 406  *      in our own requests, buffer head is NULL and idetape_end_request
 407  *      will update the errors field if the request was not completed.
 408  */
 409 
 410 #define IDETAPE_READ_REQUEST                    92
 411 #define IDETAPE_WRITE_REQUEST                   93
 412 
 413 #define IDETAPE_LAST_REQUEST                    93
 414 
 415 /*
 416  *      A macro which can be used to check if a we support a given
 417  *      request command.
 418  */
 419 
 420 #define IDETAPE_REQUEST_CMD(cmd)        ((cmd >= IDETAPE_FIRST_REQUEST) && (cmd <= IDETAPE_LAST_REQUEST))
 421 
 422 /*
 423  *      We are now able to postpone an idetape request in the stage
 424  *      where it is polling for DSC and service requests from the other
 425  *      ide device meanwhile.
 426  */
 427 
 428 #define IDETAPE_RQ_POSTPONED            0x1234
 429 
 430 /*
 431  *      Error codes which are returned in rq->errors to the higher part
 432  *      of the driver.
 433  */
 434 
 435 #define IDETAPE_RQ_ERROR_GENERAL        1 
 436 #define IDETAPE_RQ_ERROR_FILEMARK       2
 437 #define IDETAPE_RQ_ERROR_EOD            3
 438 
 439 /*
 440  *      ATAPI Task File Registers (Re-definition of the ATA Task File
 441  *      Registers for an ATAPI packet command).
 442  *      From Table 3-2 of QIC-157C.
 443  */
 444 
 445 /* Read Access */
 446 
 447 #define IDETAPE_DATA_OFFSET             (0)
 448 #define IDETAPE_ERROR_OFFSET            (1)
 449 #define IDETAPE_IREASON_OFFSET          (2)
 450 #define IDETAPE_RESERVED3_OFFSET        (3)
 451 #define IDETAPE_BCOUNTL_OFFSET          (4)
 452 #define IDETAPE_BCOUNTH_OFFSET          (5)
 453 #define IDETAPE_DRIVESEL_OFFSET         (6)
 454 #define IDETAPE_STATUS_OFFSET           (7)
 455 
 456 #define IDETAPE_DATA_REG                (HWIF(drive)->io_base+IDETAPE_DATA_OFFSET)
 457 #define IDETAPE_ERROR_REG               (HWIF(drive)->io_base+IDETAPE_ERROR_OFFSET)
 458 #define IDETAPE_IREASON_REG             (HWIF(drive)->io_base+IDETAPE_IREASON_OFFSET)
 459 #define IDETAPE_RESERVED3_REG           (HWIF(drive)->io_base+IDETAPE_RESERVED3_OFFSET)
 460 #define IDETAPE_BCOUNTL_REG             (HWIF(drive)->io_base+IDETAPE_BCOUNTL_OFFSET)
 461 #define IDETAPE_BCOUNTH_REG             (HWIF(drive)->io_base+IDETAPE_BCOUNTH_OFFSET)
 462 #define IDETAPE_DRIVESEL_REG            (HWIF(drive)->io_base+IDETAPE_DRIVESEL_OFFSET)
 463 #define IDETAPE_STATUS_REG              (HWIF(drive)->io_base+IDETAPE_STATUS_OFFSET)
 464 
 465 /* Write Access */
 466 
 467 #define IDETAPE_FEATURES_OFFSET         (1)
 468 #define IDETAPE_ATACOMMAND_OFFSET       (7)
 469 
 470 #define IDETAPE_FEATURES_REG            (HWIF(drive)->io_base+IDETAPE_FEATURES_OFFSET)
 471 #define IDETAPE_ATACOMMAND_REG          (HWIF(drive)->io_base+IDETAPE_ATACOMMAND_OFFSET)
 472 #define IDETAPE_CONTROL_REG             (HWIF(drive)->ctl_port)
 473 
 474 
 475 /*
 476  *      Structure of the various task file registers
 477  */
 478 
 479 /*
 480  *      The ATAPI Status Register.
 481  */
 482  
 483 typedef union {
 484         unsigned all                    :8;
 485         struct {
 486                 unsigned check          :1;     /* Error occured */
 487                 unsigned idx            :1;     /* Reserved */
 488                 unsigned corr           :1;     /* Correctable error occured */
 489                 unsigned drq            :1;     /* Data is request by the device */
 490                 unsigned dsc            :1;     /* Set when a media access command is finished */
 491                                                 /* Reads / Writes are NOT media access commands */
 492                 unsigned reserved5      :1;     /* Reserved */
 493                 unsigned drdy           :1;     /* Ignored for ATAPI commands */
 494                                                 /* (The device is ready to accept ATA command) */
 495                 unsigned bsy            :1;     /* The device has access to the command block */
 496         } b;
 497 } idetape_status_reg_t;
 498 
 499 /*
 500  *      The ATAPI error register.
 501  */
 502  
 503 typedef union {
 504         unsigned all                    :8;
 505         struct {
 506                 unsigned ili            :1;     /* Illegal Length Indication */
 507                 unsigned eom            :1;     /* End Of Media Detected */
 508                 unsigned abrt           :1;     /* Aborted command - As defined by ATA */
 509                 unsigned mcr            :1;     /* Media Change Requested - As defined by ATA */
 510                 unsigned sense_key      :4;     /* Sense key of the last failed packet command */
 511         } b;
 512 } idetape_error_reg_t;
 513 
 514 /*
 515  *      ATAPI Feature Register
 516  */
 517  
 518 typedef union {
 519         unsigned all                    :8;
 520         struct {
 521                 unsigned dma            :1;     /* Using DMA of PIO */
 522                 unsigned reserved321    :3;     /* Reserved */
 523                 unsigned reserved654    :3;     /* Reserved (Tag Type) */
 524                 unsigned reserved7      :1;     /* Reserved */
 525         } b;
 526 } idetape_feature_reg_t;
 527 
 528 /*
 529  *      ATAPI Byte Count Register.
 530  */
 531  
 532 typedef union {
 533         unsigned all                    :16;
 534         struct {
 535                 unsigned low            :8;     /* LSB */
 536                 unsigned high           :8;     /* MSB */
 537         } b;
 538 } idetape_bcount_reg_t;
 539 
 540 /*
 541  *      ATAPI Interrupt Reason Register.
 542  */
 543  
 544 typedef union {
 545         unsigned all                    :8;
 546         struct {
 547                 unsigned cod            :1;     /* Information transferred is command (1) or data (0) */
 548                 unsigned io             :1;     /* The device requests us to read (1) or write (0) */
 549                 unsigned reserved       :6;     /* Reserved */
 550         } b;
 551 } idetape_ireason_reg_t;
 552 
 553 /*
 554  *      ATAPI Drive Select Register
 555  */
 556  
 557 typedef union { 
 558         unsigned all                    :8;
 559         struct {
 560                 unsigned sam_lun        :4;     /* Should be zero with ATAPI (not used) */
 561                 unsigned drv            :1;     /* The responding drive will be drive 0 (0) or drive 1 (1) */
 562                 unsigned one5           :1;     /* Should be set to 1 */
 563                 unsigned reserved6      :1;     /* Reserved */
 564                 unsigned one7           :1;     /* Should be set to 1 */
 565         } b;
 566 } idetape_drivesel_reg_t;
 567 
 568 /*
 569  *      ATAPI Device Control Register
 570  */
 571  
 572 typedef union {                 
 573         unsigned all                    :8;
 574         struct {
 575                 unsigned zero0          :1;     /* Should be set to zero */
 576                 unsigned nien           :1;     /* Device interrupt is disabled (1) or enabled (0) */
 577                 unsigned srst           :1;     /* ATA software reset. ATAPI devices should use the new ATAPI srst. */
 578                 unsigned one3           :1;     /* Should be set to 1 */
 579                 unsigned reserved4567   :4;     /* Reserved */
 580         } b;
 581 } idetape_control_reg_t;
 582 
 583 /*
 584  *      idetape_chrdev_t provides the link between out character device
 585  *      interface and our block device interface and the corresponding
 586  *      ide_drive_t structure.
 587  *
 588  *      We currently support only one tape drive.
 589  * 
 590  */
 591  
 592 typedef struct {
 593         ide_drive_t *drive;
 594         int major,minor;
 595         char name[4];
 596 } idetape_chrdev_t;
 597 
 598 /*
 599  *      The following is used to format the general configuration word of
 600  *      the ATAPI IDENTIFY DEVICE command.
 601  */
 602 
 603 struct idetape_id_gcw { 
 604 
 605         unsigned packet_size    :2;     /* Packet Size */
 606         unsigned reserved2      :1;     /* Reserved */
 607         unsigned reserved3      :1;     /* Reserved */
 608         unsigned reserved4      :1;     /* Reserved */
 609         unsigned drq_type       :2;     /* Command packet DRQ type */
 610         unsigned removable      :1;     /* Removable media */
 611         unsigned device_type    :5;     /* Device type */
 612         unsigned reserved13     :1;     /* Reserved */
 613         unsigned protocol       :2;     /* Protocol type */
 614 };
 615 
 616 /*
 617  *      INQUIRY packet command - Data Format (From Table 6-8 of QIC-157C)
 618  */
 619  
 620 typedef struct {
 621         unsigned device_type    :5;     /* Peripheral Device Type */
 622         unsigned reserved0_765  :3;     /* Peripheral Qualifier - Reserved */
 623         unsigned reserved1_6t0  :7;     /* Reserved */
 624         unsigned rmb            :1;     /* Removable Medium Bit */
 625         unsigned ansi_version   :3;     /* ANSI Version */
 626         unsigned ecma_version   :3;     /* ECMA Version */
 627         unsigned iso_version    :2;     /* ISO Version */
 628         unsigned response_format :4;    /* Response Data Format */
 629         unsigned reserved3_45   :2;     /* Reserved */
 630         unsigned reserved3_6    :1;     /* TrmIOP - Reserved */
 631         unsigned reserved3_7    :1;     /* AENC - Reserved */
 632         byte additional_length;         /* Additional Length (total_length-4) */
 633         byte reserved_5;                /* Reserved */
 634         byte reserved_6;                /* Reserved */
 635         unsigned reserved7_0    :1;     /* SftRe - Reserved */
 636         unsigned reserved7_1    :1;     /* CmdQue - Reserved */
 637         unsigned reserved7_2    :1;     /* Reserved */
 638         unsigned reserved7_3    :1;     /* Linked - Reserved */
 639         unsigned reserved7_4    :1;     /* Sync - Reserved */
 640         unsigned reserved7_5    :1;     /* WBus16 - Reserved */
 641         unsigned reserved7_6    :1;     /* WBus32 - Reserved */
 642         unsigned reserved7_7    :1;     /* RelAdr - Reserved */
 643         byte vendor_id [8];             /* Vendor Identification */
 644         byte product_id [16];           /* Product Identification */
 645         byte revision_level [4];        /* Revision Level */
 646         byte vendor_specific [20];      /* Vendor Specific - Optional */
 647         byte reserved56t95 [40];        /* Reserved - Optional */
 648         
 649                                         /* Additional information may be returned */
 650 } idetape_inquiry_result_t;
 651 
 652 /*
 653  *      READ POSITION packet command - Data Format (From Table 6-57)
 654  */
 655  
 656 typedef struct {
 657         unsigned reserved0_10   :2;     /* Reserved */
 658         unsigned bpu            :1;     /* Block Position Unknown */    
 659         unsigned reserved0_543  :3;     /* Reserved */
 660         unsigned eop            :1;     /* End Of Partition */
 661         unsigned bop            :1;     /* Begining Of Partition */
 662         byte partition_num;             /* Partition Number */
 663         byte reserved_2;                /* Reserved */
 664         byte reserved_3;                /* Reserved */
 665         unsigned long first_block;      /* First Block Location */
 666         unsigned long last_block;       /* Last Block Location (Optional) */
 667         byte reserved_12;               /* Reserved */
 668         byte blocks_in_buffer_2;        /* Blocks In Buffer - MSB (Optional) */
 669         byte blocks_in_buffer_1;
 670         byte blocks_in_buffer_0;        /* Blocks In Buffer - LSB (Optional) */
 671         unsigned long bytes_in_buffer;  /* Bytes In Buffer (Optional) */
 672 } idetape_read_position_result_t;
 673 
 674 /*
 675  *      REQUEST SENSE packet command result - Data Format.
 676  */
 677 
 678 typedef struct {
 679         unsigned error_code     :7;     /* Current of deferred errors */
 680         unsigned valid          :1;     /* The information field conforms to QIC-157C */
 681         unsigned reserved_1     :8;     /* Segment Number - Reserved */
 682         unsigned sense_key      :4;     /* Sense Key */
 683         unsigned reserved2_4    :1;     /* Reserved */
 684         unsigned ili            :1;     /* Incorrect Length Indicator */
 685         unsigned eom            :1;     /* End Of Medium */
 686         unsigned filemark       :1;     /* Filemark */
 687 
 688         /*
 689          *      We can't use a 32 bit variable, since it will be re-aligned
 690          *      by GCC, as we are not on a 32 bit boundary.
 691          */
 692 
 693         byte information1;              /* MSB - Information - Command specific */
 694         byte information2;
 695         byte information3;
 696         byte information4;              /* LSB */
 697         byte asl;                       /* Additional sense length (n-7) */
 698         unsigned long command_specific; /* Additional command specific information */
 699         byte asc;                       /* Additional Sense Code */
 700         byte ascq;                      /* Additional Sense Code Qualifier */
 701         byte replaceable_unit_code;     /* Field Replaceable Unit Code */
 702         unsigned sk_specific1   :7;     /* Sense Key Specific */
 703         unsigned sksv           :1;     /* Sense Key Specific informatio is valid */
 704         byte sk_specific2;              /* Sense Key Specific */
 705         byte sk_specific3;              /* Sense Key Specific */
 706         byte pad [2];                   /* Padding to 20 bytes */
 707 } idetape_request_sense_result_t;
 708 
 709 /*
 710  *      Follows structures which are realted to the SELECT SENSE / MODE SENSE
 711  *      packet commands. Those packet commands are still not supported
 712  *      by ide-tape.
 713  */
 714 
 715 #define IDETAPE_CAPABILITIES_PAGE       0x2a
 716 
 717 /*
 718  *      Mode Parameter Header for the MODE SENSE packet command
 719  */
 720 
 721 typedef struct {
 722         byte mode_data_length;          /* The length of the following data that is */
 723                                         /* available to be transferred */
 724         byte medium_type;               /* Medium Type */
 725         byte dsp;                       /* Device Specific Parameter */
 726         byte bdl;                       /* Block Descriptor Length */
 727 } idetape_mode_parameter_header_t;
 728 
 729 /*
 730  *      Mode Parameter Block Descriptor the MODE SENSE packet command
 731  *
 732  *      Support for block descriptors is optional.
 733  */
 734 
 735 typedef struct {
 736         byte density_code;              /* Medium density code */
 737         byte blocks1;                   /* Number of blocks - MSB */
 738         byte blocks2;                   /* Number of blocks - Middle byte */
 739         byte blocks3;                   /* Number of blocks - LSB */
 740         byte reserved4;                 /* Reserved */
 741         byte length1;                   /* Block Length - MSB */
 742         byte length2;                   /* Block Length - Middle byte */
 743         byte length3;                   /* Block Length - LSB */
 744 } idetape_parameter_block_descriptor_t;
 745 
 746 /*
 747  *      The Data Compression Page, as returned by the MODE SENSE packet command.
 748  */
 749  
 750 typedef struct {
 751         unsigned page_code      :6;     /* Page Code - Should be 0xf */
 752         unsigned reserved       :1;     /* Reserved */
 753         unsigned ps             :1;
 754         byte page_length;               /* Page Length - Should be 14 */
 755         unsigned reserved2      :6;     /* Reserved */
 756         unsigned dcc            :1;     /* Data Compression Capable */
 757         unsigned dce            :1;     /* Data Compression Enable */
 758         unsigned reserved3      :5;     /* Reserved */
 759         unsigned red            :2;     /* Report Exception on Decompression */
 760         unsigned dde            :1;     /* Data Decompression Enable */
 761         unsigned long ca;               /* Compression Algorithm */
 762         unsigned long da;               /* Decompression Algorithm */
 763         byte reserved_12;               /* Reserved */
 764         byte reserved_13;               /* Reserved */
 765         byte reserved_14;               /* Reserved */
 766         byte reserved_15;               /* Reserved */
 767 } idetape_data_compression_page_t;
 768 
 769 /*
 770  *      The Medium Partition Page, as returned by the MODE SENSE packet command.
 771  */
 772 
 773 typedef struct {
 774         unsigned page_code      :6;     /* Page Code - Should be 0x11 */
 775         unsigned reserved1_6    :1;     /* Reserved */
 776         unsigned ps             :1;
 777         byte page_length;               /* Page Length - Should be 6 */
 778         byte map;                       /* Maximum Additional Partitions - Should be 0 */
 779         byte apd;                       /* Additional Partitions Defined - Should be 0 */
 780         unsigned reserved4_012  :3;     /* Reserved */
 781         unsigned psum           :2;     /* Should be 0 */
 782         unsigned idp            :1;     /* Should be 0 */
 783         unsigned sdp            :1;     /* Should be 0 */
 784         unsigned fdp            :1;     /* Fixed Data Partitions */
 785         byte mfr;                       /* Medium Format Recognition */
 786         byte reserved6;                 /* Reserved */
 787         byte reserved7;                 /* Reserved */
 788 } idetape_medium_partition_page_t;
 789 
 790 /*
 791  *      Prototypes of various functions in ide-tape.c
 792  *
 793  *      The following functions are called from ide.c, and their prototypes
 794  *      are available in ide.h:
 795  *
 796  *              idetape_identify_device
 797  *              idetape_setup
 798  *              idetape_blkdev_ioctl
 799  *              idetape_do_request
 800  *              idetape_blkdev_open
 801  *              idetape_blkdev_release
 802  *              idetape_register_chrdev (void);
 803  */
 804 
 805 /*
 806  *      The following functions are used to transfer data from / to the
 807  *      tape's data register.
 808  */
 809  
 810 void idetape_input_data (ide_drive_t *drive,void *buffer, unsigned long bcount);
 811 void idetape_output_data (ide_drive_t *drive,void *buffer, unsigned long bcount);
 812 void idetape_discard_data (ide_drive_t *drive, unsigned long bcount);
 813 
 814 /*
 815  *      Packet command related functions.
 816  */
 817  
 818 void idetape_issue_packet_command  (ide_drive_t *drive,idetape_packet_command_t *pc,ide_handler_t *handler);
 819 void idetape_pc_intr (ide_drive_t *drive);
 820 
 821 /*
 822  *      DSC handling functions.
 823  */
 824  
 825 void idetape_postpone_request (ide_drive_t *drive);
 826 void idetape_poll_for_dsc (unsigned long data);
 827 void idetape_poll_for_dsc_direct (unsigned long data);
 828 void idetape_put_back_postponed_request (ide_drive_t *drive);
 829 void idetape_media_access_finished (ide_drive_t *drive);
 830 
 831 /*
 832  *      Some more packet command related functions.
 833  */
 834  
 835 void idetape_pc_callback (ide_drive_t *drive);
 836 void idetape_retry_pc (ide_drive_t *drive);
 837 void idetape_zero_packet_command (idetape_packet_command_t *pc);
 838 void idetape_queue_pc_head (ide_drive_t *drive,idetape_packet_command_t *pc,struct request *rq);
 839 void idetape_analyze_error (ide_drive_t *drive,idetape_request_sense_result_t *result);
 840 
 841 idetape_packet_command_t *idetape_next_pc_storage (ide_drive_t *drive);
 842 struct request *idetape_next_rq_storage (ide_drive_t *drive);
 843 
 844 /*
 845  *      Various packet commands
 846  */
 847  
 848 void idetape_create_inquiry_cmd (idetape_packet_command_t *pc);
 849 void idetape_inquiry_callback (ide_drive_t *drive);
 850 void idetape_create_locate_cmd (idetape_packet_command_t *pc,unsigned long block,byte partition);
 851 void idetape_create_rewind_cmd (idetape_packet_command_t *pc);
 852 void idetape_create_write_filemark_cmd (idetape_packet_command_t *pc,int write_filemark);
 853 void idetape_create_load_unload_cmd (idetape_packet_command_t *pc,int cmd);
 854 void idetape_create_space_cmd (idetape_packet_command_t *pc,long count,byte cmd);
 855 void idetape_create_erase_cmd (idetape_packet_command_t *pc);
 856 void idetape_create_test_unit_ready_cmd (idetape_packet_command_t *pc);
 857 void idetape_create_read_position_cmd (idetape_packet_command_t *pc);
 858 void idetape_read_position_callback (ide_drive_t *drive);
 859 void idetape_create_read_cmd (idetape_packet_command_t *pc,unsigned long length);
 860 void idetape_read_callback (ide_drive_t *drive);
 861 void idetape_create_write_cmd (idetape_packet_command_t *pc,unsigned long length);
 862 void idetape_write_callback (ide_drive_t *drive);
 863 void idetape_create_request_sense_cmd (idetape_packet_command_t *pc);
 864 void idetape_create_mode_sense_cmd (idetape_packet_command_t *pc,byte page_code);
 865 void idetape_request_sense_callback (ide_drive_t *drive);
 866 
 867 void idetape_display_inquiry_result (byte *buffer);
 868 
 869 /*
 870  *      Character device callback functions.
 871  *
 872  *      We currently support:
 873  *
 874  *              OPEN, RELEASE, READ, WRITE and IOCTL.
 875  */
 876 
 877 int idetape_chrdev_read (struct inode *inode, struct file *file, char *buf, int count);
 878 int idetape_chrdev_write (struct inode *inode, struct file *file, const char *buf, int count);
 879 int idetape_chrdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
 880 int idetape_chrdev_open (struct inode *inode, struct file *file);
 881 void idetape_chrdev_release (struct inode *inode,struct file *file);
 882 
 883 /*
 884  *      idetape_mtioctop implements general magnetic tape io control
 885  *      commands, as defined in include/linux/mtio.h. Those commands are
 886  *      accessed through the character device interface, using the MTIOCTOP
 887  *      ioctl.
 888  */
 889  
 890 int idetape_mtioctop (ide_drive_t *drive,short mt_op,int mt_count);
 891 
 892 /*
 893  *      idetape_space_over_filemarks handles the MTFSF, MTFSFM, ... mtio.h
 894  *      commands.
 895  */
 896  
 897 int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count);
 898 
 899 /*
 900  *      idetape_add_chrdev_read_request is called from idetape_chrdev_read
 901  *      to service a character device read request and add read-ahead
 902  *      requests to our pipeline.
 903  */
 904  
 905 int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks,char *buffer);
 906 
 907 /*
 908  *      idetape_add_chrdev_write_request adds a character device write
 909  *      request to the pipeline.
 910  */
 911  
 912 int idetape_add_chrdev_write_request (ide_drive_t *drive,int blocks,char *buffer);
 913 
 914 /*
 915  *      idetape_queue_rw_tail will add a command to the tail of the device
 916  *      request queue and wait for it to finish. This is used when we
 917  *      can not allocate pipeline stages (or in non-pipelined mode).
 918  */
 919  
 920 int idetape_queue_rw_tail (ide_drive_t *drive,int cmd,int blocks,char *buffer);
 921 
 922 /*
 923  *      Adds a packet command request to the tail of the device request
 924  *      queue and waits for it to be serviced.
 925  */
 926  
 927 int idetape_queue_pc_tail (ide_drive_t *drive,idetape_packet_command_t *pc);
 928 
 929 int idetape_position_tape (ide_drive_t *drive,unsigned long block);
 930 int idetape_rewind_tape (ide_drive_t *drive);
 931 int idetape_flush_tape_buffers (ide_drive_t *drive);
 932 
 933 /*
 934  *      Used to get device information
 935  */
 936 
 937 void idetape_get_mode_sense_results (ide_drive_t *drive);
 938 
 939 /*
 940  *      General utility functions
 941  */
 942  
 943 unsigned long idetape_swap_long (unsigned long temp);
 944 unsigned short idetape_swap_short (unsigned short temp);
 945 
 946 #define IDETAPE_MIN(a,b)        ((a)<(b) ? (a):(b))
 947 
 948 /*
 949  *      Pipeline related functions
 950  */
 951 
 952 idetape_pipeline_stage_t *idetape_kmalloc_stage (ide_drive_t *drive);
 953 void idetape_kfree_stage (idetape_pipeline_stage_t *stage);
 954 void idetape_copy_buffer_from_stage (idetape_pipeline_stage_t *stage,char *buffer);
 955 void idetape_copy_buffer_to_stage (idetape_pipeline_stage_t *stage,char *buffer);
 956 void idetape_increase_max_pipeline_stages (ide_drive_t *drive);
 957 void idetape_add_stage_tail (ide_drive_t *drive,idetape_pipeline_stage_t *stage);
 958 void idetape_remove_stage_head (ide_drive_t *drive);
 959 void idetape_active_next_stage (ide_drive_t *drive);
 960 void idetape_wait_for_pipeline (ide_drive_t *drive);
 961 void idetape_discard_read_pipeline (ide_drive_t *drive);
 962 void idetape_empty_write_pipeline (ide_drive_t *drive);
 963 void idetape_insert_pipeline_into_queue (ide_drive_t *drive);
 964 
 965 /*
 966  *      For general magnetic tape device compatibility.
 967  */
 968  
 969 #include <linux/mtio.h>
 970 
 971 /*
 972  *      Global variables
 973  *
 974  *      The variables below are used for the character device interface.
 975  *
 976  *      Additional state variables are defined in our ide_drive_t structure.
 977  */
 978  
 979 idetape_chrdev_t idetape_chrdev;                /* Character device interface information */
 980 byte idetape_drive_already_found=0;             /* 1 when the above data structure is initialized */
 981 
 982 /*
 983  *      Our character device supporting functions, passed to register_chrdev.
 984  */
 985  
 986 static struct file_operations idetape_fops = {
 987         NULL,                   /* lseek - default */
 988         idetape_chrdev_read,    /* read  */
 989         idetape_chrdev_write,   /* write */
 990         NULL,                   /* readdir - bad */
 991         NULL,                   /* select */
 992         idetape_chrdev_ioctl,   /* ioctl */
 993         NULL,                   /* mmap */
 994         idetape_chrdev_open,    /* open */
 995         idetape_chrdev_release, /* release */
 996         NULL,                   /* fsync */
 997         NULL,                   /* fasync */
 998         NULL,                   /* check_media_change */
 999         NULL                    /* revalidate */
1000 };
1001 
1002 
1003 /*
1004  *      idetape_identify_device is called by do_identify in ide.c during
1005  *      the device probing stage to check the contents of the ATAPI IDENTIFY
1006  *      command results, in case the device type is tape. We return:
1007  *
1008  *      1       If the tape can be supported by us, based on the information
1009  *              we have so far.
1010  *
1011  *      0       If this tape driver is not currently supported by us.
1012  *
1013  *      In case we decide to support the tape, we store the current drive
1014  *      pointer in our character device global variables, so that we can
1015  *      pass between both interfaces.
1016  */
1017  
1018 int idetape_identify_device (ide_drive_t *drive,struct hd_driveid *id)
     /* [previous][next][first][last][top][bottom][index][help] */
1019 
1020 {
1021         struct idetape_id_gcw gcw;
1022         unsigned short *ptr;
1023         int support=1;
1024 #if IDETAPE_DEBUG_LOG
1025         unsigned short mask,i;
1026 #endif /* IDETAPE_DEBUG_LOG */
1027                 
1028         ptr=(unsigned short *) &gcw;
1029         *ptr=id->config;
1030 
1031 #if IDETAPE_DEBUG_LOG
1032         printk ("Dumping ATAPI Identify Device tape parameters\n");
1033         
1034         printk ("Protocol Type: ");
1035         switch (gcw.protocol) {
1036                 case 0: case 1: printk ("ATA\n");break;
1037                 case 2: printk ("ATAPI\n");break;
1038                 case 3: printk ("Reserved (Unknown to ide-tape)\n");break;
1039         }
1040         
1041         printk ("Device Type: %x - ",gcw.device_type);  
1042         switch (gcw.device_type) {
1043                 case 0: printk ("Direct-access Device\n");break;
1044                 case 1: printk ("Streaming Tape Device\n");break;
1045                 case 2: case 3: case 4: printk ("Reserved\n");break;
1046                 case 5: printk ("CD-ROM Device\n");break;
1047                 case 6: printk ("Reserved\n");
1048                 case 7: printk ("Optical memory Device\n");break;
1049                 case 0x1f: printk ("Unknown or no Device type\n");break;
1050                 default: printk ("Reserved\n");
1051         }
1052         printk ("Removable: %s",gcw.removable ? "Yes\n":"No\n");        
1053                 
1054         printk ("Command Packet DRQ Type: ");
1055         switch (gcw.drq_type) {
1056                 case 0: printk ("Microprocessor DRQ\n");break;
1057                 case 1: printk ("Interrupt DRQ\n");break;
1058                 case 2: printk ("Accelerated DRQ\n");break;
1059                 case 3: printk ("Reserved\n");break;
1060         }
1061         
1062         printk ("Command Packet Size: ");
1063         switch (gcw.packet_size) {
1064                 case 0: printk ("12 bytes\n");break;
1065                 case 1: printk ("16 bytes\n");break;
1066                 default: printk ("Reserved\n");break;
1067         }
1068         printk ("Model: %s\n",id->model);
1069         printk ("Firmware Revision: %s\n",id->fw_rev);
1070         printk ("Serial Number: %s\n",id->serial_no);
1071         printk ("Write buffer size: %d bytes\n",id->buf_size*512);
1072         printk ("DMA: %s",id->capability & 0x01 ? "Yes\n":"No\n");
1073         printk ("LBA: %s",id->capability & 0x02 ? "Yes\n":"No\n");
1074         printk ("IORDY can be disabled: %s",id->capability & 0x04 ? "Yes\n":"No\n");
1075         printk ("IORDY supported: %s",id->capability & 0x08 ? "Yes\n":"Unknown\n");
1076         printk ("PIO Cycle Timing Category: %d\n",id->tPIO);
1077         printk ("DMA Cycle Timing Category: %d\n",id->tDMA);
1078         printk ("Single Word DMA supported modes: ");
1079         for (i=0,mask=1;i<8;i++,mask=mask << 1) {
1080                 if (id->dma_1word & mask)
1081                         printk ("%d ",i);
1082                 if (id->dma_1word & (mask << 8))
1083                         printk ("(active) ");
1084         }
1085         printk ("\n");
1086 
1087         printk ("Multi Word DMA supported modes: ");
1088         for (i=0,mask=1;i<8;i++,mask=mask << 1) {
1089                 if (id->dma_mword & mask)
1090                         printk ("%d ",i);
1091                 if (id->dma_mword & (mask << 8))
1092                         printk ("(active) ");
1093         }
1094         printk ("\n");
1095 
1096         if (id->field_valid & 0x0002) {
1097                 printk ("Enhanced PIO Modes: %s\n",id->eide_pio_modes & 1 ? "Mode 3":"None");
1098                 printk ("Minimum Multi-word DMA cycle per word: ");
1099                 if (id->eide_dma_min == 0)
1100                         printk ("Not supported\n");
1101                 else
1102                         printk ("%d ns\n",id->eide_dma_min);
1103 
1104                 printk ("Manafactuer\'s Recommended Multi-word cycle: ");
1105                 if (id->eide_dma_time == 0)
1106                         printk ("Not supported\n");
1107                 else
1108                         printk ("%d ns\n",id->eide_dma_time);
1109 
1110                 printk ("Minimum PIO cycle without IORDY: ");
1111                 if (id->eide_pio == 0)
1112                         printk ("Not supported\n");
1113                 else
1114                         printk ("%d ns\n",id->eide_pio);
1115 
1116                 printk ("Minimum PIO cycle with IORDY: ");
1117                 if (id->eide_pio_iordy == 0)
1118                         printk ("Not supported\n");
1119                 else
1120                         printk ("%d ns\n",id->eide_pio_iordy);
1121                 
1122         }
1123 
1124         else {
1125                 printk ("According to the device, fields 64-70 are not valid.\n");
1126         }
1127 #endif /* IDETAPE_DEBUG_LOG */
1128 
1129         /* Check that we can support this device */
1130 
1131         if (gcw.protocol !=2 ) {
1132                 printk ("ide-tape: Protocol is not ATAPI\n");support=0;
1133         }
1134 
1135         if (gcw.device_type != 1) {
1136                 printk ("ide-tape: Device type is not set to tape\n");support=0;
1137         }
1138 
1139         if (!gcw.removable) {
1140                 printk ("ide-tape: The removable flag is not set\n");support=0;
1141         }
1142 
1143         if (gcw.drq_type != 2) {
1144                 printk ("ide-tape: Sorry, DRQ types other than Accelerated DRQ\n");
1145                 printk ("ide-tape: are still not supproted by the driver\n");support=0;
1146         }
1147 
1148         if (gcw.packet_size != 0) {
1149                 printk ("ide-tape: Packet size is not 12 bytes long\n");
1150                 if (gcw.packet_size == 1)
1151                         printk ("ide-tape: Sorry, padding to 16 bytes is still not supported\n");
1152                 support=0;                      
1153         }
1154 
1155         if (idetape_drive_already_found) {
1156                 printk ("ide-tape: Sorry, only one ide tape drive is supported by the driver\n");
1157                 support=0;
1158         }
1159         else {
1160                 idetape_drive_already_found=1;
1161                 idetape_chrdev.drive=drive;
1162                 idetape_chrdev.major=IDETAPE_MAJOR;
1163                 idetape_chrdev.minor=0;
1164                 idetape_chrdev.name[0]='h';
1165                 idetape_chrdev.name[1]='t';
1166                 idetape_chrdev.name[2]='0';
1167                 idetape_chrdev.name[3]=0;
1168         }
1169 
1170         return (support);               /* In case support=0, we will not install the driver */
1171 }
1172 
1173 /*
1174  *      idetape_register_chrdev calls register_chrdev to register our character
1175  *      device interface. The connection to the ide_drive_t structure, which
1176  *      is used by the entire ide driver is provided by our global variable
1177  *      idetape_chrdev.drive, which was initialized earlier, during the device
1178  *      probing stage.
1179  */
1180  
1181 void idetape_register_chrdev (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1182 
1183 {
1184         int major,minor;
1185         ide_drive_t *drive;
1186 
1187         if (!idetape_drive_already_found)
1188                 return;
1189 
1190         drive=idetape_chrdev.drive;
1191         major=idetape_chrdev.major;
1192         minor=idetape_chrdev.minor;
1193         
1194         if (register_chrdev (major,idetape_chrdev.name,&idetape_fops)) {
1195                 printk ("Unable to register character device interface !\n");
1196                 /* ??? */
1197         }
1198         else {
1199                 printk ("ide-tape: %s <-> %s : Character device interface on major = %d\n",
1200                         drive->name,idetape_chrdev.name,major);
1201         }
1202 }
1203 
1204 /*
1205  *      idetape_setup is called from the ide driver in the partition table
1206  *      identification stage, to:
1207  *
1208  *              1.      Initialize our various state variables.
1209  *              2.      Ask the tape for its capabilities.
1210  *              3.      Allocate a buffer which will be used for data
1211  *                      transfer. The buffer size is chosen based on
1212  *                      the recommendation which we received in step (2).
1213  *
1214  *      Note that at this point ide.c already assigned us an irq, so that
1215  *      we can queue requests here and wait for their completion.
1216  */
1217  
1218 void idetape_setup (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1219 
1220 {
1221         idetape_tape_t *tape=&(drive->tape);
1222         unsigned int allocation_length;
1223         double service_time,nr_units;
1224                 
1225 #if IDETAPE_DEBUG_LOG
1226         printk ("ide-tape: Reached idetape_setup\n");
1227 #endif /* IDETAPE_DEBUG_LOG */  
1228         
1229         drive->ready_stat = 0;                  /* With an ATAPI device, we can issue packet commands */
1230                                                 /* regardless of the state of DRDY */
1231         HWIF(drive)->tape_drive=drive;
1232 
1233         tape->block_address=0;                  
1234         tape->block_address_valid=0;
1235         tape->pc_stack_index=0;
1236         tape->failed_pc=NULL;
1237         tape->postponed_rq=NULL;
1238         tape->busy=0;
1239         tape->active_data_request=NULL;
1240         tape->current_number_of_stages=0;
1241         tape->first_stage=tape->next_stage=tape->last_stage=NULL;
1242         tape->error_in_pipeline_stage=0;
1243         tape->request_status=0;
1244         tape->chrdev_direction=idetape_direction_none;
1245         tape->reset_issued=0;
1246 
1247 #if IDETAPE_PIPELINE
1248         tape->max_number_of_stages=IDETAPE_MIN_PIPELINE_STAGES;
1249         printk ("ide-tape: Operating in pipelined (fast and tricky) operation mode.\n");
1250 #else
1251         tape->max_number_of_stages=0;
1252         printk ("ide-tape: Operating in non-pipelined (slow and safe) operation mode.\n");
1253 #endif /* IDETAPE_PIPELINE */
1254 
1255         idetape_get_mode_sense_results (drive);
1256 
1257         tape->data_buffer_size=tape->capabilities.ctl*tape->tape_block_size;
1258 
1259         allocation_length=tape->data_buffer_size;
1260         if (tape->data_buffer_size % IDETAPE_ALLOCATION_BLOCK)
1261                 allocation_length+=IDETAPE_ALLOCATION_BLOCK;
1262         
1263 #if IDETAPE_MINIMIZE_IDLE_MEMORY_USAGE
1264         tape->data_buffer=tape->merge_buffer=NULL;
1265 #else
1266         tape->data_buffer=kmalloc (allocation_length,GFP_KERNEL);
1267         tape->merge_buffer=kmalloc (allocation_length,GFP_KERNEL);
1268         if (tape->data_buffer == NULL || tape->merge_buffer == NULL) {
1269                 printk ("ide-tape: FATAL - Can not allocate 2 buffers of %d bytes each\n",allocation_length);
1270                 printk ("ide-tape: Aborting character device installation\n");
1271                 idetape_drive_already_found=0;
1272                 unregister_chrdev (idetape_chrdev.major,idetape_chrdev.name);
1273                 return;
1274         }
1275 #endif /* IDETAPE_MINIMIZE_IDLE_MEMORY_USAGE */
1276 
1277         tape->merge_buffer_size=tape->merge_buffer_offset=0;
1278         
1279 #if IDETAPE_ANTICIPATE_READ_WRITE_DSC
1280 
1281         /*
1282          *      Cleverly select the DSC read/write polling frequency, based
1283          *      on the tape's speed, its recommended transfer unit, its
1284          *      internal buffer size and our operation mode.
1285          *
1286          *      In the pipelined operation mode we aim for "catching" the
1287          *      tape when its internal buffer is about 50% full. This will
1288          *      dramatically reduce our polling frequency and will also
1289          *      leave enough time for the ongoing request of the other device
1290          *      to complete before the buffer is completly empty. We will
1291          *      then completly refill the buffer with requests from our
1292          *      internal pipeline.
1293          *
1294          *      When operating in the non-pipelined operation mode, we
1295          *      can't allow ourself this luxury. Instead, we will try to take
1296          *      full advantage of the internal tape buffer by waiting only
1297          *      for one request to complete. This will increase our load
1298          *      on linux but will usually still fail to keep the tape
1299          *      constantly streaming.
1300          */
1301 
1302         service_time=((double) tape->data_buffer_size/1024.0)/((double) tape->capabilities.speed*(1000.0/1024.0));
1303         nr_units=(double) tape->capabilities.buffer_size*512.0/(double) tape->data_buffer_size;
1304 
1305         if (tape->max_number_of_stages) 
1306                 tape->best_dsc_rw_frequency=(unsigned long) (0.5*nr_units*service_time*HZ);
1307         else            
1308                 tape->best_dsc_rw_frequency=(unsigned long) (service_time*HZ);
1309         
1310         /*
1311          *      Ensure that the number we got makes sense.
1312          */
1313 
1314         if (tape->best_dsc_rw_frequency > IDETAPE_DSC_READ_WRITE_LOWEST_FREQUENCY) {
1315                 printk ("ide-tape: Although the recommended polling period is %lu jiffies, \n",tape->best_dsc_rw_frequency);
1316                 printk ("ide-tape: we will use %u jiffies\n",IDETAPE_DSC_READ_WRITE_LOWEST_FREQUENCY);
1317                 printk ("ide-tape: (It may well be that we are wrong here)\n");
1318                 tape->best_dsc_rw_frequency = IDETAPE_DSC_READ_WRITE_LOWEST_FREQUENCY;
1319         }
1320 
1321         if (tape->best_dsc_rw_frequency < IDETAPE_DSC_READ_WRITE_FALLBACK_FREQUENCY) {
1322                 printk ("ide-tape: Although the recommended polling period is %lu jiffies, \n",tape->best_dsc_rw_frequency);
1323                 printk ("ide-tape: we will use %u jiffies\n",IDETAPE_DSC_READ_WRITE_FALLBACK_FREQUENCY);
1324                 tape->best_dsc_rw_frequency = IDETAPE_DSC_READ_WRITE_FALLBACK_FREQUENCY;
1325         }
1326 
1327 #else
1328         tape->best_dsc_rw_frequency=IDETAPE_DSC_READ_WRITE_FALLBACK_FREQUENCY;
1329 #endif /* IDETAPE_ANTICIPATE_READ_WRITE_DSC */
1330 
1331         printk ("ide-tape: Tape speed - %d KBps. Recommended transfer unit - %d bytes.\n",tape->capabilities.speed,tape->data_buffer_size);
1332 
1333         return;
1334 }
1335 
1336 /*
1337  *      idetape_get_mode_sense_results asks the tape about its various
1338  *      parameters. In particular, we will adjust our data transfer buffer
1339  *      size to the recommended value as returned by the tape.
1340  */
1341 
1342 void idetape_get_mode_sense_results (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1343 
1344 {
1345         int retval;
1346         idetape_tape_t *tape=&(drive->tape);
1347         idetape_mode_parameter_header_t *header;
1348         idetape_capabilities_page_t *capabilities;
1349         idetape_packet_command_t pc;
1350         
1351         idetape_create_mode_sense_cmd (&pc,IDETAPE_CAPABILITIES_PAGE);
1352         pc.buffer=pc.temp_buffer;
1353         pc.buffer_size=IDETAPE_TEMP_BUFFER_SIZE;
1354         pc.current_position=pc.temp_buffer;
1355         retval=idetape_queue_pc_tail (drive,&pc);
1356 
1357         header=(idetape_mode_parameter_header_t *) pc.buffer;   
1358         capabilities=(idetape_capabilities_page_t *) (pc.buffer+sizeof (idetape_mode_parameter_header_t));
1359 
1360         capabilities->max_speed=idetape_swap_short (capabilities->max_speed);
1361         capabilities->ctl=idetape_swap_short (capabilities->ctl);
1362         capabilities->speed=idetape_swap_short (capabilities->speed);
1363         capabilities->buffer_size=idetape_swap_short (capabilities->buffer_size);
1364 
1365         tape->capabilities=*capabilities;               /* Save us a copy */
1366         tape->tape_block_size=capabilities->blk512 ? 512:1024;
1367 
1368         if (retval) {
1369                 printk ("ide-tape: Can't get tape parameters\n");
1370                 printk ("ide-tape: Assuming some default parameters\n");
1371                 tape->tape_block_size=512;
1372                 tape->capabilities.ctl=52;
1373                 tape->capabilities.speed=450;
1374                 tape->capabilities.buffer_size=6*52;
1375                 return;
1376         }
1377 
1378 #if IDETAPE_DEBUG_LOG
1379         printk ("Dumping the results of the MODE SENSE packet command\n");
1380         printk ("Mode Parameter Header:\n");
1381         printk ("Mode Data Length - %d\n",header->mode_data_length);
1382         printk ("Medium Type - %d\n",header->medium_type);
1383         printk ("Device Specific Parameter - %d\n",header->dsp);
1384         printk ("Block Descriptor Length - %d\n",header->bdl);
1385         
1386         printk ("Capabilities and Mechanical Status Page:\n");
1387         printk ("Page code - %d\n",capabilities->page_code);
1388         printk ("Page length - %d\n",capabilities->page_length);
1389         printk ("Read only - %s\n",capabilities->ro ? "Yes":"No");
1390         printk ("Supports reverse space - %s\n",capabilities->sprev ? "Yes":"No");
1391         printk ("Supports erase initiated formatting - %s\n",capabilities->efmt ? "Yes":"No");
1392         printk ("Supports QFA two Partition format - %s\n",capabilities->qfa ? "Yes":"No");
1393         printk ("Supports locking the medium - %s\n",capabilities->lock ? "Yes":"No");
1394         printk ("The volume is currently locked - %s\n",capabilities->locked ? "Yes":"No");
1395         printk ("The device defaults in the prevent state - %s\n",capabilities->prevent ? "Yes":"No");
1396         printk ("Supports ejecting the medium - %s\n",capabilities->eject ? "Yes":"No");
1397         printk ("Supports error correction - %s\n",capabilities->ecc ? "Yes":"No");
1398         printk ("Supports data compression - %s\n",capabilities->cmprs ? "Yes":"No");
1399         printk ("Supports 512 bytes block size - %s\n",capabilities->blk512 ? "Yes":"No");
1400         printk ("Supports 1024 bytes block size - %s\n",capabilities->blk1024 ? "Yes":"No");
1401         printk ("Restricted byte count for PIO transfers - %s\n",capabilities->slowb ? "Yes":"No");
1402         printk ("Maximum supported speed in KBps - %d\n",capabilities->max_speed);
1403         printk ("Continuous transfer limits in blocks - %d\n",capabilities->ctl);
1404         printk ("Current speed in KBps - %d\n",capabilities->speed);    
1405         printk ("Buffer size - %d\n",capabilities->buffer_size*512);
1406 #endif /* IDETAPE_DEBUG_LOG */
1407 }
1408 
1409 /*
1410  *      Packet Command Interface
1411  *
1412  *      The current Packet Command is available in tape->pc, and will not
1413  *      change until we finish handling it. Each packet command is associated
1414  *      with a callback function that will be called when the command is
1415  *      finished.
1416  *
1417  *      The handling will be done in three stages:
1418  *
1419  *      1.      idetape_issue_packet_command will send the packet command to the
1420  *              drive, and will set the interrupt handler to idetape_pc_intr.
1421  *
1422  *      2.      On each interrupt, idetape_pc_intr will be called. This step
1423  *              will be repeated until the device signals us that no more
1424  *              interrupts will be issued.
1425  *
1426  *      3.      ATAPI Tape media access commands have immediate status with a
1427  *              delayed process. In case of a successfull initiation of a
1428  *              media access packet command, the DSC bit will be set when the
1429  *              actual execution of the command is finished. 
1430  *              Since the tape drive will not issue an interrupt, we have to
1431  *              poll for this event. In this case, we define the request as
1432  *              "low priority request" by setting rq_status to
1433  *              IDETAPE_RQ_POSTPONED,   set a timer to poll for DSC and exit
1434  *              the driver.
1435  *
1436  *              ide.c will then give higher priority to requests which
1437  *              originate from the other device, until will change rq_status
1438  *              to RQ_ACTIVE.
1439  *
1440  *      4.      When the packet command is finished, it will be checked for errors.
1441  *
1442  *      5.      In case an error was found, we queue a request sense packet command
1443  *              in front of the request queue and retry the operation up to
1444  *              IDETAPE_MAX_PC_RETRIES times.
1445  *
1446  *      6.      In case no error was found, or we decided to give up and not
1447  *              to retry again, the callback function will be called and then
1448  *              we will handle the next request.
1449  *
1450  */
1451 
1452 void idetape_issue_packet_command  (ide_drive_t *drive,idetape_packet_command_t *pc,ide_handler_t *handler)
     /* [previous][next][first][last][top][bottom][index][help] */
1453 
1454 {
1455         idetape_tape_t *tape;
1456         idetape_bcount_reg_t bcount;
1457         idetape_ireason_reg_t ireason;
1458         int dma_ok=0;
1459 
1460         tape=&(drive->tape);
1461                 
1462 #if IDETAPE_DEBUG_BUGS
1463         if (tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1464                 printk ("ide-tape: possible ide-tape.c bug - Two request sense in serial were issued\n");
1465         }
1466 #endif /* IDETAPE_DEBUG_BUGS */
1467 
1468         if (tape->failed_pc == NULL && pc->c[0] != IDETAPE_REQUEST_SENSE_CMD)
1469                 tape->failed_pc=pc;
1470         tape->pc=pc;                                                    /* Set the current packet command */
1471 
1472         if (pc->retries > IDETAPE_MAX_PC_RETRIES || pc->abort) {
1473 
1474                 /*
1475                  *      We will "abort" retrying a packet command in case
1476                  *      a legitimate error code was received (crossing a
1477                  *      filemark, or DMA error in the end of media, for
1478                  *      example).
1479                  */
1480 
1481                 if (!pc->abort) {
1482                         printk ("ide-tape: %s: I/O error, ",drive->name);
1483                         printk ("pc = %x, key = %x, asc = %x, ascq = %x\n",pc->c[0],tape->sense_key,tape->asc,tape->ascq);
1484                         printk ("ide-tape: Maximum retries reached - Giving up\n");
1485                         pc->error=1;                                    /* Giving up */
1486                 }
1487                 tape->failed_pc=NULL;
1488 #if IDETAPE_DEBUG_BUGS
1489                 if (pc->callback==NULL)
1490                         printk ("ide-tape: ide-tape bug - Callback function not set !\n");
1491                 else
1492 #endif /* IDETAPE_DEBUG_BUGS */
1493                         (*pc->callback)(drive);
1494                 return;
1495         }
1496 
1497 #if IDETAPE_DEBUG_LOG
1498         printk ("Retry number - %d\n",pc->retries);
1499 #endif /* IDETAPE_DEBUG_LOG */
1500 
1501         pc->retries++;
1502 
1503 /*
1504  *      We no longer call ide_wait_stat to wait for the drive to be ready,
1505  *      as ide.c already does this for us in do_request.
1506  */
1507  
1508         pc->actually_transferred=0;                                     /* We haven't transferred any data yet */
1509         pc->current_position=pc->buffer;        
1510         bcount.all=pc->request_transfer;                                /* Request to transfer the entire buffer at once */
1511 
1512 #ifdef CONFIG_BLK_DEV_TRITON
1513         if (pc->dma_error) {
1514                 printk ("ide-tape: DMA disabled, reverting to PIO\n");
1515                 drive->using_dma=0;
1516                 pc->dma_error=0;
1517         }
1518         if (pc->request_transfer && pc->dma_recommended && drive->using_dma) {
1519                 dma_ok=!(HWIF(drive)->dmaproc(pc->writing ? ide_dma_write : ide_dma_read, drive));
1520         }               
1521 #endif /* CONFIG_BLK_DEV_TRITON */
1522 
1523         OUT_BYTE (dma_ok ? 1:0,IDETAPE_FEATURES_REG);                   /* Use PIO/DMA */
1524         OUT_BYTE (bcount.b.high,IDETAPE_BCOUNTH_REG);
1525         OUT_BYTE (bcount.b.low,IDETAPE_BCOUNTL_REG);
1526         OUT_BYTE (drive->select.all,IDETAPE_DRIVESEL_REG);
1527         
1528         ide_set_handler (drive,handler,WAIT_CMD);                       /* Set the interrupt routine */
1529         OUT_BYTE (WIN_PACKETCMD,IDETAPE_ATACOMMAND_REG);                /* Issue the packet command */
1530         if (ide_wait_stat (drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {      /* Wait for DRQ to be ready - Assuming Accelerated DRQ */       
1531                 /*
1532                  *      We currently only support tape drives which report
1533                  *      accelerated DRQ assertion. For this case, specs
1534                  *      allow up to 50us. We really shouldn't get here.
1535                  *
1536                  *      ??? Still needs to think what to do if we reach
1537                  *      here anyway.
1538                  */
1539                  
1540                 printk ("ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
1541                 return;
1542         }
1543         
1544         ireason.all=IN_BYTE (IDETAPE_IREASON_REG);
1545         if (!ireason.b.cod || ireason.b.io) {
1546                 printk ("ide-tape: (IO,CoD) != (0,1) while issuing a packet command\n");
1547                 ide_do_reset (drive);
1548                 return;         
1549         }
1550                 
1551         ide_output_data (drive,pc->c,12/4);                     /* Send the actual packet */
1552 #ifdef CONFIG_BLK_DEV_TRITON
1553         if ((pc->dma_in_progress=dma_ok)) {                     /* Begin DMA, if necessary */
1554                 pc->dma_error=0;
1555                 (void) (HWIF(drive)->dmaproc(ide_dma_begin, drive));
1556         }
1557 #endif /* CONFIG_BLK_DEV_TRITON */
1558 }
1559 
1560 /*
1561  *      idetape_pc_intr is the usual interrupt handler which will be called
1562  *      during a packet command. We will transfer some of the data (as
1563  *      requested by the drive) and will re-point interrupt handler to us.
1564  *      When data transfer is finished, we will act according to the
1565  *      algorithm described before idetape_issue_packet_command.
1566  *
1567  */
1568  
1569 
1570 void idetape_pc_intr (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1571 
1572 {
1573         idetape_tape_t *tape=&(drive->tape);
1574         idetape_status_reg_t status;
1575         idetape_bcount_reg_t bcount;
1576         idetape_ireason_reg_t ireason;
1577         idetape_packet_command_t *pc=tape->pc;
1578         unsigned long temp;
1579 
1580 #ifdef CONFIG_BLK_DEV_TRITON
1581         if (pc->dma_in_progress) {
1582                 if ((pc->dma_error=HWIF(drive)->dmaproc(ide_dma_status_bad, drive)))
1583                         /*
1584                          *      We will currently correct the following in
1585                          *      idetape_analyze_error.
1586                          */
1587                         pc->actually_transferred=HWIF(drive)->dmaproc(ide_dma_transferred, drive);
1588                 else
1589                         pc->actually_transferred=pc->request_transfer;
1590                 (void) (HWIF(drive)->dmaproc(ide_dma_abort, drive));    /* End DMA */
1591 #if IDETAPE_DEBUG_LOG
1592                 printk ("ide-tape: DMA finished\n");
1593 #endif /* IDETAPE_DEBUG_LOG */
1594         }
1595 #endif /* CONFIG_BLK_DEV_TRITON */
1596 
1597         status.all=IN_BYTE (IDETAPE_STATUS_REG);                /* Clear the interrupt */
1598 
1599 #if IDETAPE_DEBUG_LOG
1600         printk ("ide-tape: Reached idetape_pc_intr interrupt handler\n");
1601 #endif /* IDETAPE_DEBUG_LOG */  
1602 
1603         if (!status.b.drq) {                                    /* No more interrupts */
1604 #if IDETAPE_DEBUG_LOG
1605                 printk ("Packet command completed\n");
1606                 printk ("Total bytes transferred: %lu\n",pc->actually_transferred);
1607 #endif /* IDETAPE_DEBUG_LOG */
1608                 pc->dma_in_progress=0;
1609                                                 
1610                 sti ();
1611 
1612                 if (status.b.check || pc->dma_error) {                  /* Error detected */
1613 #if IDETAPE_DEBUG_LOG
1614         /*
1615          *      Without debugging, we only log an error if we decided to
1616          *      give up retrying.
1617          */
1618                         printk ("ide-tape: %s: I/O error, ",drive->name);
1619 #endif /* IDETAPE_DEBUG_LOG */
1620                         if (pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1621                                 printk ("ide-tape: I/O error in request sense command\n");
1622                                 ide_do_reset (drive);
1623                                 return;
1624                         }                       
1625                                                 
1626                         idetape_retry_pc (drive);                       /* Retry operation */
1627                         return;
1628                 }
1629                 pc->error=0;
1630                 if (pc->wait_for_dsc && !status.b.dsc) {                                /* Media access command */
1631                         tape->dsc_polling_frequency=IDETAPE_DSC_FAST_MEDIA_ACCESS_FREQUENCY;
1632                         idetape_postpone_request (drive);               /* Allow ide.c to handle other requests */
1633                         return;
1634                 }
1635                 if (tape->failed_pc == pc)
1636                         tape->failed_pc=NULL;
1637 #if IDETAPE_DEBUG_BUGS
1638                 if (pc->callback==NULL)                 
1639                         printk ("ide-tape: ide-tape bug - Callback function not set !\n");
1640                 else
1641 #endif /* IDETAPE_DEBUG_BUGS */
1642                         (*pc->callback)(drive);                 /* Command finished - Call the callback function */
1643                 return;
1644         }
1645 #ifdef CONFIG_BLK_DEV_TRITON
1646         if (pc->dma_in_progress) {
1647                 pc->dma_in_progress=0;
1648                 printk ("ide-tape: The tape wants to issue more interrupts in DMA mode\n");
1649                 printk ("ide-tape: DMA disabled, reverting to PIO\n");
1650                 drive->using_dma=0;
1651                 ide_do_reset (drive);
1652                 return;
1653         }
1654 #endif /* CONFIG_BLK_DEV_TRITON */
1655         bcount.b.high=IN_BYTE (IDETAPE_BCOUNTH_REG);                    /* Get the number of bytes to transfer */
1656         bcount.b.low=IN_BYTE (IDETAPE_BCOUNTL_REG);                     /* on this interrupt */
1657         ireason.all=IN_BYTE (IDETAPE_IREASON_REG);                      /* Read the interrupt reason register */
1658 
1659         if (ireason.b.cod) {
1660                 printk ("ide-tape: CoD != 0 in idetape_pc_intr\n");
1661                 ide_do_reset (drive);
1662                 return;
1663         }
1664         if (ireason.b.io != !(pc->writing)) {                   /* Hopefully, we will never get here */
1665                 printk ("ide-tape: We wanted to %s, ",pc->writing ? "Write":"Read");
1666                 printk ("but the tape wants us to %s !\n",ireason.b.io ? "Read":"Write");
1667                 ide_do_reset (drive);
1668                 return;
1669         }
1670         
1671         if (!pc->writing) {                                     /* Reading - Check that we have enough space */
1672                 temp=(unsigned long) pc->actually_transferred + bcount.all;
1673                 if ( temp > pc->request_transfer) {
1674                         printk ("ide-tape: The tape wants to send us more data than requested - ");
1675                         if (temp > pc->buffer_size) {
1676                                 printk ("Discarding data\n");
1677                                 idetape_discard_data (drive,bcount.all);
1678                                 ide_set_handler (drive,&idetape_pc_intr,WAIT_CMD);
1679                                 return;
1680                         }
1681                         else
1682                                 printk ("Allowing transfer\n");
1683                 }
1684         }
1685 #if IDETAPE_DEBUG_BUGS  
1686         if (bcount.all && !pc->buffer) {        
1687                 printk ("ide-tape: ide-tape.c bug - Buffer not set in idetape_pc_intr. Discarding data.\n");
1688                 
1689                 if (!pc->writing) {
1690                         printk ("ide-tape: Discarding data\n");
1691                         idetape_discard_data (drive,bcount.all);
1692                         ide_set_handler (drive,&idetape_pc_intr,WAIT_CMD);
1693                         return;
1694                 }
1695                 else {  /* ??? */
1696                 }
1697         }
1698 #endif /* IDETAPE_DEBUG_BUGS */
1699         if (pc->writing)
1700                 idetape_output_data (drive,pc->current_position,bcount.all);    /* Write the current buffer */
1701         else
1702                 idetape_input_data (drive,pc->current_position,bcount.all);     /* Read the current buffer */
1703 #if IDETAPE_DEBUG_LOG
1704         printk ("ide-tape: %s %d bytes\n",pc->writing ? "Wrote":"Received",bcount.all);
1705 #endif /* IDETAPE_DEBUG_LOG */
1706         pc->actually_transferred+=bcount.all;                                   /* Update the current position */
1707         pc->current_position+=bcount.all;
1708 
1709         ide_set_handler (drive,&idetape_pc_intr,WAIT_CMD);              /* And set the interrupt handler again */
1710 }
1711 
1712 /*
1713  *      idetape_postpone_request postpones the current request so that
1714  *      ide.c will be able to service requests from another device on
1715  *      the same hwgroup while we are polling for DSC.
1716  */
1717 
1718 void idetape_postpone_request (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1719 
1720 {
1721         idetape_tape_t *tape=&(drive->tape);
1722         struct request *rq;
1723         idetape_status_reg_t status;
1724         
1725 #if IDETAPE_DEBUG_LOG
1726         printk ("Reached idetape_postpone_request\n");
1727 #endif /* IDETAPE_DEBUG_LOG */
1728 #if IDETAPE_DEBUG_BUGS
1729         if (tape->postponed_rq != NULL)
1730                 printk ("ide-tape.c bug - postponed_rq not NULL in idetape_postpone_request\n");
1731 #endif /* IDETAPE_DEBUG_BUGS */
1732 
1733         tape->dsc_timer.expires=jiffies + tape->dsc_polling_frequency;  /* Set timer to poll for */
1734         tape->dsc_timeout=jiffies+IDETAPE_DSC_TIMEOUT;                  /* actual completion */
1735         tape->dsc_timer.data=(unsigned long) drive;
1736         tape->dsc_timer.function=&idetape_poll_for_dsc;
1737         init_timer (&(tape->dsc_timer));
1738 
1739         /*
1740          * Remove current request from the request queue:
1741          */
1742 
1743         tape->postponed_rq = rq = HWGROUP(drive)->rq;
1744         rq->rq_status = IDETAPE_RQ_POSTPONED;   
1745         blk_dev[MAJOR(rq->rq_dev)].current_request = rq->next;
1746         HWGROUP(drive)->rq = NULL;
1747 
1748         /*
1749          *      Check the status again - Maybe we can save one polling period.
1750          */
1751          
1752         status.all=IN_BYTE (IDETAPE_STATUS_REG);
1753         tape->last_status=status.all;
1754         tape->request_status=1; 
1755         
1756         tape->dsc_polling_start=jiffies;
1757         add_timer(&(tape->dsc_timer));          /* Activate the polling timer */
1758 }
1759 
1760 /*
1761  *      idetape_poll_for_dsc_direct is called from idetape_poll_for_dsc
1762  *      to handle the case in which we can safely communicate with the tape
1763  *      (since no other request for this hwgroup is active).
1764  */
1765  
1766 void idetape_poll_for_dsc_direct (unsigned long data)
     /* [previous][next][first][last][top][bottom][index][help] */
1767 
1768 {
1769         ide_drive_t *drive=(ide_drive_t *) data;
1770         idetape_tape_t *tape=&(drive->tape);
1771         idetape_status_reg_t status;
1772 
1773 #if IDETAPE_DEBUG_LOG
1774         printk ("%s: idetape_poll_for_dsc_direct called\n",drive->name);
1775 #endif /* IDETAPE_DEBUG_LOG */  
1776 
1777         OUT_BYTE(drive->select.all,IDE_SELECT_REG);
1778         status.all=IN_BYTE (IDETAPE_STATUS_REG);
1779         
1780         if (status.b.dsc) {                                     /* DSC received */
1781                 tape->dsc_received=1;
1782                 del_timer (&(tape->dsc_timer));                 /* Stop polling and put back the postponed */
1783                 idetape_put_back_postponed_request (drive);     /* request in the request queue */
1784                 return;
1785         }
1786 
1787         if (jiffies > tape->dsc_timeout)        { /* Timeout */
1788                 tape->dsc_received=0;
1789                 del_timer (&(tape->dsc_timer));
1790                 /* ??? */
1791                 idetape_put_back_postponed_request (drive);
1792                 return;
1793         }
1794 
1795         /* Poll again */
1796         
1797         if (jiffies - tape->dsc_polling_start > IDETAPE_FAST_SLOW_THRESHOLD)
1798                 tape->dsc_timer.expires = jiffies + IDETAPE_DSC_SLOW_MEDIA_ACCESS_FREQUENCY;
1799         else
1800                 tape->dsc_timer.expires = jiffies + tape->dsc_polling_frequency;
1801         add_timer(&(tape->dsc_timer));
1802         return;
1803 }
1804 
1805 /*
1806  *      idetape_poll_for_dsc gets invoked by a timer (which was set
1807  *      by idetape_postpone_request) to poll for the DSC bit
1808  *      in the status register.
1809  *
1810  *      We take care not to perform any tape access if the driver is
1811  *      accessing the other device. We will instead ask ide.c to sample
1812  *      the tape status register on our behalf in the next call to do_request,
1813  *      at the point in which the other device is idle, or assume that
1814  *      DSC was received even though we won't verify it (but when we assume
1815  *      that, it will usually have a solid basis).
1816  *
1817  *      The use of cli () below is a must, as we inspect and change
1818  *      the device request list while another request is active.
1819  */
1820  
1821 void idetape_poll_for_dsc (unsigned long data)
     /* [previous][next][first][last][top][bottom][index][help] */
1822 
1823 {
1824         ide_drive_t *drive=(ide_drive_t *) data;
1825         unsigned int major = HWIF(drive)->major;
1826         idetape_tape_t *tape=&(drive->tape);
1827         struct blk_dev_struct *bdev = &blk_dev[major];
1828         unsigned long flags;
1829         idetape_status_reg_t status;
1830 
1831 #if IDETAPE_DEBUG_LOG
1832         printk ("%s: idetape_poll_for_dsc called\n",drive->name);
1833 #endif /* IDETAPE_DEBUG_LOG */  
1834 
1835         save_flags (flags);cli ();
1836 
1837         /*
1838          *      Check if the other device is idle. If there are no requests,
1839          *      we can safely access the tape.
1840          */
1841 
1842         if (bdev->current_request == NULL) {
1843                 sti ();
1844                 idetape_poll_for_dsc_direct (data);
1845                 return;
1846         }
1847 
1848         /*
1849          *      If DSC was received, re-insert our postponed request into
1850          *      the request queue (using ide_next).
1851          */
1852 
1853         status.all=tape->last_status;
1854 
1855         if (status.b.dsc) {                                     /* DSC received */
1856                 tape->dsc_received=1;
1857                 idetape_put_back_postponed_request (drive);
1858                 del_timer (&(tape->dsc_timer));
1859                 restore_flags (flags);
1860                 return;
1861         }
1862 
1863         /*
1864          *      At this point, DSC may have been received, but we can't
1865          *      check it. We now have two options:
1866          *
1867          *              1.      The "simple" method - We can continue polling
1868          *                      until we know the value of DSC.
1869          *
1870          *      but we also have a more clever option :-)
1871          *
1872          *              2.      We can sometimes more or less anticipate in
1873          *                      advance how much time it will take for
1874          *                      the tape to perform the request. This is the
1875          *                      place to take advantage of this !
1876          *
1877          *                      We can assume that DSC was received, put
1878          *                      back our request, and hope that we will have
1879          *                      a "cache hit". This will only work when
1880          *                      we haven't initiated the packet command yet,
1881          *                      but this is the common read/write case. As
1882          *                      for the slower media access commands, fallback
1883          *                      to method 1 above.
1884          *
1885          *      When using method 2, we can also take advantage of the
1886          *      knowledge of the tape's internal buffer size - We can
1887          *      precalculate the time it will take for the tape to complete
1888          *      servicing not only one request, but rather, say, 50% of its
1889          *      internal buffer. The polling period will then be much larger,
1890          *      decreasing our load on Linux, and we will also call
1891          *      idetape_postpone_request less often, as there will usually
1892          *      be more room in the internal tape buffer while we are in
1893          *      idetape_do_request.
1894          *
1895          *      For this method to work well, the ongoing request of the
1896          *      other device should be serviced by the time the tape is
1897          *      still working on its remaining 50% internal buffer. This
1898          *      will usually happen when the other device is much faster
1899          *      than the tape.
1900          */
1901 
1902 #if IDETAPE_ANTICIPATE_READ_WRITE_DSC
1903 
1904         /*
1905          *      Method 2.
1906          *
1907          *      There is a high chance that DSC was received, even though
1908          *      we couldn't verify it. Let's hope that it's a "cache hit"
1909          *      rather than a "cache miss". Someday I will probably add a
1910          *      feedback loop around the number of "cache hits" which will
1911          *      fine-tune the polling period.
1912          */
1913          
1914         if (tape->postponed_rq->cmd != IDETAPE_PACKET_COMMAND_REQUEST_TYPE1) {
1915 
1916                 /*
1917                  *      We can use this method only when the packet command
1918                  *      was still not initiated.
1919                  */
1920                  
1921                 idetape_put_back_postponed_request (drive);
1922                 del_timer (&(tape->dsc_timer));
1923                 restore_flags (flags);
1924                 return;
1925         }
1926 #endif /* IDETAPE_ANTICIPATE_READ_WRITE_DSC */
1927 
1928         /*
1929          *      Fallback to method 1.
1930          */
1931                 
1932         if (bdev->current_request->next == NULL) {
1933                 /*
1934                  *      There will not be another request after the currently
1935                  *      ongoing request, so ide.c won't be able to sample
1936                  *      the status register on our behalf in do_request.
1937                  *
1938                  *      In case we are waiting for DSC before the packet
1939                  *      command was initiated, we will put back our postponed
1940                  *      request and have another look at the status register
1941                  *      in idetape_do_request, as done in method 2 above.
1942                  *
1943                  *      In case we already initiated the command, we can't
1944                  *      put it back, but it is anyway a slow media access
1945                  *      command. We will just give up and poll again until
1946                  *      we are lucky.
1947                  */
1948 
1949                 if (tape->postponed_rq->cmd == IDETAPE_PACKET_COMMAND_REQUEST_TYPE1) {
1950 
1951                         /*
1952                          *      Media access command - Poll again.
1953                          *
1954                          *      We set tape->request_status to 1, just in case
1955                          *      other requests are added while we are waiting.
1956                          */
1957                          
1958                         tape->request_status=1;
1959                         restore_flags (flags);
1960                         tape->dsc_timer.expires = jiffies + tape->dsc_polling_frequency;
1961                         add_timer(&(tape->dsc_timer));
1962                         return;
1963                 }
1964                 
1965                 /*
1966                  *      The packet command hasn't been sent to the tape yet -
1967                  *      We can safely put back the request and have another
1968                  *      look at the status register in idetape_do_request.
1969                  */
1970 
1971                 idetape_put_back_postponed_request (drive);
1972                 del_timer (&(tape->dsc_timer));
1973                 restore_flags (flags);
1974                 return;
1975         }
1976 
1977         /*
1978          *      There will be another request after the current request.
1979          *
1980          *      Request ide.c to sample for us the tape's status register
1981          *      before the next request.
1982          */
1983 
1984         tape->request_status=1;
1985         restore_flags (flags);
1986 
1987         if (jiffies > tape->dsc_timeout)        {               /* Timeout */
1988                 tape->dsc_received=0;
1989                 /* ??? */
1990                 idetape_put_back_postponed_request (drive);
1991                 del_timer (&(tape->dsc_timer));
1992                 restore_flags (flags);
1993                 return;
1994         }
1995 
1996         /* Poll again */
1997         
1998         if (jiffies - tape->dsc_polling_start > IDETAPE_FAST_SLOW_THRESHOLD)
1999                 tape->dsc_timer.expires = jiffies + IDETAPE_DSC_SLOW_MEDIA_ACCESS_FREQUENCY;
2000         else
2001                 tape->dsc_timer.expires = jiffies + tape->dsc_polling_frequency;
2002         add_timer(&(tape->dsc_timer));
2003         return;
2004 }
2005 
2006 /*
2007  *      idetape_put_back_postponed_request gets called when we decided to
2008  *      stop polling for DSC and continue servicing our postponed request.
2009  */
2010 
2011 void idetape_put_back_postponed_request (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2012 
2013 {
2014         idetape_tape_t *tape = &(drive->tape);
2015 
2016 #if IDETAPE_DEBUG_LOG
2017         printk ("ide-tape: Putting back postponed request\n");
2018 #endif /* IDETAPE_DEBUG_LOG */
2019 #if IDETAPE_DEBUG_BUGS
2020         if (tape->postponed_rq == NULL) {
2021                 printk ("tape->postponed_rq is NULL in put_back_postponed_request\n");
2022                 return;
2023         }
2024 #endif /* IDETAPE_DEBUG_BUGS */
2025         (void) ide_do_drive_cmd (drive, tape->postponed_rq, ide_next);
2026 
2027         /*
2028          *      Note that the procedure done here is differnet than the method
2029          *      we are using in idetape_queue_pc_head - There we are putting
2030          *      request(s) before our currently called request.
2031          *
2032          *      Here, on the other hand, HWGROUP(drive)->rq is not our
2033          *      request but rather a request to another device. Therefore,
2034          *      we will let it finish and only then service our postponed
2035          *      request --> We don't touch HWGROUP(drive)->rq.
2036          */
2037 }
2038 
2039 void idetape_media_access_finished (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2040 
2041 {
2042         idetape_tape_t *tape=&(drive->tape);
2043         idetape_status_reg_t status;
2044         idetape_packet_command_t *pc;
2045 
2046         pc=tape->pc;
2047         
2048         status.all=IN_BYTE (IDETAPE_STATUS_REG);
2049 
2050         if (tape->dsc_received) {
2051 #if IDETAPE_DEBUG_LOG
2052                 printk ("DSC received\n");
2053 #endif /* IDETAPE_DEBUG_LOG */
2054                 if (status.b.check) {                                   /* Error detected */
2055                         printk ("ide-tape: %s: I/O error, ",drive->name);
2056                         idetape_retry_pc (drive);                       /* Retry operation */
2057                         return;
2058                 }
2059                 pc->error=0;
2060                 if (tape->failed_pc == pc)
2061                         tape->failed_pc=NULL;
2062 #if IDETAPE_DEBUG_BUGS
2063                 if (pc->callback==NULL)
2064                         printk ("ide-tape: ide-tape bug - Callback function not set !\n");
2065                 else
2066 #endif /* IDETAPE_DEBUG_BUGS */
2067                         (*pc->callback)(drive);
2068 
2069                 return;
2070         }
2071         else {
2072                 printk ("ide-tape: %s: DSC timeout.\n",drive->name);
2073                 /* ??? */
2074                 pc->error=1;
2075                 tape->failed_pc=NULL;
2076 #if IDETAPE_DEBUG_BUGS
2077                 if (pc->callback==NULL)
2078                         printk ("ide-tape: ide-tape bug - Callback function not set !\n");
2079                 else
2080 #endif /* IDETAPE_DEBUG_BUGS */
2081                         (*pc->callback)(drive);
2082                 return;
2083         }
2084 }
2085 
2086 
2087 /*
2088  *      idetape_retry_pc is called when an error was detected during the
2089  *      last packet command. We queue a request sense packet command in
2090  *      the head of the request list.
2091  */
2092  
2093 void idetape_retry_pc (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2094 
2095 {
2096         idetape_packet_command_t *pc;
2097         struct request *new_rq;
2098 
2099         idetape_error_reg_t error;
2100         error.all=IN_BYTE (IDETAPE_ERROR_REG);
2101         pc=idetape_next_pc_storage (drive);
2102         new_rq=idetape_next_rq_storage (drive);
2103         idetape_create_request_sense_cmd (pc); 
2104         pc->buffer=pc->temp_buffer;
2105         pc->buffer_size=IDETAPE_TEMP_BUFFER_SIZE;
2106         pc->current_position=pc->temp_buffer;
2107         idetape_queue_pc_head (drive,pc,new_rq);
2108 }
2109 
2110 /*
2111  *      General packet command callback function.
2112  */
2113  
2114 void idetape_pc_callback (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2115 
2116 {
2117         idetape_tape_t *tape;
2118         struct request *rq;
2119         
2120         tape=&(drive->tape);
2121         rq=HWGROUP(drive)->rq;
2122         
2123 #if IDETAPE_DEBUG_LOG
2124         printk ("ide-tape: Reached idetape_pc_callback\n");
2125 #endif /* IDETAPE_DEBUG_LOG */
2126         if (!tape->pc->error) {
2127 #if IDETAPE_DEBUG_LOG
2128                 printk ("Request completed\n");
2129 #endif /* IDETAPE_DEBUG_LOG */
2130                 idetape_end_request (1,HWGROUP (drive));
2131         }
2132         else {
2133                 idetape_end_request (0,HWGROUP (drive));
2134         }
2135         return;
2136 }
2137 
2138 
2139 void idetape_read_callback (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2140 
2141 {
2142         idetape_tape_t *tape=&(drive->tape);
2143         struct request *rq=HWGROUP(drive)->rq;
2144         int blocks_read=tape->pc->actually_transferred/tape->tape_block_size;
2145 
2146 #if IDETAPE_DEBUG_LOG   
2147         printk ("ide-tape: Reached idetape_read_callback\n");
2148 #endif /* IDETAPE_DEBUG_LOG */
2149 
2150         tape->block_address+=blocks_read;
2151         rq->current_nr_sectors-=blocks_read;    
2152 
2153         if (!tape->pc->error)
2154                 idetape_end_request (1,HWGROUP (drive));
2155         else {
2156                 rq->errors=tape->pc->error;
2157                 switch (rq->errors) {
2158                         case IDETAPE_RQ_ERROR_FILEMARK:
2159                         case IDETAPE_RQ_ERROR_EOD:
2160                                 break;
2161                 }
2162                 idetape_end_request (0,HWGROUP (drive));
2163         }
2164         return;
2165 }
2166 
2167 void idetape_write_callback (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2168 
2169 {
2170         idetape_tape_t *tape=&(drive->tape);
2171         struct request *rq=HWGROUP(drive)->rq;
2172         int blocks_written=tape->pc->actually_transferred/tape->tape_block_size;
2173                 
2174 #if IDETAPE_DEBUG_LOG   
2175         printk ("ide-tape: Reached idetape_write_callback\n");
2176 #endif /* IDETAPE_DEBUG_LOG */
2177 
2178         tape->block_address+=blocks_written;
2179         rq->current_nr_sectors-=blocks_written;
2180 
2181         if (!tape->pc->error)
2182                 idetape_end_request (1,HWGROUP (drive));
2183         else {
2184                 rq->errors=tape->pc->error;
2185                 idetape_end_request (0,HWGROUP (drive));
2186         }
2187         return;
2188 }
2189 
2190 void idetape_inquiry_callback (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2191 
2192 {
2193         idetape_tape_t *tape;
2194         
2195         tape=&(drive->tape);
2196         
2197         idetape_display_inquiry_result (tape->pc->buffer);
2198         idetape_pc_callback (drive);
2199         return;
2200 }
2201 
2202 /*
2203  *      idetape_input_data is called to read data from the tape's data
2204  *      register. We basically let ide_input_data do the job, but we also
2205  *      take care about the remaining bytes which can not be transferred
2206  *      in 32-bit data transfers.
2207  */
2208  
2209 void idetape_input_data (ide_drive_t *drive,void *buffer, unsigned long bcount)
     /* [previous][next][first][last][top][bottom][index][help] */
2210 
2211 {
2212         unsigned long wcount;
2213         
2214         wcount=bcount >> 2;
2215         bcount -= 4*wcount;
2216         
2217         if (wcount)
2218                 ide_input_data (drive,buffer,wcount);
2219         
2220         if (bcount) {
2221                 ((byte *)buffer) += 4*wcount;
2222                 insb (IDETAPE_DATA_REG,buffer,bcount);
2223         }
2224 }
2225 
2226 /*
2227  *      idetape_output_data is used to write data to the tape.
2228  */
2229  
2230 void idetape_output_data (ide_drive_t *drive,void *buffer, unsigned long bcount)
     /* [previous][next][first][last][top][bottom][index][help] */
2231 
2232 {
2233         unsigned long wcount;
2234         
2235         wcount=bcount >> 2;
2236         bcount -= 4*wcount;
2237         
2238         if (wcount)
2239                 ide_output_data (drive,buffer,wcount);
2240         
2241         if (bcount) {
2242                 ((byte *)buffer) += 4*wcount;
2243                 outsb (IDETAPE_DATA_REG,buffer,bcount);
2244         }
2245 }
2246 
2247 /*
2248  *      Too bad. The drive wants to send us data which we are not ready to accept.
2249  *      Just throw it away.
2250  */
2251  
2252 void idetape_discard_data (ide_drive_t *drive, unsigned long bcount)
     /* [previous][next][first][last][top][bottom][index][help] */
2253 
2254 {
2255         unsigned long i;
2256         
2257         for (i=0;i<bcount;i++)
2258                 IN_BYTE (IDETAPE_DATA_REG);
2259 }
2260 
2261 /*
2262  *      Issue an INQUIRY packet command.
2263  */
2264  
2265 void idetape_create_inquiry_cmd (idetape_packet_command_t *pc)
     /* [previous][next][first][last][top][bottom][index][help] */
2266 
2267 {
2268 #if IDETAPE_DEBUG_LOG
2269         printk ("ide-tape: Creating INQUIRY packet command\n");
2270 #endif /* IDETAPE_DEBUG_LOG */  
2271         pc->request_transfer=36;
2272         pc->callback=&idetape_inquiry_callback;
2273         pc->writing=0;
2274         
2275         idetape_zero_packet_command (pc);
2276         pc->c[0]=IDETAPE_INQUIRY_CMD;
2277         pc->c[4]=255;
2278 }
2279 
2280 /*
2281  *      Format the INQUIRY command results.
2282  */
2283  
2284 void idetape_display_inquiry_result (byte *buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
2285 
2286 {
2287         idetape_inquiry_result_t *result;
2288 
2289         result=(idetape_inquiry_result_t *) buffer;
2290         ide_fixstring (result->vendor_id,8,0);
2291         ide_fixstring (result->product_id,16,0);
2292         ide_fixstring (result->revision_level,4,0);
2293 
2294         if (result->response_format != 2) {
2295                 printk ("The INQUIRY Data Format is unknown to us !\n");
2296                 printk ("Assuming QIC-157C format.\n");
2297         }
2298 
2299 #if IDETAPE_DEBUG_LOG
2300         printk ("Dumping INQUIRY command results:\n");
2301         printk ("Response Data Format: %d - ",result->response_format);
2302         switch (result->response_format) {
2303                 case 2:
2304                         printk ("As specified in QIC-157 Revision C\n");
2305                         break;
2306                 default:
2307                         printk ("Unknown\n");
2308                         break;
2309         }
2310         
2311         printk ("Device Type: %x - ",result->device_type);      
2312         switch (result->device_type) {
2313                 case 0: printk ("Direct-access Device\n");break;
2314                 case 1: printk ("Streaming Tape Device\n");break;
2315                 case 2: case 3: case 4: printk ("Reserved\n");break;
2316                 case 5: printk ("CD-ROM Device\n");break;
2317                 case 6: printk ("Reserved\n");
2318                 case 7: printk ("Optical memory Device\n");break;
2319                 case 0x1f: printk ("Unknown or no Device type\n");break;
2320                 default: printk ("Reserved\n");
2321         }
2322         
2323         printk ("Removable Medium: %s",result->rmb ? "Yes\n":"No\n");
2324 
2325         printk ("ANSI Version: %d - ",result->ansi_version);
2326         switch (result->ansi_version) {
2327                 case 2:
2328                         printk ("QIC-157 Revision C\n");
2329                         break;
2330                 default:
2331                         printk ("Unknown\n");
2332                         break;
2333         }
2334 
2335         printk ("ECMA Version: ");
2336         if (result->ecma_version)
2337                 printk ("%d\n",result->ecma_version);
2338         else
2339                 printk ("Not supported\n");
2340 
2341         printk ("ISO Version: ");
2342         if (result->iso_version)
2343                 printk ("%d\n",result->iso_version);
2344         else
2345                 printk ("Not supported\n");
2346 
2347         printk ("Additional Length: %d\n",result->additional_length);
2348         printk ("Vendor Identification: %s\n",result->vendor_id);
2349         printk ("Product Identification: %s\n",result->product_id);
2350         printk ("Product Revision Level: %s\n",result->revision_level);
2351 #endif /* IDETAPE_DEBUG_LOG */
2352 
2353         if (result->device_type != 1)
2354                 printk ("Device type is not set to tape\n");
2355 
2356         if (!result->rmb)
2357                 printk ("The removable flag is not set\n");
2358 
2359         if (result->ansi_version != 2) {
2360                 printk ("The Ansi Version is unknown to us !\n");
2361                 printk ("Assuming compliance with QIC-157C specification.\n");
2362         }
2363 }
2364 
2365 void idetape_create_request_sense_cmd (idetape_packet_command_t *pc)
     /* [previous][next][first][last][top][bottom][index][help] */
2366 
2367 {
2368 #if IDETAPE_DEBUG_LOG
2369         printk ("ide-tape: Creating REQUEST SENSE packet command\n");
2370 #endif /* IDETAPE_DEBUG_LOG */  
2371         pc->request_transfer=18;
2372         pc->callback=&idetape_request_sense_callback;
2373         pc->writing=0;
2374         
2375         idetape_zero_packet_command (pc);       
2376         pc->c[0]=IDETAPE_REQUEST_SENSE_CMD;
2377         pc->c[4]=255;
2378 }
2379 
2380 void idetape_request_sense_callback (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2381 
2382 {
2383         idetape_tape_t *tape=&(drive->tape);
2384 
2385 #if IDETAPE_DEBUG_LOG
2386         printk ("ide-tape: Reached idetape_request_sense_callback\n");
2387 #endif /* IDETAPE_DEBUG_LOG */
2388         if (!tape->pc->error) {
2389 #if IDETAPE_DEBUG_LOG
2390                 printk ("Request completed\n");
2391 #endif /* IDETAPE_DEBUG_LOG */
2392                 idetape_analyze_error (drive,(idetape_request_sense_result_t *) tape->pc->buffer);
2393                 idetape_end_request (1,HWGROUP (drive));
2394         }
2395         else {
2396                 printk ("Error in REQUEST SENSE itself - Aborting request!\n");
2397                 idetape_end_request (0,HWGROUP (drive));
2398         }
2399         return;
2400 }
2401 
2402 /*
2403  *      idetape_analyze_error is called on each failed packet command retry
2404  *      to analyze the request sense. We currently do not utilize this
2405  *      information.
2406  */
2407  
2408 void idetape_analyze_error (ide_drive_t *drive,idetape_request_sense_result_t *result)
     /* [previous][next][first][last][top][bottom][index][help] */
2409 
2410 {
2411         idetape_tape_t *tape=&(drive->tape);
2412         idetape_packet_command_t *pc=tape->failed_pc;
2413                 
2414         tape->sense_key=result->sense_key;
2415         tape->asc=result->asc;
2416         tape->ascq=result->ascq;
2417         
2418 #if IDETAPE_DEBUG_LOG   
2419         /*
2420          *      Without debugging, we only log an error if we decided to
2421          *      give up retrying.
2422          */
2423         printk ("ide-tape: pc = %x, sense key = %x, asc = %x, ascq = %x\n",pc->c[0],result->sense_key,result->asc,result->ascq);
2424 #endif /* IDETAPE_DEBUG_LOG */
2425 
2426         if (pc->c[0] == IDETAPE_READ_CMD) {
2427                 if (result->filemark) {
2428                         pc->error=IDETAPE_RQ_ERROR_FILEMARK;
2429                         pc->abort=1;
2430                 }
2431         }
2432 
2433         if (pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD) {
2434                 if (result->sense_key == 8) {
2435                         pc->error=IDETAPE_RQ_ERROR_EOD;
2436                         pc->abort=1;
2437                 }
2438         }
2439 
2440 #if 1
2441 #ifdef CONFIG_BLK_DEV_TRITON
2442 
2443         /*
2444          *      Correct pc->actually_transferred by asking the tape.
2445          */
2446 
2447         if (pc->dma_error && pc->abort) {
2448                 unsigned long *long_ptr=(unsigned long *) &(result->information1);
2449                 pc->actually_transferred=pc->request_transfer-tape->tape_block_size*idetape_swap_long (*long_ptr);
2450         }               
2451 #endif /* CONFIG_BLK_DEV_TRITON */
2452 #endif
2453 }
2454 
2455 void idetape_create_test_unit_ready_cmd (idetape_packet_command_t *pc)
     /* [previous][next][first][last][top][bottom][index][help] */
2456 
2457 {
2458 #if IDETAPE_DEBUG_LOG
2459         printk ("ide-tape: Creating TEST UNIT READY packet command\n");
2460 #endif /* IDETAPE_DEBUG_LOG */  
2461         pc->request_transfer=0;
2462         pc->buffer=NULL;
2463         pc->current_position=NULL;
2464         pc->callback=&idetape_pc_callback;
2465         pc->writing=0;
2466         
2467         idetape_zero_packet_command (pc);       
2468         pc->c[0]=IDETAPE_TEST_UNIT_READY_CMD;
2469 }
2470 
2471 void idetape_create_locate_cmd (idetape_packet_command_t *pc,unsigned long block,byte partition)
     /* [previous][next][first][last][top][bottom][index][help] */
2472 
2473 {
2474         unsigned long *ptr;
2475 
2476 #if IDETAPE_DEBUG_LOG
2477         printk ("ide-tape: Creating LOCATE packet command\n");
2478 #endif /* IDETAPE_DEBUG_LOG */
2479         pc->request_transfer=0;
2480         pc->buffer=NULL;
2481         pc->current_position=NULL;
2482         pc->buffer_size=0;
2483         pc->wait_for_dsc=1;
2484         pc->callback=&idetape_pc_callback;
2485         pc->writing=0;
2486                 
2487         idetape_zero_packet_command (pc);
2488         pc->c [0]=IDETAPE_LOCATE_CMD;
2489         pc->c [1]=2;
2490         ptr=(unsigned long *) &(pc->c[3]);
2491         *ptr=idetape_swap_long (block);
2492         pc->c[8]=partition;
2493 }
2494 
2495 void idetape_create_rewind_cmd (idetape_packet_command_t *pc)
     /* [previous][next][first][last][top][bottom][index][help] */
2496 
2497 {
2498 #if IDETAPE_DEBUG_LOG
2499         printk ("ide-tape: Creating REWIND packet command\n");
2500 #endif /* IDETAPE_DEBUG_LOG */
2501         pc->request_transfer=0;
2502         pc->buffer=NULL;
2503         pc->current_position=NULL;
2504         pc->buffer_size=0;
2505         pc->wait_for_dsc=1;
2506         pc->callback=&idetape_pc_callback;
2507         pc->writing=0;
2508                 
2509         idetape_zero_packet_command (pc);
2510         pc->c [0]=IDETAPE_REWIND_CMD;
2511 }
2512 
2513 /*
2514  *      A mode sense command is used to "sense" tape parameters.
2515  */
2516 
2517 void idetape_create_mode_sense_cmd (idetape_packet_command_t *pc,byte page_code)
     /* [previous][next][first][last][top][bottom][index][help] */
2518 
2519 {
2520 #if IDETAPE_DEBUG_LOG
2521         printk ("ide-tape: Creating MODE SENSE packet command - Page %d\n",page_code);
2522 #endif /* IDETAPE_DEBUG_LOG */
2523 
2524         pc->wait_for_dsc=0;
2525         pc->callback=&idetape_pc_callback;
2526         pc->writing=0;
2527 
2528         switch (page_code) {
2529                 case IDETAPE_CAPABILITIES_PAGE:
2530                         pc->request_transfer=24;
2531         }
2532                 
2533         idetape_zero_packet_command (pc);
2534         pc->c [0]=IDETAPE_MODE_SENSE_CMD;
2535         pc->c [1]=8;                            /* DBD = 1 - Don't return block descriptors for now */
2536         pc->c [2]=page_code;
2537         pc->c [3]=255;                          /* Don't limit the returned information */
2538         pc->c [4]=255;                          /* (We will just discard data in that case) */
2539 }
2540 
2541 /*
2542  *      idetape_create_write_filemark_cmd will:
2543  *
2544  *              1.      Write a filemark if write_filemark=1.
2545  *              2.      Flush the device buffers without writing a filemark
2546  *                      if write_filemark=0.
2547  *
2548  */
2549  
2550 void idetape_create_write_filemark_cmd (idetape_packet_command_t *pc,int write_filemark)
     /* [previous][next][first][last][top][bottom][index][help] */
2551 
2552 {
2553 #if IDETAPE_DEBUG_LOG
2554         printk ("Creating WRITE FILEMARK packet command\n");
2555         if (!write_filemark)
2556                 printk ("which will only flush buffered data\n");
2557 #endif /* IDETAPE_DEBUG_LOG */
2558         pc->request_transfer=0;
2559         pc->buffer=NULL;
2560         pc->current_position=NULL;
2561         pc->buffer_size=0;
2562         pc->wait_for_dsc=1;
2563         pc->callback=&idetape_pc_callback;
2564         pc->writing=0;
2565                 
2566         idetape_zero_packet_command (pc);
2567         pc->c [0]=IDETAPE_WRITE_FILEMARK_CMD;
2568         if (write_filemark)
2569                 pc->c [4]=1;
2570 }
2571 
2572 void idetape_create_load_unload_cmd (idetape_packet_command_t *pc,int cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
2573 
2574 {
2575 #if IDETAPE_DEBUG_LOG
2576         printk ("Creating LOAD UNLOAD packet command, cmd=%d\n",cmd);
2577 #endif /* IDETAPE_DEBUG_LOG */
2578         pc->request_transfer=0;
2579         pc->buffer=NULL;
2580         pc->current_position=NULL;
2581         pc->buffer_size=0;
2582         pc->wait_for_dsc=1;
2583         pc->callback=&idetape_pc_callback;
2584         pc->writing=0;
2585                 
2586         idetape_zero_packet_command (pc);
2587         pc->c [0]=IDETAPE_LOAD_UNLOAD_CMD;
2588         pc->c [4]=cmd;
2589 }
2590 
2591 void idetape_create_erase_cmd (idetape_packet_command_t *pc)
     /* [previous][next][first][last][top][bottom][index][help] */
2592 
2593 {
2594 
2595 #if IDETAPE_DEBUG_LOG
2596         printk ("Creating ERASE command\n");
2597 #endif /* IDETAPE_DEBUG_LOG */
2598 
2599         pc->request_transfer=0;
2600         pc->buffer=NULL;
2601         pc->current_position=NULL;
2602         pc->buffer_size=0;
2603         pc->wait_for_dsc=1;
2604         pc->callback=&idetape_pc_callback;
2605         pc->writing=0;
2606                 
2607         idetape_zero_packet_command (pc);
2608         pc->c [0]=IDETAPE_ERASE_CMD;
2609         pc->c [1]=1;
2610 }
2611 
2612 void idetape_create_read_cmd (idetape_packet_command_t *pc,unsigned long length)
     /* [previous][next][first][last][top][bottom][index][help] */
2613 
2614 {
2615         union convert {
2616                 unsigned all    :32;
2617                 struct {
2618                         unsigned b1     :8;
2619                         unsigned b2     :8;
2620                         unsigned b3     :8;
2621                         unsigned b4     :8;
2622                 } b;
2623         } original;
2624         
2625 #if IDETAPE_DEBUG_LOG
2626         printk ("ide-tape: Creating READ packet command\n");
2627 #endif /* IDETAPE_DEBUG_LOG */
2628 
2629         original.all=length;
2630 
2631         pc->wait_for_dsc=0;
2632         pc->callback=&idetape_read_callback;
2633         pc->writing=0;
2634 
2635         idetape_zero_packet_command (pc);
2636 
2637         pc->c [0]=IDETAPE_READ_CMD;
2638         pc->c [1]=1;
2639         pc->c [4]=original.b.b1;
2640         pc->c [3]=original.b.b2;
2641         pc->c [2]=original.b.b3;
2642 
2643         if (length)
2644                 pc->dma_recommended=1;
2645 
2646         return;
2647 }
2648 
2649 void idetape_create_space_cmd (idetape_packet_command_t *pc,long count,byte cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
2650 
2651 {
2652         union convert {
2653                 unsigned all    :32;
2654                 struct {
2655                         unsigned b1     :8;
2656                         unsigned b2     :8;
2657                         unsigned b3     :8;
2658                         unsigned b4     :8;
2659                 } b;
2660         } original;
2661         
2662 #if IDETAPE_DEBUG_LOG
2663         printk ("ide-tape: Creating SPACE packet command\n");
2664 #endif /* IDETAPE_DEBUG_LOG */
2665 
2666         original.all=count;
2667 
2668         pc->request_transfer=0;
2669         pc->buffer=NULL;
2670         pc->current_position=NULL;
2671         pc->buffer_size=0;
2672         pc->wait_for_dsc=1;
2673         pc->callback=&idetape_pc_callback;
2674         pc->writing=0;
2675 
2676         idetape_zero_packet_command (pc);
2677         pc->c [0]=IDETAPE_SPACE_CMD;
2678         pc->c [1]=cmd;
2679         pc->c [4]=original.b.b1;
2680         pc->c [3]=original.b.b2;
2681         pc->c [2]=original.b.b3;
2682 
2683         return;
2684 }
2685 
2686 void idetape_create_write_cmd (idetape_packet_command_t *pc,unsigned long length)
     /* [previous][next][first][last][top][bottom][index][help] */
2687 
2688 {
2689         union convert {
2690                 unsigned all    :32;
2691                 struct {
2692                         unsigned b1     :8;
2693                         unsigned b2     :8;
2694                         unsigned b3     :8;
2695                         unsigned b4     :8;
2696                 } b;
2697         } original;
2698         
2699 #if IDETAPE_DEBUG_LOG
2700         printk ("ide-tape: Creating WRITE packet command\n");
2701 #endif /* IDETAPE_DEBUG_LOG */
2702 
2703         original.all=length;
2704 
2705         pc->wait_for_dsc=0;
2706         pc->callback=&idetape_write_callback;
2707         pc->writing=1;
2708 
2709         idetape_zero_packet_command (pc);
2710 
2711         pc->c [0]=IDETAPE_WRITE_CMD;
2712         pc->c [1]=1;
2713         pc->c [4]=original.b.b1;
2714         pc->c [3]=original.b.b2;
2715         pc->c [2]=original.b.b3;
2716 
2717         if (length)
2718                 pc->dma_recommended=1;
2719 
2720         return;
2721 }
2722 
2723 void idetape_create_read_position_cmd (idetape_packet_command_t *pc)
     /* [previous][next][first][last][top][bottom][index][help] */
2724 
2725 {
2726 #if IDETAPE_DEBUG_LOG
2727         printk ("ide-tape: Creating READ POSITION packet command\n");
2728 #endif /* IDETAPE_DEBUG_LOG */
2729 
2730         pc->request_transfer=20;
2731         pc->wait_for_dsc=0;
2732         pc->callback=&idetape_read_position_callback;
2733         pc->writing=0;
2734 
2735         idetape_zero_packet_command (pc);
2736         pc->c [0]=IDETAPE_READ_POSITION_CMD;
2737         pc->c [1]=0;
2738 }
2739 
2740 void idetape_read_position_callback (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2741 
2742 {
2743         idetape_tape_t *tape;
2744         struct request *rq;
2745         idetape_read_position_result_t *result;
2746         
2747         tape=&(drive->tape);
2748         
2749 #if IDETAPE_DEBUG_LOG
2750         printk ("ide-tape: Reached idetape_read_position_callback\n");
2751 #endif /* IDETAPE_DEBUG_LOG */
2752 
2753         rq=HWGROUP(drive)->rq;
2754         
2755         if (!tape->pc->error) {
2756                 result=(idetape_read_position_result_t *) tape->pc->buffer;
2757 #if IDETAPE_DEBUG_LOG
2758                 printk ("Request completed\n");
2759                 printk ("Dumping the results of the READ POSITION command\n");
2760                 printk ("BOP - %s\n",result->bop ? "Yes":"No");
2761                 printk ("EOP - %s\n",result->eop ? "Yes":"No");
2762 #endif /* IDETAPE_DEBUG_LOG */
2763                 if (result->bpu) {
2764                         printk ("ide-tape: Block location is unknown to the tape\n");
2765                         printk ("Aborting request\n");
2766                         tape->block_address_valid=0;
2767                         idetape_end_request (0,HWGROUP (drive));
2768                 }
2769                 else {
2770 #if IDETAPE_DEBUG_LOG
2771                         printk ("Block Location - %lu\n",idetape_swap_long (result->first_block));
2772 #endif /* IDETAPE_DEBUG_LOG */
2773                         tape->block_address=idetape_swap_long (result->first_block);
2774                         tape->block_address_valid=1;
2775                         idetape_end_request (1,HWGROUP (drive));
2776                 }
2777         }
2778         else {
2779                 printk ("Aborting request\n");
2780                 idetape_end_request (0,HWGROUP (drive));
2781         }
2782         return;
2783 }
2784 
2785 /*
2786  *      Our special ide-tape ioctl's.
2787  *
2788  *      Currently there aren't any significant ioctl's.
2789  *      mtio.h compatible commands should be issued to the character device
2790  *      interface.
2791  */
2792  
2793 int idetape_blkdev_ioctl (ide_drive_t *drive, struct inode *inode, struct file *file,
     /* [previous][next][first][last][top][bottom][index][help] */
2794                         unsigned int cmd, unsigned long arg)
2795 {
2796         idetape_packet_command_t pc;
2797         
2798         pc.buffer=pc.temp_buffer;
2799         pc.buffer_size=IDETAPE_TEMP_BUFFER_SIZE;
2800         pc.current_position=pc.temp_buffer;
2801 
2802 #if IDETAPE_DEBUG_LOG   
2803         printk ("ide-tape: Reached idetape_blkdev_ioctl\n");
2804 #endif /* IDETAPE_DEBUG_LOG */
2805         switch (cmd) {
2806                 default:
2807                         return -EIO;
2808         }
2809 }
2810 
2811 /*
2812  *      Functions which handle requests.
2813  */
2814 
2815 /*
2816  *      idetape_end_request is used to end a request.
2817  */
2818 
2819 void idetape_end_request (byte uptodate, ide_hwgroup_t *hwgroup)
     /* [previous][next][first][last][top][bottom][index][help] */
2820 
2821 {
2822         ide_drive_t *drive = hwgroup->drive;
2823         struct request *rq = hwgroup->rq;
2824         idetape_tape_t *tape = &(drive->tape);
2825         unsigned int major = HWIF(drive)->major;
2826         struct blk_dev_struct *bdev = &blk_dev[major];
2827         int error;
2828 
2829 #if IDETAPE_DEBUG_LOG
2830         printk ("Reached idetape_end_request\n");
2831 #endif /* IDETAPE_DEBUG_LOG */
2832 
2833         bdev->current_request=rq;                       /* Since we may have taken it out */
2834 
2835         if (!rq->errors)                                /* In case rq->errors is already set, */
2836                 rq->errors=!uptodate;                   /* we won't change it. */
2837         error=rq->errors;
2838                 
2839         if (tape->active_data_request == rq) {          /* The request was a pipelined data transfer request */
2840 
2841                 if (rq->cmd == IDETAPE_READ_REQUEST) {
2842 #if IDETAPE_DEBUG_BUGS
2843                         if (tape->active_stage == NULL)
2844                                 printk ("ide-tape: bug: active_stage is NULL in idetape_end_request\n");
2845                         else                            
2846 #endif /* IDETAPE_DEBUG_BUGS */
2847                         idetape_copy_buffer_to_stage (tape->active_stage,tape->data_buffer);
2848                 }
2849 
2850                 tape->active_stage=NULL;
2851                 tape->active_data_request=NULL;
2852 
2853                 if (rq->cmd == IDETAPE_WRITE_REQUEST) {
2854                         if (rq->errors)
2855                                 tape->error_in_pipeline_stage=rq->errors;
2856                         idetape_remove_stage_head (drive);
2857                 }
2858                 
2859                 if (tape->next_stage == NULL) {
2860                         if (!error)
2861                                 idetape_increase_max_pipeline_stages (drive);
2862                         ide_end_drive_cmd (drive, 0, 0);
2863                         return;
2864                 }
2865 
2866                 idetape_active_next_stage (drive);
2867 
2868                 /*
2869                  *      Insert the next request into the request queue.
2870                  *
2871                  *      The choice of using ide_next or ide_end is now left
2872                  *      to the user.
2873                  */
2874                  
2875 #if IDETAPE_LOW_TAPE_PRIORITY
2876                 (void) ide_do_drive_cmd (drive,tape->active_data_request,ide_end);
2877 #else
2878                 (void) ide_do_drive_cmd (drive,tape->active_data_request,ide_next);
2879 #endif /* IDETAPE_LOW_TAPE_PRIORITY */
2880         }
2881         ide_end_drive_cmd (drive, 0, 0);
2882 }
2883 
2884 /*
2885  *      idetape_do_request is our request handling function.    
2886  */
2887 
2888 void idetape_do_request (ide_drive_t *drive, struct request *rq, unsigned long block)
     /* [previous][next][first][last][top][bottom][index][help] */
2889 
2890 {
2891         idetape_tape_t *tape=&(drive->tape);
2892         idetape_packet_command_t *pc;
2893         unsigned int major = HWIF(drive)->major;
2894         struct blk_dev_struct *bdev = &blk_dev[major];
2895         idetape_status_reg_t status;
2896 
2897 #if IDETAPE_DEBUG_LOG
2898         printk ("Current request:\n");
2899         printk ("rq_status: %d, rq_dev: %u, cmd: %d, errors: %d\n",rq->rq_status,(unsigned int) rq->rq_dev,rq->cmd,rq->errors);
2900         printk ("sector: %ld, nr_sectors: %ld, current_nr_sectors: %ld\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
2901 #endif /* IDETAPE_DEBUG_LOG */
2902 
2903         if (!IDETAPE_REQUEST_CMD (rq->cmd)) {
2904 
2905                 /*
2906                  *      We do not support buffer cache originated requests.
2907                  */
2908 
2909                 printk ("ide-tape: Unsupported command in request queue\n");
2910                 printk ("ide-tape: The block device interface should not be used for data transfers.\n");
2911                 printk ("ide-tape: Use the character device interfaces\n");
2912                 printk ("ide-tape: /dev/ht0 and /dev/nht0 instead.\n");
2913                 printk ("ide-tape: (Run linux/drivers/block/MAKEDEV.ide to create them)\n");
2914                 printk ("ide-tape: Aborting request.\n");
2915 
2916                 ide_end_request (0,HWGROUP (drive));                    /* Let the common code handle it */
2917                 return;
2918         }
2919 
2920         /*
2921          *      This is an important point. We will try to remove our request
2922          *      from the block device request queue while we service the
2923          *      request. Note that the request must be returned to
2924          *      bdev->current_request before the next call to
2925          *      ide_end_drive_cmd or ide_do_drive_cmd to conform with the
2926          *      normal behavior of the IDE driver, which leaves the active
2927          *      request in bdev->current_request during I/O.
2928          *
2929          *      This will eliminate fragmentation of disk/cdrom requests
2930          *      around a tape request, now that we are using ide_next to
2931          *      insert pending pipeline requests, since we have only one
2932          *      ide-tape.c data request in the device request queue, and
2933          *      thus once removed, ll_rw_blk.c will only see requests from
2934          *      the other device.
2935          *
2936          *      The potential fragmentation inefficiency was pointed to me
2937          *      by Mark Lord.
2938          */
2939          
2940         if (rq->next != NULL && rq->rq_dev != rq->next->rq_dev)
2941                 bdev->current_request=rq->next;
2942 
2943         /* Retry a failed packet command */
2944 
2945         if (tape->failed_pc != NULL && tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
2946                 idetape_issue_packet_command (drive,tape->failed_pc,&idetape_pc_intr);
2947                 return;
2948         }
2949 
2950         /* Check if we have a postponed request */
2951         
2952         if (tape->postponed_rq != NULL) {
2953 #if IDETAPE_DEBUG_BUGS
2954                 if (tape->postponed_rq->rq_status != RQ_ACTIVE || rq != tape->postponed_rq) {
2955                         printk ("ide-tape: ide-tape.c bug - Two DSC requests were queued\n");
2956                         idetape_end_request (0,HWGROUP (drive));
2957                         return;
2958                 }
2959 #endif /* IDETAPE_DEBUG_BUGS */
2960                 if (rq->cmd == IDETAPE_PACKET_COMMAND_REQUEST_TYPE1) {
2961         
2962                         /* Media access command */
2963                         
2964                         tape->postponed_rq = NULL;
2965                         idetape_media_access_finished (drive);
2966                         return;
2967                 }
2968                 
2969                 /*
2970                  * Read / Write command - DSC polling was done before the
2971                  * actual command - Continue normally so that the command
2972                  * will be performed below.
2973                  */
2974                  
2975                  tape->postponed_rq = NULL;
2976         }       
2977 
2978         status.all=IN_BYTE (IDETAPE_STATUS_REG);
2979 
2980         /*
2981          *      After a software reset, the status register is locked. We
2982          *      will ignore the DSC value for our very first packet command,
2983          *      which will restore DSC operation.
2984          */
2985 
2986         if (tape->reset_issued) {
2987                 status.b.dsc=1;
2988                 tape->reset_issued=0;
2989         }
2990         
2991         switch (rq->cmd) {
2992                 case IDETAPE_READ_REQUEST:
2993                         if (!status.b.dsc) {                            /* Tape buffer not ready to accept r/w command */
2994 #if IDETAPE_DEBUG_LOG
2995                                 printk ("ide-tape: DSC != 1 - Postponing read request\n");
2996 #endif /* IDETAPE_DEBUG_LOG */  
2997                                 tape->dsc_polling_frequency=tape->best_dsc_rw_frequency;
2998                                 idetape_postpone_request (drive);       /* Allow ide.c to process requests from */
2999                                 return;
3000                         }                       
3001 
3002                         pc=idetape_next_pc_storage (drive);
3003 
3004                         idetape_create_read_cmd (pc,rq->current_nr_sectors);
3005                         
3006                         pc->buffer=rq->buffer;
3007                         pc->buffer_size=rq->current_nr_sectors*tape->tape_block_size;
3008                         pc->current_position=rq->buffer;
3009                         pc->request_transfer=rq->current_nr_sectors*tape->tape_block_size;
3010 
3011                         idetape_issue_packet_command (drive,pc,&idetape_pc_intr);
3012                         return;
3013                 
3014                 case IDETAPE_WRITE_REQUEST:
3015                         if (!status.b.dsc) {                            /* Tape buffer not ready to accept r/w command */
3016 #if IDETAPE_DEBUG_LOG
3017                                 printk ("ide-tape: DSC != 1 - Postponing write request\n");
3018 #endif /* IDETAPE_DEBUG_LOG */  
3019                                 tape->dsc_polling_frequency=tape->best_dsc_rw_frequency;
3020                                 idetape_postpone_request (drive);       /* Allow ide.c to process requests from */
3021                                 return;
3022                         }                       
3023 
3024                         pc=idetape_next_pc_storage (drive);
3025 
3026                         idetape_create_write_cmd (pc,rq->current_nr_sectors);
3027                         
3028                         pc->buffer=rq->buffer;
3029                         pc->buffer_size=rq->current_nr_sectors*tape->tape_block_size;
3030                         pc->current_position=rq->buffer;
3031                         pc->request_transfer=rq->current_nr_sectors*tape->tape_block_size;
3032 
3033                         idetape_issue_packet_command (drive,pc,&idetape_pc_intr);
3034                         return;
3035                                         
3036                 case IDETAPE_PACKET_COMMAND_REQUEST_TYPE1:
3037                 case IDETAPE_PACKET_COMMAND_REQUEST_TYPE2:
3038 /*
3039  *      This should be unnecessary (postponing of a general packet command),
3040  *      but I have occasionally missed DSC on a media access command otherwise.
3041  *      ??? Still have to figure it out ...
3042  */
3043                         if (!status.b.dsc) {                            /* Tape buffers are still not ready */
3044 #if IDETAPE_DEBUG_LOG
3045                                 printk ("ide-tape: DSC != 1 - Postponing packet command request\n");
3046 #endif /* IDETAPE_DEBUG_LOG */
3047                                 rq->cmd=IDETAPE_PACKET_COMMAND_REQUEST_TYPE2;   /* Note that we are waiting for DSC *before* we */
3048                                                                                 /* even issued the command */
3049                                 tape->dsc_polling_frequency=IDETAPE_DSC_READ_WRITE_FALLBACK_FREQUENCY;
3050                                 idetape_postpone_request (drive);       /* Allow ide.c to process requests from */
3051                                 return;
3052                         }
3053                         rq->cmd=IDETAPE_PACKET_COMMAND_REQUEST_TYPE1;
3054                         pc=(idetape_packet_command_t *) rq->buffer;
3055                         idetape_issue_packet_command (drive,pc,&idetape_pc_intr);
3056                         return;
3057 #if IDETAPE_DEBUG_BUGS
3058                 default:
3059                         printk ("ide-tape: bug in IDETAPE_REQUEST_CMD macro\n");
3060                         idetape_end_request (0,HWGROUP (drive));
3061 #endif /* IDETAPE_DEBUG_BUGS */
3062         }       
3063 }
3064 
3065 /*
3066  *      idetape_queue_pc_tail is based on the following functions:
3067  *
3068  *      ide_do_drive_cmd from ide.c
3069  *      cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
3070  *
3071  *      We add a special packet command request to the tail of the request queue,
3072  *      and wait for it to be serviced.
3073  *
3074  *      This is not to be called from within the request handling part
3075  *      of the driver ! We allocate here data in the stack, and it is valid
3076  *      until the request is finished. This is not the case for the bottom
3077  *      part of the driver, where we are always leaving the functions to wait
3078  *      for an interrupt or a timer event.
3079  *
3080  *      From the bottom part of the driver, we should allocate safe memory
3081  *      using idetape_next_pc_storage and idetape_next_rq_storage, and add
3082  *      the request to the request list without waiting for it to be serviced !
3083  *      In that case, we usually use idetape_queue_pc_head.
3084  */
3085 
3086 int idetape_queue_pc_tail (ide_drive_t *drive,idetape_packet_command_t *pc)
     /* [previous][next][first][last][top][bottom][index][help] */
3087 {
3088         struct request rq;
3089 
3090         ide_init_drive_cmd (&rq);
3091         rq.buffer = (char *) pc;
3092         rq.cmd = IDETAPE_PACKET_COMMAND_REQUEST_TYPE1;
3093         return ide_do_drive_cmd (drive, &rq, ide_wait);
3094 }
3095 
3096 /*
3097  *      idetape_queue_pc_head generates a new packet command request in front
3098  *      of the request queue, before the current request, so that it will be
3099  *      processed immediately, on the next pass through the driver.
3100  *
3101  *      idetape_queue_pc_head is called from the request handling part of
3102  *      the driver (the "bottom" part). Safe storage for the request should
3103  *      be allocated with idetape_next_pc_storage and idetape_next_rq_storage
3104  *      before calling idetape_queue_pc_head.
3105  *
3106  *      Memory for those requests is pre-allocated at initialization time, and
3107  *      is limited to IDETAPE_PC_STACK requests. We assume that we have enough
3108  *      space for the maximum possible number of inter-dependent packet commands.
3109  *
3110  *      The higher level of the driver - The ioctl handler and the character
3111  *      device handling functions should queue request to the lower level part
3112  *      and wait for their completion using idetape_queue_pc_tail or
3113  *      idetape_queue_rw_tail.
3114  */
3115  
3116 void idetape_queue_pc_head (ide_drive_t *drive,idetape_packet_command_t *pc,struct request *rq)
     /* [previous][next][first][last][top][bottom][index][help] */
3117 
3118 {
3119         unsigned int major = HWIF(drive)->major;
3120         struct blk_dev_struct *bdev = &blk_dev[major];
3121 
3122         bdev->current_request=HWGROUP (drive)->rq;              /* Since we may have taken it out */
3123 
3124         ide_init_drive_cmd (rq);
3125         rq->buffer = (char *) pc;
3126         rq->cmd = IDETAPE_PACKET_COMMAND_REQUEST_TYPE1;
3127         (void) ide_do_drive_cmd (drive, rq, ide_preempt);
3128 }
3129 
3130 /*
3131  *      idetape_wait_for_request installs a semaphore in a pending request
3132  *      and sleeps until it is serviced.
3133  *
3134  *      The caller should ensure that the request will not be serviced
3135  *      before we install the semaphore (usually by disabling interrupts).
3136  */
3137  
3138 void idetape_wait_for_request (struct request *rq)
     /* [previous][next][first][last][top][bottom][index][help] */
3139 
3140 {
3141         struct semaphore sem = MUTEX_LOCKED;
3142 
3143 #if IDETAPE_DEBUG_BUGS
3144         if (rq == NULL || !IDETAPE_REQUEST_CMD (rq->cmd)) {
3145                 printk ("ide-tape: bug: Trying to sleep on non-valid request\n");
3146                 return;         
3147         }
3148 #endif /* IDETAPE_DEBUG_BUGS */
3149 
3150         rq->sem=&sem;
3151         down (&sem);
3152 }
3153 
3154 /*
3155  *      idetape_queue_rw_tail generates a read/write request for the block
3156  *      device interface and wait for it to be serviced.
3157  */
3158 
3159 int idetape_queue_rw_tail (ide_drive_t *drive,int cmd,int blocks,char *buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
3160 
3161 {
3162         idetape_tape_t *tape = &(drive->tape);
3163         struct request rq;
3164 
3165 #if IDETAPE_DEBUG_LOG
3166         printk ("idetape_queue_rw_tail: cmd=%d\n",cmd);
3167 #endif /* IDETAPE_DEBUG_LOG */
3168 #if IDETAPE_DEBUG_BUGS
3169         if (tape->active_data_request != NULL) {
3170                 printk ("ide-tape: bug: the pipeline is active in idetape_queue_rw_tail\n");
3171                 return (0);
3172         }
3173 #endif /* IDETAPE_DEBUG_BUGS */ 
3174 
3175         ide_init_drive_cmd (&rq);
3176         rq.buffer = buffer;
3177         rq.cmd = cmd;
3178         rq.sector = tape->block_address;
3179         rq.nr_sectors = rq.current_nr_sectors = blocks;
3180         (void) ide_do_drive_cmd (drive, &rq, ide_wait);
3181 
3182         return (tape->tape_block_size*(blocks-rq.current_nr_sectors));
3183 }
3184 
3185 /*
3186  *      idetape_add_chrdev_read_request handles character device read requests
3187  *      when operating in the pipelined operation mode.
3188  */
3189  
3190 int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks,char *buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
3191 
3192 {
3193         idetape_tape_t *tape = &(drive->tape);
3194         idetape_pipeline_stage_t *new_stage;
3195         unsigned long flags;
3196         struct request rq,*rq_ptr;
3197         int bytes_read;
3198         
3199 #if IDETAPE_DEBUG_LOG
3200         printk ("Reached idetape_add_chrdev_read_request\n");
3201 #endif /* IDETAPE_DEBUG_LOG */
3202 
3203         ide_init_drive_cmd (&rq);
3204         rq.cmd = IDETAPE_READ_REQUEST;
3205         rq.sector = tape->block_address;
3206         rq.nr_sectors = rq.current_nr_sectors = blocks;
3207 
3208         if (tape->current_number_of_stages < 0.5*tape->max_number_of_stages) {
3209                 new_stage=idetape_kmalloc_stage (drive);
3210                 while (new_stage != NULL) {
3211                         new_stage->rq=rq;
3212                         save_flags (flags);cli ();
3213                         idetape_add_stage_tail (drive,new_stage);
3214                         restore_flags (flags);
3215                         new_stage=idetape_kmalloc_stage (drive);
3216                 }
3217                 if (tape->active_data_request == NULL)
3218                         idetape_insert_pipeline_into_queue (drive);
3219         }
3220 
3221         if (tape->first_stage == NULL) {
3222 
3223                 /*
3224                  *      Linux is short on memory. Revert to non-pipelined
3225                  *      operation mode for this request.
3226                  */
3227                  
3228                 return (idetape_queue_rw_tail (drive,IDETAPE_READ_REQUEST,blocks,buffer));
3229         }               
3230         
3231         save_flags (flags);cli ();
3232         if (tape->active_data_request == &(tape->first_stage->rq))
3233                 idetape_wait_for_request (tape->active_data_request);
3234         restore_flags (flags);
3235 
3236         rq_ptr=&(tape->first_stage->rq);
3237         bytes_read=tape->tape_block_size*(rq_ptr->nr_sectors-rq_ptr->current_nr_sectors);
3238         rq_ptr->nr_sectors=rq_ptr->current_nr_sectors=0;
3239         idetape_copy_buffer_from_stage (tape->first_stage,buffer);
3240         if (rq_ptr->errors != IDETAPE_RQ_ERROR_FILEMARK)
3241                 idetape_remove_stage_head (drive);
3242 #if IDETAPE_DEBUG_BUGS
3243         if (bytes_read > blocks*tape->tape_block_size) {
3244                 printk ("ide-tape: bug: trying to return more bytes than requested\n");
3245                 bytes_read=blocks*tape->tape_block_size;
3246         }
3247 #endif /* IDETAPE_DEBUG_BUGS */
3248         return (bytes_read);
3249 }
3250 
3251 /*
3252  *      idetape_add_chrdev_write_request tries to add a character device
3253  *      originated write request to our pipeline. In case we don't succeed,
3254  *      we revert to non-piplined operation mode for this request.
3255  *
3256  *      1.      Try to allocate a new pipeline stage.
3257  *      2.      If we can't, wait for more and more requests to be serviced
3258  *              and try again each time.
3259  *      3.      If we still can't allocate a stage, fallback to
3260  *              non-pipelined operation mode for this request.
3261  */
3262 
3263 int idetape_add_chrdev_write_request (ide_drive_t *drive,int blocks,char *buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
3264 
3265 {
3266         idetape_tape_t *tape = &(drive->tape);
3267         idetape_pipeline_stage_t *new_stage;
3268         unsigned long flags;
3269         struct request *rq;
3270 
3271 #if IDETAPE_DEBUG_LOG
3272         printk ("Reached idetape_add_chrdev_write_request\n");
3273 #endif /* IDETAPE_DEBUG_LOG */
3274         
3275         
3276         new_stage=idetape_kmalloc_stage (drive);
3277 
3278         /*
3279          *      If we don't have a new stage, wait for more and more requests
3280          *      to finish, and try to allocate after each one.
3281          *
3282          *      Pay special attention to possible race conditions.
3283          */
3284 
3285         while (new_stage == NULL) {
3286                 save_flags (flags);cli ();
3287                 if (tape->active_data_request != NULL) {
3288                         idetape_wait_for_request (tape->active_data_request);
3289                         restore_flags (flags);
3290                         new_stage=idetape_kmalloc_stage (drive);
3291                 }
3292                 else {
3293                         /*
3294                          *      Linux is short on memory. Fallback to
3295                          *      non-pipelined operation mode for this request.
3296                          */
3297                         
3298                         restore_flags (flags);
3299                         return (idetape_queue_rw_tail (drive,IDETAPE_WRITE_REQUEST,blocks,buffer));
3300                 }
3301         }
3302 
3303         rq=&(new_stage->rq);
3304 
3305         ide_init_drive_cmd (rq);
3306         rq->cmd = IDETAPE_WRITE_REQUEST;
3307         rq->sector = tape->block_address;       /* Doesn't actually matter - We always assume sequential access */
3308         rq->nr_sectors = blocks;
3309         rq->current_nr_sectors = blocks;
3310 
3311         idetape_copy_buffer_to_stage (new_stage,buffer);
3312 
3313         save_flags (flags);cli ();
3314         idetape_add_stage_tail (drive,new_stage);
3315         restore_flags (flags);
3316 
3317         /*
3318          *      Check if we are currently servicing requests in the bottom
3319          *      part of the driver.
3320          *
3321          *      If not, wait for the pipeline to be full enough (75%) before
3322          *      starting to service requests, so that we will be able to
3323          *      keep up with the higher speeds of the tape.
3324          */
3325 
3326         if (tape->active_data_request == NULL && tape->current_number_of_stages >= 0.75*tape->max_number_of_stages)
3327                 idetape_insert_pipeline_into_queue (drive);             
3328 
3329         if (tape->error_in_pipeline_stage) {            /* Return a deferred error */
3330                 tape->error_in_pipeline_stage=0;
3331                 return (-EIO);
3332         }
3333         
3334         return (blocks);
3335 }
3336 
3337 void idetape_discard_read_pipeline (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
3338 
3339 {
3340         idetape_tape_t *tape = &(drive->tape);
3341         unsigned long flags;
3342 
3343 #if IDETAPE_DEBUG_BUGS
3344         if (tape->chrdev_direction != idetape_direction_read) {
3345                 printk ("ide-tape: bug: Trying to discard read pipeline, but we are not reading.\n");
3346                 return;
3347         }
3348 #endif /* IDETAPE_DEBUG_BUGS */
3349 
3350         tape->merge_buffer_size=tape->merge_buffer_offset=0;
3351         tape->chrdev_direction=idetape_direction_none;
3352         
3353         if (tape->first_stage == NULL)
3354                 return;
3355                 
3356         save_flags (flags);cli ();
3357         tape->next_stage=NULL;
3358         if (tape->active_data_request != NULL)
3359                 idetape_wait_for_request (tape->active_data_request);
3360         restore_flags (flags);
3361 
3362         while (tape->first_stage != NULL)
3363                 idetape_remove_stage_head (drive);
3364 
3365 #if IDETAPE_PIPELINE
3366         tape->max_number_of_stages=IDETAPE_MIN_PIPELINE_STAGES;
3367 #else
3368         tape->max_number_of_stages=0;
3369 #endif /* IDETAPE_PIPELINE */
3370 }
3371 
3372 /*
3373  *      idetape_wait_for_pipeline will wait until all pending pipeline
3374  *      requests are serviced. Typically called on device close.
3375  */
3376  
3377 void idetape_wait_for_pipeline (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
3378 
3379 {
3380         idetape_tape_t *tape = &(drive->tape);
3381         unsigned long flags;
3382 
3383         if (tape->active_data_request == NULL)
3384                 idetape_insert_pipeline_into_queue (drive);             
3385 
3386         if (tape->active_data_request == NULL)
3387                 return;
3388 
3389         save_flags (flags);cli ();
3390         if (tape->last_stage != NULL)
3391                 idetape_wait_for_request (&(tape->last_stage->rq));
3392 
3393         else if (tape->active_data_request != NULL)
3394                 idetape_wait_for_request (tape->active_data_request);
3395         restore_flags (flags);
3396 }
3397 
3398 void idetape_empty_write_pipeline (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
3399 
3400 {
3401         idetape_tape_t *tape = &(drive->tape);
3402         int blocks;
3403         
3404 #if IDETAPE_DEBUG_BUGS
3405         if (tape->chrdev_direction != idetape_direction_write) {
3406                 printk ("ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
3407                 return;
3408         }
3409         if (tape->merge_buffer_size > tape->data_buffer_size) {
3410                 printk ("ide-tape: bug: merge_buffer too big\n");
3411                 tape->merge_buffer_size = tape->data_buffer_size;
3412         }
3413 #endif /* IDETAPE_DEBUG_BUGS */
3414 
3415         if (tape->merge_buffer_size) {
3416                 blocks=tape->merge_buffer_size/tape->tape_block_size;
3417                 if (tape->merge_buffer_size % tape->tape_block_size) {
3418                         blocks++;
3419                         memset (tape->merge_buffer+tape->merge_buffer_size,0,tape->data_buffer_size-tape->merge_buffer_size);
3420                 }
3421                 (void) idetape_add_chrdev_write_request (drive,blocks,tape->merge_buffer);
3422                 tape->merge_buffer_size=0;
3423         }
3424         
3425         idetape_wait_for_pipeline (drive);
3426 
3427         tape->error_in_pipeline_stage=0;
3428         tape->chrdev_direction=idetape_direction_none;
3429 
3430         /*
3431          *      On the next backup, perform the feedback loop again.
3432          *      (I don't want to keep sense information between backups,
3433          *       as some systems are constantly on, and the system load
3434          *       can be totally different on the next backup).
3435          */
3436 
3437 #if IDETAPE_PIPELINE
3438         tape->max_number_of_stages=IDETAPE_MIN_PIPELINE_STAGES;
3439 #else
3440         tape->max_number_of_stages=0;
3441 #endif /* IDETAPE_PIPELINE */
3442 #if IDETAPE_DEBUG_BUGS
3443         if (tape->first_stage != NULL || tape->next_stage != NULL || tape->last_stage != NULL || tape->current_number_of_stages != 0) {
3444                 printk ("ide-tape: ide-tape pipeline bug\n");           
3445         }
3446 #endif /* IDETAPE_DEBUG_BUGS */
3447 }
3448 
3449 /*
3450  *      idetape_zero_packet_command just zeros a packet command and
3451  *      sets the number of retries to 0, as we haven't retried it yet.
3452  */
3453  
3454 void idetape_zero_packet_command (idetape_packet_command_t *pc)
     /* [previous][next][first][last][top][bottom][index][help] */
3455 
3456 {
3457         int i;
3458         
3459         for (i=0;i<12;i++)
3460                 pc->c[i]=0;
3461         pc->retries=0;
3462         pc->abort=0;
3463         pc->dma_recommended=0;
3464         pc->dma_error=0;
3465 }
3466 
3467 /*
3468  *      idetape_swap_shorts converts a 16 bit number from little endian
3469  *      to big endian format.
3470  */
3471  
3472 unsigned short idetape_swap_short (unsigned short temp)
     /* [previous][next][first][last][top][bottom][index][help] */
3473 
3474 {
3475         union convert {
3476                 unsigned all    :16;
3477                 struct {
3478                         unsigned b1     :8;
3479                         unsigned b2     :8;
3480                 } b;
3481         } original,converted;
3482         
3483         original.all=temp;
3484         converted.b.b1=original.b.b2;
3485         converted.b.b2=original.b.b1;
3486         return (converted.all);
3487 }
3488 
3489 /*
3490  *      idetape_swap_long converts from little endian to big endian format.
3491  */
3492  
3493 unsigned long idetape_swap_long (unsigned long temp)
     /* [previous][next][first][last][top][bottom][index][help] */
3494 
3495 {
3496         union convert {
3497                 unsigned all    :32;
3498                 struct {
3499                         unsigned b1     :8;
3500                         unsigned b2     :8;
3501                         unsigned b3     :8;
3502                         unsigned b4     :8;
3503                 } b;
3504         } original,converted;
3505         
3506         original.all=temp;
3507         converted.b.b1=original.b.b4;
3508         converted.b.b2=original.b.b3;
3509         converted.b.b3=original.b.b2;
3510         converted.b.b4=original.b.b1;
3511         return (converted.all);
3512 }
3513 
3514 
3515 /*
3516  *      idetape_next_pc_storage returns a pointer to a place in which we can
3517  *      safely store a packet command, even though we intend to leave the
3518  *      driver. A storage space for a maximum of IDETAPE_PC_STACK packet
3519  *      commands is allocated at initialization time.
3520  */
3521  
3522 idetape_packet_command_t *idetape_next_pc_storage (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
3523 
3524 {
3525         idetape_tape_t *tape;
3526         
3527         tape=&(drive->tape);
3528 #if IDETAPE_DEBUG_LOG
3529         printk ("ide-tape: pc_stack_index=%d\n",tape->pc_stack_index);
3530 #endif /* IDETAPE_DEBUG_LOG */
3531         if (tape->pc_stack_index==IDETAPE_PC_STACK)
3532                 tape->pc_stack_index=0;
3533         return (&(tape->pc_stack [tape->pc_stack_index++]));
3534 }
3535 
3536 /*
3537  *      idetape_next_rq_storage is used along with idetape_next_pc_storage.
3538  *      Since we queue packet commands in the request queue, we need to
3539  *      allocate a request, along with the allocation of a packet command.
3540  */
3541  
3542 /**************************************************************
3543  *                                                            *
3544  *  This should get fixed to use kmalloc(GFP_ATOMIC, ..)      *
3545  *  followed later on by kfree().   -ml                       *
3546  *                                                            *
3547  **************************************************************/
3548  
3549 struct request *idetape_next_rq_storage (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
3550 
3551 {
3552         idetape_tape_t *tape;
3553         
3554         tape=&(drive->tape);
3555 
3556 #if IDETAPE_DEBUG_LOG
3557         printk ("ide-tape: rq_stack_index=%d\n",tape->rq_stack_index);
3558 #endif /* IDETAPE_DEBUG_LOG */
3559         if (tape->rq_stack_index==IDETAPE_PC_STACK)
3560                 tape->rq_stack_index=0;
3561         return (&(tape->rq_stack [tape->rq_stack_index++]));
3562 }
3563 
3564 /*
3565  *      Block device interface functions
3566  *
3567  *      The block device interface should not be used for data transfers.
3568  *      However, we still allow opening it so that we can issue general
3569  *      ide driver configuration ioctl's, such as the interrupt unmask feature.
3570  */
3571 
3572 int idetape_blkdev_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
3573 
3574 {
3575         idetape_tape_t *tape=&(drive->tape);
3576         unsigned long flags;
3577                         
3578         save_flags (flags);cli ();
3579 
3580 #if IDETAPE_DEBUG_LOG
3581         printk ("Reached idetape_blkdev_open\n");
3582 #endif /* IDETAPE_DEBUG_LOG */
3583 
3584         if (tape->busy) {
3585                 restore_flags (flags);          /* Allowing access only through one */
3586                 return (-EBUSY);                /* one file descriptor */
3587         }
3588 
3589         tape->busy=1;
3590         restore_flags (flags);
3591 
3592         return (0);
3593 }
3594 
3595 void idetape_blkdev_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
3596 
3597 {
3598         idetape_tape_t *tape=&(drive->tape);
3599         unsigned long flags;
3600                         
3601 #if IDETAPE_DEBUG_LOG
3602         printk ("Reached idetape_blkdev_release\n");
3603 #endif /* IDETAPE_DEBUG_LOG */
3604 
3605         save_flags (flags);cli ();
3606         tape->busy=0;
3607         restore_flags (flags);
3608 
3609         return;
3610 }
3611 
3612 /*
3613  *      Character device interface functions
3614  */
3615 
3616 /*
3617  *      Our character device read / write functions.
3618  *
3619  *      The tape is optimized to maximize throughput when it is transfering
3620  *      an integral number of the "continuous transfer limit", which is
3621  *      a parameter of the specific tape (26 KB on my particular tape).
3622  *
3623  *      For best results use an integral number of the tape's parameter
3624  *      (which is displayed in the driver installation stage and is returned
3625  *       by the MTIOCGET ioctl).
3626  *
3627  *      As of version 1.3 of the driver, the character device provides an
3628  *      abstract continuous view of the media - any mix of block sizes (even 1
3629  *      byte) on the same backup/restore procedure is supported. The driver
3630  *      will internally convert the requests to the recommended transfer unit,
3631  *      so that an unmatch between the user's block size to the recommended
3632  *      size will only result in a (slightly) increased driver overhead, but
3633  *      will no longer hit performance.
3634  */
3635 
3636 int idetape_chrdev_read (struct inode *inode, struct file *file, char *buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
3637 
3638 {
3639         ide_drive_t *drive=idetape_chrdev.drive;
3640         idetape_tape_t *tape=&(drive->tape);
3641         char *buf_ptr=buf;
3642         int bytes_read,temp,actually_read=0;
3643 
3644 #if IDETAPE_DEBUG_LOG
3645         printk ("Reached idetape_chrdev_read\n");
3646 #endif /* IDETAPE_DEBUG_LOG */
3647 
3648         if (tape->chrdev_direction != idetape_direction_read) {         /* Initialize read operation */
3649                 if (tape->chrdev_direction == idetape_direction_write) {
3650                         idetape_empty_write_pipeline (drive);
3651                         idetape_flush_tape_buffers (drive);
3652                 }
3653                 
3654                 /*
3655                  *      Issue a read 0 command to ensure that DSC handshake
3656                  *      is switched from completion mode to buffer available
3657                  *      mode.
3658                  */
3659                  
3660                 bytes_read=idetape_queue_rw_tail (drive,IDETAPE_READ_REQUEST,0,tape->merge_buffer);
3661                 if (bytes_read < 0)
3662                         return (bytes_read);
3663 
3664                 tape->chrdev_direction=idetape_direction_read;
3665         }
3666         
3667         if (count==0)
3668                 return (0);
3669 
3670         if (tape->merge_buffer_size) {
3671 #if IDETAPE_DEBUG_BUGS
3672                 if (tape->merge_buffer_offset+tape->merge_buffer_size > tape->data_buffer_size) {
3673                         printk ("ide-tape: bug: merge buffer too big\n");
3674                         tape->merge_buffer_offset=0;tape->merge_buffer_size=tape->data_buffer_size-1;
3675                 }
3676 #endif /* IDETAPE_DEBUG_BUGS */
3677                 actually_read=IDETAPE_MIN (tape->merge_buffer_size,count);
3678                 memcpy_tofs (buf_ptr,tape->merge_buffer+tape->merge_buffer_offset,actually_read);
3679                 buf_ptr+=actually_read;tape->merge_buffer_size-=actually_read;
3680                 count-=actually_read;tape->merge_buffer_offset+=actually_read;
3681         }
3682 
3683         while (count >= tape->data_buffer_size) {
3684                 bytes_read=idetape_add_chrdev_read_request (drive,tape->capabilities.ctl,tape->merge_buffer);
3685                 if (bytes_read <= 0)
3686                         return (actually_read);
3687                 memcpy_tofs (buf_ptr,tape->merge_buffer,bytes_read);
3688                 buf_ptr+=bytes_read;count-=bytes_read;actually_read+=bytes_read;
3689         }
3690 
3691         if (count) {
3692                 bytes_read=idetape_add_chrdev_read_request (drive,tape->capabilities.ctl,tape->merge_buffer);
3693                 if (bytes_read <= 0)
3694                         return (actually_read);
3695                 temp=IDETAPE_MIN (count,bytes_read);
3696                 memcpy_tofs (buf_ptr,tape->merge_buffer,temp);
3697                 actually_read+=temp;
3698                 tape->merge_buffer_offset=temp;
3699                 tape->merge_buffer_size=bytes_read-temp;
3700         }
3701         return (actually_read);
3702 }
3703  
3704 int idetape_chrdev_write (struct inode *inode, struct file *file, const char *buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
3705 
3706 {
3707         ide_drive_t *drive=idetape_chrdev.drive;
3708         idetape_tape_t *tape=&(drive->tape);
3709         const char *buf_ptr=buf;
3710         int retval,actually_written=0;
3711 
3712 #if IDETAPE_DEBUG_LOG
3713         printk ("Reached idetape_chrdev_write\n");
3714 #endif /* IDETAPE_DEBUG_LOG */
3715 
3716         if (tape->chrdev_direction != idetape_direction_write) {        /* Initialize write operation */
3717                 if (tape->chrdev_direction == idetape_direction_read)
3718                         idetape_discard_read_pipeline (drive);
3719 
3720                 /*
3721                  *      Issue a write 0 command to ensure that DSC handshake
3722                  *      is switched from completion mode to buffer available
3723                  *      mode.
3724                  */
3725 
3726                 retval=idetape_queue_rw_tail (drive,IDETAPE_WRITE_REQUEST,0,tape->merge_buffer);
3727                 if (retval < 0)
3728                         return (retval);                
3729 
3730                 tape->chrdev_direction=idetape_direction_write;
3731         }
3732 
3733         if (count==0)
3734                 return (0);
3735 
3736         if (tape->merge_buffer_size) {
3737 #if IDETAPE_DEBUG_BUGS
3738                 if (tape->merge_buffer_size >= tape->data_buffer_size) {
3739                         printk ("ide-tape: bug: merge buffer too big\n");
3740                         tape->merge_buffer_size=0;
3741                 }
3742 #endif /* IDETAPE_DEBUG_BUGS */
3743 
3744                 actually_written=IDETAPE_MIN (tape->data_buffer_size-tape->merge_buffer_size,count);
3745                 memcpy_fromfs (tape->merge_buffer+tape->merge_buffer_size,buf_ptr,actually_written);
3746                 buf_ptr+=actually_written;tape->merge_buffer_size+=actually_written;count-=actually_written;
3747 
3748                 if (tape->merge_buffer_size == tape->data_buffer_size) {
3749                         tape->merge_buffer_size=0;
3750                         retval=idetape_add_chrdev_write_request (drive,tape->capabilities.ctl,tape->merge_buffer);
3751                         if (retval <= 0)
3752                                 return (retval);
3753                 }
3754         }
3755 
3756         while (count >= tape->data_buffer_size) {
3757                 memcpy_fromfs (tape->merge_buffer,buf_ptr,tape->data_buffer_size);
3758                 buf_ptr+=tape->data_buffer_size;count-=tape->data_buffer_size;
3759                 retval=idetape_add_chrdev_write_request (drive,tape->capabilities.ctl,tape->merge_buffer);
3760                 actually_written+=tape->data_buffer_size;
3761                 if (retval <= 0)
3762                         return (retval);
3763         }
3764 
3765         if (count) {
3766                 actually_written+=count;
3767                 memcpy_fromfs (tape->merge_buffer,buf_ptr,count);
3768                 tape->merge_buffer_size+=count;
3769         }
3770         return (actually_written);
3771 }
3772 
3773 /*
3774  *      Our character device ioctls.
3775  *
3776  *      General mtio.h magnetic io commands are supported here, and not in
3777  *      the correspoding block interface.
3778  *
3779  *      The following ioctls are supported:
3780  *
3781  *      MTIOCTOP -      Refer to idetape_mtioctop for detailed description.
3782  *
3783  *      MTIOCGET -      The mt_dsreg field in the returned mtget structure
3784  *                      will be set to (recommended block size <<
3785  *                      MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK, which
3786  *                      is currently equal to the size itself.
3787  *                      The other mtget fields are not supported.
3788  *
3789  *                      Note that we do not actually return the tape's
3790  *                      block size. Rather, we provide the recommended
3791  *                      number of bytes which sould be used as a "user
3792  *                      block size" with the character device read/write
3793  *                      functions to maximize throughput.
3794  *
3795  *      MTIOCPOS -      The current tape "position" is returned.
3796  *                      (A unique number which can be used with the MTSEEK
3797  *                       operation to return to this position in some
3798  *                       future time, provided this place was not overwritten
3799  *                       meanwhile).
3800  *
3801  *      Our own ide-tape ioctls are supported on both interfaces.
3802  */
3803 
3804 int idetape_chrdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
3805 
3806 {
3807         ide_drive_t *drive=idetape_chrdev.drive;
3808         idetape_tape_t *tape=&(drive->tape);
3809         idetape_packet_command_t pc;
3810         struct mtop mtop;
3811         struct mtget mtget;
3812         struct mtpos mtpos;
3813         int retval;
3814 
3815 #if IDETAPE_DEBUG_LOG
3816         printk ("Reached idetape_chrdev_ioctl, cmd=%u\n",cmd);
3817 #endif /* IDETAPE_DEBUG_LOG */
3818 
3819         if (tape->chrdev_direction == idetape_direction_write) {
3820                 idetape_empty_write_pipeline (drive);
3821                 idetape_flush_tape_buffers (drive);
3822         }
3823 
3824         if (tape->chrdev_direction == idetape_direction_read && cmd != MTIOCTOP)
3825                 idetape_discard_read_pipeline (drive);
3826         
3827         pc.buffer=pc.temp_buffer;
3828         pc.buffer_size=IDETAPE_TEMP_BUFFER_SIZE;
3829         pc.current_position=pc.temp_buffer;
3830 
3831         switch (cmd) {
3832                 case MTIOCTOP:
3833                         retval=verify_area (VERIFY_READ,(char *) arg,sizeof (struct mtop));
3834                         if (retval) return (retval);
3835                         memcpy_fromfs ((char *) &mtop, (char *) arg, sizeof (struct mtop));
3836                         return (idetape_mtioctop (drive,mtop.mt_op,mtop.mt_count));
3837                 case MTIOCGET:
3838                         mtget.mt_dsreg=(tape->data_buffer_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
3839                         retval=verify_area (VERIFY_WRITE,(char *) arg,sizeof (struct mtget));
3840                         if (retval) return (retval);
3841                         memcpy_tofs ((char *) arg,(char *) &mtget, sizeof (struct mtget));
3842                         return (0);
3843                 case MTIOCPOS:
3844                         idetape_create_read_position_cmd (&pc);
3845                         retval=idetape_queue_pc_tail (drive,&pc);
3846                         if (retval) return (retval);
3847                         mtpos.mt_blkno=tape->block_address;
3848                         retval=verify_area (VERIFY_WRITE,(char *) arg,sizeof (struct mtpos));
3849                         if (retval) return (retval);
3850                         memcpy_tofs ((char *) arg,(char *) &mtpos, sizeof (struct mtpos));
3851                         return (0);
3852                 default:
3853                         return (idetape_blkdev_ioctl (drive,inode,file,cmd,arg));
3854         }
3855 }
3856 
3857 /*
3858  *      idetape_mtioctop is called from idetape_chrdev_ioctl when
3859  *      the general mtio MTIOCTOP ioctl is requested.
3860  *
3861  *      We currently support the following mtio.h operations:
3862  *
3863  *      MTFSF   -       Space over mt_count filemarks in the positive direction.
3864  *                      The tape is positioned after the last spaced filemark.
3865  *
3866  *      MTFSFM  -       Same as MTFSF, but the tape is positioned before the
3867  *                      last filemark.
3868  *
3869  *      MTBSF   -       Steps background over mt_count filemarks, tape is
3870  *                      positioned before the last filemark.
3871  *
3872  *      MTBSFM  -       Like MTBSF, only tape is positioned after the last filemark.
3873  *
3874  *
3875  *      Note:
3876  *
3877  *              MTBSF and MTBSFM are not supported when the tape doesn't
3878  *              supports spacing over filemarks in the reverse direction.
3879  *              In this case, MTFSFM is also usually not supported (it is
3880  *              supported in the rare case in which we crossed the filemark
3881  *              during our read-ahead pipelined operation mode).
3882  *              
3883  *      MTWEOF  -       Writes mt_count filemarks. Tape is positioned after
3884  *                      the last written filemark.
3885  *
3886  *      MTREW   -       Rewindes tape.
3887  *
3888  *      MTOFFL  -       Puts the tape drive "Offline": Rewinds the tape and
3889  *                      prevents further access until the media is replaced.
3890  *
3891  *      MTNOP   -       Flushes tape buffers.
3892  *
3893  *      MTRETEN -       Retension media. This typically consists of one end
3894  *                      to end pass on the media.
3895  *
3896  *      MTEOM   -       Moves to the end of recorded data.
3897  *
3898  *      MTERASE -       Erases tape.
3899  *
3900  *      MTSEEK  -       Positions the tape in a specific block number, which
3901  *                      was previously received using the MTIOCPOS ioctl,
3902  *                      assuming this place was not overwritten meanwhile.
3903  *
3904  *      The following commands are currently not supported:
3905  *
3906  *      MTFSR, MTBSR, MTFSS, MTBSS, MTWSM, MTSETBLK, MTSETDENSITY,
3907  *      MTSETDRVBUFFER, MT_ST_BOOLEANS, MT_ST_WRITE_THRESHOLD.
3908  */
3909  
3910 int idetape_mtioctop (ide_drive_t *drive,short mt_op,int mt_count)
     /* [previous][next][first][last][top][bottom][index][help] */
3911 
3912 {
3913         idetape_tape_t *tape=&(drive->tape);
3914         idetape_packet_command_t pc;
3915         int i,retval;
3916 
3917         pc.buffer=pc.temp_buffer;
3918         pc.buffer_size=IDETAPE_TEMP_BUFFER_SIZE;
3919         pc.current_position=pc.temp_buffer;
3920 
3921 #if IDETAPE_DEBUG_LOG
3922         printk ("Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",mt_op,mt_count);
3923 #endif /* IDETAPE_DEBUG_LOG */
3924 
3925         /*
3926          *      Commands which need our pipelined read-ahead stages.
3927          */
3928 
3929         switch (mt_op) {
3930                 case MTFSF:
3931                 case MTFSFM:
3932                 case MTBSF:
3933                 case MTBSFM:
3934                         if (!mt_count)
3935                                 return (0);
3936                         return (idetape_space_over_filemarks (drive,mt_op,mt_count));
3937                 default:
3938                         break;
3939         }
3940 
3941         /*
3942          *      Empty the pipeline.
3943          */
3944 
3945         if (tape->chrdev_direction == idetape_direction_read)
3946                 idetape_discard_read_pipeline (drive);
3947 
3948         switch (mt_op) {
3949                 case MTWEOF:
3950                         for (i=0;i<mt_count;i++) {
3951                                 idetape_create_write_filemark_cmd (&pc,1);
3952                                 retval=idetape_queue_pc_tail (drive,&pc);
3953                                 if (retval) return (retval);
3954                         }
3955                         return (0);
3956                 case MTREW:
3957                         return (idetape_rewind_tape (drive));
3958                 case MTOFFL:
3959                         idetape_create_load_unload_cmd (&pc,!IDETAPE_LU_LOAD_MASK);
3960                         return (idetape_queue_pc_tail (drive,&pc));
3961                 case MTNOP:
3962                         return (idetape_flush_tape_buffers (drive));
3963                 case MTRETEN:
3964                         idetape_create_load_unload_cmd (&pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
3965                         return (idetape_queue_pc_tail (drive,&pc));
3966                 case MTEOM:
3967                         idetape_create_space_cmd (&pc,0,IDETAPE_SPACE_TO_EOD);
3968                         return (idetape_queue_pc_tail (drive,&pc));
3969                 case MTERASE:
3970                         retval=idetape_rewind_tape (drive);
3971                         if (retval) return (retval);
3972                         idetape_create_erase_cmd (&pc);
3973                         return (idetape_queue_pc_tail (drive,&pc));
3974                 case MTSEEK:
3975                         return (idetape_position_tape (drive,mt_count));
3976                 default:
3977                         printk ("ide-tape: MTIO operation %d not supported\n",mt_op);
3978                         return (-EIO);
3979         }
3980 }
3981 
3982 /*
3983  *      idetape_space_over_filemarks is now a bit more complicated than just
3984  *      passing the command to the tape since we may have crossed some
3985  *      filemarks during our pipelined read-ahead mode.
3986  *
3987  *      As a minor side effect, the pipeline enables us to support MTFSFM when
3988  *      the filemark is in our internal pipeline even if the tape doesn't
3989  *      support spacing over filemarks in the reverse direction.
3990  */
3991  
3992 int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
     /* [previous][next][first][last][top][bottom][index][help] */
3993 
3994 {
3995         idetape_tape_t *tape=&(drive->tape);
3996         idetape_packet_command_t pc;
3997         unsigned long flags;
3998         int retval,count=0,errors;
3999 
4000         if (tape->chrdev_direction == idetape_direction_read) {
4001 
4002                 /*
4003                  *      We have a read-ahead buffer. Scan it for crossed
4004                  *      filemarks.
4005                  */
4006 
4007                 tape->merge_buffer_size=tape->merge_buffer_offset=0;
4008                 while (tape->first_stage != NULL) {
4009                         
4010                         /*
4011                          *      Wait until the first read-ahead request
4012                          *      is serviced.
4013                          */
4014                 
4015                         save_flags (flags);cli ();
4016                         if (tape->active_data_request == &(tape->first_stage->rq))
4017                                 idetape_wait_for_request (tape->active_data_request);
4018                         restore_flags (flags);
4019 
4020                         errors=tape->first_stage->rq.errors;
4021                         if (errors == IDETAPE_RQ_ERROR_FILEMARK)
4022                                 count++;
4023 
4024                         if (count == mt_count) {
4025                                 switch (mt_op) {
4026                                         case MTFSF:
4027                                                 idetape_remove_stage_head (drive);
4028                                         case MTFSFM:
4029                                                 return (0);
4030                                 }
4031                         }
4032                         idetape_remove_stage_head (drive);
4033                 }
4034                 idetape_discard_read_pipeline (drive);
4035         }
4036 
4037         /*
4038          *      The filemark was not found in our internal pipeline.
4039          *      Now we can issue the space command.
4040          */
4041 
4042         pc.buffer=pc.temp_buffer;
4043         pc.buffer_size=IDETAPE_TEMP_BUFFER_SIZE;
4044         pc.current_position=pc.temp_buffer;
4045 
4046         switch (mt_op) {
4047                 case MTFSF:
4048                         idetape_create_space_cmd (&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
4049                         return (idetape_queue_pc_tail (drive,&pc));
4050                 case MTFSFM:
4051                         if (!tape->capabilities.sprev)
4052                                 return (-EIO);
4053                         retval=idetape_mtioctop (drive,MTFSF,mt_count-count);
4054                         if (retval) return (retval);
4055                         return (idetape_mtioctop (drive,MTBSF,1));
4056                 case MTBSF:
4057                         if (!tape->capabilities.sprev)
4058                                 return (-EIO);
4059                         idetape_create_space_cmd (&pc,-(mt_count+count),IDETAPE_SPACE_OVER_FILEMARK);
4060                         return (idetape_queue_pc_tail (drive,&pc));
4061                 case MTBSFM:
4062                         if (!tape->capabilities.sprev)
4063                                 return (-EIO);
4064                         retval=idetape_mtioctop (drive,MTBSF,mt_count+count);
4065                         if (retval) return (retval);
4066                         return (idetape_mtioctop (drive,MTFSF,1));
4067                 default:
4068                         printk ("ide-tape: MTIO operation %d not supported\n",mt_op);
4069                         return (-EIO);
4070         }
4071 }
4072 
4073 /*
4074  *      Our character device open function.
4075  */
4076 
4077 int idetape_chrdev_open (struct inode *inode, struct file *filp)
     /* [previous][next][first][last][top][bottom][index][help] */
4078 
4079 {
4080         ide_drive_t *drive=idetape_chrdev.drive;
4081         idetape_tape_t *tape=&(drive->tape);
4082         unsigned long flags;
4083         unsigned int minor=MINOR (inode->i_rdev),allocation_length;
4084                         
4085         save_flags (flags);cli ();
4086 
4087 #if IDETAPE_DEBUG_LOG
4088         printk ("Reached idetape_chrdev_open\n");
4089 #endif /* IDETAPE_DEBUG_LOG */
4090 
4091         if (minor!=0 && minor!=128) {           /* Currently supporting only one */
4092                 restore_flags (flags);          /* tape drive */
4093                 return (-ENXIO);
4094         }
4095 
4096         if (tape->busy) {
4097                 restore_flags (flags);          /* Allowing access only through one */
4098                 return (-EBUSY);                /* one file descriptor */
4099         }
4100 
4101         tape->busy=1;
4102         restore_flags (flags);
4103 
4104         allocation_length=tape->data_buffer_size;
4105         if (tape->data_buffer_size % IDETAPE_ALLOCATION_BLOCK)
4106                 allocation_length+=IDETAPE_ALLOCATION_BLOCK;
4107 
4108 #if IDETAPE_MINIMIZE_IDLE_MEMORY_USAGE
4109         if (tape->data_buffer == NULL)
4110                 tape->data_buffer=kmalloc (allocation_length,GFP_KERNEL);
4111         if (tape->data_buffer == NULL)
4112                 goto sorry;
4113         if (tape->merge_buffer == NULL)
4114                 tape->merge_buffer=kmalloc (allocation_length,GFP_KERNEL);
4115         if (tape->merge_buffer == NULL) {
4116                 kfree (tape->data_buffer);
4117         sorry:
4118                 printk ("ide-tape: FATAL - Can not allocate continuous buffer of %d bytes\n",allocation_length);
4119                 tape->busy=0;
4120                 return (-EIO);
4121         }
4122 #endif /* IDETAPE_MINIMIZE_IDLE_MEMORY_USAGE */
4123 
4124         if (!tape->block_address_valid) {
4125                 if (idetape_rewind_tape (drive)) {
4126                         printk ("ide-tape: Rewinding tape failed\n");
4127                         tape->busy=0;
4128                         return (-EIO);
4129                 }
4130         }
4131 
4132         return (0);
4133 }
4134 
4135 /*
4136  *      Our character device release function.
4137  */
4138 
4139 void idetape_chrdev_release (struct inode *inode, struct file *filp)
     /* [previous][next][first][last][top][bottom][index][help] */
4140 
4141 {
4142         ide_drive_t *drive=idetape_chrdev.drive;
4143         idetape_tape_t *tape=&(drive->tape);
4144         unsigned int minor=MINOR (inode->i_rdev);
4145         idetape_packet_command_t pc;
4146         unsigned long flags;
4147                         
4148 #if IDETAPE_DEBUG_LOG
4149         printk ("Reached idetape_chrdev_release\n");
4150 #endif /* IDETAPE_DEBUG_LOG */
4151 
4152         if (tape->chrdev_direction == idetape_direction_write) {
4153                 idetape_empty_write_pipeline (drive);
4154                 idetape_create_write_filemark_cmd (&pc,1);      /* Write a filemark */
4155                 if (idetape_queue_pc_tail (drive,&pc))
4156                         printk ("ide-tape: Couldn't write a filemark\n");
4157         }
4158         
4159         if (tape->chrdev_direction == idetape_direction_read) {
4160                 if (minor < 128)
4161                         idetape_discard_read_pipeline (drive);
4162                 else
4163                         idetape_wait_for_pipeline (drive);
4164         }
4165         
4166         if (minor < 128)
4167                 if (idetape_rewind_tape (drive))
4168                         printk ("ide-tape: Rewinding tape failed\n");
4169 
4170 #if IDETAPE_MINIMIZE_IDLE_MEMORY_USAGE
4171         kfree (tape->data_buffer);
4172         tape->data_buffer=NULL;
4173         if (!tape->merge_buffer_size) {
4174                 kfree (tape->merge_buffer);
4175                 tape->merge_buffer=NULL;
4176         }
4177 #endif /* IDETAPE_MINIMIZE_IDLE_MEMORY_USAGE */
4178 
4179         save_flags (flags);cli ();
4180         tape->busy=0;
4181         restore_flags (flags);
4182 
4183         return;
4184 }
4185 
4186 /*
4187  *      idetape_position_tape positions the tape to the requested block
4188  *      using the LOCATE packet command. A READ POSITION command is then
4189  *      issued to check where we are positioned.
4190  *
4191  *      Like all higher level operations, we queue the commands at the tail
4192  *      of the request queue and wait for their completion.
4193  *      
4194  */
4195  
4196 int idetape_position_tape (ide_drive_t *drive,unsigned long block)
     /* [previous][next][first][last][top][bottom][index][help] */
4197 
4198 {
4199         int retval;
4200         idetape_packet_command_t pc;
4201 
4202         idetape_create_locate_cmd (&pc,block,0);
4203         retval=idetape_queue_pc_tail (drive,&pc);
4204         if (retval!=0) return (retval);
4205                         
4206         idetape_create_read_position_cmd (&pc);
4207         pc.buffer=pc.temp_buffer;
4208         pc.buffer_size=IDETAPE_TEMP_BUFFER_SIZE;
4209         pc.current_position=pc.temp_buffer;
4210         return (idetape_queue_pc_tail (drive,&pc));
4211 }
4212 
4213 /*
4214  *      Rewinds the tape to the Begining Of the current Partition (BOP).
4215  *
4216  *      We currently support only one partition.
4217  */ 
4218 
4219 int idetape_rewind_tape (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
4220 
4221 {
4222         int retval;
4223         idetape_packet_command_t pc;
4224 #if IDETAPE_DEBUG_LOG
4225         printk ("Reached idetape_rewind_tape\n");
4226 #endif /* IDETAPE_DEBUG_LOG */  
4227         
4228         idetape_create_rewind_cmd (&pc);
4229         retval=idetape_queue_pc_tail (drive,&pc);
4230         if (retval) return (retval);
4231                         
4232         idetape_create_read_position_cmd (&pc);
4233         pc.buffer=pc.temp_buffer;
4234         pc.buffer_size=IDETAPE_TEMP_BUFFER_SIZE;
4235         pc.current_position=pc.temp_buffer;
4236         return (idetape_queue_pc_tail (drive,&pc));
4237 }
4238 
4239 int idetape_flush_tape_buffers (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
4240 
4241 {
4242         idetape_packet_command_t pc;
4243 
4244         idetape_create_write_filemark_cmd (&pc,0);
4245         return (idetape_queue_pc_tail (drive,&pc));
4246 }
4247 
4248 /*
4249  *      Pipeline related functions
4250  */
4251 
4252 /*
4253  *      idetape_kmalloc_stage uses kmalloc to allocate a pipeline stage,
4254  *      along with all the necessary small buffers which together make
4255  *      a buffer of size tape->data_buffer_size or a bit more, in case
4256  *      it is not a multiply of IDETAPE_ALLOCATION_BLOCK (it isn't ...).
4257  *
4258  *      Returns a pointer to the new allocated stage, or NULL if we
4259  *      can't (or don't want to, in case we already have too many stages)
4260  *      allocate a stage.
4261  *
4262  *      Pipeline stages are optional and are used to increase performance.
4263  *      If we can't allocate them, we'll manage without them.
4264  */
4265  
4266 idetape_pipeline_stage_t *idetape_kmalloc_stage (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
4267 
4268 {
4269         idetape_tape_t *tape=&(drive->tape);
4270         idetape_pipeline_stage_t *new_stage;
4271         idetape_buffer_head_t *prev_bh,*bh;
4272         int buffers_num,i;
4273         
4274 #if IDETAPE_DEBUG_LOG
4275         printk ("Reached idetape_kmalloc_stage\n");
4276 #endif /* IDETAPE_DEBUG_LOG */
4277 
4278         if (tape->current_number_of_stages>=tape->max_number_of_stages) {
4279                 return (NULL);
4280         }
4281                 
4282         new_stage=(idetape_pipeline_stage_t *) kmalloc (sizeof (idetape_pipeline_stage_t),GFP_KERNEL);
4283         if (new_stage==NULL)
4284                 return (NULL);
4285                 
4286         new_stage->next=new_stage->prev=NULL;
4287 
4288         buffers_num=tape->data_buffer_size / IDETAPE_ALLOCATION_BLOCK;
4289         if (tape->data_buffer_size % IDETAPE_ALLOCATION_BLOCK)
4290                 buffers_num++;
4291 
4292         prev_bh=new_stage->bh=(idetape_buffer_head_t *) kmalloc (sizeof (idetape_buffer_head_t),GFP_KERNEL);
4293         if (new_stage->bh==NULL) {
4294                 idetape_kfree_stage (new_stage);
4295                 return (NULL);
4296         }
4297         new_stage->bh->next=NULL;
4298 
4299         new_stage->bh->data=kmalloc (IDETAPE_ALLOCATION_BLOCK,GFP_KERNEL);
4300         if (new_stage->bh->data==NULL) {
4301                 idetape_kfree_stage (new_stage);
4302                 return (NULL);
4303         }
4304         
4305         for (i=1;i<buffers_num;i++) {
4306                 bh=(idetape_buffer_head_t *) kmalloc (sizeof (idetape_buffer_head_t),GFP_KERNEL);
4307                 if (bh==NULL) {
4308                         idetape_kfree_stage (new_stage);
4309                         return (NULL);
4310                 }
4311                 bh->next=NULL;
4312                 prev_bh->next=bh;
4313                 bh->data=kmalloc (IDETAPE_ALLOCATION_BLOCK,GFP_KERNEL);
4314                 if (bh->data == NULL) {
4315                         idetape_kfree_stage (new_stage);
4316                         return (NULL);
4317                 }
4318                 prev_bh=bh;
4319         }
4320         return (new_stage);
4321 }
4322 
4323 /*
4324  *      idetape_kfree_stage calls kfree to completly free a stage, along with
4325  *      its related buffers.
4326  */
4327  
4328 void idetape_kfree_stage (idetape_pipeline_stage_t *stage)
     /* [previous][next][first][last][top][bottom][index][help] */
4329 
4330 {
4331         idetape_buffer_head_t *prev_bh,*bh;
4332         
4333         if (stage == NULL)
4334                 return;
4335 
4336 #if IDETAPE_DEBUG_LOG
4337         printk ("Reached idetape_kfree_stage\n");
4338 #endif /* IDETAPE_DEBUG_LOG */
4339         
4340         bh=stage->bh;
4341         
4342         while (bh != NULL) {
4343                 prev_bh=bh;
4344                 if (bh->data != NULL)
4345                         kfree (bh->data);
4346                 bh=bh->next;
4347                 kfree (prev_bh);
4348         }
4349         
4350         kfree (stage);
4351         return;
4352 }
4353 
4354 /*
4355  *      idetape_copy_buffer_from_stage and idetape_copy_buffer_to_stage
4356  *      copy data from/to the small buffers into/from a continuous buffer.
4357  */
4358   
4359 void idetape_copy_buffer_from_stage (idetape_pipeline_stage_t *stage,char *buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
4360 
4361 {
4362         idetape_buffer_head_t *bh;
4363         char *ptr;
4364 
4365 #if IDETAPE_DEBUG_LOG
4366         printk ("Reached idetape_copy_buffer_from_stage\n");
4367 #endif /* IDETAPE_DEBUG_LOG */
4368 #if IDETAPE_DEBUG_BUGS
4369         if (buffer == NULL) {
4370                 printk ("ide-tape: bug: buffer is null in copy_buffer_from_stage\n");
4371                 return;
4372         }
4373 #endif /* IDETAPE_DEBUG_BUGS */
4374         
4375         ptr=buffer;
4376         bh=stage->bh;
4377         
4378         while (bh != NULL) {
4379 #if IDETAPE_DEBUG_BUGS
4380                 if (bh->data == NULL) {
4381                         printk ("ide-tape: bug: bh->data is null\n");
4382                         return;
4383                 }
4384 #endif /* IDETAPE_DEBUG_BUGS */
4385                 memcpy (ptr,bh->data,IDETAPE_ALLOCATION_BLOCK);
4386                 bh=bh->next;
4387                 ptr=ptr+IDETAPE_ALLOCATION_BLOCK;
4388         }
4389         return;
4390 }
4391 
4392 /*
4393  *      Here we copy a continuous data buffer to the various small buffers
4394  *      in the pipeline stage.
4395  */
4396  
4397 void idetape_copy_buffer_to_stage (idetape_pipeline_stage_t *stage,char *buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
4398 
4399 {
4400         idetape_buffer_head_t *bh;
4401         char *ptr;
4402 
4403 #if IDETAPE_DEBUG_LOG
4404         printk ("Reached idetape_copy_buffer_to_stage\n");
4405 #endif /* IDETAPE_DEBUG_LOG */
4406 #if IDETAPE_DEBUG_BUGS
4407         if (buffer == NULL) {
4408                 printk ("ide-tape: bug: buffer is null in copy_buffer_to_stage\n");
4409                 return;
4410         }
4411 #endif /* IDETAPE_DEBUG_BUGS */
4412 
4413         ptr=buffer;
4414         bh=stage->bh;
4415         
4416         while (bh != NULL) {
4417 #if IDETAPE_DEBUG_BUGS
4418                 if (bh->data == NULL) {
4419                         printk ("ide-tape: bug: bh->data is null\n");
4420                         return;
4421                 }
4422 #endif /* IDETAPE_DEBUG_BUGS */
4423                 memcpy (bh->data,ptr,IDETAPE_ALLOCATION_BLOCK);
4424                 bh=bh->next;
4425                 ptr=ptr+IDETAPE_ALLOCATION_BLOCK;
4426         }
4427         return;
4428 }
4429 
4430 /*
4431  *      idetape_increase_max_pipeline_stages is a part of the feedback
4432  *      loop which tries to find the optimum number of stages. In the
4433  *      feedback loop, we are starting from a minimum maximum number of
4434  *      stages, and if we sense that the pipeline is empty, we try to
4435  *      increase it, until we reach the user compile time memory limit.
4436  */
4437 
4438 void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
4439 
4440 {
4441         idetape_tape_t *tape=&(drive->tape);
4442         
4443 #if IDETAPE_DEBUG_LOG
4444         printk ("Reached idetape_increase_max_pipeline_stages\n");
4445 #endif /* IDETAPE_DEBUG_LOG */
4446 
4447         tape->max_number_of_stages+=IDETAPE_INCREASE_STAGES_RATE*
4448                                         (IDETAPE_MAX_PIPELINE_STAGES-IDETAPE_MIN_PIPELINE_STAGES);
4449 
4450         if (tape->max_number_of_stages >= IDETAPE_MAX_PIPELINE_STAGES)
4451                 tape->max_number_of_stages = IDETAPE_MAX_PIPELINE_STAGES;
4452 
4453 #if IDETAPE_DEBUG_LOG
4454         printk ("Maximum number of stages: %d\n",tape->max_number_of_stages);
4455 #endif /* IDETAPE_DEBUG_LOG */
4456 
4457         return;
4458 }
4459 
4460 /*
4461  *      idetape_add_stage_tail adds a new stage at the end of the pipeline.
4462  *
4463  *      Caller should disable interrupts, if necessary.
4464  */
4465  
4466 void idetape_add_stage_tail (ide_drive_t *drive,idetape_pipeline_stage_t *stage)
     /* [previous][next][first][last][top][bottom][index][help] */
4467 
4468 {
4469         idetape_tape_t *tape=&(drive->tape);
4470         
4471 #if IDETAPE_DEBUG_LOG
4472                 printk ("Reached idetape_add_stage_tail\n");
4473 #endif /* IDETAPE_DEBUG_LOG */
4474 
4475         stage->next=NULL;
4476         stage->prev=tape->last_stage;
4477         if (tape->last_stage != NULL)
4478                 tape->last_stage->next=stage;
4479         else
4480                 tape->first_stage=tape->next_stage=stage;
4481         tape->last_stage=stage;
4482         if (tape->next_stage == NULL)
4483                 tape->next_stage=tape->last_stage;
4484         tape->current_number_of_stages++;
4485 }
4486 
4487 /*
4488  *      idetape_remove_stage_head removes tape->first_stage from the pipeline.
4489  *
4490  *      Again, caller should avoid race conditions.
4491  */
4492  
4493 void idetape_remove_stage_head (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
4494 
4495 {
4496         idetape_tape_t *tape=&(drive->tape);
4497         idetape_pipeline_stage_t *stage;
4498         
4499 #if IDETAPE_DEBUG_LOG
4500                 printk ("Reached idetape_remove_stage_head\n");
4501 #endif /* IDETAPE_DEBUG_LOG */
4502 #if IDETAPE_DEBUG_BUGS
4503         if (tape->first_stage == NULL) {
4504                 printk ("ide-tape: bug: tape->first_stage is NULL\n");
4505                 return;         
4506         }
4507         if (tape->active_stage == tape->first_stage) {
4508                 printk ("ide-tape: bug: Trying to free our active pipeline stage\n");
4509                 return;
4510         }
4511 #endif /* IDETAPE_DEBUG_BUGS */
4512         stage=tape->first_stage;
4513         tape->first_stage=stage->next;
4514         idetape_kfree_stage (stage);
4515         tape->current_number_of_stages--;
4516         if (tape->first_stage == NULL) {
4517                 tape->last_stage=NULL;
4518 #if IDETAPE_DEBUG_BUGS
4519                 if (tape->next_stage != NULL)
4520                         printk ("ide-tape: bug: tape->next_stage != NULL\n");
4521                 if (tape->current_number_of_stages)
4522                         printk ("ide-tape: bug: current_number_of_stages should be 0 now\n");
4523 #endif /* IDETAPE_DEBUG_BUGS */
4524         }
4525 }
4526 
4527 /*
4528  *      idetape_insert_pipeline_into_queue is used to start servicing the
4529  *      pipeline stages, starting from tape->next_stage.
4530  */
4531  
4532 void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
4533 
4534 {
4535         idetape_tape_t *tape=&(drive->tape);
4536 
4537         if (tape->next_stage == NULL)
4538                 return;
4539 
4540         if (tape->active_data_request == NULL) {
4541                 idetape_active_next_stage (drive);
4542                 (void) (ide_do_drive_cmd (drive,tape->active_data_request,ide_end));
4543                 return;
4544         }
4545 }
4546 
4547 /*
4548  *      idetape_active_next_stage will declare the next stage as "active".
4549  */
4550  
4551 void idetape_active_next_stage (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
4552 
4553 {
4554         idetape_tape_t *tape=&(drive->tape);
4555         idetape_pipeline_stage_t *stage=tape->next_stage;
4556         struct request *rq=&(stage->rq);
4557 
4558 #if IDETAPE_DEBUG_LOG
4559         printk ("Reached idetape_active_next_stage\n");
4560 #endif /* IDETAPE_DEBUG_LOG */
4561 #if IDETAPE_DEBUG_BUGS
4562         if (stage == NULL) {
4563                 printk ("ide-tape: bug: Trying to activate a non existing stage\n");
4564                 return;
4565         }
4566 #endif /* IDETAPE_DEBUG_BUGS */ 
4567         if (rq->cmd == IDETAPE_WRITE_REQUEST)
4568                 idetape_copy_buffer_from_stage (stage,tape->data_buffer);
4569         
4570         rq->buffer=tape->data_buffer;
4571         tape->active_data_request=rq;
4572         tape->active_stage=stage;
4573         tape->next_stage=stage->next;
4574 }

/* [previous][next][first][last][top][bottom][index][help] */