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

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