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_empty_read_pipeline
  47. idetape_empty_write_pipeline
  48. idetape_zero_packet_command
  49. idetape_swap_short
  50. idetape_swap_long
  51. idetape_next_pc_storage
  52. idetape_next_rq_storage
  53. idetape_blkdev_open
  54. idetape_blkdev_release
  55. idetape_chrdev_read
  56. idetape_chrdev_read_remainder
  57. idetape_chrdev_write
  58. idetape_chrdev_write_remainder
  59. idetape_chrdev_ioctl
  60. idetape_mtioctop
  61. idetape_space_over_filemarks
  62. idetape_chrdev_open
  63. idetape_chrdev_release
  64. idetape_position_tape
  65. idetape_rewind_tape
  66. idetape_flush_tape_buffers
  67. idetape_kmalloc_stage
  68. idetape_kfree_stage
  69. idetape_copy_buffer_from_stage
  70. idetape_copy_buffer_to_stage
  71. idetape_increase_max_pipeline_stages
  72. idetape_add_stage_tail
  73. idetape_remove_stage_head
  74. idetape_insert_pipeline_into_queue
  75. idetape_active_next_stage

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

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