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/blk.h>
18 #include <linux/version.h>
19 #include <linux/delay.h>
20 #include <linux/pci.h>
21 #include <linux/spinlock.h>
22 
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>
32 
33 #include "scsi.h"
34 #include "hosts.h"
35 #include "wd33c93.h"
36 #include "sgiwd93.h"
37 
38 #include <linux/stat.h>
39 
40 #if 0
41 #define DPRINTK(args...)	printk(args)
42 #else
43 #define DPRINTK(args...)
44 #endif
45 
46 #define HDATA(ptr) ((struct ip22_hostdata *)((ptr)->hostdata))
47 
48 struct ip22_hostdata {
49 	struct WD33C93_hostdata wh;
50 	struct hpc_data {
51 		dma_addr_t      dma;
52 		void            * cpu;
53 	} hd;
54 };
55 
56 struct hpc_chunk {
57 	struct hpc_dma_desc desc;
58 	u32 _padding;	/* align to quadword boundary */
59 };
60 
61 struct Scsi_Host *sgiwd93_host;
62 struct Scsi_Host *sgiwd93_host1;
63 
64 /* Wuff wuff, wuff, wd33c93.c, wuff wuff, object oriented, bow wow. */
write_wd33c93_count(const wd33c93_regs regs,unsigned long value)65 static inline void write_wd33c93_count(const wd33c93_regs regs,
66                                       unsigned long value)
67 {
68 	*regs.SASR = WD_TRANSFER_COUNT_MSB;
69 	mb();
70 	*regs.SCMD = ((value >> 16) & 0xff);
71 	*regs.SCMD = ((value >>  8) & 0xff);
72 	*regs.SCMD = ((value >>  0) & 0xff);
73 	mb();
74 }
75 
read_wd33c93_count(const wd33c93_regs regs)76 static inline unsigned long read_wd33c93_count(const wd33c93_regs regs)
77 {
78 	unsigned long value;
79 
80 	*regs.SASR = WD_TRANSFER_COUNT_MSB;
81 	mb();
82 	value =  ((*regs.SCMD & 0xff) << 16);
83 	value |= ((*regs.SCMD & 0xff) <<  8);
84 	value |= ((*regs.SCMD & 0xff) <<  0);
85 	mb();
86 	return value;
87 }
88 
sgiwd93_intr(int irq,void * dev_id,struct pt_regs * regs)89 static void sgiwd93_intr(int irq, void *dev_id, struct pt_regs *regs)
90 {
91 	unsigned long flags;
92 
93 	spin_lock_irqsave(&io_request_lock, flags);
94 	wd33c93_intr((struct Scsi_Host *) dev_id);
95 	spin_unlock_irqrestore(&io_request_lock, flags);
96 }
97 
98 static inline
fill_hpc_entries(struct hpc_chunk * hcp,Scsi_Cmnd * cmd,int datainp)99 void fill_hpc_entries(struct hpc_chunk *hcp, Scsi_Cmnd *cmd, int datainp)
100 {
101 	unsigned long len = cmd->SCp.this_residual;
102 	void *addr = cmd->SCp.ptr;
103 	dma_addr_t physaddr;
104 	unsigned long count;
105 
106 	physaddr = pci_map_single(NULL, addr, len,
107 		scsi_to_pci_dma_dir(cmd->sc_data_direction));
108 	cmd->SCp.dma_handle = physaddr;
109 
110 	while (len) {
111 		/*
112 		 * even cntinfo could be up to 16383, without
113 		 * magic only 8192 works correctly
114 		 */
115 		count = len > 8192 ? 8192 : len;
116 		hcp->desc.pbuf = physaddr;
117 		hcp->desc.cntinfo = count;
118 		hcp++;
119 		len -= count;
120 		physaddr += count;
121 	}
122 
123 	/*
124 	 * To make sure, if we trip an HPC bug, that we transfer every single
125 	 * byte, we tag on an extra zero length dma descriptor at the end of
126 	 * the chain.
127 	 */
128 	hcp->desc.pbuf = 0;
129 	hcp->desc.cntinfo = HPCDMA_EOX;
130 }
131 
dma_setup(Scsi_Cmnd * cmd,int datainp)132 static int dma_setup(Scsi_Cmnd *cmd, int datainp)
133 {
134 	struct ip22_hostdata *hdata = HDATA(cmd->host);
135 	struct hpc3_scsiregs *hregs =
136 		(struct hpc3_scsiregs *) cmd->host->base;
137 	struct hpc_chunk *hcp = (struct hpc_chunk *) hdata->hd.cpu;
138 
139 	DPRINTK("dma_setup: datainp<%d> hcp<%p> ", datainp, hcp);
140 
141 	hdata->wh.dma_dir = datainp;
142 
143 	/*
144 	 * wd33c93 shouldn't pass us bogus dma_setups, but it does:-(  The
145 	 * other wd33c93 drivers deal with it the same way (which isn't that
146 	 * obvious).  IMHO a better fix would be, not to do these dma setups
147 	 * in the first place.
148 	 */
149 	if (cmd->SCp.ptr == NULL || cmd->SCp.this_residual == 0)
150 		return 1;
151 
152 	fill_hpc_entries(hcp, cmd, datainp);
153 
154 	DPRINTK(" HPCGO\n");
155 
156 	/* Start up the HPC. */
157 	hregs->ndptr = hdata->hd.dma;
158 	if (datainp)
159 		hregs->ctrl = HPC3_SCTRL_ACTIVE;
160 	else
161 		hregs->ctrl = HPC3_SCTRL_ACTIVE | HPC3_SCTRL_DIR;
162 
163 	return 0;
164 }
165 
dma_stop(struct Scsi_Host * instance,Scsi_Cmnd * SCpnt,int status)166 static void dma_stop(struct Scsi_Host *instance, Scsi_Cmnd *SCpnt,
167 		     int status)
168 {
169 	struct ip22_hostdata *hdata = HDATA(instance);
170 	struct hpc3_scsiregs *hregs;
171 
172 	if (!SCpnt)
173 		return;
174 
175 	hregs = (struct hpc3_scsiregs *) SCpnt->host->base;
176 
177 	DPRINTK("dma_stop: status<%d> ", status);
178 
179 	/* First stop the HPC and flush it's FIFO. */
180 	if (hdata->wh.dma_dir) {
181 		hregs->ctrl |= HPC3_SCTRL_FLUSH;
182 		while (hregs->ctrl & HPC3_SCTRL_ACTIVE)
183 			barrier();
184 	}
185 	hregs->ctrl = 0;
186 	pci_unmap_single(NULL, SCpnt->SCp.dma_handle, SCpnt->SCp.this_residual,
187 	                 scsi_to_pci_dma_dir(SCpnt->sc_data_direction));
188 
189 	DPRINTK("\n");
190 }
191 
sgiwd93_reset(unsigned long base)192 void sgiwd93_reset(unsigned long base)
193 {
194 	struct hpc3_scsiregs *hregs = (struct hpc3_scsiregs *) base;
195 
196 	hregs->ctrl = HPC3_SCTRL_CRESET;
197 	udelay(50);
198 	hregs->ctrl = 0;
199 }
200 
init_hpc_chain(struct hpc_data * hd)201 static inline void init_hpc_chain(struct hpc_data *hd)
202 {
203 	struct hpc_chunk *hcp = (struct hpc_chunk *) hd->cpu;
204 	struct hpc_chunk *dma = (struct hpc_chunk *) hd->dma;
205 	unsigned long start, end;
206 
207 	start = (unsigned long) hcp;
208 	end = start + PAGE_SIZE;
209 	while (start < end) {
210 		hcp->desc.pnext = (u32) (dma + 1);
211 		hcp->desc.cntinfo = HPCDMA_EOX;
212 		hcp++; dma++;
213 		start += sizeof(struct hpc_chunk);
214 	};
215 	hcp--;
216 	hcp->desc.pnext = hd->dma;
217 }
218 
sgiwd93_setup_scsi(Scsi_Host_Template * SGIblows,int unit,int irq,struct hpc3_scsiregs * hregs,unsigned char * wdregs)219 static struct Scsi_Host * __init sgiwd93_setup_scsi(
220 	Scsi_Host_Template *SGIblows, int unit, int irq,
221 	struct hpc3_scsiregs *hregs, unsigned char *wdregs)
222 {
223 	struct ip22_hostdata *hdata;
224 	struct Scsi_Host *host;
225 	wd33c93_regs regs;
226 
227 	host = scsi_register(SGIblows, sizeof(struct ip22_hostdata));
228 	if (!host)
229 		return NULL;
230 
231 	host->base = (unsigned long) hregs;
232 	host->irq = irq;
233 
234 	hdata = HDATA(host);
235 	hdata->hd.cpu = pci_alloc_consistent(NULL, PAGE_SIZE, &hdata->hd.dma);
236 	if (!hdata->hd.cpu) {
237 		printk(KERN_WARNING "sgiwd93: Could not allocate memory for "
238 		       "host %d buffer.\n", unit);
239 		goto out_unregister;
240 	}
241 	init_hpc_chain(&hdata->hd);
242 
243 	regs.SASR = wdregs + 3;
244 	regs.SCMD = wdregs + 7;
245 
246 	wd33c93_init(host, regs, dma_setup, dma_stop, WD33C93_FS_16_20);
247 
248 	hdata->wh.no_sync = 0;
249 
250 	if (request_irq(irq, sgiwd93_intr, 0, "SGI WD93", (void *) host)) {
251 		printk(KERN_WARNING "sgiwd93: Could not register irq %d "
252 		       "for host %d.\n", irq, unit);
253 		goto out_free;
254 	}
255 	return host;
256 
257 out_free:
258 	pci_free_consistent(NULL, PAGE_SIZE, hdata->hd.cpu, hdata->hd.dma);
259 	wd33c93_release();
260 
261 out_unregister:
262 	scsi_unregister(host);
263 
264 	return NULL;
265 }
266 
sgiwd93_detect(Scsi_Host_Template * SGIblows)267 int __init sgiwd93_detect(Scsi_Host_Template *SGIblows)
268 {
269 	int found = 0;
270 
271 	SGIblows->proc_name = "SGIWD93";
272 	sgiwd93_host = sgiwd93_setup_scsi(SGIblows, 0, SGI_WD93_0_IRQ,
273 	                                  &hpc3c0->scsi_chan0,
274 	                                  (unsigned char *)hpc3c0->scsi0_ext);
275 	if (sgiwd93_host)
276 		found++;
277 
278 	/* Set up second controller on the Indigo2 */
279 	if (ip22_is_fullhouse()) {
280 		sgiwd93_host1 = sgiwd93_setup_scsi(SGIblows, 1, SGI_WD93_1_IRQ,
281 		                          &hpc3c0->scsi_chan1,
282 		                          (unsigned char *)hpc3c0->scsi1_ext);
283 		if (sgiwd93_host1)
284 			found++;
285 	}
286 
287 	return found;
288 }
289 
290 #define HOSTS_C
291 
292 #include "sgiwd93.h"
293 
294 static Scsi_Host_Template driver_template = SGIWD93_SCSI;
295 
296 #include "scsi_module.c"
297 
sgiwd93_release(struct Scsi_Host * instance)298 int sgiwd93_release(struct Scsi_Host *instance)
299 {
300 #ifdef MODULE
301 	free_irq(SGI_WD93_0_IRQ, sgiwd93_intr);
302 	pci_free_consistent(NULL, PAGE_SIZE, hdata->hd.cpu, hdata->hd.dma);
303 	wd33c93_release();
304 	if (ip22_is_fullhouse()) {
305 		free_irq(SGI_WD93_1_IRQ, sgiwd93_intr);
306 		pci_free_consistent(NULL, PAGE_SIZE, hdata1->hd.cpu,
307 		                    hdata1->hd.dma);
308 		wd33c93_release();
309 	}
310 #endif
311 	return 1;
312 }
313