xref: /OK3568_Linux_fs/kernel/drivers/scsi/mesh.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * SCSI low-level driver for the MESH (Macintosh Enhanced SCSI Hardware)
4*4882a593Smuzhiyun  * bus adaptor found on Power Macintosh computers.
5*4882a593Smuzhiyun  * We assume the MESH is connected to a DBDMA (descriptor-based DMA)
6*4882a593Smuzhiyun  * controller.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Paul Mackerras, August 1996.
9*4882a593Smuzhiyun  * Copyright (C) 1996 Paul Mackerras.
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * Apr. 21 2002  - BenH		Rework bus reset code for new error handler
12*4882a593Smuzhiyun  *                              Add delay after initial bus reset
13*4882a593Smuzhiyun  *                              Add module parameters
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * Sep. 27 2003  - BenH		Move to new driver model, fix some write posting
16*4882a593Smuzhiyun  *				issues
17*4882a593Smuzhiyun  * To do:
18*4882a593Smuzhiyun  * - handle aborts correctly
19*4882a593Smuzhiyun  * - retry arbitration if lost (unless higher levels do this for us)
20*4882a593Smuzhiyun  * - power down the chip when no device is detected
21*4882a593Smuzhiyun  */
22*4882a593Smuzhiyun #include <linux/module.h>
23*4882a593Smuzhiyun #include <linux/kernel.h>
24*4882a593Smuzhiyun #include <linux/delay.h>
25*4882a593Smuzhiyun #include <linux/types.h>
26*4882a593Smuzhiyun #include <linux/string.h>
27*4882a593Smuzhiyun #include <linux/blkdev.h>
28*4882a593Smuzhiyun #include <linux/proc_fs.h>
29*4882a593Smuzhiyun #include <linux/stat.h>
30*4882a593Smuzhiyun #include <linux/interrupt.h>
31*4882a593Smuzhiyun #include <linux/reboot.h>
32*4882a593Smuzhiyun #include <linux/spinlock.h>
33*4882a593Smuzhiyun #include <linux/pci.h>
34*4882a593Smuzhiyun #include <linux/pgtable.h>
35*4882a593Smuzhiyun #include <asm/dbdma.h>
36*4882a593Smuzhiyun #include <asm/io.h>
37*4882a593Smuzhiyun #include <asm/prom.h>
38*4882a593Smuzhiyun #include <asm/irq.h>
39*4882a593Smuzhiyun #include <asm/hydra.h>
40*4882a593Smuzhiyun #include <asm/processor.h>
41*4882a593Smuzhiyun #include <asm/machdep.h>
42*4882a593Smuzhiyun #include <asm/pmac_feature.h>
43*4882a593Smuzhiyun #include <asm/macio.h>
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun #include <scsi/scsi.h>
46*4882a593Smuzhiyun #include <scsi/scsi_cmnd.h>
47*4882a593Smuzhiyun #include <scsi/scsi_device.h>
48*4882a593Smuzhiyun #include <scsi/scsi_host.h>
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun #include "mesh.h"
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun #if 1
53*4882a593Smuzhiyun #undef KERN_DEBUG
54*4882a593Smuzhiyun #define KERN_DEBUG KERN_WARNING
55*4882a593Smuzhiyun #endif
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun MODULE_AUTHOR("Paul Mackerras (paulus@samba.org)");
58*4882a593Smuzhiyun MODULE_DESCRIPTION("PowerMac MESH SCSI driver");
59*4882a593Smuzhiyun MODULE_LICENSE("GPL");
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun static int sync_rate = CONFIG_SCSI_MESH_SYNC_RATE;
62*4882a593Smuzhiyun static int sync_targets = 0xff;
63*4882a593Smuzhiyun static int resel_targets = 0xff;
64*4882a593Smuzhiyun static int debug_targets = 0;	/* print debug for these targets */
65*4882a593Smuzhiyun static int init_reset_delay = CONFIG_SCSI_MESH_RESET_DELAY_MS;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun module_param(sync_rate, int, 0);
68*4882a593Smuzhiyun MODULE_PARM_DESC(sync_rate, "Synchronous rate (0..10, 0=async)");
69*4882a593Smuzhiyun module_param(sync_targets, int, 0);
70*4882a593Smuzhiyun MODULE_PARM_DESC(sync_targets, "Bitmask of targets allowed to set synchronous");
71*4882a593Smuzhiyun module_param(resel_targets, int, 0);
72*4882a593Smuzhiyun MODULE_PARM_DESC(resel_targets, "Bitmask of targets allowed to set disconnect");
73*4882a593Smuzhiyun module_param(debug_targets, int, 0644);
74*4882a593Smuzhiyun MODULE_PARM_DESC(debug_targets, "Bitmask of debugged targets");
75*4882a593Smuzhiyun module_param(init_reset_delay, int, 0);
76*4882a593Smuzhiyun MODULE_PARM_DESC(init_reset_delay, "Initial bus reset delay (0=no reset)");
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun static int mesh_sync_period = 100;
79*4882a593Smuzhiyun static int mesh_sync_offset = 0;
80*4882a593Smuzhiyun static unsigned char use_active_neg = 0;  /* bit mask for SEQ_ACTIVE_NEG if used */
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun #define ALLOW_SYNC(tgt)		((sync_targets >> (tgt)) & 1)
83*4882a593Smuzhiyun #define ALLOW_RESEL(tgt)	((resel_targets >> (tgt)) & 1)
84*4882a593Smuzhiyun #define ALLOW_DEBUG(tgt)	((debug_targets >> (tgt)) & 1)
85*4882a593Smuzhiyun #define DEBUG_TARGET(cmd)	((cmd) && ALLOW_DEBUG((cmd)->device->id))
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun #undef MESH_DBG
88*4882a593Smuzhiyun #define N_DBG_LOG	50
89*4882a593Smuzhiyun #define N_DBG_SLOG	20
90*4882a593Smuzhiyun #define NUM_DBG_EVENTS	13
91*4882a593Smuzhiyun #undef	DBG_USE_TB		/* bombs on 601 */
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun struct dbglog {
94*4882a593Smuzhiyun 	char	*fmt;
95*4882a593Smuzhiyun 	u32	tb;
96*4882a593Smuzhiyun 	u8	phase;
97*4882a593Smuzhiyun 	u8	bs0;
98*4882a593Smuzhiyun 	u8	bs1;
99*4882a593Smuzhiyun 	u8	tgt;
100*4882a593Smuzhiyun 	int	d;
101*4882a593Smuzhiyun };
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun enum mesh_phase {
104*4882a593Smuzhiyun 	idle,
105*4882a593Smuzhiyun 	arbitrating,
106*4882a593Smuzhiyun 	selecting,
107*4882a593Smuzhiyun 	commanding,
108*4882a593Smuzhiyun 	dataing,
109*4882a593Smuzhiyun 	statusing,
110*4882a593Smuzhiyun 	busfreeing,
111*4882a593Smuzhiyun 	disconnecting,
112*4882a593Smuzhiyun 	reselecting,
113*4882a593Smuzhiyun 	sleeping
114*4882a593Smuzhiyun };
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun enum msg_phase {
117*4882a593Smuzhiyun 	msg_none,
118*4882a593Smuzhiyun 	msg_out,
119*4882a593Smuzhiyun 	msg_out_xxx,
120*4882a593Smuzhiyun 	msg_out_last,
121*4882a593Smuzhiyun 	msg_in,
122*4882a593Smuzhiyun 	msg_in_bad,
123*4882a593Smuzhiyun };
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun enum sdtr_phase {
126*4882a593Smuzhiyun 	do_sdtr,
127*4882a593Smuzhiyun 	sdtr_sent,
128*4882a593Smuzhiyun 	sdtr_done
129*4882a593Smuzhiyun };
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun struct mesh_target {
132*4882a593Smuzhiyun 	enum sdtr_phase sdtr_state;
133*4882a593Smuzhiyun 	int	sync_params;
134*4882a593Smuzhiyun 	int	data_goes_out;		/* guess as to data direction */
135*4882a593Smuzhiyun 	struct scsi_cmnd *current_req;
136*4882a593Smuzhiyun 	u32	saved_ptr;
137*4882a593Smuzhiyun #ifdef MESH_DBG
138*4882a593Smuzhiyun 	int	log_ix;
139*4882a593Smuzhiyun 	int	n_log;
140*4882a593Smuzhiyun 	struct dbglog log[N_DBG_LOG];
141*4882a593Smuzhiyun #endif
142*4882a593Smuzhiyun };
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun struct mesh_state {
145*4882a593Smuzhiyun 	volatile struct	mesh_regs __iomem *mesh;
146*4882a593Smuzhiyun 	int	meshintr;
147*4882a593Smuzhiyun 	volatile struct	dbdma_regs __iomem *dma;
148*4882a593Smuzhiyun 	int	dmaintr;
149*4882a593Smuzhiyun 	struct	Scsi_Host *host;
150*4882a593Smuzhiyun 	struct	mesh_state *next;
151*4882a593Smuzhiyun 	struct scsi_cmnd *request_q;
152*4882a593Smuzhiyun 	struct scsi_cmnd *request_qtail;
153*4882a593Smuzhiyun 	enum mesh_phase phase;		/* what we're currently trying to do */
154*4882a593Smuzhiyun 	enum msg_phase msgphase;
155*4882a593Smuzhiyun 	int	conn_tgt;		/* target we're connected to */
156*4882a593Smuzhiyun 	struct scsi_cmnd *current_req;		/* req we're currently working on */
157*4882a593Smuzhiyun 	int	data_ptr;
158*4882a593Smuzhiyun 	int	dma_started;
159*4882a593Smuzhiyun 	int	dma_count;
160*4882a593Smuzhiyun 	int	stat;
161*4882a593Smuzhiyun 	int	aborting;
162*4882a593Smuzhiyun 	int	expect_reply;
163*4882a593Smuzhiyun 	int	n_msgin;
164*4882a593Smuzhiyun 	u8	msgin[16];
165*4882a593Smuzhiyun 	int	n_msgout;
166*4882a593Smuzhiyun 	int	last_n_msgout;
167*4882a593Smuzhiyun 	u8	msgout[16];
168*4882a593Smuzhiyun 	struct dbdma_cmd *dma_cmds;	/* space for dbdma commands, aligned */
169*4882a593Smuzhiyun 	dma_addr_t dma_cmd_bus;
170*4882a593Smuzhiyun 	void	*dma_cmd_space;
171*4882a593Smuzhiyun 	int	dma_cmd_size;
172*4882a593Smuzhiyun 	int	clk_freq;
173*4882a593Smuzhiyun 	struct mesh_target tgts[8];
174*4882a593Smuzhiyun 	struct macio_dev *mdev;
175*4882a593Smuzhiyun 	struct pci_dev* pdev;
176*4882a593Smuzhiyun #ifdef MESH_DBG
177*4882a593Smuzhiyun 	int	log_ix;
178*4882a593Smuzhiyun 	int	n_log;
179*4882a593Smuzhiyun 	struct dbglog log[N_DBG_SLOG];
180*4882a593Smuzhiyun #endif
181*4882a593Smuzhiyun };
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun /*
184*4882a593Smuzhiyun  * Driver is too messy, we need a few prototypes...
185*4882a593Smuzhiyun  */
186*4882a593Smuzhiyun static void mesh_done(struct mesh_state *ms, int start_next);
187*4882a593Smuzhiyun static void mesh_interrupt(struct mesh_state *ms);
188*4882a593Smuzhiyun static void cmd_complete(struct mesh_state *ms);
189*4882a593Smuzhiyun static void set_dma_cmds(struct mesh_state *ms, struct scsi_cmnd *cmd);
190*4882a593Smuzhiyun static void halt_dma(struct mesh_state *ms);
191*4882a593Smuzhiyun static void phase_mismatch(struct mesh_state *ms);
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun /*
195*4882a593Smuzhiyun  * Some debugging & logging routines
196*4882a593Smuzhiyun  */
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun #ifdef MESH_DBG
199*4882a593Smuzhiyun 
readtb(void)200*4882a593Smuzhiyun static inline u32 readtb(void)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun 	u32 tb;
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun #ifdef DBG_USE_TB
205*4882a593Smuzhiyun 	/* Beware: if you enable this, it will crash on 601s. */
206*4882a593Smuzhiyun 	asm ("mftb %0" : "=r" (tb) : );
207*4882a593Smuzhiyun #else
208*4882a593Smuzhiyun 	tb = 0;
209*4882a593Smuzhiyun #endif
210*4882a593Smuzhiyun 	return tb;
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun 
dlog(struct mesh_state * ms,char * fmt,int a)213*4882a593Smuzhiyun static void dlog(struct mesh_state *ms, char *fmt, int a)
214*4882a593Smuzhiyun {
215*4882a593Smuzhiyun 	struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
216*4882a593Smuzhiyun 	struct dbglog *tlp, *slp;
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	tlp = &tp->log[tp->log_ix];
219*4882a593Smuzhiyun 	slp = &ms->log[ms->log_ix];
220*4882a593Smuzhiyun 	tlp->fmt = fmt;
221*4882a593Smuzhiyun 	tlp->tb = readtb();
222*4882a593Smuzhiyun 	tlp->phase = (ms->msgphase << 4) + ms->phase;
223*4882a593Smuzhiyun 	tlp->bs0 = ms->mesh->bus_status0;
224*4882a593Smuzhiyun 	tlp->bs1 = ms->mesh->bus_status1;
225*4882a593Smuzhiyun 	tlp->tgt = ms->conn_tgt;
226*4882a593Smuzhiyun 	tlp->d = a;
227*4882a593Smuzhiyun 	*slp = *tlp;
228*4882a593Smuzhiyun 	if (++tp->log_ix >= N_DBG_LOG)
229*4882a593Smuzhiyun 		tp->log_ix = 0;
230*4882a593Smuzhiyun 	if (tp->n_log < N_DBG_LOG)
231*4882a593Smuzhiyun 		++tp->n_log;
232*4882a593Smuzhiyun 	if (++ms->log_ix >= N_DBG_SLOG)
233*4882a593Smuzhiyun 		ms->log_ix = 0;
234*4882a593Smuzhiyun 	if (ms->n_log < N_DBG_SLOG)
235*4882a593Smuzhiyun 		++ms->n_log;
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun 
dumplog(struct mesh_state * ms,int t)238*4882a593Smuzhiyun static void dumplog(struct mesh_state *ms, int t)
239*4882a593Smuzhiyun {
240*4882a593Smuzhiyun 	struct mesh_target *tp = &ms->tgts[t];
241*4882a593Smuzhiyun 	struct dbglog *lp;
242*4882a593Smuzhiyun 	int i;
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	if (tp->n_log == 0)
245*4882a593Smuzhiyun 		return;
246*4882a593Smuzhiyun 	i = tp->log_ix - tp->n_log;
247*4882a593Smuzhiyun 	if (i < 0)
248*4882a593Smuzhiyun 		i += N_DBG_LOG;
249*4882a593Smuzhiyun 	tp->n_log = 0;
250*4882a593Smuzhiyun 	do {
251*4882a593Smuzhiyun 		lp = &tp->log[i];
252*4882a593Smuzhiyun 		printk(KERN_DEBUG "mesh log %d: bs=%.2x%.2x ph=%.2x ",
253*4882a593Smuzhiyun 		       t, lp->bs1, lp->bs0, lp->phase);
254*4882a593Smuzhiyun #ifdef DBG_USE_TB
255*4882a593Smuzhiyun 		printk("tb=%10u ", lp->tb);
256*4882a593Smuzhiyun #endif
257*4882a593Smuzhiyun 		printk(lp->fmt, lp->d);
258*4882a593Smuzhiyun 		printk("\n");
259*4882a593Smuzhiyun 		if (++i >= N_DBG_LOG)
260*4882a593Smuzhiyun 			i = 0;
261*4882a593Smuzhiyun 	} while (i != tp->log_ix);
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun 
dumpslog(struct mesh_state * ms)264*4882a593Smuzhiyun static void dumpslog(struct mesh_state *ms)
265*4882a593Smuzhiyun {
266*4882a593Smuzhiyun 	struct dbglog *lp;
267*4882a593Smuzhiyun 	int i;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	if (ms->n_log == 0)
270*4882a593Smuzhiyun 		return;
271*4882a593Smuzhiyun 	i = ms->log_ix - ms->n_log;
272*4882a593Smuzhiyun 	if (i < 0)
273*4882a593Smuzhiyun 		i += N_DBG_SLOG;
274*4882a593Smuzhiyun 	ms->n_log = 0;
275*4882a593Smuzhiyun 	do {
276*4882a593Smuzhiyun 		lp = &ms->log[i];
277*4882a593Smuzhiyun 		printk(KERN_DEBUG "mesh log: bs=%.2x%.2x ph=%.2x t%d ",
278*4882a593Smuzhiyun 		       lp->bs1, lp->bs0, lp->phase, lp->tgt);
279*4882a593Smuzhiyun #ifdef DBG_USE_TB
280*4882a593Smuzhiyun 		printk("tb=%10u ", lp->tb);
281*4882a593Smuzhiyun #endif
282*4882a593Smuzhiyun 		printk(lp->fmt, lp->d);
283*4882a593Smuzhiyun 		printk("\n");
284*4882a593Smuzhiyun 		if (++i >= N_DBG_SLOG)
285*4882a593Smuzhiyun 			i = 0;
286*4882a593Smuzhiyun 	} while (i != ms->log_ix);
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun #else
290*4882a593Smuzhiyun 
dlog(struct mesh_state * ms,char * fmt,int a)291*4882a593Smuzhiyun static inline void dlog(struct mesh_state *ms, char *fmt, int a)
292*4882a593Smuzhiyun {}
dumplog(struct mesh_state * ms,int tgt)293*4882a593Smuzhiyun static inline void dumplog(struct mesh_state *ms, int tgt)
294*4882a593Smuzhiyun {}
dumpslog(struct mesh_state * ms)295*4882a593Smuzhiyun static inline void dumpslog(struct mesh_state *ms)
296*4882a593Smuzhiyun {}
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun #endif /* MESH_DBG */
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun #define MKWORD(a, b, c, d)	(((a) << 24) + ((b) << 16) + ((c) << 8) + (d))
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun static void
mesh_dump_regs(struct mesh_state * ms)303*4882a593Smuzhiyun mesh_dump_regs(struct mesh_state *ms)
304*4882a593Smuzhiyun {
305*4882a593Smuzhiyun 	volatile struct mesh_regs __iomem *mr = ms->mesh;
306*4882a593Smuzhiyun 	volatile struct dbdma_regs __iomem *md = ms->dma;
307*4882a593Smuzhiyun 	int t;
308*4882a593Smuzhiyun 	struct mesh_target *tp;
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	printk(KERN_DEBUG "mesh: state at %p, regs at %p, dma at %p\n",
311*4882a593Smuzhiyun 	       ms, mr, md);
312*4882a593Smuzhiyun 	printk(KERN_DEBUG "    ct=%4x seq=%2x bs=%4x fc=%2x "
313*4882a593Smuzhiyun 	       "exc=%2x err=%2x im=%2x int=%2x sp=%2x\n",
314*4882a593Smuzhiyun 	       (mr->count_hi << 8) + mr->count_lo, mr->sequence,
315*4882a593Smuzhiyun 	       (mr->bus_status1 << 8) + mr->bus_status0, mr->fifo_count,
316*4882a593Smuzhiyun 	       mr->exception, mr->error, mr->intr_mask, mr->interrupt,
317*4882a593Smuzhiyun 	       mr->sync_params);
318*4882a593Smuzhiyun 	while(in_8(&mr->fifo_count))
319*4882a593Smuzhiyun 		printk(KERN_DEBUG " fifo data=%.2x\n",in_8(&mr->fifo));
320*4882a593Smuzhiyun 	printk(KERN_DEBUG "    dma stat=%x cmdptr=%x\n",
321*4882a593Smuzhiyun 	       in_le32(&md->status), in_le32(&md->cmdptr));
322*4882a593Smuzhiyun 	printk(KERN_DEBUG "    phase=%d msgphase=%d conn_tgt=%d data_ptr=%d\n",
323*4882a593Smuzhiyun 	       ms->phase, ms->msgphase, ms->conn_tgt, ms->data_ptr);
324*4882a593Smuzhiyun 	printk(KERN_DEBUG "    dma_st=%d dma_ct=%d n_msgout=%d\n",
325*4882a593Smuzhiyun 	       ms->dma_started, ms->dma_count, ms->n_msgout);
326*4882a593Smuzhiyun 	for (t = 0; t < 8; ++t) {
327*4882a593Smuzhiyun 		tp = &ms->tgts[t];
328*4882a593Smuzhiyun 		if (tp->current_req == NULL)
329*4882a593Smuzhiyun 			continue;
330*4882a593Smuzhiyun 		printk(KERN_DEBUG "    target %d: req=%p goes_out=%d saved_ptr=%d\n",
331*4882a593Smuzhiyun 		       t, tp->current_req, tp->data_goes_out, tp->saved_ptr);
332*4882a593Smuzhiyun 	}
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun /*
337*4882a593Smuzhiyun  * Flush write buffers on the bus path to the mesh
338*4882a593Smuzhiyun  */
mesh_flush_io(volatile struct mesh_regs __iomem * mr)339*4882a593Smuzhiyun static inline void mesh_flush_io(volatile struct mesh_regs __iomem *mr)
340*4882a593Smuzhiyun {
341*4882a593Smuzhiyun 	(void)in_8(&mr->mesh_id);
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun /*
346*4882a593Smuzhiyun  * Complete a SCSI command
347*4882a593Smuzhiyun  */
mesh_completed(struct mesh_state * ms,struct scsi_cmnd * cmd)348*4882a593Smuzhiyun static void mesh_completed(struct mesh_state *ms, struct scsi_cmnd *cmd)
349*4882a593Smuzhiyun {
350*4882a593Smuzhiyun 	(*cmd->scsi_done)(cmd);
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun /* Called with  meshinterrupt disabled, initialize the chipset
355*4882a593Smuzhiyun  * and eventually do the initial bus reset. The lock must not be
356*4882a593Smuzhiyun  * held since we can schedule.
357*4882a593Smuzhiyun  */
mesh_init(struct mesh_state * ms)358*4882a593Smuzhiyun static void mesh_init(struct mesh_state *ms)
359*4882a593Smuzhiyun {
360*4882a593Smuzhiyun 	volatile struct mesh_regs __iomem *mr = ms->mesh;
361*4882a593Smuzhiyun 	volatile struct dbdma_regs __iomem *md = ms->dma;
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	mesh_flush_io(mr);
364*4882a593Smuzhiyun 	udelay(100);
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	/* Reset controller */
367*4882a593Smuzhiyun 	out_le32(&md->control, (RUN|PAUSE|FLUSH|WAKE) << 16);	/* stop dma */
368*4882a593Smuzhiyun 	out_8(&mr->exception, 0xff);	/* clear all exception bits */
369*4882a593Smuzhiyun 	out_8(&mr->error, 0xff);	/* clear all error bits */
370*4882a593Smuzhiyun 	out_8(&mr->sequence, SEQ_RESETMESH);
371*4882a593Smuzhiyun 	mesh_flush_io(mr);
372*4882a593Smuzhiyun 	udelay(10);
373*4882a593Smuzhiyun 	out_8(&mr->intr_mask, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
374*4882a593Smuzhiyun 	out_8(&mr->source_id, ms->host->this_id);
375*4882a593Smuzhiyun 	out_8(&mr->sel_timeout, 25);	/* 250ms */
376*4882a593Smuzhiyun 	out_8(&mr->sync_params, ASYNC_PARAMS);
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 	if (init_reset_delay) {
379*4882a593Smuzhiyun 		printk(KERN_INFO "mesh: performing initial bus reset...\n");
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 		/* Reset bus */
382*4882a593Smuzhiyun 		out_8(&mr->bus_status1, BS1_RST);	/* assert RST */
383*4882a593Smuzhiyun 		mesh_flush_io(mr);
384*4882a593Smuzhiyun 		udelay(30);			/* leave it on for >= 25us */
385*4882a593Smuzhiyun 		out_8(&mr->bus_status1, 0);	/* negate RST */
386*4882a593Smuzhiyun 		mesh_flush_io(mr);
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 		/* Wait for bus to come back */
389*4882a593Smuzhiyun 		msleep(init_reset_delay);
390*4882a593Smuzhiyun 	}
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	/* Reconfigure controller */
393*4882a593Smuzhiyun 	out_8(&mr->interrupt, 0xff);	/* clear all interrupt bits */
394*4882a593Smuzhiyun 	out_8(&mr->sequence, SEQ_FLUSHFIFO);
395*4882a593Smuzhiyun 	mesh_flush_io(mr);
396*4882a593Smuzhiyun 	udelay(1);
397*4882a593Smuzhiyun 	out_8(&mr->sync_params, ASYNC_PARAMS);
398*4882a593Smuzhiyun 	out_8(&mr->sequence, SEQ_ENBRESEL);
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	ms->phase = idle;
401*4882a593Smuzhiyun 	ms->msgphase = msg_none;
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 
mesh_start_cmd(struct mesh_state * ms,struct scsi_cmnd * cmd)405*4882a593Smuzhiyun static void mesh_start_cmd(struct mesh_state *ms, struct scsi_cmnd *cmd)
406*4882a593Smuzhiyun {
407*4882a593Smuzhiyun 	volatile struct mesh_regs __iomem *mr = ms->mesh;
408*4882a593Smuzhiyun 	int t, id;
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	id = cmd->device->id;
411*4882a593Smuzhiyun 	ms->current_req = cmd;
412*4882a593Smuzhiyun 	ms->tgts[id].data_goes_out = cmd->sc_data_direction == DMA_TO_DEVICE;
413*4882a593Smuzhiyun 	ms->tgts[id].current_req = cmd;
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun #if 1
416*4882a593Smuzhiyun 	if (DEBUG_TARGET(cmd)) {
417*4882a593Smuzhiyun 		int i;
418*4882a593Smuzhiyun 		printk(KERN_DEBUG "mesh_start: %p tgt=%d cmd=", cmd, id);
419*4882a593Smuzhiyun 		for (i = 0; i < cmd->cmd_len; ++i)
420*4882a593Smuzhiyun 			printk(" %x", cmd->cmnd[i]);
421*4882a593Smuzhiyun 		printk(" use_sg=%d buffer=%p bufflen=%u\n",
422*4882a593Smuzhiyun 		       scsi_sg_count(cmd), scsi_sglist(cmd), scsi_bufflen(cmd));
423*4882a593Smuzhiyun 	}
424*4882a593Smuzhiyun #endif
425*4882a593Smuzhiyun 	if (ms->dma_started)
426*4882a593Smuzhiyun 		panic("mesh: double DMA start !\n");
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	ms->phase = arbitrating;
429*4882a593Smuzhiyun 	ms->msgphase = msg_none;
430*4882a593Smuzhiyun 	ms->data_ptr = 0;
431*4882a593Smuzhiyun 	ms->dma_started = 0;
432*4882a593Smuzhiyun 	ms->n_msgout = 0;
433*4882a593Smuzhiyun 	ms->last_n_msgout = 0;
434*4882a593Smuzhiyun 	ms->expect_reply = 0;
435*4882a593Smuzhiyun 	ms->conn_tgt = id;
436*4882a593Smuzhiyun 	ms->tgts[id].saved_ptr = 0;
437*4882a593Smuzhiyun 	ms->stat = DID_OK;
438*4882a593Smuzhiyun 	ms->aborting = 0;
439*4882a593Smuzhiyun #ifdef MESH_DBG
440*4882a593Smuzhiyun 	ms->tgts[id].n_log = 0;
441*4882a593Smuzhiyun 	dlog(ms, "start cmd=%x", (int) cmd);
442*4882a593Smuzhiyun #endif
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 	/* Off we go */
445*4882a593Smuzhiyun 	dlog(ms, "about to arb, intr/exc/err/fc=%.8x",
446*4882a593Smuzhiyun 	     MKWORD(mr->interrupt, mr->exception, mr->error, mr->fifo_count));
447*4882a593Smuzhiyun 	out_8(&mr->interrupt, INT_CMDDONE);
448*4882a593Smuzhiyun 	out_8(&mr->sequence, SEQ_ENBRESEL);
449*4882a593Smuzhiyun 	mesh_flush_io(mr);
450*4882a593Smuzhiyun 	udelay(1);
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	if (in_8(&mr->bus_status1) & (BS1_BSY | BS1_SEL)) {
453*4882a593Smuzhiyun 		/*
454*4882a593Smuzhiyun 		 * Some other device has the bus or is arbitrating for it -
455*4882a593Smuzhiyun 		 * probably a target which is about to reselect us.
456*4882a593Smuzhiyun 		 */
457*4882a593Smuzhiyun 		dlog(ms, "busy b4 arb, intr/exc/err/fc=%.8x",
458*4882a593Smuzhiyun 		     MKWORD(mr->interrupt, mr->exception,
459*4882a593Smuzhiyun 			    mr->error, mr->fifo_count));
460*4882a593Smuzhiyun 		for (t = 100; t > 0; --t) {
461*4882a593Smuzhiyun 			if ((in_8(&mr->bus_status1) & (BS1_BSY | BS1_SEL)) == 0)
462*4882a593Smuzhiyun 				break;
463*4882a593Smuzhiyun 			if (in_8(&mr->interrupt) != 0) {
464*4882a593Smuzhiyun 				dlog(ms, "intr b4 arb, intr/exc/err/fc=%.8x",
465*4882a593Smuzhiyun 				     MKWORD(mr->interrupt, mr->exception,
466*4882a593Smuzhiyun 					    mr->error, mr->fifo_count));
467*4882a593Smuzhiyun 				mesh_interrupt(ms);
468*4882a593Smuzhiyun 				if (ms->phase != arbitrating)
469*4882a593Smuzhiyun 					return;
470*4882a593Smuzhiyun 			}
471*4882a593Smuzhiyun 			udelay(1);
472*4882a593Smuzhiyun 		}
473*4882a593Smuzhiyun 		if (in_8(&mr->bus_status1) & (BS1_BSY | BS1_SEL)) {
474*4882a593Smuzhiyun 			/* XXX should try again in a little while */
475*4882a593Smuzhiyun 			ms->stat = DID_BUS_BUSY;
476*4882a593Smuzhiyun 			ms->phase = idle;
477*4882a593Smuzhiyun 			mesh_done(ms, 0);
478*4882a593Smuzhiyun 			return;
479*4882a593Smuzhiyun 		}
480*4882a593Smuzhiyun 	}
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	/*
483*4882a593Smuzhiyun 	 * Apparently the mesh has a bug where it will assert both its
484*4882a593Smuzhiyun 	 * own bit and the target's bit on the bus during arbitration.
485*4882a593Smuzhiyun 	 */
486*4882a593Smuzhiyun 	out_8(&mr->dest_id, mr->source_id);
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 	/*
489*4882a593Smuzhiyun 	 * There appears to be a race with reselection sometimes,
490*4882a593Smuzhiyun 	 * where a target reselects us just as we issue the
491*4882a593Smuzhiyun 	 * arbitrate command.  It seems that then the arbitrate
492*4882a593Smuzhiyun 	 * command just hangs waiting for the bus to be free
493*4882a593Smuzhiyun 	 * without giving us a reselection exception.
494*4882a593Smuzhiyun 	 * The only way I have found to get it to respond correctly
495*4882a593Smuzhiyun 	 * is this: disable reselection before issuing the arbitrate
496*4882a593Smuzhiyun 	 * command, then after issuing it, if it looks like a target
497*4882a593Smuzhiyun 	 * is trying to reselect us, reset the mesh and then enable
498*4882a593Smuzhiyun 	 * reselection.
499*4882a593Smuzhiyun 	 */
500*4882a593Smuzhiyun 	out_8(&mr->sequence, SEQ_DISRESEL);
501*4882a593Smuzhiyun 	if (in_8(&mr->interrupt) != 0) {
502*4882a593Smuzhiyun 		dlog(ms, "intr after disresel, intr/exc/err/fc=%.8x",
503*4882a593Smuzhiyun 		     MKWORD(mr->interrupt, mr->exception,
504*4882a593Smuzhiyun 			    mr->error, mr->fifo_count));
505*4882a593Smuzhiyun 		mesh_interrupt(ms);
506*4882a593Smuzhiyun 		if (ms->phase != arbitrating)
507*4882a593Smuzhiyun 			return;
508*4882a593Smuzhiyun 		dlog(ms, "after intr after disresel, intr/exc/err/fc=%.8x",
509*4882a593Smuzhiyun 		     MKWORD(mr->interrupt, mr->exception,
510*4882a593Smuzhiyun 			    mr->error, mr->fifo_count));
511*4882a593Smuzhiyun 	}
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 	out_8(&mr->sequence, SEQ_ARBITRATE);
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun 	for (t = 230; t > 0; --t) {
516*4882a593Smuzhiyun 		if (in_8(&mr->interrupt) != 0)
517*4882a593Smuzhiyun 			break;
518*4882a593Smuzhiyun 		udelay(1);
519*4882a593Smuzhiyun 	}
520*4882a593Smuzhiyun 	dlog(ms, "after arb, intr/exc/err/fc=%.8x",
521*4882a593Smuzhiyun 	     MKWORD(mr->interrupt, mr->exception, mr->error, mr->fifo_count));
522*4882a593Smuzhiyun 	if (in_8(&mr->interrupt) == 0 && (in_8(&mr->bus_status1) & BS1_SEL)
523*4882a593Smuzhiyun 	    && (in_8(&mr->bus_status0) & BS0_IO)) {
524*4882a593Smuzhiyun 		/* looks like a reselection - try resetting the mesh */
525*4882a593Smuzhiyun 		dlog(ms, "resel? after arb, intr/exc/err/fc=%.8x",
526*4882a593Smuzhiyun 		     MKWORD(mr->interrupt, mr->exception, mr->error, mr->fifo_count));
527*4882a593Smuzhiyun 		out_8(&mr->sequence, SEQ_RESETMESH);
528*4882a593Smuzhiyun 		mesh_flush_io(mr);
529*4882a593Smuzhiyun 		udelay(10);
530*4882a593Smuzhiyun 		out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
531*4882a593Smuzhiyun 		out_8(&mr->intr_mask, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
532*4882a593Smuzhiyun 		out_8(&mr->sequence, SEQ_ENBRESEL);
533*4882a593Smuzhiyun 		mesh_flush_io(mr);
534*4882a593Smuzhiyun 		for (t = 10; t > 0 && in_8(&mr->interrupt) == 0; --t)
535*4882a593Smuzhiyun 			udelay(1);
536*4882a593Smuzhiyun 		dlog(ms, "tried reset after arb, intr/exc/err/fc=%.8x",
537*4882a593Smuzhiyun 		     MKWORD(mr->interrupt, mr->exception, mr->error, mr->fifo_count));
538*4882a593Smuzhiyun #ifndef MESH_MULTIPLE_HOSTS
539*4882a593Smuzhiyun 		if (in_8(&mr->interrupt) == 0 && (in_8(&mr->bus_status1) & BS1_SEL)
540*4882a593Smuzhiyun 		    && (in_8(&mr->bus_status0) & BS0_IO)) {
541*4882a593Smuzhiyun 			printk(KERN_ERR "mesh: controller not responding"
542*4882a593Smuzhiyun 			       " to reselection!\n");
543*4882a593Smuzhiyun 			/*
544*4882a593Smuzhiyun 			 * If this is a target reselecting us, and the
545*4882a593Smuzhiyun 			 * mesh isn't responding, the higher levels of
546*4882a593Smuzhiyun 			 * the scsi code will eventually time out and
547*4882a593Smuzhiyun 			 * reset the bus.
548*4882a593Smuzhiyun 			 */
549*4882a593Smuzhiyun 		}
550*4882a593Smuzhiyun #endif
551*4882a593Smuzhiyun 	}
552*4882a593Smuzhiyun }
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun /*
555*4882a593Smuzhiyun  * Start the next command for a MESH.
556*4882a593Smuzhiyun  * Should be called with interrupts disabled.
557*4882a593Smuzhiyun  */
mesh_start(struct mesh_state * ms)558*4882a593Smuzhiyun static void mesh_start(struct mesh_state *ms)
559*4882a593Smuzhiyun {
560*4882a593Smuzhiyun 	struct scsi_cmnd *cmd, *prev, *next;
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	if (ms->phase != idle || ms->current_req != NULL) {
563*4882a593Smuzhiyun 		printk(KERN_ERR "inappropriate mesh_start (phase=%d, ms=%p)",
564*4882a593Smuzhiyun 		       ms->phase, ms);
565*4882a593Smuzhiyun 		return;
566*4882a593Smuzhiyun 	}
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 	while (ms->phase == idle) {
569*4882a593Smuzhiyun 		prev = NULL;
570*4882a593Smuzhiyun 		for (cmd = ms->request_q; ; cmd = (struct scsi_cmnd *) cmd->host_scribble) {
571*4882a593Smuzhiyun 			if (cmd == NULL)
572*4882a593Smuzhiyun 				return;
573*4882a593Smuzhiyun 			if (ms->tgts[cmd->device->id].current_req == NULL)
574*4882a593Smuzhiyun 				break;
575*4882a593Smuzhiyun 			prev = cmd;
576*4882a593Smuzhiyun 		}
577*4882a593Smuzhiyun 		next = (struct scsi_cmnd *) cmd->host_scribble;
578*4882a593Smuzhiyun 		if (prev == NULL)
579*4882a593Smuzhiyun 			ms->request_q = next;
580*4882a593Smuzhiyun 		else
581*4882a593Smuzhiyun 			prev->host_scribble = (void *) next;
582*4882a593Smuzhiyun 		if (next == NULL)
583*4882a593Smuzhiyun 			ms->request_qtail = prev;
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 		mesh_start_cmd(ms, cmd);
586*4882a593Smuzhiyun 	}
587*4882a593Smuzhiyun }
588*4882a593Smuzhiyun 
mesh_done(struct mesh_state * ms,int start_next)589*4882a593Smuzhiyun static void mesh_done(struct mesh_state *ms, int start_next)
590*4882a593Smuzhiyun {
591*4882a593Smuzhiyun 	struct scsi_cmnd *cmd;
592*4882a593Smuzhiyun 	struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun 	cmd = ms->current_req;
595*4882a593Smuzhiyun 	ms->current_req = NULL;
596*4882a593Smuzhiyun 	tp->current_req = NULL;
597*4882a593Smuzhiyun 	if (cmd) {
598*4882a593Smuzhiyun 		cmd->result = (ms->stat << 16) | cmd->SCp.Status;
599*4882a593Smuzhiyun 		if (ms->stat == DID_OK)
600*4882a593Smuzhiyun 			cmd->result |= cmd->SCp.Message << 8;
601*4882a593Smuzhiyun 		if (DEBUG_TARGET(cmd)) {
602*4882a593Smuzhiyun 			printk(KERN_DEBUG "mesh_done: result = %x, data_ptr=%d, buflen=%d\n",
603*4882a593Smuzhiyun 			       cmd->result, ms->data_ptr, scsi_bufflen(cmd));
604*4882a593Smuzhiyun #if 0
605*4882a593Smuzhiyun 			/* needs to use sg? */
606*4882a593Smuzhiyun 			if ((cmd->cmnd[0] == 0 || cmd->cmnd[0] == 0x12 || cmd->cmnd[0] == 3)
607*4882a593Smuzhiyun 			    && cmd->request_buffer != 0) {
608*4882a593Smuzhiyun 				unsigned char *b = cmd->request_buffer;
609*4882a593Smuzhiyun 				printk(KERN_DEBUG "buffer = %x %x %x %x %x %x %x %x\n",
610*4882a593Smuzhiyun 				       b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
611*4882a593Smuzhiyun 			}
612*4882a593Smuzhiyun #endif
613*4882a593Smuzhiyun 		}
614*4882a593Smuzhiyun 		cmd->SCp.this_residual -= ms->data_ptr;
615*4882a593Smuzhiyun 		mesh_completed(ms, cmd);
616*4882a593Smuzhiyun 	}
617*4882a593Smuzhiyun 	if (start_next) {
618*4882a593Smuzhiyun 		out_8(&ms->mesh->sequence, SEQ_ENBRESEL);
619*4882a593Smuzhiyun 		mesh_flush_io(ms->mesh);
620*4882a593Smuzhiyun 		udelay(1);
621*4882a593Smuzhiyun 		ms->phase = idle;
622*4882a593Smuzhiyun 		mesh_start(ms);
623*4882a593Smuzhiyun 	}
624*4882a593Smuzhiyun }
625*4882a593Smuzhiyun 
add_sdtr_msg(struct mesh_state * ms)626*4882a593Smuzhiyun static inline void add_sdtr_msg(struct mesh_state *ms)
627*4882a593Smuzhiyun {
628*4882a593Smuzhiyun 	int i = ms->n_msgout;
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun 	ms->msgout[i] = EXTENDED_MESSAGE;
631*4882a593Smuzhiyun 	ms->msgout[i+1] = 3;
632*4882a593Smuzhiyun 	ms->msgout[i+2] = EXTENDED_SDTR;
633*4882a593Smuzhiyun 	ms->msgout[i+3] = mesh_sync_period/4;
634*4882a593Smuzhiyun 	ms->msgout[i+4] = (ALLOW_SYNC(ms->conn_tgt)? mesh_sync_offset: 0);
635*4882a593Smuzhiyun 	ms->n_msgout = i + 5;
636*4882a593Smuzhiyun }
637*4882a593Smuzhiyun 
set_sdtr(struct mesh_state * ms,int period,int offset)638*4882a593Smuzhiyun static void set_sdtr(struct mesh_state *ms, int period, int offset)
639*4882a593Smuzhiyun {
640*4882a593Smuzhiyun 	struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
641*4882a593Smuzhiyun 	volatile struct mesh_regs __iomem *mr = ms->mesh;
642*4882a593Smuzhiyun 	int v, tr;
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 	tp->sdtr_state = sdtr_done;
645*4882a593Smuzhiyun 	if (offset == 0) {
646*4882a593Smuzhiyun 		/* asynchronous */
647*4882a593Smuzhiyun 		if (SYNC_OFF(tp->sync_params))
648*4882a593Smuzhiyun 			printk(KERN_INFO "mesh: target %d now asynchronous\n",
649*4882a593Smuzhiyun 			       ms->conn_tgt);
650*4882a593Smuzhiyun 		tp->sync_params = ASYNC_PARAMS;
651*4882a593Smuzhiyun 		out_8(&mr->sync_params, ASYNC_PARAMS);
652*4882a593Smuzhiyun 		return;
653*4882a593Smuzhiyun 	}
654*4882a593Smuzhiyun 	/*
655*4882a593Smuzhiyun 	 * We need to compute ceil(clk_freq * period / 500e6) - 2
656*4882a593Smuzhiyun 	 * without incurring overflow.
657*4882a593Smuzhiyun 	 */
658*4882a593Smuzhiyun 	v = (ms->clk_freq / 5000) * period;
659*4882a593Smuzhiyun 	if (v <= 250000) {
660*4882a593Smuzhiyun 		/* special case: sync_period == 5 * clk_period */
661*4882a593Smuzhiyun 		v = 0;
662*4882a593Smuzhiyun 		/* units of tr are 100kB/s */
663*4882a593Smuzhiyun 		tr = (ms->clk_freq + 250000) / 500000;
664*4882a593Smuzhiyun 	} else {
665*4882a593Smuzhiyun 		/* sync_period == (v + 2) * 2 * clk_period */
666*4882a593Smuzhiyun 		v = (v + 99999) / 100000 - 2;
667*4882a593Smuzhiyun 		if (v > 15)
668*4882a593Smuzhiyun 			v = 15;	/* oops */
669*4882a593Smuzhiyun 		tr = ((ms->clk_freq / (v + 2)) + 199999) / 200000;
670*4882a593Smuzhiyun 	}
671*4882a593Smuzhiyun 	if (offset > 15)
672*4882a593Smuzhiyun 		offset = 15;	/* can't happen */
673*4882a593Smuzhiyun 	tp->sync_params = SYNC_PARAMS(offset, v);
674*4882a593Smuzhiyun 	out_8(&mr->sync_params, tp->sync_params);
675*4882a593Smuzhiyun 	printk(KERN_INFO "mesh: target %d synchronous at %d.%d MB/s\n",
676*4882a593Smuzhiyun 	       ms->conn_tgt, tr/10, tr%10);
677*4882a593Smuzhiyun }
678*4882a593Smuzhiyun 
start_phase(struct mesh_state * ms)679*4882a593Smuzhiyun static void start_phase(struct mesh_state *ms)
680*4882a593Smuzhiyun {
681*4882a593Smuzhiyun 	int i, seq, nb;
682*4882a593Smuzhiyun 	volatile struct mesh_regs __iomem *mr = ms->mesh;
683*4882a593Smuzhiyun 	volatile struct dbdma_regs __iomem *md = ms->dma;
684*4882a593Smuzhiyun 	struct scsi_cmnd *cmd = ms->current_req;
685*4882a593Smuzhiyun 	struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 	dlog(ms, "start_phase nmo/exc/fc/seq = %.8x",
688*4882a593Smuzhiyun 	     MKWORD(ms->n_msgout, mr->exception, mr->fifo_count, mr->sequence));
689*4882a593Smuzhiyun 	out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
690*4882a593Smuzhiyun 	seq = use_active_neg + (ms->n_msgout? SEQ_ATN: 0);
691*4882a593Smuzhiyun 	switch (ms->msgphase) {
692*4882a593Smuzhiyun 	case msg_none:
693*4882a593Smuzhiyun 		break;
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 	case msg_in:
696*4882a593Smuzhiyun 		out_8(&mr->count_hi, 0);
697*4882a593Smuzhiyun 		out_8(&mr->count_lo, 1);
698*4882a593Smuzhiyun 		out_8(&mr->sequence, SEQ_MSGIN + seq);
699*4882a593Smuzhiyun 		ms->n_msgin = 0;
700*4882a593Smuzhiyun 		return;
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun 	case msg_out:
703*4882a593Smuzhiyun 		/*
704*4882a593Smuzhiyun 		 * To make sure ATN drops before we assert ACK for
705*4882a593Smuzhiyun 		 * the last byte of the message, we have to do the
706*4882a593Smuzhiyun 		 * last byte specially.
707*4882a593Smuzhiyun 		 */
708*4882a593Smuzhiyun 		if (ms->n_msgout <= 0) {
709*4882a593Smuzhiyun 			printk(KERN_ERR "mesh: msg_out but n_msgout=%d\n",
710*4882a593Smuzhiyun 			       ms->n_msgout);
711*4882a593Smuzhiyun 			mesh_dump_regs(ms);
712*4882a593Smuzhiyun 			ms->msgphase = msg_none;
713*4882a593Smuzhiyun 			break;
714*4882a593Smuzhiyun 		}
715*4882a593Smuzhiyun 		if (ALLOW_DEBUG(ms->conn_tgt)) {
716*4882a593Smuzhiyun 			printk(KERN_DEBUG "mesh: sending %d msg bytes:",
717*4882a593Smuzhiyun 			       ms->n_msgout);
718*4882a593Smuzhiyun 			for (i = 0; i < ms->n_msgout; ++i)
719*4882a593Smuzhiyun 				printk(" %x", ms->msgout[i]);
720*4882a593Smuzhiyun 			printk("\n");
721*4882a593Smuzhiyun 		}
722*4882a593Smuzhiyun 		dlog(ms, "msgout msg=%.8x", MKWORD(ms->n_msgout, ms->msgout[0],
723*4882a593Smuzhiyun 						ms->msgout[1], ms->msgout[2]));
724*4882a593Smuzhiyun 		out_8(&mr->count_hi, 0);
725*4882a593Smuzhiyun 		out_8(&mr->sequence, SEQ_FLUSHFIFO);
726*4882a593Smuzhiyun 		mesh_flush_io(mr);
727*4882a593Smuzhiyun 		udelay(1);
728*4882a593Smuzhiyun 		/*
729*4882a593Smuzhiyun 		 * If ATN is not already asserted, we assert it, then
730*4882a593Smuzhiyun 		 * issue a SEQ_MSGOUT to get the mesh to drop ACK.
731*4882a593Smuzhiyun 		 */
732*4882a593Smuzhiyun 		if ((in_8(&mr->bus_status0) & BS0_ATN) == 0) {
733*4882a593Smuzhiyun 			dlog(ms, "bus0 was %.2x explicitly asserting ATN", mr->bus_status0);
734*4882a593Smuzhiyun 			out_8(&mr->bus_status0, BS0_ATN); /* explicit ATN */
735*4882a593Smuzhiyun 			mesh_flush_io(mr);
736*4882a593Smuzhiyun 			udelay(1);
737*4882a593Smuzhiyun 			out_8(&mr->count_lo, 1);
738*4882a593Smuzhiyun 			out_8(&mr->sequence, SEQ_MSGOUT + seq);
739*4882a593Smuzhiyun 			out_8(&mr->bus_status0, 0); /* release explicit ATN */
740*4882a593Smuzhiyun 			dlog(ms,"hace: after explicit ATN bus0=%.2x",mr->bus_status0);
741*4882a593Smuzhiyun 		}
742*4882a593Smuzhiyun 		if (ms->n_msgout == 1) {
743*4882a593Smuzhiyun 			/*
744*4882a593Smuzhiyun 			 * We can't issue the SEQ_MSGOUT without ATN
745*4882a593Smuzhiyun 			 * until the target has asserted REQ.  The logic
746*4882a593Smuzhiyun 			 * in cmd_complete handles both situations:
747*4882a593Smuzhiyun 			 * REQ already asserted or not.
748*4882a593Smuzhiyun 			 */
749*4882a593Smuzhiyun 			cmd_complete(ms);
750*4882a593Smuzhiyun 		} else {
751*4882a593Smuzhiyun 			out_8(&mr->count_lo, ms->n_msgout - 1);
752*4882a593Smuzhiyun 			out_8(&mr->sequence, SEQ_MSGOUT + seq);
753*4882a593Smuzhiyun 			for (i = 0; i < ms->n_msgout - 1; ++i)
754*4882a593Smuzhiyun 				out_8(&mr->fifo, ms->msgout[i]);
755*4882a593Smuzhiyun 		}
756*4882a593Smuzhiyun 		return;
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun 	default:
759*4882a593Smuzhiyun 		printk(KERN_ERR "mesh bug: start_phase msgphase=%d\n",
760*4882a593Smuzhiyun 		       ms->msgphase);
761*4882a593Smuzhiyun 	}
762*4882a593Smuzhiyun 
763*4882a593Smuzhiyun 	switch (ms->phase) {
764*4882a593Smuzhiyun 	case selecting:
765*4882a593Smuzhiyun 		out_8(&mr->dest_id, ms->conn_tgt);
766*4882a593Smuzhiyun 		out_8(&mr->sequence, SEQ_SELECT + SEQ_ATN);
767*4882a593Smuzhiyun 		break;
768*4882a593Smuzhiyun 	case commanding:
769*4882a593Smuzhiyun 		out_8(&mr->sync_params, tp->sync_params);
770*4882a593Smuzhiyun 		out_8(&mr->count_hi, 0);
771*4882a593Smuzhiyun 		if (cmd) {
772*4882a593Smuzhiyun 			out_8(&mr->count_lo, cmd->cmd_len);
773*4882a593Smuzhiyun 			out_8(&mr->sequence, SEQ_COMMAND + seq);
774*4882a593Smuzhiyun 			for (i = 0; i < cmd->cmd_len; ++i)
775*4882a593Smuzhiyun 				out_8(&mr->fifo, cmd->cmnd[i]);
776*4882a593Smuzhiyun 		} else {
777*4882a593Smuzhiyun 			out_8(&mr->count_lo, 6);
778*4882a593Smuzhiyun 			out_8(&mr->sequence, SEQ_COMMAND + seq);
779*4882a593Smuzhiyun 			for (i = 0; i < 6; ++i)
780*4882a593Smuzhiyun 				out_8(&mr->fifo, 0);
781*4882a593Smuzhiyun 		}
782*4882a593Smuzhiyun 		break;
783*4882a593Smuzhiyun 	case dataing:
784*4882a593Smuzhiyun 		/* transfer data, if any */
785*4882a593Smuzhiyun 		if (!ms->dma_started) {
786*4882a593Smuzhiyun 			set_dma_cmds(ms, cmd);
787*4882a593Smuzhiyun 			out_le32(&md->cmdptr, virt_to_phys(ms->dma_cmds));
788*4882a593Smuzhiyun 			out_le32(&md->control, (RUN << 16) | RUN);
789*4882a593Smuzhiyun 			ms->dma_started = 1;
790*4882a593Smuzhiyun 		}
791*4882a593Smuzhiyun 		nb = ms->dma_count;
792*4882a593Smuzhiyun 		if (nb > 0xfff0)
793*4882a593Smuzhiyun 			nb = 0xfff0;
794*4882a593Smuzhiyun 		ms->dma_count -= nb;
795*4882a593Smuzhiyun 		ms->data_ptr += nb;
796*4882a593Smuzhiyun 		out_8(&mr->count_lo, nb);
797*4882a593Smuzhiyun 		out_8(&mr->count_hi, nb >> 8);
798*4882a593Smuzhiyun 		out_8(&mr->sequence, (tp->data_goes_out?
799*4882a593Smuzhiyun 				SEQ_DATAOUT: SEQ_DATAIN) + SEQ_DMA_MODE + seq);
800*4882a593Smuzhiyun 		break;
801*4882a593Smuzhiyun 	case statusing:
802*4882a593Smuzhiyun 		out_8(&mr->count_hi, 0);
803*4882a593Smuzhiyun 		out_8(&mr->count_lo, 1);
804*4882a593Smuzhiyun 		out_8(&mr->sequence, SEQ_STATUS + seq);
805*4882a593Smuzhiyun 		break;
806*4882a593Smuzhiyun 	case busfreeing:
807*4882a593Smuzhiyun 	case disconnecting:
808*4882a593Smuzhiyun 		out_8(&mr->sequence, SEQ_ENBRESEL);
809*4882a593Smuzhiyun 		mesh_flush_io(mr);
810*4882a593Smuzhiyun 		udelay(1);
811*4882a593Smuzhiyun 		dlog(ms, "enbresel intr/exc/err/fc=%.8x",
812*4882a593Smuzhiyun 		     MKWORD(mr->interrupt, mr->exception, mr->error,
813*4882a593Smuzhiyun 			    mr->fifo_count));
814*4882a593Smuzhiyun 		out_8(&mr->sequence, SEQ_BUSFREE);
815*4882a593Smuzhiyun 		break;
816*4882a593Smuzhiyun 	default:
817*4882a593Smuzhiyun 		printk(KERN_ERR "mesh: start_phase called with phase=%d\n",
818*4882a593Smuzhiyun 		       ms->phase);
819*4882a593Smuzhiyun 		dumpslog(ms);
820*4882a593Smuzhiyun 	}
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun }
823*4882a593Smuzhiyun 
get_msgin(struct mesh_state * ms)824*4882a593Smuzhiyun static inline void get_msgin(struct mesh_state *ms)
825*4882a593Smuzhiyun {
826*4882a593Smuzhiyun 	volatile struct mesh_regs __iomem *mr = ms->mesh;
827*4882a593Smuzhiyun 	int i, n;
828*4882a593Smuzhiyun 
829*4882a593Smuzhiyun 	n = mr->fifo_count;
830*4882a593Smuzhiyun 	if (n != 0) {
831*4882a593Smuzhiyun 		i = ms->n_msgin;
832*4882a593Smuzhiyun 		ms->n_msgin = i + n;
833*4882a593Smuzhiyun 		for (; n > 0; --n)
834*4882a593Smuzhiyun 			ms->msgin[i++] = in_8(&mr->fifo);
835*4882a593Smuzhiyun 	}
836*4882a593Smuzhiyun }
837*4882a593Smuzhiyun 
msgin_length(struct mesh_state * ms)838*4882a593Smuzhiyun static inline int msgin_length(struct mesh_state *ms)
839*4882a593Smuzhiyun {
840*4882a593Smuzhiyun 	int b, n;
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun 	n = 1;
843*4882a593Smuzhiyun 	if (ms->n_msgin > 0) {
844*4882a593Smuzhiyun 		b = ms->msgin[0];
845*4882a593Smuzhiyun 		if (b == 1) {
846*4882a593Smuzhiyun 			/* extended message */
847*4882a593Smuzhiyun 			n = ms->n_msgin < 2? 2: ms->msgin[1] + 2;
848*4882a593Smuzhiyun 		} else if (0x20 <= b && b <= 0x2f) {
849*4882a593Smuzhiyun 			/* 2-byte message */
850*4882a593Smuzhiyun 			n = 2;
851*4882a593Smuzhiyun 		}
852*4882a593Smuzhiyun 	}
853*4882a593Smuzhiyun 	return n;
854*4882a593Smuzhiyun }
855*4882a593Smuzhiyun 
reselected(struct mesh_state * ms)856*4882a593Smuzhiyun static void reselected(struct mesh_state *ms)
857*4882a593Smuzhiyun {
858*4882a593Smuzhiyun 	volatile struct mesh_regs __iomem *mr = ms->mesh;
859*4882a593Smuzhiyun 	struct scsi_cmnd *cmd;
860*4882a593Smuzhiyun 	struct mesh_target *tp;
861*4882a593Smuzhiyun 	int b, t, prev;
862*4882a593Smuzhiyun 
863*4882a593Smuzhiyun 	switch (ms->phase) {
864*4882a593Smuzhiyun 	case idle:
865*4882a593Smuzhiyun 		break;
866*4882a593Smuzhiyun 	case arbitrating:
867*4882a593Smuzhiyun 		if ((cmd = ms->current_req) != NULL) {
868*4882a593Smuzhiyun 			/* put the command back on the queue */
869*4882a593Smuzhiyun 			cmd->host_scribble = (void *) ms->request_q;
870*4882a593Smuzhiyun 			if (ms->request_q == NULL)
871*4882a593Smuzhiyun 				ms->request_qtail = cmd;
872*4882a593Smuzhiyun 			ms->request_q = cmd;
873*4882a593Smuzhiyun 			tp = &ms->tgts[cmd->device->id];
874*4882a593Smuzhiyun 			tp->current_req = NULL;
875*4882a593Smuzhiyun 		}
876*4882a593Smuzhiyun 		break;
877*4882a593Smuzhiyun 	case busfreeing:
878*4882a593Smuzhiyun 		ms->phase = reselecting;
879*4882a593Smuzhiyun 		mesh_done(ms, 0);
880*4882a593Smuzhiyun 		break;
881*4882a593Smuzhiyun 	case disconnecting:
882*4882a593Smuzhiyun 		break;
883*4882a593Smuzhiyun 	default:
884*4882a593Smuzhiyun 		printk(KERN_ERR "mesh: reselected in phase %d/%d tgt %d\n",
885*4882a593Smuzhiyun 		       ms->msgphase, ms->phase, ms->conn_tgt);
886*4882a593Smuzhiyun 		dumplog(ms, ms->conn_tgt);
887*4882a593Smuzhiyun 		dumpslog(ms);
888*4882a593Smuzhiyun 	}
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun 	if (ms->dma_started) {
891*4882a593Smuzhiyun 		printk(KERN_ERR "mesh: reselected with DMA started !\n");
892*4882a593Smuzhiyun 		halt_dma(ms);
893*4882a593Smuzhiyun 	}
894*4882a593Smuzhiyun 	ms->current_req = NULL;
895*4882a593Smuzhiyun 	ms->phase = dataing;
896*4882a593Smuzhiyun 	ms->msgphase = msg_in;
897*4882a593Smuzhiyun 	ms->n_msgout = 0;
898*4882a593Smuzhiyun 	ms->last_n_msgout = 0;
899*4882a593Smuzhiyun 	prev = ms->conn_tgt;
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun 	/*
902*4882a593Smuzhiyun 	 * We seem to get abortive reselections sometimes.
903*4882a593Smuzhiyun 	 */
904*4882a593Smuzhiyun 	while ((in_8(&mr->bus_status1) & BS1_BSY) == 0) {
905*4882a593Smuzhiyun 		static int mesh_aborted_resels;
906*4882a593Smuzhiyun 		mesh_aborted_resels++;
907*4882a593Smuzhiyun 		out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
908*4882a593Smuzhiyun 		mesh_flush_io(mr);
909*4882a593Smuzhiyun 		udelay(1);
910*4882a593Smuzhiyun 		out_8(&mr->sequence, SEQ_ENBRESEL);
911*4882a593Smuzhiyun 		mesh_flush_io(mr);
912*4882a593Smuzhiyun 		udelay(5);
913*4882a593Smuzhiyun 		dlog(ms, "extra resel err/exc/fc = %.6x",
914*4882a593Smuzhiyun 		     MKWORD(0, mr->error, mr->exception, mr->fifo_count));
915*4882a593Smuzhiyun 	}
916*4882a593Smuzhiyun 	out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
917*4882a593Smuzhiyun        	mesh_flush_io(mr);
918*4882a593Smuzhiyun 	udelay(1);
919*4882a593Smuzhiyun 	out_8(&mr->sequence, SEQ_ENBRESEL);
920*4882a593Smuzhiyun        	mesh_flush_io(mr);
921*4882a593Smuzhiyun 	udelay(1);
922*4882a593Smuzhiyun 	out_8(&mr->sync_params, ASYNC_PARAMS);
923*4882a593Smuzhiyun 
924*4882a593Smuzhiyun 	/*
925*4882a593Smuzhiyun 	 * Find out who reselected us.
926*4882a593Smuzhiyun 	 */
927*4882a593Smuzhiyun 	if (in_8(&mr->fifo_count) == 0) {
928*4882a593Smuzhiyun 		printk(KERN_ERR "mesh: reselection but nothing in fifo?\n");
929*4882a593Smuzhiyun 		ms->conn_tgt = ms->host->this_id;
930*4882a593Smuzhiyun 		goto bogus;
931*4882a593Smuzhiyun 	}
932*4882a593Smuzhiyun 	/* get the last byte in the fifo */
933*4882a593Smuzhiyun 	do {
934*4882a593Smuzhiyun 		b = in_8(&mr->fifo);
935*4882a593Smuzhiyun 		dlog(ms, "reseldata %x", b);
936*4882a593Smuzhiyun 	} while (in_8(&mr->fifo_count));
937*4882a593Smuzhiyun 	for (t = 0; t < 8; ++t)
938*4882a593Smuzhiyun 		if ((b & (1 << t)) != 0 && t != ms->host->this_id)
939*4882a593Smuzhiyun 			break;
940*4882a593Smuzhiyun 	if (b != (1 << t) + (1 << ms->host->this_id)) {
941*4882a593Smuzhiyun 		printk(KERN_ERR "mesh: bad reselection data %x\n", b);
942*4882a593Smuzhiyun 		ms->conn_tgt = ms->host->this_id;
943*4882a593Smuzhiyun 		goto bogus;
944*4882a593Smuzhiyun 	}
945*4882a593Smuzhiyun 
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun 	/*
948*4882a593Smuzhiyun 	 * Set up to continue with that target's transfer.
949*4882a593Smuzhiyun 	 */
950*4882a593Smuzhiyun 	ms->conn_tgt = t;
951*4882a593Smuzhiyun 	tp = &ms->tgts[t];
952*4882a593Smuzhiyun 	out_8(&mr->sync_params, tp->sync_params);
953*4882a593Smuzhiyun 	if (ALLOW_DEBUG(t)) {
954*4882a593Smuzhiyun 		printk(KERN_DEBUG "mesh: reselected by target %d\n", t);
955*4882a593Smuzhiyun 		printk(KERN_DEBUG "mesh: saved_ptr=%x goes_out=%d cmd=%p\n",
956*4882a593Smuzhiyun 		       tp->saved_ptr, tp->data_goes_out, tp->current_req);
957*4882a593Smuzhiyun 	}
958*4882a593Smuzhiyun 	ms->current_req = tp->current_req;
959*4882a593Smuzhiyun 	if (tp->current_req == NULL) {
960*4882a593Smuzhiyun 		printk(KERN_ERR "mesh: reselected by tgt %d but no cmd!\n", t);
961*4882a593Smuzhiyun 		goto bogus;
962*4882a593Smuzhiyun 	}
963*4882a593Smuzhiyun 	ms->data_ptr = tp->saved_ptr;
964*4882a593Smuzhiyun 	dlog(ms, "resel prev tgt=%d", prev);
965*4882a593Smuzhiyun 	dlog(ms, "resel err/exc=%.4x", MKWORD(0, 0, mr->error, mr->exception));
966*4882a593Smuzhiyun 	start_phase(ms);
967*4882a593Smuzhiyun 	return;
968*4882a593Smuzhiyun 
969*4882a593Smuzhiyun bogus:
970*4882a593Smuzhiyun 	dumplog(ms, ms->conn_tgt);
971*4882a593Smuzhiyun 	dumpslog(ms);
972*4882a593Smuzhiyun 	ms->data_ptr = 0;
973*4882a593Smuzhiyun 	ms->aborting = 1;
974*4882a593Smuzhiyun 	start_phase(ms);
975*4882a593Smuzhiyun }
976*4882a593Smuzhiyun 
do_abort(struct mesh_state * ms)977*4882a593Smuzhiyun static void do_abort(struct mesh_state *ms)
978*4882a593Smuzhiyun {
979*4882a593Smuzhiyun 	ms->msgout[0] = ABORT;
980*4882a593Smuzhiyun 	ms->n_msgout = 1;
981*4882a593Smuzhiyun 	ms->aborting = 1;
982*4882a593Smuzhiyun 	ms->stat = DID_ABORT;
983*4882a593Smuzhiyun 	dlog(ms, "abort", 0);
984*4882a593Smuzhiyun }
985*4882a593Smuzhiyun 
handle_reset(struct mesh_state * ms)986*4882a593Smuzhiyun static void handle_reset(struct mesh_state *ms)
987*4882a593Smuzhiyun {
988*4882a593Smuzhiyun 	int tgt;
989*4882a593Smuzhiyun 	struct mesh_target *tp;
990*4882a593Smuzhiyun 	struct scsi_cmnd *cmd;
991*4882a593Smuzhiyun 	volatile struct mesh_regs __iomem *mr = ms->mesh;
992*4882a593Smuzhiyun 
993*4882a593Smuzhiyun 	for (tgt = 0; tgt < 8; ++tgt) {
994*4882a593Smuzhiyun 		tp = &ms->tgts[tgt];
995*4882a593Smuzhiyun 		if ((cmd = tp->current_req) != NULL) {
996*4882a593Smuzhiyun 			cmd->result = DID_RESET << 16;
997*4882a593Smuzhiyun 			tp->current_req = NULL;
998*4882a593Smuzhiyun 			mesh_completed(ms, cmd);
999*4882a593Smuzhiyun 		}
1000*4882a593Smuzhiyun 		ms->tgts[tgt].sdtr_state = do_sdtr;
1001*4882a593Smuzhiyun 		ms->tgts[tgt].sync_params = ASYNC_PARAMS;
1002*4882a593Smuzhiyun 	}
1003*4882a593Smuzhiyun 	ms->current_req = NULL;
1004*4882a593Smuzhiyun 	while ((cmd = ms->request_q) != NULL) {
1005*4882a593Smuzhiyun 		ms->request_q = (struct scsi_cmnd *) cmd->host_scribble;
1006*4882a593Smuzhiyun 		cmd->result = DID_RESET << 16;
1007*4882a593Smuzhiyun 		mesh_completed(ms, cmd);
1008*4882a593Smuzhiyun 	}
1009*4882a593Smuzhiyun 	ms->phase = idle;
1010*4882a593Smuzhiyun 	ms->msgphase = msg_none;
1011*4882a593Smuzhiyun 	out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
1012*4882a593Smuzhiyun 	out_8(&mr->sequence, SEQ_FLUSHFIFO);
1013*4882a593Smuzhiyun        	mesh_flush_io(mr);
1014*4882a593Smuzhiyun 	udelay(1);
1015*4882a593Smuzhiyun 	out_8(&mr->sync_params, ASYNC_PARAMS);
1016*4882a593Smuzhiyun 	out_8(&mr->sequence, SEQ_ENBRESEL);
1017*4882a593Smuzhiyun }
1018*4882a593Smuzhiyun 
do_mesh_interrupt(int irq,void * dev_id)1019*4882a593Smuzhiyun static irqreturn_t do_mesh_interrupt(int irq, void *dev_id)
1020*4882a593Smuzhiyun {
1021*4882a593Smuzhiyun 	unsigned long flags;
1022*4882a593Smuzhiyun 	struct mesh_state *ms = dev_id;
1023*4882a593Smuzhiyun 	struct Scsi_Host *dev = ms->host;
1024*4882a593Smuzhiyun 
1025*4882a593Smuzhiyun 	spin_lock_irqsave(dev->host_lock, flags);
1026*4882a593Smuzhiyun 	mesh_interrupt(ms);
1027*4882a593Smuzhiyun 	spin_unlock_irqrestore(dev->host_lock, flags);
1028*4882a593Smuzhiyun 	return IRQ_HANDLED;
1029*4882a593Smuzhiyun }
1030*4882a593Smuzhiyun 
handle_error(struct mesh_state * ms)1031*4882a593Smuzhiyun static void handle_error(struct mesh_state *ms)
1032*4882a593Smuzhiyun {
1033*4882a593Smuzhiyun 	int err, exc, count;
1034*4882a593Smuzhiyun 	volatile struct mesh_regs __iomem *mr = ms->mesh;
1035*4882a593Smuzhiyun 
1036*4882a593Smuzhiyun 	err = in_8(&mr->error);
1037*4882a593Smuzhiyun 	exc = in_8(&mr->exception);
1038*4882a593Smuzhiyun 	out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
1039*4882a593Smuzhiyun 	dlog(ms, "error err/exc/fc/cl=%.8x",
1040*4882a593Smuzhiyun 	     MKWORD(err, exc, mr->fifo_count, mr->count_lo));
1041*4882a593Smuzhiyun 	if (err & ERR_SCSIRESET) {
1042*4882a593Smuzhiyun 		/* SCSI bus was reset */
1043*4882a593Smuzhiyun 		printk(KERN_INFO "mesh: SCSI bus reset detected: "
1044*4882a593Smuzhiyun 		       "waiting for end...");
1045*4882a593Smuzhiyun 		while ((in_8(&mr->bus_status1) & BS1_RST) != 0)
1046*4882a593Smuzhiyun 			udelay(1);
1047*4882a593Smuzhiyun 		printk("done\n");
1048*4882a593Smuzhiyun 		if (ms->dma_started)
1049*4882a593Smuzhiyun 			halt_dma(ms);
1050*4882a593Smuzhiyun 		handle_reset(ms);
1051*4882a593Smuzhiyun 		/* request_q is empty, no point in mesh_start() */
1052*4882a593Smuzhiyun 		return;
1053*4882a593Smuzhiyun 	}
1054*4882a593Smuzhiyun 	if (err & ERR_UNEXPDISC) {
1055*4882a593Smuzhiyun 		/* Unexpected disconnect */
1056*4882a593Smuzhiyun 		if (exc & EXC_RESELECTED) {
1057*4882a593Smuzhiyun 			reselected(ms);
1058*4882a593Smuzhiyun 			return;
1059*4882a593Smuzhiyun 		}
1060*4882a593Smuzhiyun 		if (!ms->aborting) {
1061*4882a593Smuzhiyun 			printk(KERN_WARNING "mesh: target %d aborted\n",
1062*4882a593Smuzhiyun 			       ms->conn_tgt);
1063*4882a593Smuzhiyun 			dumplog(ms, ms->conn_tgt);
1064*4882a593Smuzhiyun 			dumpslog(ms);
1065*4882a593Smuzhiyun 		}
1066*4882a593Smuzhiyun 		out_8(&mr->interrupt, INT_CMDDONE);
1067*4882a593Smuzhiyun 		ms->stat = DID_ABORT;
1068*4882a593Smuzhiyun 		mesh_done(ms, 1);
1069*4882a593Smuzhiyun 		return;
1070*4882a593Smuzhiyun 	}
1071*4882a593Smuzhiyun 	if (err & ERR_PARITY) {
1072*4882a593Smuzhiyun 		if (ms->msgphase == msg_in) {
1073*4882a593Smuzhiyun 			printk(KERN_ERR "mesh: msg parity error, target %d\n",
1074*4882a593Smuzhiyun 			       ms->conn_tgt);
1075*4882a593Smuzhiyun 			ms->msgout[0] = MSG_PARITY_ERROR;
1076*4882a593Smuzhiyun 			ms->n_msgout = 1;
1077*4882a593Smuzhiyun 			ms->msgphase = msg_in_bad;
1078*4882a593Smuzhiyun 			cmd_complete(ms);
1079*4882a593Smuzhiyun 			return;
1080*4882a593Smuzhiyun 		}
1081*4882a593Smuzhiyun 		if (ms->stat == DID_OK) {
1082*4882a593Smuzhiyun 			printk(KERN_ERR "mesh: parity error, target %d\n",
1083*4882a593Smuzhiyun 			       ms->conn_tgt);
1084*4882a593Smuzhiyun 			ms->stat = DID_PARITY;
1085*4882a593Smuzhiyun 		}
1086*4882a593Smuzhiyun 		count = (mr->count_hi << 8) + mr->count_lo;
1087*4882a593Smuzhiyun 		if (count == 0) {
1088*4882a593Smuzhiyun 			cmd_complete(ms);
1089*4882a593Smuzhiyun 		} else {
1090*4882a593Smuzhiyun 			/* reissue the data transfer command */
1091*4882a593Smuzhiyun 			out_8(&mr->sequence, mr->sequence);
1092*4882a593Smuzhiyun 		}
1093*4882a593Smuzhiyun 		return;
1094*4882a593Smuzhiyun 	}
1095*4882a593Smuzhiyun 	if (err & ERR_SEQERR) {
1096*4882a593Smuzhiyun 		if (exc & EXC_RESELECTED) {
1097*4882a593Smuzhiyun 			/* This can happen if we issue a command to
1098*4882a593Smuzhiyun 			   get the bus just after the target reselects us. */
1099*4882a593Smuzhiyun 			static int mesh_resel_seqerr;
1100*4882a593Smuzhiyun 			mesh_resel_seqerr++;
1101*4882a593Smuzhiyun 			reselected(ms);
1102*4882a593Smuzhiyun 			return;
1103*4882a593Smuzhiyun 		}
1104*4882a593Smuzhiyun 		if (exc == EXC_PHASEMM) {
1105*4882a593Smuzhiyun 			static int mesh_phasemm_seqerr;
1106*4882a593Smuzhiyun 			mesh_phasemm_seqerr++;
1107*4882a593Smuzhiyun 			phase_mismatch(ms);
1108*4882a593Smuzhiyun 			return;
1109*4882a593Smuzhiyun 		}
1110*4882a593Smuzhiyun 		printk(KERN_ERR "mesh: sequence error (err=%x exc=%x)\n",
1111*4882a593Smuzhiyun 		       err, exc);
1112*4882a593Smuzhiyun 	} else {
1113*4882a593Smuzhiyun 		printk(KERN_ERR "mesh: unknown error %x (exc=%x)\n", err, exc);
1114*4882a593Smuzhiyun 	}
1115*4882a593Smuzhiyun 	mesh_dump_regs(ms);
1116*4882a593Smuzhiyun 	dumplog(ms, ms->conn_tgt);
1117*4882a593Smuzhiyun 	if (ms->phase > selecting && (in_8(&mr->bus_status1) & BS1_BSY)) {
1118*4882a593Smuzhiyun 		/* try to do what the target wants */
1119*4882a593Smuzhiyun 		do_abort(ms);
1120*4882a593Smuzhiyun 		phase_mismatch(ms);
1121*4882a593Smuzhiyun 		return;
1122*4882a593Smuzhiyun 	}
1123*4882a593Smuzhiyun 	ms->stat = DID_ERROR;
1124*4882a593Smuzhiyun 	mesh_done(ms, 1);
1125*4882a593Smuzhiyun }
1126*4882a593Smuzhiyun 
handle_exception(struct mesh_state * ms)1127*4882a593Smuzhiyun static void handle_exception(struct mesh_state *ms)
1128*4882a593Smuzhiyun {
1129*4882a593Smuzhiyun 	int exc;
1130*4882a593Smuzhiyun 	volatile struct mesh_regs __iomem *mr = ms->mesh;
1131*4882a593Smuzhiyun 
1132*4882a593Smuzhiyun 	exc = in_8(&mr->exception);
1133*4882a593Smuzhiyun 	out_8(&mr->interrupt, INT_EXCEPTION | INT_CMDDONE);
1134*4882a593Smuzhiyun 	if (exc & EXC_RESELECTED) {
1135*4882a593Smuzhiyun 		static int mesh_resel_exc;
1136*4882a593Smuzhiyun 		mesh_resel_exc++;
1137*4882a593Smuzhiyun 		reselected(ms);
1138*4882a593Smuzhiyun 	} else if (exc == EXC_ARBLOST) {
1139*4882a593Smuzhiyun 		printk(KERN_DEBUG "mesh: lost arbitration\n");
1140*4882a593Smuzhiyun 		ms->stat = DID_BUS_BUSY;
1141*4882a593Smuzhiyun 		mesh_done(ms, 1);
1142*4882a593Smuzhiyun 	} else if (exc == EXC_SELTO) {
1143*4882a593Smuzhiyun 		/* selection timed out */
1144*4882a593Smuzhiyun 		ms->stat = DID_BAD_TARGET;
1145*4882a593Smuzhiyun 		mesh_done(ms, 1);
1146*4882a593Smuzhiyun 	} else if (exc == EXC_PHASEMM) {
1147*4882a593Smuzhiyun 		/* target wants to do something different:
1148*4882a593Smuzhiyun 		   find out what it wants and do it. */
1149*4882a593Smuzhiyun 		phase_mismatch(ms);
1150*4882a593Smuzhiyun 	} else {
1151*4882a593Smuzhiyun 		printk(KERN_ERR "mesh: can't cope with exception %x\n", exc);
1152*4882a593Smuzhiyun 		mesh_dump_regs(ms);
1153*4882a593Smuzhiyun 		dumplog(ms, ms->conn_tgt);
1154*4882a593Smuzhiyun 		do_abort(ms);
1155*4882a593Smuzhiyun 		phase_mismatch(ms);
1156*4882a593Smuzhiyun 	}
1157*4882a593Smuzhiyun }
1158*4882a593Smuzhiyun 
handle_msgin(struct mesh_state * ms)1159*4882a593Smuzhiyun static void handle_msgin(struct mesh_state *ms)
1160*4882a593Smuzhiyun {
1161*4882a593Smuzhiyun 	int i, code;
1162*4882a593Smuzhiyun 	struct scsi_cmnd *cmd = ms->current_req;
1163*4882a593Smuzhiyun 	struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
1164*4882a593Smuzhiyun 
1165*4882a593Smuzhiyun 	if (ms->n_msgin == 0)
1166*4882a593Smuzhiyun 		return;
1167*4882a593Smuzhiyun 	code = ms->msgin[0];
1168*4882a593Smuzhiyun 	if (ALLOW_DEBUG(ms->conn_tgt)) {
1169*4882a593Smuzhiyun 		printk(KERN_DEBUG "got %d message bytes:", ms->n_msgin);
1170*4882a593Smuzhiyun 		for (i = 0; i < ms->n_msgin; ++i)
1171*4882a593Smuzhiyun 			printk(" %x", ms->msgin[i]);
1172*4882a593Smuzhiyun 		printk("\n");
1173*4882a593Smuzhiyun 	}
1174*4882a593Smuzhiyun 	dlog(ms, "msgin msg=%.8x",
1175*4882a593Smuzhiyun 	     MKWORD(ms->n_msgin, code, ms->msgin[1], ms->msgin[2]));
1176*4882a593Smuzhiyun 
1177*4882a593Smuzhiyun 	ms->expect_reply = 0;
1178*4882a593Smuzhiyun 	ms->n_msgout = 0;
1179*4882a593Smuzhiyun 	if (ms->n_msgin < msgin_length(ms))
1180*4882a593Smuzhiyun 		goto reject;
1181*4882a593Smuzhiyun 	if (cmd)
1182*4882a593Smuzhiyun 		cmd->SCp.Message = code;
1183*4882a593Smuzhiyun 	switch (code) {
1184*4882a593Smuzhiyun 	case COMMAND_COMPLETE:
1185*4882a593Smuzhiyun 		break;
1186*4882a593Smuzhiyun 	case EXTENDED_MESSAGE:
1187*4882a593Smuzhiyun 		switch (ms->msgin[2]) {
1188*4882a593Smuzhiyun 		case EXTENDED_MODIFY_DATA_POINTER:
1189*4882a593Smuzhiyun 			ms->data_ptr += (ms->msgin[3] << 24) + ms->msgin[6]
1190*4882a593Smuzhiyun 				+ (ms->msgin[4] << 16) + (ms->msgin[5] << 8);
1191*4882a593Smuzhiyun 			break;
1192*4882a593Smuzhiyun 		case EXTENDED_SDTR:
1193*4882a593Smuzhiyun 			if (tp->sdtr_state != sdtr_sent) {
1194*4882a593Smuzhiyun 				/* reply with an SDTR */
1195*4882a593Smuzhiyun 				add_sdtr_msg(ms);
1196*4882a593Smuzhiyun 				/* limit period to at least his value,
1197*4882a593Smuzhiyun 				   offset to no more than his */
1198*4882a593Smuzhiyun 				if (ms->msgout[3] < ms->msgin[3])
1199*4882a593Smuzhiyun 					ms->msgout[3] = ms->msgin[3];
1200*4882a593Smuzhiyun 				if (ms->msgout[4] > ms->msgin[4])
1201*4882a593Smuzhiyun 					ms->msgout[4] = ms->msgin[4];
1202*4882a593Smuzhiyun 				set_sdtr(ms, ms->msgout[3], ms->msgout[4]);
1203*4882a593Smuzhiyun 				ms->msgphase = msg_out;
1204*4882a593Smuzhiyun 			} else {
1205*4882a593Smuzhiyun 				set_sdtr(ms, ms->msgin[3], ms->msgin[4]);
1206*4882a593Smuzhiyun 			}
1207*4882a593Smuzhiyun 			break;
1208*4882a593Smuzhiyun 		default:
1209*4882a593Smuzhiyun 			goto reject;
1210*4882a593Smuzhiyun 		}
1211*4882a593Smuzhiyun 		break;
1212*4882a593Smuzhiyun 	case SAVE_POINTERS:
1213*4882a593Smuzhiyun 		tp->saved_ptr = ms->data_ptr;
1214*4882a593Smuzhiyun 		break;
1215*4882a593Smuzhiyun 	case RESTORE_POINTERS:
1216*4882a593Smuzhiyun 		ms->data_ptr = tp->saved_ptr;
1217*4882a593Smuzhiyun 		break;
1218*4882a593Smuzhiyun 	case DISCONNECT:
1219*4882a593Smuzhiyun 		ms->phase = disconnecting;
1220*4882a593Smuzhiyun 		break;
1221*4882a593Smuzhiyun 	case ABORT:
1222*4882a593Smuzhiyun 		break;
1223*4882a593Smuzhiyun 	case MESSAGE_REJECT:
1224*4882a593Smuzhiyun 		if (tp->sdtr_state == sdtr_sent)
1225*4882a593Smuzhiyun 			set_sdtr(ms, 0, 0);
1226*4882a593Smuzhiyun 		break;
1227*4882a593Smuzhiyun 	case NOP:
1228*4882a593Smuzhiyun 		break;
1229*4882a593Smuzhiyun 	default:
1230*4882a593Smuzhiyun 		if (IDENTIFY_BASE <= code && code <= IDENTIFY_BASE + 7) {
1231*4882a593Smuzhiyun 			if (cmd == NULL) {
1232*4882a593Smuzhiyun 				do_abort(ms);
1233*4882a593Smuzhiyun 				ms->msgphase = msg_out;
1234*4882a593Smuzhiyun 			} else if (code != cmd->device->lun + IDENTIFY_BASE) {
1235*4882a593Smuzhiyun 				printk(KERN_WARNING "mesh: lun mismatch "
1236*4882a593Smuzhiyun 				       "(%d != %llu) on reselection from "
1237*4882a593Smuzhiyun 				       "target %d\n", code - IDENTIFY_BASE,
1238*4882a593Smuzhiyun 				       cmd->device->lun, ms->conn_tgt);
1239*4882a593Smuzhiyun 			}
1240*4882a593Smuzhiyun 			break;
1241*4882a593Smuzhiyun 		}
1242*4882a593Smuzhiyun 		goto reject;
1243*4882a593Smuzhiyun 	}
1244*4882a593Smuzhiyun 	return;
1245*4882a593Smuzhiyun 
1246*4882a593Smuzhiyun  reject:
1247*4882a593Smuzhiyun 	printk(KERN_WARNING "mesh: rejecting message from target %d:",
1248*4882a593Smuzhiyun 	       ms->conn_tgt);
1249*4882a593Smuzhiyun 	for (i = 0; i < ms->n_msgin; ++i)
1250*4882a593Smuzhiyun 		printk(" %x", ms->msgin[i]);
1251*4882a593Smuzhiyun 	printk("\n");
1252*4882a593Smuzhiyun 	ms->msgout[0] = MESSAGE_REJECT;
1253*4882a593Smuzhiyun 	ms->n_msgout = 1;
1254*4882a593Smuzhiyun 	ms->msgphase = msg_out;
1255*4882a593Smuzhiyun }
1256*4882a593Smuzhiyun 
1257*4882a593Smuzhiyun /*
1258*4882a593Smuzhiyun  * Set up DMA commands for transferring data.
1259*4882a593Smuzhiyun  */
set_dma_cmds(struct mesh_state * ms,struct scsi_cmnd * cmd)1260*4882a593Smuzhiyun static void set_dma_cmds(struct mesh_state *ms, struct scsi_cmnd *cmd)
1261*4882a593Smuzhiyun {
1262*4882a593Smuzhiyun 	int i, dma_cmd, total, off, dtot;
1263*4882a593Smuzhiyun 	struct scatterlist *scl;
1264*4882a593Smuzhiyun 	struct dbdma_cmd *dcmds;
1265*4882a593Smuzhiyun 
1266*4882a593Smuzhiyun 	dma_cmd = ms->tgts[ms->conn_tgt].data_goes_out?
1267*4882a593Smuzhiyun 		OUTPUT_MORE: INPUT_MORE;
1268*4882a593Smuzhiyun 	dcmds = ms->dma_cmds;
1269*4882a593Smuzhiyun 	dtot = 0;
1270*4882a593Smuzhiyun 	if (cmd) {
1271*4882a593Smuzhiyun 		int nseg;
1272*4882a593Smuzhiyun 
1273*4882a593Smuzhiyun 		cmd->SCp.this_residual = scsi_bufflen(cmd);
1274*4882a593Smuzhiyun 
1275*4882a593Smuzhiyun 		nseg = scsi_dma_map(cmd);
1276*4882a593Smuzhiyun 		BUG_ON(nseg < 0);
1277*4882a593Smuzhiyun 
1278*4882a593Smuzhiyun 		if (nseg) {
1279*4882a593Smuzhiyun 			total = 0;
1280*4882a593Smuzhiyun 			off = ms->data_ptr;
1281*4882a593Smuzhiyun 
1282*4882a593Smuzhiyun 			scsi_for_each_sg(cmd, scl, nseg, i) {
1283*4882a593Smuzhiyun 				u32 dma_addr = sg_dma_address(scl);
1284*4882a593Smuzhiyun 				u32 dma_len = sg_dma_len(scl);
1285*4882a593Smuzhiyun 
1286*4882a593Smuzhiyun 				total += scl->length;
1287*4882a593Smuzhiyun 				if (off >= dma_len) {
1288*4882a593Smuzhiyun 					off -= dma_len;
1289*4882a593Smuzhiyun 					continue;
1290*4882a593Smuzhiyun 				}
1291*4882a593Smuzhiyun 				if (dma_len > 0xffff)
1292*4882a593Smuzhiyun 					panic("mesh: scatterlist element >= 64k");
1293*4882a593Smuzhiyun 				dcmds->req_count = cpu_to_le16(dma_len - off);
1294*4882a593Smuzhiyun 				dcmds->command = cpu_to_le16(dma_cmd);
1295*4882a593Smuzhiyun 				dcmds->phy_addr = cpu_to_le32(dma_addr + off);
1296*4882a593Smuzhiyun 				dcmds->xfer_status = 0;
1297*4882a593Smuzhiyun 				++dcmds;
1298*4882a593Smuzhiyun 				dtot += dma_len - off;
1299*4882a593Smuzhiyun 				off = 0;
1300*4882a593Smuzhiyun 			}
1301*4882a593Smuzhiyun 		}
1302*4882a593Smuzhiyun 	}
1303*4882a593Smuzhiyun 	if (dtot == 0) {
1304*4882a593Smuzhiyun 		/* Either the target has overrun our buffer,
1305*4882a593Smuzhiyun 		   or the caller didn't provide a buffer. */
1306*4882a593Smuzhiyun 		static char mesh_extra_buf[64];
1307*4882a593Smuzhiyun 
1308*4882a593Smuzhiyun 		dtot = sizeof(mesh_extra_buf);
1309*4882a593Smuzhiyun 		dcmds->req_count = cpu_to_le16(dtot);
1310*4882a593Smuzhiyun 		dcmds->phy_addr = cpu_to_le32(virt_to_phys(mesh_extra_buf));
1311*4882a593Smuzhiyun 		dcmds->xfer_status = 0;
1312*4882a593Smuzhiyun 		++dcmds;
1313*4882a593Smuzhiyun 	}
1314*4882a593Smuzhiyun 	dma_cmd += OUTPUT_LAST - OUTPUT_MORE;
1315*4882a593Smuzhiyun 	dcmds[-1].command = cpu_to_le16(dma_cmd);
1316*4882a593Smuzhiyun 	memset(dcmds, 0, sizeof(*dcmds));
1317*4882a593Smuzhiyun 	dcmds->command = cpu_to_le16(DBDMA_STOP);
1318*4882a593Smuzhiyun 	ms->dma_count = dtot;
1319*4882a593Smuzhiyun }
1320*4882a593Smuzhiyun 
halt_dma(struct mesh_state * ms)1321*4882a593Smuzhiyun static void halt_dma(struct mesh_state *ms)
1322*4882a593Smuzhiyun {
1323*4882a593Smuzhiyun 	volatile struct dbdma_regs __iomem *md = ms->dma;
1324*4882a593Smuzhiyun 	volatile struct mesh_regs __iomem *mr = ms->mesh;
1325*4882a593Smuzhiyun 	struct scsi_cmnd *cmd = ms->current_req;
1326*4882a593Smuzhiyun 	int t, nb;
1327*4882a593Smuzhiyun 
1328*4882a593Smuzhiyun 	if (!ms->tgts[ms->conn_tgt].data_goes_out) {
1329*4882a593Smuzhiyun 		/* wait a little while until the fifo drains */
1330*4882a593Smuzhiyun 		t = 50;
1331*4882a593Smuzhiyun 		while (t > 0 && in_8(&mr->fifo_count) != 0
1332*4882a593Smuzhiyun 		       && (in_le32(&md->status) & ACTIVE) != 0) {
1333*4882a593Smuzhiyun 			--t;
1334*4882a593Smuzhiyun 			udelay(1);
1335*4882a593Smuzhiyun 		}
1336*4882a593Smuzhiyun 	}
1337*4882a593Smuzhiyun 	out_le32(&md->control, RUN << 16);	/* turn off RUN bit */
1338*4882a593Smuzhiyun 	nb = (mr->count_hi << 8) + mr->count_lo;
1339*4882a593Smuzhiyun 	dlog(ms, "halt_dma fc/count=%.6x",
1340*4882a593Smuzhiyun 	     MKWORD(0, mr->fifo_count, 0, nb));
1341*4882a593Smuzhiyun 	if (ms->tgts[ms->conn_tgt].data_goes_out)
1342*4882a593Smuzhiyun 		nb += mr->fifo_count;
1343*4882a593Smuzhiyun 	/* nb is the number of bytes not yet transferred
1344*4882a593Smuzhiyun 	   to/from the target. */
1345*4882a593Smuzhiyun 	ms->data_ptr -= nb;
1346*4882a593Smuzhiyun 	dlog(ms, "data_ptr %x", ms->data_ptr);
1347*4882a593Smuzhiyun 	if (ms->data_ptr < 0) {
1348*4882a593Smuzhiyun 		printk(KERN_ERR "mesh: halt_dma: data_ptr=%d (nb=%d, ms=%p)\n",
1349*4882a593Smuzhiyun 		       ms->data_ptr, nb, ms);
1350*4882a593Smuzhiyun 		ms->data_ptr = 0;
1351*4882a593Smuzhiyun #ifdef MESH_DBG
1352*4882a593Smuzhiyun 		dumplog(ms, ms->conn_tgt);
1353*4882a593Smuzhiyun 		dumpslog(ms);
1354*4882a593Smuzhiyun #endif /* MESH_DBG */
1355*4882a593Smuzhiyun 	} else if (cmd && scsi_bufflen(cmd) &&
1356*4882a593Smuzhiyun 		   ms->data_ptr > scsi_bufflen(cmd)) {
1357*4882a593Smuzhiyun 		printk(KERN_DEBUG "mesh: target %d overrun, "
1358*4882a593Smuzhiyun 		       "data_ptr=%x total=%x goes_out=%d\n",
1359*4882a593Smuzhiyun 		       ms->conn_tgt, ms->data_ptr, scsi_bufflen(cmd),
1360*4882a593Smuzhiyun 		       ms->tgts[ms->conn_tgt].data_goes_out);
1361*4882a593Smuzhiyun 	}
1362*4882a593Smuzhiyun 	if (cmd)
1363*4882a593Smuzhiyun 		scsi_dma_unmap(cmd);
1364*4882a593Smuzhiyun 	ms->dma_started = 0;
1365*4882a593Smuzhiyun }
1366*4882a593Smuzhiyun 
phase_mismatch(struct mesh_state * ms)1367*4882a593Smuzhiyun static void phase_mismatch(struct mesh_state *ms)
1368*4882a593Smuzhiyun {
1369*4882a593Smuzhiyun 	volatile struct mesh_regs __iomem *mr = ms->mesh;
1370*4882a593Smuzhiyun 	int phase;
1371*4882a593Smuzhiyun 
1372*4882a593Smuzhiyun 	dlog(ms, "phasemm ch/cl/seq/fc=%.8x",
1373*4882a593Smuzhiyun 	     MKWORD(mr->count_hi, mr->count_lo, mr->sequence, mr->fifo_count));
1374*4882a593Smuzhiyun 	phase = in_8(&mr->bus_status0) & BS0_PHASE;
1375*4882a593Smuzhiyun 	if (ms->msgphase == msg_out_xxx && phase == BP_MSGOUT) {
1376*4882a593Smuzhiyun 		/* output the last byte of the message, without ATN */
1377*4882a593Smuzhiyun 		out_8(&mr->count_lo, 1);
1378*4882a593Smuzhiyun 		out_8(&mr->sequence, SEQ_MSGOUT + use_active_neg);
1379*4882a593Smuzhiyun 		mesh_flush_io(mr);
1380*4882a593Smuzhiyun 		udelay(1);
1381*4882a593Smuzhiyun 		out_8(&mr->fifo, ms->msgout[ms->n_msgout-1]);
1382*4882a593Smuzhiyun 		ms->msgphase = msg_out_last;
1383*4882a593Smuzhiyun 		return;
1384*4882a593Smuzhiyun 	}
1385*4882a593Smuzhiyun 
1386*4882a593Smuzhiyun 	if (ms->msgphase == msg_in) {
1387*4882a593Smuzhiyun 		get_msgin(ms);
1388*4882a593Smuzhiyun 		if (ms->n_msgin)
1389*4882a593Smuzhiyun 			handle_msgin(ms);
1390*4882a593Smuzhiyun 	}
1391*4882a593Smuzhiyun 
1392*4882a593Smuzhiyun 	if (ms->dma_started)
1393*4882a593Smuzhiyun 		halt_dma(ms);
1394*4882a593Smuzhiyun 	if (mr->fifo_count) {
1395*4882a593Smuzhiyun 		out_8(&mr->sequence, SEQ_FLUSHFIFO);
1396*4882a593Smuzhiyun 		mesh_flush_io(mr);
1397*4882a593Smuzhiyun 		udelay(1);
1398*4882a593Smuzhiyun 	}
1399*4882a593Smuzhiyun 
1400*4882a593Smuzhiyun 	ms->msgphase = msg_none;
1401*4882a593Smuzhiyun 	switch (phase) {
1402*4882a593Smuzhiyun 	case BP_DATAIN:
1403*4882a593Smuzhiyun 		ms->tgts[ms->conn_tgt].data_goes_out = 0;
1404*4882a593Smuzhiyun 		ms->phase = dataing;
1405*4882a593Smuzhiyun 		break;
1406*4882a593Smuzhiyun 	case BP_DATAOUT:
1407*4882a593Smuzhiyun 		ms->tgts[ms->conn_tgt].data_goes_out = 1;
1408*4882a593Smuzhiyun 		ms->phase = dataing;
1409*4882a593Smuzhiyun 		break;
1410*4882a593Smuzhiyun 	case BP_COMMAND:
1411*4882a593Smuzhiyun 		ms->phase = commanding;
1412*4882a593Smuzhiyun 		break;
1413*4882a593Smuzhiyun 	case BP_STATUS:
1414*4882a593Smuzhiyun 		ms->phase = statusing;
1415*4882a593Smuzhiyun 		break;
1416*4882a593Smuzhiyun 	case BP_MSGIN:
1417*4882a593Smuzhiyun 		ms->msgphase = msg_in;
1418*4882a593Smuzhiyun 		ms->n_msgin = 0;
1419*4882a593Smuzhiyun 		break;
1420*4882a593Smuzhiyun 	case BP_MSGOUT:
1421*4882a593Smuzhiyun 		ms->msgphase = msg_out;
1422*4882a593Smuzhiyun 		if (ms->n_msgout == 0) {
1423*4882a593Smuzhiyun 			if (ms->aborting) {
1424*4882a593Smuzhiyun 				do_abort(ms);
1425*4882a593Smuzhiyun 			} else {
1426*4882a593Smuzhiyun 				if (ms->last_n_msgout == 0) {
1427*4882a593Smuzhiyun 					printk(KERN_DEBUG
1428*4882a593Smuzhiyun 					       "mesh: no msg to repeat\n");
1429*4882a593Smuzhiyun 					ms->msgout[0] = NOP;
1430*4882a593Smuzhiyun 					ms->last_n_msgout = 1;
1431*4882a593Smuzhiyun 				}
1432*4882a593Smuzhiyun 				ms->n_msgout = ms->last_n_msgout;
1433*4882a593Smuzhiyun 			}
1434*4882a593Smuzhiyun 		}
1435*4882a593Smuzhiyun 		break;
1436*4882a593Smuzhiyun 	default:
1437*4882a593Smuzhiyun 		printk(KERN_DEBUG "mesh: unknown scsi phase %x\n", phase);
1438*4882a593Smuzhiyun 		ms->stat = DID_ERROR;
1439*4882a593Smuzhiyun 		mesh_done(ms, 1);
1440*4882a593Smuzhiyun 		return;
1441*4882a593Smuzhiyun 	}
1442*4882a593Smuzhiyun 
1443*4882a593Smuzhiyun 	start_phase(ms);
1444*4882a593Smuzhiyun }
1445*4882a593Smuzhiyun 
cmd_complete(struct mesh_state * ms)1446*4882a593Smuzhiyun static void cmd_complete(struct mesh_state *ms)
1447*4882a593Smuzhiyun {
1448*4882a593Smuzhiyun 	volatile struct mesh_regs __iomem *mr = ms->mesh;
1449*4882a593Smuzhiyun 	struct scsi_cmnd *cmd = ms->current_req;
1450*4882a593Smuzhiyun 	struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
1451*4882a593Smuzhiyun 	int seq, n, t;
1452*4882a593Smuzhiyun 
1453*4882a593Smuzhiyun 	dlog(ms, "cmd_complete fc=%x", mr->fifo_count);
1454*4882a593Smuzhiyun 	seq = use_active_neg + (ms->n_msgout? SEQ_ATN: 0);
1455*4882a593Smuzhiyun 	switch (ms->msgphase) {
1456*4882a593Smuzhiyun 	case msg_out_xxx:
1457*4882a593Smuzhiyun 		/* huh?  we expected a phase mismatch */
1458*4882a593Smuzhiyun 		ms->n_msgin = 0;
1459*4882a593Smuzhiyun 		ms->msgphase = msg_in;
1460*4882a593Smuzhiyun 		fallthrough;
1461*4882a593Smuzhiyun 
1462*4882a593Smuzhiyun 	case msg_in:
1463*4882a593Smuzhiyun 		/* should have some message bytes in fifo */
1464*4882a593Smuzhiyun 		get_msgin(ms);
1465*4882a593Smuzhiyun 		n = msgin_length(ms);
1466*4882a593Smuzhiyun 		if (ms->n_msgin < n) {
1467*4882a593Smuzhiyun 			out_8(&mr->count_lo, n - ms->n_msgin);
1468*4882a593Smuzhiyun 			out_8(&mr->sequence, SEQ_MSGIN + seq);
1469*4882a593Smuzhiyun 		} else {
1470*4882a593Smuzhiyun 			ms->msgphase = msg_none;
1471*4882a593Smuzhiyun 			handle_msgin(ms);
1472*4882a593Smuzhiyun 			start_phase(ms);
1473*4882a593Smuzhiyun 		}
1474*4882a593Smuzhiyun 		break;
1475*4882a593Smuzhiyun 
1476*4882a593Smuzhiyun 	case msg_in_bad:
1477*4882a593Smuzhiyun 		out_8(&mr->sequence, SEQ_FLUSHFIFO);
1478*4882a593Smuzhiyun 		mesh_flush_io(mr);
1479*4882a593Smuzhiyun 		udelay(1);
1480*4882a593Smuzhiyun 		out_8(&mr->count_lo, 1);
1481*4882a593Smuzhiyun 		out_8(&mr->sequence, SEQ_MSGIN + SEQ_ATN + use_active_neg);
1482*4882a593Smuzhiyun 		break;
1483*4882a593Smuzhiyun 
1484*4882a593Smuzhiyun 	case msg_out:
1485*4882a593Smuzhiyun 		/*
1486*4882a593Smuzhiyun 		 * To get the right timing on ATN wrt ACK, we have
1487*4882a593Smuzhiyun 		 * to get the MESH to drop ACK, wait until REQ gets
1488*4882a593Smuzhiyun 		 * asserted, then drop ATN.  To do this we first
1489*4882a593Smuzhiyun 		 * issue a SEQ_MSGOUT with ATN and wait for REQ,
1490*4882a593Smuzhiyun 		 * then change the command to a SEQ_MSGOUT w/o ATN.
1491*4882a593Smuzhiyun 		 * If we don't see REQ in a reasonable time, we
1492*4882a593Smuzhiyun 		 * change the command to SEQ_MSGIN with ATN,
1493*4882a593Smuzhiyun 		 * wait for the phase mismatch interrupt, then
1494*4882a593Smuzhiyun 		 * issue the SEQ_MSGOUT without ATN.
1495*4882a593Smuzhiyun 		 */
1496*4882a593Smuzhiyun 		out_8(&mr->count_lo, 1);
1497*4882a593Smuzhiyun 		out_8(&mr->sequence, SEQ_MSGOUT + use_active_neg + SEQ_ATN);
1498*4882a593Smuzhiyun 		t = 30;		/* wait up to 30us */
1499*4882a593Smuzhiyun 		while ((in_8(&mr->bus_status0) & BS0_REQ) == 0 && --t >= 0)
1500*4882a593Smuzhiyun 			udelay(1);
1501*4882a593Smuzhiyun 		dlog(ms, "last_mbyte err/exc/fc/cl=%.8x",
1502*4882a593Smuzhiyun 		     MKWORD(mr->error, mr->exception,
1503*4882a593Smuzhiyun 			    mr->fifo_count, mr->count_lo));
1504*4882a593Smuzhiyun 		if (in_8(&mr->interrupt) & (INT_ERROR | INT_EXCEPTION)) {
1505*4882a593Smuzhiyun 			/* whoops, target didn't do what we expected */
1506*4882a593Smuzhiyun 			ms->last_n_msgout = ms->n_msgout;
1507*4882a593Smuzhiyun 			ms->n_msgout = 0;
1508*4882a593Smuzhiyun 			if (in_8(&mr->interrupt) & INT_ERROR) {
1509*4882a593Smuzhiyun 				printk(KERN_ERR "mesh: error %x in msg_out\n",
1510*4882a593Smuzhiyun 				       in_8(&mr->error));
1511*4882a593Smuzhiyun 				handle_error(ms);
1512*4882a593Smuzhiyun 				return;
1513*4882a593Smuzhiyun 			}
1514*4882a593Smuzhiyun 			if (in_8(&mr->exception) != EXC_PHASEMM)
1515*4882a593Smuzhiyun 				printk(KERN_ERR "mesh: exc %x in msg_out\n",
1516*4882a593Smuzhiyun 				       in_8(&mr->exception));
1517*4882a593Smuzhiyun 			else
1518*4882a593Smuzhiyun 				printk(KERN_DEBUG "mesh: bs0=%x in msg_out\n",
1519*4882a593Smuzhiyun 				       in_8(&mr->bus_status0));
1520*4882a593Smuzhiyun 			handle_exception(ms);
1521*4882a593Smuzhiyun 			return;
1522*4882a593Smuzhiyun 		}
1523*4882a593Smuzhiyun 		if (in_8(&mr->bus_status0) & BS0_REQ) {
1524*4882a593Smuzhiyun 			out_8(&mr->sequence, SEQ_MSGOUT + use_active_neg);
1525*4882a593Smuzhiyun 			mesh_flush_io(mr);
1526*4882a593Smuzhiyun 			udelay(1);
1527*4882a593Smuzhiyun 			out_8(&mr->fifo, ms->msgout[ms->n_msgout-1]);
1528*4882a593Smuzhiyun 			ms->msgphase = msg_out_last;
1529*4882a593Smuzhiyun 		} else {
1530*4882a593Smuzhiyun 			out_8(&mr->sequence, SEQ_MSGIN + use_active_neg + SEQ_ATN);
1531*4882a593Smuzhiyun 			ms->msgphase = msg_out_xxx;
1532*4882a593Smuzhiyun 		}
1533*4882a593Smuzhiyun 		break;
1534*4882a593Smuzhiyun 
1535*4882a593Smuzhiyun 	case msg_out_last:
1536*4882a593Smuzhiyun 		ms->last_n_msgout = ms->n_msgout;
1537*4882a593Smuzhiyun 		ms->n_msgout = 0;
1538*4882a593Smuzhiyun 		ms->msgphase = ms->expect_reply? msg_in: msg_none;
1539*4882a593Smuzhiyun 		start_phase(ms);
1540*4882a593Smuzhiyun 		break;
1541*4882a593Smuzhiyun 
1542*4882a593Smuzhiyun 	case msg_none:
1543*4882a593Smuzhiyun 		switch (ms->phase) {
1544*4882a593Smuzhiyun 		case idle:
1545*4882a593Smuzhiyun 			printk(KERN_ERR "mesh: interrupt in idle phase?\n");
1546*4882a593Smuzhiyun 			dumpslog(ms);
1547*4882a593Smuzhiyun 			return;
1548*4882a593Smuzhiyun 		case selecting:
1549*4882a593Smuzhiyun 			dlog(ms, "Selecting phase at command completion",0);
1550*4882a593Smuzhiyun 			ms->msgout[0] = IDENTIFY(ALLOW_RESEL(ms->conn_tgt),
1551*4882a593Smuzhiyun 						 (cmd? cmd->device->lun: 0));
1552*4882a593Smuzhiyun 			ms->n_msgout = 1;
1553*4882a593Smuzhiyun 			ms->expect_reply = 0;
1554*4882a593Smuzhiyun 			if (ms->aborting) {
1555*4882a593Smuzhiyun 				ms->msgout[0] = ABORT;
1556*4882a593Smuzhiyun 				ms->n_msgout++;
1557*4882a593Smuzhiyun 			} else if (tp->sdtr_state == do_sdtr) {
1558*4882a593Smuzhiyun 				/* add SDTR message */
1559*4882a593Smuzhiyun 				add_sdtr_msg(ms);
1560*4882a593Smuzhiyun 				ms->expect_reply = 1;
1561*4882a593Smuzhiyun 				tp->sdtr_state = sdtr_sent;
1562*4882a593Smuzhiyun 			}
1563*4882a593Smuzhiyun 			ms->msgphase = msg_out;
1564*4882a593Smuzhiyun 			/*
1565*4882a593Smuzhiyun 			 * We need to wait for REQ before dropping ATN.
1566*4882a593Smuzhiyun 			 * We wait for at most 30us, then fall back to
1567*4882a593Smuzhiyun 			 * a scheme where we issue a SEQ_COMMAND with ATN,
1568*4882a593Smuzhiyun 			 * which will give us a phase mismatch interrupt
1569*4882a593Smuzhiyun 			 * when REQ does come, and then we send the message.
1570*4882a593Smuzhiyun 			 */
1571*4882a593Smuzhiyun 			t = 230;		/* wait up to 230us */
1572*4882a593Smuzhiyun 			while ((in_8(&mr->bus_status0) & BS0_REQ) == 0) {
1573*4882a593Smuzhiyun 				if (--t < 0) {
1574*4882a593Smuzhiyun 					dlog(ms, "impatient for req", ms->n_msgout);
1575*4882a593Smuzhiyun 					ms->msgphase = msg_none;
1576*4882a593Smuzhiyun 					break;
1577*4882a593Smuzhiyun 				}
1578*4882a593Smuzhiyun 				udelay(1);
1579*4882a593Smuzhiyun 			}
1580*4882a593Smuzhiyun 			break;
1581*4882a593Smuzhiyun 		case dataing:
1582*4882a593Smuzhiyun 			if (ms->dma_count != 0) {
1583*4882a593Smuzhiyun 				start_phase(ms);
1584*4882a593Smuzhiyun 				return;
1585*4882a593Smuzhiyun 			}
1586*4882a593Smuzhiyun 			/*
1587*4882a593Smuzhiyun 			 * We can get a phase mismatch here if the target
1588*4882a593Smuzhiyun 			 * changes to the status phase, even though we have
1589*4882a593Smuzhiyun 			 * had a command complete interrupt.  Then, if we
1590*4882a593Smuzhiyun 			 * issue the SEQ_STATUS command, we'll get a sequence
1591*4882a593Smuzhiyun 			 * error interrupt.  Which isn't so bad except that
1592*4882a593Smuzhiyun 			 * occasionally the mesh actually executes the
1593*4882a593Smuzhiyun 			 * SEQ_STATUS *as well as* giving us the sequence
1594*4882a593Smuzhiyun 			 * error and phase mismatch exception.
1595*4882a593Smuzhiyun 			 */
1596*4882a593Smuzhiyun 			out_8(&mr->sequence, 0);
1597*4882a593Smuzhiyun 			out_8(&mr->interrupt,
1598*4882a593Smuzhiyun 			      INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
1599*4882a593Smuzhiyun 			halt_dma(ms);
1600*4882a593Smuzhiyun 			break;
1601*4882a593Smuzhiyun 		case statusing:
1602*4882a593Smuzhiyun 			if (cmd) {
1603*4882a593Smuzhiyun 				cmd->SCp.Status = mr->fifo;
1604*4882a593Smuzhiyun 				if (DEBUG_TARGET(cmd))
1605*4882a593Smuzhiyun 					printk(KERN_DEBUG "mesh: status is %x\n",
1606*4882a593Smuzhiyun 					       cmd->SCp.Status);
1607*4882a593Smuzhiyun 			}
1608*4882a593Smuzhiyun 			ms->msgphase = msg_in;
1609*4882a593Smuzhiyun 			break;
1610*4882a593Smuzhiyun 		case busfreeing:
1611*4882a593Smuzhiyun 			mesh_done(ms, 1);
1612*4882a593Smuzhiyun 			return;
1613*4882a593Smuzhiyun 		case disconnecting:
1614*4882a593Smuzhiyun 			ms->current_req = NULL;
1615*4882a593Smuzhiyun 			ms->phase = idle;
1616*4882a593Smuzhiyun 			mesh_start(ms);
1617*4882a593Smuzhiyun 			return;
1618*4882a593Smuzhiyun 		default:
1619*4882a593Smuzhiyun 			break;
1620*4882a593Smuzhiyun 		}
1621*4882a593Smuzhiyun 		++ms->phase;
1622*4882a593Smuzhiyun 		start_phase(ms);
1623*4882a593Smuzhiyun 		break;
1624*4882a593Smuzhiyun 	}
1625*4882a593Smuzhiyun }
1626*4882a593Smuzhiyun 
1627*4882a593Smuzhiyun 
1628*4882a593Smuzhiyun /*
1629*4882a593Smuzhiyun  * Called by midlayer with host locked to queue a new
1630*4882a593Smuzhiyun  * request
1631*4882a593Smuzhiyun  */
mesh_queue_lck(struct scsi_cmnd * cmd,void (* done)(struct scsi_cmnd *))1632*4882a593Smuzhiyun static int mesh_queue_lck(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
1633*4882a593Smuzhiyun {
1634*4882a593Smuzhiyun 	struct mesh_state *ms;
1635*4882a593Smuzhiyun 
1636*4882a593Smuzhiyun 	cmd->scsi_done = done;
1637*4882a593Smuzhiyun 	cmd->host_scribble = NULL;
1638*4882a593Smuzhiyun 
1639*4882a593Smuzhiyun 	ms = (struct mesh_state *) cmd->device->host->hostdata;
1640*4882a593Smuzhiyun 
1641*4882a593Smuzhiyun 	if (ms->request_q == NULL)
1642*4882a593Smuzhiyun 		ms->request_q = cmd;
1643*4882a593Smuzhiyun 	else
1644*4882a593Smuzhiyun 		ms->request_qtail->host_scribble = (void *) cmd;
1645*4882a593Smuzhiyun 	ms->request_qtail = cmd;
1646*4882a593Smuzhiyun 
1647*4882a593Smuzhiyun 	if (ms->phase == idle)
1648*4882a593Smuzhiyun 		mesh_start(ms);
1649*4882a593Smuzhiyun 
1650*4882a593Smuzhiyun 	return 0;
1651*4882a593Smuzhiyun }
1652*4882a593Smuzhiyun 
DEF_SCSI_QCMD(mesh_queue)1653*4882a593Smuzhiyun static DEF_SCSI_QCMD(mesh_queue)
1654*4882a593Smuzhiyun 
1655*4882a593Smuzhiyun /*
1656*4882a593Smuzhiyun  * Called to handle interrupts, either call by the interrupt
1657*4882a593Smuzhiyun  * handler (do_mesh_interrupt) or by other functions in
1658*4882a593Smuzhiyun  * exceptional circumstances
1659*4882a593Smuzhiyun  */
1660*4882a593Smuzhiyun static void mesh_interrupt(struct mesh_state *ms)
1661*4882a593Smuzhiyun {
1662*4882a593Smuzhiyun 	volatile struct mesh_regs __iomem *mr = ms->mesh;
1663*4882a593Smuzhiyun 	int intr;
1664*4882a593Smuzhiyun 
1665*4882a593Smuzhiyun #if 0
1666*4882a593Smuzhiyun 	if (ALLOW_DEBUG(ms->conn_tgt))
1667*4882a593Smuzhiyun 		printk(KERN_DEBUG "mesh_intr, bs0=%x int=%x exc=%x err=%x "
1668*4882a593Smuzhiyun 		       "phase=%d msgphase=%d\n", mr->bus_status0,
1669*4882a593Smuzhiyun 		       mr->interrupt, mr->exception, mr->error,
1670*4882a593Smuzhiyun 		       ms->phase, ms->msgphase);
1671*4882a593Smuzhiyun #endif
1672*4882a593Smuzhiyun 	while ((intr = in_8(&mr->interrupt)) != 0) {
1673*4882a593Smuzhiyun 		dlog(ms, "interrupt intr/err/exc/seq=%.8x",
1674*4882a593Smuzhiyun 		     MKWORD(intr, mr->error, mr->exception, mr->sequence));
1675*4882a593Smuzhiyun 		if (intr & INT_ERROR) {
1676*4882a593Smuzhiyun 			handle_error(ms);
1677*4882a593Smuzhiyun 		} else if (intr & INT_EXCEPTION) {
1678*4882a593Smuzhiyun 			handle_exception(ms);
1679*4882a593Smuzhiyun 		} else if (intr & INT_CMDDONE) {
1680*4882a593Smuzhiyun 			out_8(&mr->interrupt, INT_CMDDONE);
1681*4882a593Smuzhiyun 			cmd_complete(ms);
1682*4882a593Smuzhiyun 		}
1683*4882a593Smuzhiyun 	}
1684*4882a593Smuzhiyun }
1685*4882a593Smuzhiyun 
1686*4882a593Smuzhiyun /* Todo: here we can at least try to remove the command from the
1687*4882a593Smuzhiyun  * queue if it isn't connected yet, and for pending command, assert
1688*4882a593Smuzhiyun  * ATN until the bus gets freed.
1689*4882a593Smuzhiyun  */
mesh_abort(struct scsi_cmnd * cmd)1690*4882a593Smuzhiyun static int mesh_abort(struct scsi_cmnd *cmd)
1691*4882a593Smuzhiyun {
1692*4882a593Smuzhiyun 	struct mesh_state *ms = (struct mesh_state *) cmd->device->host->hostdata;
1693*4882a593Smuzhiyun 
1694*4882a593Smuzhiyun 	printk(KERN_DEBUG "mesh_abort(%p)\n", cmd);
1695*4882a593Smuzhiyun 	mesh_dump_regs(ms);
1696*4882a593Smuzhiyun 	dumplog(ms, cmd->device->id);
1697*4882a593Smuzhiyun 	dumpslog(ms);
1698*4882a593Smuzhiyun 	return FAILED;
1699*4882a593Smuzhiyun }
1700*4882a593Smuzhiyun 
1701*4882a593Smuzhiyun /*
1702*4882a593Smuzhiyun  * Called by the midlayer with the lock held to reset the
1703*4882a593Smuzhiyun  * SCSI host and bus.
1704*4882a593Smuzhiyun  * The midlayer will wait for devices to come back, we don't need
1705*4882a593Smuzhiyun  * to do that ourselves
1706*4882a593Smuzhiyun  */
mesh_host_reset(struct scsi_cmnd * cmd)1707*4882a593Smuzhiyun static int mesh_host_reset(struct scsi_cmnd *cmd)
1708*4882a593Smuzhiyun {
1709*4882a593Smuzhiyun 	struct mesh_state *ms = (struct mesh_state *) cmd->device->host->hostdata;
1710*4882a593Smuzhiyun 	volatile struct mesh_regs __iomem *mr = ms->mesh;
1711*4882a593Smuzhiyun 	volatile struct dbdma_regs __iomem *md = ms->dma;
1712*4882a593Smuzhiyun 	unsigned long flags;
1713*4882a593Smuzhiyun 
1714*4882a593Smuzhiyun 	printk(KERN_DEBUG "mesh_host_reset\n");
1715*4882a593Smuzhiyun 
1716*4882a593Smuzhiyun 	spin_lock_irqsave(ms->host->host_lock, flags);
1717*4882a593Smuzhiyun 
1718*4882a593Smuzhiyun 	if (ms->dma_started)
1719*4882a593Smuzhiyun 		halt_dma(ms);
1720*4882a593Smuzhiyun 
1721*4882a593Smuzhiyun 	/* Reset the controller & dbdma channel */
1722*4882a593Smuzhiyun 	out_le32(&md->control, (RUN|PAUSE|FLUSH|WAKE) << 16);	/* stop dma */
1723*4882a593Smuzhiyun 	out_8(&mr->exception, 0xff);	/* clear all exception bits */
1724*4882a593Smuzhiyun 	out_8(&mr->error, 0xff);	/* clear all error bits */
1725*4882a593Smuzhiyun 	out_8(&mr->sequence, SEQ_RESETMESH);
1726*4882a593Smuzhiyun        	mesh_flush_io(mr);
1727*4882a593Smuzhiyun 	udelay(1);
1728*4882a593Smuzhiyun 	out_8(&mr->intr_mask, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
1729*4882a593Smuzhiyun 	out_8(&mr->source_id, ms->host->this_id);
1730*4882a593Smuzhiyun 	out_8(&mr->sel_timeout, 25);	/* 250ms */
1731*4882a593Smuzhiyun 	out_8(&mr->sync_params, ASYNC_PARAMS);
1732*4882a593Smuzhiyun 
1733*4882a593Smuzhiyun 	/* Reset the bus */
1734*4882a593Smuzhiyun 	out_8(&mr->bus_status1, BS1_RST);	/* assert RST */
1735*4882a593Smuzhiyun        	mesh_flush_io(mr);
1736*4882a593Smuzhiyun 	udelay(30);			/* leave it on for >= 25us */
1737*4882a593Smuzhiyun 	out_8(&mr->bus_status1, 0);	/* negate RST */
1738*4882a593Smuzhiyun 
1739*4882a593Smuzhiyun 	/* Complete pending commands */
1740*4882a593Smuzhiyun 	handle_reset(ms);
1741*4882a593Smuzhiyun 
1742*4882a593Smuzhiyun 	spin_unlock_irqrestore(ms->host->host_lock, flags);
1743*4882a593Smuzhiyun 	return SUCCESS;
1744*4882a593Smuzhiyun }
1745*4882a593Smuzhiyun 
set_mesh_power(struct mesh_state * ms,int state)1746*4882a593Smuzhiyun static void set_mesh_power(struct mesh_state *ms, int state)
1747*4882a593Smuzhiyun {
1748*4882a593Smuzhiyun 	if (!machine_is(powermac))
1749*4882a593Smuzhiyun 		return;
1750*4882a593Smuzhiyun 	if (state) {
1751*4882a593Smuzhiyun 		pmac_call_feature(PMAC_FTR_MESH_ENABLE, macio_get_of_node(ms->mdev), 0, 1);
1752*4882a593Smuzhiyun 		msleep(200);
1753*4882a593Smuzhiyun 	} else {
1754*4882a593Smuzhiyun 		pmac_call_feature(PMAC_FTR_MESH_ENABLE, macio_get_of_node(ms->mdev), 0, 0);
1755*4882a593Smuzhiyun 		msleep(10);
1756*4882a593Smuzhiyun 	}
1757*4882a593Smuzhiyun }
1758*4882a593Smuzhiyun 
1759*4882a593Smuzhiyun 
1760*4882a593Smuzhiyun #ifdef CONFIG_PM
mesh_suspend(struct macio_dev * mdev,pm_message_t mesg)1761*4882a593Smuzhiyun static int mesh_suspend(struct macio_dev *mdev, pm_message_t mesg)
1762*4882a593Smuzhiyun {
1763*4882a593Smuzhiyun 	struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
1764*4882a593Smuzhiyun 	unsigned long flags;
1765*4882a593Smuzhiyun 
1766*4882a593Smuzhiyun 	switch (mesg.event) {
1767*4882a593Smuzhiyun 	case PM_EVENT_SUSPEND:
1768*4882a593Smuzhiyun 	case PM_EVENT_HIBERNATE:
1769*4882a593Smuzhiyun 	case PM_EVENT_FREEZE:
1770*4882a593Smuzhiyun 		break;
1771*4882a593Smuzhiyun 	default:
1772*4882a593Smuzhiyun 		return 0;
1773*4882a593Smuzhiyun 	}
1774*4882a593Smuzhiyun 	if (ms->phase == sleeping)
1775*4882a593Smuzhiyun 		return 0;
1776*4882a593Smuzhiyun 
1777*4882a593Smuzhiyun 	scsi_block_requests(ms->host);
1778*4882a593Smuzhiyun 	spin_lock_irqsave(ms->host->host_lock, flags);
1779*4882a593Smuzhiyun 	while(ms->phase != idle) {
1780*4882a593Smuzhiyun 		spin_unlock_irqrestore(ms->host->host_lock, flags);
1781*4882a593Smuzhiyun 		msleep(10);
1782*4882a593Smuzhiyun 		spin_lock_irqsave(ms->host->host_lock, flags);
1783*4882a593Smuzhiyun 	}
1784*4882a593Smuzhiyun 	ms->phase = sleeping;
1785*4882a593Smuzhiyun 	spin_unlock_irqrestore(ms->host->host_lock, flags);
1786*4882a593Smuzhiyun 	disable_irq(ms->meshintr);
1787*4882a593Smuzhiyun 	set_mesh_power(ms, 0);
1788*4882a593Smuzhiyun 
1789*4882a593Smuzhiyun 	return 0;
1790*4882a593Smuzhiyun }
1791*4882a593Smuzhiyun 
mesh_resume(struct macio_dev * mdev)1792*4882a593Smuzhiyun static int mesh_resume(struct macio_dev *mdev)
1793*4882a593Smuzhiyun {
1794*4882a593Smuzhiyun 	struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
1795*4882a593Smuzhiyun 	unsigned long flags;
1796*4882a593Smuzhiyun 
1797*4882a593Smuzhiyun 	if (ms->phase != sleeping)
1798*4882a593Smuzhiyun 		return 0;
1799*4882a593Smuzhiyun 
1800*4882a593Smuzhiyun 	set_mesh_power(ms, 1);
1801*4882a593Smuzhiyun 	mesh_init(ms);
1802*4882a593Smuzhiyun 	spin_lock_irqsave(ms->host->host_lock, flags);
1803*4882a593Smuzhiyun 	mesh_start(ms);
1804*4882a593Smuzhiyun 	spin_unlock_irqrestore(ms->host->host_lock, flags);
1805*4882a593Smuzhiyun 	enable_irq(ms->meshintr);
1806*4882a593Smuzhiyun 	scsi_unblock_requests(ms->host);
1807*4882a593Smuzhiyun 
1808*4882a593Smuzhiyun 	return 0;
1809*4882a593Smuzhiyun }
1810*4882a593Smuzhiyun 
1811*4882a593Smuzhiyun #endif /* CONFIG_PM */
1812*4882a593Smuzhiyun 
1813*4882a593Smuzhiyun /*
1814*4882a593Smuzhiyun  * If we leave drives set for synchronous transfers (especially
1815*4882a593Smuzhiyun  * CDROMs), and reboot to MacOS, it gets confused, poor thing.
1816*4882a593Smuzhiyun  * So, on reboot we reset the SCSI bus.
1817*4882a593Smuzhiyun  */
mesh_shutdown(struct macio_dev * mdev)1818*4882a593Smuzhiyun static int mesh_shutdown(struct macio_dev *mdev)
1819*4882a593Smuzhiyun {
1820*4882a593Smuzhiyun 	struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
1821*4882a593Smuzhiyun 	volatile struct mesh_regs __iomem *mr;
1822*4882a593Smuzhiyun 	unsigned long flags;
1823*4882a593Smuzhiyun 
1824*4882a593Smuzhiyun        	printk(KERN_INFO "resetting MESH scsi bus(es)\n");
1825*4882a593Smuzhiyun 	spin_lock_irqsave(ms->host->host_lock, flags);
1826*4882a593Smuzhiyun        	mr = ms->mesh;
1827*4882a593Smuzhiyun 	out_8(&mr->intr_mask, 0);
1828*4882a593Smuzhiyun 	out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
1829*4882a593Smuzhiyun 	out_8(&mr->bus_status1, BS1_RST);
1830*4882a593Smuzhiyun 	mesh_flush_io(mr);
1831*4882a593Smuzhiyun 	udelay(30);
1832*4882a593Smuzhiyun 	out_8(&mr->bus_status1, 0);
1833*4882a593Smuzhiyun 	spin_unlock_irqrestore(ms->host->host_lock, flags);
1834*4882a593Smuzhiyun 
1835*4882a593Smuzhiyun 	return 0;
1836*4882a593Smuzhiyun }
1837*4882a593Smuzhiyun 
1838*4882a593Smuzhiyun static struct scsi_host_template mesh_template = {
1839*4882a593Smuzhiyun 	.proc_name			= "mesh",
1840*4882a593Smuzhiyun 	.name				= "MESH",
1841*4882a593Smuzhiyun 	.queuecommand			= mesh_queue,
1842*4882a593Smuzhiyun 	.eh_abort_handler		= mesh_abort,
1843*4882a593Smuzhiyun 	.eh_host_reset_handler		= mesh_host_reset,
1844*4882a593Smuzhiyun 	.can_queue			= 20,
1845*4882a593Smuzhiyun 	.this_id			= 7,
1846*4882a593Smuzhiyun 	.sg_tablesize			= SG_ALL,
1847*4882a593Smuzhiyun 	.cmd_per_lun			= 2,
1848*4882a593Smuzhiyun 	.max_segment_size		= 65535,
1849*4882a593Smuzhiyun };
1850*4882a593Smuzhiyun 
mesh_probe(struct macio_dev * mdev,const struct of_device_id * match)1851*4882a593Smuzhiyun static int mesh_probe(struct macio_dev *mdev, const struct of_device_id *match)
1852*4882a593Smuzhiyun {
1853*4882a593Smuzhiyun 	struct device_node *mesh = macio_get_of_node(mdev);
1854*4882a593Smuzhiyun 	struct pci_dev* pdev = macio_get_pci_dev(mdev);
1855*4882a593Smuzhiyun 	int tgt, minper;
1856*4882a593Smuzhiyun 	const int *cfp;
1857*4882a593Smuzhiyun 	struct mesh_state *ms;
1858*4882a593Smuzhiyun 	struct Scsi_Host *mesh_host;
1859*4882a593Smuzhiyun 	void *dma_cmd_space;
1860*4882a593Smuzhiyun 	dma_addr_t dma_cmd_bus;
1861*4882a593Smuzhiyun 
1862*4882a593Smuzhiyun 	switch (mdev->bus->chip->type) {
1863*4882a593Smuzhiyun 	case macio_heathrow:
1864*4882a593Smuzhiyun 	case macio_gatwick:
1865*4882a593Smuzhiyun 	case macio_paddington:
1866*4882a593Smuzhiyun 		use_active_neg = 0;
1867*4882a593Smuzhiyun 		break;
1868*4882a593Smuzhiyun 	default:
1869*4882a593Smuzhiyun 		use_active_neg = SEQ_ACTIVE_NEG;
1870*4882a593Smuzhiyun 	}
1871*4882a593Smuzhiyun 
1872*4882a593Smuzhiyun 	if (macio_resource_count(mdev) != 2 || macio_irq_count(mdev) != 2) {
1873*4882a593Smuzhiyun        		printk(KERN_ERR "mesh: expected 2 addrs and 2 intrs"
1874*4882a593Smuzhiyun 	       	       " (got %d,%d)\n", macio_resource_count(mdev),
1875*4882a593Smuzhiyun 		       macio_irq_count(mdev));
1876*4882a593Smuzhiyun 		return -ENODEV;
1877*4882a593Smuzhiyun 	}
1878*4882a593Smuzhiyun 
1879*4882a593Smuzhiyun 	if (macio_request_resources(mdev, "mesh") != 0) {
1880*4882a593Smuzhiyun        		printk(KERN_ERR "mesh: unable to request memory resources");
1881*4882a593Smuzhiyun 		return -EBUSY;
1882*4882a593Smuzhiyun 	}
1883*4882a593Smuzhiyun        	mesh_host = scsi_host_alloc(&mesh_template, sizeof(struct mesh_state));
1884*4882a593Smuzhiyun 	if (mesh_host == NULL) {
1885*4882a593Smuzhiyun 		printk(KERN_ERR "mesh: couldn't register host");
1886*4882a593Smuzhiyun 		goto out_release;
1887*4882a593Smuzhiyun 	}
1888*4882a593Smuzhiyun 
1889*4882a593Smuzhiyun 	/* Old junk for root discovery, that will die ultimately */
1890*4882a593Smuzhiyun #if !defined(MODULE)
1891*4882a593Smuzhiyun        	note_scsi_host(mesh, mesh_host);
1892*4882a593Smuzhiyun #endif
1893*4882a593Smuzhiyun 
1894*4882a593Smuzhiyun 	mesh_host->base = macio_resource_start(mdev, 0);
1895*4882a593Smuzhiyun 	mesh_host->irq = macio_irq(mdev, 0);
1896*4882a593Smuzhiyun        	ms = (struct mesh_state *) mesh_host->hostdata;
1897*4882a593Smuzhiyun 	macio_set_drvdata(mdev, ms);
1898*4882a593Smuzhiyun 	ms->host = mesh_host;
1899*4882a593Smuzhiyun 	ms->mdev = mdev;
1900*4882a593Smuzhiyun 	ms->pdev = pdev;
1901*4882a593Smuzhiyun 
1902*4882a593Smuzhiyun 	ms->mesh = ioremap(macio_resource_start(mdev, 0), 0x1000);
1903*4882a593Smuzhiyun 	if (ms->mesh == NULL) {
1904*4882a593Smuzhiyun 		printk(KERN_ERR "mesh: can't map registers\n");
1905*4882a593Smuzhiyun 		goto out_free;
1906*4882a593Smuzhiyun 	}
1907*4882a593Smuzhiyun 	ms->dma = ioremap(macio_resource_start(mdev, 1), 0x1000);
1908*4882a593Smuzhiyun 	if (ms->dma == NULL) {
1909*4882a593Smuzhiyun 		printk(KERN_ERR "mesh: can't map registers\n");
1910*4882a593Smuzhiyun 		iounmap(ms->mesh);
1911*4882a593Smuzhiyun 		goto out_free;
1912*4882a593Smuzhiyun 	}
1913*4882a593Smuzhiyun 
1914*4882a593Smuzhiyun        	ms->meshintr = macio_irq(mdev, 0);
1915*4882a593Smuzhiyun        	ms->dmaintr = macio_irq(mdev, 1);
1916*4882a593Smuzhiyun 
1917*4882a593Smuzhiyun        	/* Space for dma command list: +1 for stop command,
1918*4882a593Smuzhiyun        	 * +1 to allow for aligning.
1919*4882a593Smuzhiyun 	 */
1920*4882a593Smuzhiyun 	ms->dma_cmd_size = (mesh_host->sg_tablesize + 2) * sizeof(struct dbdma_cmd);
1921*4882a593Smuzhiyun 
1922*4882a593Smuzhiyun 	/* We use the PCI APIs for now until the generic one gets fixed
1923*4882a593Smuzhiyun 	 * enough or until we get some macio-specific versions
1924*4882a593Smuzhiyun 	 */
1925*4882a593Smuzhiyun 	dma_cmd_space = dma_alloc_coherent(&macio_get_pci_dev(mdev)->dev,
1926*4882a593Smuzhiyun 					   ms->dma_cmd_size, &dma_cmd_bus,
1927*4882a593Smuzhiyun 					   GFP_KERNEL);
1928*4882a593Smuzhiyun 	if (dma_cmd_space == NULL) {
1929*4882a593Smuzhiyun 		printk(KERN_ERR "mesh: can't allocate DMA table\n");
1930*4882a593Smuzhiyun 		goto out_unmap;
1931*4882a593Smuzhiyun 	}
1932*4882a593Smuzhiyun 
1933*4882a593Smuzhiyun 	ms->dma_cmds = (struct dbdma_cmd *) DBDMA_ALIGN(dma_cmd_space);
1934*4882a593Smuzhiyun        	ms->dma_cmd_space = dma_cmd_space;
1935*4882a593Smuzhiyun 	ms->dma_cmd_bus = dma_cmd_bus + ((unsigned long)ms->dma_cmds)
1936*4882a593Smuzhiyun 		- (unsigned long)dma_cmd_space;
1937*4882a593Smuzhiyun 	ms->current_req = NULL;
1938*4882a593Smuzhiyun        	for (tgt = 0; tgt < 8; ++tgt) {
1939*4882a593Smuzhiyun 	       	ms->tgts[tgt].sdtr_state = do_sdtr;
1940*4882a593Smuzhiyun 	       	ms->tgts[tgt].sync_params = ASYNC_PARAMS;
1941*4882a593Smuzhiyun 	       	ms->tgts[tgt].current_req = NULL;
1942*4882a593Smuzhiyun        	}
1943*4882a593Smuzhiyun 
1944*4882a593Smuzhiyun 	if ((cfp = of_get_property(mesh, "clock-frequency", NULL)))
1945*4882a593Smuzhiyun        		ms->clk_freq = *cfp;
1946*4882a593Smuzhiyun 	else {
1947*4882a593Smuzhiyun        		printk(KERN_INFO "mesh: assuming 50MHz clock frequency\n");
1948*4882a593Smuzhiyun 	       	ms->clk_freq = 50000000;
1949*4882a593Smuzhiyun        	}
1950*4882a593Smuzhiyun 
1951*4882a593Smuzhiyun        	/* The maximum sync rate is clock / 5; increase
1952*4882a593Smuzhiyun        	 * mesh_sync_period if necessary.
1953*4882a593Smuzhiyun 	 */
1954*4882a593Smuzhiyun 	minper = 1000000000 / (ms->clk_freq / 5); /* ns */
1955*4882a593Smuzhiyun 	if (mesh_sync_period < minper)
1956*4882a593Smuzhiyun 		mesh_sync_period = minper;
1957*4882a593Smuzhiyun 
1958*4882a593Smuzhiyun 	/* Power up the chip */
1959*4882a593Smuzhiyun 	set_mesh_power(ms, 1);
1960*4882a593Smuzhiyun 
1961*4882a593Smuzhiyun 	/* Set it up */
1962*4882a593Smuzhiyun        	mesh_init(ms);
1963*4882a593Smuzhiyun 
1964*4882a593Smuzhiyun 	/* Request interrupt */
1965*4882a593Smuzhiyun        	if (request_irq(ms->meshintr, do_mesh_interrupt, 0, "MESH", ms)) {
1966*4882a593Smuzhiyun 	       	printk(KERN_ERR "MESH: can't get irq %d\n", ms->meshintr);
1967*4882a593Smuzhiyun 		goto out_shutdown;
1968*4882a593Smuzhiyun 	}
1969*4882a593Smuzhiyun 
1970*4882a593Smuzhiyun 	/* Add scsi host & scan */
1971*4882a593Smuzhiyun 	if (scsi_add_host(mesh_host, &mdev->ofdev.dev))
1972*4882a593Smuzhiyun 		goto out_release_irq;
1973*4882a593Smuzhiyun 	scsi_scan_host(mesh_host);
1974*4882a593Smuzhiyun 
1975*4882a593Smuzhiyun 	return 0;
1976*4882a593Smuzhiyun 
1977*4882a593Smuzhiyun  out_release_irq:
1978*4882a593Smuzhiyun 	free_irq(ms->meshintr, ms);
1979*4882a593Smuzhiyun  out_shutdown:
1980*4882a593Smuzhiyun 	/* shutdown & reset bus in case of error or macos can be confused
1981*4882a593Smuzhiyun 	 * at reboot if the bus was set to synchronous mode already
1982*4882a593Smuzhiyun 	 */
1983*4882a593Smuzhiyun 	mesh_shutdown(mdev);
1984*4882a593Smuzhiyun 	set_mesh_power(ms, 0);
1985*4882a593Smuzhiyun 	dma_free_coherent(&macio_get_pci_dev(mdev)->dev, ms->dma_cmd_size,
1986*4882a593Smuzhiyun 			    ms->dma_cmd_space, ms->dma_cmd_bus);
1987*4882a593Smuzhiyun  out_unmap:
1988*4882a593Smuzhiyun 	iounmap(ms->dma);
1989*4882a593Smuzhiyun 	iounmap(ms->mesh);
1990*4882a593Smuzhiyun  out_free:
1991*4882a593Smuzhiyun 	scsi_host_put(mesh_host);
1992*4882a593Smuzhiyun  out_release:
1993*4882a593Smuzhiyun 	macio_release_resources(mdev);
1994*4882a593Smuzhiyun 
1995*4882a593Smuzhiyun 	return -ENODEV;
1996*4882a593Smuzhiyun }
1997*4882a593Smuzhiyun 
mesh_remove(struct macio_dev * mdev)1998*4882a593Smuzhiyun static int mesh_remove(struct macio_dev *mdev)
1999*4882a593Smuzhiyun {
2000*4882a593Smuzhiyun 	struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
2001*4882a593Smuzhiyun 	struct Scsi_Host *mesh_host = ms->host;
2002*4882a593Smuzhiyun 
2003*4882a593Smuzhiyun 	scsi_remove_host(mesh_host);
2004*4882a593Smuzhiyun 
2005*4882a593Smuzhiyun 	free_irq(ms->meshintr, ms);
2006*4882a593Smuzhiyun 
2007*4882a593Smuzhiyun 	/* Reset scsi bus */
2008*4882a593Smuzhiyun 	mesh_shutdown(mdev);
2009*4882a593Smuzhiyun 
2010*4882a593Smuzhiyun 	/* Shut down chip & termination */
2011*4882a593Smuzhiyun 	set_mesh_power(ms, 0);
2012*4882a593Smuzhiyun 
2013*4882a593Smuzhiyun 	/* Unmap registers & dma controller */
2014*4882a593Smuzhiyun 	iounmap(ms->mesh);
2015*4882a593Smuzhiyun        	iounmap(ms->dma);
2016*4882a593Smuzhiyun 
2017*4882a593Smuzhiyun 	/* Free DMA commands memory */
2018*4882a593Smuzhiyun 	dma_free_coherent(&macio_get_pci_dev(mdev)->dev, ms->dma_cmd_size,
2019*4882a593Smuzhiyun 			    ms->dma_cmd_space, ms->dma_cmd_bus);
2020*4882a593Smuzhiyun 
2021*4882a593Smuzhiyun 	/* Release memory resources */
2022*4882a593Smuzhiyun 	macio_release_resources(mdev);
2023*4882a593Smuzhiyun 
2024*4882a593Smuzhiyun 	scsi_host_put(mesh_host);
2025*4882a593Smuzhiyun 
2026*4882a593Smuzhiyun 	return 0;
2027*4882a593Smuzhiyun }
2028*4882a593Smuzhiyun 
2029*4882a593Smuzhiyun 
2030*4882a593Smuzhiyun static struct of_device_id mesh_match[] =
2031*4882a593Smuzhiyun {
2032*4882a593Smuzhiyun 	{
2033*4882a593Smuzhiyun 	.name 		= "mesh",
2034*4882a593Smuzhiyun 	},
2035*4882a593Smuzhiyun 	{
2036*4882a593Smuzhiyun 	.type		= "scsi",
2037*4882a593Smuzhiyun 	.compatible	= "chrp,mesh0"
2038*4882a593Smuzhiyun 	},
2039*4882a593Smuzhiyun 	{},
2040*4882a593Smuzhiyun };
2041*4882a593Smuzhiyun MODULE_DEVICE_TABLE (of, mesh_match);
2042*4882a593Smuzhiyun 
2043*4882a593Smuzhiyun static struct macio_driver mesh_driver =
2044*4882a593Smuzhiyun {
2045*4882a593Smuzhiyun 	.driver = {
2046*4882a593Smuzhiyun 		.name 		= "mesh",
2047*4882a593Smuzhiyun 		.owner		= THIS_MODULE,
2048*4882a593Smuzhiyun 		.of_match_table	= mesh_match,
2049*4882a593Smuzhiyun 	},
2050*4882a593Smuzhiyun 	.probe		= mesh_probe,
2051*4882a593Smuzhiyun 	.remove		= mesh_remove,
2052*4882a593Smuzhiyun 	.shutdown	= mesh_shutdown,
2053*4882a593Smuzhiyun #ifdef CONFIG_PM
2054*4882a593Smuzhiyun 	.suspend	= mesh_suspend,
2055*4882a593Smuzhiyun 	.resume		= mesh_resume,
2056*4882a593Smuzhiyun #endif
2057*4882a593Smuzhiyun };
2058*4882a593Smuzhiyun 
2059*4882a593Smuzhiyun 
init_mesh(void)2060*4882a593Smuzhiyun static int __init init_mesh(void)
2061*4882a593Smuzhiyun {
2062*4882a593Smuzhiyun 
2063*4882a593Smuzhiyun 	/* Calculate sync rate from module parameters */
2064*4882a593Smuzhiyun 	if (sync_rate > 10)
2065*4882a593Smuzhiyun 		sync_rate = 10;
2066*4882a593Smuzhiyun 	if (sync_rate > 0) {
2067*4882a593Smuzhiyun 		printk(KERN_INFO "mesh: configured for synchronous %d MB/s\n", sync_rate);
2068*4882a593Smuzhiyun 		mesh_sync_period = 1000 / sync_rate;	/* ns */
2069*4882a593Smuzhiyun 		mesh_sync_offset = 15;
2070*4882a593Smuzhiyun 	} else
2071*4882a593Smuzhiyun 		printk(KERN_INFO "mesh: configured for asynchronous\n");
2072*4882a593Smuzhiyun 
2073*4882a593Smuzhiyun 	return macio_register_driver(&mesh_driver);
2074*4882a593Smuzhiyun }
2075*4882a593Smuzhiyun 
exit_mesh(void)2076*4882a593Smuzhiyun static void __exit exit_mesh(void)
2077*4882a593Smuzhiyun {
2078*4882a593Smuzhiyun 	return macio_unregister_driver(&mesh_driver);
2079*4882a593Smuzhiyun }
2080*4882a593Smuzhiyun 
2081*4882a593Smuzhiyun module_init(init_mesh);
2082*4882a593Smuzhiyun module_exit(exit_mesh);
2083