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_erase_cmd
  31. idetape_create_read_cmd
  32. idetape_create_space_cmd
  33. idetape_create_write_cmd
  34. idetape_create_read_position_cmd
  35. idetape_read_position_callback
  36. idetape_blkdev_ioctl
  37. idetape_end_request
  38. idetape_do_request
  39. idetape_queue_pc_tail
  40. idetape_queue_pc_head
  41. idetape_wait_for_request
  42. idetape_queue_rw_tail
  43. idetape_add_chrdev_write_request
  44. idetape_empty_pipeline
  45. idetape_zero_packet_command
  46. idetape_swap_short
  47. idetape_swap_long
  48. idetape_next_pc_storage
  49. idetape_next_rq_storage
  50. idetape_blkdev_open
  51. idetape_blkdev_release
  52. idetape_chrdev_lseek
  53. idetape_chrdev_read
  54. idetape_chrdev_read_remainder
  55. idetape_chrdev_write
  56. idetape_chrdev_write_remainder
  57. idetape_chrdev_ioctl
  58. idetape_mtioctop
  59. idetape_chrdev_open
  60. idetape_chrdev_release
  61. idetape_position_tape
  62. idetape_rewind_tape
  63. idetape_kmalloc_stage
  64. idetape_kfree_stage
  65. idetape_copy_buffer_from_stage
  66. idetape_copy_buffer_to_stage
  67. idetape_increase_max_pipeline_stages
  68. idetape_add_stage_tail
  69. idetape_insert_pipeline_into_queue
  70. idetape_active_next_stage

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

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