Merge tag 'v2.6.21-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds...
[linux-2.6/linux-mips.git] / drivers / scsi / sgiwd93.c
blob8882a587aa0d9c83695af5b5cb067321a6a228f9
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.
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)
11 * (In all truth, Jed Schimmel wrote all this code.)
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/types.h>
16 #include <linux/mm.h>
17 #include <linux/module.h>
18 #include <linux/blkdev.h>
19 #include <linux/delay.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/spinlock.h>
23 #include <asm/page.h>
24 #include <asm/pgtable.h>
25 #include <asm/sgialib.h>
26 #include <asm/sgi/sgi.h>
27 #include <asm/sgi/mc.h>
28 #include <asm/sgi/hpc3.h>
29 #include <asm/sgi/ip22.h>
30 #include <asm/irq.h>
31 #include <asm/io.h>
33 #include "scsi.h"
34 #include <scsi/scsi_host.h>
35 #include "wd33c93.h"
37 #include <linux/stat.h>
39 #if 0
40 #define DPRINTK(args...) printk(args)
41 #else
42 #define DPRINTK(args...)
43 #endif
45 #define HDATA(ptr) ((struct ip22_hostdata *)((ptr)->hostdata))
47 struct ip22_hostdata {
48 struct WD33C93_hostdata wh;
49 struct hpc_data {
50 dma_addr_t dma;
51 void * cpu;
52 } hd;
55 struct hpc_chunk {
56 struct hpc_dma_desc desc;
57 u32 _padding; /* align to quadword boundary */
60 struct Scsi_Host *sgiwd93_host;
61 struct Scsi_Host *sgiwd93_host1;
63 /* Wuff wuff, wuff, wd33c93.c, wuff wuff, object oriented, bow wow. */
64 static inline void write_wd33c93_count(const wd33c93_regs regs,
65 unsigned long value)
67 *regs.SASR = WD_TRANSFER_COUNT_MSB;
68 mb();
69 *regs.SCMD = ((value >> 16) & 0xff);
70 *regs.SCMD = ((value >> 8) & 0xff);
71 *regs.SCMD = ((value >> 0) & 0xff);
72 mb();
75 static inline unsigned long read_wd33c93_count(const wd33c93_regs regs)
77 unsigned long value;
79 *regs.SASR = WD_TRANSFER_COUNT_MSB;
80 mb();
81 value = ((*regs.SCMD & 0xff) << 16);
82 value |= ((*regs.SCMD & 0xff) << 8);
83 value |= ((*regs.SCMD & 0xff) << 0);
84 mb();
85 return value;
88 static irqreturn_t sgiwd93_intr(int irq, void *dev_id)
90 struct Scsi_Host * host = (struct Scsi_Host *) dev_id;
91 unsigned long flags;
93 spin_lock_irqsave(host->host_lock, flags);
94 wd33c93_intr(host);
95 spin_unlock_irqrestore(host->host_lock, flags);
97 return IRQ_HANDLED;
100 static inline
101 void fill_hpc_entries(struct hpc_chunk *hcp, struct scsi_cmnd *cmd, int datainp)
103 unsigned long len = cmd->SCp.this_residual;
104 void *addr = cmd->SCp.ptr;
105 dma_addr_t physaddr;
106 unsigned long count;
108 physaddr = dma_map_single(NULL, addr, len, cmd->sc_data_direction);
109 cmd->SCp.dma_handle = physaddr;
111 while (len) {
113 * even cntinfo could be up to 16383, without
114 * magic only 8192 works correctly
116 count = len > 8192 ? 8192 : len;
117 hcp->desc.pbuf = physaddr;
118 hcp->desc.cntinfo = count;
119 hcp++;
120 len -= count;
121 physaddr += count;
125 * To make sure, if we trip an HPC bug, that we transfer every single
126 * byte, we tag on an extra zero length dma descriptor at the end of
127 * the chain.
129 hcp->desc.pbuf = 0;
130 hcp->desc.cntinfo = HPCDMA_EOX;
133 static int dma_setup(struct scsi_cmnd *cmd, int datainp)
135 struct ip22_hostdata *hdata = HDATA(cmd->device->host);
136 struct hpc3_scsiregs *hregs =
137 (struct hpc3_scsiregs *) cmd->device->host->base;
138 struct hpc_chunk *hcp = (struct hpc_chunk *) hdata->hd.cpu;
140 DPRINTK("dma_setup: datainp<%d> hcp<%p> ", datainp, hcp);
142 hdata->wh.dma_dir = datainp;
145 * wd33c93 shouldn't pass us bogus dma_setups, but it does:-( The
146 * other wd33c93 drivers deal with it the same way (which isn't that
147 * obvious). IMHO a better fix would be, not to do these dma setups
148 * in the first place.
150 if (cmd->SCp.ptr == NULL || cmd->SCp.this_residual == 0)
151 return 1;
153 fill_hpc_entries(hcp, cmd, datainp);
155 DPRINTK(" HPCGO\n");
157 /* Start up the HPC. */
158 hregs->ndptr = hdata->hd.dma;
159 if (datainp)
160 hregs->ctrl = HPC3_SCTRL_ACTIVE;
161 else
162 hregs->ctrl = HPC3_SCTRL_ACTIVE | HPC3_SCTRL_DIR;
164 return 0;
167 static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
168 int status)
170 struct ip22_hostdata *hdata = HDATA(instance);
171 struct hpc3_scsiregs *hregs;
173 if (!SCpnt)
174 return;
176 hregs = (struct hpc3_scsiregs *) SCpnt->device->host->base;
178 DPRINTK("dma_stop: status<%d> ", status);
180 /* First stop the HPC and flush it's FIFO. */
181 if (hdata->wh.dma_dir) {
182 hregs->ctrl |= HPC3_SCTRL_FLUSH;
183 while (hregs->ctrl & HPC3_SCTRL_ACTIVE)
184 barrier();
186 hregs->ctrl = 0;
187 dma_unmap_single(NULL, SCpnt->SCp.dma_handle, SCpnt->SCp.this_residual,
188 SCpnt->sc_data_direction);
190 DPRINTK("\n");
193 void sgiwd93_reset(unsigned long base)
195 struct hpc3_scsiregs *hregs = (struct hpc3_scsiregs *) base;
197 hregs->ctrl = HPC3_SCTRL_CRESET;
198 udelay(50);
199 hregs->ctrl = 0;
201 EXPORT_SYMBOL_GPL(sgiwd93_reset);
203 static inline void init_hpc_chain(struct hpc_data *hd)
205 struct hpc_chunk *hcp = (struct hpc_chunk *) hd->cpu;
206 struct hpc_chunk *dma = (struct hpc_chunk *) hd->dma;
207 unsigned long start, end;
209 start = (unsigned long) hcp;
210 end = start + PAGE_SIZE;
211 while (start < end) {
212 hcp->desc.pnext = (u32) (dma + 1);
213 hcp->desc.cntinfo = HPCDMA_EOX;
214 hcp++; dma++;
215 start += sizeof(struct hpc_chunk);
217 hcp--;
218 hcp->desc.pnext = hd->dma;
221 static struct Scsi_Host * __init sgiwd93_setup_scsi(
222 struct scsi_host_template *SGIblows, int unit, int irq,
223 struct hpc3_scsiregs *hregs, unsigned char *wdregs)
225 struct ip22_hostdata *hdata;
226 struct Scsi_Host *host;
227 wd33c93_regs regs;
229 host = scsi_register(SGIblows, sizeof(struct ip22_hostdata));
230 if (!host)
231 return NULL;
233 host->base = (unsigned long) hregs;
234 host->irq = irq;
236 hdata = HDATA(host);
237 hdata->hd.cpu = dma_alloc_coherent(NULL, PAGE_SIZE, &hdata->hd.dma,
238 GFP_KERNEL);
239 if (!hdata->hd.cpu) {
240 printk(KERN_WARNING "sgiwd93: Could not allocate memory for "
241 "host %d buffer.\n", unit);
242 goto out_unregister;
244 init_hpc_chain(&hdata->hd);
246 regs.SASR = wdregs + 3;
247 regs.SCMD = wdregs + 7;
249 wd33c93_init(host, regs, dma_setup, dma_stop, WD33C93_FS_MHZ(20));
251 if (hdata->wh.no_sync == 0xff)
252 hdata->wh.no_sync = 0;
254 if (request_irq(irq, sgiwd93_intr, 0, "SGI WD93", (void *) host)) {
255 printk(KERN_WARNING "sgiwd93: Could not register irq %d "
256 "for host %d.\n", irq, unit);
257 goto out_free;
259 return host;
261 out_free:
262 dma_free_coherent(NULL, PAGE_SIZE, hdata->hd.cpu, hdata->hd.dma);
263 wd33c93_release();
265 out_unregister:
266 scsi_unregister(host);
268 return NULL;
271 static int __init sgiwd93_detect(struct scsi_host_template *SGIblows)
273 int found = 0;
275 SGIblows->proc_name = "SGIWD93";
276 sgiwd93_host = sgiwd93_setup_scsi(SGIblows, 0, SGI_WD93_0_IRQ,
277 &hpc3c0->scsi_chan0,
278 (unsigned char *)hpc3c0->scsi0_ext);
279 if (sgiwd93_host)
280 found++;
282 /* Set up second controller on the Indigo2 */
283 if (ip22_is_fullhouse()) {
284 sgiwd93_host1 = sgiwd93_setup_scsi(SGIblows, 1, SGI_WD93_1_IRQ,
285 &hpc3c0->scsi_chan1,
286 (unsigned char *)hpc3c0->scsi1_ext);
287 if (sgiwd93_host1)
288 found++;
291 return found;
294 static int sgiwd93_release(struct Scsi_Host *instance)
296 struct ip22_hostdata *hdata = HDATA(instance);
297 int irq = 0;
299 if (sgiwd93_host && sgiwd93_host == instance)
300 irq = SGI_WD93_0_IRQ;
301 else if (sgiwd93_host1 && sgiwd93_host1 == instance)
302 irq = SGI_WD93_1_IRQ;
304 free_irq(irq, sgiwd93_intr);
305 dma_free_coherent(NULL, PAGE_SIZE, hdata->hd.cpu, hdata->hd.dma);
306 wd33c93_release();
308 return 1;
311 static int sgiwd93_bus_reset(struct scsi_cmnd *cmd)
313 /* FIXME perform bus-specific reset */
315 /* FIXME 2: kill this function, and let midlayer fallback
316 to the same result, calling wd33c93_host_reset() */
318 spin_lock_irq(cmd->device->host->host_lock);
319 wd33c93_host_reset(cmd);
320 spin_unlock_irq(cmd->device->host->host_lock);
322 return SUCCESS;
326 * Kludge alert - the SCSI code calls the abort and reset method with int
327 * arguments not with pointers. So this is going to blow up beautyfully
328 * on 64-bit systems with memory outside the compat address spaces.
330 static struct scsi_host_template driver_template = {
331 .proc_name = "SGIWD93",
332 .name = "SGI WD93",
333 .detect = sgiwd93_detect,
334 .release = sgiwd93_release,
335 .queuecommand = wd33c93_queuecommand,
336 .eh_abort_handler = wd33c93_abort,
337 .eh_bus_reset_handler = sgiwd93_bus_reset,
338 .eh_host_reset_handler = wd33c93_host_reset,
339 .can_queue = 16,
340 .this_id = 7,
341 .sg_tablesize = SG_ALL,
342 .cmd_per_lun = 8,
343 .use_clustering = DISABLE_CLUSTERING,
345 #include "scsi_module.c"