sgiwd93.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
  7. * Copyright (C) 1999 Andrew R. Baker (andrewb@uab.edu)
  8. * Copyright (C) 2001 Florian Lohoff (flo@rfc822.org)
  9. * Copyright (C) 2003, 07 Ralf Baechle (ralf@linux-mips.org)
  10. *
  11. * (In all truth, Jed Schimmel wrote all this code.)
  12. */
  13. #undef DEBUG
  14. #include <linux/delay.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/gfp.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/types.h>
  21. #include <linux/module.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/spinlock.h>
  24. #include <asm/sgi/hpc3.h>
  25. #include <asm/sgi/ip22.h>
  26. #include <asm/sgi/wd.h>
  27. #include "scsi.h"
  28. #include "wd33c93.h"
  29. struct ip22_hostdata {
  30. struct WD33C93_hostdata wh;
  31. struct hpc_data {
  32. dma_addr_t dma;
  33. void *cpu;
  34. } hd;
  35. };
  36. #define host_to_hostdata(host) ((struct ip22_hostdata *)((host)->hostdata))
  37. struct hpc_chunk {
  38. struct hpc_dma_desc desc;
  39. u32 _padding; /* align to quadword boundary */
  40. };
  41. static irqreturn_t sgiwd93_intr(int irq, void *dev_id)
  42. {
  43. struct Scsi_Host * host = dev_id;
  44. unsigned long flags;
  45. spin_lock_irqsave(host->host_lock, flags);
  46. wd33c93_intr(host);
  47. spin_unlock_irqrestore(host->host_lock, flags);
  48. return IRQ_HANDLED;
  49. }
  50. static inline
  51. void fill_hpc_entries(struct hpc_chunk *hcp, struct scsi_cmnd *cmd, int datainp)
  52. {
  53. unsigned long len = cmd->SCp.this_residual;
  54. void *addr = cmd->SCp.ptr;
  55. dma_addr_t physaddr;
  56. unsigned long count;
  57. physaddr = dma_map_single(NULL, addr, len, cmd->sc_data_direction);
  58. cmd->SCp.dma_handle = physaddr;
  59. while (len) {
  60. /*
  61. * even cntinfo could be up to 16383, without
  62. * magic only 8192 works correctly
  63. */
  64. count = len > 8192 ? 8192 : len;
  65. hcp->desc.pbuf = physaddr;
  66. hcp->desc.cntinfo = count;
  67. hcp++;
  68. len -= count;
  69. physaddr += count;
  70. }
  71. /*
  72. * To make sure, if we trip an HPC bug, that we transfer every single
  73. * byte, we tag on an extra zero length dma descriptor at the end of
  74. * the chain.
  75. */
  76. hcp->desc.pbuf = 0;
  77. hcp->desc.cntinfo = HPCDMA_EOX;
  78. }
  79. static int dma_setup(struct scsi_cmnd *cmd, int datainp)
  80. {
  81. struct ip22_hostdata *hdata = host_to_hostdata(cmd->device->host);
  82. struct hpc3_scsiregs *hregs =
  83. (struct hpc3_scsiregs *) cmd->device->host->base;
  84. struct hpc_chunk *hcp = (struct hpc_chunk *) hdata->hd.cpu;
  85. pr_debug("dma_setup: datainp<%d> hcp<%p> ", datainp, hcp);
  86. hdata->wh.dma_dir = datainp;
  87. /*
  88. * wd33c93 shouldn't pass us bogus dma_setups, but it does:-( The
  89. * other wd33c93 drivers deal with it the same way (which isn't that
  90. * obvious). IMHO a better fix would be, not to do these dma setups
  91. * in the first place.
  92. */
  93. if (cmd->SCp.ptr == NULL || cmd->SCp.this_residual == 0)
  94. return 1;
  95. fill_hpc_entries(hcp, cmd, datainp);
  96. pr_debug(" HPCGO\n");
  97. /* Start up the HPC. */
  98. hregs->ndptr = hdata->hd.dma;
  99. if (datainp)
  100. hregs->ctrl = HPC3_SCTRL_ACTIVE;
  101. else
  102. hregs->ctrl = HPC3_SCTRL_ACTIVE | HPC3_SCTRL_DIR;
  103. return 0;
  104. }
  105. static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
  106. int status)
  107. {
  108. struct ip22_hostdata *hdata = host_to_hostdata(instance);
  109. struct hpc3_scsiregs *hregs;
  110. if (!SCpnt)
  111. return;
  112. hregs = (struct hpc3_scsiregs *) SCpnt->device->host->base;
  113. pr_debug("dma_stop: status<%d> ", status);
  114. /* First stop the HPC and flush it's FIFO. */
  115. if (hdata->wh.dma_dir) {
  116. hregs->ctrl |= HPC3_SCTRL_FLUSH;
  117. while (hregs->ctrl & HPC3_SCTRL_ACTIVE)
  118. barrier();
  119. }
  120. hregs->ctrl = 0;
  121. dma_unmap_single(NULL, SCpnt->SCp.dma_handle, SCpnt->SCp.this_residual,
  122. SCpnt->sc_data_direction);
  123. pr_debug("\n");
  124. }
  125. void sgiwd93_reset(unsigned long base)
  126. {
  127. struct hpc3_scsiregs *hregs = (struct hpc3_scsiregs *) base;
  128. hregs->ctrl = HPC3_SCTRL_CRESET;
  129. udelay(50);
  130. hregs->ctrl = 0;
  131. }
  132. static inline void init_hpc_chain(struct hpc_data *hd)
  133. {
  134. struct hpc_chunk *hcp = (struct hpc_chunk *) hd->cpu;
  135. struct hpc_chunk *dma = (struct hpc_chunk *) hd->dma;
  136. unsigned long start, end;
  137. start = (unsigned long) hcp;
  138. end = start + PAGE_SIZE;
  139. while (start < end) {
  140. hcp->desc.pnext = (u32) (dma + 1);
  141. hcp->desc.cntinfo = HPCDMA_EOX;
  142. hcp++; dma++;
  143. start += sizeof(struct hpc_chunk);
  144. };
  145. hcp--;
  146. hcp->desc.pnext = hd->dma;
  147. }
  148. static int sgiwd93_bus_reset(struct scsi_cmnd *cmd)
  149. {
  150. /* FIXME perform bus-specific reset */
  151. /* FIXME 2: kill this function, and let midlayer fallback
  152. to the same result, calling wd33c93_host_reset() */
  153. spin_lock_irq(cmd->device->host->host_lock);
  154. wd33c93_host_reset(cmd);
  155. spin_unlock_irq(cmd->device->host->host_lock);
  156. return SUCCESS;
  157. }
  158. /*
  159. * Kludge alert - the SCSI code calls the abort and reset method with int
  160. * arguments not with pointers. So this is going to blow up beautyfully
  161. * on 64-bit systems with memory outside the compat address spaces.
  162. */
  163. static struct scsi_host_template sgiwd93_template = {
  164. .module = THIS_MODULE,
  165. .proc_name = "SGIWD93",
  166. .name = "SGI WD93",
  167. .queuecommand = wd33c93_queuecommand,
  168. .eh_abort_handler = wd33c93_abort,
  169. .eh_bus_reset_handler = sgiwd93_bus_reset,
  170. .eh_host_reset_handler = wd33c93_host_reset,
  171. .can_queue = 16,
  172. .this_id = 7,
  173. .sg_tablesize = SG_ALL,
  174. .cmd_per_lun = 8,
  175. .use_clustering = DISABLE_CLUSTERING,
  176. };
  177. static int __init sgiwd93_probe(struct platform_device *pdev)
  178. {
  179. struct sgiwd93_platform_data *pd = pdev->dev.platform_data;
  180. unsigned char *wdregs = pd->wdregs;
  181. struct hpc3_scsiregs *hregs = pd->hregs;
  182. struct ip22_hostdata *hdata;
  183. struct Scsi_Host *host;
  184. wd33c93_regs regs;
  185. unsigned int unit = pd->unit;
  186. unsigned int irq = pd->irq;
  187. int err;
  188. host = scsi_host_alloc(&sgiwd93_template, sizeof(struct ip22_hostdata));
  189. if (!host) {
  190. err = -ENOMEM;
  191. goto out;
  192. }
  193. host->base = (unsigned long) hregs;
  194. host->irq = irq;
  195. hdata = host_to_hostdata(host);
  196. hdata->hd.cpu = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
  197. &hdata->hd.dma, GFP_KERNEL);
  198. if (!hdata->hd.cpu) {
  199. printk(KERN_WARNING "sgiwd93: Could not allocate memory for "
  200. "host %d buffer.\n", unit);
  201. err = -ENOMEM;
  202. goto out_put;
  203. }
  204. init_hpc_chain(&hdata->hd);
  205. regs.SASR = wdregs + 3;
  206. regs.SCMD = wdregs + 7;
  207. wd33c93_init(host, regs, dma_setup, dma_stop, WD33C93_FS_MHZ(20));
  208. if (hdata->wh.no_sync == 0xff)
  209. hdata->wh.no_sync = 0;
  210. err = request_irq(irq, sgiwd93_intr, 0, "SGI WD93", host);
  211. if (err) {
  212. printk(KERN_WARNING "sgiwd93: Could not register irq %d "
  213. "for host %d.\n", irq, unit);
  214. goto out_free;
  215. }
  216. platform_set_drvdata(pdev, host);
  217. err = scsi_add_host(host, NULL);
  218. if (err)
  219. goto out_irq;
  220. scsi_scan_host(host);
  221. return 0;
  222. out_irq:
  223. free_irq(irq, host);
  224. out_free:
  225. dma_free_coherent(NULL, PAGE_SIZE, hdata->hd.cpu, hdata->hd.dma);
  226. out_put:
  227. scsi_host_put(host);
  228. out:
  229. return err;
  230. }
  231. static void __exit sgiwd93_remove(struct platform_device *pdev)
  232. {
  233. struct Scsi_Host *host = platform_get_drvdata(pdev);
  234. struct ip22_hostdata *hdata = (struct ip22_hostdata *) host->hostdata;
  235. struct sgiwd93_platform_data *pd = pdev->dev.platform_data;
  236. scsi_remove_host(host);
  237. free_irq(pd->irq, host);
  238. dma_free_coherent(&pdev->dev, PAGE_SIZE, hdata->hd.cpu, hdata->hd.dma);
  239. scsi_host_put(host);
  240. }
  241. static struct platform_driver sgiwd93_driver = {
  242. .probe = sgiwd93_probe,
  243. .remove = __devexit_p(sgiwd93_remove),
  244. .driver = {
  245. .name = "sgiwd93"
  246. }
  247. };
  248. static int __init sgiwd93_module_init(void)
  249. {
  250. return platform_driver_register(&sgiwd93_driver);
  251. }
  252. static void __exit sgiwd93_module_exit(void)
  253. {
  254. return platform_driver_unregister(&sgiwd93_driver);
  255. }
  256. module_init(sgiwd93_module_init);
  257. module_exit(sgiwd93_module_exit);
  258. MODULE_DESCRIPTION("SGI WD33C93 driver");
  259. MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");
  260. MODULE_LICENSE("GPL");