ide-tape.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:217k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * linux/drivers/ide/ide-tape.c Version 1.17a Jan, 2001
  3.  *
  4.  * Copyright (C) 1995 - 1999 Gadi Oxman <gadio@netvision.net.il>
  5.  *
  6.  * $Header$
  7.  *
  8.  * This driver was constructed as a student project in the software laboratory
  9.  * of the faculty of electrical engineering in the Technion - Israel's
  10.  * Institute Of Technology, with the guide of Avner Lottem and Dr. Ilana David.
  11.  *
  12.  * It is hereby placed under the terms of the GNU general public license.
  13.  * (See linux/COPYING).
  14.  */
  15.  
  16. /*
  17.  * IDE ATAPI streaming tape driver.
  18.  *
  19.  * This driver is a part of the Linux ide driver and works in co-operation
  20.  * with linux/drivers/block/ide.c.
  21.  *
  22.  * The driver, in co-operation with ide.c, basically traverses the 
  23.  * request-list for the block device interface. The character device
  24.  * interface, on the other hand, creates new requests, adds them
  25.  * to the request-list of the block device, and waits for their completion.
  26.  *
  27.  * Pipelined operation mode is now supported on both reads and writes.
  28.  *
  29.  * The block device major and minor numbers are determined from the
  30.  * tape's relative position in the ide interfaces, as explained in ide.c.
  31.  *
  32.  * The character device interface consists of the following devices:
  33.  *
  34.  * ht0 major 37, minor 0 first  IDE tape, rewind on close.
  35.  * ht1 major 37, minor 1 second IDE tape, rewind on close.
  36.  * ...
  37.  * nht0 major 37, minor 128 first  IDE tape, no rewind on close.
  38.  * nht1 major 37, minor 129 second IDE tape, no rewind on close.
  39.  * ...
  40.  *
  41.  * Run linux/scripts/MAKEDEV.ide to create the above entries.
  42.  *
  43.  * The general magnetic tape commands compatible interface, as defined by
  44.  * include/linux/mtio.h, is accessible through the character device.
  45.  *
  46.  * General ide driver configuration options, such as the interrupt-unmask
  47.  * flag, can be configured by issuing an ioctl to the block device interface,
  48.  * as any other ide device.
  49.  *
  50.  * Our own ide-tape ioctl's can be issued to either the block device or
  51.  * the character device interface.
  52.  *
  53.  * Maximal throughput with minimal bus load will usually be achieved in the
  54.  * following scenario:
  55.  *
  56.  * 1. ide-tape is operating in the pipelined operation mode.
  57.  * 2. No buffering is performed by the user backup program.
  58.  *
  59.  * Testing was done with a 2 GB CONNER CTMA 4000 IDE ATAPI Streaming Tape Drive.
  60.  * 
  61.  * Ver 0.1   Nov  1 95   Pre-working code :-)
  62.  * Ver 0.2   Nov 23 95   A short backup (few megabytes) and restore procedure
  63.  *                        was successful ! (Using tar cvf ... on the block
  64.  *                        device interface).
  65.  *                       A longer backup resulted in major swapping, bad
  66.  *                        overall Linux performance and eventually failed as
  67.  *                        we received non serial read-ahead requests from the
  68.  *                        buffer cache.
  69.  * Ver 0.3   Nov 28 95   Long backups are now possible, thanks to the
  70.  *                        character device interface. Linux's responsiveness
  71.  *                        and performance doesn't seem to be much affected
  72.  *                        from the background backup procedure.
  73.  *                       Some general mtio.h magnetic tape operations are
  74.  *                        now supported by our character device. As a result,
  75.  *                        popular tape utilities are starting to work with
  76.  *                        ide tapes :-)
  77.  *                       The following configurations were tested:
  78.  *                        1. An IDE ATAPI TAPE shares the same interface
  79.  *                           and irq with an IDE ATAPI CDROM.
  80.  *                         2. An IDE ATAPI TAPE shares the same interface
  81.  *                              and irq with a normal IDE disk.
  82.  *                        Both configurations seemed to work just fine !
  83.  *                        However, to be on the safe side, it is meanwhile
  84.  *                        recommended to give the IDE TAPE its own interface
  85.  *                        and irq.
  86.  *                       The one thing which needs to be done here is to
  87.  *                        add a "request postpone" feature to ide.c,
  88.  *                        so that we won't have to wait for the tape to finish
  89.  *                        performing a long media access (DSC) request (such
  90.  *                        as a rewind) before we can access the other device
  91.  *                        on the same interface. This effect doesn't disturb
  92.  *                        normal operation most of the time because read/write
  93.  *                        requests are relatively fast, and once we are
  94.  *                        performing one tape r/w request, a lot of requests
  95.  *                        from the other device can be queued and ide.c will
  96.  *   service all of them after this single tape request.
  97.  * Ver 1.0   Dec 11 95   Integrated into Linux 1.3.46 development tree.
  98.  *                       On each read / write request, we now ask the drive
  99.  *                        if we can transfer a constant number of bytes
  100.  *                        (a parameter of the drive) only to its buffers,
  101.  *                        without causing actual media access. If we can't,
  102.  *                        we just wait until we can by polling the DSC bit.
  103.  *                        This ensures that while we are not transferring
  104.  *                        more bytes than the constant referred to above, the
  105.  *                        interrupt latency will not become too high and
  106.  *                        we won't cause an interrupt timeout, as happened
  107.  *                        occasionally in the previous version.
  108.  *                       While polling for DSC, the current request is
  109.  *                        postponed and ide.c is free to handle requests from
  110.  *                        the other device. This is handled transparently to
  111.  *                        ide.c. The hwgroup locking method which was used
  112.  *                        in the previous version was removed.
  113.  *                       Use of new general features which are provided by
  114.  *                        ide.c for use with atapi devices.
  115.  *                        (Programming done by Mark Lord)
  116.  *                       Few potential bug fixes (Again, suggested by Mark)
  117.  *                       Single character device data transfers are now
  118.  *                        not limited in size, as they were before.
  119.  *                       We are asking the tape about its recommended
  120.  *                        transfer unit and send a larger data transfer
  121.  *                        as several transfers of the above size.
  122.  *                        For best results, use an integral number of this
  123.  *                        basic unit (which is shown during driver
  124.  *                        initialization). I will soon add an ioctl to get
  125.  *                        this important parameter.
  126.  *                       Our data transfer buffer is allocated on startup,
  127.  *                        rather than before each data transfer. This should
  128.  *                        ensure that we will indeed have a data buffer.
  129.  * Ver 1.1   Dec 14 95   Fixed random problems which occurred when the tape
  130.  *                        shared an interface with another device.
  131.  *                        (poll_for_dsc was a complete mess).
  132.  *                       Removed some old (non-active) code which had
  133.  *                        to do with supporting buffer cache originated
  134.  *                        requests.
  135.  *                       The block device interface can now be opened, so
  136.  *                        that general ide driver features like the unmask
  137.  *                        interrupts flag can be selected with an ioctl.
  138.  *                        This is the only use of the block device interface.
  139.  *                       New fast pipelined operation mode (currently only on
  140.  *                        writes). When using the pipelined mode, the
  141.  *                        throughput can potentially reach the maximum
  142.  *                        tape supported throughput, regardless of the
  143.  *                        user backup program. On my tape drive, it sometimes
  144.  *                        boosted performance by a factor of 2. Pipelined
  145.  *                        mode is enabled by default, but since it has a few
  146.  *                        downfalls as well, you may want to disable it.
  147.  *                        A short explanation of the pipelined operation mode
  148.  *                        is available below.
  149.  * Ver 1.2   Jan  1 96   Eliminated pipelined mode race condition.
  150.  *                       Added pipeline read mode. As a result, restores
  151.  *                        are now as fast as backups.
  152.  *                       Optimized shared interface behavior. The new behavior
  153.  *                        typically results in better IDE bus efficiency and
  154.  *                        higher tape throughput.
  155.  *                       Pre-calculation of the expected read/write request
  156.  *                        service time, based on the tape's parameters. In
  157.  *                        the pipelined operation mode, this allows us to
  158.  *                        adjust our polling frequency to a much lower value,
  159.  *                        and thus to dramatically reduce our load on Linux,
  160.  *                        without any decrease in performance.
  161.  *                       Implemented additional mtio.h operations.
  162.  *                       The recommended user block size is returned by
  163.  *                        the MTIOCGET ioctl.
  164.  *                       Additional minor changes.
  165.  * Ver 1.3   Feb  9 96   Fixed pipelined read mode bug which prevented the
  166.  *                        use of some block sizes during a restore procedure.
  167.  *                       The character device interface will now present a
  168.  *                        continuous view of the media - any mix of block sizes
  169.  *                        during a backup/restore procedure is supported. The
  170.  *                        driver will buffer the requests internally and
  171.  *                        convert them to the tape's recommended transfer
  172.  *                        unit, making performance almost independent of the
  173.  *                        chosen user block size.
  174.  *                       Some improvements in error recovery.
  175.  *                       By cooperating with ide-dma.c, bus mastering DMA can
  176.  *                        now sometimes be used with IDE tape drives as well.
  177.  *                        Bus mastering DMA has the potential to dramatically
  178.  *                        reduce the CPU's overhead when accessing the device,
  179.  *                        and can be enabled by using hdparm -d1 on the tape's
  180.  *                        block device interface. For more info, read the
  181.  *                        comments in ide-dma.c.
  182.  * Ver 1.4   Mar 13 96   Fixed serialize support.
  183.  * Ver 1.5   Apr 12 96   Fixed shared interface operation, broken in 1.3.85.
  184.  *                       Fixed pipelined read mode inefficiency.
  185.  *                       Fixed nasty null dereferencing bug.
  186.  * Ver 1.6   Aug 16 96   Fixed FPU usage in the driver.
  187.  *                       Fixed end of media bug.
  188.  * Ver 1.7   Sep 10 96   Minor changes for the CONNER CTT8000-A model.
  189.  * Ver 1.8   Sep 26 96   Attempt to find a better balance between good
  190.  *                        interactive response and high system throughput.
  191.  * Ver 1.9   Nov  5 96   Automatically cross encountered filemarks rather
  192.  *                        than requiring an explicit FSF command.
  193.  *                       Abort pending requests at end of media.
  194.  *                       MTTELL was sometimes returning incorrect results.
  195.  *                       Return the real block size in the MTIOCGET ioctl.
  196.  *                       Some error recovery bug fixes.
  197.  * Ver 1.10  Nov  5 96   Major reorganization.
  198.  *                       Reduced CPU overhead a bit by eliminating internal
  199.  *                        bounce buffers.
  200.  *                       Added module support.
  201.  *                       Added multiple tape drives support.
  202.  *                       Added partition support.
  203.  *                       Rewrote DSC handling.
  204.  *                       Some portability fixes.
  205.  *                       Removed ide-tape.h.
  206.  *                       Additional minor changes.
  207.  * Ver 1.11  Dec  2 96   Bug fix in previous DSC timeout handling.
  208.  *                       Use ide_stall_queue() for DSC overlap.
  209.  *                       Use the maximum speed rather than the current speed
  210.  *                        to compute the request service time.
  211.  * Ver 1.12  Dec  7 97   Fix random memory overwriting and/or last block data
  212.  *                        corruption, which could occur if the total number
  213.  *                        of bytes written to the tape was not an integral
  214.  *                        number of tape blocks.
  215.  *                       Add support for INTERRUPT DRQ devices.
  216.  * Ver 1.13  Jan  2 98   Add "speed == 0" work-around for HP COLORADO 5GB
  217.  * Ver 1.14  Dec 30 98   Partial fixes for the Sony/AIWA tape drives.
  218.  *                       Replace cli()/sti() with hwgroup spinlocks.
  219.  * Ver 1.15  Mar 25 99   Fix SMP race condition by replacing hwgroup
  220.  *                        spinlock with private per-tape spinlock.
  221.  * Ver 1.16  Sep  1 99   Add OnStream tape support.
  222.  *                       Abort read pipeline on EOD.
  223.  *                       Wait for the tape to become ready in case it returns
  224.  *                        "in the process of becoming ready" on open().
  225.  *                       Fix zero padding of the last written block in
  226.  *                        case the tape block size is larger than PAGE_SIZE.
  227.  *                       Decrease the default disconnection time to tn.
  228.  * Ver 1.16e Oct  3 99   Minor fixes.
  229.  * Ver 1.16e1 Oct 13 99  Patches by Arnold Niessen,
  230.  *                          niessen@iae.nl / arnold.niessen@philips.com
  231.  *                   GO-1)  Undefined code in idetape_read_position
  232.  * according to Gadi's email
  233.  *                   AJN-1) Minor fix asc == 11 should be asc == 0x11
  234.  *                               in idetape_issue_packet_command (did effect
  235.  *                               debugging output only)
  236.  *                   AJN-2) Added more debugging output, and
  237.  *                              added ide-tape: where missing. I would also
  238.  * like to add tape->name where possible
  239.  *                   AJN-3) Added different debug_level's 
  240.  *                              via /proc/ide/hdc/settings
  241.  *  "debug_level" determines amount of debugging output;
  242.  *  can be changed using /proc/ide/hdx/settings
  243.  *  0 : almost no debugging output
  244.  *  1 : 0+output errors only
  245.  *  2 : 1+output all sensekey/asc
  246.  *  3 : 2+follow all chrdev related procedures
  247.  *  4 : 3+follow all procedures
  248.  *  5 : 4+include pc_stack rq_stack info
  249.  *  6 : 5+USE_COUNT updates
  250.  *                   AJN-4) Fixed timeout for retension in idetape_queue_pc_tail
  251.  * from 5 to 10 minutes
  252.  *                   AJN-5) Changed maximum number of blocks to skip when
  253.  *                              reading tapes with multiple consecutive write
  254.  *                              errors from 100 to 1000 in idetape_get_logical_blk
  255.  *                   Proposed changes to code:
  256.  *                   1) output "logical_blk_num" via /proc
  257.  *                   2) output "current_operation" via /proc
  258.  *                   3) Either solve or document the fact that `mt rewind' is
  259.  *                      required after reading from /dev/nhtx to be
  260.  * able to rmmod the idetape module;
  261.  * Also, sometimes an application finishes but the
  262.  * device remains `busy' for some time. Same cause ?
  263.  *                   Proposed changes to release-notes:
  264.  *      4) write a simple `quickstart' section in the
  265.  *                      release notes; I volunteer if you don't want to
  266.  *       5) include a pointer to video4linux in the doc
  267.  *                      to stimulate video applications
  268.  *                   6) release notes lines 331 and 362: explain what happens
  269.  * if the application data rate is higher than 1100 KB/s; 
  270.  * similar approach to lower-than-500 kB/s ?
  271.  *      7) 6.6 Comparison; wouldn't it be better to allow different 
  272.  * strategies for read and write ?
  273.  * Wouldn't it be better to control the tape buffer
  274.  * contents instead of the bandwidth ?
  275.  *      8) line 536: replace will by would (if I understand
  276.  * this section correctly, a hypothetical and unwanted situation
  277.  *  is being described)
  278.  * Ver 1.16f Dec 15 99   Change place of the secondary OnStream header frames.
  279.  * Ver 1.17  Nov 2000 / Jan 2001  Marcel Mol, marcel@mesa.nl
  280.  * - Add idetape_onstream_mode_sense_tape_parameter_page
  281.  *   function to get tape capacity in frames: tape->capacity.
  282.  * - Add support for DI-50 drives( or any DI- drive).
  283.  * - 'workaround' for read error/blank block arround block 3000.
  284.  * - Implement Early warning for end of media for Onstream.
  285.  * - Cosmetic code changes for readability.
  286.  * - Idetape_position_tape should not use SKIP bit during
  287.  *   Onstream read recovery.
  288.  * - Add capacity, logical_blk_num and first/last_frame_position
  289.  *   to /proc/ide/hd?/settings.
  290.  * - Module use count was gone in the Linux 2.4 driver.
  291.  * Ver 1.17a Apr 2001 Willem Riede osst@riede.org
  292.  *  - Get drive's actual block size from mode sense block descriptor
  293.  *  - Limit size of pipeline
  294.  *
  295.  * Here are some words from the first releases of hd.c, which are quoted
  296.  * in ide.c and apply here as well:
  297.  *
  298.  * | Special care is recommended.  Have Fun!
  299.  *
  300.  */
  301. /*
  302.  * An overview of the pipelined operation mode.
  303.  *
  304.  * In the pipelined write mode, we will usually just add requests to our
  305.  * pipeline and return immediately, before we even start to service them. The
  306.  * user program will then have enough time to prepare the next request while
  307.  * we are still busy servicing previous requests. In the pipelined read mode,
  308.  * the situation is similar - we add read-ahead requests into the pipeline,
  309.  * before the user even requested them.
  310.  *
  311.  * The pipeline can be viewed as a "safety net" which will be activated when
  312.  * the system load is high and prevents the user backup program from keeping up
  313.  * with the current tape speed. At this point, the pipeline will get
  314.  * shorter and shorter but the tape will still be streaming at the same speed.
  315.  * Assuming we have enough pipeline stages, the system load will hopefully
  316.  * decrease before the pipeline is completely empty, and the backup program
  317.  * will be able to "catch up" and refill the pipeline again.
  318.  * 
  319.  * When using the pipelined mode, it would be best to disable any type of
  320.  * buffering done by the user program, as ide-tape already provides all the
  321.  * benefits in the kernel, where it can be done in a more efficient way.
  322.  * As we will usually not block the user program on a request, the most
  323.  * efficient user code will then be a simple read-write-read-... cycle.
  324.  * Any additional logic will usually just slow down the backup process.
  325.  *
  326.  * Using the pipelined mode, I get a constant over 400 KBps throughput,
  327.  * which seems to be the maximum throughput supported by my tape.
  328.  *
  329.  * However, there are some downfalls:
  330.  *
  331.  * 1. We use memory (for data buffers) in proportional to the number
  332.  * of pipeline stages (each stage is about 26 KB with my tape).
  333.  * 2. In the pipelined write mode, we cheat and postpone error codes
  334.  * to the user task. In read mode, the actual tape position
  335.  * will be a bit further than the last requested block.
  336.  *
  337.  * Concerning (1):
  338.  *
  339.  * 1. We allocate stages dynamically only when we need them. When
  340.  * we don't need them, we don't consume additional memory. In
  341.  * case we can't allocate stages, we just manage without them
  342.  * (at the expense of decreased throughput) so when Linux is
  343.  * tight in memory, we will not pose additional difficulties.
  344.  *
  345.  * 2. The maximum number of stages (which is, in fact, the maximum
  346.  * amount of memory) which we allocate is limited by the compile
  347.  * time parameter IDETAPE_MAX_PIPELINE_STAGES.
  348.  *
  349.  * 3. The maximum number of stages is a controlled parameter - We
  350.  * don't start from the user defined maximum number of stages
  351.  * but from the lower IDETAPE_MIN_PIPELINE_STAGES (again, we
  352.  * will not even allocate this amount of stages if the user
  353.  * program can't handle the speed). We then implement a feedback
  354.  * loop which checks if the pipeline is empty, and if it is, we
  355.  * increase the maximum number of stages as necessary until we
  356.  * reach the optimum value which just manages to keep the tape
  357.  * busy with minimum allocated memory or until we reach
  358.  * IDETAPE_MAX_PIPELINE_STAGES.
  359.  *
  360.  * Concerning (2):
  361.  *
  362.  * In pipelined write mode, ide-tape can not return accurate error codes
  363.  * to the user program since we usually just add the request to the
  364.  *      pipeline without waiting for it to be serviced. In case an error
  365.  *      occurs, I will report it on the next user request.
  366.  *
  367.  * In the pipelined read mode, subsequent read requests or forward
  368.  * filemark spacing will perform correctly, as we preserve all blocks
  369.  * and filemarks which we encountered during our excess read-ahead.
  370.  * 
  371.  * For accurate tape positioning and error reporting, disabling
  372.  * pipelined mode might be the best option.
  373.  *
  374.  * You can enable/disable/tune the pipelined operation mode by adjusting
  375.  * the compile time parameters below.
  376.  */
  377. /*
  378.  * Possible improvements.
  379.  *
  380.  * 1. Support for the ATAPI overlap protocol.
  381.  *
  382.  * In order to maximize bus throughput, we currently use the DSC
  383.  * overlap method which enables ide.c to service requests from the
  384.  * other device while the tape is busy executing a command. The
  385.  * DSC overlap method involves polling the tape's status register
  386.  * for the DSC bit, and servicing the other device while the tape
  387.  * isn't ready.
  388.  *
  389.  * In the current QIC development standard (December 1995),
  390.  * it is recommended that new tape drives will *in addition* 
  391.  * implement the ATAPI overlap protocol, which is used for the
  392.  * same purpose - efficient use of the IDE bus, but is interrupt
  393.  * driven and thus has much less CPU overhead.
  394.  *
  395.  * ATAPI overlap is likely to be supported in most new ATAPI
  396.  * devices, including new ATAPI cdroms, and thus provides us
  397.  * a method by which we can achieve higher throughput when
  398.  * sharing a (fast) ATA-2 disk with any (slow) new ATAPI device.
  399.  */
  400. #define IDETAPE_VERSION "1.17a"
  401. #include <linux/config.h>
  402. #include <linux/module.h>
  403. #include <linux/types.h>
  404. #include <linux/string.h>
  405. #include <linux/kernel.h>
  406. #include <linux/delay.h>
  407. #include <linux/timer.h>
  408. #include <linux/mm.h>
  409. #include <linux/interrupt.h>
  410. #include <linux/major.h>
  411. #include <linux/devfs_fs_kernel.h>
  412. #include <linux/errno.h>
  413. #include <linux/genhd.h>
  414. #include <linux/slab.h>
  415. #include <linux/pci.h>
  416. #include <linux/ide.h>
  417. #include <linux/smp_lock.h>
  418. #include <linux/completion.h>
  419. #include <asm/byteorder.h>
  420. #include <asm/irq.h>
  421. #include <asm/uaccess.h>
  422. #include <asm/io.h>
  423. #include <asm/unaligned.h>
  424. #include <asm/bitops.h>
  425. #define NO_LONGER_REQUIRED (1)
  426. /*
  427.  * OnStream support
  428.  */
  429. #define ONSTREAM_DEBUG (0)
  430. #define OS_CONFIG_PARTITION (0xff)
  431. #define OS_DATA_PARTITION (0)
  432. #define OS_PARTITION_VERSION (1)
  433. #define OS_EW 300
  434. #define OS_ADR_MINREV 2
  435. #define OS_DATA_STARTFRAME1 20
  436. #define OS_DATA_ENDFRAME1 2980
  437. /*
  438.  * partition
  439.  */
  440. typedef struct os_partition_s {
  441. __u8 partition_num;
  442. __u8 par_desc_ver;
  443. __u16 wrt_pass_cntr;
  444. __u32 first_frame_addr;
  445. __u32 last_frame_addr;
  446. __u32 eod_frame_addr;
  447. } os_partition_t;
  448. /*
  449.  * DAT entry
  450.  */
  451. typedef struct os_dat_entry_s {
  452. __u32 blk_sz;
  453. __u16 blk_cnt;
  454. __u8 flags;
  455. __u8 reserved;
  456. } os_dat_entry_t;
  457. /*
  458.  * DAT
  459.  */
  460. #define OS_DAT_FLAGS_DATA (0xc)
  461. #define OS_DAT_FLAGS_MARK (0x1)
  462. typedef struct os_dat_s {
  463. __u8 dat_sz;
  464. __u8 reserved1;
  465. __u8 entry_cnt;
  466. __u8 reserved3;
  467. os_dat_entry_t dat_list[16];
  468. } os_dat_t;
  469. /*
  470.  * Frame types
  471.  */
  472. #define OS_FRAME_TYPE_FILL (0)
  473. #define OS_FRAME_TYPE_EOD (1 << 0)
  474. #define OS_FRAME_TYPE_MARKER (1 << 1)
  475. #define OS_FRAME_TYPE_HEADER (1 << 3)
  476. #define OS_FRAME_TYPE_DATA (1 << 7)
  477. /*
  478.  * AUX
  479.  */
  480. typedef struct os_aux_s {
  481. __u32 format_id; /* hardware compability AUX is based on */
  482. char application_sig[4]; /* driver used to write this media */
  483. __u32 hdwr; /* reserved */
  484. __u32 update_frame_cntr; /* for configuration frame */
  485. __u8 frame_type;
  486. __u8 frame_type_reserved;
  487. __u8 reserved_18_19[2];
  488. os_partition_t partition;
  489. __u8 reserved_36_43[8];
  490. __u32 frame_seq_num;
  491. __u32 logical_blk_num_high;
  492. __u32 logical_blk_num;
  493. os_dat_t dat;
  494. __u8 reserved188_191[4];
  495. __u32 filemark_cnt;
  496. __u32 phys_fm;
  497. __u32 last_mark_addr;
  498. __u8 reserved204_223[20];
  499. /*
  500.  * __u8 app_specific[32];
  501.  *
  502.  * Linux specific fields:
  503.  */
  504.  __u32 next_mark_addr; /* when known, points to next marker */
  505.  __u8 linux_specific[28];
  506. __u8 reserved_256_511[256];
  507. } os_aux_t;
  508. typedef struct os_header_s {
  509. char ident_str[8];
  510. __u8 major_rev;
  511. __u8 minor_rev;
  512. __u8 reserved10_15[6];
  513. __u8 par_num;
  514. __u8 reserved1_3[3];
  515. os_partition_t partition;
  516. } os_header_t;
  517. /*
  518.  * OnStream Tape Parameters Page
  519.  */
  520. typedef struct {
  521. unsigned page_code :6; /* Page code - Should be 0x2b */
  522. unsigned reserved1_6 :1;
  523. unsigned ps :1;
  524. __u8 reserved2;
  525. __u8 density; /* kbpi */
  526. __u8 reserved3,reserved4;
  527. __u16 segtrk;                 /* segment of per track */
  528. __u16 trks;                   /* tracks per tape */
  529. __u8 reserved5,reserved6,reserved7,reserved8,reserved9,reserved10;
  530. } onstream_tape_paramtr_page_t;
  531. /*
  532.  * OnStream ADRL frame
  533.  */
  534. #define OS_FRAME_SIZE (32 * 1024 + 512)
  535. #define OS_DATA_SIZE (32 * 1024)
  536. #define OS_AUX_SIZE (512)
  537. /*
  538.  * internal error codes for onstream
  539.  */
  540. #define OS_PART_ERROR    2
  541. #define OS_WRITE_ERROR   1
  542. #include <linux/mtio.h>
  543. /**************************** Tunable parameters *****************************/
  544. /*
  545.  * Pipelined mode parameters.
  546.  *
  547.  * We try to use the minimum number of stages which is enough to
  548.  * keep the tape constantly streaming. To accomplish that, we implement
  549.  * a feedback loop around the maximum number of stages:
  550.  *
  551.  * We start from MIN maximum stages (we will not even use MIN stages
  552.  *      if we don't need them), increment it by RATE*(MAX-MIN)
  553.  * whenever we sense that the pipeline is empty, until we reach
  554.  * the optimum value or until we reach MAX.
  555.  *
  556.  * Setting the following parameter to 0 will disable the pipelined mode.
  557.  */
  558. #define IDETAPE_MIN_PIPELINE_STAGES 200
  559. #define IDETAPE_MAX_PIPELINE_STAGES 400
  560. #define IDETAPE_INCREASE_STAGES_RATE  20
  561. /*
  562.  * The following are used to debug the driver:
  563.  *
  564.  * Setting IDETAPE_DEBUG_INFO to 1 will report device capabilities.
  565.  * Setting IDETAPE_DEBUG_LOG to 1 will log driver flow control.
  566.  * Setting IDETAPE_DEBUG_BUGS to 1 will enable self-sanity checks in
  567.  * some places.
  568.  *
  569.  * Setting them to 0 will restore normal operation mode:
  570.  *
  571.  * 1. Disable logging normal successful operations.
  572.  * 2. Disable self-sanity checks.
  573.  * 3. Errors will still be logged, of course.
  574.  *
  575.  * All the #if DEBUG code will be removed some day, when the driver
  576.  * is verified to be stable enough. This will make it much more
  577.  * esthetic.
  578.  */
  579. #define IDETAPE_DEBUG_INFO 1
  580. #define IDETAPE_DEBUG_LOG 1
  581. #define IDETAPE_DEBUG_LOG_VERBOSE 0
  582. #define IDETAPE_DEBUG_BUGS 1
  583. /*
  584.  * After each failed packet command we issue a request sense command
  585.  * and retry the packet command IDETAPE_MAX_PC_RETRIES times.
  586.  *
  587.  * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
  588.  */
  589. #define IDETAPE_MAX_PC_RETRIES 3
  590. /*
  591.  * With each packet command, we allocate a buffer of
  592.  * IDETAPE_PC_BUFFER_SIZE bytes. This is used for several packet
  593.  * commands (Not for READ/WRITE commands).
  594.  */
  595. #define IDETAPE_PC_BUFFER_SIZE 256
  596. /*
  597.  * In various places in the driver, we need to allocate storage
  598.  * for packet commands and requests, which will remain valid while
  599.  * we leave the driver to wait for an interrupt or a timeout event.
  600.  */
  601. #define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES)
  602. /*
  603.  * Some tape drives require a long irq timeout
  604.  */
  605. #define IDETAPE_WAIT_CMD (60*HZ)
  606. /*
  607.  * The following parameter is used to select the point in the internal
  608.  * tape fifo in which we will start to refill the buffer. Decreasing
  609.  * the following parameter will improve the system's latency and
  610.  * interactive response, while using a high value might improve sytem
  611.  * throughput.
  612.  */
  613. #define IDETAPE_FIFO_THRESHOLD  2
  614. /*
  615.  * DSC polling parameters.
  616.  *
  617.  * Polling for DSC (a single bit in the status register) is a very
  618.  * important function in ide-tape. There are two cases in which we
  619.  * poll for DSC:
  620.  *
  621.  * 1. Before a read/write packet command, to ensure that we
  622.  * can transfer data from/to the tape's data buffers, without
  623.  * causing an actual media access. In case the tape is not
  624.  * ready yet, we take out our request from the device
  625.  * request queue, so that ide.c will service requests from
  626.  * the other device on the same interface meanwhile.
  627.  *
  628.  * 2. After the successful initialization of a "media access
  629.  * packet command", which is a command which can take a long
  630.  * time to complete (it can be several seconds or even an hour).
  631.  *
  632.  * Again, we postpone our request in the middle to free the bus
  633.  * for the other device. The polling frequency here should be
  634.  * lower than the read/write frequency since those media access
  635.  * commands are slow. We start from a "fast" frequency -
  636.  * IDETAPE_DSC_MA_FAST (one second), and if we don't receive DSC
  637.  * after IDETAPE_DSC_MA_THRESHOLD (5 minutes), we switch it to a
  638.  * lower frequency - IDETAPE_DSC_MA_SLOW (1 minute).
  639.  *
  640.  * We also set a timeout for the timer, in case something goes wrong.
  641.  * The timeout should be longer then the maximum execution time of a
  642.  * tape operation.
  643.  */
  644.  
  645. /*
  646.  * DSC timings.
  647.  */
  648. #define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */
  649. #define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */
  650. #define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */
  651. #define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */
  652. #define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */
  653. #define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */
  654. #define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */
  655. /*************************** End of tunable parameters ***********************/
  656. /*
  657.  * Debugging/Performance analysis
  658.  *
  659.  * I/O trace support
  660.  */
  661. #define USE_IOTRACE 0
  662. #if USE_IOTRACE
  663. #include <linux/io_trace.h>
  664. #define IO_IDETAPE_FIFO 500
  665. #endif
  666. /*
  667.  * Read/Write error simulation
  668.  */
  669. #define SIMULATE_ERRORS 0
  670. /*
  671.  * For general magnetic tape device compatibility.
  672.  */
  673. typedef enum {
  674. idetape_direction_none,
  675. idetape_direction_read,
  676. idetape_direction_write
  677. } idetape_chrdev_direction_t;
  678. /*
  679.  * Our view of a packet command.
  680.  */
  681. typedef struct idetape_packet_command_s {
  682. u8 c[12]; /* Actual packet bytes */
  683. int retries; /* On each retry, we increment retries */
  684. int error; /* Error code */
  685. int request_transfer; /* Bytes to transfer */
  686. int actually_transferred; /* Bytes actually transferred */
  687. int buffer_size; /* Size of our data buffer */
  688. struct buffer_head *bh;
  689. char *b_data;
  690. int b_count;
  691. byte *buffer; /* Data buffer */
  692. byte *current_position; /* Pointer into the above buffer */
  693. ide_startstop_t (*callback) (ide_drive_t *); /* Called when this packet command is completed */
  694. byte pc_buffer[IDETAPE_PC_BUFFER_SIZE]; /* Temporary buffer */
  695. unsigned long flags; /* Status/Action bit flags: long for set_bit */
  696. } idetape_pc_t;
  697. /*
  698.  * Packet command flag bits.
  699.  */
  700. #define PC_ABORT 0 /* Set when an error is considered normal - We won't retry */
  701. #define PC_WAIT_FOR_DSC 1 /* 1 When polling for DSC on a media access command */
  702. #define PC_DMA_RECOMMENDED 2 /* 1 when we prefer to use DMA if possible */
  703. #define PC_DMA_IN_PROGRESS 3 /* 1 while DMA in progress */
  704. #define PC_DMA_ERROR 4 /* 1 when encountered problem during DMA */
  705. #define PC_WRITING 5 /* Data direction */
  706. /*
  707.  * Capabilities and Mechanical Status Page
  708.  */
  709. typedef struct {
  710. unsigned page_code :6; /* Page code - Should be 0x2a */
  711. __u8 reserved0_6 :1;
  712. __u8 ps :1; /* parameters saveable */
  713. __u8 page_length; /* Page Length - Should be 0x12 */
  714. __u8 reserved2, reserved3;
  715. unsigned ro :1; /* Read Only Mode */
  716. unsigned reserved4_1234 :4;
  717. unsigned sprev :1; /* Supports SPACE in the reverse direction */
  718. unsigned reserved4_67 :2;
  719. unsigned reserved5_012 :3;
  720. unsigned efmt :1; /* Supports ERASE command initiated formatting */
  721. unsigned reserved5_4 :1;
  722. unsigned qfa :1; /* Supports the QFA two partition formats */
  723. unsigned reserved5_67 :2;
  724. unsigned lock :1; /* Supports locking the volume */
  725. unsigned locked :1; /* The volume is locked */
  726. unsigned prevent :1; /* The device defaults in the prevent state after power up */
  727. unsigned eject :1; /* The device can eject the volume */
  728. __u8 disconnect :1; /* The device can break request > ctl */
  729. __u8 reserved6_5 :1;
  730. unsigned ecc :1; /* Supports error correction */
  731. unsigned cmprs :1; /* Supports data compression */
  732. unsigned reserved7_0 :1;
  733. unsigned blk512 :1; /* Supports 512 bytes block size */
  734. unsigned blk1024 :1; /* Supports 1024 bytes block size */
  735. unsigned reserved7_3_6 :4;
  736. unsigned blk32768 :1; /* slowb - the device restricts the byte count for PIO */
  737. /* transfers for slow buffer memory ??? */
  738. /* Also 32768 block size in some cases */
  739. __u16 max_speed; /* Maximum speed supported in KBps */
  740. __u8 reserved10, reserved11;
  741. __u16 ctl; /* Continuous Transfer Limit in blocks */
  742. __u16 speed; /* Current Speed, in KBps */
  743. __u16 buffer_size; /* Buffer Size, in 512 bytes */
  744. __u8 reserved18, reserved19;
  745. } idetape_capabilities_page_t;
  746. /*
  747.  * Block Size Page
  748.  */
  749. typedef struct {
  750. unsigned page_code :6; /* Page code - Should be 0x30 */
  751. unsigned reserved1_6 :1;
  752. unsigned ps :1;
  753. __u8 page_length; /* Page Length - Should be 2 */
  754. __u8 reserved2;
  755. unsigned play32 :1;
  756. unsigned play32_5 :1;
  757. unsigned reserved2_23 :2;
  758. unsigned record32 :1;
  759. unsigned record32_5 :1;
  760. unsigned reserved2_6 :1;
  761. unsigned one :1;
  762. } idetape_block_size_page_t;
  763. /*
  764.  * A pipeline stage.
  765.  */
  766. typedef struct idetape_stage_s {
  767. struct request rq; /* The corresponding request */
  768. struct buffer_head *bh; /* The data buffers */
  769. struct idetape_stage_s *next; /* Pointer to the next stage */
  770. os_aux_t *aux; /* OnStream aux ptr */
  771. } idetape_stage_t;
  772. /*
  773.  * REQUEST SENSE packet command result - Data Format.
  774.  */
  775. typedef struct {
  776. unsigned error_code :7; /* Current of deferred errors */
  777. unsigned valid :1; /* The information field conforms to QIC-157C */
  778. __u8 reserved1 :8; /* Segment Number - Reserved */
  779. unsigned sense_key :4; /* Sense Key */
  780. unsigned reserved2_4 :1; /* Reserved */
  781. unsigned ili :1; /* Incorrect Length Indicator */
  782. unsigned eom :1; /* End Of Medium */
  783. unsigned filemark  :1; /* Filemark */
  784. __u32 information __attribute__ ((packed));
  785. __u8 asl; /* Additional sense length (n-7) */
  786. __u32 command_specific; /* Additional command specific information */
  787. __u8 asc; /* Additional Sense Code */
  788. __u8 ascq; /* Additional Sense Code Qualifier */
  789. __u8 replaceable_unit_code; /* Field Replaceable Unit Code */
  790. unsigned sk_specific1  :7; /* Sense Key Specific */
  791. unsigned sksv :1; /* Sense Key Specific information is valid */
  792. __u8 sk_specific2; /* Sense Key Specific */
  793. __u8 sk_specific3; /* Sense Key Specific */
  794. __u8 pad[2]; /* Padding to 20 bytes */
  795. } idetape_request_sense_result_t;
  796. /*
  797.  * Most of our global data which we need to save even as we leave the
  798.  * driver due to an interrupt or a timer event is stored in a variable
  799.  * of type idetape_tape_t, defined below.
  800.  */
  801. typedef struct {
  802. ide_drive_t *drive;
  803. devfs_handle_t de_r, de_n;
  804. /*
  805.  * Since a typical character device operation requires more
  806.  * than one packet command, we provide here enough memory
  807.  * for the maximum of interconnected packet commands.
  808.  * The packet commands are stored in the circular array pc_stack.
  809.  * pc_stack_index points to the last used entry, and warps around
  810.  * to the start when we get to the last array entry.
  811.  *
  812.  * pc points to the current processed packet command.
  813.  *
  814.  * failed_pc points to the last failed packet command, or contains
  815.  * NULL if we do not need to retry any packet command. This is
  816.  * required since an additional packet command is needed before the
  817.  * retry, to get detailed information on what went wrong.
  818.  */
  819. idetape_pc_t *pc; /* Current packet command */
  820. idetape_pc_t *failed_pc;  /* Last failed packet command */
  821. idetape_pc_t pc_stack[IDETAPE_PC_STACK];/* Packet command stack */
  822. int pc_stack_index; /* Next free packet command storage space */
  823. struct request rq_stack[IDETAPE_PC_STACK];
  824. int rq_stack_index; /* We implement a circular array */
  825. /*
  826.  * DSC polling variables.
  827.  *
  828.  * While polling for DSC we use postponed_rq to postpone the
  829.  * current request so that ide.c will be able to service
  830.  * pending requests on the other device. Note that at most
  831.  * we will have only one DSC (usually data transfer) request
  832.  * in the device request queue. Additional requests can be
  833.  * queued in our internal pipeline, but they will be visible
  834.  * to ide.c only one at a time.
  835.  */
  836. struct request *postponed_rq;
  837. unsigned long dsc_polling_start; /* The time in which we started polling for DSC */
  838. struct timer_list dsc_timer; /* Timer used to poll for dsc */
  839. unsigned long best_dsc_rw_frequency; /* Read/Write dsc polling frequency */
  840. unsigned long dsc_polling_frequency; /* The current polling frequency */
  841. unsigned long dsc_timeout; /* Maximum waiting time */
  842. /*
  843.  * Read position information
  844.  */
  845. byte partition;
  846. unsigned int first_frame_position; /* Current block */
  847. unsigned int last_frame_position;
  848. unsigned int blocks_in_buffer;
  849. /*
  850.  * Last error information
  851.  */
  852. byte sense_key, asc, ascq;
  853. /*
  854.  * Character device operation
  855.  */
  856. unsigned int minor;
  857. char name[4]; /* device name */
  858. idetape_chrdev_direction_t chrdev_direction; /* Current character device data transfer direction */
  859. /*
  860.  * Device information
  861.  */
  862. unsigned short tape_block_size; /* Usually 512 or 1024 bytes */
  863. int user_bs_factor;
  864. idetape_capabilities_page_t capabilities; /* Copy of the tape's Capabilities and Mechanical Page */
  865. /*
  866.  * Active data transfer request parameters.
  867.  *
  868.  * At most, there is only one ide-tape originated data transfer
  869.  * request in the device request queue. This allows ide.c to
  870.  * easily service requests from the other device when we
  871.  * postpone our active request. In the pipelined operation
  872.  * mode, we use our internal pipeline structure to hold
  873.  * more data requests.
  874.  *
  875.  * The data buffer size is chosen based on the tape's
  876.  * recommendation.
  877.  */
  878. struct request *active_data_request; /* Pointer to the request which is waiting in the device request queue */
  879. int stage_size; /* Data buffer size (chosen based on the tape's recommendation */
  880. idetape_stage_t *merge_stage;
  881. int merge_stage_size;
  882. struct buffer_head *bh;
  883. char *b_data;
  884. int b_count;
  885. /*
  886.  * Pipeline parameters.
  887.  *
  888.  * To accomplish non-pipelined mode, we simply set the following
  889.  * variables to zero (or NULL, where appropriate).
  890.  */
  891. int nr_stages; /* Number of currently used stages */
  892. int nr_pending_stages; /* Number of pending stages */
  893. int max_stages, min_pipeline, max_pipeline; /* We will not allocate more than this number of stages */
  894. idetape_stage_t *first_stage; /* The first stage which will be removed from the pipeline */
  895. idetape_stage_t *active_stage; /* The currently active stage */
  896. idetape_stage_t *next_stage; /* Will be serviced after the currently active request */
  897. idetape_stage_t *last_stage; /* New requests will be added to the pipeline here */
  898. idetape_stage_t *cache_stage; /* Optional free stage which we can use */
  899. int pages_per_stage;
  900. int excess_bh_size; /* Wasted space in each stage */
  901. unsigned long flags; /* Status/Action flags: long for set_bit */
  902. spinlock_t spinlock; /* protects the ide-tape queue */
  903. /*
  904.  * Measures average tape speed
  905.  */
  906. unsigned long avg_time;
  907. int avg_size;
  908. int avg_speed;
  909. idetape_request_sense_result_t sense; /* last sense information */
  910. char vendor_id[10];
  911. char product_id[18];
  912. char firmware_revision[6];
  913. int firmware_revision_num;
  914. int door_locked; /* the door is currently locked */
  915. /*
  916.  * OnStream flags
  917.  */
  918. int onstream; /* the tape is an OnStream tape */
  919. int raw; /* OnStream raw access (32.5KB block size) */
  920. int cur_frames; /* current number of frames in internal buffer */
  921. int max_frames; /* max number of frames in internal buffer */
  922. int logical_blk_num; /* logical block number */
  923. __u16 wrt_pass_cntr; /* write pass counter */
  924. __u32 update_frame_cntr; /* update frame counter */
  925. struct completion *waiting;
  926. int onstream_write_error; /* write error recovery active */
  927. int header_ok; /* header frame verified ok */
  928. int linux_media; /* reading linux-specifc media */
  929. int linux_media_version;
  930. char application_sig[5]; /* application signature */
  931. int filemark_cnt;
  932. int first_mark_addr;
  933. int last_mark_addr;
  934. int eod_frame_addr;
  935. unsigned long cmd_start_time;
  936. unsigned long max_cmd_time;
  937. unsigned capacity;
  938. /*
  939.  * Optimize the number of "buffer filling"
  940.  * mode sense commands.
  941.  */
  942. unsigned long last_buffer_fill; /* last time in which we issued fill cmd */
  943. int req_buffer_fill; /* buffer fill command requested */
  944. int writes_since_buffer_fill;
  945. int reads_since_buffer_fill;
  946. /*
  947.  * Limit the number of times a request can
  948.  * be postponed, to avoid an infinite postpone
  949.  * deadlock.
  950.  */
  951. int postpone_cnt; /* request postpone count limit */
  952. /*
  953.  * Measures number of frames:
  954.  *
  955.  * 1. written/read to/from the driver pipeline (pipeline_head).
  956.  * 2. written/read to/from the tape buffers (buffer_head).
  957.  * 3. written/read by the tape to/from the media (tape_head).
  958.  */
  959. int pipeline_head;
  960. int buffer_head;
  961. int tape_head;
  962. int last_tape_head;
  963. /*
  964.  * Speed control at the tape buffers input/output
  965.  */
  966. unsigned long insert_time;
  967. int insert_size;
  968. int insert_speed;
  969. int max_insert_speed;
  970. int measure_insert_time;
  971. /*
  972.  * Measure tape still time, in milliseconds
  973.  */
  974. unsigned long tape_still_time_begin;
  975. int tape_still_time;
  976. /*
  977.  * Speed regulation negative feedback loop
  978.  */
  979. int speed_control;
  980. int pipeline_head_speed, controlled_pipeline_head_speed, uncontrolled_pipeline_head_speed;
  981. int controlled_last_pipeline_head, uncontrolled_last_pipeline_head;
  982. unsigned long uncontrolled_pipeline_head_time, controlled_pipeline_head_time;
  983. int controlled_previous_pipeline_head, uncontrolled_previous_pipeline_head;
  984. unsigned long controlled_previous_head_time, uncontrolled_previous_head_time;
  985. int restart_speed_control_req;
  986.         /*
  987.          * Debug_level determines amount of debugging output;
  988.          * can be changed using /proc/ide/hdx/settings
  989.          * 0 : almost no debugging output
  990.          * 1 : 0+output errors only
  991.          * 2 : 1+output all sensekey/asc
  992.          * 3 : 2+follow all chrdev related procedures
  993.          * 4 : 3+follow all procedures
  994.          * 5 : 4+include pc_stack rq_stack info
  995.          * 6 : 5+USE_COUNT updates
  996.          */
  997.          int debug_level; 
  998. } idetape_tape_t;
  999. /*
  1000.  * Tape door status
  1001.  */
  1002. #define DOOR_UNLOCKED 0
  1003. #define DOOR_LOCKED 1
  1004. #define DOOR_EXPLICITLY_LOCKED 2
  1005. /*
  1006.  * Tape flag bits values.
  1007.  */
  1008. #define IDETAPE_IGNORE_DSC 0
  1009. #define IDETAPE_ADDRESS_VALID 1 /* 0 When the tape position is unknown */
  1010. #define IDETAPE_BUSY 2 /* Device already opened */
  1011. #define IDETAPE_PIPELINE_ERROR 3 /* Error detected in a pipeline stage */
  1012. #define IDETAPE_DETECT_BS 4 /* Attempt to auto-detect the current user block size */
  1013. #define IDETAPE_FILEMARK 5 /* Currently on a filemark */
  1014. #define IDETAPE_DRQ_INTERRUPT 6 /* DRQ interrupt device */
  1015. #define IDETAPE_READ_ERROR 7
  1016. #define IDETAPE_PIPELINE_ACTIVE 8 /* pipeline active */
  1017. /*
  1018.  * Supported ATAPI tape drives packet commands
  1019.  */
  1020. #define IDETAPE_TEST_UNIT_READY_CMD 0x00
  1021. #define IDETAPE_REWIND_CMD 0x01
  1022. #define IDETAPE_REQUEST_SENSE_CMD 0x03
  1023. #define IDETAPE_READ_CMD 0x08
  1024. #define IDETAPE_WRITE_CMD 0x0a
  1025. #define IDETAPE_WRITE_FILEMARK_CMD 0x10
  1026. #define IDETAPE_SPACE_CMD 0x11
  1027. #define IDETAPE_INQUIRY_CMD 0x12
  1028. #define IDETAPE_ERASE_CMD 0x19
  1029. #define IDETAPE_MODE_SENSE_CMD 0x1a
  1030. #define IDETAPE_MODE_SELECT_CMD 0x15
  1031. #define IDETAPE_LOAD_UNLOAD_CMD 0x1b
  1032. #define IDETAPE_PREVENT_CMD 0x1e
  1033. #define IDETAPE_LOCATE_CMD 0x2b
  1034. #define IDETAPE_READ_POSITION_CMD 0x34
  1035. #define IDETAPE_READ_BUFFER_CMD 0x3c
  1036. #define IDETAPE_SET_SPEED_CMD 0xbb
  1037. /*
  1038.  * Some defines for the READ BUFFER command
  1039.  */
  1040. #define IDETAPE_RETRIEVE_FAULTY_BLOCK 6
  1041. /*
  1042.  * Some defines for the SPACE command
  1043.  */
  1044. #define IDETAPE_SPACE_OVER_FILEMARK 1
  1045. #define IDETAPE_SPACE_TO_EOD 3
  1046. /*
  1047.  * Some defines for the LOAD UNLOAD command
  1048.  */
  1049. #define IDETAPE_LU_LOAD_MASK 1
  1050. #define IDETAPE_LU_RETENSION_MASK 2
  1051. #define IDETAPE_LU_EOT_MASK 4
  1052. /*
  1053.  * Special requests for our block device strategy routine.
  1054.  *
  1055.  * In order to service a character device command, we add special
  1056.  * requests to the tail of our block device request queue and wait
  1057.  * for their completion.
  1058.  *
  1059.  */
  1060. #define IDETAPE_FIRST_RQ 90
  1061. /*
  1062.  *  IDETAPE_PC_RQ is used to queue a packet command in the request queue.
  1063.  */
  1064. #define IDETAPE_PC_RQ1 90
  1065. #define IDETAPE_PC_RQ2 91
  1066. /*
  1067.  * IDETAPE_READ_RQ and IDETAPE_WRITE_RQ are used by our
  1068.  * character device interface to request read/write operations from
  1069.  * our block device interface.
  1070.  */
  1071. #define IDETAPE_READ_RQ 92
  1072. #define IDETAPE_WRITE_RQ 93
  1073. #define IDETAPE_ABORTED_WRITE_RQ 94
  1074. #define IDETAPE_ABORTED_READ_RQ 95
  1075. #define IDETAPE_READ_BUFFER_RQ 96
  1076. #define IDETAPE_LAST_RQ 96
  1077. /*
  1078.  * A macro which can be used to check if a we support a given
  1079.  * request command.
  1080.  */
  1081. #define IDETAPE_RQ_CMD(cmd)  ((cmd >= IDETAPE_FIRST_RQ) && (cmd <= IDETAPE_LAST_RQ))
  1082. /*
  1083.  * Error codes which are returned in rq->errors to the higher part
  1084.  * of the driver.
  1085.  */
  1086. #define IDETAPE_ERROR_GENERAL 101
  1087. #define IDETAPE_ERROR_FILEMARK 102
  1088. #define IDETAPE_ERROR_EOD 103
  1089. /*
  1090.  * The ATAPI Status Register.
  1091.  */
  1092. typedef union {
  1093. unsigned all :8;
  1094. struct {
  1095. unsigned check :1; /* Error occurred */
  1096. unsigned idx :1; /* Reserved */
  1097. unsigned corr :1; /* Correctable error occurred */
  1098. unsigned drq :1; /* Data is request by the device */
  1099. unsigned dsc :1; /* Buffer availability / Media access command finished */
  1100. unsigned reserved5 :1; /* Reserved */
  1101. unsigned drdy :1; /* Ignored for ATAPI commands (ready to accept ATA command) */
  1102. unsigned bsy :1; /* The device has access to the command block */
  1103. } b;
  1104. } idetape_status_reg_t;
  1105. /*
  1106.  * The ATAPI error register.
  1107.  */
  1108. typedef union {
  1109. unsigned all :8;
  1110. struct {
  1111. unsigned ili :1; /* Illegal Length Indication */
  1112. unsigned eom :1; /* End Of Media Detected */
  1113. unsigned abrt :1; /* Aborted command - As defined by ATA */
  1114. unsigned mcr :1; /* Media Change Requested - As defined by ATA */
  1115. unsigned sense_key :4; /* Sense key of the last failed packet command */
  1116. } b;
  1117. } idetape_error_reg_t;
  1118. /*
  1119.  * ATAPI Feature Register
  1120.  */
  1121. typedef union {
  1122. unsigned all :8;
  1123. struct {
  1124. unsigned dma :1; /* Using DMA or PIO */
  1125. unsigned reserved321 :3; /* Reserved */
  1126. unsigned reserved654 :3; /* Reserved (Tag Type) */
  1127. unsigned reserved7 :1; /* Reserved */
  1128. } b;
  1129. } idetape_feature_reg_t;
  1130. /*
  1131.  * ATAPI Byte Count Register.
  1132.  */
  1133. typedef union {
  1134. unsigned all :16;
  1135. struct {
  1136. unsigned low :8; /* LSB */
  1137. unsigned high :8; /* MSB */
  1138. } b;
  1139. } idetape_bcount_reg_t;
  1140. /*
  1141.  * ATAPI Interrupt Reason Register.
  1142.  */
  1143. typedef union {
  1144. unsigned all :8;
  1145. struct {
  1146. unsigned cod :1; /* Information transferred is command (1) or data (0) */
  1147. unsigned io :1; /* The device requests us to read (1) or write (0) */
  1148. unsigned reserved :6; /* Reserved */
  1149. } b;
  1150. } idetape_ireason_reg_t;
  1151. /*
  1152.  * ATAPI Drive Select Register
  1153.  */
  1154. typedef union {
  1155. unsigned all :8;
  1156. struct {
  1157. unsigned sam_lun :4; /* Should be zero with ATAPI (not used) */
  1158. unsigned drv :1; /* The responding drive will be drive 0 (0) or drive 1 (1) */
  1159. unsigned one5 :1; /* Should be set to 1 */
  1160. unsigned reserved6 :1; /* Reserved */
  1161. unsigned one7 :1; /* Should be set to 1 */
  1162. } b;
  1163. } idetape_drivesel_reg_t;
  1164. /*
  1165.  * ATAPI Device Control Register
  1166.  */
  1167. typedef union {
  1168. unsigned all :8;
  1169. struct {
  1170. unsigned zero0 :1; /* Should be set to zero */
  1171. unsigned nien :1; /* Device interrupt is disabled (1) or enabled (0) */
  1172. unsigned srst :1; /* ATA software reset. ATAPI devices should use the new ATAPI srst. */
  1173. unsigned one3 :1; /* Should be set to 1 */
  1174. unsigned reserved4567 :4; /* Reserved */
  1175. } b;
  1176. } idetape_control_reg_t;
  1177. /*
  1178.  * idetape_chrdev_t provides the link between out character device
  1179.  * interface and our block device interface and the corresponding
  1180.  * ide_drive_t structure.
  1181.  */
  1182. typedef struct {
  1183. ide_drive_t *drive;
  1184. } idetape_chrdev_t;
  1185. /*
  1186.  * The following is used to format the general configuration word of
  1187.  * the ATAPI IDENTIFY DEVICE command.
  1188.  */
  1189. struct idetape_id_gcw {
  1190. unsigned packet_size :2; /* Packet Size */
  1191. unsigned reserved234 :3; /* Reserved */
  1192. unsigned drq_type :2; /* Command packet DRQ type */
  1193. unsigned removable :1; /* Removable media */
  1194. unsigned device_type :5; /* Device type */
  1195. unsigned reserved13 :1; /* Reserved */
  1196. unsigned protocol :2; /* Protocol type */
  1197. };
  1198. /*
  1199.  * INQUIRY packet command - Data Format (From Table 6-8 of QIC-157C)
  1200.  */
  1201. typedef struct {
  1202. unsigned device_type :5; /* Peripheral Device Type */
  1203. unsigned reserved0_765 :3; /* Peripheral Qualifier - Reserved */
  1204. unsigned reserved1_6t0 :7; /* Reserved */
  1205. unsigned rmb :1; /* Removable Medium Bit */
  1206. unsigned ansi_version :3; /* ANSI Version */
  1207. unsigned ecma_version :3; /* ECMA Version */
  1208. unsigned iso_version :2; /* ISO Version */
  1209. unsigned response_format :4; /* Response Data Format */
  1210. unsigned reserved3_45 :2; /* Reserved */
  1211. unsigned reserved3_6 :1; /* TrmIOP - Reserved */
  1212. unsigned reserved3_7 :1; /* AENC - Reserved */
  1213. __u8 additional_length; /* Additional Length (total_length-4) */
  1214. __u8 rsv5, rsv6, rsv7; /* Reserved */
  1215. __u8 vendor_id[8]; /* Vendor Identification */
  1216. __u8 product_id[16]; /* Product Identification */
  1217. __u8 revision_level[4]; /* Revision Level */
  1218. __u8 vendor_specific[20]; /* Vendor Specific - Optional */
  1219. __u8 reserved56t95[40]; /* Reserved - Optional */
  1220. /* Additional information may be returned */
  1221. } idetape_inquiry_result_t;
  1222. /*
  1223.  * READ POSITION packet command - Data Format (From Table 6-57)
  1224.  */
  1225. typedef struct {
  1226. unsigned reserved0_10 :2; /* Reserved */
  1227. unsigned bpu :1; /* Block Position Unknown */
  1228. unsigned reserved0_543 :3; /* Reserved */
  1229. unsigned eop :1; /* End Of Partition */
  1230. unsigned bop :1; /* Beginning Of Partition */
  1231. u8 partition; /* Partition Number */
  1232. u8 reserved2, reserved3; /* Reserved */
  1233. u32 first_block; /* First Block Location */
  1234. u32 last_block; /* Last Block Location (Optional) */
  1235. u8 reserved12; /* Reserved */
  1236. u8 blocks_in_buffer[3]; /* Blocks In Buffer - (Optional) */
  1237. u32 bytes_in_buffer; /* Bytes In Buffer (Optional) */
  1238. } idetape_read_position_result_t;
  1239. /*
  1240.  * Follows structures which are related to the SELECT SENSE / MODE SENSE
  1241.  * packet commands. Those packet commands are still not supported
  1242.  * by ide-tape.
  1243.  */
  1244. #define IDETAPE_BLOCK_DESCRIPTOR 0
  1245. #define IDETAPE_CAPABILITIES_PAGE 0x2a
  1246. #define IDETAPE_PARAMTR_PAGE 0x2b   /* Onstream DI-x0 only */
  1247. #define IDETAPE_BLOCK_SIZE_PAGE 0x30
  1248. #define IDETAPE_BUFFER_FILLING_PAGE 0x33
  1249. /*
  1250.  * Mode Parameter Header for the MODE SENSE packet command
  1251.  */
  1252. typedef struct {
  1253. __u8 mode_data_length; /* Length of the following data transfer */
  1254. __u8 medium_type; /* Medium Type */
  1255. __u8 dsp; /* Device Specific Parameter */
  1256. __u8 bdl; /* Block Descriptor Length */
  1257. #if 0
  1258. /* data transfer page */
  1259. __u8 page_code :6;
  1260. __u8 reserved0_6 :1;
  1261. __u8 ps :1; /* parameters saveable */
  1262. __u8 page_length; /* page Length == 0x02 */
  1263. __u8 reserved2;
  1264. __u8 read32k :1; /* 32k blk size (data only) */
  1265. __u8 read32k5 :1; /* 32.5k blk size (data&AUX) */
  1266. __u8 reserved3_23 :2;
  1267. __u8 write32k :1; /* 32k blk size (data only) */
  1268. __u8 write32k5 :1; /* 32.5k blk size (data&AUX) */
  1269. __u8 reserved3_6 :1;
  1270. __u8 streaming :1; /* streaming mode enable */
  1271. #endif
  1272. } idetape_mode_parameter_header_t;
  1273. /*
  1274.  * Mode Parameter Block Descriptor the MODE SENSE packet command
  1275.  *
  1276.  * Support for block descriptors is optional.
  1277.  */
  1278. typedef struct {
  1279. __u8 density_code; /* Medium density code */
  1280. __u8 blocks[3]; /* Number of blocks */
  1281. __u8 reserved4; /* Reserved */
  1282. __u8 length[3]; /* Block Length */
  1283. } idetape_parameter_block_descriptor_t;
  1284. /*
  1285.  * The Data Compression Page, as returned by the MODE SENSE packet command.
  1286.  */
  1287. typedef struct {
  1288. unsigned page_code :6; /* Page Code - Should be 0xf */
  1289. unsigned reserved0 :1; /* Reserved */
  1290. unsigned ps :1;
  1291. __u8 page_length; /* Page Length - Should be 14 */
  1292. unsigned reserved2 :6; /* Reserved */
  1293. unsigned dcc :1; /* Data Compression Capable */
  1294. unsigned dce :1; /* Data Compression Enable */
  1295. unsigned reserved3 :5; /* Reserved */
  1296. unsigned red :2; /* Report Exception on Decompression */
  1297. unsigned dde :1; /* Data Decompression Enable */
  1298. __u32 ca; /* Compression Algorithm */
  1299. __u32 da; /* Decompression Algorithm */
  1300. __u8 reserved[4]; /* Reserved */
  1301. } idetape_data_compression_page_t;
  1302. /*
  1303.  * The Medium Partition Page, as returned by the MODE SENSE packet command.
  1304.  */
  1305. typedef struct {
  1306. unsigned page_code :6; /* Page Code - Should be 0x11 */
  1307. unsigned reserved1_6 :1; /* Reserved */
  1308. unsigned ps :1;
  1309. __u8 page_length; /* Page Length - Should be 6 */
  1310. __u8 map; /* Maximum Additional Partitions - Should be 0 */
  1311. __u8 apd; /* Additional Partitions Defined - Should be 0 */
  1312. unsigned reserved4_012 :3; /* Reserved */
  1313. unsigned psum :2; /* Should be 0 */
  1314. unsigned idp :1; /* Should be 0 */
  1315. unsigned sdp :1; /* Should be 0 */
  1316. unsigned fdp :1; /* Fixed Data Partitions */
  1317. __u8 mfr; /* Medium Format Recognition */
  1318. __u8 reserved[2]; /* Reserved */
  1319. } idetape_medium_partition_page_t;
  1320. /*
  1321.  * Run time configurable parameters.
  1322.  */
  1323. typedef struct {
  1324. int dsc_rw_frequency;
  1325. int dsc_media_access_frequency;
  1326. int nr_stages;
  1327. } idetape_config_t;
  1328. /*
  1329.  * The variables below are used for the character device interface.
  1330.  * Additional state variables are defined in our ide_drive_t structure.
  1331.  */
  1332. static idetape_chrdev_t idetape_chrdevs[MAX_HWIFS * MAX_DRIVES];
  1333. static int idetape_chrdev_present = 0;
  1334. #if IDETAPE_DEBUG_LOG_VERBOSE
  1335. /*
  1336.  * DO NOT REMOVE, BUILDING A VERBOSE DEBUG SCHEME FOR ATAPI
  1337.  */
  1338. char *idetape_sense_key_verbose (byte idetape_sense_key)
  1339. {
  1340. switch (idetape_sense_key) {
  1341. default: {
  1342. char buf[22];
  1343. sprintf(buf, "IDETAPE_SENSE (0x%02x)", idetape_sense_key);
  1344. return(buf);
  1345. }
  1346. }
  1347. }
  1348. char *idetape_command_key_verbose (byte idetape_command_key)
  1349. {
  1350. switch (idetape_command_key) {
  1351. case IDETAPE_TEST_UNIT_READY_CMD: return("TEST_UNIT_READY_CMD");
  1352. case IDETAPE_REWIND_CMD: return("REWIND_CMD");
  1353. case IDETAPE_REQUEST_SENSE_CMD: return("REQUEST_SENSE_CMD");
  1354. case IDETAPE_READ_CMD: return("READ_CMD");
  1355. case IDETAPE_WRITE_CMD: return("WRITE_CMD");
  1356. case IDETAPE_WRITE_FILEMARK_CMD: return("WRITE_FILEMARK_CMD");
  1357. case IDETAPE_SPACE_CMD: return("SPACE_CMD");
  1358. case IDETAPE_INQUIRY_CMD: return("INQUIRY_CMD");
  1359. case IDETAPE_ERASE_CMD: return("ERASE_CMD");
  1360. case IDETAPE_MODE_SENSE_CMD: return("MODE_SENSE_CMD");
  1361. case IDETAPE_MODE_SELECT_CMD: return("MODE_SELECT_CMD");
  1362. case IDETAPE_LOAD_UNLOAD_CMD: return("LOAD_UNLOAD_CMD");
  1363. case IDETAPE_PREVENT_CMD: return("PREVENT_CMD");
  1364. case IDETAPE_LOCATE_CMD: return("LOCATE_CMD");
  1365. case IDETAPE_READ_POSITION_CMD: return("READ_POSITION_CMD");
  1366. case IDETAPE_READ_BUFFER_CMD: return("READ_BUFFER_CMD");
  1367. case IDETAPE_SET_SPEED_CMD: return("SET_SPEED_CMD");
  1368. default: {
  1369. char buf[20];
  1370. sprintf(buf, "CMD (0x%02x)", idetape_command_key);
  1371. return(buf);
  1372. }
  1373. }
  1374. }
  1375. #endif /* IDETAPE_DEBUG_LOG_VERBOSE */
  1376. /*
  1377.  *      Function declarations
  1378.  *
  1379.  */
  1380. static void idetape_onstream_mode_sense_tape_parameter_page(ide_drive_t *drive, int debug);
  1381. static int idetape_chrdev_release (struct inode *inode, struct file *filp);
  1382. static void idetape_write_release (struct inode *inode);
  1383. /*
  1384.  * Too bad. The drive wants to send us data which we are not ready to accept.
  1385.  * Just throw it away.
  1386.  */
  1387. static void idetape_discard_data (ide_drive_t *drive, unsigned int bcount)
  1388. {
  1389. while (bcount--)
  1390. IN_BYTE (IDE_DATA_REG);
  1391. }
  1392. static void idetape_input_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
  1393. {
  1394. struct buffer_head *bh = pc->bh;
  1395. int count;
  1396. while (bcount) {
  1397. #if IDETAPE_DEBUG_BUGS
  1398. if (bh == NULL) {
  1399. printk (KERN_ERR "ide-tape: bh == NULL in idetape_input_buffersn");
  1400. idetape_discard_data (drive, bcount);
  1401. return;
  1402. }
  1403. #endif /* IDETAPE_DEBUG_BUGS */
  1404. count = IDE_MIN (bh->b_size - atomic_read(&bh->b_count), bcount);
  1405. atapi_input_bytes (drive, bh->b_data + atomic_read(&bh->b_count), count);
  1406. bcount -= count;
  1407. atomic_add(count, &bh->b_count);
  1408. if (atomic_read(&bh->b_count) == bh->b_size) {
  1409. bh = bh->b_reqnext;
  1410. if (bh)
  1411. atomic_set(&bh->b_count, 0);
  1412. }
  1413. }
  1414. pc->bh = bh;
  1415. }
  1416. static void idetape_output_buffers (ide_drive_t *drive, idetape_pc_t *pc, unsigned int bcount)
  1417. {
  1418. struct buffer_head *bh = pc->bh;
  1419. int count;
  1420. while (bcount) {
  1421. #if IDETAPE_DEBUG_BUGS
  1422. if (bh == NULL) {
  1423. printk (KERN_ERR "ide-tape: bh == NULL in idetape_output_buffersn");
  1424. return;
  1425. }
  1426. #endif /* IDETAPE_DEBUG_BUGS */
  1427. count = IDE_MIN (pc->b_count, bcount);
  1428. atapi_output_bytes (drive, pc->b_data, count);
  1429. bcount -= count;
  1430. pc->b_data += count;
  1431. pc->b_count -= count;
  1432. if (!pc->b_count) {
  1433. pc->bh = bh = bh->b_reqnext;
  1434. if (bh) {
  1435. pc->b_data = bh->b_data;
  1436. pc->b_count = atomic_read(&bh->b_count);
  1437. }
  1438. }
  1439. }
  1440. }
  1441. #ifdef CONFIG_BLK_DEV_IDEDMA
  1442. static void idetape_update_buffers (idetape_pc_t *pc)
  1443. {
  1444. struct buffer_head *bh = pc->bh;
  1445. int count, bcount = pc->actually_transferred;
  1446. if (test_bit (PC_WRITING, &pc->flags))
  1447. return;
  1448. while (bcount) {
  1449. #if IDETAPE_DEBUG_BUGS
  1450. if (bh == NULL) {
  1451. printk (KERN_ERR "ide-tape: bh == NULL in idetape_update_buffersn");
  1452. return;
  1453. }
  1454. #endif /* IDETAPE_DEBUG_BUGS */
  1455. count = IDE_MIN (bh->b_size, bcount);
  1456. atomic_set(&bh->b_count, count);
  1457. if (atomic_read(&bh->b_count) == bh->b_size)
  1458. bh = bh->b_reqnext;
  1459. bcount -= count;
  1460. }
  1461. pc->bh = bh;
  1462. }
  1463. #endif /* CONFIG_BLK_DEV_IDEDMA */
  1464. /*
  1465.  * idetape_next_pc_storage returns a pointer to a place in which we can
  1466.  * safely store a packet command, even though we intend to leave the
  1467.  * driver. A storage space for a maximum of IDETAPE_PC_STACK packet
  1468.  * commands is allocated at initialization time.
  1469.  */
  1470. static idetape_pc_t *idetape_next_pc_storage (ide_drive_t *drive)
  1471. {
  1472. idetape_tape_t *tape = drive->driver_data;
  1473. #if IDETAPE_DEBUG_LOG
  1474. if (tape->debug_level >= 5)
  1475. printk (KERN_INFO "ide-tape: pc_stack_index=%dn",tape->pc_stack_index);
  1476. #endif /* IDETAPE_DEBUG_LOG */
  1477. if (tape->pc_stack_index==IDETAPE_PC_STACK)
  1478. tape->pc_stack_index=0;
  1479. return (&tape->pc_stack[tape->pc_stack_index++]);
  1480. }
  1481. /*
  1482.  * idetape_next_rq_storage is used along with idetape_next_pc_storage.
  1483.  * Since we queue packet commands in the request queue, we need to
  1484.  * allocate a request, along with the allocation of a packet command.
  1485.  */
  1486.  
  1487. /**************************************************************
  1488.  *                                                            *
  1489.  *  This should get fixed to use kmalloc(.., GFP_ATOMIC)      *
  1490.  *  followed later on by kfree().   -ml                       *
  1491.  *                                                            *
  1492.  **************************************************************/
  1493.  
  1494. static struct request *idetape_next_rq_storage (ide_drive_t *drive)
  1495. {
  1496. idetape_tape_t *tape = drive->driver_data;
  1497. #if IDETAPE_DEBUG_LOG
  1498. if (tape->debug_level >= 5)
  1499. printk (KERN_INFO "ide-tape: rq_stack_index=%dn",tape->rq_stack_index);
  1500. #endif /* IDETAPE_DEBUG_LOG */
  1501. if (tape->rq_stack_index==IDETAPE_PC_STACK)
  1502. tape->rq_stack_index=0;
  1503. return (&tape->rq_stack[tape->rq_stack_index++]);
  1504. }
  1505. /*
  1506.  * idetape_init_pc initializes a packet command.
  1507.  */
  1508. static void idetape_init_pc (idetape_pc_t *pc)
  1509. {
  1510. memset (pc->c, 0, 12);
  1511. pc->retries = 0;
  1512. pc->flags = 0;
  1513. pc->request_transfer = 0;
  1514. pc->buffer = pc->pc_buffer;
  1515. pc->buffer_size = IDETAPE_PC_BUFFER_SIZE;
  1516. pc->bh = NULL;
  1517. pc->b_data = NULL;
  1518. }
  1519. /*
  1520.  * idetape_analyze_error is called on each failed packet command retry
  1521.  * to analyze the request sense. We currently do not utilize this
  1522.  * information.
  1523.  */
  1524. static void idetape_analyze_error (ide_drive_t *drive, idetape_request_sense_result_t *result)
  1525. {
  1526. idetape_tape_t *tape = drive->driver_data;
  1527. idetape_pc_t *pc = tape->failed_pc;
  1528. tape->sense     = *result;
  1529. tape->sense_key = result->sense_key;
  1530. tape->asc       = result->asc;
  1531. tape->ascq      = result->ascq;
  1532. #if IDETAPE_DEBUG_LOG
  1533. /*
  1534.  * Without debugging, we only log an error if we decided to
  1535.  * give up retrying.
  1536.  */
  1537. if (tape->debug_level >= 1)
  1538. printk (KERN_INFO "ide-tape: pc = %x, sense key = %x, asc = %x, ascq = %xn",
  1539. pc->c[0], result->sense_key, result->asc, result->ascq);
  1540. #if IDETAPE_DEBUG_LOG_VERBOSE
  1541. if (tape->debug_level >= 1)
  1542. printk (KERN_INFO "ide-tape: pc = %s, sense key = %x, asc = %x, ascq = %xn",
  1543. idetape_command_key_verbose((byte) pc->c[0]),
  1544. result->sense_key,
  1545. result->asc,
  1546. result->ascq);
  1547. #endif /* IDETAPE_DEBUG_LOG_VERBOSE */
  1548. #endif /* IDETAPE_DEBUG_LOG */
  1549. if (tape->onstream && result->sense_key == 2 && result->asc == 0x53 && result->ascq == 2) {
  1550. clear_bit(PC_DMA_ERROR, &pc->flags);
  1551. ide_stall_queue(drive, HZ / 2);
  1552. return;
  1553. }
  1554. #ifdef CONFIG_BLK_DEV_IDEDMA
  1555. /*
  1556.  * Correct pc->actually_transferred by asking the tape.
  1557.  */
  1558. if (test_bit (PC_DMA_ERROR, &pc->flags)) {
  1559. pc->actually_transferred = pc->request_transfer - tape->tape_block_size * ntohl (get_unaligned (&result->information));
  1560. idetape_update_buffers (pc);
  1561. }
  1562. #endif /* CONFIG_BLK_DEV_IDEDMA */
  1563. if (pc->c[0] == IDETAPE_READ_CMD && result->filemark) {
  1564. pc->error = IDETAPE_ERROR_FILEMARK;
  1565. set_bit (PC_ABORT, &pc->flags);
  1566. }
  1567. if (pc->c[0] == IDETAPE_WRITE_CMD) {
  1568. if (result->eom || (result->sense_key == 0xd && result->asc == 0x0 && result->ascq == 0x2)) {
  1569. pc->error = IDETAPE_ERROR_EOD;
  1570. set_bit (PC_ABORT, &pc->flags);
  1571. }
  1572. }
  1573. if (pc->c[0] == IDETAPE_READ_CMD || pc->c[0] == IDETAPE_WRITE_CMD) {
  1574. if (result->sense_key == 8) {
  1575. pc->error = IDETAPE_ERROR_EOD;
  1576. set_bit (PC_ABORT, &pc->flags);
  1577. }
  1578. if (!test_bit (PC_ABORT, &pc->flags) && (tape->onstream || pc->actually_transferred))
  1579. pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
  1580. }
  1581. }
  1582. static void idetape_abort_pipeline (ide_drive_t *drive)
  1583. {
  1584. idetape_tape_t *tape = drive->driver_data;
  1585. idetape_stage_t *stage = tape->next_stage;
  1586. #if IDETAPE_DEBUG_LOG
  1587. if (tape->debug_level >= 4)
  1588. printk(KERN_INFO "ide-tape: %s: idetape_abort_pipeline calledn", tape->name);
  1589. #endif
  1590. while (stage) {
  1591. if (stage->rq.cmd == IDETAPE_WRITE_RQ)
  1592. stage->rq.cmd = IDETAPE_ABORTED_WRITE_RQ;
  1593. else if (stage->rq.cmd == IDETAPE_READ_RQ)
  1594. stage->rq.cmd = IDETAPE_ABORTED_READ_RQ;
  1595. stage = stage->next;
  1596. }
  1597. }
  1598. /*
  1599.  * idetape_active_next_stage will declare the next stage as "active".
  1600.  */
  1601. static void idetape_active_next_stage (ide_drive_t *drive)
  1602. {
  1603. idetape_tape_t *tape = drive->driver_data;
  1604. idetape_stage_t *stage = tape->next_stage;
  1605. struct request *rq = &stage->rq;
  1606. #if IDETAPE_DEBUG_LOG
  1607. if (tape->debug_level >= 4)
  1608. printk (KERN_INFO "ide-tape: Reached idetape_active_next_stagen");
  1609. #endif /* IDETAPE_DEBUG_LOG */
  1610. #if IDETAPE_DEBUG_BUGS
  1611. if (stage == NULL) {
  1612. printk (KERN_ERR "ide-tape: bug: Trying to activate a non existing stagen");
  1613. return;
  1614. }
  1615. #endif /* IDETAPE_DEBUG_BUGS */
  1616. rq->buffer = NULL;
  1617. rq->bh = stage->bh;
  1618. tape->active_data_request = rq;
  1619. tape->active_stage = stage;
  1620. tape->next_stage = stage->next;
  1621. }
  1622. /*
  1623.  * idetape_increase_max_pipeline_stages is a part of the feedback
  1624.  * loop which tries to find the optimum number of stages. In the
  1625.  * feedback loop, we are starting from a minimum maximum number of
  1626.  * stages, and if we sense that the pipeline is empty, we try to
  1627.  * increase it, until we reach the user compile time memory limit.
  1628.  */
  1629. static void idetape_increase_max_pipeline_stages (ide_drive_t *drive)
  1630. {
  1631. idetape_tape_t *tape = drive->driver_data;
  1632. int increase = (tape->max_pipeline - tape->min_pipeline) / 10;
  1633. #if IDETAPE_DEBUG_LOG
  1634. if (tape->debug_level >= 4)
  1635. printk (KERN_INFO "ide-tape: Reached idetape_increase_max_pipeline_stagesn");
  1636. #endif /* IDETAPE_DEBUG_LOG */
  1637. tape->max_stages += increase;
  1638. tape->max_stages = IDE_MAX(tape->max_stages, tape->min_pipeline);
  1639. tape->max_stages = IDE_MIN(tape->max_stages, tape->max_pipeline);
  1640. }
  1641. /*
  1642.  * idetape_kfree_stage calls kfree to completely free a stage, along with
  1643.  * its related buffers.
  1644.  */
  1645. static void __idetape_kfree_stage (idetape_stage_t *stage)
  1646. {
  1647. struct buffer_head *prev_bh, *bh = stage->bh;
  1648. int size;
  1649. while (bh != NULL) {
  1650. if (bh->b_data != NULL) {
  1651. size = (int) bh->b_size;
  1652. while (size > 0) {
  1653. free_page ((unsigned long) bh->b_data);
  1654. size -= PAGE_SIZE;
  1655. bh->b_data += PAGE_SIZE;
  1656. }
  1657. }
  1658. prev_bh = bh;
  1659. bh = bh->b_reqnext;
  1660. kfree (prev_bh);
  1661. }
  1662. kfree (stage);
  1663. }
  1664. static void idetape_kfree_stage (idetape_tape_t *tape, idetape_stage_t *stage)
  1665. {
  1666. __idetape_kfree_stage (stage);
  1667. }
  1668. /*
  1669.  * idetape_remove_stage_head removes tape->first_stage from the pipeline.
  1670.  * The caller should avoid race conditions.
  1671.  */
  1672. static void idetape_remove_stage_head (ide_drive_t *drive)
  1673. {
  1674. idetape_tape_t *tape = drive->driver_data;
  1675. idetape_stage_t *stage;
  1676. #if IDETAPE_DEBUG_LOG
  1677. if (tape->debug_level >= 4)
  1678. printk (KERN_INFO "ide-tape: Reached idetape_remove_stage_headn");
  1679. #endif /* IDETAPE_DEBUG_LOG */
  1680. #if IDETAPE_DEBUG_BUGS
  1681. if (tape->first_stage == NULL) {
  1682. printk (KERN_ERR "ide-tape: bug: tape->first_stage is NULLn");
  1683. return;
  1684. }
  1685. if (tape->active_stage == tape->first_stage) {
  1686. printk (KERN_ERR "ide-tape: bug: Trying to free our active pipeline stagen");
  1687. return;
  1688. }
  1689. #endif /* IDETAPE_DEBUG_BUGS */
  1690. stage = tape->first_stage;
  1691. tape->first_stage = stage->next;
  1692. idetape_kfree_stage (tape, stage);
  1693. tape->nr_stages--;
  1694. if (tape->first_stage == NULL) {
  1695. tape->last_stage = NULL;
  1696. #if IDETAPE_DEBUG_BUGS
  1697. if (tape->next_stage != NULL)
  1698. printk (KERN_ERR "ide-tape: bug: tape->next_stage != NULLn");
  1699. if (tape->nr_stages)
  1700. printk (KERN_ERR "ide-tape: bug: nr_stages should be 0 nown");
  1701. #endif /* IDETAPE_DEBUG_BUGS */
  1702. }
  1703. }
  1704. /*
  1705.  * idetape_end_request is used to finish servicing a request, and to
  1706.  * insert a pending pipeline request into the main device queue.
  1707.  */
  1708. static void idetape_end_request (byte uptodate, ide_hwgroup_t *hwgroup)
  1709. {
  1710. ide_drive_t *drive = hwgroup->drive;
  1711. struct request *rq = hwgroup->rq;
  1712. idetape_tape_t *tape = drive->driver_data;
  1713. unsigned long flags;
  1714. int error;
  1715. int remove_stage = 0;
  1716. #if ONSTREAM_DEBUG
  1717. idetape_stage_t *stage;
  1718. os_aux_t *aux;
  1719. unsigned char *p;
  1720. #endif
  1721. #if IDETAPE_DEBUG_LOG
  1722.         if (tape->debug_level >= 4)
  1723. printk (KERN_INFO "ide-tape: Reached idetape_end_requestn");
  1724. #endif /* IDETAPE_DEBUG_LOG */
  1725. switch (uptodate) {
  1726. case 0: error = IDETAPE_ERROR_GENERAL; break;
  1727. case 1: error = 0; break;
  1728. default: error = uptodate;
  1729. }
  1730. rq->errors = error;
  1731. if (error)
  1732. tape->failed_pc = NULL;
  1733. spin_lock_irqsave(&tape->spinlock, flags);
  1734. if (tape->active_data_request == rq) { /* The request was a pipelined data transfer request */
  1735. tape->active_stage = NULL;
  1736. tape->active_data_request = NULL;
  1737. tape->nr_pending_stages--;
  1738. if (rq->cmd == IDETAPE_WRITE_RQ) {
  1739. #if ONSTREAM_DEBUG
  1740. if (tape->debug_level >= 2) {
  1741. if (tape->onstream) {
  1742. stage = tape->first_stage;
  1743. aux = stage->aux;
  1744. p = stage->bh->b_data;
  1745. if (ntohl(aux->logical_blk_num) < 11300 && ntohl(aux->logical_blk_num) > 11100)
  1746. printk(KERN_INFO "ide-tape: finished writing logical blk %u (data %x %x %x %x)n", ntohl(aux->logical_blk_num), *p++, *p++, *p++, *p++);
  1747. }
  1748. }
  1749. #endif
  1750. if (tape->onstream && !tape->raw) {
  1751. if (tape->first_frame_position == OS_DATA_ENDFRAME1) { 
  1752. #if ONSTREAM_DEBUG
  1753. if (tape->debug_level >= 2)
  1754. printk("ide-tape: %s: skipping over config parition..n", tape->name);
  1755. #endif
  1756. tape->onstream_write_error = OS_PART_ERROR;
  1757. if (tape->waiting)
  1758. complete(tape->waiting);
  1759. }
  1760. }
  1761. remove_stage = 1;
  1762. if (error) {
  1763. set_bit (IDETAPE_PIPELINE_ERROR, &tape->flags);
  1764. if (error == IDETAPE_ERROR_EOD)
  1765. idetape_abort_pipeline (drive);
  1766. if (tape->onstream && !tape->raw && error == IDETAPE_ERROR_GENERAL && tape->sense.sense_key == 3) {
  1767. clear_bit (IDETAPE_PIPELINE_ERROR, &tape->flags);
  1768. printk(KERN_ERR "ide-tape: %s: write error, enabling error recoveryn", tape->name);
  1769. tape->onstream_write_error = OS_WRITE_ERROR;
  1770. remove_stage = 0;
  1771. tape->nr_pending_stages++;
  1772. tape->next_stage = tape->first_stage;
  1773. rq->current_nr_sectors = rq->nr_sectors;
  1774. if (tape->waiting)
  1775. complete(tape->waiting);
  1776. }
  1777. }
  1778. } else if (rq->cmd == IDETAPE_READ_RQ) {
  1779. if (error == IDETAPE_ERROR_EOD) {
  1780. set_bit (IDETAPE_PIPELINE_ERROR, &tape->flags);
  1781. idetape_abort_pipeline(drive);
  1782. }
  1783. }
  1784. if (tape->next_stage != NULL && !tape->onstream_write_error) {
  1785. idetape_active_next_stage (drive);
  1786. /*
  1787.  * Insert the next request into the request queue.
  1788.  */
  1789. (void) ide_do_drive_cmd (drive, tape->active_data_request, ide_end);
  1790. } else if (!error) {
  1791. if (!tape->onstream)
  1792. idetape_increase_max_pipeline_stages (drive);
  1793. }
  1794. }
  1795. ide_end_drive_cmd (drive, 0, 0);
  1796. if (remove_stage)
  1797. idetape_remove_stage_head (drive);
  1798. if (tape->active_data_request == NULL)
  1799. clear_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
  1800. spin_unlock_irqrestore(&tape->spinlock, flags);
  1801. }
  1802. static ide_startstop_t idetape_request_sense_callback (ide_drive_t *drive)
  1803. {
  1804. idetape_tape_t *tape = drive->driver_data;
  1805. #if IDETAPE_DEBUG_LOG
  1806. if (tape->debug_level >= 4)
  1807. printk (KERN_INFO "ide-tape: Reached idetape_request_sense_callbackn");
  1808. #endif /* IDETAPE_DEBUG_LOG */
  1809. if (!tape->pc->error) {
  1810. idetape_analyze_error (drive, (idetape_request_sense_result_t *) tape->pc->buffer);
  1811. idetape_end_request (1, HWGROUP (drive));
  1812. } else {
  1813. printk (KERN_ERR "ide-tape: Error in REQUEST SENSE itself - Aborting request!n");
  1814. idetape_end_request (0, HWGROUP (drive));
  1815. }
  1816. return ide_stopped;
  1817. }
  1818. static void idetape_create_request_sense_cmd (idetape_pc_t *pc)
  1819. {
  1820. idetape_init_pc (pc);
  1821. pc->c[0] = IDETAPE_REQUEST_SENSE_CMD;
  1822. pc->c[4] = 20;
  1823. pc->request_transfer = 18;
  1824. pc->callback = &idetape_request_sense_callback;
  1825. }
  1826. /*
  1827.  * idetape_queue_pc_head generates a new packet command request in front
  1828.  * of the request queue, before the current request, so that it will be
  1829.  * processed immediately, on the next pass through the driver.
  1830.  *
  1831.  * idetape_queue_pc_head is called from the request handling part of
  1832.  * the driver (the "bottom" part). Safe storage for the request should
  1833.  * be allocated with idetape_next_pc_storage and idetape_next_rq_storage
  1834.  * before calling idetape_queue_pc_head.
  1835.  *
  1836.  * Memory for those requests is pre-allocated at initialization time, and
  1837.  * is limited to IDETAPE_PC_STACK requests. We assume that we have enough
  1838.  * space for the maximum possible number of inter-dependent packet commands.
  1839.  *
  1840.  * The higher level of the driver - The ioctl handler and the character
  1841.  * device handling functions should queue request to the lower level part
  1842.  * and wait for their completion using idetape_queue_pc_tail or
  1843.  * idetape_queue_rw_tail.
  1844.  */
  1845. static void idetape_queue_pc_head (ide_drive_t *drive,idetape_pc_t *pc,struct request *rq)
  1846. {
  1847. ide_init_drive_cmd (rq);
  1848. rq->buffer = (char *) pc;
  1849. rq->cmd = IDETAPE_PC_RQ1;
  1850. (void) ide_do_drive_cmd (drive, rq, ide_preempt);
  1851. }
  1852. /*
  1853.  * idetape_retry_pc is called when an error was detected during the
  1854.  * last packet command. We queue a request sense packet command in
  1855.  * the head of the request list.
  1856.  */
  1857. static ide_startstop_t idetape_retry_pc (ide_drive_t *drive)
  1858. {
  1859. idetape_tape_t *tape = drive->driver_data;
  1860. idetape_pc_t *pc;
  1861. struct request *rq;
  1862. idetape_error_reg_t error;
  1863. error.all = IN_BYTE (IDE_ERROR_REG);
  1864. pc = idetape_next_pc_storage (drive);
  1865. rq = idetape_next_rq_storage (drive);
  1866. idetape_create_request_sense_cmd (pc);
  1867. set_bit (IDETAPE_IGNORE_DSC, &tape->flags);
  1868. idetape_queue_pc_head (drive, pc, rq);
  1869. return ide_stopped;
  1870. }
  1871. /*
  1872.  * idetape_postpone_request postpones the current request so that
  1873.  * ide.c will be able to service requests from another device on
  1874.  * the same hwgroup while we are polling for DSC.
  1875.  */
  1876. static void idetape_postpone_request (ide_drive_t *drive)
  1877. {
  1878. idetape_tape_t *tape = drive->driver_data;
  1879. #if IDETAPE_DEBUG_LOG
  1880. if (tape->debug_level >= 4)
  1881. printk(KERN_INFO "ide-tape: idetape_postpone_requestn");
  1882. #endif
  1883. tape->postponed_rq = HWGROUP(drive)->rq;
  1884. ide_stall_queue(drive, tape->dsc_polling_frequency);
  1885. }
  1886. /*
  1887.  * idetape_pc_intr is the usual interrupt handler which will be called
  1888.  * during a packet command. We will transfer some of the data (as
  1889.  * requested by the drive) and will re-point interrupt handler to us.
  1890.  * When data transfer is finished, we will act according to the
  1891.  * algorithm described before idetape_issue_packet_command.
  1892.  *
  1893.  */
  1894. static ide_startstop_t idetape_pc_intr (ide_drive_t *drive)
  1895. {
  1896. idetape_tape_t *tape = drive->driver_data;
  1897. idetape_status_reg_t status;
  1898. idetape_bcount_reg_t bcount;
  1899. idetape_ireason_reg_t ireason;
  1900. idetape_pc_t *pc = tape->pc;
  1901. unsigned int temp;
  1902. unsigned long cmd_time;
  1903. #if SIMULATE_ERRORS
  1904. static int error_sim_count = 0;
  1905. #endif
  1906. #if IDETAPE_DEBUG_LOG
  1907. if (tape->debug_level >= 4)
  1908. printk (KERN_INFO "ide-tape: Reached idetape_pc_intr interrupt handlern");
  1909. #endif /* IDETAPE_DEBUG_LOG */
  1910. status.all = GET_STAT(); /* Clear the interrupt */
  1911. #ifdef CONFIG_BLK_DEV_IDEDMA
  1912. if (test_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
  1913. if (HWIF(drive)->dmaproc(ide_dma_end, drive)) {
  1914. /*
  1915.  * A DMA error is sometimes expected. For example,
  1916.  * if the tape is crossing a filemark during a
  1917.  * READ command, it will issue an irq and position
  1918.  * itself before the filemark, so that only a partial
  1919.  * data transfer will occur (which causes the DMA
  1920.  * error). In that case, we will later ask the tape
  1921.  * how much bytes of the original request were
  1922.  * actually transferred (we can't receive that
  1923.  * information from the DMA engine on most chipsets).
  1924.  */
  1925. set_bit (PC_DMA_ERROR, &pc->flags);
  1926. } else if (!status.b.check) {
  1927. pc->actually_transferred = pc->request_transfer;
  1928. idetape_update_buffers (pc);
  1929. }
  1930. #if IDETAPE_DEBUG_LOG
  1931. if (tape->debug_level >= 4)
  1932. printk (KERN_INFO "ide-tape: DMA finishedn");
  1933. #endif /* IDETAPE_DEBUG_LOG */
  1934. }
  1935. #endif /* CONFIG_BLK_DEV_IDEDMA */
  1936. if (!status.b.drq) { /* No more interrupts */
  1937. cmd_time = (jiffies - tape->cmd_start_time) * 1000 / HZ;
  1938. tape->max_cmd_time = IDE_MAX(cmd_time, tape->max_cmd_time);
  1939. #if IDETAPE_DEBUG_LOG
  1940. if (tape->debug_level >= 2)
  1941. printk (KERN_INFO "ide-tape: Packet command completed, %d bytes transferredn", pc->actually_transferred);
  1942. #endif /* IDETAPE_DEBUG_LOG */
  1943. clear_bit (PC_DMA_IN_PROGRESS, &pc->flags);
  1944. ide__sti(); /* local CPU only */
  1945. #if SIMULATE_ERRORS
  1946. if ((pc->c[0] == IDETAPE_WRITE_CMD || pc->c[0] == IDETAPE_READ_CMD) && (++error_sim_count % 100) == 0) {
  1947. printk(KERN_INFO "ide-tape: %s: simulating errorn", tape->name);
  1948. status.b.check = 1;
  1949. }
  1950. #endif
  1951. if (status.b.check && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD)
  1952. status.b.check = 0;
  1953. if (status.b.check || test_bit (PC_DMA_ERROR, &pc->flags)) { /* Error detected */
  1954. #if IDETAPE_DEBUG_LOG
  1955. if (tape->debug_level >= 1)
  1956. printk (KERN_INFO "ide-tape: %s: I/O error, ",tape->name);
  1957. #endif /* IDETAPE_DEBUG_LOG */
  1958. if (pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
  1959. printk (KERN_ERR "ide-tape: I/O error in request sense commandn");
  1960. return ide_do_reset (drive);
  1961. }
  1962. #if IDETAPE_DEBUG_LOG
  1963. if (tape->debug_level >= 1)
  1964. printk(KERN_INFO "ide-tape: [cmd %x]: check conditionn", pc->c[0]);
  1965. #endif
  1966. return idetape_retry_pc (drive); /* Retry operation */
  1967. }
  1968. pc->error = 0;
  1969. if (!tape->onstream && test_bit (PC_WAIT_FOR_DSC, &pc->flags) && !status.b.dsc) { /* Media access command */
  1970. tape->dsc_polling_start = jiffies;
  1971. tape->dsc_polling_frequency = IDETAPE_DSC_MA_FAST;
  1972. tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
  1973. idetape_postpone_request (drive); /* Allow ide.c to handle other requests */
  1974. return ide_stopped;
  1975. }
  1976. if (tape->failed_pc == pc)
  1977. tape->failed_pc = NULL;
  1978. return pc->callback(drive); /* Command finished - Call the callback function */
  1979. }
  1980. #ifdef CONFIG_BLK_DEV_IDEDMA
  1981. if (test_and_clear_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
  1982. printk (KERN_ERR "ide-tape: The tape wants to issue more interrupts in DMA moden");
  1983. printk (KERN_ERR "ide-tape: DMA disabled, reverting to PIOn");
  1984. (void) HWIF(drive)->dmaproc(ide_dma_off, drive);
  1985. return ide_do_reset (drive);
  1986. }
  1987. #endif /* CONFIG_BLK_DEV_IDEDMA */
  1988. bcount.b.high = IN_BYTE (IDE_BCOUNTH_REG); /* Get the number of bytes to transfer */
  1989. bcount.b.low  = IN_BYTE (IDE_BCOUNTL_REG); /* on this interrupt */
  1990. ireason.all   = IN_BYTE (IDE_IREASON_REG);
  1991. if (ireason.b.cod) {
  1992. printk (KERN_ERR "ide-tape: CoD != 0 in idetape_pc_intrn");
  1993. return ide_do_reset (drive);
  1994. }
  1995. if (ireason.b.io == test_bit (PC_WRITING, &pc->flags)) { /* Hopefully, we will never get here */
  1996. printk (KERN_ERR "ide-tape: We wanted to %s, ", ireason.b.io ? "Write":"Read");
  1997. printk (KERN_ERR "ide-tape: but the tape wants us to %s !n",ireason.b.io ? "Read":"Write");
  1998. return ide_do_reset (drive);
  1999. }
  2000. if (!test_bit (PC_WRITING, &pc->flags)) { /* Reading - Check that we have enough space */
  2001. temp = pc->actually_transferred + bcount.all;
  2002. if ( temp > pc->request_transfer) {
  2003. if (temp > pc->buffer_size) {
  2004. printk (KERN_ERR "ide-tape: The tape wants to send us more data than expected - discarding datan");
  2005. idetape_discard_data (drive, bcount.all);
  2006. ide_set_handler (drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL);
  2007. return ide_started;
  2008. }
  2009. #if IDETAPE_DEBUG_LOG
  2010. if (tape->debug_level >= 2)
  2011. printk (KERN_NOTICE "ide-tape: The tape wants to send us more data than expected - allowing transfern");
  2012. #endif /* IDETAPE_DEBUG_LOG */
  2013. }
  2014. }
  2015. if (test_bit (PC_WRITING, &pc->flags)) {
  2016. if (pc->bh != NULL)
  2017. idetape_output_buffers (drive, pc, bcount.all);
  2018. else
  2019. atapi_output_bytes (drive,pc->current_position,bcount.all); /* Write the current buffer */
  2020. } else {
  2021. if (pc->bh != NULL)
  2022. idetape_input_buffers (drive, pc, bcount.all);
  2023. else
  2024. atapi_input_bytes (drive,pc->current_position,bcount.all); /* Read the current buffer */
  2025. }
  2026. pc->actually_transferred += bcount.all; /* Update the current position */
  2027. pc->current_position+=bcount.all;
  2028. #if IDETAPE_DEBUG_LOG
  2029. if (tape->debug_level >= 2)
  2030. printk(KERN_INFO "ide-tape: [cmd %x] transferred %d bytes on that interruptn", pc->c[0], bcount.all);
  2031. #endif
  2032. ide_set_handler (drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL); /* And set the interrupt handler again */
  2033. return ide_started;
  2034. }
  2035. /*
  2036.  * Packet Command Interface
  2037.  *
  2038.  * The current Packet Command is available in tape->pc, and will not
  2039.  * change until we finish handling it. Each packet command is associated
  2040.  * with a callback function that will be called when the command is
  2041.  * finished.
  2042.  *
  2043.  * The handling will be done in three stages:
  2044.  *
  2045.  * 1. idetape_issue_packet_command will send the packet command to the
  2046.  * drive, and will set the interrupt handler to idetape_pc_intr.
  2047.  *
  2048.  * 2. On each interrupt, idetape_pc_intr will be called. This step
  2049.  * will be repeated until the device signals us that no more
  2050.  * interrupts will be issued.
  2051.  *
  2052.  * 3. ATAPI Tape media access commands have immediate status with a
  2053.  * delayed process. In case of a successful initiation of a
  2054.  * media access packet command, the DSC bit will be set when the
  2055.  * actual execution of the command is finished. 
  2056.  * Since the tape drive will not issue an interrupt, we have to
  2057.  * poll for this event. In this case, we define the request as
  2058.  * "low priority request" by setting rq_status to
  2059.  * IDETAPE_RQ_POSTPONED,  set a timer to poll for DSC and exit
  2060.  * the driver.
  2061.  *
  2062.  * ide.c will then give higher priority to requests which
  2063.  * originate from the other device, until will change rq_status
  2064.  * to RQ_ACTIVE.
  2065.  *
  2066.  * 4. When the packet command is finished, it will be checked for errors.
  2067.  *
  2068.  * 5. In case an error was found, we queue a request sense packet command
  2069.  * in front of the request queue and retry the operation up to
  2070.  * IDETAPE_MAX_PC_RETRIES times.
  2071.  *
  2072.  * 6. In case no error was found, or we decided to give up and not
  2073.  * to retry again, the callback function will be called and then
  2074.  * we will handle the next request.
  2075.  *
  2076.  */
  2077. static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive)
  2078. {
  2079. idetape_tape_t *tape = drive->driver_data;
  2080. idetape_pc_t *pc = tape->pc;
  2081. idetape_ireason_reg_t ireason;
  2082. int retries = 100;
  2083. ide_startstop_t startstop;
  2084. if (ide_wait_stat (&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
  2085. printk (KERN_ERR "ide-tape: Strange, packet command initiated yet DRQ isn't assertedn");
  2086. return startstop;
  2087. }
  2088. ireason.all = IN_BYTE (IDE_IREASON_REG);
  2089. while (retries-- && (!ireason.b.cod || ireason.b.io)) {
  2090. printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing a packet command, retryingn");
  2091. udelay(100);
  2092. ireason.all = IN_BYTE(IDE_IREASON_REG);
  2093. if (retries == 0) {
  2094. printk(KERN_ERR "ide-tape: (IO,CoD != (0,1) while issuing a packet command, ignoringn");
  2095. ireason.b.cod = 1;
  2096. ireason.b.io = 0;
  2097. }
  2098. }
  2099. if (!ireason.b.cod || ireason.b.io) {
  2100. printk (KERN_ERR "ide-tape: (IO,CoD) != (0,1) while issuing a packet commandn");
  2101. return ide_do_reset (drive);
  2102. }
  2103. tape->cmd_start_time = jiffies;
  2104. ide_set_handler(drive, &idetape_pc_intr, IDETAPE_WAIT_CMD, NULL); /* Set the interrupt routine */
  2105. atapi_output_bytes (drive,pc->c,12); /* Send the actual packet */
  2106. return ide_started;
  2107. }
  2108. static ide_startstop_t idetape_issue_packet_command (ide_drive_t *drive, idetape_pc_t *pc)
  2109. {
  2110. idetape_tape_t *tape = drive->driver_data;
  2111. idetape_bcount_reg_t bcount;
  2112. int dma_ok = 0;
  2113. #if IDETAPE_DEBUG_BUGS
  2114. if (tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD && pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
  2115. printk (KERN_ERR "ide-tape: possible ide-tape.c bug - Two request sense in serial were issuedn");
  2116. }
  2117. #endif /* IDETAPE_DEBUG_BUGS */
  2118. if (tape->failed_pc == NULL && pc->c[0] != IDETAPE_REQUEST_SENSE_CMD)
  2119. tape->failed_pc = pc;
  2120. tape->pc = pc; /* Set the current packet command */
  2121. if (pc->retries > IDETAPE_MAX_PC_RETRIES || test_bit (PC_ABORT, &pc->flags)) {
  2122. /*
  2123.  * We will "abort" retrying a packet command in case
  2124.  * a legitimate error code was received (crossing a
  2125.  * filemark, or DMA error in the end of media, for
  2126.  * example).
  2127.  */
  2128. if (!test_bit (PC_ABORT, &pc->flags)) {
  2129. if (!(pc->c[0] == IDETAPE_TEST_UNIT_READY_CMD && tape->sense_key == 2 &&
  2130.       tape->asc == 4 && (tape->ascq == 1 || tape->ascq == 8))) {
  2131. printk (KERN_ERR "ide-tape: %s: I/O error, pc = %2x, key = %2x, asc = %2x, ascq = %2xn",
  2132. tape->name, pc->c[0], tape->sense_key, tape->asc, tape->ascq);
  2133. if (tape->onstream && pc->c[0] == IDETAPE_READ_CMD && tape->sense_key == 3 && tape->asc == 0x11)  /* AJN-1: 11 should be 0x11 */
  2134. printk(KERN_ERR "ide-tape: %s: enabling read error recoveryn", tape->name);
  2135. }
  2136. pc->error = IDETAPE_ERROR_GENERAL; /* Giving up */
  2137. }
  2138. tape->failed_pc = NULL;
  2139. return pc->callback(drive);
  2140. }
  2141. #if IDETAPE_DEBUG_LOG
  2142. if (tape->debug_level >= 2)
  2143. printk (KERN_INFO "ide-tape: Retry number - %dn", pc->retries);
  2144. #endif /* IDETAPE_DEBUG_LOG */
  2145. pc->retries++;
  2146. pc->actually_transferred = 0; /* We haven't transferred any data yet */
  2147. pc->current_position=pc->buffer;
  2148. bcount.all=pc->request_transfer; /* Request to transfer the entire buffer at once */
  2149. #ifdef CONFIG_BLK_DEV_IDEDMA
  2150. if (test_and_clear_bit (PC_DMA_ERROR, &pc->flags)) {
  2151. printk (KERN_WARNING "ide-tape: DMA disabled, reverting to PIOn");
  2152. (void) HWIF(drive)->dmaproc(ide_dma_off, drive);
  2153. }
  2154. if (test_bit (PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
  2155. dma_ok = !HWIF(drive)->dmaproc(test_bit (PC_WRITING, &pc->flags) ? ide_dma_write : ide_dma_read, drive);
  2156. #endif /* CONFIG_BLK_DEV_IDEDMA */
  2157. if (IDE_CONTROL_REG)
  2158. OUT_BYTE (drive->ctl, IDE_CONTROL_REG);
  2159. OUT_BYTE (dma_ok ? 1 : 0,    IDE_FEATURE_REG); /* Use PIO/DMA */
  2160. OUT_BYTE (bcount.b.high,     IDE_BCOUNTH_REG);
  2161. OUT_BYTE (bcount.b.low,      IDE_BCOUNTL_REG);
  2162. OUT_BYTE (drive->select.all, IDE_SELECT_REG);
  2163. #ifdef CONFIG_BLK_DEV_IDEDMA
  2164. if (dma_ok) { /* Begin DMA, if necessary */
  2165. set_bit (PC_DMA_IN_PROGRESS, &pc->flags);
  2166. (void) (HWIF(drive)->dmaproc(ide_dma_begin, drive));
  2167. }
  2168. #endif /* CONFIG_BLK_DEV_IDEDMA */
  2169. if (test_bit(IDETAPE_DRQ_INTERRUPT, &tape->flags)) {
  2170. ide_set_handler(drive, &idetape_transfer_pc, IDETAPE_WAIT_CMD, NULL);
  2171. OUT_BYTE(WIN_PACKETCMD, IDE_COMMAND_REG);
  2172. return ide_started;
  2173. } else {
  2174. OUT_BYTE(WIN_PACKETCMD, IDE_COMMAND_REG);
  2175. return idetape_transfer_pc(drive);
  2176. }
  2177. }
  2178. /*
  2179.  * General packet command callback function.
  2180.  */
  2181. static ide_startstop_t idetape_pc_callback (ide_drive_t *drive)
  2182. {
  2183. idetape_tape_t *tape = drive->driver_data;
  2184. #if IDETAPE_DEBUG_LOG
  2185. if (tape->debug_level >= 4)
  2186. printk (KERN_INFO "ide-tape: Reached idetape_pc_callbackn");
  2187. #endif /* IDETAPE_DEBUG_LOG */
  2188. idetape_end_request (tape->pc->error ? 0 : 1, HWGROUP(drive));
  2189. return ide_stopped;
  2190. }
  2191. /*
  2192.  * A mode sense command is used to "sense" tape parameters.
  2193.  */
  2194. static void idetape_create_mode_sense_cmd (idetape_pc_t *pc, byte page_code)
  2195. {
  2196. idetape_init_pc (pc);
  2197. pc->c[0] = IDETAPE_MODE_SENSE_CMD;
  2198. if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
  2199. pc->c[1] = 8; /* DBD = 1 - Don't return block descriptors */
  2200. pc->c[2] = page_code;
  2201. pc->c[3] = 255; /* Don't limit the returned information */
  2202. pc->c[4] = 255; /* (We will just discard data in that case) */
  2203. if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
  2204. pc->request_transfer = 12;
  2205. else if (page_code == IDETAPE_CAPABILITIES_PAGE)
  2206. pc->request_transfer = 24;
  2207. else
  2208. pc->request_transfer = 50;
  2209. pc->callback = &idetape_pc_callback;
  2210. }
  2211. static ide_startstop_t idetape_onstream_buffer_fill_callback (ide_drive_t *drive)
  2212. {
  2213. idetape_tape_t *tape = drive->driver_data;
  2214. tape->max_frames = tape->pc->buffer[4 + 2];
  2215. tape->cur_frames = tape->pc->buffer[4 + 3];
  2216. if (tape->chrdev_direction == idetape_direction_write)
  2217. tape->tape_head = tape->buffer_head - tape->cur_frames;
  2218. else
  2219. tape->tape_head = tape->buffer_head + tape->cur_frames;
  2220. if (tape->tape_head != tape->last_tape_head) {
  2221. tape->last_tape_head = tape->tape_head;
  2222. tape->tape_still_time_begin = jiffies;
  2223. if (tape->tape_still_time > 200)
  2224. tape->measure_insert_time = 1;
  2225. }
  2226. tape->tape_still_time = (jiffies - tape->tape_still_time_begin) * 1000 / HZ;
  2227. #if USE_IOTRACE
  2228. IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
  2229. #endif
  2230. #if IDETAPE_DEBUG_LOG
  2231. if (tape->debug_level >= 1)
  2232. printk(KERN_INFO "ide-tape: buffer fill callback, %d/%dn", tape->cur_frames, tape->max_frames);
  2233. #endif
  2234. idetape_end_request (tape->pc->error ? 0 : 1, HWGROUP(drive));
  2235. return ide_stopped;
  2236. }
  2237. static void idetape_queue_onstream_buffer_fill (ide_drive_t *drive)
  2238. {
  2239. idetape_pc_t *pc;
  2240. struct request *rq;
  2241. pc = idetape_next_pc_storage (drive);
  2242. rq = idetape_next_rq_storage (drive);
  2243. idetape_create_mode_sense_cmd (pc, IDETAPE_BUFFER_FILLING_PAGE);
  2244. pc->callback = idetape_onstream_buffer_fill_callback;
  2245. idetape_queue_pc_head (drive, pc, rq);
  2246. }
  2247. static void calculate_speeds(ide_drive_t *drive)
  2248. {
  2249. idetape_tape_t *tape = drive->driver_data;
  2250. int full = 125, empty = 75;
  2251. if (time_after(jiffies, tape->controlled_pipeline_head_time + 120 * HZ)) {
  2252. tape->controlled_previous_pipeline_head = tape->controlled_last_pipeline_head;
  2253. tape->controlled_previous_head_time = tape->controlled_pipeline_head_time;
  2254. tape->controlled_last_pipeline_head = tape->pipeline_head;
  2255. tape->controlled_pipeline_head_time = jiffies;
  2256. }
  2257. if (time_after(jiffies, tape->controlled_pipeline_head_time + 60 * HZ))
  2258. tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_last_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_pipeline_head_time);
  2259. else if (time_after(jiffies, tape->controlled_previous_head_time))
  2260. tape->controlled_pipeline_head_speed = (tape->pipeline_head - tape->controlled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->controlled_previous_head_time);
  2261. if (tape->nr_pending_stages < tape->max_stages /*- 1 */) { /* -1 for read mode error recovery */
  2262. if (time_after(jiffies, tape->uncontrolled_previous_head_time + 10 * HZ)) {
  2263. tape->uncontrolled_pipeline_head_time = jiffies;
  2264. tape->uncontrolled_pipeline_head_speed = (tape->pipeline_head - tape->uncontrolled_previous_pipeline_head) * 32 * HZ / (jiffies - tape->uncontrolled_previous_head_time);
  2265. }
  2266. } else {
  2267. tape->uncontrolled_previous_head_time = jiffies;
  2268. tape->uncontrolled_previous_pipeline_head = tape->pipeline_head;
  2269. if (time_after(jiffies, tape->uncontrolled_pipeline_head_time + 30 * HZ)) {
  2270. tape->uncontrolled_pipeline_head_time = jiffies;
  2271. }
  2272. }
  2273. tape->pipeline_head_speed = IDE_MAX(tape->uncontrolled_pipeline_head_speed, tape->controlled_pipeline_head_speed);
  2274. if (tape->speed_control == 0) {
  2275. tape->max_insert_speed = 5000;
  2276. } else if (tape->speed_control == 1) {
  2277. if (tape->nr_pending_stages >= tape->max_stages / 2)
  2278. tape->max_insert_speed = tape->pipeline_head_speed +
  2279. (1100 - tape->pipeline_head_speed) * 2 * (tape->nr_pending_stages - tape->max_stages / 2) / tape->max_stages;
  2280. else
  2281. tape->max_insert_speed = 500 +
  2282. (tape->pipeline_head_speed - 500) * 2 * tape->nr_pending_stages / tape->max_stages;
  2283. if (tape->nr_pending_stages >= tape->max_stages * 99 / 100)
  2284. tape->max_insert_speed = 5000;
  2285. } else if (tape->speed_control == 2) {
  2286. tape->max_insert_speed = tape->pipeline_head_speed * empty / 100 +
  2287. (tape->pipeline_head_speed * full / 100 - tape->pipeline_head_speed * empty / 100) * tape->nr_pending_stages / tape->max_stages;
  2288. } else
  2289. tape->max_insert_speed = tape->speed_control;
  2290. tape->max_insert_speed = IDE_MAX(tape->max_insert_speed, 500);
  2291. }
  2292. static ide_startstop_t idetape_media_access_finished (ide_drive_t *drive)
  2293. {
  2294. idetape_tape_t *tape = drive->driver_data;
  2295. idetape_pc_t *pc = tape->pc;
  2296. idetape_status_reg_t status;
  2297. if (tape->onstream)
  2298. printk(KERN_INFO "ide-tape: bug: onstream, media_access_finishedn");
  2299. status.all = GET_STAT();
  2300. if (status.b.dsc) {
  2301. if (status.b.check) { /* Error detected */
  2302. printk (KERN_ERR "ide-tape: %s: I/O error, ",tape->name);
  2303. return idetape_retry_pc (drive); /* Retry operation */
  2304. }
  2305. pc->error = 0;
  2306. if (tape->failed_pc == pc)
  2307. tape->failed_pc = NULL;
  2308. } else {
  2309. pc->error = IDETAPE_ERROR_GENERAL;
  2310. tape->failed_pc = NULL;
  2311. }
  2312. return pc->callback (drive);
  2313. }
  2314. static ide_startstop_t idetape_rw_callback (ide_drive_t *drive)
  2315. {
  2316. idetape_tape_t *tape = drive->driver_data;
  2317. struct request *rq = HWGROUP(drive)->rq;
  2318. int blocks = tape->pc->actually_transferred / tape->tape_block_size;
  2319. tape->avg_size += blocks * tape->tape_block_size;
  2320. tape->insert_size += blocks * tape->tape_block_size;
  2321. if (tape->insert_size > 1024 * 1024)
  2322. tape->measure_insert_time = 1;
  2323. if (tape->measure_insert_time) {
  2324. tape->measure_insert_time = 0;
  2325. tape->insert_time = jiffies;
  2326. tape->insert_size = 0;
  2327. }
  2328. if (time_after(jiffies, tape->insert_time))
  2329. tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
  2330. if (jiffies - tape->avg_time >= HZ) {
  2331. tape->avg_speed = tape->avg_size * HZ / (jiffies - tape->avg_time) / 1024;
  2332. tape->avg_size = 0;
  2333. tape->avg_time = jiffies;
  2334. }
  2335. #if IDETAPE_DEBUG_LOG
  2336. if (tape->debug_level >= 4)
  2337. printk (KERN_INFO "ide-tape: Reached idetape_rw_callbackn");
  2338. #endif /* IDETAPE_DEBUG_LOG */
  2339. tape->first_frame_position += blocks;
  2340. rq->current_nr_sectors -= blocks;
  2341. if (!tape->pc->error)
  2342. idetape_end_request (1, HWGROUP (drive));
  2343. else
  2344. idetape_end_request (tape->pc->error, HWGROUP (drive));
  2345. return ide_stopped;
  2346. }
  2347. static void idetape_create_read_cmd (idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct buffer_head *bh)
  2348. {
  2349. struct buffer_head *p = bh;
  2350. idetape_init_pc (pc);
  2351. pc->c[0] = IDETAPE_READ_CMD;
  2352. put_unaligned (htonl (length), (unsigned int *) &pc->c[1]);
  2353. pc->c[1] = 1;
  2354. pc->callback = &idetape_rw_callback;
  2355. pc->bh = bh;
  2356. atomic_set(&bh->b_count, 0);
  2357. pc->buffer = NULL;
  2358. if (tape->onstream) {
  2359. while (p) {
  2360. atomic_set(&p->b_count, 0);
  2361. p = p->b_reqnext;
  2362. }
  2363. }
  2364. if (!tape->onstream) {
  2365. pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
  2366. if (pc->request_transfer == tape->stage_size)
  2367. set_bit (PC_DMA_RECOMMENDED, &pc->flags);
  2368. } else  {
  2369. if (length) {
  2370. pc->request_transfer = pc->buffer_size = 32768 + 512;
  2371. set_bit (PC_DMA_RECOMMENDED, &pc->flags);
  2372. } else
  2373. pc->request_transfer = 0;
  2374. }
  2375. }
  2376. static void idetape_create_read_buffer_cmd(idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct buffer_head *bh)
  2377. {
  2378. int size = 32768;
  2379. struct buffer_head *p = bh;
  2380. idetape_init_pc (pc);
  2381. pc->c[0] = IDETAPE_READ_BUFFER_CMD;
  2382. pc->c[1] = IDETAPE_RETRIEVE_FAULTY_BLOCK;
  2383. pc->c[7] = size >> 8;
  2384. pc->c[8] = size & 0xff;
  2385. pc->callback = &idetape_pc_callback;
  2386. pc->bh = bh;
  2387. atomic_set(&bh->b_count, 0);
  2388. pc->buffer = NULL;
  2389. while (p) {
  2390. atomic_set(&p->b_count, 0);
  2391. p = p->b_reqnext;
  2392. }
  2393. pc->request_transfer = pc->buffer_size = size;
  2394. }
  2395. static void idetape_create_write_cmd (idetape_tape_t *tape, idetape_pc_t *pc, unsigned int length, struct buffer_head *bh)
  2396. {
  2397. struct buffer_head *p = bh;
  2398. idetape_init_pc (pc);
  2399. pc->c[0] = IDETAPE_WRITE_CMD;
  2400. put_unaligned (htonl (length), (unsigned int *) &pc->c[1]);
  2401. pc->c[1] = 1;
  2402. pc->callback = &idetape_rw_callback;
  2403. set_bit (PC_WRITING, &pc->flags);
  2404. if (tape->onstream) {
  2405. while (p) {
  2406. atomic_set(&p->b_count, p->b_size);
  2407. p = p->b_reqnext;
  2408. }
  2409. }
  2410. pc->bh = bh;
  2411. pc->b_data = bh->b_data;
  2412. pc->b_count = atomic_read(&bh->b_count);
  2413. pc->buffer = NULL;
  2414. if (!tape->onstream) {
  2415. pc->request_transfer = pc->buffer_size = length * tape->tape_block_size;
  2416. if (pc->request_transfer == tape->stage_size)
  2417. set_bit (PC_DMA_RECOMMENDED, &pc->flags);
  2418. } else  {
  2419. if (length) {
  2420. pc->request_transfer = pc->buffer_size = 32768 + 512;
  2421. set_bit (PC_DMA_RECOMMENDED, &pc->flags);
  2422. } else
  2423. pc->request_transfer = 0;
  2424. }
  2425. }
  2426. /*
  2427.  * idetape_do_request is our request handling function.
  2428.  */
  2429. static ide_startstop_t idetape_do_request (ide_drive_t *drive, struct request *rq, unsigned long block)
  2430. {
  2431. idetape_tape_t *tape = drive->driver_data;
  2432. idetape_pc_t *pc;
  2433. struct request *postponed_rq = tape->postponed_rq;
  2434. idetape_status_reg_t status;
  2435. #if IDETAPE_DEBUG_LOG
  2436. if (tape->debug_level >= 5)
  2437. printk (KERN_INFO "ide-tape: rq_status: %d, rq_dev: %u, cmd: %d, errors: %dn",rq->rq_status,(unsigned int) rq->rq_dev,rq->cmd,rq->errors);
  2438. if (tape->debug_level >= 2)
  2439. printk (KERN_INFO "ide-tape: sector: %ld, nr_sectors: %ld, current_nr_sectors: %ldn",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
  2440. #endif /* IDETAPE_DEBUG_LOG */
  2441. if (!IDETAPE_RQ_CMD (rq->cmd)) {
  2442. /*
  2443.  * We do not support buffer cache originated requests.
  2444.  */
  2445. printk (KERN_NOTICE "ide-tape: %s: Unsupported command in request queue (%d)n", drive->name, rq->cmd);
  2446. ide_end_request (0, HWGROUP (drive)); /* Let the common code handle it */
  2447. return ide_stopped;
  2448. }
  2449. /*
  2450.  * Retry a failed packet command
  2451.  */
  2452. if (tape->failed_pc != NULL && tape->pc->c[0] == IDETAPE_REQUEST_SENSE_CMD) {
  2453. return idetape_issue_packet_command (drive, tape->failed_pc);
  2454. }
  2455. #if IDETAPE_DEBUG_BUGS
  2456. if (postponed_rq != NULL)
  2457. if (rq != postponed_rq) {
  2458. printk (KERN_ERR "ide-tape: ide-tape.c bug - Two DSC requests were queuedn");
  2459. idetape_end_request (0, HWGROUP (drive));
  2460. return ide_stopped;
  2461. }
  2462. #endif /* IDETAPE_DEBUG_BUGS */
  2463. tape->postponed_rq = NULL;
  2464. /*
  2465.  * If the tape is still busy, postpone our request and service
  2466.  * the other device meanwhile.
  2467.  */
  2468. status.all = GET_STAT();
  2469. /*
  2470.  * The OnStream tape drive doesn't support DSC. Assume
  2471.  * that DSC is always set.
  2472.  */
  2473. if (tape->onstream)
  2474. status.b.dsc = 1;
  2475. if (!drive->dsc_overlap && rq->cmd != IDETAPE_PC_RQ2)
  2476. set_bit (IDETAPE_IGNORE_DSC, &tape->flags);
  2477. /*
  2478.  * For the OnStream tape, check the current status of the tape
  2479.  * internal buffer using data gathered from the buffer fill
  2480.  * mode page, and postpone our request, effectively "disconnecting"
  2481.  * from the IDE bus, in case the buffer is full (writing) or
  2482.  * empty (reading), and there is a danger that our request will
  2483.  * hold the IDE bus during actual media access.
  2484.  */
  2485. if (tape->tape_still_time > 100 && tape->tape_still_time < 200)
  2486. tape->measure_insert_time = 1;
  2487. if (tape->req_buffer_fill && (rq->cmd == IDETAPE_WRITE_RQ || rq->cmd == IDETAPE_READ_RQ)) {
  2488. tape->req_buffer_fill = 0;
  2489. tape->writes_since_buffer_fill = 0;
  2490. tape->reads_since_buffer_fill = 0;
  2491. tape->last_buffer_fill = jiffies;
  2492. idetape_queue_onstream_buffer_fill(drive);
  2493. if (time_after(jiffies, tape->insert_time))
  2494. tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
  2495. return ide_stopped;
  2496. }
  2497. if (time_after(jiffies, tape->insert_time))
  2498. tape->insert_speed = tape->insert_size / 1024 * HZ / (jiffies - tape->insert_time);
  2499. calculate_speeds(drive);
  2500. if (tape->onstream && tape->max_frames &&
  2501.     ((rq->cmd == IDETAPE_WRITE_RQ &&
  2502.               ( tape->cur_frames == tape->max_frames ||
  2503.                 ( tape->speed_control && tape->cur_frames > 5 &&
  2504.                        (tape->insert_speed > tape->max_insert_speed ||
  2505.                         (0 /* tape->cur_frames > 30 && tape->tape_still_time > 200 */) ) ) ) ) ||
  2506.      (rq->cmd == IDETAPE_READ_RQ &&
  2507.       ( tape->cur_frames == 0 ||
  2508. ( tape->speed_control && (tape->cur_frames < tape->max_frames - 5) &&
  2509. tape->insert_speed > tape->max_insert_speed ) ) && rq->nr_sectors) ) ) {
  2510. #if IDETAPE_DEBUG_LOG
  2511. if (tape->debug_level >= 4)
  2512. printk(KERN_INFO "ide-tape: postponing request, cmd %d, cur %d, max %dn",
  2513. rq->cmd, tape->cur_frames, tape->max_frames);
  2514. #endif
  2515. if (tape->postpone_cnt++ < 500) {
  2516. status.b.dsc = 0;
  2517. tape->req_buffer_fill = 1;
  2518. }
  2519. #if ONSTREAM_DEBUG
  2520. else if (tape->debug_level >= 4) 
  2521. printk(KERN_INFO "ide-tape: %s: postpone_cnt %dn", tape->name, tape->postpone_cnt);
  2522. #endif
  2523. }
  2524. if (!test_and_clear_bit (IDETAPE_IGNORE_DSC, &tape->flags) && !status.b.dsc) {
  2525. if (postponed_rq == NULL) {
  2526. tape->dsc_polling_start = jiffies;
  2527. tape->dsc_polling_frequency = tape->best_dsc_rw_frequency;
  2528. tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
  2529. } else if ((signed long) (jiffies - tape->dsc_timeout) > 0) {
  2530. printk (KERN_ERR "ide-tape: %s: DSC timeoutn", tape->name);
  2531. if (rq->cmd == IDETAPE_PC_RQ2) {
  2532. idetape_media_access_finished (drive);
  2533. return ide_stopped;
  2534. } else {
  2535. return ide_do_reset (drive);
  2536. }
  2537. } else if (jiffies - tape->dsc_polling_start > IDETAPE_DSC_MA_THRESHOLD)
  2538. tape->dsc_polling_frequency = IDETAPE_DSC_MA_SLOW;
  2539. idetape_postpone_request (drive);
  2540. return ide_stopped;
  2541. }
  2542. switch (rq->cmd) {
  2543. case IDETAPE_READ_RQ:
  2544. tape->buffer_head++;
  2545. #if USE_IOTRACE
  2546. IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
  2547. #endif
  2548. tape->postpone_cnt = 0;
  2549. tape->reads_since_buffer_fill++;
  2550. if (tape->onstream) {
  2551. if (tape->cur_frames - tape->reads_since_buffer_fill <= 0)
  2552. tape->req_buffer_fill = 1;
  2553. if (time_after(jiffies, tape->last_buffer_fill + 5 * HZ / 100))
  2554. tape->req_buffer_fill = 1;
  2555. }
  2556. pc = idetape_next_pc_storage (drive);
  2557. idetape_create_read_cmd (tape, pc, rq->current_nr_sectors, rq->bh);
  2558. break;
  2559. case IDETAPE_WRITE_RQ:
  2560. tape->buffer_head++;
  2561. #if USE_IOTRACE
  2562. IO_trace(IO_IDETAPE_FIFO, tape->pipeline_head, tape->buffer_head, tape->tape_head, tape->minor);
  2563. #endif
  2564. tape->postpone_cnt = 0;
  2565. tape->writes_since_buffer_fill++;
  2566. if (tape->onstream) {
  2567. if (tape->cur_frames + tape->writes_since_buffer_fill >= tape->max_frames)
  2568. tape->req_buffer_fill = 1;
  2569. if (time_after(jiffies, tape->last_buffer_fill + 5 * HZ / 100))
  2570. tape->req_buffer_fill = 1;
  2571. calculate_speeds(drive);
  2572. }
  2573. pc = idetape_next_pc_storage (drive);
  2574. idetape_create_write_cmd (tape, pc, rq->current_nr_sectors, rq->bh);
  2575. break;
  2576. case IDETAPE_READ_BUFFER_RQ:
  2577. tape->postpone_cnt = 0;
  2578. pc = idetape_next_pc_storage (drive);
  2579. idetape_create_read_buffer_cmd (tape, pc, rq->current_nr_sectors, rq->bh);
  2580. break;
  2581. case IDETAPE_ABORTED_WRITE_RQ:
  2582. rq->cmd = IDETAPE_WRITE_RQ;
  2583. idetape_end_request (IDETAPE_ERROR_EOD, HWGROUP(drive));
  2584. return ide_stopped;
  2585. case IDETAPE_ABORTED_READ_RQ:
  2586. #if IDETAPE_DEBUG_LOG
  2587. if (tape->debug_level >= 2)
  2588. printk(KERN_INFO "ide-tape: %s: detected aborted read rqn", tape->name);
  2589. #endif
  2590. rq->cmd = IDETAPE_READ_RQ;
  2591. idetape_end_request (IDETAPE_ERROR_EOD, HWGROUP(drive));
  2592. return ide_stopped;
  2593. case IDETAPE_PC_RQ1:
  2594. pc = (idetape_pc_t *) rq->buffer;
  2595. rq->cmd = IDETAPE_PC_RQ2;
  2596. break;
  2597. case IDETAPE_PC_RQ2:
  2598. idetape_media_access_finished (drive);
  2599. return ide_stopped;
  2600. default:
  2601. printk (KERN_ERR "ide-tape: bug in IDETAPE_RQ_CMD macron");
  2602. idetape_end_request (0, HWGROUP (drive));
  2603. return ide_stopped;
  2604. }
  2605. return idetape_issue_packet_command (drive, pc);
  2606. }
  2607. /*
  2608.  * Pipeline related functions
  2609.  */
  2610. static inline int idetape_pipeline_active (idetape_tape_t *tape)
  2611. {
  2612. int rc1, rc2;
  2613. rc1 = test_bit(IDETAPE_PIPELINE_ACTIVE, &tape->flags);
  2614. rc2 = (tape->active_data_request != NULL);
  2615. return rc1;
  2616. }
  2617. /*
  2618.  * idetape_kmalloc_stage uses __get_free_page to allocate a pipeline
  2619.  * stage, along with all the necessary small buffers which together make
  2620.  * a buffer of size tape->stage_size (or a bit more). We attempt to
  2621.  * combine sequential pages as much as possible.
  2622.  *
  2623.  * Returns a pointer to the new allocated stage, or NULL if we
  2624.  * can't (or don't want to) allocate a stage.
  2625.  *
  2626.  * Pipeline stages are optional and are used to increase performance.
  2627.  * If we can't allocate them, we'll manage without them.
  2628.  */
  2629. static idetape_stage_t *__idetape_kmalloc_stage (idetape_tape_t *tape, int full, int clear)
  2630. {
  2631. idetape_stage_t *stage;
  2632. struct buffer_head *prev_bh, *bh;
  2633. int pages = tape->pages_per_stage;
  2634. char *b_data;
  2635. if ((stage = (idetape_stage_t *) kmalloc (sizeof (idetape_stage_t),GFP_KERNEL)) == NULL)
  2636. return NULL;
  2637. stage->next = NULL;
  2638. bh = stage->bh = (struct buffer_head *) kmalloc (sizeof (struct buffer_head), GFP_KERNEL);
  2639. if (bh == NULL)
  2640. goto abort;
  2641. bh->b_reqnext = NULL;
  2642. if ((bh->b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
  2643. goto abort;
  2644. if (clear)
  2645. memset(bh->b_data, 0, PAGE_SIZE);
  2646. bh->b_size = PAGE_SIZE;
  2647. atomic_set(&bh->b_count, full ? bh->b_size : 0);
  2648. set_bit (BH_Lock, &bh->b_state);
  2649. while (--pages) {
  2650. if ((b_data = (char *) __get_free_page (GFP_KERNEL)) == NULL)
  2651. goto abort;
  2652. if (clear)
  2653. memset(b_data, 0, PAGE_SIZE);
  2654. if (bh->b_data == b_data + PAGE_SIZE) {
  2655. bh->b_size += PAGE_SIZE;
  2656. bh->b_data -= PAGE_SIZE;
  2657. if (full)
  2658. atomic_add(PAGE_SIZE, &bh->b_count);
  2659. continue;
  2660. }
  2661. if (b_data == bh->b_data + bh->b_size) {
  2662. bh->b_size += PAGE_SIZE;
  2663. if (full)
  2664. atomic_add(PAGE_SIZE, &bh->b_count);
  2665. continue;
  2666. }
  2667. prev_bh = bh;
  2668. if ((bh = (struct buffer_head *) kmalloc (sizeof (struct buffer_head), GFP_KERNEL)) == NULL) {
  2669. free_page ((unsigned long) b_data);
  2670. goto abort;
  2671. }
  2672. bh->b_reqnext = NULL;
  2673. bh->b_data = b_data;
  2674. bh->b_size = PAGE_SIZE;
  2675. atomic_set(&bh->b_count, full ? bh->b_size : 0);
  2676. set_bit (BH_Lock, &bh->b_state);
  2677. prev_bh->b_reqnext = bh;
  2678. }
  2679. bh->b_size -= tape->excess_bh_size;
  2680. if (full)
  2681. atomic_sub(tape->excess_bh_size, &bh->b_count);
  2682. if (tape->onstream)
  2683. stage->aux = (os_aux_t *) (bh->b_data + bh->b_size - OS_AUX_SIZE);
  2684. return stage;
  2685. abort:
  2686. __idetape_kfree_stage (stage);
  2687. return NULL;
  2688. }
  2689. static idetape_stage_t *idetape_kmalloc_stage (idetape_tape_t *tape)
  2690. {
  2691. idetape_stage_t *cache_stage = tape->cache_stage;
  2692. #if IDETAPE_DEBUG_LOG
  2693. if (tape->debug_level >= 4)
  2694. printk (KERN_INFO "ide-tape: Reached idetape_kmalloc_stagen");
  2695. #endif /* IDETAPE_DEBUG_LOG */
  2696. if (tape->nr_stages >= tape->max_stages)
  2697. return NULL;
  2698. if (cache_stage != NULL) {
  2699. tape->cache_stage = NULL;
  2700. return cache_stage;
  2701. }
  2702. return __idetape_kmalloc_stage (tape, 0, 0);
  2703. }
  2704. static void idetape_copy_stage_from_user (idetape_tape_t *tape, idetape_stage_t *stage, const char *buf, int n)
  2705. {
  2706. struct buffer_head *bh = tape->bh;
  2707. int count;
  2708. while (n) {
  2709. #if IDETAPE_DEBUG_BUGS
  2710. if (bh == NULL) {
  2711. printk (KERN_ERR "ide-tape: bh == NULL in idetape_copy_stage_from_usern");
  2712. return;
  2713. }
  2714. #endif /* IDETAPE_DEBUG_BUGS */
  2715. count = IDE_MIN (bh->b_size - atomic_read(&bh->b_count), n);
  2716. copy_from_user (bh->b_data + atomic_read(&bh->b_count), buf, count);
  2717. n -= count;
  2718. atomic_add(count, &bh->b_count);
  2719. buf += count;
  2720. if (atomic_read(&bh->b_count) == bh->b_size) {
  2721. bh = bh->b_reqnext;
  2722. if (bh)
  2723. atomic_set(&bh->b_count, 0);
  2724. }
  2725. }
  2726. tape->bh = bh;
  2727. }
  2728. static void idetape_copy_stage_to_user (idetape_tape_t *tape, char *buf, idetape_stage_t *stage, int n)
  2729. {
  2730. struct buffer_head *bh = tape->bh;
  2731. int count;
  2732. while (n) {
  2733. #if IDETAPE_DEBUG_BUGS
  2734. if (bh == NULL) {
  2735. printk (KERN_ERR "ide-tape: bh == NULL in idetape_copy_stage_to_usern");
  2736. return;
  2737. }
  2738. #endif /* IDETAPE_DEBUG_BUGS */
  2739. count = IDE_MIN (tape->b_count, n);
  2740. copy_to_user (buf, tape->b_data, count);
  2741. n -= count;
  2742. tape->b_data += count;
  2743. tape->b_count -= count;
  2744. buf += count;
  2745. if (!tape->b_count) {
  2746. tape->bh = bh = bh->b_reqnext;
  2747. if (bh) {
  2748. tape->b_data = bh->b_data;
  2749. tape->b_count = atomic_read(&bh->b_count);
  2750. }
  2751. }
  2752. }
  2753. }
  2754. static void idetape_init_merge_stage (idetape_tape_t *tape)
  2755. {
  2756. struct buffer_head *bh = tape->merge_stage->bh;
  2757. tape->bh = bh;
  2758. if (tape->chrdev_direction == idetape_direction_write)
  2759. atomic_set(&bh->b_count, 0);
  2760. else {
  2761. tape->b_data = bh->b_data;
  2762. tape->b_count = atomic_read(&bh->b_count);
  2763. }
  2764. }
  2765. static void idetape_switch_buffers (idetape_tape_t *tape, idetape_stage_t *stage)
  2766. {
  2767. struct buffer_head *tmp;
  2768. os_aux_t *tmp_aux;
  2769. tmp = stage->bh; tmp_aux = stage->aux;
  2770. stage->bh = tape->merge_stage->bh; stage->aux = tape->merge_stage->aux;
  2771. tape->merge_stage->bh = tmp; tape->merge_stage->aux = tmp_aux;
  2772. idetape_init_merge_stage (tape);
  2773. }
  2774. /*
  2775.  * idetape_add_stage_tail adds a new stage at the end of the pipeline.
  2776.  */
  2777. static void idetape_add_stage_tail (ide_drive_t *drive,idetape_stage_t *stage)
  2778. {
  2779. idetape_tape_t *tape = drive->driver_data;
  2780. unsigned long flags;
  2781. #if IDETAPE_DEBUG_LOG
  2782. if (tape->debug_level >= 4)
  2783. printk (KERN_INFO "ide-tape: Reached idetape_add_stage_tailn");
  2784. #endif /* IDETAPE_DEBUG_LOG */
  2785. spin_lock_irqsave(&tape->spinlock, flags);
  2786. stage->next=NULL;
  2787. if (tape->last_stage != NULL)
  2788. tape->last_stage->next=stage;
  2789. else
  2790. tape->first_stage = tape->next_stage=stage;
  2791. tape->last_stage = stage;
  2792. if (tape->next_stage == NULL)
  2793. tape->next_stage = tape->last_stage;
  2794. tape->nr_stages++;
  2795. tape->nr_pending_stages++;
  2796. spin_unlock_irqrestore(&tape->spinlock, flags);
  2797. }
  2798. /*
  2799.  * Initialize the OnStream AUX
  2800.  */
  2801. static void idetape_init_stage (ide_drive_t *drive, idetape_stage_t *stage, int frame_type, int logical_blk_num)
  2802. {
  2803. idetape_tape_t *tape = drive->driver_data;
  2804. os_aux_t *aux = stage->aux;
  2805. os_partition_t *par = &aux->partition;
  2806. os_dat_t *dat = &aux->dat;
  2807. if (!tape->onstream || tape->raw)
  2808. return;
  2809. memset(aux, 0, sizeof(*aux));
  2810. aux->format_id = htonl(0);
  2811. memcpy(aux->application_sig, "LIN3", 4);
  2812. aux->hdwr = htonl(0);
  2813. aux->frame_type = frame_type;
  2814. if (frame_type == OS_FRAME_TYPE_HEADER) {
  2815. aux->update_frame_cntr = htonl(tape->update_frame_cntr);
  2816. par->partition_num = OS_CONFIG_PARTITION;
  2817. par->par_desc_ver = OS_PARTITION_VERSION;
  2818. par->wrt_pass_cntr = htons(0xffff);
  2819. par->first_frame_addr = htonl(0);
  2820. par->last_frame_addr = htonl(0xbb7); /* 2999 */
  2821. aux->frame_seq_num = htonl(0);
  2822. aux->logical_blk_num_high = htonl(0);
  2823. aux->logical_blk_num = htonl(0);
  2824. aux->next_mark_addr = htonl(tape->first_mark_addr);
  2825. } else {
  2826. aux->update_frame_cntr = htonl(0);
  2827. par->partition_num = OS_DATA_PARTITION;
  2828. par->par_desc_ver = OS_PARTITION_VERSION;
  2829. par->wrt_pass_cntr = htons(tape->wrt_pass_cntr);
  2830. par->first_frame_addr = htonl(OS_DATA_STARTFRAME1);
  2831. par->last_frame_addr = htonl(tape->capacity);
  2832. aux->frame_seq_num = htonl(logical_blk_num);
  2833. aux->logical_blk_num_high = htonl(0);
  2834. aux->logical_blk_num = htonl(logical_blk_num);
  2835. dat->dat_sz = 8;
  2836. dat->reserved1 = 0;
  2837. dat->entry_cnt = 1;
  2838. dat->reserved3 = 0;
  2839. if (frame_type == OS_FRAME_TYPE_DATA)
  2840. dat->dat_list[0].blk_sz = htonl(32 * 1024);
  2841. else
  2842. dat->dat_list[0].blk_sz = 0;
  2843. dat->dat_list[0].blk_cnt = htons(1);
  2844. if (frame_type == OS_FRAME_TYPE_MARKER)
  2845. dat->dat_list[0].flags = OS_DAT_FLAGS_MARK;
  2846. else
  2847. dat->dat_list[0].flags = OS_DAT_FLAGS_DATA;
  2848. dat->dat_list[0].reserved = 0;
  2849. aux->filemark_cnt = ntohl(tape->filemark_cnt); /* shouldn't this be htonl ?? */
  2850. aux->phys_fm = ntohl(0xffffffff); /* shouldn't this be htonl ?? */
  2851. aux->last_mark_addr = ntohl(tape->last_mark_addr); /* shouldn't this be htonl ?? */
  2852. }
  2853. /*
  2854.  * idetape_wait_for_request installs a completion in a pending request
  2855.  * and sleeps until it is serviced.
  2856.  *
  2857.  * The caller should ensure that the request will not be serviced
  2858.  * before we install the completion (usually by disabling interrupts).
  2859.  */
  2860. static void idetape_wait_for_request (ide_drive_t *drive, struct request *rq)
  2861. {
  2862. DECLARE_COMPLETION(wait);
  2863. idetape_tape_t *tape = drive->driver_data;
  2864. #if IDETAPE_DEBUG_BUGS
  2865. if (rq == NULL || !IDETAPE_RQ_CMD (rq->cmd)) {
  2866. printk (KERN_ERR "ide-tape: bug: Trying to sleep on non-valid requestn");
  2867. return;
  2868. }
  2869. #endif /* IDETAPE_DEBUG_BUGS */
  2870. rq->waiting = &wait;
  2871. tape->waiting = &wait;
  2872. spin_unlock(&tape->spinlock);
  2873. wait_for_completion(&wait);
  2874. rq->waiting = NULL;
  2875. tape->waiting = NULL;
  2876. spin_lock_irq(&tape->spinlock);
  2877. }
  2878. static ide_startstop_t idetape_read_position_callback (ide_drive_t *drive)
  2879. {
  2880. idetape_tape_t *tape = drive->driver_data;
  2881. idetape_read_position_result_t *result;
  2882. #if IDETAPE_DEBUG_LOG
  2883. if (tape->debug_level >= 4)
  2884. printk (KERN_INFO "ide-tape: Reached idetape_read_position_callbackn");
  2885. #endif /* IDETAPE_DEBUG_LOG */
  2886. if (!tape->pc->error) {
  2887. result = (idetape_read_position_result_t *) tape->pc->buffer;
  2888. #if IDETAPE_DEBUG_LOG
  2889. if (tape->debug_level >= 2)
  2890. printk (KERN_INFO "ide-tape: BOP - %sn",result->bop ? "Yes":"No");
  2891. if (tape->debug_level >= 2)
  2892. printk (KERN_INFO "ide-tape: EOP - %sn",result->eop ? "Yes":"No");
  2893. #endif /* IDETAPE_DEBUG_LOG */
  2894. if (result->bpu) {
  2895. printk (KERN_INFO "ide-tape: Block location is unknown to the tapen");
  2896. clear_bit (IDETAPE_ADDRESS_VALID, &tape->flags);
  2897. idetape_end_request (0, HWGROUP (drive));
  2898. } else {
  2899. #if IDETAPE_DEBUG_LOG
  2900. if (tape->debug_level >= 2)
  2901. printk (KERN_INFO "ide-tape: Block Location - %un", ntohl (result->first_block));
  2902. #endif /* IDETAPE_DEBUG_LOG */
  2903. tape->partition = result->partition;
  2904. tape->first_frame_position = ntohl (result->first_block);
  2905. tape->last_frame_position = ntohl (result->last_block);
  2906. tape->blocks_in_buffer = result->blocks_in_buffer[2];
  2907. set_bit (IDETAPE_ADDRESS_VALID, &tape->flags);
  2908. idetape_end_request (1, HWGROUP (drive));
  2909. }
  2910. } else {
  2911. idetape_end_request (0, HWGROUP (drive));
  2912. }
  2913. return ide_stopped;
  2914. }
  2915. /*
  2916.  * idetape_create_write_filemark_cmd will:
  2917.  *
  2918.  * 1. Write a filemark if write_filemark=1.
  2919.  * 2. Flush the device buffers without writing a filemark
  2920.  * if write_filemark=0.
  2921.  *
  2922.  */
  2923. static void idetape_create_write_filemark_cmd (ide_drive_t *drive, idetape_pc_t *pc,int write_filemark)
  2924. {
  2925. idetape_tape_t *tape = drive->driver_data;
  2926. idetape_init_pc (pc);
  2927. pc->c[0] = IDETAPE_WRITE_FILEMARK_CMD;
  2928. if (tape->onstream)
  2929. pc->c[1] = 1; /* Immed bit */
  2930. pc->c[4] = write_filemark;  /* not used for OnStream ?? */
  2931. set_bit (PC_WAIT_FOR_DSC, &pc->flags);
  2932. pc->callback = &idetape_pc_callback;
  2933. }
  2934. static void idetape_create_test_unit_ready_cmd(idetape_pc_t *pc)
  2935. {
  2936. idetape_init_pc(pc);
  2937. pc->c[0] = IDETAPE_TEST_UNIT_READY_CMD;
  2938. pc->callback = &idetape_pc_callback;
  2939. }
  2940. /*
  2941.  * idetape_queue_pc_tail is based on the following functions:
  2942.  *
  2943.  * ide_do_drive_cmd from ide.c
  2944.  * cdrom_queue_request and cdrom_queue_packet_command from ide-cd.c
  2945.  *
  2946.  * We add a special packet command request to the tail of the request queue,
  2947.  * and wait for it to be serviced.
  2948.  *
  2949.  * This is not to be called from within the request handling part
  2950.  * of the driver ! We allocate here data in the stack, and it is valid
  2951.  * until the request is finished. This is not the case for the bottom
  2952.  * part of the driver, where we are always leaving the functions to wait
  2953.  * for an interrupt or a timer event.
  2954.  *
  2955.  * From the bottom part of the driver, we should allocate safe memory
  2956.  * using idetape_next_pc_storage and idetape_next_rq_storage, and add
  2957.  * the request to the request list without waiting for it to be serviced !
  2958.  * In that case, we usually use idetape_queue_pc_head.
  2959.  */
  2960. static int __idetape_queue_pc_tail (ide_drive_t *drive, idetape_pc_t *pc)
  2961. {
  2962. struct request rq;
  2963. ide_init_drive_cmd (&rq);
  2964. rq.buffer = (char *) pc;