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 <tgud@tochnapc2.technion.ac.il>
   5  *
   6  * This driver was constructed as a student project in the software laboratory
   7  * of the faculty of electrical engineering in the Technion - Israel's
   8  * Institute Of Technology, with the guide of Avner Lottem and Dr. Ilana David.
   9  *
  10  * It is hereby placed under the terms of the GNU general public license.
  11  * (See linux/COPYING).
  12  */
  13  
  14 /*
  15  * IDE ATAPI streaming tape driver.
  16  *
  17  * This driver is a part of the Linux ide driver and works in co-operation
  18  * with linux/drivers/block/ide.c.
  19  *
  20  * The driver, in co-operation with ide.c, basically traverses the 
  21  * request-list for the block device interface. The character device
  22  * interface, on the other hand, creates new requests, adds them
  23  * to the request-list of the block device, and waits for their completion.
  24  *
  25  * Pipelined operation mode is now supported on both reads and writes.
  26  *
  27  * The block device major and minor numbers are determined from the
  28  * tape's relative position in the ide interfaces, as explained in ide.c.
  29  *
  30  * The character device interface consists of two devices:
  31  *
  32  * ht0          major=37,minor=0        first IDE tape, rewind on close.
  33  * nht0         major=37,minor=128      first IDE tape, no rewind on close.
  34  *
  35  * Run /usr/src/linux/drivers/block/MAKEDEV.ide to create the above entries.
  36  * We currently support only one ide tape drive.
  37  *
  38  * The general magnetic tape commands compatible interface, as defined by
  39  * include/linux/mtio.h, is accessible through the character device.
  40  *
  41  * General ide driver configuration options, such as the interrupt-unmask
  42  * flag, can be configured by issuing an ioctl to the block device interface,
  43  * as any other ide device.
  44  *
  45  * Our own ide-tape ioctl's can can be issued to either the block device or
  46  * the character device interface.
  47  *
  48  * Maximal throughput with minimal bus load will usually be achieved in the
  49  * following scenario:
  50  *
  51  *      1.      ide-tape is operating in the pipelined operation mode.
  52  *      2.      All character device read/write requests consist of an
  53  *              integral number of the tape's recommended data transfer unit
  54  *              (which is shown on initialization and can be received with
  55  *               an ioctl).
  56  *              As of version 1.3 of the driver, this is no longer as critical
  57  *              as it used to be.
  58  *      3.      No buffering is performed by the user backup program.
  59  *
  60  * Testing was done with a 2 GB CONNER CTMA 4000 IDE ATAPI Streaming Tape Drive.
  61  * 
  62  * Ver 0.1   Nov  1 95   Pre-working code :-)
  63  * Ver 0.2   Nov 23 95   A short backup (few megabytes) and restore procedure
  64  *                        was successful ! (Using tar cvf ... on the block
  65  *                        device interface).
  66  *                       A longer backup resulted in major swapping, bad
  67  *                        overall Linux performance and eventually failed as
  68  *                        we received non serial read-ahead requests from the
  69  *                        buffer cache.
  70  * Ver 0.3   Nov 28 95   Long backups are now possible, thanks to the
  71  *                        character device interface. Linux's responsiveness
  72  *                        and performance doesn't seem to be much affected
  73  *                        from the background backup procedure.
  74  *                       Some general mtio.h magnetic tape operations are
  75  *                        now supported by our character device. As a result,
  76  *                        popular tape utilities are starting to work with
  77  *                        ide tapes :-)
  78  *                       The following configurations were tested:
  79  *                              1. An IDE ATAPI TAPE shares the same interface
  80  *                                 and irq with an IDE ATAPI CDROM.
  81  *                              2. An IDE ATAPI TAPE shares the same interface
  82  *                                 and irq with a normal IDE disk.
  83  *                        Both configurations seemed to work just fine !
  84  *                        However, to be on the safe side, it is meanwhile
  85  *                        recommended to give the IDE TAPE its own interface
  86  *                        and irq.
  87  *                       The one thing which needs to be done here is to
  88  *                        add a "request postpone" feature to ide.c,
  89  *                        so that we won't have to wait for the tape to finish
  90  *                        performing a long media access (DSC) request (such
  91  *                        as a rewind) before we can access the other device
  92  *                        on the same interface. This effect doesn't disturb
  93  *                        normal operation most of the time because read/write
  94  *                        requests are relatively fast, and once we are
  95  *                        performing one tape r/w request, a lot of requests
  96  *                        from the other device can be queued and ide.c will
  97  *                        service all of them after this single tape request.
  98  * Ver 1.0   Dec 11 95   Integrated into Linux 1.3.46 development tree.
  99  *                       On each read / write request, we now ask the drive
 100  *                        if we can transfer a constant number of bytes
 101  *                        (a parameter of the drive) only to its buffers,
 102  *                        without causing actual media access. If we can't,
 103  *                        we just wait until we can by polling the DSC bit.
 104  *                        This ensures that while we are not transferring
 105  *                        more bytes than the constant referred to above, the
 106  *                        interrupt latency will not become too high and
 107  *                        we won't cause an interrupt timeout, as happened
 108  *                        occasionally in the previous version.
 109  *                       While polling for DSC, the current request is
 110  *                        postponed and ide.c is free to handle requests from
 111  *                        the other device. This is handled transparently to
 112  *                        ide.c. The hwgroup locking method which was used
 113  *                        in the previous version was removed.
 114  *                       Use of new general features which are provided by
 115  *                        ide.c for use with atapi devices.
 116  *                        (Programming done by Mark Lord)
 117  *                       Few potential bug fixes (Again, suggested by Mark)
 118  *                       Single character device data transfers are now
 119  *                        not limited in size, as they were before.
 120  *                       We are asking the tape about its recommended
 121  *                        transfer unit and send a larger data transfer
 122  *                        as several transfers of the above size.
 123  *                        For best results, use an integral number of this
 124  *                        basic unit (which is shown during driver
 125  *                        initialization). I will soon add an ioctl to get
 126  *                        this important parameter.
 127  *                       Our data transfer buffer is allocated on startup,
 128  *                        rather than before each data transfer. This should
 129  *                        ensure that we will indeed have a data buffer.
 130  * Ver 1.1   Dec 14 95   Fixed random problems which occurred when the tape
 131  *                        shared an interface with another device.
 132  *                        (poll_for_dsc was a complete mess).
 133  *                       Removed some old (non-active) code which had
 134  *                        to do with supporting buffer cache originated
 135  *                        requests.
 136  *                       The block device interface can now be opened, so
 137  *                        that general ide driver features like the unmask
 138  *                        interrupts flag can be selected with an ioctl.
 139  *                        This is the only use of the block device interface.
 140  *                       New fast pipelined operation mode (currently only on
 141  *                        writes). When using the pipelined mode, the
 142  *                        throughput can potentially reach the maximum
 143  *                        tape supported throughput, regardless of the
 144  *                        user backup program. On my tape drive, it sometimes
 145  *                        boosted performance by a factor of 2. Pipelined
 146  *                        mode is enabled by default, but since it has a few
 147  *                        downfalls as well, you may want to disable it.
 148  *                        A short explanation of the pipelined operation mode
 149  *                        is available below.
 150  * Ver 1.2   Jan  1 96   Eliminated pipelined mode race condition.
 151  *                       Added pipeline read mode. As a result, restores
 152  *                        are now as fast as backups.
 153  *                       Optimized shared interface behavior. The new behavior
 154  *                        typically results in better IDE bus efficiency and
 155  *                        higher tape throughput.
 156  *                       Pre-calculation of the expected read/write request
 157  *                        service time, based on the tape's parameters. In
 158  *                        the pipelined operation mode, this allows us to
 159  *                        adjust our polling frequency to a much lower value,
 160  *                        and thus to dramatically reduce our load on Linux,
 161  *                        without any decrease in performance.
 162  *                       Implemented additional mtio.h operations.
 163  *                       The recommended user block size is returned by
 164  *                        the MTIOCGET ioctl.
 165  *                       Additional minor changes.
 166  * Ver 1.3   Feb  9 96   Fixed pipelined read mode bug which prevented the
 167  *                        use of some block sizes during a restore procedure.
 168  *                       The character device interface will now present a
 169  *                        continuous view of the media - any mix of block sizes
 170  *                        during a backup/restore procedure is supported. The
 171  *                        driver will buffer the requests internally and
 172  *                        convert them to the tape's recommended transfer
 173  *                        unit, making performance almost independent of the
 174  *                        chosen user block size.
 175  *                       Some improvements in error recovery.
 176  *                       By cooperating with triton.c, bus mastering DMA can
 177  *                        now sometimes be used with IDE tape drives as well.
 178  *                        Bus mastering DMA has the potential to dramatically
 179  *                        reduce the CPU's overhead when accessing the device,
 180  *                        and can be enabled by using hdparm -d1 on the tape's
 181  *                        block device interface. For more info, read the
 182  *                        comments in triton.c.
 183  * Ver 1.4   Mar 13 96   Fixed serialize support.
 184  * 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 (dma_ok ? 1:0,IDETAPE_FEATURES_REG);                   /* Use PIO/DMA */
1529         OUT_BYTE (bcount.b.high,IDETAPE_BCOUNTH_REG);
1530         OUT_BYTE (bcount.b.low,IDETAPE_BCOUNTL_REG);
1531         OUT_BYTE (drive->select.all,IDETAPE_DRIVESEL_REG);
1532         
1533         ide_set_handler (drive,handler,WAIT_CMD);                       /* Set the interrupt routine */
1534         OUT_BYTE (WIN_PACKETCMD,IDETAPE_ATACOMMAND_REG);                /* Issue the packet command */
1535         if (ide_wait_stat (drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {      /* Wait for DRQ to be ready - Assuming Accelerated DRQ */       
1536                 /*
1537                  *      We currently only support tape drives which report
1538                  *      accelerated DRQ assertion. For this case, specs
1539                  *      allow up to 50us. We really shouldn't get here.
1540                  *
1541                  *      ??? Still needs to think what to do if we reach
1542                  *      here anyway.
1543                  */
1544                  
1545                 printk ("ide-tape: Strange, packet command initiated yet DRQ isn't asserted\n");
1546                 return;
1547         }
1548         
1549         ireason.all=IN_BYTE (IDETAPE_IREASON_REG);
1550         if (!ireason.b.cod || ireason.b.io) {
1551                 printk ("ide-tape: (IO,CoD) != (0,1) while issuing a packet command\n");
1552                 ide_do_reset (drive);
1553                 return;         
1554         }
1555                 
1556         ide_output_data (drive,pc->c,12/4);                     /* Send the actual packet */
1557 #ifdef CONFIG_BLK_DEV_TRITON
1558         if ((pc->dma_in_progress=dma_ok)) {                     /* Begin DMA, if necessary */
1559                 pc->dma_error=0;
1560                 (void) (HWIF(drive)->dmaproc(ide_dma_begin, drive));
1561         }
1562 #endif /* CONFIG_BLK_DEV_TRITON */
1563 }
1564 
1565 /*
1566  *      idetape_pc_intr is the usual interrupt handler which will be called
1567  *      during a packet command. We will transfer some of the data (as
1568  *      requested by the drive) and will re-point interrupt handler to us.
1569  *      When data transfer is finished, we will act according to the
1570  *      algorithm described before idetape_issue_packet_command.
1571  *
1572  */
1573  
1574 
1575 void idetape_pc_intr (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1576 
1577 {
1578         idetape_tape_t *tape=&(drive->tape);
1579         idetape_status_reg_t status;
1580         idetape_bcount_reg_t bcount;
1581         idetape_ireason_reg_t ireason;
1582         idetape_packet_command_t *pc=tape->pc;
1583         unsigned long temp;
1584 
1585 #ifdef CONFIG_BLK_DEV_TRITON
1586         if (pc->dma_in_progress) {
1587                 if ((pc->dma_error=HWIF(drive)->dmaproc(ide_dma_status_bad, drive)))
1588                         /*
1589                          *      We will currently correct the following in
1590                          *      idetape_analyze_error.
1591                          */
1592                         pc->actually_transferred=HWIF(drive)->dmaproc(ide_dma_transferred, drive);
1593                 else
1594                         pc->actually_transferred=pc->request_transfer;
1595                 (void) (HWIF(drive)->dmaproc(ide_dma_abort, drive));    /* End DMA */
1596 #if IDETAPE_DEBUG_LOG
1597                 printk ("ide-tape: DMA finished\n");
1598 #endif /* IDETAPE_DEBUG_LOG */
1599         }
1600 #endif /* CONFIG_BLK_DEV_TRITON */
1601 
1602         status.all=IN_BYTE (IDETAPE_STATUS_REG);                /* Clear the interrupt */
1603 
1604 #if IDETAPE_DEBUG_LOG
1605         printk ("ide-tape: Reached idetape_pc_intr interrupt handler\n");
1606 #endif /* IDETAPE_DEBUG_LOG */  
1607 
1608         if (!status.b.drq) {                                    /* No more interrupts */
1609 #if IDETAPE_DEBUG_LOG
1610                 printk ("Packet command completed\n");
1611                 printk ("Total bytes transferred: %lu\n",pc->actually_transferred);
1612 #endif /* IDETAPE_DEBUG_LOG */
1613                 pc->dma_in_progress=0;
1614                                                 
1615                 sti ();
1616 
1617                 if (status.b.check || pc->dma_error) {                  /* Error detected */
1618 #if IDETAPE_DEBUG_LOG
1619         /*
1620          *      Without debugging, we only log an error if we decided to
1621          *      give up retrying.
1622          */
1623                         printk ("ide-tape: %s: I/O error, ",drive->name);
1624 #endif /* IDETAPE_DEBUG_LOG */
1625                         if (pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
1626                                 printk ("ide-tape: I/O error in request sense command\n");
1627                                 ide_do_reset (drive);
1628                                 return;
1629                         }                       
1630                                                 
1631                         idetape_retry_pc (drive);                       /* Retry operation */
1632                         return;
1633                 }
1634                 pc->error=0;
1635                 if (pc->wait_for_dsc && !status.b.dsc) {                                /* Media access command */
1636                         tape->dsc_polling_frequency=IDETAPE_DSC_FAST_MEDIA_ACCESS_FREQUENCY;
1637                         idetape_postpone_request (drive);               /* Allow ide.c to handle other requests */
1638                         return;
1639                 }
1640                 if (tape->failed_pc == pc)
1641                         tape->failed_pc=NULL;
1642 #if IDETAPE_DEBUG_BUGS
1643                 if (pc->callback==NULL)                 
1644                         printk ("ide-tape: ide-tape bug - Callback function not set !\n");
1645                 else
1646 #endif /* IDETAPE_DEBUG_BUGS */
1647                         (*pc->callback)(drive);                 /* Command finished - Call the callback function */
1648                 return;
1649         }
1650 #ifdef CONFIG_BLK_DEV_TRITON
1651         if (pc->dma_in_progress) {
1652                 pc->dma_in_progress=0;
1653                 printk ("ide-tape: The tape wants to issue more interrupts in DMA mode\n");
1654                 printk ("ide-tape: DMA disabled, reverting to PIO\n");
1655                 drive->using_dma=0;
1656                 ide_do_reset (drive);
1657                 return;
1658         }
1659 #endif /* CONFIG_BLK_DEV_TRITON */
1660         bcount.b.high=IN_BYTE (IDETAPE_BCOUNTH_REG);                    /* Get the number of bytes to transfer */
1661         bcount.b.low=IN_BYTE (IDETAPE_BCOUNTL_REG);                     /* on this interrupt */
1662         ireason.all=IN_BYTE (IDETAPE_IREASON_REG);                      /* Read the interrupt reason register */
1663 
1664         if (ireason.b.cod) {
1665                 printk ("ide-tape: CoD != 0 in idetape_pc_intr\n");
1666                 ide_do_reset (drive);
1667                 return;
1668         }
1669         if (ireason.b.io != !(pc->writing)) {                   /* Hopefully, we will never get here */
1670                 printk ("ide-tape: We wanted to %s, ",pc->writing ? "Write":"Read");
1671                 printk ("but the tape wants us to %s !\n",ireason.b.io ? "Read":"Write");
1672                 ide_do_reset (drive);
1673                 return;
1674         }
1675         
1676         if (!pc->writing) {                                     /* Reading - Check that we have enough space */
1677                 temp=(unsigned long) pc->actually_transferred + bcount.all;
1678                 if ( temp > pc->request_transfer) {
1679                         printk ("ide-tape: The tape wants to send us more data than requested - ");
1680                         if (temp > pc->buffer_size) {
1681                                 printk ("Discarding data\n");
1682                                 idetape_discard_data (drive,bcount.all);
1683                                 ide_set_handler (drive,&idetape_pc_intr,WAIT_CMD);
1684                                 return;
1685                         }
1686                         else
1687                                 printk ("Allowing transfer\n");
1688                 }
1689         }
1690 #if IDETAPE_DEBUG_BUGS  
1691         if (bcount.all && !pc->buffer) {        
1692                 printk ("ide-tape: ide-tape.c bug - Buffer not set in idetape_pc_intr. Discarding data.\n");
1693                 
1694                 if (!pc->writing) {
1695                         printk ("ide-tape: Discarding data\n");
1696                         idetape_discard_data (drive,bcount.all);
1697                         ide_set_handler (drive,&idetape_pc_intr,WAIT_CMD);
1698                         return;
1699                 }
1700                 else {  /* ??? */
1701                 }
1702         }
1703 #endif /* IDETAPE_DEBUG_BUGS */
1704         if (pc->writing)
1705                 idetape_output_data (drive,pc->current_position,bcount.all);    /* Write the current buffer */
1706         else
1707                 idetape_input_data (drive,pc->current_position,bcount.all);     /* Read the current buffer */
1708 #if IDETAPE_DEBUG_LOG
1709         printk ("ide-tape: %s %d bytes\n",pc->writing ? "Wrote":"Received",bcount.all);
1710 #endif /* IDETAPE_DEBUG_LOG */
1711         pc->actually_transferred+=bcount.all;                                   /* Update the current position */
1712         pc->current_position+=bcount.all;
1713 
1714         ide_set_handler (drive,&idetape_pc_intr,WAIT_CMD);              /* And set the interrupt handler again */
1715 }
1716 
1717 /*
1718  *      idetape_postpone_request postpones the current request so that
1719  *      ide.c will be able to service requests from another device on
1720  *      the same hwgroup while we are polling for DSC.
1721  */
1722 
1723 void idetape_postpone_request (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1724 
1725 {
1726         idetape_tape_t *tape=&(drive->tape);
1727         struct request *rq;
1728         idetape_status_reg_t status;
1729         
1730 #if IDETAPE_DEBUG_LOG
1731         printk ("Reached idetape_postpone_request\n");
1732 #endif /* IDETAPE_DEBUG_LOG */
1733 #if IDETAPE_DEBUG_BUGS
1734         if (tape->postponed_rq != NULL)
1735                 printk ("ide-tape.c bug - postponed_rq not NULL in idetape_postpone_request\n");
1736 #endif /* IDETAPE_DEBUG_BUGS */
1737 
1738         tape->dsc_timer.expires=jiffies + tape->dsc_polling_frequency;  /* Set timer to poll for */
1739         tape->dsc_timeout=jiffies+IDETAPE_DSC_TIMEOUT;                  /* actual completion */
1740         tape->dsc_timer.data=(unsigned long) drive;
1741         tape->dsc_timer.function=&idetape_poll_for_dsc;
1742         init_timer (&(tape->dsc_timer));
1743 
1744         /*
1745          * Remove current request from the request queue:
1746          */
1747 
1748         tape->postponed_rq = rq = HWGROUP(drive)->rq;
1749         rq->rq_status = IDETAPE_RQ_POSTPONED;   
1750         blk_dev[MAJOR(rq->rq_dev)].current_request = rq->next;
1751         HWGROUP(drive)->rq = NULL;
1752 
1753         /*
1754          *      Check the status again - Maybe we can save one polling period.
1755          */
1756          
1757         status.all=IN_BYTE (IDETAPE_STATUS_REG);
1758         tape->last_status=status.all;
1759         tape->request_status=1; 
1760         
1761         tape->dsc_polling_start=jiffies;
1762         add_timer(&(tape->dsc_timer));          /* Activate the polling timer */
1763 }
1764 
1765 /*
1766  *      idetape_poll_for_dsc_direct is called from idetape_poll_for_dsc
1767  *      to handle the case in which we can safely communicate with the tape
1768  *      (since no other request for this hwgroup is active).
1769  */
1770  
1771 void idetape_poll_for_dsc_direct (unsigned long data)
     /* [previous][next][first][last][top][bottom][index][help] */
1772 
1773 {
1774         ide_drive_t *drive=(ide_drive_t *) data;
1775         idetape_tape_t *tape=&(drive->tape);
1776         idetape_status_reg_t status;
1777 
1778 #if IDETAPE_DEBUG_LOG
1779         printk ("%s: idetape_poll_for_dsc_direct called\n",drive->name);
1780 #endif /* IDETAPE_DEBUG_LOG */  
1781 
1782         OUT_BYTE(drive->select.all,IDE_SELECT_REG);
1783         status.all=IN_BYTE (IDETAPE_STATUS_REG);
1784         
1785         if (status.b.dsc) {                                     /* DSC received */
1786                 tape->dsc_received=1;
1787                 del_timer (&(tape->dsc_timer));                 /* Stop polling and put back the postponed */
1788                 idetape_put_back_postponed_request (drive);     /* request in the request queue */
1789                 return;
1790         }
1791 
1792         if (jiffies > tape->dsc_timeout)        { /* Timeout */
1793                 tape->dsc_received=0;
1794                 del_timer (&(tape->dsc_timer));
1795                 /* ??? */
1796                 idetape_put_back_postponed_request (drive);
1797                 return;
1798         }
1799 
1800         /* Poll again */
1801         
1802         if (jiffies - tape->dsc_polling_start > IDETAPE_FAST_SLOW_THRESHOLD)
1803                 tape->dsc_timer.expires = jiffies + IDETAPE_DSC_SLOW_MEDIA_ACCESS_FREQUENCY;
1804         else
1805                 tape->dsc_timer.expires = jiffies + tape->dsc_polling_frequency;
1806         add_timer(&(tape->dsc_timer));
1807         return;
1808 }
1809 
1810 /*
1811  *      idetape_poll_for_dsc gets invoked by a timer (which was set
1812  *      by idetape_postpone_request) to poll for the DSC bit
1813  *      in the status register.
1814  *
1815  *      We take care not to perform any tape access if the driver is
1816  *      accessing the other device. We will instead ask ide.c to sample
1817  *      the tape status register on our behalf in the next call to do_request,
1818  *      at the point in which the other device is idle, or assume that
1819  *      DSC was received even though we won't verify it (but when we assume
1820  *      that, it will usually have a solid basis).
1821  *
1822  *      The use of cli () below is a must, as we inspect and change
1823  *      the device request list while another request is active.
1824  */
1825  
1826 void idetape_poll_for_dsc (unsigned long data)
     /* [previous][next][first][last][top][bottom][index][help] */
1827 
1828 {
1829         ide_drive_t *drive=(ide_drive_t *) data;
1830         unsigned int major = HWIF(drive)->major;
1831         idetape_tape_t *tape=&(drive->tape);
1832         struct blk_dev_struct *bdev = &blk_dev[major];
1833         struct request *next_rq;
1834         unsigned long flags;
1835         idetape_status_reg_t status;
1836 
1837 #if IDETAPE_DEBUG_LOG
1838         printk ("%s: idetape_poll_for_dsc called\n",drive->name);
1839 #endif /* IDETAPE_DEBUG_LOG */  
1840 
1841         save_flags (flags);cli ();
1842 
1843         /*
1844          *      Check if the other device is idle. If there are no requests,
1845          *      we can safely access the tape.
1846          */
1847 
1848         if (HWGROUP (drive)->rq == NULL) {
1849                 sti ();
1850                 idetape_poll_for_dsc_direct (data);
1851                 return;
1852         }
1853 
1854         /*
1855          *      If DSC was received, re-insert our postponed request into
1856          *      the request queue (using ide_next).
1857          */
1858 
1859         status.all=tape->last_status;
1860 
1861         if (status.b.dsc) {                                     /* DSC received */
1862                 tape->dsc_received=1;
1863                 idetape_put_back_postponed_request (drive);
1864                 del_timer (&(tape->dsc_timer));
1865                 restore_flags (flags);
1866                 return;
1867         }
1868 
1869         /*
1870          *      At this point, DSC may have been received, but we can't
1871          *      check it. We now have two options:
1872          *
1873          *              1.      The "simple" method - We can continue polling
1874          *                      until we know the value of DSC.
1875          *
1876          *      but we also have a more clever option :-)
1877          *
1878          *              2.      We can sometimes more or less anticipate in
1879          *                      advance how much time it will take for
1880          *                      the tape to perform the request. This is the
1881          *                      place to take advantage of this !
1882          *
1883          *                      We can assume that DSC was received, put
1884          *                      back our request, and hope that we will have
1885          *                      a "cache hit". This will only work when
1886          *                      we haven't initiated the packet command yet,
1887          *                      but this is the common read/write case. As
1888          *                      for the slower media access commands, fallback
1889          *                      to method 1 above.
1890          *
1891          *      When using method 2, we can also take advantage of the
1892          *      knowledge of the tape's internal buffer size - We can
1893          *      precalculate the time it will take for the tape to complete
1894          *      servicing not only one request, but rather, say, 50% of its
1895          *      internal buffer. The polling period will then be much larger,
1896          *      decreasing our load on Linux, and we will also call
1897          *      idetape_postpone_request less often, as there will usually
1898          *      be more room in the internal tape buffer while we are in
1899          *      idetape_do_request.
1900          *
1901          *      For this method to work well, the ongoing request of the
1902          *      other device should be serviced by the time the tape is
1903          *      still working on its remaining 50% internal buffer. This
1904          *      will usually happen when the other device is much faster
1905          *      than the tape.
1906          */
1907 
1908 #if IDETAPE_ANTICIPATE_READ_WRITE_DSC
1909 
1910         /*
1911          *      Method 2.
1912          *
1913          *      There is a high chance that DSC was received, even though
1914          *      we couldn't verify it. Let's hope that it's a "cache hit"
1915          *      rather than a "cache miss". Someday I will probably add a
1916          *      feedback loop around the number of "cache hits" which will
1917          *      fine-tune the polling period.
1918          */
1919          
1920         if (tape->postponed_rq->cmd != IDETAPE_PACKET_COMMAND_REQUEST_TYPE1) {
1921 
1922                 /*
1923                  *      We can use this method only when the packet command
1924                  *      was still not initiated.
1925                  */
1926                  
1927                 idetape_put_back_postponed_request (drive);
1928                 del_timer (&(tape->dsc_timer));
1929                 restore_flags (flags);
1930                 return;
1931         }
1932 #endif /* IDETAPE_ANTICIPATE_READ_WRITE_DSC */
1933 
1934         /*
1935          *      Fallback to method 1.
1936          */
1937 
1938         next_rq=bdev->current_request;
1939         if (next_rq == HWGROUP (drive)->rq)
1940                 next_rq=next_rq->next;
1941 
1942         if (next_rq == NULL) {
1943 
1944                 /*
1945                  *      There will not be another request after the currently
1946                  *      ongoing request, so ide.c won't be able to sample
1947                  *      the status register on our behalf in do_request.
1948                  *
1949                  *      In case we are waiting for DSC before the packet
1950                  *      command was initiated, we will put back our postponed
1951                  *      request and have another look at the status register
1952                  *      in idetape_do_request, as done in method 2 above.
1953                  *
1954                  *      In case we already initiated the command, we can't
1955                  *      put it back, but it is anyway a slow media access
1956                  *      command. We will just give up and poll again until
1957                  *      we are lucky.
1958                  */
1959 
1960                 if (tape->postponed_rq->cmd == IDETAPE_PACKET_COMMAND_REQUEST_TYPE1) {
1961 
1962                         /*
1963                          *      Media access command - Poll again.
1964                          *
1965                          *      We set tape->request_status to 1, just in case
1966                          *      other requests are added while we are waiting.
1967                          */
1968                          
1969                         tape->request_status=1;
1970                         restore_flags (flags);
1971                         tape->dsc_timer.expires = jiffies + tape->dsc_polling_frequency;
1972                         add_timer(&(tape->dsc_timer));
1973                         return;
1974                 }
1975                 
1976                 /*
1977                  *      The packet command hasn't been sent to the tape yet -
1978                  *      We can safely put back the request and have another
1979                  *      look at the status register in idetape_do_request.
1980                  */
1981 
1982                 idetape_put_back_postponed_request (drive);
1983                 del_timer (&(tape->dsc_timer));
1984                 restore_flags (flags);
1985                 return;
1986         }
1987 
1988         /*
1989          *      There will be another request after the current request.
1990          *
1991          *      Request ide.c to sample for us the tape's status register
1992          *      before the next request.
1993          */
1994 
1995         tape->request_status=1;
1996         restore_flags (flags);
1997 
1998         if (jiffies > tape->dsc_timeout)        {               /* Timeout */
1999                 tape->dsc_received=0;
2000                 /* ??? */
2001                 idetape_put_back_postponed_request (drive);
2002                 del_timer (&(tape->dsc_timer));
2003                 restore_flags (flags);
2004                 return;
2005         }
2006 
2007         /* Poll again */
2008         
2009         if (jiffies - tape->dsc_polling_start > IDETAPE_FAST_SLOW_THRESHOLD)
2010                 tape->dsc_timer.expires = jiffies + IDETAPE_DSC_SLOW_MEDIA_ACCESS_FREQUENCY;
2011         else
2012                 tape->dsc_timer.expires = jiffies + tape->dsc_polling_frequency;
2013         add_timer(&(tape->dsc_timer));
2014         return;
2015 }
2016 
2017 /*
2018  *      idetape_put_back_postponed_request gets called when we decided to
2019  *      stop polling for DSC and continue servicing our postponed request.
2020  */
2021 
2022 void idetape_put_back_postponed_request (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2023 
2024 {
2025         idetape_tape_t *tape = &(drive->tape);
2026 
2027 #if IDETAPE_DEBUG_LOG
2028         printk ("ide-tape: Putting back postponed request\n");
2029 #endif /* IDETAPE_DEBUG_LOG */
2030 #if IDETAPE_DEBUG_BUGS
2031         if (tape->postponed_rq == NULL) {
2032                 printk ("tape->postponed_rq is NULL in put_back_postponed_request\n");
2033                 return;
2034         }
2035 #endif /* IDETAPE_DEBUG_BUGS */
2036         (void) ide_do_drive_cmd (drive, tape->postponed_rq, ide_next);
2037 
2038         /*
2039          *      Note that the procedure done here is different than the method
2040          *      we are using in idetape_queue_pc_head - There we are putting
2041          *      request(s) before our currently called request.
2042          *
2043          *      Here, on the other hand, HWGROUP(drive)->rq is not our
2044          *      request but rather a request to another device. Therefore,
2045          *      we will let it finish and only then service our postponed
2046          *      request --> We don't touch HWGROUP(drive)->rq.
2047          */
2048 }
2049 
2050 void idetape_media_access_finished (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2051 
2052 {
2053         idetape_tape_t *tape=&(drive->tape);
2054         idetape_status_reg_t status;
2055         idetape_packet_command_t *pc;
2056 
2057         pc=tape->pc;
2058         
2059         status.all=IN_BYTE (IDETAPE_STATUS_REG);
2060 
2061         if (tape->dsc_received) {
2062 #if IDETAPE_DEBUG_LOG
2063                 printk ("DSC received\n");
2064 #endif /* IDETAPE_DEBUG_LOG */
2065                 if (status.b.check) {                                   /* Error detected */
2066                         printk ("ide-tape: %s: I/O error, ",drive->name);
2067                         idetape_retry_pc (drive);                       /* Retry operation */
2068                         return;
2069                 }
2070                 pc->error=0;
2071                 if (tape->failed_pc == pc)
2072                         tape->failed_pc=NULL;
2073 #if IDETAPE_DEBUG_BUGS
2074                 if (pc->callback==NULL)
2075                         printk ("ide-tape: ide-tape bug - Callback function not set !\n");
2076                 else
2077 #endif /* IDETAPE_DEBUG_BUGS */
2078                         (*pc->callback)(drive);
2079 
2080                 return;
2081         }
2082         else {
2083                 printk ("ide-tape: %s: DSC timeout.\n",drive->name);
2084                 /* ??? */
2085                 pc->error=1;
2086                 tape->failed_pc=NULL;
2087 #if IDETAPE_DEBUG_BUGS
2088                 if (pc->callback==NULL)
2089                         printk ("ide-tape: ide-tape bug - Callback function not set !\n");
2090                 else
2091 #endif /* IDETAPE_DEBUG_BUGS */
2092                         (*pc->callback)(drive);
2093                 return;
2094         }
2095 }
2096 
2097 
2098 /*
2099  *      idetape_retry_pc is called when an error was detected during the
2100  *      last packet command. We queue a request sense packet command in
2101  *      the head of the request list.
2102  */
2103  
2104 void idetape_retry_pc (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2105 
2106 {
2107         idetape_packet_command_t *pc;
2108         struct request *new_rq;
2109 
2110         idetape_error_reg_t error;
2111         error.all=IN_BYTE (IDETAPE_ERROR_REG);
2112         pc=idetape_next_pc_storage (drive);
2113         new_rq=idetape_next_rq_storage (drive);
2114         idetape_create_request_sense_cmd (pc); 
2115         pc->buffer=pc->temp_buffer;
2116         pc->buffer_size=IDETAPE_TEMP_BUFFER_SIZE;
2117         pc->current_position=pc->temp_buffer;
2118         idetape_queue_pc_head (drive,pc,new_rq);
2119 }
2120 
2121 /*
2122  *      General packet command callback function.
2123  */
2124  
2125 void idetape_pc_callback (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2126 
2127 {
2128         idetape_tape_t *tape;
2129         struct request *rq;
2130         
2131         tape=&(drive->tape);
2132         rq=HWGROUP(drive)->rq;
2133         
2134 #if IDETAPE_DEBUG_LOG
2135         printk ("ide-tape: Reached idetape_pc_callback\n");
2136 #endif /* IDETAPE_DEBUG_LOG */
2137         if (!tape->pc->error) {
2138 #if IDETAPE_DEBUG_LOG
2139                 printk ("Request completed\n");
2140 #endif /* IDETAPE_DEBUG_LOG */
2141                 idetape_end_request (1,HWGROUP (drive));
2142         }
2143         else {
2144                 idetape_end_request (0,HWGROUP (drive));
2145         }
2146         return;
2147 }
2148 
2149 
2150 void idetape_read_callback (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2151 
2152 {
2153         idetape_tape_t *tape=&(drive->tape);
2154         struct request *rq=HWGROUP(drive)->rq;
2155         int blocks_read=tape->pc->actually_transferred/tape->tape_block_size;
2156 
2157 #if IDETAPE_DEBUG_LOG   
2158         printk ("ide-tape: Reached idetape_read_callback\n");
2159 #endif /* IDETAPE_DEBUG_LOG */
2160 
2161         tape->block_address+=blocks_read;
2162         rq->current_nr_sectors-=blocks_read;    
2163 
2164         if (!tape->pc->error)
2165                 idetape_end_request (1,HWGROUP (drive));
2166         else {
2167                 rq->errors=tape->pc->error;
2168                 switch (rq->errors) {
2169                         case IDETAPE_RQ_ERROR_FILEMARK:
2170                         case IDETAPE_RQ_ERROR_EOD:
2171                                 break;
2172                 }
2173                 idetape_end_request (0,HWGROUP (drive));
2174         }
2175         return;
2176 }
2177 
2178 void idetape_write_callback (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2179 
2180 {
2181         idetape_tape_t *tape=&(drive->tape);
2182         struct request *rq=HWGROUP(drive)->rq;
2183         int blocks_written=tape->pc->actually_transferred/tape->tape_block_size;
2184                 
2185 #if IDETAPE_DEBUG_LOG   
2186         printk ("ide-tape: Reached idetape_write_callback\n");
2187 #endif /* IDETAPE_DEBUG_LOG */
2188 
2189         tape->block_address+=blocks_written;
2190         rq->current_nr_sectors-=blocks_written;
2191 
2192         if (!tape->pc->error)
2193                 idetape_end_request (1,HWGROUP (drive));
2194         else {
2195                 rq->errors=tape->pc->error;
2196                 idetape_end_request (0,HWGROUP (drive));
2197         }
2198         return;
2199 }
2200 
2201 void idetape_inquiry_callback (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2202 
2203 {
2204         idetape_tape_t *tape;
2205         
2206         tape=&(drive->tape);
2207         
2208         idetape_display_inquiry_result (tape->pc->buffer);
2209         idetape_pc_callback (drive);
2210         return;
2211 }
2212 
2213 /*
2214  *      idetape_input_data is called to read data from the tape's data
2215  *      register. We basically let ide_input_data do the job, but we also
2216  *      take care about the remaining bytes which can not be transferred
2217  *      in 32-bit data transfers.
2218  */
2219  
2220 void idetape_input_data (ide_drive_t *drive,void *buffer, unsigned long bcount)
     /* [previous][next][first][last][top][bottom][index][help] */
2221 
2222 {
2223         unsigned long wcount;
2224         
2225         wcount=bcount >> 2;
2226         bcount -= 4*wcount;
2227         
2228         if (wcount)
2229                 ide_input_data (drive,buffer,wcount);
2230         
2231         if (bcount) {
2232                 ((byte *)buffer) += 4*wcount;
2233                 insb (IDETAPE_DATA_REG,buffer,bcount);
2234         }
2235 }
2236 
2237 /*
2238  *      idetape_output_data is used to write data to the tape.
2239  */
2240  
2241 void idetape_output_data (ide_drive_t *drive,void *buffer, unsigned long bcount)
     /* [previous][next][first][last][top][bottom][index][help] */
2242 
2243 {
2244         unsigned long wcount;
2245         
2246         wcount=bcount >> 2;
2247         bcount -= 4*wcount;
2248         
2249         if (wcount)
2250                 ide_output_data (drive,buffer,wcount);
2251         
2252         if (bcount) {
2253                 ((byte *)buffer) += 4*wcount;
2254                 outsb (IDETAPE_DATA_REG,buffer,bcount);
2255         }
2256 }
2257 
2258 /*
2259  *      Too bad. The drive wants to send us data which we are not ready to accept.
2260  *      Just throw it away.
2261  */
2262  
2263 void idetape_discard_data (ide_drive_t *drive, unsigned long bcount)
     /* [previous][next][first][last][top][bottom][index][help] */
2264 
2265 {
2266         unsigned long i;
2267         
2268         for (i=0;i<bcount;i++)
2269                 IN_BYTE (IDETAPE_DATA_REG);
2270 }
2271 
2272 /*
2273  *      Issue an INQUIRY packet command.
2274  */
2275  
2276 void idetape_create_inquiry_cmd (idetape_packet_command_t *pc)
     /* [previous][next][first][last][top][bottom][index][help] */
2277 
2278 {
2279 #if IDETAPE_DEBUG_LOG
2280         printk ("ide-tape: Creating INQUIRY packet command\n");
2281 #endif /* IDETAPE_DEBUG_LOG */  
2282         pc->request_transfer=36;
2283         pc->callback=&idetape_inquiry_callback;
2284         pc->writing=0;
2285         
2286         idetape_zero_packet_command (pc);
2287         pc->c[0]=IDETAPE_INQUIRY_CMD;
2288         pc->c[4]=255;
2289 }
2290 
2291 /*
2292  *      Format the INQUIRY command results.
2293  */
2294  
2295 void idetape_display_inquiry_result (byte *buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
2296 
2297 {
2298         idetape_inquiry_result_t *result;
2299 
2300         result=(idetape_inquiry_result_t *) buffer;
2301         ide_fixstring (result->vendor_id,8,0);
2302         ide_fixstring (result->product_id,16,0);
2303         ide_fixstring (result->revision_level,4,0);
2304 
2305         if (result->response_format != 2) {
2306                 printk ("The INQUIRY Data Format is unknown to us !\n");
2307                 printk ("Assuming QIC-157C format.\n");
2308         }
2309 
2310 #if IDETAPE_DEBUG_LOG
2311         printk ("Dumping INQUIRY command results:\n");
2312         printk ("Response Data Format: %d - ",result->response_format);
2313         switch (result->response_format) {
2314                 case 2:
2315                         printk ("As specified in QIC-157 Revision C\n");
2316                         break;
2317                 default:
2318                         printk ("Unknown\n");
2319                         break;
2320         }
2321         
2322         printk ("Device Type: %x - ",result->device_type);      
2323         switch (result->device_type) {
2324                 case 0: printk ("Direct-access Device\n");break;
2325                 case 1: printk ("Streaming Tape Device\n");break;
2326                 case 2: case 3: case 4: printk ("Reserved\n");break;
2327                 case 5: printk ("CD-ROM Device\n");break;
2328                 case 6: printk ("Reserved\n");
2329                 case 7: printk ("Optical memory Device\n");break;
2330                 case 0x1f: printk ("Unknown or no Device type\n");break;
2331                 default: printk ("Reserved\n");
2332         }
2333         
2334         printk ("Removable Medium: %s",result->rmb ? "Yes\n":"No\n");
2335 
2336         printk ("ANSI Version: %d - ",result->ansi_version);
2337         switch (result->ansi_version) {
2338                 case 2:
2339                         printk ("QIC-157 Revision C\n");
2340                         break;
2341                 default:
2342                         printk ("Unknown\n");
2343                         break;
2344         }
2345 
2346         printk ("ECMA Version: ");
2347         if (result->ecma_version)
2348                 printk ("%d\n",result->ecma_version);
2349         else
2350                 printk ("Not supported\n");
2351 
2352         printk ("ISO Version: ");
2353         if (result->iso_version)
2354                 printk ("%d\n",result->iso_version);
2355         else
2356                 printk ("Not supported\n");
2357 
2358         printk ("Additional Length: %d\n",result->additional_length);
2359         printk ("Vendor Identification: %s\n",result->vendor_id);
2360         printk ("Product Identification: %s\n",result->product_id);
2361         printk ("Product Revision Level: %s\n",result->revision_level);
2362 #endif /* IDETAPE_DEBUG_LOG */
2363 
2364         if (result->device_type != 1)
2365                 printk ("Device type is not set to tape\n");
2366 
2367         if (!result->rmb)
2368                 printk ("The removable flag is not set\n");
2369 
2370         if (result->ansi_version != 2) {
2371                 printk ("The Ansi Version is unknown to us !\n");
2372                 printk ("Assuming compliance with QIC-157C specification.\n");
2373         }
2374 }
2375 
2376 void idetape_create_request_sense_cmd (idetape_packet_command_t *pc)
     /* [previous][next][first][last][top][bottom][index][help] */
2377 
2378 {
2379 #if IDETAPE_DEBUG_LOG
2380         printk ("ide-tape: Creating REQUEST SENSE packet command\n");
2381 #endif /* IDETAPE_DEBUG_LOG */  
2382         pc->request_transfer=18;
2383         pc->callback=&idetape_request_sense_callback;
2384         pc->writing=0;
2385         
2386         idetape_zero_packet_command (pc);       
2387         pc->c[0]=IDETAPE_REQUEST_SENSE_CMD;
2388         pc->c[4]=255;
2389 }
2390 
2391 void idetape_request_sense_callback (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2392 
2393 {
2394         idetape_tape_t *tape=&(drive->tape);
2395 
2396 #if IDETAPE_DEBUG_LOG
2397         printk ("ide-tape: Reached idetape_request_sense_callback\n");
2398 #endif /* IDETAPE_DEBUG_LOG */
2399         if (!tape->pc->error) {
2400 #if IDETAPE_DEBUG_LOG
2401                 printk ("Request completed\n");
2402 #endif /* IDETAPE_DEBUG_LOG */
2403                 idetape_analyze_error (drive,(idetape_request_sense_result_t *) tape->pc->buffer);
2404                 idetape_end_request (1,HWGROUP (drive));
2405         }
2406         else {
2407                 printk ("Error in REQUEST SENSE itself - Aborting request!\n");
2408                 idetape_end_request (0,HWGROUP (drive));
2409         }
2410         return;
2411 }
2412 
2413 /*
2414  *      idetape_analyze_error is called on each failed packet command retry
2415  *      to analyze the request sense. We currently do not utilize this
2416  *      information.
2417  */
2418  
2419 void idetape_analyze_error (ide_drive_t *drive,idetape_request_sense_result_t *result)
     /* [previous][next][first][last][top][bottom][index][help] */
2420 
2421 {
2422         idetape_tape_t *tape=&(drive->tape);
2423         idetape_packet_command_t *pc=tape->failed_pc;
2424                 
2425         tape->sense_key=result->sense_key;
2426         tape->asc=result->asc;
2427         tape->ascq=result->ascq;
2428         
2429 #if IDETAPE_DEBUG_LOG   
2430         /*
2431          *      Without debugging, we only log an error if we decided to
2432          *      give up retrying.
2433          */
2434         printk ("ide-tape: pc = %x, sense key = %x, asc = %x, ascq = %x\n",pc->c[0],result->sense_key,result->asc,result->ascq);
2435 #endif /* IDETAPE_DEBUG_LOG */
2436 
2437         if (pc->c[0] == IDETAPE_READ_CMD) {
2438                 if (result->filemark) {
2439                         pc->error=IDETAPE_RQ_ERROR_FILEMARK;
2440                         pc->abort=1;
2441                 }
2442         }
2443 
2444         if (pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD) {
2445                 if (result->sense_key == 8) {
2446                         pc->error=IDETAPE_RQ_ERROR_EOD;
2447                         pc->abort=1;
2448                 }
2449         }
2450 
2451 #if 1
2452 #ifdef CONFIG_BLK_DEV_TRITON
2453 
2454         /*
2455          *      Correct pc->actually_transferred by asking the tape.
2456          */
2457 
2458         if (pc->dma_error && pc->abort) {
2459                 unsigned long *long_ptr=(unsigned long *) &(result->information1);
2460                 pc->actually_transferred=pc->request_transfer-tape->tape_block_size*idetape_swap_long (*long_ptr);
2461         }               
2462 #endif /* CONFIG_BLK_DEV_TRITON */
2463 #endif
2464 }
2465 
2466 void idetape_create_test_unit_ready_cmd (idetape_packet_command_t *pc)
     /* [previous][next][first][last][top][bottom][index][help] */
2467 
2468 {
2469 #if IDETAPE_DEBUG_LOG
2470         printk ("ide-tape: Creating TEST UNIT READY packet command\n");
2471 #endif /* IDETAPE_DEBUG_LOG */  
2472         pc->request_transfer=0;
2473         pc->buffer=NULL;
2474         pc->current_position=NULL;
2475         pc->callback=&idetape_pc_callback;
2476         pc->writing=0;
2477         
2478         idetape_zero_packet_command (pc);       
2479         pc->c[0]=IDETAPE_TEST_UNIT_READY_CMD;
2480 }
2481 
2482 void idetape_create_locate_cmd (idetape_packet_command_t *pc,unsigned long block,byte partition)
     /* [previous][next][first][last][top][bottom][index][help] */
2483 
2484 {
2485         unsigned long *ptr;
2486 
2487 #if IDETAPE_DEBUG_LOG
2488         printk ("ide-tape: Creating LOCATE packet command\n");
2489 #endif /* IDETAPE_DEBUG_LOG */
2490         pc->request_transfer=0;
2491         pc->buffer=NULL;
2492         pc->current_position=NULL;
2493         pc->buffer_size=0;
2494         pc->wait_for_dsc=1;
2495         pc->callback=&idetape_pc_callback;
2496         pc->writing=0;
2497                 
2498         idetape_zero_packet_command (pc);
2499         pc->c [0]=IDETAPE_LOCATE_CMD;
2500         pc->c [1]=2;
2501         ptr=(unsigned long *) &(pc->c[3]);
2502         *ptr=idetape_swap_long (block);
2503         pc->c[8]=partition;
2504 }
2505 
2506 void idetape_create_rewind_cmd (idetape_packet_command_t *pc)
     /* [previous][next][first][last][top][bottom][index][help] */
2507 
2508 {
2509 #if IDETAPE_DEBUG_LOG
2510         printk ("ide-tape: Creating REWIND packet command\n");
2511 #endif /* IDETAPE_DEBUG_LOG */
2512         pc->request_transfer=0;
2513         pc->buffer=NULL;
2514         pc->current_position=NULL;
2515         pc->buffer_size=0;
2516         pc->wait_for_dsc=1;
2517         pc->callback=&idetape_pc_callback;
2518         pc->writing=0;
2519                 
2520         idetape_zero_packet_command (pc);
2521         pc->c [0]=IDETAPE_REWIND_CMD;
2522 }
2523 
2524 /*
2525  *      A mode sense command is used to "sense" tape parameters.
2526  */
2527 
2528 void idetape_create_mode_sense_cmd (idetape_packet_command_t *pc,byte page_code)
     /* [previous][next][first][last][top][bottom][index][help] */
2529 
2530 {
2531 #if IDETAPE_DEBUG_LOG
2532         printk ("ide-tape: Creating MODE SENSE packet command - Page %d\n",page_code);
2533 #endif /* IDETAPE_DEBUG_LOG */
2534 
2535         pc->wait_for_dsc=0;
2536         pc->callback=&idetape_pc_callback;
2537         pc->writing=0;
2538 
2539         switch (page_code) {
2540                 case IDETAPE_CAPABILITIES_PAGE:
2541                         pc->request_transfer=24;
2542         }
2543                 
2544         idetape_zero_packet_command (pc);
2545         pc->c [0]=IDETAPE_MODE_SENSE_CMD;
2546         pc->c [1]=8;                            /* DBD = 1 - Don't return block descriptors for now */
2547         pc->c [2]=page_code;
2548         pc->c [3]=255;                          /* Don't limit the returned information */
2549         pc->c [4]=255;                          /* (We will just discard data in that case) */
2550 }
2551 
2552 /*
2553  *      idetape_create_write_filemark_cmd will:
2554  *
2555  *              1.      Write a filemark if write_filemark=1.
2556  *              2.      Flush the device buffers without writing a filemark
2557  *                      if write_filemark=0.
2558  *
2559  */
2560  
2561 void idetape_create_write_filemark_cmd (idetape_packet_command_t *pc,int write_filemark)
     /* [previous][next][first][last][top][bottom][index][help] */
2562 
2563 {
2564 #if IDETAPE_DEBUG_LOG
2565         printk ("Creating WRITE FILEMARK packet command\n");
2566         if (!write_filemark)
2567                 printk ("which will only flush buffered data\n");
2568 #endif /* IDETAPE_DEBUG_LOG */
2569         pc->request_transfer=0;
2570         pc->buffer=NULL;
2571         pc->current_position=NULL;
2572         pc->buffer_size=0;
2573         pc->wait_for_dsc=1;
2574         pc->callback=&idetape_pc_callback;
2575         pc->writing=0;
2576                 
2577         idetape_zero_packet_command (pc);
2578         pc->c [0]=IDETAPE_WRITE_FILEMARK_CMD;
2579         if (write_filemark)
2580                 pc->c [4]=1;
2581 }
2582 
2583 void idetape_create_load_unload_cmd (idetape_packet_command_t *pc,int cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
2584 
2585 {
2586 #if IDETAPE_DEBUG_LOG
2587         printk ("Creating LOAD UNLOAD packet command, cmd=%d\n",cmd);
2588 #endif /* IDETAPE_DEBUG_LOG */
2589         pc->request_transfer=0;
2590         pc->buffer=NULL;
2591         pc->current_position=NULL;
2592         pc->buffer_size=0;
2593         pc->wait_for_dsc=1;
2594         pc->callback=&idetape_pc_callback;
2595         pc->writing=0;
2596                 
2597         idetape_zero_packet_command (pc);
2598         pc->c [0]=IDETAPE_LOAD_UNLOAD_CMD;
2599         pc->c [4]=cmd;
2600 }
2601 
2602 void idetape_create_erase_cmd (idetape_packet_command_t *pc)
     /* [previous][next][first][last][top][bottom][index][help] */
2603 
2604 {
2605 
2606 #if IDETAPE_DEBUG_LOG
2607         printk ("Creating ERASE command\n");
2608 #endif /* IDETAPE_DEBUG_LOG */
2609 
2610         pc->request_transfer=0;
2611         pc->buffer=NULL;
2612         pc->current_position=NULL;
2613         pc->buffer_size=0;
2614         pc->wait_for_dsc=1;
2615         pc->callback=&idetape_pc_callback;
2616         pc->writing=0;
2617                 
2618         idetape_zero_packet_command (pc);
2619         pc->c [0]=IDETAPE_ERASE_CMD;
2620         pc->c [1]=1;
2621 }
2622 
2623 void idetape_create_read_cmd (idetape_packet_command_t *pc,unsigned long length)
     /* [previous][next][first][last][top][bottom][index][help] */
2624 
2625 {
2626         union convert {
2627                 unsigned all    :32;
2628                 struct {
2629                         unsigned b1     :8;
2630                         unsigned b2     :8;
2631                         unsigned b3     :8;
2632                         unsigned b4     :8;
2633                 } b;
2634         } original;
2635         
2636 #if IDETAPE_DEBUG_LOG
2637         printk ("ide-tape: Creating READ packet command\n");
2638 #endif /* IDETAPE_DEBUG_LOG */
2639 
2640         original.all=length;
2641 
2642         pc->wait_for_dsc=0;
2643         pc->callback=&idetape_read_callback;
2644         pc->writing=0;
2645 
2646         idetape_zero_packet_command (pc);
2647 
2648         pc->c [0]=IDETAPE_READ_CMD;
2649         pc->c [1]=1;
2650         pc->c [4]=original.b.b1;
2651         pc->c [3]=original.b.b2;
2652         pc->c [2]=original.b.b3;
2653 
2654         if (length)
2655                 pc->dma_recommended=1;
2656 
2657         return;
2658 }
2659 
2660 void idetape_create_space_cmd (idetape_packet_command_t *pc,long count,byte cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
2661 
2662 {
2663         union convert {
2664                 unsigned all    :32;
2665                 struct {
2666                         unsigned b1     :8;
2667                         unsigned b2     :8;
2668                         unsigned b3     :8;
2669                         unsigned b4     :8;
2670                 } b;
2671         } original;
2672         
2673 #if IDETAPE_DEBUG_LOG
2674         printk ("ide-tape: Creating SPACE packet command\n");
2675 #endif /* IDETAPE_DEBUG_LOG */
2676 
2677         original.all=count;
2678 
2679         pc->request_transfer=0;
2680         pc->buffer=NULL;
2681         pc->current_position=NULL;
2682         pc->buffer_size=0;
2683         pc->wait_for_dsc=1;
2684         pc->callback=&idetape_pc_callback;
2685         pc->writing=0;
2686 
2687         idetape_zero_packet_command (pc);
2688         pc->c [0]=IDETAPE_SPACE_CMD;
2689         pc->c [1]=cmd;
2690         pc->c [4]=original.b.b1;
2691         pc->c [3]=original.b.b2;
2692         pc->c [2]=original.b.b3;
2693 
2694         return;
2695 }
2696 
2697 void idetape_create_write_cmd (idetape_packet_command_t *pc,unsigned long length)
     /* [previous][next][first][last][top][bottom][index][help] */
2698 
2699 {
2700         union convert {
2701                 unsigned all    :32;
2702                 struct {
2703                         unsigned b1     :8;
2704                         unsigned b2     :8;
2705                         unsigned b3     :8;
2706                         unsigned b4     :8;
2707                 } b;
2708         } original;
2709         
2710 #if IDETAPE_DEBUG_LOG
2711         printk ("ide-tape: Creating WRITE packet command\n");
2712 #endif /* IDETAPE_DEBUG_LOG */
2713 
2714         original.all=length;
2715 
2716         pc->wait_for_dsc=0;
2717         pc->callback=&idetape_write_callback;
2718         pc->writing=1;
2719 
2720         idetape_zero_packet_command (pc);
2721 
2722         pc->c [0]=IDETAPE_WRITE_CMD;
2723         pc->c [1]=1;
2724         pc->c [4]=original.b.b1;
2725         pc->c [3]=original.b.b2;
2726         pc->c [2]=original.b.b3;
2727 
2728         if (length)
2729                 pc->dma_recommended=1;
2730 
2731         return;
2732 }
2733 
2734 void idetape_create_read_position_cmd (idetape_packet_command_t *pc)
     /* [previous][next][first][last][top][bottom][index][help] */
2735 
2736 {
2737 #if IDETAPE_DEBUG_LOG
2738         printk ("ide-tape: Creating READ POSITION packet command\n");
2739 #endif /* IDETAPE_DEBUG_LOG */
2740 
2741         pc->request_transfer=20;
2742         pc->wait_for_dsc=0;
2743         pc->callback=&idetape_read_position_callback;
2744         pc->writing=0;
2745 
2746         idetape_zero_packet_command (pc);
2747         pc->c [0]=IDETAPE_READ_POSITION_CMD;
2748         pc->c [1]=0;
2749 }
2750 
2751 void idetape_read_position_callback (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2752 
2753 {
2754         idetape_tape_t *tape;
2755         struct request *rq;
2756         idetape_read_position_result_t *result;
2757         
2758         tape=&(drive->tape);
2759         
2760 #if IDETAPE_DEBUG_LOG
2761         printk ("ide-tape: Reached idetape_read_position_callback\n");
2762 #endif /* IDETAPE_DEBUG_LOG */
2763 
2764         rq=HWGROUP(drive)->rq;
2765         
2766         if (!tape->pc->error) {
2767                 result=(idetape_read_position_result_t *) tape->pc->buffer;
2768 #if IDETAPE_DEBUG_LOG
2769                 printk ("Request completed\n");
2770                 printk ("Dumping the results of the READ POSITION command\n");
2771                 printk ("BOP - %s\n",result->bop ? "Yes":"No");
2772                 printk ("EOP - %s\n",result->eop ? "Yes":"No");
2773 #endif /* IDETAPE_DEBUG_LOG */
2774                 if (result->bpu) {
2775                         printk ("ide-tape: Block location is unknown to the tape\n");
2776                         printk ("Aborting request\n");
2777                         tape->block_address_valid=0;
2778                         idetape_end_request (0,HWGROUP (drive));
2779                 }
2780                 else {
2781 #if IDETAPE_DEBUG_LOG
2782                         printk ("Block Location - %lu\n",idetape_swap_long (result->first_block));
2783 #endif /* IDETAPE_DEBUG_LOG */
2784                         tape->block_address=idetape_swap_long (result->first_block);
2785                         tape->block_address_valid=1;
2786                         idetape_end_request (1,HWGROUP (drive));
2787                 }
2788         }
2789         else {
2790                 printk ("Aborting request\n");
2791                 idetape_end_request (0,HWGROUP (drive));
2792         }
2793         return;
2794 }
2795 
2796 /*
2797  *      Our special ide-tape ioctl's.
2798  *
2799  *      Currently there aren't any significant ioctl's.
2800  *      mtio.h compatible commands should be issued to the character device
2801  *      interface.
2802  */
2803  
2804 int idetape_blkdev_ioctl (ide_drive_t *drive, struct inode *inode, struct file *file,
     /* [previous][next][first][last][top][bottom][index][help] */
2805                         unsigned int cmd, unsigned long arg)
2806 {
2807         idetape_packet_command_t pc;
2808         
2809         pc.buffer=pc.temp_buffer;
2810         pc.buffer_size=IDETAPE_TEMP_BUFFER_SIZE;
2811         pc.current_position=pc.temp_buffer;
2812 
2813 #if IDETAPE_DEBUG_LOG   
2814         printk ("ide-tape: Reached idetape_blkdev_ioctl\n");
2815 #endif /* IDETAPE_DEBUG_LOG */
2816         switch (cmd) {
2817                 default:
2818                         return -EIO;
2819         }
2820 }
2821 
2822 /*
2823  *      Functions which handle requests.
2824  */
2825 
2826 /*
2827  *      idetape_end_request is used to end a request.
2828  */
2829 
2830 void idetape_end_request (byte uptodate, ide_hwgroup_t *hwgroup)
     /* [previous][next][first][last][top][bottom][index][help] */
2831 
2832 {
2833         ide_drive_t *drive = hwgroup->drive;
2834         struct request *rq = hwgroup->rq;
2835         idetape_tape_t *tape = &(drive->tape);
2836         unsigned int major = HWIF(drive)->major;
2837         struct blk_dev_struct *bdev = &blk_dev[major];
2838         int error;
2839 
2840 #if IDETAPE_DEBUG_LOG
2841         printk ("Reached idetape_end_request\n");
2842 #endif /* IDETAPE_DEBUG_LOG */
2843 
2844         bdev->current_request=rq;                       /* Since we may have taken it out */
2845 
2846         if (!rq->errors)                                /* In case rq->errors is already set, */
2847                 rq->errors=!uptodate;                   /* we won't change it. */
2848         error=rq->errors;
2849                 
2850         if (tape->active_data_request == rq) {          /* The request was a pipelined data transfer request */
2851 
2852                 if (rq->cmd == IDETAPE_READ_REQUEST) {
2853 #if IDETAPE_DEBUG_BUGS
2854                         if (tape->active_stage == NULL)
2855                                 printk ("ide-tape: bug: active_stage is NULL in idetape_end_request\n");
2856                         else                            
2857 #endif /* IDETAPE_DEBUG_BUGS */
2858                         idetape_copy_buffer_to_stage (tape->active_stage,tape->data_buffer);
2859                 }
2860 
2861                 tape->active_stage=NULL;
2862                 tape->active_data_request=NULL;
2863 
2864                 if (rq->cmd == IDETAPE_WRITE_REQUEST) {
2865                         if (rq->errors)
2866                                 tape->error_in_pipeline_stage=rq->errors;
2867                         idetape_remove_stage_head (drive);
2868                 }
2869                 
2870                 if (tape->next_stage == NULL) {
2871                         if (!error)
2872                                 idetape_increase_max_pipeline_stages (drive);
2873                         ide_end_drive_cmd (drive, 0, 0);
2874                         return;
2875                 }
2876 
2877                 idetape_active_next_stage (drive);
2878 
2879                 /*
2880                  *      Insert the next request into the request queue.
2881                  *
2882                  *      The choice of using ide_next or ide_end is now left
2883                  *      to the user.
2884                  */
2885                  
2886 #if IDETAPE_LOW_TAPE_PRIORITY
2887                 (void) ide_do_drive_cmd (drive,tape->active_data_request,ide_end);
2888 #else
2889                 (void) ide_do_drive_cmd (drive,tape->active_data_request,ide_next);
2890 #endif /* IDETAPE_LOW_TAPE_PRIORITY */
2891         }
2892         ide_end_drive_cmd (drive, 0, 0);
2893 }
2894 
2895 /*
2896  *      idetape_do_request is our request handling function.    
2897  */
2898 
2899 void idetape_do_request (ide_drive_t *drive, struct request *rq, unsigned long block)
     /* [previous][next][first][last][top][bottom][index][help] */
2900 
2901 {
2902         idetape_tape_t *tape=&(drive->tape);
2903         idetape_packet_command_t *pc;
2904         unsigned int major = HWIF(drive)->major;
2905         struct blk_dev_struct *bdev = &blk_dev[major];
2906         idetape_status_reg_t status;
2907 
2908 #if IDETAPE_DEBUG_LOG
2909         printk ("Current request:\n");
2910         printk ("rq_status: %d, rq_dev: %u, cmd: %d, errors: %d\n",rq->rq_status,(unsigned int) rq->rq_dev,rq->cmd,rq->errors);
2911         printk ("sector: %ld, nr_sectors: %ld, current_nr_sectors: %ld\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
2912 #endif /* IDETAPE_DEBUG_LOG */
2913 
2914         if (!IDETAPE_REQUEST_CMD (rq->cmd)) {
2915 
2916                 /*
2917                  *      We do not support buffer cache originated requests.
2918                  */
2919 
2920                 printk ("ide-tape: Unsupported command in request queue\n");
2921                 printk ("ide-tape: The block device interface should not be used for data transfers.\n");
2922                 printk ("ide-tape: Use the character device interfaces\n");
2923                 printk ("ide-tape: /dev/ht0 and /dev/nht0 instead.\n");
2924                 printk ("ide-tape: (Run linux/drivers/block/MAKEDEV.ide to create them)\n");
2925                 printk ("ide-tape: Aborting request.\n");
2926 
2927                 ide_end_request (0,HWGROUP (drive));                    /* Let the common code handle it */
2928                 return;
2929         }
2930 
2931         /*
2932          *      This is an important point. We will try to remove our request
2933          *      from the block device request queue while we service the
2934          *      request. Note that the request must be returned to
2935          *      bdev->current_request before the next call to
2936          *      ide_end_drive_cmd or ide_do_drive_cmd to conform with the
2937          *      normal behavior of the IDE driver, which leaves the active
2938          *      request in bdev->current_request during I/O.
2939          *
2940          *      This will eliminate fragmentation of disk/cdrom requests
2941          *      around a tape request, now that we are using ide_next to
2942          *      insert pending pipeline requests, since we have only one
2943          *      ide-tape.c data request in the device request queue, and
2944          *      thus once removed, ll_rw_blk.c will only see requests from
2945          *      the other device.
2946          *
2947          *      The potential fragmentation inefficiency was pointed to me
2948          *      by Mark Lord.
2949          */
2950          
2951         if (rq->next != NULL && rq->rq_dev != rq->next->rq_dev)
2952                 bdev->current_request=rq->next;
2953 
2954         /* Retry a failed packet command */
2955 
2956         if (tape->failed_pc != NULL && tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
2957                 idetape_issue_packet_command (drive,tape->failed_pc,&idetape_pc_intr);
2958                 return;
2959         }
2960 
2961         /* Check if we have a postponed request */
2962         
2963         if (tape->postponed_rq != NULL) {
2964 #if IDETAPE_DEBUG_BUGS
2965                 if (tape->postponed_rq->rq_status != RQ_ACTIVE || rq != tape->postponed_rq) {
2966                         printk ("ide-tape: ide-tape.c bug - Two DSC requests were queued\n");
2967                         idetape_end_request (0,HWGROUP (drive));
2968                         return;
2969                 }
2970 #endif /* IDETAPE_DEBUG_BUGS */
2971                 if (rq->cmd == IDETAPE_PACKET_COMMAND_REQUEST_TYPE1) {
2972         
2973                         /* Media access command */
2974                         
2975                         tape->postponed_rq = NULL;
2976                         idetape_media_access_finished (drive);
2977                         return;
2978                 }
2979                 
2980                 /*
2981                  * Read / Write command - DSC polling was done before the
2982                  * actual command - Continue normally so that the command
2983                  * will be performed below.
2984                  */
2985                  
2986                  tape->postponed_rq = NULL;
2987         }       
2988 
2989         status.all=IN_BYTE (IDETAPE_STATUS_REG);
2990 
2991         /*
2992          *      After a software reset, the status register is locked. We
2993          *      will ignore the DSC value for our very first packet command,
2994          *      which will restore DSC operation.
2995          */
2996 
2997         if (tape->reset_issued) {
2998                 status.b.dsc=1;
2999                 tape->reset_issued=0;
3000         }
3001         
3002         switch (rq->cmd) {
3003                 case IDETAPE_READ_REQUEST:
3004                         if (!status.b.dsc) {                            /* Tape buffer not ready to accept r/w command */
3005 #if IDETAPE_DEBUG_LOG
3006                                 printk ("ide-tape: DSC != 1 - Postponing read request\n");
3007 #endif /* IDETAPE_DEBUG_LOG */  
3008                                 tape->dsc_polling_frequency=tape->best_dsc_rw_frequency;
3009                                 idetape_postpone_request (drive);       /* Allow ide.c to process requests from */
3010                                 return;
3011                         }                       
3012 
3013                         pc=idetape_next_pc_storage (drive);
3014 
3015                         idetape_create_read_cmd (pc,rq->current_nr_sectors);
3016                         
3017                         pc->buffer=rq->buffer;
3018                         pc->buffer_size=rq->current_nr_sectors*tape->tape_block_size;
3019                         pc->current_position=rq->buffer;
3020                         pc->request_transfer=rq->current_nr_sectors*tape->tape_block_size;
3021 
3022                         idetape_issue_packet_command (drive,pc,&idetape_pc_intr);
3023                         return;
3024                 
3025                 case IDETAPE_WRITE_REQUEST:
3026                         if (!status.b.dsc) {                            /* Tape buffer not ready to accept r/w command */
3027 #if IDETAPE_DEBUG_LOG
3028                                 printk ("ide-tape: DSC != 1 - Postponing write request\n");
3029 #endif /* IDETAPE_DEBUG_LOG */  
3030                                 tape->dsc_polling_frequency=tape->best_dsc_rw_frequency;
3031                                 idetape_postpone_request (drive);       /* Allow ide.c to process requests from */
3032                                 return;
3033                         }                       
3034 
3035                         pc=idetape_next_pc_storage (drive);
3036 
3037                         idetape_create_write_cmd (pc,rq->current_nr_sectors);
3038                         
3039                         pc->buffer=rq->buffer;
3040                         pc->buffer_size=rq->current_nr_sectors*tape->tape_block_size;
3041                         pc->current_position=rq->buffer;
3042                         pc->request_transfer=rq->current_nr_sectors*tape->tape_block_size;
3043 
3044                         idetape_issue_packet_command (drive,pc,&idetape_pc_intr);
3045                         return;
3046                                         
3047                 case IDETAPE_PACKET_COMMAND_REQUEST_TYPE1:
3048                 case IDETAPE_PACKET_COMMAND_REQUEST_TYPE2:
3049 /*
3050  *      This should be unnecessary (postponing of a general packet command),
3051  *      but I have occasionally missed DSC on a media access command otherwise.
3052  *      ??? Still have to figure it out ...
3053  */
3054                         if (!status.b.dsc) {                            /* Tape buffers are still not ready */
3055 #if IDETAPE_DEBUG_LOG
3056                                 printk ("ide-tape: DSC != 1 - Postponing packet command request\n");
3057 #endif /* IDETAPE_DEBUG_LOG */
3058                                 rq->cmd=IDETAPE_PACKET_COMMAND_REQUEST_TYPE2;   /* Note that we are waiting for DSC *before* we */
3059                                                                                 /* even issued the command */
3060                                 tape->dsc_polling_frequency=IDETAPE_DSC_READ_WRITE_FALLBACK_FREQUENCY;
3061                                 idetape_postpone_request (drive);       /* Allow ide.c to process requests from */
3062                                 return;
3063                         }
3064                         rq->cmd=IDETAPE_PACKET_COMMAND_REQUEST_TYPE1;
3065                         pc=(idetape_packet_command_t *) rq->buffer;
3066                         idetape_issue_packet_command (drive,pc,&idetape_pc_intr);
3067                         return;
3068 #if IDETAPE_DEBUG_BUGS
3069                 default:
3070                         printk ("ide-tape: bug in IDETAPE_REQUEST_CMD macro\n");
3071                         idetape_end_request (0,HWGROUP (drive));
3072 #endif /* IDETAPE_DEBUG_BUGS */
3073         }       
3074 }
3075 
3076 /*
3077  *      idetape_queue_pc_tail is based on the following functions:
3078  *
3079  *      ide_do_drive_cmd from ide.c
3080  *      cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
3081  *
3082  *      We add a special packet command request to the tail of the request queue,
3083  *      and wait for it to be serviced.
3084  *
3085  *      This is not to be called from within the request handling part
3086  *      of the driver ! We allocate here data in the stack, and it is valid
3087  *      until the request is finished. This is not the case for the bottom
3088  *      part of the driver, where we are always leaving the functions to wait
3089  *      for an interrupt or a timer event.
3090  *
3091  *      From the bottom part of the driver, we should allocate safe memory
3092  *      using idetape_next_pc_storage and idetape_next_rq_storage, and add
3093  *      the request to the request list without waiting for it to be serviced !
3094  *      In that case, we usually use idetape_queue_pc_head.
3095  */
3096 
3097 int idetape_queue_pc_tail (ide_drive_t *drive,idetape_packet_command_t *pc)
     /* [previous][next][first][last][top][bottom][index][help] */
3098 {
3099         struct request rq;
3100 
3101         ide_init_drive_cmd (&rq);
3102         rq.buffer = (char *) pc;
3103         rq.cmd = IDETAPE_PACKET_COMMAND_REQUEST_TYPE1;
3104         return ide_do_drive_cmd (drive, &rq, ide_wait);
3105 }
3106 
3107 /*
3108  *      idetape_queue_pc_head generates a new packet command request in front
3109  *      of the request queue, before the current request, so that it will be
3110  *      processed immediately, on the next pass through the driver.
3111  *
3112  *      idetape_queue_pc_head is called from the request handling part of
3113  *      the driver (the "bottom" part). Safe storage for the request should
3114  *      be allocated with idetape_next_pc_storage and idetape_next_rq_storage
3115  *      before calling idetape_queue_pc_head.
3116  *
3117  *      Memory for those requests is pre-allocated at initialization time, and
3118  *      is limited to IDETAPE_PC_STACK requests. We assume that we have enough
3119  *      space for the maximum possible number of inter-dependent packet commands.
3120  *
3121  *      The higher level of the driver - The ioctl handler and the character
3122  *      device handling functions should queue request to the lower level part
3123  *      and wait for their completion using idetape_queue_pc_tail or
3124  *      idetape_queue_rw_tail.
3125  */
3126  
3127 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] */
3128 
3129 {
3130         unsigned int major = HWIF(drive)->major;
3131         struct blk_dev_struct *bdev = &blk_dev[major];
3132 
3133         bdev->current_request=HWGROUP (drive)->rq;              /* Since we may have taken it out */
3134 
3135         ide_init_drive_cmd (rq);
3136         rq->buffer = (char *) pc;
3137         rq->cmd = IDETAPE_PACKET_COMMAND_REQUEST_TYPE1;
3138         (void) ide_do_drive_cmd (drive, rq, ide_preempt);
3139 }
3140 
3141 /*
3142  *      idetape_wait_for_request installs a semaphore in a pending request
3143  *      and sleeps until it is serviced.
3144  *
3145  *      The caller should ensure that the request will not be serviced
3146  *      before we install the semaphore (usually by disabling interrupts).
3147  */
3148  
3149 void idetape_wait_for_request (struct request *rq)
     /* [previous][next][first][last][top][bottom][index][help] */
3150 
3151 {
3152         struct semaphore sem = MUTEX_LOCKED;
3153 
3154 #if IDETAPE_DEBUG_BUGS
3155         if (rq == NULL || !IDETAPE_REQUEST_CMD (rq->cmd)) {
3156                 printk ("ide-tape: bug: Trying to sleep on non-valid request\n");
3157                 return;         
3158         }
3159 #endif /* IDETAPE_DEBUG_BUGS */
3160 
3161         rq->sem=&sem;
3162         down (&sem);
3163 }
3164 
3165 /*
3166  *      idetape_queue_rw_tail generates a read/write request for the block
3167  *      device interface and wait for it to be serviced.
3168  */
3169 
3170 int idetape_queue_rw_tail (ide_drive_t *drive,int cmd,int blocks,char *buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
3171 
3172 {
3173         idetape_tape_t *tape = &(drive->tape);
3174         struct request rq;
3175 
3176 #if IDETAPE_DEBUG_LOG
3177         printk ("idetape_queue_rw_tail: cmd=%d\n",cmd);
3178 #endif /* IDETAPE_DEBUG_LOG */
3179 #if IDETAPE_DEBUG_BUGS
3180         if (tape->active_data_request != NULL) {
3181                 printk ("ide-tape: bug: the pipeline is active in idetape_queue_rw_tail\n");
3182                 return (0);
3183         }
3184 #endif /* IDETAPE_DEBUG_BUGS */ 
3185 
3186         ide_init_drive_cmd (&rq);
3187         rq.buffer = buffer;
3188         rq.cmd = cmd;
3189         rq.sector = tape->block_address;
3190         rq.nr_sectors = rq.current_nr_sectors = blocks;
3191         (void) ide_do_drive_cmd (drive, &rq, ide_wait);
3192 
3193         return (tape->tape_block_size*(blocks-rq.current_nr_sectors));
3194 }
3195 
3196 /*
3197  *      idetape_add_chrdev_read_request handles character device read requests
3198  *      when operating in the pipelined operation mode.
3199  */
3200  
3201 int idetape_add_chrdev_read_request (ide_drive_t *drive,int blocks,char *buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
3202 
3203 {
3204         idetape_tape_t *tape = &(drive->tape);
3205         idetape_pipeline_stage_t *new_stage;
3206         unsigned long flags;
3207         struct request rq,*rq_ptr;
3208         int bytes_read;
3209         
3210 #if IDETAPE_DEBUG_LOG
3211         printk ("Reached idetape_add_chrdev_read_request\n");
3212 #endif /* IDETAPE_DEBUG_LOG */
3213 
3214         ide_init_drive_cmd (&rq);
3215         rq.cmd = IDETAPE_READ_REQUEST;
3216         rq.sector = tape->block_address;
3217         rq.nr_sectors = rq.current_nr_sectors = blocks;
3218 
3219         if (tape->active_data_request != NULL || tape->current_number_of_stages <= 0.25*tape->max_number_of_stages) {
3220                 new_stage=idetape_kmalloc_stage (drive);
3221                 while (new_stage != NULL) {
3222                         new_stage->rq=rq;
3223                         save_flags (flags);cli ();
3224                         idetape_add_stage_tail (drive,new_stage);
3225                         restore_flags (flags);
3226                         new_stage=idetape_kmalloc_stage (drive);
3227                 }
3228                 if (tape->active_data_request == NULL)
3229                         idetape_insert_pipeline_into_queue (drive);
3230         }
3231 
3232         if (tape->first_stage == NULL) {
3233 
3234                 /*
3235                  *      Linux is short on memory. Revert to non-pipelined
3236                  *      operation mode for this request.
3237                  */
3238                  
3239                 return (idetape_queue_rw_tail (drive,IDETAPE_READ_REQUEST,blocks,buffer));
3240         }               
3241         
3242         save_flags (flags);cli ();
3243         if (tape->active_data_request == &(tape->first_stage->rq))
3244                 idetape_wait_for_request (tape->active_data_request);
3245         restore_flags (flags);
3246 
3247         rq_ptr=&(tape->first_stage->rq);
3248         bytes_read=tape->tape_block_size*(rq_ptr->nr_sectors-rq_ptr->current_nr_sectors);
3249         rq_ptr->nr_sectors=rq_ptr->current_nr_sectors=0;
3250         idetape_copy_buffer_from_stage (tape->first_stage,buffer);
3251         if (rq_ptr->errors != IDETAPE_RQ_ERROR_FILEMARK)
3252                 idetape_remove_stage_head (drive);
3253 #if IDETAPE_DEBUG_BUGS
3254         if (bytes_read > blocks*tape->tape_block_size) {
3255                 printk ("ide-tape: bug: trying to return more bytes than requested\n");
3256                 bytes_read=blocks*tape->tape_block_size;
3257         }
3258 #endif /* IDETAPE_DEBUG_BUGS */
3259         return (bytes_read);
3260 }
3261 
3262 /*
3263  *      idetape_add_chrdev_write_request tries to add a character device
3264  *      originated write request to our pipeline. In case we don't succeed,
3265  *      we revert to non-pipelined operation mode for this request.
3266  *
3267  *      1.      Try to allocate a new pipeline stage.
3268  *      2.      If we can't, wait for more and more requests to be serviced
3269  *              and try again each time.
3270  *      3.      If we still can't allocate a stage, fallback to
3271  *              non-pipelined operation mode for this request.
3272  */
3273 
3274 int idetape_add_chrdev_write_request (ide_drive_t *drive,int blocks,char *buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
3275 
3276 {
3277         idetape_tape_t *tape = &(drive->tape);
3278         idetape_pipeline_stage_t *new_stage;
3279         unsigned long flags;
3280         struct request *rq;
3281 
3282 #if IDETAPE_DEBUG_LOG
3283         printk ("Reached idetape_add_chrdev_write_request\n");
3284 #endif /* IDETAPE_DEBUG_LOG */
3285         
3286         
3287         new_stage=idetape_kmalloc_stage (drive);
3288 
3289         /*
3290          *      If we don't have a new stage, wait for more and more requests
3291          *      to finish, and try to allocate after each one.
3292          *
3293          *      Pay special attention to possible race conditions.
3294          */
3295 
3296         while (new_stage == NULL) {
3297                 save_flags (flags);cli ();
3298                 if (tape->active_data_request != NULL) {
3299                         idetape_wait_for_request (tape->active_data_request);
3300                         restore_flags (flags);
3301                         new_stage=idetape_kmalloc_stage (drive);
3302                 }
3303                 else {
3304                         /*
3305                          *      Linux is short on memory. Fallback to
3306                          *      non-pipelined operation mode for this request.
3307                          */
3308                         
3309                         restore_flags (flags);
3310                         return (idetape_queue_rw_tail (drive,IDETAPE_WRITE_REQUEST,blocks,buffer));
3311                 }
3312         }
3313 
3314         rq=&(new_stage->rq);
3315 
3316         ide_init_drive_cmd (rq);
3317         rq->cmd = IDETAPE_WRITE_REQUEST;
3318         rq->sector = tape->block_address;       /* Doesn't actually matter - We always assume sequential access */
3319         rq->nr_sectors = blocks;
3320         rq->current_nr_sectors = blocks;
3321 
3322         idetape_copy_buffer_to_stage (new_stage,buffer);
3323 
3324         save_flags (flags);cli ();
3325         idetape_add_stage_tail (drive,new_stage);
3326         restore_flags (flags);
3327 
3328         /*
3329          *      Check if we are currently servicing requests in the bottom
3330          *      part of the driver.
3331          *
3332          *      If not, wait for the pipeline to be full enough (75%) before
3333          *      starting to service requests, so that we will be able to
3334          *      keep up with the higher speeds of the tape.
3335          */
3336 
3337         if (tape->active_data_request == NULL && tape->current_number_of_stages >= 0.75*tape->max_number_of_stages)
3338                 idetape_insert_pipeline_into_queue (drive);             
3339 
3340         if (tape->error_in_pipeline_stage) {            /* Return a deferred error */
3341                 tape->error_in_pipeline_stage=0;
3342                 return (-EIO);
3343         }
3344         
3345         return (blocks);
3346 }
3347 
3348 void idetape_discard_read_pipeline (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
3349 
3350 {
3351         idetape_tape_t *tape = &(drive->tape);
3352         unsigned long flags;
3353 
3354 #if IDETAPE_DEBUG_BUGS
3355         if (tape->chrdev_direction != idetape_direction_read) {
3356                 printk ("ide-tape: bug: Trying to discard read pipeline, but we are not reading.\n");
3357                 return;
3358         }
3359 #endif /* IDETAPE_DEBUG_BUGS */
3360 
3361         tape->merge_buffer_size=tape->merge_buffer_offset=0;
3362         tape->chrdev_direction=idetape_direction_none;
3363         
3364         if (tape->first_stage == NULL)
3365                 return;
3366                 
3367         save_flags (flags);cli ();
3368         tape->next_stage=NULL;
3369         if (tape->active_data_request != NULL)
3370                 idetape_wait_for_request (tape->active_data_request);
3371         restore_flags (flags);
3372 
3373         while (tape->first_stage != NULL)
3374                 idetape_remove_stage_head (drive);
3375 
3376 #if IDETAPE_PIPELINE
3377         tape->max_number_of_stages=IDETAPE_MIN_PIPELINE_STAGES;
3378 #else
3379         tape->max_number_of_stages=0;
3380 #endif /* IDETAPE_PIPELINE */
3381 }
3382 
3383 /*
3384  *      idetape_wait_for_pipeline will wait until all pending pipeline
3385  *      requests are serviced. Typically called on device close.
3386  */
3387  
3388 void idetape_wait_for_pipeline (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
3389 
3390 {
3391         idetape_tape_t *tape = &(drive->tape);
3392         unsigned long flags;
3393 
3394         if (tape->active_data_request == NULL)
3395                 idetape_insert_pipeline_into_queue (drive);             
3396 
3397         save_flags (flags);cli ();
3398         if (tape->active_data_request == NULL) {
3399                 restore_flags (flags);
3400                 return;
3401         }
3402         
3403         if (tape->last_stage != NULL)
3404                 idetape_wait_for_request (&(tape->last_stage->rq));
3405 
3406         else if (tape->active_data_request != NULL)
3407                 idetape_wait_for_request (tape->active_data_request);
3408         restore_flags (flags);
3409 }
3410 
3411 void idetape_empty_write_pipeline (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
3412 
3413 {
3414         idetape_tape_t *tape = &(drive->tape);
3415         int blocks;
3416         
3417 #if IDETAPE_DEBUG_BUGS
3418         if (tape->chrdev_direction != idetape_direction_write) {
3419                 printk ("ide-tape: bug: Trying to empty write pipeline, but we are not writing.\n");
3420                 return;
3421         }
3422         if (tape->merge_buffer_size > tape->data_buffer_size) {
3423                 printk ("ide-tape: bug: merge_buffer too big\n");
3424                 tape->merge_buffer_size = tape->data_buffer_size;
3425         }
3426 #endif /* IDETAPE_DEBUG_BUGS */
3427 
3428         if (tape->merge_buffer_size) {
3429                 blocks=tape->merge_buffer_size/tape->tape_block_size;
3430                 if (tape->merge_buffer_size % tape->tape_block_size) {
3431                         blocks++;
3432                         memset (tape->merge_buffer+tape->merge_buffer_size,0,tape->data_buffer_size-tape->merge_buffer_size);
3433                 }
3434                 (void) idetape_add_chrdev_write_request (drive,blocks,tape->merge_buffer);
3435                 tape->merge_buffer_size=0;
3436         }
3437         
3438         idetape_wait_for_pipeline (drive);
3439 
3440         tape->error_in_pipeline_stage=0;
3441         tape->chrdev_direction=idetape_direction_none;
3442 
3443         /*
3444          *      On the next backup, perform the feedback loop again.
3445          *      (I don't want to keep sense information between backups,
3446          *       as some systems are constantly on, and the system load
3447          *       can be totally different on the next backup).
3448          */
3449 
3450 #if IDETAPE_PIPELINE
3451         tape->max_number_of_stages=IDETAPE_MIN_PIPELINE_STAGES;
3452 #else
3453         tape->max_number_of_stages=0;
3454 #endif /* IDETAPE_PIPELINE */
3455 #if IDETAPE_DEBUG_BUGS
3456         if (tape->first_stage != NULL || tape->next_stage != NULL || tape->last_stage != NULL || tape->current_number_of_stages != 0) {
3457                 printk ("ide-tape: ide-tape pipeline bug\n");           
3458         }
3459 #endif /* IDETAPE_DEBUG_BUGS */
3460 }
3461 
3462 /*
3463  *      idetape_zero_packet_command just zeros a packet command and
3464  *      sets the number of retries to 0, as we haven't retried it yet.
3465  */
3466  
3467 void idetape_zero_packet_command (idetape_packet_command_t *pc)
     /* [previous][next][first][last][top][bottom][index][help] */
3468 
3469 {
3470         int i;
3471         
3472         for (i=0;i<12;i++)
3473                 pc->c[i]=0;
3474         pc->retries=0;
3475         pc->abort=0;
3476         pc->dma_recommended=0;
3477         pc->dma_error=0;
3478 }
3479 
3480 /*
3481  *      idetape_swap_shorts converts a 16 bit number from little endian
3482  *      to big endian format.
3483  */
3484  
3485 unsigned short idetape_swap_short (unsigned short temp)
     /* [previous][next][first][last][top][bottom][index][help] */
3486 
3487 {
3488         union convert {
3489                 unsigned all    :16;
3490                 struct {
3491                         unsigned b1     :8;
3492                         unsigned b2     :8;
3493                 } b;
3494         } original,converted;
3495         
3496         original.all=temp;
3497         converted.b.b1=original.b.b2;
3498         converted.b.b2=original.b.b1;
3499         return (converted.all);
3500 }
3501 
3502 /*
3503  *      idetape_swap_long converts from little endian to big endian format.
3504  */
3505  
3506 unsigned long idetape_swap_long (unsigned long temp)
     /* [previous][next][first][last][top][bottom][index][help] */
3507 
3508 {
3509         union convert {
3510                 unsigned all    :32;
3511                 struct {
3512                         unsigned b1     :8;
3513                         unsigned b2     :8;
3514                         unsigned b3     :8;
3515                         unsigned b4     :8;
3516                 } b;
3517         } original,converted;
3518         
3519         original.all=temp;
3520         converted.b.b1=original.b.b4;
3521         converted.b.b2=original.b.b3;
3522         converted.b.b3=original.b.b2;
3523         converted.b.b4=original.b.b1;
3524         return (converted.all);
3525 }
3526 
3527 
3528 /*
3529  *      idetape_next_pc_storage returns a pointer to a place in which we can
3530  *      safely store a packet command, even though we intend to leave the
3531  *      driver. A storage space for a maximum of IDETAPE_PC_STACK packet
3532  *      commands is allocated at initialization time.
3533  */
3534  
3535 idetape_packet_command_t *idetape_next_pc_storage (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
3536 
3537 {
3538         idetape_tape_t *tape;
3539         
3540         tape=&(drive->tape);
3541 #if IDETAPE_DEBUG_LOG
3542         printk ("ide-tape: pc_stack_index=%d\n",tape->pc_stack_index);
3543 #endif /* IDETAPE_DEBUG_LOG */
3544         if (tape->pc_stack_index==IDETAPE_PC_STACK)
3545                 tape->pc_stack_index=0;
3546         return (&(tape->pc_stack [tape->pc_stack_index++]));
3547 }
3548 
3549 /*
3550  *      idetape_next_rq_storage is used along with idetape_next_pc_storage.
3551  *      Since we queue packet commands in the request queue, we need to
3552  *      allocate a request, along with the allocation of a packet command.
3553  */
3554  
3555 /**************************************************************
3556  *                                                            *
3557  *  This should get fixed to use kmalloc(GFP_ATOMIC, ..)      *
3558  *  followed later on by kfree().   -ml                       *
3559  *                                                            *
3560  **************************************************************/
3561  
3562 struct request *idetape_next_rq_storage (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
3563 
3564 {
3565         idetape_tape_t *tape;
3566         
3567         tape=&(drive->tape);
3568 
3569 #if IDETAPE_DEBUG_LOG
3570         printk ("ide-tape: rq_stack_index=%d\n",tape->rq_stack_index);
3571 #endif /* IDETAPE_DEBUG_LOG */
3572         if (tape->rq_stack_index==IDETAPE_PC_STACK)
3573                 tape->rq_stack_index=0;
3574         return (&(tape->rq_stack [tape->rq_stack_index++]));
3575 }
3576 
3577 /*
3578  *      Block device interface functions
3579  *
3580  *      The block device interface should not be used for data transfers.
3581  *      However, we still allow opening it so that we can issue general
3582  *      ide driver configuration ioctl's, such as the interrupt unmask feature.
3583  */
3584 
3585 int idetape_blkdev_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
3586 
3587 {
3588         idetape_tape_t *tape=&(drive->tape);
3589         unsigned long flags;
3590                         
3591         save_flags (flags);cli ();
3592 
3593 #if IDETAPE_DEBUG_LOG
3594         printk ("Reached idetape_blkdev_open\n");
3595 #endif /* IDETAPE_DEBUG_LOG */
3596 
3597         if (tape->busy) {
3598                 restore_flags (flags);          /* Allowing access only through one */
3599                 return (-EBUSY);                /* one file descriptor */
3600         }
3601 
3602         tape->busy=1;
3603         restore_flags (flags);
3604 
3605         return (0);
3606 }
3607 
3608 void idetape_blkdev_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
3609 
3610 {
3611         idetape_tape_t *tape=&(drive->tape);
3612         unsigned long flags;
3613                         
3614 #if IDETAPE_DEBUG_LOG
3615         printk ("Reached idetape_blkdev_release\n");
3616 #endif /* IDETAPE_DEBUG_LOG */
3617 
3618         save_flags (flags);cli ();
3619         tape->busy=0;
3620         restore_flags (flags);
3621 
3622         return;
3623 }
3624 
3625 /*
3626  *      Character device interface functions
3627  */
3628 
3629 /*
3630  *      Our character device read / write functions.
3631  *
3632  *      The tape is optimized to maximize throughput when it is transferring
3633  *      an integral number of the "continuous transfer limit", which is
3634  *      a parameter of the specific tape (26 KB on my particular tape).
3635  *
3636  *      For best results use an integral number of the tape's parameter
3637  *      (which is displayed in the driver installation stage and is returned
3638  *       by the MTIOCGET ioctl).
3639  *
3640  *      As of version 1.3 of the driver, the character device provides an
3641  *      abstract continuous view of the media - any mix of block sizes (even 1
3642  *      byte) on the same backup/restore procedure is supported. The driver
3643  *      will internally convert the requests to the recommended transfer unit,
3644  *      so that an unmatch between the user's block size to the recommended
3645  *      size will only result in a (slightly) increased driver overhead, but
3646  *      will no longer hit performance.
3647  */
3648 
3649 int idetape_chrdev_read (struct inode *inode, struct file *file, char *buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
3650 
3651 {
3652         ide_drive_t *drive=idetape_chrdev.drive;
3653         idetape_tape_t *tape=&(drive->tape);
3654         char *buf_ptr=buf;
3655         int bytes_read,temp,actually_read=0;
3656 
3657 #if IDETAPE_DEBUG_LOG
3658         printk ("Reached idetape_chrdev_read\n");
3659 #endif /* IDETAPE_DEBUG_LOG */
3660 
3661         if (tape->chrdev_direction != idetape_direction_read) {         /* Initialize read operation */
3662                 if (tape->chrdev_direction == idetape_direction_write) {
3663                         idetape_empty_write_pipeline (drive);
3664                         idetape_flush_tape_buffers (drive);
3665                 }
3666                 
3667                 /*
3668                  *      Issue a read 0 command to ensure that DSC handshake
3669                  *      is switched from completion mode to buffer available
3670                  *      mode.
3671                  */
3672                  
3673                 bytes_read=idetape_queue_rw_tail (drive,IDETAPE_READ_REQUEST,0,tape->merge_buffer);
3674                 if (bytes_read < 0)
3675                         return (bytes_read);
3676 
3677                 tape->chrdev_direction=idetape_direction_read;
3678         }
3679         
3680         if (count==0)
3681                 return (0);
3682 
3683         if (tape->merge_buffer_size) {
3684 #if IDETAPE_DEBUG_BUGS
3685                 if (tape->merge_buffer_offset+tape->merge_buffer_size > tape->data_buffer_size) {
3686                         printk ("ide-tape: bug: merge buffer too big\n");
3687                         tape->merge_buffer_offset=0;tape->merge_buffer_size=tape->data_buffer_size-1;
3688                 }
3689 #endif /* IDETAPE_DEBUG_BUGS */
3690                 actually_read=IDETAPE_MIN (tape->merge_buffer_size,count);
3691                 memcpy_tofs (buf_ptr,tape->merge_buffer+tape->merge_buffer_offset,actually_read);
3692                 buf_ptr+=actually_read;tape->merge_buffer_size-=actually_read;
3693                 count-=actually_read;tape->merge_buffer_offset+=actually_read;
3694         }
3695 
3696         while (count >= tape->data_buffer_size) {
3697                 bytes_read=idetape_add_chrdev_read_request (drive,tape->capabilities.ctl,tape->merge_buffer);
3698                 if (bytes_read <= 0)
3699                         return (actually_read);
3700                 memcpy_tofs (buf_ptr,tape->merge_buffer,bytes_read);
3701                 buf_ptr+=bytes_read;count-=bytes_read;actually_read+=bytes_read;
3702         }
3703 
3704         if (count) {
3705                 bytes_read=idetape_add_chrdev_read_request (drive,tape->capabilities.ctl,tape->merge_buffer);
3706                 if (bytes_read <= 0)
3707                         return (actually_read);
3708                 temp=IDETAPE_MIN (count,bytes_read);
3709                 memcpy_tofs (buf_ptr,tape->merge_buffer,temp);
3710                 actually_read+=temp;
3711                 tape->merge_buffer_offset=temp;
3712                 tape->merge_buffer_size=bytes_read-temp;
3713         }
3714         return (actually_read);
3715 }
3716  
3717 int idetape_chrdev_write (struct inode *inode, struct file *file, const char *buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
3718 
3719 {
3720         ide_drive_t *drive=idetape_chrdev.drive;
3721         idetape_tape_t *tape=&(drive->tape);
3722         const char *buf_ptr=buf;
3723         int retval,actually_written=0;
3724 
3725 #if IDETAPE_DEBUG_LOG
3726         printk ("Reached idetape_chrdev_write\n");
3727 #endif /* IDETAPE_DEBUG_LOG */
3728 
3729         if (tape->chrdev_direction != idetape_direction_write) {        /* Initialize write operation */
3730                 if (tape->chrdev_direction == idetape_direction_read)
3731                         idetape_discard_read_pipeline (drive);
3732 
3733                 /*
3734                  *      Issue a write 0 command to ensure that DSC handshake
3735                  *      is switched from completion mode to buffer available
3736                  *      mode.
3737                  */
3738 
3739                 retval=idetape_queue_rw_tail (drive,IDETAPE_WRITE_REQUEST,0,tape->merge_buffer);
3740                 if (retval < 0)
3741                         return (retval);                
3742 
3743                 tape->chrdev_direction=idetape_direction_write;
3744         }
3745 
3746         if (count==0)
3747                 return (0);
3748 
3749         if (tape->merge_buffer_size) {
3750 #if IDETAPE_DEBUG_BUGS
3751                 if (tape->merge_buffer_size >= tape->data_buffer_size) {
3752                         printk ("ide-tape: bug: merge buffer too big\n");
3753                         tape->merge_buffer_size=0;
3754                 }
3755 #endif /* IDETAPE_DEBUG_BUGS */
3756 
3757                 actually_written=IDETAPE_MIN (tape->data_buffer_size-tape->merge_buffer_size,count);
3758                 memcpy_fromfs (tape->merge_buffer+tape->merge_buffer_size,buf_ptr,actually_written);
3759                 buf_ptr+=actually_written;tape->merge_buffer_size+=actually_written;count-=actually_written;
3760 
3761                 if (tape->merge_buffer_size == tape->data_buffer_size) {
3762                         tape->merge_buffer_size=0;
3763                         retval=idetape_add_chrdev_write_request (drive,tape->capabilities.ctl,tape->merge_buffer);
3764                         if (retval <= 0)
3765                                 return (retval);
3766                 }
3767         }
3768 
3769         while (count >= tape->data_buffer_size) {
3770                 memcpy_fromfs (tape->merge_buffer,buf_ptr,tape->data_buffer_size);
3771                 buf_ptr+=tape->data_buffer_size;count-=tape->data_buffer_size;
3772                 retval=idetape_add_chrdev_write_request (drive,tape->capabilities.ctl,tape->merge_buffer);
3773                 actually_written+=tape->data_buffer_size;
3774                 if (retval <= 0)
3775                         return (retval);
3776         }
3777 
3778         if (count) {
3779                 actually_written+=count;
3780                 memcpy_fromfs (tape->merge_buffer,buf_ptr,count);
3781                 tape->merge_buffer_size+=count;
3782         }
3783         return (actually_written);
3784 }
3785 
3786 /*
3787  *      Our character device ioctls.
3788  *
3789  *      General mtio.h magnetic io commands are supported here, and not in
3790  *      the corresponding block interface.
3791  *
3792  *      The following ioctls are supported:
3793  *
3794  *      MTIOCTOP -      Refer to idetape_mtioctop for detailed description.
3795  *
3796  *      MTIOCGET -      The mt_dsreg field in the returned mtget structure
3797  *                      will be set to (recommended block size <<
3798  *                      MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK, which
3799  *                      is currently equal to the size itself.
3800  *                      The other mtget fields are not supported.
3801  *
3802  *                      Note that we do not actually return the tape's
3803  *                      block size. Rather, we provide the recommended
3804  *                      number of bytes which should be used as a "user
3805  *                      block size" with the character device read/write
3806  *                      functions to maximize throughput.
3807  *
3808  *      MTIOCPOS -      The current tape "position" is returned.
3809  *                      (A unique number which can be used with the MTSEEK
3810  *                       operation to return to this position in some
3811  *                       future time, provided this place was not overwritten
3812  *                       meanwhile).
3813  *
3814  *      Our own ide-tape ioctls are supported on both interfaces.
3815  */
3816 
3817 int idetape_chrdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
3818 
3819 {
3820         ide_drive_t *drive=idetape_chrdev.drive;
3821         idetape_tape_t *tape=&(drive->tape);
3822         idetape_packet_command_t pc;
3823         struct mtop mtop;
3824         struct mtget mtget;
3825         struct mtpos mtpos;
3826         int retval;
3827 
3828 #if IDETAPE_DEBUG_LOG
3829         printk ("Reached idetape_chrdev_ioctl, cmd=%u\n",cmd);
3830 #endif /* IDETAPE_DEBUG_LOG */
3831 
3832         if (tape->chrdev_direction == idetape_direction_write) {
3833                 idetape_empty_write_pipeline (drive);
3834                 idetape_flush_tape_buffers (drive);
3835         }
3836 
3837         if (tape->chrdev_direction == idetape_direction_read && cmd != MTIOCTOP)
3838                 idetape_discard_read_pipeline (drive);
3839         
3840         pc.buffer=pc.temp_buffer;
3841         pc.buffer_size=IDETAPE_TEMP_BUFFER_SIZE;
3842         pc.current_position=pc.temp_buffer;
3843 
3844         switch (cmd) {
3845                 case MTIOCTOP:
3846                         retval=verify_area (VERIFY_READ,(char *) arg,sizeof (struct mtop));
3847                         if (retval) return (retval);
3848                         memcpy_fromfs ((char *) &mtop, (char *) arg, sizeof (struct mtop));
3849                         return (idetape_mtioctop (drive,mtop.mt_op,mtop.mt_count));
3850                 case MTIOCGET:
3851                         mtget.mt_dsreg=(tape->data_buffer_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
3852                         retval=verify_area (VERIFY_WRITE,(char *) arg,sizeof (struct mtget));
3853                         if (retval) return (retval);
3854                         memcpy_tofs ((char *) arg,(char *) &mtget, sizeof (struct mtget));
3855                         return (0);
3856                 case MTIOCPOS:
3857                         idetape_create_read_position_cmd (&pc);
3858                         retval=idetape_queue_pc_tail (drive,&pc);
3859                         if (retval) return (retval);
3860                         mtpos.mt_blkno=tape->block_address;
3861                         retval=verify_area (VERIFY_WRITE,(char *) arg,sizeof (struct mtpos));
3862                         if (retval) return (retval);
3863                         memcpy_tofs ((char *) arg,(char *) &mtpos, sizeof (struct mtpos));
3864                         return (0);
3865                 default:
3866                         return (idetape_blkdev_ioctl (drive,inode,file,cmd,arg));
3867         }
3868 }
3869 
3870 /*
3871  *      idetape_mtioctop is called from idetape_chrdev_ioctl when
3872  *      the general mtio MTIOCTOP ioctl is requested.
3873  *
3874  *      We currently support the following mtio.h operations:
3875  *
3876  *      MTFSF   -       Space over mt_count filemarks in the positive direction.
3877  *                      The tape is positioned after the last spaced filemark.
3878  *
3879  *      MTFSFM  -       Same as MTFSF, but the tape is positioned before the
3880  *                      last filemark.
3881  *
3882  *      MTBSF   -       Steps background over mt_count filemarks, tape is
3883  *                      positioned before the last filemark.
3884  *
3885  *      MTBSFM  -       Like MTBSF, only tape is positioned after the last filemark.
3886  *
3887  *
3888  *      Note:
3889  *
3890  *              MTBSF and MTBSFM are not supported when the tape doesn't
3891  *              supports spacing over filemarks in the reverse direction.
3892  *              In this case, MTFSFM is also usually not supported (it is
3893  *              supported in the rare case in which we crossed the filemark
3894  *              during our read-ahead pipelined operation mode).
3895  *              
3896  *      MTWEOF  -       Writes mt_count filemarks. Tape is positioned after
3897  *                      the last written filemark.
3898  *
3899  *      MTREW   -       Rewinds tape.
3900  *
3901  *      MTOFFL  -       Puts the tape drive "Offline": Rewinds the tape and
3902  *                      prevents further access until the media is replaced.
3903  *
3904  *      MTNOP   -       Flushes tape buffers.
3905  *
3906  *      MTRETEN -       Retension media. This typically consists of one end
3907  *                      to end pass on the media.
3908  *
3909  *      MTEOM   -       Moves to the end of recorded data.
3910  *
3911  *      MTERASE -       Erases tape.
3912  *
3913  *      MTSEEK  -       Positions the tape in a specific block number, which
3914  *                      was previously received using the MTIOCPOS ioctl,
3915  *                      assuming this place was not overwritten meanwhile.
3916  *
3917  *      The following commands are currently not supported:
3918  *
3919  *      MTFSR, MTBSR, MTFSS, MTBSS, MTWSM, MTSETBLK, MTSETDENSITY,
3920  *      MTSETDRVBUFFER, MT_ST_BOOLEANS, MT_ST_WRITE_THRESHOLD.
3921  */
3922  
3923 int idetape_mtioctop (ide_drive_t *drive,short mt_op,int mt_count)
     /* [previous][next][first][last][top][bottom][index][help] */
3924 
3925 {
3926         idetape_tape_t *tape=&(drive->tape);
3927         idetape_packet_command_t pc;
3928         int i,retval;
3929 
3930         pc.buffer=pc.temp_buffer;
3931         pc.buffer_size=IDETAPE_TEMP_BUFFER_SIZE;
3932         pc.current_position=pc.temp_buffer;
3933 
3934 #if IDETAPE_DEBUG_LOG
3935         printk ("Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n",mt_op,mt_count);
3936 #endif /* IDETAPE_DEBUG_LOG */
3937 
3938         /*
3939          *      Commands which need our pipelined read-ahead stages.
3940          */
3941 
3942         switch (mt_op) {
3943                 case MTFSF:
3944                 case MTFSFM:
3945                 case MTBSF:
3946                 case MTBSFM:
3947                         if (!mt_count)
3948                                 return (0);
3949                         return (idetape_space_over_filemarks (drive,mt_op,mt_count));
3950                 default:
3951                         break;
3952         }
3953 
3954         /*
3955          *      Empty the pipeline.
3956          */
3957 
3958         if (tape->chrdev_direction == idetape_direction_read)
3959                 idetape_discard_read_pipeline (drive);
3960 
3961         switch (mt_op) {
3962                 case MTWEOF:
3963                         for (i=0;i<mt_count;i++) {
3964                                 idetape_create_write_filemark_cmd (&pc,1);
3965                                 retval=idetape_queue_pc_tail (drive,&pc);
3966                                 if (retval) return (retval);
3967                         }
3968                         return (0);
3969                 case MTREW:
3970                         return (idetape_rewind_tape (drive));
3971                 case MTOFFL:
3972                         idetape_create_load_unload_cmd (&pc,!IDETAPE_LU_LOAD_MASK);
3973                         return (idetape_queue_pc_tail (drive,&pc));
3974                 case MTNOP:
3975                         return (idetape_flush_tape_buffers (drive));
3976                 case MTRETEN:
3977                         idetape_create_load_unload_cmd (&pc,IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
3978                         return (idetape_queue_pc_tail (drive,&pc));
3979                 case MTEOM:
3980                         idetape_create_space_cmd (&pc,0,IDETAPE_SPACE_TO_EOD);
3981                         return (idetape_queue_pc_tail (drive,&pc));
3982                 case MTERASE:
3983                         retval=idetape_rewind_tape (drive);
3984                         if (retval) return (retval);
3985                         idetape_create_erase_cmd (&pc);
3986                         return (idetape_queue_pc_tail (drive,&pc));
3987                 case MTSEEK:
3988                         return (idetape_position_tape (drive,mt_count));
3989                 default:
3990                         printk ("ide-tape: MTIO operation %d not supported\n",mt_op);
3991                         return (-EIO);
3992         }
3993 }
3994 
3995 /*
3996  *      idetape_space_over_filemarks is now a bit more complicated than just
3997  *      passing the command to the tape since we may have crossed some
3998  *      filemarks during our pipelined read-ahead mode.
3999  *
4000  *      As a minor side effect, the pipeline enables us to support MTFSFM when
4001  *      the filemark is in our internal pipeline even if the tape doesn't
4002  *      support spacing over filemarks in the reverse direction.
4003  */
4004  
4005 int idetape_space_over_filemarks (ide_drive_t *drive,short mt_op,int mt_count)
     /* [previous][next][first][last][top][bottom][index][help] */
4006 
4007 {
4008         idetape_tape_t *tape=&(drive->tape);
4009         idetape_packet_command_t pc;
4010         unsigned long flags;
4011         int retval,count=0,errors;
4012 
4013         if (tape->chrdev_direction == idetape_direction_read) {
4014 
4015                 /*
4016                  *      We have a read-ahead buffer. Scan it for crossed
4017                  *      filemarks.
4018                  */
4019 
4020                 tape->merge_buffer_size=tape->merge_buffer_offset=0;
4021                 while (tape->first_stage != NULL) {
4022                         
4023                         /*
4024                          *      Wait until the first read-ahead request
4025                          *      is serviced.
4026                          */
4027                 
4028                         save_flags (flags);cli ();
4029                         if (tape->active_data_request == &(tape->first_stage->rq))
4030                                 idetape_wait_for_request (tape->active_data_request);
4031                         restore_flags (flags);
4032 
4033                         errors=tape->first_stage->rq.errors;
4034                         if (errors == IDETAPE_RQ_ERROR_FILEMARK)
4035                                 count++;
4036 
4037                         if (count == mt_count) {
4038                                 switch (mt_op) {
4039                                         case MTFSF:
4040                                                 idetape_remove_stage_head (drive);
4041                                         case MTFSFM:
4042                                                 return (0);
4043                                 }
4044                         }
4045                         idetape_remove_stage_head (drive);
4046                 }
4047                 idetape_discard_read_pipeline (drive);
4048         }
4049 
4050         /*
4051          *      The filemark was not found in our internal pipeline.
4052          *      Now we can issue the space command.
4053          */
4054 
4055         pc.buffer=pc.temp_buffer;
4056         pc.buffer_size=IDETAPE_TEMP_BUFFER_SIZE;
4057         pc.current_position=pc.temp_buffer;
4058 
4059         switch (mt_op) {
4060                 case MTFSF:
4061                         idetape_create_space_cmd (&pc,mt_count-count,IDETAPE_SPACE_OVER_FILEMARK);
4062                         return (idetape_queue_pc_tail (drive,&pc));
4063                 case MTFSFM:
4064                         if (!tape->capabilities.sprev)
4065                                 return (-EIO);
4066                         retval=idetape_mtioctop (drive,MTFSF,mt_count-count);
4067                         if (retval) return (retval);
4068                         return (idetape_mtioctop (drive,MTBSF,1));
4069                 case MTBSF:
4070                         if (!tape->capabilities.sprev)
4071                                 return (-EIO);
4072                         idetape_create_space_cmd (&pc,-(mt_count+count),IDETAPE_SPACE_OVER_FILEMARK);
4073                         return (idetape_queue_pc_tail (drive,&pc));
4074                 case MTBSFM:
4075                         if (!tape->capabilities.sprev)
4076                                 return (-EIO);
4077                         retval=idetape_mtioctop (drive,MTBSF,mt_count+count);
4078                         if (retval) return (retval);
4079                         return (idetape_mtioctop (drive,MTFSF,1));
4080                 default:
4081                         printk ("ide-tape: MTIO operation %d not supported\n",mt_op);
4082                         return (-EIO);
4083         }
4084 }
4085 
4086 /*
4087  *      Our character device open function.
4088  */
4089 
4090 int idetape_chrdev_open (struct inode *inode, struct file *filp)
     /* [previous][next][first][last][top][bottom][index][help] */
4091 
4092 {
4093         ide_drive_t *drive=idetape_chrdev.drive;
4094         idetape_tape_t *tape=&(drive->tape);
4095         unsigned long flags;
4096         unsigned int minor=MINOR (inode->i_rdev),allocation_length;
4097                         
4098         save_flags (flags);cli ();
4099 
4100 #if IDETAPE_DEBUG_LOG
4101         printk ("Reached idetape_chrdev_open\n");
4102 #endif /* IDETAPE_DEBUG_LOG */
4103 
4104         if (minor!=0 && minor!=128) {           /* Currently supporting only one */
4105                 restore_flags (flags);          /* tape drive */
4106                 return (-ENXIO);
4107         }
4108 
4109         if (tape->busy) {
4110                 restore_flags (flags);          /* Allowing access only through one */
4111                 return (-EBUSY);                /* one file descriptor */
4112         }
4113 
4114         tape->busy=1;
4115         restore_flags (flags);
4116 
4117         allocation_length=tape->data_buffer_size;
4118         if (tape->data_buffer_size % IDETAPE_ALLOCATION_BLOCK)
4119                 allocation_length+=IDETAPE_ALLOCATION_BLOCK;
4120 
4121 #if IDETAPE_MINIMIZE_IDLE_MEMORY_USAGE
4122         if (tape->data_buffer == NULL)
4123                 tape->data_buffer=kmalloc (allocation_length,GFP_KERNEL);
4124         if (tape->data_buffer == NULL)
4125                 goto sorry;
4126         if (tape->merge_buffer == NULL)
4127                 tape->merge_buffer=kmalloc (allocation_length,GFP_KERNEL);
4128         if (tape->merge_buffer == NULL) {
4129                 kfree (tape->data_buffer);
4130         sorry:
4131                 printk ("ide-tape: FATAL - Can not allocate continuous buffer of %d bytes\n",allocation_length);
4132                 tape->busy=0;
4133                 return (-EIO);
4134         }
4135 #endif /* IDETAPE_MINIMIZE_IDLE_MEMORY_USAGE */
4136 
4137         if (!tape->block_address_valid) {
4138                 if (idetape_rewind_tape (drive)) {
4139                         printk ("ide-tape: Rewinding tape failed\n");
4140                         tape->busy=0;
4141                         return (-EIO);
4142                 }
4143         }
4144 
4145         return (0);
4146 }
4147 
4148 /*
4149  *      Our character device release function.
4150  */
4151 
4152 void idetape_chrdev_release (struct inode *inode, struct file *filp)
     /* [previous][next][first][last][top][bottom][index][help] */
4153 
4154 {
4155         ide_drive_t *drive=idetape_chrdev.drive;
4156         idetape_tape_t *tape=&(drive->tape);
4157         unsigned int minor=MINOR (inode->i_rdev);
4158         idetape_packet_command_t pc;
4159         unsigned long flags;
4160                         
4161 #if IDETAPE_DEBUG_LOG
4162         printk ("Reached idetape_chrdev_release\n");
4163 #endif /* IDETAPE_DEBUG_LOG */
4164 
4165         if (tape->chrdev_direction == idetape_direction_write) {
4166                 idetape_empty_write_pipeline (drive);
4167                 idetape_create_write_filemark_cmd (&pc,1);      /* Write a filemark */
4168                 if (idetape_queue_pc_tail (drive,&pc))
4169                         printk ("ide-tape: Couldn't write a filemark\n");
4170         }
4171         
4172         if (tape->chrdev_direction == idetape_direction_read) {
4173                 if (minor < 128)
4174                         idetape_discard_read_pipeline (drive);
4175                 else
4176                         idetape_wait_for_pipeline (drive);
4177         }
4178         
4179         if (minor < 128)
4180                 if (idetape_rewind_tape (drive))
4181                         printk ("ide-tape: Rewinding tape failed\n");
4182 
4183 #if IDETAPE_MINIMIZE_IDLE_MEMORY_USAGE
4184         kfree (tape->data_buffer);
4185         tape->data_buffer=NULL;
4186         if (!tape->merge_buffer_size) {
4187                 kfree (tape->merge_buffer);
4188                 tape->merge_buffer=NULL;
4189         }
4190 #endif /* IDETAPE_MINIMIZE_IDLE_MEMORY_USAGE */
4191 
4192         save_flags (flags);cli ();
4193         tape->busy=0;
4194         restore_flags (flags);
4195 
4196         return;
4197 }
4198 
4199 /*
4200  *      idetape_position_tape positions the tape to the requested block
4201  *      using the LOCATE packet command. A READ POSITION command is then
4202  *      issued to check where we are positioned.
4203  *
4204  *      Like all higher level operations, we queue the commands at the tail
4205  *      of the request queue and wait for their completion.
4206  *      
4207  */
4208  
4209 int idetape_position_tape (ide_drive_t *drive,unsigned long block)
     /* [previous][next][first][last][top][bottom][index][help] */
4210 
4211 {
4212         int retval;
4213         idetape_packet_command_t pc;
4214 
4215         idetape_create_locate_cmd (&pc,block,0);
4216         retval=idetape_queue_pc_tail (drive,&pc);
4217         if (retval!=0) return (retval);
4218                         
4219         idetape_create_read_position_cmd (&pc);
4220         pc.buffer=pc.temp_buffer;
4221         pc.buffer_size=IDETAPE_TEMP_BUFFER_SIZE;
4222         pc.current_position=pc.temp_buffer;
4223         return (idetape_queue_pc_tail (drive,&pc));
4224 }
4225 
4226 /*
4227  *      Rewinds the tape to the Beginning Of the current Partition (BOP).
4228  *
4229  *      We currently support only one partition.
4230  */ 
4231 
4232 int idetape_rewind_tape (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
4233 
4234 {
4235         int retval;
4236         idetape_packet_command_t pc;
4237 #if IDETAPE_DEBUG_LOG
4238         printk ("Reached idetape_rewind_tape\n");
4239 #endif /* IDETAPE_DEBUG_LOG */  
4240         
4241         idetape_create_rewind_cmd (&pc);
4242         retval=idetape_queue_pc_tail (drive,&pc);
4243         if (retval) return (retval);
4244                         
4245         idetape_create_read_position_cmd (&pc);
4246         pc.buffer=pc.temp_buffer;
4247         pc.buffer_size=IDETAPE_TEMP_BUFFER_SIZE;
4248         pc.current_position=pc.temp_buffer;
4249         return (idetape_queue_pc_tail (drive,&pc));
4250 }
4251 
4252 int idetape_flush_tape_buffers (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
4253 
4254 {
4255         idetape_packet_command_t pc;
4256 
4257         idetape_create_write_filemark_cmd (&pc,0);
4258         return (idetape_queue_pc_tail (drive,&pc));
4259 }
4260 
4261 /*
4262  *      Pipeline related functions
4263  */
4264 
4265 /*
4266  *      idetape_kmalloc_stage uses kmalloc to allocate a pipeline stage,
4267  *      along with all the necessary small buffers which together make
4268  *      a buffer of size tape->data_buffer_size or a bit more, in case
4269  *      it is not a multiply of IDETAPE_ALLOCATION_BLOCK (it isn't ...).
4270  *
4271  *      Returns a pointer to the new allocated stage, or NULL if we
4272  *      can't (or don't want to, in case we already have too many stages)
4273  *      allocate a stage.
4274  *
4275  *      Pipeline stages are optional and are used to increase performance.
4276  *      If we can't allocate them, we'll manage without them.
4277  */
4278  
4279 idetape_pipeline_stage_t *idetape_kmalloc_stage (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
4280 
4281 {
4282         idetape_tape_t *tape=&(drive->tape);
4283         idetape_pipeline_stage_t *new_stage;
4284         idetape_buffer_head_t *prev_bh,*bh;
4285         int buffers_num,i;
4286         
4287 #if IDETAPE_DEBUG_LOG
4288         printk ("Reached idetape_kmalloc_stage\n");
4289 #endif /* IDETAPE_DEBUG_LOG */
4290 
4291         if (tape->current_number_of_stages>=tape->max_number_of_stages) {
4292                 return (NULL);
4293         }
4294                 
4295         new_stage=(idetape_pipeline_stage_t *) kmalloc (sizeof (idetape_pipeline_stage_t),GFP_KERNEL);
4296         if (new_stage==NULL)
4297                 return (NULL);
4298                 
4299         new_stage->next=new_stage->prev=NULL;
4300 
4301         buffers_num=tape->data_buffer_size / IDETAPE_ALLOCATION_BLOCK;
4302         if (tape->data_buffer_size % IDETAPE_ALLOCATION_BLOCK)
4303                 buffers_num++;
4304 
4305         prev_bh=new_stage->bh=(idetape_buffer_head_t *) kmalloc (sizeof (idetape_buffer_head_t),GFP_KERNEL);
4306         if (new_stage->bh==NULL) {
4307                 idetape_kfree_stage (new_stage);
4308                 return (NULL);
4309         }
4310         new_stage->bh->next=NULL;
4311 
4312         new_stage->bh->data=kmalloc (IDETAPE_ALLOCATION_BLOCK,GFP_KERNEL);
4313         if (new_stage->bh->data==NULL) {
4314                 idetape_kfree_stage (new_stage);
4315                 return (NULL);
4316         }
4317         
4318         for (i=1;i<buffers_num;i++) {
4319                 bh=(idetape_buffer_head_t *) kmalloc (sizeof (idetape_buffer_head_t),GFP_KERNEL);
4320                 if (bh==NULL) {
4321                         idetape_kfree_stage (new_stage);
4322                         return (NULL);
4323                 }
4324                 bh->next=NULL;
4325                 prev_bh->next=bh;
4326                 bh->data=kmalloc (IDETAPE_ALLOCATION_BLOCK,GFP_KERNEL);
4327                 if (bh->data == NULL) {
4328                         idetape_kfree_stage (new_stage);
4329                         return (NULL);
4330                 }
4331                 prev_bh=bh;
4332         }
4333         return (new_stage);
4334 }
4335 
4336 /*
4337  *      idetape_kfree_stage calls kfree to completely free a stage, along with
4338  *      its related buffers.
4339  */
4340  
4341 void idetape_kfree_stage (idetape_pipeline_stage_t *stage)
     /* [previous][next][first][last][top][bottom][index][help] */
4342 
4343 {
4344         idetape_buffer_head_t *prev_bh,*bh;
4345         
4346         if (stage == NULL)
4347                 return;
4348 
4349 #if IDETAPE_DEBUG_LOG
4350         printk ("Reached idetape_kfree_stage\n");
4351 #endif /* IDETAPE_DEBUG_LOG */
4352         
4353         bh=stage->bh;
4354         
4355         while (bh != NULL) {
4356                 prev_bh=bh;
4357                 if (bh->data != NULL)
4358                         kfree (bh->data);
4359                 bh=bh->next;
4360                 kfree (prev_bh);
4361         }
4362         
4363         kfree (stage);
4364         return;
4365 }
4366 
4367 /*
4368  *      idetape_copy_buffer_from_stage and idetape_copy_buffer_to_stage
4369  *      copy data from/to the small buffers into/from a continuous buffer.
4370  */
4371   
4372 void idetape_copy_buffer_from_stage (idetape_pipeline_stage_t *stage,char *buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
4373 
4374 {
4375         idetape_buffer_head_t *bh;
4376         char *ptr;
4377 
4378 #if IDETAPE_DEBUG_LOG
4379         printk ("Reached idetape_copy_buffer_from_stage\n");
4380 #endif /* IDETAPE_DEBUG_LOG */
4381 #if IDETAPE_DEBUG_BUGS
4382         if (buffer == NULL) {
4383                 printk ("ide-tape: bug: buffer is null in copy_buffer_from_stage\n");
4384                 return;
4385         }
4386 #endif /* IDETAPE_DEBUG_BUGS */
4387         
4388         ptr=buffer;
4389         bh=stage->bh;
4390         
4391         while (bh != NULL) {
4392 #if IDETAPE_DEBUG_BUGS
4393                 if (bh->data == NULL) {
4394                         printk ("ide-tape: bug: bh->data is null\n");
4395                         return;
4396                 }
4397 #endif /* IDETAPE_DEBUG_BUGS */
4398                 memcpy (ptr,bh->data,IDETAPE_ALLOCATION_BLOCK);
4399                 bh=bh->next;
4400                 ptr=ptr+IDETAPE_ALLOCATION_BLOCK;
4401         }
4402         return;
4403 }
4404 
4405 /*
4406  *      Here we copy a continuous data buffer to the various small buffers
4407  *      in the pipeline stage.
4408  */
4409  
4410 void idetape_copy_buffer_to_stage (idetape_pipeline_stage_t *stage,char *buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
4411 
4412 {
4413         idetape_buffer_head_t *bh;
4414         char *ptr;
4415 
4416 #if IDETAPE_DEBUG_LOG
4417         printk ("Reached idetape_copy_buffer_to_stage\n");
4418 #endif /* IDETAPE_DEBUG_LOG */
4419 #if IDETAPE_DEBUG_BUGS
4420         if (buffer == NULL) {
4421                 printk ("ide-tape: bug: buffer is null in copy_buffer_to_stage\n");
4422                 return;
4423         }
4424 #endif /* IDETAPE_DEBUG_BUGS */
4425 
4426         ptr=buffer;
4427         bh=stage->bh;
4428         
4429         while (bh != NULL) {
4430 #if IDETAPE_DEBUG_BUGS
4431                 if (bh->data == NULL) {
4432                         printk ("ide-tape: bug: bh->data is null\n");
4433                         return;
4434                 }
4435 #endif /* IDETAPE_DEBUG_BUGS */
4436                 memcpy (bh->data,ptr,IDETAPE_ALLOCATION_BLOCK);
4437                 bh=bh->next;
4438                 ptr=ptr+IDETAPE_ALLOCATION_BLOCK;
4439         }
4440         return;
4441 }
4442 
4443 /*
4444  *      idetape_increase_max_pipeline_stages is a part of the feedback
4445  *      loop which tries to find the optimum number of stages. In the
4446  *      feedback loop, we are starting from a minimum maximum number of
4447  *      stages, and if we sense that the pipeline is empty, we try to
4448  *      increase it, until we reach the user compile time memory limit.
4449  */
4450 
4451 void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
4452 
4453 {
4454         idetape_tape_t *tape=&(drive->tape);
4455         
4456 #if IDETAPE_DEBUG_LOG
4457         printk ("Reached idetape_increase_max_pipeline_stages\n");
4458 #endif /* IDETAPE_DEBUG_LOG */
4459 
4460         tape->max_number_of_stages+=IDETAPE_INCREASE_STAGES_RATE*
4461                                         (IDETAPE_MAX_PIPELINE_STAGES-IDETAPE_MIN_PIPELINE_STAGES);
4462 
4463         if (tape->max_number_of_stages >= IDETAPE_MAX_PIPELINE_STAGES)
4464                 tape->max_number_of_stages = IDETAPE_MAX_PIPELINE_STAGES;
4465 
4466 #if IDETAPE_DEBUG_LOG
4467         printk ("Maximum number of stages: %d\n",tape->max_number_of_stages);
4468 #endif /* IDETAPE_DEBUG_LOG */
4469 
4470         return;
4471 }
4472 
4473 /*
4474  *      idetape_add_stage_tail adds a new stage at the end of the pipeline.
4475  *
4476  *      Caller should disable interrupts, if necessary.
4477  */
4478  
4479 void idetape_add_stage_tail (ide_drive_t *drive,idetape_pipeline_stage_t *stage)
     /* [previous][next][first][last][top][bottom][index][help] */
4480 
4481 {
4482         idetape_tape_t *tape=&(drive->tape);
4483         
4484 #if IDETAPE_DEBUG_LOG
4485                 printk ("Reached idetape_add_stage_tail\n");
4486 #endif /* IDETAPE_DEBUG_LOG */
4487 
4488         stage->next=NULL;
4489         stage->prev=tape->last_stage;
4490         if (tape->last_stage != NULL)
4491                 tape->last_stage->next=stage;
4492         else
4493                 tape->first_stage=tape->next_stage=stage;
4494         tape->last_stage=stage;
4495         if (tape->next_stage == NULL)
4496                 tape->next_stage=tape->last_stage;
4497         tape->current_number_of_stages++;
4498 }
4499 
4500 /*
4501  *      idetape_remove_stage_head removes tape->first_stage from the pipeline.
4502  *
4503  *      Again, caller should avoid race conditions.
4504  */
4505  
4506 void idetape_remove_stage_head (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
4507 
4508 {
4509         idetape_tape_t *tape=&(drive->tape);
4510         idetape_pipeline_stage_t *stage;
4511         
4512 #if IDETAPE_DEBUG_LOG
4513                 printk ("Reached idetape_remove_stage_head\n");
4514 #endif /* IDETAPE_DEBUG_LOG */
4515 #if IDETAPE_DEBUG_BUGS
4516         if (tape->first_stage == NULL) {
4517                 printk ("ide-tape: bug: tape->first_stage is NULL\n");
4518                 return;         
4519         }
4520         if (tape->active_stage == tape->first_stage) {
4521                 printk ("ide-tape: bug: Trying to free our active pipeline stage\n");
4522                 return;
4523         }
4524 #endif /* IDETAPE_DEBUG_BUGS */
4525         stage=tape->first_stage;
4526         tape->first_stage=stage->next;
4527         idetape_kfree_stage (stage);
4528         tape->current_number_of_stages--;
4529         if (tape->first_stage == NULL) {
4530                 tape->last_stage=NULL;
4531 #if IDETAPE_DEBUG_BUGS
4532                 if (tape->next_stage != NULL)
4533                         printk ("ide-tape: bug: tape->next_stage != NULL\n");
4534                 if (tape->current_number_of_stages)
4535                         printk ("ide-tape: bug: current_number_of_stages should be 0 now\n");
4536 #endif /* IDETAPE_DEBUG_BUGS */
4537         }
4538 }
4539 
4540 /*
4541  *      idetape_insert_pipeline_into_queue is used to start servicing the
4542  *      pipeline stages, starting from tape->next_stage.
4543  */
4544  
4545 void idetape_insert_pipeline_into_queue (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
4546 
4547 {
4548         idetape_tape_t *tape=&(drive->tape);
4549 
4550         if (tape->next_stage == NULL)
4551                 return;
4552 
4553         if (tape->active_data_request == NULL) {
4554                 idetape_active_next_stage (drive);
4555                 (void) (ide_do_drive_cmd (drive,tape->active_data_request,ide_end));
4556                 return;
4557         }
4558 }
4559 
4560 /*
4561  *      idetape_active_next_stage will declare the next stage as "active".
4562  */
4563  
4564 void idetape_active_next_stage (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
4565 
4566 {
4567         idetape_tape_t *tape=&(drive->tape);
4568         idetape_pipeline_stage_t *stage=tape->next_stage;
4569         struct request *rq=&(stage->rq);
4570 
4571 #if IDETAPE_DEBUG_LOG
4572         printk ("Reached idetape_active_next_stage\n");
4573 #endif /* IDETAPE_DEBUG_LOG */
4574 #if IDETAPE_DEBUG_BUGS
4575         if (stage == NULL) {
4576                 printk ("ide-tape: bug: Trying to activate a non existing stage\n");
4577                 return;
4578         }
4579 #endif /* IDETAPE_DEBUG_BUGS */ 
4580         if (rq->cmd == IDETAPE_WRITE_REQUEST)
4581                 idetape_copy_buffer_from_stage (stage,tape->data_buffer);
4582         
4583         rq->buffer=tape->data_buffer;
4584         tape->active_data_request=rq;
4585         tape->active_stage=stage;
4586         tape->next_stage=stage->next;
4587 }

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