/drivers/scsi/arm/acornscsi.c

http://github.com/mirrors/linux · C · 3013 lines · 1938 code · 411 blank · 664 comment · 268 complexity · 294037fe137c2819fa624c5a905159b9 MD5 · raw file

Large files are truncated click here to view the full file

  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/drivers/acorn/scsi/acornscsi.c
  4. *
  5. * Acorn SCSI 3 driver
  6. * By R.M.King.
  7. *
  8. * Abandoned using the Select and Transfer command since there were
  9. * some nasty races between our software and the target devices that
  10. * were not easy to solve, and the device errata had a lot of entries
  11. * for this command, some of them quite nasty...
  12. *
  13. * Changelog:
  14. * 26-Sep-1997 RMK Re-jigged to use the queue module.
  15. * Re-coded state machine to be based on driver
  16. * state not scsi state. Should be easier to debug.
  17. * Added acornscsi_release to clean up properly.
  18. * Updated proc/scsi reporting.
  19. * 05-Oct-1997 RMK Implemented writing to SCSI devices.
  20. * 06-Oct-1997 RMK Corrected small (non-serious) bug with the connect/
  21. * reconnect race condition causing a warning message.
  22. * 12-Oct-1997 RMK Added catch for re-entering interrupt routine.
  23. * 15-Oct-1997 RMK Improved handling of commands.
  24. * 27-Jun-1998 RMK Changed asm/delay.h to linux/delay.h.
  25. * 13-Dec-1998 RMK Better abort code and command handling. Extra state
  26. * transitions added to allow dodgy devices to work.
  27. */
  28. #define DEBUG_NO_WRITE 1
  29. #define DEBUG_QUEUES 2
  30. #define DEBUG_DMA 4
  31. #define DEBUG_ABORT 8
  32. #define DEBUG_DISCON 16
  33. #define DEBUG_CONNECT 32
  34. #define DEBUG_PHASES 64
  35. #define DEBUG_WRITE 128
  36. #define DEBUG_LINK 256
  37. #define DEBUG_MESSAGES 512
  38. #define DEBUG_RESET 1024
  39. #define DEBUG_ALL (DEBUG_RESET|DEBUG_MESSAGES|DEBUG_LINK|DEBUG_WRITE|\
  40. DEBUG_PHASES|DEBUG_CONNECT|DEBUG_DISCON|DEBUG_ABORT|\
  41. DEBUG_DMA|DEBUG_QUEUES)
  42. /* DRIVER CONFIGURATION
  43. *
  44. * SCSI-II Tagged queue support.
  45. *
  46. * I don't have any SCSI devices that support it, so it is totally untested
  47. * (except to make sure that it doesn't interfere with any non-tagging
  48. * devices). It is not fully implemented either - what happens when a
  49. * tagging device reconnects???
  50. *
  51. * You can tell if you have a device that supports tagged queueing my
  52. * cating (eg) /proc/scsi/acornscsi/0 and see if the SCSI revision is reported
  53. * as '2 TAG'.
  54. *
  55. * Also note that CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE is normally set in the config
  56. * scripts, but disabled here. Once debugged, remove the #undef, otherwise to debug,
  57. * comment out the undef.
  58. */
  59. #undef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
  60. /*
  61. * SCSI-II Synchronous transfer support.
  62. *
  63. * Tried and tested...
  64. *
  65. * SDTR_SIZE - maximum number of un-acknowledged bytes (0 = off, 12 = max)
  66. * SDTR_PERIOD - period of REQ signal (min=125, max=1020)
  67. * DEFAULT_PERIOD - default REQ period.
  68. */
  69. #define SDTR_SIZE 12
  70. #define SDTR_PERIOD 125
  71. #define DEFAULT_PERIOD 500
  72. /*
  73. * Debugging information
  74. *
  75. * DEBUG - bit mask from list above
  76. * DEBUG_TARGET - is defined to the target number if you want to debug
  77. * a specific target. [only recon/write/dma].
  78. */
  79. #define DEBUG (DEBUG_RESET|DEBUG_WRITE|DEBUG_NO_WRITE)
  80. /* only allow writing to SCSI device 0 */
  81. #define NO_WRITE 0xFE
  82. /*#define DEBUG_TARGET 2*/
  83. /*
  84. * Select timeout time (in 10ms units)
  85. *
  86. * This is the timeout used between the start of selection and the WD33C93
  87. * chip deciding that the device isn't responding.
  88. */
  89. #define TIMEOUT_TIME 10
  90. /*
  91. * Define this if you want to have verbose explanation of SCSI
  92. * status/messages.
  93. */
  94. #undef CONFIG_ACORNSCSI_CONSTANTS
  95. /*
  96. * Define this if you want to use the on board DMAC [don't remove this option]
  97. * If not set, then use PIO mode (not currently supported).
  98. */
  99. #define USE_DMAC
  100. /*
  101. * ====================================================================================
  102. */
  103. #ifdef DEBUG_TARGET
  104. #define DBG(cmd,xxx...) \
  105. if (cmd->device->id == DEBUG_TARGET) { \
  106. xxx; \
  107. }
  108. #else
  109. #define DBG(cmd,xxx...) xxx
  110. #endif
  111. #include <linux/module.h>
  112. #include <linux/kernel.h>
  113. #include <linux/string.h>
  114. #include <linux/signal.h>
  115. #include <linux/errno.h>
  116. #include <linux/proc_fs.h>
  117. #include <linux/ioport.h>
  118. #include <linux/blkdev.h>
  119. #include <linux/delay.h>
  120. #include <linux/interrupt.h>
  121. #include <linux/init.h>
  122. #include <linux/bitops.h>
  123. #include <linux/stringify.h>
  124. #include <linux/io.h>
  125. #include <asm/ecard.h>
  126. #include "../scsi.h"
  127. #include <scsi/scsi_dbg.h>
  128. #include <scsi/scsi_host.h>
  129. #include <scsi/scsi_transport_spi.h>
  130. #include "acornscsi.h"
  131. #include "msgqueue.h"
  132. #include "scsi.h"
  133. #include <scsi/scsicam.h>
  134. #define VER_MAJOR 2
  135. #define VER_MINOR 0
  136. #define VER_PATCH 6
  137. #ifndef ABORT_TAG
  138. #define ABORT_TAG 0xd
  139. #else
  140. #error "Yippee! ABORT TAG is now defined! Remove this error!"
  141. #endif
  142. #ifdef USE_DMAC
  143. /*
  144. * DMAC setup parameters
  145. */
  146. #define INIT_DEVCON0 (DEVCON0_RQL|DEVCON0_EXW|DEVCON0_CMP)
  147. #define INIT_DEVCON1 (DEVCON1_BHLD)
  148. #define DMAC_READ (MODECON_READ)
  149. #define DMAC_WRITE (MODECON_WRITE)
  150. #define INIT_SBICDMA (CTRL_DMABURST)
  151. #define scsi_xferred have_data_in
  152. /*
  153. * Size of on-board DMA buffer
  154. */
  155. #define DMAC_BUFFER_SIZE 65536
  156. #endif
  157. #define STATUS_BUFFER_TO_PRINT 24
  158. unsigned int sdtr_period = SDTR_PERIOD;
  159. unsigned int sdtr_size = SDTR_SIZE;
  160. static void acornscsi_done(AS_Host *host, struct scsi_cmnd **SCpntp,
  161. unsigned int result);
  162. static int acornscsi_reconnect_finish(AS_Host *host);
  163. static void acornscsi_dma_cleanup(AS_Host *host);
  164. static void acornscsi_abortcmd(AS_Host *host, unsigned char tag);
  165. /* ====================================================================================
  166. * Miscellaneous
  167. */
  168. /* Offsets from MEMC base */
  169. #define SBIC_REGIDX 0x2000
  170. #define SBIC_REGVAL 0x2004
  171. #define DMAC_OFFSET 0x3000
  172. /* Offsets from FAST IOC base */
  173. #define INT_REG 0x2000
  174. #define PAGE_REG 0x3000
  175. static inline void sbic_arm_write(AS_Host *host, unsigned int reg, unsigned int value)
  176. {
  177. writeb(reg, host->base + SBIC_REGIDX);
  178. writeb(value, host->base + SBIC_REGVAL);
  179. }
  180. static inline int sbic_arm_read(AS_Host *host, unsigned int reg)
  181. {
  182. if(reg == SBIC_ASR)
  183. return readl(host->base + SBIC_REGIDX) & 255;
  184. writeb(reg, host->base + SBIC_REGIDX);
  185. return readl(host->base + SBIC_REGVAL) & 255;
  186. }
  187. #define sbic_arm_writenext(host, val) writeb((val), (host)->base + SBIC_REGVAL)
  188. #define sbic_arm_readnext(host) readb((host)->base + SBIC_REGVAL)
  189. #ifdef USE_DMAC
  190. #define dmac_read(host,reg) \
  191. readb((host)->base + DMAC_OFFSET + ((reg) << 2))
  192. #define dmac_write(host,reg,value) \
  193. ({ writeb((value), (host)->base + DMAC_OFFSET + ((reg) << 2)); })
  194. #define dmac_clearintr(host) writeb(0, (host)->fast + INT_REG)
  195. static inline unsigned int dmac_address(AS_Host *host)
  196. {
  197. return dmac_read(host, DMAC_TXADRHI) << 16 |
  198. dmac_read(host, DMAC_TXADRMD) << 8 |
  199. dmac_read(host, DMAC_TXADRLO);
  200. }
  201. static
  202. void acornscsi_dumpdma(AS_Host *host, char *where)
  203. {
  204. unsigned int mode, addr, len;
  205. mode = dmac_read(host, DMAC_MODECON);
  206. addr = dmac_address(host);
  207. len = dmac_read(host, DMAC_TXCNTHI) << 8 |
  208. dmac_read(host, DMAC_TXCNTLO);
  209. printk("scsi%d: %s: DMAC %02x @%06x+%04x msk %02x, ",
  210. host->host->host_no, where,
  211. mode, addr, (len + 1) & 0xffff,
  212. dmac_read(host, DMAC_MASKREG));
  213. printk("DMA @%06x, ", host->dma.start_addr);
  214. printk("BH @%p +%04x, ", host->scsi.SCp.ptr,
  215. host->scsi.SCp.this_residual);
  216. printk("DT @+%04x ST @+%04x", host->dma.transferred,
  217. host->scsi.SCp.scsi_xferred);
  218. printk("\n");
  219. }
  220. #endif
  221. static
  222. unsigned long acornscsi_sbic_xfcount(AS_Host *host)
  223. {
  224. unsigned long length;
  225. length = sbic_arm_read(host, SBIC_TRANSCNTH) << 16;
  226. length |= sbic_arm_readnext(host) << 8;
  227. length |= sbic_arm_readnext(host);
  228. return length;
  229. }
  230. static int
  231. acornscsi_sbic_wait(AS_Host *host, int stat_mask, int stat, int timeout, char *msg)
  232. {
  233. int asr;
  234. do {
  235. asr = sbic_arm_read(host, SBIC_ASR);
  236. if ((asr & stat_mask) == stat)
  237. return 0;
  238. udelay(1);
  239. } while (--timeout);
  240. printk("scsi%d: timeout while %s\n", host->host->host_no, msg);
  241. return -1;
  242. }
  243. static
  244. int acornscsi_sbic_issuecmd(AS_Host *host, int command)
  245. {
  246. if (acornscsi_sbic_wait(host, ASR_CIP, 0, 1000, "issuing command"))
  247. return -1;
  248. sbic_arm_write(host, SBIC_CMND, command);
  249. return 0;
  250. }
  251. static void
  252. acornscsi_csdelay(unsigned int cs)
  253. {
  254. unsigned long target_jiffies, flags;
  255. target_jiffies = jiffies + 1 + cs * HZ / 100;
  256. local_save_flags(flags);
  257. local_irq_enable();
  258. while (time_before(jiffies, target_jiffies)) barrier();
  259. local_irq_restore(flags);
  260. }
  261. static
  262. void acornscsi_resetcard(AS_Host *host)
  263. {
  264. unsigned int i, timeout;
  265. /* assert reset line */
  266. host->card.page_reg = 0x80;
  267. writeb(host->card.page_reg, host->fast + PAGE_REG);
  268. /* wait 3 cs. SCSI standard says 25ms. */
  269. acornscsi_csdelay(3);
  270. host->card.page_reg = 0;
  271. writeb(host->card.page_reg, host->fast + PAGE_REG);
  272. /*
  273. * Should get a reset from the card
  274. */
  275. timeout = 1000;
  276. do {
  277. if (readb(host->fast + INT_REG) & 8)
  278. break;
  279. udelay(1);
  280. } while (--timeout);
  281. if (timeout == 0)
  282. printk("scsi%d: timeout while resetting card\n",
  283. host->host->host_no);
  284. sbic_arm_read(host, SBIC_ASR);
  285. sbic_arm_read(host, SBIC_SSR);
  286. /* setup sbic - WD33C93A */
  287. sbic_arm_write(host, SBIC_OWNID, OWNID_EAF | host->host->this_id);
  288. sbic_arm_write(host, SBIC_CMND, CMND_RESET);
  289. /*
  290. * Command should cause a reset interrupt
  291. */
  292. timeout = 1000;
  293. do {
  294. if (readb(host->fast + INT_REG) & 8)
  295. break;
  296. udelay(1);
  297. } while (--timeout);
  298. if (timeout == 0)
  299. printk("scsi%d: timeout while resetting card\n",
  300. host->host->host_no);
  301. sbic_arm_read(host, SBIC_ASR);
  302. if (sbic_arm_read(host, SBIC_SSR) != 0x01)
  303. printk(KERN_CRIT "scsi%d: WD33C93A didn't give enhanced reset interrupt\n",
  304. host->host->host_no);
  305. sbic_arm_write(host, SBIC_CTRL, INIT_SBICDMA | CTRL_IDI);
  306. sbic_arm_write(host, SBIC_TIMEOUT, TIMEOUT_TIME);
  307. sbic_arm_write(host, SBIC_SYNCHTRANSFER, SYNCHTRANSFER_2DBA);
  308. sbic_arm_write(host, SBIC_SOURCEID, SOURCEID_ER | SOURCEID_DSP);
  309. host->card.page_reg = 0x40;
  310. writeb(host->card.page_reg, host->fast + PAGE_REG);
  311. /* setup dmac - uPC71071 */
  312. dmac_write(host, DMAC_INIT, 0);
  313. #ifdef USE_DMAC
  314. dmac_write(host, DMAC_INIT, INIT_8BIT);
  315. dmac_write(host, DMAC_CHANNEL, CHANNEL_0);
  316. dmac_write(host, DMAC_DEVCON0, INIT_DEVCON0);
  317. dmac_write(host, DMAC_DEVCON1, INIT_DEVCON1);
  318. #endif
  319. host->SCpnt = NULL;
  320. host->scsi.phase = PHASE_IDLE;
  321. host->scsi.disconnectable = 0;
  322. memset(host->busyluns, 0, sizeof(host->busyluns));
  323. for (i = 0; i < 8; i++) {
  324. host->device[i].sync_state = SYNC_NEGOCIATE;
  325. host->device[i].disconnect_ok = 1;
  326. }
  327. /* wait 25 cs. SCSI standard says 250ms. */
  328. acornscsi_csdelay(25);
  329. }
  330. /*=============================================================================================
  331. * Utility routines (eg. debug)
  332. */
  333. #ifdef CONFIG_ACORNSCSI_CONSTANTS
  334. static char *acornscsi_interrupttype[] = {
  335. "rst", "suc", "p/a", "3",
  336. "term", "5", "6", "7",
  337. "serv", "9", "a", "b",
  338. "c", "d", "e", "f"
  339. };
  340. static signed char acornscsi_map[] = {
  341. 0, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  342. -1, 2, -1, -1, -1, -1, 3, -1, 4, 5, 6, 7, 8, 9, 10, 11,
  343. 12, 13, 14, -1, -1, -1, -1, -1, 4, 5, 6, 7, 8, 9, 10, 11,
  344. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  345. 15, 16, 17, 18, 19, -1, -1, 20, 4, 5, 6, 7, 8, 9, 10, 11,
  346. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  347. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  348. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  349. 21, 22, -1, -1, -1, 23, -1, -1, 4, 5, 6, 7, 8, 9, 10, 11,
  350. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  351. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  352. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  353. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  354. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  355. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  356. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  357. };
  358. static char *acornscsi_interruptcode[] = {
  359. /* 0 */
  360. "reset - normal mode", /* 00 */
  361. "reset - advanced mode", /* 01 */
  362. /* 2 */
  363. "sel", /* 11 */
  364. "sel+xfer", /* 16 */
  365. "data-out", /* 18 */
  366. "data-in", /* 19 */
  367. "cmd", /* 1A */
  368. "stat", /* 1B */
  369. "??-out", /* 1C */
  370. "??-in", /* 1D */
  371. "msg-out", /* 1E */
  372. "msg-in", /* 1F */
  373. /* 12 */
  374. "/ACK asserted", /* 20 */
  375. "save-data-ptr", /* 21 */
  376. "{re}sel", /* 22 */
  377. /* 15 */
  378. "inv cmd", /* 40 */
  379. "unexpected disconnect", /* 41 */
  380. "sel timeout", /* 42 */
  381. "P err", /* 43 */
  382. "P err+ATN", /* 44 */
  383. "bad status byte", /* 47 */
  384. /* 21 */
  385. "resel, no id", /* 80 */
  386. "resel", /* 81 */
  387. "discon", /* 85 */
  388. };
  389. static
  390. void print_scsi_status(unsigned int ssr)
  391. {
  392. if (acornscsi_map[ssr] != -1)
  393. printk("%s:%s",
  394. acornscsi_interrupttype[(ssr >> 4)],
  395. acornscsi_interruptcode[acornscsi_map[ssr]]);
  396. else
  397. printk("%X:%X", ssr >> 4, ssr & 0x0f);
  398. }
  399. #endif
  400. static
  401. void print_sbic_status(int asr, int ssr, int cmdphase)
  402. {
  403. #ifdef CONFIG_ACORNSCSI_CONSTANTS
  404. printk("sbic: %c%c%c%c%c%c ",
  405. asr & ASR_INT ? 'I' : 'i',
  406. asr & ASR_LCI ? 'L' : 'l',
  407. asr & ASR_BSY ? 'B' : 'b',
  408. asr & ASR_CIP ? 'C' : 'c',
  409. asr & ASR_PE ? 'P' : 'p',
  410. asr & ASR_DBR ? 'D' : 'd');
  411. printk("scsi: ");
  412. print_scsi_status(ssr);
  413. printk(" ph %02X\n", cmdphase);
  414. #else
  415. printk("sbic: %02X scsi: %X:%X ph: %02X\n",
  416. asr, (ssr & 0xf0)>>4, ssr & 0x0f, cmdphase);
  417. #endif
  418. }
  419. static void
  420. acornscsi_dumplogline(AS_Host *host, int target, int line)
  421. {
  422. unsigned long prev;
  423. signed int ptr;
  424. ptr = host->status_ptr[target] - STATUS_BUFFER_TO_PRINT;
  425. if (ptr < 0)
  426. ptr += STATUS_BUFFER_SIZE;
  427. printk("%c: %3s:", target == 8 ? 'H' : '0' + target,
  428. line == 0 ? "ph" : line == 1 ? "ssr" : "int");
  429. prev = host->status[target][ptr].when;
  430. for (; ptr != host->status_ptr[target]; ptr = (ptr + 1) & (STATUS_BUFFER_SIZE - 1)) {
  431. unsigned long time_diff;
  432. if (!host->status[target][ptr].when)
  433. continue;
  434. switch (line) {
  435. case 0:
  436. printk("%c%02X", host->status[target][ptr].irq ? '-' : ' ',
  437. host->status[target][ptr].ph);
  438. break;
  439. case 1:
  440. printk(" %02X", host->status[target][ptr].ssr);
  441. break;
  442. case 2:
  443. time_diff = host->status[target][ptr].when - prev;
  444. prev = host->status[target][ptr].when;
  445. if (time_diff == 0)
  446. printk("==^");
  447. else if (time_diff >= 100)
  448. printk(" ");
  449. else
  450. printk(" %02ld", time_diff);
  451. break;
  452. }
  453. }
  454. printk("\n");
  455. }
  456. static
  457. void acornscsi_dumplog(AS_Host *host, int target)
  458. {
  459. do {
  460. acornscsi_dumplogline(host, target, 0);
  461. acornscsi_dumplogline(host, target, 1);
  462. acornscsi_dumplogline(host, target, 2);
  463. if (target == 8)
  464. break;
  465. target = 8;
  466. } while (1);
  467. }
  468. static
  469. char acornscsi_target(AS_Host *host)
  470. {
  471. if (host->SCpnt)
  472. return '0' + host->SCpnt->device->id;
  473. return 'H';
  474. }
  475. /*
  476. * Prototype: cmdtype_t acornscsi_cmdtype(int command)
  477. * Purpose : differentiate READ from WRITE from other commands
  478. * Params : command - command to interpret
  479. * Returns : CMD_READ - command reads data,
  480. * CMD_WRITE - command writes data,
  481. * CMD_MISC - everything else
  482. */
  483. static inline
  484. cmdtype_t acornscsi_cmdtype(int command)
  485. {
  486. switch (command) {
  487. case WRITE_6: case WRITE_10: case WRITE_12:
  488. return CMD_WRITE;
  489. case READ_6: case READ_10: case READ_12:
  490. return CMD_READ;
  491. default:
  492. return CMD_MISC;
  493. }
  494. }
  495. /*
  496. * Prototype: int acornscsi_datadirection(int command)
  497. * Purpose : differentiate between commands that have a DATA IN phase
  498. * and a DATA OUT phase
  499. * Params : command - command to interpret
  500. * Returns : DATADIR_OUT - data out phase expected
  501. * DATADIR_IN - data in phase expected
  502. */
  503. static
  504. datadir_t acornscsi_datadirection(int command)
  505. {
  506. switch (command) {
  507. case CHANGE_DEFINITION: case COMPARE: case COPY:
  508. case COPY_VERIFY: case LOG_SELECT: case MODE_SELECT:
  509. case MODE_SELECT_10: case SEND_DIAGNOSTIC: case WRITE_BUFFER:
  510. case FORMAT_UNIT: case REASSIGN_BLOCKS: case RESERVE:
  511. case SEARCH_EQUAL: case SEARCH_HIGH: case SEARCH_LOW:
  512. case WRITE_6: case WRITE_10: case WRITE_VERIFY:
  513. case UPDATE_BLOCK: case WRITE_LONG: case WRITE_SAME:
  514. case SEARCH_HIGH_12: case SEARCH_EQUAL_12: case SEARCH_LOW_12:
  515. case WRITE_12: case WRITE_VERIFY_12: case SET_WINDOW:
  516. case MEDIUM_SCAN: case SEND_VOLUME_TAG: case 0xea:
  517. return DATADIR_OUT;
  518. default:
  519. return DATADIR_IN;
  520. }
  521. }
  522. /*
  523. * Purpose : provide values for synchronous transfers with 33C93.
  524. * Copyright: Copyright (c) 1996 John Shifflett, GeoLog Consulting
  525. * Modified by Russell King for 8MHz WD33C93A
  526. */
  527. static struct sync_xfer_tbl {
  528. unsigned int period_ns;
  529. unsigned char reg_value;
  530. } sync_xfer_table[] = {
  531. { 1, 0x20 }, { 249, 0x20 }, { 374, 0x30 },
  532. { 499, 0x40 }, { 624, 0x50 }, { 749, 0x60 },
  533. { 874, 0x70 }, { 999, 0x00 }, { 0, 0 }
  534. };
  535. /*
  536. * Prototype: int acornscsi_getperiod(unsigned char syncxfer)
  537. * Purpose : period for the synchronous transfer setting
  538. * Params : syncxfer SYNCXFER register value
  539. * Returns : period in ns.
  540. */
  541. static
  542. int acornscsi_getperiod(unsigned char syncxfer)
  543. {
  544. int i;
  545. syncxfer &= 0xf0;
  546. if (syncxfer == 0x10)
  547. syncxfer = 0;
  548. for (i = 1; sync_xfer_table[i].period_ns; i++)
  549. if (syncxfer == sync_xfer_table[i].reg_value)
  550. return sync_xfer_table[i].period_ns;
  551. return 0;
  552. }
  553. /*
  554. * Prototype: int round_period(unsigned int period)
  555. * Purpose : return index into above table for a required REQ period
  556. * Params : period - time (ns) for REQ
  557. * Returns : table index
  558. * Copyright: Copyright (c) 1996 John Shifflett, GeoLog Consulting
  559. */
  560. static inline
  561. int round_period(unsigned int period)
  562. {
  563. int i;
  564. for (i = 1; sync_xfer_table[i].period_ns; i++) {
  565. if ((period <= sync_xfer_table[i].period_ns) &&
  566. (period > sync_xfer_table[i - 1].period_ns))
  567. return i;
  568. }
  569. return 7;
  570. }
  571. /*
  572. * Prototype: unsigned char calc_sync_xfer(unsigned int period, unsigned int offset)
  573. * Purpose : calculate value for 33c93s SYNC register
  574. * Params : period - time (ns) for REQ
  575. * offset - offset in bytes between REQ/ACK
  576. * Returns : value for SYNC register
  577. * Copyright: Copyright (c) 1996 John Shifflett, GeoLog Consulting
  578. */
  579. static
  580. unsigned char __maybe_unused calc_sync_xfer(unsigned int period,
  581. unsigned int offset)
  582. {
  583. return sync_xfer_table[round_period(period)].reg_value |
  584. ((offset < SDTR_SIZE) ? offset : SDTR_SIZE);
  585. }
  586. /* ====================================================================================
  587. * Command functions
  588. */
  589. /*
  590. * Function: acornscsi_kick(AS_Host *host)
  591. * Purpose : kick next command to interface
  592. * Params : host - host to send command to
  593. * Returns : INTR_IDLE if idle, otherwise INTR_PROCESSING
  594. * Notes : interrupts are always disabled!
  595. */
  596. static
  597. intr_ret_t acornscsi_kick(AS_Host *host)
  598. {
  599. int from_queue = 0;
  600. struct scsi_cmnd *SCpnt;
  601. /* first check to see if a command is waiting to be executed */
  602. SCpnt = host->origSCpnt;
  603. host->origSCpnt = NULL;
  604. /* retrieve next command */
  605. if (!SCpnt) {
  606. SCpnt = queue_remove_exclude(&host->queues.issue, host->busyluns);
  607. if (!SCpnt)
  608. return INTR_IDLE;
  609. from_queue = 1;
  610. }
  611. if (host->scsi.disconnectable && host->SCpnt) {
  612. queue_add_cmd_tail(&host->queues.disconnected, host->SCpnt);
  613. host->scsi.disconnectable = 0;
  614. #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
  615. DBG(host->SCpnt, printk("scsi%d.%c: moved command to disconnected queue\n",
  616. host->host->host_no, acornscsi_target(host)));
  617. #endif
  618. host->SCpnt = NULL;
  619. }
  620. /*
  621. * If we have an interrupt pending, then we may have been reselected.
  622. * In this case, we don't want to write to the registers
  623. */
  624. if (!(sbic_arm_read(host, SBIC_ASR) & (ASR_INT|ASR_BSY|ASR_CIP))) {
  625. sbic_arm_write(host, SBIC_DESTID, SCpnt->device->id);
  626. sbic_arm_write(host, SBIC_CMND, CMND_SELWITHATN);
  627. }
  628. /*
  629. * claim host busy - all of these must happen atomically wrt
  630. * our interrupt routine. Failure means command loss.
  631. */
  632. host->scsi.phase = PHASE_CONNECTING;
  633. host->SCpnt = SCpnt;
  634. host->scsi.SCp = SCpnt->SCp;
  635. host->dma.xfer_setup = 0;
  636. host->dma.xfer_required = 0;
  637. host->dma.xfer_done = 0;
  638. #if (DEBUG & (DEBUG_ABORT|DEBUG_CONNECT))
  639. DBG(SCpnt,printk("scsi%d.%c: starting cmd %02X\n",
  640. host->host->host_no, '0' + SCpnt->device->id,
  641. SCpnt->cmnd[0]));
  642. #endif
  643. if (from_queue) {
  644. #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
  645. /*
  646. * tagged queueing - allocate a new tag to this command
  647. */
  648. if (SCpnt->device->simple_tags) {
  649. SCpnt->device->current_tag += 1;
  650. if (SCpnt->device->current_tag == 0)
  651. SCpnt->device->current_tag = 1;
  652. SCpnt->tag = SCpnt->device->current_tag;
  653. } else
  654. #endif
  655. set_bit(SCpnt->device->id * 8 +
  656. (u8)(SCpnt->device->lun & 0x07), host->busyluns);
  657. host->stats.removes += 1;
  658. switch (acornscsi_cmdtype(SCpnt->cmnd[0])) {
  659. case CMD_WRITE:
  660. host->stats.writes += 1;
  661. break;
  662. case CMD_READ:
  663. host->stats.reads += 1;
  664. break;
  665. case CMD_MISC:
  666. host->stats.miscs += 1;
  667. break;
  668. }
  669. }
  670. return INTR_PROCESSING;
  671. }
  672. /*
  673. * Function: void acornscsi_done(AS_Host *host, struct scsi_cmnd **SCpntp, unsigned int result)
  674. * Purpose : complete processing for command
  675. * Params : host - interface that completed
  676. * result - driver byte of result
  677. */
  678. static void acornscsi_done(AS_Host *host, struct scsi_cmnd **SCpntp,
  679. unsigned int result)
  680. {
  681. struct scsi_cmnd *SCpnt = *SCpntp;
  682. /* clean up */
  683. sbic_arm_write(host, SBIC_SOURCEID, SOURCEID_ER | SOURCEID_DSP);
  684. host->stats.fins += 1;
  685. if (SCpnt) {
  686. *SCpntp = NULL;
  687. acornscsi_dma_cleanup(host);
  688. SCpnt->result = result << 16 | host->scsi.SCp.Message << 8 | host->scsi.SCp.Status;
  689. /*
  690. * In theory, this should not happen. In practice, it seems to.
  691. * Only trigger an error if the device attempts to report all happy
  692. * but with untransferred buffers... If we don't do something, then
  693. * data loss will occur. Should we check SCpnt->underflow here?
  694. * It doesn't appear to be set to something meaningful by the higher
  695. * levels all the time.
  696. */
  697. if (result == DID_OK) {
  698. int xfer_warn = 0;
  699. if (SCpnt->underflow == 0) {
  700. if (host->scsi.SCp.ptr &&
  701. acornscsi_cmdtype(SCpnt->cmnd[0]) != CMD_MISC)
  702. xfer_warn = 1;
  703. } else {
  704. if (host->scsi.SCp.scsi_xferred < SCpnt->underflow ||
  705. host->scsi.SCp.scsi_xferred != host->dma.transferred)
  706. xfer_warn = 1;
  707. }
  708. /* ANSI standard says: (SCSI-2 Rev 10c Sect 5.6.6)
  709. * Targets which break data transfers into multiple
  710. * connections shall end each successful connection
  711. * (except possibly the last) with a SAVE DATA
  712. * POINTER - DISCONNECT message sequence.
  713. *
  714. * This makes it difficult to ensure that a transfer has
  715. * completed. If we reach the end of a transfer during
  716. * the command, then we can only have finished the transfer.
  717. * therefore, if we seem to have some data remaining, this
  718. * is not a problem.
  719. */
  720. if (host->dma.xfer_done)
  721. xfer_warn = 0;
  722. if (xfer_warn) {
  723. switch (status_byte(SCpnt->result)) {
  724. case CHECK_CONDITION:
  725. case COMMAND_TERMINATED:
  726. case BUSY:
  727. case QUEUE_FULL:
  728. case RESERVATION_CONFLICT:
  729. break;
  730. default:
  731. scmd_printk(KERN_ERR, SCpnt,
  732. "incomplete data transfer detected: "
  733. "result=%08X", SCpnt->result);
  734. scsi_print_command(SCpnt);
  735. acornscsi_dumpdma(host, "done");
  736. acornscsi_dumplog(host, SCpnt->device->id);
  737. set_host_byte(SCpnt, DID_ERROR);
  738. }
  739. }
  740. }
  741. if (!SCpnt->scsi_done)
  742. panic("scsi%d.H: null scsi_done function in acornscsi_done", host->host->host_no);
  743. clear_bit(SCpnt->device->id * 8 +
  744. (u8)(SCpnt->device->lun & 0x7), host->busyluns);
  745. SCpnt->scsi_done(SCpnt);
  746. } else
  747. printk("scsi%d: null command in acornscsi_done", host->host->host_no);
  748. host->scsi.phase = PHASE_IDLE;
  749. }
  750. /* ====================================================================================
  751. * DMA routines
  752. */
  753. /*
  754. * Purpose : update SCSI Data Pointer
  755. * Notes : this will only be one SG entry or less
  756. */
  757. static
  758. void acornscsi_data_updateptr(AS_Host *host, struct scsi_pointer *SCp, unsigned int length)
  759. {
  760. SCp->ptr += length;
  761. SCp->this_residual -= length;
  762. if (SCp->this_residual == 0 && next_SCp(SCp) == 0)
  763. host->dma.xfer_done = 1;
  764. }
  765. /*
  766. * Prototype: void acornscsi_data_read(AS_Host *host, char *ptr,
  767. * unsigned int start_addr, unsigned int length)
  768. * Purpose : read data from DMA RAM
  769. * Params : host - host to transfer from
  770. * ptr - DRAM address
  771. * start_addr - host mem address
  772. * length - number of bytes to transfer
  773. * Notes : this will only be one SG entry or less
  774. */
  775. static
  776. void acornscsi_data_read(AS_Host *host, char *ptr,
  777. unsigned int start_addr, unsigned int length)
  778. {
  779. extern void __acornscsi_in(void __iomem *, char *buf, int len);
  780. unsigned int page, offset, len = length;
  781. page = (start_addr >> 12);
  782. offset = start_addr & ((1 << 12) - 1);
  783. writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
  784. while (len > 0) {
  785. unsigned int this_len;
  786. if (len + offset > (1 << 12))
  787. this_len = (1 << 12) - offset;
  788. else
  789. this_len = len;
  790. __acornscsi_in(host->base + (offset << 1), ptr, this_len);
  791. offset += this_len;
  792. ptr += this_len;
  793. len -= this_len;
  794. if (offset == (1 << 12)) {
  795. offset = 0;
  796. page ++;
  797. writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
  798. }
  799. }
  800. writeb(host->card.page_reg, host->fast + PAGE_REG);
  801. }
  802. /*
  803. * Prototype: void acornscsi_data_write(AS_Host *host, char *ptr,
  804. * unsigned int start_addr, unsigned int length)
  805. * Purpose : write data to DMA RAM
  806. * Params : host - host to transfer from
  807. * ptr - DRAM address
  808. * start_addr - host mem address
  809. * length - number of bytes to transfer
  810. * Notes : this will only be one SG entry or less
  811. */
  812. static
  813. void acornscsi_data_write(AS_Host *host, char *ptr,
  814. unsigned int start_addr, unsigned int length)
  815. {
  816. extern void __acornscsi_out(void __iomem *, char *buf, int len);
  817. unsigned int page, offset, len = length;
  818. page = (start_addr >> 12);
  819. offset = start_addr & ((1 << 12) - 1);
  820. writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
  821. while (len > 0) {
  822. unsigned int this_len;
  823. if (len + offset > (1 << 12))
  824. this_len = (1 << 12) - offset;
  825. else
  826. this_len = len;
  827. __acornscsi_out(host->base + (offset << 1), ptr, this_len);
  828. offset += this_len;
  829. ptr += this_len;
  830. len -= this_len;
  831. if (offset == (1 << 12)) {
  832. offset = 0;
  833. page ++;
  834. writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
  835. }
  836. }
  837. writeb(host->card.page_reg, host->fast + PAGE_REG);
  838. }
  839. /* =========================================================================================
  840. * On-board DMA routines
  841. */
  842. #ifdef USE_DMAC
  843. /*
  844. * Prototype: void acornscsi_dmastop(AS_Host *host)
  845. * Purpose : stop all DMA
  846. * Params : host - host on which to stop DMA
  847. * Notes : This is called when leaving DATA IN/OUT phase,
  848. * or when interface is RESET
  849. */
  850. static inline
  851. void acornscsi_dma_stop(AS_Host *host)
  852. {
  853. dmac_write(host, DMAC_MASKREG, MASK_ON);
  854. dmac_clearintr(host);
  855. #if (DEBUG & DEBUG_DMA)
  856. DBG(host->SCpnt, acornscsi_dumpdma(host, "stop"));
  857. #endif
  858. }
  859. /*
  860. * Function: void acornscsi_dma_setup(AS_Host *host, dmadir_t direction)
  861. * Purpose : setup DMA controller for data transfer
  862. * Params : host - host to setup
  863. * direction - data transfer direction
  864. * Notes : This is called when entering DATA I/O phase, not
  865. * while we're in a DATA I/O phase
  866. */
  867. static
  868. void acornscsi_dma_setup(AS_Host *host, dmadir_t direction)
  869. {
  870. unsigned int address, length, mode;
  871. host->dma.direction = direction;
  872. dmac_write(host, DMAC_MASKREG, MASK_ON);
  873. if (direction == DMA_OUT) {
  874. #if (DEBUG & DEBUG_NO_WRITE)
  875. if (NO_WRITE & (1 << host->SCpnt->device->id)) {
  876. printk(KERN_CRIT "scsi%d.%c: I can't handle DMA_OUT!\n",
  877. host->host->host_no, acornscsi_target(host));
  878. return;
  879. }
  880. #endif
  881. mode = DMAC_WRITE;
  882. } else
  883. mode = DMAC_READ;
  884. /*
  885. * Allocate some buffer space, limited to half the buffer size
  886. */
  887. length = min_t(unsigned int, host->scsi.SCp.this_residual, DMAC_BUFFER_SIZE / 2);
  888. if (length) {
  889. host->dma.start_addr = address = host->dma.free_addr;
  890. host->dma.free_addr = (host->dma.free_addr + length) &
  891. (DMAC_BUFFER_SIZE - 1);
  892. /*
  893. * Transfer data to DMA memory
  894. */
  895. if (direction == DMA_OUT)
  896. acornscsi_data_write(host, host->scsi.SCp.ptr, host->dma.start_addr,
  897. length);
  898. length -= 1;
  899. dmac_write(host, DMAC_TXCNTLO, length);
  900. dmac_write(host, DMAC_TXCNTHI, length >> 8);
  901. dmac_write(host, DMAC_TXADRLO, address);
  902. dmac_write(host, DMAC_TXADRMD, address >> 8);
  903. dmac_write(host, DMAC_TXADRHI, 0);
  904. dmac_write(host, DMAC_MODECON, mode);
  905. dmac_write(host, DMAC_MASKREG, MASK_OFF);
  906. #if (DEBUG & DEBUG_DMA)
  907. DBG(host->SCpnt, acornscsi_dumpdma(host, "strt"));
  908. #endif
  909. host->dma.xfer_setup = 1;
  910. }
  911. }
  912. /*
  913. * Function: void acornscsi_dma_cleanup(AS_Host *host)
  914. * Purpose : ensure that all DMA transfers are up-to-date & host->scsi.SCp is correct
  915. * Params : host - host to finish
  916. * Notes : This is called when a command is:
  917. * terminating, RESTORE_POINTERS, SAVE_POINTERS, DISCONNECT
  918. * : This must not return until all transfers are completed.
  919. */
  920. static
  921. void acornscsi_dma_cleanup(AS_Host *host)
  922. {
  923. dmac_write(host, DMAC_MASKREG, MASK_ON);
  924. dmac_clearintr(host);
  925. /*
  926. * Check for a pending transfer
  927. */
  928. if (host->dma.xfer_required) {
  929. host->dma.xfer_required = 0;
  930. if (host->dma.direction == DMA_IN)
  931. acornscsi_data_read(host, host->dma.xfer_ptr,
  932. host->dma.xfer_start, host->dma.xfer_length);
  933. }
  934. /*
  935. * Has a transfer been setup?
  936. */
  937. if (host->dma.xfer_setup) {
  938. unsigned int transferred;
  939. host->dma.xfer_setup = 0;
  940. #if (DEBUG & DEBUG_DMA)
  941. DBG(host->SCpnt, acornscsi_dumpdma(host, "cupi"));
  942. #endif
  943. /*
  944. * Calculate number of bytes transferred from DMA.
  945. */
  946. transferred = dmac_address(host) - host->dma.start_addr;
  947. host->dma.transferred += transferred;
  948. if (host->dma.direction == DMA_IN)
  949. acornscsi_data_read(host, host->scsi.SCp.ptr,
  950. host->dma.start_addr, transferred);
  951. /*
  952. * Update SCSI pointers
  953. */
  954. acornscsi_data_updateptr(host, &host->scsi.SCp, transferred);
  955. #if (DEBUG & DEBUG_DMA)
  956. DBG(host->SCpnt, acornscsi_dumpdma(host, "cupo"));
  957. #endif
  958. }
  959. }
  960. /*
  961. * Function: void acornscsi_dmacintr(AS_Host *host)
  962. * Purpose : handle interrupts from DMAC device
  963. * Params : host - host to process
  964. * Notes : If reading, we schedule the read to main memory &
  965. * allow the transfer to continue.
  966. * : If writing, we fill the onboard DMA memory from main
  967. * memory.
  968. * : Called whenever DMAC finished it's current transfer.
  969. */
  970. static
  971. void acornscsi_dma_intr(AS_Host *host)
  972. {
  973. unsigned int address, length, transferred;
  974. #if (DEBUG & DEBUG_DMA)
  975. DBG(host->SCpnt, acornscsi_dumpdma(host, "inti"));
  976. #endif
  977. dmac_write(host, DMAC_MASKREG, MASK_ON);
  978. dmac_clearintr(host);
  979. /*
  980. * Calculate amount transferred via DMA
  981. */
  982. transferred = dmac_address(host) - host->dma.start_addr;
  983. host->dma.transferred += transferred;
  984. /*
  985. * Schedule DMA transfer off board
  986. */
  987. if (host->dma.direction == DMA_IN) {
  988. host->dma.xfer_start = host->dma.start_addr;
  989. host->dma.xfer_length = transferred;
  990. host->dma.xfer_ptr = host->scsi.SCp.ptr;
  991. host->dma.xfer_required = 1;
  992. }
  993. acornscsi_data_updateptr(host, &host->scsi.SCp, transferred);
  994. /*
  995. * Allocate some buffer space, limited to half the on-board RAM size
  996. */
  997. length = min_t(unsigned int, host->scsi.SCp.this_residual, DMAC_BUFFER_SIZE / 2);
  998. if (length) {
  999. host->dma.start_addr = address = host->dma.free_addr;
  1000. host->dma.free_addr = (host->dma.free_addr + length) &
  1001. (DMAC_BUFFER_SIZE - 1);
  1002. /*
  1003. * Transfer data to DMA memory
  1004. */
  1005. if (host->dma.direction == DMA_OUT)
  1006. acornscsi_data_write(host, host->scsi.SCp.ptr, host->dma.start_addr,
  1007. length);
  1008. length -= 1;
  1009. dmac_write(host, DMAC_TXCNTLO, length);
  1010. dmac_write(host, DMAC_TXCNTHI, length >> 8);
  1011. dmac_write(host, DMAC_TXADRLO, address);
  1012. dmac_write(host, DMAC_TXADRMD, address >> 8);
  1013. dmac_write(host, DMAC_TXADRHI, 0);
  1014. dmac_write(host, DMAC_MASKREG, MASK_OFF);
  1015. #if (DEBUG & DEBUG_DMA)
  1016. DBG(host->SCpnt, acornscsi_dumpdma(host, "into"));
  1017. #endif
  1018. } else {
  1019. host->dma.xfer_setup = 0;
  1020. #if 0
  1021. /*
  1022. * If the interface still wants more, then this is an error.
  1023. * We give it another byte, but we also attempt to raise an
  1024. * attention condition. We continue giving one byte until
  1025. * the device recognises the attention.
  1026. */
  1027. if (dmac_read(host, DMAC_STATUS) & STATUS_RQ0) {
  1028. acornscsi_abortcmd(host, host->SCpnt->tag);
  1029. dmac_write(host, DMAC_TXCNTLO, 0);
  1030. dmac_write(host, DMAC_TXCNTHI, 0);
  1031. dmac_write(host, DMAC_TXADRLO, 0);
  1032. dmac_write(host, DMAC_TXADRMD, 0);
  1033. dmac_write(host, DMAC_TXADRHI, 0);
  1034. dmac_write(host, DMAC_MASKREG, MASK_OFF);
  1035. }
  1036. #endif
  1037. }
  1038. }
  1039. /*
  1040. * Function: void acornscsi_dma_xfer(AS_Host *host)
  1041. * Purpose : transfer data between AcornSCSI and memory
  1042. * Params : host - host to process
  1043. */
  1044. static
  1045. void acornscsi_dma_xfer(AS_Host *host)
  1046. {
  1047. host->dma.xfer_required = 0;
  1048. if (host->dma.direction == DMA_IN)
  1049. acornscsi_data_read(host, host->dma.xfer_ptr,
  1050. host->dma.xfer_start, host->dma.xfer_length);
  1051. }
  1052. /*
  1053. * Function: void acornscsi_dma_adjust(AS_Host *host)
  1054. * Purpose : adjust DMA pointers & count for bytes transferred to
  1055. * SBIC but not SCSI bus.
  1056. * Params : host - host to adjust DMA count for
  1057. */
  1058. static
  1059. void acornscsi_dma_adjust(AS_Host *host)
  1060. {
  1061. if (host->dma.xfer_setup) {
  1062. signed long transferred;
  1063. #if (DEBUG & (DEBUG_DMA|DEBUG_WRITE))
  1064. DBG(host->SCpnt, acornscsi_dumpdma(host, "adji"));
  1065. #endif
  1066. /*
  1067. * Calculate correct DMA address - DMA is ahead of SCSI bus while
  1068. * writing.
  1069. * host->scsi.SCp.scsi_xferred is the number of bytes
  1070. * actually transferred to/from the SCSI bus.
  1071. * host->dma.transferred is the number of bytes transferred
  1072. * over DMA since host->dma.start_addr was last set.
  1073. *
  1074. * real_dma_addr = host->dma.start_addr + host->scsi.SCp.scsi_xferred
  1075. * - host->dma.transferred
  1076. */
  1077. transferred = host->scsi.SCp.scsi_xferred - host->dma.transferred;
  1078. if (transferred < 0)
  1079. printk("scsi%d.%c: Ack! DMA write correction %ld < 0!\n",
  1080. host->host->host_no, acornscsi_target(host), transferred);
  1081. else if (transferred == 0)
  1082. host->dma.xfer_setup = 0;
  1083. else {
  1084. transferred += host->dma.start_addr;
  1085. dmac_write(host, DMAC_TXADRLO, transferred);
  1086. dmac_write(host, DMAC_TXADRMD, transferred >> 8);
  1087. dmac_write(host, DMAC_TXADRHI, transferred >> 16);
  1088. #if (DEBUG & (DEBUG_DMA|DEBUG_WRITE))
  1089. DBG(host->SCpnt, acornscsi_dumpdma(host, "adjo"));
  1090. #endif
  1091. }
  1092. }
  1093. }
  1094. #endif
  1095. /* =========================================================================================
  1096. * Data I/O
  1097. */
  1098. static int
  1099. acornscsi_write_pio(AS_Host *host, char *bytes, int *ptr, int len, unsigned int max_timeout)
  1100. {
  1101. unsigned int asr, timeout = max_timeout;
  1102. int my_ptr = *ptr;
  1103. while (my_ptr < len) {
  1104. asr = sbic_arm_read(host, SBIC_ASR);
  1105. if (asr & ASR_DBR) {
  1106. timeout = max_timeout;
  1107. sbic_arm_write(host, SBIC_DATA, bytes[my_ptr++]);
  1108. } else if (asr & ASR_INT)
  1109. break;
  1110. else if (--timeout == 0)
  1111. break;
  1112. udelay(1);
  1113. }
  1114. *ptr = my_ptr;
  1115. return (timeout == 0) ? -1 : 0;
  1116. }
  1117. /*
  1118. * Function: void acornscsi_sendcommand(AS_Host *host)
  1119. * Purpose : send a command to a target
  1120. * Params : host - host which is connected to target
  1121. */
  1122. static void
  1123. acornscsi_sendcommand(AS_Host *host)
  1124. {
  1125. struct scsi_cmnd *SCpnt = host->SCpnt;
  1126. sbic_arm_write(host, SBIC_TRANSCNTH, 0);
  1127. sbic_arm_writenext(host, 0);
  1128. sbic_arm_writenext(host, SCpnt->cmd_len - host->scsi.SCp.sent_command);
  1129. acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
  1130. if (acornscsi_write_pio(host, SCpnt->cmnd,
  1131. (int *)&host->scsi.SCp.sent_command, SCpnt->cmd_len, 1000000))
  1132. printk("scsi%d: timeout while sending command\n", host->host->host_no);
  1133. host->scsi.phase = PHASE_COMMAND;
  1134. }
  1135. static
  1136. void acornscsi_sendmessage(AS_Host *host)
  1137. {
  1138. unsigned int message_length = msgqueue_msglength(&host->scsi.msgs);
  1139. unsigned int msgnr;
  1140. struct message *msg;
  1141. #if (DEBUG & DEBUG_MESSAGES)
  1142. printk("scsi%d.%c: sending message ",
  1143. host->host->host_no, acornscsi_target(host));
  1144. #endif
  1145. switch (message_length) {
  1146. case 0:
  1147. acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
  1148. acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "sending message 1");
  1149. sbic_arm_write(host, SBIC_DATA, NOP);
  1150. host->scsi.last_message = NOP;
  1151. #if (DEBUG & DEBUG_MESSAGES)
  1152. printk("NOP");
  1153. #endif
  1154. break;
  1155. case 1:
  1156. acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
  1157. msg = msgqueue_getmsg(&host->scsi.msgs, 0);
  1158. acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "sending message 2");
  1159. sbic_arm_write(host, SBIC_DATA, msg->msg[0]);
  1160. host->scsi.last_message = msg->msg[0];
  1161. #if (DEBUG & DEBUG_MESSAGES)
  1162. spi_print_msg(msg->msg);
  1163. #endif
  1164. break;
  1165. default:
  1166. /*
  1167. * ANSI standard says: (SCSI-2 Rev 10c Sect 5.6.14)
  1168. * 'When a target sends this (MESSAGE_REJECT) message, it
  1169. * shall change to MESSAGE IN phase and send this message
  1170. * prior to requesting additional message bytes from the
  1171. * initiator. This provides an interlock so that the
  1172. * initiator can determine which message byte is rejected.
  1173. */
  1174. sbic_arm_write(host, SBIC_TRANSCNTH, 0);
  1175. sbic_arm_writenext(host, 0);
  1176. sbic_arm_writenext(host, message_length);
  1177. acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
  1178. msgnr = 0;
  1179. while ((msg = msgqueue_getmsg(&host->scsi.msgs, msgnr++)) != NULL) {
  1180. unsigned int i;
  1181. #if (DEBUG & DEBUG_MESSAGES)
  1182. spi_print_msg(msg);
  1183. #endif
  1184. i = 0;
  1185. if (acornscsi_write_pio(host, msg->msg, &i, msg->length, 1000000))
  1186. printk("scsi%d: timeout while sending message\n", host->host->host_no);
  1187. host->scsi.last_message = msg->msg[0];
  1188. if (msg->msg[0] == EXTENDED_MESSAGE)
  1189. host->scsi.last_message |= msg->msg[2] << 8;
  1190. if (i != msg->length)
  1191. break;
  1192. }
  1193. break;
  1194. }
  1195. #if (DEBUG & DEBUG_MESSAGES)
  1196. printk("\n");
  1197. #endif
  1198. }
  1199. /*
  1200. * Function: void acornscsi_readstatusbyte(AS_Host *host)
  1201. * Purpose : Read status byte from connected target
  1202. * Params : host - host connected to target
  1203. */
  1204. static
  1205. void acornscsi_readstatusbyte(AS_Host *host)
  1206. {
  1207. acornscsi_sbic_issuecmd(host, CMND_XFERINFO|CMND_SBT);
  1208. acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "reading status byte");
  1209. host->scsi.SCp.Status = sbic_arm_read(host, SBIC_DATA);
  1210. }
  1211. /*
  1212. * Function: unsigned char acornscsi_readmessagebyte(AS_Host *host)
  1213. * Purpose : Read one message byte from connected target
  1214. * Params : host - host connected to target
  1215. */
  1216. static
  1217. unsigned char acornscsi_readmessagebyte(AS_Host *host)
  1218. {
  1219. unsigned char message;
  1220. acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
  1221. acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "for message byte");
  1222. message = sbic_arm_read(host, SBIC_DATA);
  1223. /* wait for MSGIN-XFER-PAUSED */
  1224. acornscsi_sbic_wait(host, ASR_INT, ASR_INT, 1000, "for interrupt after message byte");
  1225. sbic_arm_read(host, SBIC_SSR);
  1226. return message;
  1227. }
  1228. /*
  1229. * Function: void acornscsi_message(AS_Host *host)
  1230. * Purpose : Read complete message from connected target & action message
  1231. * Params : host - host connected to target
  1232. */
  1233. static
  1234. void acornscsi_message(AS_Host *host)
  1235. {
  1236. unsigned char message[16];
  1237. unsigned int msgidx = 0, msglen = 1;
  1238. do {
  1239. message[msgidx] = acornscsi_readmessagebyte(host);
  1240. switch (msgidx) {
  1241. case 0:
  1242. if (message[0] == EXTENDED_MESSAGE ||
  1243. (message[0] >= 0x20 && message[0] <= 0x2f))
  1244. msglen = 2;
  1245. break;
  1246. case 1:
  1247. if (message[0] == EXTENDED_MESSAGE)
  1248. msglen += message[msgidx];
  1249. break;
  1250. }
  1251. msgidx += 1;
  1252. if (msgidx < msglen) {
  1253. acornscsi_sbic_issuecmd(host, CMND_NEGATEACK);
  1254. /* wait for next msg-in */
  1255. acornscsi_sbic_wait(host, ASR_INT, ASR_INT, 1000, "for interrupt after negate ack");
  1256. sbic_arm_read(host, SBIC_SSR);
  1257. }
  1258. } while (msgidx < msglen);
  1259. #if (DEBUG & DEBUG_MESSAGES)
  1260. printk("scsi%d.%c: message in: ",
  1261. host->host->host_no, acornscsi_target(host));
  1262. spi_print_msg(message);
  1263. printk("\n");
  1264. #endif
  1265. if (host->scsi.phase == PHASE_RECONNECTED) {
  1266. /*
  1267. * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.17)
  1268. * 'Whenever a target reconnects to an initiator to continue
  1269. * a tagged I/O process, the SIMPLE QUEUE TAG message shall
  1270. * be sent immediately following the IDENTIFY message...'
  1271. */
  1272. if (message[0] == SIMPLE_QUEUE_TAG)
  1273. host->scsi.reconnected.tag = message[1];
  1274. if (acornscsi_reconnect_finish(host))
  1275. host->scsi.phase = PHASE_MSGIN;
  1276. }
  1277. switch (message[0]) {
  1278. case ABORT:
  1279. case ABORT_TAG:
  1280. case COMMAND_COMPLETE:
  1281. if (host->scsi.phase != PHASE_STATUSIN) {
  1282. printk(KERN_ERR "scsi%d.%c: command complete following non-status in phase?\n",
  1283. host->host->host_no, acornscsi_target(host));
  1284. acornscsi_dumplog(host, host->SCpnt->device->id);
  1285. }
  1286. host->scsi.phase = PHASE_DONE;
  1287. host->scsi.SCp.Message = message[0];
  1288. break;
  1289. case SAVE_POINTERS:
  1290. /*
  1291. * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.20)
  1292. * 'The SAVE DATA POINTER message is sent from a target to
  1293. * direct the initiator to copy the active data pointer to
  1294. * the saved data pointer for the current I/O process.
  1295. */
  1296. acornscsi_dma_cleanup(host);
  1297. host->SCpnt->SCp = host->scsi.SCp;
  1298. host->SCpnt->SCp.sent_command = 0;
  1299. host->scsi.phase = PHASE_MSGIN;
  1300. break;
  1301. case RESTORE_POINTERS:
  1302. /*
  1303. * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.19)
  1304. * 'The RESTORE POINTERS message is sent from a target to
  1305. * direct the initiator to copy the most recently saved
  1306. * command, data, and status pointers for the I/O process
  1307. * to the corresponding active pointers. The command and
  1308. * status pointers shall be restored to the beginning of
  1309. * the present command and status areas.'
  1310. */
  1311. acornscsi_dma_cleanup(host);
  1312. host->scsi.SCp = host->SCpnt->SCp;
  1313. host->scsi.phase = PHASE_MSGIN;
  1314. break;
  1315. case DISCONNECT:
  1316. /*
  1317. * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 6.4.2)
  1318. * 'On those occasions when an error or exception condition occurs
  1319. * and the target elects to repeat the information transfer, the
  1320. * target may repeat the transfer either issuing a RESTORE POINTERS
  1321. * message or by disconnecting without issuing a SAVE POINTERS
  1322. * message. When reconnection is completed, the most recent
  1323. * saved pointer values are restored.'
  1324. */
  1325. acornscsi_dma_cleanup(host);
  1326. host->scsi.phase = PHASE_DISCONNECT;
  1327. break;
  1328. case MESSAGE_REJECT:
  1329. #if 0 /* this isn't needed any more */
  1330. /*
  1331. * If we were negociating sync transfer, we don't yet know if
  1332. * this REJECT is for the sync transfer or for the tagged queue/wide
  1333. * transfer. Re-initiate sync transfer negotiation now, and if
  1334. * we got a REJECT in response to SDTR, then it'll be set to DONE.
  1335. */
  1336. if (host->device[host->SCpnt->device->id].sync_state == SYNC_SENT_REQUEST)
  1337. host->device[host->SCpnt->device->id].sync_state = SYNC_NEGOCIATE;
  1338. #endif
  1339. /*
  1340. * If we have any messages waiting to go out, then assert ATN now
  1341. */
  1342. if (msgqueue_msglength(&host->scsi.msgs))
  1343. acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
  1344. switch (host->scsi.last_message) {
  1345. #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
  1346. case HEAD_OF_QUEUE_TAG:
  1347. case ORDERED_QUEUE_TAG:
  1348. case SIMPLE_QUEUE_TAG:
  1349. /*
  1350. * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.17)
  1351. * If a target does not implement tagged queuing and a queue tag
  1352. * message is received, it shall respond with a MESSAGE REJECT
  1353. * message and accept the I/O process as if it were untagged.
  1354. */
  1355. printk(KERN_NOTICE "scsi%d.%c: disabling tagged queueing\n",
  1356. host->host->host_no, acornscsi_target(host));
  1357. host->SCpnt->device->simple_tags = 0;
  1358. set_bit(host->SCpnt->device->id * 8 +
  1359. (u8)(host->SCpnt->device->lun & 0x7), host->busyluns);
  1360. break;
  1361. #endif
  1362. case EXTENDED_MESSAGE | (EXTENDED_SDTR << 8):
  1363. /*
  1364. * Target can't handle synchronous transfers
  1365. */
  1366. printk(KERN_NOTICE "scsi%d.%c: Using asynchronous transfer\n",
  1367. host->host->host_no, acornscsi_target(host));
  1368. host->device[host->SCpnt->device->id].sync_xfer = SYNCHTRANSFER_2DBA;
  1369. host->device[host->SCpnt->device->id].sync_state = SYNC_ASYNCHRONOUS;
  1370. sbic_arm_write(host, SBIC_SYNCHTRANSFER, host->device[host->SCpnt->device->id].sync_xfer);
  1371. break;
  1372. default:
  1373. break;
  1374. }
  1375. break;
  1376. case QUEUE_FULL:
  1377. /* TODO: target queue is full */
  1378. break;
  1379. case SIMPLE_QUEUE_TAG:
  1380. /* tag queue reconnect... message[1] = queue tag. Print something to indicate something happened! */
  1381. printk("scsi%d.%c: reconnect queue tag %02X\n",
  1382. host->host->host_no, acornscsi_target(host),
  1383. message[1]);
  1384. break;
  1385. case EXTENDED_MESSAGE:
  1386. switch (message[2]) {
  1387. #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
  1388. case EXTENDED_SDTR:
  1389. if (host->device[host->SCpnt->device->id].sync_state == SYNC_SENT_REQUEST) {
  1390. /*
  1391. * We requested synchronous transfers. This isn't quite right...
  1392. * We can only say if this succeeded if we proceed on to execute the
  1393. * command from this message. If we get a MESSAGE PARITY ERROR,
  1394. * and the target retries fail, then we fallback to asynchronous mode
  1395. */
  1396. host->device[host->SCpnt->device->id].sync_state = SYNC_COMPLETED;
  1397. printk(KERN_NOTICE "scsi%d.%c: Using synchronous transfer, offset %d, %d ns\n",
  1398. host->host->host_no, acornscsi_target(host),
  1399. message[4], message[3] * 4);
  1400. host->device[host->SCpnt->device->id].sync_xfer =
  1401. calc_sync_xfer(message[3] * 4, message[4]);
  1402. } else {
  1403. unsigned char period, length;
  1404. /*
  1405. * Target requested synchronous transfers. The agreement is only
  1406. * to be in operation AFTER the target leaves message out phase.
  1407. */
  1408. acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
  1409. period = max_t(unsigned int, message[3], sdtr_period / 4);
  1410. length = min_t(unsigned int, message[4], sdtr_size);
  1411. msgqueue_addmsg(&host->scsi.msgs, 5, EXTENDED_MESSAGE, 3,
  1412. EXTENDED_SDTR, period, length);
  1413. host->device[host->SCpnt->device->id].sync_xfer =
  1414. calc_sync_xfer(period * 4, length);
  1415. }
  1416. sbic_arm_write(host, SBIC_SYNCHTRANSFER, host->device[host->SCpnt->device->id].sync_xfer);
  1417. break;
  1418. #else
  1419. /* We do not accept synchronous transfers. Respond with a
  1420. * MESSAGE_REJECT.
  1421. */
  1422. #endif
  1423. case EXTENDED_WDTR:
  1424. /* The WD33C93A is only 8-bit. We respond with a MESSAGE_REJECT
  1425. * to a wide data transfer request.
  1426. */
  1427. default:
  1428. acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
  1429. msgqueue_flush(&host->scsi.msgs);
  1430. msgqueue_addmsg(&host->scsi.msgs, 1, MESSAGE_REJECT);
  1431. break;
  1432. }
  1433. break;
  1434. default: /* reject message */
  1435. printk(KERN_ERR "scsi%d.%c: unrecognised message %02X, rejecting\n",
  1436. host->host->host_no, acornscsi_target(host),
  1437. message[0]);
  1438. acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
  1439. msgqueue_flush(&host->scsi.msgs);
  1440. msgqueue_addmsg(&host->scsi.msgs, 1, MESSAGE_REJECT);
  1441. host->scsi.phase = PHASE_MSGIN;
  1442. break;
  1443. }
  1444. acornscsi_sbic_issuecmd(host, CMND_NEGATEACK);
  1445. }
  1446. /*
  1447. * Function: int acornscsi_buildmessages(AS_Host *host)
  1448. * Purpose : build the connection messages for a host
  1449. * Params : host - host to add messages to
  1450. */
  1451. static
  1452. void acornscsi_buildmessages(AS_Host *host)
  1453. {
  1454. #if 0
  1455. /* does the device need resetting? */
  1456. if (cmd_reset) {
  1457. msgqueue_addmsg(&host->scsi.msgs, 1, BUS_DEVICE_RESET);
  1458. return;
  1459. }
  1460. #endif
  1461. msgqueue_addmsg(&host->scsi.msgs, 1,
  1462. IDENTIFY(host->device[host->SCpnt->device->id].disconnect_ok,
  1463. host->SCpnt->device->lun));
  1464. #if 0
  1465. /* does the device need the current command aborted */
  1466. if (cmd_aborted) {
  1467. acornscsi_abortcmd(host->SCpnt->tag);
  1468. return;
  1469. }
  1470. #endif
  1471. #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
  1472. if (host->SCpnt->tag) {
  1473. unsigned int tag_type;
  1474. if (host->SCpnt->cmnd[0] == REQUEST_SENSE ||
  1475. host->SCpnt->cmnd[0] == TEST_UNIT_READY ||
  1476. host->SCpnt->cmnd[0] == INQUIRY)
  1477. tag_type = HEAD_OF_QUEUE_TAG;
  1478. else
  1479. tag_type = SIMPLE_QUEUE_TAG;
  1480. msgqueue_addmsg(&host->scsi.msgs, 2, tag_type, host->SCpnt->tag);
  1481. }
  1482. #endif
  1483. #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
  1484. if (host->device[host->SCpnt->device->id].sync_state == SYNC_NEGOCIATE) {
  1485. host->device[host->SCpnt->device->id].sync_state = SYNC_SENT_REQUEST;
  1486. msgqueue_addmsg(&host->scsi.msgs, 5,
  1487. EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
  1488. sdtr_period / 4, sdtr_size);
  1489. }
  1490. #endif
  1491. }
  1492. /*
  1493. * Function: int acornscsi_starttransfer(AS_Host *host)
  1494. * Purpose : transfer data to/from connected target
  1495. * Params : host - host to which target is connected
  1496. * Returns : 0 if failure
  1497. */
  1498. static
  1499. int acornscsi_starttransfer(AS_Host *host)
  1500. {
  1501. int residual;
  1502. if (!host->scsi.SCp.ptr /*&& host->scsi.SCp.this_residual*/) {
  1503. printk(KERN_ERR "scsi%d.%c: null buffer passed to acornscsi_starttransfer\n",
  1504. host->host->host_no, acornscsi_target(host));
  1505. return 0;
  1506. }
  1507. residual = scsi_bufflen(host->SCpnt) - host->scsi.SCp.scsi_xferred;
  1508. sbic_arm_write(host, SBIC_SYNCHTRANSFER, host->device[host->SCpnt->device->id].sync_xfer);
  1509. sbic_arm_writenext(host, residual >> 16);
  1510. sbic_arm_writenext(host, residual >> 8);
  1511. sbic_arm_writenext(host, residual);
  1512. acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
  1513. return 1;
  1514. }
  1515. /* =========================================================================================
  1516. * Connection & Disconnection
  1517. */
  1518. /*
  1519. * Function : acornscsi_reconnect(AS_Host *host)
  1520. * Purpose : reconnect a previously disconnected command
  1521. * Params : host - host specific data
  1522. * Remarks : SCSI spec says:
  1523. * 'The set of active pointers is restored from the set
  1524. * of saved pointers upon reconnection of the I/O proces…