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

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