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