sgiwd93.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 Ralf Baechle (ralf@linux-mips.org)
  10. *
  11. * (In all truth, Jed Schimmel wrote all this code.)
  12. */
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/types.h>
  16. #include <linux/mm.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/version.h>
  19. #include <linux/delay.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/spinlock.h>
  22. #include <asm/page.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/sgialib.h>
  25. #include <asm/sgi/sgi.h>
  26. #include <asm/sgi/mc.h>
  27. #include <asm/sgi/hpc3.h>
  28. #include <asm/sgi/ip22.h>
  29. #include <asm/irq.h>
  30. #include <asm/io.h>
  31. #include "scsi.h"
  32. #include <scsi/scsi_host.h>
  33. #include "wd33c93.h"
  34. #include "sgiwd93.h"
  35. #include <linux/stat.h>
  36. #if 0
  37. #define DPRINTK(args...) printk(args)
  38. #else
  39. #define DPRINTK(args...)
  40. #endif
  41. #define HDATA(ptr) ((struct ip22_hostdata *)((ptr)->hostdata))
  42. struct ip22_hostdata {
  43. struct WD33C93_hostdata wh;
  44. struct hpc_data {
  45. dma_addr_t dma;
  46. void * cpu;
  47. } hd;
  48. };
  49. struct hpc_chunk {
  50. struct hpc_dma_desc desc;
  51. u32 _padding; /* align to quadword boundary */
  52. };
  53. struct Scsi_Host *sgiwd93_host;
  54. struct Scsi_Host *sgiwd93_host1;
  55. /* Wuff wuff, wuff, wd33c93.c, wuff wuff, object oriented, bow wow. */
  56. static inline void write_wd33c93_count(const wd33c93_regs regs,
  57. unsigned long value)
  58. {
  59. *regs.SASR = WD_TRANSFER_COUNT_MSB;
  60. mb();
  61. *regs.SCMD = ((value >> 16) & 0xff);
  62. *regs.SCMD = ((value >> 8) & 0xff);
  63. *regs.SCMD = ((value >> 0) & 0xff);
  64. mb();
  65. }
  66. static inline unsigned long read_wd33c93_count(const wd33c93_regs regs)
  67. {
  68. unsigned long value;
  69. *regs.SASR = WD_TRANSFER_COUNT_MSB;
  70. mb();
  71. value = ((*regs.SCMD & 0xff) << 16);
  72. value |= ((*regs.SCMD & 0xff) << 8);
  73. value |= ((*regs.SCMD & 0xff) << 0);
  74. mb();
  75. return value;
  76. }
  77. static irqreturn_t sgiwd93_intr(int irq, void *dev_id, struct pt_regs *regs)
  78. {
  79. struct Scsi_Host * host = (struct Scsi_Host *) dev_id;
  80. unsigned long flags;
  81. spin_lock_irqsave(host->host_lock, flags);
  82. wd33c93_intr(host);
  83. spin_unlock_irqrestore(host->host_lock, flags);
  84. return IRQ_HANDLED;
  85. }
  86. static inline
  87. void fill_hpc_entries(struct hpc_chunk *hcp, Scsi_Cmnd *cmd, int datainp)
  88. {
  89. unsigned long len = cmd->SCp.this_residual;
  90. void *addr = cmd->SCp.ptr;
  91. dma_addr_t physaddr;
  92. unsigned long count;
  93. physaddr = dma_map_single(NULL, addr, len, cmd->sc_data_direction);
  94. cmd->SCp.dma_handle = physaddr;
  95. while (len) {
  96. /*
  97. * even cntinfo could be up to 16383, without
  98. * magic only 8192 works correctly
  99. */
  100. count = len > 8192 ? 8192 : len;
  101. hcp->desc.pbuf = physaddr;
  102. hcp->desc.cntinfo = count;
  103. hcp++;
  104. len -= count;
  105. physaddr += count;
  106. }
  107. /*
  108. * To make sure, if we trip an HPC bug, that we transfer every single
  109. * byte, we tag on an extra zero length dma descriptor at the end of
  110. * the chain.
  111. */
  112. hcp->desc.pbuf = 0;
  113. hcp->desc.cntinfo = HPCDMA_EOX;
  114. }
  115. static int dma_setup(Scsi_Cmnd *cmd, int datainp)
  116. {
  117. struct ip22_hostdata *hdata = HDATA(cmd->device->host);
  118. struct hpc3_scsiregs *hregs =
  119. (struct hpc3_scsiregs *) cmd->device->host->base;
  120. struct hpc_chunk *hcp = (struct hpc_chunk *) hdata->hd.cpu;
  121. DPRINTK("dma_setup: datainp<%d> hcp<%p> ", datainp, hcp);
  122. hdata->wh.dma_dir = datainp;
  123. /*
  124. * wd33c93 shouldn't pass us bogus dma_setups, but it does:-( The
  125. * other wd33c93 drivers deal with it the same way (which isn't that
  126. * obvious). IMHO a better fix would be, not to do these dma setups
  127. * in the first place.
  128. */
  129. if (cmd->SCp.ptr == NULL || cmd->SCp.this_residual == 0)
  130. return 1;
  131. fill_hpc_entries(hcp, cmd, datainp);
  132. DPRINTK(" HPCGO\n");
  133. /* Start up the HPC. */
  134. hregs->ndptr = hdata->hd.dma;
  135. if (datainp)
  136. hregs->ctrl = HPC3_SCTRL_ACTIVE;
  137. else
  138. hregs->ctrl = HPC3_SCTRL_ACTIVE | HPC3_SCTRL_DIR;
  139. return 0;
  140. }
  141. static void dma_stop(struct Scsi_Host *instance, Scsi_Cmnd *SCpnt,
  142. int status)
  143. {
  144. struct ip22_hostdata *hdata = HDATA(instance);
  145. struct hpc3_scsiregs *hregs;
  146. if (!SCpnt)
  147. return;
  148. hregs = (struct hpc3_scsiregs *) SCpnt->device->host->base;
  149. DPRINTK("dma_stop: status<%d> ", status);
  150. /* First stop the HPC and flush it's FIFO. */
  151. if (hdata->wh.dma_dir) {
  152. hregs->ctrl |= HPC3_SCTRL_FLUSH;
  153. while (hregs->ctrl & HPC3_SCTRL_ACTIVE)
  154. barrier();
  155. }
  156. hregs->ctrl = 0;
  157. dma_unmap_single(NULL, SCpnt->SCp.dma_handle, SCpnt->SCp.this_residual,
  158. SCpnt->sc_data_direction);
  159. DPRINTK("\n");
  160. }
  161. void sgiwd93_reset(unsigned long base)
  162. {
  163. struct hpc3_scsiregs *hregs = (struct hpc3_scsiregs *) base;
  164. hregs->ctrl = HPC3_SCTRL_CRESET;
  165. udelay(50);
  166. hregs->ctrl = 0;
  167. }
  168. static inline void init_hpc_chain(struct hpc_data *hd)
  169. {
  170. struct hpc_chunk *hcp = (struct hpc_chunk *) hd->cpu;
  171. struct hpc_chunk *dma = (struct hpc_chunk *) hd->dma;
  172. unsigned long start, end;
  173. start = (unsigned long) hcp;
  174. end = start + PAGE_SIZE;
  175. while (start < end) {
  176. hcp->desc.pnext = (u32) (dma + 1);
  177. hcp->desc.cntinfo = HPCDMA_EOX;
  178. hcp++; dma++;
  179. start += sizeof(struct hpc_chunk);
  180. };
  181. hcp--;
  182. hcp->desc.pnext = hd->dma;
  183. }
  184. static struct Scsi_Host * __init sgiwd93_setup_scsi(
  185. Scsi_Host_Template *SGIblows, int unit, int irq,
  186. struct hpc3_scsiregs *hregs, unsigned char *wdregs)
  187. {
  188. struct ip22_hostdata *hdata;
  189. struct Scsi_Host *host;
  190. wd33c93_regs regs;
  191. host = scsi_register(SGIblows, sizeof(struct ip22_hostdata));
  192. if (!host)
  193. return NULL;
  194. host->base = (unsigned long) hregs;
  195. host->irq = irq;
  196. hdata = HDATA(host);
  197. hdata->hd.cpu = dma_alloc_coherent(NULL, PAGE_SIZE, &hdata->hd.dma,
  198. GFP_KERNEL);
  199. if (!hdata->hd.cpu) {
  200. printk(KERN_WARNING "sgiwd93: Could not allocate memory for "
  201. "host %d buffer.\n", unit);
  202. goto out_unregister;
  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_16_20);
  208. hdata->wh.no_sync = 0;
  209. if (request_irq(irq, sgiwd93_intr, 0, "SGI WD93", (void *) host)) {
  210. printk(KERN_WARNING "sgiwd93: Could not register irq %d "
  211. "for host %d.\n", irq, unit);
  212. goto out_free;
  213. }
  214. return host;
  215. out_free:
  216. dma_free_coherent(NULL, PAGE_SIZE, hdata->hd.cpu, hdata->hd.dma);
  217. wd33c93_release();
  218. out_unregister:
  219. scsi_unregister(host);
  220. return NULL;
  221. }
  222. int __init sgiwd93_detect(Scsi_Host_Template *SGIblows)
  223. {
  224. int found = 0;
  225. SGIblows->proc_name = "SGIWD93";
  226. sgiwd93_host = sgiwd93_setup_scsi(SGIblows, 0, SGI_WD93_0_IRQ,
  227. &hpc3c0->scsi_chan0,
  228. (unsigned char *)hpc3c0->scsi0_ext);
  229. if (sgiwd93_host)
  230. found++;
  231. /* Set up second controller on the Indigo2 */
  232. if (ip22_is_fullhouse()) {
  233. sgiwd93_host1 = sgiwd93_setup_scsi(SGIblows, 1, SGI_WD93_1_IRQ,
  234. &hpc3c0->scsi_chan1,
  235. (unsigned char *)hpc3c0->scsi1_ext);
  236. if (sgiwd93_host1)
  237. found++;
  238. }
  239. return found;
  240. }
  241. int sgiwd93_release(struct Scsi_Host *instance)
  242. {
  243. struct ip22_hostdata *hdata = HDATA(instance);
  244. int irq = 0;
  245. if (sgiwd93_host && sgiwd93_host == instance)
  246. irq = SGI_WD93_0_IRQ;
  247. else if (sgiwd93_host1 && sgiwd93_host1 == instance)
  248. irq = SGI_WD93_1_IRQ;
  249. free_irq(irq, sgiwd93_intr);
  250. dma_free_coherent(NULL, PAGE_SIZE, hdata->hd.cpu, hdata->hd.dma);
  251. wd33c93_release();
  252. return 1;
  253. }
  254. static int sgiwd93_bus_reset(Scsi_Cmnd *cmd)
  255. {
  256. /* FIXME perform bus-specific reset */
  257. /* FIXME 2: kill this function, and let midlayer fallback
  258. to the same result, calling wd33c93_host_reset() */
  259. spin_lock_irq(cmd->device->host->host_lock);
  260. wd33c93_host_reset(cmd);
  261. spin_unlock_irq(cmd->device->host->host_lock);
  262. return SUCCESS;
  263. }
  264. /*
  265. * Kludge alert - the SCSI code calls the abort and reset method with int
  266. * arguments not with pointers. So this is going to blow up beautyfully
  267. * on 64-bit systems with memory outside the compat address spaces.
  268. */
  269. static Scsi_Host_Template driver_template = {
  270. .proc_name = "SGIWD93",
  271. .name = "SGI WD93",
  272. .detect = sgiwd93_detect,
  273. .release = sgiwd93_release,
  274. .queuecommand = wd33c93_queuecommand,
  275. .eh_abort_handler = wd33c93_abort,
  276. .eh_bus_reset_handler = sgiwd93_bus_reset,
  277. .eh_host_reset_handler = wd33c93_host_reset,
  278. .can_queue = CAN_QUEUE,
  279. .this_id = 7,
  280. .sg_tablesize = SG_ALL,
  281. .cmd_per_lun = CMD_PER_LUN,
  282. .use_clustering = DISABLE_CLUSTERING,
  283. };
  284. #include "scsi_module.c"