ide-tape.c revision f2e3ab52afb3b05879b9d82a05740cd924993325
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 IDETAPE_VERSION "1.20" 19 20#include <linux/module.h> 21#include <linux/types.h> 22#include <linux/string.h> 23#include <linux/kernel.h> 24#include <linux/delay.h> 25#include <linux/timer.h> 26#include <linux/mm.h> 27#include <linux/interrupt.h> 28#include <linux/jiffies.h> 29#include <linux/major.h> 30#include <linux/errno.h> 31#include <linux/genhd.h> 32#include <linux/slab.h> 33#include <linux/pci.h> 34#include <linux/ide.h> 35#include <linux/smp_lock.h> 36#include <linux/completion.h> 37#include <linux/bitops.h> 38#include <linux/mutex.h> 39#include <scsi/scsi.h> 40 41#include <asm/byteorder.h> 42#include <linux/irq.h> 43#include <linux/uaccess.h> 44#include <linux/io.h> 45#include <asm/unaligned.h> 46#include <linux/mtio.h> 47 48enum { 49 /* output errors only */ 50 DBG_ERR = (1 << 0), 51 /* output all sense key/asc */ 52 DBG_SENSE = (1 << 1), 53 /* info regarding all chrdev-related procedures */ 54 DBG_CHRDEV = (1 << 2), 55 /* all remaining procedures */ 56 DBG_PROCS = (1 << 3), 57 /* buffer alloc info (pc_stack & rq_stack) */ 58 DBG_PCRQ_STACK = (1 << 4), 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 * With each packet command, we allocate a buffer of IDETAPE_PC_BUFFER_SIZE 85 * bytes. This is used for several packet commands (Not for READ/WRITE commands) 86 */ 87#define IDETAPE_PC_BUFFER_SIZE 256 88 89/* 90 * In various places in the driver, we need to allocate storage 91 * for packet commands and requests, which will remain valid while 92 * we leave the driver to wait for an interrupt or a timeout event. 93 */ 94#define IDETAPE_PC_STACK (10 + IDETAPE_MAX_PC_RETRIES) 95 96/* 97 * Some drives (for example, Seagate STT3401A Travan) require a very long 98 * timeout, because they don't return an interrupt or clear their busy bit 99 * until after the command completes (even retension commands). 100 */ 101#define IDETAPE_WAIT_CMD (900*HZ) 102 103/* 104 * The following parameter is used to select the point in the internal tape fifo 105 * in which we will start to refill the buffer. Decreasing the following 106 * parameter will improve the system's latency and interactive response, while 107 * using a high value might improve system throughput. 108 */ 109#define IDETAPE_FIFO_THRESHOLD 2 110 111/* 112 * DSC polling parameters. 113 * 114 * Polling for DSC (a single bit in the status register) is a very important 115 * function in ide-tape. There are two cases in which we poll for DSC: 116 * 117 * 1. Before a read/write packet command, to ensure that we can transfer data 118 * from/to the tape's data buffers, without causing an actual media access. 119 * In case the tape is not ready yet, we take out our request from the device 120 * request queue, so that ide.c could service requests from the other device 121 * on the same interface in the meantime. 122 * 123 * 2. After the successful initialization of a "media access packet command", 124 * which is a command that can take a long time to complete (the interval can 125 * range from several seconds to even an hour). Again, we postpone our request 126 * in the middle to free the bus for the other device. The polling frequency 127 * here should be lower than the read/write frequency since those media access 128 * commands are slow. We start from a "fast" frequency - IDETAPE_DSC_MA_FAST 129 * (1 second), and if we don't receive DSC after IDETAPE_DSC_MA_THRESHOLD 130 * (5 min), we switch it to a lower frequency - IDETAPE_DSC_MA_SLOW (1 min). 131 * 132 * We also set a timeout for the timer, in case something goes wrong. The 133 * timeout should be longer then the maximum execution time of a tape operation. 134 */ 135 136/* DSC timings. */ 137#define IDETAPE_DSC_RW_MIN 5*HZ/100 /* 50 msec */ 138#define IDETAPE_DSC_RW_MAX 40*HZ/100 /* 400 msec */ 139#define IDETAPE_DSC_RW_TIMEOUT 2*60*HZ /* 2 minutes */ 140#define IDETAPE_DSC_MA_FAST 2*HZ /* 2 seconds */ 141#define IDETAPE_DSC_MA_THRESHOLD 5*60*HZ /* 5 minutes */ 142#define IDETAPE_DSC_MA_SLOW 30*HZ /* 30 seconds */ 143#define IDETAPE_DSC_MA_TIMEOUT 2*60*60*HZ /* 2 hours */ 144 145/*************************** End of tunable parameters ***********************/ 146 147/* tape directions */ 148enum { 149 IDETAPE_DIR_NONE = (1 << 0), 150 IDETAPE_DIR_READ = (1 << 1), 151 IDETAPE_DIR_WRITE = (1 << 2), 152}; 153 154struct idetape_bh { 155 u32 b_size; 156 atomic_t b_count; 157 struct idetape_bh *b_reqnext; 158 char *b_data; 159}; 160 161/* Tape door status */ 162#define DOOR_UNLOCKED 0 163#define DOOR_LOCKED 1 164#define DOOR_EXPLICITLY_LOCKED 2 165 166/* Some defines for the SPACE command */ 167#define IDETAPE_SPACE_OVER_FILEMARK 1 168#define IDETAPE_SPACE_TO_EOD 3 169 170/* Some defines for the LOAD UNLOAD command */ 171#define IDETAPE_LU_LOAD_MASK 1 172#define IDETAPE_LU_RETENSION_MASK 2 173#define IDETAPE_LU_EOT_MASK 4 174 175/* 176 * Special requests for our block device strategy routine. 177 * 178 * In order to service a character device command, we add special requests to 179 * the tail of our block device request queue and wait for their completion. 180 */ 181 182enum { 183 REQ_IDETAPE_PC1 = (1 << 0), /* packet command (first stage) */ 184 REQ_IDETAPE_PC2 = (1 << 1), /* packet command (second stage) */ 185 REQ_IDETAPE_READ = (1 << 2), 186 REQ_IDETAPE_WRITE = (1 << 3), 187}; 188 189/* Error codes returned in rq->errors to the higher part of the driver. */ 190#define IDETAPE_ERROR_GENERAL 101 191#define IDETAPE_ERROR_FILEMARK 102 192#define IDETAPE_ERROR_EOD 103 193 194/* Structures related to the SELECT SENSE / MODE SENSE packet commands. */ 195#define IDETAPE_BLOCK_DESCRIPTOR 0 196#define IDETAPE_CAPABILITIES_PAGE 0x2a 197 198/* 199 * Most of our global data which we need to save even as we leave the driver due 200 * to an interrupt or a timer event is stored in the struct defined below. 201 */ 202typedef struct ide_tape_obj { 203 ide_drive_t *drive; 204 ide_driver_t *driver; 205 struct gendisk *disk; 206 struct kref kref; 207 208 /* 209 * Since a typical character device operation requires more 210 * than one packet command, we provide here enough memory 211 * for the maximum of interconnected packet commands. 212 * The packet commands are stored in the circular array pc_stack. 213 * pc_stack_index points to the last used entry, and warps around 214 * to the start when we get to the last array entry. 215 * 216 * pc points to the current processed packet command. 217 * 218 * failed_pc points to the last failed packet command, or contains 219 * NULL if we do not need to retry any packet command. This is 220 * required since an additional packet command is needed before the 221 * retry, to get detailed information on what went wrong. 222 */ 223 /* Current packet command */ 224 struct ide_atapi_pc *pc; 225 /* Last failed packet command */ 226 struct ide_atapi_pc *failed_pc; 227 /* Packet command stack */ 228 struct ide_atapi_pc pc_stack[IDETAPE_PC_STACK]; 229 /* Next free packet command storage space */ 230 int pc_stack_index; 231 struct request rq_stack[IDETAPE_PC_STACK]; 232 /* We implement a circular array */ 233 int rq_stack_index; 234 235 /* 236 * DSC polling variables. 237 * 238 * While polling for DSC we use postponed_rq to postpone the current 239 * request so that ide.c will be able to service pending requests on the 240 * other device. Note that at most we will have only one DSC (usually 241 * data transfer) request in the device request queue. 242 */ 243 struct request *postponed_rq; 244 /* The time in which we started polling for DSC */ 245 unsigned long dsc_polling_start; 246 /* Timer used to poll for dsc */ 247 struct timer_list dsc_timer; 248 /* Read/Write dsc polling frequency */ 249 unsigned long best_dsc_rw_freq; 250 unsigned long dsc_poll_freq; 251 unsigned long dsc_timeout; 252 253 /* Read position information */ 254 u8 partition; 255 /* Current block */ 256 unsigned int first_frame; 257 258 /* Last error information */ 259 u8 sense_key, asc, ascq; 260 261 /* Character device operation */ 262 unsigned int minor; 263 /* device name */ 264 char name[4]; 265 /* Current character device data transfer direction */ 266 u8 chrdev_dir; 267 268 /* tape block size, usually 512 or 1024 bytes */ 269 unsigned short blk_size; 270 int user_bs_factor; 271 272 /* Copy of the tape's Capabilities and Mechanical Page */ 273 u8 caps[20]; 274 275 /* 276 * Active data transfer request parameters. 277 * 278 * At most, there is only one ide-tape originated data transfer request 279 * in the device request queue. This allows ide.c to easily service 280 * requests from the other device when we postpone our active request. 281 */ 282 283 /* Data buffer size chosen based on the tape's recommendation */ 284 int buffer_size; 285 /* merge buffer */ 286 struct idetape_bh *merge_bh; 287 /* size of the merge buffer */ 288 int merge_bh_size; 289 /* pointer to current buffer head within the merge buffer */ 290 struct idetape_bh *bh; 291 char *b_data; 292 int b_count; 293 294 int pages_per_buffer; 295 /* Wasted space in each stage */ 296 int excess_bh_size; 297 298 /* protects the ide-tape queue */ 299 spinlock_t lock; 300 301 /* Measures average tape speed */ 302 unsigned long avg_time; 303 int avg_size; 304 int avg_speed; 305 306 /* the door is currently locked */ 307 int door_locked; 308 /* the tape hardware is write protected */ 309 char drv_write_prot; 310 /* the tape is write protected (hardware or opened as read-only) */ 311 char write_prot; 312 313 u32 debug_mask; 314} idetape_tape_t; 315 316static DEFINE_MUTEX(idetape_ref_mutex); 317 318static struct class *idetape_sysfs_class; 319 320#define to_ide_tape(obj) container_of(obj, struct ide_tape_obj, kref) 321 322#define ide_tape_g(disk) \ 323 container_of((disk)->private_data, struct ide_tape_obj, driver) 324 325static struct ide_tape_obj *ide_tape_get(struct gendisk *disk) 326{ 327 struct ide_tape_obj *tape = NULL; 328 329 mutex_lock(&idetape_ref_mutex); 330 tape = ide_tape_g(disk); 331 if (tape) 332 kref_get(&tape->kref); 333 mutex_unlock(&idetape_ref_mutex); 334 return tape; 335} 336 337static void ide_tape_release(struct kref *); 338 339static void ide_tape_put(struct ide_tape_obj *tape) 340{ 341 mutex_lock(&idetape_ref_mutex); 342 kref_put(&tape->kref, ide_tape_release); 343 mutex_unlock(&idetape_ref_mutex); 344} 345 346/* 347 * The variables below are used for the character device interface. Additional 348 * state variables are defined in our ide_drive_t structure. 349 */ 350static struct ide_tape_obj *idetape_devs[MAX_HWIFS * MAX_DRIVES]; 351 352#define ide_tape_f(file) ((file)->private_data) 353 354static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i) 355{ 356 struct ide_tape_obj *tape = NULL; 357 358 mutex_lock(&idetape_ref_mutex); 359 tape = idetape_devs[i]; 360 if (tape) 361 kref_get(&tape->kref); 362 mutex_unlock(&idetape_ref_mutex); 363 return tape; 364} 365 366static void idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc, 367 unsigned int bcount) 368{ 369 struct idetape_bh *bh = pc->bh; 370 int count; 371 372 while (bcount) { 373 if (bh == NULL) { 374 printk(KERN_ERR "ide-tape: bh == NULL in " 375 "idetape_input_buffers\n"); 376 ide_pad_transfer(drive, 0, bcount); 377 return; 378 } 379 count = min( 380 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)), 381 bcount); 382 drive->hwif->tp_ops->input_data(drive, NULL, bh->b_data + 383 atomic_read(&bh->b_count), count); 384 bcount -= count; 385 atomic_add(count, &bh->b_count); 386 if (atomic_read(&bh->b_count) == bh->b_size) { 387 bh = bh->b_reqnext; 388 if (bh) 389 atomic_set(&bh->b_count, 0); 390 } 391 } 392 pc->bh = bh; 393} 394 395static void idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc, 396 unsigned int bcount) 397{ 398 struct idetape_bh *bh = pc->bh; 399 int count; 400 401 while (bcount) { 402 if (bh == NULL) { 403 printk(KERN_ERR "ide-tape: bh == NULL in %s\n", 404 __func__); 405 return; 406 } 407 count = min((unsigned int)pc->b_count, (unsigned int)bcount); 408 drive->hwif->tp_ops->output_data(drive, NULL, pc->b_data, count); 409 bcount -= count; 410 pc->b_data += count; 411 pc->b_count -= count; 412 if (!pc->b_count) { 413 bh = bh->b_reqnext; 414 pc->bh = bh; 415 if (bh) { 416 pc->b_data = bh->b_data; 417 pc->b_count = atomic_read(&bh->b_count); 418 } 419 } 420 } 421} 422 423static void idetape_update_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc) 424{ 425 struct idetape_bh *bh = pc->bh; 426 int count; 427 unsigned int bcount = pc->xferred; 428 429 if (pc->flags & PC_FLAG_WRITING) 430 return; 431 while (bcount) { 432 if (bh == NULL) { 433 printk(KERN_ERR "ide-tape: bh == NULL in %s\n", 434 __func__); 435 return; 436 } 437 count = min((unsigned int)bh->b_size, (unsigned int)bcount); 438 atomic_set(&bh->b_count, count); 439 if (atomic_read(&bh->b_count) == bh->b_size) 440 bh = bh->b_reqnext; 441 bcount -= count; 442 } 443 pc->bh = bh; 444} 445 446/* 447 * idetape_next_pc_storage returns a pointer to a place in which we can 448 * safely store a packet command, even though we intend to leave the 449 * driver. A storage space for a maximum of IDETAPE_PC_STACK packet 450 * commands is allocated at initialization time. 451 */ 452static struct ide_atapi_pc *idetape_next_pc_storage(ide_drive_t *drive) 453{ 454 idetape_tape_t *tape = drive->driver_data; 455 456 debug_log(DBG_PCRQ_STACK, "pc_stack_index=%d\n", tape->pc_stack_index); 457 458 if (tape->pc_stack_index == IDETAPE_PC_STACK) 459 tape->pc_stack_index = 0; 460 return (&tape->pc_stack[tape->pc_stack_index++]); 461} 462 463/* 464 * idetape_next_rq_storage is used along with idetape_next_pc_storage. 465 * Since we queue packet commands in the request queue, we need to 466 * allocate a request, along with the allocation of a packet command. 467 */ 468 469/************************************************************** 470 * * 471 * This should get fixed to use kmalloc(.., GFP_ATOMIC) * 472 * followed later on by kfree(). -ml * 473 * * 474 **************************************************************/ 475 476static struct request *idetape_next_rq_storage(ide_drive_t *drive) 477{ 478 idetape_tape_t *tape = drive->driver_data; 479 480 debug_log(DBG_PCRQ_STACK, "rq_stack_index=%d\n", tape->rq_stack_index); 481 482 if (tape->rq_stack_index == IDETAPE_PC_STACK) 483 tape->rq_stack_index = 0; 484 return (&tape->rq_stack[tape->rq_stack_index++]); 485} 486 487/* 488 * called on each failed packet command retry to analyze the request sense. We 489 * currently do not utilize this information. 490 */ 491static void idetape_analyze_error(ide_drive_t *drive, u8 *sense) 492{ 493 idetape_tape_t *tape = drive->driver_data; 494 struct ide_atapi_pc *pc = tape->failed_pc; 495 496 tape->sense_key = sense[2] & 0xF; 497 tape->asc = sense[12]; 498 tape->ascq = sense[13]; 499 500 debug_log(DBG_ERR, "pc = %x, sense key = %x, asc = %x, ascq = %x\n", 501 pc->c[0], tape->sense_key, tape->asc, tape->ascq); 502 503 /* Correct pc->xferred by asking the tape. */ 504 if (pc->flags & PC_FLAG_DMA_ERROR) { 505 pc->xferred = pc->req_xfer - 506 tape->blk_size * 507 get_unaligned_be32(&sense[3]); 508 idetape_update_buffers(drive, pc); 509 } 510 511 /* 512 * If error was the result of a zero-length read or write command, 513 * with sense key=5, asc=0x22, ascq=0, let it slide. Some drives 514 * (i.e. Seagate STT3401A Travan) don't support 0-length read/writes. 515 */ 516 if ((pc->c[0] == READ_6 || pc->c[0] == WRITE_6) 517 /* length == 0 */ 518 && pc->c[4] == 0 && pc->c[3] == 0 && pc->c[2] == 0) { 519 if (tape->sense_key == 5) { 520 /* don't report an error, everything's ok */ 521 pc->error = 0; 522 /* don't retry read/write */ 523 pc->flags |= PC_FLAG_ABORT; 524 } 525 } 526 if (pc->c[0] == READ_6 && (sense[2] & 0x80)) { 527 pc->error = IDETAPE_ERROR_FILEMARK; 528 pc->flags |= PC_FLAG_ABORT; 529 } 530 if (pc->c[0] == WRITE_6) { 531 if ((sense[2] & 0x40) || (tape->sense_key == 0xd 532 && tape->asc == 0x0 && tape->ascq == 0x2)) { 533 pc->error = IDETAPE_ERROR_EOD; 534 pc->flags |= PC_FLAG_ABORT; 535 } 536 } 537 if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) { 538 if (tape->sense_key == 8) { 539 pc->error = IDETAPE_ERROR_EOD; 540 pc->flags |= PC_FLAG_ABORT; 541 } 542 if (!(pc->flags & PC_FLAG_ABORT) && 543 pc->xferred) 544 pc->retries = IDETAPE_MAX_PC_RETRIES + 1; 545 } 546} 547 548/* Free data buffers completely. */ 549static void ide_tape_kfree_buffer(idetape_tape_t *tape) 550{ 551 struct idetape_bh *prev_bh, *bh = tape->merge_bh; 552 553 while (bh) { 554 u32 size = bh->b_size; 555 556 while (size) { 557 unsigned int order = fls(size >> PAGE_SHIFT)-1; 558 559 if (bh->b_data) 560 free_pages((unsigned long)bh->b_data, order); 561 562 size &= (order-1); 563 bh->b_data += (1 << order) * PAGE_SIZE; 564 } 565 prev_bh = bh; 566 bh = bh->b_reqnext; 567 kfree(prev_bh); 568 } 569} 570 571static int idetape_end_request(ide_drive_t *drive, int uptodate, int nr_sects) 572{ 573 struct request *rq = HWGROUP(drive)->rq; 574 idetape_tape_t *tape = drive->driver_data; 575 unsigned long flags; 576 int error; 577 578 debug_log(DBG_PROCS, "Enter %s\n", __func__); 579 580 switch (uptodate) { 581 case 0: error = IDETAPE_ERROR_GENERAL; break; 582 case 1: error = 0; break; 583 default: error = uptodate; 584 } 585 rq->errors = error; 586 if (error) 587 tape->failed_pc = NULL; 588 589 if (!blk_special_request(rq)) { 590 ide_end_request(drive, uptodate, nr_sects); 591 return 0; 592 } 593 594 spin_lock_irqsave(&tape->lock, flags); 595 596 ide_end_drive_cmd(drive, 0, 0); 597 598 spin_unlock_irqrestore(&tape->lock, flags); 599 return 0; 600} 601 602static void ide_tape_callback(ide_drive_t *drive) 603{ 604 idetape_tape_t *tape = drive->driver_data; 605 struct ide_atapi_pc *pc = tape->pc; 606 int uptodate = pc->error ? 0 : 1; 607 608 debug_log(DBG_PROCS, "Enter %s\n", __func__); 609 610 if (tape->failed_pc == pc) 611 tape->failed_pc = NULL; 612 613 if (pc->c[0] == REQUEST_SENSE) { 614 if (uptodate) 615 idetape_analyze_error(drive, pc->buf); 616 else 617 printk(KERN_ERR "ide-tape: Error in REQUEST SENSE " 618 "itself - Aborting request!\n"); 619 } else if (pc->c[0] == READ_6 || pc->c[0] == WRITE_6) { 620 struct request *rq = drive->hwif->hwgroup->rq; 621 int blocks = pc->xferred / tape->blk_size; 622 623 tape->avg_size += blocks * tape->blk_size; 624 625 if (time_after_eq(jiffies, tape->avg_time + HZ)) { 626 tape->avg_speed = tape->avg_size * HZ / 627 (jiffies - tape->avg_time) / 1024; 628 tape->avg_size = 0; 629 tape->avg_time = jiffies; 630 } 631 632 tape->first_frame += blocks; 633 rq->current_nr_sectors -= blocks; 634 635 if (pc->error) 636 uptodate = pc->error; 637 } else if (pc->c[0] == READ_POSITION && uptodate) { 638 u8 *readpos = tape->pc->buf; 639 640 debug_log(DBG_SENSE, "BOP - %s\n", 641 (readpos[0] & 0x80) ? "Yes" : "No"); 642 debug_log(DBG_SENSE, "EOP - %s\n", 643 (readpos[0] & 0x40) ? "Yes" : "No"); 644 645 if (readpos[0] & 0x4) { 646 printk(KERN_INFO "ide-tape: Block location is unknown" 647 "to the tape\n"); 648 clear_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags); 649 uptodate = 0; 650 } else { 651 debug_log(DBG_SENSE, "Block Location - %u\n", 652 be32_to_cpu(*(u32 *)&readpos[4])); 653 654 tape->partition = readpos[1]; 655 tape->first_frame = be32_to_cpu(*(u32 *)&readpos[4]); 656 set_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags); 657 } 658 } 659 660 idetape_end_request(drive, uptodate, 0); 661} 662 663static void idetape_init_pc(struct ide_atapi_pc *pc) 664{ 665 memset(pc->c, 0, 12); 666 pc->retries = 0; 667 pc->flags = 0; 668 pc->req_xfer = 0; 669 pc->buf = pc->pc_buf; 670 pc->buf_size = IDETAPE_PC_BUFFER_SIZE; 671 pc->bh = NULL; 672 pc->b_data = NULL; 673} 674 675static void idetape_create_request_sense_cmd(struct ide_atapi_pc *pc) 676{ 677 idetape_init_pc(pc); 678 pc->c[0] = REQUEST_SENSE; 679 pc->c[4] = 20; 680 pc->req_xfer = 20; 681} 682 683static void idetape_init_rq(struct request *rq, u8 cmd) 684{ 685 blk_rq_init(NULL, rq); 686 rq->cmd_type = REQ_TYPE_SPECIAL; 687 rq->cmd[13] = cmd; 688} 689 690/* 691 * Generate a new packet command request in front of the request queue, before 692 * the current request, so that it will be processed immediately, on the next 693 * pass through the driver. The function below is called from the request 694 * handling part of the driver (the "bottom" part). Safe storage for the request 695 * should be allocated with ide_tape_next_{pc,rq}_storage() prior to that. 696 * 697 * Memory for those requests is pre-allocated at initialization time, and is 698 * limited to IDETAPE_PC_STACK requests. We assume that we have enough space for 699 * the maximum possible number of inter-dependent packet commands. 700 * 701 * The higher level of the driver - The ioctl handler and the character device 702 * handling functions should queue request to the lower level part and wait for 703 * their completion using idetape_queue_pc_tail or idetape_queue_rw_tail. 704 */ 705static void idetape_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc, 706 struct request *rq) 707{ 708 struct ide_tape_obj *tape = drive->driver_data; 709 710 idetape_init_rq(rq, REQ_IDETAPE_PC1); 711 rq->cmd_flags |= REQ_PREEMPT; 712 rq->buffer = (char *) pc; 713 rq->rq_disk = tape->disk; 714 memcpy(rq->cmd, pc->c, 12); 715 ide_do_drive_cmd(drive, rq); 716} 717 718/* 719 * idetape_retry_pc is called when an error was detected during the 720 * last packet command. We queue a request sense packet command in 721 * the head of the request list. 722 */ 723static void idetape_retry_pc(ide_drive_t *drive) 724{ 725 struct ide_atapi_pc *pc; 726 struct request *rq; 727 728 (void)ide_read_error(drive); 729 pc = idetape_next_pc_storage(drive); 730 rq = idetape_next_rq_storage(drive); 731 idetape_create_request_sense_cmd(pc); 732 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags); 733 idetape_queue_pc_head(drive, pc, rq); 734} 735 736/* 737 * Postpone the current request so that ide.c will be able to service requests 738 * from another device on the same hwgroup while we are polling for DSC. 739 */ 740static void idetape_postpone_request(ide_drive_t *drive) 741{ 742 idetape_tape_t *tape = drive->driver_data; 743 744 debug_log(DBG_PROCS, "Enter %s\n", __func__); 745 746 tape->postponed_rq = HWGROUP(drive)->rq; 747 ide_stall_queue(drive, tape->dsc_poll_freq); 748} 749 750static void ide_tape_handle_dsc(ide_drive_t *drive) 751{ 752 idetape_tape_t *tape = drive->driver_data; 753 754 /* Media access command */ 755 tape->dsc_polling_start = jiffies; 756 tape->dsc_poll_freq = IDETAPE_DSC_MA_FAST; 757 tape->dsc_timeout = jiffies + IDETAPE_DSC_MA_TIMEOUT; 758 /* Allow ide.c to handle other requests */ 759 idetape_postpone_request(drive); 760} 761 762static void ide_tape_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc, 763 unsigned int bcount, int write) 764{ 765 if (write) 766 idetape_output_buffers(drive, pc, bcount); 767 else 768 idetape_input_buffers(drive, pc, bcount); 769} 770 771/* 772 * This is the usual interrupt handler which will be called during a packet 773 * command. We will transfer some of the data (as requested by the drive) and 774 * will re-point interrupt handler to us. When data transfer is finished, we 775 * will act according to the algorithm described before 776 * idetape_issue_pc. 777 */ 778static ide_startstop_t idetape_pc_intr(ide_drive_t *drive) 779{ 780 idetape_tape_t *tape = drive->driver_data; 781 782 return ide_pc_intr(drive, tape->pc, idetape_pc_intr, IDETAPE_WAIT_CMD, 783 NULL, idetape_update_buffers, idetape_retry_pc, 784 ide_tape_handle_dsc, ide_tape_io_buffers); 785} 786 787/* 788 * Packet Command Interface 789 * 790 * The current Packet Command is available in tape->pc, and will not change 791 * until we finish handling it. Each packet command is associated with a 792 * callback function that will be called when the command is finished. 793 * 794 * The handling will be done in three stages: 795 * 796 * 1. idetape_issue_pc will send the packet command to the drive, and will set 797 * the interrupt handler to idetape_pc_intr. 798 * 799 * 2. On each interrupt, idetape_pc_intr will be called. This step will be 800 * repeated until the device signals us that no more interrupts will be issued. 801 * 802 * 3. ATAPI Tape media access commands have immediate status with a delayed 803 * process. In case of a successful initiation of a media access packet command, 804 * the DSC bit will be set when the actual execution of the command is finished. 805 * Since the tape drive will not issue an interrupt, we have to poll for this 806 * event. In this case, we define the request as "low priority request" by 807 * setting rq_status to IDETAPE_RQ_POSTPONED, set a timer to poll for DSC and 808 * exit the driver. 809 * 810 * ide.c will then give higher priority to requests which originate from the 811 * other device, until will change rq_status to RQ_ACTIVE. 812 * 813 * 4. When the packet command is finished, it will be checked for errors. 814 * 815 * 5. In case an error was found, we queue a request sense packet command in 816 * front of the request queue and retry the operation up to 817 * IDETAPE_MAX_PC_RETRIES times. 818 * 819 * 6. In case no error was found, or we decided to give up and not to retry 820 * again, the callback function will be called and then we will handle the next 821 * request. 822 */ 823static ide_startstop_t idetape_transfer_pc(ide_drive_t *drive) 824{ 825 idetape_tape_t *tape = drive->driver_data; 826 827 return ide_transfer_pc(drive, tape->pc, idetape_pc_intr, 828 IDETAPE_WAIT_CMD, NULL); 829} 830 831static ide_startstop_t idetape_issue_pc(ide_drive_t *drive, 832 struct ide_atapi_pc *pc) 833{ 834 idetape_tape_t *tape = drive->driver_data; 835 836 if (tape->pc->c[0] == REQUEST_SENSE && 837 pc->c[0] == REQUEST_SENSE) { 838 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - " 839 "Two request sense in serial were issued\n"); 840 } 841 842 if (tape->failed_pc == NULL && pc->c[0] != REQUEST_SENSE) 843 tape->failed_pc = pc; 844 /* Set the current packet command */ 845 tape->pc = pc; 846 847 if (pc->retries > IDETAPE_MAX_PC_RETRIES || 848 (pc->flags & PC_FLAG_ABORT)) { 849 /* 850 * We will "abort" retrying a packet command in case legitimate 851 * error code was received (crossing a filemark, or end of the 852 * media, for example). 853 */ 854 if (!(pc->flags & PC_FLAG_ABORT)) { 855 if (!(pc->c[0] == TEST_UNIT_READY && 856 tape->sense_key == 2 && tape->asc == 4 && 857 (tape->ascq == 1 || tape->ascq == 8))) { 858 printk(KERN_ERR "ide-tape: %s: I/O error, " 859 "pc = %2x, key = %2x, " 860 "asc = %2x, ascq = %2x\n", 861 tape->name, pc->c[0], 862 tape->sense_key, tape->asc, 863 tape->ascq); 864 } 865 /* Giving up */ 866 pc->error = IDETAPE_ERROR_GENERAL; 867 } 868 tape->failed_pc = NULL; 869 drive->pc_callback(drive); 870 return ide_stopped; 871 } 872 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]); 873 874 pc->retries++; 875 876 return ide_issue_pc(drive, pc, idetape_transfer_pc, 877 IDETAPE_WAIT_CMD, NULL); 878} 879 880/* A mode sense command is used to "sense" tape parameters. */ 881static void idetape_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code) 882{ 883 idetape_init_pc(pc); 884 pc->c[0] = MODE_SENSE; 885 if (page_code != IDETAPE_BLOCK_DESCRIPTOR) 886 /* DBD = 1 - Don't return block descriptors */ 887 pc->c[1] = 8; 888 pc->c[2] = page_code; 889 /* 890 * Changed pc->c[3] to 0 (255 will at best return unused info). 891 * 892 * For SCSI this byte is defined as subpage instead of high byte 893 * of length and some IDE drives seem to interpret it this way 894 * and return an error when 255 is used. 895 */ 896 pc->c[3] = 0; 897 /* We will just discard data in that case */ 898 pc->c[4] = 255; 899 if (page_code == IDETAPE_BLOCK_DESCRIPTOR) 900 pc->req_xfer = 12; 901 else if (page_code == IDETAPE_CAPABILITIES_PAGE) 902 pc->req_xfer = 24; 903 else 904 pc->req_xfer = 50; 905} 906 907static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive) 908{ 909 ide_hwif_t *hwif = drive->hwif; 910 idetape_tape_t *tape = drive->driver_data; 911 struct ide_atapi_pc *pc = tape->pc; 912 u8 stat; 913 914 stat = hwif->tp_ops->read_status(hwif); 915 916 if (stat & SEEK_STAT) { 917 if (stat & ERR_STAT) { 918 /* Error detected */ 919 if (pc->c[0] != TEST_UNIT_READY) 920 printk(KERN_ERR "ide-tape: %s: I/O error, ", 921 tape->name); 922 /* Retry operation */ 923 idetape_retry_pc(drive); 924 return ide_stopped; 925 } 926 pc->error = 0; 927 } else { 928 pc->error = IDETAPE_ERROR_GENERAL; 929 tape->failed_pc = NULL; 930 } 931 drive->pc_callback(drive); 932 return ide_stopped; 933} 934 935static void ide_tape_create_rw_cmd(idetape_tape_t *tape, 936 struct ide_atapi_pc *pc, struct request *rq, 937 u8 opcode) 938{ 939 struct idetape_bh *bh = (struct idetape_bh *)rq->special; 940 unsigned int length = rq->current_nr_sectors; 941 942 idetape_init_pc(pc); 943 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]); 944 pc->c[1] = 1; 945 pc->bh = bh; 946 pc->buf = NULL; 947 pc->buf_size = length * tape->blk_size; 948 pc->req_xfer = pc->buf_size; 949 if (pc->req_xfer == tape->buffer_size) 950 pc->flags |= PC_FLAG_DMA_OK; 951 952 if (opcode == READ_6) { 953 pc->c[0] = READ_6; 954 atomic_set(&bh->b_count, 0); 955 } else if (opcode == WRITE_6) { 956 pc->c[0] = WRITE_6; 957 pc->flags |= PC_FLAG_WRITING; 958 pc->b_data = bh->b_data; 959 pc->b_count = atomic_read(&bh->b_count); 960 } 961 962 memcpy(rq->cmd, pc->c, 12); 963} 964 965static ide_startstop_t idetape_do_request(ide_drive_t *drive, 966 struct request *rq, sector_t block) 967{ 968 ide_hwif_t *hwif = drive->hwif; 969 idetape_tape_t *tape = drive->driver_data; 970 struct ide_atapi_pc *pc = NULL; 971 struct request *postponed_rq = tape->postponed_rq; 972 u8 stat; 973 974 debug_log(DBG_SENSE, "sector: %ld, nr_sectors: %ld," 975 " current_nr_sectors: %d\n", 976 rq->sector, rq->nr_sectors, rq->current_nr_sectors); 977 978 if (!blk_special_request(rq)) { 979 /* We do not support buffer cache originated requests. */ 980 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in " 981 "request queue (%d)\n", drive->name, rq->cmd_type); 982 ide_end_request(drive, 0, 0); 983 return ide_stopped; 984 } 985 986 /* Retry a failed packet command */ 987 if (tape->failed_pc && tape->pc->c[0] == REQUEST_SENSE) { 988 pc = tape->failed_pc; 989 goto out; 990 } 991 992 if (postponed_rq != NULL) 993 if (rq != postponed_rq) { 994 printk(KERN_ERR "ide-tape: ide-tape.c bug - " 995 "Two DSC requests were queued\n"); 996 idetape_end_request(drive, 0, 0); 997 return ide_stopped; 998 } 999 1000 tape->postponed_rq = NULL; 1001 1002 /* 1003 * If the tape is still busy, postpone our request and service 1004 * the other device meanwhile. 1005 */ 1006 stat = hwif->tp_ops->read_status(hwif); 1007 1008 if (!drive->dsc_overlap && !(rq->cmd[13] & REQ_IDETAPE_PC2)) 1009 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags); 1010 1011 if (drive->post_reset == 1) { 1012 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags); 1013 drive->post_reset = 0; 1014 } 1015 1016 if (!test_and_clear_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags) && 1017 (stat & SEEK_STAT) == 0) { 1018 if (postponed_rq == NULL) { 1019 tape->dsc_polling_start = jiffies; 1020 tape->dsc_poll_freq = tape->best_dsc_rw_freq; 1021 tape->dsc_timeout = jiffies + IDETAPE_DSC_RW_TIMEOUT; 1022 } else if (time_after(jiffies, tape->dsc_timeout)) { 1023 printk(KERN_ERR "ide-tape: %s: DSC timeout\n", 1024 tape->name); 1025 if (rq->cmd[13] & REQ_IDETAPE_PC2) { 1026 idetape_media_access_finished(drive); 1027 return ide_stopped; 1028 } else { 1029 return ide_do_reset(drive); 1030 } 1031 } else if (time_after(jiffies, 1032 tape->dsc_polling_start + 1033 IDETAPE_DSC_MA_THRESHOLD)) 1034 tape->dsc_poll_freq = IDETAPE_DSC_MA_SLOW; 1035 idetape_postpone_request(drive); 1036 return ide_stopped; 1037 } 1038 if (rq->cmd[13] & REQ_IDETAPE_READ) { 1039 pc = idetape_next_pc_storage(drive); 1040 ide_tape_create_rw_cmd(tape, pc, rq, READ_6); 1041 goto out; 1042 } 1043 if (rq->cmd[13] & REQ_IDETAPE_WRITE) { 1044 pc = idetape_next_pc_storage(drive); 1045 ide_tape_create_rw_cmd(tape, pc, rq, WRITE_6); 1046 goto out; 1047 } 1048 if (rq->cmd[13] & REQ_IDETAPE_PC1) { 1049 pc = (struct ide_atapi_pc *) rq->buffer; 1050 rq->cmd[13] &= ~(REQ_IDETAPE_PC1); 1051 rq->cmd[13] |= REQ_IDETAPE_PC2; 1052 goto out; 1053 } 1054 if (rq->cmd[13] & REQ_IDETAPE_PC2) { 1055 idetape_media_access_finished(drive); 1056 return ide_stopped; 1057 } 1058 BUG(); 1059 1060out: 1061 return idetape_issue_pc(drive, pc); 1062} 1063 1064/* 1065 * The function below uses __get_free_pages to allocate a data buffer of size 1066 * tape->buffer_size (or a bit more). We attempt to combine sequential pages as 1067 * much as possible. 1068 * 1069 * It returns a pointer to the newly allocated buffer, or NULL in case of 1070 * failure. 1071 */ 1072static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape, 1073 int full, int clear) 1074{ 1075 struct idetape_bh *prev_bh, *bh, *merge_bh; 1076 int pages = tape->pages_per_buffer; 1077 unsigned int order, b_allocd; 1078 char *b_data = NULL; 1079 1080 merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL); 1081 bh = merge_bh; 1082 if (bh == NULL) 1083 goto abort; 1084 1085 order = fls(pages) - 1; 1086 bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order); 1087 if (!bh->b_data) 1088 goto abort; 1089 b_allocd = (1 << order) * PAGE_SIZE; 1090 pages &= (order-1); 1091 1092 if (clear) 1093 memset(bh->b_data, 0, b_allocd); 1094 bh->b_reqnext = NULL; 1095 bh->b_size = b_allocd; 1096 atomic_set(&bh->b_count, full ? bh->b_size : 0); 1097 1098 while (pages) { 1099 order = fls(pages) - 1; 1100 b_data = (char *) __get_free_pages(GFP_KERNEL, order); 1101 if (!b_data) 1102 goto abort; 1103 b_allocd = (1 << order) * PAGE_SIZE; 1104 1105 if (clear) 1106 memset(b_data, 0, b_allocd); 1107 1108 /* newly allocated page frames below buffer header or ...*/ 1109 if (bh->b_data == b_data + b_allocd) { 1110 bh->b_size += b_allocd; 1111 bh->b_data -= b_allocd; 1112 if (full) 1113 atomic_add(b_allocd, &bh->b_count); 1114 continue; 1115 } 1116 /* they are above the header */ 1117 if (b_data == bh->b_data + bh->b_size) { 1118 bh->b_size += b_allocd; 1119 if (full) 1120 atomic_add(b_allocd, &bh->b_count); 1121 continue; 1122 } 1123 prev_bh = bh; 1124 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL); 1125 if (!bh) { 1126 free_pages((unsigned long) b_data, order); 1127 goto abort; 1128 } 1129 bh->b_reqnext = NULL; 1130 bh->b_data = b_data; 1131 bh->b_size = b_allocd; 1132 atomic_set(&bh->b_count, full ? bh->b_size : 0); 1133 prev_bh->b_reqnext = bh; 1134 1135 pages &= (order-1); 1136 } 1137 1138 bh->b_size -= tape->excess_bh_size; 1139 if (full) 1140 atomic_sub(tape->excess_bh_size, &bh->b_count); 1141 return merge_bh; 1142abort: 1143 ide_tape_kfree_buffer(tape); 1144 return NULL; 1145} 1146 1147static int idetape_copy_stage_from_user(idetape_tape_t *tape, 1148 const char __user *buf, int n) 1149{ 1150 struct idetape_bh *bh = tape->bh; 1151 int count; 1152 int ret = 0; 1153 1154 while (n) { 1155 if (bh == NULL) { 1156 printk(KERN_ERR "ide-tape: bh == NULL in %s\n", 1157 __func__); 1158 return 1; 1159 } 1160 count = min((unsigned int) 1161 (bh->b_size - atomic_read(&bh->b_count)), 1162 (unsigned int)n); 1163 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, 1164 count)) 1165 ret = 1; 1166 n -= count; 1167 atomic_add(count, &bh->b_count); 1168 buf += count; 1169 if (atomic_read(&bh->b_count) == bh->b_size) { 1170 bh = bh->b_reqnext; 1171 if (bh) 1172 atomic_set(&bh->b_count, 0); 1173 } 1174 } 1175 tape->bh = bh; 1176 return ret; 1177} 1178 1179static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf, 1180 int n) 1181{ 1182 struct idetape_bh *bh = tape->bh; 1183 int count; 1184 int ret = 0; 1185 1186 while (n) { 1187 if (bh == NULL) { 1188 printk(KERN_ERR "ide-tape: bh == NULL in %s\n", 1189 __func__); 1190 return 1; 1191 } 1192 count = min(tape->b_count, n); 1193 if (copy_to_user(buf, tape->b_data, count)) 1194 ret = 1; 1195 n -= count; 1196 tape->b_data += count; 1197 tape->b_count -= count; 1198 buf += count; 1199 if (!tape->b_count) { 1200 bh = bh->b_reqnext; 1201 tape->bh = bh; 1202 if (bh) { 1203 tape->b_data = bh->b_data; 1204 tape->b_count = atomic_read(&bh->b_count); 1205 } 1206 } 1207 } 1208 return ret; 1209} 1210 1211static void idetape_init_merge_buffer(idetape_tape_t *tape) 1212{ 1213 struct idetape_bh *bh = tape->merge_bh; 1214 tape->bh = tape->merge_bh; 1215 1216 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) 1217 atomic_set(&bh->b_count, 0); 1218 else { 1219 tape->b_data = bh->b_data; 1220 tape->b_count = atomic_read(&bh->b_count); 1221 } 1222} 1223 1224/* 1225 * Write a filemark if write_filemark=1. Flush the device buffers without 1226 * writing a filemark otherwise. 1227 */ 1228static void idetape_create_write_filemark_cmd(ide_drive_t *drive, 1229 struct ide_atapi_pc *pc, int write_filemark) 1230{ 1231 idetape_init_pc(pc); 1232 pc->c[0] = WRITE_FILEMARKS; 1233 pc->c[4] = write_filemark; 1234 pc->flags |= PC_FLAG_WAIT_FOR_DSC; 1235} 1236 1237static void idetape_create_test_unit_ready_cmd(struct ide_atapi_pc *pc) 1238{ 1239 idetape_init_pc(pc); 1240 pc->c[0] = TEST_UNIT_READY; 1241} 1242 1243/* 1244 * We add a special packet command request to the tail of the request queue, and 1245 * wait for it to be serviced. This is not to be called from within the request 1246 * handling part of the driver! We allocate here data on the stack and it is 1247 * valid until the request is finished. This is not the case for the bottom part 1248 * of the driver, where we are always leaving the functions to wait for an 1249 * interrupt or a timer event. 1250 * 1251 * From the bottom part of the driver, we should allocate safe memory using 1252 * idetape_next_pc_storage() and ide_tape_next_rq_storage(), and add the request 1253 * to the request list without waiting for it to be serviced! In that case, we 1254 * usually use idetape_queue_pc_head(). 1255 */ 1256static int idetape_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc) 1257{ 1258 struct ide_tape_obj *tape = drive->driver_data; 1259 struct request *rq; 1260 int error; 1261 1262 rq = blk_get_request(drive->queue, READ, __GFP_WAIT); 1263 rq->cmd_type = REQ_TYPE_SPECIAL; 1264 rq->cmd[13] = REQ_IDETAPE_PC1; 1265 rq->buffer = (char *)pc; 1266 memcpy(rq->cmd, pc->c, 12); 1267 error = blk_execute_rq(drive->queue, tape->disk, rq, 0); 1268 blk_put_request(rq); 1269 return error; 1270} 1271 1272static void idetape_create_load_unload_cmd(ide_drive_t *drive, 1273 struct ide_atapi_pc *pc, int cmd) 1274{ 1275 idetape_init_pc(pc); 1276 pc->c[0] = START_STOP; 1277 pc->c[4] = cmd; 1278 pc->flags |= PC_FLAG_WAIT_FOR_DSC; 1279} 1280 1281static int idetape_wait_ready(ide_drive_t *drive, unsigned long timeout) 1282{ 1283 idetape_tape_t *tape = drive->driver_data; 1284 struct ide_atapi_pc pc; 1285 int load_attempted = 0; 1286 1287 /* Wait for the tape to become ready */ 1288 set_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags); 1289 timeout += jiffies; 1290 while (time_before(jiffies, timeout)) { 1291 idetape_create_test_unit_ready_cmd(&pc); 1292 if (!idetape_queue_pc_tail(drive, &pc)) 1293 return 0; 1294 if ((tape->sense_key == 2 && tape->asc == 4 && tape->ascq == 2) 1295 || (tape->asc == 0x3A)) { 1296 /* no media */ 1297 if (load_attempted) 1298 return -ENOMEDIUM; 1299 idetape_create_load_unload_cmd(drive, &pc, 1300 IDETAPE_LU_LOAD_MASK); 1301 idetape_queue_pc_tail(drive, &pc); 1302 load_attempted = 1; 1303 /* not about to be ready */ 1304 } else if (!(tape->sense_key == 2 && tape->asc == 4 && 1305 (tape->ascq == 1 || tape->ascq == 8))) 1306 return -EIO; 1307 msleep(100); 1308 } 1309 return -EIO; 1310} 1311 1312static int idetape_flush_tape_buffers(ide_drive_t *drive) 1313{ 1314 struct ide_atapi_pc pc; 1315 int rc; 1316 1317 idetape_create_write_filemark_cmd(drive, &pc, 0); 1318 rc = idetape_queue_pc_tail(drive, &pc); 1319 if (rc) 1320 return rc; 1321 idetape_wait_ready(drive, 60 * 5 * HZ); 1322 return 0; 1323} 1324 1325static void idetape_create_read_position_cmd(struct ide_atapi_pc *pc) 1326{ 1327 idetape_init_pc(pc); 1328 pc->c[0] = READ_POSITION; 1329 pc->req_xfer = 20; 1330} 1331 1332static int idetape_read_position(ide_drive_t *drive) 1333{ 1334 idetape_tape_t *tape = drive->driver_data; 1335 struct ide_atapi_pc pc; 1336 int position; 1337 1338 debug_log(DBG_PROCS, "Enter %s\n", __func__); 1339 1340 idetape_create_read_position_cmd(&pc); 1341 if (idetape_queue_pc_tail(drive, &pc)) 1342 return -1; 1343 position = tape->first_frame; 1344 return position; 1345} 1346 1347static void idetape_create_locate_cmd(ide_drive_t *drive, 1348 struct ide_atapi_pc *pc, 1349 unsigned int block, u8 partition, int skip) 1350{ 1351 idetape_init_pc(pc); 1352 pc->c[0] = POSITION_TO_ELEMENT; 1353 pc->c[1] = 2; 1354 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[3]); 1355 pc->c[8] = partition; 1356 pc->flags |= PC_FLAG_WAIT_FOR_DSC; 1357} 1358 1359static int idetape_create_prevent_cmd(ide_drive_t *drive, 1360 struct ide_atapi_pc *pc, int prevent) 1361{ 1362 idetape_tape_t *tape = drive->driver_data; 1363 1364 /* device supports locking according to capabilities page */ 1365 if (!(tape->caps[6] & 0x01)) 1366 return 0; 1367 1368 idetape_init_pc(pc); 1369 pc->c[0] = ALLOW_MEDIUM_REMOVAL; 1370 pc->c[4] = prevent; 1371 return 1; 1372} 1373 1374static void __ide_tape_discard_merge_buffer(ide_drive_t *drive) 1375{ 1376 idetape_tape_t *tape = drive->driver_data; 1377 1378 if (tape->chrdev_dir != IDETAPE_DIR_READ) 1379 return; 1380 1381 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags); 1382 tape->merge_bh_size = 0; 1383 if (tape->merge_bh != NULL) { 1384 ide_tape_kfree_buffer(tape); 1385 tape->merge_bh = NULL; 1386 } 1387 1388 tape->chrdev_dir = IDETAPE_DIR_NONE; 1389} 1390 1391/* 1392 * Position the tape to the requested block using the LOCATE packet command. 1393 * A READ POSITION command is then issued to check where we are positioned. Like 1394 * all higher level operations, we queue the commands at the tail of the request 1395 * queue and wait for their completion. 1396 */ 1397static int idetape_position_tape(ide_drive_t *drive, unsigned int block, 1398 u8 partition, int skip) 1399{ 1400 idetape_tape_t *tape = drive->driver_data; 1401 int retval; 1402 struct ide_atapi_pc pc; 1403 1404 if (tape->chrdev_dir == IDETAPE_DIR_READ) 1405 __ide_tape_discard_merge_buffer(drive); 1406 idetape_wait_ready(drive, 60 * 5 * HZ); 1407 idetape_create_locate_cmd(drive, &pc, block, partition, skip); 1408 retval = idetape_queue_pc_tail(drive, &pc); 1409 if (retval) 1410 return (retval); 1411 1412 idetape_create_read_position_cmd(&pc); 1413 return (idetape_queue_pc_tail(drive, &pc)); 1414} 1415 1416static void ide_tape_discard_merge_buffer(ide_drive_t *drive, 1417 int restore_position) 1418{ 1419 idetape_tape_t *tape = drive->driver_data; 1420 int seek, position; 1421 1422 __ide_tape_discard_merge_buffer(drive); 1423 if (restore_position) { 1424 position = idetape_read_position(drive); 1425 seek = position > 0 ? position : 0; 1426 if (idetape_position_tape(drive, seek, 0, 0)) { 1427 printk(KERN_INFO "ide-tape: %s: position_tape failed in" 1428 " %s\n", tape->name, __func__); 1429 return; 1430 } 1431 } 1432} 1433 1434/* 1435 * Generate a read/write request for the block device interface and wait for it 1436 * to be serviced. 1437 */ 1438static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, 1439 struct idetape_bh *bh) 1440{ 1441 idetape_tape_t *tape = drive->driver_data; 1442 struct request *rq; 1443 int ret, errors; 1444 1445 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd); 1446 1447 rq = blk_get_request(drive->queue, READ, __GFP_WAIT); 1448 rq->cmd_type = REQ_TYPE_SPECIAL; 1449 rq->cmd[13] = cmd; 1450 rq->rq_disk = tape->disk; 1451 rq->special = (void *)bh; 1452 rq->sector = tape->first_frame; 1453 rq->nr_sectors = blocks; 1454 rq->current_nr_sectors = blocks; 1455 blk_execute_rq(drive->queue, tape->disk, rq, 0); 1456 1457 errors = rq->errors; 1458 ret = tape->blk_size * (blocks - rq->current_nr_sectors); 1459 blk_put_request(rq); 1460 1461 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0) 1462 return 0; 1463 1464 if (tape->merge_bh) 1465 idetape_init_merge_buffer(tape); 1466 if (errors == IDETAPE_ERROR_GENERAL) 1467 return -EIO; 1468 return ret; 1469} 1470 1471static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc) 1472{ 1473 idetape_init_pc(pc); 1474 pc->c[0] = INQUIRY; 1475 pc->c[4] = 254; 1476 pc->req_xfer = 254; 1477} 1478 1479static void idetape_create_rewind_cmd(ide_drive_t *drive, 1480 struct ide_atapi_pc *pc) 1481{ 1482 idetape_init_pc(pc); 1483 pc->c[0] = REZERO_UNIT; 1484 pc->flags |= PC_FLAG_WAIT_FOR_DSC; 1485} 1486 1487static void idetape_create_erase_cmd(struct ide_atapi_pc *pc) 1488{ 1489 idetape_init_pc(pc); 1490 pc->c[0] = ERASE; 1491 pc->c[1] = 1; 1492 pc->flags |= PC_FLAG_WAIT_FOR_DSC; 1493} 1494 1495static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd) 1496{ 1497 idetape_init_pc(pc); 1498 pc->c[0] = SPACE; 1499 put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]); 1500 pc->c[1] = cmd; 1501 pc->flags |= PC_FLAG_WAIT_FOR_DSC; 1502} 1503 1504/* Queue up a character device originated write request. */ 1505static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks) 1506{ 1507 idetape_tape_t *tape = drive->driver_data; 1508 1509 debug_log(DBG_CHRDEV, "Enter %s\n", __func__); 1510 1511 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, 1512 blocks, tape->merge_bh); 1513} 1514 1515static void ide_tape_flush_merge_buffer(ide_drive_t *drive) 1516{ 1517 idetape_tape_t *tape = drive->driver_data; 1518 int blocks, min; 1519 struct idetape_bh *bh; 1520 1521 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) { 1522 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer" 1523 " but we are not writing.\n"); 1524 return; 1525 } 1526 if (tape->merge_bh_size > tape->buffer_size) { 1527 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n"); 1528 tape->merge_bh_size = tape->buffer_size; 1529 } 1530 if (tape->merge_bh_size) { 1531 blocks = tape->merge_bh_size / tape->blk_size; 1532 if (tape->merge_bh_size % tape->blk_size) { 1533 unsigned int i; 1534 1535 blocks++; 1536 i = tape->blk_size - tape->merge_bh_size % 1537 tape->blk_size; 1538 bh = tape->bh->b_reqnext; 1539 while (bh) { 1540 atomic_set(&bh->b_count, 0); 1541 bh = bh->b_reqnext; 1542 } 1543 bh = tape->bh; 1544 while (i) { 1545 if (bh == NULL) { 1546 printk(KERN_INFO "ide-tape: bug," 1547 " bh NULL\n"); 1548 break; 1549 } 1550 min = min(i, (unsigned int)(bh->b_size - 1551 atomic_read(&bh->b_count))); 1552 memset(bh->b_data + atomic_read(&bh->b_count), 1553 0, min); 1554 atomic_add(min, &bh->b_count); 1555 i -= min; 1556 bh = bh->b_reqnext; 1557 } 1558 } 1559 (void) idetape_add_chrdev_write_request(drive, blocks); 1560 tape->merge_bh_size = 0; 1561 } 1562 if (tape->merge_bh != NULL) { 1563 ide_tape_kfree_buffer(tape); 1564 tape->merge_bh = NULL; 1565 } 1566 tape->chrdev_dir = IDETAPE_DIR_NONE; 1567} 1568 1569static int idetape_init_read(ide_drive_t *drive) 1570{ 1571 idetape_tape_t *tape = drive->driver_data; 1572 int bytes_read; 1573 1574 /* Initialize read operation */ 1575 if (tape->chrdev_dir != IDETAPE_DIR_READ) { 1576 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) { 1577 ide_tape_flush_merge_buffer(drive); 1578 idetape_flush_tape_buffers(drive); 1579 } 1580 if (tape->merge_bh || tape->merge_bh_size) { 1581 printk(KERN_ERR "ide-tape: merge_bh_size should be" 1582 " 0 now\n"); 1583 tape->merge_bh_size = 0; 1584 } 1585 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0); 1586 if (!tape->merge_bh) 1587 return -ENOMEM; 1588 tape->chrdev_dir = IDETAPE_DIR_READ; 1589 1590 /* 1591 * Issue a read 0 command to ensure that DSC handshake is 1592 * switched from completion mode to buffer available mode. 1593 * No point in issuing this if DSC overlap isn't supported, some 1594 * drives (Seagate STT3401A) will return an error. 1595 */ 1596 if (drive->dsc_overlap) { 1597 bytes_read = idetape_queue_rw_tail(drive, 1598 REQ_IDETAPE_READ, 0, 1599 tape->merge_bh); 1600 if (bytes_read < 0) { 1601 ide_tape_kfree_buffer(tape); 1602 tape->merge_bh = NULL; 1603 tape->chrdev_dir = IDETAPE_DIR_NONE; 1604 return bytes_read; 1605 } 1606 } 1607 } 1608 1609 return 0; 1610} 1611 1612/* called from idetape_chrdev_read() to service a chrdev read request. */ 1613static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks) 1614{ 1615 idetape_tape_t *tape = drive->driver_data; 1616 1617 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks); 1618 1619 /* If we are at a filemark, return a read length of 0 */ 1620 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) 1621 return 0; 1622 1623 idetape_init_read(drive); 1624 1625 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks, 1626 tape->merge_bh); 1627} 1628 1629static void idetape_pad_zeros(ide_drive_t *drive, int bcount) 1630{ 1631 idetape_tape_t *tape = drive->driver_data; 1632 struct idetape_bh *bh; 1633 int blocks; 1634 1635 while (bcount) { 1636 unsigned int count; 1637 1638 bh = tape->merge_bh; 1639 count = min(tape->buffer_size, bcount); 1640 bcount -= count; 1641 blocks = count / tape->blk_size; 1642 while (count) { 1643 atomic_set(&bh->b_count, 1644 min(count, (unsigned int)bh->b_size)); 1645 memset(bh->b_data, 0, atomic_read(&bh->b_count)); 1646 count -= atomic_read(&bh->b_count); 1647 bh = bh->b_reqnext; 1648 } 1649 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, 1650 tape->merge_bh); 1651 } 1652} 1653 1654/* 1655 * Rewinds the tape to the Beginning Of the current Partition (BOP). We 1656 * currently support only one partition. 1657 */ 1658static int idetape_rewind_tape(ide_drive_t *drive) 1659{ 1660 int retval; 1661 struct ide_atapi_pc pc; 1662 idetape_tape_t *tape; 1663 tape = drive->driver_data; 1664 1665 debug_log(DBG_SENSE, "Enter %s\n", __func__); 1666 1667 idetape_create_rewind_cmd(drive, &pc); 1668 retval = idetape_queue_pc_tail(drive, &pc); 1669 if (retval) 1670 return retval; 1671 1672 idetape_create_read_position_cmd(&pc); 1673 retval = idetape_queue_pc_tail(drive, &pc); 1674 if (retval) 1675 return retval; 1676 return 0; 1677} 1678 1679/* mtio.h compatible commands should be issued to the chrdev interface. */ 1680static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, 1681 unsigned long arg) 1682{ 1683 idetape_tape_t *tape = drive->driver_data; 1684 void __user *argp = (void __user *)arg; 1685 1686 struct idetape_config { 1687 int dsc_rw_frequency; 1688 int dsc_media_access_frequency; 1689 int nr_stages; 1690 } config; 1691 1692 debug_log(DBG_PROCS, "Enter %s\n", __func__); 1693 1694 switch (cmd) { 1695 case 0x0340: 1696 if (copy_from_user(&config, argp, sizeof(config))) 1697 return -EFAULT; 1698 tape->best_dsc_rw_freq = config.dsc_rw_frequency; 1699 break; 1700 case 0x0350: 1701 config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq; 1702 config.nr_stages = 1; 1703 if (copy_to_user(argp, &config, sizeof(config))) 1704 return -EFAULT; 1705 break; 1706 default: 1707 return -EIO; 1708 } 1709 return 0; 1710} 1711 1712static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op, 1713 int mt_count) 1714{ 1715 idetape_tape_t *tape = drive->driver_data; 1716 struct ide_atapi_pc pc; 1717 int retval, count = 0; 1718 int sprev = !!(tape->caps[4] & 0x20); 1719 1720 if (mt_count == 0) 1721 return 0; 1722 if (MTBSF == mt_op || MTBSFM == mt_op) { 1723 if (!sprev) 1724 return -EIO; 1725 mt_count = -mt_count; 1726 } 1727 1728 if (tape->chrdev_dir == IDETAPE_DIR_READ) { 1729 tape->merge_bh_size = 0; 1730 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) 1731 ++count; 1732 ide_tape_discard_merge_buffer(drive, 0); 1733 } 1734 1735 switch (mt_op) { 1736 case MTFSF: 1737 case MTBSF: 1738 idetape_create_space_cmd(&pc, mt_count - count, 1739 IDETAPE_SPACE_OVER_FILEMARK); 1740 return idetape_queue_pc_tail(drive, &pc); 1741 case MTFSFM: 1742 case MTBSFM: 1743 if (!sprev) 1744 return -EIO; 1745 retval = idetape_space_over_filemarks(drive, MTFSF, 1746 mt_count - count); 1747 if (retval) 1748 return retval; 1749 count = (MTBSFM == mt_op ? 1 : -1); 1750 return idetape_space_over_filemarks(drive, MTFSF, count); 1751 default: 1752 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n", 1753 mt_op); 1754 return -EIO; 1755 } 1756} 1757 1758/* 1759 * Our character device read / write functions. 1760 * 1761 * The tape is optimized to maximize throughput when it is transferring an 1762 * integral number of the "continuous transfer limit", which is a parameter of 1763 * the specific tape (26kB on my particular tape, 32kB for Onstream). 1764 * 1765 * As of version 1.3 of the driver, the character device provides an abstract 1766 * continuous view of the media - any mix of block sizes (even 1 byte) on the 1767 * same backup/restore procedure is supported. The driver will internally 1768 * convert the requests to the recommended transfer unit, so that an unmatch 1769 * between the user's block size to the recommended size will only result in a 1770 * (slightly) increased driver overhead, but will no longer hit performance. 1771 * This is not applicable to Onstream. 1772 */ 1773static ssize_t idetape_chrdev_read(struct file *file, char __user *buf, 1774 size_t count, loff_t *ppos) 1775{ 1776 struct ide_tape_obj *tape = ide_tape_f(file); 1777 ide_drive_t *drive = tape->drive; 1778 ssize_t bytes_read, temp, actually_read = 0, rc; 1779 ssize_t ret = 0; 1780 u16 ctl = *(u16 *)&tape->caps[12]; 1781 1782 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count); 1783 1784 if (tape->chrdev_dir != IDETAPE_DIR_READ) { 1785 if (test_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags)) 1786 if (count > tape->blk_size && 1787 (count % tape->blk_size) == 0) 1788 tape->user_bs_factor = count / tape->blk_size; 1789 } 1790 rc = idetape_init_read(drive); 1791 if (rc < 0) 1792 return rc; 1793 if (count == 0) 1794 return (0); 1795 if (tape->merge_bh_size) { 1796 actually_read = min((unsigned int)(tape->merge_bh_size), 1797 (unsigned int)count); 1798 if (idetape_copy_stage_to_user(tape, buf, actually_read)) 1799 ret = -EFAULT; 1800 buf += actually_read; 1801 tape->merge_bh_size -= actually_read; 1802 count -= actually_read; 1803 } 1804 while (count >= tape->buffer_size) { 1805 bytes_read = idetape_add_chrdev_read_request(drive, ctl); 1806 if (bytes_read <= 0) 1807 goto finish; 1808 if (idetape_copy_stage_to_user(tape, buf, bytes_read)) 1809 ret = -EFAULT; 1810 buf += bytes_read; 1811 count -= bytes_read; 1812 actually_read += bytes_read; 1813 } 1814 if (count) { 1815 bytes_read = idetape_add_chrdev_read_request(drive, ctl); 1816 if (bytes_read <= 0) 1817 goto finish; 1818 temp = min((unsigned long)count, (unsigned long)bytes_read); 1819 if (idetape_copy_stage_to_user(tape, buf, temp)) 1820 ret = -EFAULT; 1821 actually_read += temp; 1822 tape->merge_bh_size = bytes_read-temp; 1823 } 1824finish: 1825 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) { 1826 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name); 1827 1828 idetape_space_over_filemarks(drive, MTFSF, 1); 1829 return 0; 1830 } 1831 1832 return ret ? ret : actually_read; 1833} 1834 1835static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf, 1836 size_t count, loff_t *ppos) 1837{ 1838 struct ide_tape_obj *tape = ide_tape_f(file); 1839 ide_drive_t *drive = tape->drive; 1840 ssize_t actually_written = 0; 1841 ssize_t ret = 0; 1842 u16 ctl = *(u16 *)&tape->caps[12]; 1843 1844 /* The drive is write protected. */ 1845 if (tape->write_prot) 1846 return -EACCES; 1847 1848 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count); 1849 1850 /* Initialize write operation */ 1851 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) { 1852 if (tape->chrdev_dir == IDETAPE_DIR_READ) 1853 ide_tape_discard_merge_buffer(drive, 1); 1854 if (tape->merge_bh || tape->merge_bh_size) { 1855 printk(KERN_ERR "ide-tape: merge_bh_size " 1856 "should be 0 now\n"); 1857 tape->merge_bh_size = 0; 1858 } 1859 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0); 1860 if (!tape->merge_bh) 1861 return -ENOMEM; 1862 tape->chrdev_dir = IDETAPE_DIR_WRITE; 1863 idetape_init_merge_buffer(tape); 1864 1865 /* 1866 * Issue a write 0 command to ensure that DSC handshake is 1867 * switched from completion mode to buffer available mode. No 1868 * point in issuing this if DSC overlap isn't supported, some 1869 * drives (Seagate STT3401A) will return an error. 1870 */ 1871 if (drive->dsc_overlap) { 1872 ssize_t retval = idetape_queue_rw_tail(drive, 1873 REQ_IDETAPE_WRITE, 0, 1874 tape->merge_bh); 1875 if (retval < 0) { 1876 ide_tape_kfree_buffer(tape); 1877 tape->merge_bh = NULL; 1878 tape->chrdev_dir = IDETAPE_DIR_NONE; 1879 return retval; 1880 } 1881 } 1882 } 1883 if (count == 0) 1884 return (0); 1885 if (tape->merge_bh_size) { 1886 if (tape->merge_bh_size >= tape->buffer_size) { 1887 printk(KERN_ERR "ide-tape: bug: merge buf too big\n"); 1888 tape->merge_bh_size = 0; 1889 } 1890 actually_written = min((unsigned int) 1891 (tape->buffer_size - tape->merge_bh_size), 1892 (unsigned int)count); 1893 if (idetape_copy_stage_from_user(tape, buf, actually_written)) 1894 ret = -EFAULT; 1895 buf += actually_written; 1896 tape->merge_bh_size += actually_written; 1897 count -= actually_written; 1898 1899 if (tape->merge_bh_size == tape->buffer_size) { 1900 ssize_t retval; 1901 tape->merge_bh_size = 0; 1902 retval = idetape_add_chrdev_write_request(drive, ctl); 1903 if (retval <= 0) 1904 return (retval); 1905 } 1906 } 1907 while (count >= tape->buffer_size) { 1908 ssize_t retval; 1909 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size)) 1910 ret = -EFAULT; 1911 buf += tape->buffer_size; 1912 count -= tape->buffer_size; 1913 retval = idetape_add_chrdev_write_request(drive, ctl); 1914 actually_written += tape->buffer_size; 1915 if (retval <= 0) 1916 return (retval); 1917 } 1918 if (count) { 1919 actually_written += count; 1920 if (idetape_copy_stage_from_user(tape, buf, count)) 1921 ret = -EFAULT; 1922 tape->merge_bh_size += count; 1923 } 1924 return ret ? ret : actually_written; 1925} 1926 1927static int idetape_write_filemark(ide_drive_t *drive) 1928{ 1929 struct ide_atapi_pc pc; 1930 1931 /* Write a filemark */ 1932 idetape_create_write_filemark_cmd(drive, &pc, 1); 1933 if (idetape_queue_pc_tail(drive, &pc)) { 1934 printk(KERN_ERR "ide-tape: Couldn't write a filemark\n"); 1935 return -EIO; 1936 } 1937 return 0; 1938} 1939 1940/* 1941 * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is 1942 * requested. 1943 * 1944 * Note: MTBSF and MTBSFM are not supported when the tape doesn't support 1945 * spacing over filemarks in the reverse direction. In this case, MTFSFM is also 1946 * usually not supported. 1947 * 1948 * The following commands are currently not supported: 1949 * 1950 * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS, 1951 * MT_ST_WRITE_THRESHOLD. 1952 */ 1953static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count) 1954{ 1955 idetape_tape_t *tape = drive->driver_data; 1956 struct ide_atapi_pc pc; 1957 int i, retval; 1958 1959 debug_log(DBG_ERR, "Handling MTIOCTOP ioctl: mt_op=%d, mt_count=%d\n", 1960 mt_op, mt_count); 1961 1962 switch (mt_op) { 1963 case MTFSF: 1964 case MTFSFM: 1965 case MTBSF: 1966 case MTBSFM: 1967 if (!mt_count) 1968 return 0; 1969 return idetape_space_over_filemarks(drive, mt_op, mt_count); 1970 default: 1971 break; 1972 } 1973 1974 switch (mt_op) { 1975 case MTWEOF: 1976 if (tape->write_prot) 1977 return -EACCES; 1978 ide_tape_discard_merge_buffer(drive, 1); 1979 for (i = 0; i < mt_count; i++) { 1980 retval = idetape_write_filemark(drive); 1981 if (retval) 1982 return retval; 1983 } 1984 return 0; 1985 case MTREW: 1986 ide_tape_discard_merge_buffer(drive, 0); 1987 if (idetape_rewind_tape(drive)) 1988 return -EIO; 1989 return 0; 1990 case MTLOAD: 1991 ide_tape_discard_merge_buffer(drive, 0); 1992 idetape_create_load_unload_cmd(drive, &pc, 1993 IDETAPE_LU_LOAD_MASK); 1994 return idetape_queue_pc_tail(drive, &pc); 1995 case MTUNLOAD: 1996 case MTOFFL: 1997 /* 1998 * If door is locked, attempt to unlock before 1999 * attempting to eject. 2000 */ 2001 if (tape->door_locked) { 2002 if (idetape_create_prevent_cmd(drive, &pc, 0)) 2003 if (!idetape_queue_pc_tail(drive, &pc)) 2004 tape->door_locked = DOOR_UNLOCKED; 2005 } 2006 ide_tape_discard_merge_buffer(drive, 0); 2007 idetape_create_load_unload_cmd(drive, &pc, 2008 !IDETAPE_LU_LOAD_MASK); 2009 retval = idetape_queue_pc_tail(drive, &pc); 2010 if (!retval) 2011 clear_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags); 2012 return retval; 2013 case MTNOP: 2014 ide_tape_discard_merge_buffer(drive, 0); 2015 return idetape_flush_tape_buffers(drive); 2016 case MTRETEN: 2017 ide_tape_discard_merge_buffer(drive, 0); 2018 idetape_create_load_unload_cmd(drive, &pc, 2019 IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK); 2020 return idetape_queue_pc_tail(drive, &pc); 2021 case MTEOM: 2022 idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD); 2023 return idetape_queue_pc_tail(drive, &pc); 2024 case MTERASE: 2025 (void)idetape_rewind_tape(drive); 2026 idetape_create_erase_cmd(&pc); 2027 return idetape_queue_pc_tail(drive, &pc); 2028 case MTSETBLK: 2029 if (mt_count) { 2030 if (mt_count < tape->blk_size || 2031 mt_count % tape->blk_size) 2032 return -EIO; 2033 tape->user_bs_factor = mt_count / tape->blk_size; 2034 clear_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags); 2035 } else 2036 set_bit(IDE_AFLAG_DETECT_BS, &drive->atapi_flags); 2037 return 0; 2038 case MTSEEK: 2039 ide_tape_discard_merge_buffer(drive, 0); 2040 return idetape_position_tape(drive, 2041 mt_count * tape->user_bs_factor, tape->partition, 0); 2042 case MTSETPART: 2043 ide_tape_discard_merge_buffer(drive, 0); 2044 return idetape_position_tape(drive, 0, mt_count, 0); 2045 case MTFSR: 2046 case MTBSR: 2047 case MTLOCK: 2048 if (!idetape_create_prevent_cmd(drive, &pc, 1)) 2049 return 0; 2050 retval = idetape_queue_pc_tail(drive, &pc); 2051 if (retval) 2052 return retval; 2053 tape->door_locked = DOOR_EXPLICITLY_LOCKED; 2054 return 0; 2055 case MTUNLOCK: 2056 if (!idetape_create_prevent_cmd(drive, &pc, 0)) 2057 return 0; 2058 retval = idetape_queue_pc_tail(drive, &pc); 2059 if (retval) 2060 return retval; 2061 tape->door_locked = DOOR_UNLOCKED; 2062 return 0; 2063 default: 2064 printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n", 2065 mt_op); 2066 return -EIO; 2067 } 2068} 2069 2070/* 2071 * Our character device ioctls. General mtio.h magnetic io commands are 2072 * supported here, and not in the corresponding block interface. Our own 2073 * ide-tape ioctls are supported on both interfaces. 2074 */ 2075static int idetape_chrdev_ioctl(struct inode *inode, struct file *file, 2076 unsigned int cmd, unsigned long arg) 2077{ 2078 struct ide_tape_obj *tape = ide_tape_f(file); 2079 ide_drive_t *drive = tape->drive; 2080 struct mtop mtop; 2081 struct mtget mtget; 2082 struct mtpos mtpos; 2083 int block_offset = 0, position = tape->first_frame; 2084 void __user *argp = (void __user *)arg; 2085 2086 debug_log(DBG_CHRDEV, "Enter %s, cmd=%u\n", __func__, cmd); 2087 2088 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) { 2089 ide_tape_flush_merge_buffer(drive); 2090 idetape_flush_tape_buffers(drive); 2091 } 2092 if (cmd == MTIOCGET || cmd == MTIOCPOS) { 2093 block_offset = tape->merge_bh_size / 2094 (tape->blk_size * tape->user_bs_factor); 2095 position = idetape_read_position(drive); 2096 if (position < 0) 2097 return -EIO; 2098 } 2099 switch (cmd) { 2100 case MTIOCTOP: 2101 if (copy_from_user(&mtop, argp, sizeof(struct mtop))) 2102 return -EFAULT; 2103 return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count); 2104 case MTIOCGET: 2105 memset(&mtget, 0, sizeof(struct mtget)); 2106 mtget.mt_type = MT_ISSCSI2; 2107 mtget.mt_blkno = position / tape->user_bs_factor - block_offset; 2108 mtget.mt_dsreg = 2109 ((tape->blk_size * tape->user_bs_factor) 2110 << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK; 2111 2112 if (tape->drv_write_prot) 2113 mtget.mt_gstat |= GMT_WR_PROT(0xffffffff); 2114 2115 if (copy_to_user(argp, &mtget, sizeof(struct mtget))) 2116 return -EFAULT; 2117 return 0; 2118 case MTIOCPOS: 2119 mtpos.mt_blkno = position / tape->user_bs_factor - block_offset; 2120 if (copy_to_user(argp, &mtpos, sizeof(struct mtpos))) 2121 return -EFAULT; 2122 return 0; 2123 default: 2124 if (tape->chrdev_dir == IDETAPE_DIR_READ) 2125 ide_tape_discard_merge_buffer(drive, 1); 2126 return idetape_blkdev_ioctl(drive, cmd, arg); 2127 } 2128} 2129 2130/* 2131 * Do a mode sense page 0 with block descriptor and if it succeeds set the tape 2132 * block size with the reported value. 2133 */ 2134static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive) 2135{ 2136 idetape_tape_t *tape = drive->driver_data; 2137 struct ide_atapi_pc pc; 2138 2139 idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR); 2140 if (idetape_queue_pc_tail(drive, &pc)) { 2141 printk(KERN_ERR "ide-tape: Can't get block descriptor\n"); 2142 if (tape->blk_size == 0) { 2143 printk(KERN_WARNING "ide-tape: Cannot deal with zero " 2144 "block size, assuming 32k\n"); 2145 tape->blk_size = 32768; 2146 } 2147 return; 2148 } 2149 tape->blk_size = (pc.buf[4 + 5] << 16) + 2150 (pc.buf[4 + 6] << 8) + 2151 pc.buf[4 + 7]; 2152 tape->drv_write_prot = (pc.buf[2] & 0x80) >> 7; 2153} 2154 2155static int idetape_chrdev_open(struct inode *inode, struct file *filp) 2156{ 2157 unsigned int minor = iminor(inode), i = minor & ~0xc0; 2158 ide_drive_t *drive; 2159 idetape_tape_t *tape; 2160 struct ide_atapi_pc pc; 2161 int retval; 2162 2163 if (i >= MAX_HWIFS * MAX_DRIVES) 2164 return -ENXIO; 2165 2166 lock_kernel(); 2167 tape = ide_tape_chrdev_get(i); 2168 if (!tape) { 2169 unlock_kernel(); 2170 return -ENXIO; 2171 } 2172 2173 debug_log(DBG_CHRDEV, "Enter %s\n", __func__); 2174 2175 /* 2176 * We really want to do nonseekable_open(inode, filp); here, but some 2177 * versions of tar incorrectly call lseek on tapes and bail out if that 2178 * fails. So we disallow pread() and pwrite(), but permit lseeks. 2179 */ 2180 filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE); 2181 2182 drive = tape->drive; 2183 2184 filp->private_data = tape; 2185 2186 if (test_and_set_bit(IDE_AFLAG_BUSY, &drive->atapi_flags)) { 2187 retval = -EBUSY; 2188 goto out_put_tape; 2189 } 2190 2191 retval = idetape_wait_ready(drive, 60 * HZ); 2192 if (retval) { 2193 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags); 2194 printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name); 2195 goto out_put_tape; 2196 } 2197 2198 idetape_read_position(drive); 2199 if (!test_bit(IDE_AFLAG_ADDRESS_VALID, &drive->atapi_flags)) 2200 (void)idetape_rewind_tape(drive); 2201 2202 /* Read block size and write protect status from drive. */ 2203 ide_tape_get_bsize_from_bdesc(drive); 2204 2205 /* Set write protect flag if device is opened as read-only. */ 2206 if ((filp->f_flags & O_ACCMODE) == O_RDONLY) 2207 tape->write_prot = 1; 2208 else 2209 tape->write_prot = tape->drv_write_prot; 2210 2211 /* Make sure drive isn't write protected if user wants to write. */ 2212 if (tape->write_prot) { 2213 if ((filp->f_flags & O_ACCMODE) == O_WRONLY || 2214 (filp->f_flags & O_ACCMODE) == O_RDWR) { 2215 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags); 2216 retval = -EROFS; 2217 goto out_put_tape; 2218 } 2219 } 2220 2221 /* Lock the tape drive door so user can't eject. */ 2222 if (tape->chrdev_dir == IDETAPE_DIR_NONE) { 2223 if (idetape_create_prevent_cmd(drive, &pc, 1)) { 2224 if (!idetape_queue_pc_tail(drive, &pc)) { 2225 if (tape->door_locked != DOOR_EXPLICITLY_LOCKED) 2226 tape->door_locked = DOOR_LOCKED; 2227 } 2228 } 2229 } 2230 unlock_kernel(); 2231 return 0; 2232 2233out_put_tape: 2234 ide_tape_put(tape); 2235 unlock_kernel(); 2236 return retval; 2237} 2238 2239static void idetape_write_release(ide_drive_t *drive, unsigned int minor) 2240{ 2241 idetape_tape_t *tape = drive->driver_data; 2242 2243 ide_tape_flush_merge_buffer(drive); 2244 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0); 2245 if (tape->merge_bh != NULL) { 2246 idetape_pad_zeros(drive, tape->blk_size * 2247 (tape->user_bs_factor - 1)); 2248 ide_tape_kfree_buffer(tape); 2249 tape->merge_bh = NULL; 2250 } 2251 idetape_write_filemark(drive); 2252 idetape_flush_tape_buffers(drive); 2253 idetape_flush_tape_buffers(drive); 2254} 2255 2256static int idetape_chrdev_release(struct inode *inode, struct file *filp) 2257{ 2258 struct ide_tape_obj *tape = ide_tape_f(filp); 2259 ide_drive_t *drive = tape->drive; 2260 struct ide_atapi_pc pc; 2261 unsigned int minor = iminor(inode); 2262 2263 lock_kernel(); 2264 tape = drive->driver_data; 2265 2266 debug_log(DBG_CHRDEV, "Enter %s\n", __func__); 2267 2268 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) 2269 idetape_write_release(drive, minor); 2270 if (tape->chrdev_dir == IDETAPE_DIR_READ) { 2271 if (minor < 128) 2272 ide_tape_discard_merge_buffer(drive, 1); 2273 } 2274 2275 if (minor < 128 && test_bit(IDE_AFLAG_MEDIUM_PRESENT, &drive->atapi_flags)) 2276 (void) idetape_rewind_tape(drive); 2277 if (tape->chrdev_dir == IDETAPE_DIR_NONE) { 2278 if (tape->door_locked == DOOR_LOCKED) { 2279 if (idetape_create_prevent_cmd(drive, &pc, 0)) { 2280 if (!idetape_queue_pc_tail(drive, &pc)) 2281 tape->door_locked = DOOR_UNLOCKED; 2282 } 2283 } 2284 } 2285 clear_bit(IDE_AFLAG_BUSY, &drive->atapi_flags); 2286 ide_tape_put(tape); 2287 unlock_kernel(); 2288 return 0; 2289} 2290 2291/* 2292 * check the contents of the ATAPI IDENTIFY command results. We return: 2293 * 2294 * 1 - If the tape can be supported by us, based on the information we have so 2295 * far. 2296 * 2297 * 0 - If this tape driver is not currently supported by us. 2298 */ 2299static int idetape_identify_device(ide_drive_t *drive) 2300{ 2301 u8 gcw[2], protocol, device_type, removable, packet_size; 2302 2303 if (drive->id_read == 0) 2304 return 1; 2305 2306 *((unsigned short *) &gcw) = drive->id->config; 2307 2308 protocol = (gcw[1] & 0xC0) >> 6; 2309 device_type = gcw[1] & 0x1F; 2310 removable = !!(gcw[0] & 0x80); 2311 packet_size = gcw[0] & 0x3; 2312 2313 /* Check that we can support this device */ 2314 if (protocol != 2) 2315 printk(KERN_ERR "ide-tape: Protocol (0x%02x) is not ATAPI\n", 2316 protocol); 2317 else if (device_type != 1) 2318 printk(KERN_ERR "ide-tape: Device type (0x%02x) is not set " 2319 "to tape\n", device_type); 2320 else if (!removable) 2321 printk(KERN_ERR "ide-tape: The removable flag is not set\n"); 2322 else if (packet_size != 0) { 2323 printk(KERN_ERR "ide-tape: Packet size (0x%02x) is not 12" 2324 " bytes\n", packet_size); 2325 } else 2326 return 1; 2327 return 0; 2328} 2329 2330static void idetape_get_inquiry_results(ide_drive_t *drive) 2331{ 2332 idetape_tape_t *tape = drive->driver_data; 2333 struct ide_atapi_pc pc; 2334 char fw_rev[6], vendor_id[10], product_id[18]; 2335 2336 idetape_create_inquiry_cmd(&pc); 2337 if (idetape_queue_pc_tail(drive, &pc)) { 2338 printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n", 2339 tape->name); 2340 return; 2341 } 2342 memcpy(vendor_id, &pc.buf[8], 8); 2343 memcpy(product_id, &pc.buf[16], 16); 2344 memcpy(fw_rev, &pc.buf[32], 4); 2345 2346 ide_fixstring(vendor_id, 10, 0); 2347 ide_fixstring(product_id, 18, 0); 2348 ide_fixstring(fw_rev, 6, 0); 2349 2350 printk(KERN_INFO "ide-tape: %s <-> %s: %s %s rev %s\n", 2351 drive->name, tape->name, vendor_id, product_id, fw_rev); 2352} 2353 2354/* 2355 * Ask the tape about its various parameters. In particular, we will adjust our 2356 * data transfer buffer size to the recommended value as returned by the tape. 2357 */ 2358static void idetape_get_mode_sense_results(ide_drive_t *drive) 2359{ 2360 idetape_tape_t *tape = drive->driver_data; 2361 struct ide_atapi_pc pc; 2362 u8 *caps; 2363 u8 speed, max_speed; 2364 2365 idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE); 2366 if (idetape_queue_pc_tail(drive, &pc)) { 2367 printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming" 2368 " some default values\n"); 2369 tape->blk_size = 512; 2370 put_unaligned(52, (u16 *)&tape->caps[12]); 2371 put_unaligned(540, (u16 *)&tape->caps[14]); 2372 put_unaligned(6*52, (u16 *)&tape->caps[16]); 2373 return; 2374 } 2375 caps = pc.buf + 4 + pc.buf[3]; 2376 2377 /* convert to host order and save for later use */ 2378 speed = be16_to_cpu(*(u16 *)&caps[14]); 2379 max_speed = be16_to_cpu(*(u16 *)&caps[8]); 2380 2381 put_unaligned(max_speed, (u16 *)&caps[8]); 2382 put_unaligned(be16_to_cpu(*(u16 *)&caps[12]), (u16 *)&caps[12]); 2383 put_unaligned(speed, (u16 *)&caps[14]); 2384 put_unaligned(be16_to_cpu(*(u16 *)&caps[16]), (u16 *)&caps[16]); 2385 2386 if (!speed) { 2387 printk(KERN_INFO "ide-tape: %s: invalid tape speed " 2388 "(assuming 650KB/sec)\n", drive->name); 2389 put_unaligned(650, (u16 *)&caps[14]); 2390 } 2391 if (!max_speed) { 2392 printk(KERN_INFO "ide-tape: %s: invalid max_speed " 2393 "(assuming 650KB/sec)\n", drive->name); 2394 put_unaligned(650, (u16 *)&caps[8]); 2395 } 2396 2397 memcpy(&tape->caps, caps, 20); 2398 if (caps[7] & 0x02) 2399 tape->blk_size = 512; 2400 else if (caps[7] & 0x04) 2401 tape->blk_size = 1024; 2402} 2403 2404#ifdef CONFIG_IDE_PROC_FS 2405static void idetape_add_settings(ide_drive_t *drive) 2406{ 2407 idetape_tape_t *tape = drive->driver_data; 2408 2409 ide_add_setting(drive, "buffer", SETTING_READ, TYPE_SHORT, 0, 0xffff, 2410 1, 2, (u16 *)&tape->caps[16], NULL); 2411 ide_add_setting(drive, "speed", SETTING_READ, TYPE_SHORT, 0, 0xffff, 2412 1, 1, (u16 *)&tape->caps[14], NULL); 2413 ide_add_setting(drive, "buffer_size", SETTING_READ, TYPE_INT, 0, 0xffff, 2414 1, 1024, &tape->buffer_size, NULL); 2415 ide_add_setting(drive, "tdsc", SETTING_RW, TYPE_INT, IDETAPE_DSC_RW_MIN, 2416 IDETAPE_DSC_RW_MAX, 1000, HZ, &tape->best_dsc_rw_freq, 2417 NULL); 2418 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, 2419 1, &drive->dsc_overlap, NULL); 2420 ide_add_setting(drive, "avg_speed", SETTING_READ, TYPE_INT, 0, 0xffff, 2421 1, 1, &tape->avg_speed, NULL); 2422 ide_add_setting(drive, "debug_mask", SETTING_RW, TYPE_INT, 0, 0xffff, 1, 2423 1, &tape->debug_mask, NULL); 2424} 2425#else 2426static inline void idetape_add_settings(ide_drive_t *drive) { ; } 2427#endif 2428 2429/* 2430 * The function below is called to: 2431 * 2432 * 1. Initialize our various state variables. 2433 * 2. Ask the tape for its capabilities. 2434 * 3. Allocate a buffer which will be used for data transfer. The buffer size 2435 * is chosen based on the recommendation which we received in step 2. 2436 * 2437 * Note that at this point ide.c already assigned us an irq, so that we can 2438 * queue requests here and wait for their completion. 2439 */ 2440static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor) 2441{ 2442 unsigned long t; 2443 int speed; 2444 int buffer_size; 2445 u8 gcw[2]; 2446 u16 *ctl = (u16 *)&tape->caps[12]; 2447 2448 drive->pc_callback = ide_tape_callback; 2449 2450 spin_lock_init(&tape->lock); 2451 drive->dsc_overlap = 1; 2452 if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) { 2453 printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n", 2454 tape->name); 2455 drive->dsc_overlap = 0; 2456 } 2457 /* Seagate Travan drives do not support DSC overlap. */ 2458 if (strstr(drive->id->model, "Seagate STT3401")) 2459 drive->dsc_overlap = 0; 2460 tape->minor = minor; 2461 tape->name[0] = 'h'; 2462 tape->name[1] = 't'; 2463 tape->name[2] = '0' + minor; 2464 tape->chrdev_dir = IDETAPE_DIR_NONE; 2465 tape->pc = tape->pc_stack; 2466 *((unsigned short *) &gcw) = drive->id->config; 2467 2468 /* Command packet DRQ type */ 2469 if (((gcw[0] & 0x60) >> 5) == 1) 2470 set_bit(IDE_AFLAG_DRQ_INTERRUPT, &drive->atapi_flags); 2471 2472 idetape_get_inquiry_results(drive); 2473 idetape_get_mode_sense_results(drive); 2474 ide_tape_get_bsize_from_bdesc(drive); 2475 tape->user_bs_factor = 1; 2476 tape->buffer_size = *ctl * tape->blk_size; 2477 while (tape->buffer_size > 0xffff) { 2478 printk(KERN_NOTICE "ide-tape: decreasing stage size\n"); 2479 *ctl /= 2; 2480 tape->buffer_size = *ctl * tape->blk_size; 2481 } 2482 buffer_size = tape->buffer_size; 2483 tape->pages_per_buffer = buffer_size / PAGE_SIZE; 2484 if (buffer_size % PAGE_SIZE) { 2485 tape->pages_per_buffer++; 2486 tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE; 2487 } 2488 2489 /* select the "best" DSC read/write polling freq */ 2490 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]); 2491 2492 t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000); 2493 2494 /* 2495 * Ensure that the number we got makes sense; limit it within 2496 * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX. 2497 */ 2498 tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN, 2499 IDETAPE_DSC_RW_MAX); 2500 printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, " 2501 "%lums tDSC%s\n", 2502 drive->name, tape->name, *(u16 *)&tape->caps[14], 2503 (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size, 2504 tape->buffer_size / 1024, 2505 tape->best_dsc_rw_freq * 1000 / HZ, 2506 drive->using_dma ? ", DMA":""); 2507 2508 idetape_add_settings(drive); 2509} 2510 2511static void ide_tape_remove(ide_drive_t *drive) 2512{ 2513 idetape_tape_t *tape = drive->driver_data; 2514 2515 ide_proc_unregister_driver(drive, tape->driver); 2516 2517 ide_unregister_region(tape->disk); 2518 2519 ide_tape_put(tape); 2520} 2521 2522static void ide_tape_release(struct kref *kref) 2523{ 2524 struct ide_tape_obj *tape = to_ide_tape(kref); 2525 ide_drive_t *drive = tape->drive; 2526 struct gendisk *g = tape->disk; 2527 2528 BUG_ON(tape->merge_bh_size); 2529 2530 drive->dsc_overlap = 0; 2531 drive->driver_data = NULL; 2532 device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor)); 2533 device_destroy(idetape_sysfs_class, 2534 MKDEV(IDETAPE_MAJOR, tape->minor + 128)); 2535 idetape_devs[tape->minor] = NULL; 2536 g->private_data = NULL; 2537 put_disk(g); 2538 kfree(tape); 2539} 2540 2541#ifdef CONFIG_IDE_PROC_FS 2542static int proc_idetape_read_name 2543 (char *page, char **start, off_t off, int count, int *eof, void *data) 2544{ 2545 ide_drive_t *drive = (ide_drive_t *) data; 2546 idetape_tape_t *tape = drive->driver_data; 2547 char *out = page; 2548 int len; 2549 2550 len = sprintf(out, "%s\n", tape->name); 2551 PROC_IDE_READ_RETURN(page, start, off, count, eof, len); 2552} 2553 2554static ide_proc_entry_t idetape_proc[] = { 2555 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL }, 2556 { "name", S_IFREG|S_IRUGO, proc_idetape_read_name, NULL }, 2557 { NULL, 0, NULL, NULL } 2558}; 2559#endif 2560 2561static int ide_tape_probe(ide_drive_t *); 2562 2563static ide_driver_t idetape_driver = { 2564 .gen_driver = { 2565 .owner = THIS_MODULE, 2566 .name = "ide-tape", 2567 .bus = &ide_bus_type, 2568 }, 2569 .probe = ide_tape_probe, 2570 .remove = ide_tape_remove, 2571 .version = IDETAPE_VERSION, 2572 .media = ide_tape, 2573 .supports_dsc_overlap = 1, 2574 .do_request = idetape_do_request, 2575 .end_request = idetape_end_request, 2576 .error = __ide_error, 2577#ifdef CONFIG_IDE_PROC_FS 2578 .proc = idetape_proc, 2579#endif 2580}; 2581 2582/* Our character device supporting functions, passed to register_chrdev. */ 2583static const struct file_operations idetape_fops = { 2584 .owner = THIS_MODULE, 2585 .read = idetape_chrdev_read, 2586 .write = idetape_chrdev_write, 2587 .ioctl = idetape_chrdev_ioctl, 2588 .open = idetape_chrdev_open, 2589 .release = idetape_chrdev_release, 2590}; 2591 2592static int idetape_open(struct inode *inode, struct file *filp) 2593{ 2594 struct gendisk *disk = inode->i_bdev->bd_disk; 2595 struct ide_tape_obj *tape; 2596 2597 tape = ide_tape_get(disk); 2598 if (!tape) 2599 return -ENXIO; 2600 2601 return 0; 2602} 2603 2604static int idetape_release(struct inode *inode, struct file *filp) 2605{ 2606 struct gendisk *disk = inode->i_bdev->bd_disk; 2607 struct ide_tape_obj *tape = ide_tape_g(disk); 2608 2609 ide_tape_put(tape); 2610 2611 return 0; 2612} 2613 2614static int idetape_ioctl(struct inode *inode, struct file *file, 2615 unsigned int cmd, unsigned long arg) 2616{ 2617 struct block_device *bdev = inode->i_bdev; 2618 struct ide_tape_obj *tape = ide_tape_g(bdev->bd_disk); 2619 ide_drive_t *drive = tape->drive; 2620 int err = generic_ide_ioctl(drive, file, bdev, cmd, arg); 2621 if (err == -EINVAL) 2622 err = idetape_blkdev_ioctl(drive, cmd, arg); 2623 return err; 2624} 2625 2626static struct block_device_operations idetape_block_ops = { 2627 .owner = THIS_MODULE, 2628 .open = idetape_open, 2629 .release = idetape_release, 2630 .ioctl = idetape_ioctl, 2631}; 2632 2633static int ide_tape_probe(ide_drive_t *drive) 2634{ 2635 idetape_tape_t *tape; 2636 struct gendisk *g; 2637 int minor; 2638 2639 if (!strstr("ide-tape", drive->driver_req)) 2640 goto failed; 2641 if (!drive->present) 2642 goto failed; 2643 if (drive->media != ide_tape) 2644 goto failed; 2645 if (!idetape_identify_device(drive)) { 2646 printk(KERN_ERR "ide-tape: %s: not supported by this version of" 2647 " the driver\n", drive->name); 2648 goto failed; 2649 } 2650 tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL); 2651 if (tape == NULL) { 2652 printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n", 2653 drive->name); 2654 goto failed; 2655 } 2656 2657 g = alloc_disk(1 << PARTN_BITS); 2658 if (!g) 2659 goto out_free_tape; 2660 2661 ide_init_disk(g, drive); 2662 2663 ide_proc_register_driver(drive, &idetape_driver); 2664 2665 kref_init(&tape->kref); 2666 2667 tape->drive = drive; 2668 tape->driver = &idetape_driver; 2669 tape->disk = g; 2670 2671 g->private_data = &tape->driver; 2672 2673 drive->driver_data = tape; 2674 2675 mutex_lock(&idetape_ref_mutex); 2676 for (minor = 0; idetape_devs[minor]; minor++) 2677 ; 2678 idetape_devs[minor] = tape; 2679 mutex_unlock(&idetape_ref_mutex); 2680 2681 idetape_setup(drive, tape, minor); 2682 2683 device_create_drvdata(idetape_sysfs_class, &drive->gendev, 2684 MKDEV(IDETAPE_MAJOR, minor), NULL, 2685 "%s", tape->name); 2686 device_create_drvdata(idetape_sysfs_class, &drive->gendev, 2687 MKDEV(IDETAPE_MAJOR, minor + 128), NULL, 2688 "n%s", tape->name); 2689 2690 g->fops = &idetape_block_ops; 2691 ide_register_region(g); 2692 2693 return 0; 2694 2695out_free_tape: 2696 kfree(tape); 2697failed: 2698 return -ENODEV; 2699} 2700 2701static void __exit idetape_exit(void) 2702{ 2703 driver_unregister(&idetape_driver.gen_driver); 2704 class_destroy(idetape_sysfs_class); 2705 unregister_chrdev(IDETAPE_MAJOR, "ht"); 2706} 2707 2708static int __init idetape_init(void) 2709{ 2710 int error = 1; 2711 idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape"); 2712 if (IS_ERR(idetape_sysfs_class)) { 2713 idetape_sysfs_class = NULL; 2714 printk(KERN_ERR "Unable to create sysfs class for ide tapes\n"); 2715 error = -EBUSY; 2716 goto out; 2717 } 2718 2719 if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) { 2720 printk(KERN_ERR "ide-tape: Failed to register chrdev" 2721 " interface\n"); 2722 error = -EBUSY; 2723 goto out_free_class; 2724 } 2725 2726 error = driver_register(&idetape_driver.gen_driver); 2727 if (error) 2728 goto out_free_driver; 2729 2730 return 0; 2731 2732out_free_driver: 2733 driver_unregister(&idetape_driver.gen_driver); 2734out_free_class: 2735 class_destroy(idetape_sysfs_class); 2736out: 2737 return error; 2738} 2739 2740MODULE_ALIAS("ide:*m-tape*"); 2741module_init(idetape_init); 2742module_exit(idetape_exit); 2743MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR); 2744MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver"); 2745MODULE_LICENSE("GPL"); 2746