Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2)  * IDE ATAPI streaming tape driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * Copyright (C) 1995-1999  Gadi Oxman <gadio@netvision.net.il>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (C) 2003-2005  Bartlomiej Zolnierkiewicz
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * This driver was constructed as a student project in the software laboratory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * of the faculty of electrical engineering in the Technion - Israel's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  * Institute Of Technology, with the guide of Avner Lottem and Dr. Ilana David.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  * It is hereby placed under the terms of the GNU general public license.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  * (See linux/COPYING).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  * For a historical changelog see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  * Documentation/ide/ChangeLog.ide-tape.1995-2002
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #define DRV_NAME "ide-tape"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #define IDETAPE_VERSION "1.20"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/major.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <linux/genhd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include <linux/pci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include <linux/ide.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #include <linux/completion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #include <scsi/scsi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #include <asm/byteorder.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) #include <linux/mtio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) /* define to see debug info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) #undef IDETAPE_DEBUG_LOG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) #ifdef IDETAPE_DEBUG_LOG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) #define ide_debug_log(lvl, fmt, args...) __ide_debug_log(lvl, fmt, ## args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) #define ide_debug_log(lvl, fmt, args...) do {} while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) /**************************** Tunable parameters *****************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61)  * After each failed packet command we issue a request sense command and retry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62)  * the packet command IDETAPE_MAX_PC_RETRIES times.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64)  * Setting IDETAPE_MAX_PC_RETRIES to 0 will disable retries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) #define IDETAPE_MAX_PC_RETRIES		3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69)  * The following parameter is used to select the point in the internal tape fifo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70)  * in which we will start to refill the buffer. Decreasing the following
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71)  * parameter will improve the system's latency and interactive response, while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72)  * using a high value might improve system throughput.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) #define IDETAPE_FIFO_THRESHOLD		2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77)  * DSC polling parameters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79)  * Polling for DSC (a single bit in the status register) is a very important
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80)  * function in ide-tape. There are two cases in which we poll for DSC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82)  * 1. Before a read/write packet command, to ensure that we can transfer data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83)  * from/to the tape's data buffers, without causing an actual media access.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84)  * In case the tape is not ready yet, we take out our request from the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85)  * request queue, so that ide.c could service requests from the other device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86)  * on the same interface in the meantime.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88)  * 2. After the successful initialization of a "media access packet command",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89)  * which is a command that can take a long time to complete (the interval can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90)  * range from several seconds to even an hour). Again, we postpone our request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91)  * in the middle to free the bus for the other device. The polling frequency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92)  * here should be lower than the read/write frequency since those media access
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93)  * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94)  * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95)  * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97)  * We also set a timeout for the timer, in case something goes wrong. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98)  * timeout should be longer then the maximum execution time of a tape operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) /* DSC timings. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) #define IDETAPE_DSC_RW_MIN		5*HZ/100	/* 50 msec */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) #define IDETAPE_DSC_RW_MAX		40*HZ/100	/* 400 msec */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) #define IDETAPE_DSC_RW_TIMEOUT		2*60*HZ		/* 2 minutes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) #define IDETAPE_DSC_MA_FAST		2*HZ		/* 2 seconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) #define IDETAPE_DSC_MA_THRESHOLD	5*60*HZ		/* 5 minutes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) #define IDETAPE_DSC_MA_SLOW		30*HZ		/* 30 seconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) #define IDETAPE_DSC_MA_TIMEOUT		2*60*60*HZ	/* 2 hours */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) /*************************** End of tunable parameters ***********************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) /* tape directions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	IDETAPE_DIR_NONE  = (1 << 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	IDETAPE_DIR_READ  = (1 << 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	IDETAPE_DIR_WRITE = (1 << 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) /* Tape door status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) #define DOOR_UNLOCKED			0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) #define DOOR_LOCKED			1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) #define DOOR_EXPLICITLY_LOCKED		2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) /* Some defines for the SPACE command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) #define IDETAPE_SPACE_OVER_FILEMARK	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) #define IDETAPE_SPACE_TO_EOD		3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) /* Some defines for the LOAD UNLOAD command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) #define IDETAPE_LU_LOAD_MASK		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) #define IDETAPE_LU_RETENSION_MASK	2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) #define IDETAPE_LU_EOT_MASK		4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) /* Structures related to the SELECT SENSE / MODE SENSE packet commands. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) #define IDETAPE_BLOCK_DESCRIPTOR	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) #define IDETAPE_CAPABILITIES_PAGE	0x2a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138)  * Most of our global data which we need to save even as we leave the driver due
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139)  * to an interrupt or a timer event is stored in the struct defined below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) typedef struct ide_tape_obj {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	ide_drive_t		*drive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	struct ide_driver	*driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	struct gendisk		*disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	struct device		dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	/* used by REQ_IDETAPE_{READ,WRITE} requests */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	struct ide_atapi_pc queued_pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	 * DSC polling variables.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	 * While polling for DSC we use postponed_rq to postpone the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	 * request so that ide.c will be able to service pending requests on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	 * other device. Note that at most we will have only one DSC (usually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	 * data transfer) request in the device request queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	bool postponed_rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	/* The time in which we started polling for DSC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	unsigned long dsc_polling_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	/* Timer used to poll for dsc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	struct timer_list dsc_timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	/* Read/Write dsc polling frequency */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	unsigned long best_dsc_rw_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	unsigned long dsc_poll_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	unsigned long dsc_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	/* Read position information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	u8 partition;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	/* Current block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	unsigned int first_frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	/* Last error information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	u8 sense_key, asc, ascq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	/* Character device operation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	unsigned int minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	/* device name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	char name[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	/* Current character device data transfer direction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	u8 chrdev_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	/* tape block size, usually 512 or 1024 bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	unsigned short blk_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	int user_bs_factor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	/* Copy of the tape's Capabilities and Mechanical Page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	u8 caps[20];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	 * Active data transfer request parameters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	 * At most, there is only one ide-tape originated data transfer request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	 * in the device request queue. This allows ide.c to easily service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	 * requests from the other device when we postpone our active request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	/* Data buffer size chosen based on the tape's recommendation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	int buffer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	/* Staging buffer of buffer_size bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	/* The read/write cursor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	void *cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	/* The number of valid bytes in buf */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	size_t valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	/* Measures average tape speed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	unsigned long avg_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	int avg_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	int avg_speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	/* the door is currently locked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	int door_locked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	/* the tape hardware is write protected */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	char drv_write_prot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	/* the tape is write protected (hardware or opened as read-only) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	char write_prot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) } idetape_tape_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) static DEFINE_MUTEX(ide_tape_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) static DEFINE_MUTEX(idetape_ref_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) static DEFINE_MUTEX(idetape_chrdev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) static struct class *idetape_sysfs_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) static void ide_tape_release(struct device *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) static struct ide_tape_obj *ide_tape_get(struct gendisk *disk, bool cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 					 unsigned int i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	struct ide_tape_obj *tape = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	mutex_lock(&idetape_ref_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	if (cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 		tape = idetape_devs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		tape = ide_drv_g(disk, ide_tape_obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	if (tape) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 		if (ide_device_get(tape->drive))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 			tape = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 			get_device(&tape->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	mutex_unlock(&idetape_ref_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	return tape;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) static void ide_tape_put(struct ide_tape_obj *tape)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	ide_drive_t *drive = tape->drive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	mutex_lock(&idetape_ref_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	put_device(&tape->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	ide_device_put(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	mutex_unlock(&idetape_ref_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266)  * called on each failed packet command retry to analyze the request sense. We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267)  * currently do not utilize this information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) static void idetape_analyze_error(ide_drive_t *drive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	idetape_tape_t *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	struct ide_atapi_pc *pc = drive->failed_pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	struct request *rq = drive->hwif->rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	u8 *sense = bio_data(rq->bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	tape->sense_key = sense[2] & 0xF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	tape->asc       = sense[12];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	tape->ascq      = sense[13];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	ide_debug_log(IDE_DBG_FUNC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		      "cmd: 0x%x, sense key = %x, asc = %x, ascq = %x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 		      rq->cmd[0], tape->sense_key, tape->asc, tape->ascq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	/* correct remaining bytes to transfer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	if (pc->flags & PC_FLAG_DMA_ERROR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		scsi_req(rq)->resid_len = tape->blk_size * get_unaligned_be32(&sense[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	 * If error was the result of a zero-length read or write command,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	 * with sense key=5, asc=0x22, ascq=0, let it slide.  Some drives
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	    /* length == 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	    && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 		if (tape->sense_key == 5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 			/* don't report an error, everything's ok */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 			pc->error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 			/* don't retry read/write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 			pc->flags |= PC_FLAG_ABORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	if (pc->c[0] == READ_6 && (sense[2] & 0x80)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 		pc->error = IDE_DRV_ERROR_FILEMARK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 		pc->flags |= PC_FLAG_ABORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	if (pc->c[0] == WRITE_6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		if ((sense[2] & 0x40) || (tape->sense_key == 0xd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 		     && tape->asc == 0x0 && tape->ascq == 0x2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 			pc->error = IDE_DRV_ERROR_EOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 			pc->flags |= PC_FLAG_ABORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 		if (tape->sense_key == 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 			pc->error = IDE_DRV_ERROR_EOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 			pc->flags |= PC_FLAG_ABORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 		if (!(pc->flags & PC_FLAG_ABORT) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		    (blk_rq_bytes(rq) - scsi_req(rq)->resid_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 			pc->retries = IDETAPE_MAX_PC_RETRIES + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) static void ide_tape_handle_dsc(ide_drive_t *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) static int ide_tape_callback(ide_drive_t *drive, int dsc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	idetape_tape_t *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	struct ide_atapi_pc *pc = drive->pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	struct request *rq = drive->hwif->rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	int uptodate = pc->error ? 0 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	int err = uptodate ? 0 : IDE_DRV_ERROR_GENERAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc: %d, err: %d", rq->cmd[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 		      dsc, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	if (dsc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 		ide_tape_handle_dsc(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	if (drive->failed_pc == pc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 		drive->failed_pc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	if (pc->c[0] == REQUEST_SENSE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 		if (uptodate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 			idetape_analyze_error(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 			printk(KERN_ERR "ide-tape: Error in REQUEST SENSE "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 					"itself - Aborting request!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	} else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 		unsigned int blocks =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 			(blk_rq_bytes(rq) - scsi_req(rq)->resid_len) / tape->blk_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 		tape->avg_size += blocks * tape->blk_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 		if (time_after_eq(jiffies, tape->avg_time + HZ)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 			tape->avg_speed = tape->avg_size * HZ /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 				(jiffies - tape->avg_time) / 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 			tape->avg_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 			tape->avg_time = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 		tape->first_frame += blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 		if (pc->error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 			uptodate = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 			err = pc->error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	scsi_req(rq)->result = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	return uptodate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376)  * Postpone the current request so that ide.c will be able to service requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377)  * from another device on the same port while we are polling for DSC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) static void ide_tape_stall_queue(ide_drive_t *drive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	idetape_tape_t *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, dsc_poll_freq: %lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		      drive->hwif->rq->cmd[0], tape->dsc_poll_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	tape->postponed_rq = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	ide_stall_queue(drive, tape->dsc_poll_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) static void ide_tape_handle_dsc(ide_drive_t *drive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	idetape_tape_t *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	/* Media access command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	tape->dsc_polling_start = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	/* Allow ide.c to handle other requests */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	ide_tape_stall_queue(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404)  * Packet Command Interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406)  * The current Packet Command is available in drive->pc, and will not change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407)  * until we finish handling it. Each packet command is associated with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408)  * callback function that will be called when the command is finished.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410)  * The handling will be done in three stages:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412)  * 1. ide_tape_issue_pc will send the packet command to the drive, and will set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413)  * the interrupt handler to ide_pc_intr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415)  * 2. On each interrupt, ide_pc_intr will be called. This step will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416)  * repeated until the device signals us that no more interrupts will be issued.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418)  * 3. ATAPI Tape media access commands have immediate status with a delayed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419)  * process. In case of a successful initiation of a media access packet command,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420)  * the DSC bit will be set when the actual execution of the command is finished.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421)  * Since the tape drive will not issue an interrupt, we have to poll for this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422)  * event. In this case, we define the request as "low priority request" by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423)  * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424)  * exit the driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426)  * ide.c will then give higher priority to requests which originate from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427)  * other device, until will change rq_status to RQ_ACTIVE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429)  * 4. When the packet command is finished, it will be checked for errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431)  * 5. In case an error was found, we queue a request sense packet command in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432)  * front of the request queue and retry the operation up to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433)  * IDETAPE_MAX_PC_RETRIES times.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435)  * 6. In case no error was found, or we decided to give up and not to retry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436)  * again, the callback function will be called and then we will handle the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437)  * request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) static ide_startstop_t ide_tape_issue_pc(ide_drive_t *drive,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 					 struct ide_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 					 struct ide_atapi_pc *pc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	idetape_tape_t *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	struct request *rq = drive->hwif->rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 		drive->failed_pc = pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	/* Set the current packet command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	drive->pc = pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 		(pc->flags & PC_FLAG_ABORT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 		 * We will "abort" retrying a packet command in case legitimate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		 * error code was received (crossing a filemark, or end of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 		 * media, for example).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 		if (!(pc->flags & PC_FLAG_ABORT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 			if (!(pc->c[0] == TEST_UNIT_READY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 			      tape->sense_key == 2 && tape->asc == 4 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 			     (tape->ascq == 1 || tape->ascq == 8))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 				printk(KERN_ERR "ide-tape: %s: I/O error, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 						"pc = %2x, key = %2x, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 						"asc = %2x, ascq = %2x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 						tape->name, pc->c[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 						tape->sense_key, tape->asc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 						tape->ascq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 			/* Giving up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 			pc->error = IDE_DRV_ERROR_GENERAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 		drive->failed_pc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		drive->pc_callback(drive, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 		ide_complete_rq(drive, BLK_STS_IOERR, blk_rq_bytes(rq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 		return ide_stopped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	ide_debug_log(IDE_DBG_SENSE, "retry #%d, cmd: 0x%02x", pc->retries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 		      pc->c[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	pc->retries++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	return ide_issue_pc(drive, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) /* A mode sense command is used to "sense" tape parameters. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	ide_init_pc(pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	pc->c[0] = MODE_SENSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	if (page_code != IDETAPE_BLOCK_DESCRIPTOR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 		/* DBD = 1 - Don't return block descriptors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 		pc->c[1] = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	pc->c[2] = page_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	 * Changed pc->c[3] to 0 (255 will at best return unused info).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	 * For SCSI this byte is defined as subpage instead of high byte
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	 * of length and some IDE drives seem to interpret it this way
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	 * and return an error when 255 is used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	pc->c[3] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	/* We will just discard data in that case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	pc->c[4] = 255;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	if (page_code == IDETAPE_BLOCK_DESCRIPTOR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 		pc->req_xfer = 12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	else if (page_code == IDETAPE_CAPABILITIES_PAGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		pc->req_xfer = 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 		pc->req_xfer = 50;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	ide_hwif_t *hwif = drive->hwif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	idetape_tape_t *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	struct ide_atapi_pc *pc = drive->pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	u8 stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	stat = hwif->tp_ops->read_status(hwif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	if (stat & ATA_DSC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 		if (stat & ATA_ERR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 			/* Error detected */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 			if (pc->c[0] != TEST_UNIT_READY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 				printk(KERN_ERR "ide-tape: %s: I/O error, ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 						tape->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 			/* Retry operation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 			ide_retry_pc(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 			return ide_stopped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 		pc->error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		pc->error = IDE_DRV_ERROR_GENERAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 		drive->failed_pc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	drive->pc_callback(drive, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	return ide_stopped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 				   struct ide_atapi_pc *pc, struct request *rq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 				   u8 opcode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	unsigned int length = blk_rq_sectors(rq) / (tape->blk_size >> 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	ide_init_pc(pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	pc->c[1] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	if (blk_rq_bytes(rq) == tape->buffer_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 		pc->flags |= PC_FLAG_DMA_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	if (opcode == READ_6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		pc->c[0] = READ_6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	else if (opcode == WRITE_6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 		pc->c[0] = WRITE_6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 		pc->flags |= PC_FLAG_WRITING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	memcpy(scsi_req(rq)->cmd, pc->c, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) static ide_startstop_t idetape_do_request(ide_drive_t *drive,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 					  struct request *rq, sector_t block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	ide_hwif_t *hwif = drive->hwif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	idetape_tape_t *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	struct ide_atapi_pc *pc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	struct ide_cmd cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	struct scsi_request *req = scsi_req(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	u8 stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, sector: %llu, nr_sectors: %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 		      req->cmd[0], (unsigned long long)blk_rq_pos(rq),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		      blk_rq_sectors(rq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	BUG_ON(!blk_rq_is_private(rq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	BUG_ON(ide_req(rq)->type != ATA_PRIV_MISC &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	       ide_req(rq)->type != ATA_PRIV_SENSE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	/* Retry a failed packet command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	if (drive->failed_pc && drive->pc->c[0] == REQUEST_SENSE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		pc = drive->failed_pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	 * If the tape is still busy, postpone our request and service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	 * the other device meanwhile.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	stat = hwif->tp_ops->read_status(hwif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	if ((drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	    (req->cmd[13] & REQ_IDETAPE_PC2) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	if (drive->dev_flags & IDE_DFLAG_POST_RESET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 		drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 		drive->dev_flags &= ~IDE_DFLAG_POST_RESET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	if (!(drive->atapi_flags & IDE_AFLAG_IGNORE_DSC) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	    !(stat & ATA_DSC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 		if (!tape->postponed_rq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 			tape->dsc_polling_start = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 			tape->dsc_poll_freq = tape->best_dsc_rw_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 			tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 		} else if (time_after(jiffies, tape->dsc_timeout)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 			printk(KERN_ERR "ide-tape: %s: DSC timeout\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 				tape->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 			if (req->cmd[13] & REQ_IDETAPE_PC2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 				idetape_media_access_finished(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 				return ide_stopped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 				return ide_do_reset(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		} else if (time_after(jiffies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 					tape->dsc_polling_start +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 					IDETAPE_DSC_MA_THRESHOLD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 			tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 		ide_tape_stall_queue(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		return ide_stopped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		drive->atapi_flags &= ~IDE_AFLAG_IGNORE_DSC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 		tape->postponed_rq = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	if (req->cmd[13] & REQ_IDETAPE_READ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 		pc = &tape->queued_pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 		ide_tape_create_rw_cmd(tape, pc, rq, READ_6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	if (req->cmd[13] & REQ_IDETAPE_WRITE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		pc = &tape->queued_pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	if (req->cmd[13] & REQ_IDETAPE_PC1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 		pc = (struct ide_atapi_pc *)ide_req(rq)->special;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 		req->cmd[13] &= ~(REQ_IDETAPE_PC1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 		req->cmd[13] |= REQ_IDETAPE_PC2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	if (req->cmd[13] & REQ_IDETAPE_PC2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		idetape_media_access_finished(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 		return ide_stopped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	/* prepare sense request for this command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	ide_prep_sense(drive, rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	memset(&cmd, 0, sizeof(cmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	if (rq_data_dir(rq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 		cmd.tf_flags |= IDE_TFLAG_WRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	cmd.rq = rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	ide_init_sg_cmd(&cmd, blk_rq_bytes(rq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	ide_map_sg(drive, &cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	return ide_tape_issue_pc(drive, &cmd, pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672)  * Write a filemark if write_filemark=1. Flush the device buffers without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673)  * writing a filemark otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) static void idetape_create_write_filemark_cmd(ide_drive_t *drive,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		struct ide_atapi_pc *pc, int write_filemark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	ide_init_pc(pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	pc->c[0] = WRITE_FILEMARKS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	pc->c[4] = write_filemark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	pc->flags |= PC_FLAG_WAIT_FOR_DSC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	idetape_tape_t *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	struct gendisk *disk = tape->disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	int load_attempted = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	/* Wait for the tape to become ready */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	set_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT), &drive->atapi_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	timeout += jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	while (time_before(jiffies, timeout)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		if (ide_do_test_unit_ready(drive, disk) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 		if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 		    || (tape->asc == 0x3A)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 			/* no media */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 			if (load_attempted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 				return -ENOMEDIUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 			ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 			load_attempted = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 		/* not about to be ready */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 		} else if (!(tape->sense_key == 2 && tape->asc == 4 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 			     (tape->ascq == 1 || tape->ascq == 8)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 		msleep(100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) static int idetape_flush_tape_buffers(ide_drive_t *drive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	struct ide_tape_obj *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	struct ide_atapi_pc pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	idetape_create_write_filemark_cmd(drive, &pc, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	rc = ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	idetape_wait_ready(drive, 60 * 5 * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) static int ide_tape_read_position(ide_drive_t *drive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	idetape_tape_t *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	struct ide_atapi_pc pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	u8 buf[20];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	ide_debug_log(IDE_DBG_FUNC, "enter");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	/* prep cmd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	ide_init_pc(&pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	pc.c[0] = READ_POSITION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	pc.req_xfer = 20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	if (!pc.error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 		ide_debug_log(IDE_DBG_FUNC, "BOP - %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 				(buf[0] & 0x80) ? "Yes" : "No");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 		ide_debug_log(IDE_DBG_FUNC, "EOP - %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 				(buf[0] & 0x40) ? "Yes" : "No");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		if (buf[0] & 0x4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 			printk(KERN_INFO "ide-tape: Block location is unknown"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 					 "to the tape\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 			clear_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 				  &drive->atapi_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 			ide_debug_log(IDE_DBG_FUNC, "Block Location: %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 				      be32_to_cpup((__be32 *)&buf[4]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 			tape->partition = buf[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 			tape->first_frame = be32_to_cpup((__be32 *)&buf[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 			set_bit(ilog2(IDE_AFLAG_ADDRESS_VALID),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 				&drive->atapi_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	return tape->first_frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) static void idetape_create_locate_cmd(ide_drive_t *drive,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		struct ide_atapi_pc *pc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 		unsigned int block, u8 partition, int skip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	ide_init_pc(pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	pc->c[0] = POSITION_TO_ELEMENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	pc->c[1] = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	pc->c[8] = partition;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	pc->flags |= PC_FLAG_WAIT_FOR_DSC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	idetape_tape_t *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	if (tape->chrdev_dir != IDETAPE_DIR_READ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	clear_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	tape->valid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	if (tape->buf != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		kfree(tape->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		tape->buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	tape->chrdev_dir = IDETAPE_DIR_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798)  * Position the tape to the requested block using the LOCATE packet command.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799)  * A READ POSITION command is then issued to check where we are positioned. Like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800)  * all higher level operations, we queue the commands at the tail of the request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801)  * queue and wait for their completion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) static int idetape_position_tape(ide_drive_t *drive, unsigned int block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 		u8 partition, int skip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	idetape_tape_t *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	struct gendisk *disk = tape->disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	struct ide_atapi_pc pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	if (tape->chrdev_dir == IDETAPE_DIR_READ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 		__ide_tape_discard_merge_buffer(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	idetape_wait_ready(drive, 60 * 5 * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	idetape_create_locate_cmd(drive, &pc, block, partition, skip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	ret = ide_tape_read_position(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 					  int restore_position)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	idetape_tape_t *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	int seek, position;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	__ide_tape_discard_merge_buffer(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	if (restore_position) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		position = ide_tape_read_position(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 		seek = position > 0 ? position : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 		if (idetape_position_tape(drive, seek, 0, 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 			printk(KERN_INFO "ide-tape: %s: position_tape failed in"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 					 " %s\n", tape->name, __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844)  * Generate a read/write request for the block device interface and wait for it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845)  * to be serviced.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	idetape_tape_t *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	struct request *rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, size: %d", cmd, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	BUG_ON(cmd != REQ_IDETAPE_READ && cmd != REQ_IDETAPE_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	BUG_ON(size < 0 || size % tape->blk_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	ide_req(rq)->type = ATA_PRIV_MISC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	scsi_req(rq)->cmd[13] = cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	rq->rq_disk = tape->disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	rq->__sector = tape->first_frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	if (size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 		ret = blk_rq_map_kern(drive->queue, rq, tape->buf, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 				      GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 			goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	blk_execute_rq(drive->queue, tape->disk, rq, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	/* calculate the number of transferred bytes and update buffer state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	size -= scsi_req(rq)->resid_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	tape->cur = tape->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	if (cmd == REQ_IDETAPE_READ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		tape->valid = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		tape->valid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	ret = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	if (scsi_req(rq)->result == IDE_DRV_ERROR_GENERAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) out_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	blk_put_request(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	ide_init_pc(pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	pc->c[0] = INQUIRY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	pc->c[4] = 254;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	pc->req_xfer = 254;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) static void idetape_create_rewind_cmd(ide_drive_t *drive,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 		struct ide_atapi_pc *pc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	ide_init_pc(pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	pc->c[0] = REZERO_UNIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	pc->flags |= PC_FLAG_WAIT_FOR_DSC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) static void idetape_create_erase_cmd(struct ide_atapi_pc *pc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	ide_init_pc(pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	pc->c[0] = ERASE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	pc->c[1] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	pc->flags |= PC_FLAG_WAIT_FOR_DSC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	ide_init_pc(pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	pc->c[0] = SPACE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	pc->c[1] = cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	pc->flags |= PC_FLAG_WAIT_FOR_DSC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	idetape_tape_t *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 		printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 				" but we are not writing.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	if (tape->buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		size_t aligned = roundup(tape->valid, tape->blk_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 		memset(tape->cur, 0, aligned - tape->valid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 		idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, aligned);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		kfree(tape->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		tape->buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	tape->chrdev_dir = IDETAPE_DIR_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) static int idetape_init_rw(ide_drive_t *drive, int dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	idetape_tape_t *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	BUG_ON(dir != IDETAPE_DIR_READ && dir != IDETAPE_DIR_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	if (tape->chrdev_dir == dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	if (tape->chrdev_dir == IDETAPE_DIR_READ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 		ide_tape_discard_merge_buffer(drive, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	else if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 		ide_tape_flush_merge_buffer(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		idetape_flush_tape_buffers(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	if (tape->buf || tape->valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		printk(KERN_ERR "ide-tape: valid should be 0 now\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		tape->valid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	if (!tape->buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	tape->chrdev_dir = dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	tape->cur = tape->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	 * Issue a 0 rw command to ensure that DSC handshake is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	 * switched from completion mode to buffer available mode.  No
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	 * point in issuing this if DSC overlap isn't supported, some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	 * drives (Seagate STT3401A) will return an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		int cmd = dir == IDETAPE_DIR_READ ? REQ_IDETAPE_READ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 						  : REQ_IDETAPE_WRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 		rc = idetape_queue_rw_tail(drive, cmd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 			kfree(tape->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 			tape->buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 			tape->chrdev_dir = IDETAPE_DIR_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	idetape_tape_t *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	memset(tape->buf, 0, tape->buffer_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	while (bcount) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		unsigned int count = min(tape->buffer_size, bcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 		idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 		bcount -= count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)  * Rewinds the tape to the Beginning Of the current Partition (BOP). We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008)  * currently support only one partition.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) static int idetape_rewind_tape(ide_drive_t *drive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	struct ide_tape_obj *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	struct gendisk *disk = tape->disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	struct ide_atapi_pc pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	ide_debug_log(IDE_DBG_FUNC, "enter");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	idetape_create_rewind_cmd(drive, &pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	ret = ide_tape_read_position(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) /* mtio.h compatible commands should be issued to the chrdev interface. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 				unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	idetape_tape_t *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	void __user *argp = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	struct idetape_config {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 		int dsc_rw_frequency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 		int dsc_media_access_frequency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 		int nr_stages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	} config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%04x", cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	case 0x0340:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 		if (copy_from_user(&config, argp, sizeof(config)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 		tape->best_dsc_rw_freq = config.dsc_rw_frequency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	case 0x0350:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		memset(&config, 0, sizeof(config));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 		config.nr_stages = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 		if (copy_to_user(argp, &config, sizeof(config)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 					int mt_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	idetape_tape_t *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	struct gendisk *disk = tape->disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	struct ide_atapi_pc pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	int retval, count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	int sprev = !!(tape->caps[4] & 0x20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	ide_debug_log(IDE_DBG_FUNC, "mt_op: %d, mt_count: %d", mt_op, mt_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	if (mt_count == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	if (MTBSF == mt_op || MTBSFM == mt_op) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 		if (!sprev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 		mt_count = -mt_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	if (tape->chrdev_dir == IDETAPE_DIR_READ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		tape->valid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 		if (test_and_clear_bit(ilog2(IDE_AFLAG_FILEMARK),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 				       &drive->atapi_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 			++count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 		ide_tape_discard_merge_buffer(drive, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	switch (mt_op) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	case MTFSF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	case MTBSF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 		idetape_create_space_cmd(&pc, mt_count - count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 					 IDETAPE_SPACE_OVER_FILEMARK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 		return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	case MTFSFM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	case MTBSFM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 		if (!sprev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 		retval = idetape_space_over_filemarks(drive, MTFSF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 						      mt_count - count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 		if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 			return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 		count = (MTBSFM == mt_op ? 1 : -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 		return idetape_space_over_filemarks(drive, MTFSF, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 		printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 				mt_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116)  * Our character device read / write functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118)  * The tape is optimized to maximize throughput when it is transferring an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119)  * integral number of the "continuous transfer limit", which is a parameter of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120)  * the specific tape (26kB on my particular tape, 32kB for Onstream).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122)  * As of version 1.3 of the driver, the character device provides an abstract
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123)  * continuous view of the media - any mix of block sizes (even 1 byte) on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124)  * same backup/restore procedure is supported. The driver will internally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125)  * convert the requests to the recommended transfer unit, so that an unmatch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126)  * between the user's block size to the recommended size will only result in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127)  * (slightly) increased driver overhead, but will no longer hit performance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128)  * This is not applicable to Onstream.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 				   size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	struct ide_tape_obj *tape = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	ide_drive_t *drive = tape->drive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	size_t done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	ssize_t ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	ide_debug_log(IDE_DBG_FUNC, "count %zd", count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	if (tape->chrdev_dir != IDETAPE_DIR_READ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 		if (test_bit(ilog2(IDE_AFLAG_DETECT_BS), &drive->atapi_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 			if (count > tape->blk_size &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 			    (count % tape->blk_size) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 				tape->user_bs_factor = count / tape->blk_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	rc = idetape_init_rw(drive, IDETAPE_DIR_READ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	while (done < count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 		size_t todo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 		/* refill if staging buffer is empty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 		if (!tape->valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 			/* If we are at a filemark, nothing more to read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 			if (test_bit(ilog2(IDE_AFLAG_FILEMARK),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 				     &drive->atapi_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 			/* read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 			if (idetape_queue_rw_tail(drive, REQ_IDETAPE_READ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 						  tape->buffer_size) <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 		/* copy out */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 		todo = min_t(size_t, count - done, tape->valid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 		if (copy_to_user(buf + done, tape->cur, todo))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 			ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 		tape->cur += todo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 		tape->valid -= todo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 		done += todo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	if (!done && test_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 		idetape_space_over_filemarks(drive, MTFSF, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	return ret ? ret : done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 				     size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	struct ide_tape_obj *tape = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	ide_drive_t *drive = tape->drive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	size_t done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	ssize_t ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	/* The drive is write protected. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	if (tape->write_prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 		return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	ide_debug_log(IDE_DBG_FUNC, "count %zd", count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	/* Initialize write operation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	rc = idetape_init_rw(drive, IDETAPE_DIR_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	while (done < count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 		size_t todo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 		/* flush if staging buffer is full */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		if (tape->valid == tape->buffer_size &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 		    idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 					  tape->buffer_size) <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 		/* copy in */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 		todo = min_t(size_t, count - done,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 			     tape->buffer_size - tape->valid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 		if (copy_from_user(tape->cur, buf + done, todo))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 			ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 		tape->cur += todo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 		tape->valid += todo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 		done += todo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	return ret ? ret : done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) static int idetape_write_filemark(ide_drive_t *drive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	struct ide_tape_obj *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	struct ide_atapi_pc pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	/* Write a filemark */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	idetape_create_write_filemark_cmd(drive, &pc, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 	if (ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 		printk(KERN_ERR "ide-tape: Couldn't write a filemark\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243)  * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244)  * requested.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246)  * Note: MTBSF and MTBSFM are not supported when the tape doesn't support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247)  * spacing over filemarks in the reverse direction. In this case, MTFSFM is also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248)  * usually not supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250)  * The following commands are currently not supported:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252)  * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253)  * MT_ST_WRITE_THRESHOLD.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	idetape_tape_t *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	struct gendisk *disk = tape->disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	struct ide_atapi_pc pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	int i, retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	ide_debug_log(IDE_DBG_FUNC, "MTIOCTOP ioctl: mt_op: %d, mt_count: %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 		      mt_op, mt_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	switch (mt_op) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 	case MTFSF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	case MTFSFM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	case MTBSF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	case MTBSFM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 		if (!mt_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 		return idetape_space_over_filemarks(drive, mt_op, mt_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	switch (mt_op) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	case MTWEOF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 		if (tape->write_prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 			return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 		ide_tape_discard_merge_buffer(drive, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 		for (i = 0; i < mt_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 			retval = idetape_write_filemark(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 			if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 				return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	case MTREW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 		ide_tape_discard_merge_buffer(drive, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 		if (idetape_rewind_tape(drive))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	case MTLOAD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 		ide_tape_discard_merge_buffer(drive, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 		return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	case MTUNLOAD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	case MTOFFL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 		 * If door is locked, attempt to unlock before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 		 * attempting to eject.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 		if (tape->door_locked) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 			if (!ide_set_media_lock(drive, disk, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 				tape->door_locked = DOOR_UNLOCKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		ide_tape_discard_merge_buffer(drive, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 		retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 		if (!retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 			clear_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 				  &drive->atapi_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	case MTNOP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 		ide_tape_discard_merge_buffer(drive, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 		return idetape_flush_tape_buffers(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	case MTRETEN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 		ide_tape_discard_merge_buffer(drive, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 		return ide_do_start_stop(drive, disk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 			IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 	case MTEOM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 		idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 		return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	case MTERASE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 		(void)idetape_rewind_tape(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 		idetape_create_erase_cmd(&pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 		return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	case MTSETBLK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 		if (mt_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 			if (mt_count < tape->blk_size ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 			    mt_count % tape->blk_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 				return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 			tape->user_bs_factor = mt_count / tape->blk_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 			clear_bit(ilog2(IDE_AFLAG_DETECT_BS),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 				  &drive->atapi_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 			set_bit(ilog2(IDE_AFLAG_DETECT_BS),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 				&drive->atapi_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	case MTSEEK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 		ide_tape_discard_merge_buffer(drive, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 		return idetape_position_tape(drive,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 			mt_count * tape->user_bs_factor, tape->partition, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	case MTSETPART:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 		ide_tape_discard_merge_buffer(drive, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 		return idetape_position_tape(drive, 0, mt_count, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	case MTFSR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 	case MTBSR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	case MTLOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 		retval = ide_set_media_lock(drive, disk, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 		if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 			return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 		tape->door_locked = DOOR_EXPLICITLY_LOCKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	case MTUNLOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 		retval = ide_set_media_lock(drive, disk, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 		if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 			return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 		tape->door_locked = DOOR_UNLOCKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 		printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 				mt_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367)  * Our character device ioctls. General mtio.h magnetic io commands are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368)  * supported here, and not in the corresponding block interface. Our own
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369)  * ide-tape ioctls are supported on both interfaces.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) static long do_idetape_chrdev_ioctl(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 				unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	struct ide_tape_obj *tape = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	ide_drive_t *drive = tape->drive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 	struct mtop mtop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	struct mtget mtget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	struct mtpos mtpos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	int block_offset = 0, position = tape->first_frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	void __user *argp = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 	ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x", cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 		ide_tape_flush_merge_buffer(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 		idetape_flush_tape_buffers(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 	if (cmd == MTIOCGET || cmd == MTIOCPOS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 		block_offset = tape->valid /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 			(tape->blk_size * tape->user_bs_factor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 		position = ide_tape_read_position(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 		if (position < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	case MTIOCTOP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 		if (copy_from_user(&mtop, argp, sizeof(struct mtop)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 		return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 	case MTIOCGET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 		memset(&mtget, 0, sizeof(struct mtget));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 		mtget.mt_type = MT_ISSCSI2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 		mtget.mt_blkno = position / tape->user_bs_factor - block_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 		mtget.mt_dsreg =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 			((tape->blk_size * tape->user_bs_factor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 			 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 		if (tape->drv_write_prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 			mtget.mt_gstat |= GMT_WR_PROT(0xffffffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 		return put_user_mtget(argp, &mtget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	case MTIOCPOS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 		mtpos.mt_blkno = position / tape->user_bs_factor - block_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 		return put_user_mtpos(argp, &mtpos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 		if (tape->chrdev_dir == IDETAPE_DIR_READ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 			ide_tape_discard_merge_buffer(drive, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 		return idetape_blkdev_ioctl(drive, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) static long idetape_chrdev_ioctl(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 				unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 	mutex_lock(&ide_tape_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	ret = do_idetape_chrdev_ioctl(file, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	mutex_unlock(&ide_tape_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) static long idetape_chrdev_compat_ioctl(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 				unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 	long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	if (cmd == MTIOCPOS32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 		cmd = MTIOCPOS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 	else if (cmd == MTIOCGET32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 		cmd = MTIOCGET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	mutex_lock(&ide_tape_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	ret = do_idetape_chrdev_ioctl(file, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	mutex_unlock(&ide_tape_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449)  * Do a mode sense page 0 with block descriptor and if it succeeds set the tape
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450)  * block size with the reported value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	idetape_tape_t *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	struct ide_atapi_pc pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	u8 buf[12];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 		printk(KERN_ERR "ide-tape: Can't get block descriptor\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 		if (tape->blk_size == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 			printk(KERN_WARNING "ide-tape: Cannot deal with zero "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 					    "block size, assuming 32k\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 			tape->blk_size = 32768;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 	tape->blk_size = (buf[4 + 5] << 16) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 				(buf[4 + 6] << 8)  +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 				 buf[4 + 7];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 	tape->drv_write_prot = (buf[2] & 0x80) >> 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 	ide_debug_log(IDE_DBG_FUNC, "blk_size: %d, write_prot: %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 		      tape->blk_size, tape->drv_write_prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) static int idetape_chrdev_open(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 	unsigned int minor = iminor(inode), i = minor & ~0xc0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	ide_drive_t *drive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	idetape_tape_t *tape;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	if (i >= MAX_HWIFS * MAX_DRIVES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	mutex_lock(&idetape_chrdev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	tape = ide_tape_get(NULL, true, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	if (!tape) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 		mutex_unlock(&idetape_chrdev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	drive = tape->drive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	filp->private_data = tape;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	ide_debug_log(IDE_DBG_FUNC, "enter");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	 * We really want to do nonseekable_open(inode, filp); here, but some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 	 * versions of tar incorrectly call lseek on tapes and bail out if that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	 * fails.  So we disallow pread() and pwrite(), but permit lseeks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 	filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	if (test_and_set_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 		retval = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 		goto out_put_tape;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	retval = idetape_wait_ready(drive, 60 * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 		clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 		printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 		goto out_put_tape;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	ide_tape_read_position(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	if (!test_bit(ilog2(IDE_AFLAG_ADDRESS_VALID), &drive->atapi_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 		(void)idetape_rewind_tape(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	/* Read block size and write protect status from drive. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	ide_tape_get_bsize_from_bdesc(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	/* Set write protect flag if device is opened as read-only. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 		tape->write_prot = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 		tape->write_prot = tape->drv_write_prot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	/* Make sure drive isn't write protected if user wants to write. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	if (tape->write_prot) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 		if ((filp->f_flags & O_ACCMODE) == O_WRONLY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 		    (filp->f_flags & O_ACCMODE) == O_RDWR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 			clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 			retval = -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 			goto out_put_tape;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 	/* Lock the tape drive door so user can't eject. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 		if (!ide_set_media_lock(drive, tape->disk, 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 			if (tape->door_locked != DOOR_EXPLICITLY_LOCKED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 				tape->door_locked = DOOR_LOCKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	mutex_unlock(&idetape_chrdev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) out_put_tape:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 	ide_tape_put(tape);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 	mutex_unlock(&idetape_chrdev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	idetape_tape_t *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	ide_tape_flush_merge_buffer(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	if (tape->buf != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 		idetape_pad_zeros(drive, tape->blk_size *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 				(tape->user_bs_factor - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 		kfree(tape->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 		tape->buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	idetape_write_filemark(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	idetape_flush_tape_buffers(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	idetape_flush_tape_buffers(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) static int idetape_chrdev_release(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 	struct ide_tape_obj *tape = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	ide_drive_t *drive = tape->drive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 	unsigned int minor = iminor(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	mutex_lock(&idetape_chrdev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 	ide_debug_log(IDE_DBG_FUNC, "enter");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 		idetape_write_release(drive, minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	if (tape->chrdev_dir == IDETAPE_DIR_READ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 		if (minor < 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 			ide_tape_discard_merge_buffer(drive, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 	if (minor < 128 && test_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 				    &drive->atapi_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 		(void) idetape_rewind_tape(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 	if (tape->chrdev_dir == IDETAPE_DIR_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 		if (tape->door_locked == DOOR_LOCKED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 			if (!ide_set_media_lock(drive, tape->disk, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 				tape->door_locked = DOOR_UNLOCKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 	clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 	ide_tape_put(tape);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	mutex_unlock(&idetape_chrdev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) static void idetape_get_inquiry_results(ide_drive_t *drive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 	idetape_tape_t *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 	struct ide_atapi_pc pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	u8 pc_buf[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	char fw_rev[4], vendor_id[8], product_id[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	idetape_create_inquiry_cmd(&pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	if (ide_queue_pc_tail(drive, tape->disk, &pc, pc_buf, pc.req_xfer)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 		printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 				tape->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 	memcpy(vendor_id, &pc_buf[8], 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 	memcpy(product_id, &pc_buf[16], 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 	memcpy(fw_rev, &pc_buf[32], 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 	ide_fixstring(vendor_id, 8, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 	ide_fixstring(product_id, 16, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 	ide_fixstring(fw_rev, 4, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 	printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 			drive->name, tape->name, vendor_id, product_id, fw_rev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642)  * Ask the tape about its various parameters. In particular, we will adjust our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643)  * data transfer buffer	size to the recommended value as returned by the tape.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) static void idetape_get_mode_sense_results(ide_drive_t *drive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 	idetape_tape_t *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	struct ide_atapi_pc pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	u8 buf[24], *caps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 	u8 speed, max_speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 	if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 		printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 				" some default values\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 		tape->blk_size = 512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 		put_unaligned(52,   (u16 *)&tape->caps[12]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 		put_unaligned(540,  (u16 *)&tape->caps[14]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 		put_unaligned(6*52, (u16 *)&tape->caps[16]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	caps = buf + 4 + buf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 	/* convert to host order and save for later use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 	speed = be16_to_cpup((__be16 *)&caps[14]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 	max_speed = be16_to_cpup((__be16 *)&caps[8]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 	*(u16 *)&caps[8] = max_speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	*(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 	*(u16 *)&caps[14] = speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 	*(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 	if (!speed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 		printk(KERN_INFO "ide-tape: %s: invalid tape speed "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 				"(assuming 650KB/sec)\n", drive->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 		*(u16 *)&caps[14] = 650;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 	if (!max_speed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 		printk(KERN_INFO "ide-tape: %s: invalid max_speed "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 				"(assuming 650KB/sec)\n", drive->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 		*(u16 *)&caps[8] = 650;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 	memcpy(&tape->caps, caps, 20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 	/* device lacks locking support according to capabilities page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	if ((caps[6] & 1) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 		drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 	if (caps[7] & 0x02)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 		tape->blk_size = 512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 	else if (caps[7] & 0x04)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 		tape->blk_size = 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) #ifdef CONFIG_IDE_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) #define ide_tape_devset_get(name, field) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) static int get_##name(ide_drive_t *drive) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 	idetape_tape_t *tape = drive->driver_data; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 	return tape->field; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) #define ide_tape_devset_set(name, field) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) static int set_##name(ide_drive_t *drive, int arg) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 	idetape_tape_t *tape = drive->driver_data; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 	tape->field = arg; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 	return 0; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) #define ide_tape_devset_rw_field(_name, _field) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) ide_tape_devset_get(_name, _field) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) ide_tape_devset_set(_name, _field) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) #define ide_tape_devset_r_field(_name, _field) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) ide_tape_devset_get(_name, _field) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) IDE_DEVSET(_name, 0, get_##_name, NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) static int mulf_tdsc(ide_drive_t *drive)	{ return 1000; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) static int divf_tdsc(ide_drive_t *drive)	{ return   HZ; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) static int divf_buffer(ide_drive_t *drive)	{ return    2; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) static int divf_buffer_size(ide_drive_t *drive)	{ return 1024; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) ide_tape_devset_r_field(avg_speed, avg_speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) ide_tape_devset_r_field(speed, caps[14]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) ide_tape_devset_r_field(buffer, caps[16]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) ide_tape_devset_r_field(buffer_size, buffer_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) static const struct ide_proc_devset idetape_settings[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 	__IDE_PROC_DEVSET(avg_speed,	0, 0xffff, NULL, NULL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 	__IDE_PROC_DEVSET(buffer,	0, 0xffff, NULL, divf_buffer),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 	__IDE_PROC_DEVSET(buffer_size,	0, 0xffff, NULL, divf_buffer_size),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 	__IDE_PROC_DEVSET(dsc_overlap,	0,      1, NULL, NULL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 	__IDE_PROC_DEVSET(speed,	0, 0xffff, NULL, NULL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 	__IDE_PROC_DEVSET(tdsc,		IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 					mulf_tdsc, divf_tdsc),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 	{ NULL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748)  * The function below is called to:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750)  * 1. Initialize our various state variables.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751)  * 2. Ask the tape for its capabilities.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752)  * 3. Allocate a buffer which will be used for data transfer. The buffer size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753)  * is chosen based on the recommendation which we received in step 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755)  * Note that at this point ide.c already assigned us an irq, so that we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756)  * queue requests here and wait for their completion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 	unsigned long t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 	int speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 	u16 *ctl = (u16 *)&tape->caps[12];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 	ide_debug_log(IDE_DBG_FUNC, "minor: %d", minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 	drive->pc_callback = ide_tape_callback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 	drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 	if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 		printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 				 tape->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 		drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 	/* Seagate Travan drives do not support DSC overlap. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 		drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 	tape->minor = minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	tape->name[0] = 'h';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	tape->name[1] = 't';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 	tape->name[2] = '0' + minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	tape->chrdev_dir = IDETAPE_DIR_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 	idetape_get_inquiry_results(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 	idetape_get_mode_sense_results(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 	ide_tape_get_bsize_from_bdesc(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 	tape->user_bs_factor = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 	tape->buffer_size = *ctl * tape->blk_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	while (tape->buffer_size > 0xffff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 		printk(KERN_NOTICE "ide-tape: decreasing stage size\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 		*ctl /= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 		tape->buffer_size = *ctl * tape->blk_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 	/* select the "best" DSC read/write polling freq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 	speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 	t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 	 * Ensure that the number we got makes sense; limit it within
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 	 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 	tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 					 IDETAPE_DSC_RW_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 	printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 		"%ums tDSC%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 		drive->name, tape->name, *(u16 *)&tape->caps[14],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 		(*(u16 *)&tape->caps[16] * 512) / tape->buffer_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 		tape->buffer_size / 1024,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 		jiffies_to_msecs(tape->best_dsc_rw_freq),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 		(drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 	ide_proc_register_driver(drive, tape->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) static void ide_tape_remove(ide_drive_t *drive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 	idetape_tape_t *tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 	ide_proc_unregister_driver(drive, tape->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 	device_del(&tape->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 	ide_unregister_region(tape->disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 	mutex_lock(&idetape_ref_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 	put_device(&tape->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 	mutex_unlock(&idetape_ref_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) static void ide_tape_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 	struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 	ide_drive_t *drive = tape->drive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 	struct gendisk *g = tape->disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 	BUG_ON(tape->valid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 	drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 	drive->driver_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 	device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 	device_destroy(idetape_sysfs_class,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 			MKDEV(IDETAPE_MAJOR, tape->minor + 128));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 	idetape_devs[tape->minor] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 	g->private_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 	put_disk(g);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 	kfree(tape);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) #ifdef CONFIG_IDE_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) static int idetape_name_proc_show(struct seq_file *m, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 	ide_drive_t	*drive = (ide_drive_t *) m->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 	idetape_tape_t	*tape = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 	seq_printf(m, "%s\n", tape->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) static ide_proc_entry_t idetape_proc[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 	{ "capacity",	S_IFREG|S_IRUGO,	ide_capacity_proc_show	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 	{ "name",	S_IFREG|S_IRUGO,	idetape_name_proc_show	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 	{}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	return idetape_proc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 	return idetape_settings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) static int ide_tape_probe(ide_drive_t *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) static struct ide_driver idetape_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 	.gen_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 		.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 		.name		= "ide-tape",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 		.bus		= &ide_bus_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 	.probe			= ide_tape_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 	.remove			= ide_tape_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 	.version		= IDETAPE_VERSION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 	.do_request		= idetape_do_request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) #ifdef CONFIG_IDE_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 	.proc_entries		= ide_tape_proc_entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 	.proc_devsets		= ide_tape_proc_devsets,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) /* Our character device supporting functions, passed to register_chrdev. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) static const struct file_operations idetape_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 	.read		= idetape_chrdev_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 	.write		= idetape_chrdev_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 	.unlocked_ioctl	= idetape_chrdev_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 	.compat_ioctl	= IS_ENABLED(CONFIG_COMPAT) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 			  idetape_chrdev_compat_ioctl : NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 	.open		= idetape_chrdev_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 	.release	= idetape_chrdev_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 	.llseek		= noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) static int idetape_open(struct block_device *bdev, fmode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 	struct ide_tape_obj *tape;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 	mutex_lock(&ide_tape_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 	tape = ide_tape_get(bdev->bd_disk, false, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 	mutex_unlock(&ide_tape_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 	if (!tape)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) static void idetape_release(struct gendisk *disk, fmode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 	struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 	mutex_lock(&ide_tape_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 	ide_tape_put(tape);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 	mutex_unlock(&ide_tape_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) static int idetape_ioctl(struct block_device *bdev, fmode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 			unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 	struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 	ide_drive_t *drive = tape->drive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 	mutex_lock(&ide_tape_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 	err = generic_ide_ioctl(drive, bdev, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 	if (err == -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 		err = idetape_blkdev_ioctl(drive, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 	mutex_unlock(&ide_tape_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) static int idetape_compat_ioctl(struct block_device *bdev, fmode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 				unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951)         if (cmd == 0x0340 || cmd == 0x350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 		arg = (unsigned long)compat_ptr(arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 	return idetape_ioctl(bdev, mode, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) static const struct block_device_operations idetape_block_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 	.open		= idetape_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 	.release	= idetape_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 	.ioctl		= idetape_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	.compat_ioctl	= IS_ENABLED(CONFIG_COMPAT) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 				idetape_compat_ioctl : NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) static int ide_tape_probe(ide_drive_t *drive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 	idetape_tape_t *tape;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 	struct gendisk *g;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 	int minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 	ide_debug_log(IDE_DBG_FUNC, "enter");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 	if (!strstr(DRV_NAME, drive->driver_req))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 	if (drive->media != ide_tape)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 	if ((drive->dev_flags & IDE_DFLAG_ID_READ) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 	    ide_check_atapi_device(drive, DRV_NAME) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 		printk(KERN_ERR "ide-tape: %s: not supported by this version of"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 				" the driver\n", drive->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 	tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 	if (tape == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 		printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 				drive->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 	g = alloc_disk(1 << PARTN_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 	if (!g)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 		goto out_free_tape;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 	ide_init_disk(g, drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 	tape->dev.parent = &drive->gendev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 	tape->dev.release = ide_tape_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 	dev_set_name(&tape->dev, "%s", dev_name(&drive->gendev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 	if (device_register(&tape->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 		goto out_free_disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 	tape->drive = drive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 	tape->driver = &idetape_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 	tape->disk = g;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 	g->private_data = &tape->driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 	drive->driver_data = tape;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 	mutex_lock(&idetape_ref_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 	for (minor = 0; idetape_devs[minor]; minor++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 		;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 	idetape_devs[minor] = tape;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 	mutex_unlock(&idetape_ref_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 	idetape_setup(drive, tape, minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 	device_create(idetape_sysfs_class, &drive->gendev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 		      MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 	device_create(idetape_sysfs_class, &drive->gendev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 		      MKDEV(IDETAPE_MAJOR, minor + 128), NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 		      "n%s", tape->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 	g->fops = &idetape_block_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 	ide_register_region(g);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) out_free_disk:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 	put_disk(g);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) out_free_tape:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 	kfree(tape);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 	return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) static void __exit idetape_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 	driver_unregister(&idetape_driver.gen_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 	class_destroy(idetape_sysfs_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 	unregister_chrdev(IDETAPE_MAJOR, "ht");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) static int __init idetape_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 	int error = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 	idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 	if (IS_ERR(idetape_sysfs_class)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 		idetape_sysfs_class = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 		printk(KERN_ERR "Unable to create sysfs class for ide tapes\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 		error = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 	if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 		printk(KERN_ERR "ide-tape: Failed to register chrdev"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 				" interface\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 		error = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 		goto out_free_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 	error = driver_register(&idetape_driver.gen_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 		goto out_free_chrdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) out_free_chrdev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 	unregister_chrdev(IDETAPE_MAJOR, "ht");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) out_free_class:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 	class_destroy(idetape_sysfs_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) MODULE_ALIAS("ide:*m-tape*");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) module_init(idetape_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) module_exit(idetape_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) MODULE_LICENSE("GPL");