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