| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |    SCSI Tape Driver for Linux version 1.1 and newer. See the accompanying | 
 | 3 |    file Documentation/scsi/st.txt for more information. | 
 | 4 |  | 
 | 5 |    History: | 
 | 6 |    Rewritten from Dwayne Forsyth's SCSI tape driver by Kai Makisara. | 
 | 7 |    Contribution and ideas from several people including (in alphabetical | 
 | 8 |    order) Klaus Ehrenfried, Eugene Exarevsky, Eric Lee Green, Wolfgang Denk, | 
 | 9 |    Steve Hirsch, Andreas Koppenh"ofer, Michael Leodolter, Eyal Lebedinsky, | 
 | 10 |    Michael Schaefer, J"org Weule, and Eric Youngdale. | 
 | 11 |  | 
 | 12 |    Copyright 1992 - 2005 Kai Makisara | 
 | 13 |    email Kai.Makisara@kolumbus.fi | 
 | 14 |  | 
 | 15 |    Some small formal changes - aeb, 950809 | 
 | 16 |  | 
 | 17 |    Last modified: 18-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Devfs support | 
 | 18 |  */ | 
 | 19 |  | 
| Kai Makisara  | 16c4b3e | 2005-05-01 18:11:55 +0300 | [diff] [blame] | 20 | static char *verstr = "20050501"; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 |  | 
 | 22 | #include <linux/module.h> | 
 | 23 |  | 
 | 24 | #include <linux/fs.h> | 
 | 25 | #include <linux/kernel.h> | 
 | 26 | #include <linux/sched.h> | 
 | 27 | #include <linux/mm.h> | 
 | 28 | #include <linux/init.h> | 
 | 29 | #include <linux/string.h> | 
 | 30 | #include <linux/errno.h> | 
 | 31 | #include <linux/mtio.h> | 
| Kai Makisara  | 16c4b3e | 2005-05-01 18:11:55 +0300 | [diff] [blame] | 32 | #include <linux/cdrom.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/ioctl.h> | 
 | 34 | #include <linux/fcntl.h> | 
 | 35 | #include <linux/spinlock.h> | 
 | 36 | #include <linux/blkdev.h> | 
 | 37 | #include <linux/moduleparam.h> | 
 | 38 | #include <linux/devfs_fs_kernel.h> | 
 | 39 | #include <linux/cdev.h> | 
 | 40 | #include <linux/delay.h> | 
 | 41 |  | 
 | 42 | #include <asm/uaccess.h> | 
 | 43 | #include <asm/dma.h> | 
 | 44 | #include <asm/system.h> | 
 | 45 |  | 
 | 46 | #include <scsi/scsi.h> | 
 | 47 | #include <scsi/scsi_dbg.h> | 
 | 48 | #include <scsi/scsi_device.h> | 
 | 49 | #include <scsi/scsi_driver.h> | 
 | 50 | #include <scsi/scsi_eh.h> | 
 | 51 | #include <scsi/scsi_host.h> | 
 | 52 | #include <scsi/scsi_ioctl.h> | 
 | 53 | #include <scsi/scsi_request.h> | 
| Kai Makisara  | 16c4b3e | 2005-05-01 18:11:55 +0300 | [diff] [blame] | 54 | #include <scsi/sg.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 |  | 
 | 56 |  | 
 | 57 | /* The driver prints some debugging information on the console if DEBUG | 
 | 58 |    is defined and non-zero. */ | 
 | 59 | #define DEBUG 0 | 
 | 60 |  | 
 | 61 | #if DEBUG | 
 | 62 | /* The message level for the debug messages is currently set to KERN_NOTICE | 
 | 63 |    so that people can easily see the messages. Later when the debugging messages | 
 | 64 |    in the drivers are more widely classified, this may be changed to KERN_DEBUG. */ | 
 | 65 | #define ST_DEB_MSG  KERN_NOTICE | 
 | 66 | #define DEB(a) a | 
 | 67 | #define DEBC(a) if (debugging) { a ; } | 
 | 68 | #else | 
 | 69 | #define DEB(a) | 
 | 70 | #define DEBC(a) | 
 | 71 | #endif | 
 | 72 |  | 
 | 73 | #define ST_KILOBYTE 1024 | 
 | 74 |  | 
 | 75 | #include "st_options.h" | 
 | 76 | #include "st.h" | 
 | 77 |  | 
 | 78 | static int buffer_kbs; | 
 | 79 | static int max_sg_segs; | 
 | 80 | static int try_direct_io = TRY_DIRECT_IO; | 
 | 81 | static int try_rdio = 1; | 
 | 82 | static int try_wdio = 1; | 
 | 83 |  | 
 | 84 | static int st_dev_max; | 
 | 85 | static int st_nr_dev; | 
 | 86 |  | 
| gregkh@suse.de | d253878 | 2005-03-23 09:55:22 -0800 | [diff] [blame] | 87 | static struct class *st_sysfs_class; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 |  | 
 | 89 | MODULE_AUTHOR("Kai Makisara"); | 
 | 90 | MODULE_DESCRIPTION("SCSI Tape Driver"); | 
 | 91 | MODULE_LICENSE("GPL"); | 
 | 92 |  | 
 | 93 | /* Set 'perm' (4th argument) to 0 to disable module_param's definition | 
 | 94 |  * of sysfs parameters (which module_param doesn't yet support). | 
 | 95 |  * Sysfs parameters defined explicitly later. | 
 | 96 |  */ | 
 | 97 | module_param_named(buffer_kbs, buffer_kbs, int, 0); | 
 | 98 | MODULE_PARM_DESC(buffer_kbs, "Default driver buffer size for fixed block mode (KB; 32)"); | 
 | 99 | module_param_named(max_sg_segs, max_sg_segs, int, 0); | 
 | 100 | MODULE_PARM_DESC(max_sg_segs, "Maximum number of scatter/gather segments to use (256)"); | 
 | 101 | module_param_named(try_direct_io, try_direct_io, int, 0); | 
 | 102 | MODULE_PARM_DESC(try_direct_io, "Try direct I/O between user buffer and tape drive (1)"); | 
 | 103 |  | 
 | 104 | /* Extra parameters for testing */ | 
 | 105 | module_param_named(try_rdio, try_rdio, int, 0); | 
 | 106 | MODULE_PARM_DESC(try_rdio, "Try direct read i/o when possible"); | 
 | 107 | module_param_named(try_wdio, try_wdio, int, 0); | 
 | 108 | MODULE_PARM_DESC(try_wdio, "Try direct write i/o when possible"); | 
 | 109 |  | 
 | 110 | #ifndef MODULE | 
 | 111 | static int write_threshold_kbs;  /* retained for compatibility */ | 
 | 112 | static struct st_dev_parm { | 
 | 113 | 	char *name; | 
 | 114 | 	int *val; | 
 | 115 | } parms[] __initdata = { | 
 | 116 | 	{ | 
 | 117 | 		"buffer_kbs", &buffer_kbs | 
 | 118 | 	}, | 
 | 119 | 	{       /* Retained for compatibility with 2.4 */ | 
 | 120 | 		"write_threshold_kbs", &write_threshold_kbs | 
 | 121 | 	}, | 
 | 122 | 	{ | 
 | 123 | 		"max_sg_segs", NULL | 
 | 124 | 	}, | 
 | 125 | 	{ | 
 | 126 | 		"try_direct_io", &try_direct_io | 
 | 127 | 	} | 
 | 128 | }; | 
 | 129 | #endif | 
 | 130 |  | 
 | 131 | /* Restrict the number of modes so that names for all are assigned */ | 
 | 132 | #if ST_NBR_MODES > 16 | 
 | 133 | #error "Maximum number of modes is 16" | 
 | 134 | #endif | 
 | 135 | /* Bit reversed order to get same names for same minors with all | 
 | 136 |    mode counts */ | 
 | 137 | static char *st_formats[] = { | 
 | 138 | 	"",  "r", "k", "s", "l", "t", "o", "u", | 
 | 139 | 	"m", "v", "p", "x", "a", "y", "q", "z"};  | 
 | 140 |  | 
 | 141 | /* The default definitions have been moved to st_options.h */ | 
 | 142 |  | 
 | 143 | #define ST_FIXED_BUFFER_SIZE (ST_FIXED_BUFFER_BLOCKS * ST_KILOBYTE) | 
 | 144 |  | 
 | 145 | /* The buffer size should fit into the 24 bits for length in the | 
 | 146 |    6-byte SCSI read and write commands. */ | 
 | 147 | #if ST_FIXED_BUFFER_SIZE >= (2 << 24 - 1) | 
 | 148 | #error "Buffer size should not exceed (2 << 24 - 1) bytes!" | 
 | 149 | #endif | 
 | 150 |  | 
 | 151 | static int debugging = DEBUG; | 
 | 152 |  | 
 | 153 | #define MAX_RETRIES 0 | 
 | 154 | #define MAX_WRITE_RETRIES 0 | 
 | 155 | #define MAX_READY_RETRIES 0 | 
 | 156 | #define NO_TAPE  NOT_READY | 
 | 157 |  | 
 | 158 | #define ST_TIMEOUT (900 * HZ) | 
 | 159 | #define ST_LONG_TIMEOUT (14000 * HZ) | 
 | 160 |  | 
 | 161 | /* Remove mode bits and auto-rewind bit (7) */ | 
 | 162 | #define TAPE_NR(x) ( ((iminor(x) & ~255) >> (ST_NBR_MODE_BITS + 1)) | \ | 
 | 163 |     (iminor(x) & ~(-1 << ST_MODE_SHIFT)) ) | 
 | 164 | #define TAPE_MODE(x) ((iminor(x) & ST_MODE_MASK) >> ST_MODE_SHIFT) | 
 | 165 |  | 
 | 166 | /* Construct the minor number from the device (d), mode (m), and non-rewind (n) data */ | 
 | 167 | #define TAPE_MINOR(d, m, n) (((d & ~(255 >> (ST_NBR_MODE_BITS + 1))) << (ST_NBR_MODE_BITS + 1)) | \ | 
 | 168 |   (d & (255 >> (ST_NBR_MODE_BITS + 1))) | (m << ST_MODE_SHIFT) | ((n != 0) << 7) ) | 
 | 169 |  | 
 | 170 | /* Internal ioctl to set both density (uppermost 8 bits) and blocksize (lower | 
 | 171 |    24 bits) */ | 
 | 172 | #define SET_DENS_AND_BLK 0x10001 | 
 | 173 |  | 
 | 174 | static DEFINE_RWLOCK(st_dev_arr_lock); | 
 | 175 |  | 
 | 176 | static int st_fixed_buffer_size = ST_FIXED_BUFFER_SIZE; | 
 | 177 | static int st_max_sg_segs = ST_MAX_SG; | 
 | 178 |  | 
 | 179 | static struct scsi_tape **scsi_tapes = NULL; | 
 | 180 |  | 
 | 181 | static int modes_defined; | 
 | 182 |  | 
 | 183 | static struct st_buffer *new_tape_buffer(int, int, int); | 
 | 184 | static int enlarge_buffer(struct st_buffer *, int, int); | 
 | 185 | static void normalize_buffer(struct st_buffer *); | 
 | 186 | static int append_to_buffer(const char __user *, struct st_buffer *, int); | 
 | 187 | static int from_buffer(struct st_buffer *, char __user *, int); | 
 | 188 | static void move_buffer_data(struct st_buffer *, int); | 
 | 189 | static void buf_to_sg(struct st_buffer *, unsigned int); | 
 | 190 |  | 
 | 191 | static int st_map_user_pages(struct scatterlist *, const unsigned int,  | 
 | 192 | 			     unsigned long, size_t, int, unsigned long); | 
 | 193 | static int sgl_map_user_pages(struct scatterlist *, const unsigned int,  | 
 | 194 | 			      unsigned long, size_t, int); | 
 | 195 | static int sgl_unmap_user_pages(struct scatterlist *, const unsigned int, int); | 
 | 196 |  | 
 | 197 | static int st_probe(struct device *); | 
 | 198 | static int st_remove(struct device *); | 
 | 199 | static int st_init_command(struct scsi_cmnd *); | 
 | 200 |  | 
 | 201 | static void do_create_driverfs_files(void); | 
 | 202 | static void do_remove_driverfs_files(void); | 
 | 203 | static void do_create_class_files(struct scsi_tape *, int, int); | 
 | 204 |  | 
 | 205 | static struct scsi_driver st_template = { | 
 | 206 | 	.owner			= THIS_MODULE, | 
 | 207 | 	.gendrv = { | 
 | 208 | 		.name		= "st", | 
 | 209 | 		.probe		= st_probe, | 
 | 210 | 		.remove		= st_remove, | 
 | 211 | 	}, | 
 | 212 | 	.init_command		= st_init_command, | 
 | 213 | }; | 
 | 214 |  | 
 | 215 | static int st_compression(struct scsi_tape *, int); | 
 | 216 |  | 
 | 217 | static int find_partition(struct scsi_tape *); | 
 | 218 | static int switch_partition(struct scsi_tape *); | 
 | 219 |  | 
 | 220 | static int st_int_ioctl(struct scsi_tape *, unsigned int, unsigned long); | 
 | 221 |  | 
 | 222 |  | 
 | 223 | #include "osst_detect.h" | 
 | 224 | #ifndef SIGS_FROM_OSST | 
 | 225 | #define SIGS_FROM_OSST \ | 
 | 226 | 	{"OnStream", "SC-", "", "osst"}, \ | 
 | 227 | 	{"OnStream", "DI-", "", "osst"}, \ | 
 | 228 | 	{"OnStream", "DP-", "", "osst"}, \ | 
 | 229 | 	{"OnStream", "USB", "", "osst"}, \ | 
 | 230 | 	{"OnStream", "FW-", "", "osst"} | 
 | 231 | #endif | 
 | 232 |  | 
 | 233 | struct st_reject_data { | 
 | 234 | 	char *vendor; | 
 | 235 | 	char *model; | 
 | 236 | 	char *rev; | 
 | 237 | 	char *driver_hint; /* Name of the correct driver, NULL if unknown */ | 
 | 238 | }; | 
 | 239 |  | 
 | 240 | static struct st_reject_data reject_list[] = { | 
 | 241 | 	/* {"XXX", "Yy-", "", NULL},  example */ | 
 | 242 | 	SIGS_FROM_OSST, | 
 | 243 | 	{NULL, }}; | 
 | 244 |  | 
 | 245 | /* If the device signature is on the list of incompatible drives, the | 
 | 246 |    function returns a pointer to the name of the correct driver (if known) */ | 
 | 247 | static char * st_incompatible(struct scsi_device* SDp) | 
 | 248 | { | 
 | 249 | 	struct st_reject_data *rp; | 
 | 250 |  | 
 | 251 | 	for (rp=&(reject_list[0]); rp->vendor != NULL; rp++) | 
 | 252 | 		if (!strncmp(rp->vendor, SDp->vendor, strlen(rp->vendor)) && | 
 | 253 | 		    !strncmp(rp->model, SDp->model, strlen(rp->model)) && | 
 | 254 | 		    !strncmp(rp->rev, SDp->rev, strlen(rp->rev))) { | 
 | 255 | 			if (rp->driver_hint) | 
 | 256 | 				return rp->driver_hint; | 
 | 257 | 			else | 
 | 258 | 				return "unknown"; | 
 | 259 | 		} | 
 | 260 | 	return NULL; | 
 | 261 | } | 
 | 262 |  | 
 | 263 |  | 
 | 264 | static inline char *tape_name(struct scsi_tape *tape) | 
 | 265 | { | 
 | 266 | 	return tape->disk->disk_name; | 
 | 267 | } | 
 | 268 |  | 
 | 269 |  | 
 | 270 | static void st_analyze_sense(struct scsi_request *SRpnt, struct st_cmdstatus *s) | 
 | 271 | { | 
 | 272 | 	const u8 *ucp; | 
 | 273 | 	const u8 *sense = SRpnt->sr_sense_buffer; | 
 | 274 |  | 
 | 275 | 	s->have_sense = scsi_request_normalize_sense(SRpnt, &s->sense_hdr); | 
 | 276 | 	s->flags = 0; | 
 | 277 |  | 
 | 278 | 	if (s->have_sense) { | 
 | 279 | 		s->deferred = 0; | 
 | 280 | 		s->remainder_valid = | 
 | 281 | 			scsi_get_sense_info_fld(sense, SCSI_SENSE_BUFFERSIZE, &s->uremainder64); | 
 | 282 | 		switch (sense[0] & 0x7f) { | 
 | 283 | 		case 0x71: | 
 | 284 | 			s->deferred = 1; | 
 | 285 | 		case 0x70: | 
 | 286 | 			s->fixed_format = 1; | 
 | 287 | 			s->flags = sense[2] & 0xe0; | 
 | 288 | 			break; | 
 | 289 | 		case 0x73: | 
 | 290 | 			s->deferred = 1; | 
 | 291 | 		case 0x72: | 
 | 292 | 			s->fixed_format = 0; | 
 | 293 | 			ucp = scsi_sense_desc_find(sense, SCSI_SENSE_BUFFERSIZE, 4); | 
 | 294 | 			s->flags = ucp ? (ucp[3] & 0xe0) : 0; | 
 | 295 | 			break; | 
 | 296 | 		} | 
 | 297 | 	} | 
 | 298 | } | 
 | 299 |  | 
 | 300 |  | 
 | 301 | /* Convert the result to success code */ | 
 | 302 | static int st_chk_result(struct scsi_tape *STp, struct scsi_request * SRpnt) | 
 | 303 | { | 
 | 304 | 	int result = SRpnt->sr_result; | 
 | 305 | 	u8 scode; | 
 | 306 | 	DEB(const char *stp;) | 
 | 307 | 	char *name = tape_name(STp); | 
 | 308 | 	struct st_cmdstatus *cmdstatp; | 
 | 309 |  | 
 | 310 | 	if (!result) | 
 | 311 | 		return 0; | 
 | 312 |  | 
 | 313 | 	cmdstatp = &STp->buffer->cmdstat; | 
 | 314 | 	st_analyze_sense(STp->buffer->last_SRpnt, cmdstatp); | 
 | 315 |  | 
 | 316 | 	if (cmdstatp->have_sense) | 
 | 317 | 		scode = STp->buffer->cmdstat.sense_hdr.sense_key; | 
 | 318 | 	else | 
 | 319 | 		scode = 0; | 
 | 320 |  | 
 | 321 |         DEB( | 
 | 322 |         if (debugging) { | 
 | 323 |                 printk(ST_DEB_MSG "%s: Error: %x, cmd: %x %x %x %x %x %x Len: %d\n", | 
 | 324 | 		       name, result, | 
 | 325 | 		       SRpnt->sr_cmnd[0], SRpnt->sr_cmnd[1], SRpnt->sr_cmnd[2], | 
 | 326 | 		       SRpnt->sr_cmnd[3], SRpnt->sr_cmnd[4], SRpnt->sr_cmnd[5], | 
 | 327 | 		       SRpnt->sr_bufflen); | 
 | 328 | 		if (cmdstatp->have_sense) | 
 | 329 | 			scsi_print_req_sense("st", SRpnt); | 
 | 330 | 	} ) /* end DEB */ | 
 | 331 | 	if (!debugging) { /* Abnormal conditions for tape */ | 
 | 332 | 		if (!cmdstatp->have_sense) | 
 | 333 | 			printk(KERN_WARNING | 
 | 334 | 			       "%s: Error %x (sugg. bt 0x%x, driver bt 0x%x, host bt 0x%x).\n", | 
 | 335 | 			       name, result, suggestion(result), | 
 | 336 | 			       driver_byte(result) & DRIVER_MASK, host_byte(result)); | 
 | 337 | 		else if (cmdstatp->have_sense && | 
 | 338 | 			 scode != NO_SENSE && | 
 | 339 | 			 scode != RECOVERED_ERROR && | 
 | 340 | 			 /* scode != UNIT_ATTENTION && */ | 
 | 341 | 			 scode != BLANK_CHECK && | 
 | 342 | 			 scode != VOLUME_OVERFLOW && | 
 | 343 | 			 SRpnt->sr_cmnd[0] != MODE_SENSE && | 
 | 344 | 			 SRpnt->sr_cmnd[0] != TEST_UNIT_READY) { | 
 | 345 | 				printk(KERN_WARNING "%s: Error with sense data: ", name); | 
 | 346 | 				scsi_print_req_sense("st", SRpnt); | 
 | 347 | 		} | 
 | 348 | 	} | 
 | 349 |  | 
 | 350 | 	if (cmdstatp->fixed_format && | 
 | 351 | 	    STp->cln_mode >= EXTENDED_SENSE_START) {  /* Only fixed format sense */ | 
 | 352 | 		if (STp->cln_sense_value) | 
 | 353 | 			STp->cleaning_req |= ((SRpnt->sr_sense_buffer[STp->cln_mode] & | 
 | 354 | 					       STp->cln_sense_mask) == STp->cln_sense_value); | 
 | 355 | 		else | 
 | 356 | 			STp->cleaning_req |= ((SRpnt->sr_sense_buffer[STp->cln_mode] & | 
 | 357 | 					       STp->cln_sense_mask) != 0); | 
 | 358 | 	} | 
 | 359 | 	if (cmdstatp->have_sense && | 
 | 360 | 	    cmdstatp->sense_hdr.asc == 0 && cmdstatp->sense_hdr.ascq == 0x17) | 
 | 361 | 		STp->cleaning_req = 1; /* ASC and ASCQ => cleaning requested */ | 
 | 362 |  | 
 | 363 | 	STp->pos_unknown |= STp->device->was_reset; | 
 | 364 |  | 
 | 365 | 	if (cmdstatp->have_sense && | 
 | 366 | 	    scode == RECOVERED_ERROR | 
 | 367 | #if ST_RECOVERED_WRITE_FATAL | 
 | 368 | 	    && SRpnt->sr_cmnd[0] != WRITE_6 | 
 | 369 | 	    && SRpnt->sr_cmnd[0] != WRITE_FILEMARKS | 
 | 370 | #endif | 
 | 371 | 	    ) { | 
 | 372 | 		STp->recover_count++; | 
 | 373 | 		STp->recover_reg++; | 
 | 374 |  | 
 | 375 |                 DEB( | 
 | 376 | 		if (debugging) { | 
 | 377 | 			if (SRpnt->sr_cmnd[0] == READ_6) | 
 | 378 | 				stp = "read"; | 
 | 379 | 			else if (SRpnt->sr_cmnd[0] == WRITE_6) | 
 | 380 | 				stp = "write"; | 
 | 381 | 			else | 
 | 382 | 				stp = "ioctl"; | 
 | 383 | 			printk(ST_DEB_MSG "%s: Recovered %s error (%d).\n", name, stp, | 
 | 384 | 			       STp->recover_count); | 
 | 385 | 		} ) /* end DEB */ | 
 | 386 |  | 
 | 387 | 		if (cmdstatp->flags == 0) | 
 | 388 | 			return 0; | 
 | 389 | 	} | 
 | 390 | 	return (-EIO); | 
 | 391 | } | 
 | 392 |  | 
 | 393 |  | 
 | 394 | /* Wakeup from interrupt */ | 
 | 395 | static void st_sleep_done(struct scsi_cmnd * SCpnt) | 
 | 396 | { | 
 | 397 | 	struct scsi_tape *STp = container_of(SCpnt->request->rq_disk->private_data, | 
 | 398 | 					     struct scsi_tape, driver); | 
 | 399 |  | 
 | 400 | 	(STp->buffer)->cmdstat.midlevel_result = SCpnt->result; | 
 | 401 | 	SCpnt->request->rq_status = RQ_SCSI_DONE; | 
 | 402 | 	(STp->buffer)->last_SRpnt = SCpnt->sc_request; | 
 | 403 | 	DEB( STp->write_pending = 0; ) | 
 | 404 |  | 
 | 405 | 	complete(SCpnt->request->waiting); | 
 | 406 | } | 
 | 407 |  | 
 | 408 | /* Do the scsi command. Waits until command performed if do_wait is true. | 
 | 409 |    Otherwise write_behind_check() is used to check that the command | 
 | 410 |    has finished. */ | 
 | 411 | static struct scsi_request * | 
 | 412 | st_do_scsi(struct scsi_request * SRpnt, struct scsi_tape * STp, unsigned char *cmd, | 
 | 413 | 	   int bytes, int direction, int timeout, int retries, int do_wait) | 
 | 414 | { | 
 | 415 | 	unsigned char *bp; | 
 | 416 |  | 
 | 417 | 	if (SRpnt == NULL) { | 
 | 418 | 		SRpnt = scsi_allocate_request(STp->device, GFP_ATOMIC); | 
 | 419 | 		if (SRpnt == NULL) { | 
 | 420 | 			DEBC( printk(KERN_ERR "%s: Can't get SCSI request.\n", | 
 | 421 | 				     tape_name(STp)); ); | 
 | 422 | 			if (signal_pending(current)) | 
 | 423 | 				(STp->buffer)->syscall_result = (-EINTR); | 
 | 424 | 			else | 
 | 425 | 				(STp->buffer)->syscall_result = (-EBUSY); | 
 | 426 | 			return NULL; | 
 | 427 | 		} | 
 | 428 | 	} | 
 | 429 |  | 
 | 430 | 	init_completion(&STp->wait); | 
 | 431 | 	SRpnt->sr_use_sg = STp->buffer->do_dio || (bytes > (STp->buffer)->frp[0].length); | 
 | 432 | 	if (SRpnt->sr_use_sg) { | 
 | 433 | 		if (!STp->buffer->do_dio) | 
 | 434 | 			buf_to_sg(STp->buffer, bytes); | 
 | 435 | 		SRpnt->sr_use_sg = (STp->buffer)->sg_segs; | 
 | 436 | 		bp = (char *) &((STp->buffer)->sg[0]); | 
 | 437 | 	} else | 
 | 438 | 		bp = (STp->buffer)->b_data; | 
 | 439 | 	SRpnt->sr_data_direction = direction; | 
 | 440 | 	SRpnt->sr_cmd_len = 0; | 
 | 441 | 	SRpnt->sr_request->waiting = &(STp->wait); | 
 | 442 | 	SRpnt->sr_request->rq_status = RQ_SCSI_BUSY; | 
 | 443 | 	SRpnt->sr_request->rq_disk = STp->disk; | 
 | 444 | 	STp->buffer->cmdstat.have_sense = 0; | 
 | 445 |  | 
 | 446 | 	scsi_do_req(SRpnt, (void *) cmd, bp, bytes, | 
 | 447 | 		    st_sleep_done, timeout, retries); | 
 | 448 |  | 
 | 449 | 	if (do_wait) { | 
 | 450 | 		wait_for_completion(SRpnt->sr_request->waiting); | 
 | 451 | 		SRpnt->sr_request->waiting = NULL; | 
 | 452 | 		(STp->buffer)->syscall_result = st_chk_result(STp, SRpnt); | 
 | 453 | 	} | 
 | 454 | 	return SRpnt; | 
 | 455 | } | 
 | 456 |  | 
 | 457 |  | 
 | 458 | /* Handle the write-behind checking (waits for completion). Returns -ENOSPC if | 
 | 459 |    write has been correct but EOM early warning reached, -EIO if write ended in | 
 | 460 |    error or zero if write successful. Asynchronous writes are used only in | 
 | 461 |    variable block mode. */ | 
 | 462 | static int write_behind_check(struct scsi_tape * STp) | 
 | 463 | { | 
 | 464 | 	int retval = 0; | 
 | 465 | 	struct st_buffer *STbuffer; | 
 | 466 | 	struct st_partstat *STps; | 
 | 467 | 	struct st_cmdstatus *cmdstatp; | 
 | 468 |  | 
 | 469 | 	STbuffer = STp->buffer; | 
 | 470 | 	if (!STbuffer->writing) | 
 | 471 | 		return 0; | 
 | 472 |  | 
 | 473 |         DEB( | 
 | 474 | 	if (STp->write_pending) | 
 | 475 | 		STp->nbr_waits++; | 
 | 476 | 	else | 
 | 477 | 		STp->nbr_finished++; | 
 | 478 |         ) /* end DEB */ | 
 | 479 |  | 
 | 480 | 	wait_for_completion(&(STp->wait)); | 
 | 481 | 	(STp->buffer)->last_SRpnt->sr_request->waiting = NULL; | 
 | 482 |  | 
 | 483 | 	(STp->buffer)->syscall_result = st_chk_result(STp, (STp->buffer)->last_SRpnt); | 
 | 484 | 	scsi_release_request((STp->buffer)->last_SRpnt); | 
 | 485 |  | 
 | 486 | 	STbuffer->buffer_bytes -= STbuffer->writing; | 
 | 487 | 	STps = &(STp->ps[STp->partition]); | 
 | 488 | 	if (STps->drv_block >= 0) { | 
 | 489 | 		if (STp->block_size == 0) | 
 | 490 | 			STps->drv_block++; | 
 | 491 | 		else | 
 | 492 | 			STps->drv_block += STbuffer->writing / STp->block_size; | 
 | 493 | 	} | 
 | 494 |  | 
 | 495 | 	cmdstatp = &STbuffer->cmdstat; | 
 | 496 | 	if (STbuffer->syscall_result) { | 
 | 497 | 		retval = -EIO; | 
 | 498 | 		if (cmdstatp->have_sense && !cmdstatp->deferred && | 
 | 499 | 		    (cmdstatp->flags & SENSE_EOM) && | 
 | 500 | 		    (cmdstatp->sense_hdr.sense_key == NO_SENSE || | 
 | 501 | 		     cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR)) { | 
 | 502 | 			/* EOM at write-behind, has all data been written? */ | 
 | 503 | 			if (!cmdstatp->remainder_valid || | 
 | 504 | 			    cmdstatp->uremainder64 == 0) | 
 | 505 | 				retval = -ENOSPC; | 
 | 506 | 		} | 
 | 507 | 		if (retval == -EIO) | 
 | 508 | 			STps->drv_block = -1; | 
 | 509 | 	} | 
 | 510 | 	STbuffer->writing = 0; | 
 | 511 |  | 
 | 512 | 	DEB(if (debugging && retval) | 
 | 513 | 	    printk(ST_DEB_MSG "%s: Async write error %x, return value %d.\n", | 
 | 514 | 		   tape_name(STp), STbuffer->cmdstat.midlevel_result, retval);) /* end DEB */ | 
 | 515 |  | 
 | 516 | 	return retval; | 
 | 517 | } | 
 | 518 |  | 
 | 519 |  | 
 | 520 | /* Step over EOF if it has been inadvertently crossed (ioctl not used because | 
 | 521 |    it messes up the block number). */ | 
 | 522 | static int cross_eof(struct scsi_tape * STp, int forward) | 
 | 523 | { | 
 | 524 | 	struct scsi_request *SRpnt; | 
 | 525 | 	unsigned char cmd[MAX_COMMAND_SIZE]; | 
 | 526 |  | 
 | 527 | 	cmd[0] = SPACE; | 
 | 528 | 	cmd[1] = 0x01;		/* Space FileMarks */ | 
 | 529 | 	if (forward) { | 
 | 530 | 		cmd[2] = cmd[3] = 0; | 
 | 531 | 		cmd[4] = 1; | 
 | 532 | 	} else | 
 | 533 | 		cmd[2] = cmd[3] = cmd[4] = 0xff;	/* -1 filemarks */ | 
 | 534 | 	cmd[5] = 0; | 
 | 535 |  | 
 | 536 |         DEBC(printk(ST_DEB_MSG "%s: Stepping over filemark %s.\n", | 
 | 537 | 		   tape_name(STp), forward ? "forward" : "backward")); | 
 | 538 |  | 
 | 539 | 	SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE, | 
 | 540 | 			   STp->device->timeout, MAX_RETRIES, 1); | 
 | 541 | 	if (!SRpnt) | 
 | 542 | 		return (STp->buffer)->syscall_result; | 
 | 543 |  | 
 | 544 | 	scsi_release_request(SRpnt); | 
 | 545 | 	SRpnt = NULL; | 
 | 546 |  | 
 | 547 | 	if ((STp->buffer)->cmdstat.midlevel_result != 0) | 
 | 548 | 		printk(KERN_ERR "%s: Stepping over filemark %s failed.\n", | 
 | 549 | 		   tape_name(STp), forward ? "forward" : "backward"); | 
 | 550 |  | 
 | 551 | 	return (STp->buffer)->syscall_result; | 
 | 552 | } | 
 | 553 |  | 
 | 554 |  | 
 | 555 | /* Flush the write buffer (never need to write if variable blocksize). */ | 
 | 556 | static int flush_write_buffer(struct scsi_tape * STp) | 
 | 557 | { | 
 | 558 | 	int offset, transfer, blks; | 
 | 559 | 	int result; | 
 | 560 | 	unsigned char cmd[MAX_COMMAND_SIZE]; | 
 | 561 | 	struct scsi_request *SRpnt; | 
 | 562 | 	struct st_partstat *STps; | 
 | 563 |  | 
 | 564 | 	result = write_behind_check(STp); | 
 | 565 | 	if (result) | 
 | 566 | 		return result; | 
 | 567 |  | 
 | 568 | 	result = 0; | 
 | 569 | 	if (STp->dirty == 1) { | 
 | 570 |  | 
 | 571 | 		offset = (STp->buffer)->buffer_bytes; | 
 | 572 | 		transfer = ((offset + STp->block_size - 1) / | 
 | 573 | 			    STp->block_size) * STp->block_size; | 
 | 574 |                 DEBC(printk(ST_DEB_MSG "%s: Flushing %d bytes.\n", | 
 | 575 |                                tape_name(STp), transfer)); | 
 | 576 |  | 
 | 577 | 		memset((STp->buffer)->b_data + offset, 0, transfer - offset); | 
 | 578 |  | 
 | 579 | 		memset(cmd, 0, MAX_COMMAND_SIZE); | 
 | 580 | 		cmd[0] = WRITE_6; | 
 | 581 | 		cmd[1] = 1; | 
 | 582 | 		blks = transfer / STp->block_size; | 
 | 583 | 		cmd[2] = blks >> 16; | 
 | 584 | 		cmd[3] = blks >> 8; | 
 | 585 | 		cmd[4] = blks; | 
 | 586 |  | 
 | 587 | 		SRpnt = st_do_scsi(NULL, STp, cmd, transfer, DMA_TO_DEVICE, | 
 | 588 | 				   STp->device->timeout, MAX_WRITE_RETRIES, 1); | 
 | 589 | 		if (!SRpnt) | 
 | 590 | 			return (STp->buffer)->syscall_result; | 
 | 591 |  | 
 | 592 | 		STps = &(STp->ps[STp->partition]); | 
 | 593 | 		if ((STp->buffer)->syscall_result != 0) { | 
 | 594 | 			struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat; | 
 | 595 |  | 
 | 596 | 			if (cmdstatp->have_sense && !cmdstatp->deferred && | 
 | 597 | 			    (cmdstatp->flags & SENSE_EOM) && | 
 | 598 | 			    (cmdstatp->sense_hdr.sense_key == NO_SENSE || | 
 | 599 | 			     cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) && | 
 | 600 | 			    (!cmdstatp->remainder_valid || | 
 | 601 | 			     cmdstatp->uremainder64 == 0)) { /* All written at EOM early warning */ | 
 | 602 | 				STp->dirty = 0; | 
 | 603 | 				(STp->buffer)->buffer_bytes = 0; | 
 | 604 | 				if (STps->drv_block >= 0) | 
 | 605 | 					STps->drv_block += blks; | 
 | 606 | 				result = (-ENOSPC); | 
 | 607 | 			} else { | 
 | 608 | 				printk(KERN_ERR "%s: Error on flush.\n", | 
 | 609 |                                        tape_name(STp)); | 
 | 610 | 				STps->drv_block = (-1); | 
 | 611 | 				result = (-EIO); | 
 | 612 | 			} | 
 | 613 | 		} else { | 
 | 614 | 			if (STps->drv_block >= 0) | 
 | 615 | 				STps->drv_block += blks; | 
 | 616 | 			STp->dirty = 0; | 
 | 617 | 			(STp->buffer)->buffer_bytes = 0; | 
 | 618 | 		} | 
 | 619 | 		scsi_release_request(SRpnt); | 
 | 620 | 		SRpnt = NULL; | 
 | 621 | 	} | 
 | 622 | 	return result; | 
 | 623 | } | 
 | 624 |  | 
 | 625 |  | 
 | 626 | /* Flush the tape buffer. The tape will be positioned correctly unless | 
 | 627 |    seek_next is true. */ | 
 | 628 | static int flush_buffer(struct scsi_tape *STp, int seek_next) | 
 | 629 | { | 
 | 630 | 	int backspace, result; | 
 | 631 | 	struct st_buffer *STbuffer; | 
 | 632 | 	struct st_partstat *STps; | 
 | 633 |  | 
 | 634 | 	STbuffer = STp->buffer; | 
 | 635 |  | 
 | 636 | 	/* | 
 | 637 | 	 * If there was a bus reset, block further access | 
 | 638 | 	 * to this device. | 
 | 639 | 	 */ | 
 | 640 | 	if (STp->pos_unknown) | 
 | 641 | 		return (-EIO); | 
 | 642 |  | 
 | 643 | 	if (STp->ready != ST_READY) | 
 | 644 | 		return 0; | 
 | 645 | 	STps = &(STp->ps[STp->partition]); | 
 | 646 | 	if (STps->rw == ST_WRITING)	/* Writing */ | 
 | 647 | 		return flush_write_buffer(STp); | 
 | 648 |  | 
 | 649 | 	if (STp->block_size == 0) | 
 | 650 | 		return 0; | 
 | 651 |  | 
 | 652 | 	backspace = ((STp->buffer)->buffer_bytes + | 
 | 653 | 		     (STp->buffer)->read_pointer) / STp->block_size - | 
 | 654 | 	    ((STp->buffer)->read_pointer + STp->block_size - 1) / | 
 | 655 | 	    STp->block_size; | 
 | 656 | 	(STp->buffer)->buffer_bytes = 0; | 
 | 657 | 	(STp->buffer)->read_pointer = 0; | 
 | 658 | 	result = 0; | 
 | 659 | 	if (!seek_next) { | 
 | 660 | 		if (STps->eof == ST_FM_HIT) { | 
 | 661 | 			result = cross_eof(STp, 0);	/* Back over the EOF hit */ | 
 | 662 | 			if (!result) | 
 | 663 | 				STps->eof = ST_NOEOF; | 
 | 664 | 			else { | 
 | 665 | 				if (STps->drv_file >= 0) | 
 | 666 | 					STps->drv_file++; | 
 | 667 | 				STps->drv_block = 0; | 
 | 668 | 			} | 
 | 669 | 		} | 
 | 670 | 		if (!result && backspace > 0) | 
 | 671 | 			result = st_int_ioctl(STp, MTBSR, backspace); | 
 | 672 | 	} else if (STps->eof == ST_FM_HIT) { | 
 | 673 | 		if (STps->drv_file >= 0) | 
 | 674 | 			STps->drv_file++; | 
 | 675 | 		STps->drv_block = 0; | 
 | 676 | 		STps->eof = ST_NOEOF; | 
 | 677 | 	} | 
 | 678 | 	return result; | 
 | 679 |  | 
 | 680 | } | 
 | 681 |  | 
 | 682 | /* Set the mode parameters */ | 
 | 683 | static int set_mode_densblk(struct scsi_tape * STp, struct st_modedef * STm) | 
 | 684 | { | 
 | 685 | 	int set_it = 0; | 
 | 686 | 	unsigned long arg; | 
 | 687 | 	char *name = tape_name(STp); | 
 | 688 |  | 
 | 689 | 	if (!STp->density_changed && | 
 | 690 | 	    STm->default_density >= 0 && | 
 | 691 | 	    STm->default_density != STp->density) { | 
 | 692 | 		arg = STm->default_density; | 
 | 693 | 		set_it = 1; | 
 | 694 | 	} else | 
 | 695 | 		arg = STp->density; | 
 | 696 | 	arg <<= MT_ST_DENSITY_SHIFT; | 
 | 697 | 	if (!STp->blksize_changed && | 
 | 698 | 	    STm->default_blksize >= 0 && | 
 | 699 | 	    STm->default_blksize != STp->block_size) { | 
 | 700 | 		arg |= STm->default_blksize; | 
 | 701 | 		set_it = 1; | 
 | 702 | 	} else | 
 | 703 | 		arg |= STp->block_size; | 
 | 704 | 	if (set_it && | 
 | 705 | 	    st_int_ioctl(STp, SET_DENS_AND_BLK, arg)) { | 
 | 706 | 		printk(KERN_WARNING | 
 | 707 | 		       "%s: Can't set default block size to %d bytes and density %x.\n", | 
 | 708 | 		       name, STm->default_blksize, STm->default_density); | 
 | 709 | 		if (modes_defined) | 
 | 710 | 			return (-EINVAL); | 
 | 711 | 	} | 
 | 712 | 	return 0; | 
 | 713 | } | 
 | 714 |  | 
 | 715 |  | 
 | 716 | /* Lock or unlock the drive door. Don't use when scsi_request allocated. */ | 
 | 717 | static int do_door_lock(struct scsi_tape * STp, int do_lock) | 
 | 718 | { | 
 | 719 | 	int retval, cmd; | 
 | 720 | 	DEB(char *name = tape_name(STp);) | 
 | 721 |  | 
 | 722 |  | 
 | 723 | 	cmd = do_lock ? SCSI_IOCTL_DOORLOCK : SCSI_IOCTL_DOORUNLOCK; | 
 | 724 | 	DEBC(printk(ST_DEB_MSG "%s: %socking drive door.\n", name, | 
 | 725 | 		    do_lock ? "L" : "Unl")); | 
 | 726 | 	retval = scsi_ioctl(STp->device, cmd, NULL); | 
 | 727 | 	if (!retval) { | 
 | 728 | 		STp->door_locked = do_lock ? ST_LOCKED_EXPLICIT : ST_UNLOCKED; | 
 | 729 | 	} | 
 | 730 | 	else { | 
 | 731 | 		STp->door_locked = ST_LOCK_FAILS; | 
 | 732 | 	} | 
 | 733 | 	return retval; | 
 | 734 | } | 
 | 735 |  | 
 | 736 |  | 
 | 737 | /* Set the internal state after reset */ | 
 | 738 | static void reset_state(struct scsi_tape *STp) | 
 | 739 | { | 
 | 740 | 	int i; | 
 | 741 | 	struct st_partstat *STps; | 
 | 742 |  | 
 | 743 | 	STp->pos_unknown = 0; | 
 | 744 | 	for (i = 0; i < ST_NBR_PARTITIONS; i++) { | 
 | 745 | 		STps = &(STp->ps[i]); | 
 | 746 | 		STps->rw = ST_IDLE; | 
 | 747 | 		STps->eof = ST_NOEOF; | 
 | 748 | 		STps->at_sm = 0; | 
 | 749 | 		STps->last_block_valid = 0; | 
 | 750 | 		STps->drv_block = -1; | 
 | 751 | 		STps->drv_file = -1; | 
 | 752 | 	} | 
 | 753 | 	if (STp->can_partitions) { | 
 | 754 | 		STp->partition = find_partition(STp); | 
 | 755 | 		if (STp->partition < 0) | 
 | 756 | 			STp->partition = 0; | 
 | 757 | 		STp->new_partition = STp->partition; | 
 | 758 | 	} | 
 | 759 | } | 
 | 760 |  | 
 | 761 | /* Test if the drive is ready. Returns either one of the codes below or a negative system | 
 | 762 |    error code. */ | 
 | 763 | #define CHKRES_READY       0 | 
 | 764 | #define CHKRES_NEW_SESSION 1 | 
 | 765 | #define CHKRES_NOT_READY   2 | 
 | 766 | #define CHKRES_NO_TAPE     3 | 
 | 767 |  | 
 | 768 | #define MAX_ATTENTIONS    10 | 
 | 769 |  | 
 | 770 | static int test_ready(struct scsi_tape *STp, int do_wait) | 
 | 771 | { | 
 | 772 | 	int attentions, waits, max_wait, scode; | 
 | 773 | 	int retval = CHKRES_READY, new_session = 0; | 
 | 774 | 	unsigned char cmd[MAX_COMMAND_SIZE]; | 
 | 775 | 	struct scsi_request *SRpnt = NULL; | 
 | 776 | 	struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat; | 
 | 777 |  | 
 | 778 | 	max_wait = do_wait ? ST_BLOCK_SECONDS : 0; | 
 | 779 |  | 
 | 780 | 	for (attentions=waits=0; ; ) { | 
 | 781 | 		memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE); | 
 | 782 | 		cmd[0] = TEST_UNIT_READY; | 
 | 783 | 		SRpnt = st_do_scsi(SRpnt, STp, cmd, 0, DMA_NONE, | 
 | 784 | 				   STp->long_timeout, MAX_READY_RETRIES, 1); | 
 | 785 |  | 
 | 786 | 		if (!SRpnt) { | 
 | 787 | 			retval = (STp->buffer)->syscall_result; | 
 | 788 | 			break; | 
 | 789 | 		} | 
 | 790 |  | 
 | 791 | 		if (cmdstatp->have_sense) { | 
 | 792 |  | 
 | 793 | 			scode = cmdstatp->sense_hdr.sense_key; | 
 | 794 |  | 
 | 795 | 			if (scode == UNIT_ATTENTION) { /* New media? */ | 
 | 796 | 				new_session = 1; | 
 | 797 | 				if (attentions < MAX_ATTENTIONS) { | 
 | 798 | 					attentions++; | 
 | 799 | 					continue; | 
 | 800 | 				} | 
 | 801 | 				else { | 
 | 802 | 					retval = (-EIO); | 
 | 803 | 					break; | 
 | 804 | 				} | 
 | 805 | 			} | 
 | 806 |  | 
 | 807 | 			if (scode == NOT_READY) { | 
 | 808 | 				if (waits < max_wait) { | 
 | 809 | 					if (msleep_interruptible(1000)) { | 
 | 810 | 						retval = (-EINTR); | 
 | 811 | 						break; | 
 | 812 | 					} | 
 | 813 | 					waits++; | 
 | 814 | 					continue; | 
 | 815 | 				} | 
 | 816 | 				else { | 
 | 817 | 					if ((STp->device)->scsi_level >= SCSI_2 && | 
 | 818 | 					    cmdstatp->sense_hdr.asc == 0x3a)	/* Check ASC */ | 
 | 819 | 						retval = CHKRES_NO_TAPE; | 
 | 820 | 					else | 
 | 821 | 						retval = CHKRES_NOT_READY; | 
 | 822 | 					break; | 
 | 823 | 				} | 
 | 824 | 			} | 
 | 825 | 		} | 
 | 826 |  | 
 | 827 | 		retval = (STp->buffer)->syscall_result; | 
 | 828 | 		if (!retval) | 
 | 829 | 			retval = new_session ? CHKRES_NEW_SESSION : CHKRES_READY; | 
 | 830 | 		break; | 
 | 831 | 	} | 
 | 832 |  | 
 | 833 | 	if (SRpnt != NULL) | 
 | 834 | 		scsi_release_request(SRpnt); | 
 | 835 | 	return retval; | 
 | 836 | } | 
 | 837 |  | 
 | 838 |  | 
 | 839 | /* See if the drive is ready and gather information about the tape. Return values: | 
 | 840 |    < 0   negative error code from errno.h | 
 | 841 |    0     drive ready | 
 | 842 |    1     drive not ready (possibly no tape) | 
 | 843 | */ | 
 | 844 | static int check_tape(struct scsi_tape *STp, struct file *filp) | 
 | 845 | { | 
 | 846 | 	int i, retval, new_session = 0, do_wait; | 
 | 847 | 	unsigned char cmd[MAX_COMMAND_SIZE], saved_cleaning; | 
 | 848 | 	unsigned short st_flags = filp->f_flags; | 
 | 849 | 	struct scsi_request *SRpnt = NULL; | 
 | 850 | 	struct st_modedef *STm; | 
 | 851 | 	struct st_partstat *STps; | 
 | 852 | 	char *name = tape_name(STp); | 
 | 853 | 	struct inode *inode = filp->f_dentry->d_inode; | 
 | 854 | 	int mode = TAPE_MODE(inode); | 
 | 855 |  | 
 | 856 | 	STp->ready = ST_READY; | 
 | 857 |  | 
 | 858 | 	if (mode != STp->current_mode) { | 
 | 859 |                 DEBC(printk(ST_DEB_MSG "%s: Mode change from %d to %d.\n", | 
 | 860 | 			       name, STp->current_mode, mode)); | 
 | 861 | 		new_session = 1; | 
 | 862 | 		STp->current_mode = mode; | 
 | 863 | 	} | 
 | 864 | 	STm = &(STp->modes[STp->current_mode]); | 
 | 865 |  | 
 | 866 | 	saved_cleaning = STp->cleaning_req; | 
 | 867 | 	STp->cleaning_req = 0; | 
 | 868 |  | 
 | 869 | 	do_wait = ((filp->f_flags & O_NONBLOCK) == 0); | 
 | 870 | 	retval = test_ready(STp, do_wait); | 
 | 871 |  | 
 | 872 | 	if (retval < 0) | 
 | 873 | 	    goto err_out; | 
 | 874 |  | 
 | 875 | 	if (retval == CHKRES_NEW_SESSION) { | 
 | 876 | 		STp->pos_unknown = 0; | 
 | 877 | 		STp->partition = STp->new_partition = 0; | 
 | 878 | 		if (STp->can_partitions) | 
 | 879 | 			STp->nbr_partitions = 1; /* This guess will be updated later | 
 | 880 |                                                     if necessary */ | 
 | 881 | 		for (i = 0; i < ST_NBR_PARTITIONS; i++) { | 
 | 882 | 			STps = &(STp->ps[i]); | 
 | 883 | 			STps->rw = ST_IDLE; | 
 | 884 | 			STps->eof = ST_NOEOF; | 
 | 885 | 			STps->at_sm = 0; | 
 | 886 | 			STps->last_block_valid = 0; | 
 | 887 | 			STps->drv_block = 0; | 
 | 888 | 			STps->drv_file = 0; | 
 | 889 | 		} | 
 | 890 | 		new_session = 1; | 
 | 891 | 	} | 
 | 892 | 	else { | 
 | 893 | 		STp->cleaning_req |= saved_cleaning; | 
 | 894 |  | 
 | 895 | 		if (retval == CHKRES_NOT_READY || retval == CHKRES_NO_TAPE) { | 
 | 896 | 			if (retval == CHKRES_NO_TAPE) | 
 | 897 | 				STp->ready = ST_NO_TAPE; | 
 | 898 | 			else | 
 | 899 | 				STp->ready = ST_NOT_READY; | 
 | 900 |  | 
 | 901 | 			STp->density = 0;	/* Clear the erroneous "residue" */ | 
 | 902 | 			STp->write_prot = 0; | 
 | 903 | 			STp->block_size = 0; | 
 | 904 | 			STp->ps[0].drv_file = STp->ps[0].drv_block = (-1); | 
 | 905 | 			STp->partition = STp->new_partition = 0; | 
 | 906 | 			STp->door_locked = ST_UNLOCKED; | 
 | 907 | 			return CHKRES_NOT_READY; | 
 | 908 | 		} | 
 | 909 | 	} | 
 | 910 |  | 
 | 911 | 	if (STp->omit_blklims) | 
 | 912 | 		STp->min_block = STp->max_block = (-1); | 
 | 913 | 	else { | 
 | 914 | 		memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE); | 
 | 915 | 		cmd[0] = READ_BLOCK_LIMITS; | 
 | 916 |  | 
 | 917 | 		SRpnt = st_do_scsi(SRpnt, STp, cmd, 6, DMA_FROM_DEVICE, | 
 | 918 | 				   STp->device->timeout, MAX_READY_RETRIES, 1); | 
 | 919 | 		if (!SRpnt) { | 
 | 920 | 			retval = (STp->buffer)->syscall_result; | 
 | 921 | 			goto err_out; | 
 | 922 | 		} | 
 | 923 |  | 
 | 924 | 		if (!SRpnt->sr_result && !STp->buffer->cmdstat.have_sense) { | 
 | 925 | 			STp->max_block = ((STp->buffer)->b_data[1] << 16) | | 
 | 926 | 			    ((STp->buffer)->b_data[2] << 8) | (STp->buffer)->b_data[3]; | 
 | 927 | 			STp->min_block = ((STp->buffer)->b_data[4] << 8) | | 
 | 928 | 			    (STp->buffer)->b_data[5]; | 
 | 929 | 			if ( DEB( debugging || ) !STp->inited) | 
 | 930 | 				printk(KERN_WARNING | 
 | 931 |                                        "%s: Block limits %d - %d bytes.\n", name, | 
 | 932 |                                        STp->min_block, STp->max_block); | 
 | 933 | 		} else { | 
 | 934 | 			STp->min_block = STp->max_block = (-1); | 
 | 935 |                         DEBC(printk(ST_DEB_MSG "%s: Can't read block limits.\n", | 
 | 936 |                                        name)); | 
 | 937 | 		} | 
 | 938 | 	} | 
 | 939 |  | 
 | 940 | 	memset((void *) &cmd[0], 0, MAX_COMMAND_SIZE); | 
 | 941 | 	cmd[0] = MODE_SENSE; | 
 | 942 | 	cmd[4] = 12; | 
 | 943 |  | 
 | 944 | 	SRpnt = st_do_scsi(SRpnt, STp, cmd, 12, DMA_FROM_DEVICE, | 
 | 945 | 			STp->device->timeout, MAX_READY_RETRIES, 1); | 
 | 946 | 	if (!SRpnt) { | 
 | 947 | 		retval = (STp->buffer)->syscall_result; | 
 | 948 | 		goto err_out; | 
 | 949 | 	} | 
 | 950 |  | 
 | 951 | 	if ((STp->buffer)->syscall_result != 0) { | 
 | 952 |                 DEBC(printk(ST_DEB_MSG "%s: No Mode Sense.\n", name)); | 
 | 953 | 		STp->block_size = ST_DEFAULT_BLOCK;	/* Educated guess (?) */ | 
 | 954 | 		(STp->buffer)->syscall_result = 0;	/* Prevent error propagation */ | 
 | 955 | 		STp->drv_write_prot = 0; | 
 | 956 | 	} else { | 
 | 957 |                 DEBC(printk(ST_DEB_MSG | 
 | 958 |                             "%s: Mode sense. Length %d, medium %x, WBS %x, BLL %d\n", | 
 | 959 |                             name, | 
 | 960 |                             (STp->buffer)->b_data[0], (STp->buffer)->b_data[1], | 
 | 961 |                             (STp->buffer)->b_data[2], (STp->buffer)->b_data[3])); | 
 | 962 |  | 
 | 963 | 		if ((STp->buffer)->b_data[3] >= 8) { | 
 | 964 | 			STp->drv_buffer = ((STp->buffer)->b_data[2] >> 4) & 7; | 
 | 965 | 			STp->density = (STp->buffer)->b_data[4]; | 
 | 966 | 			STp->block_size = (STp->buffer)->b_data[9] * 65536 + | 
 | 967 | 			    (STp->buffer)->b_data[10] * 256 + (STp->buffer)->b_data[11]; | 
 | 968 |                         DEBC(printk(ST_DEB_MSG | 
 | 969 |                                     "%s: Density %x, tape length: %x, drv buffer: %d\n", | 
 | 970 |                                     name, STp->density, (STp->buffer)->b_data[5] * 65536 + | 
 | 971 |                                     (STp->buffer)->b_data[6] * 256 + (STp->buffer)->b_data[7], | 
 | 972 |                                     STp->drv_buffer)); | 
 | 973 | 		} | 
 | 974 | 		STp->drv_write_prot = ((STp->buffer)->b_data[2] & 0x80) != 0; | 
 | 975 | 	} | 
 | 976 | 	scsi_release_request(SRpnt); | 
 | 977 | 	SRpnt = NULL; | 
 | 978 |         STp->inited = 1; | 
 | 979 |  | 
 | 980 | 	if (STp->block_size > 0) | 
 | 981 | 		(STp->buffer)->buffer_blocks = | 
 | 982 |                         (STp->buffer)->buffer_size / STp->block_size; | 
 | 983 | 	else | 
 | 984 | 		(STp->buffer)->buffer_blocks = 1; | 
 | 985 | 	(STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0; | 
 | 986 |  | 
 | 987 |         DEBC(printk(ST_DEB_MSG | 
 | 988 |                        "%s: Block size: %d, buffer size: %d (%d blocks).\n", name, | 
 | 989 | 		       STp->block_size, (STp->buffer)->buffer_size, | 
 | 990 | 		       (STp->buffer)->buffer_blocks)); | 
 | 991 |  | 
 | 992 | 	if (STp->drv_write_prot) { | 
 | 993 | 		STp->write_prot = 1; | 
 | 994 |  | 
 | 995 |                 DEBC(printk(ST_DEB_MSG "%s: Write protected\n", name)); | 
 | 996 |  | 
 | 997 | 		if (do_wait && | 
 | 998 | 		    ((st_flags & O_ACCMODE) == O_WRONLY || | 
 | 999 | 		     (st_flags & O_ACCMODE) == O_RDWR)) { | 
 | 1000 | 			retval = (-EROFS); | 
 | 1001 | 			goto err_out; | 
 | 1002 | 		} | 
 | 1003 | 	} | 
 | 1004 |  | 
 | 1005 | 	if (STp->can_partitions && STp->nbr_partitions < 1) { | 
 | 1006 | 		/* This code is reached when the device is opened for the first time | 
 | 1007 | 		   after the driver has been initialized with tape in the drive and the | 
 | 1008 | 		   partition support has been enabled. */ | 
 | 1009 |                 DEBC(printk(ST_DEB_MSG | 
 | 1010 |                             "%s: Updating partition number in status.\n", name)); | 
 | 1011 | 		if ((STp->partition = find_partition(STp)) < 0) { | 
 | 1012 | 			retval = STp->partition; | 
 | 1013 | 			goto err_out; | 
 | 1014 | 		} | 
 | 1015 | 		STp->new_partition = STp->partition; | 
 | 1016 | 		STp->nbr_partitions = 1; /* This guess will be updated when necessary */ | 
 | 1017 | 	} | 
 | 1018 |  | 
 | 1019 | 	if (new_session) {	/* Change the drive parameters for the new mode */ | 
 | 1020 | 		STp->density_changed = STp->blksize_changed = 0; | 
 | 1021 | 		STp->compression_changed = 0; | 
 | 1022 | 		if (!(STm->defaults_for_writes) && | 
 | 1023 | 		    (retval = set_mode_densblk(STp, STm)) < 0) | 
 | 1024 | 		    goto err_out; | 
 | 1025 |  | 
 | 1026 | 		if (STp->default_drvbuffer != 0xff) { | 
 | 1027 | 			if (st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer)) | 
 | 1028 | 				printk(KERN_WARNING | 
 | 1029 |                                        "%s: Can't set default drive buffering to %d.\n", | 
 | 1030 | 				       name, STp->default_drvbuffer); | 
 | 1031 | 		} | 
 | 1032 | 	} | 
 | 1033 |  | 
 | 1034 | 	return CHKRES_READY; | 
 | 1035 |  | 
 | 1036 |  err_out: | 
 | 1037 | 	return retval; | 
 | 1038 | } | 
 | 1039 |  | 
 | 1040 |  | 
 | 1041 | /* Open the device. Needs to be called with BKL only because of incrementing the SCSI host | 
 | 1042 |    module count. */ | 
 | 1043 | static int st_open(struct inode *inode, struct file *filp) | 
 | 1044 | { | 
 | 1045 | 	int i, retval = (-EIO); | 
 | 1046 | 	struct scsi_tape *STp; | 
 | 1047 | 	struct st_partstat *STps; | 
 | 1048 | 	int dev = TAPE_NR(inode); | 
 | 1049 | 	char *name; | 
 | 1050 |  | 
 | 1051 | 	/* | 
 | 1052 | 	 * We really want to do nonseekable_open(inode, filp); here, but some | 
 | 1053 | 	 * versions of tar incorrectly call lseek on tapes and bail out if that | 
 | 1054 | 	 * fails.  So we disallow pread() and pwrite(), but permit lseeks. | 
 | 1055 | 	 */ | 
 | 1056 | 	filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE); | 
 | 1057 |  | 
 | 1058 | 	write_lock(&st_dev_arr_lock); | 
 | 1059 | 	if (dev >= st_dev_max || scsi_tapes == NULL || | 
 | 1060 | 	    ((STp = scsi_tapes[dev]) == NULL)) { | 
 | 1061 | 		write_unlock(&st_dev_arr_lock); | 
 | 1062 | 		return (-ENXIO); | 
 | 1063 | 	} | 
 | 1064 | 	filp->private_data = STp; | 
 | 1065 | 	name = tape_name(STp); | 
 | 1066 |  | 
 | 1067 | 	if (STp->in_use) { | 
 | 1068 | 		write_unlock(&st_dev_arr_lock); | 
 | 1069 | 		DEB( printk(ST_DEB_MSG "%s: Device already in use.\n", name); ) | 
 | 1070 | 		return (-EBUSY); | 
 | 1071 | 	} | 
 | 1072 |  | 
 | 1073 | 	if(scsi_device_get(STp->device)) { | 
 | 1074 | 		write_unlock(&st_dev_arr_lock); | 
 | 1075 | 		return (-ENXIO); | 
 | 1076 | 	} | 
 | 1077 | 	STp->in_use = 1; | 
 | 1078 | 	write_unlock(&st_dev_arr_lock); | 
 | 1079 | 	STp->rew_at_close = STp->autorew_dev = (iminor(inode) & 0x80) == 0; | 
 | 1080 |  | 
 | 1081 | 	if (!scsi_block_when_processing_errors(STp->device)) { | 
 | 1082 | 		retval = (-ENXIO); | 
 | 1083 | 		goto err_out; | 
 | 1084 | 	} | 
 | 1085 |  | 
 | 1086 | 	/* See that we have at least a one page buffer available */ | 
 | 1087 | 	if (!enlarge_buffer(STp->buffer, PAGE_SIZE, STp->restr_dma)) { | 
 | 1088 | 		printk(KERN_WARNING "%s: Can't allocate one page tape buffer.\n", | 
 | 1089 | 		       name); | 
 | 1090 | 		retval = (-EOVERFLOW); | 
 | 1091 | 		goto err_out; | 
 | 1092 | 	} | 
 | 1093 |  | 
 | 1094 | 	(STp->buffer)->writing = 0; | 
 | 1095 | 	(STp->buffer)->syscall_result = 0; | 
 | 1096 |  | 
 | 1097 | 	STp->write_prot = ((filp->f_flags & O_ACCMODE) == O_RDONLY); | 
 | 1098 |  | 
 | 1099 | 	STp->dirty = 0; | 
 | 1100 | 	for (i = 0; i < ST_NBR_PARTITIONS; i++) { | 
 | 1101 | 		STps = &(STp->ps[i]); | 
 | 1102 | 		STps->rw = ST_IDLE; | 
 | 1103 | 	} | 
 | 1104 | 	STp->recover_count = 0; | 
 | 1105 | 	DEB( STp->nbr_waits = STp->nbr_finished = 0; | 
 | 1106 | 	     STp->nbr_requests = STp->nbr_dio = STp->nbr_pages = STp->nbr_combinable = 0; ) | 
 | 1107 |  | 
 | 1108 | 	retval = check_tape(STp, filp); | 
 | 1109 | 	if (retval < 0) | 
 | 1110 | 		goto err_out; | 
 | 1111 | 	if ((filp->f_flags & O_NONBLOCK) == 0 && | 
 | 1112 | 	    retval != CHKRES_READY) { | 
 | 1113 | 		retval = (-EIO); | 
 | 1114 | 		goto err_out; | 
 | 1115 | 	} | 
 | 1116 | 	return 0; | 
 | 1117 |  | 
 | 1118 |  err_out: | 
 | 1119 | 	normalize_buffer(STp->buffer); | 
 | 1120 | 	STp->in_use = 0; | 
 | 1121 | 	scsi_device_put(STp->device); | 
 | 1122 | 	return retval; | 
 | 1123 |  | 
 | 1124 | } | 
 | 1125 |  | 
 | 1126 |  | 
 | 1127 | /* Flush the tape buffer before close */ | 
 | 1128 | static int st_flush(struct file *filp) | 
 | 1129 | { | 
 | 1130 | 	int result = 0, result2; | 
 | 1131 | 	unsigned char cmd[MAX_COMMAND_SIZE]; | 
 | 1132 | 	struct scsi_request *SRpnt; | 
 | 1133 | 	struct scsi_tape *STp = filp->private_data; | 
 | 1134 | 	struct st_modedef *STm = &(STp->modes[STp->current_mode]); | 
 | 1135 | 	struct st_partstat *STps = &(STp->ps[STp->partition]); | 
 | 1136 | 	char *name = tape_name(STp); | 
 | 1137 |  | 
 | 1138 | 	if (file_count(filp) > 1) | 
 | 1139 | 		return 0; | 
 | 1140 |  | 
 | 1141 | 	if (STps->rw == ST_WRITING && !STp->pos_unknown) { | 
 | 1142 | 		result = flush_write_buffer(STp); | 
 | 1143 | 		if (result != 0 && result != (-ENOSPC)) | 
 | 1144 | 			goto out; | 
 | 1145 | 	} | 
 | 1146 |  | 
 | 1147 | 	if (STp->can_partitions && | 
 | 1148 | 	    (result2 = switch_partition(STp)) < 0) { | 
 | 1149 |                 DEBC(printk(ST_DEB_MSG | 
 | 1150 |                                "%s: switch_partition at close failed.\n", name)); | 
 | 1151 | 		if (result == 0) | 
 | 1152 | 			result = result2; | 
 | 1153 | 		goto out; | 
 | 1154 | 	} | 
 | 1155 |  | 
 | 1156 | 	DEBC( if (STp->nbr_requests) | 
 | 1157 | 		printk(KERN_WARNING "%s: Number of r/w requests %d, dio used in %d, pages %d (%d).\n", | 
 | 1158 | 		       name, STp->nbr_requests, STp->nbr_dio, STp->nbr_pages, STp->nbr_combinable)); | 
 | 1159 |  | 
 | 1160 | 	if (STps->rw == ST_WRITING && !STp->pos_unknown) { | 
 | 1161 | 		struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat; | 
 | 1162 |  | 
 | 1163 |                 DEBC(printk(ST_DEB_MSG "%s: Async write waits %d, finished %d.\n", | 
 | 1164 |                             name, STp->nbr_waits, STp->nbr_finished); | 
 | 1165 | 		) | 
 | 1166 |  | 
 | 1167 | 		memset(cmd, 0, MAX_COMMAND_SIZE); | 
 | 1168 | 		cmd[0] = WRITE_FILEMARKS; | 
 | 1169 | 		cmd[4] = 1 + STp->two_fm; | 
 | 1170 |  | 
 | 1171 | 		SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE, | 
 | 1172 | 				   STp->device->timeout, MAX_WRITE_RETRIES, 1); | 
 | 1173 | 		if (!SRpnt) { | 
 | 1174 | 			result = (STp->buffer)->syscall_result; | 
 | 1175 | 			goto out; | 
 | 1176 | 		} | 
 | 1177 |  | 
 | 1178 | 		if (STp->buffer->syscall_result == 0 || | 
 | 1179 | 		    (cmdstatp->have_sense && !cmdstatp->deferred && | 
 | 1180 | 		     (cmdstatp->flags & SENSE_EOM) && | 
 | 1181 | 		     (cmdstatp->sense_hdr.sense_key == NO_SENSE || | 
 | 1182 | 		      cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) && | 
 | 1183 | 		     (!cmdstatp->remainder_valid || cmdstatp->uremainder64 == 0))) { | 
 | 1184 | 			/* Write successful at EOM */ | 
 | 1185 | 			scsi_release_request(SRpnt); | 
 | 1186 | 			SRpnt = NULL; | 
 | 1187 | 			if (STps->drv_file >= 0) | 
 | 1188 | 				STps->drv_file++; | 
 | 1189 | 			STps->drv_block = 0; | 
 | 1190 | 			if (STp->two_fm) | 
 | 1191 | 				cross_eof(STp, 0); | 
 | 1192 | 			STps->eof = ST_FM; | 
 | 1193 | 		} | 
 | 1194 | 		else { /* Write error */ | 
 | 1195 | 			scsi_release_request(SRpnt); | 
 | 1196 | 			SRpnt = NULL; | 
 | 1197 | 			printk(KERN_ERR "%s: Error on write filemark.\n", name); | 
 | 1198 | 			if (result == 0) | 
 | 1199 | 				result = (-EIO); | 
 | 1200 | 		} | 
 | 1201 |  | 
 | 1202 |                 DEBC(printk(ST_DEB_MSG "%s: Buffer flushed, %d EOF(s) written\n", | 
 | 1203 |                             name, cmd[4])); | 
 | 1204 | 	} else if (!STp->rew_at_close) { | 
 | 1205 | 		STps = &(STp->ps[STp->partition]); | 
 | 1206 | 		if (!STm->sysv || STps->rw != ST_READING) { | 
 | 1207 | 			if (STp->can_bsr) | 
 | 1208 | 				result = flush_buffer(STp, 0); | 
 | 1209 | 			else if (STps->eof == ST_FM_HIT) { | 
 | 1210 | 				result = cross_eof(STp, 0); | 
 | 1211 | 				if (result) { | 
 | 1212 | 					if (STps->drv_file >= 0) | 
 | 1213 | 						STps->drv_file++; | 
 | 1214 | 					STps->drv_block = 0; | 
 | 1215 | 					STps->eof = ST_FM; | 
 | 1216 | 				} else | 
 | 1217 | 					STps->eof = ST_NOEOF; | 
 | 1218 | 			} | 
 | 1219 | 		} else if ((STps->eof == ST_NOEOF && | 
 | 1220 | 			    !(result = cross_eof(STp, 1))) || | 
 | 1221 | 			   STps->eof == ST_FM_HIT) { | 
 | 1222 | 			if (STps->drv_file >= 0) | 
 | 1223 | 				STps->drv_file++; | 
 | 1224 | 			STps->drv_block = 0; | 
 | 1225 | 			STps->eof = ST_FM; | 
 | 1226 | 		} | 
 | 1227 | 	} | 
 | 1228 |  | 
 | 1229 |       out: | 
 | 1230 | 	if (STp->rew_at_close) { | 
 | 1231 | 		result2 = st_int_ioctl(STp, MTREW, 1); | 
 | 1232 | 		if (result == 0) | 
 | 1233 | 			result = result2; | 
 | 1234 | 	} | 
 | 1235 | 	return result; | 
 | 1236 | } | 
 | 1237 |  | 
 | 1238 |  | 
 | 1239 | /* Close the device and release it. BKL is not needed: this is the only thread | 
 | 1240 |    accessing this tape. */ | 
 | 1241 | static int st_release(struct inode *inode, struct file *filp) | 
 | 1242 | { | 
 | 1243 | 	int result = 0; | 
 | 1244 | 	struct scsi_tape *STp = filp->private_data; | 
 | 1245 |  | 
 | 1246 | 	if (STp->door_locked == ST_LOCKED_AUTO) | 
 | 1247 | 		do_door_lock(STp, 0); | 
 | 1248 |  | 
 | 1249 | 	normalize_buffer(STp->buffer); | 
 | 1250 | 	write_lock(&st_dev_arr_lock); | 
 | 1251 | 	STp->in_use = 0; | 
 | 1252 | 	write_unlock(&st_dev_arr_lock); | 
 | 1253 | 	scsi_device_put(STp->device); | 
 | 1254 |  | 
 | 1255 | 	return result; | 
 | 1256 | } | 
 | 1257 |  | 
 | 1258 | /* The checks common to both reading and writing */ | 
 | 1259 | static ssize_t rw_checks(struct scsi_tape *STp, struct file *filp, size_t count) | 
 | 1260 | { | 
 | 1261 | 	ssize_t retval = 0; | 
 | 1262 |  | 
 | 1263 | 	/* | 
 | 1264 | 	 * If we are in the middle of error recovery, don't let anyone | 
 | 1265 | 	 * else try and use this device.  Also, if error recovery fails, it | 
 | 1266 | 	 * may try and take the device offline, in which case all further | 
 | 1267 | 	 * access to the device is prohibited. | 
 | 1268 | 	 */ | 
 | 1269 | 	if (!scsi_block_when_processing_errors(STp->device)) { | 
 | 1270 | 		retval = (-ENXIO); | 
 | 1271 | 		goto out; | 
 | 1272 | 	} | 
 | 1273 |  | 
 | 1274 | 	if (STp->ready != ST_READY) { | 
 | 1275 | 		if (STp->ready == ST_NO_TAPE) | 
 | 1276 | 			retval = (-ENOMEDIUM); | 
 | 1277 | 		else | 
 | 1278 | 			retval = (-EIO); | 
 | 1279 | 		goto out; | 
 | 1280 | 	} | 
 | 1281 |  | 
 | 1282 | 	if (! STp->modes[STp->current_mode].defined) { | 
 | 1283 | 		retval = (-ENXIO); | 
 | 1284 | 		goto out; | 
 | 1285 | 	} | 
 | 1286 |  | 
 | 1287 |  | 
 | 1288 | 	/* | 
 | 1289 | 	 * If there was a bus reset, block further access | 
 | 1290 | 	 * to this device. | 
 | 1291 | 	 */ | 
 | 1292 | 	if (STp->pos_unknown) { | 
 | 1293 | 		retval = (-EIO); | 
 | 1294 | 		goto out; | 
 | 1295 | 	} | 
 | 1296 |  | 
 | 1297 | 	if (count == 0) | 
 | 1298 | 		goto out; | 
 | 1299 |  | 
 | 1300 |         DEB( | 
 | 1301 | 	if (!STp->in_use) { | 
 | 1302 | 		printk(ST_DEB_MSG "%s: Incorrect device.\n", tape_name(STp)); | 
 | 1303 | 		retval = (-EIO); | 
 | 1304 | 		goto out; | 
 | 1305 | 	} ) /* end DEB */ | 
 | 1306 |  | 
 | 1307 | 	if (STp->can_partitions && | 
 | 1308 | 	    (retval = switch_partition(STp)) < 0) | 
 | 1309 | 		goto out; | 
 | 1310 |  | 
 | 1311 | 	if (STp->block_size == 0 && STp->max_block > 0 && | 
 | 1312 | 	    (count < STp->min_block || count > STp->max_block)) { | 
 | 1313 | 		retval = (-EINVAL); | 
 | 1314 | 		goto out; | 
 | 1315 | 	} | 
 | 1316 |  | 
 | 1317 | 	if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED && | 
 | 1318 | 	    !do_door_lock(STp, 1)) | 
 | 1319 | 		STp->door_locked = ST_LOCKED_AUTO; | 
 | 1320 |  | 
 | 1321 |  out: | 
 | 1322 | 	return retval; | 
 | 1323 | } | 
 | 1324 |  | 
 | 1325 |  | 
 | 1326 | static int setup_buffering(struct scsi_tape *STp, const char __user *buf, | 
 | 1327 | 			   size_t count, int is_read) | 
 | 1328 | { | 
 | 1329 | 	int i, bufsize, retval = 0; | 
 | 1330 | 	struct st_buffer *STbp = STp->buffer; | 
 | 1331 |  | 
 | 1332 | 	if (is_read) | 
 | 1333 | 		i = STp->try_dio && try_rdio; | 
 | 1334 | 	else | 
 | 1335 | 		i = STp->try_dio && try_wdio; | 
 | 1336 | 	if (i && ((unsigned long)buf & queue_dma_alignment( | 
 | 1337 | 					STp->device->request_queue)) == 0) { | 
 | 1338 | 		i = st_map_user_pages(&(STbp->sg[0]), STbp->use_sg, | 
 | 1339 | 				      (unsigned long)buf, count, (is_read ? READ : WRITE), | 
 | 1340 | 				      STp->max_pfn); | 
 | 1341 | 		if (i > 0) { | 
 | 1342 | 			STbp->do_dio = i; | 
 | 1343 | 			STbp->buffer_bytes = 0;   /* can be used as transfer counter */ | 
 | 1344 | 		} | 
 | 1345 | 		else | 
 | 1346 | 			STbp->do_dio = 0;  /* fall back to buffering with any error */ | 
 | 1347 | 		STbp->sg_segs = STbp->do_dio; | 
 | 1348 | 		STbp->frp_sg_current = 0; | 
 | 1349 | 		DEB( | 
 | 1350 | 		     if (STbp->do_dio) { | 
 | 1351 | 			STp->nbr_dio++; | 
 | 1352 | 			STp->nbr_pages += STbp->do_dio; | 
 | 1353 | 			for (i=1; i < STbp->do_dio; i++) | 
 | 1354 | 				if (page_to_pfn(STbp->sg[i].page) == page_to_pfn(STbp->sg[i-1].page) + 1) | 
 | 1355 | 					STp->nbr_combinable++; | 
 | 1356 | 		     } | 
 | 1357 | 		) | 
 | 1358 | 	} else | 
 | 1359 | 		STbp->do_dio = 0; | 
 | 1360 | 	DEB( STp->nbr_requests++; ) | 
 | 1361 |  | 
 | 1362 | 	if (!STbp->do_dio) { | 
 | 1363 | 		if (STp->block_size) | 
 | 1364 | 			bufsize = STp->block_size > st_fixed_buffer_size ? | 
 | 1365 | 				STp->block_size : st_fixed_buffer_size; | 
 | 1366 | 		else | 
 | 1367 | 			bufsize = count; | 
 | 1368 | 		if (bufsize > STbp->buffer_size && | 
 | 1369 | 		    !enlarge_buffer(STbp, bufsize, STp->restr_dma)) { | 
 | 1370 | 			printk(KERN_WARNING "%s: Can't allocate %d byte tape buffer.\n", | 
 | 1371 | 			       tape_name(STp), bufsize); | 
 | 1372 | 			retval = (-EOVERFLOW); | 
 | 1373 | 			goto out; | 
 | 1374 | 		} | 
 | 1375 | 		if (STp->block_size) | 
 | 1376 | 			STbp->buffer_blocks = bufsize / STp->block_size; | 
 | 1377 | 	} | 
 | 1378 |  | 
 | 1379 |  out: | 
 | 1380 | 	return retval; | 
 | 1381 | } | 
 | 1382 |  | 
 | 1383 |  | 
 | 1384 | /* Can be called more than once after each setup_buffer() */ | 
 | 1385 | static void release_buffering(struct scsi_tape *STp) | 
 | 1386 | { | 
 | 1387 | 	struct st_buffer *STbp; | 
 | 1388 |  | 
 | 1389 | 	STbp = STp->buffer; | 
 | 1390 | 	if (STbp->do_dio) { | 
 | 1391 | 		sgl_unmap_user_pages(&(STbp->sg[0]), STbp->do_dio, 0); | 
 | 1392 | 		STbp->do_dio = 0; | 
 | 1393 | 	} | 
 | 1394 | } | 
 | 1395 |  | 
 | 1396 |  | 
 | 1397 | /* Write command */ | 
 | 1398 | static ssize_t | 
 | 1399 | st_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos) | 
 | 1400 | { | 
 | 1401 | 	ssize_t total; | 
 | 1402 | 	ssize_t i, do_count, blks, transfer; | 
 | 1403 | 	ssize_t retval; | 
 | 1404 | 	int undone, retry_eot = 0, scode; | 
 | 1405 | 	int async_write; | 
 | 1406 | 	unsigned char cmd[MAX_COMMAND_SIZE]; | 
 | 1407 | 	const char __user *b_point; | 
 | 1408 | 	struct scsi_request *SRpnt = NULL; | 
 | 1409 | 	struct scsi_tape *STp = filp->private_data; | 
 | 1410 | 	struct st_modedef *STm; | 
 | 1411 | 	struct st_partstat *STps; | 
 | 1412 | 	struct st_buffer *STbp; | 
 | 1413 | 	char *name = tape_name(STp); | 
 | 1414 |  | 
 | 1415 | 	if (down_interruptible(&STp->lock)) | 
 | 1416 | 		return -ERESTARTSYS; | 
 | 1417 |  | 
 | 1418 | 	retval = rw_checks(STp, filp, count); | 
 | 1419 | 	if (retval || count == 0) | 
 | 1420 | 		goto out; | 
 | 1421 |  | 
 | 1422 | 	/* Write must be integral number of blocks */ | 
 | 1423 | 	if (STp->block_size != 0 && (count % STp->block_size) != 0) { | 
 | 1424 | 		printk(KERN_WARNING "%s: Write not multiple of tape block size.\n", | 
 | 1425 | 		       name); | 
 | 1426 | 		retval = (-EINVAL); | 
 | 1427 | 		goto out; | 
 | 1428 | 	} | 
 | 1429 |  | 
 | 1430 | 	STm = &(STp->modes[STp->current_mode]); | 
 | 1431 | 	STps = &(STp->ps[STp->partition]); | 
 | 1432 |  | 
 | 1433 | 	if (STp->write_prot) { | 
 | 1434 | 		retval = (-EACCES); | 
 | 1435 | 		goto out; | 
 | 1436 | 	} | 
 | 1437 |  | 
 | 1438 |  | 
 | 1439 | 	if (STps->rw == ST_READING) { | 
 | 1440 | 		retval = flush_buffer(STp, 0); | 
 | 1441 | 		if (retval) | 
 | 1442 | 			goto out; | 
 | 1443 | 		STps->rw = ST_WRITING; | 
 | 1444 | 	} else if (STps->rw != ST_WRITING && | 
 | 1445 | 		   STps->drv_file == 0 && STps->drv_block == 0) { | 
 | 1446 | 		if ((retval = set_mode_densblk(STp, STm)) < 0) | 
 | 1447 | 			goto out; | 
 | 1448 | 		if (STm->default_compression != ST_DONT_TOUCH && | 
 | 1449 | 		    !(STp->compression_changed)) { | 
 | 1450 | 			if (st_compression(STp, (STm->default_compression == ST_YES))) { | 
 | 1451 | 				printk(KERN_WARNING "%s: Can't set default compression.\n", | 
 | 1452 | 				       name); | 
 | 1453 | 				if (modes_defined) { | 
 | 1454 | 					retval = (-EINVAL); | 
 | 1455 | 					goto out; | 
 | 1456 | 				} | 
 | 1457 | 			} | 
 | 1458 | 		} | 
 | 1459 | 	} | 
 | 1460 |  | 
 | 1461 | 	STbp = STp->buffer; | 
 | 1462 | 	i = write_behind_check(STp); | 
 | 1463 | 	if (i) { | 
 | 1464 | 		if (i == -ENOSPC) | 
 | 1465 | 			STps->eof = ST_EOM_OK; | 
 | 1466 | 		else | 
 | 1467 | 			STps->eof = ST_EOM_ERROR; | 
 | 1468 | 	} | 
 | 1469 |  | 
 | 1470 | 	if (STps->eof == ST_EOM_OK) { | 
 | 1471 | 		STps->eof = ST_EOD_1;  /* allow next write */ | 
 | 1472 | 		retval = (-ENOSPC); | 
 | 1473 | 		goto out; | 
 | 1474 | 	} | 
 | 1475 | 	else if (STps->eof == ST_EOM_ERROR) { | 
 | 1476 | 		retval = (-EIO); | 
 | 1477 | 		goto out; | 
 | 1478 | 	} | 
 | 1479 |  | 
 | 1480 | 	/* Check the buffer readability in cases where copy_user might catch | 
 | 1481 | 	   the problems after some tape movement. */ | 
 | 1482 | 	if (STp->block_size != 0 && | 
 | 1483 | 	    !STbp->do_dio && | 
 | 1484 | 	    (copy_from_user(&i, buf, 1) != 0 || | 
 | 1485 | 	     copy_from_user(&i, buf + count - 1, 1) != 0)) { | 
 | 1486 | 		retval = (-EFAULT); | 
 | 1487 | 		goto out; | 
 | 1488 | 	} | 
 | 1489 |  | 
 | 1490 | 	retval = setup_buffering(STp, buf, count, 0); | 
 | 1491 | 	if (retval) | 
 | 1492 | 		goto out; | 
 | 1493 |  | 
 | 1494 | 	total = count; | 
 | 1495 |  | 
 | 1496 | 	memset(cmd, 0, MAX_COMMAND_SIZE); | 
 | 1497 | 	cmd[0] = WRITE_6; | 
 | 1498 | 	cmd[1] = (STp->block_size != 0); | 
 | 1499 |  | 
 | 1500 | 	STps->rw = ST_WRITING; | 
 | 1501 |  | 
 | 1502 | 	b_point = buf; | 
 | 1503 | 	while (count > 0 && !retry_eot) { | 
 | 1504 |  | 
 | 1505 | 		if (STbp->do_dio) { | 
 | 1506 | 			do_count = count; | 
 | 1507 | 		} | 
 | 1508 | 		else { | 
 | 1509 | 			if (STp->block_size == 0) | 
 | 1510 | 				do_count = count; | 
 | 1511 | 			else { | 
 | 1512 | 				do_count = STbp->buffer_blocks * STp->block_size - | 
 | 1513 | 					STbp->buffer_bytes; | 
 | 1514 | 				if (do_count > count) | 
 | 1515 | 					do_count = count; | 
 | 1516 | 			} | 
 | 1517 |  | 
 | 1518 | 			i = append_to_buffer(b_point, STbp, do_count); | 
 | 1519 | 			if (i) { | 
 | 1520 | 				retval = i; | 
 | 1521 | 				goto out; | 
 | 1522 | 			} | 
 | 1523 | 		} | 
 | 1524 | 		count -= do_count; | 
 | 1525 | 		b_point += do_count; | 
 | 1526 |  | 
 | 1527 | 		async_write = STp->block_size == 0 && !STbp->do_dio && | 
 | 1528 | 			STm->do_async_writes && STps->eof < ST_EOM_OK; | 
 | 1529 |  | 
 | 1530 | 		if (STp->block_size != 0 && STm->do_buffer_writes && | 
 | 1531 | 		    !(STp->try_dio && try_wdio) && STps->eof < ST_EOM_OK && | 
 | 1532 | 		    STbp->buffer_bytes < STbp->buffer_size) { | 
 | 1533 | 			STp->dirty = 1; | 
 | 1534 | 			/* Don't write a buffer that is not full enough. */ | 
 | 1535 | 			if (!async_write && count == 0) | 
 | 1536 | 				break; | 
 | 1537 | 		} | 
 | 1538 |  | 
 | 1539 | 	retry_write: | 
 | 1540 | 		if (STp->block_size == 0) | 
 | 1541 | 			blks = transfer = do_count; | 
 | 1542 | 		else { | 
 | 1543 | 			if (!STbp->do_dio) | 
 | 1544 | 				blks = STbp->buffer_bytes; | 
 | 1545 | 			else | 
 | 1546 | 				blks = do_count; | 
 | 1547 | 			blks /= STp->block_size; | 
 | 1548 | 			transfer = blks * STp->block_size; | 
 | 1549 | 		} | 
 | 1550 | 		cmd[2] = blks >> 16; | 
 | 1551 | 		cmd[3] = blks >> 8; | 
 | 1552 | 		cmd[4] = blks; | 
 | 1553 |  | 
 | 1554 | 		SRpnt = st_do_scsi(SRpnt, STp, cmd, transfer, DMA_TO_DEVICE, | 
 | 1555 | 				   STp->device->timeout, MAX_WRITE_RETRIES, !async_write); | 
 | 1556 | 		if (!SRpnt) { | 
 | 1557 | 			retval = STbp->syscall_result; | 
 | 1558 | 			goto out; | 
 | 1559 | 		} | 
 | 1560 | 		if (async_write) { | 
 | 1561 | 			STbp->writing = transfer; | 
 | 1562 | 			STp->dirty = !(STbp->writing == | 
 | 1563 | 				       STbp->buffer_bytes); | 
 | 1564 | 			SRpnt = NULL;  /* Prevent releasing this request! */ | 
 | 1565 | 			DEB( STp->write_pending = 1; ) | 
 | 1566 | 			break; | 
 | 1567 | 		} | 
 | 1568 |  | 
 | 1569 | 		if (STbp->syscall_result != 0) { | 
 | 1570 | 			struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat; | 
 | 1571 |  | 
 | 1572 |                         DEBC(printk(ST_DEB_MSG "%s: Error on write:\n", name)); | 
 | 1573 | 			if (cmdstatp->have_sense && (cmdstatp->flags & SENSE_EOM)) { | 
 | 1574 | 				scode = cmdstatp->sense_hdr.sense_key; | 
 | 1575 | 				if (cmdstatp->remainder_valid) | 
 | 1576 | 					undone = (int)cmdstatp->uremainder64; | 
 | 1577 | 				else if (STp->block_size == 0 && | 
 | 1578 | 					 scode == VOLUME_OVERFLOW) | 
 | 1579 | 					undone = transfer; | 
 | 1580 | 				else | 
 | 1581 | 					undone = 0; | 
 | 1582 | 				if (STp->block_size != 0) | 
 | 1583 | 					undone *= STp->block_size; | 
 | 1584 | 				if (undone <= do_count) { | 
 | 1585 | 					/* Only data from this write is not written */ | 
 | 1586 | 					count += undone; | 
 | 1587 | 					do_count -= undone; | 
 | 1588 | 					if (STp->block_size) | 
 | 1589 | 						blks = (transfer - undone) / STp->block_size; | 
 | 1590 | 					STps->eof = ST_EOM_OK; | 
 | 1591 | 					/* Continue in fixed block mode if all written | 
 | 1592 | 					   in this request but still something left to write | 
 | 1593 | 					   (retval left to zero) | 
 | 1594 | 					*/ | 
 | 1595 | 					if (STp->block_size == 0 || | 
 | 1596 | 					    undone > 0 || count == 0) | 
 | 1597 | 						retval = (-ENOSPC); /* EOM within current request */ | 
 | 1598 |                                         DEBC(printk(ST_DEB_MSG | 
 | 1599 |                                                        "%s: EOM with %d bytes unwritten.\n", | 
 | 1600 | 						       name, (int)count)); | 
 | 1601 | 				} else { | 
 | 1602 | 					/* EOT within data buffered earlier (possible only | 
 | 1603 | 					   in fixed block mode without direct i/o) */ | 
 | 1604 | 					if (!retry_eot && !cmdstatp->deferred && | 
 | 1605 | 					    (scode == NO_SENSE || scode == RECOVERED_ERROR)) { | 
 | 1606 | 						move_buffer_data(STp->buffer, transfer - undone); | 
 | 1607 | 						retry_eot = 1; | 
 | 1608 | 						if (STps->drv_block >= 0) { | 
 | 1609 | 							STps->drv_block += (transfer - undone) / | 
 | 1610 | 								STp->block_size; | 
 | 1611 | 						} | 
 | 1612 | 						STps->eof = ST_EOM_OK; | 
 | 1613 | 						DEBC(printk(ST_DEB_MSG | 
 | 1614 | 							    "%s: Retry write of %d bytes at EOM.\n", | 
 | 1615 | 							    name, STp->buffer->buffer_bytes)); | 
 | 1616 | 						goto retry_write; | 
 | 1617 | 					} | 
 | 1618 | 					else { | 
 | 1619 | 						/* Either error within data buffered by driver or | 
 | 1620 | 						   failed retry */ | 
 | 1621 | 						count -= do_count; | 
 | 1622 | 						blks = do_count = 0; | 
 | 1623 | 						STps->eof = ST_EOM_ERROR; | 
 | 1624 | 						STps->drv_block = (-1); /* Too cautious? */ | 
 | 1625 | 						retval = (-EIO);	/* EOM for old data */ | 
 | 1626 | 						DEBC(printk(ST_DEB_MSG | 
 | 1627 | 							    "%s: EOM with lost data.\n", | 
 | 1628 | 							    name)); | 
 | 1629 | 					} | 
 | 1630 | 				} | 
 | 1631 | 			} else { | 
 | 1632 | 				count += do_count; | 
 | 1633 | 				STps->drv_block = (-1);		/* Too cautious? */ | 
 | 1634 | 				retval = (-EIO); | 
 | 1635 | 			} | 
 | 1636 |  | 
 | 1637 | 		} | 
 | 1638 |  | 
 | 1639 | 		if (STps->drv_block >= 0) { | 
 | 1640 | 			if (STp->block_size == 0) | 
 | 1641 | 				STps->drv_block += (do_count > 0); | 
 | 1642 | 			else | 
 | 1643 | 				STps->drv_block += blks; | 
 | 1644 | 		} | 
 | 1645 |  | 
 | 1646 | 		STbp->buffer_bytes = 0; | 
 | 1647 | 		STp->dirty = 0; | 
 | 1648 |  | 
 | 1649 | 		if (retval || retry_eot) { | 
 | 1650 | 			if (count < total) | 
 | 1651 | 				retval = total - count; | 
 | 1652 | 			goto out; | 
 | 1653 | 		} | 
 | 1654 | 	} | 
 | 1655 |  | 
 | 1656 | 	if (STps->eof == ST_EOD_1) | 
 | 1657 | 		STps->eof = ST_EOM_OK; | 
 | 1658 | 	else if (STps->eof != ST_EOM_OK) | 
 | 1659 | 		STps->eof = ST_NOEOF; | 
 | 1660 | 	retval = total - count; | 
 | 1661 |  | 
 | 1662 |  out: | 
 | 1663 | 	if (SRpnt != NULL) | 
 | 1664 | 		scsi_release_request(SRpnt); | 
 | 1665 | 	release_buffering(STp); | 
 | 1666 | 	up(&STp->lock); | 
 | 1667 |  | 
 | 1668 | 	return retval; | 
 | 1669 | } | 
 | 1670 |  | 
 | 1671 | /* Read data from the tape. Returns zero in the normal case, one if the | 
 | 1672 |    eof status has changed, and the negative error code in case of a | 
 | 1673 |    fatal error. Otherwise updates the buffer and the eof state. | 
 | 1674 |  | 
 | 1675 |    Does release user buffer mapping if it is set. | 
 | 1676 | */ | 
 | 1677 | static long read_tape(struct scsi_tape *STp, long count, | 
 | 1678 | 		      struct scsi_request ** aSRpnt) | 
 | 1679 | { | 
 | 1680 | 	int transfer, blks, bytes; | 
 | 1681 | 	unsigned char cmd[MAX_COMMAND_SIZE]; | 
 | 1682 | 	struct scsi_request *SRpnt; | 
 | 1683 | 	struct st_modedef *STm; | 
 | 1684 | 	struct st_partstat *STps; | 
 | 1685 | 	struct st_buffer *STbp; | 
 | 1686 | 	int retval = 0; | 
 | 1687 | 	char *name = tape_name(STp); | 
 | 1688 |  | 
 | 1689 | 	if (count == 0) | 
 | 1690 | 		return 0; | 
 | 1691 |  | 
 | 1692 | 	STm = &(STp->modes[STp->current_mode]); | 
 | 1693 | 	STps = &(STp->ps[STp->partition]); | 
 | 1694 | 	if (STps->eof == ST_FM_HIT) | 
 | 1695 | 		return 1; | 
 | 1696 | 	STbp = STp->buffer; | 
 | 1697 |  | 
 | 1698 | 	if (STp->block_size == 0) | 
 | 1699 | 		blks = bytes = count; | 
 | 1700 | 	else { | 
 | 1701 | 		if (!(STp->try_dio && try_rdio) && STm->do_read_ahead) { | 
 | 1702 | 			blks = (STp->buffer)->buffer_blocks; | 
 | 1703 | 			bytes = blks * STp->block_size; | 
 | 1704 | 		} else { | 
 | 1705 | 			bytes = count; | 
 | 1706 | 			if (!STbp->do_dio && bytes > (STp->buffer)->buffer_size) | 
 | 1707 | 				bytes = (STp->buffer)->buffer_size; | 
 | 1708 | 			blks = bytes / STp->block_size; | 
 | 1709 | 			bytes = blks * STp->block_size; | 
 | 1710 | 		} | 
 | 1711 | 	} | 
 | 1712 |  | 
 | 1713 | 	memset(cmd, 0, MAX_COMMAND_SIZE); | 
 | 1714 | 	cmd[0] = READ_6; | 
 | 1715 | 	cmd[1] = (STp->block_size != 0); | 
 | 1716 | 	cmd[2] = blks >> 16; | 
 | 1717 | 	cmd[3] = blks >> 8; | 
 | 1718 | 	cmd[4] = blks; | 
 | 1719 |  | 
 | 1720 | 	SRpnt = *aSRpnt; | 
 | 1721 | 	SRpnt = st_do_scsi(SRpnt, STp, cmd, bytes, DMA_FROM_DEVICE, | 
 | 1722 | 			   STp->device->timeout, MAX_RETRIES, 1); | 
 | 1723 | 	release_buffering(STp); | 
 | 1724 | 	*aSRpnt = SRpnt; | 
 | 1725 | 	if (!SRpnt) | 
 | 1726 | 		return STbp->syscall_result; | 
 | 1727 |  | 
 | 1728 | 	STbp->read_pointer = 0; | 
 | 1729 | 	STps->at_sm = 0; | 
 | 1730 |  | 
 | 1731 | 	/* Something to check */ | 
 | 1732 | 	if (STbp->syscall_result) { | 
 | 1733 | 		struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat; | 
 | 1734 |  | 
 | 1735 | 		retval = 1; | 
 | 1736 | 		DEBC(printk(ST_DEB_MSG "%s: Sense: %2x %2x %2x %2x %2x %2x %2x %2x\n", | 
 | 1737 |                             name, | 
 | 1738 |                             SRpnt->sr_sense_buffer[0], SRpnt->sr_sense_buffer[1], | 
 | 1739 |                             SRpnt->sr_sense_buffer[2], SRpnt->sr_sense_buffer[3], | 
 | 1740 |                             SRpnt->sr_sense_buffer[4], SRpnt->sr_sense_buffer[5], | 
 | 1741 |                             SRpnt->sr_sense_buffer[6], SRpnt->sr_sense_buffer[7])); | 
 | 1742 | 		if (cmdstatp->have_sense) { | 
 | 1743 |  | 
 | 1744 | 			if (cmdstatp->sense_hdr.sense_key == BLANK_CHECK) | 
 | 1745 | 				cmdstatp->flags &= 0xcf;	/* No need for EOM in this case */ | 
 | 1746 |  | 
 | 1747 | 			if (cmdstatp->flags != 0) { /* EOF, EOM, or ILI */ | 
 | 1748 | 				/* Compute the residual count */ | 
 | 1749 | 				if (cmdstatp->remainder_valid) | 
 | 1750 | 					transfer = (int)cmdstatp->uremainder64; | 
 | 1751 | 				else | 
 | 1752 | 					transfer = 0; | 
 | 1753 | 				if (STp->block_size == 0 && | 
 | 1754 | 				    cmdstatp->sense_hdr.sense_key == MEDIUM_ERROR) | 
 | 1755 | 					transfer = bytes; | 
 | 1756 |  | 
 | 1757 | 				if (cmdstatp->flags & SENSE_ILI) {	/* ILI */ | 
 | 1758 | 					if (STp->block_size == 0) { | 
 | 1759 | 						if (transfer <= 0) { | 
 | 1760 | 							if (transfer < 0) | 
 | 1761 | 								printk(KERN_NOTICE | 
 | 1762 | 								       "%s: Failed to read %d byte block with %d byte transfer.\n", | 
 | 1763 | 								       name, bytes - transfer, bytes); | 
 | 1764 | 							if (STps->drv_block >= 0) | 
 | 1765 | 								STps->drv_block += 1; | 
 | 1766 | 							STbp->buffer_bytes = 0; | 
 | 1767 | 							return (-ENOMEM); | 
 | 1768 | 						} | 
 | 1769 | 						STbp->buffer_bytes = bytes - transfer; | 
 | 1770 | 					} else { | 
 | 1771 | 						scsi_release_request(SRpnt); | 
 | 1772 | 						SRpnt = *aSRpnt = NULL; | 
 | 1773 | 						if (transfer == blks) {	/* We did not get anything, error */ | 
 | 1774 | 							printk(KERN_NOTICE "%s: Incorrect block size.\n", name); | 
 | 1775 | 							if (STps->drv_block >= 0) | 
 | 1776 | 								STps->drv_block += blks - transfer + 1; | 
 | 1777 | 							st_int_ioctl(STp, MTBSR, 1); | 
 | 1778 | 							return (-EIO); | 
 | 1779 | 						} | 
 | 1780 | 						/* We have some data, deliver it */ | 
 | 1781 | 						STbp->buffer_bytes = (blks - transfer) * | 
 | 1782 | 						    STp->block_size; | 
 | 1783 |                                                 DEBC(printk(ST_DEB_MSG | 
 | 1784 |                                                             "%s: ILI but enough data received %ld %d.\n", | 
 | 1785 |                                                             name, count, STbp->buffer_bytes)); | 
 | 1786 | 						if (STps->drv_block >= 0) | 
 | 1787 | 							STps->drv_block += 1; | 
 | 1788 | 						if (st_int_ioctl(STp, MTBSR, 1)) | 
 | 1789 | 							return (-EIO); | 
 | 1790 | 					} | 
 | 1791 | 				} else if (cmdstatp->flags & SENSE_FMK) {	/* FM overrides EOM */ | 
 | 1792 | 					if (STps->eof != ST_FM_HIT) | 
 | 1793 | 						STps->eof = ST_FM_HIT; | 
 | 1794 | 					else | 
 | 1795 | 						STps->eof = ST_EOD_2; | 
 | 1796 | 					if (STp->block_size == 0) | 
 | 1797 | 						STbp->buffer_bytes = 0; | 
 | 1798 | 					else | 
 | 1799 | 						STbp->buffer_bytes = | 
 | 1800 | 						    bytes - transfer * STp->block_size; | 
 | 1801 |                                         DEBC(printk(ST_DEB_MSG | 
 | 1802 |                                                     "%s: EOF detected (%d bytes read).\n", | 
 | 1803 |                                                     name, STbp->buffer_bytes)); | 
 | 1804 | 				} else if (cmdstatp->flags & SENSE_EOM) { | 
 | 1805 | 					if (STps->eof == ST_FM) | 
 | 1806 | 						STps->eof = ST_EOD_1; | 
 | 1807 | 					else | 
 | 1808 | 						STps->eof = ST_EOM_OK; | 
 | 1809 | 					if (STp->block_size == 0) | 
 | 1810 | 						STbp->buffer_bytes = bytes - transfer; | 
 | 1811 | 					else | 
 | 1812 | 						STbp->buffer_bytes = | 
 | 1813 | 						    bytes - transfer * STp->block_size; | 
 | 1814 |  | 
 | 1815 |                                         DEBC(printk(ST_DEB_MSG "%s: EOM detected (%d bytes read).\n", | 
 | 1816 |                                                     name, STbp->buffer_bytes)); | 
 | 1817 | 				} | 
 | 1818 | 			} | 
 | 1819 | 			/* end of EOF, EOM, ILI test */  | 
 | 1820 | 			else {	/* nonzero sense key */ | 
 | 1821 |                                 DEBC(printk(ST_DEB_MSG | 
 | 1822 |                                             "%s: Tape error while reading.\n", name)); | 
 | 1823 | 				STps->drv_block = (-1); | 
 | 1824 | 				if (STps->eof == ST_FM && | 
 | 1825 | 				    cmdstatp->sense_hdr.sense_key == BLANK_CHECK) { | 
 | 1826 |                                         DEBC(printk(ST_DEB_MSG | 
 | 1827 |                                                     "%s: Zero returned for first BLANK CHECK after EOF.\n", | 
 | 1828 |                                                     name)); | 
 | 1829 | 					STps->eof = ST_EOD_2;	/* First BLANK_CHECK after FM */ | 
 | 1830 | 				} else	/* Some other extended sense code */ | 
 | 1831 | 					retval = (-EIO); | 
 | 1832 | 			} | 
 | 1833 |  | 
 | 1834 | 			if (STbp->buffer_bytes < 0)  /* Caused by bogus sense data */ | 
 | 1835 | 				STbp->buffer_bytes = 0; | 
 | 1836 | 		} | 
 | 1837 | 		/* End of extended sense test */  | 
 | 1838 | 		else {		/* Non-extended sense */ | 
 | 1839 | 			retval = STbp->syscall_result; | 
 | 1840 | 		} | 
 | 1841 |  | 
 | 1842 | 	} | 
 | 1843 | 	/* End of error handling */  | 
 | 1844 | 	else			/* Read successful */ | 
 | 1845 | 		STbp->buffer_bytes = bytes; | 
 | 1846 |  | 
 | 1847 | 	if (STps->drv_block >= 0) { | 
 | 1848 | 		if (STp->block_size == 0) | 
 | 1849 | 			STps->drv_block++; | 
 | 1850 | 		else | 
 | 1851 | 			STps->drv_block += STbp->buffer_bytes / STp->block_size; | 
 | 1852 | 	} | 
 | 1853 | 	return retval; | 
 | 1854 | } | 
 | 1855 |  | 
 | 1856 |  | 
 | 1857 | /* Read command */ | 
 | 1858 | static ssize_t | 
 | 1859 | st_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos) | 
 | 1860 | { | 
 | 1861 | 	ssize_t total; | 
 | 1862 | 	ssize_t retval = 0; | 
 | 1863 | 	ssize_t i, transfer; | 
 | 1864 | 	int special, do_dio = 0; | 
 | 1865 | 	struct scsi_request *SRpnt = NULL; | 
 | 1866 | 	struct scsi_tape *STp = filp->private_data; | 
 | 1867 | 	struct st_modedef *STm; | 
 | 1868 | 	struct st_partstat *STps; | 
 | 1869 | 	struct st_buffer *STbp = STp->buffer; | 
 | 1870 | 	DEB( char *name = tape_name(STp); ) | 
 | 1871 |  | 
 | 1872 | 	if (down_interruptible(&STp->lock)) | 
 | 1873 | 		return -ERESTARTSYS; | 
 | 1874 |  | 
 | 1875 | 	retval = rw_checks(STp, filp, count); | 
 | 1876 | 	if (retval || count == 0) | 
 | 1877 | 		goto out; | 
 | 1878 |  | 
 | 1879 | 	STm = &(STp->modes[STp->current_mode]); | 
 | 1880 | 	if (!(STm->do_read_ahead) && STp->block_size != 0 && | 
 | 1881 | 	    (count % STp->block_size) != 0) { | 
 | 1882 | 		retval = (-EINVAL);	/* Read must be integral number of blocks */ | 
 | 1883 | 		goto out; | 
 | 1884 | 	} | 
 | 1885 |  | 
 | 1886 | 	STps = &(STp->ps[STp->partition]); | 
 | 1887 | 	if (STps->rw == ST_WRITING) { | 
 | 1888 | 		retval = flush_buffer(STp, 0); | 
 | 1889 | 		if (retval) | 
 | 1890 | 			goto out; | 
 | 1891 | 		STps->rw = ST_READING; | 
 | 1892 | 	} | 
 | 1893 |         DEB( | 
 | 1894 | 	if (debugging && STps->eof != ST_NOEOF) | 
 | 1895 | 		printk(ST_DEB_MSG "%s: EOF/EOM flag up (%d). Bytes %d\n", name, | 
 | 1896 | 		       STps->eof, STbp->buffer_bytes); | 
 | 1897 |         ) /* end DEB */ | 
 | 1898 |  | 
 | 1899 | 	retval = setup_buffering(STp, buf, count, 1); | 
 | 1900 | 	if (retval) | 
 | 1901 | 		goto out; | 
 | 1902 | 	do_dio = STbp->do_dio; | 
 | 1903 |  | 
 | 1904 | 	if (STbp->buffer_bytes == 0 && | 
 | 1905 | 	    STps->eof >= ST_EOD_1) { | 
 | 1906 | 		if (STps->eof < ST_EOD) { | 
 | 1907 | 			STps->eof += 1; | 
 | 1908 | 			retval = 0; | 
 | 1909 | 			goto out; | 
 | 1910 | 		} | 
 | 1911 | 		retval = (-EIO);	/* EOM or Blank Check */ | 
 | 1912 | 		goto out; | 
 | 1913 | 	} | 
 | 1914 |  | 
 | 1915 | 	if (do_dio) { | 
 | 1916 | 		/* Check the buffer writability before any tape movement. Don't alter | 
 | 1917 | 		   buffer data. */ | 
 | 1918 | 		if (copy_from_user(&i, buf, 1) != 0 || | 
 | 1919 | 		    copy_to_user(buf, &i, 1) != 0 || | 
 | 1920 | 		    copy_from_user(&i, buf + count - 1, 1) != 0 || | 
 | 1921 | 		    copy_to_user(buf + count - 1, &i, 1) != 0) { | 
 | 1922 | 			retval = (-EFAULT); | 
 | 1923 | 			goto out; | 
 | 1924 | 		} | 
 | 1925 | 	} | 
 | 1926 |  | 
 | 1927 | 	STps->rw = ST_READING; | 
 | 1928 |  | 
 | 1929 |  | 
 | 1930 | 	/* Loop until enough data in buffer or a special condition found */ | 
 | 1931 | 	for (total = 0, special = 0; total < count && !special;) { | 
 | 1932 |  | 
 | 1933 | 		/* Get new data if the buffer is empty */ | 
 | 1934 | 		if (STbp->buffer_bytes == 0) { | 
 | 1935 | 			special = read_tape(STp, count - total, &SRpnt); | 
 | 1936 | 			if (special < 0) {	/* No need to continue read */ | 
 | 1937 | 				retval = special; | 
 | 1938 | 				goto out; | 
 | 1939 | 			} | 
 | 1940 | 		} | 
 | 1941 |  | 
 | 1942 | 		/* Move the data from driver buffer to user buffer */ | 
 | 1943 | 		if (STbp->buffer_bytes > 0) { | 
 | 1944 |                         DEB( | 
 | 1945 | 			if (debugging && STps->eof != ST_NOEOF) | 
 | 1946 | 				printk(ST_DEB_MSG | 
 | 1947 |                                        "%s: EOF up (%d). Left %d, needed %d.\n", name, | 
 | 1948 | 				       STps->eof, STbp->buffer_bytes, | 
 | 1949 |                                        (int)(count - total)); | 
 | 1950 |                         ) /* end DEB */ | 
 | 1951 | 			transfer = STbp->buffer_bytes < count - total ? | 
 | 1952 | 			    STbp->buffer_bytes : count - total; | 
 | 1953 | 			if (!do_dio) { | 
 | 1954 | 				i = from_buffer(STbp, buf, transfer); | 
 | 1955 | 				if (i) { | 
 | 1956 | 					retval = i; | 
 | 1957 | 					goto out; | 
 | 1958 | 				} | 
 | 1959 | 			} | 
 | 1960 | 			buf += transfer; | 
 | 1961 | 			total += transfer; | 
 | 1962 | 		} | 
 | 1963 |  | 
 | 1964 | 		if (STp->block_size == 0) | 
 | 1965 | 			break;	/* Read only one variable length block */ | 
 | 1966 |  | 
 | 1967 | 	}			/* for (total = 0, special = 0; | 
 | 1968 |                                    total < count && !special; ) */ | 
 | 1969 |  | 
 | 1970 | 	/* Change the eof state if no data from tape or buffer */ | 
 | 1971 | 	if (total == 0) { | 
 | 1972 | 		if (STps->eof == ST_FM_HIT) { | 
 | 1973 | 			STps->eof = ST_FM; | 
 | 1974 | 			STps->drv_block = 0; | 
 | 1975 | 			if (STps->drv_file >= 0) | 
 | 1976 | 				STps->drv_file++; | 
 | 1977 | 		} else if (STps->eof == ST_EOD_1) { | 
 | 1978 | 			STps->eof = ST_EOD_2; | 
 | 1979 | 			STps->drv_block = 0; | 
 | 1980 | 			if (STps->drv_file >= 0) | 
 | 1981 | 				STps->drv_file++; | 
 | 1982 | 		} else if (STps->eof == ST_EOD_2) | 
 | 1983 | 			STps->eof = ST_EOD; | 
 | 1984 | 	} else if (STps->eof == ST_FM) | 
 | 1985 | 		STps->eof = ST_NOEOF; | 
 | 1986 | 	retval = total; | 
 | 1987 |  | 
 | 1988 |  out: | 
 | 1989 | 	if (SRpnt != NULL) { | 
 | 1990 | 		scsi_release_request(SRpnt); | 
 | 1991 | 		SRpnt = NULL; | 
 | 1992 | 	} | 
 | 1993 | 	if (do_dio) { | 
 | 1994 | 		release_buffering(STp); | 
 | 1995 | 		STbp->buffer_bytes = 0; | 
 | 1996 | 	} | 
 | 1997 | 	up(&STp->lock); | 
 | 1998 |  | 
 | 1999 | 	return retval; | 
 | 2000 | } | 
 | 2001 |  | 
 | 2002 |  | 
 | 2003 |  | 
 | 2004 | DEB( | 
 | 2005 | /* Set the driver options */ | 
 | 2006 | static void st_log_options(struct scsi_tape * STp, struct st_modedef * STm, char *name) | 
 | 2007 | { | 
 | 2008 | 	if (debugging) { | 
 | 2009 | 		printk(KERN_INFO | 
 | 2010 | 		       "%s: Mode %d options: buffer writes: %d, async writes: %d, read ahead: %d\n", | 
 | 2011 | 		       name, STp->current_mode, STm->do_buffer_writes, STm->do_async_writes, | 
 | 2012 | 		       STm->do_read_ahead); | 
 | 2013 | 		printk(KERN_INFO | 
 | 2014 | 		       "%s:    can bsr: %d, two FMs: %d, fast mteom: %d, auto lock: %d,\n", | 
 | 2015 | 		       name, STp->can_bsr, STp->two_fm, STp->fast_mteom, STp->do_auto_lock); | 
 | 2016 | 		printk(KERN_INFO | 
 | 2017 | 		       "%s:    defs for wr: %d, no block limits: %d, partitions: %d, s2 log: %d\n", | 
 | 2018 | 		       name, STm->defaults_for_writes, STp->omit_blklims, STp->can_partitions, | 
 | 2019 | 		       STp->scsi2_logical); | 
 | 2020 | 		printk(KERN_INFO | 
 | 2021 | 		       "%s:    sysv: %d nowait: %d\n", name, STm->sysv, STp->immediate); | 
 | 2022 | 		printk(KERN_INFO "%s:    debugging: %d\n", | 
 | 2023 | 		       name, debugging); | 
 | 2024 | 	} | 
 | 2025 | } | 
 | 2026 | 	) | 
 | 2027 |  | 
 | 2028 |  | 
 | 2029 | static int st_set_options(struct scsi_tape *STp, long options) | 
 | 2030 | { | 
 | 2031 | 	int value; | 
 | 2032 | 	long code; | 
 | 2033 | 	struct st_modedef *STm; | 
 | 2034 | 	char *name = tape_name(STp); | 
 | 2035 | 	struct cdev *cd0, *cd1; | 
 | 2036 |  | 
 | 2037 | 	STm = &(STp->modes[STp->current_mode]); | 
 | 2038 | 	if (!STm->defined) { | 
 | 2039 | 		cd0 = STm->cdevs[0]; cd1 = STm->cdevs[1]; | 
 | 2040 | 		memcpy(STm, &(STp->modes[0]), sizeof(struct st_modedef)); | 
 | 2041 | 		STm->cdevs[0] = cd0; STm->cdevs[1] = cd1; | 
 | 2042 | 		modes_defined = 1; | 
 | 2043 |                 DEBC(printk(ST_DEB_MSG | 
 | 2044 |                             "%s: Initialized mode %d definition from mode 0\n", | 
 | 2045 |                             name, STp->current_mode)); | 
 | 2046 | 	} | 
 | 2047 |  | 
 | 2048 | 	code = options & MT_ST_OPTIONS; | 
 | 2049 | 	if (code == MT_ST_BOOLEANS) { | 
 | 2050 | 		STm->do_buffer_writes = (options & MT_ST_BUFFER_WRITES) != 0; | 
 | 2051 | 		STm->do_async_writes = (options & MT_ST_ASYNC_WRITES) != 0; | 
 | 2052 | 		STm->defaults_for_writes = (options & MT_ST_DEF_WRITES) != 0; | 
 | 2053 | 		STm->do_read_ahead = (options & MT_ST_READ_AHEAD) != 0; | 
 | 2054 | 		STp->two_fm = (options & MT_ST_TWO_FM) != 0; | 
 | 2055 | 		STp->fast_mteom = (options & MT_ST_FAST_MTEOM) != 0; | 
 | 2056 | 		STp->do_auto_lock = (options & MT_ST_AUTO_LOCK) != 0; | 
 | 2057 | 		STp->can_bsr = (options & MT_ST_CAN_BSR) != 0; | 
 | 2058 | 		STp->omit_blklims = (options & MT_ST_NO_BLKLIMS) != 0; | 
 | 2059 | 		if ((STp->device)->scsi_level >= SCSI_2) | 
 | 2060 | 			STp->can_partitions = (options & MT_ST_CAN_PARTITIONS) != 0; | 
 | 2061 | 		STp->scsi2_logical = (options & MT_ST_SCSI2LOGICAL) != 0; | 
 | 2062 | 		STp->immediate = (options & MT_ST_NOWAIT) != 0; | 
 | 2063 | 		STm->sysv = (options & MT_ST_SYSV) != 0; | 
 | 2064 | 		DEB( debugging = (options & MT_ST_DEBUGGING) != 0; | 
 | 2065 | 		     st_log_options(STp, STm, name); ) | 
 | 2066 | 	} else if (code == MT_ST_SETBOOLEANS || code == MT_ST_CLEARBOOLEANS) { | 
 | 2067 | 		value = (code == MT_ST_SETBOOLEANS); | 
 | 2068 | 		if ((options & MT_ST_BUFFER_WRITES) != 0) | 
 | 2069 | 			STm->do_buffer_writes = value; | 
 | 2070 | 		if ((options & MT_ST_ASYNC_WRITES) != 0) | 
 | 2071 | 			STm->do_async_writes = value; | 
 | 2072 | 		if ((options & MT_ST_DEF_WRITES) != 0) | 
 | 2073 | 			STm->defaults_for_writes = value; | 
 | 2074 | 		if ((options & MT_ST_READ_AHEAD) != 0) | 
 | 2075 | 			STm->do_read_ahead = value; | 
 | 2076 | 		if ((options & MT_ST_TWO_FM) != 0) | 
 | 2077 | 			STp->two_fm = value; | 
 | 2078 | 		if ((options & MT_ST_FAST_MTEOM) != 0) | 
 | 2079 | 			STp->fast_mteom = value; | 
 | 2080 | 		if ((options & MT_ST_AUTO_LOCK) != 0) | 
 | 2081 | 			STp->do_auto_lock = value; | 
 | 2082 | 		if ((options & MT_ST_CAN_BSR) != 0) | 
 | 2083 | 			STp->can_bsr = value; | 
 | 2084 | 		if ((options & MT_ST_NO_BLKLIMS) != 0) | 
 | 2085 | 			STp->omit_blklims = value; | 
 | 2086 | 		if ((STp->device)->scsi_level >= SCSI_2 && | 
 | 2087 | 		    (options & MT_ST_CAN_PARTITIONS) != 0) | 
 | 2088 | 			STp->can_partitions = value; | 
 | 2089 | 		if ((options & MT_ST_SCSI2LOGICAL) != 0) | 
 | 2090 | 			STp->scsi2_logical = value; | 
 | 2091 | 		if ((options & MT_ST_NOWAIT) != 0) | 
 | 2092 | 			STp->immediate = value; | 
 | 2093 | 		if ((options & MT_ST_SYSV) != 0) | 
 | 2094 | 			STm->sysv = value; | 
 | 2095 |                 DEB( | 
 | 2096 | 		if ((options & MT_ST_DEBUGGING) != 0) | 
 | 2097 | 			debugging = value; | 
 | 2098 | 			st_log_options(STp, STm, name); ) | 
 | 2099 | 	} else if (code == MT_ST_WRITE_THRESHOLD) { | 
 | 2100 | 		/* Retained for compatibility */ | 
 | 2101 | 	} else if (code == MT_ST_DEF_BLKSIZE) { | 
 | 2102 | 		value = (options & ~MT_ST_OPTIONS); | 
 | 2103 | 		if (value == ~MT_ST_OPTIONS) { | 
 | 2104 | 			STm->default_blksize = (-1); | 
 | 2105 | 			DEBC( printk(KERN_INFO "%s: Default block size disabled.\n", name)); | 
 | 2106 | 		} else { | 
 | 2107 | 			STm->default_blksize = value; | 
 | 2108 | 			DEBC( printk(KERN_INFO "%s: Default block size set to %d bytes.\n", | 
 | 2109 | 			       name, STm->default_blksize)); | 
 | 2110 | 			if (STp->ready == ST_READY) { | 
 | 2111 | 				STp->blksize_changed = 0; | 
 | 2112 | 				set_mode_densblk(STp, STm); | 
 | 2113 | 			} | 
 | 2114 | 		} | 
 | 2115 | 	} else if (code == MT_ST_TIMEOUTS) { | 
 | 2116 | 		value = (options & ~MT_ST_OPTIONS); | 
 | 2117 | 		if ((value & MT_ST_SET_LONG_TIMEOUT) != 0) { | 
 | 2118 | 			STp->long_timeout = (value & ~MT_ST_SET_LONG_TIMEOUT) * HZ; | 
 | 2119 | 			DEBC( printk(KERN_INFO "%s: Long timeout set to %d seconds.\n", name, | 
 | 2120 | 			       (value & ~MT_ST_SET_LONG_TIMEOUT))); | 
 | 2121 | 		} else { | 
 | 2122 | 			STp->device->timeout = value * HZ; | 
 | 2123 | 			DEBC( printk(KERN_INFO "%s: Normal timeout set to %d seconds.\n", | 
 | 2124 | 				name, value) ); | 
 | 2125 | 		} | 
 | 2126 | 	} else if (code == MT_ST_SET_CLN) { | 
 | 2127 | 		value = (options & ~MT_ST_OPTIONS) & 0xff; | 
 | 2128 | 		if (value != 0 && | 
 | 2129 | 		    value < EXTENDED_SENSE_START && value >= SCSI_SENSE_BUFFERSIZE) | 
 | 2130 | 			return (-EINVAL); | 
 | 2131 | 		STp->cln_mode = value; | 
 | 2132 | 		STp->cln_sense_mask = (options >> 8) & 0xff; | 
 | 2133 | 		STp->cln_sense_value = (options >> 16) & 0xff; | 
 | 2134 | 		printk(KERN_INFO | 
 | 2135 | 		       "%s: Cleaning request mode %d, mask %02x, value %02x\n", | 
 | 2136 | 		       name, value, STp->cln_sense_mask, STp->cln_sense_value); | 
 | 2137 | 	} else if (code == MT_ST_DEF_OPTIONS) { | 
 | 2138 | 		code = (options & ~MT_ST_CLEAR_DEFAULT); | 
 | 2139 | 		value = (options & MT_ST_CLEAR_DEFAULT); | 
 | 2140 | 		if (code == MT_ST_DEF_DENSITY) { | 
 | 2141 | 			if (value == MT_ST_CLEAR_DEFAULT) { | 
 | 2142 | 				STm->default_density = (-1); | 
 | 2143 | 				DEBC( printk(KERN_INFO "%s: Density default disabled.\n", | 
 | 2144 |                                        name)); | 
 | 2145 | 			} else { | 
 | 2146 | 				STm->default_density = value & 0xff; | 
 | 2147 | 				DEBC( printk(KERN_INFO "%s: Density default set to %x\n", | 
 | 2148 | 				       name, STm->default_density)); | 
 | 2149 | 				if (STp->ready == ST_READY) { | 
 | 2150 | 					STp->density_changed = 0; | 
 | 2151 | 					set_mode_densblk(STp, STm); | 
 | 2152 | 				} | 
 | 2153 | 			} | 
 | 2154 | 		} else if (code == MT_ST_DEF_DRVBUFFER) { | 
 | 2155 | 			if (value == MT_ST_CLEAR_DEFAULT) { | 
 | 2156 | 				STp->default_drvbuffer = 0xff; | 
 | 2157 | 				DEBC( printk(KERN_INFO | 
 | 2158 |                                        "%s: Drive buffer default disabled.\n", name)); | 
 | 2159 | 			} else { | 
 | 2160 | 				STp->default_drvbuffer = value & 7; | 
 | 2161 | 				DEBC( printk(KERN_INFO | 
 | 2162 |                                        "%s: Drive buffer default set to %x\n", | 
 | 2163 | 				       name, STp->default_drvbuffer)); | 
 | 2164 | 				if (STp->ready == ST_READY) | 
 | 2165 | 					st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer); | 
 | 2166 | 			} | 
 | 2167 | 		} else if (code == MT_ST_DEF_COMPRESSION) { | 
 | 2168 | 			if (value == MT_ST_CLEAR_DEFAULT) { | 
 | 2169 | 				STm->default_compression = ST_DONT_TOUCH; | 
 | 2170 | 				DEBC( printk(KERN_INFO | 
 | 2171 |                                        "%s: Compression default disabled.\n", name)); | 
 | 2172 | 			} else { | 
 | 2173 | 				if ((value & 0xff00) != 0) { | 
 | 2174 | 					STp->c_algo = (value & 0xff00) >> 8; | 
 | 2175 | 					DEBC( printk(KERN_INFO "%s: Compression algorithm set to 0x%x.\n", | 
 | 2176 | 					       name, STp->c_algo)); | 
 | 2177 | 				} | 
 | 2178 | 				if ((value & 0xff) != 0xff) { | 
 | 2179 | 					STm->default_compression = (value & 1 ? ST_YES : ST_NO); | 
 | 2180 | 					DEBC( printk(KERN_INFO "%s: Compression default set to %x\n", | 
 | 2181 | 					       name, (value & 1))); | 
 | 2182 | 					if (STp->ready == ST_READY) { | 
 | 2183 | 						STp->compression_changed = 0; | 
 | 2184 | 						st_compression(STp, (STm->default_compression == ST_YES)); | 
 | 2185 | 					} | 
 | 2186 | 				} | 
 | 2187 | 			} | 
 | 2188 | 		} | 
 | 2189 | 	} else | 
 | 2190 | 		return (-EIO); | 
 | 2191 |  | 
 | 2192 | 	return 0; | 
 | 2193 | } | 
 | 2194 |  | 
 | 2195 | #define MODE_HEADER_LENGTH  4 | 
 | 2196 |  | 
 | 2197 | /* Mode header and page byte offsets */ | 
 | 2198 | #define MH_OFF_DATA_LENGTH     0 | 
 | 2199 | #define MH_OFF_MEDIUM_TYPE     1 | 
 | 2200 | #define MH_OFF_DEV_SPECIFIC    2 | 
 | 2201 | #define MH_OFF_BDESCS_LENGTH   3 | 
 | 2202 | #define MP_OFF_PAGE_NBR        0 | 
 | 2203 | #define MP_OFF_PAGE_LENGTH     1 | 
 | 2204 |  | 
 | 2205 | /* Mode header and page bit masks */ | 
 | 2206 | #define MH_BIT_WP              0x80 | 
 | 2207 | #define MP_MSK_PAGE_NBR        0x3f | 
 | 2208 |  | 
 | 2209 | /* Don't return block descriptors */ | 
 | 2210 | #define MODE_SENSE_OMIT_BDESCS 0x08 | 
 | 2211 |  | 
 | 2212 | #define MODE_SELECT_PAGE_FORMAT 0x10 | 
 | 2213 |  | 
 | 2214 | /* Read a mode page into the tape buffer. The block descriptors are included | 
 | 2215 |    if incl_block_descs is true. The page control is ored to the page number | 
 | 2216 |    parameter, if necessary. */ | 
 | 2217 | static int read_mode_page(struct scsi_tape *STp, int page, int omit_block_descs) | 
 | 2218 | { | 
 | 2219 | 	unsigned char cmd[MAX_COMMAND_SIZE]; | 
 | 2220 | 	struct scsi_request *SRpnt = NULL; | 
 | 2221 |  | 
 | 2222 | 	memset(cmd, 0, MAX_COMMAND_SIZE); | 
 | 2223 | 	cmd[0] = MODE_SENSE; | 
 | 2224 | 	if (omit_block_descs) | 
 | 2225 | 		cmd[1] = MODE_SENSE_OMIT_BDESCS; | 
 | 2226 | 	cmd[2] = page; | 
 | 2227 | 	cmd[4] = 255; | 
 | 2228 |  | 
 | 2229 | 	SRpnt = st_do_scsi(SRpnt, STp, cmd, cmd[4], DMA_FROM_DEVICE, | 
 | 2230 | 			   STp->device->timeout, 0, 1); | 
 | 2231 | 	if (SRpnt == NULL) | 
 | 2232 | 		return (STp->buffer)->syscall_result; | 
 | 2233 |  | 
 | 2234 | 	scsi_release_request(SRpnt); | 
 | 2235 |  | 
 | 2236 | 	return (STp->buffer)->syscall_result; | 
 | 2237 | } | 
 | 2238 |  | 
 | 2239 |  | 
 | 2240 | /* Send the mode page in the tape buffer to the drive. Assumes that the mode data | 
 | 2241 |    in the buffer is correctly formatted. The long timeout is used if slow is non-zero. */ | 
 | 2242 | static int write_mode_page(struct scsi_tape *STp, int page, int slow) | 
 | 2243 | { | 
 | 2244 | 	int pgo; | 
 | 2245 | 	unsigned char cmd[MAX_COMMAND_SIZE]; | 
 | 2246 | 	struct scsi_request *SRpnt = NULL; | 
 | 2247 |  | 
 | 2248 | 	memset(cmd, 0, MAX_COMMAND_SIZE); | 
 | 2249 | 	cmd[0] = MODE_SELECT; | 
 | 2250 | 	cmd[1] = MODE_SELECT_PAGE_FORMAT; | 
 | 2251 | 	pgo = MODE_HEADER_LENGTH + (STp->buffer)->b_data[MH_OFF_BDESCS_LENGTH]; | 
 | 2252 | 	cmd[4] = pgo + (STp->buffer)->b_data[pgo + MP_OFF_PAGE_LENGTH] + 2; | 
 | 2253 |  | 
 | 2254 | 	/* Clear reserved fields */ | 
 | 2255 | 	(STp->buffer)->b_data[MH_OFF_DATA_LENGTH] = 0; | 
 | 2256 | 	(STp->buffer)->b_data[MH_OFF_MEDIUM_TYPE] = 0; | 
 | 2257 | 	(STp->buffer)->b_data[MH_OFF_DEV_SPECIFIC] &= ~MH_BIT_WP; | 
 | 2258 | 	(STp->buffer)->b_data[pgo + MP_OFF_PAGE_NBR] &= MP_MSK_PAGE_NBR; | 
 | 2259 |  | 
 | 2260 | 	SRpnt = st_do_scsi(SRpnt, STp, cmd, cmd[4], DMA_TO_DEVICE, | 
 | 2261 | 			   (slow ? STp->long_timeout : STp->device->timeout), 0, 1); | 
 | 2262 | 	if (SRpnt == NULL) | 
 | 2263 | 		return (STp->buffer)->syscall_result; | 
 | 2264 |  | 
 | 2265 | 	scsi_release_request(SRpnt); | 
 | 2266 |  | 
 | 2267 | 	return (STp->buffer)->syscall_result; | 
 | 2268 | } | 
 | 2269 |  | 
 | 2270 |  | 
 | 2271 | #define COMPRESSION_PAGE        0x0f | 
 | 2272 | #define COMPRESSION_PAGE_LENGTH 16 | 
 | 2273 |  | 
 | 2274 | #define CP_OFF_DCE_DCC          2 | 
 | 2275 | #define CP_OFF_C_ALGO           7 | 
 | 2276 |  | 
 | 2277 | #define DCE_MASK  0x80 | 
 | 2278 | #define DCC_MASK  0x40 | 
 | 2279 | #define RED_MASK  0x60 | 
 | 2280 |  | 
 | 2281 |  | 
 | 2282 | /* Control the compression with mode page 15. Algorithm not changed if zero. | 
 | 2283 |  | 
 | 2284 |    The block descriptors are read and written because Sony SDT-7000 does not | 
 | 2285 |    work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>). | 
 | 2286 |    Including block descriptors should not cause any harm to other drives. */ | 
 | 2287 |  | 
 | 2288 | static int st_compression(struct scsi_tape * STp, int state) | 
 | 2289 | { | 
 | 2290 | 	int retval; | 
 | 2291 | 	int mpoffs;  /* Offset to mode page start */ | 
 | 2292 | 	unsigned char *b_data = (STp->buffer)->b_data; | 
 | 2293 | 	DEB( char *name = tape_name(STp); ) | 
 | 2294 |  | 
 | 2295 | 	if (STp->ready != ST_READY) | 
 | 2296 | 		return (-EIO); | 
 | 2297 |  | 
 | 2298 | 	/* Read the current page contents */ | 
 | 2299 | 	retval = read_mode_page(STp, COMPRESSION_PAGE, 0); | 
 | 2300 | 	if (retval) { | 
 | 2301 |                 DEBC(printk(ST_DEB_MSG "%s: Compression mode page not supported.\n", | 
 | 2302 |                             name)); | 
 | 2303 | 		return (-EIO); | 
 | 2304 | 	} | 
 | 2305 |  | 
 | 2306 | 	mpoffs = MODE_HEADER_LENGTH + b_data[MH_OFF_BDESCS_LENGTH]; | 
 | 2307 |         DEBC(printk(ST_DEB_MSG "%s: Compression state is %d.\n", name, | 
 | 2308 |                     (b_data[mpoffs + CP_OFF_DCE_DCC] & DCE_MASK ? 1 : 0))); | 
 | 2309 |  | 
 | 2310 | 	/* Check if compression can be changed */ | 
 | 2311 | 	if ((b_data[mpoffs + CP_OFF_DCE_DCC] & DCC_MASK) == 0) { | 
 | 2312 |                 DEBC(printk(ST_DEB_MSG "%s: Compression not supported.\n", name)); | 
 | 2313 | 		return (-EIO); | 
 | 2314 | 	} | 
 | 2315 |  | 
 | 2316 | 	/* Do the change */ | 
 | 2317 | 	if (state) { | 
 | 2318 | 		b_data[mpoffs + CP_OFF_DCE_DCC] |= DCE_MASK; | 
 | 2319 | 		if (STp->c_algo != 0) | 
 | 2320 | 			b_data[mpoffs + CP_OFF_C_ALGO] = STp->c_algo; | 
 | 2321 | 	} | 
 | 2322 | 	else { | 
 | 2323 | 		b_data[mpoffs + CP_OFF_DCE_DCC] &= ~DCE_MASK; | 
 | 2324 | 		if (STp->c_algo != 0) | 
 | 2325 | 			b_data[mpoffs + CP_OFF_C_ALGO] = 0; /* no compression */ | 
 | 2326 | 	} | 
 | 2327 |  | 
 | 2328 | 	retval = write_mode_page(STp, COMPRESSION_PAGE, 0); | 
 | 2329 | 	if (retval) { | 
 | 2330 |                 DEBC(printk(ST_DEB_MSG "%s: Compression change failed.\n", name)); | 
 | 2331 | 		return (-EIO); | 
 | 2332 | 	} | 
 | 2333 |         DEBC(printk(ST_DEB_MSG "%s: Compression state changed to %d.\n", | 
 | 2334 | 		       name, state)); | 
 | 2335 |  | 
 | 2336 | 	STp->compression_changed = 1; | 
 | 2337 | 	return 0; | 
 | 2338 | } | 
 | 2339 |  | 
 | 2340 |  | 
 | 2341 | /* Process the load and unload commands (does unload if the load code is zero) */ | 
 | 2342 | static int do_load_unload(struct scsi_tape *STp, struct file *filp, int load_code) | 
 | 2343 | { | 
 | 2344 | 	int retval = (-EIO), timeout; | 
 | 2345 | 	DEB( char *name = tape_name(STp); ) | 
 | 2346 | 	unsigned char cmd[MAX_COMMAND_SIZE]; | 
 | 2347 | 	struct st_partstat *STps; | 
 | 2348 | 	struct scsi_request *SRpnt; | 
 | 2349 |  | 
 | 2350 | 	if (STp->ready != ST_READY && !load_code) { | 
 | 2351 | 		if (STp->ready == ST_NO_TAPE) | 
 | 2352 | 			return (-ENOMEDIUM); | 
 | 2353 | 		else | 
 | 2354 | 			return (-EIO); | 
 | 2355 | 	} | 
 | 2356 |  | 
 | 2357 | 	memset(cmd, 0, MAX_COMMAND_SIZE); | 
 | 2358 | 	cmd[0] = START_STOP; | 
 | 2359 | 	if (load_code) | 
 | 2360 | 		cmd[4] |= 1; | 
 | 2361 | 	/* | 
 | 2362 | 	 * If arg >= 1 && arg <= 6 Enhanced load/unload in HP C1553A | 
 | 2363 | 	 */ | 
 | 2364 | 	if (load_code >= 1 + MT_ST_HPLOADER_OFFSET | 
 | 2365 | 	    && load_code <= 6 + MT_ST_HPLOADER_OFFSET) { | 
 | 2366 | 		DEBC(printk(ST_DEB_MSG "%s: Enhanced %sload slot %2d.\n", | 
 | 2367 | 			    name, (cmd[4]) ? "" : "un", | 
 | 2368 | 			    load_code - MT_ST_HPLOADER_OFFSET)); | 
 | 2369 | 		cmd[3] = load_code - MT_ST_HPLOADER_OFFSET; /* MediaID field of C1553A */ | 
 | 2370 | 	} | 
 | 2371 | 	if (STp->immediate) { | 
 | 2372 | 		cmd[1] = 1;	/* Don't wait for completion */ | 
 | 2373 | 		timeout = STp->device->timeout; | 
 | 2374 | 	} | 
 | 2375 | 	else | 
 | 2376 | 		timeout = STp->long_timeout; | 
 | 2377 |  | 
 | 2378 | 	DEBC( | 
 | 2379 | 		if (!load_code) | 
 | 2380 | 		printk(ST_DEB_MSG "%s: Unloading tape.\n", name); | 
 | 2381 | 		else | 
 | 2382 | 		printk(ST_DEB_MSG "%s: Loading tape.\n", name); | 
 | 2383 | 		); | 
 | 2384 |  | 
 | 2385 | 	SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE, | 
 | 2386 | 			   timeout, MAX_RETRIES, 1); | 
 | 2387 | 	if (!SRpnt) | 
 | 2388 | 		return (STp->buffer)->syscall_result; | 
 | 2389 |  | 
 | 2390 | 	retval = (STp->buffer)->syscall_result; | 
 | 2391 | 	scsi_release_request(SRpnt); | 
 | 2392 |  | 
 | 2393 | 	if (!retval) {	/* SCSI command successful */ | 
 | 2394 |  | 
 | 2395 | 		if (!load_code) { | 
 | 2396 | 			STp->rew_at_close = 0; | 
 | 2397 | 			STp->ready = ST_NO_TAPE; | 
 | 2398 | 		} | 
 | 2399 | 		else { | 
 | 2400 | 			STp->rew_at_close = STp->autorew_dev; | 
 | 2401 | 			retval = check_tape(STp, filp); | 
 | 2402 | 			if (retval > 0) | 
 | 2403 | 				retval = 0; | 
 | 2404 | 		} | 
 | 2405 | 	} | 
 | 2406 | 	else { | 
 | 2407 | 		STps = &(STp->ps[STp->partition]); | 
 | 2408 | 		STps->drv_file = STps->drv_block = (-1); | 
 | 2409 | 	} | 
 | 2410 |  | 
 | 2411 | 	return retval; | 
 | 2412 | } | 
 | 2413 |  | 
 | 2414 | #if DEBUG | 
 | 2415 | #define ST_DEB_FORWARD  0 | 
 | 2416 | #define ST_DEB_BACKWARD 1 | 
 | 2417 | static void deb_space_print(char *name, int direction, char *units, unsigned char *cmd) | 
 | 2418 | { | 
 | 2419 | 	s32 sc; | 
 | 2420 |  | 
 | 2421 | 	sc = cmd[2] & 0x80 ? 0xff000000 : 0; | 
 | 2422 | 	sc |= (cmd[2] << 16) | (cmd[3] << 8) | cmd[4]; | 
 | 2423 | 	if (direction) | 
 | 2424 | 		sc = -sc; | 
 | 2425 | 	printk(ST_DEB_MSG "%s: Spacing tape %s over %d %s.\n", name, | 
 | 2426 | 	       direction ? "backward" : "forward", sc, units); | 
 | 2427 | } | 
 | 2428 | #endif | 
 | 2429 |  | 
 | 2430 |  | 
 | 2431 | /* Internal ioctl function */ | 
 | 2432 | static int st_int_ioctl(struct scsi_tape *STp, unsigned int cmd_in, unsigned long arg) | 
 | 2433 | { | 
 | 2434 | 	int timeout; | 
 | 2435 | 	long ltmp; | 
 | 2436 | 	int ioctl_result; | 
 | 2437 | 	int chg_eof = 1; | 
 | 2438 | 	unsigned char cmd[MAX_COMMAND_SIZE]; | 
 | 2439 | 	struct scsi_request *SRpnt; | 
 | 2440 | 	struct st_partstat *STps; | 
 | 2441 | 	int fileno, blkno, at_sm, undone; | 
 | 2442 | 	int datalen = 0, direction = DMA_NONE; | 
 | 2443 | 	char *name = tape_name(STp); | 
 | 2444 |  | 
 | 2445 | 	WARN_ON(STp->buffer->do_dio != 0); | 
 | 2446 | 	if (STp->ready != ST_READY) { | 
 | 2447 | 		if (STp->ready == ST_NO_TAPE) | 
 | 2448 | 			return (-ENOMEDIUM); | 
 | 2449 | 		else | 
 | 2450 | 			return (-EIO); | 
 | 2451 | 	} | 
 | 2452 | 	timeout = STp->long_timeout; | 
 | 2453 | 	STps = &(STp->ps[STp->partition]); | 
 | 2454 | 	fileno = STps->drv_file; | 
 | 2455 | 	blkno = STps->drv_block; | 
 | 2456 | 	at_sm = STps->at_sm; | 
 | 2457 |  | 
 | 2458 | 	memset(cmd, 0, MAX_COMMAND_SIZE); | 
 | 2459 | 	switch (cmd_in) { | 
 | 2460 | 	case MTFSFM: | 
 | 2461 | 		chg_eof = 0;	/* Changed from the FSF after this */ | 
 | 2462 | 	case MTFSF: | 
 | 2463 | 		cmd[0] = SPACE; | 
 | 2464 | 		cmd[1] = 0x01;	/* Space FileMarks */ | 
 | 2465 | 		cmd[2] = (arg >> 16); | 
 | 2466 | 		cmd[3] = (arg >> 8); | 
 | 2467 | 		cmd[4] = arg; | 
 | 2468 |                 DEBC(deb_space_print(name, ST_DEB_FORWARD, "filemarks", cmd);) | 
 | 2469 | 		if (fileno >= 0) | 
 | 2470 | 			fileno += arg; | 
 | 2471 | 		blkno = 0; | 
 | 2472 | 		at_sm &= (arg == 0); | 
 | 2473 | 		break; | 
 | 2474 | 	case MTBSFM: | 
 | 2475 | 		chg_eof = 0;	/* Changed from the FSF after this */ | 
 | 2476 | 	case MTBSF: | 
 | 2477 | 		cmd[0] = SPACE; | 
 | 2478 | 		cmd[1] = 0x01;	/* Space FileMarks */ | 
 | 2479 | 		ltmp = (-arg); | 
 | 2480 | 		cmd[2] = (ltmp >> 16); | 
 | 2481 | 		cmd[3] = (ltmp >> 8); | 
 | 2482 | 		cmd[4] = ltmp; | 
 | 2483 |                 DEBC(deb_space_print(name, ST_DEB_BACKWARD, "filemarks", cmd);) | 
 | 2484 | 		if (fileno >= 0) | 
 | 2485 | 			fileno -= arg; | 
 | 2486 | 		blkno = (-1);	/* We can't know the block number */ | 
 | 2487 | 		at_sm &= (arg == 0); | 
 | 2488 | 		break; | 
 | 2489 | 	case MTFSR: | 
 | 2490 | 		cmd[0] = SPACE; | 
 | 2491 | 		cmd[1] = 0x00;	/* Space Blocks */ | 
 | 2492 | 		cmd[2] = (arg >> 16); | 
 | 2493 | 		cmd[3] = (arg >> 8); | 
 | 2494 | 		cmd[4] = arg; | 
 | 2495 |                 DEBC(deb_space_print(name, ST_DEB_FORWARD, "blocks", cmd);) | 
 | 2496 | 		if (blkno >= 0) | 
 | 2497 | 			blkno += arg; | 
 | 2498 | 		at_sm &= (arg == 0); | 
 | 2499 | 		break; | 
 | 2500 | 	case MTBSR: | 
 | 2501 | 		cmd[0] = SPACE; | 
 | 2502 | 		cmd[1] = 0x00;	/* Space Blocks */ | 
 | 2503 | 		ltmp = (-arg); | 
 | 2504 | 		cmd[2] = (ltmp >> 16); | 
 | 2505 | 		cmd[3] = (ltmp >> 8); | 
 | 2506 | 		cmd[4] = ltmp; | 
 | 2507 |                 DEBC(deb_space_print(name, ST_DEB_BACKWARD, "blocks", cmd);) | 
 | 2508 | 		if (blkno >= 0) | 
 | 2509 | 			blkno -= arg; | 
 | 2510 | 		at_sm &= (arg == 0); | 
 | 2511 | 		break; | 
 | 2512 | 	case MTFSS: | 
 | 2513 | 		cmd[0] = SPACE; | 
 | 2514 | 		cmd[1] = 0x04;	/* Space Setmarks */ | 
 | 2515 | 		cmd[2] = (arg >> 16); | 
 | 2516 | 		cmd[3] = (arg >> 8); | 
 | 2517 | 		cmd[4] = arg; | 
 | 2518 |                 DEBC(deb_space_print(name, ST_DEB_FORWARD, "setmarks", cmd);) | 
 | 2519 | 		if (arg != 0) { | 
 | 2520 | 			blkno = fileno = (-1); | 
 | 2521 | 			at_sm = 1; | 
 | 2522 | 		} | 
 | 2523 | 		break; | 
 | 2524 | 	case MTBSS: | 
 | 2525 | 		cmd[0] = SPACE; | 
 | 2526 | 		cmd[1] = 0x04;	/* Space Setmarks */ | 
 | 2527 | 		ltmp = (-arg); | 
 | 2528 | 		cmd[2] = (ltmp >> 16); | 
 | 2529 | 		cmd[3] = (ltmp >> 8); | 
 | 2530 | 		cmd[4] = ltmp; | 
 | 2531 |                 DEBC(deb_space_print(name, ST_DEB_BACKWARD, "setmarks", cmd);) | 
 | 2532 | 		if (arg != 0) { | 
 | 2533 | 			blkno = fileno = (-1); | 
 | 2534 | 			at_sm = 1; | 
 | 2535 | 		} | 
 | 2536 | 		break; | 
 | 2537 | 	case MTWEOF: | 
 | 2538 | 	case MTWSM: | 
 | 2539 | 		if (STp->write_prot) | 
 | 2540 | 			return (-EACCES); | 
 | 2541 | 		cmd[0] = WRITE_FILEMARKS; | 
 | 2542 | 		if (cmd_in == MTWSM) | 
 | 2543 | 			cmd[1] = 2; | 
 | 2544 | 		cmd[2] = (arg >> 16); | 
 | 2545 | 		cmd[3] = (arg >> 8); | 
 | 2546 | 		cmd[4] = arg; | 
 | 2547 | 		timeout = STp->device->timeout; | 
 | 2548 |                 DEBC( | 
 | 2549 |                      if (cmd_in == MTWEOF) | 
 | 2550 |                                printk(ST_DEB_MSG "%s: Writing %d filemarks.\n", name, | 
 | 2551 | 				 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]); | 
 | 2552 |                      else | 
 | 2553 | 				printk(ST_DEB_MSG "%s: Writing %d setmarks.\n", name, | 
 | 2554 | 				 cmd[2] * 65536 + cmd[3] * 256 + cmd[4]); | 
 | 2555 | 		) | 
 | 2556 | 		if (fileno >= 0) | 
 | 2557 | 			fileno += arg; | 
 | 2558 | 		blkno = 0; | 
 | 2559 | 		at_sm = (cmd_in == MTWSM); | 
 | 2560 | 		break; | 
 | 2561 | 	case MTREW: | 
 | 2562 | 		cmd[0] = REZERO_UNIT; | 
 | 2563 | 		if (STp->immediate) { | 
 | 2564 | 			cmd[1] = 1;	/* Don't wait for completion */ | 
 | 2565 | 			timeout = STp->device->timeout; | 
 | 2566 | 		} | 
 | 2567 |                 DEBC(printk(ST_DEB_MSG "%s: Rewinding tape.\n", name)); | 
 | 2568 | 		fileno = blkno = at_sm = 0; | 
 | 2569 | 		break; | 
 | 2570 | 	case MTNOP: | 
 | 2571 |                 DEBC(printk(ST_DEB_MSG "%s: No op on tape.\n", name)); | 
 | 2572 | 		return 0;	/* Should do something ? */ | 
 | 2573 | 		break; | 
 | 2574 | 	case MTRETEN: | 
 | 2575 | 		cmd[0] = START_STOP; | 
 | 2576 | 		if (STp->immediate) { | 
 | 2577 | 			cmd[1] = 1;	/* Don't wait for completion */ | 
 | 2578 | 			timeout = STp->device->timeout; | 
 | 2579 | 		} | 
 | 2580 | 		cmd[4] = 3; | 
 | 2581 |                 DEBC(printk(ST_DEB_MSG "%s: Retensioning tape.\n", name)); | 
 | 2582 | 		fileno = blkno = at_sm = 0; | 
 | 2583 | 		break; | 
 | 2584 | 	case MTEOM: | 
 | 2585 | 		if (!STp->fast_mteom) { | 
 | 2586 | 			/* space to the end of tape */ | 
 | 2587 | 			ioctl_result = st_int_ioctl(STp, MTFSF, 0x7fffff); | 
 | 2588 | 			fileno = STps->drv_file; | 
 | 2589 | 			if (STps->eof >= ST_EOD_1) | 
 | 2590 | 				return 0; | 
 | 2591 | 			/* The next lines would hide the number of spaced FileMarks | 
 | 2592 | 			   That's why I inserted the previous lines. I had no luck | 
 | 2593 | 			   with detecting EOM with FSF, so we go now to EOM. | 
 | 2594 | 			   Joerg Weule */ | 
 | 2595 | 		} else | 
 | 2596 | 			fileno = (-1); | 
 | 2597 | 		cmd[0] = SPACE; | 
 | 2598 | 		cmd[1] = 3; | 
 | 2599 |                 DEBC(printk(ST_DEB_MSG "%s: Spacing to end of recorded medium.\n", | 
 | 2600 |                             name)); | 
 | 2601 | 		blkno = -1; | 
 | 2602 | 		at_sm = 0; | 
 | 2603 | 		break; | 
 | 2604 | 	case MTERASE: | 
 | 2605 | 		if (STp->write_prot) | 
 | 2606 | 			return (-EACCES); | 
 | 2607 | 		cmd[0] = ERASE; | 
 | 2608 | 		cmd[1] = (arg ? 1 : 0);	/* Long erase with non-zero argument */ | 
 | 2609 | 		if (STp->immediate) { | 
 | 2610 | 			cmd[1] |= 2;	/* Don't wait for completion */ | 
 | 2611 | 			timeout = STp->device->timeout; | 
 | 2612 | 		} | 
 | 2613 | 		else | 
 | 2614 | 			timeout = STp->long_timeout * 8; | 
 | 2615 |  | 
 | 2616 |                 DEBC(printk(ST_DEB_MSG "%s: Erasing tape.\n", name)); | 
 | 2617 | 		fileno = blkno = at_sm = 0; | 
 | 2618 | 		break; | 
 | 2619 | 	case MTSETBLK:		/* Set block length */ | 
 | 2620 | 	case MTSETDENSITY:	/* Set tape density */ | 
 | 2621 | 	case MTSETDRVBUFFER:	/* Set drive buffering */ | 
 | 2622 | 	case SET_DENS_AND_BLK:	/* Set density and block size */ | 
 | 2623 | 		chg_eof = 0; | 
 | 2624 | 		if (STp->dirty || (STp->buffer)->buffer_bytes != 0) | 
 | 2625 | 			return (-EIO);	/* Not allowed if data in buffer */ | 
 | 2626 | 		if ((cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) && | 
 | 2627 | 		    (arg & MT_ST_BLKSIZE_MASK) != 0 && | 
 | 2628 | 		    STp->max_block > 0 && | 
 | 2629 | 		    ((arg & MT_ST_BLKSIZE_MASK) < STp->min_block || | 
 | 2630 | 		     (arg & MT_ST_BLKSIZE_MASK) > STp->max_block)) { | 
 | 2631 | 			printk(KERN_WARNING "%s: Illegal block size.\n", name); | 
 | 2632 | 			return (-EINVAL); | 
 | 2633 | 		} | 
 | 2634 | 		cmd[0] = MODE_SELECT; | 
 | 2635 | 		if ((STp->use_pf & USE_PF)) | 
 | 2636 | 			cmd[1] = MODE_SELECT_PAGE_FORMAT; | 
 | 2637 | 		cmd[4] = datalen = 12; | 
 | 2638 | 		direction = DMA_TO_DEVICE; | 
 | 2639 |  | 
 | 2640 | 		memset((STp->buffer)->b_data, 0, 12); | 
 | 2641 | 		if (cmd_in == MTSETDRVBUFFER) | 
 | 2642 | 			(STp->buffer)->b_data[2] = (arg & 7) << 4; | 
 | 2643 | 		else | 
 | 2644 | 			(STp->buffer)->b_data[2] = | 
 | 2645 | 			    STp->drv_buffer << 4; | 
 | 2646 | 		(STp->buffer)->b_data[3] = 8;	/* block descriptor length */ | 
 | 2647 | 		if (cmd_in == MTSETDENSITY) { | 
 | 2648 | 			(STp->buffer)->b_data[4] = arg; | 
 | 2649 | 			STp->density_changed = 1;	/* At least we tried ;-) */ | 
 | 2650 | 		} else if (cmd_in == SET_DENS_AND_BLK) | 
 | 2651 | 			(STp->buffer)->b_data[4] = arg >> 24; | 
 | 2652 | 		else | 
 | 2653 | 			(STp->buffer)->b_data[4] = STp->density; | 
 | 2654 | 		if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) { | 
 | 2655 | 			ltmp = arg & MT_ST_BLKSIZE_MASK; | 
 | 2656 | 			if (cmd_in == MTSETBLK) | 
 | 2657 | 				STp->blksize_changed = 1; /* At least we tried ;-) */ | 
 | 2658 | 		} else | 
 | 2659 | 			ltmp = STp->block_size; | 
 | 2660 | 		(STp->buffer)->b_data[9] = (ltmp >> 16); | 
 | 2661 | 		(STp->buffer)->b_data[10] = (ltmp >> 8); | 
 | 2662 | 		(STp->buffer)->b_data[11] = ltmp; | 
 | 2663 | 		timeout = STp->device->timeout; | 
 | 2664 |                 DEBC( | 
 | 2665 | 			if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) | 
 | 2666 | 				printk(ST_DEB_MSG | 
 | 2667 |                                        "%s: Setting block size to %d bytes.\n", name, | 
 | 2668 | 				       (STp->buffer)->b_data[9] * 65536 + | 
 | 2669 | 				       (STp->buffer)->b_data[10] * 256 + | 
 | 2670 | 				       (STp->buffer)->b_data[11]); | 
 | 2671 | 			if (cmd_in == MTSETDENSITY || cmd_in == SET_DENS_AND_BLK) | 
 | 2672 | 				printk(ST_DEB_MSG | 
 | 2673 |                                        "%s: Setting density code to %x.\n", name, | 
 | 2674 | 				       (STp->buffer)->b_data[4]); | 
 | 2675 | 			if (cmd_in == MTSETDRVBUFFER) | 
 | 2676 | 				printk(ST_DEB_MSG | 
 | 2677 |                                        "%s: Setting drive buffer code to %d.\n", name, | 
 | 2678 | 				    ((STp->buffer)->b_data[2] >> 4) & 7); | 
 | 2679 | 		) | 
 | 2680 | 		break; | 
 | 2681 | 	default: | 
 | 2682 | 		return (-ENOSYS); | 
 | 2683 | 	} | 
 | 2684 |  | 
 | 2685 | 	SRpnt = st_do_scsi(NULL, STp, cmd, datalen, direction, | 
 | 2686 | 			   timeout, MAX_RETRIES, 1); | 
 | 2687 | 	if (!SRpnt) | 
 | 2688 | 		return (STp->buffer)->syscall_result; | 
 | 2689 |  | 
 | 2690 | 	ioctl_result = (STp->buffer)->syscall_result; | 
 | 2691 |  | 
 | 2692 | 	if (!ioctl_result) {	/* SCSI command successful */ | 
 | 2693 | 		scsi_release_request(SRpnt); | 
 | 2694 | 		SRpnt = NULL; | 
 | 2695 | 		STps->drv_block = blkno; | 
 | 2696 | 		STps->drv_file = fileno; | 
 | 2697 | 		STps->at_sm = at_sm; | 
 | 2698 |  | 
 | 2699 | 		if (cmd_in == MTBSFM) | 
 | 2700 | 			ioctl_result = st_int_ioctl(STp, MTFSF, 1); | 
 | 2701 | 		else if (cmd_in == MTFSFM) | 
 | 2702 | 			ioctl_result = st_int_ioctl(STp, MTBSF, 1); | 
 | 2703 |  | 
 | 2704 | 		if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) { | 
 | 2705 | 			int old_block_size = STp->block_size; | 
 | 2706 | 			STp->block_size = arg & MT_ST_BLKSIZE_MASK; | 
 | 2707 | 			if (STp->block_size != 0) { | 
 | 2708 | 				if (old_block_size == 0) | 
 | 2709 | 					normalize_buffer(STp->buffer); | 
 | 2710 | 				(STp->buffer)->buffer_blocks = | 
 | 2711 | 				    (STp->buffer)->buffer_size / STp->block_size; | 
 | 2712 | 			} | 
 | 2713 | 			(STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0; | 
 | 2714 | 			if (cmd_in == SET_DENS_AND_BLK) | 
 | 2715 | 				STp->density = arg >> MT_ST_DENSITY_SHIFT; | 
 | 2716 | 		} else if (cmd_in == MTSETDRVBUFFER) | 
 | 2717 | 			STp->drv_buffer = (arg & 7); | 
 | 2718 | 		else if (cmd_in == MTSETDENSITY) | 
 | 2719 | 			STp->density = arg; | 
 | 2720 |  | 
 | 2721 | 		if (cmd_in == MTEOM) | 
 | 2722 | 			STps->eof = ST_EOD; | 
 | 2723 | 		else if (cmd_in == MTFSF) | 
 | 2724 | 			STps->eof = ST_FM; | 
 | 2725 | 		else if (chg_eof) | 
 | 2726 | 			STps->eof = ST_NOEOF; | 
 | 2727 |  | 
 | 2728 | 		if (cmd_in == MTWEOF) | 
 | 2729 | 			STps->rw = ST_IDLE; | 
 | 2730 | 	} else { /* SCSI command was not completely successful. Don't return | 
 | 2731 |                     from this block without releasing the SCSI command block! */ | 
 | 2732 | 		struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat; | 
 | 2733 |  | 
 | 2734 | 		if (cmdstatp->flags & SENSE_EOM) { | 
 | 2735 | 			if (cmd_in != MTBSF && cmd_in != MTBSFM && | 
 | 2736 | 			    cmd_in != MTBSR && cmd_in != MTBSS) | 
 | 2737 | 				STps->eof = ST_EOM_OK; | 
 | 2738 | 			STps->drv_block = 0; | 
 | 2739 | 		} | 
 | 2740 |  | 
 | 2741 | 		if (cmdstatp->remainder_valid) | 
 | 2742 | 			undone = (int)cmdstatp->uremainder64; | 
 | 2743 | 		else | 
 | 2744 | 			undone = 0; | 
 | 2745 |  | 
 | 2746 | 		if (cmd_in == MTWEOF && | 
 | 2747 | 		    cmdstatp->have_sense && | 
 | 2748 | 		    (cmdstatp->flags & SENSE_EOM) && | 
 | 2749 | 		    (cmdstatp->sense_hdr.sense_key == NO_SENSE || | 
 | 2750 | 		     cmdstatp->sense_hdr.sense_key == RECOVERED_ERROR) && | 
 | 2751 | 		    undone == 0) { | 
 | 2752 | 			ioctl_result = 0;	/* EOF written succesfully at EOM */ | 
 | 2753 | 			if (fileno >= 0) | 
 | 2754 | 				fileno++; | 
 | 2755 | 			STps->drv_file = fileno; | 
 | 2756 | 			STps->eof = ST_NOEOF; | 
 | 2757 | 		} else if ((cmd_in == MTFSF) || (cmd_in == MTFSFM)) { | 
 | 2758 | 			if (fileno >= 0) | 
 | 2759 | 				STps->drv_file = fileno - undone; | 
 | 2760 | 			else | 
 | 2761 | 				STps->drv_file = fileno; | 
 | 2762 | 			STps->drv_block = -1; | 
 | 2763 | 			STps->eof = ST_NOEOF; | 
 | 2764 | 		} else if ((cmd_in == MTBSF) || (cmd_in == MTBSFM)) { | 
 | 2765 | 			if (arg > 0 && undone < 0)  /* Some drives get this wrong */ | 
 | 2766 | 				undone = (-undone); | 
 | 2767 | 			if (STps->drv_file >= 0) | 
 | 2768 | 				STps->drv_file = fileno + undone; | 
 | 2769 | 			STps->drv_block = 0; | 
 | 2770 | 			STps->eof = ST_NOEOF; | 
 | 2771 | 		} else if (cmd_in == MTFSR) { | 
 | 2772 | 			if (cmdstatp->flags & SENSE_FMK) {	/* Hit filemark */ | 
 | 2773 | 				if (STps->drv_file >= 0) | 
 | 2774 | 					STps->drv_file++; | 
 | 2775 | 				STps->drv_block = 0; | 
 | 2776 | 				STps->eof = ST_FM; | 
 | 2777 | 			} else { | 
 | 2778 | 				if (blkno >= undone) | 
 | 2779 | 					STps->drv_block = blkno - undone; | 
 | 2780 | 				else | 
 | 2781 | 					STps->drv_block = (-1); | 
 | 2782 | 				STps->eof = ST_NOEOF; | 
 | 2783 | 			} | 
 | 2784 | 		} else if (cmd_in == MTBSR) { | 
 | 2785 | 			if (cmdstatp->flags & SENSE_FMK) {	/* Hit filemark */ | 
 | 2786 | 				STps->drv_file--; | 
 | 2787 | 				STps->drv_block = (-1); | 
 | 2788 | 			} else { | 
 | 2789 | 				if (arg > 0 && undone < 0)  /* Some drives get this wrong */ | 
 | 2790 | 					undone = (-undone); | 
 | 2791 | 				if (STps->drv_block >= 0) | 
 | 2792 | 					STps->drv_block = blkno + undone; | 
 | 2793 | 			} | 
 | 2794 | 			STps->eof = ST_NOEOF; | 
 | 2795 | 		} else if (cmd_in == MTEOM) { | 
 | 2796 | 			STps->drv_file = (-1); | 
 | 2797 | 			STps->drv_block = (-1); | 
 | 2798 | 			STps->eof = ST_EOD; | 
 | 2799 | 		} else if (cmd_in == MTSETBLK || | 
 | 2800 | 			   cmd_in == MTSETDENSITY || | 
 | 2801 | 			   cmd_in == MTSETDRVBUFFER || | 
 | 2802 | 			   cmd_in == SET_DENS_AND_BLK) { | 
 | 2803 | 			if (cmdstatp->sense_hdr.sense_key == ILLEGAL_REQUEST && | 
 | 2804 | 			    !(STp->use_pf & PF_TESTED)) { | 
 | 2805 | 				/* Try the other possible state of Page Format if not | 
 | 2806 | 				   already tried */ | 
 | 2807 | 				STp->use_pf = !STp->use_pf | PF_TESTED; | 
 | 2808 | 				scsi_release_request(SRpnt); | 
 | 2809 | 				SRpnt = NULL; | 
 | 2810 | 				return st_int_ioctl(STp, cmd_in, arg); | 
 | 2811 | 			} | 
 | 2812 | 		} else if (chg_eof) | 
 | 2813 | 			STps->eof = ST_NOEOF; | 
 | 2814 |  | 
 | 2815 | 		if (cmdstatp->sense_hdr.sense_key == BLANK_CHECK) | 
 | 2816 | 			STps->eof = ST_EOD; | 
 | 2817 |  | 
 | 2818 | 		scsi_release_request(SRpnt); | 
 | 2819 | 		SRpnt = NULL; | 
 | 2820 | 	} | 
 | 2821 |  | 
 | 2822 | 	return ioctl_result; | 
 | 2823 | } | 
 | 2824 |  | 
 | 2825 |  | 
 | 2826 | /* Get the tape position. If bt == 2, arg points into a kernel space mt_loc | 
 | 2827 |    structure. */ | 
 | 2828 |  | 
 | 2829 | static int get_location(struct scsi_tape *STp, unsigned int *block, int *partition, | 
 | 2830 | 			int logical) | 
 | 2831 | { | 
 | 2832 | 	int result; | 
 | 2833 | 	unsigned char scmd[MAX_COMMAND_SIZE]; | 
 | 2834 | 	struct scsi_request *SRpnt; | 
 | 2835 | 	DEB( char *name = tape_name(STp); ) | 
 | 2836 |  | 
 | 2837 | 	if (STp->ready != ST_READY) | 
 | 2838 | 		return (-EIO); | 
 | 2839 |  | 
 | 2840 | 	memset(scmd, 0, MAX_COMMAND_SIZE); | 
 | 2841 | 	if ((STp->device)->scsi_level < SCSI_2) { | 
 | 2842 | 		scmd[0] = QFA_REQUEST_BLOCK; | 
 | 2843 | 		scmd[4] = 3; | 
 | 2844 | 	} else { | 
 | 2845 | 		scmd[0] = READ_POSITION; | 
 | 2846 | 		if (!logical && !STp->scsi2_logical) | 
 | 2847 | 			scmd[1] = 1; | 
 | 2848 | 	} | 
 | 2849 | 	SRpnt = st_do_scsi(NULL, STp, scmd, 20, DMA_FROM_DEVICE, | 
 | 2850 | 			STp->device->timeout, MAX_READY_RETRIES, 1); | 
 | 2851 | 	if (!SRpnt) | 
 | 2852 | 		return (STp->buffer)->syscall_result; | 
 | 2853 |  | 
 | 2854 | 	if ((STp->buffer)->syscall_result != 0 || | 
 | 2855 | 	    (STp->device->scsi_level >= SCSI_2 && | 
 | 2856 | 	     ((STp->buffer)->b_data[0] & 4) != 0)) { | 
 | 2857 | 		*block = *partition = 0; | 
 | 2858 |                 DEBC(printk(ST_DEB_MSG "%s: Can't read tape position.\n", name)); | 
 | 2859 | 		result = (-EIO); | 
 | 2860 | 	} else { | 
 | 2861 | 		result = 0; | 
 | 2862 | 		if ((STp->device)->scsi_level < SCSI_2) { | 
 | 2863 | 			*block = ((STp->buffer)->b_data[0] << 16) | 
 | 2864 | 			    + ((STp->buffer)->b_data[1] << 8) | 
 | 2865 | 			    + (STp->buffer)->b_data[2]; | 
 | 2866 | 			*partition = 0; | 
 | 2867 | 		} else { | 
 | 2868 | 			*block = ((STp->buffer)->b_data[4] << 24) | 
 | 2869 | 			    + ((STp->buffer)->b_data[5] << 16) | 
 | 2870 | 			    + ((STp->buffer)->b_data[6] << 8) | 
 | 2871 | 			    + (STp->buffer)->b_data[7]; | 
 | 2872 | 			*partition = (STp->buffer)->b_data[1]; | 
 | 2873 | 			if (((STp->buffer)->b_data[0] & 0x80) && | 
 | 2874 | 			    (STp->buffer)->b_data[1] == 0)	/* BOP of partition 0 */ | 
 | 2875 | 				STp->ps[0].drv_block = STp->ps[0].drv_file = 0; | 
 | 2876 | 		} | 
 | 2877 |                 DEBC(printk(ST_DEB_MSG "%s: Got tape pos. blk %d part %d.\n", name, | 
 | 2878 |                             *block, *partition)); | 
 | 2879 | 	} | 
 | 2880 | 	scsi_release_request(SRpnt); | 
 | 2881 | 	SRpnt = NULL; | 
 | 2882 |  | 
 | 2883 | 	return result; | 
 | 2884 | } | 
 | 2885 |  | 
 | 2886 |  | 
 | 2887 | /* Set the tape block and partition. Negative partition means that only the | 
 | 2888 |    block should be set in vendor specific way. */ | 
 | 2889 | static int set_location(struct scsi_tape *STp, unsigned int block, int partition, | 
 | 2890 | 			int logical) | 
 | 2891 | { | 
 | 2892 | 	struct st_partstat *STps; | 
 | 2893 | 	int result, p; | 
 | 2894 | 	unsigned int blk; | 
 | 2895 | 	int timeout; | 
 | 2896 | 	unsigned char scmd[MAX_COMMAND_SIZE]; | 
 | 2897 | 	struct scsi_request *SRpnt; | 
 | 2898 | 	DEB( char *name = tape_name(STp); ) | 
 | 2899 |  | 
 | 2900 | 	if (STp->ready != ST_READY) | 
 | 2901 | 		return (-EIO); | 
 | 2902 | 	timeout = STp->long_timeout; | 
 | 2903 | 	STps = &(STp->ps[STp->partition]); | 
 | 2904 |  | 
 | 2905 |         DEBC(printk(ST_DEB_MSG "%s: Setting block to %d and partition to %d.\n", | 
 | 2906 |                     name, block, partition)); | 
 | 2907 | 	DEB(if (partition < 0) | 
 | 2908 | 		return (-EIO); ) | 
 | 2909 |  | 
 | 2910 | 	/* Update the location at the partition we are leaving */ | 
 | 2911 | 	if ((!STp->can_partitions && partition != 0) || | 
 | 2912 | 	    partition >= ST_NBR_PARTITIONS) | 
 | 2913 | 		return (-EINVAL); | 
 | 2914 | 	if (partition != STp->partition) { | 
 | 2915 | 		if (get_location(STp, &blk, &p, 1)) | 
 | 2916 | 			STps->last_block_valid = 0; | 
 | 2917 | 		else { | 
 | 2918 | 			STps->last_block_valid = 1; | 
 | 2919 | 			STps->last_block_visited = blk; | 
 | 2920 |                         DEBC(printk(ST_DEB_MSG | 
 | 2921 |                                     "%s: Visited block %d for partition %d saved.\n", | 
 | 2922 |                                     name, blk, STp->partition)); | 
 | 2923 | 		} | 
 | 2924 | 	} | 
 | 2925 |  | 
 | 2926 | 	memset(scmd, 0, MAX_COMMAND_SIZE); | 
 | 2927 | 	if ((STp->device)->scsi_level < SCSI_2) { | 
 | 2928 | 		scmd[0] = QFA_SEEK_BLOCK; | 
 | 2929 | 		scmd[2] = (block >> 16); | 
 | 2930 | 		scmd[3] = (block >> 8); | 
 | 2931 | 		scmd[4] = block; | 
 | 2932 | 		scmd[5] = 0; | 
 | 2933 | 	} else { | 
 | 2934 | 		scmd[0] = SEEK_10; | 
 | 2935 | 		scmd[3] = (block >> 24); | 
 | 2936 | 		scmd[4] = (block >> 16); | 
 | 2937 | 		scmd[5] = (block >> 8); | 
 | 2938 | 		scmd[6] = block; | 
 | 2939 | 		if (!logical && !STp->scsi2_logical) | 
 | 2940 | 			scmd[1] = 4; | 
 | 2941 | 		if (STp->partition != partition) { | 
 | 2942 | 			scmd[1] |= 2; | 
 | 2943 | 			scmd[8] = partition; | 
 | 2944 |                         DEBC(printk(ST_DEB_MSG | 
 | 2945 |                                     "%s: Trying to change partition from %d to %d\n", | 
 | 2946 |                                     name, STp->partition, partition)); | 
 | 2947 | 		} | 
 | 2948 | 	} | 
 | 2949 | 	if (STp->immediate) { | 
 | 2950 | 		scmd[1] |= 1;		/* Don't wait for completion */ | 
 | 2951 | 		timeout = STp->device->timeout; | 
 | 2952 | 	} | 
 | 2953 |  | 
 | 2954 | 	SRpnt = st_do_scsi(NULL, STp, scmd, 0, DMA_NONE, | 
 | 2955 | 			   timeout, MAX_READY_RETRIES, 1); | 
 | 2956 | 	if (!SRpnt) | 
 | 2957 | 		return (STp->buffer)->syscall_result; | 
 | 2958 |  | 
 | 2959 | 	STps->drv_block = STps->drv_file = (-1); | 
 | 2960 | 	STps->eof = ST_NOEOF; | 
 | 2961 | 	if ((STp->buffer)->syscall_result != 0) { | 
 | 2962 | 		result = (-EIO); | 
 | 2963 | 		if (STp->can_partitions && | 
 | 2964 | 		    (STp->device)->scsi_level >= SCSI_2 && | 
 | 2965 | 		    (p = find_partition(STp)) >= 0) | 
 | 2966 | 			STp->partition = p; | 
 | 2967 | 	} else { | 
 | 2968 | 		if (STp->can_partitions) { | 
 | 2969 | 			STp->partition = partition; | 
 | 2970 | 			STps = &(STp->ps[partition]); | 
 | 2971 | 			if (!STps->last_block_valid || | 
 | 2972 | 			    STps->last_block_visited != block) { | 
 | 2973 | 				STps->at_sm = 0; | 
 | 2974 | 				STps->rw = ST_IDLE; | 
 | 2975 | 			} | 
 | 2976 | 		} else | 
 | 2977 | 			STps->at_sm = 0; | 
 | 2978 | 		if (block == 0) | 
 | 2979 | 			STps->drv_block = STps->drv_file = 0; | 
 | 2980 | 		result = 0; | 
 | 2981 | 	} | 
 | 2982 |  | 
 | 2983 | 	scsi_release_request(SRpnt); | 
 | 2984 | 	SRpnt = NULL; | 
 | 2985 |  | 
 | 2986 | 	return result; | 
 | 2987 | } | 
 | 2988 |  | 
 | 2989 |  | 
 | 2990 | /* Find the current partition number for the drive status. Called from open and | 
 | 2991 |    returns either partition number of negative error code. */ | 
 | 2992 | static int find_partition(struct scsi_tape *STp) | 
 | 2993 | { | 
 | 2994 | 	int i, partition; | 
 | 2995 | 	unsigned int block; | 
 | 2996 |  | 
 | 2997 | 	if ((i = get_location(STp, &block, &partition, 1)) < 0) | 
 | 2998 | 		return i; | 
 | 2999 | 	if (partition >= ST_NBR_PARTITIONS) | 
 | 3000 | 		return (-EIO); | 
 | 3001 | 	return partition; | 
 | 3002 | } | 
 | 3003 |  | 
 | 3004 |  | 
 | 3005 | /* Change the partition if necessary */ | 
 | 3006 | static int switch_partition(struct scsi_tape *STp) | 
 | 3007 | { | 
 | 3008 | 	struct st_partstat *STps; | 
 | 3009 |  | 
 | 3010 | 	if (STp->partition == STp->new_partition) | 
 | 3011 | 		return 0; | 
 | 3012 | 	STps = &(STp->ps[STp->new_partition]); | 
 | 3013 | 	if (!STps->last_block_valid) | 
 | 3014 | 		STps->last_block_visited = 0; | 
 | 3015 | 	return set_location(STp, STps->last_block_visited, STp->new_partition, 1); | 
 | 3016 | } | 
 | 3017 |  | 
 | 3018 | /* Functions for reading and writing the medium partition mode page. */ | 
 | 3019 |  | 
 | 3020 | #define PART_PAGE   0x11 | 
 | 3021 | #define PART_PAGE_FIXED_LENGTH 8 | 
 | 3022 |  | 
 | 3023 | #define PP_OFF_MAX_ADD_PARTS   2 | 
 | 3024 | #define PP_OFF_NBR_ADD_PARTS   3 | 
 | 3025 | #define PP_OFF_FLAGS           4 | 
 | 3026 | #define PP_OFF_PART_UNITS      6 | 
 | 3027 | #define PP_OFF_RESERVED        7 | 
 | 3028 |  | 
 | 3029 | #define PP_BIT_IDP             0x20 | 
 | 3030 | #define PP_MSK_PSUM_MB         0x10 | 
 | 3031 |  | 
 | 3032 | /* Get the number of partitions on the tape. As a side effect reads the | 
 | 3033 |    mode page into the tape buffer. */ | 
 | 3034 | static int nbr_partitions(struct scsi_tape *STp) | 
 | 3035 | { | 
 | 3036 | 	int result; | 
 | 3037 | 	DEB( char *name = tape_name(STp); ) | 
 | 3038 |  | 
 | 3039 | 	if (STp->ready != ST_READY) | 
 | 3040 | 		return (-EIO); | 
 | 3041 |  | 
 | 3042 | 	result = read_mode_page(STp, PART_PAGE, 1); | 
 | 3043 |  | 
 | 3044 | 	if (result) { | 
 | 3045 |                 DEBC(printk(ST_DEB_MSG "%s: Can't read medium partition page.\n", | 
 | 3046 |                             name)); | 
 | 3047 | 		result = (-EIO); | 
 | 3048 | 	} else { | 
 | 3049 | 		result = (STp->buffer)->b_data[MODE_HEADER_LENGTH + | 
 | 3050 | 					      PP_OFF_NBR_ADD_PARTS] + 1; | 
 | 3051 |                 DEBC(printk(ST_DEB_MSG "%s: Number of partitions %d.\n", name, result)); | 
 | 3052 | 	} | 
 | 3053 |  | 
 | 3054 | 	return result; | 
 | 3055 | } | 
 | 3056 |  | 
 | 3057 |  | 
 | 3058 | /* Partition the tape into two partitions if size > 0 or one partition if | 
 | 3059 |    size == 0. | 
 | 3060 |  | 
 | 3061 |    The block descriptors are read and written because Sony SDT-7000 does not | 
 | 3062 |    work without this (suggestion from Michael Schaefer <Michael.Schaefer@dlr.de>). | 
 | 3063 |  | 
 | 3064 |    My HP C1533A drive returns only one partition size field. This is used to | 
 | 3065 |    set the size of partition 1. There is no size field for the default partition. | 
 | 3066 |    Michael Schaefer's Sony SDT-7000 returns two descriptors and the second is | 
 | 3067 |    used to set the size of partition 1 (this is what the SCSI-3 standard specifies). | 
 | 3068 |    The following algorithm is used to accommodate both drives: if the number of | 
 | 3069 |    partition size fields is greater than the maximum number of additional partitions | 
 | 3070 |    in the mode page, the second field is used. Otherwise the first field is used. | 
 | 3071 |  | 
 | 3072 |    For Seagate DDS drives the page length must be 8 when no partitions is defined | 
 | 3073 |    and 10 when 1 partition is defined (information from Eric Lee Green). This is | 
 | 3074 |    is acceptable also to some other old drives and enforced if the first partition | 
 | 3075 |    size field is used for the first additional partition size. | 
 | 3076 |  */ | 
 | 3077 | static int partition_tape(struct scsi_tape *STp, int size) | 
 | 3078 | { | 
 | 3079 | 	char *name = tape_name(STp); | 
 | 3080 | 	int result; | 
 | 3081 | 	int pgo, psd_cnt, psdo; | 
 | 3082 | 	unsigned char *bp; | 
 | 3083 |  | 
 | 3084 | 	result = read_mode_page(STp, PART_PAGE, 0); | 
 | 3085 | 	if (result) { | 
 | 3086 | 		DEBC(printk(ST_DEB_MSG "%s: Can't read partition mode page.\n", name)); | 
 | 3087 | 		return result; | 
 | 3088 | 	} | 
 | 3089 | 	/* The mode page is in the buffer. Let's modify it and write it. */ | 
 | 3090 | 	bp = (STp->buffer)->b_data; | 
 | 3091 | 	pgo = MODE_HEADER_LENGTH + bp[MH_OFF_BDESCS_LENGTH]; | 
 | 3092 | 	DEBC(printk(ST_DEB_MSG "%s: Partition page length is %d bytes.\n", | 
 | 3093 | 		    name, bp[pgo + MP_OFF_PAGE_LENGTH] + 2)); | 
 | 3094 |  | 
 | 3095 | 	psd_cnt = (bp[pgo + MP_OFF_PAGE_LENGTH] + 2 - PART_PAGE_FIXED_LENGTH) / 2; | 
 | 3096 | 	psdo = pgo + PART_PAGE_FIXED_LENGTH; | 
 | 3097 | 	if (psd_cnt > bp[pgo + PP_OFF_MAX_ADD_PARTS]) { | 
 | 3098 | 		bp[psdo] = bp[psdo + 1] = 0xff;  /* Rest of the tape */ | 
 | 3099 | 		psdo += 2; | 
 | 3100 | 	} | 
 | 3101 | 	memset(bp + psdo, 0, bp[pgo + PP_OFF_NBR_ADD_PARTS] * 2); | 
 | 3102 |  | 
 | 3103 | 	DEBC(printk("%s: psd_cnt %d, max.parts %d, nbr_parts %d\n", name, | 
 | 3104 | 		    psd_cnt, bp[pgo + PP_OFF_MAX_ADD_PARTS], | 
 | 3105 | 		    bp[pgo + PP_OFF_NBR_ADD_PARTS])); | 
 | 3106 |  | 
 | 3107 | 	if (size <= 0) { | 
 | 3108 | 		bp[pgo + PP_OFF_NBR_ADD_PARTS] = 0; | 
 | 3109 | 		if (psd_cnt <= bp[pgo + PP_OFF_MAX_ADD_PARTS]) | 
 | 3110 | 		    bp[pgo + MP_OFF_PAGE_LENGTH] = 6; | 
 | 3111 |                 DEBC(printk(ST_DEB_MSG "%s: Formatting tape with one partition.\n", | 
 | 3112 |                             name)); | 
 | 3113 | 	} else { | 
 | 3114 | 		bp[psdo] = (size >> 8) & 0xff; | 
 | 3115 | 		bp[psdo + 1] = size & 0xff; | 
 | 3116 | 		bp[pgo + 3] = 1; | 
 | 3117 | 		if (bp[pgo + MP_OFF_PAGE_LENGTH] < 8) | 
 | 3118 | 		    bp[pgo + MP_OFF_PAGE_LENGTH] = 8; | 
 | 3119 |                 DEBC(printk(ST_DEB_MSG | 
 | 3120 |                             "%s: Formatting tape with two partitions (1 = %d MB).\n", | 
 | 3121 |                             name, size)); | 
 | 3122 | 	} | 
 | 3123 | 	bp[pgo + PP_OFF_PART_UNITS] = 0; | 
 | 3124 | 	bp[pgo + PP_OFF_RESERVED] = 0; | 
 | 3125 | 	bp[pgo + PP_OFF_FLAGS] = PP_BIT_IDP | PP_MSK_PSUM_MB; | 
 | 3126 |  | 
 | 3127 | 	result = write_mode_page(STp, PART_PAGE, 1); | 
 | 3128 | 	if (result) { | 
 | 3129 | 		printk(KERN_INFO "%s: Partitioning of tape failed.\n", name); | 
 | 3130 | 		result = (-EIO); | 
 | 3131 | 	} | 
 | 3132 |  | 
 | 3133 | 	return result; | 
 | 3134 | } | 
 | 3135 |  | 
 | 3136 |  | 
 | 3137 |  | 
 | 3138 | /* The ioctl command */ | 
 | 3139 | static int st_ioctl(struct inode *inode, struct file *file, | 
 | 3140 | 		    unsigned int cmd_in, unsigned long arg) | 
 | 3141 | { | 
 | 3142 | 	int i, cmd_nr, cmd_type, bt; | 
 | 3143 | 	int retval = 0; | 
 | 3144 | 	unsigned int blk; | 
 | 3145 | 	struct scsi_tape *STp = file->private_data; | 
 | 3146 | 	struct st_modedef *STm; | 
 | 3147 | 	struct st_partstat *STps; | 
 | 3148 | 	char *name = tape_name(STp); | 
 | 3149 | 	void __user *p = (void __user *)arg; | 
 | 3150 |  | 
 | 3151 | 	if (down_interruptible(&STp->lock)) | 
 | 3152 | 		return -ERESTARTSYS; | 
 | 3153 |  | 
 | 3154 |         DEB( | 
 | 3155 | 	if (debugging && !STp->in_use) { | 
 | 3156 | 		printk(ST_DEB_MSG "%s: Incorrect device.\n", name); | 
 | 3157 | 		retval = (-EIO); | 
 | 3158 | 		goto out; | 
 | 3159 | 	} ) /* end DEB */ | 
 | 3160 |  | 
 | 3161 | 	STm = &(STp->modes[STp->current_mode]); | 
 | 3162 | 	STps = &(STp->ps[STp->partition]); | 
 | 3163 |  | 
 | 3164 | 	/* | 
 | 3165 | 	 * If we are in the middle of error recovery, don't let anyone | 
 | 3166 | 	 * else try and use this device.  Also, if error recovery fails, it | 
 | 3167 | 	 * may try and take the device offline, in which case all further | 
 | 3168 | 	 * access to the device is prohibited. | 
 | 3169 | 	 */ | 
 | 3170 | 	retval = scsi_nonblockable_ioctl(STp->device, cmd_in, p, file); | 
 | 3171 | 	if (!scsi_block_when_processing_errors(STp->device) || retval != -ENODEV) | 
 | 3172 | 		goto out; | 
 | 3173 | 	retval = 0; | 
 | 3174 |  | 
 | 3175 | 	cmd_type = _IOC_TYPE(cmd_in); | 
 | 3176 | 	cmd_nr = _IOC_NR(cmd_in); | 
 | 3177 |  | 
 | 3178 | 	if (cmd_type == _IOC_TYPE(MTIOCTOP) && cmd_nr == _IOC_NR(MTIOCTOP)) { | 
 | 3179 | 		struct mtop mtc; | 
 | 3180 |  | 
 | 3181 | 		if (_IOC_SIZE(cmd_in) != sizeof(mtc)) { | 
 | 3182 | 			retval = (-EINVAL); | 
 | 3183 | 			goto out; | 
 | 3184 | 		} | 
 | 3185 |  | 
 | 3186 | 		i = copy_from_user(&mtc, p, sizeof(struct mtop)); | 
 | 3187 | 		if (i) { | 
 | 3188 | 			retval = (-EFAULT); | 
 | 3189 | 			goto out; | 
 | 3190 | 		} | 
 | 3191 |  | 
 | 3192 | 		if (mtc.mt_op == MTSETDRVBUFFER && !capable(CAP_SYS_ADMIN)) { | 
 | 3193 | 			printk(KERN_WARNING | 
 | 3194 |                                "%s: MTSETDRVBUFFER only allowed for root.\n", name); | 
 | 3195 | 			retval = (-EPERM); | 
 | 3196 | 			goto out; | 
 | 3197 | 		} | 
 | 3198 | 		if (!STm->defined && | 
 | 3199 | 		    (mtc.mt_op != MTSETDRVBUFFER && | 
 | 3200 | 		     (mtc.mt_count & MT_ST_OPTIONS) == 0)) { | 
 | 3201 | 			retval = (-ENXIO); | 
 | 3202 | 			goto out; | 
 | 3203 | 		} | 
 | 3204 |  | 
 | 3205 | 		if (!STp->pos_unknown) { | 
 | 3206 |  | 
 | 3207 | 			if (STps->eof == ST_FM_HIT) { | 
 | 3208 | 				if (mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM || | 
 | 3209 |                                     mtc.mt_op == MTEOM) { | 
 | 3210 | 					mtc.mt_count -= 1; | 
 | 3211 | 					if (STps->drv_file >= 0) | 
 | 3212 | 						STps->drv_file += 1; | 
 | 3213 | 				} else if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM) { | 
 | 3214 | 					mtc.mt_count += 1; | 
 | 3215 | 					if (STps->drv_file >= 0) | 
 | 3216 | 						STps->drv_file += 1; | 
 | 3217 | 				} | 
 | 3218 | 			} | 
 | 3219 |  | 
 | 3220 | 			if (mtc.mt_op == MTSEEK) { | 
 | 3221 | 				/* Old position must be restored if partition will be | 
 | 3222 |                                    changed */ | 
 | 3223 | 				i = !STp->can_partitions || | 
 | 3224 | 				    (STp->new_partition != STp->partition); | 
 | 3225 | 			} else { | 
 | 3226 | 				i = mtc.mt_op == MTREW || mtc.mt_op == MTOFFL || | 
 | 3227 | 				    mtc.mt_op == MTRETEN || mtc.mt_op == MTEOM || | 
 | 3228 | 				    mtc.mt_op == MTLOCK || mtc.mt_op == MTLOAD || | 
 | 3229 | 				    mtc.mt_op == MTFSF || mtc.mt_op == MTFSFM || | 
 | 3230 | 				    mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM || | 
 | 3231 | 				    mtc.mt_op == MTCOMPRESSION; | 
 | 3232 | 			} | 
 | 3233 | 			i = flush_buffer(STp, i); | 
 | 3234 | 			if (i < 0) { | 
 | 3235 | 				retval = i; | 
 | 3236 | 				goto out; | 
 | 3237 | 			} | 
 | 3238 | 			if (STps->rw == ST_WRITING && | 
 | 3239 | 			    (mtc.mt_op == MTREW || mtc.mt_op == MTOFFL || | 
 | 3240 | 			     mtc.mt_op == MTSEEK || | 
 | 3241 | 			     mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM)) { | 
 | 3242 | 				i = st_int_ioctl(STp, MTWEOF, 1); | 
 | 3243 | 				if (i < 0) { | 
 | 3244 | 					retval = i; | 
 | 3245 | 					goto out; | 
 | 3246 | 				} | 
 | 3247 | 				if (mtc.mt_op == MTBSF || mtc.mt_op == MTBSFM) | 
 | 3248 | 					mtc.mt_count++; | 
 | 3249 | 				STps->rw = ST_IDLE; | 
 | 3250 | 			     } | 
 | 3251 |  | 
 | 3252 | 		} else { | 
 | 3253 | 			/* | 
 | 3254 | 			 * If there was a bus reset, block further access | 
 | 3255 | 			 * to this device.  If the user wants to rewind the tape, | 
 | 3256 | 			 * then reset the flag and allow access again. | 
 | 3257 | 			 */ | 
 | 3258 | 			if (mtc.mt_op != MTREW && | 
 | 3259 | 			    mtc.mt_op != MTOFFL && | 
 | 3260 | 			    mtc.mt_op != MTRETEN && | 
 | 3261 | 			    mtc.mt_op != MTERASE && | 
 | 3262 | 			    mtc.mt_op != MTSEEK && | 
 | 3263 | 			    mtc.mt_op != MTEOM) { | 
 | 3264 | 				retval = (-EIO); | 
 | 3265 | 				goto out; | 
 | 3266 | 			} | 
 | 3267 | 			reset_state(STp); | 
 | 3268 | 			/* remove this when the midlevel properly clears was_reset */ | 
 | 3269 | 			STp->device->was_reset = 0; | 
 | 3270 | 		} | 
 | 3271 |  | 
 | 3272 | 		if (mtc.mt_op != MTNOP && mtc.mt_op != MTSETBLK && | 
 | 3273 | 		    mtc.mt_op != MTSETDENSITY && mtc.mt_op != MTWSM && | 
 | 3274 | 		    mtc.mt_op != MTSETDRVBUFFER && mtc.mt_op != MTSETPART) | 
 | 3275 | 			STps->rw = ST_IDLE;	/* Prevent automatic WEOF and fsf */ | 
 | 3276 |  | 
 | 3277 | 		if (mtc.mt_op == MTOFFL && STp->door_locked != ST_UNLOCKED) | 
 | 3278 | 			do_door_lock(STp, 0);	/* Ignore result! */ | 
 | 3279 |  | 
 | 3280 | 		if (mtc.mt_op == MTSETDRVBUFFER && | 
 | 3281 | 		    (mtc.mt_count & MT_ST_OPTIONS) != 0) { | 
 | 3282 | 			retval = st_set_options(STp, mtc.mt_count); | 
 | 3283 | 			goto out; | 
 | 3284 | 		} | 
 | 3285 |  | 
 | 3286 | 		if (mtc.mt_op == MTSETPART) { | 
 | 3287 | 			if (!STp->can_partitions || | 
 | 3288 | 			    mtc.mt_count < 0 || mtc.mt_count >= ST_NBR_PARTITIONS) { | 
 | 3289 | 				retval = (-EINVAL); | 
 | 3290 | 				goto out; | 
 | 3291 | 			} | 
 | 3292 | 			if (mtc.mt_count >= STp->nbr_partitions && | 
 | 3293 | 			    (STp->nbr_partitions = nbr_partitions(STp)) < 0) { | 
 | 3294 | 				retval = (-EIO); | 
 | 3295 | 				goto out; | 
 | 3296 | 			} | 
 | 3297 | 			if (mtc.mt_count >= STp->nbr_partitions) { | 
 | 3298 | 				retval = (-EINVAL); | 
 | 3299 | 				goto out; | 
 | 3300 | 			} | 
 | 3301 | 			STp->new_partition = mtc.mt_count; | 
 | 3302 | 			retval = 0; | 
 | 3303 | 			goto out; | 
 | 3304 | 		} | 
 | 3305 |  | 
 | 3306 | 		if (mtc.mt_op == MTMKPART) { | 
 | 3307 | 			if (!STp->can_partitions) { | 
 | 3308 | 				retval = (-EINVAL); | 
 | 3309 | 				goto out; | 
 | 3310 | 			} | 
 | 3311 | 			if ((i = st_int_ioctl(STp, MTREW, 0)) < 0 || | 
 | 3312 | 			    (i = partition_tape(STp, mtc.mt_count)) < 0) { | 
 | 3313 | 				retval = i; | 
 | 3314 | 				goto out; | 
 | 3315 | 			} | 
 | 3316 | 			for (i = 0; i < ST_NBR_PARTITIONS; i++) { | 
 | 3317 | 				STp->ps[i].rw = ST_IDLE; | 
 | 3318 | 				STp->ps[i].at_sm = 0; | 
 | 3319 | 				STp->ps[i].last_block_valid = 0; | 
 | 3320 | 			} | 
 | 3321 | 			STp->partition = STp->new_partition = 0; | 
 | 3322 | 			STp->nbr_partitions = 1;	/* Bad guess ?-) */ | 
 | 3323 | 			STps->drv_block = STps->drv_file = 0; | 
 | 3324 | 			retval = 0; | 
 | 3325 | 			goto out; | 
 | 3326 | 		} | 
 | 3327 |  | 
 | 3328 | 		if (mtc.mt_op == MTSEEK) { | 
 | 3329 | 			i = set_location(STp, mtc.mt_count, STp->new_partition, 0); | 
 | 3330 | 			if (!STp->can_partitions) | 
 | 3331 | 				STp->ps[0].rw = ST_IDLE; | 
 | 3332 | 			retval = i; | 
 | 3333 | 			goto out; | 
 | 3334 | 		} | 
 | 3335 |  | 
 | 3336 | 		if (mtc.mt_op == MTUNLOAD || mtc.mt_op == MTOFFL) { | 
 | 3337 | 			retval = do_load_unload(STp, file, 0); | 
 | 3338 | 			goto out; | 
 | 3339 | 		} | 
 | 3340 |  | 
 | 3341 | 		if (mtc.mt_op == MTLOAD) { | 
 | 3342 | 			retval = do_load_unload(STp, file, max(1, mtc.mt_count)); | 
 | 3343 | 			goto out; | 
 | 3344 | 		} | 
 | 3345 |  | 
 | 3346 | 		if (mtc.mt_op == MTLOCK || mtc.mt_op == MTUNLOCK) { | 
 | 3347 | 			retval = do_door_lock(STp, (mtc.mt_op == MTLOCK)); | 
 | 3348 | 			goto out; | 
 | 3349 | 		} | 
 | 3350 |  | 
 | 3351 | 		if (STp->can_partitions && STp->ready == ST_READY && | 
 | 3352 | 		    (i = switch_partition(STp)) < 0) { | 
 | 3353 | 			retval = i; | 
 | 3354 | 			goto out; | 
 | 3355 | 		} | 
 | 3356 |  | 
 | 3357 | 		if (mtc.mt_op == MTCOMPRESSION) | 
 | 3358 | 			retval = st_compression(STp, (mtc.mt_count & 1)); | 
 | 3359 | 		else | 
 | 3360 | 			retval = st_int_ioctl(STp, mtc.mt_op, mtc.mt_count); | 
 | 3361 | 		goto out; | 
 | 3362 | 	} | 
 | 3363 | 	if (!STm->defined) { | 
 | 3364 | 		retval = (-ENXIO); | 
 | 3365 | 		goto out; | 
 | 3366 | 	} | 
 | 3367 |  | 
 | 3368 | 	if ((i = flush_buffer(STp, 0)) < 0) { | 
 | 3369 | 		retval = i; | 
 | 3370 | 		goto out; | 
 | 3371 | 	} | 
 | 3372 | 	if (STp->can_partitions && | 
 | 3373 | 	    (i = switch_partition(STp)) < 0) { | 
 | 3374 | 		retval = i; | 
 | 3375 | 		goto out; | 
 | 3376 | 	} | 
 | 3377 |  | 
 | 3378 | 	if (cmd_type == _IOC_TYPE(MTIOCGET) && cmd_nr == _IOC_NR(MTIOCGET)) { | 
 | 3379 | 		struct mtget mt_status; | 
 | 3380 |  | 
 | 3381 | 		if (_IOC_SIZE(cmd_in) != sizeof(struct mtget)) { | 
 | 3382 | 			 retval = (-EINVAL); | 
 | 3383 | 			 goto out; | 
 | 3384 | 		} | 
 | 3385 |  | 
 | 3386 | 		mt_status.mt_type = STp->tape_type; | 
 | 3387 | 		mt_status.mt_dsreg = | 
 | 3388 | 		    ((STp->block_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK) | | 
 | 3389 | 		    ((STp->density << MT_ST_DENSITY_SHIFT) & MT_ST_DENSITY_MASK); | 
 | 3390 | 		mt_status.mt_blkno = STps->drv_block; | 
 | 3391 | 		mt_status.mt_fileno = STps->drv_file; | 
 | 3392 | 		if (STp->block_size != 0) { | 
 | 3393 | 			if (STps->rw == ST_WRITING) | 
 | 3394 | 				mt_status.mt_blkno += | 
 | 3395 | 				    (STp->buffer)->buffer_bytes / STp->block_size; | 
 | 3396 | 			else if (STps->rw == ST_READING) | 
 | 3397 | 				mt_status.mt_blkno -= | 
 | 3398 |                                         ((STp->buffer)->buffer_bytes + | 
 | 3399 |                                          STp->block_size - 1) / STp->block_size; | 
 | 3400 | 		} | 
 | 3401 |  | 
 | 3402 | 		mt_status.mt_gstat = 0; | 
 | 3403 | 		if (STp->drv_write_prot) | 
 | 3404 | 			mt_status.mt_gstat |= GMT_WR_PROT(0xffffffff); | 
 | 3405 | 		if (mt_status.mt_blkno == 0) { | 
 | 3406 | 			if (mt_status.mt_fileno == 0) | 
 | 3407 | 				mt_status.mt_gstat |= GMT_BOT(0xffffffff); | 
 | 3408 | 			else | 
 | 3409 | 				mt_status.mt_gstat |= GMT_EOF(0xffffffff); | 
 | 3410 | 		} | 
 | 3411 | 		mt_status.mt_erreg = (STp->recover_reg << MT_ST_SOFTERR_SHIFT); | 
 | 3412 | 		mt_status.mt_resid = STp->partition; | 
 | 3413 | 		if (STps->eof == ST_EOM_OK || STps->eof == ST_EOM_ERROR) | 
 | 3414 | 			mt_status.mt_gstat |= GMT_EOT(0xffffffff); | 
 | 3415 | 		else if (STps->eof >= ST_EOM_OK) | 
 | 3416 | 			mt_status.mt_gstat |= GMT_EOD(0xffffffff); | 
 | 3417 | 		if (STp->density == 1) | 
 | 3418 | 			mt_status.mt_gstat |= GMT_D_800(0xffffffff); | 
 | 3419 | 		else if (STp->density == 2) | 
 | 3420 | 			mt_status.mt_gstat |= GMT_D_1600(0xffffffff); | 
 | 3421 | 		else if (STp->density == 3) | 
 | 3422 | 			mt_status.mt_gstat |= GMT_D_6250(0xffffffff); | 
 | 3423 | 		if (STp->ready == ST_READY) | 
 | 3424 | 			mt_status.mt_gstat |= GMT_ONLINE(0xffffffff); | 
 | 3425 | 		if (STp->ready == ST_NO_TAPE) | 
 | 3426 | 			mt_status.mt_gstat |= GMT_DR_OPEN(0xffffffff); | 
 | 3427 | 		if (STps->at_sm) | 
 | 3428 | 			mt_status.mt_gstat |= GMT_SM(0xffffffff); | 
 | 3429 | 		if (STm->do_async_writes || | 
 | 3430 |                     (STm->do_buffer_writes && STp->block_size != 0) || | 
 | 3431 | 		    STp->drv_buffer != 0) | 
 | 3432 | 			mt_status.mt_gstat |= GMT_IM_REP_EN(0xffffffff); | 
 | 3433 | 		if (STp->cleaning_req) | 
 | 3434 | 			mt_status.mt_gstat |= GMT_CLN(0xffffffff); | 
 | 3435 |  | 
 | 3436 | 		i = copy_to_user(p, &mt_status, sizeof(struct mtget)); | 
 | 3437 | 		if (i) { | 
 | 3438 | 			retval = (-EFAULT); | 
 | 3439 | 			goto out; | 
 | 3440 | 		} | 
 | 3441 |  | 
 | 3442 | 		STp->recover_reg = 0;		/* Clear after read */ | 
 | 3443 | 		retval = 0; | 
 | 3444 | 		goto out; | 
 | 3445 | 	}			/* End of MTIOCGET */ | 
 | 3446 | 	if (cmd_type == _IOC_TYPE(MTIOCPOS) && cmd_nr == _IOC_NR(MTIOCPOS)) { | 
 | 3447 | 		struct mtpos mt_pos; | 
 | 3448 | 		if (_IOC_SIZE(cmd_in) != sizeof(struct mtpos)) { | 
 | 3449 | 			 retval = (-EINVAL); | 
 | 3450 | 			 goto out; | 
 | 3451 | 		} | 
 | 3452 | 		if ((i = get_location(STp, &blk, &bt, 0)) < 0) { | 
 | 3453 | 			retval = i; | 
 | 3454 | 			goto out; | 
 | 3455 | 		} | 
 | 3456 | 		mt_pos.mt_blkno = blk; | 
 | 3457 | 		i = copy_to_user(p, &mt_pos, sizeof(struct mtpos)); | 
 | 3458 | 		if (i) | 
 | 3459 | 			retval = (-EFAULT); | 
 | 3460 | 		goto out; | 
 | 3461 | 	} | 
 | 3462 | 	up(&STp->lock); | 
 | 3463 | 	switch (cmd_in) { | 
 | 3464 | 		case SCSI_IOCTL_GET_IDLUN: | 
 | 3465 | 		case SCSI_IOCTL_GET_BUS_NUMBER: | 
 | 3466 | 			break; | 
 | 3467 | 		default: | 
| Kai Makisara  | 16c4b3e | 2005-05-01 18:11:55 +0300 | [diff] [blame] | 3468 | 			if ((cmd_in == SG_IO || | 
 | 3469 | 			     cmd_in == SCSI_IOCTL_SEND_COMMAND || | 
 | 3470 | 			     cmd_in == CDROM_SEND_PACKET) && | 
 | 3471 | 			    !capable(CAP_SYS_RAWIO)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3472 | 				i = -EPERM; | 
 | 3473 | 			else | 
 | 3474 | 				i = scsi_cmd_ioctl(file, STp->disk, cmd_in, p); | 
 | 3475 | 			if (i != -ENOTTY) | 
 | 3476 | 				return i; | 
 | 3477 | 			break; | 
 | 3478 | 	} | 
| Kai Makisara  | 16c4b3e | 2005-05-01 18:11:55 +0300 | [diff] [blame] | 3479 | 	retval = scsi_ioctl(STp->device, cmd_in, p); | 
 | 3480 | 	if (!retval && cmd_in == SCSI_IOCTL_STOP_UNIT) { /* unload */ | 
 | 3481 | 		STp->rew_at_close = 0; | 
 | 3482 | 		STp->ready = ST_NO_TAPE; | 
 | 3483 | 	} | 
 | 3484 | 	return retval; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3485 |  | 
 | 3486 |  out: | 
 | 3487 | 	up(&STp->lock); | 
 | 3488 | 	return retval; | 
 | 3489 | } | 
 | 3490 |  | 
 | 3491 | #ifdef CONFIG_COMPAT | 
 | 3492 | static long st_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | 
 | 3493 | { | 
 | 3494 | 	struct scsi_tape *STp = file->private_data; | 
 | 3495 | 	struct scsi_device *sdev = STp->device; | 
 | 3496 | 	int ret = -ENOIOCTLCMD; | 
 | 3497 | 	if (sdev->host->hostt->compat_ioctl) {  | 
 | 3498 |  | 
 | 3499 | 		ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg); | 
 | 3500 |  | 
 | 3501 | 	} | 
 | 3502 | 	return ret; | 
 | 3503 | } | 
 | 3504 | #endif | 
 | 3505 |  | 
 | 3506 |  | 
 | 3507 |  | 
 | 3508 | /* Try to allocate a new tape buffer. Calling function must not hold | 
 | 3509 |    dev_arr_lock. */ | 
 | 3510 | static struct st_buffer * | 
 | 3511 |  new_tape_buffer(int from_initialization, int need_dma, int max_sg) | 
 | 3512 | { | 
 | 3513 | 	int i, priority, got = 0, segs = 0; | 
 | 3514 | 	struct st_buffer *tb; | 
 | 3515 |  | 
 | 3516 | 	if (from_initialization) | 
 | 3517 | 		priority = GFP_ATOMIC; | 
 | 3518 | 	else | 
 | 3519 | 		priority = GFP_KERNEL; | 
 | 3520 |  | 
 | 3521 | 	i = sizeof(struct st_buffer) + (max_sg - 1) * sizeof(struct scatterlist) + | 
 | 3522 | 		max_sg * sizeof(struct st_buf_fragment); | 
 | 3523 | 	tb = kmalloc(i, priority); | 
 | 3524 | 	if (!tb) { | 
 | 3525 | 		printk(KERN_NOTICE "st: Can't allocate new tape buffer.\n"); | 
 | 3526 | 		return NULL; | 
 | 3527 | 	} | 
 | 3528 | 	memset(tb, 0, i); | 
 | 3529 | 	tb->frp_segs = tb->orig_frp_segs = segs; | 
 | 3530 | 	tb->use_sg = max_sg; | 
 | 3531 | 	if (segs > 0) | 
 | 3532 | 		tb->b_data = page_address(tb->sg[0].page); | 
 | 3533 | 	tb->frp = (struct st_buf_fragment *)(&(tb->sg[0]) + max_sg); | 
 | 3534 |  | 
 | 3535 | 	tb->in_use = 1; | 
 | 3536 | 	tb->dma = need_dma; | 
 | 3537 | 	tb->buffer_size = got; | 
 | 3538 |  | 
 | 3539 | 	return tb; | 
 | 3540 | } | 
 | 3541 |  | 
 | 3542 |  | 
 | 3543 | /* Try to allocate enough space in the tape buffer */ | 
 | 3544 | static int enlarge_buffer(struct st_buffer * STbuffer, int new_size, int need_dma) | 
 | 3545 | { | 
 | 3546 | 	int segs, nbr, max_segs, b_size, priority, order, got; | 
 | 3547 |  | 
 | 3548 | 	if (new_size <= STbuffer->buffer_size) | 
 | 3549 | 		return 1; | 
 | 3550 |  | 
 | 3551 | 	if (STbuffer->buffer_size <= PAGE_SIZE) | 
 | 3552 | 		normalize_buffer(STbuffer);  /* Avoid extra segment */ | 
 | 3553 |  | 
 | 3554 | 	max_segs = STbuffer->use_sg; | 
 | 3555 | 	nbr = max_segs - STbuffer->frp_segs; | 
 | 3556 | 	if (nbr <= 0) | 
 | 3557 | 		return 0; | 
 | 3558 |  | 
 | 3559 | 	priority = GFP_KERNEL | __GFP_NOWARN; | 
 | 3560 | 	if (need_dma) | 
 | 3561 | 		priority |= GFP_DMA; | 
 | 3562 | 	for (b_size = PAGE_SIZE, order=0; | 
 | 3563 | 	     b_size < new_size - STbuffer->buffer_size; | 
 | 3564 | 	     order++, b_size *= 2) | 
 | 3565 | 		;  /* empty */ | 
 | 3566 |  | 
 | 3567 | 	for (segs = STbuffer->frp_segs, got = STbuffer->buffer_size; | 
 | 3568 | 	     segs < max_segs && got < new_size;) { | 
 | 3569 | 		STbuffer->frp[segs].page = alloc_pages(priority, order); | 
 | 3570 | 		if (STbuffer->frp[segs].page == NULL) { | 
 | 3571 | 			if (new_size - got <= (max_segs - segs) * b_size / 2) { | 
 | 3572 | 				b_size /= 2; /* Large enough for the rest of the buffers */ | 
 | 3573 | 				order--; | 
 | 3574 | 				continue; | 
 | 3575 | 			} | 
 | 3576 | 			DEB(STbuffer->buffer_size = got); | 
 | 3577 | 			normalize_buffer(STbuffer); | 
 | 3578 | 			return 0; | 
 | 3579 | 		} | 
 | 3580 | 		STbuffer->frp[segs].length = b_size; | 
 | 3581 | 		STbuffer->frp_segs += 1; | 
 | 3582 | 		got += b_size; | 
 | 3583 | 		STbuffer->buffer_size = got; | 
 | 3584 | 		segs++; | 
 | 3585 | 	} | 
 | 3586 | 	STbuffer->b_data = page_address(STbuffer->frp[0].page); | 
 | 3587 |  | 
 | 3588 | 	return 1; | 
 | 3589 | } | 
 | 3590 |  | 
 | 3591 |  | 
 | 3592 | /* Release the extra buffer */ | 
 | 3593 | static void normalize_buffer(struct st_buffer * STbuffer) | 
 | 3594 | { | 
 | 3595 | 	int i, order; | 
 | 3596 |  | 
 | 3597 | 	for (i = STbuffer->orig_frp_segs; i < STbuffer->frp_segs; i++) { | 
 | 3598 | 		order = get_order(STbuffer->frp[i].length); | 
 | 3599 | 		__free_pages(STbuffer->frp[i].page, order); | 
 | 3600 | 		STbuffer->buffer_size -= STbuffer->frp[i].length; | 
 | 3601 | 	} | 
 | 3602 | 	STbuffer->frp_segs = STbuffer->orig_frp_segs; | 
 | 3603 | 	STbuffer->frp_sg_current = 0; | 
 | 3604 | } | 
 | 3605 |  | 
 | 3606 |  | 
 | 3607 | /* Move data from the user buffer to the tape buffer. Returns zero (success) or | 
 | 3608 |    negative error code. */ | 
 | 3609 | static int append_to_buffer(const char __user *ubp, struct st_buffer * st_bp, int do_count) | 
 | 3610 | { | 
 | 3611 | 	int i, cnt, res, offset; | 
 | 3612 |  | 
 | 3613 | 	for (i = 0, offset = st_bp->buffer_bytes; | 
 | 3614 | 	     i < st_bp->frp_segs && offset >= st_bp->frp[i].length; i++) | 
 | 3615 | 		offset -= st_bp->frp[i].length; | 
 | 3616 | 	if (i == st_bp->frp_segs) {	/* Should never happen */ | 
 | 3617 | 		printk(KERN_WARNING "st: append_to_buffer offset overflow.\n"); | 
 | 3618 | 		return (-EIO); | 
 | 3619 | 	} | 
 | 3620 | 	for (; i < st_bp->frp_segs && do_count > 0; i++) { | 
 | 3621 | 		cnt = st_bp->frp[i].length - offset < do_count ? | 
 | 3622 | 		    st_bp->frp[i].length - offset : do_count; | 
 | 3623 | 		res = copy_from_user(page_address(st_bp->frp[i].page) + offset, ubp, cnt); | 
 | 3624 | 		if (res) | 
 | 3625 | 			return (-EFAULT); | 
 | 3626 | 		do_count -= cnt; | 
 | 3627 | 		st_bp->buffer_bytes += cnt; | 
 | 3628 | 		ubp += cnt; | 
 | 3629 | 		offset = 0; | 
 | 3630 | 	} | 
 | 3631 | 	if (do_count) /* Should never happen */ | 
 | 3632 | 		return (-EIO); | 
 | 3633 |  | 
 | 3634 | 	return 0; | 
 | 3635 | } | 
 | 3636 |  | 
 | 3637 |  | 
 | 3638 | /* Move data from the tape buffer to the user buffer. Returns zero (success) or | 
 | 3639 |    negative error code. */ | 
 | 3640 | static int from_buffer(struct st_buffer * st_bp, char __user *ubp, int do_count) | 
 | 3641 | { | 
 | 3642 | 	int i, cnt, res, offset; | 
 | 3643 |  | 
 | 3644 | 	for (i = 0, offset = st_bp->read_pointer; | 
 | 3645 | 	     i < st_bp->frp_segs && offset >= st_bp->frp[i].length; i++) | 
 | 3646 | 		offset -= st_bp->frp[i].length; | 
 | 3647 | 	if (i == st_bp->frp_segs) {	/* Should never happen */ | 
 | 3648 | 		printk(KERN_WARNING "st: from_buffer offset overflow.\n"); | 
 | 3649 | 		return (-EIO); | 
 | 3650 | 	} | 
 | 3651 | 	for (; i < st_bp->frp_segs && do_count > 0; i++) { | 
 | 3652 | 		cnt = st_bp->frp[i].length - offset < do_count ? | 
 | 3653 | 		    st_bp->frp[i].length - offset : do_count; | 
 | 3654 | 		res = copy_to_user(ubp, page_address(st_bp->frp[i].page) + offset, cnt); | 
 | 3655 | 		if (res) | 
 | 3656 | 			return (-EFAULT); | 
 | 3657 | 		do_count -= cnt; | 
 | 3658 | 		st_bp->buffer_bytes -= cnt; | 
 | 3659 | 		st_bp->read_pointer += cnt; | 
 | 3660 | 		ubp += cnt; | 
 | 3661 | 		offset = 0; | 
 | 3662 | 	} | 
 | 3663 | 	if (do_count) /* Should never happen */ | 
 | 3664 | 		return (-EIO); | 
 | 3665 |  | 
 | 3666 | 	return 0; | 
 | 3667 | } | 
 | 3668 |  | 
 | 3669 |  | 
 | 3670 | /* Move data towards start of buffer */ | 
 | 3671 | static void move_buffer_data(struct st_buffer * st_bp, int offset) | 
 | 3672 | { | 
 | 3673 | 	int src_seg, dst_seg, src_offset = 0, dst_offset; | 
 | 3674 | 	int count, total; | 
 | 3675 |  | 
 | 3676 | 	if (offset == 0) | 
 | 3677 | 		return; | 
 | 3678 |  | 
 | 3679 | 	total=st_bp->buffer_bytes - offset; | 
 | 3680 | 	for (src_seg=0; src_seg < st_bp->frp_segs; src_seg++) { | 
 | 3681 | 		src_offset = offset; | 
 | 3682 | 		if (src_offset < st_bp->frp[src_seg].length) | 
 | 3683 | 			break; | 
 | 3684 | 		offset -= st_bp->frp[src_seg].length; | 
 | 3685 | 	} | 
 | 3686 |  | 
 | 3687 | 	st_bp->buffer_bytes = st_bp->read_pointer = total; | 
 | 3688 | 	for (dst_seg=dst_offset=0; total > 0; ) { | 
 | 3689 | 		count = min(st_bp->frp[dst_seg].length - dst_offset, | 
 | 3690 | 			    st_bp->frp[src_seg].length - src_offset); | 
 | 3691 | 		memmove(page_address(st_bp->frp[dst_seg].page) + dst_offset, | 
 | 3692 | 			page_address(st_bp->frp[src_seg].page) + src_offset, count); | 
 | 3693 | 		src_offset += count; | 
 | 3694 | 		if (src_offset >= st_bp->frp[src_seg].length) { | 
 | 3695 | 			src_seg++; | 
 | 3696 | 			src_offset = 0; | 
 | 3697 | 		} | 
 | 3698 | 		dst_offset += count; | 
 | 3699 | 		if (dst_offset >= st_bp->frp[dst_seg].length) { | 
 | 3700 | 			dst_seg++; | 
 | 3701 | 			dst_offset = 0; | 
 | 3702 | 		} | 
 | 3703 | 		total -= count; | 
 | 3704 | 	} | 
 | 3705 | } | 
 | 3706 |  | 
 | 3707 |  | 
 | 3708 | /* Fill the s/g list up to the length required for this transfer */ | 
 | 3709 | static void buf_to_sg(struct st_buffer *STbp, unsigned int length) | 
 | 3710 | { | 
 | 3711 | 	int i; | 
 | 3712 | 	unsigned int count; | 
 | 3713 | 	struct scatterlist *sg; | 
 | 3714 | 	struct st_buf_fragment *frp; | 
 | 3715 |  | 
 | 3716 | 	if (length == STbp->frp_sg_current) | 
 | 3717 | 		return;   /* work already done */ | 
 | 3718 |  | 
 | 3719 | 	sg = &(STbp->sg[0]); | 
 | 3720 | 	frp = STbp->frp; | 
 | 3721 | 	for (i=count=0; count < length; i++) { | 
 | 3722 | 		sg[i].page = frp[i].page; | 
 | 3723 | 		if (length - count > frp[i].length) | 
 | 3724 | 			sg[i].length = frp[i].length; | 
 | 3725 | 		else | 
 | 3726 | 			sg[i].length = length - count; | 
 | 3727 | 		count += sg[i].length; | 
 | 3728 | 		sg[i].offset = 0; | 
 | 3729 | 	} | 
 | 3730 | 	STbp->sg_segs = i; | 
 | 3731 | 	STbp->frp_sg_current = length; | 
 | 3732 | } | 
 | 3733 |  | 
 | 3734 |  | 
 | 3735 | /* Validate the options from command line or module parameters */ | 
 | 3736 | static void validate_options(void) | 
 | 3737 | { | 
 | 3738 | 	if (buffer_kbs > 0) | 
 | 3739 | 		st_fixed_buffer_size = buffer_kbs * ST_KILOBYTE; | 
 | 3740 | 	if (max_sg_segs >= ST_FIRST_SG) | 
 | 3741 | 		st_max_sg_segs = max_sg_segs; | 
 | 3742 | } | 
 | 3743 |  | 
 | 3744 | #ifndef MODULE | 
 | 3745 | /* Set the boot options. Syntax is defined in Documenation/scsi/st.txt. | 
 | 3746 |  */ | 
 | 3747 | static int __init st_setup(char *str) | 
 | 3748 | { | 
 | 3749 | 	int i, len, ints[5]; | 
 | 3750 | 	char *stp; | 
 | 3751 |  | 
 | 3752 | 	stp = get_options(str, ARRAY_SIZE(ints), ints); | 
 | 3753 |  | 
 | 3754 | 	if (ints[0] > 0) { | 
 | 3755 | 		for (i = 0; i < ints[0] && i < ARRAY_SIZE(parms); i++) | 
 | 3756 | 			if (parms[i].val) | 
 | 3757 | 				*parms[i].val = ints[i + 1]; | 
 | 3758 | 	} else { | 
 | 3759 | 		while (stp != NULL) { | 
 | 3760 | 			for (i = 0; i < ARRAY_SIZE(parms); i++) { | 
 | 3761 | 				len = strlen(parms[i].name); | 
 | 3762 | 				if (!strncmp(stp, parms[i].name, len) && | 
 | 3763 | 				    (*(stp + len) == ':' || *(stp + len) == '=')) { | 
 | 3764 | 					if (parms[i].val) | 
 | 3765 | 						*parms[i].val = | 
 | 3766 | 							simple_strtoul(stp + len + 1, NULL, 0); | 
 | 3767 | 					else | 
 | 3768 | 						printk(KERN_WARNING "st: Obsolete parameter %s\n", | 
 | 3769 | 						       parms[i].name); | 
 | 3770 | 					break; | 
 | 3771 | 				} | 
 | 3772 | 			} | 
 | 3773 | 			if (i >= sizeof(parms) / sizeof(struct st_dev_parm)) | 
 | 3774 | 				 printk(KERN_WARNING "st: invalid parameter in '%s'\n", | 
 | 3775 | 					stp); | 
 | 3776 | 			stp = strchr(stp, ','); | 
 | 3777 | 			if (stp) | 
 | 3778 | 				stp++; | 
 | 3779 | 		} | 
 | 3780 | 	} | 
 | 3781 |  | 
 | 3782 | 	validate_options(); | 
 | 3783 |  | 
 | 3784 | 	return 1; | 
 | 3785 | } | 
 | 3786 |  | 
 | 3787 | __setup("st=", st_setup); | 
 | 3788 |  | 
 | 3789 | #endif | 
 | 3790 |  | 
 | 3791 | static struct file_operations st_fops = | 
 | 3792 | { | 
 | 3793 | 	.owner =	THIS_MODULE, | 
 | 3794 | 	.read =		st_read, | 
 | 3795 | 	.write =	st_write, | 
 | 3796 | 	.ioctl =	st_ioctl, | 
 | 3797 | #ifdef CONFIG_COMPAT | 
 | 3798 | 	.compat_ioctl = st_compat_ioctl, | 
 | 3799 | #endif | 
 | 3800 | 	.open =		st_open, | 
 | 3801 | 	.flush =	st_flush, | 
 | 3802 | 	.release =	st_release, | 
 | 3803 | }; | 
 | 3804 |  | 
 | 3805 | static int st_probe(struct device *dev) | 
 | 3806 | { | 
 | 3807 | 	struct scsi_device *SDp = to_scsi_device(dev); | 
 | 3808 | 	struct gendisk *disk = NULL; | 
 | 3809 | 	struct cdev *cdev = NULL; | 
 | 3810 | 	struct scsi_tape *tpnt = NULL; | 
 | 3811 | 	struct st_modedef *STm; | 
 | 3812 | 	struct st_partstat *STps; | 
 | 3813 | 	struct st_buffer *buffer; | 
 | 3814 | 	int i, j, mode, dev_num, error; | 
 | 3815 | 	char *stp; | 
 | 3816 | 	u64 bounce_limit; | 
 | 3817 |  | 
 | 3818 | 	if (SDp->type != TYPE_TAPE) | 
 | 3819 | 		return -ENODEV; | 
 | 3820 | 	if ((stp = st_incompatible(SDp))) { | 
 | 3821 | 		printk(KERN_INFO | 
 | 3822 | 		       "st: Found incompatible tape at scsi%d, channel %d, id %d, lun %d\n", | 
 | 3823 | 		       SDp->host->host_no, SDp->channel, SDp->id, SDp->lun); | 
 | 3824 | 		printk(KERN_INFO "st: The suggested driver is %s.\n", stp); | 
 | 3825 | 		return -ENODEV; | 
 | 3826 | 	} | 
 | 3827 |  | 
 | 3828 | 	i = SDp->host->sg_tablesize; | 
 | 3829 | 	if (st_max_sg_segs < i) | 
 | 3830 | 		i = st_max_sg_segs; | 
 | 3831 | 	buffer = new_tape_buffer(1, (SDp->host)->unchecked_isa_dma, i); | 
 | 3832 | 	if (buffer == NULL) { | 
 | 3833 | 		printk(KERN_ERR | 
 | 3834 | 		       "st: Can't allocate new tape buffer. Device not attached.\n"); | 
 | 3835 | 		goto out; | 
 | 3836 | 	} | 
 | 3837 |  | 
 | 3838 | 	disk = alloc_disk(1); | 
 | 3839 | 	if (!disk) { | 
 | 3840 | 		printk(KERN_ERR "st: out of memory. Device not attached.\n"); | 
 | 3841 | 		goto out_buffer_free; | 
 | 3842 | 	} | 
 | 3843 |  | 
 | 3844 | 	write_lock(&st_dev_arr_lock); | 
 | 3845 | 	if (st_nr_dev >= st_dev_max) { | 
 | 3846 | 		struct scsi_tape **tmp_da; | 
 | 3847 | 		int tmp_dev_max; | 
 | 3848 |  | 
 | 3849 | 		tmp_dev_max = max(st_nr_dev * 2, 8); | 
 | 3850 | 		if (tmp_dev_max > ST_MAX_TAPES) | 
 | 3851 | 			tmp_dev_max = ST_MAX_TAPES; | 
 | 3852 | 		if (tmp_dev_max <= st_nr_dev) { | 
 | 3853 | 			write_unlock(&st_dev_arr_lock); | 
 | 3854 | 			printk(KERN_ERR "st: Too many tape devices (max. %d).\n", | 
 | 3855 | 			       ST_MAX_TAPES); | 
 | 3856 | 			goto out_put_disk; | 
 | 3857 | 		} | 
 | 3858 |  | 
 | 3859 | 		tmp_da = kmalloc(tmp_dev_max * sizeof(struct scsi_tape *), GFP_ATOMIC); | 
 | 3860 | 		if (tmp_da == NULL) { | 
 | 3861 | 			write_unlock(&st_dev_arr_lock); | 
 | 3862 | 			printk(KERN_ERR "st: Can't extend device array.\n"); | 
 | 3863 | 			goto out_put_disk; | 
 | 3864 | 		} | 
 | 3865 |  | 
 | 3866 | 		memset(tmp_da, 0, tmp_dev_max * sizeof(struct scsi_tape *)); | 
 | 3867 | 		if (scsi_tapes != NULL) { | 
 | 3868 | 			memcpy(tmp_da, scsi_tapes, | 
 | 3869 | 			       st_dev_max * sizeof(struct scsi_tape *)); | 
 | 3870 | 			kfree(scsi_tapes); | 
 | 3871 | 		} | 
 | 3872 | 		scsi_tapes = tmp_da; | 
 | 3873 |  | 
 | 3874 | 		st_dev_max = tmp_dev_max; | 
 | 3875 | 	} | 
 | 3876 |  | 
 | 3877 | 	for (i = 0; i < st_dev_max; i++) | 
 | 3878 | 		if (scsi_tapes[i] == NULL) | 
 | 3879 | 			break; | 
 | 3880 | 	if (i >= st_dev_max) | 
 | 3881 | 		panic("scsi_devices corrupt (st)"); | 
 | 3882 |  | 
 | 3883 | 	tpnt = kmalloc(sizeof(struct scsi_tape), GFP_ATOMIC); | 
 | 3884 | 	if (tpnt == NULL) { | 
 | 3885 | 		write_unlock(&st_dev_arr_lock); | 
 | 3886 | 		printk(KERN_ERR "st: Can't allocate device descriptor.\n"); | 
 | 3887 | 		goto out_put_disk; | 
 | 3888 | 	} | 
 | 3889 | 	memset(tpnt, 0, sizeof(struct scsi_tape)); | 
 | 3890 | 	tpnt->disk = disk; | 
 | 3891 | 	sprintf(disk->disk_name, "st%d", i); | 
 | 3892 | 	disk->private_data = &tpnt->driver; | 
 | 3893 | 	disk->queue = SDp->request_queue; | 
 | 3894 | 	tpnt->driver = &st_template; | 
 | 3895 | 	scsi_tapes[i] = tpnt; | 
 | 3896 | 	dev_num = i; | 
 | 3897 |  | 
 | 3898 | 	tpnt->device = SDp; | 
 | 3899 | 	if (SDp->scsi_level <= 2) | 
 | 3900 | 		tpnt->tape_type = MT_ISSCSI1; | 
 | 3901 | 	else | 
 | 3902 | 		tpnt->tape_type = MT_ISSCSI2; | 
 | 3903 |  | 
 | 3904 | 	tpnt->buffer = buffer; | 
 | 3905 |  | 
 | 3906 | 	tpnt->inited = 0; | 
 | 3907 | 	tpnt->dirty = 0; | 
 | 3908 | 	tpnt->in_use = 0; | 
 | 3909 | 	tpnt->drv_buffer = 1;	/* Try buffering if no mode sense */ | 
 | 3910 | 	tpnt->restr_dma = (SDp->host)->unchecked_isa_dma; | 
 | 3911 | 	tpnt->use_pf = (SDp->scsi_level >= SCSI_2); | 
 | 3912 | 	tpnt->density = 0; | 
 | 3913 | 	tpnt->do_auto_lock = ST_AUTO_LOCK; | 
 | 3914 | 	tpnt->can_bsr = (SDp->scsi_level > 2 ? 1 : ST_IN_FILE_POS); /* BSR mandatory in SCSI3 */ | 
 | 3915 | 	tpnt->can_partitions = 0; | 
 | 3916 | 	tpnt->two_fm = ST_TWO_FM; | 
 | 3917 | 	tpnt->fast_mteom = ST_FAST_MTEOM; | 
 | 3918 | 	tpnt->scsi2_logical = ST_SCSI2LOGICAL; | 
 | 3919 | 	tpnt->immediate = ST_NOWAIT; | 
 | 3920 | 	tpnt->default_drvbuffer = 0xff;		/* No forced buffering */ | 
 | 3921 | 	tpnt->partition = 0; | 
 | 3922 | 	tpnt->new_partition = 0; | 
 | 3923 | 	tpnt->nbr_partitions = 0; | 
 | 3924 | 	tpnt->device->timeout = ST_TIMEOUT; | 
 | 3925 | 	tpnt->long_timeout = ST_LONG_TIMEOUT; | 
 | 3926 | 	tpnt->try_dio = try_direct_io && !SDp->host->unchecked_isa_dma; | 
 | 3927 |  | 
 | 3928 | 	bounce_limit = scsi_calculate_bounce_limit(SDp->host) >> PAGE_SHIFT; | 
 | 3929 | 	if (bounce_limit > ULONG_MAX) | 
 | 3930 | 		bounce_limit = ULONG_MAX; | 
 | 3931 | 	tpnt->max_pfn = bounce_limit; | 
 | 3932 |  | 
 | 3933 | 	for (i = 0; i < ST_NBR_MODES; i++) { | 
 | 3934 | 		STm = &(tpnt->modes[i]); | 
 | 3935 | 		STm->defined = 0; | 
 | 3936 | 		STm->sysv = ST_SYSV; | 
 | 3937 | 		STm->defaults_for_writes = 0; | 
 | 3938 | 		STm->do_async_writes = ST_ASYNC_WRITES; | 
 | 3939 | 		STm->do_buffer_writes = ST_BUFFER_WRITES; | 
 | 3940 | 		STm->do_read_ahead = ST_READ_AHEAD; | 
 | 3941 | 		STm->default_compression = ST_DONT_TOUCH; | 
 | 3942 | 		STm->default_blksize = (-1);	/* No forced size */ | 
 | 3943 | 		STm->default_density = (-1);	/* No forced density */ | 
 | 3944 | 	} | 
 | 3945 |  | 
 | 3946 | 	for (i = 0; i < ST_NBR_PARTITIONS; i++) { | 
 | 3947 | 		STps = &(tpnt->ps[i]); | 
 | 3948 | 		STps->rw = ST_IDLE; | 
 | 3949 | 		STps->eof = ST_NOEOF; | 
 | 3950 | 		STps->at_sm = 0; | 
 | 3951 | 		STps->last_block_valid = 0; | 
 | 3952 | 		STps->drv_block = (-1); | 
 | 3953 | 		STps->drv_file = (-1); | 
 | 3954 | 	} | 
 | 3955 |  | 
 | 3956 | 	tpnt->current_mode = 0; | 
 | 3957 | 	tpnt->modes[0].defined = 1; | 
 | 3958 |  | 
 | 3959 | 	tpnt->density_changed = tpnt->compression_changed = | 
 | 3960 | 	    tpnt->blksize_changed = 0; | 
 | 3961 | 	init_MUTEX(&tpnt->lock); | 
 | 3962 |  | 
 | 3963 | 	st_nr_dev++; | 
 | 3964 | 	write_unlock(&st_dev_arr_lock); | 
 | 3965 |  | 
 | 3966 | 	for (mode = 0; mode < ST_NBR_MODES; ++mode) { | 
 | 3967 | 		STm = &(tpnt->modes[mode]); | 
 | 3968 | 		for (j=0; j < 2; j++) { | 
 | 3969 | 			cdev = cdev_alloc(); | 
 | 3970 | 			if (!cdev) { | 
 | 3971 | 				printk(KERN_ERR | 
 | 3972 | 				       "st%d: out of memory. Device not attached.\n", | 
 | 3973 | 				       dev_num); | 
 | 3974 | 				goto out_free_tape; | 
 | 3975 | 			} | 
 | 3976 | 			cdev->owner = THIS_MODULE; | 
 | 3977 | 			cdev->ops = &st_fops; | 
 | 3978 |  | 
 | 3979 | 			error = cdev_add(cdev, | 
 | 3980 | 					 MKDEV(SCSI_TAPE_MAJOR, TAPE_MINOR(dev_num, mode, j)), | 
 | 3981 | 					 1); | 
 | 3982 | 			if (error) { | 
 | 3983 | 				printk(KERN_ERR "st%d: Can't add %s-rewind mode %d\n", | 
 | 3984 | 				       dev_num, j ? "non" : "auto", mode); | 
 | 3985 | 				printk(KERN_ERR "st%d: Device not attached.\n", dev_num); | 
 | 3986 | 				goto out_free_tape; | 
 | 3987 | 			} | 
 | 3988 | 			STm->cdevs[j] = cdev; | 
 | 3989 |  | 
 | 3990 | 		} | 
 | 3991 | 		do_create_class_files(tpnt, dev_num, mode); | 
 | 3992 | 	} | 
 | 3993 |  | 
 | 3994 | 	for (mode = 0; mode < ST_NBR_MODES; ++mode) { | 
 | 3995 | 		/* Make sure that the minor numbers corresponding to the four | 
 | 3996 | 		   first modes always get the same names */ | 
 | 3997 | 		i = mode << (4 - ST_NBR_MODE_BITS); | 
 | 3998 | 		/*  Rewind entry  */ | 
 | 3999 | 		devfs_mk_cdev(MKDEV(SCSI_TAPE_MAJOR, TAPE_MINOR(dev_num, mode, 0)), | 
 | 4000 | 			      S_IFCHR | S_IRUGO | S_IWUGO, | 
 | 4001 | 			      "%s/mt%s", SDp->devfs_name, st_formats[i]); | 
 | 4002 | 		/*  No-rewind entry  */ | 
 | 4003 | 		devfs_mk_cdev(MKDEV(SCSI_TAPE_MAJOR, TAPE_MINOR(dev_num, mode, 1)), | 
 | 4004 | 			      S_IFCHR | S_IRUGO | S_IWUGO, | 
 | 4005 | 			      "%s/mt%sn", SDp->devfs_name, st_formats[i]); | 
 | 4006 | 	} | 
 | 4007 | 	disk->number = devfs_register_tape(SDp->devfs_name); | 
 | 4008 |  | 
 | 4009 | 	printk(KERN_WARNING | 
 | 4010 | 	"Attached scsi tape %s at scsi%d, channel %d, id %d, lun %d\n", | 
 | 4011 | 	       tape_name(tpnt), SDp->host->host_no, SDp->channel, SDp->id, SDp->lun); | 
 | 4012 | 	printk(KERN_WARNING "%s: try direct i/o: %s (alignment %d B), max page reachable by HBA %lu\n", | 
 | 4013 | 	       tape_name(tpnt), tpnt->try_dio ? "yes" : "no", | 
 | 4014 | 	       queue_dma_alignment(SDp->request_queue) + 1, tpnt->max_pfn); | 
 | 4015 |  | 
 | 4016 | 	return 0; | 
 | 4017 |  | 
 | 4018 | out_free_tape: | 
 | 4019 | 	for (mode=0; mode < ST_NBR_MODES; mode++) { | 
 | 4020 | 		STm = &(tpnt->modes[mode]); | 
 | 4021 | 		sysfs_remove_link(&tpnt->device->sdev_gendev.kobj, | 
 | 4022 | 				  "tape"); | 
 | 4023 | 		for (j=0; j < 2; j++) { | 
 | 4024 | 			if (STm->cdevs[j]) { | 
 | 4025 | 				if (cdev == STm->cdevs[j]) | 
 | 4026 | 					cdev = NULL; | 
| gregkh@suse.de | d253878 | 2005-03-23 09:55:22 -0800 | [diff] [blame] | 4027 | 				class_device_destroy(st_sysfs_class, | 
 | 4028 | 						     MKDEV(SCSI_TAPE_MAJOR, | 
 | 4029 | 							   TAPE_MINOR(i, mode, j))); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4030 | 				cdev_del(STm->cdevs[j]); | 
 | 4031 | 			} | 
 | 4032 | 		} | 
 | 4033 | 	} | 
 | 4034 | 	if (cdev) | 
 | 4035 | 		cdev_del(cdev); | 
 | 4036 | 	write_lock(&st_dev_arr_lock); | 
 | 4037 | 	scsi_tapes[dev_num] = NULL; | 
 | 4038 | 	st_nr_dev--; | 
 | 4039 | 	write_unlock(&st_dev_arr_lock); | 
 | 4040 | out_put_disk: | 
 | 4041 | 	put_disk(disk); | 
 | 4042 | 	if (tpnt) | 
 | 4043 | 		kfree(tpnt); | 
 | 4044 | out_buffer_free: | 
 | 4045 | 	kfree(buffer); | 
 | 4046 | out: | 
 | 4047 | 	return -ENODEV; | 
 | 4048 | }; | 
 | 4049 |  | 
 | 4050 |  | 
 | 4051 | static int st_remove(struct device *dev) | 
 | 4052 | { | 
 | 4053 | 	struct scsi_device *SDp = to_scsi_device(dev); | 
 | 4054 | 	struct scsi_tape *tpnt; | 
 | 4055 | 	int i, j, mode; | 
 | 4056 |  | 
 | 4057 | 	write_lock(&st_dev_arr_lock); | 
 | 4058 | 	for (i = 0; i < st_dev_max; i++) { | 
 | 4059 | 		tpnt = scsi_tapes[i]; | 
 | 4060 | 		if (tpnt != NULL && tpnt->device == SDp) { | 
 | 4061 | 			scsi_tapes[i] = NULL; | 
 | 4062 | 			st_nr_dev--; | 
 | 4063 | 			write_unlock(&st_dev_arr_lock); | 
 | 4064 | 			devfs_unregister_tape(tpnt->disk->number); | 
 | 4065 | 			sysfs_remove_link(&tpnt->device->sdev_gendev.kobj, | 
 | 4066 | 					  "tape"); | 
 | 4067 | 			for (mode = 0; mode < ST_NBR_MODES; ++mode) { | 
 | 4068 | 				j = mode << (4 - ST_NBR_MODE_BITS); | 
 | 4069 | 				devfs_remove("%s/mt%s", SDp->devfs_name, st_formats[j]); | 
 | 4070 | 				devfs_remove("%s/mt%sn", SDp->devfs_name, st_formats[j]); | 
 | 4071 | 				for (j=0; j < 2; j++) { | 
| gregkh@suse.de | d253878 | 2005-03-23 09:55:22 -0800 | [diff] [blame] | 4072 | 					class_device_destroy(st_sysfs_class, | 
 | 4073 | 							     MKDEV(SCSI_TAPE_MAJOR, | 
 | 4074 | 								   TAPE_MINOR(i, mode, j))); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4075 | 					cdev_del(tpnt->modes[mode].cdevs[j]); | 
 | 4076 | 					tpnt->modes[mode].cdevs[j] = NULL; | 
 | 4077 | 				} | 
 | 4078 | 			} | 
 | 4079 | 			tpnt->device = NULL; | 
 | 4080 |  | 
 | 4081 | 			if (tpnt->buffer) { | 
 | 4082 | 				tpnt->buffer->orig_frp_segs = 0; | 
 | 4083 | 				normalize_buffer(tpnt->buffer); | 
 | 4084 | 				kfree(tpnt->buffer); | 
 | 4085 | 			} | 
 | 4086 | 			put_disk(tpnt->disk); | 
 | 4087 | 			kfree(tpnt); | 
 | 4088 | 			return 0; | 
 | 4089 | 		} | 
 | 4090 | 	} | 
 | 4091 |  | 
 | 4092 | 	write_unlock(&st_dev_arr_lock); | 
 | 4093 | 	return 0; | 
 | 4094 | } | 
 | 4095 |  | 
 | 4096 | static void st_intr(struct scsi_cmnd *SCpnt) | 
 | 4097 | { | 
 | 4098 | 	scsi_io_completion(SCpnt, (SCpnt->result ? 0: SCpnt->bufflen), 1); | 
 | 4099 | } | 
 | 4100 |  | 
 | 4101 | /* | 
 | 4102 |  * st_init_command: only called via the scsi_cmd_ioctl (block SG_IO) | 
 | 4103 |  * interface for REQ_BLOCK_PC commands. | 
 | 4104 |  */ | 
 | 4105 | static int st_init_command(struct scsi_cmnd *SCpnt) | 
 | 4106 | { | 
 | 4107 | 	struct request *rq; | 
 | 4108 |  | 
 | 4109 | 	if (!(SCpnt->request->flags & REQ_BLOCK_PC)) | 
 | 4110 | 		return 0; | 
 | 4111 |  | 
 | 4112 | 	rq = SCpnt->request; | 
 | 4113 | 	if (sizeof(rq->cmd) > sizeof(SCpnt->cmnd)) | 
 | 4114 | 		return 0; | 
 | 4115 |  | 
 | 4116 | 	memcpy(SCpnt->cmnd, rq->cmd, sizeof(SCpnt->cmnd)); | 
 | 4117 |  | 
 | 4118 | 	if (rq_data_dir(rq) == WRITE) | 
 | 4119 | 		SCpnt->sc_data_direction = DMA_TO_DEVICE; | 
 | 4120 | 	else if (rq->data_len) | 
 | 4121 | 		SCpnt->sc_data_direction = DMA_FROM_DEVICE; | 
 | 4122 | 	else | 
 | 4123 | 		SCpnt->sc_data_direction = DMA_NONE; | 
 | 4124 |  | 
 | 4125 | 	SCpnt->timeout_per_command = rq->timeout; | 
 | 4126 | 	SCpnt->transfersize = rq->data_len; | 
 | 4127 | 	SCpnt->done = st_intr; | 
 | 4128 | 	return 1; | 
 | 4129 | } | 
 | 4130 |  | 
 | 4131 | static int __init init_st(void) | 
 | 4132 | { | 
 | 4133 | 	validate_options(); | 
 | 4134 |  | 
 | 4135 | 	printk(KERN_INFO | 
 | 4136 | 		"st: Version %s, fixed bufsize %d, s/g segs %d\n", | 
 | 4137 | 		verstr, st_fixed_buffer_size, st_max_sg_segs); | 
 | 4138 |  | 
| gregkh@suse.de | d253878 | 2005-03-23 09:55:22 -0800 | [diff] [blame] | 4139 | 	st_sysfs_class = class_create(THIS_MODULE, "scsi_tape"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4140 | 	if (IS_ERR(st_sysfs_class)) { | 
 | 4141 | 		st_sysfs_class = NULL; | 
 | 4142 | 		printk(KERN_ERR "Unable create sysfs class for SCSI tapes\n"); | 
 | 4143 | 		return 1; | 
 | 4144 | 	} | 
 | 4145 |  | 
 | 4146 | 	if (!register_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0), | 
 | 4147 | 				    ST_MAX_TAPE_ENTRIES, "st")) { | 
 | 4148 | 		if (scsi_register_driver(&st_template.gendrv) == 0) { | 
 | 4149 | 			do_create_driverfs_files(); | 
 | 4150 | 			return 0; | 
 | 4151 | 		} | 
 | 4152 | 		if (st_sysfs_class) | 
| gregkh@suse.de | d253878 | 2005-03-23 09:55:22 -0800 | [diff] [blame] | 4153 | 			class_destroy(st_sysfs_class); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4154 | 		unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0), | 
 | 4155 |  | 
 | 4156 | 					 ST_MAX_TAPE_ENTRIES); | 
 | 4157 | 	} | 
 | 4158 |  | 
 | 4159 | 	printk(KERN_ERR "Unable to get major %d for SCSI tapes\n", SCSI_TAPE_MAJOR); | 
 | 4160 | 	return 1; | 
 | 4161 | } | 
 | 4162 |  | 
 | 4163 | static void __exit exit_st(void) | 
 | 4164 | { | 
 | 4165 | 	if (st_sysfs_class) | 
| gregkh@suse.de | d253878 | 2005-03-23 09:55:22 -0800 | [diff] [blame] | 4166 | 		class_destroy(st_sysfs_class); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4167 | 	st_sysfs_class = NULL; | 
 | 4168 | 	do_remove_driverfs_files(); | 
 | 4169 | 	scsi_unregister_driver(&st_template.gendrv); | 
 | 4170 | 	unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0), | 
 | 4171 | 				 ST_MAX_TAPE_ENTRIES); | 
 | 4172 | 	kfree(scsi_tapes); | 
 | 4173 | 	printk(KERN_INFO "st: Unloaded.\n"); | 
 | 4174 | } | 
 | 4175 |  | 
 | 4176 | module_init(init_st); | 
 | 4177 | module_exit(exit_st); | 
 | 4178 |  | 
 | 4179 |  | 
 | 4180 | /* The sysfs driver interface. Read-only at the moment */ | 
 | 4181 | static ssize_t st_try_direct_io_show(struct device_driver *ddp, char *buf) | 
 | 4182 | { | 
 | 4183 | 	return snprintf(buf, PAGE_SIZE, "%d\n", try_direct_io); | 
 | 4184 | } | 
 | 4185 | static DRIVER_ATTR(try_direct_io, S_IRUGO, st_try_direct_io_show, NULL); | 
 | 4186 |  | 
 | 4187 | static ssize_t st_fixed_buffer_size_show(struct device_driver *ddp, char *buf) | 
 | 4188 | { | 
 | 4189 | 	return snprintf(buf, PAGE_SIZE, "%d\n", st_fixed_buffer_size); | 
 | 4190 | } | 
 | 4191 | static DRIVER_ATTR(fixed_buffer_size, S_IRUGO, st_fixed_buffer_size_show, NULL); | 
 | 4192 |  | 
 | 4193 | static ssize_t st_max_sg_segs_show(struct device_driver *ddp, char *buf) | 
 | 4194 | { | 
 | 4195 | 	return snprintf(buf, PAGE_SIZE, "%d\n", st_max_sg_segs); | 
 | 4196 | } | 
 | 4197 | static DRIVER_ATTR(max_sg_segs, S_IRUGO, st_max_sg_segs_show, NULL); | 
 | 4198 |  | 
 | 4199 | static ssize_t st_version_show(struct device_driver *ddd, char *buf) | 
 | 4200 | { | 
 | 4201 | 	return snprintf(buf, PAGE_SIZE, "[%s]\n", verstr); | 
 | 4202 | } | 
 | 4203 | static DRIVER_ATTR(version, S_IRUGO, st_version_show, NULL); | 
 | 4204 |  | 
 | 4205 | static void do_create_driverfs_files(void) | 
 | 4206 | { | 
 | 4207 | 	struct device_driver *driverfs = &st_template.gendrv; | 
 | 4208 |  | 
 | 4209 | 	driver_create_file(driverfs, &driver_attr_try_direct_io); | 
 | 4210 | 	driver_create_file(driverfs, &driver_attr_fixed_buffer_size); | 
 | 4211 | 	driver_create_file(driverfs, &driver_attr_max_sg_segs); | 
 | 4212 | 	driver_create_file(driverfs, &driver_attr_version); | 
 | 4213 | } | 
 | 4214 |  | 
 | 4215 | static void do_remove_driverfs_files(void) | 
 | 4216 | { | 
 | 4217 | 	struct device_driver *driverfs = &st_template.gendrv; | 
 | 4218 |  | 
 | 4219 | 	driver_remove_file(driverfs, &driver_attr_version); | 
 | 4220 | 	driver_remove_file(driverfs, &driver_attr_max_sg_segs); | 
 | 4221 | 	driver_remove_file(driverfs, &driver_attr_fixed_buffer_size); | 
 | 4222 | 	driver_remove_file(driverfs, &driver_attr_try_direct_io); | 
 | 4223 | } | 
 | 4224 |  | 
 | 4225 |  | 
 | 4226 | /* The sysfs simple class interface */ | 
 | 4227 | static ssize_t st_defined_show(struct class_device *class_dev, char *buf) | 
 | 4228 | { | 
 | 4229 | 	struct st_modedef *STm = (struct st_modedef *)class_get_devdata(class_dev); | 
 | 4230 | 	ssize_t l = 0; | 
 | 4231 |  | 
 | 4232 | 	l = snprintf(buf, PAGE_SIZE, "%d\n", STm->defined); | 
 | 4233 | 	return l; | 
 | 4234 | } | 
 | 4235 |  | 
 | 4236 | CLASS_DEVICE_ATTR(defined, S_IRUGO, st_defined_show, NULL); | 
 | 4237 |  | 
 | 4238 | static ssize_t st_defblk_show(struct class_device *class_dev, char *buf) | 
 | 4239 | { | 
 | 4240 | 	struct st_modedef *STm = (struct st_modedef *)class_get_devdata(class_dev); | 
 | 4241 | 	ssize_t l = 0; | 
 | 4242 |  | 
 | 4243 | 	l = snprintf(buf, PAGE_SIZE, "%d\n", STm->default_blksize); | 
 | 4244 | 	return l; | 
 | 4245 | } | 
 | 4246 |  | 
 | 4247 | CLASS_DEVICE_ATTR(default_blksize, S_IRUGO, st_defblk_show, NULL); | 
 | 4248 |  | 
 | 4249 | static ssize_t st_defdensity_show(struct class_device *class_dev, char *buf) | 
 | 4250 | { | 
 | 4251 | 	struct st_modedef *STm = (struct st_modedef *)class_get_devdata(class_dev); | 
 | 4252 | 	ssize_t l = 0; | 
 | 4253 | 	char *fmt; | 
 | 4254 |  | 
 | 4255 | 	fmt = STm->default_density >= 0 ? "0x%02x\n" : "%d\n"; | 
 | 4256 | 	l = snprintf(buf, PAGE_SIZE, fmt, STm->default_density); | 
 | 4257 | 	return l; | 
 | 4258 | } | 
 | 4259 |  | 
 | 4260 | CLASS_DEVICE_ATTR(default_density, S_IRUGO, st_defdensity_show, NULL); | 
 | 4261 |  | 
 | 4262 | static ssize_t st_defcompression_show(struct class_device *class_dev, char *buf) | 
 | 4263 | { | 
 | 4264 | 	struct st_modedef *STm = (struct st_modedef *)class_get_devdata(class_dev); | 
 | 4265 | 	ssize_t l = 0; | 
 | 4266 |  | 
 | 4267 | 	l = snprintf(buf, PAGE_SIZE, "%d\n", STm->default_compression - 1); | 
 | 4268 | 	return l; | 
 | 4269 | } | 
 | 4270 |  | 
 | 4271 | CLASS_DEVICE_ATTR(default_compression, S_IRUGO, st_defcompression_show, NULL); | 
 | 4272 |  | 
 | 4273 | static void do_create_class_files(struct scsi_tape *STp, int dev_num, int mode) | 
 | 4274 | { | 
 | 4275 | 	int i, rew, error; | 
 | 4276 | 	char name[10]; | 
 | 4277 | 	struct class_device *st_class_member; | 
 | 4278 |  | 
 | 4279 | 	if (!st_sysfs_class) | 
 | 4280 | 		return; | 
 | 4281 |  | 
 | 4282 | 	for (rew=0; rew < 2; rew++) { | 
 | 4283 | 		/* Make sure that the minor numbers corresponding to the four | 
 | 4284 | 		   first modes always get the same names */ | 
 | 4285 | 		i = mode << (4 - ST_NBR_MODE_BITS); | 
 | 4286 | 		snprintf(name, 10, "%s%s%s", rew ? "n" : "", | 
 | 4287 | 			 STp->disk->disk_name, st_formats[i]); | 
 | 4288 | 		st_class_member = | 
| gregkh@suse.de | d253878 | 2005-03-23 09:55:22 -0800 | [diff] [blame] | 4289 | 			class_device_create(st_sysfs_class, | 
 | 4290 | 					    MKDEV(SCSI_TAPE_MAJOR, | 
 | 4291 | 						  TAPE_MINOR(dev_num, mode, rew)), | 
 | 4292 | 					    &STp->device->sdev_gendev, "%s", name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4293 | 		if (IS_ERR(st_class_member)) { | 
| gregkh@suse.de | d253878 | 2005-03-23 09:55:22 -0800 | [diff] [blame] | 4294 | 			printk(KERN_WARNING "st%d: class_device_create failed\n", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4295 | 			       dev_num); | 
 | 4296 | 			goto out; | 
 | 4297 | 		} | 
 | 4298 | 		class_set_devdata(st_class_member, &STp->modes[mode]); | 
 | 4299 |  | 
 | 4300 | 		class_device_create_file(st_class_member, | 
 | 4301 | 					 &class_device_attr_defined); | 
 | 4302 | 		class_device_create_file(st_class_member, | 
 | 4303 | 					 &class_device_attr_default_blksize); | 
 | 4304 | 		class_device_create_file(st_class_member, | 
 | 4305 | 					 &class_device_attr_default_density); | 
 | 4306 | 		class_device_create_file(st_class_member, | 
 | 4307 | 					 &class_device_attr_default_compression); | 
 | 4308 | 		if (mode == 0 && rew == 0) { | 
 | 4309 | 			error = sysfs_create_link(&STp->device->sdev_gendev.kobj, | 
 | 4310 | 						  &st_class_member->kobj, | 
 | 4311 | 						  "tape"); | 
 | 4312 | 			if (error) { | 
 | 4313 | 				printk(KERN_ERR | 
 | 4314 | 				       "st%d: Can't create sysfs link from SCSI device.\n", | 
 | 4315 | 				       dev_num); | 
 | 4316 | 			} | 
 | 4317 | 		} | 
 | 4318 | 	} | 
 | 4319 |  out: | 
 | 4320 | 	return; | 
 | 4321 | } | 
 | 4322 |  | 
 | 4323 |  | 
 | 4324 | /* Pin down user pages and put them into a scatter gather list. Returns <= 0 if | 
 | 4325 |    - mapping of all pages not successful | 
 | 4326 |    - any page is above max_pfn | 
 | 4327 |    (i.e., either completely successful or fails) | 
 | 4328 | */ | 
 | 4329 | static int st_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,  | 
 | 4330 | 			     unsigned long uaddr, size_t count, int rw, | 
 | 4331 | 			     unsigned long max_pfn) | 
 | 4332 | { | 
 | 4333 | 	int i, nr_pages; | 
 | 4334 |  | 
 | 4335 | 	nr_pages = sgl_map_user_pages(sgl, max_pages, uaddr, count, rw); | 
 | 4336 | 	if (nr_pages <= 0) | 
 | 4337 | 		return nr_pages; | 
 | 4338 |  | 
 | 4339 | 	for (i=0; i < nr_pages; i++) { | 
 | 4340 | 		if (page_to_pfn(sgl[i].page) > max_pfn) | 
 | 4341 | 			goto out_unmap; | 
 | 4342 | 	} | 
 | 4343 | 	return nr_pages; | 
 | 4344 |  | 
 | 4345 |  out_unmap: | 
 | 4346 | 	sgl_unmap_user_pages(sgl, nr_pages, 0); | 
 | 4347 | 	return 0; | 
 | 4348 | } | 
 | 4349 |  | 
 | 4350 |  | 
 | 4351 | /* The following functions may be useful for a larger audience. */ | 
 | 4352 | static int sgl_map_user_pages(struct scatterlist *sgl, const unsigned int max_pages,  | 
 | 4353 | 			      unsigned long uaddr, size_t count, int rw) | 
 | 4354 | { | 
 | 4355 | 	int res, i, j; | 
 | 4356 | 	unsigned int nr_pages; | 
 | 4357 | 	struct page **pages; | 
 | 4358 |  | 
 | 4359 | 	nr_pages = ((uaddr & ~PAGE_MASK) + count + ~PAGE_MASK) >> PAGE_SHIFT; | 
 | 4360 |  | 
 | 4361 | 	/* User attempted Overflow! */ | 
 | 4362 | 	if ((uaddr + count) < uaddr) | 
 | 4363 | 		return -EINVAL; | 
 | 4364 |  | 
 | 4365 | 	/* Too big */ | 
 | 4366 |         if (nr_pages > max_pages) | 
 | 4367 | 		return -ENOMEM; | 
 | 4368 |  | 
 | 4369 | 	/* Hmm? */ | 
 | 4370 | 	if (count == 0) | 
 | 4371 | 		return 0; | 
 | 4372 |  | 
 | 4373 | 	if ((pages = kmalloc(max_pages * sizeof(*pages), GFP_KERNEL)) == NULL) | 
 | 4374 | 		return -ENOMEM; | 
 | 4375 |  | 
 | 4376 |         /* Try to fault in all of the necessary pages */ | 
 | 4377 | 	down_read(¤t->mm->mmap_sem); | 
 | 4378 |         /* rw==READ means read from drive, write into memory area */ | 
 | 4379 | 	res = get_user_pages( | 
 | 4380 | 		current, | 
 | 4381 | 		current->mm, | 
 | 4382 | 		uaddr, | 
 | 4383 | 		nr_pages, | 
 | 4384 | 		rw == READ, | 
 | 4385 | 		0, /* don't force */ | 
 | 4386 | 		pages, | 
 | 4387 | 		NULL); | 
 | 4388 | 	up_read(¤t->mm->mmap_sem); | 
 | 4389 |  | 
 | 4390 | 	/* Errors and no page mapped should return here */ | 
 | 4391 | 	if (res < nr_pages) | 
 | 4392 | 		goto out_unmap; | 
 | 4393 |  | 
 | 4394 |         for (i=0; i < nr_pages; i++) { | 
 | 4395 |                 /* FIXME: flush superflous for rw==READ, | 
 | 4396 |                  * probably wrong function for rw==WRITE | 
 | 4397 |                  */ | 
 | 4398 | 		flush_dcache_page(pages[i]); | 
 | 4399 |         } | 
 | 4400 |  | 
 | 4401 | 	/* Populate the scatter/gather list */ | 
 | 4402 | 	sgl[0].page = pages[0];  | 
 | 4403 | 	sgl[0].offset = uaddr & ~PAGE_MASK; | 
 | 4404 | 	if (nr_pages > 1) { | 
 | 4405 | 		sgl[0].length = PAGE_SIZE - sgl[0].offset; | 
 | 4406 | 		count -= sgl[0].length; | 
 | 4407 | 		for (i=1; i < nr_pages ; i++) { | 
 | 4408 | 			sgl[i].offset = 0; | 
 | 4409 | 			sgl[i].page = pages[i];  | 
 | 4410 | 			sgl[i].length = count < PAGE_SIZE ? count : PAGE_SIZE; | 
 | 4411 | 			count -= PAGE_SIZE; | 
 | 4412 | 		} | 
 | 4413 | 	} | 
 | 4414 | 	else { | 
 | 4415 | 		sgl[0].length = count; | 
 | 4416 | 	} | 
 | 4417 |  | 
 | 4418 | 	kfree(pages); | 
 | 4419 | 	return nr_pages; | 
 | 4420 |  | 
 | 4421 |  out_unmap: | 
 | 4422 | 	if (res > 0) { | 
 | 4423 | 		for (j=0; j < res; j++) | 
 | 4424 | 			page_cache_release(pages[j]); | 
 | 4425 | 	} | 
 | 4426 | 	kfree(pages); | 
 | 4427 | 	return res; | 
 | 4428 | } | 
 | 4429 |  | 
 | 4430 |  | 
 | 4431 | /* And unmap them... */ | 
 | 4432 | static int sgl_unmap_user_pages(struct scatterlist *sgl, const unsigned int nr_pages, | 
 | 4433 | 				int dirtied) | 
 | 4434 | { | 
 | 4435 | 	int i; | 
 | 4436 |  | 
 | 4437 | 	for (i=0; i < nr_pages; i++) { | 
 | 4438 | 		if (dirtied && !PageReserved(sgl[i].page)) | 
 | 4439 | 			SetPageDirty(sgl[i].page); | 
 | 4440 | 		/* FIXME: cache flush missing for rw==READ | 
 | 4441 | 		 * FIXME: call the correct reference counting function | 
 | 4442 | 		 */ | 
 | 4443 | 		page_cache_release(sgl[i].page); | 
 | 4444 | 	} | 
 | 4445 |  | 
 | 4446 | 	return 0; | 
 | 4447 | } |