AM53C974.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:89k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. #include <linux/module.h>
  2. #include <linux/delay.h>
  3. #include <linux/signal.h>
  4. #include <linux/sched.h>
  5. #include <linux/errno.h>
  6. #include <linux/pci.h>
  7. #include <linux/string.h>
  8. #include <linux/blk.h>
  9. #include <linux/init.h>
  10. #include <linux/spinlock.h>
  11. #include <asm/io.h>
  12. #include <asm/system.h>
  13. #include "scsi.h"
  14. #include "hosts.h"
  15. #include "AM53C974.h"
  16. #include "constants.h"
  17. #include "sd.h"
  18. /* AM53/79C974 (PCscsi) driver release 0.5
  19.  * The architecture and much of the code of this device
  20.  * driver was originally developed by Drew Eckhardt for
  21.  * the NCR5380. The following copyrights apply:
  22.  *  For the architecture and all pieces of code which can also be found 
  23.  *    in the NCR5380 device driver:
  24.  *   Copyright 1993, Drew Eckhardt
  25.  *      Visionary Computing 
  26.  *      (Unix and Linux consulting and custom programming)
  27.  *      drew@colorado.edu
  28.  *      +1 (303) 666-5836
  29.  *
  30.  *  The AM53C974_nobios_detect code was originally developed by
  31.  *   Robin Cutshaw (robin@xfree86.org) and is used here in a 
  32.  *   slightly modified form.
  33.  *
  34.  *  PCI detection rewritten by Martin Mares <mj@atrey.karlin.mff.cuni.cz>
  35.  *
  36.  *  For the remaining code:
  37.  *    Copyright 1994, D. Frieauff
  38.  *    EMail: fri@rsx42sun0.dofn.de
  39.  *    Phone: x49-7545-8-2256 , x49-7541-42305
  40.  */
  41. /*
  42.  * $Log: AM53C974.c,v $
  43.  */
  44. #ifdef AM53C974_DEBUG
  45. #define DEB(x) x
  46. #ifdef AM53C974_DEBUG_KEYWAIT
  47. #define KEYWAIT() AM53C974_keywait()
  48. #else
  49. #define KEYWAIT()
  50. #endif
  51. #ifdef AM53C974_DEBUG_INIT
  52. #define DEB_INIT(x) x
  53. #else
  54. #define DEB_INIT(x)
  55. #endif
  56. #ifdef AM53C974_DEBUG_MSG
  57. #define DEB_MSG(x) x
  58. #else
  59. #define DEB_MSG(x)
  60. #endif
  61. #ifdef AM53C974_DEB_RESEL
  62. #define DEB_RESEL(x) x
  63. #else
  64. #define DEB_RESEL(x)
  65. #endif
  66. #ifdef AM53C974_DEBUG_QUEUE
  67. #define DEB_QUEUE(x) x
  68. #define LIST(x,y) {printk("LINE:%d   Adding %p to %pn", __LINE__, (void*)(x), (void*)(y)); if ((x)==(y)) udelay(5); }
  69. #define REMOVE(w,x,y,z) {printk("LINE:%d   Removing: %p->%p  %p->%p n", __LINE__, (void*)(w), (void*)(x), (void*)(y), (void*)(z)); if ((x)==(y)) udelay(5); }
  70. #else
  71. #define DEB_QUEUE(x)
  72. #define LIST(x,y)
  73. #define REMOVE(w,x,y,z)
  74. #endif
  75. #ifdef AM53C974_DEBUG_INFO
  76. #define DEB_INFO(x) x
  77. #else
  78. #define DEB_INFO(x)
  79. #endif
  80. #ifdef AM53C974_DEBUG_LINKED
  81. #define DEB_LINKED(x) x
  82. #else
  83. #define DEB_LINKED(x)
  84. #endif
  85. #ifdef AM53C974_DEBUG_INTR
  86. #define DEB_INTR(x) x
  87. #else
  88. #define DEB_INTR(x)
  89. #endif
  90. #else
  91. #define DEB_INIT(x)
  92. #define DEB(x)
  93. #define DEB_QUEUE(x)
  94. #define LIST(x,y)
  95. #define REMOVE(w,x,y,z)
  96. #define DEB_INFO(x)
  97. #define DEB_LINKED(x)
  98. #define DEB_INTR(x)
  99. #define DEB_MSG(x)
  100. #define DEB_RESEL(x)
  101. #define KEYWAIT()
  102. #endif
  103. #ifdef AM53C974_DEBUG_ABORT
  104. #define DEB_ABORT(x) x
  105. #else
  106. #define DEB_ABORT(x)
  107. #endif
  108. #ifdef VERBOSE_AM53C974_DEBUG
  109. #define VDEB(x) x
  110. #else
  111. #define VDEB(x)
  112. #endif
  113. #define INSIDE(x,l,h) ( ((x) >= (l)) && ((x) <= (h)) )
  114. #include <scsi/scsicam.h>
  115. /***************************************************************************************
  116. * Default setting of the controller's SCSI id. Edit and uncomment this only if your    *
  117. * BIOS does not correctly initialize the controller's SCSI id.                         *
  118. * If you don't get a warning during boot, it is correctly initialized.                 *
  119. ****************************************************************************************/
  120. /* #define AM53C974_SCSI_ID 7 */
  121. /***************************************************************************************
  122. * Default settings for sync. negotiation enable, transfer rate and sync. offset.       *
  123. * These settings can be replaced by LILO overrides (append) with the following syntax:          *
  124. * AM53C974=host-scsi-id, target-scsi-id, max-rate, max-offset                          *
  125. * Sync. negotiation is disabled by default and will be enabled for those targets which *
  126. * are specified in the LILO override                                                   *
  127. ****************************************************************************************/
  128. #define DEFAULT_SYNC_NEGOTIATION_ENABLED 0 /* 0 or 1 */
  129. #define DEFAULT_RATE  5 /* MHz, min: 3; max: 10 */
  130. #define DEFAULT_SYNC_OFFSET  0 /* bytes, min: 0; max: 15; use 0 for async. mode */
  131. /***************************************************************************************
  132. * If defined, don't allow targets to disconnect during commands.  This will reduce     *
  133. * performance, but may be worthwhile if you suspect the driver of corrupting data when *
  134. * a disconnect happens.                                                                *
  135. ***************************************************************************************/
  136. #define AM53C974_PROHIBIT_DISCONNECT
  137. /* --------------------- don't edit below here  --------------------- */
  138. #define AM53C974_DRIVER_REVISION_MAJOR 0
  139. #define AM53C974_DRIVER_REVISION_MINOR 5
  140. #define SEPARATOR_LINE  
  141. "--------------------------------------------------------------------------n"
  142. /* debug control */
  143. /* #define AM53C974_DEBUG */
  144. /* #define AM53C974_DEBUG_MSG */
  145. /* #define AM53C974_DEBUG_KEYWAIT */
  146. /* #define AM53C974_DEBUG_INIT */
  147. /* #define AM53C974_DEBUG_QUEUE */
  148. /* #define AM53C974_DEBUG_INFO */
  149. /* #define AM53C974_DEBUG_LINKED */
  150. /* #define VERBOSE_AM53C974_DEBUG */
  151. /* #define AM53C974_DEBUG_INTR */
  152. /* #define AM53C974_DEB_RESEL */
  153. #define AM53C974_DEBUG_ABORT
  154. /* #define AM53C974_OPTION_DEBUG_PROBE_ONLY */
  155. /* special options/constants */
  156. #define DEF_CLK                 40 /* chip clock freq. in MHz */
  157. #define MIN_PERIOD               4 /* for negotiation: min. number of clocks per cycle */
  158. #define MAX_PERIOD              13 /* for negotiation: max. number of clocks per cycle */
  159. #define MAX_OFFSET              15 /* for negotiation: max. offset (0=async) */
  160. #define DEF_SCSI_TIMEOUT        245 /* STIMREG value, 40 Mhz */
  161. #define DEF_STP                 8 /* STPREG value assuming 5.0 MB/sec, FASTCLK, FASTSCSI */
  162. #define DEF_SOF_RAD             0 /* REQ/ACK deassertion delay */
  163. #define DEF_SOF_RAA             0 /* REQ/ACK assertion delay */
  164. #define DEF_ETM                 0 /* CNTLREG1, ext. timing mode */
  165. #define DEF_PERE                1 /* CNTLREG1, parity error reporting */
  166. #define DEF_CLKF                0 /* CLKFREG,  0=40 Mhz */
  167. #define DEF_ENF                 1 /* CNTLREG2, enable features */
  168. #define DEF_ADIDCHK             0 /* CNTLREG3, additional ID check */
  169. #define DEF_FASTSCSI            1 /* CNTLREG3, fast SCSI */
  170. #define DEF_FASTCLK             1 /* CNTLREG3, fast clocking, 5 MB/sec at 40MHz chip clk */
  171. #define DEF_GLITCH              1 /* CNTLREG4, glitch eater, 0=12ns, 1=35ns, 2=25ns, 3=off */
  172. #define DEF_PWD                 0 /* CNTLREG4, reduced power feature */
  173. #define DEF_RAE                 0 /* CNTLREG4, RAE active negation on REQ, ACK only */
  174. #define DEF_RADE                1 /* 1CNTLREG4, active negation on REQ, ACK and data */
  175. /*** SCSI block ***/
  176. #define CTCLREG      0x00 /* r      current transf. count, low byte    */
  177. #define CTCMREG     0x04 /* r      current transf. count, middle byte */
  178. #define CTCHREG      0x38 /* r      current transf. count, high byte   */
  179. #define STCLREG      0x00 /* w      start transf. count, low byte      */
  180. #define STCMREG      0x04 /* w      start transf. count, middle byte   */
  181. #define STCHREG      0x38 /* w      start transf. count, high byte     */
  182. #define FFREG      0x08 /* rw     SCSI FIFO reg.                     */
  183. #define STIMREG      0x14 /* w      SCSI timeout reg.                  */
  184. #define SDIDREG      0x10 /* w      SCSI destination ID reg.           */
  185. #define SDIREG_MASK 0x07 /* mask                                      */
  186. #define STPREG      0x18 /* w      synchronous transf. period reg.    */
  187. #define STPREG_STP 0x1F /* synchr. transfer period                   */
  188. #define CLKFREG      0x24 /* w      clock factor reg.                  */
  189. #define CLKFREG_MASK 0x07 /* mask                                      */
  190. #define CMDREG      0x0C /* rw     SCSI command reg.                  */
  191. #define CMDREG_DMA          0x80 /* set DMA mode (set together with opcodes below) */
  192. #define CMDREG_IT           0x10 /* information transfer              */
  193. #define CMDREG_ICCS 0x11 /* initiator command complete steps          */
  194. #define CMDREG_MA 0x12 /* message accepted                          */
  195. #define CMDREG_TPB 0x98 /* transfer pad bytes, DMA mode only         */
  196. #define CMDREG_SATN 0x1A /* set ATN                                   */
  197. #define CMDREG_RATN 0x1B /* reset ATN                                 */
  198. #define CMDREG_SOAS 0x41 /* select without ATN steps                  */
  199. #define CMDREG_SAS 0x42 /* select with ATN steps (1 msg byte)        */
  200. #define CMDREG_SASS 0x43 /* select with ATN and stop steps            */
  201. #define CMDREG_ESR 0x44 /* enable selection/reselection      */
  202. #define CMDREG_DSR 0x45 /* disable selection/reselection     */
  203. #define CMDREG_SA3S 0x46 /* select with ATN 3 steps  (3 msg bytes)  */
  204. #define CMDREG_NOP 0x00 /* no operation                      */
  205. #define CMDREG_CFIFO 0x01 /* clear FIFO                                */
  206. #define CMDREG_RDEV 0x02 /* reset device                      */
  207. #define CMDREG_RBUS 0x03 /* reset SCSI bus                            */
  208. #define STATREG      0x10 /* r      SCSI status reg.                   */
  209. #define STATREG_INT 0x80 /* SCSI interrupt condition detected         */
  210. #define STATREG_IOE 0x40 /* SCSI illegal operation error detected   */
  211. #define STATREG_PE 0x20 /* SCSI parity error detected                */
  212. #define STATREG_CTZ 0x10 /* CTC reg decremented to zero               */
  213. #define STATREG_MSG 0x04 /* SCSI MSG phase (latched?)                 */
  214. #define STATREG_CD 0x02 /* SCSI C/D phase (latched?)                 */
  215. #define STATREG_IO 0x01 /* SCSI I/O phase (latched?)                 */
  216. #define STATREG_PHASE           0x07 /* SCSI phase mask                           */
  217. #define INSTREG      0x14 /* r      interrupt status reg.              */
  218. #define INSTREG_SRST 0x80 /* SCSI reset detected                       */
  219. #define INSTREG_ICMD 0x40 /* SCSI invalid command detected     */
  220. #define INSTREG_DIS 0x20 /* target disconnected or sel/resel timeout */
  221. #define INSTREG_SR 0x10 /* device on bus has service request       */
  222. #define INSTREG_SO 0x08 /* successful operation                      */
  223. #define INSTREG_RESEL 0x04 /* device reselected as initiator    */
  224. #define ISREG      0x18 /* r      internal state reg.                */
  225. #define ISREG_SOF 0x08 /* synchronous offset flag (act. low)        */
  226. #define ISREG_IS 0x07 /* status of intermediate op.                */
  227. #define ISREG_OK_NO_STOP        0x04 /* selection successful                    */
  228. #define ISREG_OK_STOP           0x01 /* selection successful                    */
  229. #define CFIREG      0x1C /* r      current FIFO/internal state reg.   */
  230. #define CFIREG_IS 0xE0 /* status of intermediate op.                */
  231. #define CFIREG_CF 0x1F /* number of bytes in SCSI FIFO              */
  232. #define SOFREG      0x1C /* w      synchr. offset reg.                */
  233. #define SOFREG_RAD 0xC0 /* REQ/ACK deassertion delay (sync.)         */
  234. #define SOFREG_RAA 0x30 /* REQ/ACK assertion delay (sync.)           */
  235. #define SOFREG_SO 0x0F /* synch. offset (sync.)             */
  236. #define CNTLREG1      0x20 /* rw     control register one               */
  237. #define CNTLREG1_ETM 0x80 /* set extended timing mode                  */
  238. #define CNTLREG1_DISR 0x40 /* disable interrupt on SCSI reset           */
  239. #define CNTLREG1_PERE 0x10 /* enable parity error reporting     */
  240. #define CNTLREG1_SID 0x07 /* host adapter SCSI ID                      */
  241. #define CNTLREG2      0x2C /* rw     control register two               */
  242. #define CNTLREG2_ENF 0x40 /* enable features                           */
  243. #define CNTLREG3      0x30 /* rw     control register three             */
  244. #define CNTLREG3_ADIDCHK 0x80 /* additional ID check                       */
  245. #define CNTLREG3_FASTSCSI 0x10 /* fast SCSI                                 */
  246. #define CNTLREG3_FASTCLK 0x08 /* fast SCSI clocking                        */
  247. #define CNTLREG4      0x34 /* rw     control register four              */
  248. #define CNTLREG4_GLITCH 0xC0 /* glitch eater                              */
  249. #define CNTLREG4_PWD 0x20 /* reduced power feature             */
  250. #define CNTLREG4_RAE 0x08 /* write only, active negot. ctrl.           */
  251. #define CNTLREG4_RADE 0x04 /* active negot. ctrl.                       */
  252. #define CNTLREG4_RES 0x10 /* reserved bit, must be 1                   */
  253. /*** DMA block ***/
  254. #define DMACMD      0x40 /* rw     command                            */
  255. #define DMACMD_DIR 0x80 /* transfer direction (1=read from device) */
  256. #define DMACMD_INTE_D 0x40 /* DMA transfer interrupt enable     */
  257. #define DMACMD_INTE_P 0x20 /* page transfer interrupt enable            */
  258. #define DMACMD_MDL 0x10 /* map to memory descriptor list     */
  259. #define DMACMD_DIAG 0x04 /* diagnostics, set to 0             */
  260. #define DMACMD_IDLE  0x00 /* idle cmd                                  */
  261. #define DMACMD_BLAST 0x01 /* flush FIFO to memory                      */
  262. #define DMACMD_ABORT 0x02 /* terminate DMA                     */
  263. #define DMACMD_START 0x03 /* start DMA                                 */
  264. #define DMASTATUS        0x54 /* r      status register                    */
  265. #define DMASTATUS_BCMPLT 0x20 /* BLAST complete                    */
  266. #define DMASTATUS_SCSIINT 0x10 /* SCSI interrupt pending            */
  267. #define DMASTATUS_DONE 0x08 /* DMA transfer terminated                   */
  268. #define DMASTATUS_ABORT 0x04 /* DMA transfer aborted                      */
  269. #define DMASTATUS_ERROR 0x02 /* DMA transfer error                        */
  270. #define DMASTATUS_PWDN 0x02 /* power down indicator                      */
  271. #define DMASTC      0x44 /* rw     starting transfer count            */
  272. #define DMASPA      0x48 /* rw     starting physical address          */
  273. #define DMAWBC      0x4C /* r      working byte counter               */
  274. #define DMAWAC      0x50 /* r      working address counter            */
  275. #define DMASMDLA      0x58 /* rw     starting MDL address               */
  276. #define DMAWMAC      0x5C /* r      working MDL counter                */
  277. /*** SCSI phases ***/
  278. #define PHASE_MSGIN             0x07
  279. #define PHASE_MSGOUT            0x06
  280. #define PHASE_RES_1             0x05
  281. #define PHASE_RES_0             0x04
  282. #define PHASE_STATIN            0x03
  283. #define PHASE_CMDOUT            0x02
  284. #define PHASE_DATAIN            0x01
  285. #define PHASE_DATAOUT           0x00
  286. #define AM53C974_local_declare() unsigned long io_port
  287. #define AM53C974_setio(instance) io_port = instance->io_port
  288. #define AM53C974_read_8(addr)           inb(io_port + (addr))
  289. #define AM53C974_write_8(addr,x)        outb((x), io_port + (addr))
  290. #define AM53C974_read_16(addr)          inw(io_port + (addr))
  291. #define AM53C974_write_16(addr,x)       outw((x), io_port + (addr))
  292. #define AM53C974_read_32(addr)          inl(io_port + (addr))
  293. #define AM53C974_write_32(addr,x)       outl((x), io_port + (addr))
  294. #define AM53C974_poll_int()             { do { statreg = AM53C974_read_8(STATREG); } 
  295.                                              while (!(statreg & STATREG_INT)) ; 
  296.                                           AM53C974_read_8(INSTREG) ; } /* clear int */
  297. #define AM53C974_cfifo() (AM53C974_read_8(CFIREG) & CFIREG_CF)
  298. /* These are "special" values for the tag parameter passed to AM53C974_select. */
  299. #define TAG_NEXT -1 /* Use next free tag */
  300. #define TAG_NONE -2 /* Establish I_T_L nexus instead of I_T_L_Q
  301.    * even on SCSI-II devices */
  302. /************ LILO overrides *************/
  303. typedef struct _override_t {
  304. int host_scsi_id; /* SCSI id of the bus controller */
  305. int target_scsi_id; /* SCSI id of target */
  306. int max_rate; /* max. transfer rate */
  307. int max_offset; /* max. sync. offset, 0 = asynchronous */
  308. } override_t;
  309. #ifdef AM53C974_DEBUG
  310. static void AM53C974_print_phase(struct Scsi_Host *instance);
  311. static void AM53C974_print_queues(struct Scsi_Host *instance);
  312. #endif /* AM53C974_DEBUG */
  313. static void AM53C974_print(struct Scsi_Host *instance);
  314. static void AM53C974_keywait(void);
  315. static __inline__ int AM53C974_pci_detect(Scsi_Host_Template * tpnt);
  316. static int AM53C974_init(Scsi_Host_Template * tpnt, struct pci_dev *pdev);
  317. static void AM53C974_config_after_reset(struct Scsi_Host *instance);
  318. static __inline__ void initialize_SCp(Scsi_Cmnd * cmd);
  319. static __inline__ void run_main(void);
  320. static void AM53C974_main(void);
  321. static void AM53C974_intr(int irq, void *dev_id, struct pt_regs *regs);
  322. static void do_AM53C974_intr(int irq, void *dev_id, struct pt_regs *regs);
  323. static void AM53C974_intr_disconnect(struct Scsi_Host *instance);
  324. static int AM53C974_sync_neg(struct Scsi_Host *instance, int target, unsigned char *msg);
  325. static __inline__ void AM53C974_set_async(struct Scsi_Host *instance, int target);
  326. static __inline__ void AM53C974_set_sync(struct Scsi_Host *instance, int target);
  327. static void AM53C974_information_transfer(struct Scsi_Host *instance,
  328.       unsigned char statreg, unsigned char isreg,
  329.       unsigned char instreg, unsigned char cfifo,
  330.   unsigned char dmastatus);
  331. static int AM53C974_message(struct Scsi_Host *instance, Scsi_Cmnd * cmd, unsigned char msg);
  332. static void AM53C974_select(struct Scsi_Host *instance, Scsi_Cmnd * cmd, int tag);
  333. static void AM53C974_intr_reselect(struct Scsi_Host *instance, unsigned char statreg);
  334. static __inline__ void AM53C974_transfer_dma(struct Scsi_Host *instance, short dir,
  335.        unsigned long length, char *data);
  336. static void AM53C974_dma_blast(struct Scsi_Host *instance, unsigned char dmastatus,
  337.        unsigned char statreg);
  338. static void AM53C974_intr_bus_reset(struct Scsi_Host *instance);
  339. static struct Scsi_Host *first_instance;
  340. static Scsi_Host_Template *the_template;
  341. static struct Scsi_Host *first_host; /* Head of list of AMD boards */
  342. static volatile int main_running;
  343. static int commandline_current;
  344. override_t overrides[7] =
  345. {
  346. {-1, 0, 0, 0},}; /* LILO overrides */
  347. #ifdef AM53C974_DEBUG
  348. static int deb_stop = 1;
  349. static struct {
  350. unsigned char value;
  351. char *name;
  352. } phases[] = {
  353. {
  354. PHASE_DATAOUT, "DATAOUT"
  355. }, {
  356. PHASE_DATAIN, "DATAIN"
  357. }, {
  358. PHASE_CMDOUT, "CMDOUT"
  359. },
  360. {
  361. PHASE_STATIN, "STATIN"
  362. }, {
  363. PHASE_MSGOUT, "MSGOUT"
  364. }, {
  365. PHASE_MSGIN, "MSGIN"
  366. },
  367. {
  368. PHASE_RES_0, "RESERVED 0"
  369. }, {
  370. PHASE_RES_1, "RESERVED 1"
  371. }
  372. };
  373. /************************************************************************** 
  374.  * Function : void AM53C974_print_phase(struct Scsi_Host *instance)
  375.  *
  376.  * Purpose : print the current SCSI phase for debugging purposes
  377.  *
  378.  * Input : instance - which AM53C974
  379.  **************************************************************************/
  380. static void AM53C974_print_phase(struct Scsi_Host *instance)
  381. {
  382. AM53C974_local_declare();
  383. unsigned char statreg, latched;
  384. int i;
  385. AM53C974_setio(instance);
  386. latched = (AM53C974_read_8(CNTLREG2)) & CNTLREG2_ENF;
  387. statreg = AM53C974_read_8(STATREG);
  388. for (i = 0; (phases[i].value != PHASE_RES_1) &&
  389.      (phases[i].value != (statreg & STATREG_PHASE)); ++i);
  390. if (latched)
  391. printk("scsi%d : phase %s, latched at end of last commandn", instance->host_no, phases[i].name);
  392. else
  393. printk("scsi%d : phase %s, real timen", instance->host_no, phases[i].name);
  394. }
  395. /**************************************************************************
  396.  * Function : void AM53C974_print_queues(struct Scsi_Host *instance)
  397.  *
  398.  * Purpose : print commands in the various queues
  399.  *
  400.  * Inputs : instance - which AM53C974
  401.  **************************************************************************/
  402. static void AM53C974_print_queues(struct Scsi_Host *instance)
  403. {
  404. unsigned long flags;
  405. struct AM53C974_hostdata *hostdata = (struct AM53C974_hostdata *) instance->hostdata;
  406. Scsi_Cmnd *ptr;
  407. printk("AM53C974: coroutine is%s running.n", main_running ? "" : "n't");
  408. save_flags(flags);
  409. cli();
  410. if (!hostdata->connected) {
  411. printk("scsi%d: no currently connected commandn", instance->host_no);
  412. } else {
  413. print_Scsi_Cmnd((Scsi_Cmnd *) hostdata->connected);
  414. }
  415. if (!hostdata->sel_cmd) {
  416. printk("scsi%d: no currently arbitrating commandn", instance->host_no);
  417. } else {
  418. print_Scsi_Cmnd((Scsi_Cmnd *) hostdata->sel_cmd);
  419. }
  420. printk("scsi%d: issue_queue ", instance->host_no);
  421. if (!hostdata->issue_queue)
  422. printk("emptyn");
  423. else {
  424. printk(":n");
  425. for (ptr = (Scsi_Cmnd *) hostdata->issue_queue; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
  426. print_Scsi_Cmnd(ptr);
  427. }
  428. printk("scsi%d: disconnected_queue ", instance->host_no);
  429. if (!hostdata->disconnected_queue)
  430. printk("emptyn");
  431. else {
  432. printk(":n");
  433. for (ptr = (Scsi_Cmnd *) hostdata->disconnected_queue; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
  434. print_Scsi_Cmnd(ptr);
  435. }
  436. restore_flags(flags);
  437. }
  438. #endif /* AM53C974_DEBUG */
  439. /**************************************************************************
  440.  * Function : void AM53C974_print(struct Scsi_Host *instance)
  441.  *
  442.  * Purpose : dump the chip registers for debugging purposes
  443.  *
  444.  * Input : instance - which AM53C974
  445.  **************************************************************************/
  446. static void AM53C974_print(struct Scsi_Host *instance)
  447. {
  448. AM53C974_local_declare();
  449. unsigned long flags;
  450. unsigned long ctcreg, dmastc, dmaspa, dmawbc, dmawac;
  451. unsigned char cmdreg, statreg, isreg, cfireg, cntlreg[4], dmacmd,
  452.  dmastatus;
  453. AM53C974_setio(instance);
  454. save_flags(flags);
  455. cli();
  456. ctcreg = AM53C974_read_8(CTCHREG) << 16;
  457. ctcreg |= AM53C974_read_8(CTCMREG) << 8;
  458. ctcreg |= AM53C974_read_8(CTCLREG);
  459. cmdreg = AM53C974_read_8(CMDREG);
  460. statreg = AM53C974_read_8(STATREG);
  461. isreg = AM53C974_read_8(ISREG);
  462. cfireg = AM53C974_read_8(CFIREG);
  463. cntlreg[0] = AM53C974_read_8(CNTLREG1);
  464. cntlreg[1] = AM53C974_read_8(CNTLREG2);
  465. cntlreg[2] = AM53C974_read_8(CNTLREG3);
  466. cntlreg[3] = AM53C974_read_8(CNTLREG4);
  467. dmacmd = AM53C974_read_8(DMACMD);
  468. dmastc = AM53C974_read_32(DMASTC);
  469. dmaspa = AM53C974_read_32(DMASPA);
  470. dmawbc = AM53C974_read_32(DMAWBC);
  471. dmawac = AM53C974_read_32(DMAWAC);
  472. dmastatus = AM53C974_read_8(DMASTATUS);
  473. restore_flags(flags);
  474. printk("AM53C974 register dump:n");
  475. printk("IO base: 0x%04lx; CTCREG: 0x%04lx; CMDREG: 0x%02x; STATREG: 0x%02x; ISREG: 0x%02xn",
  476.        io_port, ctcreg, cmdreg, statreg, isreg);
  477. printk("CFIREG: 0x%02x; CNTLREG1-4: 0x%02x; 0x%02x; 0x%02x; 0x%02xn",
  478.        cfireg, cntlreg[0], cntlreg[1], cntlreg[2], cntlreg[3]);
  479. printk("DMACMD: 0x%02x; DMASTC: 0x%04lx; DMASPA: 0x%04lxn", dmacmd, dmastc, dmaspa);
  480. printk("DMAWBC: 0x%04lx; DMAWAC: 0x%04lx; DMASTATUS: 0x%02xn", dmawbc, dmawac, dmastatus);
  481. printk("---------------------------------------------------------n");
  482. }
  483. /**************************************************************************
  484. * Function : void AM53C974_keywait(void)
  485. *
  486. * Purpose : wait until a key is pressed, if it was the 'r' key leave singlestep mode;
  487. *           this function is used for debugging only
  488. *
  489. * Input : none
  490. **************************************************************************/
  491. static void AM53C974_keywait(void)
  492. {
  493. unsigned long flags;
  494. #ifdef AM53C974_DEBUG
  495. int key;
  496. if (!deb_stop)
  497. return;
  498. #endif
  499. save_flags(flags);
  500. cli();
  501. while ((inb_p(0x64) & 0x01) != 0x01);
  502. #ifdef AM53C974_DEBUG
  503. key = inb(0x60);
  504. if (key == 0x93)
  505. deb_stop = 0; /* don't stop if 'r' was pressed */
  506. #endif
  507. restore_flags(flags);
  508. }
  509. #ifndef MODULE
  510. /**************************************************************************
  511. * Function : AM53C974_setup(char *str)
  512. *
  513. * Purpose : LILO command line initialization of the overrides array,
  514. * Input : str - parameter string.
  515. *
  516. * Returns : 1.
  517. *
  518. * NOTE : this function needs to be declared as an external function
  519. *         in init/main.c and included there in the bootsetups list
  520. ***************************************************************************/
  521. static int AM53C974_setup(char *str)
  522. {
  523. int ints[5];
  524. get_options(str, ARRAY_SIZE(ints), ints);
  525. if (ints[0] < 4)
  526. printk("AM53C974_setup: wrong number of parameters;n correct syntax is: AM53C974=host-scsi-id, target-scsi-id, max-rate, max-offsetn");
  527. else {
  528. if (commandline_current < (sizeof(overrides) / sizeof(override_t))) {
  529. if ((ints[1] < 0) || (ints[1] > 7) ||
  530.     (ints[2] < 0) || (ints[2] > 7) ||
  531.     (ints[1] == ints[2]) ||
  532.     (ints[3] < (DEF_CLK / MAX_PERIOD)) || (ints[3] > (DEF_CLK / MIN_PERIOD)) ||
  533.     (ints[4] < 0) || (ints[4] > MAX_OFFSET))
  534. printk("AM53C974_setup: illegal parametern");
  535. else {
  536. overrides[commandline_current].host_scsi_id = ints[1];
  537. overrides[commandline_current].target_scsi_id = ints[2];
  538. overrides[commandline_current].max_rate = ints[3];
  539. overrides[commandline_current].max_offset = ints[4];
  540. commandline_current++;
  541. }
  542. } else
  543. printk("AM53C974_setup: too many overridesn");
  544. }
  545. return 1;
  546. }
  547. __setup("AM53C974=", AM53C974_setup);
  548. #endif /* !MODULE */
  549. /**************************************************************************
  550. * Function : int AM53C974_pci_detect(Scsi_Host_Template *tpnt)
  551. *
  552. * Purpose : detects and initializes AM53C974 SCSI chips with PCI Bios
  553. *
  554. * Inputs : tpnt - host template
  555. * Returns : number of host adapters detected
  556. **************************************************************************/
  557. static int __init AM53C974_pci_detect(Scsi_Host_Template * tpnt)
  558. {
  559. int count = 0; /* number of boards detected */
  560. struct pci_dev *pdev = NULL;
  561. unsigned short command;
  562. while ((pdev = pci_find_device(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_SCSI, pdev))) {
  563. if (pci_enable_device(pdev))
  564. continue;
  565. pci_read_config_word(pdev, PCI_COMMAND, &command);
  566. /* check whether device is I/O mapped -- should be */
  567. if (!(command & PCI_COMMAND_IO))
  568. continue;
  569. pci_set_master (pdev);
  570. /* everything seems OK now, so initialize */
  571. if (AM53C974_init(tpnt, pdev))
  572. count++;
  573. }
  574. return (count);
  575. }
  576. /**************************************************************************
  577. * Function : int AM53C974_init(Scsi_Host_Template *tpnt, struct pci_dev *pdev)
  578. *
  579. * Purpose : initializes instance and corresponding AM53/79C974 chip,
  580. *
  581. * Inputs : tpnt - template, pci_config - PCI configuration,
  582. * Returns : 1 on success, 0 on failure.
  583. * NOTE: If no override for the controller's SCSI id is given and AM53C974_SCSI_ID 
  584. *       is not defined we assume that the SCSI address of this controller is correctly
  585. *       set up by the BIOS (as reflected by contents of register CNTLREG1).
  586. *       This is the only BIOS assistance we need.
  587. **************************************************************************/
  588. static int __init  AM53C974_init(Scsi_Host_Template * tpnt, struct pci_dev *pdev)
  589. {
  590. AM53C974_local_declare();
  591. int i, j;
  592. struct Scsi_Host *instance, *search;
  593. struct AM53C974_hostdata *hostdata;
  594. #ifdef AM53C974_OPTION_DEBUG_PROBE_ONLY
  595. printk("AM53C974: probe only enabled, aborting initializationn");
  596. return 0;
  597. #endif
  598. instance = scsi_register(tpnt, sizeof(struct AM53C974_hostdata));
  599. if (!instance) {
  600. printk(KERN_WARNING "AM53C974: Unable to register host, aborting.n");
  601. return 0;
  602. }
  603. scsi_set_pci_device(instance, pdev);
  604. hostdata = (struct AM53C974_hostdata *) instance->hostdata;
  605. instance->base = 0;
  606. instance->io_port = pci_resource_start(pdev, 0);
  607. instance->irq = pdev->irq;
  608. instance->dma_channel = -1;
  609. AM53C974_setio(instance);
  610. #ifdef AM53C974_SCSI_ID
  611. instance->this_id = AM53C974_SCSI_ID;
  612. AM53C974_write_8(CNTLREG1, instance->this_id & CNTLREG1_SID);
  613. #else
  614. instance->this_id = AM53C974_read_8(CNTLREG1) & CNTLREG1_SID;
  615. if (instance->this_id != 7)
  616. printk("scsi%d: WARNING: unusual hostadapter SCSI id %d; please verify!n",
  617.        instance->host_no, instance->this_id);
  618. #endif
  619. for (i = 0; i < sizeof(hostdata->msgout); i++) {
  620. hostdata->msgout[i] = NOP;
  621. hostdata->last_message[i] = NOP;
  622. }
  623. for (i = 0; i < 8; i++) {
  624. hostdata->busy[i] = 0;
  625. hostdata->sync_per[i] = DEF_STP;
  626. hostdata->sync_off[i] = 0;
  627. hostdata->sync_neg[i] = 0;
  628. hostdata->sync_en[i] = DEFAULT_SYNC_NEGOTIATION_ENABLED;
  629. hostdata->max_rate[i] = DEFAULT_RATE;
  630. hostdata->max_offset[i] = DEFAULT_SYNC_OFFSET;
  631. }
  632. /* overwrite defaults by LILO overrides */
  633. for (i = 0; i < commandline_current; i++) {
  634. if (overrides[i].host_scsi_id == instance->this_id) {
  635. j = overrides[i].target_scsi_id;
  636. hostdata->sync_en[j] = 1;
  637. hostdata->max_rate[j] = overrides[i].max_rate;
  638. hostdata->max_offset[j] = overrides[i].max_offset;
  639. }
  640. }
  641. hostdata->sel_cmd = NULL;
  642. hostdata->connected = NULL;
  643. hostdata->issue_queue = NULL;
  644. hostdata->disconnected_queue = NULL;
  645. hostdata->in_reset = 0;
  646. hostdata->aborted = 0;
  647. hostdata->selecting = 0;
  648. hostdata->disconnecting = 0;
  649. hostdata->dma_busy = 0;
  650. /* Set up an interrupt handler if we aren't already sharing an IRQ with another board */
  651. for (search = first_host;
  652.      search && (((the_template != NULL) && (search->hostt != the_template)) ||
  653.  (search->irq != instance->irq) || (search == instance));
  654.      search = search->next);
  655. if (!search) {
  656. if (request_irq(instance->irq, do_AM53C974_intr, SA_SHIRQ, "AM53C974", instance)) {
  657. printk("scsi%d: IRQ%d not free, detachingn", instance->host_no, instance->irq);
  658. scsi_unregister(instance);
  659. return 0;
  660. }
  661. } else {
  662. printk("scsi%d: using interrupt handler previously installed for scsi%dn",
  663.        instance->host_no, search->host_no);
  664. }
  665. if (!the_template) {
  666. the_template = instance->hostt;
  667. first_instance = instance;
  668. }
  669. /* do hard reset */
  670. AM53C974_write_8(CMDREG, CMDREG_RDEV); /* reset device */
  671. udelay(5);
  672. AM53C974_write_8(CMDREG, CMDREG_NOP);
  673. AM53C974_write_8(CNTLREG1, CNTLREG1_DISR | instance->this_id);
  674. AM53C974_write_8(CMDREG, CMDREG_RBUS); /* reset SCSI bus */
  675. udelay(10);
  676. AM53C974_config_after_reset(instance);
  677. mdelay(500);
  678. return (1);
  679. }
  680. /*********************************************************************
  681. * Function : AM53C974_config_after_reset(struct Scsi_Host *instance) *
  682. *                                                                    *
  683. * Purpose : initializes chip registers after reset                   *
  684. *                                                                    *
  685. * Inputs : instance - which AM53C974                                 *
  686. *                                                                    *
  687. * Returns : nothing                                                  *
  688. **********************************************************************/
  689. static void AM53C974_config_after_reset(struct Scsi_Host *instance)
  690. {
  691. AM53C974_local_declare();
  692. AM53C974_setio(instance);
  693. /* clear SCSI FIFO */
  694. AM53C974_write_8(CMDREG, CMDREG_CFIFO);
  695. /* configure device */
  696. AM53C974_write_8(STIMREG, DEF_SCSI_TIMEOUT);
  697. AM53C974_write_8(STPREG, DEF_STP & STPREG_STP);
  698. AM53C974_write_8(SOFREG, (DEF_SOF_RAD << 6) | (DEF_SOF_RAA << 4));
  699. AM53C974_write_8(CLKFREG, DEF_CLKF & CLKFREG_MASK);
  700. AM53C974_write_8(CNTLREG1, (DEF_ETM << 7) | CNTLREG1_DISR | (DEF_PERE << 4) | instance->this_id);
  701. AM53C974_write_8(CNTLREG2, (DEF_ENF << 6));
  702. AM53C974_write_8(CNTLREG3, (DEF_ADIDCHK << 7) | (DEF_FASTSCSI << 4) | (DEF_FASTCLK << 3));
  703. AM53C974_write_8(CNTLREG4, (DEF_GLITCH << 6) | (DEF_PWD << 5) | (DEF_RAE << 3) | (DEF_RADE << 2) | CNTLREG4_RES);
  704. }
  705. /***********************************************************************
  706. * Function : const char *AM53C974_info(struct Scsi_Host *instance)     *
  707. *                                                                      *
  708. * Purpose : return device driver information                           *
  709. *                                                                      *
  710. * Inputs : instance - which AM53C974                                   *
  711. *                                                                      *
  712. * Returns : info string                                                *
  713. ************************************************************************/
  714. static const char *AM53C974_info(struct Scsi_Host *instance)
  715. {
  716. static char info[100];
  717. sprintf(info, "AM53/79C974 PCscsi driver rev. %d.%d; host I/O address: 0x%lx; irq: %dn",
  718.   AM53C974_DRIVER_REVISION_MAJOR, AM53C974_DRIVER_REVISION_MINOR,
  719. instance->io_port, instance->irq);
  720. return (info);
  721. }
  722. /************************************************************************** 
  723. * Function : int AM53C974_command (Scsi_Cmnd *SCpnt)                      *
  724. *                                                                         *
  725. * Purpose : the unqueued SCSI command function, replaced by the           *
  726. *           AM53C974_queue_command function                               *
  727. *                                                                         *
  728. * Inputs : SCpnt - pointer to command structure                           *
  729. *                                                                         *
  730. * Returns :status, see hosts.h for details                                *
  731. ***************************************************************************/
  732. static int AM53C974_command(Scsi_Cmnd * SCpnt)
  733. {
  734. DEB(printk("AM53C974_command calledn"));
  735. return 0;
  736. }
  737. /**************************************************************************
  738. * Function : void initialize_SCp(Scsi_Cmnd *cmd)                          *
  739. *                                                                         *
  740. * Purpose : initialize the saved data pointers for cmd to point to the    *
  741. *     start of the buffer.                                          *                              
  742. *                                                                         *
  743. * Inputs : cmd - Scsi_Cmnd structure to have pointers reset.              *
  744. *                                                                         *
  745. * Returns : nothing                                                       *
  746. **************************************************************************/
  747. static __inline__ void initialize_SCp(Scsi_Cmnd * cmd)
  748. {
  749. if (cmd->use_sg) {
  750. cmd->SCp.buffer = (struct scatterlist *) cmd->buffer;
  751. cmd->SCp.buffers_residual = cmd->use_sg - 1;
  752. cmd->SCp.ptr = (char *) cmd->SCp.buffer->address;
  753. cmd->SCp.this_residual = cmd->SCp.buffer->length;
  754. } else {
  755. cmd->SCp.buffer = NULL;
  756. cmd->SCp.buffers_residual = 0;
  757. cmd->SCp.ptr = (char *) cmd->request_buffer;
  758. cmd->SCp.this_residual = cmd->request_bufflen;
  759. }
  760. }
  761. /**************************************************************************
  762. * Function : run_main(void)                                               *
  763. *                                                                         *
  764. * Purpose : insure that the coroutine is running and will process our     *
  765. *      request.  main_running is checked/set here (in an inline      *
  766. *           function rather than in AM53C974_main itself to reduce the    *
  767. *           chances of stack overflow.                                    *
  768. *                                                                         *
  769. *                                                                         *
  770. * Inputs : none                                                           *
  771. *                                                                         *
  772. * Returns : nothing                                                       *
  773. **************************************************************************/
  774. static __inline__ void run_main(void)
  775. {
  776. unsigned long flags;
  777. save_flags(flags);
  778. cli();
  779. if (!main_running) {
  780. /* main_running is cleared in AM53C974_main once it can't do 
  781.    more work, and AM53C974_main exits with interrupts disabled. */
  782. main_running = 1;
  783. AM53C974_main();
  784. }
  785. restore_flags(flags);
  786. }
  787. /************************************************************************** 
  788. * Function : int AM53C974_queue_command(Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
  789. *
  790. * Purpose : writes SCSI command into AM53C974 FIFO 
  791. *
  792. * Inputs : cmd - SCSI command, done - function called on completion, with
  793. * a pointer to the command descriptor.
  794. * Returns : status, see hosts.h for details
  795. *
  796. * Side effects : 
  797. *      cmd is added to the per instance issue_queue, with minor 
  798. * twiddling done to the host specific fields of cmd.  If the 
  799. * main coroutine is not running, it is restarted.
  800. **************************************************************************/
  801. static int AM53C974_queue_command(Scsi_Cmnd * cmd, void (*done) (Scsi_Cmnd *))
  802. {
  803. unsigned long flags;
  804. struct Scsi_Host *instance = cmd->host;
  805. struct AM53C974_hostdata *hostdata = (struct AM53C974_hostdata *) instance->hostdata;
  806. Scsi_Cmnd *tmp;
  807. save_flags(flags);
  808. cli();
  809. DEB_QUEUE(printk(SEPARATOR_LINE));
  810. DEB_QUEUE(printk("scsi%d: AM53C974_queue_command calledn", instance->host_no));
  811. DEB_QUEUE(printk("cmd=%02x target=%02x lun=%02x bufflen=%d use_sg = %02xn",
  812.  cmd->cmnd[0], cmd->target, cmd->lun, cmd->request_bufflen, cmd->use_sg));
  813. /* We use the host_scribble field as a pointer to the next command in a queue */
  814. cmd->host_scribble = NULL;
  815. cmd->scsi_done = done;
  816. cmd->result = 0;
  817. cmd->device->disconnect = 0;
  818. /* Insert the cmd into the issue queue. Note that REQUEST SENSE 
  819.  * commands are added to the head of the queue since any command will
  820.  * clear the contingent allegiance condition that exists and the 
  821.  * sense data is only guaranteed to be valid while the condition exists. */
  822. if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
  823. LIST(cmd, hostdata->issue_queue);
  824. cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
  825. hostdata->issue_queue = cmd;
  826. } else {
  827. for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp->host_scribble;
  828.      tmp = (Scsi_Cmnd *) tmp->host_scribble);
  829. LIST(cmd, tmp);
  830. tmp->host_scribble = (unsigned char *) cmd;
  831. }
  832. DEB_QUEUE(printk("scsi%d : command added to %s of queuen", instance->host_no,
  833.      (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail"));
  834. /* Run the coroutine if it isn't already running. */
  835. run_main();
  836. restore_flags(flags);
  837. return 0;
  838. }
  839. /**************************************************************************
  840.  * Function : AM53C974_main (void) 
  841.  *
  842.  * Purpose : AM53C974_main is a coroutine that runs as long as more work can 
  843.  * be done on the AM53C974 host adapters in a system.  Both 
  844.  * AM53C974_queue_command() and AM53C974_intr() will try to start it 
  845.  * in case it is not running.
  846.  * 
  847.  * NOTE : AM53C974_main exits with interrupts *disabled*, the caller should 
  848.  *  reenable them.  This prevents reentrancy and kernel stack overflow.
  849.  **************************************************************************/
  850. static void AM53C974_main(void)
  851. {
  852. AM53C974_local_declare();
  853. unsigned long flags;
  854. Scsi_Cmnd *tmp, *prev;
  855. struct Scsi_Host *instance;
  856. struct AM53C974_hostdata *hostdata;
  857. int done;
  858. /* We run (with interrupts disabled) until we're sure that none of 
  859.  * the host adapters have anything that can be done, at which point 
  860.  * we set main_running to 0 and exit. */
  861. save_flags(flags);
  862. cli(); /* Freeze request queues */
  863. do {
  864. done = 1;
  865. for (instance = first_instance; instance && instance->hostt == the_template;
  866.      instance = instance->next) {
  867. hostdata = (struct AM53C974_hostdata *) instance->hostdata;
  868. AM53C974_setio(instance);
  869. /* start to select target if we are not connected and not in the 
  870.    selection process */
  871. if (!hostdata->connected && !hostdata->sel_cmd) {
  872. /* Search through the issue_queue for a command destined for a target 
  873.    that is not busy. */
  874. for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, prev = NULL; tmp;
  875.      prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble) {
  876. /*  When we find one, remove it from the issue queue. */
  877. if (!(hostdata->busy[tmp->target] & (1 << tmp->lun))) {
  878. if (prev) {
  879. REMOVE(prev, (Scsi_Cmnd *) (prev->host_scribble), tmp,
  880.        (Scsi_Cmnd *) (tmp->host_scribble));
  881. prev->host_scribble = tmp->host_scribble;
  882. } else {
  883. REMOVE(-1, hostdata->issue_queue, tmp, tmp->host_scribble);
  884. hostdata->issue_queue = (Scsi_Cmnd *) tmp->host_scribble;
  885. }
  886. tmp->host_scribble = NULL;
  887. /* go into selection mode, disable reselection and wait for
  888.    SO interrupt which will continue with the selection process */
  889. hostdata->selecting = 1;
  890. hostdata->sel_cmd = tmp;
  891. AM53C974_write_8(CMDREG, CMDREG_DSR);
  892. break;
  893. } /* if target/lun is not busy */
  894. } /* for */
  895. }
  896. /* if (!hostdata->connected) */ 
  897. else {
  898. DEB(printk("main: connected; cmd = 0x%lx, sel_cmd = 0x%lxn",
  899.    (long) hostdata->connected, (long) hostdata->sel_cmd));
  900. }
  901. } /* for instance */
  902. } while (!done);
  903. main_running = 0;
  904. restore_flags(flags);
  905. }
  906. /************************************************************************
  907. * Function : AM53C974_intr(int irq, void *dev_id, struct pt_regs *regs) *
  908. *                                                                       *
  909. * Purpose : interrupt handler                                           *
  910. *                                                                       *
  911. * Inputs : irq - interrupt line, regs - ?                               *
  912. *                                                                       *
  913. * Returns : nothing                                                     *
  914. ************************************************************************/
  915. static void do_AM53C974_intr(int irq, void *dev_id, struct pt_regs *regs)
  916. {
  917. unsigned long flags;
  918. spin_lock_irqsave(&io_request_lock, flags);
  919. AM53C974_intr(irq, dev_id, regs);
  920. spin_unlock_irqrestore(&io_request_lock, flags);
  921. }
  922. /************************************************************************
  923. * Function : AM53C974_intr(int irq, void *dev_id, struct pt_regs *regs) *
  924. *                                                                       *
  925. * Purpose : interrupt handler                                           *
  926. *                                                                       *
  927. * Inputs : irq - interrupt line, regs - ?                               *
  928. *                                                                       *
  929. * Returns : nothing                                                     *
  930. ************************************************************************/
  931. static void AM53C974_intr(int irq, void *dev_id, struct pt_regs *regs)
  932. {
  933. AM53C974_local_declare();
  934. struct Scsi_Host *instance;
  935. struct AM53C974_hostdata *hostdata;
  936. unsigned char cmdreg, dmastatus, statreg, isreg, instreg, cfifo;
  937. /* find AM53C974 hostadapter responsible for this interrupt */
  938. for (instance = first_instance; instance; instance = instance->next)
  939. if ((instance->irq == irq) && (instance->hostt == the_template))
  940. goto FOUND;
  941. return;
  942. /* found; now decode and process */
  943. FOUND:
  944.     hostdata = (struct AM53C974_hostdata *) instance->hostdata;
  945. AM53C974_setio(instance);
  946. dmastatus = AM53C974_read_8(DMASTATUS);
  947. DEB_INTR(printk(SEPARATOR_LINE));
  948. DEB_INTR(printk("AM53C974 interrupt; dmastatus=0x%02xn", dmastatus));
  949. KEYWAIT();
  950. /*** DMA related interrupts ***/
  951. if (hostdata->connected && (dmastatus & (DMASTATUS_ERROR | DMASTATUS_PWDN |
  952.  DMASTATUS_ABORT))) {
  953. /* DMA error or POWERDOWN */
  954. printk("scsi%d: DMA error or powerdown; dmastatus: 0x%02xn",
  955.        instance->host_no, dmastatus);
  956. #ifdef AM53C974_DEBUG
  957. deb_stop = 1;
  958. #endif
  959. panic("scsi%d: cannot recovern", instance->host_no);
  960. }
  961. if (hostdata->connected && (dmastatus & DMASTATUS_DONE)) {
  962. /* DMA transfer done */
  963. unsigned long residual;
  964. unsigned long flags;
  965. save_flags(flags);
  966. cli();
  967. if (!(AM53C974_read_8(DMACMD) & DMACMD_DIR)) {
  968. do {
  969. dmastatus = AM53C974_read_8(DMASTATUS);
  970. residual = AM53C974_read_8(CTCLREG) | (AM53C974_read_8(CTCMREG) << 8) |
  971.     (AM53C974_read_8(CTCHREG) << 16);
  972. residual += AM53C974_read_8(CFIREG) & CFIREG_CF;
  973. } while (!(dmastatus & DMASTATUS_SCSIINT) && residual);
  974. residual = AM53C974_read_8(CTCLREG) | (AM53C974_read_8(CTCMREG) << 8) |
  975.     (AM53C974_read_8(CTCHREG) << 16);
  976. residual += AM53C974_read_8(CFIREG) & CFIREG_CF;
  977. } else
  978. residual = 0;
  979. hostdata->connected->SCp.ptr += hostdata->connected->SCp.this_residual - residual;
  980. hostdata->connected->SCp.this_residual = residual;
  981. AM53C974_write_8(DMACMD, DMACMD_IDLE);
  982. /* if service request missed before, process it now (ugly) */
  983. if (hostdata->dma_busy) {
  984. hostdata->dma_busy = 0;
  985. cmdreg = AM53C974_read_8(CMDREG);
  986. statreg = AM53C974_read_8(STATREG);
  987. isreg = AM53C974_read_8(ISREG);
  988. instreg = AM53C974_read_8(INSTREG);
  989. cfifo = AM53C974_cfifo();
  990. AM53C974_information_transfer(instance, statreg, isreg, instreg, cfifo,
  991.       dmastatus);
  992. }
  993. restore_flags(flags);
  994. }
  995. if (!(dmastatus & DMASTATUS_SCSIINT)) {
  996. return;
  997. }
  998. /*** SCSI related interrupts ***/
  999. cmdreg = AM53C974_read_8(CMDREG);
  1000. statreg = AM53C974_read_8(STATREG);
  1001. isreg = AM53C974_read_8(ISREG);
  1002. instreg = AM53C974_read_8(INSTREG);
  1003. cfifo = AM53C974_cfifo();
  1004. DEB_INTR(printk("scsi%d: statreg: 0x%02x; isreg: 0x%02x; instreg: 0x%02x; cfifo: 0x%02xn",
  1005.      instance->host_no, statreg, isreg, instreg, cfifo));
  1006. if (statreg & STATREG_PE) {
  1007. /* parity error */
  1008. #ifdef AM53C974_DEBUG
  1009. deb_stop = 1;
  1010. #endif
  1011. printk("scsi%d : PARITY errorn", instance->host_no);
  1012. if (hostdata->connected)
  1013. hostdata->sync_off[hostdata->connected->target] = 0; /* setup asynchronous transfer */
  1014. hostdata->aborted = 1;
  1015. }
  1016. if (statreg & STATREG_IOE) {
  1017. /* illegal operation error */
  1018. #ifdef AM53C974_DEBUG
  1019. deb_stop = 1;
  1020. #endif
  1021. printk("scsi%d : ILLEGAL OPERATION errorn", instance->host_no);
  1022. printk("cmdreg:  0x%02x; dmacmd:  0x%02x; statreg: 0x%02x; n"
  1023.    "isreg:   0x%02x; instreg: 0x%02x; cfifo:   0x%02xn",
  1024.        cmdreg, AM53C974_read_8(DMACMD), statreg, isreg, instreg, cfifo);
  1025. }
  1026. if (hostdata->in_reset && (instreg & INSTREG_SRST)) {
  1027. unsigned long flags;
  1028. /* RESET INTERRUPT */
  1029. #ifdef AM53C974_DEBUG
  1030. deb_stop = 1;
  1031. #endif
  1032. DEB(printk("Bus reset interrupt receivedn"));
  1033. AM53C974_intr_bus_reset(instance);
  1034. save_flags(flags);
  1035. cli();
  1036. if (hostdata->connected) {
  1037. hostdata->connected->result = DID_RESET << 16;
  1038. hostdata->connected->scsi_done((Scsi_Cmnd *) hostdata->connected);
  1039. hostdata->connected = NULL;
  1040. } else {
  1041. if (hostdata->sel_cmd) {
  1042. hostdata->sel_cmd->result = DID_RESET << 16;
  1043. hostdata->sel_cmd->scsi_done((Scsi_Cmnd *) hostdata->sel_cmd);
  1044. hostdata->sel_cmd = NULL;
  1045. }
  1046. }
  1047. restore_flags(flags);
  1048. if (hostdata->in_reset == 1)
  1049. goto EXIT;
  1050. else
  1051. return;
  1052. }
  1053. if (instreg & INSTREG_ICMD) {
  1054. /* INVALID COMMAND INTERRUPT */
  1055. #ifdef AM53C974_DEBUG
  1056. deb_stop = 1;
  1057. #endif
  1058. printk("scsi%d: Invalid command interruptn", instance->host_no);
  1059. printk("cmdreg:  0x%02x; dmacmd:  0x%02x; statreg: 0x%02x; dmastatus: 0x%02x; n"
  1060.    "isreg:   0x%02x; instreg: 0x%02x; cfifo:   0x%02xn",
  1061.        cmdreg, AM53C974_read_8(DMACMD), statreg, dmastatus, isreg, instreg, cfifo);
  1062. panic("scsi%d: cannot recovern", instance->host_no);
  1063. }
  1064. if (instreg & INSTREG_DIS) {
  1065. unsigned long flags;
  1066. /* DISCONNECT INTERRUPT */
  1067. DEB_INTR(printk("Disconnect interrupt received; "));
  1068. save_flags(flags);
  1069. cli();
  1070. AM53C974_intr_disconnect(instance);
  1071. restore_flags(flags);
  1072. goto EXIT;
  1073. }
  1074. if (instreg & INSTREG_RESEL) {
  1075. unsigned long flags;
  1076. /* RESELECTION INTERRUPT */
  1077. DEB_INTR(printk("Reselection interrupt receivedn"));
  1078. save_flags(flags);
  1079. cli();
  1080. AM53C974_intr_reselect(instance, statreg);
  1081. restore_flags(flags);
  1082. goto EXIT;
  1083. }
  1084. if (instreg & INSTREG_SO) {
  1085. DEB_INTR(printk("Successful operation interrupt receivedn"));
  1086. if (hostdata->selecting) {
  1087. unsigned long flags;
  1088. DEB_INTR(printk("DSR completed, starting selectn"));
  1089. save_flags(flags);
  1090. cli();
  1091. AM53C974_select(instance, (Scsi_Cmnd *) hostdata->sel_cmd,
  1092.   (hostdata->sel_cmd->cmnd[0] == REQUEST_SENSE) ?
  1093. TAG_NONE : TAG_NEXT);
  1094. hostdata->selecting = 0;
  1095. AM53C974_set_sync(instance, hostdata->sel_cmd->target);
  1096. restore_flags(flags);
  1097. return;
  1098. }
  1099. if (hostdata->sel_cmd != NULL) {
  1100. if (((isreg & ISREG_IS) != ISREG_OK_NO_STOP) &&
  1101.     ((isreg & ISREG_IS) != ISREG_OK_STOP)) {
  1102. unsigned long flags;
  1103. /* UNSUCCESSFUL SELECTION */
  1104. DEB_INTR(printk("unsuccessful selectionn"));
  1105. save_flags(flags);
  1106. cli();
  1107. hostdata->dma_busy = 0;
  1108. LIST(hostdata->sel_cmd, hostdata->issue_queue);
  1109. hostdata->sel_cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
  1110. hostdata->issue_queue = hostdata->sel_cmd;
  1111. hostdata->sel_cmd = NULL;
  1112. hostdata->selecting = 0;
  1113. restore_flags(flags);
  1114. goto EXIT;
  1115. } else {
  1116. unsigned long flags;
  1117. /* SUCCESSFUL SELECTION */
  1118. DEB(printk("successful selection; cmd=0x%02lxn", (long) hostdata->sel_cmd));
  1119. save_flags(flags);
  1120. cli();
  1121. hostdata->dma_busy = 0;
  1122. hostdata->disconnecting = 0;
  1123. hostdata->connected = hostdata->sel_cmd;
  1124. hostdata->sel_cmd = NULL;
  1125. hostdata->selecting = 0;
  1126. #ifdef SCSI2
  1127. if (!hostdata->connected->device->tagged_queue)
  1128. #endif
  1129. hostdata->busy[hostdata->connected->target] |= (1 << hostdata->connected->lun);
  1130. /* very strange -- use_sg is sometimes nonzero for request sense commands !! */
  1131. if ((hostdata->connected->cmnd[0] == REQUEST_SENSE) && hostdata->connected->use_sg) {
  1132. DEB(printk("scsi%d: REQUEST_SENSE command with nonzero use_sgn", instance->host_no));
  1133. KEYWAIT();
  1134. hostdata->connected->use_sg = 0;
  1135. }
  1136. initialize_SCp((Scsi_Cmnd *) hostdata->connected);
  1137. hostdata->connected->SCp.phase = PHASE_CMDOUT;
  1138. AM53C974_information_transfer(instance, statreg, isreg, instreg, cfifo, dmastatus);
  1139. restore_flags(flags);
  1140. return;
  1141. }
  1142. } else {
  1143. unsigned long flags;
  1144. save_flags(flags);
  1145. cli();
  1146. AM53C974_information_transfer(instance, statreg, isreg, instreg, cfifo, dmastatus);
  1147. restore_flags(flags);
  1148. return;
  1149. }
  1150. }
  1151. if (instreg & INSTREG_SR) {
  1152. DEB_INTR(printk("Service request interrupt received, "));
  1153. if (hostdata->connected) {
  1154. unsigned long flags;
  1155. DEB_INTR(printk("calling information_transfern"));
  1156. save_flags(flags);
  1157. cli();
  1158. AM53C974_information_transfer(instance, statreg, isreg, instreg, cfifo, dmastatus);
  1159. restore_flags(flags);
  1160. } else {
  1161. printk("scsi%d: weird: service request when no command connectedn", instance->host_no);
  1162. AM53C974_write_8(CMDREG, CMDREG_CFIFO);
  1163. } /* clear FIFO */
  1164. return;
  1165. }
  1166. EXIT:
  1167.     DEB_INTR(printk("intr: starting mainn"));
  1168. run_main();
  1169. DEB_INTR(printk("end of intrn"));
  1170. }
  1171. /************************************************************************** 
  1172. * Function : AM53C974_intr_disconnect(struct Scsi_Host *instance)
  1173. *
  1174. * Purpose : manage target disconnection
  1175. *
  1176. * Inputs : instance -- which AM53C974
  1177. * Returns : nothing
  1178. **************************************************************************/
  1179. static void AM53C974_intr_disconnect(struct Scsi_Host *instance)
  1180. {
  1181. AM53C974_local_declare();
  1182. struct AM53C974_hostdata *hostdata = (struct AM53C974_hostdata *) instance->hostdata;
  1183. Scsi_Cmnd *cmd;
  1184. AM53C974_setio(instance);
  1185. if (hostdata->sel_cmd != NULL) {
  1186. /* normal selection timeout, typical for nonexisting targets */
  1187. cmd = (Scsi_Cmnd *) hostdata->sel_cmd;
  1188. DEB_INTR(printk("bad targetn"));
  1189. cmd->result = DID_BAD_TARGET << 16;
  1190. goto EXIT_FINISHED;
  1191. }
  1192. if (!hostdata->connected) {
  1193. /* can happen if controller was reset, a device tried to reconnect,
  1194.    failed and disconnects now */
  1195. AM53C974_write_8(CMDREG, CMDREG_CFIFO);
  1196. return;
  1197. }
  1198. if (hostdata->disconnecting) {
  1199. /* target sent disconnect message, so we are prepared */
  1200. cmd = (Scsi_Cmnd *) hostdata->connected;
  1201. AM53C974_set_async(instance, cmd->target);
  1202. DEB_INTR(printk("scsi%d : disc. from cmnd %d for ta %d, lun %dn",
  1203. instance->host_no, cmd->cmnd[0], cmd->target, cmd->lun));
  1204. if (cmd->device->disconnect) {
  1205. /* target wants to reselect later */
  1206. DEB_INTR(printk("ok, re-enabling selectionn"));
  1207. LIST(cmd, hostdata->disconnected_queue);
  1208. cmd->host_scribble = (unsigned char *) hostdata->disconnected_queue;
  1209. hostdata->disconnected_queue = cmd;
  1210. DEB_QUEUE(printk("scsi%d : command for target %d lun %d this %d was moved from connected to"
  1211.  "  the disconnected_queuen", instance->host_no, cmd->target,
  1212.  cmd->lun, hostdata->disconnected_queue->SCp.this_residual));
  1213. DEB_QUEUE(AM53C974_print_queues(instance));
  1214. goto EXIT_UNFINISHED;
  1215. } else {
  1216. /* target does not want to reselect later, we are really finished */
  1217. #ifdef AM53C974_DEBUG
  1218. if (cmd->cmnd[0] == REQUEST_SENSE) {
  1219. int i;
  1220. printk("Request sense data dump:n");
  1221. for (i = 0; i < cmd->request_bufflen; i++) {
  1222. printk("%02x ", *((char *) (cmd->request_buffer) + i));
  1223. if (i && !(i % 16))
  1224. printk("n");
  1225. }
  1226. printk("n");
  1227. }
  1228. #endif
  1229. goto EXIT_FINISHED;
  1230. } /* !cmd->device->disconnect */
  1231. } /* if (hostdata->disconnecting) */
  1232. /* no disconnect message received; unexpected disconnection */
  1233. cmd = (Scsi_Cmnd *) hostdata->connected;
  1234. if (cmd) {
  1235. #ifdef AM53C974_DEBUG
  1236. deb_stop = 1;
  1237. #endif
  1238. AM53C974_set_async(instance, cmd->target);
  1239. printk("scsi%d: Unexpected disconnect; phase: %d; target: %d; this_residual: %d; buffers_residual: %d; message: %dn",
  1240.        instance->host_no, cmd->SCp.phase, cmd->target, cmd->SCp.this_residual, cmd->SCp.buffers_residual,
  1241.        cmd->SCp.Message);
  1242. printk("cmdreg: 0x%02x; statreg: 0x%02x; isreg: 0x%02x; cfifo: 0x%02xn",
  1243.        AM53C974_read_8(CMDREG), AM53C974_read_8(STATREG), AM53C974_read_8(ISREG),
  1244.        AM53C974_read_8(CFIREG) & CFIREG_CF);
  1245. if ((hostdata->last_message[0] == EXTENDED_MESSAGE) &&
  1246.     (hostdata->last_message[2] == EXTENDED_SDTR)) {
  1247. /* sync. negotiation was aborted, setup asynchronous transfer with target */
  1248. hostdata->sync_off[cmd->target] = 0;
  1249. }
  1250. if (hostdata->aborted || hostdata->msgout[0] == ABORT)
  1251. cmd->result = DID_ABORT << 16;
  1252. else
  1253. cmd->result = DID_ERROR << 16;
  1254. goto EXIT_FINISHED;
  1255. }
  1256. EXIT_FINISHED:
  1257.     hostdata->aborted = 0;
  1258. hostdata->msgout[0] = NOP;
  1259. hostdata->sel_cmd = NULL;
  1260. hostdata->connected = NULL;
  1261. hostdata->selecting = 0;
  1262. hostdata->disconnecting = 0;
  1263. hostdata->dma_busy = 0;
  1264. hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
  1265. AM53C974_write_8(CMDREG, CMDREG_CFIFO);
  1266. DEB(printk("disconnect; issue_queue: 0x%lx, disconnected_queue: 0x%lxn",
  1267.    (long) hostdata->issue_queue, (long) hostdata->disconnected_queue));
  1268. cmd->scsi_done(cmd);
  1269. if (!hostdata->selecting) {
  1270. AM53C974_set_async(instance, cmd->target);
  1271. AM53C974_write_8(CMDREG, CMDREG_ESR);
  1272. } /* allow reselect */
  1273. return;
  1274. EXIT_UNFINISHED:
  1275.     hostdata->msgout[0] = NOP;
  1276. hostdata->sel_cmd = NULL;
  1277. hostdata->connected = NULL;
  1278. hostdata->aborted = 0;
  1279. hostdata->selecting = 0;
  1280. hostdata->disconnecting = 0;
  1281. hostdata->dma_busy = 0;
  1282. DEB(printk("disconnect; issue_queue: 0x%lx, disconnected_queue: 0x%lxn",
  1283.    (long) hostdata->issue_queue, (long) hostdata->disconnected_queue));
  1284. if (!hostdata->selecting) {
  1285. AM53C974_set_async(instance, cmd->target);
  1286. AM53C974_write_8(CMDREG, CMDREG_ESR);
  1287. } /* allow reselect */
  1288. return;
  1289. }
  1290. /************************************************************************** 
  1291. * Function : int AM53C974_sync_neg(struct Scsi_Host *instance, int target, unsigned char *msg)
  1292. *
  1293. * Purpose : setup message string for sync. negotiation
  1294. *
  1295. * Inputs : instance -- which AM53C974
  1296. *          target -- which SCSI target to deal with
  1297. *          msg -- input message string
  1298. * Returns : 0 if parameters accepted or 1 if not accepted
  1299. *
  1300. * Side effects: hostdata is changed
  1301. *
  1302. * Note: we assume here that fastclk is enabled
  1303. **************************************************************************/
  1304. static int AM53C974_sync_neg(struct Scsi_Host *instance, int target, unsigned char *msg)
  1305. {
  1306. AM53C974_local_declare();
  1307. struct AM53C974_hostdata *hostdata = (struct AM53C974_hostdata *) instance->hostdata;
  1308. int period, offset, i, rate, rate_rem;
  1309. AM53C974_setio(instance);
  1310. period = (DEF_CLK * msg[3] * 8 + 1000) / 2000;
  1311. if (period < MIN_PERIOD) {
  1312. period = MIN_PERIOD;
  1313. hostdata->msgout[3] = period / 4;
  1314. } else if (period > MAX_PERIOD) {
  1315. period = MAX_PERIOD;
  1316. hostdata->msgout[3] = period / 4;
  1317. } else
  1318. hostdata->msgout[3] = msg[3];
  1319. offset = msg[4];
  1320. if (offset > MAX_OFFSET)
  1321. offset = MAX_OFFSET;
  1322. hostdata->msgout[4] = offset;
  1323. hostdata->sync_per[target] = period;
  1324. hostdata->sync_off[target] = offset;
  1325. for (i = 0; i < 3; i++)
  1326. hostdata->msgout[i] = msg[i];
  1327. if ((hostdata->msgout[3] != msg[3]) || (msg[4] != offset))
  1328. return (1);
  1329. rate = DEF_CLK / period;
  1330. rate_rem = 10 * (DEF_CLK - period * rate) / period;
  1331. if (offset)
  1332. printk("ntarget %d: rate=%d.%d Mhz, synchronous, sync offset=%d bytesn",
  1333.        target, rate, rate_rem, offset);
  1334. else
  1335. printk("ntarget %d: rate=%d.%d Mhz, asynchronousn", target, rate, rate_rem);
  1336. return (0);
  1337. }
  1338. /************************************************************************** 
  1339. * Function : AM53C974_set_async(struct Scsi_Host *instance, int target)
  1340. *
  1341. * Purpose : put controller into async. mode
  1342. *
  1343. * Inputs : instance -- which AM53C974
  1344. *          target -- which SCSI target to deal with
  1345. * Returns : nothing
  1346. **************************************************************************/
  1347. static __inline__ void AM53C974_set_async(struct Scsi_Host *instance, int target)
  1348. {
  1349. AM53C974_local_declare();
  1350. struct AM53C974_hostdata *hostdata = (struct AM53C974_hostdata *) instance->hostdata;
  1351. AM53C974_setio(instance);
  1352. AM53C974_write_8(STPREG, hostdata->sync_per[target]);
  1353. AM53C974_write_8(SOFREG, (DEF_SOF_RAD << 6) | (DEF_SOF_RAA << 4));
  1354. }
  1355. /************************************************************************** 
  1356. * Function : AM53C974_set_sync(struct Scsi_Host *instance, int target)
  1357. *
  1358. * Purpose : put controller into sync. mode
  1359. *
  1360. * Inputs : instance -- which AM53C974
  1361. *          target -- which SCSI target to deal with
  1362. * Returns : nothing
  1363. **************************************************************************/
  1364. static __inline__ void AM53C974_set_sync(struct Scsi_Host *instance, int target)
  1365. {
  1366. AM53C974_local_declare();
  1367. struct AM53C974_hostdata *hostdata = (struct AM53C974_hostdata *) instance->hostdata;
  1368. AM53C974_setio(instance);
  1369. AM53C974_write_8(STPREG, hostdata->sync_per[target]);
  1370. AM53C974_write_8(SOFREG, (SOFREG_SO & hostdata->sync_off[target]) |
  1371.  (DEF_SOF_RAD << 6) | (DEF_SOF_RAA << 4));
  1372. }
  1373. /***********************************************************************
  1374. * Function : AM53C974_information_transfer(struct Scsi_Host *instance, *
  1375. *                          unsigned char statreg, unsigned char isreg, *
  1376. *                         unsigned char instreg, unsigned char cfifo,  *
  1377. *                         unsigned char dmastatus)                     *
  1378. *                                                                      *
  1379. * Purpose : handle phase changes                                       *
  1380. *                                                                      *
  1381. * Inputs : instance - which AM53C974                                   *
  1382. *          statreg - status register                                     *
  1383. *          isreg - internal state register                             *
  1384. *          instreg - interrupt status register                         *
  1385. *          cfifo - number of bytes in FIFO                             *
  1386. *          dmastatus - dma status register                             *
  1387. *                                                                      *
  1388. * Returns : nothing                                                    *
  1389. ************************************************************************/
  1390. static void AM53C974_information_transfer(struct Scsi_Host *instance,
  1391.       unsigned char statreg, unsigned char isreg,
  1392.       unsigned char instreg, unsigned char cfifo,
  1393.   unsigned char dmastatus)
  1394. {
  1395. AM53C974_local_declare();
  1396. struct AM53C974_hostdata *hostdata = (struct AM53C974_hostdata *) instance->hostdata;
  1397. Scsi_Cmnd *cmd = (Scsi_Cmnd *) hostdata->connected;
  1398. int ret, i, len, residual = -1;
  1399. AM53C974_setio(instance);
  1400. DEB_INFO(printk(SEPARATOR_LINE));
  1401. switch (statreg & STATREG_PHASE) { /* scsi phase */
  1402. case PHASE_DATAOUT:
  1403.     DEB_INFO(printk("Dataout phase; cmd=0x%lx, sel_cmd=0x%lx, this_residual=%d, buffers_residual=%dn",
  1404.     (long) hostdata->connected, (long) hostdata->sel_cmd, cmd->SCp.this_residual, cmd->SCp.buffers_residual));
  1405. cmd->SCp.phase = PHASE_DATAOUT;
  1406. goto PHASE_DATA_IO;
  1407. case PHASE_DATAIN:
  1408.     DEB_INFO(printk("Datain phase; cmd=0x%lx, sel_cmd=0x%lx, this_residual=%d, buffers_residual=%dn",
  1409.     (long) hostdata->connected, (long) hostdata->sel_cmd, cmd->SCp.this_residual, cmd->SCp.buffers_residual));
  1410. cmd->SCp.phase = PHASE_DATAIN;
  1411. PHASE_DATA_IO:
  1412.     if (hostdata->aborted) {
  1413. AM53C974_write_8(DMACMD, DMACMD_IDLE);
  1414. AM53C974_write_8(CMDREG, CMDREG_CFIFO);
  1415. AM53C974_write_8(CMDREG, CMDREG_SATN);
  1416. return;
  1417. }
  1418. if ((!cmd->SCp.this_residual) && cmd->SCp.buffers_residual) {
  1419. cmd->SCp.buffer++;
  1420. cmd->SCp.buffers_residual--;
  1421. cmd->SCp.ptr = (unsigned char *) cmd->SCp.buffer->address;
  1422. cmd->SCp.this_residual = cmd->SCp.buffer->length;
  1423. }
  1424. if (cmd->SCp.this_residual) {
  1425. if (!(AM53C974_read_8(DMACMD) & DMACMD_START)) {
  1426. hostdata->dma_busy = 0;
  1427. AM53C974_transfer_dma(instance, statreg & STATREG_IO,
  1428.   (unsigned long) cmd->SCp.this_residual,
  1429.       cmd->SCp.ptr);
  1430. } else
  1431. hostdata->dma_busy = 1;
  1432. }
  1433. return;
  1434. case PHASE_MSGIN:
  1435.     DEB_INFO(printk("Message-In phase; cmd=0x%lx, sel_cmd=0x%lxn",
  1436.   (long) hostdata->connected, (long) hostdata->sel_cmd));
  1437. AM53C974_set_async(instance, cmd->target);
  1438. if (cmd->SCp.phase == PHASE_DATAIN)
  1439. AM53C974_dma_blast(instance, dmastatus, statreg);
  1440. if ((cmd->SCp.phase == PHASE_DATAOUT) && (AM53C974_read_8(DMACMD) & DMACMD_START)) {
  1441. AM53C974_write_8(DMACMD, DMACMD_IDLE);
  1442. residual = cfifo + (AM53C974_read_8(CTCLREG) | (AM53C974_read_8(CTCMREG) << 8) |
  1443.        (AM53C974_read_8(CTCHREG) << 16));
  1444. cmd->SCp.ptr += cmd->SCp.this_residual - residual;
  1445. cmd->SCp.this_residual = residual;
  1446. if (cfifo) {
  1447. AM53C974_write_8(CMDREG, CMDREG_CFIFO);
  1448. cfifo = 0;
  1449. }
  1450. }
  1451. if (cmd->SCp.phase == PHASE_STATIN) {
  1452. while ((AM53C974_read_8(CFIREG) & CFIREG_CF) < 2);
  1453. cmd->SCp.Status = AM53C974_read_8(FFREG);
  1454. cmd->SCp.Message = AM53C974_read_8(FFREG);
  1455. DEB_INFO(printk("Message-In phase; status=0x%02x, message=0x%02xn",
  1456.      cmd->SCp.Status, cmd->SCp.Message));
  1457. ret = AM53C974_message(instance, cmd, cmd->SCp.Message);
  1458. } else {
  1459. if (!cfifo) {
  1460. AM53C974_write_8(CMDREG, CMDREG_IT);
  1461. AM53C974_poll_int();
  1462. cmd->SCp.Message = AM53C974_read_8(FFREG);
  1463. }
  1464. ret = AM53C974_message(instance, cmd, cmd->SCp.Message);
  1465. }
  1466. cmd->SCp.phase = PHASE_MSGIN;
  1467. AM53C974_set_sync(instance, cmd->target);
  1468. break;
  1469. case PHASE_MSGOUT:
  1470.     DEB_INFO(printk("Message-Out phase; cfifo=%d; msgout[0]=0x%02xn",
  1471.     AM53C974_read_8(CFIREG) & CFIREG_CF, hostdata->msgout[0]));
  1472. AM53C974_write_8(DMACMD, DMACMD_IDLE);
  1473. AM53C974_set_async(instance, cmd->target);
  1474. for (i = 0; i < sizeof(hostdata->last_message); i++)
  1475. hostdata->last_message[i] = hostdata->msgout[i];
  1476. if ((hostdata->msgout[0] == 0) || INSIDE(hostdata->msgout[0], 0x02, 0x1F) ||
  1477.     INSIDE(hostdata->msgout[0], 0x80, 0xFF))
  1478. len = 1;
  1479. else {
  1480. if (hostdata->msgout[0] == EXTENDED_MESSAGE) {
  1481. #ifdef AM53C974_DEBUG_INFO
  1482. printk("Extended message dump:n");
  1483. for (i = 0; i < hostdata->msgout[1] + 2; i++) {
  1484. printk("%02x ", hostdata->msgout[i]);
  1485. if (i && !(i % 16))
  1486. printk("n");
  1487. }
  1488. printk("n");
  1489. #endif
  1490. len = hostdata->msgout[1] + 2;
  1491. } else
  1492. len = 2;
  1493. }
  1494. for (i = 0; i < len; i++)
  1495. AM53C974_write_8(FFREG, hostdata->msgout[i]);
  1496. AM53C974_write_8(CMDREG, CMDREG_IT);
  1497. cmd->SCp.phase = PHASE_MSGOUT;
  1498. hostdata->msgout[0] = NOP;
  1499. AM53C974_set_sync(instance, cmd->target);
  1500. break;
  1501. case PHASE_CMDOUT:
  1502.     DEB_INFO(printk("Command-Out phasen"));
  1503. AM53C974_set_async(instance, cmd->target);
  1504. for (i = 0; i < cmd->cmd_len; i++)
  1505. AM53C974_write_8(FFREG, cmd->cmnd[i]);
  1506. AM53C974_write_8(CMDREG, CMDREG_IT);
  1507. cmd->SCp.phase = PHASE_CMDOUT;
  1508. AM53C974_set_sync(instance, cmd->target);
  1509. break;
  1510. case PHASE_STATIN:
  1511.     DEB_INFO(printk("Status phasen"));
  1512. if (cmd->SCp.phase == PHASE_DATAIN)
  1513. AM53C974_dma_blast(instance, dmastatus, statreg);
  1514. AM53C974_set_async(instance, cmd->target);
  1515. if (cmd->SCp.phase == PHASE_DATAOUT) {
  1516. unsigned long residual;
  1517. if (AM53C974_read_8(DMACMD) & DMACMD_START) {
  1518. AM53C974_write_8(DMACMD, DMACMD_IDLE);
  1519. residual = cfifo + (AM53C974_read_8(CTCLREG) | (AM53C974_read_8(CTCMREG) << 8) |
  1520.        (AM53C974_read_8(CTCHREG) << 16));
  1521. cmd->SCp.ptr += cmd->SCp.this_residual - residual;
  1522. cmd->SCp.this_residual = residual;
  1523. }
  1524. if (cfifo) {
  1525. AM53C974_write_8(CMDREG, CMDREG_CFIFO);
  1526. cfifo = 0;
  1527. }
  1528. }
  1529. cmd->SCp.phase = PHASE_STATIN;
  1530. AM53C974_write_8(CMDREG, CMDREG_ICCS); /* command complete */
  1531. break;
  1532. case PHASE_RES_0:
  1533.     case PHASE_RES_1:
  1534. #ifdef AM53C974_DEBUG
  1535.     deb_stop = 1;
  1536. #endif
  1537. DEB_INFO(printk("Reserved phasen"));
  1538. break;
  1539. }
  1540. KEYWAIT();
  1541. }
  1542. /******************************************************************************
  1543. * Function : int AM53C974_message(struct Scsi_Host *instance, Scsi_Cmnd *cmd,
  1544. *                                 unsigned char msg)                
  1545. *
  1546. * Purpose : handle SCSI messages
  1547. *
  1548. * Inputs : instance -- which AM53C974
  1549. *          cmd -- SCSI command the message belongs to
  1550. *          msg -- message id byte
  1551. * Returns : 1 on success, 0 on failure.
  1552. **************************************************************************/
  1553. static int AM53C974_message(struct Scsi_Host *instance, Scsi_Cmnd * cmd,
  1554.     unsigned char msg)
  1555. {
  1556. AM53C974_local_declare();
  1557. static unsigned char extended_msg[10];
  1558. unsigned char statreg;
  1559. int len, ret = 0;
  1560. unsigned char *p;
  1561. #ifdef AM53C974_DEBUG_MSG
  1562. int j;
  1563. #endif
  1564. struct AM53C974_hostdata *hostdata = (struct AM53C974_hostdata *) instance->hostdata;
  1565. AM53C974_setio(instance);
  1566. DEB_MSG(printk(SEPARATOR_LINE));
  1567. /* Linking lets us reduce the time required to get the 
  1568.  * next command out to the device, hopefully this will
  1569.  * mean we don't waste another revolution due to the delays
  1570.  * required by ARBITRATION and another SELECTION.
  1571.  * In the current implementation proposal, low level drivers
  1572.  * merely have to start the next command, pointed to by 
  1573.  * next_link, done() is called as with unlinked commands. */
  1574. switch (msg) {
  1575. #ifdef LINKED
  1576. case LINKED_CMD_COMPLETE:
  1577.     case LINKED_FLG_CMD_COMPLETE:
  1578. /* Accept message by releasing ACK */
  1579.     DEB_LINKED(printk("scsi%d : target %d lun %d linked command complete.n",
  1580.       instance->host_no, cmd->target, cmd->lun));
  1581. /* Sanity check : A linked command should only terminate with
  1582.  * one of these messages if there are more linked commands available. */
  1583. if (!cmd->next_link) {
  1584. printk("scsi%d : target %d lun %d linked command complete, no next_linkn"
  1585.        instance->host_no, cmd->target, cmd->lun);
  1586. hostdata->aborted = 1;
  1587. AM53C974_write_8(CMDREG, CMDREG_SATN);
  1588. AM53C974_write_8(CMDREG, CMDREG_MA);
  1589. break;
  1590. }
  1591. if (hostdata->aborted) {
  1592. DEB_ABORT(printk("ATN set for cmnd %d upon reception of LINKED_CMD_COMPLETE or"
  1593.  "LINKED_FLG_CMD_COMPLETE messagen", cmd->cmnd[0]));
  1594. AM53C974_write_8(CMDREG, CMDREG_SATN);
  1595. }
  1596. AM53C974_write_8(CMDREG, CMDREG_MA);
  1597. initialize_SCp(cmd->next_link);
  1598. /* The next command is still part of this process */
  1599. cmd->next_link->tag = cmd->tag;
  1600. cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
  1601. DEB_LINKED(printk("scsi%d : target %d lun %d linked request done, calling scsi_done().n",
  1602.       instance->host_no, cmd->target, cmd->lun));
  1603. cmd->scsi_done(cmd);
  1604. cmd = hostdata->connected;
  1605. break;
  1606. #endif /* def LINKED */
  1607. case ABORT:
  1608.     case COMMAND_COMPLETE:
  1609.     DEB_MSG(printk("scsi%d: command complete message received; cmd %d for target %d, lun %dn",
  1610. instance->host_no, cmd->cmnd[0], cmd->target, cmd->lun));
  1611. hostdata->disconnecting = 1;
  1612. cmd->device->disconnect = 0;
  1613. /* I'm not sure what the correct thing to do here is : 
  1614.  * If the command that just executed is NOT a request 
  1615.  * sense, the obvious thing to do is to set the result
  1616.  * code to the values of the stored parameters.
  1617.  * If it was a REQUEST SENSE command, we need some way 
  1618.  * to differentiate between the failure code of the original
  1619.  * and the failure code of the REQUEST sense - the obvious
  1620.  * case is success, where we fall through and leave the result
  1621.  * code unchanged.
  1622.  * 
  1623.  * The non-obvious place is where the REQUEST SENSE failed  */
  1624. if (cmd->cmnd[0] != REQUEST_SENSE)
  1625. cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
  1626. else if (cmd->SCp.Status != GOOD)
  1627. cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
  1628. if (hostdata->aborted) {
  1629. AM53C974_write_8(CMDREG, CMDREG_SATN);
  1630. AM53C974_write_8(CMDREG, CMDREG_MA);
  1631. DEB_ABORT(printk("ATN set for cmnd %d upon reception of ABORT or"
  1632.     "COMMAND_COMPLETE messagen", cmd->cmnd[0]));
  1633. break;
  1634. }
  1635. if ((cmd->cmnd[0] != REQUEST_SENSE) && (cmd->SCp.Status == CHECK_CONDITION)) {
  1636. DEB_MSG(printk("scsi%d : performing request sensen", instance->host_no));
  1637. cmd->cmnd[0] = REQUEST_SENSE;
  1638. cmd->cmnd[1] &= 0xe0;
  1639. cmd->cmnd[2] = 0;
  1640. cmd->cmnd[3] = 0;
  1641. cmd->cmnd[4] = sizeof(cmd->sense_buffer);
  1642. cmd->cmnd[5] = 0;
  1643. cmd->SCp.buffer = NULL;
  1644. cmd->SCp.buffers_residual = 0;
  1645. cmd->SCp.ptr = (char *) cmd->sense_buffer;
  1646. cmd->SCp.this_residual = sizeof(cmd->sense_buffer);
  1647. LIST(cmd, hostdata->issue_queue);
  1648. cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
  1649. hostdata->issue_queue = (Scsi_Cmnd *) cmd;
  1650. DEB_MSG(printk("scsi%d : REQUEST SENSE added to head of issue queuen", instance->host_no));
  1651. }
  1652. /* Accept message by clearing ACK */
  1653. AM53C974_write_8(CMDREG, CMDREG_MA);
  1654. break;
  1655. case MESSAGE_REJECT:
  1656.     DEB_MSG(printk("scsi%d: reject message received; cmd %d for target %d, lun %dn",
  1657. instance->host_no, cmd->cmnd[0], cmd->target, cmd->lun));
  1658. switch (hostdata->last_message[0]) {
  1659. case EXTENDED_MESSAGE:
  1660.     if (hostdata->last_message[2] == EXTENDED_SDTR) {
  1661. /* sync. negotiation was rejected, setup asynchronous transfer with target */
  1662. printk("ntarget %d: rate=%d Mhz, asynchronous (sync. negotiation rejected)n",
  1663.        cmd->target, DEF_CLK / DEF_STP);
  1664. hostdata->sync_off[cmd->target] = 0;
  1665. hostdata->sync_per[cmd->target] = DEF_STP;
  1666. }
  1667. break;
  1668. case HEAD_OF_QUEUE_TAG:
  1669.     case ORDERED_QUEUE_TAG:
  1670.     case SIMPLE_QUEUE_TAG:
  1671.     cmd->device->tagged_queue = 0;
  1672. hostdata->busy[cmd->target] |= (1 << cmd->lun);
  1673. break;
  1674. default:
  1675.     break;
  1676. }
  1677. if (hostdata->aborted)
  1678. AM53C974_write_8(CMDREG, CMDREG_SATN);
  1679. AM53C974_write_8(CMDREG, CMDREG_MA);
  1680. break;
  1681. case DISCONNECT:
  1682.     DEB_MSG(printk("scsi%d: disconnect message received; cmd %d for target %d, lun %dn",
  1683. instance->host_no, cmd->cmnd[0], cmd->target, cmd->lun));
  1684. cmd->device->disconnect = 1;
  1685. hostdata->disconnecting = 1;
  1686. AM53C974_write_8(CMDREG, CMDREG_MA); /* Accept message by clearing ACK */
  1687. break;
  1688. case SAVE_POINTERS:
  1689.     case RESTORE_POINTERS:
  1690.     DEB_MSG(printk("scsi%d: save/restore pointers message received; cmd %d for target %d, lun %dn",
  1691. instance->host_no, cmd->cmnd[0], cmd->target, cmd->lun));
  1692. /* The SCSI data pointer is *IMPLICITLY* saved on a disconnect
  1693.  * operation, in violation of the SCSI spec so we can safely 
  1694.  * ignore SAVE/RESTORE pointers calls.
  1695.  *
  1696.  * Unfortunately, some disks violate the SCSI spec and 
  1697.  * don't issue the required SAVE_POINTERS message before
  1698.  * disconnecting, and we have to break spec to remain 
  1699.  * compatible. */
  1700. if (hostdata->aborted) {
  1701. DEB_ABORT(printk("ATN set for cmnd %d upon reception of SAVE/REST. POINTERS messagen",
  1702.  cmd->cmnd[0]));
  1703. AM53C974_write_8(CMDREG, CMDREG_SATN);
  1704. }
  1705. AM53C974_write_8(CMDREG, CMDREG_MA);
  1706. break;
  1707. case EXTENDED_MESSAGE:
  1708.     DEB_MSG(printk("scsi%d: extended message received; cmd %d for target %d, lun %dn",
  1709. instance->host_no, cmd->cmnd[0], cmd->target, cmd->lun));
  1710. /* Extended messages are sent in the following format :
  1711.  * Byte    
  1712.  * 0           EXTENDED_MESSAGE == 1
  1713.  * 1           length (includes one byte for code, doesn't include first two bytes)
  1714.  * 2           code
  1715.  * 3..length+1 arguments
  1716.  */
  1717. /* BEWARE!! THIS CODE IS EXTREMELY UGLY */
  1718. extended_msg[0] = EXTENDED_MESSAGE;
  1719. AM53C974_read_8(INSTREG); /* clear int */
  1720. AM53C974_write_8(CMDREG, CMDREG_MA); /* ack. msg byte, then wait for SO */
  1721. AM53C974_poll_int();
  1722. /* get length */
  1723. AM53C974_write_8(CMDREG, CMDREG_IT);
  1724. AM53C974_poll_int();
  1725. AM53C974_write_8(CMDREG, CMDREG_MA); /* ack. msg byte, then wait for SO */
  1726. AM53C974_poll_int();
  1727. extended_msg[1] = len = AM53C974_read_8(FFREG); /* get length */
  1728. p = extended_msg + 2;
  1729. /* read the remaining (len) bytes */
  1730. while (len) {
  1731. AM53C974_write_8(CMDREG, CMDREG_IT);
  1732. AM53C974_poll_int();
  1733. if (len > 1) {
  1734. AM53C974_write_8(CMDREG, CMDREG_MA); /* ack. msg byte, then wait for SO */
  1735. AM53C974_poll_int();
  1736. }
  1737. *p = AM53C974_read_8(FFREG);
  1738. p++;
  1739. len--;
  1740. }
  1741. #ifdef AM53C974_DEBUG_MSG
  1742. printk("scsi%d: received extended message: ", instance->host_no);
  1743. for (j = 0; j < extended_msg[1] + 2; j++) {
  1744. printk("0x%02x ", extended_msg[j]);
  1745. if (j && !(j % 16))
  1746. printk("n");
  1747. }
  1748. printk("n");
  1749. #endif
  1750. /* check message */
  1751. if (extended_msg[2] == EXTENDED_SDTR)
  1752. ret = AM53C974_sync_neg(instance, cmd->target, extended_msg);
  1753. if (ret || hostdata->aborted)
  1754. AM53C974_write_8(CMDREG, CMDREG_SATN);
  1755. AM53C974_write_8(CMDREG, CMDREG_MA);
  1756. break;
  1757. default:
  1758.     printk("scsi%d: unknown message 0x%02x receivedn", instance->host_no, msg);
  1759. #ifdef AM53C974_DEBUG
  1760. deb_stop = 1;
  1761. #endif
  1762. /* reject message */
  1763. hostdata->msgout[0] = MESSAGE_REJECT;
  1764. AM53C974_write_8(CMDREG, CMDREG_SATN);
  1765. AM53C974_write_8(CMDREG, CMDREG_MA);
  1766. return (0);
  1767. break;
  1768. } /* switch (msg) */
  1769. KEYWAIT();
  1770. return (1);
  1771. }
  1772. /************************************************************************** 
  1773. * Function : AM53C974_select(struct Scsi_Host *instance, Scsi_Cmnd *cmd, int tag)
  1774. *
  1775. * Purpose : try to establish nexus for the command;
  1776. *           start sync negotiation via start stop and transfer the command in 
  1777. *           cmdout phase in case of an inquiry or req. sense command with no 
  1778. *           sync. neg. performed yet
  1779. *
  1780. * Inputs : instance -- which AM53C974
  1781. *          cmd -- command which requires the selection
  1782. *          tag -- tagged queueing
  1783. * Returns : nothing
  1784. *        
  1785. * Note: this function initializes the selection process, which is continued 
  1786. *       in the interrupt handler
  1787. **************************************************************************/
  1788. static void AM53C974_select(struct Scsi_Host *instance, Scsi_Cmnd * cmd, int tag)
  1789. {
  1790. AM53C974_local_declare();
  1791. struct AM53C974_hostdata *hostdata = (struct AM53C974_hostdata *) instance->hostdata;
  1792. unsigned char cfifo, tmp[3];
  1793. unsigned int i, len, cmd_size = COMMAND_SIZE(cmd->cmnd[0]);
  1794. AM53C974_setio(instance);
  1795. cfifo = AM53C974_cfifo();
  1796. if (cfifo) {
  1797. printk("scsi%d: select error; %d residual bytes in FIFOn", instance->host_no, cfifo);
  1798. AM53C974_write_8(CMDREG, CMDREG_CFIFO); /* clear FIFO */
  1799. }
  1800. #ifdef AM53C974_PROHIBIT_DISCONNECT
  1801. tmp[0] = IDENTIFY(0, cmd->lun);
  1802. #else
  1803. tmp[0] = IDENTIFY(1, cmd->lun);
  1804. #endif
  1805. #ifdef SCSI2
  1806. if (cmd->device->tagged_queue && (tag != TAG_NONE)) {
  1807. tmp[1] = SIMPLE_QUEUE_TAG;
  1808. if (tag == TAG_NEXT) {
  1809. /* 0 is TAG_NONE, used to imply no tag for this command */
  1810. if (cmd->device->current_tag == 0)
  1811. cmd->device->current_tag = 1;
  1812. cmd->tag = cmd->device->current_tag;
  1813. cmd->device->current_tag++;
  1814. } else
  1815. cmd->tag = (unsigned char) tag;
  1816. tmp[2] = cmd->tag;
  1817. hostdata->last_message[0] = SIMPLE_QUEUE_TAG;
  1818. len = 3;
  1819. AM53C974_write_8(FFREG, tmp[0]);
  1820. AM53C974_write_8(FFREG, tmp[1]);
  1821. AM53C974_write_8(FFREG, tmp[2]);
  1822. } else
  1823. #endif /* def SCSI2 */
  1824. {
  1825. len = 1;
  1826. AM53C974_write_8(FFREG, tmp[0]);
  1827. cmd->tag = 0;
  1828. }
  1829. /* in case of an inquiry or req. sense command with no sync. neg performed yet, we start
  1830.    sync negotiation via start stops and transfer the command in cmdout phase */
  1831. if (((cmd->cmnd[0] == INQUIRY) || (cmd->cmnd[0] == REQUEST_SENSE)) &&
  1832.     !(hostdata->sync_neg[cmd->target]) && hostdata->sync_en[cmd->target]) {
  1833. hostdata->sync_neg[cmd->target] = 1;
  1834. hostdata->msgout[0] = EXTENDED_MESSAGE;
  1835. hostdata->msgout[1] = 3;
  1836. hostdata->msgout[2] = EXTENDED_SDTR;
  1837. hostdata->msgout[3] = 250 / (int) hostdata->max_rate[cmd->target];
  1838. hostdata->msgout[4] = hostdata->max_offset[cmd->target];
  1839. len += 5;
  1840. }
  1841. AM53C974_write_8(SDIDREG, SDIREG_MASK & cmd->target); /* setup dest. id  */
  1842. AM53C974_write_8(STIMREG, DEF_SCSI_TIMEOUT); /* setup timeout reg */
  1843. switch (len) {
  1844. case 1:
  1845. for (i = 0; i < cmd_size; i++)
  1846. AM53C974_write_8(FFREG, cmd->cmnd[i]);
  1847. AM53C974_write_8(CMDREG, CMDREG_SAS); /* select with ATN, 1 msg byte */
  1848. hostdata->msgout[0] = NOP;
  1849. break;
  1850. case 3:
  1851. for (i = 0; i < cmd_size; i++)
  1852. AM53C974_write_8(FFREG, cmd->cmnd[i]);
  1853. AM53C974_write_8(CMDREG, CMDREG_SA3S); /* select with ATN, 3 msg bytes */
  1854. hostdata->msgout[0] = NOP;
  1855. break;
  1856. default:
  1857.     AM53C974_write_8(CMDREG, CMDREG_SASS); /* select with ATN, stop steps; continue in message out phase */
  1858. break;
  1859. }
  1860. }
  1861. /************************************************************************** 
  1862. * Function : AM53C974_intr_select(struct Scsi_Host *instance, unsigned char statreg)
  1863. *
  1864. * Purpose : handle reselection 
  1865. *
  1866. * Inputs : instance -- which AM53C974
  1867. *          statreg -- status register
  1868. * Returns : nothing
  1869. *
  1870. * side effects: manipulates hostdata
  1871. **************************************************************************/
  1872. static void AM53C974_intr_reselect(struct Scsi_Host *instance, unsigned char statreg)
  1873. {
  1874. AM53C974_local_declare();
  1875. struct AM53C974_hostdata *hostdata = (struct AM53C974_hostdata *) instance->hostdata;
  1876. unsigned char cfifo, msg[3], lun, t, target = 0;
  1877. #ifdef SCSI2
  1878. unsigned char tag;
  1879. #endif
  1880. Scsi_Cmnd *tmp = NULL, *prev;
  1881. AM53C974_setio(instance);
  1882. cfifo = AM53C974_cfifo();
  1883. if (hostdata->selecting) {
  1884. /* caught reselect interrupt in selection process;
  1885.    put selecting command back into the issue queue and continue with the
  1886.    reselecting command */
  1887. DEB_RESEL(printk("AM53C974_intr_reselect: in selection processn"));
  1888. LIST(hostdata->sel_cmd, hostdata->issue_queue);
  1889. hostdata->sel_cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
  1890. hostdata->issue_queue = hostdata->sel_cmd;
  1891. hostdata->sel_cmd = NULL;
  1892. hostdata->selecting = 0;
  1893. }
  1894. /* 2 bytes must be in the FIFO now */
  1895. if (cfifo != 2) {
  1896. printk("scsi %d: error: %d bytes in fifo, 2 expectedn", instance->host_no, cfifo);
  1897. hostdata->aborted = 1;
  1898. goto EXIT_ABORT;
  1899. }
  1900. /* determine target which reselected */
  1901. t = AM53C974_read_8(FFREG);
  1902. if (!(t & (1 << instance->this_id))) {
  1903. printk("scsi %d: error: invalid host idn", instance->host_no);
  1904. hostdata->aborted = 1;
  1905. goto EXIT_ABORT;
  1906. }
  1907. t ^= (1 << instance->this_id);
  1908. target = 0;
  1909. while (t != 1) {
  1910. t >>= 1;
  1911. target++;
  1912. }
  1913. DEB_RESEL(printk("scsi %d: reselect; target: %dn", instance->host_no, target));
  1914. if (hostdata->aborted)
  1915. goto EXIT_ABORT;
  1916. if ((statreg & STATREG_PHASE) != PHASE_MSGIN) {
  1917. printk("scsi %d: error: upon reselection interrupt not in MSGINn", instance->host_no);
  1918. hostdata->aborted = 1;
  1919. goto EXIT_ABORT;
  1920. }
  1921. msg[0] = AM53C974_read_8(FFREG);
  1922. if (!msg[0] & 0x80) {
  1923. printk("scsi%d: error: expecting IDENTIFY message, got ", instance->host_no);
  1924. print_msg(msg);
  1925. hostdata->aborted = 1;
  1926. goto EXIT_ABORT;
  1927. }
  1928. lun = (msg[0] & 0x07);
  1929. /* We need to add code for SCSI-II to track which devices have
  1930.  * I_T_L_Q nexuses established, and which have simple I_T_L
  1931.  * nexuses so we can chose to do additional data transfer. */
  1932. #ifdef SCSI2
  1933. #error "SCSI-II tagged queueing is not supported yet"
  1934. #endif
  1935. /* Find the command corresponding to the I_T_L or I_T_L_Q  nexus we 
  1936.  * just reestablished, and remove it from the disconnected queue. */
  1937. for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue, prev = NULL;
  1938.      tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble)
  1939. if ((target == tmp->target) && (lun == tmp->lun)
  1940. #ifdef SCSI2
  1941.     && (tag == tmp->tag)
  1942. #endif
  1943.     ) {
  1944. if (prev) {
  1945. REMOVE(prev, (Scsi_Cmnd *) (prev->host_scribble), tmp,
  1946.      (Scsi_Cmnd *) (tmp->host_scribble));
  1947. prev->host_scribble = tmp->host_scribble;
  1948. } else {
  1949. REMOVE(-1, hostdata->disconnected_queue, tmp, tmp->host_scribble);
  1950. hostdata->disconnected_queue = (Scsi_Cmnd *) tmp->host_scribble;
  1951. }
  1952. tmp->host_scribble = NULL;
  1953. hostdata->connected = tmp;
  1954. break;
  1955. }
  1956. if (!tmp) {
  1957. #ifdef SCSI2
  1958. printk("scsi%d: warning : target %d lun %d tag %d not in disconnect_queue.n",
  1959.        instance->host_no, target, lun, tag);
  1960. #else
  1961. printk("scsi%d: warning : target %d lun %d not in disconnect_queue.n",
  1962.        instance->host_no, target, lun);
  1963. #endif
  1964. /* Since we have an established nexus that we can't do anything with, we must abort it. */
  1965. hostdata->aborted = 1;
  1966. DEB(AM53C974_keywait());
  1967. goto EXIT_ABORT;
  1968. } else
  1969. goto EXIT_OK;
  1970. EXIT_ABORT:
  1971.     AM53C974_write_8(CMDREG, CMDREG_SATN);
  1972. AM53C974_write_8(CMDREG, CMDREG_MA);
  1973. return;
  1974. EXIT_OK:
  1975.     DEB_RESEL(printk("scsi%d: nexus established, target = %d, lun = %d, tag = %dn",
  1976.  instance->host_no, target, tmp->lun, tmp->tag));
  1977. AM53C974_set_sync(instance, target);
  1978. AM53C974_write_8(SDIDREG, SDIREG_MASK & target); /* setup dest. id  */
  1979. AM53C974_write_8(CMDREG, CMDREG_MA);
  1980. hostdata->dma_busy = 0;
  1981. hostdata->connected->SCp.phase = PHASE_CMDOUT;
  1982. }
  1983. /************************************************************************** 
  1984. * Function : AM53C974_transfer_dma(struct Scsi_Host *instance, short dir,
  1985. *                                  unsigned long length, char *data)
  1986. *
  1987. * Purpose : setup DMA transfer
  1988. *
  1989. * Inputs : instance -- which AM53C974
  1990. *          dir -- direction flag, 0: write to device, read from memory; 
  1991. *                                 1: read from device, write to memory
  1992. *          length -- number of bytes to transfer to from buffer
  1993. *          data -- pointer to data buffer
  1994. * Returns : nothing
  1995. **************************************************************************/
  1996. static __inline__ void AM53C974_transfer_dma(struct Scsi_Host *instance, short dir,
  1997. unsigned long length, char *data)
  1998. {
  1999. AM53C974_local_declare();
  2000. AM53C974_setio(instance);
  2001. AM53C974_write_8(CMDREG, CMDREG_NOP);
  2002. AM53C974_write_8(DMACMD, (dir << 7) | DMACMD_INTE_D); /* idle command */
  2003. AM53C974_write_8(STCLREG, (unsigned char) (length & 0xff));
  2004. AM53C974_write_8(STCMREG, (unsigned char) ((length & 0xff00) >> 8));
  2005. AM53C974_write_8(STCHREG, (unsigned char) ((length & 0xff0000) >> 16));
  2006. AM53C974_write_32(DMASTC, length & 0xffffff);
  2007. AM53C974_write_32(DMASPA, virt_to_bus(data));
  2008. AM53C974_write_8(CMDREG, CMDREG_IT | CMDREG_DMA);
  2009. AM53C974_write_8(DMACMD, (dir << 7) | DMACMD_INTE_D | DMACMD_START);
  2010. }
  2011. /************************************************************************** 
  2012. * Function : AM53C974_dma_blast(struct Scsi_Host *instance, unsigned char dmastatus,
  2013. *                               unsigned char statreg)
  2014. *
  2015. * Purpose : cleanup DMA transfer
  2016. *
  2017. * Inputs : instance -- which AM53C974
  2018. *          dmastatus -- dma status register
  2019. *          statreg -- status register
  2020. * Returns : nothing
  2021. **************************************************************************/
  2022. static void AM53C974_dma_blast(struct Scsi_Host *instance, unsigned char dmastatus,
  2023.        unsigned char statreg)
  2024. {
  2025. AM53C974_local_declare();
  2026. struct AM53C974_hostdata *hostdata = (struct AM53C974_hostdata *) instance->hostdata;
  2027. unsigned long ctcreg;
  2028. int dir = statreg & STATREG_IO;
  2029. int cfifo, pio, i = 0;
  2030. AM53C974_setio(instance);
  2031. do {
  2032. cfifo = AM53C974_cfifo();
  2033. i++;
  2034. } while (cfifo && (i < 50000));
  2035. pio = (i == 50000) ? 1 : 0;
  2036. if (statreg & STATREG_CTZ) {
  2037. AM53C974_write_8(DMACMD, DMACMD_IDLE);
  2038. return;
  2039. }
  2040. if (dmastatus & DMASTATUS_DONE) {
  2041. AM53C974_write_8(DMACMD, DMACMD_IDLE);
  2042. return;
  2043. }
  2044. AM53C974_write_8(DMACMD, ((dir << 7) & DMACMD_DIR) | DMACMD_BLAST);
  2045. while (!(AM53C974_read_8(DMASTATUS) & DMASTATUS_BCMPLT));
  2046. AM53C974_write_8(DMACMD, DMACMD_IDLE);
  2047. if (pio) {
  2048. /* transfer residual bytes via PIO */
  2049. unsigned char *wac = (unsigned char *) AM53C974_read_32(DMAWAC);
  2050. printk("pio mode, residual=%dn", AM53C974_read_8(CFIREG) & CFIREG_CF);
  2051. while (AM53C974_read_8(CFIREG) & CFIREG_CF)
  2052. *(wac++) = AM53C974_read_8(FFREG);
  2053. }
  2054. ctcreg = AM53C974_read_8(CTCLREG) | (AM53C974_read_8(CTCMREG) << 8) |
  2055.     (AM53C974_read_8(CTCHREG) << 16);
  2056. hostdata->connected->SCp.ptr += hostdata->connected->SCp.this_residual - ctcreg;
  2057. hostdata->connected->SCp.this_residual = ctcreg;
  2058. }
  2059. /************************************************************************** 
  2060. * Function : AM53C974_intr_bus_reset(struct Scsi_Host *instance)
  2061. *
  2062. * Purpose : handle bus reset interrupt
  2063. *
  2064. * Inputs : instance -- which AM53C974
  2065. * Returns : nothing
  2066. **************************************************************************/
  2067. static void AM53C974_intr_bus_reset(struct Scsi_Host *instance)
  2068. {
  2069. AM53C974_local_declare();
  2070. unsigned char cntlreg1;
  2071. AM53C974_setio(instance);
  2072. AM53C974_write_8(CMDREG, CMDREG_CFIFO);
  2073. AM53C974_write_8(CMDREG, CMDREG_NOP);
  2074. cntlreg1 = AM53C974_read_8(CNTLREG1);
  2075. AM53C974_write_8(CNTLREG1, cntlreg1 | CNTLREG1_DISR);
  2076. }
  2077. /**************************************************************************
  2078. * Function : int AM53C974_abort(Scsi_Cmnd *cmd)
  2079. *
  2080. * Purpose : abort a command
  2081. *
  2082. * Inputs : cmd - the Scsi_Cmnd to abort, code - code to set the 
  2083. *  host byte of the result field to, if zero DID_ABORTED is 
  2084. * used.
  2085. *
  2086. * Returns : 0 - success, -1 on failure.
  2087.  **************************************************************************/
  2088. static int AM53C974_abort(Scsi_Cmnd * cmd)
  2089. {
  2090. AM53C974_local_declare();
  2091. unsigned long flags;
  2092. struct Scsi_Host *instance = cmd->host;
  2093. struct AM53C974_hostdata *hostdata = (struct AM53C974_hostdata *) instance->hostdata;
  2094. Scsi_Cmnd *tmp, **prev;
  2095. #ifdef AM53C974_DEBUG
  2096. deb_stop = 1;
  2097. #endif
  2098. save_flags(flags);
  2099. cli();
  2100. AM53C974_setio(instance);
  2101. DEB_ABORT(printk(SEPARATOR_LINE));
  2102. DEB_ABORT(printk("scsi%d : AM53C974_abort called -- trouble starts!!n", instance->host_no));
  2103. DEB_ABORT(AM53C974_print(instance));
  2104. DEB_ABORT(AM53C974_keywait());
  2105. /* Case 1 : If the command is the currently executing command, 
  2106.    we'll set the aborted flag and return control so that the
  2107.    information transfer routine can exit cleanly. */
  2108. if ((hostdata->connected == cmd) || (hostdata->sel_cmd == cmd)) {
  2109. DEB_ABORT(printk("scsi%d: aborting connected commandn", instance->host_no));
  2110. hostdata->aborted = 1;
  2111. hostdata->msgout[0] = ABORT;
  2112. restore_flags(flags);
  2113. return (SCSI_ABORT_PENDING);
  2114. }
  2115. /* Case 2 : If the command hasn't been issued yet,
  2116.    we simply remove it from the issue queue. */
  2117. for (prev = (Scsi_Cmnd **) & (hostdata->issue_queue),
  2118.      tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp;
  2119.      prev = (Scsi_Cmnd **) & (tmp->host_scribble),
  2120.      tmp = (Scsi_Cmnd *) tmp->host_scribble) {
  2121. if (cmd == tmp) {
  2122. DEB_ABORT(printk("scsi%d : abort removed command from issue queue.n", instance->host_no));
  2123. REMOVE(5, *prev, tmp, tmp->host_scribble);
  2124. (*prev) = (Scsi_Cmnd *) tmp->host_scribble;
  2125. tmp->host_scribble = NULL;
  2126. tmp->result = DID_ABORT << 16;
  2127. restore_flags(flags);
  2128. tmp->done(tmp);
  2129. return (SCSI_ABORT_SUCCESS);
  2130. }
  2131. #ifdef AM53C974_DEBUG_ABORT
  2132. else {
  2133. if (prev == (Scsi_Cmnd **) tmp)
  2134. printk("scsi%d : LOOPn", instance->host_no);
  2135. }
  2136. #endif
  2137. }
  2138. /* Case 3 : If any commands are connected, we're going to fail the abort
  2139.  *        and let the high level SCSI driver retry at a later time or 
  2140.  *          issue a reset.
  2141.  *
  2142.  *          Timeouts, and therefore aborted commands, will be highly unlikely
  2143.  *          and handling them cleanly in this situation would make the common
  2144.  *          case of noresets less efficient, and would pollute our code.  So,
  2145.  *          we fail. */
  2146. if (hostdata->connected || hostdata->sel_cmd) {
  2147. DEB_ABORT(printk("scsi%d : abort failed, other command connected.n", instance->host_no));
  2148. restore_flags(flags);
  2149. return (SCSI_ABORT_NOT_RUNNING);
  2150. }
  2151. /* Case 4: If the command is currently disconnected from the bus, and 
  2152.  *       there are no connected commands, we reconnect the I_T_L or 
  2153.  *         I_T_L_Q nexus associated with it, go into message out, and send 
  2154.  *         an abort message. */
  2155. for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp;
  2156.      tmp = (Scsi_Cmnd *) tmp->host_scribble) {
  2157. if (cmd == tmp) {
  2158. DEB_ABORT(printk("scsi%d: aborting disconnected commandn", instance->host_no));
  2159. hostdata->aborted = 1;
  2160. hostdata->msgout[0] = ABORT;
  2161. hostdata->selecting = 1;
  2162. hostdata->sel_cmd = tmp;
  2163. AM53C974_write_8(CMDREG, CMDREG_DSR);
  2164. restore_flags(flags);
  2165. return (SCSI_ABORT_PENDING);
  2166. }
  2167. }
  2168. /* Case 5 : If we reached this point, the command was not found in any of 
  2169.  *        the queues.
  2170.  *
  2171.  * We probably reached this point because of an unlikely race condition
  2172.  * between the command completing successfully and the abortion code,
  2173.  * so we won't panic, but we will notify the user in case something really
  2174.  * broke. */
  2175. DEB_ABORT(printk("scsi%d : abort failed, command not found.n", instance->host_no));
  2176. restore_flags(flags);
  2177. return (SCSI_ABORT_NOT_RUNNING);
  2178. }
  2179. /************************************************************************** 
  2180. * Function : int AM53C974_reset(Scsi_Cmnd *cmd)
  2181. *
  2182. * Purpose : reset the SCSI controller and bus
  2183. *
  2184. * Inputs : cmd -- which command within the command block was responsible for the reset
  2185. * Returns : status (SCSI_ABORT_SUCCESS)
  2186. * FIXME(eric) the reset_flags are ignored.
  2187. **************************************************************************/
  2188. static int AM53C974_reset(Scsi_Cmnd * cmd, unsigned int reset_flags)
  2189. {
  2190. AM53C974_local_declare();
  2191. unsigned long flags;
  2192. int i;
  2193. struct Scsi_Host *instance = cmd->host;
  2194. struct AM53C974_hostdata *hostdata = (struct AM53C974_hostdata *) instance->hostdata;
  2195. AM53C974_setio(instance);
  2196. save_flags(flags);
  2197. cli();
  2198. DEB(printk("AM53C974_reset called; "));
  2199. printk("AM53C974_reset calledn");
  2200. AM53C974_print(instance);
  2201. AM53C974_keywait();
  2202. /* do hard reset */
  2203. AM53C974_write_8(CMDREG, CMDREG_RDEV);
  2204. AM53C974_write_8(CMDREG, CMDREG_NOP);
  2205. hostdata->msgout[0] = NOP;
  2206. for (i = 0; i < 8; i++) {
  2207. hostdata->busy[i] = 0;
  2208. hostdata->sync_per[i] = DEF_STP;
  2209. hostdata->sync_off[i] = 0;
  2210. hostdata->sync_neg[i] = 0;
  2211. }
  2212. hostdata->last_message[0] = NOP;
  2213. hostdata->sel_cmd = NULL;
  2214. hostdata->connected = NULL;
  2215. hostdata->issue_queue = NULL;
  2216. hostdata->disconnected_queue = NULL;
  2217. hostdata->in_reset = 0;
  2218. hostdata->aborted = 0;
  2219. hostdata->selecting = 0;
  2220. hostdata->disconnecting = 0;
  2221. hostdata->dma_busy = 0;
  2222. /* reset bus */
  2223. AM53C974_write_8(CNTLREG1, CNTLREG1_DISR | instance->this_id); /* disable interrupt upon SCSI RESET */
  2224. AM53C974_write_8(CMDREG, CMDREG_RBUS); /* reset SCSI bus */
  2225. udelay(40);
  2226. AM53C974_config_after_reset(instance);
  2227. restore_flags(flags);
  2228. cmd->result = DID_RESET << 16;
  2229. cmd->scsi_done(cmd);
  2230. return SCSI_ABORT_SUCCESS;
  2231. }
  2232. /*
  2233.  * AM53C974_release()
  2234.  *
  2235.  * Release resources allocated for a single AM53C974 adapter.
  2236.  */
  2237. static int AM53C974_release(struct Scsi_Host *shp)
  2238. {
  2239. free_irq(shp->irq, shp);
  2240. scsi_unregister(shp);
  2241. return 0;
  2242. }
  2243. /* You can specify overrides=a,b,c,d in the same format at AM53C974=a,b,c,d
  2244.    on boot up */
  2245. MODULE_PARM(overrides, "1-32i");
  2246. MODULE_LICENSE("GPL");
  2247. static Scsi_Host_Template driver_template = AM53C974;
  2248. #include "scsi_module.c"