mac_scsi.c 15 KB

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