Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
/*
 *  scsi.c Copyright (C) 1992 Drew Eckhardt
 *         Copyright (C) 1993, 1994, 1995 Eric Youngdale
 *
 *  generic mid-level SCSI driver
 *      Initial versions: Drew Eckhardt
 *      Subsequent revisions: Eric Youngdale
 *
 *  <drew@colorado.edu>
 *
 *  Bug correction thanks go to :
 *      Rik Faith <faith@cs.unc.edu>
 *      Tommy Thorn <tthorn>
 *      Thomas Wuensche <tw@fgb1.fgb.mw.tu-muenchen.de>
 *
 *  Modified by Eric Youngdale eric@aib.com to
 *  add scatter-gather, multiple outstanding request, and other
 *  enhancements.
 *
 *  Native multichannel, wide scsi, /proc/scsi and hot plugging
 *  support added by Michael Neuffer <mike@i-connect.net>
 *
 *  Major improvements to the timeout, abort, and reset processing,
 *  as well as performance modifications for large queue depths by
 *  Leonard N. Zubkoff <lnz@dandelion.com>
 */

/*
 *#########################################################################
 *#########################################################################
 *#########################################################################
 *#########################################################################
 *		NOTE - NOTE - NOTE - NOTE - NOTE - NOTE - NOTE
 *
 *#########################################################################
 *#########################################################################
 *#########################################################################
 *#########################################################################
 *
 * This file contains the 'old' scsi error handling.  It is only present
 * while the new error handling code is being debugged, and while the low
 * level drivers are being converted to use the new code.  Once the last
 * driver uses the new code this *ENTIRE* file will be nuked.
 */

#define __NO_VERSION__
#include <linux/module.h>

#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/string.h>
#include <linux/malloc.h>
#include <linux/ioport.h>
#include <linux/kernel.h>
#include <linux/stat.h>
#include <linux/blk.h>
#include <linux/interrupt.h>
#include <linux/delay.h>

#include <asm/system.h>
#include <asm/irq.h>
#include <asm/dma.h>

#include "scsi.h"
#include "hosts.h"
#include "constants.h"

#undef USE_STATIC_SCSI_MEMORY

/*
static const char RCSid[] = "$Header: /mnt/ide/home/eric/CVSROOT/linux/drivers/scsi/scsi_obsolete.c,v 1.1 1997/05/18 23:27:21 eric Exp $";
*/


#define INTERNAL_ERROR (panic ("Internal error in file %s, line %d.\n", __FILE__, __LINE__))


static int scsi_abort (Scsi_Cmnd *, int code);
static int scsi_reset (Scsi_Cmnd *, unsigned int);

extern void scsi_old_done (Scsi_Cmnd *SCpnt);
int update_timeout (Scsi_Cmnd *, int);
extern void scsi_old_times_out (Scsi_Cmnd * SCpnt);
extern void internal_cmnd (Scsi_Cmnd * SCpnt);

extern volatile struct Scsi_Host * host_active;
#define SCSI_BLOCK(HOST) ((HOST->block && host_active && HOST != host_active) \
			  || (HOST->can_queue && HOST->host_busy >= HOST->can_queue))

static unsigned char generic_sense[6] = {REQUEST_SENSE, 0,0,0, 255, 0};

/*
 *  This is the number  of clock ticks we should wait before we time out
 *  and abort the command.  This is for  where the scsi.c module generates
 *  the command, not where it originates from a higher level, in which
 *  case the timeout is specified there.
 *
 *  ABORT_TIMEOUT and RESET_TIMEOUT are the timeouts for RESET and ABORT
 *  respectively.
 */

#ifdef DEBUG_TIMEOUT
static void scsi_dump_status(void);
#endif


#ifdef DEBUG
    #define SCSI_TIMEOUT (5*HZ)
#else
    #define SCSI_TIMEOUT (2*HZ)
#endif

#ifdef DEBUG
    #define SENSE_TIMEOUT SCSI_TIMEOUT
    #define ABORT_TIMEOUT SCSI_TIMEOUT
    #define RESET_TIMEOUT SCSI_TIMEOUT
#else
    #define SENSE_TIMEOUT (5*HZ/10)
    #define RESET_TIMEOUT (5*HZ/10)
    #define ABORT_TIMEOUT (5*HZ/10)
#endif


/* Do not call reset on error if we just did a reset within 15 sec. */
#define MIN_RESET_PERIOD (15*HZ)



/*
 *  Flag bits for the internal_timeout array
 */
#define NORMAL_TIMEOUT 0
#define IN_ABORT  1
#define IN_RESET  2
#define IN_RESET2 4
#define IN_RESET3 8

/*
 * This is our time out function, called when the timer expires for a
 * given host adapter.  It will attempt to abort the currently executing
 * command, that failing perform a kernel panic.
 */

void scsi_old_times_out (Scsi_Cmnd * SCpnt)
{
    unsigned long flags;

    spin_lock_irqsave(&io_request_lock, flags);

    /* Set the serial_number_at_timeout to the current serial_number */
    SCpnt->serial_number_at_timeout = SCpnt->serial_number;

    switch (SCpnt->internal_timeout & (IN_ABORT | IN_RESET | IN_RESET2 | IN_RESET3))
    {
    case NORMAL_TIMEOUT:
	{
#ifdef DEBUG_TIMEOUT
	    scsi_dump_status();
#endif
	}

	if (!scsi_abort (SCpnt, DID_TIME_OUT))
	    break;
    case IN_ABORT:
	printk("SCSI host %d abort (pid %ld) timed out - resetting\n",
	       SCpnt->host->host_no, SCpnt->pid);
	if (!scsi_reset (SCpnt, SCSI_RESET_ASYNCHRONOUS))
	    break;
    case IN_RESET:
    case (IN_ABORT | IN_RESET):
	/* This might be controversial, but if there is a bus hang,
	 * you might conceivably want the machine up and running
	 * esp if you have an ide disk.
	 */
	printk("SCSI host %d channel %d reset (pid %ld) timed out - "
               "trying harder\n",
	       SCpnt->host->host_no, SCpnt->channel, SCpnt->pid);
	SCpnt->internal_timeout &= ~IN_RESET;
	SCpnt->internal_timeout |= IN_RESET2;
        scsi_reset (SCpnt,
		    SCSI_RESET_ASYNCHRONOUS | SCSI_RESET_SUGGEST_BUS_RESET);
        break;
    case (IN_ABORT | IN_RESET | IN_RESET2):
	/* Obviously the bus reset didn't work.
	 * Let's try even harder and call for an HBA reset.
         * Maybe the HBA itself crashed and this will shake it loose.
	 */
	printk("SCSI host %d reset (pid %ld) timed out - trying to shake it loose\n",
	       SCpnt->host->host_no, SCpnt->pid);
	SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2);
	SCpnt->internal_timeout |= IN_RESET3;
        scsi_reset (SCpnt,
		    SCSI_RESET_ASYNCHRONOUS | SCSI_RESET_SUGGEST_HOST_RESET);
	break;

    default:
	printk("SCSI host %d reset (pid %ld) timed out again -\n",
	       SCpnt->host->host_no, SCpnt->pid);
	printk("probably an unrecoverable SCSI bus or device hang.\n");
	break;

    }
    spin_unlock_irqrestore(&io_request_lock, flags);

}

/*
 *  From what I can find in scsi_obsolete.c, this function is only called
 *  by scsi_old_done and scsi_reset.  Both of these functions run with the
 *  io_request_lock already held, so we need do nothing here about grabbing
 *  any locks.
 */
static void scsi_request_sense (Scsi_Cmnd * SCpnt)
{
    SCpnt->flags |= WAS_SENSE | ASKED_FOR_SENSE;
    update_timeout(SCpnt, SENSE_TIMEOUT);


    memcpy ((void *) SCpnt->cmnd , (void *) generic_sense,
	    sizeof(generic_sense));

    SCpnt->cmnd[1] = SCpnt->lun << 5;
    SCpnt->cmnd[4] = sizeof(SCpnt->sense_buffer);

    SCpnt->request_buffer = &SCpnt->sense_buffer;
    SCpnt->request_bufflen = sizeof(SCpnt->sense_buffer);
    SCpnt->use_sg = 0;
    SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
    internal_cmnd (SCpnt);
}




static int check_sense (Scsi_Cmnd * SCpnt)
{
    /* If there is no sense information, request it.  If we have already
     * requested it, there is no point in asking again - the firmware must
     * be confused.
     */
    if (((SCpnt->sense_buffer[0] & 0x70) >> 4) != 7) {
	if(!(SCpnt->flags & ASKED_FOR_SENSE))
	    return SUGGEST_SENSE;
	else
	    return SUGGEST_RETRY;
    }

    SCpnt->flags &= ~ASKED_FOR_SENSE;

#ifdef DEBUG_INIT
    printk("scsi%d, channel%d : ", SCpnt->host->host_no, SCpnt->channel);
    print_sense("", SCpnt);
    printk("\n");
#endif
    if (SCpnt->sense_buffer[2] & 0xe0)
	return SUGGEST_ABORT;

    switch (SCpnt->sense_buffer[2] & 0xf)
    {
    case NO_SENSE:
	return 0;
    case RECOVERED_ERROR:
	return SUGGEST_IS_OK;

    case ABORTED_COMMAND:
	return SUGGEST_RETRY;
    case NOT_READY:
    case UNIT_ATTENTION:
        /*
         * If we are expecting a CC/UA because of a bus reset that we
         * performed, treat this just as a retry.  Otherwise this is
         * information that we should pass up to the upper-level driver
         * so that we can deal with it there.
         */
        if( SCpnt->device->expecting_cc_ua )
        {
            SCpnt->device->expecting_cc_ua = 0;
            return SUGGEST_RETRY;
        }
	return SUGGEST_ABORT;

    /* these three are not supported */
    case COPY_ABORTED:
    case VOLUME_OVERFLOW:
    case MISCOMPARE:

    case MEDIUM_ERROR:
	return SUGGEST_REMAP;
    case BLANK_CHECK:
    case DATA_PROTECT:
    case HARDWARE_ERROR:
    case ILLEGAL_REQUEST:
    default:
	return SUGGEST_ABORT;
    }
}

/* This function is the mid-level interrupt routine, which decides how
 *  to handle error conditions.  Each invocation of this function must
 *  do one and *only* one of the following:
 *
 *  (1) Call last_cmnd[host].done.  This is done for fatal errors and
 *      normal completion, and indicates that the handling for this
 *      request is complete.
 *  (2) Call internal_cmnd to requeue the command.  This will result in
 *      scsi_done being called again when the retry is complete.
 *  (3) Call scsi_request_sense.  This asks the host adapter/drive for
 *      more information about the error condition.  When the information
 *      is available, scsi_done will be called again.
 *  (4) Call reset().  This is sort of a last resort, and the idea is that
 *      this may kick things loose and get the drive working again.  reset()
 *      automatically calls scsi_request_sense, and thus scsi_done will be
 *      called again once the reset is complete.
 *
 *      If none of the above actions are taken, the drive in question
 *      will hang. If more than one of the above actions are taken by
 *      scsi_done, then unpredictable behavior will result.
 */
void scsi_old_done (Scsi_Cmnd * SCpnt)
{
    int status=0;
    int exit=0;
    int checked;
    int oldto;
    struct Scsi_Host * host = SCpnt->host;
    int result = SCpnt->result;
    SCpnt->serial_number = 0;
    SCpnt->serial_number_at_timeout = 0;
    oldto = update_timeout(SCpnt, 0);

#ifdef DEBUG_TIMEOUT
    if(result) printk("Non-zero result in scsi_done %x %d:%d\n",
		      result, SCpnt->target, SCpnt->lun);
#endif

    /* If we requested an abort, (and we got it) then fix up the return
     *  status to say why
     */
    if(host_byte(result) == DID_ABORT && SCpnt->abort_reason)
	SCpnt->result = result = (result & 0xff00ffff) |
	    (SCpnt->abort_reason << 16);


#define CMD_FINISHED 0
#define MAYREDO  1
#define REDO     3
#define PENDING  4

#ifdef DEBUG
    printk("In scsi_done(host = %d, result = %06x)\n", host->host_no, result);
#endif

    if(SCpnt->flags & WAS_SENSE)
    {
	SCpnt->use_sg = SCpnt->old_use_sg;
	SCpnt->cmd_len = SCpnt->old_cmd_len;
    }

    switch (host_byte(result))
    {
    case DID_OK:
	if (status_byte(result) && (SCpnt->flags & WAS_SENSE))
	    /* Failed to obtain sense information */
	{
	    SCpnt->flags &= ~WAS_SENSE;
#if 0	/* This cannot possibly be correct. */
	    SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
#endif

	    if (!(SCpnt->flags & WAS_RESET))
	    {
		printk("scsi%d : channel %d target %d lun %d request sense"
		       " failed, performing reset.\n",
		       SCpnt->host->host_no, SCpnt->channel, SCpnt->target,
		       SCpnt->lun);
		scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
		return;
	    }
	    else
	    {
		exit = (DRIVER_HARD | SUGGEST_ABORT);
		status = CMD_FINISHED;
	    }
	}
	else switch(msg_byte(result))
	{
	case COMMAND_COMPLETE:
	    switch (status_byte(result))
	    {
	    case GOOD:
		if (SCpnt->flags & WAS_SENSE)
		{
#ifdef DEBUG
		    printk ("In scsi_done, GOOD status, COMMAND COMPLETE, "
                            "parsing sense information.\n");
#endif
		    SCpnt->flags &= ~WAS_SENSE;
#if 0	/* This cannot possibly be correct. */
		    SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
#endif

		    switch (checked = check_sense(SCpnt))
		    {
		    case SUGGEST_SENSE:
		    case 0:
#ifdef DEBUG
			printk("NO SENSE.  status = REDO\n");
#endif
			update_timeout(SCpnt, oldto);
			status = REDO;
			break;
		    case SUGGEST_IS_OK:
			break;
		    case SUGGEST_REMAP:
#ifdef DEBUG
			printk("SENSE SUGGEST REMAP - status = CMD_FINISHED\n");
#endif
			status = CMD_FINISHED;
			exit = DRIVER_SENSE | SUGGEST_ABORT;
			break;
		    case SUGGEST_RETRY:
#ifdef DEBUG
			printk("SENSE SUGGEST RETRY - status = MAYREDO\n");
#endif
			status = MAYREDO;
			exit = DRIVER_SENSE | SUGGEST_RETRY;
			break;
		    case SUGGEST_ABORT:
#ifdef DEBUG
			printk("SENSE SUGGEST ABORT - status = CMD_FINISHED");
#endif
			status = CMD_FINISHED;
			exit =  DRIVER_SENSE | SUGGEST_ABORT;
			break;
		    default:
			printk ("Internal error %s %d \n", __FILE__,
				__LINE__);
		    }
		} /* end WAS_SENSE */
		else
		{
#ifdef DEBUG
		    printk("COMMAND COMPLETE message returned, "
                           "status = CMD_FINISHED. \n");
#endif
		    exit =  DRIVER_OK;
		    status = CMD_FINISHED;
		}
		break;

	    case CHECK_CONDITION:
	    case COMMAND_TERMINATED:
		switch (check_sense(SCpnt))
		{
		case 0:
		    update_timeout(SCpnt, oldto);
		    status = REDO;
		    break;
		case SUGGEST_REMAP:
		    status = CMD_FINISHED;
		    exit =  DRIVER_SENSE | SUGGEST_ABORT;
		    break;
		case SUGGEST_RETRY:
		    status = MAYREDO;
		    exit = DRIVER_SENSE | SUGGEST_RETRY;
		    break;
		case SUGGEST_ABORT:
		    status = CMD_FINISHED;
		    exit =  DRIVER_SENSE | SUGGEST_ABORT;
		    break;
		case SUGGEST_SENSE:
		    scsi_request_sense (SCpnt);
		    status = PENDING;
		    break;
		}
		break;

	    case CONDITION_GOOD:
	    case INTERMEDIATE_GOOD:
	    case INTERMEDIATE_C_GOOD:
		break;

	    case BUSY:
	    case QUEUE_FULL:
		update_timeout(SCpnt, oldto);
		status = REDO;
		break;

	    case RESERVATION_CONFLICT:
		printk("scsi%d, channel %d : RESERVATION CONFLICT performing"
		       " reset.\n", SCpnt->host->host_no, SCpnt->channel);
		scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
		return;
#if 0
		exit = DRIVER_SOFT | SUGGEST_ABORT;
		status = MAYREDO;
		break;
#endif
	    default:
		printk ("Internal error %s %d \n"
			"status byte = %d \n", __FILE__,
			__LINE__, status_byte(result));

	    }
	    break;
	default:
	    panic("scsi: unsupported message byte %d received\n",
		  msg_byte(result));
	}
	break;
    case DID_TIME_OUT:
#ifdef DEBUG
	printk("Host returned DID_TIME_OUT - ");
#endif

	if (SCpnt->flags & WAS_TIMEDOUT)
	{
#ifdef DEBUG
	    printk("Aborting\n");
#endif
	    /*
	      Allow TEST_UNIT_READY and INQUIRY commands to timeout early
	      without causing resets.  All other commands should be retried.
	    */
	    if (SCpnt->cmnd[0] != TEST_UNIT_READY &&
		SCpnt->cmnd[0] != INQUIRY)
		    status = MAYREDO;
	    exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
	}
	else
	{
#ifdef DEBUG
	    printk ("Retrying.\n");
#endif
	    SCpnt->flags  |= WAS_TIMEDOUT;
	    SCpnt->internal_timeout &= ~IN_ABORT;
	    status = REDO;
	}
	break;
    case DID_BUS_BUSY:
    case DID_PARITY:
	status = REDO;
	break;
    case DID_NO_CONNECT:
#ifdef DEBUG
	printk("Couldn't connect.\n");
#endif
	exit  = (DRIVER_HARD | SUGGEST_ABORT);
	break;
    case DID_ERROR:
	status = MAYREDO;
	exit = (DRIVER_HARD | SUGGEST_ABORT);
	break;
    case DID_BAD_TARGET:
    case DID_ABORT:
	exit = (DRIVER_INVALID | SUGGEST_ABORT);
	break;
    case DID_RESET:
	if (SCpnt->flags & IS_RESETTING)
	{
	    SCpnt->flags &= ~IS_RESETTING;
	    status = REDO;
	    break;
	}

	if(msg_byte(result) == GOOD &&
	   status_byte(result) == CHECK_CONDITION) {
	    switch (check_sense(SCpnt)) {
	    case 0:
		update_timeout(SCpnt, oldto);
		status = REDO;
		break;
	    case SUGGEST_REMAP:
	    case SUGGEST_RETRY:
		status = MAYREDO;
		exit = DRIVER_SENSE | SUGGEST_RETRY;
		break;
	    case SUGGEST_ABORT:
		status = CMD_FINISHED;
		exit =  DRIVER_SENSE | SUGGEST_ABORT;
		break;
	    case SUGGEST_SENSE:
		scsi_request_sense (SCpnt);
		status = PENDING;
		break;
	    }
	} else {
	    status=REDO;
	    exit = SUGGEST_RETRY;
	}
	break;
    default :
	exit = (DRIVER_ERROR | SUGGEST_DIE);
    }

    switch (status)
    {
    case CMD_FINISHED:
    case PENDING:
	break;
    case MAYREDO:
#ifdef DEBUG
	printk("In MAYREDO, allowing %d retries, have %d\n",
	       SCpnt->allowed, SCpnt->retries);
#endif
	if ((++SCpnt->retries) < SCpnt->allowed)
	{
	    if ((SCpnt->retries >= (SCpnt->allowed >> 1))
		&& !(SCpnt->host->last_reset > 0 &&
		     jiffies < SCpnt->host->last_reset + MIN_RESET_PERIOD)
		&& !(SCpnt->flags & WAS_RESET))
	    {
		printk("scsi%d channel %d : resetting for second half of retries.\n",
		       SCpnt->host->host_no, SCpnt->channel);
		scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
		break;
	    }

	}
	else
	{
	    status = CMD_FINISHED;
	    break;
	}
	/* fall through to REDO */

    case REDO:

	if (SCpnt->flags & WAS_SENSE)
	    scsi_request_sense(SCpnt);
	else
	{
	    memcpy ((void *) SCpnt->cmnd,
		    (void*) SCpnt->data_cmnd,
		    sizeof(SCpnt->data_cmnd));
	    SCpnt->request_buffer = SCpnt->buffer;
	    SCpnt->request_bufflen = SCpnt->bufflen;
	    SCpnt->use_sg = SCpnt->old_use_sg;
	    SCpnt->cmd_len = SCpnt->old_cmd_len;
	    internal_cmnd (SCpnt);
	}
	break;
    default:
	INTERNAL_ERROR;
    }

    if (status == CMD_FINISHED) {
#ifdef DEBUG
	printk("Calling done function - at address %p\n", SCpnt->done);
#endif
	host->host_busy--; /* Indicate that we are free */

	if (host->block && host->host_busy == 0) {
	    host_active = NULL;

	    /* For block devices "wake_up" is done in end_scsi_request */
	    if (MAJOR(SCpnt->request.rq_dev) != SCSI_DISK_MAJOR &&
		MAJOR(SCpnt->request.rq_dev) != SCSI_CDROM_MAJOR) {
		struct Scsi_Host * next;

		for (next = host->block; next != host; next = next->block)
		    wake_up(&next->host_wait);
	    }

	}

	wake_up(&host->host_wait);
	SCpnt->result = result | ((exit & 0xff) << 24);
	SCpnt->use_sg = SCpnt->old_use_sg;
	SCpnt->cmd_len = SCpnt->old_cmd_len;
	SCpnt->done (SCpnt);
    }

#undef CMD_FINISHED
#undef REDO
#undef MAYREDO
#undef PENDING
}

/*
 * The scsi_abort function interfaces with the abort() function of the host
 * we are aborting, and causes the current command to not complete.  The
 * caller should deal with any error messages or status returned on the
 * next call.
 *
 * This will not be called reentrantly for a given host.
 */

/*
 * Since we're nice guys and specified that abort() and reset()
 * can be non-reentrant.  The internal_timeout flags are used for
 * this.
 */


static int scsi_abort (Scsi_Cmnd * SCpnt, int why)
{
    int oldto;
    struct Scsi_Host * host = SCpnt->host;

    while(1)
    {

	/*
	 * Protect against races here.  If the command is done, or we are
	 * on a different command forget it.
	 */
	if (SCpnt->serial_number != SCpnt->serial_number_at_timeout) {
	    return 0;
	}

	if (SCpnt->internal_timeout & IN_ABORT)
	{
	    spin_unlock_irq(&io_request_lock);
	    while (SCpnt->internal_timeout & IN_ABORT)
		barrier();
	    spin_lock_irq(&io_request_lock);
	}
	else
	{
	    SCpnt->internal_timeout |= IN_ABORT;
	    oldto = update_timeout(SCpnt, ABORT_TIMEOUT);

	    if ((SCpnt->flags & IS_RESETTING) && SCpnt->device->soft_reset) {
		/* OK, this command must have died when we did the
		 *  reset.  The device itself must have lied.
		 */
		printk("Stale command on %d %d:%d appears to have died when"
		       " the bus was reset\n",
		       SCpnt->channel, SCpnt->target, SCpnt->lun);
	    }

	    if (!host->host_busy) {
		SCpnt->internal_timeout &= ~IN_ABORT;
		update_timeout(SCpnt, oldto);
		return 0;
	    }
	    printk("scsi : aborting command due to timeout : pid %lu, scsi%d,"
		   " channel %d, id %d, lun %d ",
		   SCpnt->pid, SCpnt->host->host_no, (int) SCpnt->channel,
		   (int) SCpnt->target, (int) SCpnt->lun);
	    print_command (SCpnt->cmnd);
	    if (SCpnt->serial_number != SCpnt->serial_number_at_timeout)
		return 0;
	    SCpnt->abort_reason = why;
	    switch(host->hostt->abort(SCpnt)) {
		/* We do not know how to abort.  Try waiting another
		 * time increment and see if this helps. Set the
		 * WAS_TIMEDOUT flag set so we do not try this twice
		 */
	    case SCSI_ABORT_BUSY: /* Tough call - returning 1 from
				   * this is too severe
				   */
	    case SCSI_ABORT_SNOOZE:
		if(why == DID_TIME_OUT) {
		    SCpnt->internal_timeout &= ~IN_ABORT;
		    if(SCpnt->flags & WAS_TIMEDOUT) {
			return 1; /* Indicate we cannot handle this.
				   * We drop down into the reset handler
				   * and try again
				   */
		    } else {
			SCpnt->flags |= WAS_TIMEDOUT;
			oldto = SCpnt->timeout_per_command;
			update_timeout(SCpnt, oldto);
		    }
		}
		return 0;
	    case SCSI_ABORT_PENDING:
		if(why != DID_TIME_OUT) {
		    update_timeout(SCpnt, oldto);
		}
		return 0;
	    case SCSI_ABORT_SUCCESS:
		/* We should have already aborted this one.  No
		 * need to adjust timeout
		 */
                 SCpnt->internal_timeout &= ~IN_ABORT;
                 return 0;
	    case SCSI_ABORT_NOT_RUNNING:
		SCpnt->internal_timeout &= ~IN_ABORT;
		update_timeout(SCpnt, 0);
		return 0;
	    case SCSI_ABORT_ERROR:
	    default:
		SCpnt->internal_timeout &= ~IN_ABORT;
		return 1;
	    }
	}
    }
}


/* Mark a single SCSI Device as having been reset. */

static inline void scsi_mark_device_reset(Scsi_Device *Device)
{
  Device->was_reset = 1;
  Device->expecting_cc_ua = 1;
}


/* Mark all SCSI Devices on a specific Host as having been reset. */

void scsi_mark_host_reset(struct Scsi_Host *Host)
{
  Scsi_Cmnd   * SCpnt;
  Scsi_Device * SDpnt;

  for (SDpnt = Host->host_queue; SDpnt; SDpnt = SDpnt->next)
    {
      for (SCpnt = SDpnt->device_queue; SCpnt; SCpnt = SCpnt->next)
        scsi_mark_device_reset(SCpnt->device);
    }
}


/* Mark all SCSI Devices on a specific Host Bus as having been reset. */

static void scsi_mark_bus_reset(struct Scsi_Host *Host, int channel)
{
  Scsi_Cmnd *SCpnt;
  Scsi_Device * SDpnt;

  for (SDpnt = Host->host_queue; SDpnt; SDpnt = SDpnt->next)
    {
      for (SCpnt = SDpnt->device_queue; SCpnt; SCpnt = SCpnt->next)
        if (SCpnt->channel == channel)
	  scsi_mark_device_reset(SCpnt->device);
    }
}


static int scsi_reset (Scsi_Cmnd * SCpnt, unsigned int reset_flags)
{
    int temp;
    Scsi_Cmnd * SCpnt1;
    Scsi_Device * SDpnt;
    struct Scsi_Host * host = SCpnt->host;

    printk("SCSI bus is being reset for host %d channel %d.\n",
	   host->host_no, SCpnt->channel);

#if 0
    /*
     * First of all, we need to make a recommendation to the low-level
     * driver as to whether a BUS_DEVICE_RESET should be performed,
     * or whether we should do a full BUS_RESET.  There is no simple
     * algorithm here - we basically use a series of heuristics
     * to determine what we should do.
     */
    SCpnt->host->suggest_bus_reset = FALSE;

    /*
     * First see if all of the active devices on the bus have
     * been jammed up so that we are attempting resets.  If so,
     * then suggest a bus reset.  Forcing a bus reset could
     * result in some race conditions, but no more than
     * you would usually get with timeouts.  We will cross
     * that bridge when we come to it.
     *
     * This is actually a pretty bad idea, since a sequence of
     * commands will often timeout together and this will cause a
     * Bus Device Reset followed immediately by a SCSI Bus Reset.
     * If all of the active devices really are jammed up, the
     * Bus Device Reset will quickly timeout and scsi_times_out
     * will follow up with a SCSI Bus Reset anyway.
     */
    SCpnt1 = host->host_queue;
    while(SCpnt1) {
	if( SCpnt1->request.rq_status != RQ_INACTIVE
	    && (SCpnt1->flags & (WAS_RESET | IS_RESETTING)) == 0 )
            	break;
        SCpnt1 = SCpnt1->next;
 	}
    if( SCpnt1 == NULL ) {
        reset_flags |= SCSI_RESET_SUGGEST_BUS_RESET;
    }

    /*
     * If the code that called us is suggesting a hard reset, then
     * definitely request it.  This usually occurs because a
     * BUS_DEVICE_RESET times out.
     *
     * Passing reset_flags along takes care of this automatically.
     */
    if( reset_flags & SCSI_RESET_SUGGEST_BUS_RESET ) {
        SCpnt->host->suggest_bus_reset = TRUE;
    }
#endif

    while (1) {

	/*
	 * Protect against races here.  If the command is done, or we are
	 * on a different command forget it.
	 */
	if (reset_flags & SCSI_RESET_ASYNCHRONOUS)
	  if (SCpnt->serial_number != SCpnt->serial_number_at_timeout) {
	    return 0;
	  }

	if (SCpnt->internal_timeout & IN_RESET)
	{
	    spin_unlock_irq(&io_request_lock);
	    while (SCpnt->internal_timeout & IN_RESET)
		barrier();
	    spin_lock_irq(&io_request_lock);
	}
	else
	{
	    SCpnt->internal_timeout |= IN_RESET;
	    update_timeout(SCpnt, RESET_TIMEOUT);

	    if (host->host_busy)
	    {
                for(SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next)
                {
                    SCpnt1 = SDpnt->device_queue;
                    while(SCpnt1) {
                        if (SCpnt1->request.rq_status != RQ_INACTIVE) {
#if 0
                            if (!(SCpnt1->flags & IS_RESETTING) &&
                                !(SCpnt1->internal_timeout & IN_ABORT))
                                scsi_abort(SCpnt1, DID_RESET);
#endif
                            SCpnt1->flags |= (WAS_RESET | IS_RESETTING);
                        }
                        SCpnt1 = SCpnt1->next;
                    }
                }

		host->last_reset = jiffies;
		temp = host->hostt->reset(SCpnt, reset_flags);
		/*
		  This test allows the driver to introduce an additional bus
		  settle time delay by setting last_reset up to 20 seconds in
		  the future.  In the normal case where the driver does not
		  modify last_reset, it must be assumed that the actual bus
		  reset occurred immediately prior to the return to this code,
		  and so last_reset must be updated to the current time, so
		  that the delay in internal_cmnd will guarantee at least a
		  MIN_RESET_DELAY bus settle time.
		*/
		if (host->last_reset - jiffies > 20UL * HZ)
		  host->last_reset = jiffies;
	    }
	    else
	    {
		if (!host->block) host->host_busy++;
		host->last_reset = jiffies;
	        SCpnt->flags |= (WAS_RESET | IS_RESETTING);
		temp = host->hostt->reset(SCpnt, reset_flags);
		if ((host->last_reset < jiffies) ||
		    (host->last_reset > (jiffies + 20 * HZ)))
		  host->last_reset = jiffies;
		if (!host->block) host->host_busy--;
	    }

#ifdef DEBUG
	    printk("scsi reset function returned %d\n", temp);
#endif

            /*
             * Now figure out what we need to do, based upon
             * what the low level driver said that it did.
	     * If the result is SCSI_RESET_SUCCESS, SCSI_RESET_PENDING,
	     * or SCSI_RESET_WAKEUP, then the low level driver did a
	     * bus device reset or bus reset, so we should go through
	     * and mark one or all of the devices on that bus
	     * as having been reset.
             */
            switch(temp & SCSI_RESET_ACTION) {
	    case SCSI_RESET_SUCCESS:
	        if (temp & SCSI_RESET_HOST_RESET)
		  scsi_mark_host_reset(host);
	        else if (temp & SCSI_RESET_BUS_RESET)
		  scsi_mark_bus_reset(host, SCpnt->channel);
		else scsi_mark_device_reset(SCpnt->device);
		SCpnt->internal_timeout &= ~(IN_RESET|IN_RESET2|IN_RESET3);
		return 0;
	    case SCSI_RESET_PENDING:
	        if (temp & SCSI_RESET_HOST_RESET)
		  scsi_mark_host_reset(host);
	        else if (temp & SCSI_RESET_BUS_RESET)
		  scsi_mark_bus_reset(host, SCpnt->channel);
		else scsi_mark_device_reset(SCpnt->device);
	    case SCSI_RESET_NOT_RUNNING:
		return 0;
	    case SCSI_RESET_PUNT:
		SCpnt->internal_timeout &= ~(IN_RESET|IN_RESET2|IN_RESET3);
                scsi_request_sense (SCpnt);
                return 0;
	    case SCSI_RESET_WAKEUP:
	        if (temp & SCSI_RESET_HOST_RESET)
		  scsi_mark_host_reset(host);
	        else if (temp & SCSI_RESET_BUS_RESET)
		  scsi_mark_bus_reset(host, SCpnt->channel);
		else scsi_mark_device_reset(SCpnt->device);
		SCpnt->internal_timeout &= ~(IN_RESET|IN_RESET2|IN_RESET3);
		scsi_request_sense (SCpnt);
                /*
                 * If a bus reset was performed, we
                 * need to wake up each and every command
                 * that was active on the bus or if it was a HBA
                 * reset all active commands on all channels
                 */
                if( temp & SCSI_RESET_HOST_RESET )
                {
                    for(SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next)
                    {
                        SCpnt1 = SDpnt->device_queue;
                        while(SCpnt1) {
                            if (SCpnt1->request.rq_status != RQ_INACTIVE
                                && SCpnt1 != SCpnt)
                                scsi_request_sense (SCpnt1);
                            SCpnt1 = SCpnt1->next;
                        }
                    }
                } else if( temp & SCSI_RESET_BUS_RESET ) {
                    for(SDpnt = host->host_queue; SDpnt; SDpnt = SDpnt->next)
                    {
                        SCpnt1 = SDpnt->device_queue;
                        while(SCpnt1) {
                            if(SCpnt1->request.rq_status != RQ_INACTIVE
                               && SCpnt1 != SCpnt
                               && SCpnt1->channel == SCpnt->channel)
                                scsi_request_sense (SCpnt);
                            SCpnt1 = SCpnt1->next;
                        }
                    }
                }
		return 0;
	    case SCSI_RESET_SNOOZE:
		/* In this case, we set the timeout field to 0
		 * so that this command does not time out any more,
		 * and we return 1 so that we get a message on the
		 * screen.
		 */
		SCpnt->internal_timeout &= ~(IN_RESET|IN_RESET2|IN_RESET3);
		update_timeout(SCpnt, 0);
		/* If you snooze, you lose... */
	    case SCSI_RESET_ERROR:
	    default:
		return 1;
	    }

	    return temp;
	}
    }
}

/*
 * The strategy is to cause the timer code to call scsi_times_out()
 * when the soonest timeout is pending.
 * The arguments are used when we are queueing a new command, because
 * we do not want to subtract the time used from this time, but when we
 * set the timer, we want to take this value into account.
 */

int update_timeout(Scsi_Cmnd * SCset, int timeout)
{
  int	rtn;

  /*
   * We are using the new error handling code to actually register/deregister
   * timers for timeout.
   */

  if( SCset->eh_timeout.expires == 0 )
    {
      rtn = 0;
    }
  else
    {
      rtn = SCset->eh_timeout.expires - jiffies;
    }

  if( timeout == 0 )
    {
      scsi_delete_timer(SCset);
    }
  else
    {
      scsi_add_timer(SCset, timeout, scsi_old_times_out);
    }

  return rtn;
}


/*
 * Overrides for Emacs so that we follow Linus's tabbing style.
 * Emacs will notice this stuff at the end of the file and automatically
 * adjust the settings for this buffer only.  This must remain at the end
 * of the file.
 * ---------------------------------------------------------------------------
 * Local variables:
 * c-indent-level: 4
 * c-brace-imaginary-offset: 0
 * c-brace-offset: -4
 * c-argdecl-indent: 4
 * c-label-offset: -4
 * c-continued-statement-offset: 4
 * c-continued-brace-offset: 0
 * indent-tabs-mode: nil
 * tab-width: 8
 * End:
 */