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