sgiwd93.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 ([email protected])
  7. * Copyright (C) 1999 Andrew R. Baker ([email protected])
  8. * Copyright (C) 2001 Florian Lohoff ([email protected])
  9. * Copyright (C) 2003, 07 Ralf Baechle ([email protected])
  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/scsi.h>
  28. #include <scsi/scsi_cmnd.h>
  29. #include <scsi/scsi_device.h>
  30. #include <scsi/scsi_eh.h>
  31. #include <scsi/scsi_tcq.h>
  32. #include "wd33c93.h"
  33. struct ip22_hostdata {
  34. struct WD33C93_hostdata wh;
  35. dma_addr_t dma;
  36. void *cpu;
  37. struct device *dev;
  38. };
  39. #define host_to_hostdata(host) ((struct ip22_hostdata *)((host)->hostdata))
  40. struct hpc_chunk {
  41. struct hpc_dma_desc desc;
  42. u32 _padding; /* align to quadword boundary */
  43. };
  44. /* space for hpc dma descriptors */
  45. #define HPC_DMA_SIZE PAGE_SIZE
  46. #define DMA_DIR(d) ((d == DATA_OUT_DIR) ? DMA_TO_DEVICE : DMA_FROM_DEVICE)
  47. static irqreturn_t sgiwd93_intr(int irq, void *dev_id)
  48. {
  49. struct Scsi_Host * host = dev_id;
  50. unsigned long flags;
  51. spin_lock_irqsave(host->host_lock, flags);
  52. wd33c93_intr(host);
  53. spin_unlock_irqrestore(host->host_lock, flags);
  54. return IRQ_HANDLED;
  55. }
  56. static inline
  57. void fill_hpc_entries(struct ip22_hostdata *hd, struct scsi_cmnd *cmd, int din)
  58. {
  59. struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(cmd);
  60. unsigned long len = scsi_pointer->this_residual;
  61. void *addr = scsi_pointer->ptr;
  62. dma_addr_t physaddr;
  63. unsigned long count;
  64. struct hpc_chunk *hcp;
  65. physaddr = dma_map_single(hd->dev, addr, len, DMA_DIR(din));
  66. scsi_pointer->dma_handle = physaddr;
  67. hcp = hd->cpu;
  68. while (len) {
  69. /*
  70. * even cntinfo could be up to 16383, without
  71. * magic only 8192 works correctly
  72. */
  73. count = len > 8192 ? 8192 : len;
  74. hcp->desc.pbuf = physaddr;
  75. hcp->desc.cntinfo = count;
  76. hcp++;
  77. len -= count;
  78. physaddr += count;
  79. }
  80. /*
  81. * To make sure, if we trip an HPC bug, that we transfer every single
  82. * byte, we tag on an extra zero length dma descriptor at the end of
  83. * the chain.
  84. */
  85. hcp->desc.pbuf = 0;
  86. hcp->desc.cntinfo = HPCDMA_EOX;
  87. dma_sync_single_for_device(hd->dev, hd->dma,
  88. (unsigned long)(hcp + 1) - (unsigned long)hd->cpu,
  89. DMA_TO_DEVICE);
  90. }
  91. static int dma_setup(struct scsi_cmnd *cmd, int datainp)
  92. {
  93. struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(cmd);
  94. struct ip22_hostdata *hdata = host_to_hostdata(cmd->device->host);
  95. struct hpc3_scsiregs *hregs =
  96. (struct hpc3_scsiregs *) cmd->device->host->base;
  97. pr_debug("dma_setup: datainp<%d> hcp<%p> ", datainp, hdata->cpu);
  98. hdata->wh.dma_dir = datainp;
  99. /*
  100. * wd33c93 shouldn't pass us bogus dma_setups, but it does:-( The
  101. * other wd33c93 drivers deal with it the same way (which isn't that
  102. * obvious). IMHO a better fix would be, not to do these dma setups
  103. * in the first place.
  104. */
  105. if (scsi_pointer->ptr == NULL || scsi_pointer->this_residual == 0)
  106. return 1;
  107. fill_hpc_entries(hdata, cmd, datainp);
  108. pr_debug(" HPCGO\n");
  109. /* Start up the HPC. */
  110. hregs->ndptr = hdata->dma;
  111. if (datainp)
  112. hregs->ctrl = HPC3_SCTRL_ACTIVE;
  113. else
  114. hregs->ctrl = HPC3_SCTRL_ACTIVE | HPC3_SCTRL_DIR;
  115. return 0;
  116. }
  117. static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
  118. int status)
  119. {
  120. struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(SCpnt);
  121. struct ip22_hostdata *hdata = host_to_hostdata(instance);
  122. struct hpc3_scsiregs *hregs;
  123. if (!SCpnt)
  124. return;
  125. if (scsi_pointer->ptr == NULL || scsi_pointer->this_residual == 0)
  126. return;
  127. hregs = (struct hpc3_scsiregs *) SCpnt->device->host->base;
  128. pr_debug("dma_stop: status<%d> ", status);
  129. /* First stop the HPC and flush it's FIFO. */
  130. if (hdata->wh.dma_dir) {
  131. hregs->ctrl |= HPC3_SCTRL_FLUSH;
  132. while (hregs->ctrl & HPC3_SCTRL_ACTIVE)
  133. barrier();
  134. }
  135. hregs->ctrl = 0;
  136. dma_unmap_single(hdata->dev, scsi_pointer->dma_handle,
  137. scsi_pointer->this_residual,
  138. DMA_DIR(hdata->wh.dma_dir));
  139. pr_debug("\n");
  140. }
  141. void sgiwd93_reset(unsigned long base)
  142. {
  143. struct hpc3_scsiregs *hregs = (struct hpc3_scsiregs *) base;
  144. hregs->ctrl = HPC3_SCTRL_CRESET;
  145. udelay(50);
  146. hregs->ctrl = 0;
  147. }
  148. EXPORT_SYMBOL_GPL(sgiwd93_reset);
  149. static inline void init_hpc_chain(struct ip22_hostdata *hdata)
  150. {
  151. struct hpc_chunk *hcp = (struct hpc_chunk *)hdata->cpu;
  152. dma_addr_t dma = hdata->dma;
  153. unsigned long start, end;
  154. start = (unsigned long) hcp;
  155. end = start + HPC_DMA_SIZE;
  156. while (start < end) {
  157. hcp->desc.pnext = (u32) (dma + sizeof(struct hpc_chunk));
  158. hcp->desc.cntinfo = HPCDMA_EOX;
  159. hcp++;
  160. dma += sizeof(struct hpc_chunk);
  161. start += sizeof(struct hpc_chunk);
  162. }
  163. hcp--;
  164. hcp->desc.pnext = hdata->dma;
  165. }
  166. /*
  167. * Kludge alert - the SCSI code calls the abort and reset method with int
  168. * arguments not with pointers. So this is going to blow up beautyfully
  169. * on 64-bit systems with memory outside the compat address spaces.
  170. */
  171. static struct scsi_host_template sgiwd93_template = {
  172. .module = THIS_MODULE,
  173. .proc_name = "SGIWD93",
  174. .name = "SGI WD93",
  175. .queuecommand = wd33c93_queuecommand,
  176. .eh_abort_handler = wd33c93_abort,
  177. .eh_host_reset_handler = wd33c93_host_reset,
  178. .can_queue = 16,
  179. .this_id = 7,
  180. .sg_tablesize = SG_ALL,
  181. .cmd_per_lun = 8,
  182. .dma_boundary = PAGE_SIZE - 1,
  183. .cmd_size = sizeof(struct scsi_pointer),
  184. };
  185. static int sgiwd93_probe(struct platform_device *pdev)
  186. {
  187. struct sgiwd93_platform_data *pd = pdev->dev.platform_data;
  188. unsigned char *wdregs = pd->wdregs;
  189. struct hpc3_scsiregs *hregs = pd->hregs;
  190. struct ip22_hostdata *hdata;
  191. struct Scsi_Host *host;
  192. wd33c93_regs regs;
  193. unsigned int unit = pd->unit;
  194. unsigned int irq = pd->irq;
  195. int err;
  196. host = scsi_host_alloc(&sgiwd93_template, sizeof(struct ip22_hostdata));
  197. if (!host) {
  198. err = -ENOMEM;
  199. goto out;
  200. }
  201. host->base = (unsigned long) hregs;
  202. host->irq = irq;
  203. hdata = host_to_hostdata(host);
  204. hdata->dev = &pdev->dev;
  205. hdata->cpu = dma_alloc_noncoherent(&pdev->dev, HPC_DMA_SIZE,
  206. &hdata->dma, DMA_TO_DEVICE, GFP_KERNEL);
  207. if (!hdata->cpu) {
  208. printk(KERN_WARNING "sgiwd93: Could not allocate memory for "
  209. "host %d buffer.\n", unit);
  210. err = -ENOMEM;
  211. goto out_put;
  212. }
  213. init_hpc_chain(hdata);
  214. regs.SASR = wdregs + 3;
  215. regs.SCMD = wdregs + 7;
  216. hdata->wh.no_sync = 0;
  217. hdata->wh.fast = 1;
  218. hdata->wh.dma_mode = CTRL_BURST;
  219. wd33c93_init(host, regs, dma_setup, dma_stop, WD33C93_FS_MHZ(20));
  220. err = request_irq(irq, sgiwd93_intr, 0, "SGI WD93", host);
  221. if (err) {
  222. printk(KERN_WARNING "sgiwd93: Could not register irq %d "
  223. "for host %d.\n", irq, unit);
  224. goto out_free;
  225. }
  226. platform_set_drvdata(pdev, host);
  227. err = scsi_add_host(host, NULL);
  228. if (err)
  229. goto out_irq;
  230. scsi_scan_host(host);
  231. return 0;
  232. out_irq:
  233. free_irq(irq, host);
  234. out_free:
  235. dma_free_noncoherent(&pdev->dev, HPC_DMA_SIZE, hdata->cpu, hdata->dma,
  236. DMA_TO_DEVICE);
  237. out_put:
  238. scsi_host_put(host);
  239. out:
  240. return err;
  241. }
  242. static int sgiwd93_remove(struct platform_device *pdev)
  243. {
  244. struct Scsi_Host *host = platform_get_drvdata(pdev);
  245. struct ip22_hostdata *hdata = (struct ip22_hostdata *) host->hostdata;
  246. struct sgiwd93_platform_data *pd = pdev->dev.platform_data;
  247. scsi_remove_host(host);
  248. free_irq(pd->irq, host);
  249. dma_free_noncoherent(&pdev->dev, HPC_DMA_SIZE, hdata->cpu, hdata->dma,
  250. DMA_TO_DEVICE);
  251. scsi_host_put(host);
  252. return 0;
  253. }
  254. static struct platform_driver sgiwd93_driver = {
  255. .probe = sgiwd93_probe,
  256. .remove = sgiwd93_remove,
  257. .driver = {
  258. .name = "sgiwd93",
  259. }
  260. };
  261. static int __init sgiwd93_module_init(void)
  262. {
  263. return platform_driver_register(&sgiwd93_driver);
  264. }
  265. static void __exit sgiwd93_module_exit(void)
  266. {
  267. return platform_driver_unregister(&sgiwd93_driver);
  268. }
  269. module_init(sgiwd93_module_init);
  270. module_exit(sgiwd93_module_exit);
  271. MODULE_DESCRIPTION("SGI WD33C93 driver");
  272. MODULE_AUTHOR("Ralf Baechle <[email protected]>");
  273. MODULE_LICENSE("GPL");
  274. MODULE_ALIAS("platform:sgiwd93");