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

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