blob: 57d5dff62f6364692c96c1e88605eb9dc7e3ed4d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Justin P. Mattock79add622011-04-04 14:15:29 -07006 * Copyright (C) 1996 David S. Miller (davem@davemloft.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Copyright (C) 1999 Andrew R. Baker (andrewb@uab.edu)
8 * Copyright (C) 2001 Florian Lohoff (flo@rfc822.org)
Ralf Baechledf9f54082007-05-11 15:48:58 +01009 * Copyright (C) 2003, 07 Ralf Baechle (ralf@linux-mips.org)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * (In all truth, Jed Schimmel wrote all this code.)
12 */
Ralf Baechledf9f54082007-05-11 15:48:58 +010013
14#undef DEBUG
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/delay.h>
17#include <linux/dma-mapping.h>
Ralf Baechledf9f54082007-05-11 15:48:58 +010018#include <linux/gfp.h>
19#include <linux/interrupt.h>
20#include <linux/init.h>
21#include <linux/kernel.h>
22#include <linux/types.h>
23#include <linux/module.h>
24#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/spinlock.h>
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/sgi/hpc3.h>
28#include <asm/sgi/ip22.h>
Ralf Baechledf9f54082007-05-11 15:48:58 +010029#include <asm/sgi/wd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Bart Van Assche53555fb2022-02-18 11:50:34 -080031#include <scsi/scsi.h>
32#include <scsi/scsi_cmnd.h>
33#include <scsi/scsi_device.h>
34#include <scsi/scsi_eh.h>
35#include <scsi/scsi_tcq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "wd33c93.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038struct ip22_hostdata {
39 struct WD33C93_hostdata wh;
Thomas Bogendoerfer2adbfa32008-01-27 00:25:53 +010040 dma_addr_t dma;
41 void *cpu;
42 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043};
44
Ralf Baechledf9f54082007-05-11 15:48:58 +010045#define host_to_hostdata(host) ((struct ip22_hostdata *)((host)->hostdata))
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047struct hpc_chunk {
48 struct hpc_dma_desc desc;
49 u32 _padding; /* align to quadword boundary */
50};
51
Thomas Bogendoerfer2adbfa32008-01-27 00:25:53 +010052/* space for hpc dma descriptors */
53#define HPC_DMA_SIZE PAGE_SIZE
54
55#define DMA_DIR(d) ((d == DATA_OUT_DIR) ? DMA_TO_DEVICE : DMA_FROM_DEVICE)
56
David Howells7d12e782006-10-05 14:55:46 +010057static irqreturn_t sgiwd93_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Ralf Baechledf9f54082007-05-11 15:48:58 +010059 struct Scsi_Host * host = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 unsigned long flags;
61
62 spin_lock_irqsave(host->host_lock, flags);
63 wd33c93_intr(host);
64 spin_unlock_irqrestore(host->host_lock, flags);
65
66 return IRQ_HANDLED;
67}
68
69static inline
Thomas Bogendoerfer2adbfa32008-01-27 00:25:53 +010070void fill_hpc_entries(struct ip22_hostdata *hd, struct scsi_cmnd *cmd, int din)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Bart Van Asschedbb2da52022-02-18 11:51:15 -080072 struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(cmd);
73 unsigned long len = scsi_pointer->this_residual;
74 void *addr = scsi_pointer->ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 dma_addr_t physaddr;
76 unsigned long count;
Thomas Bogendoerfer2adbfa32008-01-27 00:25:53 +010077 struct hpc_chunk *hcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Thomas Bogendoerfer2adbfa32008-01-27 00:25:53 +010079 physaddr = dma_map_single(hd->dev, addr, len, DMA_DIR(din));
Bart Van Asschedbb2da52022-02-18 11:51:15 -080080 scsi_pointer->dma_handle = physaddr;
Thomas Bogendoerfer2adbfa32008-01-27 00:25:53 +010081 hcp = hd->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 while (len) {
84 /*
85 * even cntinfo could be up to 16383, without
86 * magic only 8192 works correctly
87 */
88 count = len > 8192 ? 8192 : len;
89 hcp->desc.pbuf = physaddr;
90 hcp->desc.cntinfo = count;
91 hcp++;
92 len -= count;
93 physaddr += count;
94 }
95
96 /*
97 * To make sure, if we trip an HPC bug, that we transfer every single
98 * byte, we tag on an extra zero length dma descriptor at the end of
99 * the chain.
100 */
101 hcp->desc.pbuf = 0;
102 hcp->desc.cntinfo = HPCDMA_EOX;
Christoph Hellwigbd9b8482020-09-01 13:24:21 +0200103 dma_sync_single_for_device(hd->dev, hd->dma,
Thomas Bogendoerfer2adbfa32008-01-27 00:25:53 +0100104 (unsigned long)(hcp + 1) - (unsigned long)hd->cpu,
105 DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Henrik Kretzschmar65396412006-09-12 23:49:33 +0200108static int dma_setup(struct scsi_cmnd *cmd, int datainp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Bart Van Asschedbb2da52022-02-18 11:51:15 -0800110 struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(cmd);
Ralf Baechledf9f54082007-05-11 15:48:58 +0100111 struct ip22_hostdata *hdata = host_to_hostdata(cmd->device->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 struct hpc3_scsiregs *hregs =
113 (struct hpc3_scsiregs *) cmd->device->host->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Thomas Bogendoerfer2adbfa32008-01-27 00:25:53 +0100115 pr_debug("dma_setup: datainp<%d> hcp<%p> ", datainp, hdata->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 hdata->wh.dma_dir = datainp;
118
119 /*
120 * wd33c93 shouldn't pass us bogus dma_setups, but it does:-( The
121 * other wd33c93 drivers deal with it the same way (which isn't that
122 * obvious). IMHO a better fix would be, not to do these dma setups
123 * in the first place.
124 */
Bart Van Asschedbb2da52022-02-18 11:51:15 -0800125 if (scsi_pointer->ptr == NULL || scsi_pointer->this_residual == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return 1;
127
Thomas Bogendoerfer2adbfa32008-01-27 00:25:53 +0100128 fill_hpc_entries(hdata, cmd, datainp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Ralf Baechledf9f54082007-05-11 15:48:58 +0100130 pr_debug(" HPCGO\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 /* Start up the HPC. */
Thomas Bogendoerfer2adbfa32008-01-27 00:25:53 +0100133 hregs->ndptr = hdata->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (datainp)
135 hregs->ctrl = HPC3_SCTRL_ACTIVE;
136 else
137 hregs->ctrl = HPC3_SCTRL_ACTIVE | HPC3_SCTRL_DIR;
138
139 return 0;
140}
141
Henrik Kretzschmar65396412006-09-12 23:49:33 +0200142static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 int status)
144{
Bart Van Asschedbb2da52022-02-18 11:51:15 -0800145 struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(SCpnt);
Ralf Baechledf9f54082007-05-11 15:48:58 +0100146 struct ip22_hostdata *hdata = host_to_hostdata(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 struct hpc3_scsiregs *hregs;
148
149 if (!SCpnt)
150 return;
151
Bart Van Asschedbb2da52022-02-18 11:51:15 -0800152 if (scsi_pointer->ptr == NULL || scsi_pointer->this_residual == 0)
Thomas Bogendoerfer2adbfa32008-01-27 00:25:53 +0100153 return;
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 hregs = (struct hpc3_scsiregs *) SCpnt->device->host->base;
156
Ralf Baechledf9f54082007-05-11 15:48:58 +0100157 pr_debug("dma_stop: status<%d> ", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 /* First stop the HPC and flush it's FIFO. */
160 if (hdata->wh.dma_dir) {
161 hregs->ctrl |= HPC3_SCTRL_FLUSH;
162 while (hregs->ctrl & HPC3_SCTRL_ACTIVE)
163 barrier();
164 }
165 hregs->ctrl = 0;
Bart Van Asschedbb2da52022-02-18 11:51:15 -0800166 dma_unmap_single(hdata->dev, scsi_pointer->dma_handle,
167 scsi_pointer->this_residual,
Thomas Bogendoerfer2adbfa32008-01-27 00:25:53 +0100168 DMA_DIR(hdata->wh.dma_dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Ralf Baechledf9f54082007-05-11 15:48:58 +0100170 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173void sgiwd93_reset(unsigned long base)
174{
175 struct hpc3_scsiregs *hregs = (struct hpc3_scsiregs *) base;
176
177 hregs->ctrl = HPC3_SCTRL_CRESET;
178 udelay(50);
179 hregs->ctrl = 0;
180}
Andrew Mortonfc9b5112007-12-13 16:14:23 -0800181EXPORT_SYMBOL_GPL(sgiwd93_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Thomas Bogendoerfer2adbfa32008-01-27 00:25:53 +0100183static inline void init_hpc_chain(struct ip22_hostdata *hdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Thomas Bogendoerfer2adbfa32008-01-27 00:25:53 +0100185 struct hpc_chunk *hcp = (struct hpc_chunk *)hdata->cpu;
186 dma_addr_t dma = hdata->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 unsigned long start, end;
188
189 start = (unsigned long) hcp;
Thomas Bogendoerfer2adbfa32008-01-27 00:25:53 +0100190 end = start + HPC_DMA_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 while (start < end) {
Thomas Bogendoerfer2adbfa32008-01-27 00:25:53 +0100192 hcp->desc.pnext = (u32) (dma + sizeof(struct hpc_chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 hcp->desc.cntinfo = HPCDMA_EOX;
Thomas Bogendoerfer2adbfa32008-01-27 00:25:53 +0100194 hcp++;
195 dma += sizeof(struct hpc_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 start += sizeof(struct hpc_chunk);
Jason Yanf371d532020-04-21 11:40:29 +0800197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 hcp--;
Thomas Bogendoerfer2adbfa32008-01-27 00:25:53 +0100199 hcp->desc.pnext = hdata->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/*
203 * Kludge alert - the SCSI code calls the abort and reset method with int
204 * arguments not with pointers. So this is going to blow up beautyfully
205 * on 64-bit systems with memory outside the compat address spaces.
206 */
Ralf Baechledf9f54082007-05-11 15:48:58 +0100207static struct scsi_host_template sgiwd93_template = {
208 .module = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 .proc_name = "SGIWD93",
210 .name = "SGI WD93",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 .queuecommand = wd33c93_queuecommand,
212 .eh_abort_handler = wd33c93_abort,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 .eh_host_reset_handler = wd33c93_host_reset,
Ralf Baechled25cf1c2005-10-20 19:04:34 +0100214 .can_queue = 16,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 .this_id = 7,
216 .sg_tablesize = SG_ALL,
Ralf Baechled25cf1c2005-10-20 19:04:34 +0100217 .cmd_per_lun = 8,
Christoph Hellwig4af14d12018-12-13 16:17:09 +0100218 .dma_boundary = PAGE_SIZE - 1,
Bart Van Asschedbb2da52022-02-18 11:51:15 -0800219 .cmd_size = sizeof(struct scsi_pointer),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220};
Ralf Baechledf9f54082007-05-11 15:48:58 +0100221
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800222static int sgiwd93_probe(struct platform_device *pdev)
Ralf Baechledf9f54082007-05-11 15:48:58 +0100223{
224 struct sgiwd93_platform_data *pd = pdev->dev.platform_data;
225 unsigned char *wdregs = pd->wdregs;
226 struct hpc3_scsiregs *hregs = pd->hregs;
227 struct ip22_hostdata *hdata;
228 struct Scsi_Host *host;
229 wd33c93_regs regs;
230 unsigned int unit = pd->unit;
231 unsigned int irq = pd->irq;
232 int err;
233
234 host = scsi_host_alloc(&sgiwd93_template, sizeof(struct ip22_hostdata));
235 if (!host) {
236 err = -ENOMEM;
237 goto out;
238 }
239
240 host->base = (unsigned long) hregs;
241 host->irq = irq;
242
243 hdata = host_to_hostdata(host);
Thomas Bogendoerfer2adbfa32008-01-27 00:25:53 +0100244 hdata->dev = &pdev->dev;
Christoph Hellwigbd9b8482020-09-01 13:24:21 +0200245 hdata->cpu = dma_alloc_noncoherent(&pdev->dev, HPC_DMA_SIZE,
246 &hdata->dma, DMA_TO_DEVICE, GFP_KERNEL);
Thomas Bogendoerfer2adbfa32008-01-27 00:25:53 +0100247 if (!hdata->cpu) {
Ralf Baechledf9f54082007-05-11 15:48:58 +0100248 printk(KERN_WARNING "sgiwd93: Could not allocate memory for "
249 "host %d buffer.\n", unit);
250 err = -ENOMEM;
251 goto out_put;
252 }
253
Thomas Bogendoerfer2adbfa32008-01-27 00:25:53 +0100254 init_hpc_chain(hdata);
Ralf Baechledf9f54082007-05-11 15:48:58 +0100255
256 regs.SASR = wdregs + 3;
257 regs.SCMD = wdregs + 7;
258
Thomas Bogendoerferbe3cb3d2008-03-21 22:25:43 +0100259 hdata->wh.no_sync = 0;
260 hdata->wh.fast = 1;
261 hdata->wh.dma_mode = CTRL_BURST;
Ralf Baechledf9f54082007-05-11 15:48:58 +0100262
Thomas Bogendoerferbe3cb3d2008-03-21 22:25:43 +0100263 wd33c93_init(host, regs, dma_setup, dma_stop, WD33C93_FS_MHZ(20));
Ralf Baechledf9f54082007-05-11 15:48:58 +0100264
265 err = request_irq(irq, sgiwd93_intr, 0, "SGI WD93", host);
266 if (err) {
267 printk(KERN_WARNING "sgiwd93: Could not register irq %d "
268 "for host %d.\n", irq, unit);
269 goto out_free;
270 }
271
272 platform_set_drvdata(pdev, host);
273
274 err = scsi_add_host(host, NULL);
275 if (err)
276 goto out_irq;
277
278 scsi_scan_host(host);
279
280 return 0;
281
282out_irq:
283 free_irq(irq, host);
284out_free:
Christoph Hellwigbd9b8482020-09-01 13:24:21 +0200285 dma_free_noncoherent(&pdev->dev, HPC_DMA_SIZE, hdata->cpu, hdata->dma,
286 DMA_TO_DEVICE);
Ralf Baechledf9f54082007-05-11 15:48:58 +0100287out_put:
288 scsi_host_put(host);
289out:
290
291 return err;
292}
293
Dmitry Torokhovba212222017-03-01 17:38:38 -0800294static int sgiwd93_remove(struct platform_device *pdev)
Ralf Baechledf9f54082007-05-11 15:48:58 +0100295{
296 struct Scsi_Host *host = platform_get_drvdata(pdev);
297 struct ip22_hostdata *hdata = (struct ip22_hostdata *) host->hostdata;
298 struct sgiwd93_platform_data *pd = pdev->dev.platform_data;
299
300 scsi_remove_host(host);
301 free_irq(pd->irq, host);
Christoph Hellwigbd9b8482020-09-01 13:24:21 +0200302 dma_free_noncoherent(&pdev->dev, HPC_DMA_SIZE, hdata->cpu, hdata->dma,
303 DMA_TO_DEVICE);
Ralf Baechledf9f54082007-05-11 15:48:58 +0100304 scsi_host_put(host);
Dmitri Vorobievb99b4c62008-12-16 15:04:44 +0200305 return 0;
Ralf Baechledf9f54082007-05-11 15:48:58 +0100306}
307
308static struct platform_driver sgiwd93_driver = {
309 .probe = sgiwd93_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800310 .remove = sgiwd93_remove,
Ralf Baechledf9f54082007-05-11 15:48:58 +0100311 .driver = {
Kay Sieversecc1241e2008-04-18 13:57:19 -0700312 .name = "sgiwd93",
Ralf Baechledf9f54082007-05-11 15:48:58 +0100313 }
314};
315
316static int __init sgiwd93_module_init(void)
317{
318 return platform_driver_register(&sgiwd93_driver);
319}
320
321static void __exit sgiwd93_module_exit(void)
322{
323 return platform_driver_unregister(&sgiwd93_driver);
324}
325
326module_init(sgiwd93_module_init);
327module_exit(sgiwd93_module_exit);
328
329MODULE_DESCRIPTION("SGI WD33C93 driver");
330MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");
331MODULE_LICENSE("GPL");
Kay Sieversecc1241e2008-04-18 13:57:19 -0700332MODULE_ALIAS("platform:sgiwd93");