mac_scsi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * Generic Macintosh NCR5380 driver
  3. *
  4. * Copyright 1998, Michael Schmitz <mschmitz@lbl.gov>
  5. *
  6. * Copyright 2019 Finn Thain
  7. *
  8. * derived in part from:
  9. */
  10. /*
  11. * Generic Generic NCR5380 driver
  12. *
  13. * Copyright 1995, Russell King
  14. */
  15. #include <linux/delay.h>
  16. #include <linux/types.h>
  17. #include <linux/module.h>
  18. #include <linux/ioport.h>
  19. #include <linux/init.h>
  20. #include <linux/blkdev.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/platform_device.h>
  23. #include <asm/hwtest.h>
  24. #include <asm/io.h>
  25. #include <asm/macints.h>
  26. #include <asm/setup.h>
  27. #include <scsi/scsi_host.h>
  28. /* Definitions for the core NCR5380 driver. */
  29. #define NCR5380_implementation_fields int pdma_residual
  30. #define NCR5380_read(reg) in_8(hostdata->io + ((reg) << 4))
  31. #define NCR5380_write(reg, value) out_8(hostdata->io + ((reg) << 4), value)
  32. #define NCR5380_dma_xfer_len macscsi_dma_xfer_len
  33. #define NCR5380_dma_recv_setup macscsi_pread
  34. #define NCR5380_dma_send_setup macscsi_pwrite
  35. #define NCR5380_dma_residual macscsi_dma_residual
  36. #define NCR5380_intr macscsi_intr
  37. #define NCR5380_queue_command macscsi_queue_command
  38. #define NCR5380_abort macscsi_abort
  39. #define NCR5380_host_reset macscsi_host_reset
  40. #define NCR5380_info macscsi_info
  41. #include "NCR5380.h"
  42. static int setup_can_queue = -1;
  43. module_param(setup_can_queue, int, 0);
  44. static int setup_cmd_per_lun = -1;
  45. module_param(setup_cmd_per_lun, int, 0);
  46. static int setup_sg_tablesize = -1;
  47. module_param(setup_sg_tablesize, int, 0);
  48. static int setup_use_pdma = 512;
  49. module_param(setup_use_pdma, int, 0);
  50. static int setup_hostid = -1;
  51. module_param(setup_hostid, int, 0);
  52. static int setup_toshiba_delay = -1;
  53. module_param(setup_toshiba_delay, int, 0);
  54. #ifndef MODULE
  55. static int __init mac_scsi_setup(char *str)
  56. {
  57. int ints[8];
  58. (void)get_options(str, ARRAY_SIZE(ints), ints);
  59. if (ints[0] < 1) {
  60. pr_err("Usage: mac5380=<can_queue>[,<cmd_per_lun>[,<sg_tablesize>[,<hostid>[,<use_tags>[,<use_pdma>[,<toshiba_delay>]]]]]]\n");
  61. return 0;
  62. }
  63. if (ints[0] >= 1)
  64. setup_can_queue = ints[1];
  65. if (ints[0] >= 2)
  66. setup_cmd_per_lun = ints[2];
  67. if (ints[0] >= 3)
  68. setup_sg_tablesize = ints[3];
  69. if (ints[0] >= 4)
  70. setup_hostid = ints[4];
  71. /* ints[5] (use_tagged_queuing) is ignored */
  72. if (ints[0] >= 6)
  73. setup_use_pdma = ints[6];
  74. if (ints[0] >= 7)
  75. setup_toshiba_delay = ints[7];
  76. return 1;
  77. }
  78. __setup("mac5380=", mac_scsi_setup);
  79. #endif /* !MODULE */
  80. /*
  81. * According to "Inside Macintosh: Devices", Mac OS requires disk drivers to
  82. * specify the number of bytes between the delays expected from a SCSI target.
  83. * This allows the operating system to "prevent bus errors when a target fails
  84. * to deliver the next byte within the processor bus error timeout period."
  85. * Linux SCSI drivers lack knowledge of the timing behaviour of SCSI targets
  86. * so bus errors are unavoidable.
  87. *
  88. * If a MOVE.B instruction faults, we assume that zero bytes were transferred
  89. * and simply retry. That assumption probably depends on target behaviour but
  90. * seems to hold up okay. The NOP provides synchronization: without it the
  91. * fault can sometimes occur after the program counter has moved past the
  92. * offending instruction. Post-increment addressing can't be used.
  93. */
  94. #define MOVE_BYTE(operands) \
  95. asm volatile ( \
  96. "1: moveb " operands " \n" \
  97. "11: nop \n" \
  98. " addq #1,%0 \n" \
  99. " subq #1,%1 \n" \
  100. "40: \n" \
  101. " \n" \
  102. ".section .fixup,\"ax\" \n" \
  103. ".even \n" \
  104. "90: movel #1, %2 \n" \
  105. " jra 40b \n" \
  106. ".previous \n" \
  107. " \n" \
  108. ".section __ex_table,\"a\" \n" \
  109. ".align 4 \n" \
  110. ".long 1b,90b \n" \
  111. ".long 11b,90b \n" \
  112. ".previous \n" \
  113. : "+a" (addr), "+r" (n), "+r" (result) : "a" (io))
  114. /*
  115. * If a MOVE.W (or MOVE.L) instruction faults, it cannot be retried because
  116. * the residual byte count would be uncertain. In that situation the MOVE_WORD
  117. * macro clears n in the fixup section to abort the transfer.
  118. */
  119. #define MOVE_WORD(operands) \
  120. asm volatile ( \
  121. "1: movew " operands " \n" \
  122. "11: nop \n" \
  123. " subq #2,%1 \n" \
  124. "40: \n" \
  125. " \n" \
  126. ".section .fixup,\"ax\" \n" \
  127. ".even \n" \
  128. "90: movel #0, %1 \n" \
  129. " movel #2, %2 \n" \
  130. " jra 40b \n" \
  131. ".previous \n" \
  132. " \n" \
  133. ".section __ex_table,\"a\" \n" \
  134. ".align 4 \n" \
  135. ".long 1b,90b \n" \
  136. ".long 11b,90b \n" \
  137. ".previous \n" \
  138. : "+a" (addr), "+r" (n), "+r" (result) : "a" (io))
  139. #define MOVE_16_WORDS(operands) \
  140. asm volatile ( \
  141. "1: movew " operands " \n" \
  142. "2: movew " operands " \n" \
  143. "3: movew " operands " \n" \
  144. "4: movew " operands " \n" \
  145. "5: movew " operands " \n" \
  146. "6: movew " operands " \n" \
  147. "7: movew " operands " \n" \
  148. "8: movew " operands " \n" \
  149. "9: movew " operands " \n" \
  150. "10: movew " operands " \n" \
  151. "11: movew " operands " \n" \
  152. "12: movew " operands " \n" \
  153. "13: movew " operands " \n" \
  154. "14: movew " operands " \n" \
  155. "15: movew " operands " \n" \
  156. "16: movew " operands " \n" \
  157. "17: nop \n" \
  158. " subl #32,%1 \n" \
  159. "40: \n" \
  160. " \n" \
  161. ".section .fixup,\"ax\" \n" \
  162. ".even \n" \
  163. "90: movel #0, %1 \n" \
  164. " movel #2, %2 \n" \
  165. " jra 40b \n" \
  166. ".previous \n" \
  167. " \n" \
  168. ".section __ex_table,\"a\" \n" \
  169. ".align 4 \n" \
  170. ".long 1b,90b \n" \
  171. ".long 2b,90b \n" \
  172. ".long 3b,90b \n" \
  173. ".long 4b,90b \n" \
  174. ".long 5b,90b \n" \
  175. ".long 6b,90b \n" \
  176. ".long 7b,90b \n" \
  177. ".long 8b,90b \n" \
  178. ".long 9b,90b \n" \
  179. ".long 10b,90b \n" \
  180. ".long 11b,90b \n" \
  181. ".long 12b,90b \n" \
  182. ".long 13b,90b \n" \
  183. ".long 14b,90b \n" \
  184. ".long 15b,90b \n" \
  185. ".long 16b,90b \n" \
  186. ".long 17b,90b \n" \
  187. ".previous \n" \
  188. : "+a" (addr), "+r" (n), "+r" (result) : "a" (io))
  189. #define MAC_PDMA_DELAY 32
  190. static inline int mac_pdma_recv(void __iomem *io, unsigned char *start, int n)
  191. {
  192. unsigned char *addr = start;
  193. int result = 0;
  194. if (n >= 1) {
  195. MOVE_BYTE("%3@,%0@");
  196. if (result)
  197. goto out;
  198. }
  199. if (n >= 1 && ((unsigned long)addr & 1)) {
  200. MOVE_BYTE("%3@,%0@");
  201. if (result)
  202. goto out;
  203. }
  204. while (n >= 32)
  205. MOVE_16_WORDS("%3@,%0@+");
  206. while (n >= 2)
  207. MOVE_WORD("%3@,%0@+");
  208. if (result)
  209. return start - addr; /* Negated to indicate uncertain length */
  210. if (n == 1)
  211. MOVE_BYTE("%3@,%0@");
  212. out:
  213. return addr - start;
  214. }
  215. static inline int mac_pdma_send(unsigned char *start, void __iomem *io, int n)
  216. {
  217. unsigned char *addr = start;
  218. int result = 0;
  219. if (n >= 1) {
  220. MOVE_BYTE("%0@,%3@");
  221. if (result)
  222. goto out;
  223. }
  224. if (n >= 1 && ((unsigned long)addr & 1)) {
  225. MOVE_BYTE("%0@,%3@");
  226. if (result)
  227. goto out;
  228. }
  229. while (n >= 32)
  230. MOVE_16_WORDS("%0@+,%3@");
  231. while (n >= 2)
  232. MOVE_WORD("%0@+,%3@");
  233. if (result)
  234. return start - addr; /* Negated to indicate uncertain length */
  235. if (n == 1)
  236. MOVE_BYTE("%0@,%3@");
  237. out:
  238. return addr - start;
  239. }
  240. static inline int macscsi_pread(struct NCR5380_hostdata *hostdata,
  241. unsigned char *dst, int len)
  242. {
  243. u8 __iomem *s = hostdata->pdma_io + (INPUT_DATA_REG << 4);
  244. unsigned char *d = dst;
  245. hostdata->pdma_residual = len;
  246. while (!NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG,
  247. BASR_DRQ | BASR_PHASE_MATCH,
  248. BASR_DRQ | BASR_PHASE_MATCH, HZ / 64)) {
  249. int bytes;
  250. bytes = mac_pdma_recv(s, d, min(hostdata->pdma_residual, 512));
  251. if (bytes > 0) {
  252. d += bytes;
  253. hostdata->pdma_residual -= bytes;
  254. }
  255. if (hostdata->pdma_residual == 0)
  256. return 0;
  257. if (NCR5380_poll_politely2(hostdata, STATUS_REG, SR_REQ, SR_REQ,
  258. BUS_AND_STATUS_REG, BASR_ACK,
  259. BASR_ACK, HZ / 64) < 0)
  260. scmd_printk(KERN_DEBUG, hostdata->connected,
  261. "%s: !REQ and !ACK\n", __func__);
  262. if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH))
  263. return 0;
  264. if (bytes == 0)
  265. udelay(MAC_PDMA_DELAY);
  266. if (bytes >= 0)
  267. continue;
  268. dsprintk(NDEBUG_PSEUDO_DMA, hostdata->host,
  269. "%s: bus error (%d/%d)\n", __func__, d - dst, len);
  270. NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
  271. return -1;
  272. }
  273. scmd_printk(KERN_ERR, hostdata->connected,
  274. "%s: phase mismatch or !DRQ\n", __func__);
  275. NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
  276. return -1;
  277. }
  278. static inline int macscsi_pwrite(struct NCR5380_hostdata *hostdata,
  279. unsigned char *src, int len)
  280. {
  281. unsigned char *s = src;
  282. u8 __iomem *d = hostdata->pdma_io + (OUTPUT_DATA_REG << 4);
  283. hostdata->pdma_residual = len;
  284. while (!NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG,
  285. BASR_DRQ | BASR_PHASE_MATCH,
  286. BASR_DRQ | BASR_PHASE_MATCH, HZ / 64)) {
  287. int bytes;
  288. bytes = mac_pdma_send(s, d, min(hostdata->pdma_residual, 512));
  289. if (bytes > 0) {
  290. s += bytes;
  291. hostdata->pdma_residual -= bytes;
  292. }
  293. if (hostdata->pdma_residual == 0) {
  294. if (NCR5380_poll_politely(hostdata, TARGET_COMMAND_REG,
  295. TCR_LAST_BYTE_SENT,
  296. TCR_LAST_BYTE_SENT, HZ / 64) < 0)
  297. scmd_printk(KERN_ERR, hostdata->connected,
  298. "%s: Last Byte Sent timeout\n", __func__);
  299. return 0;
  300. }
  301. if (NCR5380_poll_politely2(hostdata, STATUS_REG, SR_REQ, SR_REQ,
  302. BUS_AND_STATUS_REG, BASR_ACK,
  303. BASR_ACK, HZ / 64) < 0)
  304. scmd_printk(KERN_DEBUG, hostdata->connected,
  305. "%s: !REQ and !ACK\n", __func__);
  306. if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH))
  307. return 0;
  308. if (bytes == 0)
  309. udelay(MAC_PDMA_DELAY);
  310. if (bytes >= 0)
  311. continue;
  312. dsprintk(NDEBUG_PSEUDO_DMA, hostdata->host,
  313. "%s: bus error (%d/%d)\n", __func__, s - src, len);
  314. NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
  315. return -1;
  316. }
  317. scmd_printk(KERN_ERR, hostdata->connected,
  318. "%s: phase mismatch or !DRQ\n", __func__);
  319. NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
  320. return -1;
  321. }
  322. static int macscsi_dma_xfer_len(struct NCR5380_hostdata *hostdata,
  323. struct scsi_cmnd *cmd)
  324. {
  325. if (hostdata->flags & FLAG_NO_PSEUDO_DMA ||
  326. cmd->SCp.this_residual < setup_use_pdma)
  327. return 0;
  328. return cmd->SCp.this_residual;
  329. }
  330. static int macscsi_dma_residual(struct NCR5380_hostdata *hostdata)
  331. {
  332. return hostdata->pdma_residual;
  333. }
  334. #include "NCR5380.c"
  335. #define DRV_MODULE_NAME "mac_scsi"
  336. #define PFX DRV_MODULE_NAME ": "
  337. static struct scsi_host_template mac_scsi_template = {
  338. .module = THIS_MODULE,
  339. .proc_name = DRV_MODULE_NAME,
  340. .name = "Macintosh NCR5380 SCSI",
  341. .info = macscsi_info,
  342. .queuecommand = macscsi_queue_command,
  343. .eh_abort_handler = macscsi_abort,
  344. .eh_host_reset_handler = macscsi_host_reset,
  345. .can_queue = 16,
  346. .this_id = 7,
  347. .sg_tablesize = 1,
  348. .cmd_per_lun = 2,
  349. .use_clustering = DISABLE_CLUSTERING,
  350. .cmd_size = NCR5380_CMD_SIZE,
  351. .max_sectors = 128,
  352. };
  353. static int __init mac_scsi_probe(struct platform_device *pdev)
  354. {
  355. struct Scsi_Host *instance;
  356. struct NCR5380_hostdata *hostdata;
  357. int error;
  358. int host_flags = 0;
  359. struct resource *irq, *pio_mem, *pdma_mem = NULL;
  360. pio_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  361. if (!pio_mem)
  362. return -ENODEV;
  363. pdma_mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  364. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  365. if (!hwreg_present((unsigned char *)pio_mem->start +
  366. (STATUS_REG << 4))) {
  367. pr_info(PFX "no device detected at %pap\n", &pio_mem->start);
  368. return -ENODEV;
  369. }
  370. if (setup_can_queue > 0)
  371. mac_scsi_template.can_queue = setup_can_queue;
  372. if (setup_cmd_per_lun > 0)
  373. mac_scsi_template.cmd_per_lun = setup_cmd_per_lun;
  374. if (setup_sg_tablesize > 0)
  375. mac_scsi_template.sg_tablesize = setup_sg_tablesize;
  376. if (setup_hostid >= 0)
  377. mac_scsi_template.this_id = setup_hostid & 7;
  378. instance = scsi_host_alloc(&mac_scsi_template,
  379. sizeof(struct NCR5380_hostdata));
  380. if (!instance)
  381. return -ENOMEM;
  382. if (irq)
  383. instance->irq = irq->start;
  384. else
  385. instance->irq = NO_IRQ;
  386. hostdata = shost_priv(instance);
  387. hostdata->base = pio_mem->start;
  388. hostdata->io = (u8 __iomem *)pio_mem->start;
  389. if (pdma_mem && setup_use_pdma)
  390. hostdata->pdma_io = (u8 __iomem *)pdma_mem->start;
  391. else
  392. host_flags |= FLAG_NO_PSEUDO_DMA;
  393. host_flags |= setup_toshiba_delay > 0 ? FLAG_TOSHIBA_DELAY : 0;
  394. error = NCR5380_init(instance, host_flags | FLAG_LATE_DMA_SETUP);
  395. if (error)
  396. goto fail_init;
  397. if (instance->irq != NO_IRQ) {
  398. error = request_irq(instance->irq, macscsi_intr, IRQF_SHARED,
  399. "NCR5380", instance);
  400. if (error)
  401. goto fail_irq;
  402. }
  403. NCR5380_maybe_reset_bus(instance);
  404. error = scsi_add_host(instance, NULL);
  405. if (error)
  406. goto fail_host;
  407. platform_set_drvdata(pdev, instance);
  408. scsi_scan_host(instance);
  409. return 0;
  410. fail_host:
  411. if (instance->irq != NO_IRQ)
  412. free_irq(instance->irq, instance);
  413. fail_irq:
  414. NCR5380_exit(instance);
  415. fail_init:
  416. scsi_host_put(instance);
  417. return error;
  418. }
  419. static int __exit mac_scsi_remove(struct platform_device *pdev)
  420. {
  421. struct Scsi_Host *instance = platform_get_drvdata(pdev);
  422. scsi_remove_host(instance);
  423. if (instance->irq != NO_IRQ)
  424. free_irq(instance->irq, instance);
  425. NCR5380_exit(instance);
  426. scsi_host_put(instance);
  427. return 0;
  428. }
  429. static struct platform_driver mac_scsi_driver = {
  430. .remove = __exit_p(mac_scsi_remove),
  431. .driver = {
  432. .name = DRV_MODULE_NAME,
  433. },
  434. };
  435. module_platform_driver_probe(mac_scsi_driver, mac_scsi_probe);
  436. MODULE_ALIAS("platform:" DRV_MODULE_NAME);
  437. MODULE_LICENSE("GPL");