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