seagate.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:47k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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