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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* $Id: esp.c,v 1.99 2001/02/13 01:17:01 davem Exp $
  2.  * esp.c:  EnhancedScsiProcessor Sun SCSI driver code.
  3.  *
  4.  * Copyright (C) 1995, 1998 David S. Miller (davem@caip.rutgers.edu)
  5.  */
  6. /* TODO:
  7.  *
  8.  * 1) Maybe disable parity checking in config register one for SCSI1
  9.  *    targets.  (Gilmore says parity error on the SBus can lock up
  10.  *    old sun4c's)
  11.  * 2) Add support for DMA2 pipelining.
  12.  * 3) Add tagged queueing.
  13.  */
  14. #include <linux/config.h>
  15. #include <linux/kernel.h>
  16. #include <linux/delay.h>
  17. #include <linux/types.h>
  18. #include <linux/string.h>
  19. #include <linux/slab.h>
  20. #include <linux/blk.h>
  21. #include <linux/proc_fs.h>
  22. #include <linux/stat.h>
  23. #include <linux/init.h>
  24. #include <linux/spinlock.h>
  25. #include "scsi.h"
  26. #include "hosts.h"
  27. #include "esp.h"
  28. #include <asm/sbus.h>
  29. #include <asm/dma.h>
  30. #include <asm/system.h>
  31. #include <asm/ptrace.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/oplib.h>
  34. #include <asm/io.h>
  35. #include <asm/irq.h>
  36. #ifndef __sparc_v9__
  37. #include <asm/machines.h>
  38. #include <asm/idprom.h>
  39. #endif
  40. #include <linux/module.h>
  41. #define DEBUG_ESP
  42. /* #define DEBUG_ESP_HME */
  43. /* #define DEBUG_ESP_DATA */
  44. /* #define DEBUG_ESP_QUEUE */
  45. /* #define DEBUG_ESP_DISCONNECT */
  46. /* #define DEBUG_ESP_STATUS */
  47. /* #define DEBUG_ESP_PHASES */
  48. /* #define DEBUG_ESP_WORKBUS */
  49. /* #define DEBUG_STATE_MACHINE */
  50. /* #define DEBUG_ESP_CMDS */
  51. /* #define DEBUG_ESP_IRQS */
  52. /* #define DEBUG_SDTR */
  53. /* #define DEBUG_ESP_SG */
  54. /* Use the following to sprinkle debugging messages in a way which
  55.  * suits you if combinations of the above become too verbose when
  56.  * trying to track down a specific problem.
  57.  */
  58. /* #define DEBUG_ESP_MISC */
  59. #if defined(DEBUG_ESP)
  60. #define ESPLOG(foo)  printk foo
  61. #else
  62. #define ESPLOG(foo)
  63. #endif /* (DEBUG_ESP) */
  64. #if defined(DEBUG_ESP_HME)
  65. #define ESPHME(foo)  printk foo
  66. #else
  67. #define ESPHME(foo)
  68. #endif
  69. #if defined(DEBUG_ESP_DATA)
  70. #define ESPDATA(foo)  printk foo
  71. #else
  72. #define ESPDATA(foo)
  73. #endif
  74. #if defined(DEBUG_ESP_QUEUE)
  75. #define ESPQUEUE(foo)  printk foo
  76. #else
  77. #define ESPQUEUE(foo)
  78. #endif
  79. #if defined(DEBUG_ESP_DISCONNECT)
  80. #define ESPDISC(foo)  printk foo
  81. #else
  82. #define ESPDISC(foo)
  83. #endif
  84. #if defined(DEBUG_ESP_STATUS)
  85. #define ESPSTAT(foo)  printk foo
  86. #else
  87. #define ESPSTAT(foo)
  88. #endif
  89. #if defined(DEBUG_ESP_PHASES)
  90. #define ESPPHASE(foo)  printk foo
  91. #else
  92. #define ESPPHASE(foo)
  93. #endif
  94. #if defined(DEBUG_ESP_WORKBUS)
  95. #define ESPBUS(foo)  printk foo
  96. #else
  97. #define ESPBUS(foo)
  98. #endif
  99. #if defined(DEBUG_ESP_IRQS)
  100. #define ESPIRQ(foo)  printk foo
  101. #else
  102. #define ESPIRQ(foo)
  103. #endif
  104. #if defined(DEBUG_SDTR)
  105. #define ESPSDTR(foo)  printk foo
  106. #else
  107. #define ESPSDTR(foo)
  108. #endif
  109. #if defined(DEBUG_ESP_MISC)
  110. #define ESPMISC(foo)  printk foo
  111. #else
  112. #define ESPMISC(foo)
  113. #endif
  114. /* Command phase enumeration. */
  115. enum {
  116. not_issued    = 0x00,  /* Still in the issue_SC queue.          */
  117. /* Various forms of selecting a target. */
  118. #define in_slct_mask    0x10
  119. in_slct_norm  = 0x10,  /* ESP is arbitrating, normal selection  */
  120. in_slct_stop  = 0x11,  /* ESP will select, then stop with IRQ   */
  121. in_slct_msg   = 0x12,  /* select, then send a message           */
  122. in_slct_tag   = 0x13,  /* select and send tagged queue msg      */
  123. in_slct_sneg  = 0x14,  /* select and acquire sync capabilities  */
  124. /* Any post selection activity. */
  125. #define in_phases_mask  0x20
  126. in_datain     = 0x20,  /* Data is transferring from the bus     */
  127. in_dataout    = 0x21,  /* Data is transferring to the bus       */
  128. in_data_done  = 0x22,  /* Last DMA data operation done (maybe)  */
  129. in_msgin      = 0x23,  /* Eating message from target            */
  130. in_msgincont  = 0x24,  /* Eating more msg bytes from target     */
  131. in_msgindone  = 0x25,  /* Decide what to do with what we got    */
  132. in_msgout     = 0x26,  /* Sending message to target             */
  133. in_msgoutdone = 0x27,  /* Done sending msg out                  */
  134. in_cmdbegin   = 0x28,  /* Sending cmd after abnormal selection  */
  135. in_cmdend     = 0x29,  /* Done sending slow cmd                 */
  136. in_status     = 0x2a,  /* Was in status phase, finishing cmd    */
  137. in_freeing    = 0x2b,  /* freeing the bus for cmd cmplt or disc */
  138. in_the_dark   = 0x2c,  /* Don't know what bus phase we are in   */
  139. /* Special states, ie. not normal bus transitions... */
  140. #define in_spec_mask    0x80
  141. in_abortone   = 0x80,  /* Aborting one command currently        */
  142. in_abortall   = 0x81,  /* Blowing away all commands we have     */
  143. in_resetdev   = 0x82,  /* SCSI target reset in progress         */
  144. in_resetbus   = 0x83,  /* SCSI bus reset in progress            */
  145. in_tgterror   = 0x84,  /* Target did something stupid           */
  146. };
  147. enum {
  148. /* Zero has special meaning, see skipahead[12]. */
  149. /*0*/ do_never,
  150. /*1*/ do_phase_determine,
  151. /*2*/ do_reset_bus,
  152. /*3*/ do_reset_complete,
  153. /*4*/ do_work_bus,
  154. /*5*/ do_intr_end
  155. };
  156. /* The master ring of all esp hosts we are managing in this driver. */
  157. static struct esp *espchain;
  158. static spinlock_t espchain_lock = SPIN_LOCK_UNLOCKED;
  159. static int esps_running = 0;
  160. /* Forward declarations. */
  161. static void esp_intr(int irq, void *dev_id, struct pt_regs *pregs);
  162. /* Debugging routines */
  163. struct esp_cmdstrings {
  164. u8 cmdchar;
  165. char *text;
  166. } esp_cmd_strings[] = {
  167. /* Miscellaneous */
  168. { ESP_CMD_NULL, "ESP_NOP", },
  169. { ESP_CMD_FLUSH, "FIFO_FLUSH", },
  170. { ESP_CMD_RC, "RSTESP", },
  171. { ESP_CMD_RS, "RSTSCSI", },
  172. /* Disconnected State Group */
  173. { ESP_CMD_RSEL, "RESLCTSEQ", },
  174. { ESP_CMD_SEL, "SLCTNATN", },
  175. { ESP_CMD_SELA, "SLCTATN", },
  176. { ESP_CMD_SELAS, "SLCTATNSTOP", },
  177. { ESP_CMD_ESEL, "ENSLCTRESEL", },
  178. { ESP_CMD_DSEL, "DISSELRESEL", },
  179. { ESP_CMD_SA3, "SLCTATN3", },
  180. { ESP_CMD_RSEL3, "RESLCTSEQ", },
  181. /* Target State Group */
  182. { ESP_CMD_SMSG, "SNDMSG", },
  183. { ESP_CMD_SSTAT, "SNDSTATUS", },
  184. { ESP_CMD_SDATA, "SNDDATA", },
  185. { ESP_CMD_DSEQ, "DISCSEQ", },
  186. { ESP_CMD_TSEQ, "TERMSEQ", },
  187. { ESP_CMD_TCCSEQ, "TRGTCMDCOMPSEQ", },
  188. { ESP_CMD_DCNCT, "DISC", },
  189. { ESP_CMD_RMSG, "RCVMSG", },
  190. { ESP_CMD_RCMD, "RCVCMD", },
  191. { ESP_CMD_RDATA, "RCVDATA", },
  192. { ESP_CMD_RCSEQ, "RCVCMDSEQ", },
  193. /* Initiator State Group */
  194. { ESP_CMD_TI, "TRANSINFO", },
  195. { ESP_CMD_ICCSEQ, "INICMDSEQCOMP", },
  196. { ESP_CMD_MOK, "MSGACCEPTED", },
  197. { ESP_CMD_TPAD, "TPAD", },
  198. { ESP_CMD_SATN, "SATN", },
  199. { ESP_CMD_RATN, "RATN", },
  200. };
  201. #define NUM_ESP_COMMANDS  ((sizeof(esp_cmd_strings)) / (sizeof(struct esp_cmdstrings)))
  202. /* Print textual representation of an ESP command */
  203. static inline void esp_print_cmd(u8 espcmd)
  204. {
  205. u8 dma_bit = espcmd & ESP_CMD_DMA;
  206. int i;
  207. espcmd &= ~dma_bit;
  208. for (i = 0; i < NUM_ESP_COMMANDS; i++)
  209. if (esp_cmd_strings[i].cmdchar == espcmd)
  210. break;
  211. if (i == NUM_ESP_COMMANDS)
  212. printk("ESP_Unknown");
  213. else
  214. printk("%s%s", esp_cmd_strings[i].text,
  215.        ((dma_bit) ? "+DMA" : ""));
  216. }
  217. /* Print the status register's value */
  218. static inline void esp_print_statreg(u8 statreg)
  219. {
  220. u8 phase;
  221. printk("STATUS<");
  222. phase = statreg & ESP_STAT_PMASK;
  223. printk("%s,", (phase == ESP_DOP ? "DATA-OUT" :
  224.        (phase == ESP_DIP ? "DATA-IN" :
  225. (phase == ESP_CMDP ? "COMMAND" :
  226.  (phase == ESP_STATP ? "STATUS" :
  227.   (phase == ESP_MOP ? "MSG-OUT" :
  228.    (phase == ESP_MIP ? "MSG_IN" :
  229.     "unknown")))))));
  230. if (statreg & ESP_STAT_TDONE)
  231. printk("TRANS_DONE,");
  232. if (statreg & ESP_STAT_TCNT)
  233. printk("TCOUNT_ZERO,");
  234. if (statreg & ESP_STAT_PERR)
  235. printk("P_ERROR,");
  236. if (statreg & ESP_STAT_SPAM)
  237. printk("SPAM,");
  238. if (statreg & ESP_STAT_INTR)
  239. printk("IRQ,");
  240. printk(">");
  241. }
  242. /* Print the interrupt register's value */
  243. static inline void esp_print_ireg(u8 intreg)
  244. {
  245. printk("INTREG< ");
  246. if (intreg & ESP_INTR_S)
  247. printk("SLCT_NATN ");
  248. if (intreg & ESP_INTR_SATN)
  249. printk("SLCT_ATN ");
  250. if (intreg & ESP_INTR_RSEL)
  251. printk("RSLCT ");
  252. if (intreg & ESP_INTR_FDONE)
  253. printk("FDONE ");
  254. if (intreg & ESP_INTR_BSERV)
  255. printk("BSERV ");
  256. if (intreg & ESP_INTR_DC)
  257. printk("DISCNCT ");
  258. if (intreg & ESP_INTR_IC)
  259. printk("ILL_CMD ");
  260. if (intreg & ESP_INTR_SR)
  261. printk("SCSI_BUS_RESET ");
  262. printk(">");
  263. }
  264. /* Print the sequence step registers contents */
  265. static inline void esp_print_seqreg(u8 stepreg)
  266. {
  267. stepreg &= ESP_STEP_VBITS;
  268. printk("STEP<%s>",
  269.        (stepreg == ESP_STEP_ASEL ? "SLCT_ARB_CMPLT" :
  270. (stepreg == ESP_STEP_SID ? "1BYTE_MSG_SENT" :
  271.  (stepreg == ESP_STEP_NCMD ? "NOT_IN_CMD_PHASE" :
  272.   (stepreg == ESP_STEP_PPC ? "CMD_BYTES_LOST" :
  273.    (stepreg == ESP_STEP_FINI4 ? "CMD_SENT_OK" :
  274.     "UNKNOWN"))))));
  275. }
  276. static char *phase_string(int phase)
  277. {
  278. switch (phase) {
  279. case not_issued:
  280. return "UNISSUED";
  281. case in_slct_norm:
  282. return "SLCTNORM";
  283. case in_slct_stop:
  284. return "SLCTSTOP";
  285. case in_slct_msg:
  286. return "SLCTMSG";
  287. case in_slct_tag:
  288. return "SLCTTAG";
  289. case in_slct_sneg:
  290. return "SLCTSNEG";
  291. case in_datain:
  292. return "DATAIN";
  293. case in_dataout:
  294. return "DATAOUT";
  295. case in_data_done:
  296. return "DATADONE";
  297. case in_msgin:
  298. return "MSGIN";
  299. case in_msgincont:
  300. return "MSGINCONT";
  301. case in_msgindone:
  302. return "MSGINDONE";
  303. case in_msgout:
  304. return "MSGOUT";
  305. case in_msgoutdone:
  306. return "MSGOUTDONE";
  307. case in_cmdbegin:
  308. return "CMDBEGIN";
  309. case in_cmdend:
  310. return "CMDEND";
  311. case in_status:
  312. return "STATUS";
  313. case in_freeing:
  314. return "FREEING";
  315. case in_the_dark:
  316. return "CLUELESS";
  317. case in_abortone:
  318. return "ABORTONE";
  319. case in_abortall:
  320. return "ABORTALL";
  321. case in_resetdev:
  322. return "RESETDEV";
  323. case in_resetbus:
  324. return "RESETBUS";
  325. case in_tgterror:
  326. return "TGTERROR";
  327. default:
  328. return "UNKNOWN";
  329. };
  330. }
  331. #ifdef DEBUG_STATE_MACHINE
  332. static inline void esp_advance_phase(Scsi_Cmnd *s, int newphase)
  333. {
  334. ESPLOG(("<%s>", phase_string(newphase)));
  335. s->SCp.sent_command = s->SCp.phase;
  336. s->SCp.phase = newphase;
  337. }
  338. #else
  339. #define esp_advance_phase(__s, __newphase) 
  340. (__s)->SCp.sent_command = (__s)->SCp.phase; 
  341. (__s)->SCp.phase = (__newphase);
  342. #endif
  343. #ifdef DEBUG_ESP_CMDS
  344. static inline void esp_cmd(struct esp *esp, u8 cmd)
  345. {
  346. esp->espcmdlog[esp->espcmdent] = cmd;
  347. esp->espcmdent = (esp->espcmdent + 1) & 31;
  348. sbus_writeb(cmd, esp->eregs + ESP_CMD);
  349. }
  350. #else
  351. #define esp_cmd(__esp, __cmd)
  352. sbus_writeb((__cmd), ((__esp)->eregs) + ESP_CMD)
  353. #endif
  354. #define ESP_INTSOFF(__dregs)
  355. sbus_writel(sbus_readl((__dregs)+DMA_CSR)&~(DMA_INT_ENAB), (__dregs)+DMA_CSR)
  356. #define ESP_INTSON(__dregs)
  357. sbus_writel(sbus_readl((__dregs)+DMA_CSR)|DMA_INT_ENAB, (__dregs)+DMA_CSR)
  358. #define ESP_IRQ_P(__dregs)
  359. (sbus_readl((__dregs)+DMA_CSR) & (DMA_HNDL_INTR|DMA_HNDL_ERROR))
  360. /* How we use the various Linux SCSI data structures for operation.
  361.  *
  362.  * struct scsi_cmnd:
  363.  *
  364.  *   We keep track of the synchronous capabilities of a target
  365.  *   in the device member, using sync_min_period and
  366.  *   sync_max_offset.  These are the values we directly write
  367.  *   into the ESP registers while running a command.  If offset
  368.  *   is zero the ESP will use asynchronous transfers.
  369.  *   If the borken flag is set we assume we shouldn't even bother
  370.  *   trying to negotiate for synchronous transfer as this target
  371.  *   is really stupid.  If we notice the target is dropping the
  372.  *   bus, and we have been allowing it to disconnect, we clear
  373.  *   the disconnect flag.
  374.  */
  375. /* Manipulation of the ESP command queues.  Thanks to the aha152x driver
  376.  * and its author, Juergen E. Fischer, for the methods used here.
  377.  * Note that these are per-ESP queues, not global queues like
  378.  * the aha152x driver uses.
  379.  */
  380. static inline void append_SC(Scsi_Cmnd **SC, Scsi_Cmnd *new_SC)
  381. {
  382. Scsi_Cmnd *end;
  383. new_SC->host_scribble = (unsigned char *) NULL;
  384. if (!*SC)
  385. *SC = new_SC;
  386. else {
  387. for (end=*SC;end->host_scribble;end=(Scsi_Cmnd *)end->host_scribble)
  388. ;
  389. end->host_scribble = (unsigned char *) new_SC;
  390. }
  391. }
  392. static inline void prepend_SC(Scsi_Cmnd **SC, Scsi_Cmnd *new_SC)
  393. {
  394. new_SC->host_scribble = (unsigned char *) *SC;
  395. *SC = new_SC;
  396. }
  397. static inline Scsi_Cmnd *remove_first_SC(Scsi_Cmnd **SC)
  398. {
  399. Scsi_Cmnd *ptr;
  400. ptr = *SC;
  401. if (ptr)
  402. *SC = (Scsi_Cmnd *) (*SC)->host_scribble;
  403. return ptr;
  404. }
  405. static inline Scsi_Cmnd *remove_SC(Scsi_Cmnd **SC, int target, int lun)
  406. {
  407. Scsi_Cmnd *ptr, *prev;
  408. for (ptr = *SC, prev = NULL;
  409.      ptr && ((ptr->target != target) || (ptr->lun != lun));
  410.      prev = ptr, ptr = (Scsi_Cmnd *) ptr->host_scribble)
  411. ;
  412. if (ptr) {
  413. if (prev)
  414. prev->host_scribble=ptr->host_scribble;
  415. else
  416. *SC=(Scsi_Cmnd *)ptr->host_scribble;
  417. }
  418. return ptr;
  419. }
  420. /* Resetting various pieces of the ESP scsi driver chipset/buses. */
  421. static void esp_reset_dma(struct esp *esp)
  422. {
  423. unsigned long flags;
  424. int can_do_burst16, can_do_burst32, can_do_burst64;
  425. int can_do_sbus64;
  426. u32 tmp;
  427. can_do_burst16 = (esp->bursts & DMA_BURST16) != 0;
  428. can_do_burst32 = (esp->bursts & DMA_BURST32) != 0;
  429. can_do_burst64 = 0;
  430. can_do_sbus64 = 0;
  431. if (sbus_can_dma_64bit(esp->sdev))
  432. can_do_sbus64 = 1;
  433. if (sbus_can_burst64(esp->sdev))
  434. can_do_burst64 = (esp->bursts & DMA_BURST64) != 0;
  435. /* Punt the DVMA into a known state. */
  436. if (esp->dma->revision != dvmahme) {
  437. tmp = sbus_readl(esp->dregs + DMA_CSR);
  438. sbus_writel(tmp | DMA_RST_SCSI, esp->dregs + DMA_CSR);
  439. sbus_writel(tmp & ~DMA_RST_SCSI, esp->dregs + DMA_CSR);
  440. }
  441. switch (esp->dma->revision) {
  442. case dvmahme:
  443. /* This is the HME DVMA gate array. */
  444. save_flags(flags); cli(); /* I really hate this chip. */
  445. sbus_writel(DMA_RESET_FAS366, esp->dregs + DMA_CSR);
  446. sbus_writel(DMA_RST_SCSI, esp->dregs + DMA_CSR);
  447. esp->prev_hme_dmacsr = (DMA_PARITY_OFF|DMA_2CLKS|DMA_SCSI_DISAB|DMA_INT_ENAB);
  448. esp->prev_hme_dmacsr &= ~(DMA_ENABLE|DMA_ST_WRITE|DMA_BRST_SZ);
  449. if (can_do_burst64)
  450. esp->prev_hme_dmacsr |= DMA_BRST64;
  451. else if (can_do_burst32)
  452. esp->prev_hme_dmacsr |= DMA_BRST32;
  453. if (can_do_sbus64) {
  454. esp->prev_hme_dmacsr |= DMA_SCSI_SBUS64;
  455. sbus_set_sbus64(esp->sdev, esp->bursts);
  456. }
  457. /* This chip is horrible. */
  458. while (sbus_readl(esp->dregs + DMA_CSR) & DMA_PEND_READ)
  459. udelay(1);
  460. sbus_writel(0, esp->dregs + DMA_CSR);
  461. sbus_writel(esp->prev_hme_dmacsr, esp->dregs + DMA_CSR);
  462. /* This is necessary to avoid having the SCSI channel
  463.  * engine lock up on us.
  464.  */
  465. sbus_writel(0, esp->dregs + DMA_ADDR);
  466. restore_flags(flags);
  467. break;
  468. case dvmarev2:
  469. /* This is the gate array found in the sun4m
  470.  * NCR SBUS I/O subsystem.
  471.  */
  472. if (esp->erev != esp100) {
  473. tmp = sbus_readl(esp->dregs + DMA_CSR);
  474. sbus_writel(tmp | DMA_3CLKS, esp->dregs + DMA_CSR);
  475. }
  476. break;
  477. case dvmarev3:
  478. tmp = sbus_readl(esp->dregs + DMA_CSR);
  479. tmp &= ~DMA_3CLKS;
  480. tmp |= DMA_2CLKS;
  481. if (can_do_burst32) {
  482. tmp &= ~DMA_BRST_SZ;
  483. tmp |= DMA_BRST32;
  484. }
  485. sbus_writel(tmp, esp->dregs + DMA_CSR);
  486. break;
  487. case dvmaesc1:
  488. /* This is the DMA unit found on SCSI/Ether cards. */
  489. tmp = sbus_readl(esp->dregs + DMA_CSR);
  490. tmp |= DMA_ADD_ENABLE;
  491. tmp &= ~DMA_BCNT_ENAB;
  492. if (!can_do_burst32 && can_do_burst16) {
  493. tmp |= DMA_ESC_BURST;
  494. } else {
  495. tmp &= ~(DMA_ESC_BURST);
  496. }
  497. sbus_writel(tmp, esp->dregs + DMA_CSR);
  498. break;
  499. default:
  500. break;
  501. };
  502. ESP_INTSON(esp->dregs);
  503. }
  504. /* Reset the ESP chip, _not_ the SCSI bus. */
  505. static void __init esp_reset_esp(struct esp *esp)
  506. {
  507. u8 family_code, version;
  508. int i;
  509. /* Now reset the ESP chip */
  510. esp_cmd(esp, ESP_CMD_RC);
  511. esp_cmd(esp, ESP_CMD_NULL | ESP_CMD_DMA);
  512. esp_cmd(esp, ESP_CMD_NULL | ESP_CMD_DMA);
  513. /* Reload the configuration registers */
  514. sbus_writeb(esp->cfact, esp->eregs + ESP_CFACT);
  515. esp->prev_stp = 0;
  516. sbus_writeb(esp->prev_stp, esp->eregs + ESP_STP);
  517. esp->prev_soff = 0;
  518. sbus_writeb(esp->prev_soff, esp->eregs + ESP_SOFF);
  519. sbus_writeb(esp->neg_defp, esp->eregs + ESP_TIMEO);
  520. /* This is the only point at which it is reliable to read
  521.  * the ID-code for a fast ESP chip variants.
  522.  */
  523. esp->max_period = ((35 * esp->ccycle) / 1000);
  524. if (esp->erev == fast) {
  525. version = sbus_readb(esp->eregs + ESP_UID);
  526. family_code = (version & 0xf8) >> 3;
  527. if (family_code == 0x02)
  528. esp->erev = fas236;
  529. else if (family_code == 0x0a)
  530. esp->erev = fashme; /* Version is usually '5'. */
  531. else
  532. esp->erev = fas100a;
  533. ESPMISC(("esp%d: FAST chip is %s (family=%d, version=%d)n",
  534.  esp->esp_id,
  535.  (esp->erev == fas236) ? "fas236" :
  536.  ((esp->erev == fas100a) ? "fas100a" :
  537.   "fasHME"), family_code, (version & 7)));
  538. esp->min_period = ((4 * esp->ccycle) / 1000);
  539. } else {
  540. esp->min_period = ((5 * esp->ccycle) / 1000);
  541. }
  542. esp->max_period = (esp->max_period + 3)>>2;
  543. esp->min_period = (esp->min_period + 3)>>2;
  544. sbus_writeb(esp->config1, esp->eregs + ESP_CFG1);
  545. switch (esp->erev) {
  546. case esp100:
  547. /* nothing to do */
  548. break;
  549. case esp100a:
  550. sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
  551. break;
  552. case esp236:
  553. /* Slow 236 */
  554. sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
  555. esp->prev_cfg3 = esp->config3[0];
  556. sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
  557. break;
  558. case fashme:
  559. esp->config2 |= (ESP_CONFIG2_HME32 | ESP_CONFIG2_HMEFENAB);
  560. /* fallthrough... */
  561. case fas236:
  562. /* Fast 236 or HME */
  563. sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
  564. for (i = 0; i < 16; i++) {
  565. if (esp->erev == fashme) {
  566. u8 cfg3;
  567. cfg3 = ESP_CONFIG3_FCLOCK | ESP_CONFIG3_OBPUSH;
  568. if (esp->scsi_id >= 8)
  569. cfg3 |= ESP_CONFIG3_IDBIT3;
  570. esp->config3[i] |= cfg3;
  571. } else {
  572. esp->config3[i] |= ESP_CONFIG3_FCLK;
  573. }
  574. }
  575. esp->prev_cfg3 = esp->config3[0];
  576. sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
  577. if (esp->erev == fashme) {
  578. esp->radelay = 80;
  579. } else {
  580. if (esp->diff)
  581. esp->radelay = 0;
  582. else
  583. esp->radelay = 96;
  584. }
  585. break;
  586. case fas100a:
  587. /* Fast 100a */
  588. sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
  589. for (i = 0; i < 16; i++)
  590. esp->config3[i] |= ESP_CONFIG3_FCLOCK;
  591. esp->prev_cfg3 = esp->config3[0];
  592. sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
  593. esp->radelay = 32;
  594. break;
  595. default:
  596. panic("esp: what could it be... I wonder...");
  597. break;
  598. };
  599. /* Eat any bitrot in the chip */
  600. sbus_readb(esp->eregs + ESP_INTRPT);
  601. udelay(100);
  602. }
  603. /* This places the ESP into a known state at boot time. */
  604. static void __init esp_bootup_reset(struct esp *esp)
  605. {
  606. u8 tmp;
  607. /* Reset the DMA */
  608. esp_reset_dma(esp);
  609. /* Reset the ESP */
  610. esp_reset_esp(esp);
  611. /* Reset the SCSI bus, but tell ESP not to generate an irq */
  612. tmp = sbus_readb(esp->eregs + ESP_CFG1);
  613. tmp |= ESP_CONFIG1_SRRDISAB;
  614. sbus_writeb(tmp, esp->eregs + ESP_CFG1);
  615. esp_cmd(esp, ESP_CMD_RS);
  616. udelay(400);
  617. sbus_writeb(esp->config1, esp->eregs + ESP_CFG1);
  618. /* Eat any bitrot in the chip and we are done... */
  619. sbus_readb(esp->eregs + ESP_INTRPT);
  620. }
  621. static void esp_chain_add(struct esp *esp)
  622. {
  623. spin_lock_irq(&espchain_lock);
  624. if (espchain) {
  625. struct esp *elink = espchain;
  626. while (elink->next)
  627. elink = elink->next;
  628. elink->next = esp;
  629. } else {
  630. espchain = esp;
  631. }
  632. esp->next = NULL;
  633. spin_unlock_irq(&espchain_lock);
  634. }
  635. static void esp_chain_del(struct esp *esp)
  636. {
  637. spin_lock_irq(&espchain_lock);
  638. if (espchain == esp) {
  639. espchain = esp->next;
  640. } else {
  641. struct esp *elink = espchain;
  642. while (elink->next != esp)
  643. elink = elink->next;
  644. elink->next = esp->next;
  645. }
  646. esp->next = NULL;
  647. spin_unlock_irq(&espchain_lock);
  648. }
  649. static int __init esp_find_dvma(struct esp *esp, struct sbus_dev *dma_sdev)
  650. {
  651. struct sbus_dev *sdev = esp->sdev;
  652. struct sbus_dma *dma;
  653. if (dma_sdev != NULL) {
  654. for_each_dvma(dma) {
  655. if (dma->sdev == dma_sdev)
  656. break;
  657. }
  658. } else {
  659. for_each_dvma(dma) {
  660. /* If allocated already, can't use it. */
  661. if (dma->allocated)
  662. continue;
  663. if (dma->sdev == NULL)
  664. break;
  665. /* If bus + slot are the same and it has the
  666.  * correct OBP name, it's ours.
  667.  */
  668. if (sdev->bus == dma->sdev->bus &&
  669.     sdev->slot == dma->sdev->slot &&
  670.     (!strcmp(dma->sdev->prom_name, "dma") ||
  671.      !strcmp(dma->sdev->prom_name, "espdma")))
  672. break;
  673. }
  674. }
  675. /* If we don't know how to handle the dvma,
  676.  * do not use this device.
  677.  */
  678. if (dma == NULL) {
  679. printk("Cannot find dvma for ESP%d's SCSIn", esp->esp_id);
  680. return -1;
  681. }
  682. if (dma->allocated) {
  683. printk("esp%d: can't use my espdman", esp->esp_id);
  684. return -1;
  685. }
  686. dma->allocated = 1;
  687. esp->dma = dma;
  688. esp->dregs = dma->regs;
  689. return 0;
  690. }
  691. static int __init esp_map_regs(struct esp *esp, int hme)
  692. {
  693. struct sbus_dev *sdev = esp->sdev;
  694. struct resource *res;
  695. /* On HME, two reg sets exist, first is DVMA,
  696.  * second is ESP registers.
  697.  */
  698. if (hme)
  699. res = &sdev->resource[1];
  700. else
  701. res = &sdev->resource[0];
  702. esp->eregs = sbus_ioremap(res, 0, ESP_REG_SIZE, "ESP Registers");
  703. if (esp->eregs == 0)
  704. return -1;
  705. return 0;
  706. }
  707. static int __init esp_map_cmdarea(struct esp *esp)
  708. {
  709. struct sbus_dev *sdev = esp->sdev;
  710. esp->esp_command = sbus_alloc_consistent(sdev, 16,
  711.  &esp->esp_command_dvma);
  712. if (esp->esp_command == NULL ||
  713.     esp->esp_command_dvma == 0)
  714. return -1;
  715. return 0;
  716. }
  717. static int __init esp_register_irq(struct esp *esp)
  718. {
  719. esp->ehost->irq = esp->irq = esp->sdev->irqs[0];
  720. /* We used to try various overly-clever things to
  721.  * reduce the interrupt processing overhead on
  722.  * sun4c/sun4m when multiple ESP's shared the
  723.  * same IRQ.  It was too complex and messy to
  724.  * sanely maintain.
  725.  */
  726. if (request_irq(esp->ehost->irq, esp_intr,
  727. SA_SHIRQ, "ESP SCSI", esp)) {
  728. printk("esp%d: Cannot acquire irq linen",
  729.        esp->esp_id);
  730. return -1;
  731. }
  732. printk("esp%d: IRQ %s ", esp->esp_id,
  733.        __irq_itoa(esp->ehost->irq));
  734. return 0;
  735. }
  736. static void __init esp_get_scsi_id(struct esp *esp)
  737. {
  738. struct sbus_dev *sdev = esp->sdev;
  739. esp->scsi_id = prom_getintdefault(esp->prom_node,
  740.   "initiator-id",
  741.   -1);
  742. if (esp->scsi_id == -1)
  743. esp->scsi_id = prom_getintdefault(esp->prom_node,
  744.   "scsi-initiator-id",
  745.   -1);
  746. if (esp->scsi_id == -1)
  747. esp->scsi_id = (sdev->bus == NULL) ? 7 :
  748. prom_getintdefault(sdev->bus->prom_node,
  749.    "scsi-initiator-id",
  750.    7);
  751. esp->ehost->this_id = esp->scsi_id;
  752. esp->scsi_id_mask = (1 << esp->scsi_id);
  753. }
  754. static void __init esp_get_clock_params(struct esp *esp)
  755. {
  756. struct sbus_dev *sdev = esp->sdev;
  757. int prom_node = esp->prom_node;
  758. int sbus_prom_node;
  759. unsigned int fmhz;
  760. u8 ccf;
  761. if (sdev != NULL && sdev->bus != NULL)
  762. sbus_prom_node = sdev->bus->prom_node;
  763. else
  764. sbus_prom_node = 0;
  765. /* This is getting messy but it has to be done
  766.  * correctly or else you get weird behavior all
  767.  * over the place.  We are trying to basically
  768.  * figure out three pieces of information.
  769.  *
  770.  * a) Clock Conversion Factor
  771.  *
  772.  *    This is a representation of the input
  773.  *    crystal clock frequency going into the
  774.  *    ESP on this machine.  Any operation whose
  775.  *    timing is longer than 400ns depends on this
  776.  *    value being correct.  For example, you'll
  777.  *    get blips for arbitration/selection during
  778.  *    high load or with multiple targets if this
  779.  *    is not set correctly.
  780.  *
  781.  * b) Selection Time-Out
  782.  *
  783.  *    The ESP isn't very bright and will arbitrate
  784.  *    for the bus and try to select a target
  785.  *    forever if you let it.  This value tells
  786.  *    the ESP when it has taken too long to
  787.  *    negotiate and that it should interrupt
  788.  *    the CPU so we can see what happened.
  789.  *    The value is computed as follows (from
  790.  *    NCR/Symbios chip docs).
  791.  *
  792.  *          (Time Out Period) *  (Input Clock)
  793.  *    STO = ----------------------------------
  794.  *          (8192) * (Clock Conversion Factor)
  795.  *
  796.  *    You usually want the time out period to be
  797.  *    around 250ms, I think we'll set it a little
  798.  *    bit higher to account for fully loaded SCSI
  799.  *    bus's and slow devices that don't respond so
  800.  *    quickly to selection attempts. (yeah, I know
  801.  *    this is out of spec. but there is a lot of
  802.  *    buggy pieces of firmware out there so bite me)
  803.  *
  804.  * c) Imperical constants for synchronous offset
  805.  *    and transfer period register values
  806.  *
  807.  *    This entails the smallest and largest sync
  808.  *    period we could ever handle on this ESP.
  809.  */
  810. fmhz = prom_getintdefault(prom_node, "clock-frequency", -1);
  811. if (fmhz == -1)
  812. fmhz = (!sbus_prom_node) ? 0 :
  813. prom_getintdefault(sbus_prom_node, "clock-frequency", -1);
  814. if (fmhz <= (5000000))
  815. ccf = 0;
  816. else
  817. ccf = (((5000000 - 1) + (fmhz))/(5000000));
  818. if (!ccf || ccf > 8) {
  819. /* If we can't find anything reasonable,
  820.  * just assume 20MHZ.  This is the clock
  821.  * frequency of the older sun4c's where I've
  822.  * been unable to find the clock-frequency
  823.  * PROM property.  All other machines provide
  824.  * useful values it seems.
  825.  */
  826. ccf = ESP_CCF_F4;
  827. fmhz = (20000000);
  828. }
  829. if (ccf == (ESP_CCF_F7 + 1))
  830. esp->cfact = ESP_CCF_F0;
  831. else if (ccf == ESP_CCF_NEVER)
  832. esp->cfact = ESP_CCF_F2;
  833. else
  834. esp->cfact = ccf;
  835. esp->raw_cfact = ccf;
  836. esp->cfreq = fmhz;
  837. esp->ccycle = ESP_MHZ_TO_CYCLE(fmhz);
  838. esp->ctick = ESP_TICK(ccf, esp->ccycle);
  839. esp->neg_defp = ESP_NEG_DEFP(fmhz, ccf);
  840. esp->sync_defp = SYNC_DEFP_SLOW;
  841. printk("SCSI ID %d Clk %dMHz CCYC=%d CCF=%d TOut %d ",
  842.        esp->scsi_id, (fmhz / 1000000),
  843.        (int)esp->ccycle, (int)ccf, (int) esp->neg_defp);
  844. }
  845. static void __init esp_get_bursts(struct esp *esp, struct sbus_dev *dma)
  846. {
  847. struct sbus_dev *sdev = esp->sdev;
  848. u8 bursts;
  849. bursts = prom_getintdefault(esp->prom_node, "burst-sizes", 0xff);
  850. if (dma) {
  851. u8 tmp = prom_getintdefault(dma->prom_node,
  852.     "burst-sizes", 0xff);
  853. if (tmp != 0xff)
  854. bursts &= tmp;
  855. }
  856. if (sdev->bus) {
  857. u8 tmp = prom_getintdefault(sdev->bus->prom_node,
  858.     "burst-sizes", 0xff);
  859. if (tmp != 0xff)
  860. bursts &= tmp;
  861. }
  862. if (bursts == 0xff ||
  863.     (bursts & DMA_BURST16) == 0 ||
  864.     (bursts & DMA_BURST32) == 0)
  865. bursts = (DMA_BURST32 - 1);
  866. esp->bursts = bursts;
  867. }
  868. static void __init esp_get_revision(struct esp *esp)
  869. {
  870. u8 tmp;
  871. esp->config1 = (ESP_CONFIG1_PENABLE | (esp->scsi_id & 7));
  872. esp->config2 = (ESP_CONFIG2_SCSI2ENAB | ESP_CONFIG2_REGPARITY);
  873. sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
  874. tmp = sbus_readb(esp->eregs + ESP_CFG2);
  875. tmp &= ~ESP_CONFIG2_MAGIC;
  876. if (tmp != (ESP_CONFIG2_SCSI2ENAB | ESP_CONFIG2_REGPARITY)) {
  877. /* If what we write to cfg2 does not come back, cfg2
  878.  * is not implemented, therefore this must be a plain
  879.  * esp100.
  880.  */
  881. esp->erev = esp100;
  882. printk("NCR53C90(esp100)n");
  883. } else {
  884. esp->config2 = 0;
  885. esp->prev_cfg3 = esp->config3[0] = 5;
  886. sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
  887. sbus_writeb(0, esp->eregs + ESP_CFG3);
  888. sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
  889. tmp = sbus_readb(esp->eregs + ESP_CFG3);
  890. if (tmp != 5) {
  891. /* The cfg2 register is implemented, however
  892.  * cfg3 is not, must be esp100a.
  893.  */
  894. esp->erev = esp100a;
  895. printk("NCR53C90A(esp100a)n");
  896. } else {
  897. int target;
  898. for (target = 0; target < 16; target++)
  899. esp->config3[target] = 0;
  900. esp->prev_cfg3 = 0;
  901. sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
  902. /* All of cfg{1,2,3} implemented, must be one of
  903.  * the fas variants, figure out which one.
  904.  */
  905. if (esp->raw_cfact > ESP_CCF_F5) {
  906. esp->erev = fast;
  907. esp->sync_defp = SYNC_DEFP_FAST;
  908. printk("NCR53C9XF(espfast)n");
  909. } else {
  910. esp->erev = esp236;
  911. printk("NCR53C9x(esp236)n");
  912. }
  913. esp->config2 = 0;
  914. sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
  915. }
  916. }
  917. }
  918. static void __init esp_init_swstate(struct esp *esp)
  919. {
  920. int i;
  921. /* Driver spinlock... */
  922. spin_lock_init(&esp->lock);
  923. /* Command queues... */
  924. esp->current_SC = NULL;
  925. esp->disconnected_SC = NULL;
  926. esp->issue_SC = NULL;
  927. /* Target and current command state... */
  928. esp->targets_present = 0;
  929. esp->resetting_bus = 0;
  930. esp->snip = 0;
  931. /* Debugging... */
  932. for(i = 0; i < 32; i++)
  933. esp->espcmdlog[i] = 0;
  934. esp->espcmdent = 0;
  935. /* MSG phase state... */
  936. for(i = 0; i < 16; i++) {
  937. esp->cur_msgout[i] = 0;
  938. esp->cur_msgin[i] = 0;
  939. }
  940. esp->prevmsgout = esp->prevmsgin = 0;
  941. esp->msgout_len = esp->msgin_len = 0;
  942. /* Clear the one behind caches to hold unmatchable values. */
  943. esp->prev_soff = esp->prev_stp = esp->prev_cfg3 = 0xff;
  944. esp->prev_hme_dmacsr = 0xffffffff;
  945. }
  946. static int __init detect_one_esp(Scsi_Host_Template *tpnt, struct sbus_dev *esp_dev,
  947.  struct sbus_dev *espdma, struct sbus_bus *sbus,
  948.  int id, int hme)
  949. {
  950. struct Scsi_Host *esp_host = scsi_register(tpnt, sizeof(struct esp));
  951. struct esp *esp;
  952. if (!esp_host) {
  953. printk("ESP: Cannot register SCSI hostn");
  954. return -1;
  955. }
  956. if (hme)
  957. esp_host->max_id = 16;
  958. esp = (struct esp *) esp_host->hostdata;
  959. esp->ehost = esp_host;
  960. esp->sdev = esp_dev;
  961. esp->esp_id = id;
  962. esp->prom_node = esp_dev->prom_node;
  963. prom_getstring(esp->prom_node, "name", esp->prom_name,
  964.        sizeof(esp->prom_name));
  965. esp_chain_add(esp);
  966. if (esp_find_dvma(esp, espdma) < 0)
  967. goto fail_unlink;
  968. if (esp_map_regs(esp, hme) < 0) {
  969. printk("ESP registers unmappable");
  970. goto fail_dvma_release;
  971. }
  972. if (esp_map_cmdarea(esp) < 0) {
  973. printk("ESP DVMA transport area unmappable");
  974. goto fail_unmap_regs;
  975. }
  976. if (esp_register_irq(esp) < 0)
  977. goto fail_unmap_cmdarea;
  978. esp_get_scsi_id(esp);
  979. esp->diff = prom_getbool(esp->prom_node, "differential");
  980. if (esp->diff)
  981. printk("Differential ");
  982. esp_get_clock_params(esp);
  983. esp_get_bursts(esp, espdma);
  984. esp_get_revision(esp);
  985. esp_init_swstate(esp);
  986. esp_bootup_reset(esp);
  987. return 0;
  988. fail_unmap_cmdarea:
  989. sbus_free_consistent(esp->sdev, 16,
  990.      (void *) esp->esp_command,
  991.      esp->esp_command_dvma);
  992. fail_unmap_regs:
  993. sbus_iounmap(esp->eregs, ESP_REG_SIZE);
  994. fail_dvma_release:
  995. esp->dma->allocated = 0;
  996. fail_unlink:
  997. esp_chain_del(esp);
  998. scsi_unregister(esp_host);
  999. return -1;
  1000. }
  1001. /* Detecting ESP chips on the machine.  This is the simple and easy
  1002.  * version.
  1003.  */
  1004. #ifdef CONFIG_SUN4
  1005. #include <asm/sun4paddr.h>
  1006. int __init esp_detect(Scsi_Host_Template *tpnt)
  1007. {
  1008. static struct sbus_dev esp_dev;
  1009. int esps_in_use = 0;
  1010. espchain = 0;
  1011. if (sun4_esp_physaddr) {
  1012. memset (&esp_dev, 0, sizeof(esp_dev));
  1013. esp_dev.reg_addrs[0].phys_addr = sun4_esp_physaddr;
  1014. esp_dev.irqs[0] = 4;
  1015. esp_dev.resource[0].start = sun4_esp_physaddr;
  1016. esp_dev.resource[0].end = sun4_esp_physaddr + ESP_REG_SIZE - 1;
  1017. esp_dev.resource[0].flags = IORESOURCE_IO;
  1018. if (!detect_one_esp(tpnt, &esp_dev, NULL, NULL, 0, 0))
  1019. esps_in_use++;
  1020. printk("ESP: Total of 1 ESP hosts found, %d actually in use.n", esps_in_use);
  1021. esps_running =  esps_in_use;
  1022. }
  1023. return esps_in_use;
  1024. }
  1025. #else /* !CONFIG_SUN4 */
  1026. int __init esp_detect(Scsi_Host_Template *tpnt)
  1027. {
  1028. struct sbus_bus *sbus;
  1029. struct sbus_dev *esp_dev, *sbdev_iter;
  1030. int nesps = 0, esps_in_use = 0;
  1031. espchain = 0;
  1032. if (!sbus_root) {
  1033. #ifdef CONFIG_PCI
  1034. return 0;
  1035. #else
  1036. panic("No SBUS in esp_detect()");
  1037. #endif
  1038. }
  1039. for_each_sbus(sbus) {
  1040. for_each_sbusdev(sbdev_iter, sbus) {
  1041. struct sbus_dev *espdma = NULL;
  1042. int hme = 0;
  1043. /* Is it an esp sbus device? */
  1044. esp_dev = sbdev_iter;
  1045. if (strcmp(esp_dev->prom_name, "esp") &&
  1046.     strcmp(esp_dev->prom_name, "SUNW,esp")) {
  1047. if (!strcmp(esp_dev->prom_name, "SUNW,fas")) {
  1048. hme = 1;
  1049. espdma = esp_dev;
  1050. } else {
  1051. if (!esp_dev->child ||
  1052.     (strcmp(esp_dev->prom_name, "espdma") &&
  1053.      strcmp(esp_dev->prom_name, "dma")))
  1054. continue; /* nope... */
  1055. espdma = esp_dev;
  1056. esp_dev = esp_dev->child;
  1057. if (strcmp(esp_dev->prom_name, "esp") &&
  1058.     strcmp(esp_dev->prom_name, "SUNW,esp"))
  1059. continue; /* how can this happen? */
  1060. }
  1061. }
  1062. if (detect_one_esp(tpnt, esp_dev, espdma, sbus, nesps++, hme) < 0)
  1063. continue;
  1064. esps_in_use++;
  1065. } /* for each sbusdev */
  1066. } /* for each sbus */
  1067. printk("ESP: Total of %d ESP hosts found, %d actually in use.n", nesps,
  1068.        esps_in_use);
  1069. esps_running = esps_in_use;
  1070. return esps_in_use;
  1071. }
  1072. #endif /* !CONFIG_SUN4 */
  1073. /* The info function will return whatever useful
  1074.  * information the developer sees fit.  If not provided, then
  1075.  * the name field will be used instead.
  1076.  */
  1077. const char *esp_info(struct Scsi_Host *host)
  1078. {
  1079. struct esp *esp;
  1080. esp = (struct esp *) host->hostdata;
  1081. switch (esp->erev) {
  1082. case esp100:
  1083. return "Sparc ESP100 (NCR53C90)";
  1084. case esp100a:
  1085. return "Sparc ESP100A (NCR53C90A)";
  1086. case esp236:
  1087. return "Sparc ESP236";
  1088. case fas236:
  1089. return "Sparc ESP236-FAST";
  1090. case fashme:
  1091. return "Sparc ESP366-HME";
  1092. case fas100a:
  1093. return "Sparc ESP100A-FAST";
  1094. default:
  1095. return "Bogon ESP revision";
  1096. };
  1097. }
  1098. /* From Wolfgang Stanglmeier's NCR scsi driver. */
  1099. struct info_str
  1100. {
  1101. char *buffer;
  1102. int length;
  1103. int offset;
  1104. int pos;
  1105. };
  1106. static void copy_mem_info(struct info_str *info, char *data, int len)
  1107. {
  1108. if (info->pos + len > info->length)
  1109. len = info->length - info->pos;
  1110. if (info->pos + len < info->offset) {
  1111. info->pos += len;
  1112. return;
  1113. }
  1114. if (info->pos < info->offset) {
  1115. data += (info->offset - info->pos);
  1116. len  -= (info->offset - info->pos);
  1117. }
  1118. if (len > 0) {
  1119. memcpy(info->buffer + info->pos, data, len);
  1120. info->pos += len;
  1121. }
  1122. }
  1123. static int copy_info(struct info_str *info, char *fmt, ...)
  1124. {
  1125. va_list args;
  1126. char buf[81];
  1127. int len;
  1128. va_start(args, fmt);
  1129. len = vsprintf(buf, fmt, args);
  1130. va_end(args);
  1131. copy_mem_info(info, buf, len);
  1132. return len;
  1133. }
  1134. static int esp_host_info(struct esp *esp, char *ptr, off_t offset, int len)
  1135. {
  1136. struct info_str info;
  1137. int i;
  1138. info.buffer = ptr;
  1139. info.length = len;
  1140. info.offset = offset;
  1141. info.pos = 0;
  1142. copy_info(&info, "Sparc ESP Host Adapter:n");
  1143. copy_info(&info, "tPROM nodett%08xn", (unsigned int) esp->prom_node);
  1144. copy_info(&info, "tPROM namett%sn", esp->prom_name);
  1145. copy_info(&info, "tESP Modeltt");
  1146. switch (esp->erev) {
  1147. case esp100:
  1148. copy_info(&info, "ESP100n");
  1149. break;
  1150. case esp100a:
  1151. copy_info(&info, "ESP100An");
  1152. break;
  1153. case esp236:
  1154. copy_info(&info, "ESP236n");
  1155. break;
  1156. case fas236:
  1157. copy_info(&info, "FAS236n");
  1158. break;
  1159. case fas100a:
  1160. copy_info(&info, "FAS100An");
  1161. break;
  1162. case fast:
  1163. copy_info(&info, "FASTn");
  1164. break;
  1165. case fashme:
  1166. copy_info(&info, "Happy Meal FASn");
  1167. break;
  1168. case espunknown:
  1169. default:
  1170. copy_info(&info, "Unknown!n");
  1171. break;
  1172. };
  1173. copy_info(&info, "tDMA Revisiontt");
  1174. switch (esp->dma->revision) {
  1175. case dvmarev0:
  1176. copy_info(&info, "Rev 0n");
  1177. break;
  1178. case dvmaesc1:
  1179. copy_info(&info, "ESC Rev 1n");
  1180. break;
  1181. case dvmarev1:
  1182. copy_info(&info, "Rev 1n");
  1183. break;
  1184. case dvmarev2:
  1185. copy_info(&info, "Rev 2n");
  1186. break;
  1187. case dvmarev3:
  1188. copy_info(&info, "Rev 3n");
  1189. break;
  1190. case dvmarevplus:
  1191. copy_info(&info, "Rev 1+n");
  1192. break;
  1193. case dvmahme:
  1194. copy_info(&info, "Rev HME/FASn");
  1195. break;
  1196. default:
  1197. copy_info(&info, "Unknown!n");
  1198. break;
  1199. };
  1200. copy_info(&info, "tLive Targetstt[ ");
  1201. for (i = 0; i < 15; i++) {
  1202. if (esp->targets_present & (1 << i))
  1203. copy_info(&info, "%d ", i);
  1204. }
  1205. copy_info(&info, "]nn");
  1206. /* Now describe the state of each existing target. */
  1207. copy_info(&info, "Target #tconfig3ttSync CapabilitiestDisconnecttWiden");
  1208. for (i = 0; i < 15; i++) {
  1209. if (esp->targets_present & (1 << i)) {
  1210. Scsi_Device *SDptr = esp->ehost->host_queue;
  1211. while ((SDptr->host != esp->ehost) &&
  1212.        (SDptr->id != i) &&
  1213.        (SDptr->next))
  1214. SDptr = SDptr->next;
  1215. copy_info(&info, "%dtt", i);
  1216. copy_info(&info, "%08lxt", esp->config3[i]);
  1217. copy_info(&info, "[%02lx,%02lx]ttt", SDptr->sync_max_offset,
  1218.   SDptr->sync_min_period);
  1219. copy_info(&info, "%stt", SDptr->disconnect ? "yes" : "no");
  1220. copy_info(&info, "%sn",
  1221.   (esp->config3[i] & ESP_CONFIG3_EWIDE) ? "yes" : "no");
  1222. }
  1223. }
  1224. return info.pos > info.offset? info.pos - info.offset : 0;
  1225. }
  1226. /* ESP proc filesystem code. */
  1227. int esp_proc_info(char *buffer, char **start, off_t offset, int length,
  1228.   int hostno, int inout)
  1229. {
  1230. struct esp *esp;
  1231. if (inout)
  1232. return -EINVAL; /* not yet */
  1233. for_each_esp(esp) {
  1234. if (esp->ehost->host_no == hostno)
  1235. break;
  1236. }
  1237. if (!esp)
  1238. return -EINVAL;
  1239. if (start)
  1240. *start = buffer;
  1241. return esp_host_info(esp, buffer, offset, length);
  1242. }
  1243. static void esp_get_dmabufs(struct esp *esp, Scsi_Cmnd *sp)
  1244. {
  1245. if (sp->use_sg == 0) {
  1246. sp->SCp.this_residual = sp->request_bufflen;
  1247. sp->SCp.buffer = (struct scatterlist *) sp->request_buffer;
  1248. sp->SCp.buffers_residual = 0;
  1249. if (sp->request_bufflen) {
  1250. sp->SCp.have_data_in = sbus_map_single(esp->sdev, sp->SCp.buffer,
  1251.        sp->SCp.this_residual,
  1252.        scsi_to_sbus_dma_dir(sp->sc_data_direction));
  1253. sp->SCp.ptr = (char *) ((unsigned long)sp->SCp.have_data_in);
  1254. } else {
  1255. sp->SCp.ptr = NULL;
  1256. }
  1257. } else {
  1258. sp->SCp.buffer = (struct scatterlist *) sp->buffer;
  1259. sp->SCp.buffers_residual = sbus_map_sg(esp->sdev,
  1260.        sp->SCp.buffer,
  1261.        sp->use_sg,
  1262.        scsi_to_sbus_dma_dir(sp->sc_data_direction));
  1263. sp->SCp.this_residual = sg_dma_len(sp->SCp.buffer);
  1264. sp->SCp.ptr = (char *) ((unsigned long)sg_dma_address(sp->SCp.buffer));
  1265. }
  1266. }
  1267. static void esp_release_dmabufs(struct esp *esp, Scsi_Cmnd *sp)
  1268. {
  1269. if (sp->use_sg) {
  1270. sbus_unmap_sg(esp->sdev, sp->buffer, sp->use_sg,
  1271.       scsi_to_sbus_dma_dir(sp->sc_data_direction));
  1272. } else if (sp->request_bufflen) {
  1273. sbus_unmap_single(esp->sdev,
  1274.   sp->SCp.have_data_in,
  1275.   sp->request_bufflen,
  1276.   scsi_to_sbus_dma_dir(sp->sc_data_direction));
  1277. }
  1278. }
  1279. static void esp_restore_pointers(struct esp *esp, Scsi_Cmnd *sp)
  1280. {
  1281. struct esp_pointers *ep = &esp->data_pointers[sp->target];
  1282. sp->SCp.ptr = ep->saved_ptr;
  1283. sp->SCp.buffer = ep->saved_buffer;
  1284. sp->SCp.this_residual = ep->saved_this_residual;
  1285. sp->SCp.buffers_residual = ep->saved_buffers_residual;
  1286. }
  1287. static void esp_save_pointers(struct esp *esp, Scsi_Cmnd *sp)
  1288. {
  1289. struct esp_pointers *ep = &esp->data_pointers[sp->target];
  1290. ep->saved_ptr = sp->SCp.ptr;
  1291. ep->saved_buffer = sp->SCp.buffer;
  1292. ep->saved_this_residual = sp->SCp.this_residual;
  1293. ep->saved_buffers_residual = sp->SCp.buffers_residual;
  1294. }
  1295. /* Some rules:
  1296.  *
  1297.  *   1) Never ever panic while something is live on the bus.
  1298.  *      If there is to be any chance of syncing the disks this
  1299.  *      rule is to be obeyed.
  1300.  *
  1301.  *   2) Any target that causes a foul condition will no longer
  1302.  *      have synchronous transfers done to it, no questions
  1303.  *      asked.
  1304.  *
  1305.  *   3) Keep register accesses to a minimum.  Think about some
  1306.  *      day when we have Xbus machines this is running on and
  1307.  *      the ESP chip is on the other end of the machine on a
  1308.  *      different board from the cpu where this is running.
  1309.  */
  1310. /* Fire off a command.  We assume the bus is free and that the only
  1311.  * case where we could see an interrupt is where we have disconnected
  1312.  * commands active and they are trying to reselect us.
  1313.  */
  1314. static inline void esp_check_cmd(struct esp *esp, Scsi_Cmnd *sp)
  1315. {
  1316. switch (sp->cmd_len) {
  1317. case 6:
  1318. case 10:
  1319. case 12:
  1320. esp->esp_slowcmd = 0;
  1321. break;
  1322. default:
  1323. esp->esp_slowcmd = 1;
  1324. esp->esp_scmdleft = sp->cmd_len;
  1325. esp->esp_scmdp = &sp->cmnd[0];
  1326. break;
  1327. };
  1328. }
  1329. static inline void build_sync_nego_msg(struct esp *esp, int period, int offset)
  1330. {
  1331. esp->cur_msgout[0] = EXTENDED_MESSAGE;
  1332. esp->cur_msgout[1] = 3;
  1333. esp->cur_msgout[2] = EXTENDED_SDTR;
  1334. esp->cur_msgout[3] = period;
  1335. esp->cur_msgout[4] = offset;
  1336. esp->msgout_len = 5;
  1337. }
  1338. /* SIZE is in bits, currently HME only supports 16 bit wide transfers. */
  1339. static inline void build_wide_nego_msg(struct esp *esp, int size)
  1340. {
  1341. esp->cur_msgout[0] = EXTENDED_MESSAGE;
  1342. esp->cur_msgout[1] = 2;
  1343. esp->cur_msgout[2] = EXTENDED_WDTR;
  1344. switch (size) {
  1345. case 32:
  1346. esp->cur_msgout[3] = 2;
  1347. break;
  1348. case 16:
  1349. esp->cur_msgout[3] = 1;
  1350. break;
  1351. case 8:
  1352. default:
  1353. esp->cur_msgout[3] = 0;
  1354. break;
  1355. };
  1356. esp->msgout_len = 4;
  1357. }
  1358. static void esp_exec_cmd(struct esp *esp)
  1359. {
  1360. Scsi_Cmnd *SCptr;
  1361. Scsi_Device *SDptr;
  1362. volatile u8 *cmdp = esp->esp_command;
  1363. u8 the_esp_command;
  1364. int lun, target;
  1365. int i;
  1366. /* Hold off if we have disconnected commands and
  1367.  * an IRQ is showing...
  1368.  */
  1369. if (esp->disconnected_SC && ESP_IRQ_P(esp->dregs))
  1370. return;
  1371. /* Grab first member of the issue queue. */
  1372. SCptr = esp->current_SC = remove_first_SC(&esp->issue_SC);
  1373. /* Safe to panic here because current_SC is null. */
  1374. if (!SCptr)
  1375. panic("esp: esp_exec_cmd and issue queue is NULL");
  1376. SDptr = SCptr->device;
  1377. lun = SCptr->lun;
  1378. target = SCptr->target;
  1379. esp->snip = 0;
  1380. esp->msgout_len = 0;
  1381. /* Send it out whole, or piece by piece?   The ESP
  1382.  * only knows how to automatically send out 6, 10,
  1383.  * and 12 byte commands.  I used to think that the
  1384.  * Linux SCSI code would never throw anything other
  1385.  * than that to us, but then again there is the
  1386.  * SCSI generic driver which can send us anything.
  1387.  */
  1388. esp_check_cmd(esp, SCptr);
  1389. /* If arbitration/selection is successful, the ESP will leave
  1390.  * ATN asserted, causing the target to go into message out
  1391.  * phase.  The ESP will feed the target the identify and then
  1392.  * the target can only legally go to one of command,
  1393.  * datain/out, status, or message in phase, or stay in message
  1394.  * out phase (should we be trying to send a sync negotiation
  1395.  * message after the identify).  It is not allowed to drop
  1396.  * BSY, but some buggy targets do and we check for this
  1397.  * condition in the selection complete code.  Most of the time
  1398.  * we'll make the command bytes available to the ESP and it
  1399.  * will not interrupt us until it finishes command phase, we
  1400.  * cannot do this for command sizes the ESP does not
  1401.  * understand and in this case we'll get interrupted right
  1402.  * when the target goes into command phase.
  1403.  *
  1404.  * It is absolutely _illegal_ in the presence of SCSI-2 devices
  1405.  * to use the ESP select w/o ATN command.  When SCSI-2 devices are
  1406.  * present on the bus we _must_ always go straight to message out
  1407.  * phase with an identify message for the target.  Being that
  1408.  * selection attempts in SCSI-1 w/o ATN was an option, doing SCSI-2
  1409.  * selections should not confuse SCSI-1 we hope.
  1410.  */
  1411. if (SDptr->sync) {
  1412. /* this targets sync is known */
  1413. #ifndef __sparc_v9__
  1414. do_sync_known:
  1415. #endif
  1416. if (SDptr->disconnect)
  1417. *cmdp++ = IDENTIFY(1, lun);
  1418. else
  1419. *cmdp++ = IDENTIFY(0, lun);
  1420. if (esp->esp_slowcmd) {
  1421. the_esp_command = (ESP_CMD_SELAS | ESP_CMD_DMA);
  1422. esp_advance_phase(SCptr, in_slct_stop);
  1423. } else {
  1424. the_esp_command = (ESP_CMD_SELA | ESP_CMD_DMA);
  1425. esp_advance_phase(SCptr, in_slct_norm);
  1426. }
  1427. } else if (!(esp->targets_present & (1<<target)) || !(SDptr->disconnect)) {
  1428. /* After the bootup SCSI code sends both the
  1429.  * TEST_UNIT_READY and INQUIRY commands we want
  1430.  * to at least attempt allowing the device to
  1431.  * disconnect.
  1432.  */
  1433. ESPMISC(("esp: Selecting device for first time. target=%d "
  1434.  "lun=%dn", target, SCptr->lun));
  1435. if (!SDptr->borken && !SDptr->disconnect)
  1436. SDptr->disconnect = 1;
  1437. *cmdp++ = IDENTIFY(0, lun);
  1438. esp->prevmsgout = NOP;
  1439. esp_advance_phase(SCptr, in_slct_norm);
  1440. the_esp_command = (ESP_CMD_SELA | ESP_CMD_DMA);
  1441. /* Take no chances... */
  1442. SDptr->sync_max_offset = 0;
  1443. SDptr->sync_min_period = 0;
  1444. } else {
  1445. /* Sorry, I have had way too many problems with
  1446.  * various CDROM devices on ESP. -DaveM
  1447.  */
  1448. int cdrom_hwbug_wkaround = 0;
  1449. #ifndef __sparc_v9__
  1450. /* Never allow disconnects or synchronous transfers on
  1451.  * SparcStation1 and SparcStation1+.  Allowing those
  1452.  * to be enabled seems to lockup the machine completely.
  1453.  */
  1454. if ((idprom->id_machtype == (SM_SUN4C | SM_4C_SS1)) ||
  1455.     (idprom->id_machtype == (SM_SUN4C | SM_4C_SS1PLUS))) {
  1456. /* But we are nice and allow tapes and removable
  1457.  * disks (but not CDROMs) to disconnect.
  1458.  */
  1459. if(SDptr->type == TYPE_TAPE ||
  1460.    (SDptr->type != TYPE_ROM && SDptr->removable))
  1461. SDptr->disconnect = 1;
  1462. else
  1463. SDptr->disconnect = 0;
  1464. SDptr->sync_max_offset = 0;
  1465. SDptr->sync_min_period = 0;
  1466. SDptr->sync = 1;
  1467. esp->snip = 0;
  1468. goto do_sync_known;
  1469. }
  1470. #endif /* !(__sparc_v9__) */
  1471. /* We've talked to this guy before,
  1472.  * but never negotiated.  Let's try,
  1473.  * need to attempt WIDE first, before
  1474.  * sync nego, as per SCSI 2 standard.
  1475.  */
  1476. if (esp->erev == fashme && !SDptr->wide) {
  1477. if (!SDptr->borken &&
  1478.    SDptr->type != TYPE_ROM &&
  1479.    SDptr->removable == 0) {
  1480. build_wide_nego_msg(esp, 16);
  1481. SDptr->wide = 1;
  1482. esp->wnip = 1;
  1483. goto after_nego_msg_built;
  1484. } else {
  1485. SDptr->wide = 1;
  1486. /* Fall through and try sync. */
  1487. }
  1488. }
  1489. if (!SDptr->borken) {
  1490. if ((SDptr->type == TYPE_ROM)) {
  1491. /* Nice try sucker... */
  1492. ESPMISC(("esp%d: Disabling sync for buggy "
  1493.  "CDROM.n", esp->esp_id));
  1494. cdrom_hwbug_wkaround = 1;
  1495. build_sync_nego_msg(esp, 0, 0);
  1496. } else if (SDptr->removable != 0) {
  1497. ESPMISC(("esp%d: Not negotiating sync/wide but "
  1498.  "allowing disconnect for removable media.n",
  1499.  esp->esp_id));
  1500. build_sync_nego_msg(esp, 0, 0);
  1501. } else {
  1502. build_sync_nego_msg(esp, esp->sync_defp, 15);
  1503. }
  1504. } else {
  1505. build_sync_nego_msg(esp, 0, 0);
  1506. }
  1507. SDptr->sync = 1;
  1508. esp->snip = 1;
  1509. after_nego_msg_built:
  1510. /* A fix for broken SCSI1 targets, when they disconnect
  1511.  * they lock up the bus and confuse ESP.  So disallow
  1512.  * disconnects for SCSI1 targets for now until we
  1513.  * find a better fix.
  1514.  *
  1515.  * Addendum: This is funny, I figured out what was going
  1516.  *           on.  The blotzed SCSI1 target would disconnect,
  1517.  *           one of the other SCSI2 targets or both would be
  1518.  *           disconnected as well.  The SCSI1 target would
  1519.  *           stay disconnected long enough that we start
  1520.  *           up a command on one of the SCSI2 targets.  As
  1521.  *           the ESP is arbitrating for the bus the SCSI1
  1522.  *           target begins to arbitrate as well to reselect
  1523.  *           the ESP.  The SCSI1 target refuses to drop it's
  1524.  *           ID bit on the data bus even though the ESP is
  1525.  *           at ID 7 and is the obvious winner for any
  1526.  *           arbitration.  The ESP is a poor sport and refuses
  1527.  *           to lose arbitration, it will continue indefinately
  1528.  *           trying to arbitrate for the bus and can only be
  1529.  *           stopped via a chip reset or SCSI bus reset.
  1530.  *           Therefore _no_ disconnects for SCSI1 targets
  1531.  *           thank you very much. ;-)
  1532.  */
  1533. if(((SDptr->scsi_level < 3) &&
  1534.     (SDptr->type != TYPE_TAPE) &&
  1535.     SDptr->removable == 0) ||
  1536.     cdrom_hwbug_wkaround || SDptr->borken) {
  1537. ESPMISC((KERN_INFO "esp%d: Disabling DISCONNECT for target %d "
  1538.  "lun %dn", esp->esp_id, SCptr->target, SCptr->lun));
  1539. SDptr->disconnect = 0;
  1540. *cmdp++ = IDENTIFY(0, lun);
  1541. } else {
  1542. *cmdp++ = IDENTIFY(1, lun);
  1543. }
  1544. /* ESP fifo is only so big...
  1545.  * Make this look like a slow command.
  1546.  */
  1547. esp->esp_slowcmd = 1;
  1548. esp->esp_scmdleft = SCptr->cmd_len;
  1549. esp->esp_scmdp = &SCptr->cmnd[0];
  1550. the_esp_command = (ESP_CMD_SELAS | ESP_CMD_DMA);
  1551. esp_advance_phase(SCptr, in_slct_msg);
  1552. }
  1553. if (!esp->esp_slowcmd)
  1554. for (i = 0; i < SCptr->cmd_len; i++)
  1555. *cmdp++ = SCptr->cmnd[i];
  1556. /* HME sucks... */
  1557. if (esp->erev == fashme)
  1558. sbus_writeb((target & 0xf) | (ESP_BUSID_RESELID | ESP_BUSID_CTR32BIT),
  1559.     esp->eregs + ESP_BUSID);
  1560. else
  1561. sbus_writeb(target & 7, esp->eregs + ESP_BUSID);
  1562. if (esp->prev_soff != SDptr->sync_max_offset ||
  1563.     esp->prev_stp  != SDptr->sync_min_period ||
  1564.     (esp->erev > esp100a &&
  1565.      esp->prev_cfg3 != esp->config3[target])) {
  1566. esp->prev_soff = SDptr->sync_max_offset;
  1567. esp->prev_stp = SDptr->sync_min_period;
  1568. sbus_writeb(esp->prev_soff, esp->eregs + ESP_SOFF);
  1569. sbus_writeb(esp->prev_stp, esp->eregs + ESP_STP);
  1570. if (esp->erev > esp100a) {
  1571. esp->prev_cfg3 = esp->config3[target];
  1572. sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
  1573. }
  1574. }
  1575. i = (cmdp - esp->esp_command);
  1576. if (esp->erev == fashme) {
  1577. esp_cmd(esp, ESP_CMD_FLUSH); /* Grrr! */
  1578. /* Set up the DMA and HME counters */
  1579. sbus_writeb(i, esp->eregs + ESP_TCLOW);
  1580. sbus_writeb(0, esp->eregs + ESP_TCMED);
  1581. sbus_writeb(0, esp->eregs + FAS_RLO);
  1582. sbus_writeb(0, esp->eregs + FAS_RHI);
  1583. esp_cmd(esp, the_esp_command);
  1584. /* Talk about touchy hardware... */
  1585. esp->prev_hme_dmacsr = ((esp->prev_hme_dmacsr |
  1586.  (DMA_SCSI_DISAB | DMA_ENABLE)) &
  1587. ~(DMA_ST_WRITE));
  1588. sbus_writel(16, esp->dregs + DMA_COUNT);
  1589. sbus_writel(esp->esp_command_dvma, esp->dregs + DMA_ADDR);
  1590. sbus_writel(esp->prev_hme_dmacsr, esp->dregs + DMA_CSR);
  1591. } else {
  1592. u32 tmp;
  1593. /* Set up the DMA and ESP counters */
  1594. sbus_writeb(i, esp->eregs + ESP_TCLOW);
  1595. sbus_writeb(0, esp->eregs + ESP_TCMED);
  1596. tmp = sbus_readl(esp->dregs + DMA_CSR);
  1597. tmp &= ~DMA_ST_WRITE;
  1598. tmp |= DMA_ENABLE;
  1599. sbus_writel(tmp, esp->dregs + DMA_CSR);
  1600. if (esp->dma->revision == dvmaesc1) {
  1601. if (i) /* Workaround ESC gate array SBUS rerun bug. */
  1602. sbus_writel(PAGE_SIZE, esp->dregs + DMA_COUNT);
  1603. }
  1604. sbus_writel(esp->esp_command_dvma, esp->dregs + DMA_ADDR);
  1605. /* Tell ESP to "go". */
  1606. esp_cmd(esp, the_esp_command);
  1607. }
  1608. }
  1609. /* Queue a SCSI command delivered from the mid-level Linux SCSI code. */
  1610. int esp_queue(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
  1611. {
  1612. struct esp *esp;
  1613. unsigned long flags;
  1614. /* Set up func ptr and initial driver cmd-phase. */
  1615. SCpnt->scsi_done = done;
  1616. SCpnt->SCp.phase = not_issued;
  1617. /* We use the scratch area. */
  1618. ESPQUEUE(("esp_queue: target=%d lun=%d ", SCpnt->target, SCpnt->lun));
  1619. ESPDISC(("N<%02x,%02x>", SCpnt->target, SCpnt->lun));
  1620. esp = (struct esp *) SCpnt->host->hostdata;
  1621. esp_get_dmabufs(esp, SCpnt);
  1622. esp_save_pointers(esp, SCpnt); /* FIXME for tag queueing */
  1623. SCpnt->SCp.Status           = CHECK_CONDITION;
  1624. SCpnt->SCp.Message          = 0xff;
  1625. SCpnt->SCp.sent_command     = 0;
  1626. spin_lock_irqsave(&esp->lock, flags);
  1627. /* Place into our queue. */
  1628. if (SCpnt->cmnd[0] == REQUEST_SENSE) {
  1629. ESPQUEUE(("RQSENSEn"));
  1630. prepend_SC(&esp->issue_SC, SCpnt);
  1631. } else {
  1632. ESPQUEUE(("n"));
  1633. append_SC(&esp->issue_SC, SCpnt);
  1634. }
  1635. /* Run it now if we can. */
  1636. if (!esp->current_SC && !esp->resetting_bus)
  1637. esp_exec_cmd(esp);
  1638. spin_unlock_irqrestore(&esp->lock, flags);
  1639. return 0;
  1640. }
  1641. /* Only queuing supported in this ESP driver. */
  1642. int esp_command(Scsi_Cmnd *SCpnt)
  1643. {
  1644. struct esp *esp = (struct esp *) SCpnt->host->hostdata;
  1645. ESPLOG(("esp%d: esp_command() called...n", esp->esp_id));
  1646. return -1;
  1647. }
  1648. /* Dump driver state. */
  1649. static void esp_dump_cmd(Scsi_Cmnd *SCptr)
  1650. {
  1651. ESPLOG(("[tgt<%02x> lun<%02x> "
  1652. "pphase<%s> cphase<%s>]",
  1653. SCptr->target, SCptr->lun,
  1654. phase_string(SCptr->SCp.sent_command),
  1655. phase_string(SCptr->SCp.phase)));
  1656. }
  1657. static void esp_dump_state(struct esp *esp)
  1658. {
  1659. Scsi_Cmnd *SCptr = esp->current_SC;
  1660. #ifdef DEBUG_ESP_CMDS
  1661. int i;
  1662. #endif
  1663. ESPLOG(("esp%d: dumping staten", esp->esp_id));
  1664. ESPLOG(("esp%d: dma -- cond_reg<%08x> addr<%08x>n",
  1665. esp->esp_id,
  1666. sbus_readl(esp->dregs + DMA_CSR),
  1667. sbus_readl(esp->dregs + DMA_ADDR)));
  1668. ESPLOG(("esp%d: SW [sreg<%02x> sstep<%02x> ireg<%02x>]n",
  1669. esp->esp_id, esp->sreg, esp->seqreg, esp->ireg));
  1670. ESPLOG(("esp%d: HW reread [sreg<%02x> sstep<%02x> ireg<%02x>]n",
  1671. esp->esp_id,
  1672. sbus_readb(esp->eregs + ESP_STATUS),
  1673. sbus_readb(esp->eregs + ESP_SSTEP),
  1674. sbus_readb(esp->eregs + ESP_INTRPT)));
  1675. #ifdef DEBUG_ESP_CMDS
  1676. printk("esp%d: last ESP cmds [", esp->esp_id);
  1677. i = (esp->espcmdent - 1) & 31;
  1678. printk("<"); esp_print_cmd(esp->espcmdlog[i]); printk(">");
  1679. i = (i - 1) & 31;
  1680. printk("<"); esp_print_cmd(esp->espcmdlog[i]); printk(">");
  1681. i = (i - 1) & 31;
  1682. printk("<"); esp_print_cmd(esp->espcmdlog[i]); printk(">");
  1683. i = (i - 1) & 31;
  1684. printk("<"); esp_print_cmd(esp->espcmdlog[i]); printk(">");
  1685. printk("]n");
  1686. #endif /* (DEBUG_ESP_CMDS) */
  1687. if (SCptr) {
  1688. ESPLOG(("esp%d: current command ", esp->esp_id));
  1689. esp_dump_cmd(SCptr);
  1690. }
  1691. ESPLOG(("n"));
  1692. SCptr = esp->disconnected_SC;
  1693. ESPLOG(("esp%d: disconnected ", esp->esp_id));
  1694. while (SCptr) {
  1695. esp_dump_cmd(SCptr);
  1696. SCptr = (Scsi_Cmnd *) SCptr->host_scribble;
  1697. }
  1698. ESPLOG(("n"));
  1699. }
  1700. /* Abort a command. */
  1701. int esp_abort(Scsi_Cmnd *SCptr)
  1702. {
  1703. struct esp *esp = (struct esp *) SCptr->host->hostdata;
  1704. unsigned long flags;
  1705. int don;
  1706. spin_lock_irqsave(&esp->lock, flags);
  1707. ESPLOG(("esp%d: Aborting commandn", esp->esp_id));
  1708. esp_dump_state(esp);
  1709. /* Wheee, if this is the current command on the bus, the
  1710.  * best we can do is assert ATN and wait for msgout phase.
  1711.  * This should even fix a hung SCSI bus when we lose state
  1712.  * in the driver and timeout because the eventual phase change
  1713.  * will cause the ESP to (eventually) give an interrupt.
  1714.  */
  1715. if (esp->current_SC == SCptr) {
  1716. esp->cur_msgout[0] = ABORT;
  1717. esp->msgout_len = 1;
  1718. esp->msgout_ctr = 0;
  1719. esp_cmd(esp, ESP_CMD_SATN);
  1720. spin_unlock_irqrestore(&esp->lock, flags);
  1721. return SCSI_ABORT_PENDING;
  1722. }
  1723. /* If it is still in the issue queue then we can safely
  1724.  * call the completion routine and report abort success.
  1725.  */
  1726. don = (sbus_readl(esp->dregs + DMA_CSR) & DMA_INT_ENAB);
  1727. if (don) {
  1728. ESP_INTSOFF(esp->dregs);
  1729. }
  1730. if (esp->issue_SC) {
  1731. Scsi_Cmnd **prev, *this;
  1732. for (prev = (&esp->issue_SC), this = esp->issue_SC;
  1733.      this != NULL;
  1734.      prev = (Scsi_Cmnd **) &(this->host_scribble),
  1735.      this = (Scsi_Cmnd *) this->host_scribble) {
  1736. if (this == SCptr) {
  1737. *prev = (Scsi_Cmnd *) this->host_scribble;
  1738. this->host_scribble = NULL;
  1739. spin_unlock_irqrestore(&esp->lock, flags);
  1740. esp_release_dmabufs(esp, this);
  1741. this->result = DID_ABORT << 16;
  1742. this->scsi_done(this);
  1743. if (don)
  1744. ESP_INTSON(esp->dregs);
  1745. return SCSI_ABORT_SUCCESS;
  1746. }
  1747. }
  1748. }
  1749. /* Yuck, the command to abort is disconnected, it is not
  1750.  * worth trying to abort it now if something else is live
  1751.  * on the bus at this time.  So, we let the SCSI code wait
  1752.  * a little bit and try again later.
  1753.  */
  1754. if (esp->current_SC) {
  1755. if (don)
  1756. ESP_INTSON(esp->dregs);
  1757. spin_unlock_irqrestore(&esp->lock, flags);
  1758. return SCSI_ABORT_BUSY;
  1759. }
  1760. /* It's disconnected, we have to reconnect to re-establish
  1761.  * the nexus and tell the device to abort.  However, we really
  1762.  * cannot 'reconnect' per se, therefore we tell the upper layer
  1763.  * the safest thing we can.  This is, wait a bit, if nothing
  1764.  * happens, we are really hung so reset the bus.
  1765.  */
  1766. if (don)
  1767. ESP_INTSON(esp->dregs);
  1768. spin_unlock_irqrestore(&esp->lock, flags);
  1769. return SCSI_ABORT_SNOOZE;
  1770. }
  1771. /* We've sent ESP_CMD_RS to the ESP, the interrupt had just
  1772.  * arrived indicating the end of the SCSI bus reset.  Our job
  1773.  * is to clean out the command queues and begin re-execution
  1774.  * of SCSI commands once more.
  1775.  */
  1776. static int esp_finish_reset(struct esp *esp)
  1777. {
  1778. Scsi_Cmnd *sp = esp->current_SC;
  1779. /* Clean up currently executing command, if any. */
  1780. if (sp != NULL) {
  1781. esp->current_SC = NULL;
  1782. spin_unlock(&esp->lock);
  1783. esp_release_dmabufs(esp, sp);
  1784. sp->result = (DID_RESET << 16);
  1785. spin_lock(&io_request_lock);
  1786. sp->scsi_done(sp);
  1787. spin_unlock(&io_request_lock);
  1788. spin_lock(&esp->lock);
  1789. }
  1790. /* Clean up disconnected queue, they have been invalidated
  1791.  * by the bus reset.
  1792.  */
  1793. if (esp->disconnected_SC) {
  1794. while ((sp = remove_first_SC(&esp->disconnected_SC)) != NULL) {
  1795. spin_unlock(&esp->lock);
  1796. esp_release_dmabufs(esp, sp);
  1797. sp->result = (DID_RESET << 16);
  1798. spin_lock(&io_request_lock);
  1799. sp->scsi_done(sp);
  1800. spin_unlock(&io_request_lock);
  1801. spin_lock(&esp->lock);
  1802. }
  1803. }
  1804. /* SCSI bus reset is complete. */
  1805. esp->resetting_bus = 0;
  1806. /* Ok, now it is safe to get commands going once more. */
  1807. if (esp->issue_SC)
  1808. esp_exec_cmd(esp);
  1809. return do_intr_end;
  1810. }
  1811. static int esp_do_resetbus(struct esp *esp)
  1812. {
  1813. ESPLOG(("esp%d: Resetting scsi busn", esp->esp_id));
  1814. esp->resetting_bus = 1;
  1815. esp_cmd(esp, ESP_CMD_RS);
  1816. return do_intr_end;
  1817. }
  1818. /* Reset ESP chip, reset hanging bus, then kill active and
  1819.  * disconnected commands for targets without soft reset.
  1820.  */
  1821. int esp_reset(Scsi_Cmnd *SCptr, unsigned int how)
  1822. {
  1823. struct esp *esp = (struct esp *) SCptr->host->hostdata;
  1824. unsigned long flags;
  1825. spin_lock_irqsave(&esp->lock, flags);
  1826. (void) esp_do_resetbus(esp);
  1827. spin_unlock_irqrestore(&esp->lock, flags);
  1828. return SCSI_RESET_PENDING;
  1829. }
  1830. /* Internal ESP done function. */
  1831. static void esp_done(struct esp *esp, int error)
  1832. {
  1833. Scsi_Cmnd *done_SC = esp->current_SC;
  1834. esp->current_SC = NULL;
  1835. spin_unlock(&esp->lock);
  1836. esp_release_dmabufs(esp, done_SC);
  1837. done_SC->result = error;
  1838. spin_lock(&io_request_lock);
  1839. done_SC->scsi_done(done_SC);
  1840. spin_unlock(&io_request_lock);
  1841. /* Bus is free, issue any commands in the queue. */
  1842. spin_lock(&esp->lock);
  1843. if (esp->issue_SC && !esp->current_SC)
  1844. esp_exec_cmd(esp);
  1845. }
  1846. /* Wheee, ESP interrupt engine. */  
  1847. /* Forward declarations. */
  1848. static int esp_do_phase_determine(struct esp *esp);
  1849. static int esp_do_data_finale(struct esp *esp);
  1850. static int esp_select_complete(struct esp *esp);
  1851. static int esp_do_status(struct esp *esp);
  1852. static int esp_do_msgin(struct esp *esp);
  1853. static int esp_do_msgindone(struct esp *esp);
  1854. static int esp_do_msgout(struct esp *esp);
  1855. static int esp_do_cmdbegin(struct esp *esp);
  1856. #define sreg_datainp(__sreg)  (((__sreg) & ESP_STAT_PMASK) == ESP_DIP)
  1857. #define sreg_dataoutp(__sreg) (((__sreg) & ESP_STAT_PMASK) == ESP_DOP)
  1858. /* Read any bytes found in the FAS366 fifo, storing them into
  1859.  * the ESP driver software state structure.
  1860.  */
  1861. static void hme_fifo_read(struct esp *esp)
  1862. {
  1863. u8 count = 0;
  1864. u8 status = esp->sreg;
  1865. /* Cannot safely frob the fifo for these following cases, but
  1866.  * we must always read the fifo when the reselect interrupt
  1867.  * is pending.
  1868.  */
  1869. if (((esp->ireg & ESP_INTR_RSEL) == 0) &&
  1870.     (sreg_datainp(status) ||
  1871.      sreg_dataoutp(status) ||
  1872.      (esp->current_SC &&
  1873.       esp->current_SC->SCp.phase == in_data_done))) {
  1874. ESPHME(("<wkaround_skipped>"));
  1875. } else {
  1876. unsigned long fcnt = sbus_readb(esp->eregs + ESP_FFLAGS) & ESP_FF_FBYTES;
  1877. /* The HME stores bytes in multiples of 2 in the fifo. */
  1878. ESPHME(("hme_fifo[fcnt=%d", (int)fcnt));
  1879. while (fcnt) {
  1880. esp->hme_fifo_workaround_buffer[count++] =
  1881. sbus_readb(esp->eregs + ESP_FDATA);
  1882. esp->hme_fifo_workaround_buffer[count++] =
  1883. sbus_readb(esp->eregs + ESP_FDATA);
  1884. ESPHME(("<%02x,%02x>", esp->hme_fifo_workaround_buffer[count-2], esp->hme_fifo_workaround_buffer[count-1]));
  1885. fcnt--;
  1886. }
  1887. if (sbus_readb(esp->eregs + ESP_STATUS2) & ESP_STAT2_F1BYTE) {
  1888. ESPHME(("<poke_byte>"));
  1889. sbus_writeb(0, esp->eregs + ESP_FDATA);
  1890. esp->hme_fifo_workaround_buffer[count++] =
  1891. sbus_readb(esp->eregs + ESP_FDATA);
  1892. ESPHME(("<%02x,0x00>", esp->hme_fifo_workaround_buffer[count-1]));
  1893. ESPHME(("CMD_FLUSH"));
  1894. esp_cmd(esp, ESP_CMD_FLUSH);
  1895. } else {
  1896. ESPHME(("no_xtra_byte"));
  1897. }
  1898. }
  1899. ESPHME(("wkarnd_cnt=%d]", (int)count));
  1900. esp->hme_fifo_workaround_count = count;
  1901. }
  1902. static inline void hme_fifo_push(struct esp *esp, u8 *bytes, u8 count)
  1903. {
  1904. esp_cmd(esp, ESP_CMD_FLUSH);
  1905. while (count) {
  1906. u8 tmp = *bytes++;
  1907. sbus_writeb(tmp, esp->eregs + ESP_FDATA);
  1908. sbus_writeb(0, esp->eregs + ESP_FDATA);
  1909. count--;
  1910. }
  1911. }
  1912. /* We try to avoid some interrupts by jumping ahead and see if the ESP
  1913.  * has gotten far enough yet.  Hence the following.
  1914.  */
  1915. static inline int skipahead1(struct esp *esp, Scsi_Cmnd *scp,
  1916.      int prev_phase, int new_phase)
  1917. {
  1918. if (scp->SCp.sent_command != prev_phase)
  1919. return 0;
  1920. if (ESP_IRQ_P(esp->dregs)) {
  1921. /* Yes, we are able to save an interrupt. */
  1922. if (esp->erev == fashme)
  1923. esp->sreg2 = sbus_readb(esp->eregs + ESP_STATUS2);
  1924. esp->sreg = (sbus_readb(esp->eregs + ESP_STATUS) & ~(ESP_STAT_INTR));
  1925. esp->ireg = sbus_readb(esp->eregs + ESP_INTRPT);
  1926. if (esp->erev == fashme) {
  1927. /* This chip is really losing. */
  1928. ESPHME(("HME["));
  1929. /* Must latch fifo before reading the interrupt
  1930.  * register else garbage ends up in the FIFO
  1931.  * which confuses the driver utterly.
  1932.  * Happy Meal indeed....
  1933.  */
  1934. ESPHME(("fifo_workaround]"));
  1935. if (!(esp->sreg2 & ESP_STAT2_FEMPTY) ||
  1936.     (esp->sreg2 & ESP_STAT2_F1BYTE))
  1937. hme_fifo_read(esp);
  1938. }
  1939. if (!(esp->ireg & ESP_INTR_SR))
  1940. return 0;
  1941. else
  1942. return do_reset_complete;
  1943. }
  1944. /* Ho hum, target is taking forever... */
  1945. scp->SCp.sent_command = new_phase; /* so we don't recurse... */
  1946. return do_intr_end;
  1947. }
  1948. static inline int skipahead2(struct esp *esp, Scsi_Cmnd *scp,
  1949.      int prev_phase1, int prev_phase2, int new_phase)
  1950. {
  1951. if (scp->SCp.sent_command != prev_phase1 &&
  1952.     scp->SCp.sent_command != prev_phase2)
  1953. return 0;
  1954. if (ESP_IRQ_P(esp->dregs)) {
  1955. /* Yes, we are able to save an interrupt. */
  1956. if (esp->erev == fashme)
  1957. esp->sreg2 = sbus_readb(esp->eregs + ESP_STATUS2);
  1958. esp->sreg = (sbus_readb(esp->eregs + ESP_STATUS) & ~(ESP_STAT_INTR));
  1959. esp->ireg = sbus_readb(esp->eregs + ESP_INTRPT);
  1960. if (esp->erev == fashme) {
  1961. /* This chip is really losing. */
  1962. ESPHME(("HME["));
  1963. /* Must latch fifo before reading the interrupt
  1964.  * register else garbage ends up in the FIFO
  1965.  * which confuses the driver utterly.
  1966.  * Happy Meal indeed....
  1967.  */
  1968. ESPHME(("fifo_workaround]"));
  1969. if (!(esp->sreg2 & ESP_STAT2_FEMPTY) ||
  1970.     (esp->sreg2 & ESP_STAT2_F1BYTE))
  1971. hme_fifo_read(esp);
  1972. }
  1973. if (!(esp->ireg & ESP_INTR_SR))
  1974. return 0;
  1975. else
  1976. return do_reset_complete;
  1977. }
  1978. /* Ho hum, target is taking forever... */
  1979. scp->SCp.sent_command = new_phase; /* so we don't recurse... */
  1980. return do_intr_end;
  1981. }
  1982. /* Now some dma helpers. */
  1983. static void dma_setup(struct esp *esp, __u32 addr, int count, int write)
  1984. {
  1985. u32 nreg = sbus_readl(esp->dregs + DMA_CSR);
  1986. if (write)
  1987. nreg |= DMA_ST_WRITE;
  1988. else
  1989. nreg &= ~(DMA_ST_WRITE);
  1990. nreg |= DMA_ENABLE;
  1991. sbus_writel(nreg, esp->dregs + DMA_CSR);
  1992. if (esp->dma->revision == dvmaesc1) {
  1993. /* This ESC gate array sucks! */
  1994. __u32 src = addr;
  1995. __u32 dest = src + count;
  1996. if (dest & (PAGE_SIZE - 1))
  1997. count = PAGE_ALIGN(count);
  1998. sbus_writel(count, esp->dregs + DMA_COUNT);
  1999. }
  2000. sbus_writel(addr, esp->dregs + DMA_ADDR);
  2001. }
  2002. static void dma_drain(struct esp *esp)
  2003. {
  2004. u32 tmp;
  2005. if (esp->dma->revision == dvmahme)
  2006. return;
  2007. if ((tmp = sbus_readl(esp->dregs + DMA_CSR)) & DMA_FIFO_ISDRAIN) {
  2008. switch (esp->dma->revision) {
  2009. default:
  2010. tmp |= DMA_FIFO_STDRAIN;
  2011. sbus_writel(tmp, esp->dregs + DMA_CSR);
  2012. case dvmarev3:
  2013. case dvmaesc1:
  2014. while (sbus_readl(esp->dregs + DMA_CSR) & DMA_FIFO_ISDRAIN)
  2015. udelay(1);
  2016. };
  2017. }
  2018. }
  2019. static void dma_invalidate(struct esp *esp)
  2020. {
  2021. u32 tmp;
  2022. if (esp->dma->revision == dvmahme) {
  2023. sbus_writel(DMA_RST_SCSI, esp->dregs + DMA_CSR);
  2024. esp->prev_hme_dmacsr = ((esp->prev_hme_dmacsr |
  2025.  (DMA_PARITY_OFF | DMA_2CLKS |
  2026.   DMA_SCSI_DISAB | DMA_INT_ENAB)) &
  2027. ~(DMA_ST_WRITE | DMA_ENABLE));
  2028. sbus_writel(0, esp->dregs + DMA_CSR);
  2029. sbus_writel(esp->prev_hme_dmacsr, esp->dregs + DMA_CSR);
  2030. /* This is necessary to avoid having the SCSI channel
  2031.  * engine lock up on us.
  2032.  */
  2033. sbus_writel(0, esp->dregs + DMA_ADDR);
  2034. } else {
  2035. while ((tmp = sbus_readl(esp->dregs + DMA_CSR)) & DMA_PEND_READ)
  2036. udelay(1);
  2037. tmp &= ~(DMA_ENABLE | DMA_ST_WRITE | DMA_BCNT_ENAB);
  2038. tmp |= DMA_FIFO_INV;
  2039. sbus_writel(tmp, esp->dregs + DMA_CSR);
  2040. tmp &= ~DMA_FIFO_INV;
  2041. sbus_writel(tmp, esp->dregs + DMA_CSR);
  2042. }
  2043. }
  2044. static inline void dma_flashclear(struct esp *esp)
  2045. {
  2046. dma_drain(esp);
  2047. dma_invalidate(esp);
  2048. }
  2049. static int dma_can_transfer(struct esp *esp, Scsi_Cmnd *sp)
  2050. {
  2051. __u32 base, end, sz;
  2052. if (esp->dma->revision == dvmarev3) {
  2053. sz = sp->SCp.this_residual;
  2054. if (sz > 0x1000000)
  2055. sz = 0x1000000;
  2056. } else {
  2057. base = ((__u32)((unsigned long)sp->SCp.ptr));
  2058. base &= (0x1000000 - 1);
  2059. end = (base + sp->SCp.this_residual);
  2060. if (end > 0x1000000)
  2061. end = 0x1000000;
  2062. sz = (end - base);
  2063. }
  2064. return sz;
  2065. }
  2066. /* Misc. esp helper macros. */
  2067. #define esp_setcount(__eregs, __cnt, __hme) 
  2068. sbus_writeb(((__cnt)&0xff), (__eregs) + ESP_TCLOW); 
  2069. sbus_writeb((((__cnt)>>8)&0xff), (__eregs) + ESP_TCMED); 
  2070. if (__hme) { 
  2071. sbus_writeb((((__cnt)>>16)&0xff), (__eregs) + FAS_RLO); 
  2072. sbus_writeb(0, (__eregs) + FAS_RHI); 
  2073. }
  2074. #define esp_getcount(__eregs, __hme) 
  2075. ((sbus_readb((__eregs) + ESP_TCLOW)&0xff) | 
  2076.  ((sbus_readb((__eregs) + ESP_TCMED)&0xff) << 8) | 
  2077.          ((__hme) ? sbus_readb((__eregs) + FAS_RLO) << 16 : 0))
  2078. #define fcount(__esp) 
  2079. (((__esp)->erev == fashme) ? 
  2080.   (__esp)->hme_fifo_workaround_count : 
  2081.   sbus_readb(((__esp)->eregs) + ESP_FFLAGS) & ESP_FF_FBYTES)
  2082. #define fnzero(__esp) 
  2083. (((__esp)->erev == fashme) ? 0 : 
  2084.  sbus_readb(((__esp)->eregs) + ESP_FFLAGS) & ESP_FF_ONOTZERO)
  2085. /* XXX speculative nops unnecessary when continuing amidst a data phase
  2086.  * XXX even on esp100!!!  another case of flooding the bus with I/O reg
  2087.  * XXX writes...
  2088.  */
  2089. #define esp_maybe_nop(__esp) 
  2090. if ((__esp)->erev == esp100) 
  2091. esp_cmd((__esp), ESP_CMD_NULL)
  2092. #define sreg_to_dataphase(__sreg) 
  2093. ((((__sreg) & ESP_STAT_PMASK) == ESP_DOP) ? in_dataout : in_datain)
  2094. /* The ESP100 when in synchronous data phase, can mistake a long final
  2095.  * REQ pulse from the target as an extra byte, it places whatever is on
  2096.  * the data lines into the fifo.  For now, we will assume when this
  2097.  * happens that the target is a bit quirky and we don't want to
  2098.  * be talking synchronously to it anyways.  Regardless, we need to
  2099.  * tell the ESP to eat the extraneous byte so that we can proceed
  2100.  * to the next phase.
  2101.  */
  2102. static int esp100_sync_hwbug(struct esp *esp, Scsi_Cmnd *sp, int fifocnt)
  2103. {
  2104. /* Do not touch this piece of code. */
  2105. if ((!(esp->erev == esp100)) ||
  2106.     (!(sreg_datainp((esp->sreg = sbus_readb(esp->eregs + ESP_STATUS))) &&
  2107.        !fifocnt) &&
  2108.      !(sreg_dataoutp(esp->sreg) && !fnzero(esp)))) {
  2109. if (sp->SCp.phase == in_dataout)
  2110. esp_cmd(esp, ESP_CMD_FLUSH);
  2111. return 0;
  2112. } else {
  2113. /* Async mode for this guy. */
  2114. build_sync_nego_msg(esp, 0, 0);
  2115. /* Ack the bogus byte, but set ATN first. */
  2116. esp_cmd(esp, ESP_CMD_SATN);
  2117. esp_cmd(esp, ESP_CMD_MOK);
  2118. return 1;
  2119. }
  2120. }
  2121. /* This closes the window during a selection with a reselect pending, because
  2122.  * we use DMA for the selection process the FIFO should hold the correct
  2123.  * contents if we get reselected during this process.  So we just need to
  2124.  * ack the possible illegal cmd interrupt pending on the esp100.
  2125.  */
  2126. static inline int esp100_reconnect_hwbug(struct esp *esp)
  2127. {
  2128. u8 tmp;
  2129. if (esp->erev != esp100)
  2130. return 0;
  2131. tmp = sbus_readb(esp->eregs + ESP_INTRPT);
  2132. if (tmp & ESP_INTR_SR)
  2133. return 1;
  2134. return 0;
  2135. }
  2136. /* This verifies the BUSID bits during a reselection so that we know which
  2137.  * target is talking to us.
  2138.  */
  2139. static inline int reconnect_target(struct esp *esp)
  2140. {
  2141. int it, me = esp->scsi_id_mask, targ = 0;
  2142. if (2 != fcount(esp))
  2143. return -1;
  2144. if (esp->erev == fashme) {
  2145. /* HME does not latch it's own BUS ID bits during
  2146.  * a reselection.  Also the target number is given
  2147.  * as an unsigned char, not as a sole bit number
  2148.  * like the other ESP's do.
  2149.  * Happy Meal indeed....
  2150.  */
  2151. targ = esp->hme_fifo_workaround_buffer[0];
  2152. } else {
  2153. it = sbus_readb(esp->eregs + ESP_FDATA);
  2154. if (!(it & me))
  2155. return -1;
  2156. it &= ~me;
  2157. if (it & (it - 1))
  2158. return -1;
  2159. while (!(it & 1))
  2160. targ++, it >>= 1;
  2161. }
  2162. return targ;
  2163. }
  2164. /* This verifies the identify from the target so that we know which lun is
  2165.  * being reconnected.
  2166.  */
  2167. static inline int reconnect_lun(struct esp *esp)
  2168. {
  2169. int lun;
  2170. if ((esp->sreg & ESP_STAT_PMASK) != ESP_MIP)
  2171. return -1;
  2172. if (esp->erev == fashme)
  2173. lun = esp->hme_fifo_workaround_buffer[1];
  2174. else
  2175. lun = sbus_readb(esp->eregs + ESP_FDATA);
  2176. /* Yes, you read this correctly.  We report lun of zero
  2177.  * if we see parity error.  ESP reports parity error for
  2178.  * the lun byte, and this is the only way to hope to recover
  2179.  * because the target is connected.
  2180.  */
  2181. if (esp->sreg & ESP_STAT_PERR)
  2182. return 0;
  2183. /* Check for illegal bits being set in the lun. */
  2184. if ((lun & 0x40) || !(lun & 0x80))
  2185. return -1;
  2186. return lun & 7;
  2187. }
  2188. /* This puts the driver in a state where it can revitalize a command that
  2189.  * is being continued due to reselection.
  2190.  */
  2191. static inline void esp_connect(struct esp *esp, Scsi_Cmnd *sp)
  2192. {
  2193. Scsi_Device *dp = sp->device;
  2194. if (esp->prev_soff  != dp->sync_max_offset ||
  2195.     esp->prev_stp   != dp->sync_min_period ||
  2196.     (esp->erev > esp100a &&
  2197.      esp->prev_cfg3 != esp->config3[sp->target])) {
  2198. esp->prev_soff = dp->sync_max_offset;
  2199. esp->prev_stp = dp->sync_min_period;
  2200. sbus_writeb(esp->prev_soff, esp->eregs + ESP_SOFF);
  2201. sbus_writeb(esp->prev_stp, esp->eregs + ESP_STP);
  2202. if (esp->erev > esp100a) {
  2203. esp->prev_cfg3 = esp->config3[sp->target];
  2204. sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
  2205. }
  2206. }
  2207. esp->current_SC = sp;
  2208. }
  2209. /* This will place the current working command back into the issue queue
  2210.  * if we are to receive a reselection amidst a selection attempt.
  2211.  */
  2212. static inline void esp_reconnect(struct esp *esp, Scsi_Cmnd *sp)
  2213. {
  2214. if (!esp->disconnected_SC)
  2215. ESPLOG(("esp%d: Weird, being reselected but disconnected "
  2216. "command queue is empty.n", esp->esp_id));
  2217. esp->snip = 0;
  2218. esp->current_SC = 0;
  2219. sp->SCp.phase = not_issued;
  2220. append_SC(&esp->issue_SC, sp);
  2221. }
  2222. /* Begin message in phase. */
  2223. static int esp_do_msgin(struct esp *esp)
  2224. {
  2225. /* Must be very careful with the fifo on the HME */
  2226. if ((esp->erev != fashme) ||
  2227.     !(sbus_readb(esp->eregs + ESP_STATUS2) & ESP_STAT2_FEMPTY))
  2228. esp_cmd(esp, ESP_CMD_FLUSH);
  2229. esp_maybe_nop(esp);
  2230. esp_cmd(esp, ESP_CMD_TI);
  2231. esp->msgin_len = 1;
  2232. esp->msgin_ctr = 0;
  2233. esp_advance_phase(esp->current_SC, in_msgindone);
  2234. return do_work_bus;
  2235. }
  2236. /* This uses various DMA csr fields and the fifo flags count value to
  2237.  * determine how many bytes were successfully sent/received by the ESP.
  2238.  */
  2239. static inline int esp_bytes_sent(struct esp *esp, int fifo_count)
  2240. {
  2241. int rval = sbus_readl(esp->dregs + DMA_ADDR) - esp->esp_command_dvma;
  2242. if (esp->dma->revision == dvmarev1)
  2243. rval -= (4 - ((sbus_readl(esp->dregs + DMA_CSR) & DMA_READ_AHEAD)>>11));
  2244. return rval - fifo_count;
  2245. }
  2246. static inline void advance_sg(Scsi_Cmnd *sp)
  2247. {
  2248. ++sp->SCp.buffer;
  2249. --sp->SCp.buffers_residual;
  2250. sp->SCp.this_residual = sg_dma_len(sp->SCp.buffer);
  2251. sp->SCp.ptr = (char *)((unsigned long)sg_dma_address(sp->SCp.buffer));
  2252. }
  2253. /* Please note that the way I've coded these routines is that I _always_
  2254.  * check for a disconnect during any and all information transfer
  2255.  * phases.  The SCSI standard states that the target _can_ cause a BUS
  2256.  * FREE condition by dropping all MSG/CD/IO/BSY signals.  Also note
  2257.  * that during information transfer phases the target controls every
  2258.  * change in phase, the only thing the initiator can do is "ask" for
  2259.  * a message out phase by driving ATN true.  The target can, and sometimes
  2260.  * will, completely ignore this request so we cannot assume anything when
  2261.  * we try to force a message out phase to abort/reset a target.  Most of
  2262.  * the time the target will eventually be nice and go to message out, so
  2263.  * we may have to hold on to our state about what we want to tell the target
  2264.  * for some period of time.
  2265.  */
  2266. /* I think I have things working here correctly.  Even partial transfers
  2267.  * within a buffer or sub-buffer should not upset us at all no matter
  2268.  * how bad the target and/or ESP fucks things up.
  2269.  */
  2270. static int esp_do_data(struct esp *esp)
  2271. {
  2272. Scsi_Cmnd *SCptr = esp->current_SC;
  2273. int thisphase, hmuch;
  2274. ESPDATA(("esp_do_data: "));
  2275. esp_maybe_nop(esp);
  2276. thisphase = sreg_to_dataphase(esp->sreg);
  2277. esp_advance_phase(SCptr, thisphase);
  2278. ESPDATA(("newphase<%s> ", (thisphase == in_datain) ? "DATAIN" : "DATAOUT"));
  2279. hmuch = dma_can_transfer(esp, SCptr);
  2280. if (hmuch > (64 * 1024) && (esp->erev != fashme))
  2281. hmuch = (64 * 1024);
  2282. ESPDATA(("hmuch<%d> ", hmuch));
  2283. esp->current_transfer_size = hmuch;
  2284. if (esp->erev == fashme) {
  2285. u32 tmp = esp->prev_hme_dmacsr;
  2286. /* Always set the ESP count registers first. */
  2287. esp_setcount(esp->eregs, hmuch, 1);
  2288. /* Get the DMA csr computed. */
  2289. tmp |= (DMA_SCSI_DISAB | DMA_ENABLE);
  2290. if (thisphase == in_datain)
  2291. tmp |= DMA_ST_WRITE;
  2292. else
  2293. tmp &= ~(DMA_ST_WRITE);
  2294. esp->prev_hme_dmacsr = tmp;
  2295. ESPDATA(("DMA|TI --> do_intr_endn"));
  2296. if (thisphase == in_datain) {
  2297. sbus_writel(hmuch, esp->dregs + DMA_COUNT);
  2298. esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
  2299. } else {
  2300. esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
  2301. sbus_writel(hmuch, esp->dregs + DMA_COUNT);
  2302. }
  2303. sbus_writel((__u32)((unsigned long)SCptr->SCp.ptr), esp->dregs+DMA_ADDR);
  2304. sbus_writel(esp->prev_hme_dmacsr, esp->dregs + DMA_CSR);
  2305. } else {
  2306. esp_setcount(esp->eregs, hmuch, 0);
  2307. dma_setup(esp, ((__u32)((unsigned long)SCptr->SCp.ptr)),
  2308.   hmuch, (thisphase == in_datain));
  2309. ESPDATA(("DMA|TI --> do_intr_endn"));
  2310. esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
  2311. }
  2312. return do_intr_end;
  2313. }
  2314. /* See how successful the data transfer was. */
  2315. static int esp_do_data_finale(struct esp *esp)
  2316. {
  2317. Scsi_Cmnd *SCptr = esp->current_SC;
  2318. int bogus_data = 0, bytes_sent = 0, fifocnt, ecount = 0;
  2319. ESPDATA(("esp_do_data_finale: "));
  2320. if (SCptr->SCp.phase == in_datain) {
  2321. if (esp->sreg & ESP_STAT_PERR) {
  2322. /* Yuck, parity error.  The ESP asserts ATN
  2323.  * so that we can go to message out phase
  2324.  * immediately and inform the target that
  2325.  * something bad happened.
  2326.  */
  2327. ESPLOG(("esp%d: data bad parity detected.n",
  2328. esp->esp_id));
  2329. esp->cur_msgout[0] = INITIATOR_ERROR;
  2330. esp->msgout_len = 1;
  2331. }
  2332. dma_drain(esp);
  2333. }
  2334. dma_invalidate(esp);
  2335. /* This could happen for the above parity error case. */
  2336. if (esp->ireg != ESP_INTR_BSERV) {
  2337. /* Please go to msgout phase, please please please... */
  2338. ESPLOG(("esp%d: !BSERV after data, probably to msgoutn",
  2339. esp->esp_id));
  2340. return esp_do_phase_determine(esp);
  2341. }
  2342. /* Check for partial transfers and other horrible events.
  2343.  * Note, here we read the real fifo flags register even
  2344.  * on HME broken adapters because we skip the HME fifo
  2345.  * workaround code in esp_handle() if we are doing data
  2346.  * phase things.  We don't want to fuck directly with
  2347.  * the fifo like that, especially if doing synchronous
  2348.  * transfers!  Also, will need to double the count on
  2349.  * HME if we are doing wide transfers, as the HME fifo
  2350.  * will move and count 16-bit quantities during wide data.
  2351.  * SMCC _and_ Qlogic can both bite me.
  2352.  */
  2353. fifocnt = (sbus_readb(esp->eregs + ESP_FFLAGS) & ESP_FF_FBYTES);
  2354. if (esp->erev != fashme)
  2355. ecount = esp_getcount(esp->eregs, 0);
  2356. bytes_sent = esp->current_transfer_size;
  2357. ESPDATA(("trans_sz(%d), ", bytes_sent));
  2358. if (esp->erev == fashme) {
  2359. if (!(esp->sreg & ESP_STAT_TCNT)) {
  2360. ecount = esp_getcount(esp->eregs, 1);
  2361. bytes_sent -= ecount;
  2362. }
  2363. /* Always subtract any cruft remaining in the FIFO. */
  2364. if (esp->prev_cfg3 & ESP_CONFIG3_EWIDE)
  2365. fifocnt <<= 1;
  2366. if (SCptr->SCp.phase == in_dataout)
  2367. bytes_sent -= fifocnt;
  2368. /* I have an IBM disk which exhibits the following
  2369.  * behavior during writes to it.  It disconnects in
  2370.  * the middle of a partial transfer, the current sglist
  2371.  * buffer is 1024 bytes, the disk stops data transfer
  2372.  * at 512 bytes.
  2373.  *
  2374.  * However the FAS366 reports that 32 more bytes were
  2375.  * transferred than really were.  This is precisely
  2376.  * the size of a fully loaded FIFO in wide scsi mode.
  2377.  * The FIFO state recorded indicates that it is empty.
  2378.  *
  2379.  * I have no idea if this is a bug in the FAS366 chip
  2380.  * or a bug in the firmware on this IBM disk.  In any
  2381.  * event the following seems to be a good workaround.  -DaveM
  2382.  */
  2383. if (bytes_sent != esp->current_transfer_size &&
  2384.     SCptr->SCp.phase == in_dataout) {
  2385. int mask = (64 - 1);
  2386. if ((esp->prev_cfg3 & ESP_CONFIG3_EWIDE) == 0)
  2387. mask >>= 1;
  2388. if (bytes_sent & mask)
  2389. bytes_sent -= (bytes_sent & mask);
  2390. }
  2391. } else {
  2392. if (!(esp->sreg & ESP_STAT_TCNT))
  2393. bytes_sent -= ecount;
  2394. if (SCptr->SCp.phase == in_dataout)
  2395. bytes_sent -= fifocnt;
  2396. }
  2397. ESPDATA(("bytes_sent(%d), ", bytes_sent));
  2398. /* If we were in synchronous mode, check for peculiarities. */
  2399. if (esp->erev == fashme) {
  2400. if (SCptr->device->sync_max_offset) {
  2401. if (SCptr->SCp.phase == in_dataout)
  2402. esp_cmd(esp, ESP_CMD_FLUSH);
  2403. } else {
  2404. esp_cmd(esp, ESP_CMD_FLUSH);
  2405. }
  2406. } else {
  2407. if (SCptr->device->sync_max_offset)
  2408. bogus_data = esp100_sync_hwbug(esp, SCptr, fifocnt);
  2409. else
  2410. esp_cmd(esp, ESP_CMD_FLUSH);
  2411. }
  2412. /* Until we are sure of what has happened, we are certainly
  2413.  * in the dark.
  2414.  */
  2415. esp_advance_phase(SCptr, in_the_dark);
  2416. if (bytes_sent < 0) {
  2417. /* I've seen this happen due to lost state in this
  2418.  * driver.  No idea why it happened, but allowing
  2419.  * this value to be negative caused things to
  2420.  * lock up.  This allows greater chance of recovery.
  2421.  * In fact every time I've seen this, it has been
  2422.  * a driver bug without question.
  2423.  */
  2424. ESPLOG(("esp%d: yieee, bytes_sent < 0!n", esp->esp_id));
  2425. ESPLOG(("esp%d: csz=%d fifocount=%d ecount=%dn",
  2426. esp->esp_id,
  2427. esp->current_transfer_size, fifocnt, ecount));
  2428. ESPLOG(("esp%d: use_sg=%d ptr=%p this_residual=%dn",
  2429. esp->esp_id,
  2430. SCptr->use_sg, SCptr->SCp.ptr, SCptr->SCp.this_residual));
  2431. ESPLOG(("esp%d: Forcing async for target %dn", esp->esp_id, 
  2432. SCptr->target));
  2433. SCptr->device->borken = 1;
  2434. SCptr->device->sync = 0;
  2435. bytes_sent = 0;
  2436. }
  2437. /* Update the state of our transfer. */
  2438. SCptr->SCp.ptr += bytes_sent;
  2439. SCptr->SCp.this_residual -= bytes_sent;
  2440. if (SCptr->SCp.this_residual < 0) {
  2441. /* shit */
  2442. ESPLOG(("esp%d: Data transfer overrun.n", esp->esp_id));
  2443. SCptr->SCp.this_residual = 0;
  2444. }
  2445. /* Maybe continue. */
  2446. if (!bogus_data) {
  2447. ESPDATA(("!bogus_data, "));
  2448. /* NO MATTER WHAT, we advance the scatterlist,
  2449.  * if the target should decide to disconnect
  2450.  * in between scatter chunks (which is common)
  2451.  * we could die horribly!  I used to have the sg
  2452.  * advance occur only if we are going back into
  2453.  * (or are staying in) a data phase, you can
  2454.  * imagine the hell I went through trying to
  2455.  * figure this out.
  2456.  */
  2457. if (SCptr->use_sg && !SCptr->SCp.this_residual)
  2458. advance_sg(SCptr);
  2459. if (sreg_datainp(esp->sreg) || sreg_dataoutp(esp->sreg)) {
  2460. ESPDATA(("to more datan"));
  2461. return esp_do_data(esp);
  2462. }
  2463. ESPDATA(("to new phasen"));
  2464. return esp_do_phase_determine(esp);
  2465. }
  2466. /* Bogus data, just wait for next interrupt. */
  2467. ESPLOG(("esp%d: bogus_data during end of data phasen",
  2468. esp->esp_id));
  2469. return do_intr_end;
  2470. }
  2471. /* We received a non-good status return at the end of
  2472.  * running a SCSI command.  This is used to decide if
  2473.  * we should clear our synchronous transfer state for
  2474.  * such a device when that happens.
  2475.  *
  2476.  * The idea is that when spinning up a disk or rewinding
  2477.  * a tape, we don't want to go into a loop re-negotiating
  2478.  * synchronous capabilities over and over.
  2479.  */
  2480. static int esp_should_clear_sync(Scsi_Cmnd *sp)
  2481. {
  2482. u8 cmd1 = sp->cmnd[0];
  2483. u8 cmd2 = sp->data_cmnd[0];
  2484. /* These cases are for spinning up a disk and
  2485.  * waiting for that spinup to complete.
  2486.  */
  2487. if (cmd1 == START_STOP ||
  2488.     cmd2 == START_STOP)
  2489. return 0;
  2490. if (cmd1 == TEST_UNIT_READY ||
  2491.     cmd2 == TEST_UNIT_READY)
  2492. return 0;
  2493. /* One more special case for SCSI tape drives,
  2494.  * this is what is used to probe the device for
  2495.  * completion of a rewind or tape load operation.
  2496.  */
  2497. if (sp->device->type == TYPE_TAPE) {
  2498. if (cmd1 == MODE_SENSE ||
  2499.     cmd2 == MODE_SENSE)
  2500. return 0;
  2501. }
  2502. return 1;
  2503. }
  2504. /* Either a command is completing or a target is dropping off the bus
  2505.  * to continue the command in the background so we can do other work.
  2506.  */
  2507. static int esp_do_freebus(struct esp *esp)
  2508. {
  2509. Scsi_Cmnd *SCptr = esp->current_SC;
  2510. int rval;
  2511. rval = skipahead2(esp, SCptr, in_status, in_msgindone, in_freeing);
  2512. if (rval)
  2513. return rval;
  2514. if (esp->ireg != ESP_INTR_DC) {
  2515. ESPLOG(("esp%d: Target will not disconnectn", esp->esp_id));
  2516. return do_reset_bus; /* target will not drop BSY... */
  2517. }
  2518. esp->msgout_len = 0;
  2519. esp->prevmsgout = NOP;
  2520. if (esp->prevmsgin == COMMAND_COMPLETE) {
  2521. /* Normal end of nexus. */
  2522. if (esp->disconnected_SC || (esp->erev == fashme))
  2523. esp_cmd(esp, ESP_CMD_ESEL);
  2524. if (SCptr->SCp.Status != GOOD &&
  2525.     SCptr->SCp.Status != CONDITION_GOOD &&
  2526.     ((1<<SCptr->target) & esp->targets_present) &&
  2527.     SCptr->device->sync &&
  2528.     SCptr->device->sync_max_offset) {
  2529. /* SCSI standard says that the synchronous capabilities
  2530.  * should be renegotiated at this point.  Most likely
  2531.  * we are about to request sense from this target
  2532.  * in which case we want to avoid using sync
  2533.  * transfers until we are sure of the current target
  2534.  * state.
  2535.  */
  2536. ESPMISC(("esp: Status <%d> for target %d lun %dn",
  2537.  SCptr->SCp.Status, SCptr->target, SCptr->lun));
  2538. /* But don't do this when spinning up a disk at
  2539.  * boot time while we poll for completion as it
  2540.  * fills up the console with messages.  Also, tapes
  2541.  * can report not ready many times right after
  2542.  * loading up a tape.
  2543.  */
  2544. if (esp_should_clear_sync(SCptr) != 0)
  2545. SCptr->device->sync = 0;
  2546. }
  2547. ESPDISC(("F<%02x,%02x>", SCptr->target, SCptr->lun));
  2548. esp_done(esp, ((SCptr->SCp.Status & 0xff) |
  2549.        ((SCptr->SCp.Message & 0xff)<<8) |
  2550.        (DID_OK << 16)));
  2551. } else if (esp->prevmsgin == DISCONNECT) {
  2552. /* Normal disconnect. */
  2553. esp_cmd(esp, ESP_CMD_ESEL);
  2554. ESPDISC(("D<%02x,%02x>", SCptr->target, SCptr->lun));
  2555. append_SC(&esp->disconnected_SC, SCptr);
  2556. esp->current_SC = NULL;
  2557. if (esp->issue_SC)
  2558. esp_exec_cmd(esp);
  2559. } else {
  2560. /* Driver bug, we do not expect a disconnect here
  2561.  * and should not have advanced the state engine
  2562.  * to in_freeing.
  2563.  */
  2564. ESPLOG(("esp%d: last msg not disc and not cmd cmplt.n",
  2565. esp->esp_id));
  2566. return do_reset_bus;
  2567. }
  2568. return do_intr_end;
  2569. }
  2570. /* When a reselect occurs, and we cannot find the command to
  2571.  * reconnect to in our queues, we do this.
  2572.  */
  2573. static int esp_bad_reconnect(struct esp *esp)
  2574. {
  2575. Scsi_Cmnd *sp;
  2576. ESPLOG(("esp%d: Eieeee, reconnecting unknown command!n",
  2577. esp->esp_id));
  2578. ESPLOG(("QUEUE DUMPn"));
  2579. sp = esp->issue_SC;
  2580. ESPLOG(("esp%d: issue_SC[", esp->esp_id));
  2581. while (sp) {
  2582. ESPLOG(("<%02x,%02x>", sp->target, sp->lun));
  2583. sp = (Scsi_Cmnd *) sp->host_scribble;
  2584. }
  2585. ESPLOG(("]n"));
  2586. sp = esp->current_SC;
  2587. ESPLOG(("esp%d: current_SC[", esp->esp_id));
  2588. if (sp)
  2589. ESPLOG(("<%02x,%02x>", sp->target, sp->lun));
  2590. else
  2591. ESPLOG(("<NULL>"));
  2592. ESPLOG(("]n"));
  2593. sp = esp->disconnected_SC;
  2594. ESPLOG(("esp%d: disconnected_SC[", esp->esp_id));
  2595. while (sp) {
  2596. ESPLOG(("<%02x,%02x>", sp->target, sp->lun));
  2597. sp = (Scsi_Cmnd *) sp->host_scribble;
  2598. }
  2599. ESPLOG(("]n"));
  2600. return do_reset_bus;
  2601. }
  2602. /* Do the needy when a target tries to reconnect to us. */
  2603. static int esp_do_reconnect(struct esp *esp)
  2604. {
  2605. int lun, target;
  2606. Scsi_Cmnd *SCptr;
  2607. /* Check for all bogus conditions first. */
  2608. target = reconnect_target(esp);
  2609. if (target < 0) {
  2610. ESPDISC(("bad bus bitsn"));
  2611. return do_reset_bus;
  2612. }
  2613. lun = reconnect_lun(esp);
  2614. if (lun < 0) {
  2615. ESPDISC(("target=%2x, bad identify msgn", target));
  2616. return do_reset_bus;
  2617. }
  2618. /* Things look ok... */
  2619. ESPDISC(("R<%02x,%02x>", target, lun));
  2620. /* Must not flush FIFO or DVMA on HME. */
  2621. if (esp->erev != fashme) {
  2622. esp_cmd(esp, ESP_CMD_FLUSH);
  2623. if (esp100_reconnect_hwbug(esp))
  2624. return do_reset_bus;
  2625. esp_cmd(esp, ESP_CMD_NULL);
  2626. }
  2627. SCptr = remove_SC(&esp->disconnected_SC, (u8) target, (u8) lun);
  2628. if (!SCptr)
  2629. return esp_bad_reconnect(esp);
  2630. esp_connect(esp, SCptr);
  2631. esp_cmd(esp, ESP_CMD_MOK);
  2632. if (esp->erev == fashme)
  2633. sbus_writeb(((SCptr->target & 0xf) |
  2634.      (ESP_BUSID_RESELID | ESP_BUSID_CTR32BIT)),
  2635.     esp->eregs + ESP_BUSID);
  2636. /* Reconnect implies a restore pointers operation. */
  2637. esp_restore_pointers(esp, SCptr);
  2638. esp->snip = 0;
  2639. esp_advance_phase(SCptr, in_the_dark);
  2640. return do_intr_end;
  2641. }
  2642. /* End of NEXUS (hopefully), pick up status + message byte then leave if
  2643.  * all goes well.
  2644.  */
  2645. static int esp_do_status(struct esp *esp)
  2646. {
  2647. Scsi_Cmnd *SCptr = esp->current_SC;
  2648. int intr, rval;
  2649. rval = skipahead1(esp, SCptr, in_the_dark, in_status);
  2650. if (rval)
  2651. return rval;
  2652. intr = esp->ireg;
  2653. ESPSTAT(("esp_do_status: "));
  2654. if (intr != ESP_INTR_DC) {
  2655. int message_out = 0; /* for parity problems */
  2656. /* Ack the message. */
  2657. ESPSTAT(("ack msg, "));
  2658. esp_cmd(esp, ESP_CMD_MOK);
  2659. if (esp->erev != fashme) {
  2660. dma_flashclear(esp);
  2661. /* Wait till the first bits settle. */
  2662. while (esp->esp_command[0] == 0xff)
  2663. udelay(1);
  2664. } else {
  2665. esp->esp_command[0] = esp->hme_fifo_workaround_buffer[0];
  2666. esp->esp_command[1] = esp->hme_fifo_workaround_buffer[1];
  2667. }
  2668. ESPSTAT(("got something, "));
  2669. /* ESP chimes in with one of
  2670.  *
  2671.  * 1) function done interrupt:
  2672.  * both status and message in bytes
  2673.  * are available
  2674.  *
  2675.  * 2) bus service interrupt:
  2676.  * only status byte was acquired
  2677.  *
  2678.  * 3) Anything else:
  2679.  * can't happen, but we test for it
  2680.  * anyways
  2681.  *
  2682.  * ALSO: If bad parity was detected on either
  2683.  *       the status _or_ the message byte then
  2684.  *       the ESP has asserted ATN on the bus
  2685.  *       and we must therefore wait for the
  2686.  *       next phase change.
  2687.  */
  2688. if (intr & ESP_INTR_FDONE) {
  2689. /* We got it all, hallejulia. */
  2690. ESPSTAT(("got both, "));
  2691. SCptr->SCp.Status = esp->esp_command[0];
  2692. SCptr->SCp.Message = esp->esp_command[1];
  2693. esp->prevmsgin = SCptr->SCp.Message;
  2694. esp->cur_msgin[0] = SCptr->SCp.Message;
  2695. if (esp->sreg & ESP_STAT_PERR) {
  2696. /* There was bad parity for the
  2697.  * message byte, the status byte
  2698.  * was ok.
  2699.  */
  2700. message_out = MSG_PARITY_ERROR;
  2701. }
  2702. } else if (intr == ESP_INTR_BSERV) {
  2703. /* Only got status byte. */
  2704. ESPLOG(("esp%d: got status only, ", esp->esp_id));
  2705. if (!(esp->sreg & ESP_STAT_PERR)) {
  2706. SCptr->SCp.Status = esp->esp_command[0];
  2707. SCptr->SCp.Message = 0xff;
  2708. } else {
  2709. /* The status byte had bad parity.
  2710.  * we leave the scsi_pointer Status
  2711.  * field alone as we set it to a default
  2712.  * of CHECK_CONDITION in esp_queue.
  2713.  */
  2714. message_out = INITIATOR_ERROR;
  2715. }
  2716. } else {
  2717. /* This shouldn't happen ever. */
  2718. ESPSTAT(("got bolixedn"));
  2719. esp_advance_phase(SCptr, in_the_dark);
  2720. return esp_do_phase_determine(esp);
  2721. }
  2722. if (!message_out) {
  2723. ESPSTAT(("status=%2x msg=%2x, ", SCptr->SCp.Status,
  2724. SCptr->SCp.Message));
  2725. if (SCptr->SCp.Message == COMMAND_COMPLETE) {
  2726. ESPSTAT(("and was COMMAND_COMPLETEn"));
  2727. esp_advance_phase(SCptr, in_freeing);
  2728. return esp_do_freebus(esp);
  2729. } else {
  2730. ESPLOG(("esp%d: and _not_ COMMAND_COMPLETEn",
  2731. esp->esp_id));
  2732. esp->msgin_len = esp->msgin_ctr = 1;
  2733. esp_advance_phase(SCptr, in_msgindone);
  2734. return esp_do_msgindone(esp);
  2735. }
  2736. } else {
  2737. /* With luck we'll be able to let the target
  2738.  * know that bad parity happened, it will know
  2739.  * which byte caused the problems and send it
  2740.  * again.  For the case where the status byte
  2741.  * receives bad parity, I do not believe most
  2742.  * targets recover very well.  We'll see.
  2743.  */
  2744. ESPLOG(("esp%d: bad parity somewhere mout=%2xn",
  2745. esp->esp_id, message_out));
  2746. esp->cur_msgout[0] = message_out;
  2747. esp->msgout_len = esp->msgout_ctr = 1;
  2748. esp_advance_phase(SCptr, in_the_dark);
  2749. return esp_do_phase_determine(esp);
  2750. }
  2751. } else {
  2752. /* If we disconnect now, all hell breaks loose. */
  2753. ESPLOG(("esp%d: whoops, disconnectn", esp->esp_id));
  2754. esp_advance_phase(SCptr, in_the_dark);
  2755. return esp_do_phase_determine(esp);
  2756. }
  2757. }
  2758. static int esp_enter_status(struct esp *esp)
  2759. {
  2760. u8 thecmd = ESP_CMD_ICCSEQ;
  2761. esp_cmd(esp, ESP_CMD_FLUSH);
  2762. if (esp->erev != fashme) {
  2763. u32 tmp;
  2764. esp->esp_command[0] = esp->esp_command[1] = 0xff;
  2765. sbus_writeb(2, esp->eregs + ESP_TCLOW);
  2766. sbus_writeb(0, esp->eregs + ESP_TCMED);
  2767. tmp = sbus_readl(esp->dregs + DMA_CSR);
  2768. tmp |= (DMA_ST_WRITE | DMA_ENABLE);
  2769. sbus_writel(tmp, esp->dregs + DMA_CSR);
  2770. if (esp->dma->revision == dvmaesc1)
  2771. sbus_writel(0x100, esp->dregs + DMA_COUNT);
  2772. sbus_writel(esp->esp_command_dvma, esp->dregs + DMA_ADDR);
  2773. thecmd |= ESP_CMD_DMA;
  2774. }
  2775. esp_cmd(esp, thecmd);
  2776. esp_advance_phase(esp->current_SC, in_status);
  2777. return esp_do_status(esp);
  2778. }
  2779. static int esp_disconnect_amidst_phases(struct esp *esp)
  2780. {
  2781. Scsi_Cmnd *sp = esp->current_SC;
  2782. Scsi_Device *dp = sp->device;
  2783. /* This means real problems if we see this
  2784.  * here.  Unless we were actually trying
  2785.  * to force the device to abort/reset.
  2786.  */
  2787. ESPLOG(("esp%d Disconnect amidst phases, ", esp->esp_id));
  2788. ESPLOG(("pphase<%s> cphase<%s>, ",
  2789. phase_string(sp->SCp.phase),
  2790. phase_string(sp->SCp.sent_command)));
  2791. if (esp->disconnected_SC != NULL || (esp->erev == fashme))
  2792. esp_cmd(esp, ESP_CMD_ESEL);
  2793. switch (esp->cur_msgout[0]) {
  2794. default:
  2795. /* We didn't expect this to happen at all. */
  2796. ESPLOG(("device is bolixedn"));
  2797. esp_advance_phase(sp, in_tgterror);
  2798. esp_done(esp, (DID_ERROR << 16));
  2799. break;
  2800. case BUS_DEVICE_RESET:
  2801. ESPLOG(("device reset successfuln"));
  2802. dp->sync_max_offset = 0;
  2803. dp->sync_min_period = 0;
  2804. dp->sync = 0;
  2805. esp_advance_phase(sp, in_resetdev);
  2806. esp_done(esp, (DID_RESET << 16));
  2807. break;
  2808. case ABORT:
  2809. ESPLOG(("device abort successfuln"));
  2810. esp_advance_phase(sp, in_abortone);
  2811. esp_done(esp, (DID_ABORT << 16));
  2812. break;
  2813. };
  2814. return do_intr_end;
  2815. }
  2816. static int esp_enter_msgout(struct esp *esp)
  2817. {
  2818. esp_advance_phase(esp->current_SC, in_msgout);
  2819. return esp_do_msgout(esp);
  2820. }
  2821. static int esp_enter_msgin(struct esp *esp)
  2822. {
  2823. esp_advance_phase(esp->current_SC, in_msgin);
  2824. return esp_do_msgin(esp);
  2825. }
  2826. static int esp_enter_cmd(struct esp *esp)
  2827. {
  2828. esp_advance_phase(esp->current_SC, in_cmdbegin);
  2829. return esp_do_cmdbegin(esp);
  2830. }
  2831. static int esp_enter_badphase(struct esp *esp)
  2832. {
  2833. ESPLOG(("esp%d: Bizarre bus phase %2x.n", esp->esp_id,
  2834. esp->sreg & ESP_STAT_PMASK));
  2835. return do_reset_bus;
  2836. }
  2837. typedef int (*espfunc_t)(struct esp *);
  2838. static espfunc_t phase_vector[] = {
  2839. esp_do_data, /* ESP_DOP */
  2840. esp_do_data, /* ESP_DIP */
  2841. esp_enter_cmd, /* ESP_CMDP */
  2842. esp_enter_status, /* ESP_STATP */
  2843. esp_enter_badphase, /* ESP_STAT_PMSG */
  2844. esp_enter_badphase, /* ESP_STAT_PMSG | ESP_STAT_PIO */
  2845. esp_enter_msgout, /* ESP_MOP */
  2846. esp_enter_msgin, /* ESP_MIP */
  2847. };
  2848. /* The target has control of the bus and we have to see where it has
  2849.  * taken us.
  2850.  */
  2851. static int esp_do_phase_determine(struct esp *esp)
  2852. {
  2853. if ((esp->ireg & ESP_INTR_DC) != 0)
  2854. return esp_disconnect_amidst_phases(esp);
  2855. return phase_vector[esp->sreg & ESP_STAT_PMASK](esp);
  2856. }
  2857. /* First interrupt after exec'ing a cmd comes here. */
  2858. static int esp_select_complete(struct esp *esp)
  2859. {
  2860. Scsi_Cmnd *SCptr = esp->current_SC;
  2861. Scsi_Device *SDptr = SCptr->device;
  2862. int cmd_bytes_sent, fcnt;
  2863. if (esp->erev != fashme)
  2864. esp->seqreg = (sbus_readb(esp->eregs + ESP_SSTEP) & ESP_STEP_VBITS);
  2865. if (esp->erev == fashme)
  2866. fcnt = esp->hme_fifo_workaround_count;
  2867. else
  2868. fcnt = (sbus_readb(esp->eregs + ESP_FFLAGS) & ESP_FF_FBYTES);
  2869. cmd_bytes_sent = esp_bytes_sent(esp, fcnt);
  2870. dma_invalidate(esp);
  2871. /* Let's check to see if a reselect happened
  2872.  * while we we're trying to select.  This must
  2873.  * be checked first.
  2874.  */
  2875. if (esp->ireg == (ESP_INTR_RSEL | ESP_INTR_FDONE)) {
  2876. esp_reconnect(esp, SCptr);
  2877. return esp_do_reconnect(esp);
  2878. }
  2879. /* Looks like things worked, we should see a bus service &
  2880.  * a function complete interrupt at this point.  Note we
  2881.  * are doing a direct comparison because we don't want to
  2882.  * be fooled into thinking selection was successful if
  2883.  * ESP_INTR_DC is set, see below.
  2884.  */
  2885. if (esp->ireg == (ESP_INTR_FDONE | ESP_INTR_BSERV)) {
  2886. /* target speaks... */
  2887. esp->targets_present |= (1<<SCptr->target);
  2888. /* What if the target ignores the sdtr? */
  2889. if (esp->snip)
  2890. SDptr->sync = 1;
  2891. /* See how far, if at all, we got in getting
  2892.  * the information out to the target.
  2893.  */
  2894. switch (esp->seqreg) {
  2895. default:
  2896. case ESP_STEP_ASEL:
  2897. /* Arbitration won, target selected, but
  2898.  * we are in some phase which is not command
  2899.  * phase nor is it message out phase.
  2900.  *
  2901.  * XXX We've confused the target, obviously.
  2902.  * XXX So clear it's state, but we also end
  2903.  * XXX up clearing everyone elses.  That isn't
  2904.  * XXX so nice.  I'd like to just reset this
  2905.  * XXX target, but if I cannot even get it's
  2906.  * XXX attention and finish selection to talk
  2907.  * XXX to it, there is not much more I can do.
  2908.  * XXX If we have a loaded bus we're going to
  2909.  * XXX spend the next second or so renegotiating
  2910.  * XXX for synchronous transfers.
  2911.  */
  2912. ESPLOG(("esp%d: STEP_ASEL for tgt %dn",
  2913. esp->esp_id, SCptr->target));
  2914. case ESP_STEP_SID:
  2915. /* Arbitration won, target selected, went
  2916.  * to message out phase, sent one message
  2917.  * byte, then we stopped.  ATN is asserted
  2918.  * on the SCSI bus and the target is still
  2919.  * there hanging on.  This is a legal
  2920.  * sequence step if we gave the ESP a select
  2921.  * and stop command.
  2922.  *
  2923.  * XXX See above, I could set the borken flag
  2924.  * XXX in the device struct and retry the
  2925.  * XXX command.  But would that help for
  2926.  * XXX tagged capable targets?
  2927.  */
  2928. case ESP_STEP_NCMD:
  2929. /* Arbitration won, target selected, maybe
  2930.  * sent the one message byte in message out
  2931.  * phase, but we did not go to command phase
  2932.  * in the end.  Actually, we could have sent
  2933.  * only some of the message bytes if we tried
  2934.  * to send out the entire identify and tag
  2935.  * message using ESP_CMD_SA3.
  2936.  */
  2937. cmd_bytes_sent = 0;
  2938. break;
  2939. case ESP_STEP_PPC:
  2940. /* No, not the powerPC pinhead.  Arbitration
  2941.  * won, all message bytes sent if we went to
  2942.  * message out phase, went to command phase
  2943.  * but only part of the command was sent.
  2944.  *
  2945.  * XXX I've seen this, but usually in conjunction
  2946.  * XXX with a gross error which appears to have
  2947.  * XXX occurred between the time I told the
  2948.  * XXX ESP to arbitrate and when I got the
  2949.  * XXX interrupt.  Could I have misloaded the
  2950.  * XXX command bytes into the fifo?  Actually,
  2951.  * XXX I most likely missed a phase, and therefore
  2952.  * XXX went into never never land and didn't even
  2953.  * XXX know it.  That was the old driver though.
  2954.  * XXX What is even more peculiar is that the ESP
  2955.  * XXX showed the proper function complete and
  2956.  * XXX bus service bits in the interrupt register.
  2957.  */
  2958. case ESP_STEP_FINI4:
  2959. case ESP_STEP_FINI5:
  2960. case ESP_STEP_FINI6:
  2961. case ESP_STEP_FINI7:
  2962. /* Account for the identify message */
  2963. if (SCptr->SCp.phase == in_slct_norm)
  2964. cmd_bytes_sent -= 1;
  2965. };
  2966. if (esp->erev != fashme)
  2967. esp_cmd(esp, ESP_CMD_NULL);
  2968. /* Be careful, we could really get fucked during synchronous
  2969.  * data transfers if we try to flush the fifo now.
  2970.  */
  2971. if ((esp->erev != fashme) && /* not a Happy Meal and... */
  2972.     !fcnt && /* Fifo is empty and... */
  2973.     /* either we are not doing synchronous transfers or... */
  2974.     (!SDptr->sync_max_offset ||
  2975.      /* We are not going into data in phase. */
  2976.      ((esp->sreg & ESP_STAT_PMASK) != ESP_DIP)))
  2977. esp_cmd(esp, ESP_CMD_FLUSH); /* flush is safe */
  2978. /* See how far we got if this is not a slow command. */
  2979. if (!esp->esp_slowcmd) {
  2980. if (cmd_bytes_sent < 0)
  2981. cmd_bytes_sent = 0;
  2982. if (cmd_bytes_sent != SCptr->cmd_len) {
  2983. /* Crapola, mark it as a slowcmd
  2984.  * so that we have some chance of
  2985.  * keeping the command alive with
  2986.  * good luck.
  2987.  *
  2988.  * XXX Actually, if we didn't send it all
  2989.  * XXX this means either we didn't set things
  2990.  * XXX up properly (driver bug) or the target
  2991.  * XXX or the ESP detected parity on one of
  2992.  * XXX the command bytes.  This makes much
  2993.  * XXX more sense, and therefore this code
  2994.  * XXX should be changed to send out a
  2995.  * XXX parity error message or if the status
  2996.  * XXX register shows no parity error then
  2997.  * XXX just expect the target to bring the
  2998.  * XXX bus into message in phase so that it
  2999.  * XXX can send us the parity error message.
  3000.  * XXX SCSI sucks...
  3001.  */
  3002. esp->esp_slowcmd = 1;
  3003. esp->esp_scmdp = &(SCptr->cmnd[cmd_bytes_sent]);
  3004. esp->esp_scmdleft = (SCptr->cmd_len - cmd_bytes_sent);
  3005. }
  3006. }
  3007. /* Now figure out where we went. */
  3008. esp_advance_phase(SCptr, in_the_dark);
  3009. return esp_do_phase_determine(esp);
  3010. }
  3011. /* Did the target even make it? */
  3012. if (esp->ireg == ESP_INTR_DC) {
  3013. /* wheee... nobody there or they didn't like
  3014.  * what we told it to do, clean up.
  3015.  */
  3016. /* If anyone is off the bus, but working on
  3017.  * a command in the background for us, tell
  3018.  * the ESP to listen for them.
  3019.  */
  3020. if (esp->disconnected_SC)
  3021. esp_cmd(esp, ESP_CMD_ESEL);
  3022. if (((1<<SCptr->target) & esp->targets_present) &&
  3023.     esp->seqreg != 0 &&
  3024.     (esp->cur_msgout[0] == EXTENDED_MESSAGE) &&
  3025.     (SCptr->SCp.phase == in_slct_msg ||
  3026.      SCptr->SCp.phase == in_slct_stop)) {
  3027. /* shit */
  3028. esp->snip = 0;
  3029. ESPLOG(("esp%d: Failed synchronous negotiation for target %d "
  3030. "lun %dn", esp->esp_id, SCptr->target, SCptr->lun));
  3031. SDptr->sync_max_offset = 0;
  3032. SDptr->sync_min_period = 0;
  3033. SDptr->sync = 1; /* so we don't negotiate again */
  3034. /* Run the command again, this time though we
  3035.  * won't try to negotiate for synchronous transfers.
  3036.  *
  3037.  * XXX I'd like to do something like send an
  3038.  * XXX INITIATOR_ERROR or ABORT message to the
  3039.  * XXX target to tell it, "Sorry I confused you,
  3040.  * XXX please come back and I will be nicer next
  3041.  * XXX time".  But that requires having the target
  3042.  * XXX on the bus, and it has dropped BSY on us.
  3043.  */
  3044. esp->current_SC = NULL;
  3045. esp_advance_phase(SCptr, not_issued);
  3046. prepend_SC(&esp->issue_SC, SCptr);
  3047. esp_exec_cmd(esp);
  3048. return do_intr_end;
  3049. }
  3050. /* Ok, this is normal, this is what we see during boot
  3051.  * or whenever when we are scanning the bus for targets.
  3052.  * But first make sure that is really what is happening.
  3053.  */
  3054. if (((1<<SCptr->target) & esp->targets_present)) {
  3055. ESPLOG(("esp%d: Warning, live target %d not responding to "
  3056. "selection.n", esp->esp_id, SCptr->target));
  3057. /* This _CAN_ happen.  The SCSI standard states that
  3058.  * the target is to _not_ respond to selection if
  3059.  * _it_ detects bad parity on the bus for any reason.
  3060.  * Therefore, we assume that if we've talked successfully
  3061.  * to this target before, bad parity is the problem.
  3062.  */
  3063. esp_done(esp, (DID_PARITY << 16));
  3064. } else {
  3065. /* Else, there really isn't anyone there. */
  3066. ESPMISC(("esp: selection failure, maybe nobody there?n"));
  3067. ESPMISC(("esp: target %d lun %dn",
  3068.  SCptr->target, SCptr->lun));
  3069. esp_done(esp, (DID_BAD_TARGET << 16));
  3070. }
  3071. return do_intr_end;
  3072. }
  3073. ESPLOG(("esp%d: Selection failure.n", esp->esp_id));
  3074. printk("esp%d: Currently -- ", esp->esp_id);
  3075. esp_print_ireg(esp->ireg); printk(" ");
  3076. esp_print_statreg(esp->sreg); printk(" ");
  3077. esp_print_seqreg(esp->seqreg); printk("n");
  3078. printk("esp%d: New -- ", esp->esp_id);
  3079. esp->sreg = sbus_readb(esp->eregs + ESP_STATUS);
  3080. esp->seqreg = sbus_readb(esp->eregs + ESP_SSTEP);
  3081. esp->ireg = sbus_readb(esp->eregs + ESP_INTRPT);
  3082. esp_print_ireg(esp->ireg); printk(" ");
  3083. esp_print_statreg(esp->sreg); printk(" ");
  3084. esp_print_seqreg(esp->seqreg); printk("n");
  3085. ESPLOG(("esp%d: resetting busn", esp->esp_id));
  3086. return do_reset_bus; /* ugh... */
  3087. }
  3088. /* Continue reading bytes for msgin phase. */
  3089. static int esp_do_msgincont(struct esp *esp)
  3090. {
  3091. if (esp->ireg & ESP_INTR_BSERV) {
  3092. /* in the right phase too? */
  3093. if ((esp->sreg & ESP_STAT_PMASK) == ESP_MIP) {
  3094. /* phew... */
  3095. esp_cmd(esp, ESP_CMD_TI);
  3096. esp_advance_phase(esp->current_SC, in_msgindone);
  3097. return do_intr_end;
  3098. }
  3099. /* We changed phase but ESP shows bus service,
  3100.  * in this case it is most likely that we, the
  3101.  * hacker who has been up for 20hrs straight
  3102.  * staring at the screen, drowned in coffee
  3103.  * smelling like retched cigarette ashes
  3104.  * have miscoded something..... so, try to
  3105.  * recover as best we can.
  3106.  */
  3107. ESPLOG(("esp%d: message in mis-carriage.n", esp->esp_id));
  3108. }
  3109. esp_advance_phase(esp->current_SC, in_the_dark);
  3110. return do_phase_determine;
  3111. }
  3112. static int check_singlebyte_msg(struct esp *esp)
  3113. {
  3114. esp->prevmsgin = esp->cur_msgin[0];
  3115. if (esp->cur_msgin[0] & 0x80) {
  3116. /* wheee... */
  3117. ESPLOG(("esp%d: target sends identify amidst phasesn",
  3118. esp->esp_id));
  3119. esp_advance_phase(esp->current_SC, in_the_dark);
  3120. return 0;
  3121. } else if (((esp->cur_msgin[0] & 0xf0) == 0x20) ||
  3122.    (esp->cur_msgin[0] == EXTENDED_MESSAGE)) {
  3123. esp->msgin_len = 2;
  3124. esp_advance_phase(esp->current_SC, in_msgincont);
  3125. return 0;
  3126. }
  3127. esp_advance_phase(esp->current_SC, in_the_dark);
  3128. switch (esp->cur_msgin[0]) {
  3129. default:
  3130. /* We don't want to hear about it. */
  3131. ESPLOG(("esp%d: msg %02x which we don't know aboutn", esp->esp_id,
  3132. esp->cur_msgin[0]));
  3133. return MESSAGE_REJECT;
  3134. case NOP:
  3135. ESPLOG(("esp%d: target %d sends a nopn", esp->esp_id,
  3136. esp->current_SC->target));
  3137. return 0;
  3138. case RESTORE_POINTERS:
  3139. /* In this case we might also have to backup the
  3140.  * "slow command" pointer.  It is rare to get such
  3141.  * a save/restore pointer sequence so early in the
  3142.  * bus transition sequences, but cover it.
  3143.  */
  3144. if (esp->esp_slowcmd) {
  3145. esp->esp_scmdleft = esp->current_SC->cmd_len;
  3146. esp->esp_scmdp = &esp->current_SC->cmnd[0];
  3147. }
  3148. esp_restore_pointers(esp, esp->current_SC);
  3149. return 0;
  3150. case SAVE_POINTERS:
  3151. esp_save_pointers(esp, esp->current_SC);
  3152. return 0;
  3153. case COMMAND_COMPLETE:
  3154. case DISCONNECT:
  3155. /* Freeing the bus, let it go. */
  3156. esp->current_SC->SCp.phase = in_freeing;
  3157. return 0;
  3158. case MESSAGE_REJECT:
  3159. ESPMISC(("msg reject, "));
  3160. if (esp->prevmsgout == EXTENDED_MESSAGE) {
  3161. Scsi_Device *SDptr = esp->current_SC->device;
  3162. /* Doesn't look like this target can
  3163.  * do synchronous or WIDE transfers.
  3164.  */
  3165. ESPSDTR(("got reject, was trying nego, clearing sync/WIDEn"));
  3166. SDptr->sync = 1;
  3167. SDptr->wide = 1;
  3168. SDptr->sync_min_period = 0;
  3169. SDptr->sync_max_offset = 0;
  3170. return 0;
  3171. } else {
  3172. ESPMISC(("not sync nego, sending ABORTn"));
  3173. return ABORT;
  3174. }
  3175. };
  3176. }
  3177. /* Target negotiates for synchronous transfers before we do, this
  3178.  * is legal although very strange.  What is even funnier is that
  3179.  * the SCSI2 standard specifically recommends against targets doing
  3180.  * this because so many initiators cannot cope with this occuring.
  3181.  */
  3182. static int target_with_ants_in_pants(struct esp *esp,
  3183.      Scsi_Cmnd *SCptr,
  3184.      Scsi_Device *SDptr)
  3185. {
  3186. if (SDptr->sync || SDptr->borken) {
  3187. /* sorry, no can do */
  3188. ESPSDTR(("forcing to async, "));
  3189. build_sync_nego_msg(esp, 0, 0);
  3190. SDptr->sync = 1;
  3191. esp->snip = 1;
  3192. ESPLOG(("esp%d: hoping for msgoutn", esp->esp_id));
  3193. esp_advance_phase(SCptr, in_the_dark);
  3194. return EXTENDED_MESSAGE;
  3195. }
  3196. /* Ok, we'll check them out... */
  3197. return 0;
  3198. }
  3199. static void sync_report(struct esp *esp)
  3200. {
  3201. int msg3, msg4;
  3202. char *type;
  3203. msg3 = esp->cur_msgin[3];
  3204. msg4 = esp->cur_msgin[4];
  3205. if (msg4) {
  3206. int hz = 1000000000 / (msg3 * 4);
  3207. int integer = hz / 1000000;
  3208. int fraction = (hz - (integer * 1000000)) / 10000;
  3209. if ((esp->erev == fashme) &&
  3210.     (esp->config3[esp->current_SC->target] & ESP_CONFIG3_EWIDE)) {
  3211. type = "FAST-WIDE";
  3212. integer <<= 1;
  3213. fraction <<= 1;
  3214. } else if ((msg3 * 4) < 200) {
  3215. type = "FAST";
  3216. } else {
  3217. type = "synchronous";
  3218. }
  3219. /* Do not transform this back into one big printk
  3220.  * again, it triggers a bug in our sparc64-gcc272
  3221.  * sibling call optimization.  -DaveM
  3222.  */
  3223. ESPLOG((KERN_INFO "esp%d: target %d ",
  3224. esp->esp_id, esp->current_SC->target));
  3225. ESPLOG(("[period %dns offset %d %d.%02dMHz ",
  3226. (int) msg3 * 4, (int) msg4,
  3227. integer, fraction));
  3228. ESPLOG(("%s SCSI%s]n", type,
  3229. (((msg3 * 4) < 200) ? "-II" : "")));
  3230. } else {
  3231. ESPLOG((KERN_INFO "esp%d: target %d asynchronousn",
  3232. esp->esp_id, esp->current_SC->target));
  3233. }
  3234. }
  3235. static int check_multibyte_msg(struct esp *esp)
  3236. {
  3237. Scsi_Cmnd *SCptr = esp->current_SC;
  3238. Scsi_Device *SDptr = SCptr->device;
  3239. u8 regval = 0;
  3240. int message_out = 0;
  3241. ESPSDTR(("chk multibyte msg: "));
  3242. if (esp->cur_msgin[2] == EXTENDED_SDTR) {
  3243. int period = esp->cur_msgin[3];
  3244. int offset = esp->cur_msgin[4];
  3245. ESPSDTR(("is sync nego response, "));
  3246. if (!esp->snip) {
  3247. int rval;
  3248. /* Target negotiates first! */
  3249. ESPSDTR(("target jumps the gun, "));
  3250. message_out = EXTENDED_MESSAGE; /* we must respond */
  3251. rval = target_with_ants_in_pants(esp, SCptr, SDptr);
  3252. if (rval)
  3253. return rval;
  3254. }
  3255. ESPSDTR(("examining sdtr, "));
  3256. /* Offset cannot be larger than ESP fifo size. */
  3257. if (offset > 15) {
  3258. ESPSDTR(("offset too big %2x, ", offset));
  3259. offset = 15;
  3260. ESPSDTR(("sending back new offsetn"));
  3261. build_sync_nego_msg(esp, period, offset);
  3262. return EXTENDED_MESSAGE;
  3263. }
  3264. if (offset && period > esp->max_period) {
  3265. /* Yeee, async for this slow device. */
  3266. ESPSDTR(("period too long %2x, ", period));
  3267. build_sync_nego_msg(esp, 0, 0);
  3268. ESPSDTR(("hoping for msgoutn"));
  3269. esp_advance_phase(esp->current_SC, in_the_dark);
  3270. return EXTENDED_MESSAGE;
  3271. } else if (offset && period < esp->min_period) {
  3272. ESPSDTR(("period too short %2x, ", period));
  3273. period = esp->min_period;
  3274. if (esp->erev > esp236)
  3275. regval = 4;
  3276. else
  3277. regval = 5;
  3278. } else if (offset) {
  3279. int tmp;
  3280. ESPSDTR(("period is ok, "));
  3281. tmp = esp->ccycle / 1000;
  3282. regval = (((period << 2) + tmp - 1) / tmp);
  3283. if (regval && ((esp->erev == fas100a ||
  3284. esp->erev == fas236  ||
  3285. esp->erev == fashme))) {
  3286. if (period >= 50)
  3287. regval--;
  3288. }
  3289. }
  3290. if (offset) {
  3291. u8 bit;
  3292. SDptr->sync_min_period = (regval & 0x1f);
  3293. SDptr->sync_max_offset = (offset | esp->radelay);
  3294. if (esp->erev == fas100a || esp->erev == fas236 || esp->erev == fashme) {
  3295. if ((esp->erev == fas100a) || (esp->erev == fashme))
  3296. bit = ESP_CONFIG3_FAST;
  3297. else
  3298. bit = ESP_CONFIG3_FSCSI;
  3299. if (period < 50) {
  3300. /* On FAS366, if using fast-20 synchronous transfers
  3301.  * we need to make sure the REQ/ACK assert/deassert
  3302.  * control bits are clear.
  3303.  */
  3304. if (esp->erev == fashme)
  3305. SDptr->sync_max_offset &= ~esp->radelay;
  3306. esp->config3[SCptr->target] |= bit;
  3307. } else {
  3308. esp->config3[SCptr->target] &= ~bit;
  3309. }
  3310. esp->prev_cfg3 = esp->config3[SCptr->target];
  3311. sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
  3312. }
  3313. esp->prev_soff = SDptr->sync_max_offset;
  3314. esp->prev_stp = SDptr->sync_min_period;
  3315. sbus_writeb(esp->prev_soff, esp->eregs + ESP_SOFF);
  3316. sbus_writeb(esp->prev_stp, esp->eregs + ESP_STP);
  3317. ESPSDTR(("soff=%2x stp=%2x cfg3=%2xn",
  3318.  SDptr->sync_max_offset,
  3319.  SDptr->sync_min_period,
  3320.  esp->config3[SCptr->target]));
  3321. esp->snip = 0;
  3322. } else if (SDptr->sync_max_offset) {
  3323. u8 bit;
  3324. /* back to async mode */
  3325. ESPSDTR(("unaccaptable sync nego, forcing asyncn"));
  3326. SDptr->sync_max_offset = 0;
  3327. SDptr->sync_min_period = 0;
  3328. esp->prev_soff = 0;
  3329. esp->prev_stp = 0;
  3330. sbus_writeb(esp->prev_soff, esp->eregs + ESP_SOFF);
  3331. sbus_writeb(esp->prev_stp, esp->eregs + ESP_STP);
  3332. if (esp->erev == fas100a || esp->erev == fas236 || esp->erev == fashme) {
  3333. if ((esp->erev == fas100a) || (esp->erev == fashme))
  3334. bit = ESP_CONFIG3_FAST;
  3335. else
  3336. bit = ESP_CONFIG3_FSCSI;
  3337. esp->config3[SCptr->target] &= ~bit;
  3338. esp->prev_cfg3 = esp->config3[SCptr->target];
  3339. sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
  3340. }
  3341. }
  3342. sync_report(esp);
  3343. ESPSDTR(("chk multibyte msg: sync is known, "));
  3344. SDptr->sync = 1;
  3345. if (message_out) {
  3346. ESPLOG(("esp%d: sending sdtr back, hoping for msgoutn",
  3347. esp->esp_id));
  3348. build_sync_nego_msg(esp, period, offset);
  3349. esp_advance_phase(SCptr, in_the_dark);
  3350. return EXTENDED_MESSAGE;
  3351. }
  3352. ESPSDTR(("returning zeron"));
  3353. esp_advance_phase(SCptr, in_the_dark); /* ...or else! */
  3354. return 0;
  3355. } else if (esp->cur_msgin[2] == EXTENDED_WDTR) {
  3356. int size = 8 << esp->cur_msgin[3];
  3357. esp->wnip = 0;
  3358. if (esp->erev != fashme) {
  3359. ESPLOG(("esp%d: AIEEE wide msg received and not HME.n",
  3360. esp->esp_id));
  3361. message_out = MESSAGE_REJECT;
  3362. } else if (size > 16) {
  3363. ESPLOG(("esp%d: AIEEE wide transfer for %d size "
  3364. "not supported.n", esp->esp_id, size));
  3365. message_out = MESSAGE_REJECT;
  3366. } else {
  3367. /* Things look good; let's see what we got. */
  3368. if (size == 16) {
  3369. /* Set config 3 register for this target. */
  3370. esp->config3[SCptr->target] |= ESP_CONFIG3_EWIDE;
  3371. } else {
  3372. /* Just make sure it was one byte sized. */
  3373. if (size != 8) {
  3374. ESPLOG(("esp%d: Aieee, wide nego of %d size.n",
  3375. esp->esp_id, size));
  3376. message_out = MESSAGE_REJECT;
  3377. goto finish;
  3378. }
  3379. /* Pure paranoia. */
  3380. esp->config3[SCptr->target] &= ~(ESP_CONFIG3_EWIDE);
  3381. }
  3382. esp->prev_cfg3 = esp->config3[SCptr->target];
  3383. sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
  3384. /* Regardless, next try for sync transfers. */
  3385. build_sync_nego_msg(esp, esp->sync_defp, 15);
  3386. SDptr->sync = 1;
  3387. esp->snip = 1;
  3388. message_out = EXTENDED_MESSAGE;
  3389. }
  3390. } else if (esp->cur_msgin[2] == EXTENDED_MODIFY_DATA_POINTER) {
  3391. ESPLOG(("esp%d: rejecting modify data ptr msgn", esp->esp_id));
  3392. message_out = MESSAGE_REJECT;
  3393. }
  3394. finish:
  3395. esp_advance_phase(SCptr, in_the_dark);
  3396. return message_out;
  3397. }
  3398. static int esp_do_msgindone(struct esp *esp)
  3399. {
  3400. Scsi_Cmnd *SCptr = esp->current_SC;
  3401. int message_out = 0, it = 0, rval;
  3402. rval = skipahead1(esp, SCptr, in_msgin, in_msgindone);
  3403. if (rval)
  3404. return rval;
  3405. if (SCptr->SCp.sent_command != in_status) {
  3406. if (!(esp->ireg & ESP_INTR_DC)) {
  3407. if (esp->msgin_len && (esp->sreg & ESP_STAT_PERR)) {
  3408. message_out = MSG_PARITY_ERROR;
  3409. esp_cmd(esp, ESP_CMD_FLUSH);
  3410. } else if (esp->erev != fashme &&
  3411.   (it = (sbus_readb(esp->eregs + ESP_FFLAGS) & ESP_FF_FBYTES)) != 1) {
  3412. /* We certainly dropped the ball somewhere. */
  3413. message_out = INITIATOR_ERROR;
  3414. esp_cmd(esp, ESP_CMD_FLUSH);
  3415. } else if (!esp->msgin_len) {
  3416. if (esp->erev == fashme)
  3417. it = esp->hme_fifo_workaround_buffer[0];
  3418. else
  3419. it = sbus_readb(esp->eregs + ESP_FDATA);
  3420. esp_advance_phase(SCptr, in_msgincont);
  3421. } else {
  3422. /* it is ok and we want it */
  3423. if (esp->erev == fashme)
  3424. it = esp->cur_msgin[esp->msgin_ctr] =
  3425. esp->hme_fifo_workaround_buffer[0];
  3426. else
  3427. it = esp->cur_msgin[esp->msgin_ctr] =
  3428. sbus_readb(esp->eregs + ESP_FDATA);
  3429. esp->msgin_ctr++;
  3430. }
  3431. } else {
  3432. esp_advance_phase(SCptr, in_the_dark);
  3433. return do_work_bus;
  3434. }
  3435. } else {
  3436. it = esp->cur_msgin[0];
  3437. }
  3438. if (!message_out && esp->msgin_len) {
  3439. if (esp->msgin_ctr < esp->msgin_len) {
  3440. esp_advance_phase(SCptr, in_msgincont);
  3441. } else if (esp->msgin_len == 1) {
  3442. message_out = check_singlebyte_msg(esp);
  3443. } else if (esp->msgin_len == 2) {
  3444. if (esp->cur_msgin[0] == EXTENDED_MESSAGE) {
  3445. if ((it + 2) >= 15) {
  3446. message_out = MESSAGE_REJECT;
  3447. } else {
  3448. esp->msgin_len = (it + 2);
  3449. esp_advance_phase(SCptr, in_msgincont);
  3450. }
  3451. } else {
  3452. message_out = MESSAGE_REJECT; /* foo on you */
  3453. }
  3454. } else {
  3455. message_out = check_multibyte_msg(esp);
  3456. }
  3457. }
  3458. if (message_out < 0) {
  3459. return -message_out;
  3460. } else if (message_out) {
  3461. if (((message_out != 1) &&
  3462.      ((message_out < 0x20) || (message_out & 0x80))))
  3463. esp->msgout_len = 1;
  3464. esp->cur_msgout[0] = message_out;
  3465. esp_cmd(esp, ESP_CMD_SATN);
  3466. esp_advance_phase(SCptr, in_the_dark);
  3467. esp->msgin_len = 0;
  3468. }
  3469. esp->sreg = sbus_readb(esp->eregs + ESP_STATUS);
  3470. esp->sreg &= ~(ESP_STAT_INTR);
  3471. if ((esp->sreg & (ESP_STAT_PMSG|ESP_STAT_PCD)) == (ESP_STAT_PMSG|ESP_STAT_PCD))
  3472. esp_cmd(esp, ESP_CMD_MOK);
  3473. if ((SCptr->SCp.sent_command == in_msgindone) &&
  3474.     (SCptr->SCp.phase == in_freeing))
  3475. return esp_do_freebus(esp);
  3476. return do_intr_end;
  3477. }
  3478. static int esp_do_cmdbegin(struct esp *esp)
  3479. {
  3480. Scsi_Cmnd *SCptr = esp->current_SC;
  3481. esp_advance_phase(SCptr, in_cmdend);
  3482. if (esp->erev == fashme) {
  3483. u32 tmp = sbus_readl(esp->dregs + DMA_CSR);
  3484. int i;
  3485. for (i = 0; i < esp->esp_scmdleft; i++)
  3486. esp->esp_command[i] = *esp->esp_scmdp++;
  3487. esp->esp_scmdleft = 0;
  3488. esp_cmd(esp, ESP_CMD_FLUSH);
  3489. esp_setcount(esp->eregs, i, 1);
  3490. esp_cmd(esp, (ESP_CMD_DMA | ESP_CMD_TI));
  3491. tmp |= (DMA_SCSI_DISAB | DMA_ENABLE);
  3492. tmp &= ~(DMA_ST_WRITE);
  3493. sbus_writel(i, esp->dregs + DMA_COUNT);
  3494. sbus_writel(esp->esp_command_dvma, esp->dregs + DMA_ADDR);
  3495. sbus_writel(tmp, esp->dregs + DMA_CSR);
  3496. } else {
  3497. u8 tmp;
  3498. esp_cmd(esp, ESP_CMD_FLUSH);
  3499. tmp = *esp->esp_scmdp++;
  3500. esp->esp_scmdleft--;
  3501. sbus_writeb(tmp, esp->eregs + ESP_FDATA);
  3502. esp_cmd(esp, ESP_CMD_TI);
  3503. }
  3504. return do_intr_end;
  3505. }
  3506. static int esp_do_cmddone(struct esp *esp)
  3507. {
  3508. if (esp->erev == fashme)
  3509. dma_invalidate(esp);
  3510. else
  3511. esp_cmd(esp, ESP_CMD_NULL);
  3512. if (esp->ireg & ESP_INTR_BSERV) {
  3513. esp_advance_phase(esp->current_SC, in_the_dark);
  3514. return esp_do_phase_determine(esp);
  3515. }
  3516. ESPLOG(("esp%d: in do_cmddone() but didn't get BSERV interrupt.n",
  3517. esp->esp_id));
  3518. return do_reset_bus;
  3519. }
  3520. static int esp_do_msgout(struct esp *esp)
  3521. {
  3522. esp_cmd(esp, ESP_CMD_FLUSH);
  3523. switch (esp->msgout_len) {
  3524. case 1:
  3525. if (esp->erev == fashme)
  3526. hme_fifo_push(esp, &esp->cur_msgout[0], 1);
  3527. else
  3528. sbus_writeb(esp->cur_msgout[0], esp->eregs + ESP_FDATA);
  3529. esp_cmd(esp, ESP_CMD_TI);
  3530. break;
  3531. case 2:
  3532. esp->esp_command[0] = esp->cur_msgout[0];
  3533. esp->esp_command[1] = esp->cur_msgout[1];
  3534. if (esp->erev == fashme) {
  3535. hme_fifo_push(esp, &esp->cur_msgout[0], 2);
  3536. esp_cmd(esp, ESP_CMD_TI);
  3537. } else {
  3538. dma_setup(esp, esp->esp_command_dvma, 2, 0);
  3539. esp_setcount(esp->eregs, 2, 0);
  3540. esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
  3541. }
  3542. break;
  3543. case 4:
  3544. esp->esp_command[0] = esp->cur_msgout[0];
  3545. esp->esp_command[1] = esp->cur_msgout[1];
  3546. esp->esp_command[2] = esp->cur_msgout[2];
  3547. esp->esp_command[3] = esp->cur_msgout[3];
  3548. esp->snip = 1;
  3549. if (esp->erev == fashme) {
  3550. hme_fifo_push(esp, &esp->cur_msgout[0], 4);
  3551. esp_cmd(esp, ESP_CMD_TI);
  3552. } else {
  3553. dma_setup(esp, esp->esp_command_dvma, 4, 0);
  3554. esp_setcount(esp->eregs, 4, 0);
  3555. esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
  3556. }
  3557. break;
  3558. case 5:
  3559. esp->esp_command[0] = esp->cur_msgout[0];
  3560. esp->esp_command[1] = esp->cur_msgout[1];
  3561. esp->esp_command[2] = esp->cur_msgout[2];
  3562. esp->esp_command[3] = esp->cur_msgout[3];
  3563. esp->esp_command[4] = esp->cur_msgout[4];
  3564. esp->snip = 1;
  3565. if (esp->erev == fashme) {
  3566. hme_fifo_push(esp, &esp->cur_msgout[0], 5);
  3567. esp_cmd(esp, ESP_CMD_TI);
  3568. } else {
  3569. dma_setup(esp, esp->esp_command_dvma, 5, 0);
  3570. esp_setcount(esp->eregs, 5, 0);
  3571. esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
  3572. }
  3573. break;
  3574. default:
  3575. /* whoops */
  3576. ESPMISC(("bogus msgout sending NOPn"));
  3577. esp->cur_msgout[0] = NOP;
  3578. if (esp->erev == fashme) {
  3579. hme_fifo_push(esp, &esp->cur_msgout[0], 1);
  3580. } else {
  3581. sbus_writeb(esp->cur_msgout[0], esp->eregs + ESP_FDATA);
  3582. }
  3583. esp->msgout_len = 1;
  3584. esp_cmd(esp, ESP_CMD_TI);
  3585. break;
  3586. };
  3587. esp_advance_phase(esp->current_SC, in_msgoutdone);
  3588. return do_intr_end;
  3589. }
  3590. static int esp_do_msgoutdone(struct esp *esp)
  3591. {
  3592. if (esp->msgout_len > 1) {
  3593. /* XXX HME/FAS ATN deassert workaround required,
  3594.  * XXX no DMA flushing, only possible ESP_CMD_FLUSH
  3595.  * XXX to kill the fifo.
  3596.  */
  3597. if (esp->erev != fashme) {
  3598. u32 tmp;
  3599. while ((tmp = sbus_readl(esp->dregs + DMA_CSR)) & DMA_PEND_READ)
  3600. udelay(1);
  3601. tmp &= ~DMA_ENABLE;
  3602. sbus_writel(tmp, esp->dregs + DMA_CSR);
  3603. dma_invalidate(esp);
  3604. } else {
  3605. esp_cmd(esp, ESP_CMD_FLUSH);
  3606. }
  3607. }
  3608. if (!(esp->ireg & ESP_INTR_DC)) {
  3609. if (esp->erev != fashme)
  3610. esp_cmd(esp, ESP_CMD_NULL);
  3611. switch (esp->sreg & ESP_STAT_PMASK) {
  3612. case ESP_MOP:
  3613. /* whoops, parity error */
  3614. ESPLOG(("esp%d: still in msgout, parity error assumedn",
  3615. esp->esp_id));
  3616. if (esp->msgout_len > 1)
  3617. esp_cmd(esp, ESP_CMD_SATN);
  3618. esp_advance_phase(esp->current_SC, in_msgout);
  3619. return do_work_bus;
  3620. case ESP_DIP:
  3621. break;
  3622. default:
  3623. /* Happy Meal fifo is touchy... */
  3624. if ((esp->erev != fashme) &&
  3625.     !fcount(esp) &&
  3626.     !(esp->current_SC->device->sync_max_offset))
  3627. esp_cmd(esp, ESP_CMD_FLUSH);
  3628. break;
  3629. };
  3630. } else {
  3631. ESPLOG(("esp%d: disconnect, resetting busn", esp->esp_id));
  3632. return do_reset_bus;
  3633. }
  3634. /* If we sent out a synchronous negotiation message, update
  3635.  * our state.
  3636.  */
  3637. if (esp->cur_msgout[2] == EXTENDED_MESSAGE &&
  3638.     esp->cur_msgout[4] == EXTENDED_SDTR) {
  3639. esp->snip = 1; /* anal retentiveness... */
  3640. }
  3641. esp->prevmsgout = esp->cur_msgout[0];
  3642. esp->msgout_len = 0;
  3643. esp_advance_phase(esp->current_SC, in_the_dark);
  3644. return esp_do_phase_determine(esp);
  3645. }
  3646. static int esp_bus_unexpected(struct esp *esp)
  3647. {
  3648. ESPLOG(("esp%d: command in weird state %2xn",
  3649. esp->esp_id, esp->current_SC->SCp.phase));
  3650. return do_reset_bus;
  3651. }
  3652. static espfunc_t bus_vector[] = {
  3653. esp_do_data_finale,
  3654. esp_do_data_finale,
  3655. esp_bus_unexpected,
  3656. esp_do_msgin,
  3657. esp_do_msgincont,
  3658. esp_do_msgindone,
  3659. esp_do_msgout,
  3660. esp_do_msgoutdone,
  3661. esp_do_cmdbegin,
  3662. esp_do_cmddone,
  3663. esp_do_status,
  3664. esp_do_freebus,
  3665. esp_do_phase_determine,
  3666. esp_bus_unexpected,
  3667. esp_bus_unexpected,
  3668. esp_bus_unexpected,
  3669. };
  3670. /* This is the second tier in our dual-level SCSI state machine. */
  3671. static int esp_work_bus(struct esp *esp)
  3672. {
  3673. Scsi_Cmnd *SCptr = esp->current_SC;
  3674. unsigned int phase;
  3675. ESPBUS(("esp_work_bus: "));
  3676. if (!SCptr) {
  3677. ESPBUS(("reconnectn"));
  3678. return esp_do_reconnect(esp);
  3679. }
  3680. phase = SCptr->SCp.phase;
  3681. if ((phase & 0xf0) == in_phases_mask)
  3682. return bus_vector[(phase & 0x0f)](esp);
  3683. else if ((phase & 0xf0) == in_slct_mask)
  3684. return esp_select_complete(esp);
  3685. else
  3686. return esp_bus_unexpected(esp);
  3687. }
  3688. static espfunc_t isvc_vector[] = {
  3689. 0,
  3690. esp_do_phase_determine,
  3691. esp_do_resetbus,
  3692. esp_finish_reset,
  3693. esp_work_bus
  3694. };
  3695. /* Main interrupt handler for an esp adapter. */
  3696. static void esp_handle(struct esp *esp)
  3697. {
  3698. Scsi_Cmnd *SCptr;
  3699. int what_next = do_intr_end;
  3700. SCptr = esp->current_SC;
  3701. /* Check for errors. */
  3702. esp->sreg = sbus_readb(esp->eregs + ESP_STATUS);
  3703. esp->sreg &= (~ESP_STAT_INTR);
  3704. if (esp->erev == fashme) {
  3705. esp->sreg2 = sbus_readb(esp->eregs + ESP_STATUS2);
  3706. esp->seqreg = (sbus_readb(esp->eregs + ESP_SSTEP) & ESP_STEP_VBITS);
  3707. }
  3708. if (esp->sreg & (ESP_STAT_SPAM)) {
  3709. /* Gross error, could be due to one of:
  3710.  *
  3711.  * - top of fifo overwritten, could be because
  3712.  *   we tried to do a synchronous transfer with
  3713.  *   an offset greater than ESP fifo size
  3714.  *
  3715.  * - top of command register overwritten
  3716.  *
  3717.  * - DMA setup to go in one direction, SCSI
  3718.  *   bus points in the other, whoops
  3719.  *
  3720.  * - weird phase change during asynchronous
  3721.  *   data phase while we are initiator
  3722.  */
  3723. ESPLOG(("esp%d: Gross error sreg=%2xn", esp->esp_id, esp->sreg));
  3724. /* If a command is live on the bus we cannot safely
  3725.  * reset the bus, so we'll just let the pieces fall
  3726.  * where they may.  Here we are hoping that the
  3727.  * target will be able to cleanly go away soon
  3728.  * so we can safely reset things.
  3729.  */
  3730. if (!SCptr) {
  3731. ESPLOG(("esp%d: No current cmd during gross error, "
  3732. "resetting busn", esp->esp_id));
  3733. what_next = do_reset_bus;
  3734. goto state_machine;
  3735. }
  3736. }
  3737. if (sbus_readl(esp->dregs + DMA_CSR) & DMA_HNDL_ERROR) {
  3738. /* A DMA gate array error.  Here we must
  3739.  * be seeing one of two things.  Either the
  3740.  * virtual to physical address translation
  3741.  * on the SBUS could not occur, else the
  3742.  * translation it did get pointed to a bogus
  3743.  * page.  Ho hum...
  3744.  */
  3745. ESPLOG(("esp%d: DMA error %08xn", esp->esp_id,
  3746. sbus_readl(esp->dregs + DMA_CSR)));
  3747. /* DMA gate array itself must be reset to clear the
  3748.  * error condition.
  3749.  */
  3750. esp_reset_dma(esp);
  3751. what_next = do_reset_bus;
  3752. goto state_machine;
  3753. }
  3754. esp->ireg = sbus_readb(esp->eregs + ESP_INTRPT);   /* Unlatch intr reg */
  3755. if (esp->erev == fashme) {
  3756. /* This chip is really losing. */
  3757. ESPHME(("HME["));
  3758. ESPHME(("sreg2=%02x,", esp->sreg2));
  3759. /* Must latch fifo before reading the interrupt
  3760.  * register else garbage ends up in the FIFO
  3761.  * which confuses the driver utterly.
  3762.  */
  3763. if (!(esp->sreg2 & ESP_STAT2_FEMPTY) ||
  3764.     (esp->sreg2 & ESP_STAT2_F1BYTE)) {
  3765. ESPHME(("fifo_workaround]"));
  3766. hme_fifo_read(esp);
  3767. } else {
  3768. ESPHME(("no_fifo_workaround]"));
  3769. }
  3770. }
  3771. /* No current cmd is only valid at this point when there are
  3772.  * commands off the bus or we are trying a reset.
  3773.  */
  3774. if (!SCptr && !esp->disconnected_SC && !(esp->ireg & ESP_INTR_SR)) {
  3775. /* Panic is safe, since current_SC is null. */
  3776. ESPLOG(("esp%d: no command in esp_handle()n", esp->esp_id));
  3777. panic("esp_handle: current_SC == penguin within interrupt!");
  3778. }
  3779. if (esp->ireg & (ESP_INTR_IC)) {
  3780. /* Illegal command fed to ESP.  Outside of obvious
  3781.  * software bugs that could cause this, there is
  3782.  * a condition with esp100 where we can confuse the
  3783.  * ESP into an erroneous illegal command interrupt
  3784.  * because it does not scrape the FIFO properly
  3785.  * for reselection.  See esp100_reconnect_hwbug()
  3786.  * to see how we try very hard to avoid this.
  3787.  */
  3788. ESPLOG(("esp%d: illegal commandn", esp->esp_id));
  3789. esp_dump_state(esp);
  3790. if (SCptr != NULL) {
  3791. /* Devices with very buggy firmware can drop BSY
  3792.  * during a scatter list interrupt when using sync
  3793.  * mode transfers.  We continue the transfer as
  3794.  * expected, the target drops the bus, the ESP
  3795.  * gets confused, and we get a illegal command
  3796.  * interrupt because the bus is in the disconnected
  3797.  * state now and ESP_CMD_TI is only allowed when
  3798.  * a nexus is alive on the bus.
  3799.  */
  3800. ESPLOG(("esp%d: Forcing async and disabling disconnect for "
  3801. "target %dn", esp->esp_id, SCptr->target));
  3802. SCptr->device->borken = 1; /* foo on you */
  3803. }
  3804. what_next = do_reset_bus;
  3805. } else if (!(esp->ireg & ~(ESP_INTR_FDONE | ESP_INTR_BSERV | ESP_INTR_DC))) {
  3806. if (SCptr) {
  3807. unsigned int phase = SCptr->SCp.phase;
  3808. if (phase & in_phases_mask) {
  3809. what_next = esp_work_bus(esp);
  3810. } else if (phase & in_slct_mask) {
  3811. what_next = esp_select_complete(esp);
  3812. } else {
  3813. ESPLOG(("esp%d: interrupt for no good reason...n",
  3814. esp->esp_id));
  3815. what_next = do_intr_end;
  3816. }
  3817. } else {
  3818. ESPLOG(("esp%d: BSERV or FDONE or DC while SCptr==NULLn",
  3819. esp->esp_id));
  3820. what_next = do_reset_bus;
  3821. }
  3822. } else if (esp->ireg & ESP_INTR_SR) {
  3823. ESPLOG(("esp%d: SCSI bus reset interruptn", esp->esp_id));
  3824. what_next = do_reset_complete;
  3825. } else if (esp->ireg & (ESP_INTR_S | ESP_INTR_SATN)) {
  3826. ESPLOG(("esp%d: AIEEE we have been selected by another initiator!n",
  3827. esp->esp_id));
  3828. what_next = do_reset_bus;
  3829. } else if (esp->ireg & ESP_INTR_RSEL) {
  3830. if (SCptr == NULL) {
  3831. /* This is ok. */
  3832. what_next = esp_do_reconnect(esp);
  3833. } else if (SCptr->SCp.phase & in_slct_mask) {
  3834. /* Only selection code knows how to clean
  3835.  * up properly.
  3836.  */
  3837. ESPDISC(("Reselected during selection attemptn"));
  3838. what_next = esp_select_complete(esp);
  3839. } else {
  3840. ESPLOG(("esp%d: Reselected while bus is busyn",
  3841. esp->esp_id));
  3842. what_next = do_reset_bus;
  3843. }
  3844. }
  3845. /* This is tier-one in our dual level SCSI state machine. */
  3846. state_machine:
  3847. while (what_next != do_intr_end) {
  3848. if (what_next >= do_phase_determine &&
  3849.     what_next < do_intr_end) {
  3850. what_next = isvc_vector[what_next](esp);
  3851. } else {
  3852. /* state is completely lost ;-( */
  3853. ESPLOG(("esp%d: interrupt engine loses state, resetting busn",
  3854. esp->esp_id));
  3855. what_next = do_reset_bus;
  3856. }
  3857. }
  3858. }
  3859. /* Service only the ESP described by dev_id. */
  3860. static void esp_intr(int irq, void *dev_id, struct pt_regs *pregs)
  3861. {
  3862. struct esp *esp = dev_id;
  3863. unsigned long flags;
  3864. spin_lock_irqsave(&esp->lock, flags);
  3865. if (ESP_IRQ_P(esp->dregs)) {
  3866. ESP_INTSOFF(esp->dregs);
  3867. ESPIRQ(("I[%d:%d](", smp_processor_id(), esp->esp_id));
  3868. esp_handle(esp);
  3869. ESPIRQ((")"));
  3870. ESP_INTSON(esp->dregs);
  3871. }
  3872. spin_unlock_irqrestore(&esp->lock, flags);
  3873. }
  3874. int esp_revoke(Scsi_Device* SDptr)
  3875. {
  3876. struct esp *esp = (struct esp *) SDptr->host->hostdata;
  3877. esp->targets_present &= ~(1 << SDptr->id);
  3878. return 0;
  3879. }
  3880. static Scsi_Host_Template driver_template = SCSI_SPARC_ESP;
  3881. #include "scsi_module.c"
  3882. EXPORT_NO_SYMBOLS;