home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / KERNEL-S / V1.2 / LINUX-1.2 / LINUX-1 / linux / drivers / scsi / seagate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-23  |  41.2 KB  |  1,699 lines

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