root/kernel/blk_drv/scsi/seagate.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. borken_init
  2. borken_wait
  3. seagate_st0x_detect
  4. seagate_st0x_info
  5. seagate_reconnect_intr
  6. seagate_st0x_queue_command
  7. seagate_st0x_command
  8. internal_command
  9. seagate_st0x_abort
  10. seagate_st0x_reset
  11. seagate_st0x_biosparam

   1 /*
   2  *      seagate.c Copyright (C) 1992, 1993 Drew Eckhardt 
   3  *      low level scsi driver for ST01/ST02, Future Domain TMC-885, 
   4  *      TMC-950  by
   5  *
   6  *              Drew Eckhardt 
   7  *
   8  *      <drew@colorado.edu>
   9  *
  10  *      Note : TMC-880 boards don't work because they have two bits in 
  11  *              the status register flipped, I'll fix this "RSN"
  12  *
  13  *      This card does all the I/O via memory mapped I/O, so there is no need
  14  *      to check or snarf a region of the I/O address space.
  15  */
  16 
  17 /*
  18  * Configuration : 
  19  * To use without BIOS -DOVERRIDE=base_address -DCONTROLLER=FD or SEAGATE
  20  * -DIRQ will overide the default of 5.
  21  * 
  22  * -DFAST or -DFAST32 will use blind transfers where possible
  23  *
  24  * -DARBITRATE will cause the host adapter to arbitrate for the 
  25  *      bus for better SCSI-II compatability, rather than just 
  26  *      waiting for BUS FREE and then doing it's thing.  Should
  27  *      let us do one command per Lun when I integrate my 
  28  *      reorganization changes into the distribution sources.
  29  *
  30  * -DSLOW_HANDSHAKE will allow compatability with broken devices that don't 
  31  *      handshake fast enough (ie, some CD ROM's) for the Seagate
  32  *      code.
  33  *
  34  * -DSLOW_RATE=x, x some number will let you specify a default 
  35  *      transfer rate if handshaking isn't working correctly.
  36  */
  37 
  38 #include <linux/config.h>
  39 
  40 #if defined(CONFIG_SCSI_SEAGATE) || defined(CONFIG_SCSI_FD_88x) 
  41 #include <asm/io.h>
  42 #include <asm/system.h>
  43 #include <linux/signal.h>
  44 #include <linux/sched.h>
  45 #include "../blk.h"
  46 #include "scsi.h"
  47 #include "hosts.h"
  48 #include "seagate.h"
  49 #include "constants.h"
  50 
  51 
  52 #ifndef IRQ
  53 #define IRQ 5
  54 #endif
  55 
  56 #if (defined(FAST32) && !defined(FAST))
  57 #define FAST
  58 #endif
  59 
  60 #if defined(SLOW_RATE) && !defined(SLOW_HANDSHAKE)
  61 #define SLOW_HANDSHAKE
  62 #endif
  63 
  64 #if defined(SLOW_HANDSHAKE) && !defined(SLOW_RATE)
  65 #define SLOW_RATE 50
  66 #endif
  67 
  68 
  69 #if defined(LINKED)
  70 #undef LINKED           /* Linked commands are currently broken ! */
  71 #endif
  72 
  73 static int internal_command(unsigned char target, unsigned char lun,
  74                             const void *cmnd,
  75                          void *buff, int bufflen, int reselect);
  76 
  77 static int incommand;                   /*
  78                                                 set if arbitration has finished and we are 
  79                                                 in some command phase.
  80                                         */
  81 
  82 static void *base_address = NULL;       /*
  83                                                 Where the card ROM starts,
  84                                                 used to calculate memory mapped
  85                                                 register location.
  86                                         */
  87 static volatile int abort_confirm = 0;
  88 
  89 static volatile void *st0x_cr_sr;       /*
  90                                                 control register write,
  91                                                 status register read.
  92                                                 256 bytes in length.
  93 
  94                                                 Read is status of SCSI BUS,
  95                                                 as per STAT masks.
  96 
  97                                         */
  98 
  99 
 100 static volatile void *st0x_dr;         /*
 101                                                 data register, read write
 102                                                 256 bytes in length.
 103                                         */
 104 
 105 
 106 static volatile int st0x_aborted=0;     /* 
 107                                                 set when we are aborted, ie by a time out, etc.
 108                                         */
 109 
 110                                         /*
 111                                                 In theory, we have a nice auto
 112                                                 detect routine - but this 
 113                                                 overides it. 
 114                                         */
 115 static unsigned char controller_type;   /* set to SEAGATE for ST0x boards or FD for TMC-88x boards */
 116                         
 117 #define retcode(result) (((result) << 16) | (message << 8) | status)                    
 118 #define STATUS (*(volatile unsigned char *) st0x_cr_sr)
 119 #define CONTROL STATUS 
 120 #define DATA (*(volatile unsigned char *) st0x_dr)
 121 
 122 
 123 #ifndef OVERRIDE                
 124 static const char *  seagate_bases[] = {
 125         (char *) 0xc8000, (char *) 0xca000, (char *) 0xcc000,
 126         (char *) 0xce000, (char *) 0xdc000, (char *) 0xde000
 127 };
 128 
 129 typedef struct {
 130         char *signature ;
 131         unsigned offset;
 132         unsigned length;
 133         unsigned char type;
 134 } Signature;
 135         
 136 static const Signature signatures[] = {
 137 #ifdef CONFIG_SCSI_SEAGATE
 138 {"SCSI BIOS 2.00  (C) Copyright 1987 Seagate", 15, 40, SEAGATE},
 139 
 140 /*
 141         The following two lines are NOT mistakes.  One detects 
 142         ROM revision 3.0.0, the other 3.2.  Since seagate
 143         has only one type of SCSI adapter, and this is not 
 144         going to change, the "SEAGATE" and "SCSI" together
 145         are probably "good enough"
 146 */
 147 
 148 {"SEAGATE SCSI BIOS ",16, 17, SEAGATE},
 149 {"SEAGATE SCSI BIOS ",17, 17, SEAGATE},
 150 
 151 /*
 152         This is for the Future Domain 88x series.  I've been told that
 153         the Seagate controllers are just repackages of these, and seeing
 154         early seagate BIOS bearing the Future Domain copyright,
 155         I believe it.
 156 */
 157 
 158 {"FUTURE DOMAIN CORP. (C) 1986-1988 V4.0I 03/16/88",5,48, FD},
 159 {"FUTURE DOMAIN CORP. (C) 1986-1989 V5.0C2/14/89", 5, 46, FD},
 160 {"FUTURE DOMAIN CORP. (C) 1986-1989 V6.0A7/28/89", 5, 46, FD},
 161 {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0105/31/90",5, 47, FD},
 162 {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0209/18/90",5, 47, FD},
 163 {"FUTURE DOMAIN CORP. (C) 1986-1990 V7.009/18/90", 5, 46, FD},
 164 {"FUTURE DOMAIN CORP. (C) 1992 V8.00.004/02/92",   5, 44, FD},
 165 #endif /* CONFIG_SCSI_SEAGATE */
 166 }
 167 ;
 168 /*
 169         Note that the last signature handles BIOS revisions 3.0.0 and 
 170         3.2 - the real ID's are 
 171 
 172 SEAGATE SCSI BIOS REVISION 3.0.0
 173 SEAGATE SCSI BIOS REVISION 3.2
 174 
 175 */
 176 
 177 #define NUM_SIGNATURES (sizeof(signatures) / sizeof(Signature))
 178 #endif /* n OVERRIDE */
 179 
 180 /*
 181  * hostno stores the hostnumber, as told to us by the init routine.
 182  */
 183 
 184 static int hostno = -1;
 185 static void seagate_reconnect_intr(int);
 186 
 187 #ifdef FAST
 188 static int fast = 1;
 189 #endif 
 190 
 191 #ifdef SLOW_HANDSHAKE
 192 /* 
 193  * Support for broken devices : 
 194  * The Seagate board has a handshaking problem.  Namely, a lack 
 195  * thereof for slow devices.  You can blast 600K/second through 
 196  * it if you are polling for each byte, more if you do a blind 
 197  * transfer.  In the first case, with a fast device, REQ will 
 198  * transition high-low or high-low-high before your loop restarts 
 199  * and you'll have no problems.  In the second case, the board 
 200  * will insert wait states for up to 13.2 usecs for REQ to 
 201  * transition low->high, and everything will work.
 202  *
 203  * However, there's nothing in the state machine that says 
 204  * you *HAVE* to see a high-low-high set of transitions before
 205  * sending the next byte, and slow things like the Trantor CD ROMS
 206  * will break because of this.
 207  * 
 208  * So, we need to slow things down, which isn't as simple as it 
 209  * seems.  We can't slow things down period, because then people
 210  * who don't recompile their kernels will shoot me for ruining 
 211  * their performance.  We need to do it on a case per case basis.
 212  *
 213  * The best for performance will be to, only for borken devices 
 214  * (this is stored on a per-target basis in the scsi_devices array)
 215  * 
 216  * Wait for a low->high transition before continuing with that 
 217  * transfer.  If we timeout, continue anyways.  We don't need 
 218  * a long timeout, because REQ should only be asserted until the 
 219  * corresponding ACK is recieved and processed.
 220  *
 221  * Note that we can't use the system timer for this, because of 
 222  * resolution, and we *really* can't use the timer chip since 
 223  * gettimeofday() and the beeper routines use that.  So,
 224  * the best thing for us to do will be to calibrate a timing
 225  * loop in the initialization code using the timer chip before
 226  * gettimeofday() can screw with it.
 227  */
 228 
 229 static int borken_calibration = 0;
 230 static void borken_init (void) {
     /* [previous][next][first][last][top][bottom][index][help] */
 231   register int count = 0, start = jiffies + 1, stop = start + 25;
 232 
 233   while (jiffies < start);
 234   for (;jiffies < stop; ++count);
 235 
 236 /* 
 237  * Ok, we now have a count for .25 seconds.  Convert to a 
 238  * count per second and divide by transer rate in K.
 239  */
 240 
 241   borken_calibration =  (count * 4) / (SLOW_RATE*1024);
 242 
 243   if (borken_calibration < 1)
 244         borken_calibration = 1;
 245 #if (DEBUG & DEBUG_BORKEN)
 246   printk("scsi%d : borken calibrated to %dK/sec, %d cycles per transfer\n", 
 247         hostno, BORKEN_RATE, borken_calibration);
 248 #endif
 249 }
 250 
 251 static inline void borken_wait(void) {
     /* [previous][next][first][last][top][bottom][index][help] */
 252   register int count;
 253   for (count = borken_calibration; count && (STATUS & STAT_REQ); 
 254         --count);
 255   if (count)
 256 #if (DEBUG & DEBUG_BORKEN) 
 257         printk("scsi%d : borken timeout\n", hostno);
 258 #else
 259         ;
 260 #endif 
 261 }
 262 
 263 #endif /* def SLOW_HANDSHAKE */
 264 
 265 int seagate_st0x_detect (int hostnum)
     /* [previous][next][first][last][top][bottom][index][help] */
 266         {
 267 #ifndef OVERRIDE
 268         int i,j;
 269 #endif 
 270 static struct sigaction seagate_sigaction = {
 271         &seagate_reconnect_intr,
 272         0,
 273         SA_INTERRUPT,
 274         NULL
 275 };
 276 
 277 /*
 278  *      First, we try for the manual override.
 279  */
 280 #ifdef DEBUG 
 281         printk("Autodetecting seagate ST0x\n");
 282 #endif
 283         
 284         if (hostno != -1)
 285                 {
 286                 printk ("ERROR : seagate_st0x_detect() called twice.\n");
 287                 return 0;
 288                 }
 289         
 290         base_address = NULL;
 291 #ifdef OVERRIDE
 292         base_address = (void *) OVERRIDE;
 293 
 294 /* CONTROLLER is used to override controller (SEAGATE or FD). PM: 07/01/93 */
 295 #ifdef CONTROLLER
 296         controller_type = CONTROLLER;
 297 #else
 298 #error Please use -DCONTROLLER=SEAGATE or -DCONTROLLER=FD to override controller type
 299 #endif /* CONTROLLER */
 300 #ifdef DEBUG
 301         printk("Base address overridden to %x, controller type is %s\n",
 302                 base_address,controller_type == SEAGATE ? "SEAGATE" : "FD");
 303 #endif 
 304 #else /* OVERIDE */     
 305 /*
 306  *      To detect this card, we simply look for the signature
 307  *      from the BIOS version notice in all the possible locations
 308  *      of the ROM's.  This has a nice sideeffect of not trashing
 309  *      any register locations that might be used by something else.
 310  *
 311  * XXX - note that we probably should be probing the address
 312  * space for the on-board RAM instead.
 313  */
 314 
 315         for (i = 0; i < (sizeof (seagate_bases) / sizeof (char  * )); ++i)
 316                 for (j = 0; !base_address && j < NUM_SIGNATURES; ++j)
 317                 if (!memcmp ((void *) (seagate_bases[i] +
 318                     signatures[j].offset), (void *) signatures[j].signature,
 319                     signatures[j].length)) {
 320                         base_address = (void *) seagate_bases[i];
 321                         controller_type = signatures[j].type;
 322                 }
 323 #endif /* OVERIDE */
 324  
 325         scsi_hosts[hostnum].this_id = (controller_type == SEAGATE) ? 7 : 6;
 326 
 327         if (base_address)
 328                 {
 329                 st0x_cr_sr =(void *) (((unsigned char *) base_address) + (controller_type == SEAGATE ? 0x1a00 : 0x1c00)); 
 330                 st0x_dr = (void *) (((unsigned char *) base_address ) + (controller_type == SEAGATE ? 0x1c00 : 0x1e00));
 331 #ifdef DEBUG
 332                 printk("ST0x detected. Base address = %x, cr = %x, dr = %x\n", base_address, st0x_cr_sr, st0x_dr);
 333 #endif
 334 /*
 335  *      At all times, we will use IRQ 5.  Should also check for IRQ3 if we 
 336  *      loose our first interrupt.
 337  */
 338                 hostno = hostnum;
 339                 if (irqaction(IRQ, &seagate_sigaction)) {
 340                         printk("scsi%d : unable to allocate IRQ%d\n",
 341                                 hostno, IRQ);
 342                         return 0;
 343                 }
 344 #ifdef SLOW_HANDSHAKE
 345                 borken_init();
 346 #endif
 347                 
 348                 return -1;
 349                 }
 350         else
 351                 {
 352 #ifdef DEBUG
 353                 printk("ST0x not detected.\n");
 354 #endif
 355                 return 0;
 356                 }
 357         }
 358          
 359 const char *seagate_st0x_info(void) {
     /* [previous][next][first][last][top][bottom][index][help] */
 360       static char buffer[256];
 361         sprintf(buffer, "scsi%d : %s at irq %d address %p options :"
 362 #ifdef ARBITRATE
 363 " ARBITRATE"
 364 #endif
 365 #ifdef SLOW_HANDSHAKE
 366 " SLOW_HANDSHAKE"
 367 #endif
 368 #ifdef FAST
 369 #ifdef FAST32
 370 " FAST32"
 371 #else
 372 " FAST"
 373 #endif
 374 #endif
 375  
 376 #ifdef LINKED
 377 " LINKED"
 378 #endif
 379               "\n", hostno, (controller_type == SEAGATE) ? "seagate" : 
 380               "FD TMC-950", IRQ, base_address);
 381         return buffer;
 382 }
 383 
 384 /*
 385  * These are our saved pointers for the outstanding command that is 
 386  * waiting for a reconnect
 387  */
 388 
 389 static unsigned char current_target, current_lun;
 390 static unsigned char *current_cmnd, *current_data;
 391 static int current_nobuffs;
 392 static struct scatterlist *current_buffer;
 393 static int current_bufflen;
 394 
 395 #ifdef LINKED
 396 
 397 /* 
 398  * linked_connected indicates weather or not we are currently connected to 
 399  * linked_target, linked_lun and in an INFORMATION TRANSFER phase,
 400  * using linked commands.
 401  */
 402 
 403 static int linked_connected = 0;
 404 static unsigned char linked_target, linked_lun;
 405 #endif
 406 
 407 
 408 static void (*done_fn)(Scsi_Cmnd *) = NULL;
 409 static Scsi_Cmnd * SCint = NULL;
 410 
 411 /*
 412  * These control whether or not disconnect / reconnect will be attempted,
 413  * or are being attempted.
 414  */
 415 
 416 #define NO_RECONNECT    0
 417 #define RECONNECT_NOW   1
 418 #define CAN_RECONNECT   2
 419 
 420 #ifdef LINKED
 421 
 422 /*
 423  * LINKED_RIGHT indicates that we are currently connected to the correct target
 424  * for this command, LINKED_WRONG indicates that we are connected to the wrong 
 425  * target.  Note that these imply CAN_RECONNECT.
 426  */
 427 
 428 #define LINKED_RIGHT    3
 429 #define LINKED_WRONG    4
 430 #endif
 431 
 432 /*
 433  * This determines if we are expecting to reconnect or not.
 434  */
 435 
 436 static int should_reconnect = 0;
 437 
 438 /*
 439  * The seagate_reconnect_intr routine is called when a target reselects the 
 440  * host adapter.  This occurs on the interrupt triggered by the target 
 441  * asserting SEL.
 442  */
 443 
 444 static void seagate_reconnect_intr (int unused)
     /* [previous][next][first][last][top][bottom][index][help] */
 445         {
 446         int temp;
 447         Scsi_Cmnd * SCtmp;
 448 
 449 /* enable all other interrupts. */      
 450         sti();
 451 #if (DEBUG & PHASE_RESELECT)
 452         printk("scsi%d : seagate_reconnect_intr() called\n", hostno);
 453 #endif
 454 
 455         if (!should_reconnect)
 456             printk("scsi%d: unexpected interrupt.\n", hostno);
 457         else {
 458                  should_reconnect = 0;
 459 
 460 #if (DEBUG & PHASE_RESELECT)
 461                 printk("scsi%d : internal_command("
 462                        "%d, %08x, %08x, %d, RECONNECT_NOW\n", hostno, 
 463                         current_target, current_data, current_bufflen);
 464 #endif
 465         
 466                 temp =  internal_command (current_target, current_lun,
 467                         current_cmnd, current_data, current_bufflen,
 468                         RECONNECT_NOW);
 469 
 470                 if (msg_byte(temp) != DISCONNECT) {
 471                         if (done_fn) {
 472 #if (DEBUG & PHASE_RESELECT)
 473                                 printk("scsi%d : done_fn(%d,%08x)", hostno, 
 474                                 hostno, temp);
 475 #endif
 476                                 if(!SCint) panic("SCint == NULL in seagate");
 477                                 SCtmp = SCint;
 478                                 SCint = NULL;
 479                                 SCtmp->result = temp;
 480                                 done_fn (SCtmp);
 481                         } else
 482                                 printk("done_fn() not defined.\n");
 483                         }
 484                 }
 485         } 
 486 
 487 /* 
 488  * The seagate_st0x_queue_command() function provides a queued interface
 489  * to the seagate SCSI driver.  Basically, it just passes control onto the
 490  * seagate_command() function, after fixing it so that the done_fn()
 491  * is set to the one passed to the function.  We have to be very careful,
 492  * because there are some commands on some devices that do not disconnect,
 493  * and if we simply call the done_fn when the command is done then another
 494  * command is started and queue_command is called again...  We end up
 495  * overflowing the kernel stack, and this tends not to be such a good idea.
 496  */
 497 
 498 static int recursion_depth = 0;
 499 
 500 int seagate_st0x_queue_command (Scsi_Cmnd * SCpnt,  void (*done)(Scsi_Cmnd *))
     /* [previous][next][first][last][top][bottom][index][help] */
 501         {
 502         int result, reconnect;
 503         Scsi_Cmnd * SCtmp;
 504 
 505         done_fn = done;
 506         current_target = SCpnt->target;
 507         current_lun = SCpnt->lun;
 508         (const void *) current_cmnd = SCpnt->cmnd;
 509         current_data = (unsigned char *) SCpnt->request_buffer;
 510         current_bufflen = SCpnt->request_bufflen;
 511         SCint = SCpnt;
 512         if(recursion_depth) {
 513           return 0;
 514         };
 515         recursion_depth++;
 516         do{
 517 #ifdef LINKED
 518 /*
 519  * Set linked command bit in control field of SCSI command.
 520  */
 521 
 522           current_cmnd[COMMAND_SIZE(current_cmnd[0])] |= 0x01;
 523           if (linked_connected) {
 524 #if (DEBUG & DEBUG_LINKED) 
 525             printk("scsi%d : using linked commands, current I_T_L nexus is ",
 526               hostno);
 527 #endif
 528             if ((linked_target == current_target) && 
 529               (linked_lun == current_lun)) {
 530 #if (DEBUG & DEBUG_LINKED) 
 531             printk("correct\n");
 532 #endif
 533               reconnect = LINKED_RIGHT;
 534             } else {
 535 #if (DEBUG & DEBUG_LINKED) 
 536             printk("incorrect\n");
 537 #endif
 538               reconnect = LINKED_WRONG;
 539             }
 540           } else 
 541 #endif /* LINKED */
 542             reconnect = CAN_RECONNECT;
 543 
 544 
 545 
 546 
 547 
 548           result = internal_command (SCint->target, SCint->lun, SCint->cmnd, SCint->request_buffer,
 549                                      SCint->request_bufflen, 
 550                                      reconnect);
 551           if (msg_byte(result) == DISCONNECT)  break;
 552           SCtmp = SCint;
 553           SCint = NULL;
 554           SCtmp->result = result;
 555           done_fn (SCtmp);
 556         } while(SCint);
 557         recursion_depth--;
 558         return 0;
 559       }
 560 
 561 int seagate_st0x_command (Scsi_Cmnd * SCpnt) {
     /* [previous][next][first][last][top][bottom][index][help] */
 562         return internal_command (SCpnt->target, SCpnt->lun, SCpnt->cmnd, SCpnt->request_buffer,
 563                                  SCpnt->request_bufflen, 
 564                                  (int) NO_RECONNECT);
 565 }
 566         
 567 static int internal_command(unsigned char target, unsigned char lun, const void *cmnd,
     /* [previous][next][first][last][top][bottom][index][help] */
 568                          void *buff, int bufflen, int reselect) {
 569         int len;
 570         unsigned char *data;    
 571         struct scatterlist *buffer;
 572         int nobuffs;
 573         int clock;                      
 574         int temp;
 575 #ifdef SLOW_HANDSHAKE
 576         int borken;     /* Does the current target require Very Slow I/O ? */
 577 #endif
 578 
 579 
 580 #if (DEBUG & PHASE_DATAIN) || (DEBUG & PHASE_DATOUT) 
 581         int transfered = 0;
 582 #endif
 583 
 584 #if (((DEBUG & PHASE_ETC) == PHASE_ETC) || (DEBUG & PRINT_COMMAND) || \
 585         (DEBUG & PHASE_EXIT))   
 586         int i;
 587 #endif
 588 
 589 #if ((DEBUG & PHASE_ETC) == PHASE_ETC)
 590         int phase=0, newphase;
 591 #endif
 592 
 593         int done = 0;
 594         unsigned char status = 0;       
 595         unsigned char message = 0;
 596         register unsigned char status_read;
 597 
 598         unsigned transfersize = 0, underflow = 0;
 599 
 600         incommand = 0;
 601         st0x_aborted = 0;
 602 
 603 #ifdef SLOW_HANDSHAKE
 604         borken = (int) scsi_devices[SCint->index].borken;
 605 #endif
 606 
 607 #if (DEBUG & PRINT_COMMAND)
 608         printk ("scsi%d : target = %d, command = ", hostno, target);
 609         print_command((unsigned char *) cmnd);
 610         printk("\n");
 611 #endif
 612 
 613 #if (DEBUG & PHASE_RESELECT)
 614         switch (reselect) {
 615         case RECONNECT_NOW :
 616                 printk("scsi%d : reconnecting\n", hostno);
 617                 break;
 618 #ifdef LINKED
 619         case LINKED_RIGHT : 
 620                 printk("scsi%d : connected, can reconnect\n", hostno);
 621                 break;
 622         case LINKED_WRONG :
 623                 printk("scsi%d : connected to wrong target, can reconnect\n",
 624                         hostno);
 625                 break;          
 626 #endif
 627         case CAN_RECONNECT :
 628                 printk("scsi%d : allowed to reconnect\n", hostno);
 629                 break;
 630         default :
 631                 printk("scsi%d : not allowed to reconnect\n", hostno);
 632         }
 633 #endif
 634         
 635 
 636         if (target == (controller_type == SEAGATE ? 7 : 6))
 637                 return DID_BAD_TARGET;
 638 
 639 /*
 640  *      We work it differently depending on if this is is "the first time,"
 641  *      or a reconnect.  If this is a reselct phase, then SEL will 
 642  *      be asserted, and we must skip selection / arbitration phases.
 643  */
 644 
 645         switch (reselect) {
 646         case RECONNECT_NOW:
 647 #if (DEBUG & PHASE_RESELECT)
 648                 printk("scsi%d : phase RESELECT \n", hostno);
 649 #endif
 650 
 651 /*
 652  *      At this point, we should find the logical or of our ID and the original
 653  *      target's ID on the BUS, with BSY, SEL, and I/O signals asserted.
 654  *
 655  *      After ARBITRATION phase is completed, only SEL, BSY, and the 
 656  *      target ID are asserted.  A valid initator ID is not on the bus
 657  *      until IO is asserted, so we must wait for that.
 658  */
 659                 
 660                 for (clock = jiffies + 10, temp = 0; (jiffies < clock) &&
 661                      !(STATUS & STAT_IO););
 662                 
 663                 if (jiffies >= clock)
 664                         {
 665 #if (DEBUG & PHASE_RESELECT)
 666                         printk("scsi%d : RESELECT timed out while waiting for IO .\n",
 667                                 hostno);
 668 #endif
 669                         return (DID_BAD_INTR << 16);
 670                         }
 671 
 672 /* 
 673  *      After I/O is asserted by the target, we can read our ID and its
 674  *      ID off of the BUS.
 675  */
 676  
 677                 if (!((temp = DATA) & (controller_type == SEAGATE ? 0x80 : 0x40)))
 678                         {
 679 #if (DEBUG & PHASE_RESELECT)
 680                         printk("scsi%d : detected reconnect request to different target.\n" 
 681                                "\tData bus = %d\n", hostno, temp);
 682 #endif
 683                         return (DID_BAD_INTR << 16);
 684                         }
 685 
 686                 if (!(temp & (1 << current_target)))
 687                         {
 688                         printk("scsi%d : Unexpected reselect interrupt.  Data bus = %d\n",
 689                                 hostno, temp);
 690                         return (DID_BAD_INTR << 16);
 691                         }
 692 
 693                 buffer=current_buffer;  
 694                 cmnd=current_cmnd;      /* WDE add */
 695                 data=current_data;      /* WDE add */
 696                 len=current_bufflen;    /* WDE add */
 697                 nobuffs=current_nobuffs;
 698 
 699 /*
 700  *      We have determined that we have been selected.  At this point, 
 701  *      we must respond to the reselection by asserting BSY ourselves
 702  */
 703 
 704 #if 1
 705                 CONTROL = (BASE_CMD | CMD_DRVR_ENABLE | CMD_BSY);
 706 #else
 707                 CONTROL = (BASE_CMD | CMD_BSY);
 708 #endif
 709 
 710 /*
 711  *      The target will drop SEL, and raise BSY, at which time we must drop
 712  *      BSY.
 713  */
 714 
 715                 for (clock = jiffies + 10; (jiffies < clock) &&  (STATUS & STAT_SEL););
 716 
 717                 if (jiffies >= clock)
 718                         { 
 719                         CONTROL = (BASE_CMD | CMD_INTR);
 720 #if (DEBUG & PHASE_RESELECT)
 721                         printk("scsi%d : RESELECT timed out while waiting for SEL.\n",
 722                                 hostno);
 723 #endif
 724                         return (DID_BAD_INTR << 16);                             
 725                         }
 726 
 727                 CONTROL = BASE_CMD;
 728 
 729 /*
 730  *      At this point, we have connected with the target and can get 
 731  *      on with our lives.
 732  */      
 733                 break;
 734         case CAN_RECONNECT:
 735 
 736 #ifdef LINKED
 737 /*
 738  * This is a bletcherous hack, just as bad as the Unix #! interpreter stuff.
 739  * If it turns out we are using the wrong I_T_L nexus, the easiest way to deal
 740  * with it is to go into our INFORMATION TRANSFER PHASE code, send a ABORT 
 741  * message on MESSAGE OUT phase, and then loop back to here.
 742  */
 743   
 744 connect_loop :
 745 
 746 #endif
 747 
 748 #if (DEBUG & PHASE_BUS_FREE)
 749                 printk ("scsi%d : phase = BUS FREE \n", hostno);
 750 #endif
 751 
 752 /*
 753  *      BUS FREE PHASE
 754  *
 755  *      On entry, we make sure that the BUS is in a BUS FREE
 756  *      phase, by insuring that both BSY and SEL are low for
 757  *      at least one bus settle delay.  Several reads help
 758  *      eliminate wire glitch.
 759  */
 760 
 761                 clock = jiffies + ST0X_BUS_FREE_DELAY;  
 762 
 763 #if !defined (ARBITRATE) 
 764                 while (((STATUS |  STATUS | STATUS) & 
 765                          (STAT_BSY | STAT_SEL)) && 
 766                          (!st0x_aborted) && (jiffies < clock));
 767 
 768                 if (jiffies > clock)
 769                         return retcode(DID_BUS_BUSY);
 770                 else if (st0x_aborted)
 771                         return retcode(st0x_aborted);
 772 #endif
 773 
 774 #if (DEBUG & PHASE_SELECTION)
 775                 printk("scsi%d : phase = SELECTION\n", hostno);
 776 #endif
 777 
 778                 clock = jiffies + ST0X_SELECTION_DELAY;
 779 
 780 /*
 781  * Arbitration/selection procedure : 
 782  * 1.  Disable drivers
 783  * 2.  Write HOST adapter address bit
 784  * 3.  Set start arbitration.
 785  * 4.  We get either ARBITRATION COMPLETE or SELECT at this
 786  *     point.
 787  * 5.  OR our ID and targets on bus.
 788  * 6.  Enable SCSI drivers and asserted SEL and ATTN
 789  */
 790                 
 791 #if defined(ARBITRATE)  
 792         cli();
 793         CONTROL = 0;
 794         DATA = (controller_type == SEAGATE) ? 0x80 : 0x40;
 795         CONTROL = CMD_START_ARB; 
 796         sti();
 797         while (!((status_read = STATUS) & (STAT_ARB_CMPL | STAT_SEL)) &&
 798                 (jiffies < clock) && !st0x_aborted);
 799 
 800         if (!(status_read & STAT_ARB_CMPL)) {
 801 #if (DEBUG & PHASE_SELECTION)
 802                 if (status_read & STAT_SEL) 
 803                         printk("scsi%d : arbitration lost\n", hostno);
 804                 else
 805                         printk("scsi%d : arbitration timeout.\n", hostno);
 806 #endif
 807                 CONTROL = BASE_CMD;
 808                 return retcode(DID_NO_CONNECT);
 809         };
 810 
 811 #if (DEBUG & PHASE_SELECTION)
 812         printk("scsi%d : arbitration complete\n", hostno);
 813 #endif
 814 #endif
 815 
 816 
 817 /*
 818  *      When the SCSI device decides that we're gawking at it, it will 
 819  *      respond by asserting BUSY on the bus.
 820  *
 821  *      Note : the Seagate ST-01/02 product manual says that we should 
 822  *      twiddle the DATA register before the control register.  However,
 823  *      this does not work reliably so we do it the other way arround.
 824  *
 825  *      Probably could be a problem with arbitration too, we really should
 826  *      try this with a SCSI protocol or logic analyzer to see what is 
 827  *      going on.
 828  */
 829         cli();
 830         DATA = (unsigned char) ((1 << target) | (controller_type == SEAGATE ? 0x80 : 0x40));
 831         CONTROL = BASE_CMD | CMD_DRVR_ENABLE | CMD_SEL | 
 832                 (reselect ? CMD_ATTN : 0);
 833         sti();
 834                 while (!((status_read = STATUS) & STAT_BSY) && 
 835                         (jiffies < clock) && !st0x_aborted)
 836 
 837 #if 0 && (DEBUG & PHASE_SELECTION)
 838                 {
 839                 temp = clock - jiffies;
 840 
 841                 if (!(jiffies % 5))
 842                         printk("seagate_st0x_timeout : %d            \r",temp);
 843         
 844                 }
 845                 printk("Done.                                             \n");
 846                 printk("scsi%d : status = %02x, seagate_st0x_timeout = %d, aborted = %02x \n", 
 847                         hostno, status_read, temp, st0x_aborted);
 848 #else
 849                 ;
 850 #endif
 851         
 852 
 853                 if ((jiffies >= clock)  && !(status_read & STAT_BSY))
 854                         {
 855 #if (DEBUG & PHASE_SELECTION)
 856                         printk ("scsi%d : NO CONNECT with target %d, status = %x \n", 
 857                                 hostno, target, STATUS);
 858 #endif
 859                         return retcode(DID_NO_CONNECT);
 860                         }
 861 
 862 /*
 863  *      If we have been aborted, and we have a command in progress, IE the 
 864  *      target still has BSY asserted, then we will reset the bus, and 
 865  *      notify the midlevel driver to expect sense.
 866  */
 867 
 868                 if (st0x_aborted) {
 869                         CONTROL = BASE_CMD;
 870                         if (STATUS & STAT_BSY) {
 871                                 printk("scsi%d : BST asserted after we've been aborted.\n",
 872                                         hostno);
 873                                 seagate_st0x_reset();
 874                                 return retcode(DID_RESET);
 875                         }
 876                         return retcode(st0x_aborted);
 877                 }       
 878 
 879 /* Establish current pointers.  Take into account scatter / gather */
 880 
 881         if ((nobuffs = SCint->use_sg)) {
 882 #if (DEBUG & DEBUG_SG)
 883         {
 884         int i;
 885         printk("scsi%d : scatter gather requested, using %d buffers.\n",
 886                 hostno, nobuffs);
 887         for (i = 0; i < nobuffs; ++i)
 888                 printk("scsi%d : buffer %d address = %08x length = %d\n",
 889                         hostno, i, buffer[i].address, buffer[i].length);
 890         }
 891 #endif
 892                 
 893                 buffer = (struct scatterlist *) SCint->buffer;
 894                 len = buffer->length;
 895                 data = (unsigned char *) buffer->address;
 896         } else {
 897 #if (DEBUG & DEBUG_SG)
 898         printk("scsi%d : scatter gather not requested.\n", hostno);
 899 #endif
 900                 buffer = NULL;
 901                 len = SCint->request_bufflen;
 902                 data = (unsigned char *) SCint->request_buffer;
 903         }
 904 
 905 #if (DEBUG & (PHASE_DATAIN | PHASE_DATAOUT))
 906         printk("scsi%d : len = %d\n", hostno, len);
 907 #endif
 908 
 909                 break;
 910 #ifdef LINKED
 911         case LINKED_RIGHT:
 912                 break;
 913         case LINKED_WRONG:
 914                 break;
 915 #endif
 916         }
 917 
 918 /*
 919  *      There are several conditions under which we wish to send a message : 
 920  *      1.  When we are allowing disconnect / reconnect, and need to establish
 921  *          the I_T_L nexus via an IDENTIFY with the DiscPriv bit set.
 922  *
 923  *      2.  When we are doing linked commands, are have the wrong I_T_L nexus
 924  *          established and want to send an ABORT message.
 925  */
 926 
 927         
 928         CONTROL = BASE_CMD | CMD_DRVR_ENABLE | 
 929                 (((reselect == CAN_RECONNECT)
 930 #ifdef LINKED 
 931                 || (reselect == LINKED_WRONG)
 932 #endif 
 933                 )  ? CMD_ATTN : 0) ;
 934         
 935 /*
 936  *      INFORMATION TRANSFER PHASE
 937  *
 938  *      The nasty looking read / write inline assembler loops we use for 
 939  *      DATAIN and DATAOUT phases are approximately 4-5 times as fast as 
 940  *      the 'C' versions - since we're moving 1024 bytes of data, this
 941  *      really adds up.
 942  */
 943 
 944 #if ((DEBUG & PHASE_ETC) == PHASE_ETC)
 945         printk("scsi%d : phase = INFORMATION TRANSFER\n", hostno);
 946 #endif  
 947 
 948         incommand = 1;
 949         transfersize = SCint->transfersize;
 950         underflow = SCint->underflow;
 951 
 952 
 953 /*
 954  *      Now, we poll the device for status information,
 955  *      and handle any requests it makes.  Note that since we are unsure of 
 956  *      how much data will be flowing across the system, etc and cannot 
 957  *      make reasonable timeouts, that we will instead have the midlevel
 958  *      driver handle any timeouts that occur in this phase.
 959  */
 960 
 961         while (((status_read = STATUS) & STAT_BSY) && !st0x_aborted && !done) 
 962                 {
 963 #ifdef PARITY
 964                 if (status_read & STAT_PARITY)
 965                         {
 966                         printk("scsi%d : got parity error\n", hostno);
 967                         st0x_aborted = DID_PARITY;
 968                         }       
 969 #endif
 970 
 971                 if (status_read & STAT_REQ)
 972                         {
 973 #if ((DEBUG & PHASE_ETC) == PHASE_ETC)
 974                         if ((newphase = (status_read & REQ_MASK)) != phase)
 975                                 {
 976                                 phase = newphase;
 977                                 switch (phase)
 978                                 {
 979                                 case REQ_DATAOUT: 
 980                                         printk("scsi%d : phase = DATA OUT\n",
 981                                                 hostno); 
 982                                         break;
 983                                 case REQ_DATAIN : 
 984                                         printk("scsi%d : phase = DATA IN\n",
 985                                                 hostno); 
 986                                         break;
 987                                 case REQ_CMDOUT : 
 988                                         printk("scsi%d : phase = COMMAND OUT\n",
 989                                                 hostno); 
 990                                         break;
 991                                 case REQ_STATIN :
 992                                          printk("scsi%d : phase = STATUS IN\n",
 993                                                 hostno); 
 994                                         break;
 995                                 case REQ_MSGOUT :
 996                                         printk("scsi%d : phase = MESSAGE OUT\n",
 997                                                 hostno); 
 998                                         break;
 999                                 case REQ_MSGIN :
1000                                         printk("scsi%d : phase = MESSAGE IN\n",
1001                                                 hostno);
1002                                         break;
1003                                 default : 
1004                                         printk("scsi%d : phase = UNKNOWN\n",
1005                                                 hostno); 
1006                                         st0x_aborted = DID_ERROR; 
1007                                 }       
1008                                 }
1009 #endif
1010                 switch (status_read & REQ_MASK)
1011                 {                       
1012                 case REQ_DATAOUT : 
1013 /*
1014  * If we are in fast mode, then we simply splat the data out
1015  * in word-sized chunks as fast as we can.
1016  */
1017 
1018 #ifdef FAST 
1019 if (!len) {
1020 #if 0 
1021         printk("scsi%d: underflow to target %d lun %d \n", 
1022                 hostno, target, lun);
1023         st0x_aborted = DID_ERROR;
1024         fast = 0;
1025 #endif
1026         break;
1027 }
1028 
1029 if (fast && transfersize && !(len % transfersize) && (len >= transfersize)
1030 #ifdef FAST32
1031         && !(transfersize % 4)
1032 #endif
1033         ) {
1034 #if (DEBUG & DEBUG_FAST) 
1035         printk("scsi%d : FAST transfer, underflow = %d, transfersize = %d\n"
1036                "         len = %d, data = %08x\n", hostno, SCint->underflow, 
1037                SCint->transfersize, len, data);
1038 #endif
1039 
1040         __asm__("
1041         cld;
1042 "
1043 #ifdef FAST32
1044 "       shr $2, %%ecx;
1045 1:      lodsl;
1046         movl %%eax, (%%edi);
1047 "
1048 #else
1049 "1:     lodsb;
1050         movb %%al, (%%edi);
1051 "
1052 #endif
1053 "       loop 1b;" : :
1054         /* input */
1055         "D" (st0x_dr), "S" (data), "c" (SCint->transfersize) :
1056         /* clobbered */
1057         "eax", "ecx", "esi" );
1058 
1059         len -= transfersize;
1060         data += transfersize;
1061 
1062 #if (DEBUG & DEBUG_FAST)
1063         printk("scsi%d : FAST transfer complete len = %d data = %08x\n", 
1064                 hostno, len, data);
1065 #endif
1066 
1067 
1068 } else 
1069 #endif
1070 
1071 {
1072 /*
1073  *      We loop as long as we are in a data out phase, there is data to send, 
1074  *      and BSY is still active.
1075  */
1076                 __asm__ (
1077 
1078 /*
1079         Local variables : 
1080         len = ecx
1081         data = esi
1082         st0x_cr_sr = ebx
1083         st0x_dr =  edi
1084 
1085         Test for any data here at all.
1086 */
1087         "\torl %%ecx, %%ecx
1088         jz 2f
1089 
1090         cld
1091 
1092         movl _st0x_cr_sr, %%ebx
1093         movl _st0x_dr, %%edi
1094         
1095 1:      movb (%%ebx), %%al\n"
1096 /*
1097         Test for BSY
1098 */
1099 
1100         "\ttest $1, %%al
1101         jz 2f\n"
1102 
1103 /*
1104         Test for data out phase - STATUS & REQ_MASK should be REQ_DATAOUT, which is 0.
1105 */
1106         "\ttest $0xe, %%al
1107         jnz 2f  \n"
1108 /*
1109         Test for REQ
1110 */      
1111         "\ttest $0x10, %%al
1112         jz 1b
1113         lodsb
1114         movb %%al, (%%edi) 
1115         loop 1b
1116 
1117 2: 
1118                                                                         ":
1119 /* output */
1120 "=S" (data), "=c" (len) :
1121 /* input */
1122 "0" (data), "1" (len) :
1123 /* clobbered */
1124 "eax", "ebx", "edi"); 
1125 }
1126 
1127                         if (!len && nobuffs) {
1128                                 --nobuffs;
1129                                 ++buffer;
1130                                 len = buffer->length;
1131                                 data = (unsigned char *) buffer->address;
1132 #if (DEBUG & DEBUG_SG)
1133         printk("scsi%d : next scatter-gather buffer len = %d address = %08x\n",
1134                 hostno, len, data);
1135 #endif
1136                         }
1137                         break;
1138 
1139                 case REQ_DATAIN : 
1140 #ifdef SLOW_HANDSHAKE
1141         if (borken) {
1142 #if (DEBUG & (PHASE_DATAIN))
1143                 transfered += len;
1144 #endif
1145                 for (; len && (STATUS & (REQ_MASK | STAT_REQ)) == (REQ_DATAIN |
1146                         STAT_REQ); --len) {
1147                                 *data++ = DATA;
1148                                 borken_wait();
1149 }
1150 #if (DEBUG & (PHASE_DATAIN))
1151                 transfered -= len;
1152 #endif
1153         } else
1154 #endif
1155 #ifdef FAST
1156 if (fast && transfersize && !(len % transfersize) && (len >= transfersize)
1157 #ifdef FAST32
1158         && !(transfersize % 4)
1159 #endif
1160         ) {
1161 #if (DEBUG & DEBUG_FAST) 
1162         printk("scsi%d : FAST transfer, underflow = %d, transfersize = %d\n"
1163                "         len = %d, data = %08x\n", hostno, SCint->underflow, 
1164                SCint->transfersize, len, data);
1165 #endif
1166         __asm__("
1167         cld;
1168 "
1169 #ifdef FAST32
1170 "       shr $2, %%ecx;
1171 1:      movl (%%esi), %%eax;
1172         stosl;
1173 "
1174 #else
1175 "1:     movb (%%esi), %%al;
1176         stosb;
1177 "
1178 #endif
1179 
1180 "       loop 1b;" : :
1181         /* input */
1182         "S" (st0x_dr), "D" (data), "c" (SCint->transfersize) :
1183         /* clobbered */
1184         "eax", "ecx", "edi");
1185 
1186         len -= transfersize;
1187         data += transfersize;
1188 
1189 #if (DEBUG & PHASE_DATAIN)
1190         printk("scsi%d: transfered += %d\n", hostno, transfersize);
1191         transfered += transfersize;
1192 #endif
1193 
1194 #if (DEBUG & DEBUG_FAST)
1195         printk("scsi%d : FAST transfer complete len = %d data = %08x\n", 
1196                 hostno, len, data);
1197 #endif
1198 
1199 } else
1200 #endif
1201 {
1202 
1203 #if (DEBUG & PHASE_DATAIN)
1204         printk("scsi%d: transfered += %d\n", hostno, len);
1205         transfered += len;      /* Assume we'll transfer it all, then
1206                                    subtract what we *didn't* transfer */
1207 #endif
1208         
1209 /*
1210  *      We loop as long as we are in a data in phase, there is room to read, 
1211  *      and BSY is still active
1212  */
1213  
1214                         __asm__ (
1215 /*
1216         Local variables : 
1217         ecx = len
1218         edi = data
1219         esi = st0x_cr_sr
1220         ebx = st0x_dr
1221 
1222         Test for room to read
1223 */
1224         "\torl %%ecx, %%ecx
1225         jz 2f
1226 
1227         cld
1228         movl _st0x_cr_sr, %%esi
1229         movl _st0x_dr, %%ebx
1230 
1231 1:      movb (%%esi), %%al\n"
1232 /*
1233         Test for BSY
1234 */
1235 
1236         "\ttest $1, %%al 
1237         jz 2f\n"
1238 
1239 /*
1240         Test for data in phase - STATUS & REQ_MASK should be REQ_DATAIN, = STAT_IO, which is 4.
1241 */
1242         "\tmovb $0xe, %%ah      
1243         andb %%al, %%ah
1244         cmpb $0x04, %%ah
1245         jne 2f\n"
1246                 
1247 /*
1248         Test for REQ
1249 */      
1250         "\ttest $0x10, %%al
1251         jz 1b
1252 
1253         movb (%%ebx), %%al      
1254         stosb   
1255         loop 1b\n"
1256 
1257 "2:\n"
1258                                                                         :
1259 /* output */
1260 "=D" (data), "=c" (len) :
1261 /* input */
1262 "0" (data), "1" (len) :
1263 /* clobbered */
1264 "eax","ebx", "esi"); 
1265 
1266 #if (DEBUG & PHASE_DATAIN)
1267         printk("scsi%d: transfered -= %d\n", hostno, len);
1268         transfered -= len;              /* Since we assumed all of Len got 
1269                                          * transfered, correct our mistake */
1270 #endif
1271 }
1272         
1273                         if (!len && nobuffs) {
1274                                 --nobuffs;
1275                                 ++buffer;
1276                                 len = buffer->length;
1277                                 data = (unsigned char *) buffer->address;
1278 #if (DEBUG & DEBUG_SG)
1279         printk("scsi%d : next scatter-gather buffer len = %d address = %08x\n",
1280                 hostno, len, data);
1281 #endif
1282                         }
1283 
1284                         break;
1285 
1286                 case REQ_CMDOUT : 
1287                         while (((status_read = STATUS) & STAT_BSY) && 
1288                                ((status_read & REQ_MASK) == REQ_CMDOUT))
1289                                 if (status_read & STAT_REQ) {
1290                                         DATA = *(unsigned char *) cmnd;
1291                                         cmnd = 1+(unsigned char *) cmnd;
1292 #ifdef SLOW_HANDSHAKE
1293                                         if (borken) 
1294                                                 borken_wait();
1295 #endif
1296                                 }
1297                         break;
1298         
1299                 case REQ_STATIN : 
1300                         status = DATA;
1301                         break;
1302                                 
1303                 case REQ_MSGOUT : 
1304 /*
1305  *      We can only have sent a MSG OUT if we requested to do this 
1306  *      by raising ATTN.  So, we must drop ATTN.
1307  */
1308 
1309                         CONTROL = BASE_CMD | CMD_DRVR_ENABLE;
1310 /*
1311  *      If we are reconecting, then we must send an IDENTIFY message in 
1312  *       response  to MSGOUT.
1313  */
1314                         switch (reselect) {
1315                         case CAN_RECONNECT:
1316                                 DATA = IDENTIFY(1, lun);
1317 
1318 #if (DEBUG & (PHASE_RESELECT | PHASE_MSGOUT)) 
1319                                 printk("scsi%d : sent IDENTIFY message.\n", hostno);
1320 #endif
1321                                 break;
1322 #ifdef LINKED
1323                         case LINKED_WRONG:
1324                                 DATA = ABORT;
1325                                 linked_connected = 0;
1326                                 reselect = CAN_RECONNECT;
1327                                 goto connect_loop;
1328 #if (DEBUG & (PHASE_MSGOUT | DEBUG_LINKED))
1329                                 printk("scsi%d : sent ABORT message to cancle incorrect I_T_L nexus.\n", hostno);
1330 #endif
1331 #endif /* LINKED */
1332 #if (DEBUG & DEBUG_LINKED) 
1333             printk("correct\n");
1334 #endif
1335                         default:
1336                                 DATA = NOP;
1337                                 printk("scsi%d : target %d requested MSGOUT, sent NOP message.\n", hostno, target);
1338                         }
1339                         break;
1340                                         
1341                 case REQ_MSGIN : 
1342                         switch (message = DATA) {
1343                         case DISCONNECT :
1344                                 should_reconnect = 1;
1345                                 current_data = data;    /* WDE add */
1346                                 current_buffer = buffer;
1347                                 current_bufflen = len;  /* WDE add */
1348                                 current_nobuffs = nobuffs;
1349 #ifdef LINKED
1350                                 linked_connected = 0;
1351 #endif
1352                                 done=1;
1353 #if (DEBUG & (PHASE_RESELECT | PHASE_MSGIN))
1354                                 printk("scsi%d : disconnected.\n", hostno);
1355 #endif
1356                                 break;
1357 
1358 #ifdef LINKED
1359                         case LINKED_CMD_COMPLETE:
1360                         case LINKED_FLG_CMD_COMPLETE:
1361 #endif
1362                         case COMMAND_COMPLETE :
1363 /*
1364  * Note : we should check for underflow here.   
1365  */
1366 #if (DEBUG & PHASE_MSGIN)       
1367                                 printk("scsi%d : command complete.\n", hostno);
1368 #endif
1369                                 done = 1;
1370                                 break;
1371                         case ABORT :
1372 #if (DEBUG & PHASE_MSGIN)
1373                                 printk("scsi%d : abort message.\n", hostno);
1374 #endif
1375                                 done=1;
1376                                 break;
1377                         case SAVE_POINTERS :
1378                                 current_buffer = buffer;
1379                                 current_bufflen = len;  /* WDE add */
1380                                 current_data = data;    /* WDE mod */
1381                                 current_nobuffs = nobuffs;
1382 #if (DEBUG & PHASE_MSGIN)
1383                                 printk("scsi%d : pointers saved.\n", hostno);
1384 #endif 
1385                                 break;
1386                         case RESTORE_POINTERS:
1387                                 buffer=current_buffer;
1388                                 cmnd=current_cmnd;
1389                                 data=current_data;      /* WDE mod */
1390                                 len=current_bufflen;
1391                                 nobuffs=current_nobuffs;
1392 #if (DEBUG & PHASE_MSGIN)
1393                                 printk("scsi%d : pointers restored.\n", hostno);
1394 #endif
1395                                 break;
1396                         default:
1397 
1398 /*
1399  *      IDENTIFY distinguishes itself from the other messages by setting the
1400  *      high byte.
1401  *      
1402  *      Note : we need to handle at least one outstanding command per LUN,
1403  *      and need to hash the SCSI command for that I_T_L nexus based on the 
1404  *      known ID (at this point) and LUN.
1405  */
1406 
1407                                 if (message & 0x80) {
1408 #if (DEBUG & PHASE_MSGIN)
1409                                         printk("scsi%d : IDENTIFY message received from id %d, lun %d.\n",
1410                                                 hostno, target, message & 7);
1411 #endif
1412                                 } else {
1413 
1414 /*
1415  *      We should go into a MESSAGE OUT phase, and send  a MESSAGE_REJECT 
1416  *      if we run into a message that we don't like.  The seagate driver 
1417  *      needs some serious restructuring first though.
1418  */
1419 
1420 #if (DEBUG & PHASE_MSGIN)
1421                                         printk("scsi%d : unknown message %d from target %d.\n",
1422                                                 hostno,  message,   target);
1423 #endif  
1424                                 }
1425                         }
1426                         break;
1427 
1428                 default : 
1429                         printk("scsi%d : unknown phase.\n", hostno); 
1430                         st0x_aborted = DID_ERROR; 
1431                 }       
1432 
1433 #ifdef SLOW_HANDSHAKE
1434 /*
1435  * I really don't care to deal with borken devices in each single 
1436  * byte transfer case (ie, message in, message out, status), so
1437  * I'll do the wait here if necessary.
1438  */
1439                 if (borken)
1440                         borken_wait();
1441 #endif
1442  
1443                 } /* if ends */
1444                 } /* while ends */
1445 
1446 #if (DEBUG & (PHASE_DATAIN | PHASE_DATAOUT | PHASE_EXIT))
1447         printk("scsi%d : Transfered %d bytes\n", hostno, transfered);
1448 #endif
1449 
1450 #if (DEBUG & PHASE_EXIT)
1451 #if 0           /* Doesn't work for scatter / gather */
1452         printk("Buffer : \n");
1453         for (i = 0; i < 20; ++i) 
1454                 printk ("%02x  ", ((unsigned char *) data)[i]); /* WDE mod */
1455         printk("\n");
1456 #endif
1457         printk("scsi%d : status = ", hostno);
1458         print_status(status);
1459         printk("message = %02x\n", message);
1460 #endif
1461 
1462 
1463 /* We shouldn't reach this until *after* BSY has been deasserted */
1464 #ifdef notyet
1465         if (st0x_aborted) {
1466                 if (STATUS & STAT_BSY) {        
1467                         seagate_st0x_reset();
1468                         st0x_aborted = DID_RESET;
1469                 } 
1470                 abort_confirm = 1;
1471         } 
1472 #endif
1473 
1474 #ifdef LINKED
1475 else {
1476 /*
1477  * Fix the message byte so that unsuspecting high level drivers don't 
1478  * puke when they see a LINKED COMMAND message in place of the COMMAND 
1479  * COMPLETE they may be expecting.  Shouldn't be necessary, but it's 
1480  * better to be on the safe side. 
1481  *
1482  * A non LINKED* message byte will indicate that the command completed, 
1483  * and we are now disconnected.
1484  */
1485 
1486                 switch (message) {
1487                 case LINKED_CMD_COMPLETE :
1488                 case LINKED_FLG_CMD_COMPLETE : 
1489                         message = COMMAND_COMPLETE;
1490                         linked_target = current_target;
1491                         linked_lun = current_lun;
1492                         linked_connected = 1;
1493 #if (DEBUG & DEBUG_LINKED)
1494                         printk("scsi%d : keeping I_T_L nexus established for linked command.\n", 
1495                                 hostno);
1496 #endif
1497 /*
1498  * We also will need to adjust status to accomodate intermediate conditions.
1499  */
1500                         if ((status == INTERMEDIATE_GOOD) ||
1501                                 (status == INTERMEDIATE_C_GOOD))
1502                                 status = GOOD;
1503                         
1504                         break;
1505 /*
1506  * We should also handle what are "normal" termination messages 
1507  * here (ABORT, BUS_DEVICE_RESET?, and COMMAND_COMPLETE individually, 
1508  * and flake if things aren't right.
1509  */
1510 
1511                 default :
1512 #if (DEBUG & DEBUG_LINKED)
1513                         printk("scsi%d : closing I_T_L nexus.\n", hostno);
1514 #endif
1515                         linked_connected = 0;
1516                 }
1517         }
1518 #endif /* LINKED */
1519 
1520 
1521 
1522 
1523         if (should_reconnect) {
1524 #if (DEBUG & PHASE_RESELECT)
1525                 printk("scsi%d : exiting seagate_st0x_queue_command() with reconnect enabled.\n",
1526                         hostno);
1527 #endif
1528                 CONTROL = BASE_CMD | CMD_INTR ;
1529         } else 
1530                 CONTROL = BASE_CMD;
1531 
1532         return retcode (st0x_aborted);
1533         }
1534 
1535 int seagate_st0x_abort (Scsi_Cmnd * SCpnt, int code)
     /* [previous][next][first][last][top][bottom][index][help] */
1536         {
1537         if (code)
1538                 st0x_aborted = code;
1539         else
1540                 st0x_aborted = DID_ABORT;
1541 
1542                 return 0;
1543         }
1544 
1545 /*
1546         the seagate_st0x_reset function resets the SCSI bus
1547 */
1548         
1549 int seagate_st0x_reset (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1550         {
1551         unsigned clock;
1552         /*
1553                 No timeouts - this command is going to fail because 
1554                 it was reset.
1555         */
1556 
1557 #ifdef DEBUG
1558         printk("In seagate_st0x_reset()\n");
1559 #endif
1560 
1561 
1562         /* assert  RESET signal on SCSI bus.  */
1563                 
1564         CONTROL = BASE_CMD  | CMD_RST;
1565         clock=jiffies+2;
1566 
1567         
1568         /* Wait.  */
1569         
1570         while (jiffies < clock);
1571 
1572         CONTROL = BASE_CMD;
1573         
1574         st0x_aborted = DID_RESET;
1575 
1576 #ifdef DEBUG
1577         printk("SCSI bus reset.\n");
1578 #endif
1579         return 0;
1580         }
1581 
1582 #ifdef CONFIG_BLK_DEV_SD
1583 
1584 #include <asm/segment.h>
1585 #include "sd.h"
1586 #include "scsi_ioctl.h"
1587 
1588 int seagate_st0x_biosparam(int size, int dev, int* ip) {
     /* [previous][next][first][last][top][bottom][index][help] */
1589   unsigned char buf[256 + sizeof(int) * 2], cmd[6], *data, *page;
1590   int *sizes, result, formatted_sectors, total_sectors;
1591   int cylinders, heads, sectors;
1592 
1593   Scsi_Device *disk;
1594 
1595   disk = rscsi_disks[MINOR(dev) >> 4].device;
1596 
1597 /*
1598  * Only SCSI-I CCS drives and later implement the necessary mode sense 
1599  * pages.  
1600  */
1601 
1602   if (disk->scsi_level < 2) 
1603         return -1;
1604 
1605   sizes = (int *) buf;
1606   data = (unsigned char *) (sizes + 2);
1607 
1608   cmd[0] = MODE_SENSE;
1609   cmd[1] = (disk->lun << 5) & 0xe5;
1610   cmd[2] = 0x04; /* Read page 4, rigid disk geometry page current values */
1611   cmd[3] = 0;
1612   cmd[4] = 255;
1613   cmd[5] = 0;
1614 
1615 /*
1616  * We are transfering 0 bytes in the out direction, and expect to get back
1617  * 24 bytes for each mode page.
1618  */
1619 
1620   sizes[0] = 0;
1621   sizes[1] = 256;
1622 
1623   memcpy (data, cmd, 6);
1624 
1625   if (!(result = kernel_scsi_ioctl (disk, SCSI_IOCTL_SEND_COMMAND, (void *) buf))) {
1626 /*
1627  * The mode page lies beyond the MODE SENSE header, with length 4, and 
1628  * the BLOCK DESCRIPTOR, with length header[3].
1629  */
1630 
1631     page = data + 4 + data[3];
1632     heads = (int) page[5];
1633     cylinders = (page[2] << 16) | (page[3] << 8) | page[4];
1634 
1635     cmd[2] = 0x03; /* Read page 3, format page current values */
1636     memcpy (data, cmd, 6);
1637 
1638     if (!(result = kernel_scsi_ioctl (disk, SCSI_IOCTL_SEND_COMMAND, (void *) buf))) {
1639       page = data + 4 + data[3];
1640       sectors = (page[10] << 8) | page[11];     
1641 
1642         
1643 /*
1644  * Get the total number of formatted sectors from the block descriptor, 
1645  * so we can tell how many are being used for alternates.  
1646  */
1647 
1648       formatted_sectors = (data[4 + 1] << 16) | (data[4 + 2] << 8) |
1649         data[4 + 3] ;
1650 
1651       total_sectors = (heads * cylinders * sectors);
1652 
1653 /*
1654  * Adjust the real geometry by subtracting 
1655  * (spare sectors / (heads * tracks)) cylinders from the number of cylinders.
1656  *
1657  * It appears that the CE cylinder CAN be a partial cylinder.
1658  */
1659 
1660      
1661 printk("scsi%d : heads = %d cylinders = %d sectors = %d total = %d formatted = %d\n",
1662     hostno, heads, cylinders, sectors, total_sectors, formatted_sectors);
1663 
1664       if (!heads || !sectors)
1665         cylinders = 1025;
1666       else
1667         cylinders -= ((total_sectors - formatted_sectors) / (heads * sectors));
1668 
1669 /*
1670  * Now, we need to do a sanity check on the geometry to see if it is 
1671  * BIOS compatable.  The maximum BIOS geometry is 1024 cylinders * 
1672  * 256 heads * 64 sectors. 
1673  */
1674 
1675       if ((cylinders > 1024) || (sectors > 64)) 
1676         result = -1;
1677       else {
1678         ip[0] = heads;
1679         ip[1] = sectors;
1680         ip[2] = cylinders;
1681       }
1682 
1683 /* 
1684  * There should be an alternate mapping for things the seagate doesn't
1685  * understand, but I couldn't say what it is with reasonable certainty.
1686  */
1687 
1688       }
1689     }
1690     
1691   return result;
1692 }
1693 #endif /* CONFIG_BLK_DEV_SD */
1694 
1695 #endif  /* defined(CONFIG_SCSI_SEGATE) */
1696 

/* [previous][next][first][last][top][bottom][index][help] */