aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/ide/ide-tape.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/ide/ide-tape.c')
-rw-r--r--drivers/ide/ide-tape.c742
1 files changed, 165 insertions, 577 deletions
diff --git a/drivers/ide/ide-tape.c b/drivers/ide/ide-tape.c
index cb942a9b580f..d9764f0bc82f 100644
--- a/drivers/ide/ide-tape.c
+++ b/drivers/ide/ide-tape.c
@@ -131,13 +131,6 @@ enum {
131 IDETAPE_DIR_WRITE = (1 << 2), 131 IDETAPE_DIR_WRITE = (1 << 2),
132}; 132};
133 133
134struct idetape_bh {
135 u32 b_size;
136 atomic_t b_count;
137 struct idetape_bh *b_reqnext;
138 char *b_data;
139};
140
141/* Tape door status */ 134/* Tape door status */
142#define DOOR_UNLOCKED 0 135#define DOOR_UNLOCKED 0
143#define DOOR_LOCKED 1 136#define DOOR_LOCKED 1
@@ -219,18 +212,12 @@ typedef struct ide_tape_obj {
219 212
220 /* Data buffer size chosen based on the tape's recommendation */ 213 /* Data buffer size chosen based on the tape's recommendation */
221 int buffer_size; 214 int buffer_size;
222 /* merge buffer */ 215 /* Staging buffer of buffer_size bytes */
223 struct idetape_bh *merge_bh; 216 void *buf;
224 /* size of the merge buffer */ 217 /* The read/write cursor */
225 int merge_bh_size; 218 void *cur;
226 /* pointer to current buffer head within the merge buffer */ 219 /* The number of valid bytes in buf */
227 struct idetape_bh *bh; 220 size_t valid;
228 char *b_data;
229 int b_count;
230
231 int pages_per_buffer;
232 /* Wasted space in each stage */
233 int excess_bh_size;
234 221
235 /* Measures average tape speed */ 222 /* Measures average tape speed */
236 unsigned long avg_time; 223 unsigned long avg_time;
@@ -297,84 +284,6 @@ static struct ide_tape_obj *ide_tape_chrdev_get(unsigned int i)
297 return tape; 284 return tape;
298} 285}
299 286
300static int idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
301 unsigned int bcount)
302{
303 struct idetape_bh *bh = pc->bh;
304 int count;
305
306 while (bcount) {
307 if (bh == NULL)
308 break;
309 count = min(
310 (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
311 bcount);
312 drive->hwif->tp_ops->input_data(drive, NULL, bh->b_data +
313 atomic_read(&bh->b_count), count);
314 bcount -= count;
315 atomic_add(count, &bh->b_count);
316 if (atomic_read(&bh->b_count) == bh->b_size) {
317 bh = bh->b_reqnext;
318 if (bh)
319 atomic_set(&bh->b_count, 0);
320 }
321 }
322
323 pc->bh = bh;
324
325 return bcount;
326}
327
328static int idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
329 unsigned int bcount)
330{
331 struct idetape_bh *bh = pc->bh;
332 int count;
333
334 while (bcount) {
335 if (bh == NULL)
336 break;
337 count = min((unsigned int)pc->b_count, (unsigned int)bcount);
338 drive->hwif->tp_ops->output_data(drive, NULL, pc->b_data, count);
339 bcount -= count;
340 pc->b_data += count;
341 pc->b_count -= count;
342 if (!pc->b_count) {
343 bh = bh->b_reqnext;
344 pc->bh = bh;
345 if (bh) {
346 pc->b_data = bh->b_data;
347 pc->b_count = atomic_read(&bh->b_count);
348 }
349 }
350 }
351
352 return bcount;
353}
354
355static void idetape_update_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc)
356{
357 struct idetape_bh *bh = pc->bh;
358 int count;
359 unsigned int bcount = pc->xferred;
360
361 if (pc->flags & PC_FLAG_WRITING)
362 return;
363 while (bcount) {
364 if (bh == NULL) {
365 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
366 __func__);
367 return;
368 }
369 count = min((unsigned int)bh->b_size, (unsigned int)bcount);
370 atomic_set(&bh->b_count, count);
371 if (atomic_read(&bh->b_count) == bh->b_size)
372 bh = bh->b_reqnext;
373 bcount -= count;
374 }
375 pc->bh = bh;
376}
377
378/* 287/*
379 * called on each failed packet command retry to analyze the request sense. We 288 * called on each failed packet command retry to analyze the request sense. We
380 * currently do not utilize this information. 289 * currently do not utilize this information.
@@ -392,12 +301,10 @@ static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
392 pc->c[0], tape->sense_key, tape->asc, tape->ascq); 301 pc->c[0], tape->sense_key, tape->asc, tape->ascq);
393 302
394 /* Correct pc->xferred by asking the tape. */ 303 /* Correct pc->xferred by asking the tape. */
395 if (pc->flags & PC_FLAG_DMA_ERROR) { 304 if (pc->flags & PC_FLAG_DMA_ERROR)
396 pc->xferred = pc->req_xfer - 305 pc->xferred = pc->req_xfer -
397 tape->blk_size * 306 tape->blk_size *
398 get_unaligned_be32(&sense[3]); 307 get_unaligned_be32(&sense[3]);
399 idetape_update_buffers(drive, pc);
400 }
401 308
402 /* 309 /*
403 * If error was the result of a zero-length read or write command, 310 * If error was the result of a zero-length read or write command,
@@ -436,29 +343,6 @@ static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
436 } 343 }
437} 344}
438 345
439/* Free data buffers completely. */
440static void ide_tape_kfree_buffer(idetape_tape_t *tape)
441{
442 struct idetape_bh *prev_bh, *bh = tape->merge_bh;
443
444 while (bh) {
445 u32 size = bh->b_size;
446
447 while (size) {
448 unsigned int order = fls(size >> PAGE_SHIFT)-1;
449
450 if (bh->b_data)
451 free_pages((unsigned long)bh->b_data, order);
452
453 size &= (order-1);
454 bh->b_data += (1 << order) * PAGE_SIZE;
455 }
456 prev_bh = bh;
457 bh = bh->b_reqnext;
458 kfree(prev_bh);
459 }
460}
461
462static void ide_tape_handle_dsc(ide_drive_t *); 346static void ide_tape_handle_dsc(ide_drive_t *);
463 347
464static int ide_tape_callback(ide_drive_t *drive, int dsc) 348static int ide_tape_callback(ide_drive_t *drive, int dsc)
@@ -496,7 +380,7 @@ static int ide_tape_callback(ide_drive_t *drive, int dsc)
496 } 380 }
497 381
498 tape->first_frame += blocks; 382 tape->first_frame += blocks;
499 rq->current_nr_sectors -= blocks; 383 rq->resid_len -= blocks * tape->blk_size;
500 384
501 if (pc->error) { 385 if (pc->error) {
502 uptodate = 0; 386 uptodate = 0;
@@ -558,19 +442,6 @@ static void ide_tape_handle_dsc(ide_drive_t *drive)
558 idetape_postpone_request(drive); 442 idetape_postpone_request(drive);
559} 443}
560 444
561static int ide_tape_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
562 unsigned int bcount, int write)
563{
564 unsigned int bleft;
565
566 if (write)
567 bleft = idetape_output_buffers(drive, pc, bcount);
568 else
569 bleft = idetape_input_buffers(drive, pc, bcount);
570
571 return bcount - bleft;
572}
573
574/* 445/*
575 * Packet Command Interface 446 * Packet Command Interface
576 * 447 *
@@ -614,12 +485,6 @@ static ide_startstop_t ide_tape_issue_pc(ide_drive_t *drive,
614{ 485{
615 idetape_tape_t *tape = drive->driver_data; 486 idetape_tape_t *tape = drive->driver_data;
616 487
617 if (drive->pc->c[0] == REQUEST_SENSE &&
618 pc->c[0] == REQUEST_SENSE) {
619 printk(KERN_ERR "ide-tape: possible ide-tape.c bug - "
620 "Two request sense in serial were issued\n");
621 }
622
623 if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE) 488 if (drive->failed_pc == NULL && pc->c[0] != REQUEST_SENSE)
624 drive->failed_pc = pc; 489 drive->failed_pc = pc;
625 490
@@ -628,6 +493,8 @@ static ide_startstop_t ide_tape_issue_pc(ide_drive_t *drive,
628 493
629 if (pc->retries > IDETAPE_MAX_PC_RETRIES || 494 if (pc->retries > IDETAPE_MAX_PC_RETRIES ||
630 (pc->flags & PC_FLAG_ABORT)) { 495 (pc->flags & PC_FLAG_ABORT)) {
496 unsigned int done = blk_rq_bytes(drive->hwif->rq);
497
631 /* 498 /*
632 * We will "abort" retrying a packet command in case legitimate 499 * We will "abort" retrying a packet command in case legitimate
633 * error code was received (crossing a filemark, or end of the 500 * error code was received (crossing a filemark, or end of the
@@ -647,8 +514,10 @@ static ide_startstop_t ide_tape_issue_pc(ide_drive_t *drive,
647 /* Giving up */ 514 /* Giving up */
648 pc->error = IDE_DRV_ERROR_GENERAL; 515 pc->error = IDE_DRV_ERROR_GENERAL;
649 } 516 }
517
650 drive->failed_pc = NULL; 518 drive->failed_pc = NULL;
651 drive->pc_callback(drive, 0); 519 drive->pc_callback(drive, 0);
520 ide_complete_rq(drive, -EIO, done);
652 return ide_stopped; 521 return ide_stopped;
653 } 522 }
654 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]); 523 debug_log(DBG_SENSE, "Retry #%d, cmd = %02X\n", pc->retries, pc->c[0]);
@@ -701,7 +570,7 @@ static ide_startstop_t idetape_media_access_finished(ide_drive_t *drive)
701 printk(KERN_ERR "ide-tape: %s: I/O error, ", 570 printk(KERN_ERR "ide-tape: %s: I/O error, ",
702 tape->name); 571 tape->name);
703 /* Retry operation */ 572 /* Retry operation */
704 ide_retry_pc(drive, tape->disk); 573 ide_retry_pc(drive);
705 return ide_stopped; 574 return ide_stopped;
706 } 575 }
707 pc->error = 0; 576 pc->error = 0;
@@ -717,27 +586,22 @@ static void ide_tape_create_rw_cmd(idetape_tape_t *tape,
717 struct ide_atapi_pc *pc, struct request *rq, 586 struct ide_atapi_pc *pc, struct request *rq,
718 u8 opcode) 587 u8 opcode)
719{ 588{
720 struct idetape_bh *bh = (struct idetape_bh *)rq->special; 589 unsigned int length = blk_rq_sectors(rq);
721 unsigned int length = rq->current_nr_sectors;
722 590
723 ide_init_pc(pc); 591 ide_init_pc(pc);
724 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]); 592 put_unaligned(cpu_to_be32(length), (unsigned int *) &pc->c[1]);
725 pc->c[1] = 1; 593 pc->c[1] = 1;
726 pc->bh = bh;
727 pc->buf = NULL; 594 pc->buf = NULL;
728 pc->buf_size = length * tape->blk_size; 595 pc->buf_size = length * tape->blk_size;
729 pc->req_xfer = pc->buf_size; 596 pc->req_xfer = pc->buf_size;
730 if (pc->req_xfer == tape->buffer_size) 597 if (pc->req_xfer == tape->buffer_size)
731 pc->flags |= PC_FLAG_DMA_OK; 598 pc->flags |= PC_FLAG_DMA_OK;
732 599
733 if (opcode == READ_6) { 600 if (opcode == READ_6)
734 pc->c[0] = READ_6; 601 pc->c[0] = READ_6;
735 atomic_set(&bh->b_count, 0); 602 else if (opcode == WRITE_6) {
736 } else if (opcode == WRITE_6) {
737 pc->c[0] = WRITE_6; 603 pc->c[0] = WRITE_6;
738 pc->flags |= PC_FLAG_WRITING; 604 pc->flags |= PC_FLAG_WRITING;
739 pc->b_data = bh->b_data;
740 pc->b_count = atomic_read(&bh->b_count);
741 } 605 }
742 606
743 memcpy(rq->cmd, pc->c, 12); 607 memcpy(rq->cmd, pc->c, 12);
@@ -753,12 +617,10 @@ static ide_startstop_t idetape_do_request(ide_drive_t *drive,
753 struct ide_cmd cmd; 617 struct ide_cmd cmd;
754 u8 stat; 618 u8 stat;
755 619
756 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %lu," 620 debug_log(DBG_SENSE, "sector: %llu, nr_sectors: %u\n"
757 " current_nr_sectors: %u\n", 621 (unsigned long long)blk_rq_pos(rq), blk_rq_sectors(rq));
758 (unsigned long long)rq->sector, rq->nr_sectors,
759 rq->current_nr_sectors);
760 622
761 if (!blk_special_request(rq)) { 623 if (!(blk_special_request(rq) || blk_sense_request(rq))) {
762 /* We do not support buffer cache originated requests. */ 624 /* We do not support buffer cache originated requests. */
763 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in " 625 printk(KERN_NOTICE "ide-tape: %s: Unsupported request in "
764 "request queue (%d)\n", drive->name, rq->cmd_type); 626 "request queue (%d)\n", drive->name, rq->cmd_type);
@@ -834,7 +696,7 @@ static ide_startstop_t idetape_do_request(ide_drive_t *drive,
834 goto out; 696 goto out;
835 } 697 }
836 if (rq->cmd[13] & REQ_IDETAPE_PC1) { 698 if (rq->cmd[13] & REQ_IDETAPE_PC1) {
837 pc = (struct ide_atapi_pc *) rq->buffer; 699 pc = (struct ide_atapi_pc *)rq->special;
838 rq->cmd[13] &= ~(REQ_IDETAPE_PC1); 700 rq->cmd[13] &= ~(REQ_IDETAPE_PC1);
839 rq->cmd[13] |= REQ_IDETAPE_PC2; 701 rq->cmd[13] |= REQ_IDETAPE_PC2;
840 goto out; 702 goto out;
@@ -846,6 +708,9 @@ static ide_startstop_t idetape_do_request(ide_drive_t *drive,
846 BUG(); 708 BUG();
847 709
848out: 710out:
711 /* prepare sense request for this command */
712 ide_prep_sense(drive, rq);
713
849 memset(&cmd, 0, sizeof(cmd)); 714 memset(&cmd, 0, sizeof(cmd));
850 715
851 if (rq_data_dir(rq)) 716 if (rq_data_dir(rq))
@@ -853,167 +718,10 @@ out:
853 718
854 cmd.rq = rq; 719 cmd.rq = rq;
855 720
856 return ide_tape_issue_pc(drive, &cmd, pc); 721 ide_init_sg_cmd(&cmd, pc->req_xfer);
857} 722 ide_map_sg(drive, &cmd);
858
859/*
860 * The function below uses __get_free_pages to allocate a data buffer of size
861 * tape->buffer_size (or a bit more). We attempt to combine sequential pages as
862 * much as possible.
863 *
864 * It returns a pointer to the newly allocated buffer, or NULL in case of
865 * failure.
866 */
867static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
868 int full, int clear)
869{
870 struct idetape_bh *prev_bh, *bh, *merge_bh;
871 int pages = tape->pages_per_buffer;
872 unsigned int order, b_allocd;
873 char *b_data = NULL;
874
875 merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
876 bh = merge_bh;
877 if (bh == NULL)
878 goto abort;
879
880 order = fls(pages) - 1;
881 bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
882 if (!bh->b_data)
883 goto abort;
884 b_allocd = (1 << order) * PAGE_SIZE;
885 pages &= (order-1);
886
887 if (clear)
888 memset(bh->b_data, 0, b_allocd);
889 bh->b_reqnext = NULL;
890 bh->b_size = b_allocd;
891 atomic_set(&bh->b_count, full ? bh->b_size : 0);
892
893 while (pages) {
894 order = fls(pages) - 1;
895 b_data = (char *) __get_free_pages(GFP_KERNEL, order);
896 if (!b_data)
897 goto abort;
898 b_allocd = (1 << order) * PAGE_SIZE;
899
900 if (clear)
901 memset(b_data, 0, b_allocd);
902
903 /* newly allocated page frames below buffer header or ...*/
904 if (bh->b_data == b_data + b_allocd) {
905 bh->b_size += b_allocd;
906 bh->b_data -= b_allocd;
907 if (full)
908 atomic_add(b_allocd, &bh->b_count);
909 continue;
910 }
911 /* they are above the header */
912 if (b_data == bh->b_data + bh->b_size) {
913 bh->b_size += b_allocd;
914 if (full)
915 atomic_add(b_allocd, &bh->b_count);
916 continue;
917 }
918 prev_bh = bh;
919 bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
920 if (!bh) {
921 free_pages((unsigned long) b_data, order);
922 goto abort;
923 }
924 bh->b_reqnext = NULL;
925 bh->b_data = b_data;
926 bh->b_size = b_allocd;
927 atomic_set(&bh->b_count, full ? bh->b_size : 0);
928 prev_bh->b_reqnext = bh;
929
930 pages &= (order-1);
931 }
932 723
933 bh->b_size -= tape->excess_bh_size; 724 return ide_tape_issue_pc(drive, &cmd, pc);
934 if (full)
935 atomic_sub(tape->excess_bh_size, &bh->b_count);
936 return merge_bh;
937abort:
938 ide_tape_kfree_buffer(tape);
939 return NULL;
940}
941
942static int idetape_copy_stage_from_user(idetape_tape_t *tape,
943 const char __user *buf, int n)
944{
945 struct idetape_bh *bh = tape->bh;
946 int count;
947 int ret = 0;
948
949 while (n) {
950 if (bh == NULL) {
951 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
952 __func__);
953 return 1;
954 }
955 count = min((unsigned int)
956 (bh->b_size - atomic_read(&bh->b_count)),
957 (unsigned int)n);
958 if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
959 count))
960 ret = 1;
961 n -= count;
962 atomic_add(count, &bh->b_count);
963 buf += count;
964 if (atomic_read(&bh->b_count) == bh->b_size) {
965 bh = bh->b_reqnext;
966 if (bh)
967 atomic_set(&bh->b_count, 0);
968 }
969 }
970 tape->bh = bh;
971 return ret;
972}
973
974static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
975 int n)
976{
977 struct idetape_bh *bh = tape->bh;
978 int count;
979 int ret = 0;
980
981 while (n) {
982 if (bh == NULL) {
983 printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
984 __func__);
985 return 1;
986 }
987 count = min(tape->b_count, n);
988 if (copy_to_user(buf, tape->b_data, count))
989 ret = 1;
990 n -= count;
991 tape->b_data += count;
992 tape->b_count -= count;
993 buf += count;
994 if (!tape->b_count) {
995 bh = bh->b_reqnext;
996 tape->bh = bh;
997 if (bh) {
998 tape->b_data = bh->b_data;
999 tape->b_count = atomic_read(&bh->b_count);
1000 }
1001 }
1002 }
1003 return ret;
1004}
1005
1006static void idetape_init_merge_buffer(idetape_tape_t *tape)
1007{
1008 struct idetape_bh *bh = tape->merge_bh;
1009 tape->bh = tape->merge_bh;
1010
1011 if (tape->chrdev_dir == IDETAPE_DIR_WRITE)
1012 atomic_set(&bh->b_count, 0);
1013 else {
1014 tape->b_data = bh->b_data;
1015 tape->b_count = atomic_read(&bh->b_count);
1016 }
1017} 725}
1018 726
1019/* 727/*
@@ -1113,10 +821,10 @@ static void __ide_tape_discard_merge_buffer(ide_drive_t *drive)
1113 return; 821 return;
1114 822
1115 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags); 823 clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags);
1116 tape->merge_bh_size = 0; 824 tape->valid = 0;
1117 if (tape->merge_bh != NULL) { 825 if (tape->buf != NULL) {
1118 ide_tape_kfree_buffer(tape); 826 kfree(tape->buf);
1119 tape->merge_bh = NULL; 827 tape->buf = NULL;
1120 } 828 }
1121 829
1122 tape->chrdev_dir = IDETAPE_DIR_NONE; 830 tape->chrdev_dir = IDETAPE_DIR_NONE;
@@ -1170,36 +878,44 @@ static void ide_tape_discard_merge_buffer(ide_drive_t *drive,
1170 * Generate a read/write request for the block device interface and wait for it 878 * Generate a read/write request for the block device interface and wait for it
1171 * to be serviced. 879 * to be serviced.
1172 */ 880 */
1173static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int blocks, 881static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int size)
1174 struct idetape_bh *bh)
1175{ 882{
1176 idetape_tape_t *tape = drive->driver_data; 883 idetape_tape_t *tape = drive->driver_data;
1177 struct request *rq; 884 struct request *rq;
1178 int ret, errors; 885 int ret;
1179 886
1180 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd); 887 debug_log(DBG_SENSE, "%s: cmd=%d\n", __func__, cmd);
888 BUG_ON(cmd != REQ_IDETAPE_READ && cmd != REQ_IDETAPE_WRITE);
889 BUG_ON(size < 0 || size % tape->blk_size);
1181 890
1182 rq = blk_get_request(drive->queue, READ, __GFP_WAIT); 891 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
1183 rq->cmd_type = REQ_TYPE_SPECIAL; 892 rq->cmd_type = REQ_TYPE_SPECIAL;
1184 rq->cmd[13] = cmd; 893 rq->cmd[13] = cmd;
1185 rq->rq_disk = tape->disk; 894 rq->rq_disk = tape->disk;
1186 rq->special = (void *)bh; 895 rq->__sector = tape->first_frame;
1187 rq->sector = tape->first_frame;
1188 rq->nr_sectors = blocks;
1189 rq->current_nr_sectors = blocks;
1190 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1191 896
1192 errors = rq->errors; 897 if (size) {
1193 ret = tape->blk_size * (blocks - rq->current_nr_sectors); 898 ret = blk_rq_map_kern(drive->queue, rq, tape->buf, size,
1194 blk_put_request(rq); 899 __GFP_WAIT);
900 if (ret)
901 goto out_put;
902 }
1195 903
1196 if ((cmd & (REQ_IDETAPE_READ | REQ_IDETAPE_WRITE)) == 0) 904 blk_execute_rq(drive->queue, tape->disk, rq, 0);
1197 return 0;
1198 905
1199 if (tape->merge_bh) 906 /* calculate the number of transferred bytes and update buffer state */
1200 idetape_init_merge_buffer(tape); 907 size -= rq->resid_len;
1201 if (errors == IDE_DRV_ERROR_GENERAL) 908 tape->cur = tape->buf;
1202 return -EIO; 909 if (cmd == REQ_IDETAPE_READ)
910 tape->valid = size;
911 else
912 tape->valid = 0;
913
914 ret = size;
915 if (rq->errors == IDE_DRV_ERROR_GENERAL)
916 ret = -EIO;
917out_put:
918 blk_put_request(rq);
1203 return ret; 919 return ret;
1204} 920}
1205 921
@@ -1236,153 +952,87 @@ static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd)
1236 pc->flags |= PC_FLAG_WAIT_FOR_DSC; 952 pc->flags |= PC_FLAG_WAIT_FOR_DSC;
1237} 953}
1238 954
1239/* Queue up a character device originated write request. */
1240static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
1241{
1242 idetape_tape_t *tape = drive->driver_data;
1243
1244 debug_log(DBG_CHRDEV, "Enter %s\n", __func__);
1245
1246 return idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1247 blocks, tape->merge_bh);
1248}
1249
1250static void ide_tape_flush_merge_buffer(ide_drive_t *drive) 955static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
1251{ 956{
1252 idetape_tape_t *tape = drive->driver_data; 957 idetape_tape_t *tape = drive->driver_data;
1253 int blocks, min;
1254 struct idetape_bh *bh;
1255 958
1256 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) { 959 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
1257 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer" 960 printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer"
1258 " but we are not writing.\n"); 961 " but we are not writing.\n");
1259 return; 962 return;
1260 } 963 }
1261 if (tape->merge_bh_size > tape->buffer_size) { 964 if (tape->buf) {
1262 printk(KERN_ERR "ide-tape: bug: merge_buffer too big\n"); 965 size_t aligned = roundup(tape->valid, tape->blk_size);
1263 tape->merge_bh_size = tape->buffer_size; 966
1264 } 967 memset(tape->cur, 0, aligned - tape->valid);
1265 if (tape->merge_bh_size) { 968 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, aligned);
1266 blocks = tape->merge_bh_size / tape->blk_size; 969 kfree(tape->buf);
1267 if (tape->merge_bh_size % tape->blk_size) { 970 tape->buf = NULL;
1268 unsigned int i;
1269
1270 blocks++;
1271 i = tape->blk_size - tape->merge_bh_size %
1272 tape->blk_size;
1273 bh = tape->bh->b_reqnext;
1274 while (bh) {
1275 atomic_set(&bh->b_count, 0);
1276 bh = bh->b_reqnext;
1277 }
1278 bh = tape->bh;
1279 while (i) {
1280 if (bh == NULL) {
1281 printk(KERN_INFO "ide-tape: bug,"
1282 " bh NULL\n");
1283 break;
1284 }
1285 min = min(i, (unsigned int)(bh->b_size -
1286 atomic_read(&bh->b_count)));
1287 memset(bh->b_data + atomic_read(&bh->b_count),
1288 0, min);
1289 atomic_add(min, &bh->b_count);
1290 i -= min;
1291 bh = bh->b_reqnext;
1292 }
1293 }
1294 (void) idetape_add_chrdev_write_request(drive, blocks);
1295 tape->merge_bh_size = 0;
1296 }
1297 if (tape->merge_bh != NULL) {
1298 ide_tape_kfree_buffer(tape);
1299 tape->merge_bh = NULL;
1300 } 971 }
1301 tape->chrdev_dir = IDETAPE_DIR_NONE; 972 tape->chrdev_dir = IDETAPE_DIR_NONE;
1302} 973}
1303 974
1304static int idetape_init_read(ide_drive_t *drive) 975static int idetape_init_rw(ide_drive_t *drive, int dir)
1305{ 976{
1306 idetape_tape_t *tape = drive->driver_data; 977 idetape_tape_t *tape = drive->driver_data;
1307 int bytes_read; 978 int rc;
1308
1309 /* Initialize read operation */
1310 if (tape->chrdev_dir != IDETAPE_DIR_READ) {
1311 if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
1312 ide_tape_flush_merge_buffer(drive);
1313 idetape_flush_tape_buffers(drive);
1314 }
1315 if (tape->merge_bh || tape->merge_bh_size) {
1316 printk(KERN_ERR "ide-tape: merge_bh_size should be"
1317 " 0 now\n");
1318 tape->merge_bh_size = 0;
1319 }
1320 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1321 if (!tape->merge_bh)
1322 return -ENOMEM;
1323 tape->chrdev_dir = IDETAPE_DIR_READ;
1324 979
1325 /* 980 BUG_ON(dir != IDETAPE_DIR_READ && dir != IDETAPE_DIR_WRITE);
1326 * Issue a read 0 command to ensure that DSC handshake is
1327 * switched from completion mode to buffer available mode.
1328 * No point in issuing this if DSC overlap isn't supported, some
1329 * drives (Seagate STT3401A) will return an error.
1330 */
1331 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
1332 bytes_read = idetape_queue_rw_tail(drive,
1333 REQ_IDETAPE_READ, 0,
1334 tape->merge_bh);
1335 if (bytes_read < 0) {
1336 ide_tape_kfree_buffer(tape);
1337 tape->merge_bh = NULL;
1338 tape->chrdev_dir = IDETAPE_DIR_NONE;
1339 return bytes_read;
1340 }
1341 }
1342 }
1343 981
1344 return 0; 982 if (tape->chrdev_dir == dir)
1345} 983 return 0;
1346 984
1347/* called from idetape_chrdev_read() to service a chrdev read request. */ 985 if (tape->chrdev_dir == IDETAPE_DIR_READ)
1348static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks) 986 ide_tape_discard_merge_buffer(drive, 1);
1349{ 987 else if (tape->chrdev_dir == IDETAPE_DIR_WRITE) {
1350 idetape_tape_t *tape = drive->driver_data; 988 ide_tape_flush_merge_buffer(drive);
989 idetape_flush_tape_buffers(drive);
990 }
1351 991
1352 debug_log(DBG_PROCS, "Enter %s, %d blocks\n", __func__, blocks); 992 if (tape->buf || tape->valid) {
993 printk(KERN_ERR "ide-tape: valid should be 0 now\n");
994 tape->valid = 0;
995 }
1353 996
1354 /* If we are at a filemark, return a read length of 0 */ 997 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
1355 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) 998 if (!tape->buf)
1356 return 0; 999 return -ENOMEM;
1000 tape->chrdev_dir = dir;
1001 tape->cur = tape->buf;
1357 1002
1358 idetape_init_read(drive); 1003 /*
1004 * Issue a 0 rw command to ensure that DSC handshake is
1005 * switched from completion mode to buffer available mode. No
1006 * point in issuing this if DSC overlap isn't supported, some
1007 * drives (Seagate STT3401A) will return an error.
1008 */
1009 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) {
1010 int cmd = dir == IDETAPE_DIR_READ ? REQ_IDETAPE_READ
1011 : REQ_IDETAPE_WRITE;
1012
1013 rc = idetape_queue_rw_tail(drive, cmd, 0);
1014 if (rc < 0) {
1015 kfree(tape->buf);
1016 tape->buf = NULL;
1017 tape->chrdev_dir = IDETAPE_DIR_NONE;
1018 return rc;
1019 }
1020 }
1359 1021
1360 return idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, blocks, 1022 return 0;
1361 tape->merge_bh);
1362} 1023}
1363 1024
1364static void idetape_pad_zeros(ide_drive_t *drive, int bcount) 1025static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
1365{ 1026{
1366 idetape_tape_t *tape = drive->driver_data; 1027 idetape_tape_t *tape = drive->driver_data;
1367 struct idetape_bh *bh; 1028
1368 int blocks; 1029 memset(tape->buf, 0, tape->buffer_size);
1369 1030
1370 while (bcount) { 1031 while (bcount) {
1371 unsigned int count; 1032 unsigned int count = min(tape->buffer_size, bcount);
1372 1033
1373 bh = tape->merge_bh; 1034 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, count);
1374 count = min(tape->buffer_size, bcount);
1375 bcount -= count; 1035 bcount -= count;
1376 blocks = count / tape->blk_size;
1377 while (count) {
1378 atomic_set(&bh->b_count,
1379 min(count, (unsigned int)bh->b_size));
1380 memset(bh->b_data, 0, atomic_read(&bh->b_count));
1381 count -= atomic_read(&bh->b_count);
1382 bh = bh->b_reqnext;
1383 }
1384 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
1385 tape->merge_bh);
1386 } 1036 }
1387} 1037}
1388 1038
@@ -1462,7 +1112,7 @@ static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op,
1462 } 1112 }
1463 1113
1464 if (tape->chrdev_dir == IDETAPE_DIR_READ) { 1114 if (tape->chrdev_dir == IDETAPE_DIR_READ) {
1465 tape->merge_bh_size = 0; 1115 tape->valid = 0;
1466 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) 1116 if (test_and_clear_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
1467 ++count; 1117 ++count;
1468 ide_tape_discard_merge_buffer(drive, 0); 1118 ide_tape_discard_merge_buffer(drive, 0);
@@ -1511,9 +1161,9 @@ static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1511{ 1161{
1512 struct ide_tape_obj *tape = file->private_data; 1162 struct ide_tape_obj *tape = file->private_data;
1513 ide_drive_t *drive = tape->drive; 1163 ide_drive_t *drive = tape->drive;
1514 ssize_t bytes_read, temp, actually_read = 0, rc; 1164 size_t done = 0;
1515 ssize_t ret = 0; 1165 ssize_t ret = 0;
1516 u16 ctl = *(u16 *)&tape->caps[12]; 1166 int rc;
1517 1167
1518 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count); 1168 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
1519 1169
@@ -1523,49 +1173,43 @@ static ssize_t idetape_chrdev_read(struct file *file, char __user *buf,
1523 (count % tape->blk_size) == 0) 1173 (count % tape->blk_size) == 0)
1524 tape->user_bs_factor = count / tape->blk_size; 1174 tape->user_bs_factor = count / tape->blk_size;
1525 } 1175 }
1526 rc = idetape_init_read(drive); 1176
1177 rc = idetape_init_rw(drive, IDETAPE_DIR_READ);
1527 if (rc < 0) 1178 if (rc < 0)
1528 return rc; 1179 return rc;
1529 if (count == 0) 1180
1530 return (0); 1181 while (done < count) {
1531 if (tape->merge_bh_size) { 1182 size_t todo;
1532 actually_read = min((unsigned int)(tape->merge_bh_size), 1183
1533 (unsigned int)count); 1184 /* refill if staging buffer is empty */
1534 if (idetape_copy_stage_to_user(tape, buf, actually_read)) 1185 if (!tape->valid) {
1535 ret = -EFAULT; 1186 /* If we are at a filemark, nothing more to read */
1536 buf += actually_read; 1187 if (test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags))
1537 tape->merge_bh_size -= actually_read; 1188 break;
1538 count -= actually_read; 1189 /* read */
1539 } 1190 if (idetape_queue_rw_tail(drive, REQ_IDETAPE_READ,
1540 while (count >= tape->buffer_size) { 1191 tape->buffer_size) <= 0)
1541 bytes_read = idetape_add_chrdev_read_request(drive, ctl); 1192 break;
1542 if (bytes_read <= 0) 1193 }
1543 goto finish; 1194
1544 if (idetape_copy_stage_to_user(tape, buf, bytes_read)) 1195 /* copy out */
1545 ret = -EFAULT; 1196 todo = min_t(size_t, count - done, tape->valid);
1546 buf += bytes_read; 1197 if (copy_to_user(buf + done, tape->cur, todo))
1547 count -= bytes_read;
1548 actually_read += bytes_read;
1549 }
1550 if (count) {
1551 bytes_read = idetape_add_chrdev_read_request(drive, ctl);
1552 if (bytes_read <= 0)
1553 goto finish;
1554 temp = min((unsigned long)count, (unsigned long)bytes_read);
1555 if (idetape_copy_stage_to_user(tape, buf, temp))
1556 ret = -EFAULT; 1198 ret = -EFAULT;
1557 actually_read += temp; 1199
1558 tape->merge_bh_size = bytes_read-temp; 1200 tape->cur += todo;
1201 tape->valid -= todo;
1202 done += todo;
1559 } 1203 }
1560finish: 1204
1561 if (!actually_read && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) { 1205 if (!done && test_bit(IDE_AFLAG_FILEMARK, &drive->atapi_flags)) {
1562 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name); 1206 debug_log(DBG_SENSE, "%s: spacing over filemark\n", tape->name);
1563 1207
1564 idetape_space_over_filemarks(drive, MTFSF, 1); 1208 idetape_space_over_filemarks(drive, MTFSF, 1);
1565 return 0; 1209 return 0;
1566 } 1210 }
1567 1211
1568 return ret ? ret : actually_read; 1212 return ret ? ret : done;
1569} 1213}
1570 1214
1571static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf, 1215static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
@@ -1573,9 +1217,9 @@ static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
1573{ 1217{
1574 struct ide_tape_obj *tape = file->private_data; 1218 struct ide_tape_obj *tape = file->private_data;
1575 ide_drive_t *drive = tape->drive; 1219 ide_drive_t *drive = tape->drive;
1576 ssize_t actually_written = 0; 1220 size_t done = 0;
1577 ssize_t ret = 0; 1221 ssize_t ret = 0;
1578 u16 ctl = *(u16 *)&tape->caps[12]; 1222 int rc;
1579 1223
1580 /* The drive is write protected. */ 1224 /* The drive is write protected. */
1581 if (tape->write_prot) 1225 if (tape->write_prot)
@@ -1584,80 +1228,31 @@ static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
1584 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count); 1228 debug_log(DBG_CHRDEV, "Enter %s, count %Zd\n", __func__, count);
1585 1229
1586 /* Initialize write operation */ 1230 /* Initialize write operation */
1587 if (tape->chrdev_dir != IDETAPE_DIR_WRITE) { 1231 rc = idetape_init_rw(drive, IDETAPE_DIR_WRITE);
1588 if (tape->chrdev_dir == IDETAPE_DIR_READ) 1232 if (rc < 0)
1589 ide_tape_discard_merge_buffer(drive, 1); 1233 return rc;
1590 if (tape->merge_bh || tape->merge_bh_size) {
1591 printk(KERN_ERR "ide-tape: merge_bh_size "
1592 "should be 0 now\n");
1593 tape->merge_bh_size = 0;
1594 }
1595 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0);
1596 if (!tape->merge_bh)
1597 return -ENOMEM;
1598 tape->chrdev_dir = IDETAPE_DIR_WRITE;
1599 idetape_init_merge_buffer(tape);
1600 1234
1601 /* 1235 while (done < count) {
1602 * Issue a write 0 command to ensure that DSC handshake is 1236 size_t todo;
1603 * switched from completion mode to buffer available mode. No 1237
1604 * point in issuing this if DSC overlap isn't supported, some 1238 /* flush if staging buffer is full */
1605 * drives (Seagate STT3401A) will return an error. 1239 if (tape->valid == tape->buffer_size &&
1606 */ 1240 idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE,
1607 if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) { 1241 tape->buffer_size) <= 0)
1608 ssize_t retval = idetape_queue_rw_tail(drive, 1242 return rc;
1609 REQ_IDETAPE_WRITE, 0, 1243
1610 tape->merge_bh); 1244 /* copy in */
1611 if (retval < 0) { 1245 todo = min_t(size_t, count - done,
1612 ide_tape_kfree_buffer(tape); 1246 tape->buffer_size - tape->valid);
1613 tape->merge_bh = NULL; 1247 if (copy_from_user(tape->cur, buf + done, todo))
1614 tape->chrdev_dir = IDETAPE_DIR_NONE;
1615 return retval;
1616 }
1617 }
1618 }
1619 if (count == 0)
1620 return (0);
1621 if (tape->merge_bh_size) {
1622 if (tape->merge_bh_size >= tape->buffer_size) {
1623 printk(KERN_ERR "ide-tape: bug: merge buf too big\n");
1624 tape->merge_bh_size = 0;
1625 }
1626 actually_written = min((unsigned int)
1627 (tape->buffer_size - tape->merge_bh_size),
1628 (unsigned int)count);
1629 if (idetape_copy_stage_from_user(tape, buf, actually_written))
1630 ret = -EFAULT;
1631 buf += actually_written;
1632 tape->merge_bh_size += actually_written;
1633 count -= actually_written;
1634
1635 if (tape->merge_bh_size == tape->buffer_size) {
1636 ssize_t retval;
1637 tape->merge_bh_size = 0;
1638 retval = idetape_add_chrdev_write_request(drive, ctl);
1639 if (retval <= 0)
1640 return (retval);
1641 }
1642 }
1643 while (count >= tape->buffer_size) {
1644 ssize_t retval;
1645 if (idetape_copy_stage_from_user(tape, buf, tape->buffer_size))
1646 ret = -EFAULT;
1647 buf += tape->buffer_size;
1648 count -= tape->buffer_size;
1649 retval = idetape_add_chrdev_write_request(drive, ctl);
1650 actually_written += tape->buffer_size;
1651 if (retval <= 0)
1652 return (retval);
1653 }
1654 if (count) {
1655 actually_written += count;
1656 if (idetape_copy_stage_from_user(tape, buf, count))
1657 ret = -EFAULT; 1248 ret = -EFAULT;
1658 tape->merge_bh_size += count; 1249
1250 tape->cur += todo;
1251 tape->valid += todo;
1252 done += todo;
1659 } 1253 }
1660 return ret ? ret : actually_written; 1254
1255 return ret ? ret : done;
1661} 1256}
1662 1257
1663static int idetape_write_filemark(ide_drive_t *drive) 1258static int idetape_write_filemark(ide_drive_t *drive)
@@ -1818,7 +1413,7 @@ static int idetape_chrdev_ioctl(struct inode *inode, struct file *file,
1818 idetape_flush_tape_buffers(drive); 1413 idetape_flush_tape_buffers(drive);
1819 } 1414 }
1820 if (cmd == MTIOCGET || cmd == MTIOCPOS) { 1415 if (cmd == MTIOCGET || cmd == MTIOCPOS) {
1821 block_offset = tape->merge_bh_size / 1416 block_offset = tape->valid /
1822 (tape->blk_size * tape->user_bs_factor); 1417 (tape->blk_size * tape->user_bs_factor);
1823 position = idetape_read_position(drive); 1418 position = idetape_read_position(drive);
1824 if (position < 0) 1419 if (position < 0)
@@ -1966,12 +1561,12 @@ static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
1966 idetape_tape_t *tape = drive->driver_data; 1561 idetape_tape_t *tape = drive->driver_data;
1967 1562
1968 ide_tape_flush_merge_buffer(drive); 1563 ide_tape_flush_merge_buffer(drive);
1969 tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0); 1564 tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL);
1970 if (tape->merge_bh != NULL) { 1565 if (tape->buf != NULL) {
1971 idetape_pad_zeros(drive, tape->blk_size * 1566 idetape_pad_zeros(drive, tape->blk_size *
1972 (tape->user_bs_factor - 1)); 1567 (tape->user_bs_factor - 1));
1973 ide_tape_kfree_buffer(tape); 1568 kfree(tape->buf);
1974 tape->merge_bh = NULL; 1569 tape->buf = NULL;
1975 } 1570 }
1976 idetape_write_filemark(drive); 1571 idetape_write_filemark(drive);
1977 idetape_flush_tape_buffers(drive); 1572 idetape_flush_tape_buffers(drive);
@@ -2165,8 +1760,6 @@ static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
2165 u16 *ctl = (u16 *)&tape->caps[12]; 1760 u16 *ctl = (u16 *)&tape->caps[12];
2166 1761
2167 drive->pc_callback = ide_tape_callback; 1762 drive->pc_callback = ide_tape_callback;
2168 drive->pc_update_buffers = idetape_update_buffers;
2169 drive->pc_io_buffers = ide_tape_io_buffers;
2170 1763
2171 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP; 1764 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
2172 1765
@@ -2197,11 +1790,6 @@ static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
2197 tape->buffer_size = *ctl * tape->blk_size; 1790 tape->buffer_size = *ctl * tape->blk_size;
2198 } 1791 }
2199 buffer_size = tape->buffer_size; 1792 buffer_size = tape->buffer_size;
2200 tape->pages_per_buffer = buffer_size / PAGE_SIZE;
2201 if (buffer_size % PAGE_SIZE) {
2202 tape->pages_per_buffer++;
2203 tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
2204 }
2205 1793
2206 /* select the "best" DSC read/write polling freq */ 1794 /* select the "best" DSC read/write polling freq */
2207 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]); 1795 speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
@@ -2244,7 +1832,7 @@ static void ide_tape_release(struct device *dev)
2244 ide_drive_t *drive = tape->drive; 1832 ide_drive_t *drive = tape->drive;
2245 struct gendisk *g = tape->disk; 1833 struct gendisk *g = tape->disk;
2246 1834
2247 BUG_ON(tape->merge_bh_size); 1835 BUG_ON(tape->valid);
2248 1836
2249 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP; 1837 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
2250 drive->driver_data = NULL; 1838 drive->driver_data = NULL;