1 /*        $NetBSD: mscpvar.h,v 1.19 2024/01/11 06:19:49 mrg 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  */
97 struct 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  */
107 struct 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 
114 struct    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 
123 struct mscp_softc;
124 
125 struct    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           void      (*me_online_cb)     /* drive on line, thread context */
133              (struct work *wk);
134           int       (*me_gotstatus) /* got unit status */
135              (device_t, struct mscp *);
136           void      (*me_replace)       /* replace done */
137              (device_t, struct mscp *);
138           int       (*me_ioerr)         /* read or write failed */
139              (device_t, struct mscp *, struct buf *);
140           void      (*me_bb)  /* B_BAD io done */
141              (device_t, struct mscp *, struct buf *);
142           void      (*me_fillin)        /* Fill in mscp info for this drive */
143              (struct buf *,struct mscp *);
144           void      (*me_cmddone)       /* Non-data transfer operation is done */
145              (device_t, struct mscp *);
146 };
147 
148 /*
149  * This struct is used when attaching a mscpbus.
150  */
151 struct    mscp_attach_args {
152           struct    mscp_ctlr *ma_mc;   /* Pointer to ctlr's mscp_ctlr */
153           int       ma_type;            /* disk/tape bus type */
154           struct    mscp_pack *ma_uda;  /* comm area virtual */
155           struct    mscp_softc **ma_softc;        /* backpointer to bus softc */
156           bus_dmamap_t           ma_dmam;         /* This comm area dma info */
157           bus_dma_tag_t          ma_dmat;
158           bus_space_tag_t        ma_iot;
159           bus_space_handle_t ma_iph;    /* initialisation and polling */
160           bus_space_handle_t ma_sah;    /* status & address (read part) */
161           bus_space_handle_t ma_swh;    /* status & address (write part) */
162           short     ma_ivec;            /* Interrupt vector to use */
163           char      ma_ctlrnr;                    /* Phys ctlr nr */
164           char      ma_adapnr;                    /* Phys adapter nr */
165 };
166 #define MSCPBUS_DISK          001       /* Bus is used for disk mounts */
167 #define MSCPBUS_TAPE          002       /* Bus is used for tape mounts */
168 #define MSCPBUS_UDA 004       /* ctlr is disk on unibus/qbus */
169 #define MSCPBUS_KDB 010       /* ctlr is disk on BI */
170 #define MSCPBUS_KLE 020       /* ctlr is tape on unibus/qbus */
171 
172 /*
173  * Used when going for child devices.
174  */
175 struct    drive_attach_args {
176           struct    mscp *da_mp;        /* this devices response struct */
177           int       da_typ;             /* Parent of type */
178 };
179 
180 /*
181  * Return values from functions.
182  * MSCP_RESTARTED is peculiar to I/O errors.
183  */
184 #define MSCP_DONE   0                   /* all ok */
185 #define MSCP_FAILED 1                   /* no go */
186 #define MSCP_RESTARTED        2                   /* transfer restarted */
187 
188 /* Work item for autoconf setup */
189 struct mscp_work {
190           struct work mw_work;
191           struct mscp_softc *mw_mi;
192           struct mscp mw_mp;
193           bool mw_online;
194           SLIST_ENTRY(mscp_work) mw_list;
195 };
196 
197 /*
198  * Per device information.
199  *
200  * mi_ip is a pointer to the inverting pointers (things that get `ui's
201  * given unit numbers) FOR THIS CONTROLLER (NOT the whole set!).
202  *
203  * b_actf holds a queue of those transfers that were started but have
204  * not yet finished.  Other Unibus drivers do not need this as they hand
205  * out requests one at a time.          MSCP devices, however, take a slew of
206  * requests and pick their own order to execute them.  This means that
207  * we have to have a place to move transfers that were given to the
208  * controller, so we can tell those apart from those that have not yet
209  * been handed out; b_actf is that place.
210  */
211 struct mscp_softc {
212           device_t mi_dev;              /* Autoconf stuff */
213           struct    mscp_ri mi_cmd;               /* MSCP command ring info */
214           struct    mscp_ri mi_rsp;               /* MSCP response ring info */
215           bus_dma_tag_t       mi_dmat;
216           bus_dmamap_t        mi_dmam;
217           struct    mscp_xi mi_xi[NCMD];
218           int       mi_mxiuse;                    /* Bitfield of inuse mxi packets */
219           short     mi_credits;                   /* transfer credits */
220           char      mi_wantcmd;                   /* waiting for command packet */
221           char      mi_wantcredits;               /* waiting for transfer credits */
222           struct    mscp_ctlr *mi_mc;   /* Pointer to parent's mscp_ctlr */
223           struct    mscp_device *mi_me; /* Pointer to child's mscp_device */
224           device_t *mi_dp;              /* array of backpointers */
225           int       mi_driveno;                   /* Max physical drive number found */
226           char      mi_ctlrnr;                    /* Phys ctlr nr */
227           char      mi_adapnr;                    /* Phys adapter nr */
228           int       mi_flags;
229           struct    mscp_pack *mi_uda;  /* virtual address */
230           int       mi_type;
231           short     mi_ivec;            /* Interrupt vector to use */
232           short     mi_ierr;            /* Init err counter */
233           bus_space_tag_t        mi_iot;
234           bus_space_handle_t mi_iph;    /* initialisation and polling */
235           bus_space_handle_t mi_sah;    /* status & address (read part) */
236           bus_space_handle_t mi_swh;    /* status & address (write part) */
237           struct bufq_state *mi_resq;   /* While waiting for packets */
238           struct workqueue *mi_wq;      /* Autoconf workqueue */
239           kmutex_t mi_mtx;              /* Freelist mutex */
240           SLIST_HEAD(, mscp_work) mi_freelist; /* Work item freelist */
241 };
242 
243 /* mi_flags */
244 #define MSC_STARTPOLL         1
245 #define MSC_INSTART 2
246 #define MSC_IGNOREINTR        4
247 #define MSC_READY   8
248 
249 /*
250  * We have run out of credits when mi_credits is <= MSCP_MINCREDITS.
251  * It is still possible to issue one command in this case, but it must
252  * not be a data transfer.  E.g., `get command status' or `abort command'
253  * is legal, while `read' is not.
254  */
255 #define MSCP_MINCREDITS 1
256 
257 /*
258  * Flags for mscp_getcp().
259  */
260 #define MSCP_WAIT   1
261 #define MSCP_DONTWAIT         0
262 
263           /* get a command packet */
264 
265 /*
266  * Unit flags
267  */
268 #define UNIT_ONLINE 0x01      /* drive is on line */
269 #define UNIT_HAVESTATUS 0x02  /* got unit status */
270 #define UNIT_REQUEUE          0x04      /* requeue after response */
271 
272 /*
273  * Handle a command ring transition: wake up sleepers for command packets.
274  * This is too simple to bother with a function call.
275  */
276 #define MSCP_DOCMD(mi) { \
277           if ((mi)->mi_wantcmd) { \
278                     (mi)->mi_wantcmd = 0; \
279                     wakeup((void *) &(mi)->mi_wantcmd); \
280           } \
281 }
282 
283 /* Prototypes */
284 struct    mscp *mscp_getcp(struct mscp_softc *, int);
285 void      mscp_printevent(struct mscp *);
286 void      mscp_go(struct mscp_softc *, struct mscp *, int);
287 void      mscp_requeue(struct mscp_softc *);
288 void      mscp_dorsp(struct mscp_softc *);
289 int       mscp_decodeerror(const char *, struct mscp *, struct mscp_softc *);
290 int       mscp_print(void *, const char *);
291 void      mscp_hexdump(struct mscp *);
292 void      mscp_strategy(struct buf *, device_t);
293 void      mscp_printtype(int, int);
294 int       mscp_waitstep(struct mscp_softc *, int, int);
295 void      mscp_dgo(struct mscp_softc *, struct mscp_xi *);
296 void      mscp_intr(struct mscp_softc *);
297 void      mscp_worker(struct work *, void *);
298