Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | * | |
79add627 | 6 | * Copyright (C) 1996 David S. Miller (davem@davemloft.net) |
1da177e4 LT |
7 | * Copyright (C) 1999 Andrew R. Baker (andrewb@uab.edu) |
8 | * Copyright (C) 2001 Florian Lohoff (flo@rfc822.org) | |
df9f5408 | 9 | * Copyright (C) 2003, 07 Ralf Baechle (ralf@linux-mips.org) |
1da177e4 LT |
10 | * |
11 | * (In all truth, Jed Schimmel wrote all this code.) | |
12 | */ | |
df9f5408 RB |
13 | |
14 | #undef DEBUG | |
15 | ||
1da177e4 LT |
16 | #include <linux/delay.h> |
17 | #include <linux/dma-mapping.h> | |
df9f5408 RB |
18 | #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> | |
1da177e4 LT |
25 | #include <linux/spinlock.h> |
26 | ||
1da177e4 LT |
27 | #include <asm/sgi/hpc3.h> |
28 | #include <asm/sgi/ip22.h> | |
df9f5408 | 29 | #include <asm/sgi/wd.h> |
1da177e4 | 30 | |
53555fb7 BVA |
31 | #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> | |
1da177e4 | 36 | #include "wd33c93.h" |
1da177e4 | 37 | |
1da177e4 LT |
38 | struct ip22_hostdata { |
39 | struct WD33C93_hostdata wh; | |
2adbfa33 TB |
40 | dma_addr_t dma; |
41 | void *cpu; | |
42 | struct device *dev; | |
1da177e4 LT |
43 | }; |
44 | ||
df9f5408 RB |
45 | #define host_to_hostdata(host) ((struct ip22_hostdata *)((host)->hostdata)) |
46 | ||
1da177e4 LT |
47 | struct hpc_chunk { |
48 | struct hpc_dma_desc desc; | |
49 | u32 _padding; /* align to quadword boundary */ | |
50 | }; | |
51 | ||
2adbfa33 TB |
52 | /* 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 | ||
7d12e780 | 57 | static irqreturn_t sgiwd93_intr(int irq, void *dev_id) |
1da177e4 | 58 | { |
df9f5408 | 59 | struct Scsi_Host * host = dev_id; |
1da177e4 LT |
60 | 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 | ||
69 | static inline | |
2adbfa33 | 70 | void fill_hpc_entries(struct ip22_hostdata *hd, struct scsi_cmnd *cmd, int din) |
1da177e4 | 71 | { |
dbb2da55 BVA |
72 | struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(cmd); |
73 | unsigned long len = scsi_pointer->this_residual; | |
74 | void *addr = scsi_pointer->ptr; | |
1da177e4 LT |
75 | dma_addr_t physaddr; |
76 | unsigned long count; | |
2adbfa33 | 77 | struct hpc_chunk *hcp; |
1da177e4 | 78 | |
2adbfa33 | 79 | physaddr = dma_map_single(hd->dev, addr, len, DMA_DIR(din)); |
dbb2da55 | 80 | scsi_pointer->dma_handle = physaddr; |
2adbfa33 | 81 | hcp = hd->cpu; |
1da177e4 LT |
82 | |
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; | |
bd9b8485 | 103 | dma_sync_single_for_device(hd->dev, hd->dma, |
2adbfa33 TB |
104 | (unsigned long)(hcp + 1) - (unsigned long)hd->cpu, |
105 | DMA_TO_DEVICE); | |
1da177e4 LT |
106 | } |
107 | ||
65396410 | 108 | static int dma_setup(struct scsi_cmnd *cmd, int datainp) |
1da177e4 | 109 | { |
dbb2da55 | 110 | struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(cmd); |
df9f5408 | 111 | struct ip22_hostdata *hdata = host_to_hostdata(cmd->device->host); |
1da177e4 LT |
112 | struct hpc3_scsiregs *hregs = |
113 | (struct hpc3_scsiregs *) cmd->device->host->base; | |
1da177e4 | 114 | |
2adbfa33 | 115 | pr_debug("dma_setup: datainp<%d> hcp<%p> ", datainp, hdata->cpu); |
1da177e4 LT |
116 | |
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 | */ | |
dbb2da55 | 125 | if (scsi_pointer->ptr == NULL || scsi_pointer->this_residual == 0) |
1da177e4 LT |
126 | return 1; |
127 | ||
2adbfa33 | 128 | fill_hpc_entries(hdata, cmd, datainp); |
1da177e4 | 129 | |
df9f5408 | 130 | pr_debug(" HPCGO\n"); |
1da177e4 LT |
131 | |
132 | /* Start up the HPC. */ | |
2adbfa33 | 133 | hregs->ndptr = hdata->dma; |
1da177e4 LT |
134 | 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 | ||
65396410 | 142 | static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt, |
1da177e4 LT |
143 | int status) |
144 | { | |
dbb2da55 | 145 | struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(SCpnt); |
df9f5408 | 146 | struct ip22_hostdata *hdata = host_to_hostdata(instance); |
1da177e4 LT |
147 | struct hpc3_scsiregs *hregs; |
148 | ||
149 | if (!SCpnt) | |
150 | return; | |
151 | ||
dbb2da55 | 152 | if (scsi_pointer->ptr == NULL || scsi_pointer->this_residual == 0) |
2adbfa33 TB |
153 | return; |
154 | ||
1da177e4 LT |
155 | hregs = (struct hpc3_scsiregs *) SCpnt->device->host->base; |
156 | ||
df9f5408 | 157 | pr_debug("dma_stop: status<%d> ", status); |
1da177e4 LT |
158 | |
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; | |
dbb2da55 BVA |
166 | dma_unmap_single(hdata->dev, scsi_pointer->dma_handle, |
167 | scsi_pointer->this_residual, | |
2adbfa33 | 168 | DMA_DIR(hdata->wh.dma_dir)); |
1da177e4 | 169 | |
df9f5408 | 170 | pr_debug("\n"); |
1da177e4 LT |
171 | } |
172 | ||
173 | void 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 | } | |
fc9b5118 | 181 | EXPORT_SYMBOL_GPL(sgiwd93_reset); |
1da177e4 | 182 | |
2adbfa33 | 183 | static inline void init_hpc_chain(struct ip22_hostdata *hdata) |
1da177e4 | 184 | { |
2adbfa33 TB |
185 | struct hpc_chunk *hcp = (struct hpc_chunk *)hdata->cpu; |
186 | dma_addr_t dma = hdata->dma; | |
1da177e4 LT |
187 | unsigned long start, end; |
188 | ||
189 | start = (unsigned long) hcp; | |
2adbfa33 | 190 | end = start + HPC_DMA_SIZE; |
1da177e4 | 191 | while (start < end) { |
2adbfa33 | 192 | hcp->desc.pnext = (u32) (dma + sizeof(struct hpc_chunk)); |
1da177e4 | 193 | hcp->desc.cntinfo = HPCDMA_EOX; |
2adbfa33 TB |
194 | hcp++; |
195 | dma += sizeof(struct hpc_chunk); | |
1da177e4 | 196 | start += sizeof(struct hpc_chunk); |
f371d534 | 197 | } |
1da177e4 | 198 | hcp--; |
2adbfa33 | 199 | hcp->desc.pnext = hdata->dma; |
1da177e4 LT |
200 | } |
201 | ||
df9f5408 RB |
202 | /* |
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 | */ | |
207 | static struct scsi_host_template sgiwd93_template = { | |
208 | .module = THIS_MODULE, | |
209 | .proc_name = "SGIWD93", | |
210 | .name = "SGI WD93", | |
211 | .queuecommand = wd33c93_queuecommand, | |
212 | .eh_abort_handler = wd33c93_abort, | |
df9f5408 RB |
213 | .eh_host_reset_handler = wd33c93_host_reset, |
214 | .can_queue = 16, | |
215 | .this_id = 7, | |
216 | .sg_tablesize = SG_ALL, | |
217 | .cmd_per_lun = 8, | |
4af14d11 | 218 | .dma_boundary = PAGE_SIZE - 1, |
dbb2da55 | 219 | .cmd_size = sizeof(struct scsi_pointer), |
df9f5408 RB |
220 | }; |
221 | ||
6f039790 | 222 | static int sgiwd93_probe(struct platform_device *pdev) |
1da177e4 | 223 | { |
df9f5408 RB |
224 | struct sgiwd93_platform_data *pd = pdev->dev.platform_data; |
225 | unsigned char *wdregs = pd->wdregs; | |
226 | struct hpc3_scsiregs *hregs = pd->hregs; | |
1da177e4 LT |
227 | struct ip22_hostdata *hdata; |
228 | struct Scsi_Host *host; | |
229 | wd33c93_regs regs; | |
df9f5408 RB |
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 | } | |
1da177e4 LT |
239 | |
240 | host->base = (unsigned long) hregs; | |
241 | host->irq = irq; | |
242 | ||
df9f5408 | 243 | hdata = host_to_hostdata(host); |
2adbfa33 | 244 | hdata->dev = &pdev->dev; |
bd9b8485 CH |
245 | hdata->cpu = dma_alloc_noncoherent(&pdev->dev, HPC_DMA_SIZE, |
246 | &hdata->dma, DMA_TO_DEVICE, GFP_KERNEL); | |
2adbfa33 | 247 | if (!hdata->cpu) { |
1da177e4 LT |
248 | printk(KERN_WARNING "sgiwd93: Could not allocate memory for " |
249 | "host %d buffer.\n", unit); | |
df9f5408 RB |
250 | err = -ENOMEM; |
251 | goto out_put; | |
1da177e4 | 252 | } |
df9f5408 | 253 | |
2adbfa33 | 254 | init_hpc_chain(hdata); |
1da177e4 LT |
255 | |
256 | regs.SASR = wdregs + 3; | |
257 | regs.SCMD = wdregs + 7; | |
258 | ||
be3cb3d8 TB |
259 | hdata->wh.no_sync = 0; |
260 | hdata->wh.fast = 1; | |
261 | hdata->wh.dma_mode = CTRL_BURST; | |
1da177e4 | 262 | |
be3cb3d8 | 263 | wd33c93_init(host, regs, dma_setup, dma_stop, WD33C93_FS_MHZ(20)); |
1da177e4 | 264 | |
df9f5408 RB |
265 | err = request_irq(irq, sgiwd93_intr, 0, "SGI WD93", host); |
266 | if (err) { | |
1da177e4 LT |
267 | printk(KERN_WARNING "sgiwd93: Could not register irq %d " |
268 | "for host %d.\n", irq, unit); | |
269 | goto out_free; | |
270 | } | |
1da177e4 | 271 | |
df9f5408 | 272 | platform_set_drvdata(pdev, host); |
1da177e4 | 273 | |
df9f5408 RB |
274 | err = scsi_add_host(host, NULL); |
275 | if (err) | |
276 | goto out_irq; | |
1da177e4 | 277 | |
df9f5408 | 278 | scsi_scan_host(host); |
1da177e4 | 279 | |
df9f5408 | 280 | return 0; |
1da177e4 | 281 | |
df9f5408 RB |
282 | out_irq: |
283 | free_irq(irq, host); | |
284 | out_free: | |
bd9b8485 CH |
285 | dma_free_noncoherent(&pdev->dev, HPC_DMA_SIZE, hdata->cpu, hdata->dma, |
286 | DMA_TO_DEVICE); | |
df9f5408 RB |
287 | out_put: |
288 | scsi_host_put(host); | |
289 | out: | |
1da177e4 | 290 | |
df9f5408 | 291 | return err; |
1da177e4 LT |
292 | } |
293 | ||
ba21222d | 294 | static int sgiwd93_remove(struct platform_device *pdev) |
1da177e4 | 295 | { |
df9f5408 RB |
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); | |
bd9b8485 CH |
302 | dma_free_noncoherent(&pdev->dev, HPC_DMA_SIZE, hdata->cpu, hdata->dma, |
303 | DMA_TO_DEVICE); | |
df9f5408 | 304 | scsi_host_put(host); |
b99b4c67 | 305 | return 0; |
df9f5408 | 306 | } |
68b3aa7c | 307 | |
df9f5408 RB |
308 | static struct platform_driver sgiwd93_driver = { |
309 | .probe = sgiwd93_probe, | |
6f039790 | 310 | .remove = sgiwd93_remove, |
df9f5408 | 311 | .driver = { |
ecc1241e | 312 | .name = "sgiwd93", |
df9f5408 RB |
313 | } |
314 | }; | |
df0ae249 | 315 | |
df9f5408 RB |
316 | static int __init sgiwd93_module_init(void) |
317 | { | |
318 | return platform_driver_register(&sgiwd93_driver); | |
319 | } | |
68b3aa7c | 320 | |
df9f5408 RB |
321 | static void __exit sgiwd93_module_exit(void) |
322 | { | |
323 | return platform_driver_unregister(&sgiwd93_driver); | |
1da177e4 LT |
324 | } |
325 | ||
df9f5408 RB |
326 | module_init(sgiwd93_module_init); |
327 | module_exit(sgiwd93_module_exit); | |
328 | ||
329 | MODULE_DESCRIPTION("SGI WD33C93 driver"); | |
330 | MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>"); | |
331 | MODULE_LICENSE("GPL"); | |
ecc1241e | 332 | MODULE_ALIAS("platform:sgiwd93"); |