1/*	$NetBSD: mscpvar.h,v 1.16 2009/01/19 19:15:07 mjf Exp $	*/
2/*
3 * Copyright (c) 1988 Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Chris Torek.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	@(#)mscpvar.h	7.3 (Berkeley) 6/28/90
34 */
35
36/*
37 * Copyright (c) 1996 Ludd, University of Lule}, Sweden.
38 *
39 * This code is derived from software contributed to Berkeley by
40 * Chris Torek.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 *    notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 *    notice, this list of conditions and the following disclaimer in the
49 *    documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 *    must display the following acknowledgement:
52 *	This product includes software developed by the University of
53 *	California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 *    may be used to endorse or promote products derived from this software
56 *    without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 *	@(#)mscpvar.h	7.3 (Berkeley) 6/28/90
71 */
72
73/*
74 * MSCP generic driver configuration
75 */
76
77/*
78 * Enabling MSCP_PARANOIA makes the response code perform various checks
79 * on the hardware.  (Right now it verifies only the buffer pointer in
80 * mscp_cmdref.)
81 *
82 * Enabling AVOID_EMULEX_BUG selects an alternative method of identifying
83 * transfers in progress, which gets around a rather peculiar bug in the
84 * SC41/MS.  Enabling MSCP_PARANOIA instead should work, but will cause
85 * `extra' Unibus resets.
86 *
87 * Either of these flags can simply be included as an `options' line in
88 * your configuration file.
89 */
90
91/* #define MSCP_PARANOIA */
92/* #define AVOID_EMULEX_BUG */
93
94/*
95 * Ring information, per ring (one each for commands and responses).
96 */
97struct mscp_ri {
98	int	mri_size;		/* ring size */
99	int	mri_next;		/* next (expected|free) */
100	long	*mri_desc;		/* base address of descriptors */
101	struct	mscp *mri_ring;		/* base address of packets */
102};
103
104/*
105 * Transfer info, one per command packet.
106 */
107struct mscp_xi {
108	bus_dmamap_t	mxi_dmam;	/* Allocated DMA map for this entry */
109	struct buf *	mxi_bp;		/* Buffer used in this command */
110	struct mscp *	mxi_mp;		/* Packet used in this command */
111	int		mxi_inuse;
112};
113
114struct	mscp_ctlr {
115	void	(*mc_ctlrdone)		/* controller operation complete */
116	   (device_t);
117	void	(*mc_go)		/* device-specific start routine */
118	   (device_t, struct mscp_xi *);
119	void	(*mc_saerror)		/* ctlr error handling */
120	   (device_t, int);
121};
122
123struct mscp_softc;
124
125struct	mscp_device {
126	void	(*me_dgram)	/* error datagram */
127	   (device_t, struct mscp *, struct mscp_softc *);
128	void	(*me_iodone)	/* normal I/O is done */
129	   (device_t, struct buf *);
130	int	(*me_online)	/* drive on line */
131	   (device_t, struct mscp *);
132	int	(*me_gotstatus) /* got unit status */
133	   (device_t, struct mscp *);
134	void	(*me_replace)	/* replace done */
135	   (device_t, struct mscp *);
136	int	(*me_ioerr)	/* read or write failed */
137	   (device_t, struct mscp *, struct buf *);
138	void	(*me_bb)	/* B_BAD io done */
139	   (device_t, struct mscp *, struct buf *);
140	void	(*me_fillin)	/* Fill in mscp info for this drive */
141	   (struct buf *,struct mscp *);
142	void	(*me_cmddone)	/* Non-data transfer operation is done */
143	   (device_t, struct mscp *);
144};
145
146/*
147 * This struct is used when attaching a mscpbus.
148 */
149struct	mscp_attach_args {
150	struct	mscp_ctlr *ma_mc;	/* Pointer to ctlr's mscp_ctlr */
151	int	ma_type;		/* disk/tape bus type */
152	struct	mscp_pack *ma_uda;	/* comm area virtual */
153	struct	mscp_softc **ma_softc;	/* backpointer to bus softc */
154	bus_dmamap_t	   ma_dmam;	/* This comm area dma info */
155	bus_dma_tag_t	   ma_dmat;
156	bus_space_tag_t	   ma_iot;
157	bus_space_handle_t ma_iph;	/* initialisation and polling */
158	bus_space_handle_t ma_sah;	/* status & address (read part) */
159	bus_space_handle_t ma_swh;	/* status & address (write part) */
160	short	ma_ivec;		/* Interrupt vector to use */
161	char	ma_ctlrnr;		/* Phys ctlr nr */
162	char	ma_adapnr;		/* Phys adapter nr */
163};
164#define MSCPBUS_DISK	001	/* Bus is used for disk mounts */
165#define MSCPBUS_TAPE	002	/* Bus is used for tape mounts */
166#define MSCPBUS_UDA	004	/* ctlr is disk on unibus/qbus */
167#define MSCPBUS_KDB	010	/* ctlr is disk on BI */
168#define MSCPBUS_KLE	020	/* ctlr is tape on unibus/qbus */
169
170/*
171 * Used when going for child devices.
172 */
173struct	drive_attach_args {
174	struct	mscp *da_mp;	/* this devices response struct */
175	int	da_typ;		/* Parent of type */
176};
177
178/*
179 * Return values from functions.
180 * MSCP_RESTARTED is peculiar to I/O errors.
181 */
182#define MSCP_DONE	0		/* all ok */
183#define MSCP_FAILED	1		/* no go */
184#define MSCP_RESTARTED	2		/* transfer restarted */
185
186/* Work item for autoconf setup */
187struct mscp_work {
188	struct work mw_work;
189	struct mscp_softc *mw_mi;
190	struct mscp mw_mp;
191	SLIST_ENTRY(mscp_work) mw_list;
192};
193
194/*
195 * Per device information.
196 *
197 * mi_ip is a pointer to the inverting pointers (things that get `ui's
198 * given unit numbers) FOR THIS CONTROLLER (NOT the whole set!).
199 *
200 * b_actf holds a queue of those transfers that were started but have
201 * not yet finished.  Other Unibus drivers do not need this as they hand
202 * out requests one at a time.	MSCP devices, however, take a slew of
203 * requests and pick their own order to execute them.  This means that
204 * we have to have a place to move transfers that were given to the
205 * controller, so we can tell those apart from those that have not yet
206 * been handed out; b_actf is that place.
207 */
208struct mscp_softc {
209	struct	device mi_dev;		/* Autoconf stuff */
210	struct	mscp_ri mi_cmd;		/* MSCP command ring info */
211	struct	mscp_ri mi_rsp;		/* MSCP response ring info */
212	bus_dma_tag_t	mi_dmat;
213	bus_dmamap_t	mi_dmam;
214	struct	mscp_xi mi_xi[NCMD];
215	int	mi_mxiuse;		/* Bitfield of inuse mxi packets */
216	short	mi_credits;		/* transfer credits */
217	char	mi_wantcmd;		/* waiting for command packet */
218	char	mi_wantcredits;		/* waiting for transfer credits */
219	struct	mscp_ctlr *mi_mc;	/* Pointer to parent's mscp_ctlr */
220	struct	mscp_device *mi_me;	/* Pointer to child's mscp_device */
221	struct	device **mi_dp;		/* array of backpointers */
222	int	mi_driveno;		/* Max physical drive number found */
223	char	mi_ctlrnr;		/* Phys ctlr nr */
224	char	mi_adapnr;		/* Phys adapter nr */
225	int	mi_flags;
226	struct	mscp_pack *mi_uda;	/* virtual address */
227	int	mi_type;
228	short	mi_ivec;		/* Interrupt vector to use */
229	short	mi_ierr;		/* Init err counter */
230	bus_space_tag_t	   mi_iot;
231	bus_space_handle_t mi_iph;	/* initialisation and polling */
232	bus_space_handle_t mi_sah;	/* status & address (read part) */
233	bus_space_handle_t mi_swh;	/* status & address (write part) */
234	struct bufq_state *mi_resq;	/* While waiting for packets */
235	struct workqueue *mi_wq;	/* Autoconf workqueue */
236	kmutex_t mi_mtx;		/* Freelist mutex */
237	SLIST_HEAD(, mscp_work) mi_freelist; /* Work item freelist */
238};
239
240/* mi_flags */
241#define MSC_STARTPOLL	1
242#define MSC_INSTART	2
243#define MSC_IGNOREINTR	4
244#define MSC_READY	8
245
246/*
247 * We have run out of credits when mi_credits is <= MSCP_MINCREDITS.
248 * It is still possible to issue one command in this case, but it must
249 * not be a data transfer.  E.g., `get command status' or `abort command'
250 * is legal, while `read' is not.
251 */
252#define MSCP_MINCREDITS 1
253
254/*
255 * Flags for mscp_getcp().
256 */
257#define MSCP_WAIT	1
258#define MSCP_DONTWAIT	0
259
260	/* get a command packet */
261
262/*
263 * Unit flags
264 */
265#define UNIT_ONLINE	0x01	/* drive is on line */
266#define UNIT_HAVESTATUS 0x02	/* got unit status */
267#define UNIT_REQUEUE	0x04	/* requeue after response */
268
269/*
270 * Handle a command ring transition: wake up sleepers for command packets.
271 * This is too simple to bother with a function call.
272 */
273#define MSCP_DOCMD(mi) { \
274	if ((mi)->mi_wantcmd) { \
275		(mi)->mi_wantcmd = 0; \
276		wakeup((void *) &(mi)->mi_wantcmd); \
277	} \
278}
279
280/* Prototypes */
281struct	mscp *mscp_getcp(struct mscp_softc *, int);
282void	mscp_printevent(struct mscp *);
283void	mscp_go(struct mscp_softc *, struct mscp *, int);
284void	mscp_requeue(struct mscp_softc *);
285void	mscp_dorsp(struct mscp_softc *);
286int	mscp_decodeerror(const char *, struct mscp *, struct mscp_softc *);
287int	mscp_print(void *, const char *);
288void	mscp_hexdump(struct mscp *);
289void	mscp_strategy(struct buf *, device_t);
290void	mscp_printtype(int, int);
291int	mscp_waitstep(struct mscp_softc *, int, int);
292void	mscp_dgo(struct mscp_softc *, struct mscp_xi *);
293void	mscp_intr(struct mscp_softc *);
294void	mscp_worker(struct work *, void *);
295