iop.c revision 1.5 1 1.5 ad /* $NetBSD: iop.c,v 1.5 2000/12/03 13:17:03 ad Exp $ */
2 1.1 ad
3 1.1 ad /*-
4 1.1 ad * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.1 ad * All rights reserved.
6 1.1 ad *
7 1.1 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 ad * by Andrew Doran.
9 1.1 ad *
10 1.1 ad * Redistribution and use in source and binary forms, with or without
11 1.1 ad * modification, are permitted provided that the following conditions
12 1.1 ad * are met:
13 1.1 ad * 1. Redistributions of source code must retain the above copyright
14 1.1 ad * notice, this list of conditions and the following disclaimer.
15 1.1 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 ad * notice, this list of conditions and the following disclaimer in the
17 1.1 ad * documentation and/or other materials provided with the distribution.
18 1.1 ad * 3. All advertising materials mentioning features or use of this software
19 1.1 ad * must display the following acknowledgement:
20 1.1 ad * This product includes software developed by the NetBSD
21 1.1 ad * Foundation, Inc. and its contributors.
22 1.1 ad * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 ad * contributors may be used to endorse or promote products derived
24 1.1 ad * from this software without specific prior written permission.
25 1.1 ad *
26 1.1 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 ad * POSSIBILITY OF SUCH DAMAGE.
37 1.1 ad */
38 1.1 ad
39 1.1 ad /*
40 1.1 ad * Support for I2O IOPs (intelligent I/O processors).
41 1.1 ad */
42 1.1 ad
43 1.1 ad #include "opt_i2o.h"
44 1.5 ad #include "iop.h"
45 1.1 ad
46 1.1 ad #include <sys/param.h>
47 1.1 ad #include <sys/systm.h>
48 1.1 ad #include <sys/kernel.h>
49 1.1 ad #include <sys/device.h>
50 1.1 ad #include <sys/queue.h>
51 1.1 ad #include <sys/proc.h>
52 1.1 ad #include <sys/malloc.h>
53 1.1 ad #include <sys/ioctl.h>
54 1.1 ad #include <sys/endian.h>
55 1.1 ad #include <sys/pool.h>
56 1.5 ad #include <sys/conf.h>
57 1.5 ad #include <sys/kthread.h>
58 1.1 ad
59 1.4 thorpej #include <uvm/uvm_extern.h>
60 1.4 thorpej
61 1.1 ad #include <machine/bus.h>
62 1.1 ad
63 1.1 ad #include <dev/i2o/i2o.h>
64 1.1 ad #include <dev/i2o/iopreg.h>
65 1.1 ad #include <dev/i2o/iopvar.h>
66 1.1 ad
67 1.1 ad #define POLL(ms, cond) \
68 1.1 ad do { \
69 1.1 ad int i; \
70 1.1 ad for (i = (ms) * 10; i; i--) { \
71 1.1 ad if (cond) \
72 1.1 ad break; \
73 1.1 ad DELAY(100); \
74 1.1 ad } \
75 1.1 ad } while (/* CONSTCOND */0);
76 1.1 ad
77 1.1 ad #ifdef I2ODEBUG
78 1.1 ad #define DPRINTF(x) printf x
79 1.1 ad #else
80 1.1 ad #define DPRINTF(x)
81 1.1 ad #endif
82 1.1 ad
83 1.1 ad #ifdef I2OVERBOSE
84 1.5 ad #define IFVERBOSE(x) x
85 1.1 ad #else
86 1.1 ad #define IFVERBOSE(x)
87 1.1 ad #endif
88 1.1 ad
89 1.5 ad #define COMMENT(x) ""
90 1.1 ad
91 1.5 ad #define IOP_ICTXHASH_NBUCKETS 16
92 1.5 ad #define IOP_ICTXHASH(ictx) (&iop_ictxhashtbl[(ictx) & iop_ictxhash])
93 1.5 ad #define IOP_TCTXHASH_NBUCKETS 64
94 1.5 ad #define IOP_TCTXHASH(tctx) (&iop_tctxhashtbl[(tctx) & iop_tctxhash])
95 1.5 ad
96 1.5 ad static LIST_HEAD(, iop_initiator) *iop_ictxhashtbl;
97 1.5 ad static u_long iop_ictxhash;
98 1.5 ad static TAILQ_HEAD(, iop_msg) *iop_tctxhashtbl;
99 1.5 ad static u_long iop_tctxhash;
100 1.1 ad static void *iop_sdh;
101 1.1 ad static struct pool *iop_msgpool;
102 1.5 ad static struct i2o_systab *iop_systab;
103 1.5 ad static int iop_systab_size;
104 1.1 ad
105 1.1 ad extern struct cfdriver iop_cd;
106 1.1 ad
107 1.5 ad #define IC_CONFIGURE 0x01
108 1.1 ad
109 1.1 ad struct iop_class {
110 1.5 ad u_short ic_class;
111 1.5 ad u_short ic_flags;
112 1.1 ad const char *ic_caption;
113 1.1 ad } static const iop_class[] = {
114 1.1 ad {
115 1.1 ad I2O_CLASS_EXECUTIVE,
116 1.1 ad 0,
117 1.5 ad COMMENT("executive")
118 1.1 ad },
119 1.1 ad {
120 1.1 ad I2O_CLASS_DDM,
121 1.1 ad 0,
122 1.5 ad COMMENT("device driver module")
123 1.1 ad },
124 1.1 ad {
125 1.1 ad I2O_CLASS_RANDOM_BLOCK_STORAGE,
126 1.5 ad IC_CONFIGURE,
127 1.1 ad IFVERBOSE("random block storage")
128 1.1 ad },
129 1.1 ad {
130 1.1 ad I2O_CLASS_SEQUENTIAL_STORAGE,
131 1.5 ad IC_CONFIGURE,
132 1.1 ad IFVERBOSE("sequential storage")
133 1.1 ad },
134 1.1 ad {
135 1.1 ad I2O_CLASS_LAN,
136 1.1 ad IC_CONFIGURE,
137 1.1 ad IFVERBOSE("LAN port")
138 1.1 ad },
139 1.1 ad {
140 1.1 ad I2O_CLASS_WAN,
141 1.1 ad IC_CONFIGURE,
142 1.1 ad IFVERBOSE("WAN port")
143 1.1 ad },
144 1.1 ad {
145 1.1 ad I2O_CLASS_FIBRE_CHANNEL_PORT,
146 1.1 ad IC_CONFIGURE,
147 1.1 ad IFVERBOSE("fibrechannel port")
148 1.1 ad },
149 1.1 ad {
150 1.1 ad I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL,
151 1.1 ad 0,
152 1.5 ad COMMENT("fibrechannel peripheral")
153 1.1 ad },
154 1.1 ad {
155 1.1 ad I2O_CLASS_SCSI_PERIPHERAL,
156 1.1 ad 0,
157 1.5 ad COMMENT("SCSI peripheral")
158 1.1 ad },
159 1.1 ad {
160 1.1 ad I2O_CLASS_ATE_PORT,
161 1.1 ad IC_CONFIGURE,
162 1.1 ad IFVERBOSE("ATE port")
163 1.1 ad },
164 1.1 ad {
165 1.1 ad I2O_CLASS_ATE_PERIPHERAL,
166 1.1 ad 0,
167 1.5 ad COMMENT("ATE peripheral")
168 1.1 ad },
169 1.1 ad {
170 1.1 ad I2O_CLASS_FLOPPY_CONTROLLER,
171 1.1 ad IC_CONFIGURE,
172 1.1 ad IFVERBOSE("floppy controller")
173 1.1 ad },
174 1.1 ad {
175 1.1 ad I2O_CLASS_FLOPPY_DEVICE,
176 1.1 ad 0,
177 1.5 ad COMMENT("floppy device")
178 1.1 ad },
179 1.1 ad {
180 1.1 ad I2O_CLASS_BUS_ADAPTER_PORT,
181 1.1 ad IC_CONFIGURE,
182 1.1 ad IFVERBOSE("bus adapter port" )
183 1.1 ad },
184 1.1 ad };
185 1.1 ad
186 1.1 ad #if defined(I2ODEBUG) && defined(I2OVERBOSE)
187 1.1 ad static const char *iop_status[] = {
188 1.1 ad "success",
189 1.1 ad "abort (dirty)",
190 1.1 ad "abort (no data transfer)",
191 1.1 ad "abort (partial transfer)",
192 1.1 ad "error (dirty)",
193 1.1 ad "error (no data transfer)",
194 1.1 ad "error (partial transfer)",
195 1.1 ad "undefined error code",
196 1.1 ad "process abort (dirty)",
197 1.1 ad "process abort (no data transfer)",
198 1.1 ad "process abort (partial transfer)",
199 1.1 ad "transaction error",
200 1.1 ad };
201 1.1 ad #endif
202 1.1 ad
203 1.5 ad static inline u_int32_t iop_inl(struct iop_softc *, int);
204 1.5 ad static inline void iop_outl(struct iop_softc *, int, u_int32_t);
205 1.5 ad
206 1.1 ad static void iop_config_interrupts(struct device *);
207 1.5 ad static void iop_configure_devices(struct iop_softc *);
208 1.1 ad static void iop_devinfo(int, char *);
209 1.1 ad static int iop_print(void *, const char *);
210 1.5 ad static int iop_reconfigure(struct iop_softc *, u_int32_t);
211 1.1 ad static void iop_shutdown(void *);
212 1.1 ad static int iop_submatch(struct device *, struct cfdata *, void *);
213 1.5 ad #ifdef notyet
214 1.1 ad static int iop_vendor_print(void *, const char *);
215 1.5 ad #endif
216 1.1 ad
217 1.5 ad static void iop_intr_event(struct device *, struct iop_msg *, void *);
218 1.1 ad static int iop_hrt_get(struct iop_softc *);
219 1.1 ad static int iop_hrt_get0(struct iop_softc *, struct i2o_hrt *, int);
220 1.5 ad static int iop_lct_get0(struct iop_softc *, struct i2o_lct *, int,
221 1.5 ad u_int32_t);
222 1.5 ad static int iop_msg_wait(struct iop_softc *, struct iop_msg *, int);
223 1.1 ad static int iop_ofifo_init(struct iop_softc *);
224 1.5 ad static int iop_handle_reply(struct iop_softc *, u_int32_t);
225 1.5 ad static void iop_reconfigure_proc(void *);
226 1.1 ad static void iop_release_mfa(struct iop_softc *, u_int32_t);
227 1.1 ad static int iop_reset(struct iop_softc *);
228 1.1 ad static int iop_status_get(struct iop_softc *);
229 1.1 ad static int iop_systab_set(struct iop_softc *);
230 1.1 ad
231 1.1 ad #ifdef I2ODEBUG
232 1.1 ad static void iop_reply_print(struct iop_softc *, struct iop_msg *,
233 1.1 ad struct i2o_reply *);
234 1.1 ad #endif
235 1.1 ad
236 1.5 ad cdev_decl(iop);
237 1.5 ad
238 1.5 ad static inline u_int32_t
239 1.5 ad iop_inl(struct iop_softc *sc, int off)
240 1.5 ad {
241 1.5 ad
242 1.5 ad bus_space_barrier(sc->sc_iot, sc->sc_ioh, off, 4,
243 1.5 ad BUS_SPACE_BARRIER_WRITE | BUS_SPACE_BARRIER_READ);
244 1.5 ad return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, off));
245 1.5 ad }
246 1.5 ad
247 1.5 ad static inline void
248 1.5 ad iop_outl(struct iop_softc *sc, int off, u_int32_t val)
249 1.5 ad {
250 1.5 ad
251 1.5 ad bus_space_write_4(sc->sc_iot, sc->sc_ioh, off, val);
252 1.5 ad bus_space_barrier(sc->sc_iot, sc->sc_ioh, off, 4,
253 1.5 ad BUS_SPACE_BARRIER_WRITE);
254 1.5 ad }
255 1.5 ad
256 1.1 ad /*
257 1.1 ad * Initialise the adapter.
258 1.1 ad */
259 1.5 ad void
260 1.1 ad iop_init(struct iop_softc *sc, const char *intrstr)
261 1.1 ad {
262 1.1 ad int rv;
263 1.1 ad u_int32_t mask;
264 1.1 ad static int again;
265 1.1 ad char ident[64];
266 1.1 ad
267 1.1 ad if (again == 0) {
268 1.5 ad /* Create the shared message wrapper pool and hashes. */
269 1.1 ad iop_msgpool = pool_create(sizeof(struct iop_msg), 0, 0, 0,
270 1.1 ad "ioppl", 0, NULL, NULL, M_DEVBUF);
271 1.5 ad iop_ictxhashtbl = hashinit(IOP_ICTXHASH_NBUCKETS, HASH_LIST,
272 1.5 ad M_DEVBUF, M_NOWAIT, &iop_ictxhash);
273 1.5 ad iop_tctxhashtbl = hashinit(IOP_TCTXHASH_NBUCKETS, HASH_TAILQ,
274 1.5 ad M_DEVBUF, M_NOWAIT, &iop_tctxhash);
275 1.1 ad again = 1;
276 1.1 ad }
277 1.1 ad
278 1.5 ad /* Reset the IOP and request status. */
279 1.1 ad printf("I2O adapter");
280 1.5 ad
281 1.5 ad if ((rv = iop_reset(sc)) != 0) {
282 1.5 ad printf("%s: not responding (reset)\n", sc->sc_dv.dv_xname);
283 1.5 ad return;
284 1.5 ad }
285 1.1 ad if ((rv = iop_status_get(sc)) != 0) {
286 1.5 ad printf("%s: not responding (get status)\n", sc->sc_dv.dv_xname);
287 1.5 ad return;
288 1.1 ad }
289 1.5 ad DPRINTF((" (state=%d)",
290 1.5 ad (le32toh(sc->sc_status.segnumber) >> 16) & 0xff));
291 1.5 ad sc->sc_flags |= IOP_HAVESTATUS;
292 1.1 ad
293 1.5 ad iop_strvis(sc, sc->sc_status.productid, sizeof(sc->sc_status.productid),
294 1.1 ad ident, sizeof(ident));
295 1.5 ad printf(" <%s>\n", ident);
296 1.5 ad
297 1.5 ad #ifdef I2ODEBUG
298 1.5 ad printf("%s: orgid=0x%04x version=%d\n", sc->sc_dv.dv_xname,
299 1.5 ad le16toh(sc->sc_status.orgid),
300 1.5 ad (le32toh(sc->sc_status.segnumber) >> 12) & 15);
301 1.5 ad printf("%s: type want have cbase\n", sc->sc_dv.dv_xname);
302 1.5 ad printf("%s: mem %04x %04x %08x\n", sc->sc_dv.dv_xname,
303 1.5 ad le32toh(sc->sc_status.desiredprivmemsize),
304 1.5 ad le32toh(sc->sc_status.currentprivmemsize),
305 1.5 ad le32toh(sc->sc_status.currentprivmembase));
306 1.5 ad printf("%s: i/o %04x %04x %08x\n", sc->sc_dv.dv_xname,
307 1.5 ad le32toh(sc->sc_status.desiredpriviosize),
308 1.5 ad le32toh(sc->sc_status.currentpriviosize),
309 1.5 ad le32toh(sc->sc_status.currentpriviobase));
310 1.5 ad #endif
311 1.1 ad
312 1.1 ad sc->sc_maxreplycnt = le32toh(sc->sc_status.maxoutboundmframes);
313 1.1 ad if (sc->sc_maxreplycnt > IOP_MAX_HW_REPLYCNT)
314 1.1 ad sc->sc_maxreplycnt = IOP_MAX_HW_REPLYCNT;
315 1.5 ad sc->sc_maxqueuecnt = le32toh(sc->sc_status.maxinboundmframes);
316 1.5 ad if (sc->sc_maxqueuecnt > IOP_MAX_HW_QUEUECNT)
317 1.5 ad sc->sc_maxqueuecnt = IOP_MAX_HW_QUEUECNT;
318 1.1 ad
319 1.5 ad if (iop_ofifo_init(sc) != 0) {
320 1.5 ad printf("%s: unable to init oubound FIFO\n", sc->sc_dv.dv_xname);
321 1.5 ad return;
322 1.5 ad }
323 1.1 ad
324 1.5 ad /*
325 1.5 ad * Defer further configuration until (a) interrupts are working and
326 1.5 ad * (b) we have enough information to build the system table.
327 1.5 ad */
328 1.1 ad config_interrupts((struct device *)sc, iop_config_interrupts);
329 1.1 ad
330 1.5 ad /* Configure shutdown hook before we start any device activity. */
331 1.1 ad if (iop_sdh == NULL)
332 1.1 ad iop_sdh = shutdownhook_establish(iop_shutdown, NULL);
333 1.1 ad
334 1.1 ad /* Ensure interrupts are enabled at the IOP. */
335 1.5 ad mask = iop_inl(sc, IOP_REG_INTR_MASK);
336 1.5 ad iop_outl(sc, IOP_REG_INTR_MASK, mask & ~IOP_INTR_OFIFO);
337 1.1 ad
338 1.1 ad if (intrstr != NULL)
339 1.1 ad printf("%s: interrupting at %s\n", sc->sc_dv.dv_xname,
340 1.1 ad intrstr);
341 1.1 ad
342 1.1 ad #ifdef I2ODEBUG
343 1.1 ad printf("%s: queue depths: inbound %d/%d, outbound %d/%d\n",
344 1.1 ad sc->sc_dv.dv_xname,
345 1.1 ad sc->sc_maxqueuecnt, le32toh(sc->sc_status.maxinboundmframes),
346 1.1 ad sc->sc_maxreplycnt, le32toh(sc->sc_status.maxoutboundmframes));
347 1.1 ad #endif
348 1.1 ad
349 1.5 ad lockinit(&sc->sc_conflock, PRIBIO, "iopconf", hz * 30, 0);
350 1.1 ad SIMPLEQ_INIT(&sc->sc_queue);
351 1.1 ad }
352 1.1 ad
353 1.1 ad /*
354 1.5 ad * Perform autoconfiguration tasks.
355 1.1 ad */
356 1.1 ad static void
357 1.1 ad iop_config_interrupts(struct device *self)
358 1.1 ad {
359 1.5 ad struct iop_softc *sc, *iop;
360 1.5 ad struct i2o_systab_entry *ste;
361 1.5 ad int rv, i, niop;
362 1.1 ad
363 1.1 ad sc = (struct iop_softc *)self;
364 1.5 ad LIST_INIT(&sc->sc_iilist);
365 1.5 ad
366 1.5 ad printf("%s: configuring...\n", sc->sc_dv.dv_xname);
367 1.1 ad
368 1.5 ad if (iop_hrt_get(sc) != 0) {
369 1.5 ad printf("%s: unable to retrieve HRT\n", sc->sc_dv.dv_xname);
370 1.5 ad return;
371 1.5 ad }
372 1.1 ad
373 1.5 ad /*
374 1.5 ad * Build the system table.
375 1.5 ad */
376 1.5 ad if (iop_systab == NULL) {
377 1.5 ad for (i = 0, niop = 0; i < iop_cd.cd_ndevs; i++) {
378 1.5 ad if ((iop = device_lookup(&iop_cd, i)) == NULL)
379 1.5 ad continue;
380 1.5 ad if ((iop->sc_flags & IOP_HAVESTATUS) == 0)
381 1.5 ad continue;
382 1.5 ad if (iop_status_get(iop) != 0) {
383 1.5 ad printf("%s: unable to retrieve status\n",
384 1.5 ad sc->sc_dv.dv_xname);
385 1.5 ad iop->sc_flags &= ~IOP_HAVESTATUS;
386 1.5 ad continue;
387 1.5 ad }
388 1.5 ad niop++;
389 1.5 ad }
390 1.5 ad if (niop == 0)
391 1.5 ad return;
392 1.5 ad
393 1.5 ad i = sizeof(struct i2o_systab_entry) * (niop - 1) +
394 1.5 ad sizeof(struct i2o_systab);
395 1.5 ad iop_systab_size = i;
396 1.5 ad iop_systab = malloc(i, M_DEVBUF, M_NOWAIT);
397 1.5 ad
398 1.5 ad memset(iop_systab, 0, i);
399 1.5 ad iop_systab->numentries = niop;
400 1.5 ad iop_systab->version = I2O_VERSION_11;
401 1.5 ad
402 1.5 ad for (i = 0, ste = iop_systab->entry; i < iop_cd.cd_ndevs; i++) {
403 1.5 ad if ((iop = device_lookup(&iop_cd, i)) == NULL)
404 1.5 ad continue;
405 1.5 ad if ((iop->sc_flags & IOP_HAVESTATUS) == 0)
406 1.5 ad continue;
407 1.5 ad
408 1.5 ad ste->orgid = iop->sc_status.orgid;
409 1.5 ad ste->iopid = iop->sc_dv.dv_unit + 2;
410 1.5 ad ste->segnumber =
411 1.5 ad htole32(le32toh(iop->sc_status.segnumber) & ~4095);
412 1.5 ad ste->iopcaps = iop->sc_status.iopcaps;
413 1.5 ad ste->inboundmsgframesize =
414 1.5 ad iop->sc_status.inboundmframesize;
415 1.5 ad ste->inboundmsgportaddresslow =
416 1.5 ad htole32(iop->sc_memaddr + IOP_REG_IFIFO);
417 1.5 ad ste++;
418 1.5 ad }
419 1.5 ad }
420 1.5 ad
421 1.5 ad if (iop_systab_set(sc) != 0) {
422 1.5 ad printf("%s: unable to set system table\n", sc->sc_dv.dv_xname);
423 1.5 ad return;
424 1.5 ad }
425 1.5 ad if (iop_simple_cmd(sc, I2O_TID_IOP, I2O_EXEC_SYS_ENABLE, IOP_ICTX, 1,
426 1.5 ad 5000) != 0) {
427 1.5 ad printf("%s: unable to enable system\n", sc->sc_dv.dv_xname);
428 1.5 ad return;
429 1.5 ad }
430 1.5 ad
431 1.5 ad /*
432 1.5 ad * Set up an event handler for this IOP.
433 1.5 ad */
434 1.5 ad sc->sc_eventii.ii_dv = self;
435 1.5 ad sc->sc_eventii.ii_intr = iop_intr_event;
436 1.5 ad sc->sc_eventii.ii_flags = II_DISCARD | II_UTILITY;
437 1.5 ad sc->sc_eventii.ii_tid = I2O_TID_IOP;
438 1.5 ad if (iop_initiator_register(sc, &sc->sc_eventii) != 0) {
439 1.5 ad printf("%s: unable to register initiator", sc->sc_dv.dv_xname);
440 1.5 ad return;
441 1.5 ad }
442 1.5 ad if (iop_util_eventreg(sc, &sc->sc_eventii, 0xffffffff)) {
443 1.5 ad printf("%s: unable to register for events", sc->sc_dv.dv_xname);
444 1.5 ad return;
445 1.5 ad }
446 1.5 ad
447 1.5 ad #ifdef notyet
448 1.1 ad /* Attempt to match and attach a product-specific extension. */
449 1.1 ad ia.ia_class = I2O_CLASS_ANY;
450 1.1 ad ia.ia_tid = I2O_TID_IOP;
451 1.1 ad config_found_sm(self, &ia, iop_vendor_print, iop_submatch);
452 1.5 ad #endif
453 1.5 ad
454 1.5 ad if ((rv = iop_reconfigure(sc, 0)) != 0) {
455 1.5 ad printf("%s: configure failed (%d)\n", sc->sc_dv.dv_xname, rv);
456 1.5 ad return;
457 1.5 ad }
458 1.5 ad
459 1.5 ad sc->sc_flags |= IOP_ONLINE;
460 1.5 ad
461 1.5 ad rv = kthread_create1(iop_reconfigure_proc, sc, &sc->sc_reconf_proc,
462 1.5 ad "%s", sc->sc_dv.dv_xname);
463 1.5 ad if (rv != 0) {
464 1.5 ad printf("%s: unable to create thread (%d)",
465 1.5 ad sc->sc_dv.dv_xname, rv);
466 1.5 ad return;
467 1.5 ad }
468 1.5 ad }
469 1.5 ad
470 1.5 ad /*
471 1.5 ad * Reconfiguration thread; listens for LCT change notification, and
472 1.5 ad * initiates re-configuration if recieved.
473 1.5 ad */
474 1.5 ad static void
475 1.5 ad iop_reconfigure_proc(void *cookie)
476 1.5 ad {
477 1.5 ad struct iop_softc *sc;
478 1.5 ad struct i2o_lct lct;
479 1.5 ad u_int32_t chgind;
480 1.5 ad
481 1.5 ad sc = cookie;
482 1.5 ad
483 1.5 ad for (;;) {
484 1.5 ad chgind = le32toh(sc->sc_chgindicator) + 1;
485 1.5 ad
486 1.5 ad if (iop_lct_get0(sc, &lct, sizeof(lct), chgind) == 0) {
487 1.5 ad DPRINTF(("%s: async reconfiguration (0x%08x)\n",
488 1.5 ad sc->sc_dv.dv_xname, le32toh(lct.changeindicator)));
489 1.5 ad iop_reconfigure(sc, lct.changeindicator);
490 1.5 ad }
491 1.5 ad
492 1.5 ad tsleep(iop_reconfigure_proc, PWAIT, "iopzzz", hz * 5);
493 1.5 ad }
494 1.5 ad }
495 1.5 ad
496 1.5 ad /*
497 1.5 ad * Reconfigure: find new and removed devices.
498 1.5 ad */
499 1.5 ad static int
500 1.5 ad iop_reconfigure(struct iop_softc *sc, u_int32_t chgind)
501 1.5 ad {
502 1.5 ad struct iop_msg *im;
503 1.5 ad struct i2o_hba_bus_scan *mb;
504 1.5 ad struct i2o_lct_entry *le;
505 1.5 ad struct iop_initiator *ii, *nextii;
506 1.5 ad int rv, tid, i;
507 1.5 ad
508 1.5 ad rv = lockmgr(&sc->sc_conflock, LK_EXCLUSIVE | LK_RECURSEFAIL, NULL);
509 1.5 ad if (rv != 0) {
510 1.5 ad DPRINTF(("iop_reconfigure: unable to acquire lock\n"));
511 1.5 ad return (rv);
512 1.5 ad }
513 1.1 ad
514 1.1 ad /*
515 1.5 ad * If the reconfiguration request isn't the result of LCT change
516 1.5 ad * notification, then be more thorough: ask all bus ports to scan
517 1.5 ad * their busses. Wait up to 5 minutes for each bus port to complete
518 1.5 ad * the request.
519 1.1 ad */
520 1.5 ad if (chgind == 0) {
521 1.5 ad if ((rv = iop_lct_get(sc)) != 0) {
522 1.5 ad DPRINTF(("iop_reconfigure: unable to read LCT\n"));
523 1.5 ad goto done;
524 1.5 ad }
525 1.5 ad
526 1.5 ad le = sc->sc_lct->entry;
527 1.5 ad for (i = 0; i < sc->sc_nlctent; i++, le++) {
528 1.5 ad if ((le16toh(le->classid) & 4095) !=
529 1.5 ad I2O_CLASS_BUS_ADAPTER_PORT)
530 1.5 ad continue;
531 1.5 ad tid = le32toh(le->localtid) & 4095;
532 1.5 ad
533 1.5 ad rv = iop_msg_alloc(sc, NULL, &im, IM_NOINTR);
534 1.5 ad if (rv != 0) {
535 1.5 ad DPRINTF(("iop_reconfigure: alloc msg\n"));
536 1.5 ad goto done;
537 1.5 ad }
538 1.5 ad
539 1.5 ad mb = (struct i2o_hba_bus_scan *)im->im_msg;
540 1.5 ad mb->msgflags = I2O_MSGFLAGS(i2o_hba_bus_scan);
541 1.5 ad mb->msgfunc = I2O_MSGFUNC(tid, I2O_HBA_BUS_SCAN);
542 1.5 ad mb->msgictx = IOP_ICTX;
543 1.5 ad mb->msgtctx = im->im_tctx;
544 1.5 ad
545 1.5 ad DPRINTF(("%s: scanning bus %d\n", sc->sc_dv.dv_xname,
546 1.5 ad tid));
547 1.5 ad
548 1.5 ad rv = iop_msg_enqueue(sc, im, 5*60*1000);
549 1.5 ad iop_msg_free(sc, NULL, im);
550 1.5 ad if (rv != 0) {
551 1.5 ad DPRINTF(("iop_reconfigure: scan failed\n"));
552 1.5 ad goto done;
553 1.5 ad }
554 1.5 ad }
555 1.5 ad } else if (chgind == sc->sc_chgindicator) {
556 1.5 ad DPRINTF(("%s: LCT unchanged (async)\n", sc->sc_dv.dv_xname));
557 1.5 ad goto done;
558 1.5 ad }
559 1.5 ad
560 1.5 ad /* Re-read the LCT and determine if it has changed. */
561 1.5 ad if ((rv = iop_lct_get(sc)) != 0) {
562 1.5 ad DPRINTF(("iop_reconfigure: unable to re-read LCT\n"));
563 1.5 ad goto done;
564 1.5 ad }
565 1.5 ad DPRINTF(("%s: %d LCT entries\n", sc->sc_dv.dv_xname, sc->sc_nlctent));
566 1.5 ad
567 1.5 ad if (sc->sc_lct->changeindicator == sc->sc_chgindicator) {
568 1.5 ad DPRINTF(("%s: LCT unchanged\n", sc->sc_dv.dv_xname));
569 1.5 ad /* Nothing to do. */
570 1.5 ad rv = 0;
571 1.5 ad goto done;
572 1.5 ad }
573 1.5 ad DPRINTF(("%s: LCT changed\n", sc->sc_dv.dv_xname));
574 1.5 ad sc->sc_chgindicator = sc->sc_lct->changeindicator;
575 1.5 ad
576 1.5 ad if (sc->sc_tidmap != NULL)
577 1.5 ad free(sc->sc_tidmap, M_DEVBUF);
578 1.5 ad sc->sc_tidmap = malloc(sc->sc_nlctent * sizeof(struct iop_tidmap),
579 1.5 ad M_DEVBUF, M_NOWAIT);
580 1.5 ad memset(sc->sc_tidmap, 0, sc->sc_nlctent * sizeof(struct iop_tidmap));
581 1.5 ad
582 1.5 ad /* Match and attach child devices. */
583 1.5 ad iop_configure_devices(sc);
584 1.5 ad
585 1.5 ad for (ii = LIST_FIRST(&sc->sc_iilist); ii != NULL; ii = nextii) {
586 1.5 ad nextii = LIST_NEXT(ii, ii_list);
587 1.5 ad if ((ii->ii_flags & II_UTILITY) != 0)
588 1.5 ad continue;
589 1.5 ad if ((ii->ii_flags & II_CONFIGURED) == 0) {
590 1.5 ad ii->ii_flags |= II_CONFIGURED;
591 1.5 ad continue;
592 1.5 ad }
593 1.5 ad
594 1.5 ad /* Detach devices that were configured, but are now gone. */
595 1.5 ad for (i = 0; i < sc->sc_nlctent; i++)
596 1.5 ad if (ii->ii_tid == sc->sc_tidmap[i].it_tid)
597 1.5 ad break;
598 1.5 ad if (i == sc->sc_nlctent ||
599 1.5 ad (sc->sc_tidmap[i].it_flags & IT_CONFIGURED) == 0)
600 1.5 ad config_detach(ii->ii_dv, DETACH_FORCE);
601 1.5 ad
602 1.5 ad /*
603 1.5 ad * Tell initiators that existed before the re-configuration
604 1.5 ad * to re-configure.
605 1.5 ad */
606 1.5 ad if (ii->ii_reconfig == NULL)
607 1.5 ad continue;
608 1.5 ad if ((rv = (*ii->ii_reconfig)(ii->ii_dv)) != 0)
609 1.5 ad printf("%s: %s failed reconfigure (%d)\n",
610 1.5 ad sc->sc_dv.dv_xname, ii->ii_dv->dv_xname, rv);
611 1.5 ad }
612 1.5 ad rv = 0;
613 1.5 ad
614 1.5 ad done:
615 1.5 ad lockmgr(&sc->sc_conflock, LK_RELEASE, NULL);
616 1.5 ad return (rv);
617 1.1 ad }
618 1.1 ad
619 1.1 ad /*
620 1.5 ad * Configure I2O devices into the system.
621 1.1 ad */
622 1.1 ad static void
623 1.5 ad iop_configure_devices(struct iop_softc *sc)
624 1.1 ad {
625 1.1 ad struct iop_attach_args ia;
626 1.5 ad struct iop_initiator *ii;
627 1.1 ad const struct i2o_lct_entry *le;
628 1.5 ad int i, nent;
629 1.1 ad
630 1.1 ad nent = sc->sc_nlctent;
631 1.1 ad for (i = 0, le = sc->sc_lct->entry; i < nent; i++, le++) {
632 1.5 ad /*
633 1.5 ad * Ignore the device if it's in use.
634 1.5 ad */
635 1.5 ad if ((le32toh(le->usertid) & 4095) != 4095)
636 1.1 ad continue;
637 1.1 ad
638 1.1 ad ia.ia_class = le16toh(le->classid) & 4095;
639 1.5 ad ia.ia_tid = le32toh(le->localtid) & 4095;
640 1.1 ad
641 1.1 ad /*
642 1.5 ad * Try to configure the device only if it's not already
643 1.5 ad * configured.
644 1.1 ad */
645 1.5 ad LIST_FOREACH(ii, &sc->sc_iilist, ii_list)
646 1.5 ad if (ia.ia_tid == ii->ii_tid)
647 1.5 ad break;
648 1.5 ad if (ii != NULL)
649 1.5 ad continue;
650 1.5 ad
651 1.5 ad if (config_found_sm(&sc->sc_dv, &ia, iop_print, iop_submatch))
652 1.5 ad sc->sc_tidmap[i].it_flags |= IT_CONFIGURED;
653 1.1 ad }
654 1.1 ad }
655 1.1 ad
656 1.1 ad static void
657 1.1 ad iop_devinfo(int class, char *devinfo)
658 1.1 ad {
659 1.1 ad #ifdef I2OVERBOSE
660 1.1 ad int i;
661 1.1 ad
662 1.1 ad for (i = 0; i < sizeof(iop_class) / sizeof(iop_class[0]); i++)
663 1.1 ad if (class == iop_class[i].ic_class)
664 1.1 ad break;
665 1.1 ad
666 1.1 ad if (i == sizeof(iop_class) / sizeof(iop_class[0]))
667 1.1 ad sprintf(devinfo, "device (class 0x%x)", class);
668 1.1 ad else
669 1.1 ad strcpy(devinfo, iop_class[i].ic_caption);
670 1.1 ad #else
671 1.1 ad
672 1.1 ad sprintf(devinfo, "device (class 0x%x)", class);
673 1.1 ad #endif
674 1.1 ad }
675 1.1 ad
676 1.1 ad static int
677 1.1 ad iop_print(void *aux, const char *pnp)
678 1.1 ad {
679 1.1 ad struct iop_attach_args *ia;
680 1.1 ad char devinfo[256];
681 1.1 ad
682 1.1 ad ia = aux;
683 1.1 ad
684 1.1 ad if (pnp != NULL) {
685 1.1 ad iop_devinfo(ia->ia_class, devinfo);
686 1.1 ad printf("%s at %s", devinfo, pnp);
687 1.1 ad }
688 1.1 ad printf(" tid %d", ia->ia_tid);
689 1.1 ad return (UNCONF);
690 1.1 ad }
691 1.1 ad
692 1.5 ad #ifdef notyet
693 1.1 ad static int
694 1.1 ad iop_vendor_print(void *aux, const char *pnp)
695 1.1 ad {
696 1.1 ad
697 1.1 ad if (pnp != NULL)
698 1.1 ad printf("vendor specific extension at %s", pnp);
699 1.1 ad return (UNCONF);
700 1.1 ad }
701 1.5 ad #endif
702 1.1 ad
703 1.1 ad static int
704 1.1 ad iop_submatch(struct device *parent, struct cfdata *cf, void *aux)
705 1.1 ad {
706 1.1 ad struct iop_attach_args *ia;
707 1.1 ad
708 1.1 ad ia = aux;
709 1.1 ad
710 1.1 ad if (cf->iopcf_tid != IOPCF_TID_DEFAULT && cf->iopcf_tid != ia->ia_tid)
711 1.1 ad return (0);
712 1.1 ad
713 1.1 ad return ((*cf->cf_attach->ca_match)(parent, cf, aux));
714 1.1 ad }
715 1.1 ad
716 1.1 ad /*
717 1.1 ad * Shut down all configured IOPs.
718 1.1 ad */
719 1.1 ad static void
720 1.1 ad iop_shutdown(void *junk)
721 1.1 ad {
722 1.1 ad struct iop_softc *sc;
723 1.1 ad int i;
724 1.1 ad
725 1.1 ad printf("shutting down iop devices... ");
726 1.1 ad
727 1.1 ad for (i = 0; i < iop_cd.cd_ndevs; i++) {
728 1.1 ad if ((sc = device_lookup(&iop_cd, i)) == NULL)
729 1.1 ad continue;
730 1.5 ad if ((sc->sc_flags & IOP_ONLINE) == 0)
731 1.5 ad continue;
732 1.5 ad iop_simple_cmd(sc, I2O_TID_IOP, I2O_EXEC_SYS_QUIESCE, IOP_ICTX,
733 1.5 ad 0, 5000);
734 1.5 ad iop_simple_cmd(sc, I2O_TID_IOP, I2O_EXEC_IOP_CLEAR, IOP_ICTX,
735 1.5 ad 0, 5000);
736 1.1 ad }
737 1.1 ad
738 1.1 ad /* Wait. Some boards could still be flushing, stupidly enough. */
739 1.1 ad delay(5000*1000);
740 1.1 ad printf(" done\n");
741 1.1 ad }
742 1.1 ad
743 1.1 ad /*
744 1.1 ad * Retrieve adapter status.
745 1.1 ad */
746 1.1 ad static int
747 1.1 ad iop_status_get(struct iop_softc *sc)
748 1.1 ad {
749 1.1 ad struct iop_msg *im;
750 1.1 ad struct i2o_exec_status_get *mb;
751 1.1 ad int rv, s;
752 1.1 ad
753 1.1 ad if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOWAIT | IM_NOICTX)) != 0)
754 1.1 ad return (rv);
755 1.1 ad
756 1.1 ad mb = (struct i2o_exec_status_get *)im->im_msg;
757 1.1 ad mb->msgflags = I2O_MSGFLAGS(i2o_exec_status_get);
758 1.1 ad mb->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_STATUS_GET);
759 1.1 ad mb->reserved[0] = 0;
760 1.1 ad mb->reserved[1] = 0;
761 1.1 ad mb->reserved[2] = 0;
762 1.1 ad mb->reserved[3] = 0;
763 1.1 ad mb->addrlow = kvtop((caddr_t)&sc->sc_status); /* XXX */
764 1.1 ad mb->addrhigh = 0;
765 1.1 ad mb->length = sizeof(sc->sc_status);
766 1.1 ad
767 1.1 ad s = splbio();
768 1.5 ad memset(&sc->sc_status, 0, sizeof(sc->sc_status));
769 1.1 ad
770 1.1 ad if ((rv = iop_msg_send(sc, im, 0)) != 0) {
771 1.1 ad splx(s);
772 1.1 ad iop_msg_free(sc, NULL, im);
773 1.1 ad return (rv);
774 1.1 ad }
775 1.1 ad
776 1.1 ad /* XXX */
777 1.1 ad POLL(2500, *((volatile u_char *)&sc->sc_status.syncbyte) == 0xff);
778 1.1 ad
779 1.1 ad splx(s);
780 1.1 ad iop_msg_free(sc, NULL, im);
781 1.1 ad return (*((volatile u_char *)&sc->sc_status.syncbyte) != 0xff);
782 1.1 ad }
783 1.1 ad
784 1.1 ad /*
785 1.1 ad * Initalize and populate the adapter's outbound FIFO.
786 1.1 ad */
787 1.1 ad static int
788 1.1 ad iop_ofifo_init(struct iop_softc *sc)
789 1.1 ad {
790 1.1 ad struct iop_msg *im;
791 1.1 ad volatile u_int32_t status;
792 1.1 ad bus_addr_t addr;
793 1.5 ad bus_dma_segment_t seg;
794 1.1 ad struct i2o_exec_outbound_init *mb;
795 1.5 ad int i, rseg, rv;
796 1.1 ad
797 1.1 ad if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOWAIT | IM_NOICTX)) != 0)
798 1.1 ad return (rv);
799 1.1 ad
800 1.1 ad mb = (struct i2o_exec_outbound_init *)im->im_msg;
801 1.1 ad mb->msgflags = I2O_MSGFLAGS(i2o_exec_outbound_init);
802 1.1 ad mb->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_OUTBOUND_INIT);
803 1.1 ad mb->msgictx = IOP_ICTX;
804 1.1 ad mb->msgtctx = im->im_tctx;
805 1.4 thorpej mb->pagesize = PAGE_SIZE;
806 1.5 ad mb->flags = 0x80 | ((IOP_MAX_REPLY_SIZE >> 2) << 16); /* XXX */
807 1.1 ad
808 1.1 ad status = 0;
809 1.5 ad
810 1.5 ad /*
811 1.5 ad * The I2O spec says that there are two SGLs: one for the status
812 1.5 ad * word, and one for a list of discarded MFAs. It continues to say
813 1.5 ad * that if you don't want to get the list of MFAs, an IGNORE SGL is
814 1.5 ad * necessary; this isn't the case (and in fact appears to be a bad
815 1.5 ad * thing).
816 1.5 ad */
817 1.1 ad iop_msg_map(sc, im, (void *)&status, sizeof(status), 0);
818 1.1 ad if ((rv = iop_msg_send(sc, im, 0)) != 0) {
819 1.1 ad iop_msg_free(sc, NULL, im);
820 1.1 ad return (rv);
821 1.1 ad }
822 1.1 ad iop_msg_unmap(sc, im);
823 1.1 ad iop_msg_free(sc, NULL, im);
824 1.1 ad
825 1.5 ad /* XXX */
826 1.5 ad POLL(5000, status == I2O_EXEC_OUTBOUND_INIT_COMPLETE);
827 1.5 ad if (status != I2O_EXEC_OUTBOUND_INIT_COMPLETE) {
828 1.5 ad printf("%s: outbound FIFO init failed\n", sc->sc_dv.dv_xname);
829 1.5 ad return (EIO);
830 1.1 ad }
831 1.1 ad
832 1.1 ad /* If we need to allocate DMA safe memory, do it now. */
833 1.1 ad if (sc->sc_rep_phys == 0) {
834 1.5 ad sc->sc_rep_size = sc->sc_maxreplycnt * IOP_MAX_REPLY_SIZE;
835 1.5 ad
836 1.5 ad rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_rep_size, PAGE_SIZE,
837 1.5 ad 0, &seg, 1, &rseg, BUS_DMA_NOWAIT);
838 1.5 ad if (rv != 0) {
839 1.5 ad printf("%s: dma alloc = %d\n", sc->sc_dv.dv_xname,
840 1.5 ad rv);
841 1.5 ad return (rv);
842 1.5 ad }
843 1.5 ad
844 1.5 ad rv = bus_dmamem_map(sc->sc_dmat, &seg, rseg, sc->sc_rep_size,
845 1.5 ad &sc->sc_rep, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
846 1.5 ad if (rv != 0) {
847 1.5 ad printf("%s: dma map = %d\n", sc->sc_dv.dv_xname, rv);
848 1.5 ad return (rv);
849 1.5 ad }
850 1.5 ad
851 1.5 ad rv = bus_dmamap_create(sc->sc_dmat, sc->sc_rep_size, 1,
852 1.5 ad sc->sc_rep_size, 0, BUS_DMA_NOWAIT, &sc->sc_rep_dmamap);
853 1.5 ad if (rv != 0) {
854 1.5 ad printf("%s: dma create = %d\n", sc->sc_dv.dv_xname, rv);
855 1.5 ad return (rv);
856 1.5 ad }
857 1.5 ad
858 1.5 ad rv = bus_dmamap_load(sc->sc_dmat, sc->sc_rep_dmamap, sc->sc_rep,
859 1.5 ad sc->sc_rep_size, NULL, BUS_DMA_NOWAIT);
860 1.5 ad if (rv != 0) {
861 1.5 ad printf("%s: dma load = %d\n", sc->sc_dv.dv_xname, rv);
862 1.5 ad return (rv);
863 1.5 ad }
864 1.5 ad
865 1.5 ad sc->sc_rep_phys = sc->sc_rep_dmamap->dm_segs[0].ds_addr;
866 1.1 ad }
867 1.1 ad
868 1.1 ad /* Populate the outbound FIFO. */
869 1.5 ad for (i = sc->sc_maxreplycnt, addr = sc->sc_rep_phys; i != 0; i--) {
870 1.5 ad iop_outl(sc, IOP_REG_OFIFO, (u_int32_t)addr);
871 1.5 ad addr += IOP_MAX_REPLY_SIZE;
872 1.1 ad }
873 1.1 ad
874 1.1 ad return (0);
875 1.1 ad }
876 1.1 ad
877 1.1 ad /*
878 1.1 ad * Read the specified number of bytes from the IOP's hardware resource table.
879 1.1 ad */
880 1.1 ad static int
881 1.1 ad iop_hrt_get0(struct iop_softc *sc, struct i2o_hrt *hrt, int size)
882 1.1 ad {
883 1.1 ad struct iop_msg *im;
884 1.1 ad int rv;
885 1.1 ad struct i2o_exec_hrt_get *mb;
886 1.1 ad
887 1.5 ad if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOINTR)) != 0)
888 1.1 ad return (rv);
889 1.1 ad
890 1.1 ad mb = (struct i2o_exec_hrt_get *)im->im_msg;
891 1.1 ad mb->msgflags = I2O_MSGFLAGS(i2o_exec_hrt_get);
892 1.1 ad mb->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_HRT_GET);
893 1.1 ad mb->msgictx = IOP_ICTX;
894 1.1 ad mb->msgtctx = im->im_tctx;
895 1.1 ad
896 1.1 ad iop_msg_map(sc, im, hrt, size, 0);
897 1.5 ad rv = iop_msg_enqueue(sc, im, 5000);
898 1.1 ad iop_msg_unmap(sc, im);
899 1.1 ad iop_msg_free(sc, NULL, im);
900 1.1 ad return (rv);
901 1.1 ad }
902 1.1 ad
903 1.1 ad /*
904 1.5 ad * Read the IOP's hardware resource table.
905 1.1 ad */
906 1.1 ad static int
907 1.1 ad iop_hrt_get(struct iop_softc *sc)
908 1.1 ad {
909 1.1 ad struct i2o_hrt hrthdr, *hrt;
910 1.1 ad int size, rv;
911 1.1 ad
912 1.1 ad if ((rv = iop_hrt_get0(sc, &hrthdr, sizeof(hrthdr))) != 0)
913 1.1 ad return (rv);
914 1.1 ad
915 1.5 ad DPRINTF(("%s: %d hrt entries\n", sc->sc_dv.dv_xname,
916 1.5 ad le16toh(hrthdr.numentries)));
917 1.5 ad
918 1.5 ad size = sizeof(struct i2o_hrt) +
919 1.5 ad (htole32(hrthdr.numentries) - 1) * sizeof(struct i2o_hrt_entry);
920 1.1 ad hrt = (struct i2o_hrt *)malloc(size, M_DEVBUF, M_NOWAIT);
921 1.1 ad
922 1.1 ad if ((rv = iop_hrt_get0(sc, hrt, size)) != 0) {
923 1.1 ad free(hrt, M_DEVBUF);
924 1.1 ad return (rv);
925 1.1 ad }
926 1.1 ad
927 1.1 ad if (sc->sc_hrt != NULL)
928 1.1 ad free(sc->sc_hrt, M_DEVBUF);
929 1.1 ad sc->sc_hrt = hrt;
930 1.1 ad return (0);
931 1.1 ad }
932 1.1 ad
933 1.1 ad /*
934 1.1 ad * Request the specified number of bytes from the IOP's logical
935 1.5 ad * configuration table. If a change indicator is specified, this
936 1.5 ad * is an verbatim notification request, so the caller is prepared
937 1.5 ad * to wait indefinitely.
938 1.1 ad */
939 1.1 ad static int
940 1.5 ad iop_lct_get0(struct iop_softc *sc, struct i2o_lct *lct, int size,
941 1.5 ad u_int32_t chgind)
942 1.1 ad {
943 1.1 ad struct iop_msg *im;
944 1.1 ad struct i2o_exec_lct_notify *mb;
945 1.1 ad int rv;
946 1.1 ad
947 1.1 ad if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOINTR)) != 0)
948 1.1 ad return (rv);
949 1.5 ad
950 1.1 ad memset(lct, 0, size);
951 1.5 ad memset(im->im_msg, 0, sizeof(im->im_msg));
952 1.1 ad
953 1.1 ad mb = (struct i2o_exec_lct_notify *)im->im_msg;
954 1.1 ad mb->msgflags = I2O_MSGFLAGS(i2o_exec_lct_notify);
955 1.1 ad mb->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_LCT_NOTIFY);
956 1.1 ad mb->msgictx = IOP_ICTX;
957 1.1 ad mb->msgtctx = im->im_tctx;
958 1.1 ad mb->classid = I2O_CLASS_ANY;
959 1.5 ad mb->changeindicator = chgind;
960 1.5 ad
961 1.5 ad DPRINTF(("iop_lct_get0: reading LCT\n"));
962 1.1 ad
963 1.1 ad iop_msg_map(sc, im, lct, size, 0);
964 1.5 ad rv = iop_msg_enqueue(sc, im, (chgind == 0 ? 120*1000 : 0));
965 1.1 ad iop_msg_unmap(sc, im);
966 1.1 ad iop_msg_free(sc, NULL, im);
967 1.1 ad return (rv);
968 1.1 ad }
969 1.1 ad
970 1.1 ad /*
971 1.5 ad * Read the IOP's logical configuration table. Access to our private copy
972 1.5 ad * of the LCT must be serialized through sc_conflock; this copy should match
973 1.5 ad * the current Un*x device configuration.
974 1.1 ad */
975 1.1 ad int
976 1.1 ad iop_lct_get(struct iop_softc *sc)
977 1.1 ad {
978 1.5 ad int esize, size, rv;
979 1.5 ad struct i2o_lct *lct;
980 1.1 ad
981 1.5 ad esize = le32toh(sc->sc_status.expectedlctsize);
982 1.5 ad lct = (struct i2o_lct *)malloc(esize, M_DEVBUF, M_WAITOK);
983 1.5 ad if (lct == NULL)
984 1.1 ad return (ENOMEM);
985 1.1 ad
986 1.5 ad if ((rv = iop_lct_get0(sc, lct, esize, 0)) != 0) {
987 1.1 ad free(lct, M_DEVBUF);
988 1.1 ad return (rv);
989 1.1 ad }
990 1.1 ad
991 1.5 ad size = le16toh(lct->tablesize) << 2;
992 1.5 ad if (esize != size) {
993 1.1 ad free(lct, M_DEVBUF);
994 1.5 ad lct = (struct i2o_lct *)malloc(size, M_DEVBUF, M_WAITOK);
995 1.5 ad if (lct == NULL)
996 1.5 ad return (ENOMEM);
997 1.5 ad
998 1.5 ad if ((rv = iop_lct_get0(sc, lct, size, 0)) != 0) {
999 1.5 ad free(lct, M_DEVBUF);
1000 1.5 ad return (rv);
1001 1.5 ad }
1002 1.1 ad }
1003 1.5 ad
1004 1.5 ad /* Swap in the new LCT. */
1005 1.1 ad if (sc->sc_lct != NULL)
1006 1.1 ad free(sc->sc_lct, M_DEVBUF);
1007 1.1 ad sc->sc_lct = lct;
1008 1.1 ad sc->sc_nlctent = ((le16toh(sc->sc_lct->tablesize) << 2) -
1009 1.1 ad sizeof(struct i2o_lct) + sizeof(struct i2o_lct_entry)) /
1010 1.1 ad sizeof(struct i2o_lct_entry);
1011 1.1 ad return (0);
1012 1.1 ad }
1013 1.1 ad
1014 1.1 ad /*
1015 1.5 ad * Request the specified parameter group from the target.
1016 1.1 ad */
1017 1.1 ad int
1018 1.5 ad iop_param_op(struct iop_softc *sc, int tid, int write, int group, void *buf,
1019 1.5 ad int size)
1020 1.1 ad {
1021 1.1 ad struct iop_msg *im;
1022 1.5 ad struct i2o_util_params_op *mb;
1023 1.5 ad int rv, func, op;
1024 1.1 ad struct {
1025 1.1 ad struct i2o_param_op_list_header olh;
1026 1.1 ad struct i2o_param_op_all_template oat;
1027 1.1 ad } req;
1028 1.1 ad
1029 1.1 ad if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOINTR)) != 0)
1030 1.1 ad return (rv);
1031 1.1 ad
1032 1.5 ad if (write) {
1033 1.5 ad func = I2O_UTIL_PARAMS_SET;
1034 1.5 ad op = I2O_PARAMS_OP_FIELD_SET;
1035 1.5 ad } else {
1036 1.5 ad func = I2O_UTIL_PARAMS_GET;
1037 1.5 ad op = I2O_PARAMS_OP_FIELD_GET;
1038 1.5 ad }
1039 1.5 ad
1040 1.5 ad mb = (struct i2o_util_params_op *)im->im_msg;
1041 1.5 ad mb->msgflags = I2O_MSGFLAGS(i2o_util_params_op);
1042 1.5 ad mb->msgfunc = I2O_MSGFUNC(tid, func);
1043 1.1 ad mb->msgictx = IOP_ICTX;
1044 1.1 ad mb->msgtctx = im->im_tctx;
1045 1.1 ad mb->flags = 0;
1046 1.1 ad
1047 1.1 ad req.olh.count = htole16(1);
1048 1.1 ad req.olh.reserved = htole16(0);
1049 1.5 ad req.oat.operation = htole16(op);
1050 1.1 ad req.oat.fieldcount = htole16(0xffff);
1051 1.1 ad req.oat.group = htole16(group);
1052 1.1 ad
1053 1.5 ad memset(buf, 0, size);
1054 1.1 ad iop_msg_map(sc, im, &req, sizeof(req), 1);
1055 1.5 ad iop_msg_map(sc, im, buf, size, write);
1056 1.1 ad
1057 1.5 ad rv = iop_msg_enqueue(sc, im, 5000);
1058 1.1 ad iop_msg_unmap(sc, im);
1059 1.1 ad iop_msg_free(sc, NULL, im);
1060 1.1 ad return (rv);
1061 1.1 ad }
1062 1.1 ad
1063 1.1 ad /*
1064 1.5 ad * Execute a simple command (no parameters).
1065 1.1 ad */
1066 1.1 ad int
1067 1.5 ad iop_simple_cmd(struct iop_softc *sc, int tid, int function, int ictx,
1068 1.5 ad int async, int timo)
1069 1.1 ad {
1070 1.1 ad struct iop_msg *im;
1071 1.1 ad struct i2o_msg *mb;
1072 1.5 ad int rv, fl;
1073 1.1 ad
1074 1.5 ad fl = (async != 0 ? IM_NOWAIT : 0);
1075 1.5 ad if ((rv = iop_msg_alloc(sc, NULL, &im, fl | IM_NOINTR)) != 0)
1076 1.1 ad return (rv);
1077 1.1 ad
1078 1.1 ad mb = (struct i2o_msg *)im->im_msg;
1079 1.1 ad mb->msgflags = I2O_MSGFLAGS(i2o_msg);
1080 1.1 ad mb->msgfunc = I2O_MSGFUNC(tid, function);
1081 1.1 ad mb->msgictx = ictx;
1082 1.1 ad mb->msgtctx = im->im_tctx;
1083 1.1 ad
1084 1.5 ad if (async)
1085 1.5 ad rv = iop_msg_enqueue(sc, im, timo);
1086 1.5 ad else
1087 1.5 ad rv = iop_msg_send(sc, im, timo);
1088 1.1 ad iop_msg_free(sc, NULL, im);
1089 1.1 ad return (rv);
1090 1.1 ad }
1091 1.1 ad
1092 1.1 ad /*
1093 1.5 ad * Post the system table to the IOP.
1094 1.1 ad */
1095 1.1 ad static int
1096 1.1 ad iop_systab_set(struct iop_softc *sc)
1097 1.1 ad {
1098 1.5 ad struct i2o_exec_sys_tab_set *mb;
1099 1.1 ad struct iop_msg *im;
1100 1.1 ad u_int32_t mema[2], ioa[2];
1101 1.1 ad int rv;
1102 1.1 ad
1103 1.5 ad if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOINTR)) != 0)
1104 1.1 ad return (rv);
1105 1.1 ad
1106 1.1 ad mb = (struct i2o_exec_sys_tab_set *)im->im_msg;
1107 1.1 ad mb->msgflags = I2O_MSGFLAGS(i2o_exec_sys_tab_set);
1108 1.1 ad mb->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_SYS_TAB_SET);
1109 1.1 ad mb->msgictx = IOP_ICTX;
1110 1.1 ad mb->msgtctx = im->im_tctx;
1111 1.5 ad mb->iopid = (sc->sc_dv.dv_unit + 2) << 12;
1112 1.5 ad mb->segnumber = 0;
1113 1.5 ad
1114 1.5 ad /* XXX This is questionable, but better than nothing... */
1115 1.5 ad mema[0] = le32toh(sc->sc_status.currentprivmembase);
1116 1.5 ad mema[1] = le32toh(sc->sc_status.currentprivmemsize);
1117 1.5 ad ioa[0] = le32toh(sc->sc_status.currentpriviobase);
1118 1.5 ad ioa[1] = le32toh(sc->sc_status.currentpriviosize);
1119 1.1 ad
1120 1.5 ad iop_msg_map(sc, im, iop_systab, iop_systab_size, 1);
1121 1.1 ad iop_msg_map(sc, im, mema, sizeof(mema), 1);
1122 1.1 ad iop_msg_map(sc, im, ioa, sizeof(ioa), 1);
1123 1.1 ad
1124 1.5 ad rv = iop_msg_enqueue(sc, im, 5000);
1125 1.1 ad iop_msg_unmap(sc, im);
1126 1.1 ad iop_msg_free(sc, NULL, im);
1127 1.1 ad return (rv);
1128 1.1 ad }
1129 1.1 ad
1130 1.1 ad /*
1131 1.1 ad * Reset the adapter. Must be called with interrupts disabled.
1132 1.1 ad */
1133 1.1 ad static int
1134 1.1 ad iop_reset(struct iop_softc *sc)
1135 1.1 ad {
1136 1.1 ad struct iop_msg *im;
1137 1.1 ad volatile u_int32_t sw;
1138 1.1 ad u_int32_t mfa;
1139 1.1 ad struct i2o_exec_iop_reset *mb;
1140 1.1 ad int rv;
1141 1.1 ad
1142 1.1 ad if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOWAIT | IM_NOICTX)) != 0)
1143 1.1 ad return (rv);
1144 1.1 ad
1145 1.1 ad sw = 0;
1146 1.1 ad
1147 1.1 ad mb = (struct i2o_exec_iop_reset *)im->im_msg;
1148 1.1 ad mb->msgflags = I2O_MSGFLAGS(i2o_exec_iop_reset);
1149 1.1 ad mb->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_IOP_RESET);
1150 1.1 ad mb->reserved[0] = 0;
1151 1.1 ad mb->reserved[1] = 0;
1152 1.1 ad mb->reserved[2] = 0;
1153 1.1 ad mb->reserved[3] = 0;
1154 1.1 ad mb->statuslow = kvtop((caddr_t)&sw); /* XXX */
1155 1.1 ad mb->statushigh = 0;
1156 1.1 ad
1157 1.1 ad if ((rv = iop_msg_send(sc, im, 0)))
1158 1.1 ad return (rv);
1159 1.1 ad iop_msg_free(sc, NULL, im);
1160 1.1 ad
1161 1.1 ad POLL(2500, sw != 0); /* XXX */
1162 1.1 ad if (sw != I2O_RESET_IN_PROGRESS) {
1163 1.1 ad printf("%s: reset rejected\n", sc->sc_dv.dv_xname);
1164 1.1 ad return (EIO);
1165 1.1 ad }
1166 1.1 ad
1167 1.1 ad /*
1168 1.5 ad * IOP is now in the INIT state. Wait no more than 10 seconds for
1169 1.1 ad * the inbound queue to become responsive.
1170 1.1 ad */
1171 1.5 ad POLL(10000, (mfa = iop_inl(sc, IOP_REG_IFIFO)) != IOP_MFA_EMPTY);
1172 1.1 ad if (mfa == IOP_MFA_EMPTY) {
1173 1.1 ad printf("%s: reset failed\n", sc->sc_dv.dv_xname);
1174 1.1 ad return (EIO);
1175 1.1 ad }
1176 1.1 ad
1177 1.5 ad if (sw == I2O_RESET_REJECTED)
1178 1.5 ad printf("%s: reset rejected?\n", sc->sc_dv.dv_xname);
1179 1.5 ad
1180 1.1 ad iop_release_mfa(sc, mfa);
1181 1.1 ad return (0);
1182 1.1 ad }
1183 1.1 ad
1184 1.1 ad /*
1185 1.1 ad * Register a new initiator.
1186 1.1 ad */
1187 1.1 ad int
1188 1.1 ad iop_initiator_register(struct iop_softc *sc, struct iop_initiator *ii)
1189 1.1 ad {
1190 1.5 ad static int ictx;
1191 1.5 ad static int stctx;
1192 1.5 ad
1193 1.5 ad /* 0 is reserved for system messages. */
1194 1.5 ad ii->ii_ictx = ++ictx;
1195 1.5 ad ii->ii_stctx = ++stctx | 0x80000000;
1196 1.1 ad
1197 1.5 ad LIST_INSERT_HEAD(&sc->sc_iilist, ii, ii_list);
1198 1.5 ad LIST_INSERT_HEAD(IOP_ICTXHASH(ii->ii_ictx), ii, ii_hash);
1199 1.1 ad
1200 1.1 ad return (0);
1201 1.1 ad }
1202 1.1 ad
1203 1.1 ad /*
1204 1.1 ad * Unregister an initiator.
1205 1.1 ad */
1206 1.1 ad void
1207 1.1 ad iop_initiator_unregister(struct iop_softc *sc, struct iop_initiator *ii)
1208 1.1 ad {
1209 1.1 ad
1210 1.5 ad LIST_REMOVE(ii, ii_list);
1211 1.5 ad LIST_REMOVE(ii, ii_hash);
1212 1.1 ad }
1213 1.1 ad
1214 1.1 ad /*
1215 1.5 ad * Handle a reply frame from the adapter.
1216 1.1 ad */
1217 1.1 ad static int
1218 1.5 ad iop_handle_reply(struct iop_softc *sc, u_int32_t rmfa)
1219 1.1 ad {
1220 1.1 ad struct iop_msg *im;
1221 1.1 ad struct i2o_reply *rb;
1222 1.1 ad struct iop_initiator *ii;
1223 1.5 ad u_int off, ictx, tctx, status, size;
1224 1.1 ad
1225 1.1 ad off = (int)(rmfa - sc->sc_rep_phys);
1226 1.1 ad rb = (struct i2o_reply *)(sc->sc_rep + off);
1227 1.1 ad
1228 1.5 ad /* Perform reply queue DMA synchronisation... */
1229 1.1 ad bus_dmamap_sync(sc->sc_dmat, sc->sc_rep_dmamap, off, IOP_MAX_MSG_SIZE,
1230 1.1 ad BUS_DMASYNC_POSTREAD);
1231 1.1 ad if (--sc->sc_stat.is_cur_hwqueue != 0)
1232 1.1 ad bus_dmamap_sync(sc->sc_dmat, sc->sc_rep_dmamap,
1233 1.1 ad 0, sc->sc_rep_size, BUS_DMASYNC_PREREAD);
1234 1.1 ad
1235 1.1 ad #ifdef I2ODEBUG
1236 1.1 ad if ((le32toh(rb->msgflags) & I2O_MSGFLAGS_64BIT) != 0)
1237 1.5 ad panic("iop_handle_reply: 64-bit reply");
1238 1.1 ad #endif
1239 1.1 ad /*
1240 1.1 ad * Find the initiator.
1241 1.1 ad */
1242 1.1 ad ictx = le32toh(rb->msgictx);
1243 1.1 ad if (ictx == IOP_ICTX)
1244 1.1 ad ii = NULL;
1245 1.1 ad else {
1246 1.5 ad ii = LIST_FIRST(IOP_ICTXHASH(ictx));
1247 1.5 ad for (; ii != NULL; ii = LIST_NEXT(ii, ii_hash))
1248 1.5 ad if (ii->ii_ictx == ictx)
1249 1.5 ad break;
1250 1.5 ad if (ii == NULL) {
1251 1.1 ad #ifdef I2ODEBUG
1252 1.5 ad iop_reply_print(sc, NULL, rb);
1253 1.1 ad #endif
1254 1.5 ad printf("%s: WARNING: bad ictx returned (%x)",
1255 1.5 ad sc->sc_dv.dv_xname, ictx);
1256 1.5 ad
1257 1.5 ad /* Return the reply frame to the IOP's outbound FIFO. */
1258 1.5 ad iop_outl(sc, IOP_REG_OFIFO, rmfa);
1259 1.5 ad return (-1);
1260 1.5 ad }
1261 1.1 ad }
1262 1.1 ad
1263 1.1 ad status = rb->reqstatus;
1264 1.1 ad
1265 1.1 ad if (ii == NULL || (ii->ii_flags & II_DISCARD) == 0) {
1266 1.1 ad /*
1267 1.1 ad * This initiator tracks state using message wrappers.
1268 1.1 ad *
1269 1.1 ad * Find the originating message wrapper, and if requested
1270 1.1 ad * notify the initiator.
1271 1.1 ad */
1272 1.1 ad tctx = le32toh(rb->msgtctx);
1273 1.5 ad im = TAILQ_FIRST(IOP_TCTXHASH(tctx));
1274 1.1 ad for (; im != NULL; im = TAILQ_NEXT(im, im_hash))
1275 1.1 ad if (im->im_tctx == tctx)
1276 1.1 ad break;
1277 1.5 ad if (im == NULL || (im->im_flags & IM_ALLOCED) == 0) {
1278 1.5 ad #ifdef I2ODEBUG
1279 1.5 ad iop_reply_print(sc, NULL, rb);
1280 1.5 ad #endif
1281 1.5 ad printf("%s: WARNING: bad tctx returned (%x, %p)",
1282 1.1 ad sc->sc_dv.dv_xname, tctx, im);
1283 1.5 ad
1284 1.5 ad /* Return the reply frame to the IOP's outbound FIFO. */
1285 1.5 ad iop_outl(sc, IOP_REG_OFIFO, rmfa);
1286 1.5 ad return (-1);
1287 1.5 ad }
1288 1.1 ad #ifdef I2ODEBUG
1289 1.1 ad if ((im->im_flags & IM_REPLIED) != 0)
1290 1.5 ad panic("%s: dup reply", sc->sc_dv.dv_xname);
1291 1.1 ad #endif
1292 1.1 ad
1293 1.1 ad im->im_flags |= IM_REPLIED;
1294 1.1 ad
1295 1.1 ad #ifdef I2ODEBUG
1296 1.1 ad if (rb->reqstatus != 0)
1297 1.1 ad iop_reply_print(sc, im, rb);
1298 1.1 ad #endif
1299 1.1 ad /* Notify the initiator. */
1300 1.5 ad if ((im->im_flags & IM_WAITING) != 0) {
1301 1.5 ad size = (le32toh(rb->msgflags) >> 14) & ~3;
1302 1.5 ad if (size > IOP_MAX_REPLY_SIZE)
1303 1.5 ad size = IOP_MAX_REPLY_SIZE;
1304 1.5 ad memcpy(im->im_msg, rb, size);
1305 1.1 ad wakeup(im);
1306 1.5 ad } else if ((im->im_flags & IM_NOINTR) == 0)
1307 1.1 ad (*ii->ii_intr)(ii->ii_dv, im, rb);
1308 1.1 ad } else {
1309 1.1 ad /*
1310 1.1 ad * This initiator discards message wrappers.
1311 1.1 ad *
1312 1.1 ad * Simply pass the reply frame to the initiator.
1313 1.1 ad */
1314 1.1 ad (*ii->ii_intr)(ii->ii_dv, NULL, rb);
1315 1.1 ad }
1316 1.1 ad
1317 1.1 ad /* Return the reply frame to the IOP's outbound FIFO. */
1318 1.5 ad iop_outl(sc, IOP_REG_OFIFO, rmfa);
1319 1.1 ad
1320 1.1 ad /* Run the queue. */
1321 1.1 ad if ((im = SIMPLEQ_FIRST(&sc->sc_queue)) != NULL)
1322 1.5 ad iop_msg_enqueue(sc, im, 0);
1323 1.1 ad
1324 1.1 ad return (status);
1325 1.1 ad }
1326 1.1 ad
1327 1.1 ad /*
1328 1.1 ad * Handle an interrupt from the adapter.
1329 1.1 ad */
1330 1.1 ad int
1331 1.1 ad iop_intr(void *arg)
1332 1.1 ad {
1333 1.1 ad struct iop_softc *sc;
1334 1.5 ad u_int32_t rmfa;
1335 1.1 ad
1336 1.1 ad sc = arg;
1337 1.1 ad
1338 1.5 ad if ((iop_inl(sc, IOP_REG_INTR_STATUS) & IOP_INTR_OFIFO) == 0)
1339 1.5 ad return (0);
1340 1.5 ad
1341 1.5 ad for (;;) {
1342 1.5 ad /* Double read to account for IOP bug. */
1343 1.5 ad if ((rmfa = iop_inl(sc, IOP_REG_OFIFO)) == IOP_MFA_EMPTY &&
1344 1.5 ad (rmfa = iop_inl(sc, IOP_REG_OFIFO)) == IOP_MFA_EMPTY)
1345 1.5 ad break;
1346 1.5 ad iop_handle_reply(sc, rmfa);
1347 1.1 ad }
1348 1.1 ad
1349 1.5 ad return (1);
1350 1.5 ad }
1351 1.5 ad
1352 1.5 ad /*
1353 1.5 ad * Handle an event signalled by the executive.
1354 1.5 ad */
1355 1.5 ad static void
1356 1.5 ad iop_intr_event(struct device *dv, struct iop_msg *im, void *reply)
1357 1.5 ad {
1358 1.5 ad struct i2o_util_event_register_reply *rb;
1359 1.5 ad struct iop_softc *sc;
1360 1.5 ad u_int event;
1361 1.5 ad
1362 1.5 ad sc = (struct iop_softc *)dv;
1363 1.5 ad rb = reply;
1364 1.5 ad event = le32toh(rb->event);
1365 1.5 ad
1366 1.5 ad #ifndef I2ODEBUG
1367 1.5 ad if (event == I2O_EVENT_GEN_EVENT_MASK_MODIFIED)
1368 1.5 ad return;
1369 1.1 ad #endif
1370 1.5 ad
1371 1.5 ad printf("%s: event 0x%08x received\n", dv->dv_xname, event);
1372 1.1 ad }
1373 1.1 ad
1374 1.1 ad /*
1375 1.1 ad * Allocate a message wrapper.
1376 1.1 ad */
1377 1.1 ad int
1378 1.1 ad iop_msg_alloc(struct iop_softc *sc, struct iop_initiator *ii,
1379 1.1 ad struct iop_msg **imp, int flags)
1380 1.1 ad {
1381 1.1 ad struct iop_msg *im;
1382 1.5 ad static int tctxgen = 666;
1383 1.5 ad int s, rv, i, tctx;
1384 1.1 ad
1385 1.1 ad #ifdef I2ODEBUG
1386 1.1 ad if ((flags & IM_SYSMASK) != 0)
1387 1.1 ad panic("iop_msg_alloc: system flags specified");
1388 1.1 ad #endif
1389 1.1 ad
1390 1.1 ad s = splbio(); /* XXX */
1391 1.1 ad
1392 1.5 ad if (ii != NULL && (ii->ii_flags & II_DISCARD) != 0) {
1393 1.5 ad flags |= IM_DISCARD;
1394 1.5 ad tctx = ii->ii_stctx;
1395 1.5 ad } else
1396 1.5 ad tctx = tctxgen++ & 0x7fffffff;
1397 1.1 ad
1398 1.1 ad im = (struct iop_msg *)pool_get(iop_msgpool,
1399 1.1 ad (flags & IM_NOWAIT) == 0 ? PR_WAITOK : 0);
1400 1.1 ad if (im == NULL) {
1401 1.1 ad splx(s);
1402 1.1 ad return (ENOMEM);
1403 1.1 ad }
1404 1.1 ad
1405 1.1 ad /* XXX */
1406 1.1 ad rv = bus_dmamap_create(sc->sc_dmat, IOP_MAX_XFER, IOP_MAX_SGL_ENTRIES,
1407 1.1 ad IOP_MAX_XFER, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
1408 1.1 ad &im->im_xfer[0].ix_map);
1409 1.1 ad if (rv != 0) {
1410 1.1 ad pool_put(iop_msgpool, im);
1411 1.1 ad splx(s);
1412 1.1 ad return (rv);
1413 1.1 ad }
1414 1.1 ad
1415 1.1 ad if ((flags & (IM_DISCARD | IM_NOICTX)) == 0)
1416 1.5 ad TAILQ_INSERT_TAIL(IOP_TCTXHASH(tctx), im, im_hash);
1417 1.1 ad
1418 1.1 ad splx(s);
1419 1.1 ad
1420 1.5 ad im->im_tctx = tctx;
1421 1.1 ad im->im_flags = flags | IM_ALLOCED;
1422 1.1 ad for (i = 0; i < IOP_MAX_MSG_XFERS; i++)
1423 1.1 ad im->im_xfer[i].ix_size = 0;
1424 1.1 ad *imp = im;
1425 1.1 ad
1426 1.1 ad return (0);
1427 1.1 ad }
1428 1.1 ad
1429 1.1 ad /*
1430 1.1 ad * Free a message wrapper.
1431 1.1 ad */
1432 1.1 ad void
1433 1.1 ad iop_msg_free(struct iop_softc *sc, struct iop_initiator *ii, struct iop_msg *im)
1434 1.1 ad {
1435 1.1 ad int s;
1436 1.1 ad
1437 1.1 ad #ifdef I2ODEBUG
1438 1.1 ad if ((im->im_flags & IM_ALLOCED) == 0)
1439 1.1 ad panic("iop_msg_free: wrapper not allocated");
1440 1.1 ad #endif
1441 1.1 ad
1442 1.1 ad /* XXX */
1443 1.1 ad bus_dmamap_destroy(sc->sc_dmat, im->im_xfer[0].ix_map);
1444 1.1 ad
1445 1.1 ad s = splbio(); /* XXX */
1446 1.1 ad
1447 1.1 ad if ((im->im_flags & (IM_DISCARD | IM_NOICTX)) == 0)
1448 1.5 ad TAILQ_REMOVE(IOP_TCTXHASH(im->im_tctx), im, im_hash);
1449 1.1 ad
1450 1.1 ad im->im_flags = 0;
1451 1.1 ad pool_put(iop_msgpool, im);
1452 1.1 ad splx(s);
1453 1.1 ad }
1454 1.1 ad
1455 1.1 ad /*
1456 1.5 ad * Map a data transfer. Write a scatter-gather list into the message frame.
1457 1.1 ad */
1458 1.1 ad int
1459 1.1 ad iop_msg_map(struct iop_softc *sc, struct iop_msg *im, void *xferaddr,
1460 1.1 ad int xfersize, int out)
1461 1.1 ad {
1462 1.1 ad struct iop_xfer *ix;
1463 1.1 ad u_int32_t *mb;
1464 1.5 ad int rv, seg, i;
1465 1.5 ad
1466 1.1 ad for (i = 0, ix = im->im_xfer; i < IOP_MAX_MSG_XFERS; i++, ix++)
1467 1.1 ad if (ix->ix_size == 0)
1468 1.1 ad break;
1469 1.1 ad #ifdef I2ODEBUG
1470 1.1 ad if (i == IOP_MAX_MSG_XFERS)
1471 1.1 ad panic("iop_msg_map: too many xfers");
1472 1.1 ad #endif
1473 1.1 ad
1474 1.1 ad /* Only the first DMA map is static. */
1475 1.1 ad if (i != 0) {
1476 1.1 ad rv = bus_dmamap_create(sc->sc_dmat, IOP_MAX_XFER,
1477 1.1 ad IOP_MAX_SGL_ENTRIES, IOP_MAX_XFER, 0,
1478 1.1 ad BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &ix->ix_map);
1479 1.1 ad if (rv != 0)
1480 1.1 ad return (rv);
1481 1.1 ad }
1482 1.1 ad
1483 1.5 ad ix->ix_flags = (out ? IX_OUT : IX_IN);
1484 1.1 ad ix->ix_size = xfersize;
1485 1.1 ad
1486 1.1 ad rv = bus_dmamap_load(sc->sc_dmat, ix->ix_map, xferaddr, xfersize,
1487 1.1 ad NULL, 0);
1488 1.1 ad if (rv != 0)
1489 1.1 ad return (rv);
1490 1.1 ad bus_dmamap_sync(sc->sc_dmat, ix->ix_map, 0, xfersize,
1491 1.1 ad out ? BUS_DMASYNC_POSTWRITE : BUS_DMASYNC_POSTREAD);
1492 1.1 ad
1493 1.1 ad mb = im->im_msg + (im->im_msg[0] >> 16);
1494 1.1 ad if (out)
1495 1.1 ad out = I2O_SGL_SIMPLE | I2O_SGL_DATA_OUT;
1496 1.1 ad else
1497 1.1 ad out = I2O_SGL_SIMPLE;
1498 1.1 ad
1499 1.1 ad for (seg = 0; seg < ix->ix_map->dm_nsegs; seg++) {
1500 1.1 ad #ifdef I2ODEBUG
1501 1.1 ad if ((seg << 1) + (im->im_msg[0] >> 16) >=
1502 1.1 ad (IOP_MAX_MSG_SIZE >> 2))
1503 1.1 ad panic("iop_map_xfer: message frame too large");
1504 1.1 ad #endif
1505 1.1 ad if (seg == ix->ix_map->dm_nsegs - 1)
1506 1.1 ad out |= I2O_SGL_END_BUFFER;
1507 1.1 ad *mb++ = (u_int32_t)ix->ix_map->dm_segs[seg].ds_len | out;
1508 1.1 ad *mb++ = (u_int32_t)ix->ix_map->dm_segs[seg].ds_addr;
1509 1.1 ad }
1510 1.1 ad
1511 1.1 ad /*
1512 1.1 ad * If this is the first xfer we've mapped for this message, adjust
1513 1.1 ad * the SGL offset field in the message header.
1514 1.1 ad */
1515 1.2 ad if ((im->im_flags & IM_SGLOFFADJ) == 0) {
1516 1.1 ad im->im_msg[0] += ((im->im_msg[0] >> 16) + seg * 2) << 4;
1517 1.2 ad im->im_flags |= IM_SGLOFFADJ;
1518 1.2 ad }
1519 1.1 ad im->im_msg[0] += (seg << 17);
1520 1.1 ad return (0);
1521 1.1 ad }
1522 1.1 ad
1523 1.1 ad /*
1524 1.1 ad * Unmap all data transfers associated with a message wrapper.
1525 1.1 ad */
1526 1.1 ad void
1527 1.1 ad iop_msg_unmap(struct iop_softc *sc, struct iop_msg *im)
1528 1.1 ad {
1529 1.1 ad struct iop_xfer *ix;
1530 1.1 ad int i;
1531 1.1 ad
1532 1.1 ad for (i = 0, ix = im->im_xfer; i < IOP_MAX_MSG_XFERS; i++, ix++) {
1533 1.1 ad if (ix->ix_size == 0)
1534 1.2 ad break;
1535 1.1 ad bus_dmamap_sync(sc->sc_dmat, ix->ix_map, 0, ix->ix_size,
1536 1.1 ad ix->ix_flags & IX_OUT ? BUS_DMASYNC_POSTWRITE :
1537 1.1 ad BUS_DMASYNC_POSTREAD);
1538 1.1 ad bus_dmamap_unload(sc->sc_dmat, ix->ix_map);
1539 1.1 ad
1540 1.1 ad /* Only the first DMA map is static. */
1541 1.1 ad if (i != 0)
1542 1.1 ad bus_dmamap_destroy(sc->sc_dmat, ix->ix_map);
1543 1.1 ad
1544 1.1 ad ix->ix_size = 0;
1545 1.1 ad }
1546 1.1 ad }
1547 1.1 ad
1548 1.1 ad /*
1549 1.1 ad * Send a message to the IOP. Optionally, poll on completion. Return
1550 1.1 ad * non-zero if failure status is returned and IM_NOINTR is set.
1551 1.1 ad */
1552 1.1 ad int
1553 1.1 ad iop_msg_send(struct iop_softc *sc, struct iop_msg *im, int timo)
1554 1.1 ad {
1555 1.5 ad u_int32_t mfa, rmfa;
1556 1.2 ad int rv, status, i, s;
1557 1.1 ad
1558 1.1 ad #ifdef I2ODEBUG
1559 1.1 ad if ((im->im_flags & IM_NOICTX) == 0)
1560 1.1 ad if (im->im_msg[3] == IOP_ICTX &&
1561 1.1 ad (im->im_flags & IM_NOINTR) == 0)
1562 1.1 ad panic("iop_msg_send: IOP_ICTX and !IM_NOINTR");
1563 1.1 ad if ((im->im_flags & IM_DISCARD) != 0)
1564 1.1 ad panic("iop_msg_send: IM_DISCARD");
1565 1.1 ad #endif
1566 1.1 ad
1567 1.2 ad s = splbio(); /* XXX */
1568 1.1 ad
1569 1.1 ad /* Wait up to 250ms for an MFA. */
1570 1.5 ad POLL(250, (mfa = iop_inl(sc, IOP_REG_IFIFO)) != IOP_MFA_EMPTY);
1571 1.1 ad if (mfa == IOP_MFA_EMPTY) {
1572 1.1 ad DPRINTF(("%s: mfa not forthcoming\n", sc->sc_dv.dv_xname));
1573 1.2 ad splx(s);
1574 1.1 ad return (EBUSY);
1575 1.1 ad }
1576 1.1 ad
1577 1.1 ad /* Perform reply queue DMA synchronisation and update counters. */
1578 1.1 ad if ((im->im_flags & IM_NOICTX) == 0) {
1579 1.1 ad if (sc->sc_stat.is_cur_hwqueue == 0)
1580 1.1 ad bus_dmamap_sync(sc->sc_dmat, sc->sc_rep_dmamap, 0,
1581 1.1 ad sc->sc_rep_size, BUS_DMASYNC_PREREAD);
1582 1.1 ad for (i = 0; i < IOP_MAX_MSG_XFERS; i++)
1583 1.1 ad sc->sc_stat.is_bytes += im->im_xfer[i].ix_size;
1584 1.1 ad sc->sc_stat.is_requests++;
1585 1.1 ad if (++sc->sc_stat.is_cur_hwqueue > sc->sc_stat.is_peak_hwqueue)
1586 1.1 ad sc->sc_stat.is_peak_hwqueue =
1587 1.1 ad sc->sc_stat.is_cur_hwqueue;
1588 1.1 ad }
1589 1.1 ad
1590 1.1 ad /* Terminate scatter/gather lists. */
1591 1.1 ad if ((im->im_flags & IM_SGLOFFADJ) != 0)
1592 1.1 ad im->im_msg[(im->im_msg[0] >> 16) - 2] |= I2O_SGL_END;
1593 1.1 ad
1594 1.1 ad /* Post the message frame. */
1595 1.1 ad bus_space_write_region_4(sc->sc_iot, sc->sc_ioh, mfa,
1596 1.1 ad im->im_msg, im->im_msg[0] >> 16);
1597 1.5 ad bus_space_barrier(sc->sc_iot, sc->sc_ioh, mfa,
1598 1.5 ad (im->im_msg[0] >> 14) & ~3, BUS_SPACE_BARRIER_WRITE);
1599 1.1 ad
1600 1.1 ad /* Post the MFA back to the IOP, thus starting the command. */
1601 1.5 ad iop_outl(sc, IOP_REG_IFIFO, mfa);
1602 1.1 ad
1603 1.1 ad if (timo == 0) {
1604 1.2 ad splx(s);
1605 1.1 ad return (0);
1606 1.1 ad }
1607 1.1 ad
1608 1.1 ad /* Wait for completion. */
1609 1.1 ad for (timo *= 10; timo != 0; timo--) {
1610 1.5 ad if ((iop_inl(sc, IOP_REG_INTR_STATUS) & IOP_INTR_OFIFO) != 0) {
1611 1.5 ad /* Double read to account for IOP bug. */
1612 1.5 ad rmfa = iop_inl(sc, IOP_REG_OFIFO);
1613 1.5 ad if (rmfa == IOP_MFA_EMPTY)
1614 1.5 ad rmfa = iop_inl(sc, IOP_REG_OFIFO);
1615 1.5 ad if (rmfa != IOP_MFA_EMPTY)
1616 1.5 ad status = iop_handle_reply(sc, rmfa);
1617 1.5 ad }
1618 1.1 ad if ((im->im_flags & IM_REPLIED) != 0)
1619 1.1 ad break;
1620 1.1 ad DELAY(100);
1621 1.1 ad }
1622 1.1 ad
1623 1.2 ad splx(s);
1624 1.1 ad
1625 1.1 ad if (timo == 0) {
1626 1.5 ad #ifdef I2ODEBUG
1627 1.5 ad printf("%s: poll - no reply\n", sc->sc_dv.dv_xname);
1628 1.5 ad if (iop_status_get(sc) != 0)
1629 1.5 ad printf("iop_msg_send: unable to retrieve status\n");
1630 1.5 ad else
1631 1.5 ad printf("iop_msg_send: IOP state = %d\n",
1632 1.5 ad (le32toh(sc->sc_status.segnumber) >> 16) & 0xff);
1633 1.5 ad #endif
1634 1.1 ad rv = EBUSY;
1635 1.1 ad } else if ((im->im_flags & IM_NOINTR) != 0)
1636 1.1 ad rv = (status != I2O_STATUS_SUCCESS ? EIO : 0);
1637 1.1 ad
1638 1.1 ad return (rv);
1639 1.1 ad }
1640 1.1 ad
1641 1.1 ad /*
1642 1.1 ad * Try to post a message to the adapter; if that's not possible, enqueue it
1643 1.5 ad * with us. If a timeout is specified, wait for the message to complete.
1644 1.1 ad */
1645 1.1 ad int
1646 1.5 ad iop_msg_enqueue(struct iop_softc *sc, struct iop_msg *im, int timo)
1647 1.1 ad {
1648 1.1 ad u_int mfa;
1649 1.5 ad int s, fromqueue, i, rv;
1650 1.1 ad
1651 1.1 ad #ifdef I2ODEBUG
1652 1.1 ad if (im == NULL)
1653 1.1 ad panic("iop_msg_enqueue: im == NULL");
1654 1.1 ad if (sc == NULL)
1655 1.1 ad panic("iop_msg_enqueue: sc == NULL");
1656 1.1 ad if ((im->im_flags & IM_NOICTX) != 0)
1657 1.1 ad panic("iop_msg_enqueue: IM_NOICTX");
1658 1.1 ad if (im->im_msg[3] == IOP_ICTX && (im->im_flags & IM_NOINTR) == 0)
1659 1.5 ad panic("iop_msg_enqueue: IOP_ICTX and no IM_NOINTR");
1660 1.5 ad if ((im->im_flags & IM_DISCARD) != 0 && timo != 0)
1661 1.5 ad panic("iop_msg_enqueue: IM_DISCARD && timo != 0");
1662 1.5 ad if ((im->im_flags & IM_NOINTR) == 0 && timo != 0)
1663 1.5 ad panic("iop_msg_enqueue: !IM_NOINTR && timo != 0");
1664 1.1 ad #endif
1665 1.1 ad
1666 1.1 ad s = splbio(); /* XXX */
1667 1.1 ad fromqueue = (im == SIMPLEQ_FIRST(&sc->sc_queue));
1668 1.1 ad
1669 1.1 ad if (sc->sc_stat.is_cur_hwqueue >= sc->sc_maxqueuecnt) {
1670 1.1 ad /*
1671 1.1 ad * While the IOP may be able to accept more inbound message
1672 1.1 ad * frames than it advertises, don't push harder than it
1673 1.1 ad * wants to go lest we starve it.
1674 1.1 ad *
1675 1.1 ad * XXX We should be handling IOP resource shortages.
1676 1.1 ad */
1677 1.5 ad mfa = IOP_MFA_EMPTY;
1678 1.5 ad DPRINTF(("iop_msg_enqueue: exceeded max queue count\n"));
1679 1.1 ad } else {
1680 1.1 ad /* Double read to account for IOP bug. */
1681 1.5 ad if ((mfa = iop_inl(sc, IOP_REG_IFIFO)) == IOP_MFA_EMPTY)
1682 1.5 ad mfa = iop_inl(sc, IOP_REG_IFIFO);
1683 1.1 ad }
1684 1.1 ad
1685 1.1 ad if (mfa == IOP_MFA_EMPTY) {
1686 1.5 ad DPRINTF(("iop_msg_enqueue: no mfa\n"));
1687 1.1 ad /* Can't transfer to h/w queue - queue with us. */
1688 1.1 ad if (!fromqueue) {
1689 1.1 ad SIMPLEQ_INSERT_TAIL(&sc->sc_queue, im, im_queue);
1690 1.1 ad if (++sc->sc_stat.is_cur_swqueue >
1691 1.1 ad sc->sc_stat.is_peak_swqueue)
1692 1.1 ad sc->sc_stat.is_peak_swqueue =
1693 1.1 ad sc->sc_stat.is_cur_swqueue;
1694 1.1 ad }
1695 1.1 ad splx(s);
1696 1.5 ad if ((im->im_flags & IM_NOINTR) != 0)
1697 1.5 ad rv = iop_msg_wait(sc, im, timo);
1698 1.5 ad else
1699 1.5 ad rv = 0;
1700 1.5 ad return (rv);
1701 1.1 ad } else if (fromqueue) {
1702 1.1 ad SIMPLEQ_REMOVE_HEAD(&sc->sc_queue, im, im_queue);
1703 1.1 ad sc->sc_stat.is_cur_swqueue--;
1704 1.1 ad }
1705 1.1 ad
1706 1.5 ad if ((im->im_flags & IM_NOINTR) != 0)
1707 1.5 ad im->im_flags |= IM_WAITING;
1708 1.5 ad
1709 1.1 ad /* Perform reply queue DMA synchronisation and update counters. */
1710 1.1 ad if (sc->sc_stat.is_cur_hwqueue == 0)
1711 1.1 ad bus_dmamap_sync(sc->sc_dmat, sc->sc_rep_dmamap, 0,
1712 1.1 ad sc->sc_rep_size, BUS_DMASYNC_PREREAD);
1713 1.1 ad
1714 1.1 ad for (i = 0; i < IOP_MAX_MSG_XFERS; i++)
1715 1.1 ad sc->sc_stat.is_bytes += im->im_xfer[i].ix_size;
1716 1.1 ad sc->sc_stat.is_requests++;
1717 1.1 ad if (++sc->sc_stat.is_cur_hwqueue > sc->sc_stat.is_peak_hwqueue)
1718 1.1 ad sc->sc_stat.is_peak_hwqueue = sc->sc_stat.is_cur_hwqueue;
1719 1.1 ad
1720 1.1 ad /* Terminate the scatter/gather list. */
1721 1.1 ad if ((im->im_flags & IM_SGLOFFADJ) != 0)
1722 1.1 ad im->im_msg[(im->im_msg[0] >> 16) - 2] |= I2O_SGL_END;
1723 1.1 ad
1724 1.1 ad /* Post the message frame. */
1725 1.1 ad bus_space_write_region_4(sc->sc_iot, sc->sc_ioh, mfa,
1726 1.1 ad im->im_msg, im->im_msg[0] >> 16);
1727 1.5 ad bus_space_barrier(sc->sc_iot, sc->sc_ioh, mfa,
1728 1.5 ad (im->im_msg[0] >> 14) & ~3, BUS_SPACE_BARRIER_WRITE);
1729 1.1 ad
1730 1.1 ad /* Post the MFA back to the IOP, thus starting the command. */
1731 1.5 ad iop_outl(sc, IOP_REG_IFIFO, mfa);
1732 1.1 ad
1733 1.1 ad /* If this is a discardable message wrapper, free it. */
1734 1.1 ad if ((im->im_flags & IM_DISCARD) != 0)
1735 1.1 ad iop_msg_free(sc, NULL, im);
1736 1.1 ad splx(s);
1737 1.5 ad
1738 1.5 ad if ((im->im_flags & IM_NOINTR) != 0)
1739 1.5 ad rv = iop_msg_wait(sc, im, timo);
1740 1.5 ad else
1741 1.5 ad rv = 0;
1742 1.5 ad return (rv);
1743 1.1 ad }
1744 1.1 ad
1745 1.1 ad /*
1746 1.5 ad * Wait for the specified message to complete.
1747 1.1 ad */
1748 1.5 ad static int
1749 1.1 ad iop_msg_wait(struct iop_softc *sc, struct iop_msg *im, int timo)
1750 1.1 ad {
1751 1.5 ad struct i2o_reply *rb;
1752 1.5 ad int rv, s;
1753 1.1 ad
1754 1.5 ad s = splbio();
1755 1.5 ad if ((im->im_flags & IM_REPLIED) != 0) {
1756 1.5 ad splx(s);
1757 1.1 ad return (0);
1758 1.5 ad }
1759 1.5 ad rv = tsleep(im, PRIBIO, "iopmsg", timo * hz / 1000);
1760 1.5 ad splx(s);
1761 1.5 ad #ifdef I2ODEBUG
1762 1.5 ad if (rv != 0) {
1763 1.5 ad printf("iop_msg_wait: tsleep() == %d\n", rv);
1764 1.5 ad if (iop_status_get(sc) != 0)
1765 1.5 ad printf("iop_msg_wait: unable to retrieve status\n");
1766 1.5 ad else
1767 1.5 ad printf("iop_msg_wait: IOP state = %d\n",
1768 1.5 ad (le32toh(sc->sc_status.segnumber) >> 16) & 0xff);
1769 1.5 ad }
1770 1.5 ad #endif
1771 1.5 ad if ((im->im_flags & (IM_REPLIED | IM_NOSTATUS)) == IM_REPLIED) {
1772 1.5 ad rb = (struct i2o_reply *)im->im_msg;
1773 1.5 ad rv = (rb->reqstatus != I2O_STATUS_SUCCESS ? EIO : 0);
1774 1.5 ad }
1775 1.1 ad return (rv);
1776 1.1 ad }
1777 1.1 ad
1778 1.1 ad /*
1779 1.1 ad * Release an unused message frame back to the IOP's inbound fifo.
1780 1.1 ad */
1781 1.1 ad static void
1782 1.1 ad iop_release_mfa(struct iop_softc *sc, u_int32_t mfa)
1783 1.1 ad {
1784 1.1 ad
1785 1.1 ad /* Use the frame to issue a no-op. */
1786 1.5 ad iop_outl(sc, mfa, I2O_VERSION_11 | (4 << 16));
1787 1.5 ad iop_outl(sc, mfa + 4, I2O_MSGFUNC(I2O_TID_IOP, I2O_UTIL_NOP));
1788 1.5 ad iop_outl(sc, mfa + 8, 0);
1789 1.5 ad iop_outl(sc, mfa + 12, 0);
1790 1.1 ad
1791 1.5 ad iop_outl(sc, IOP_REG_IFIFO, mfa);
1792 1.1 ad }
1793 1.1 ad
1794 1.1 ad #ifdef I2ODEBUG
1795 1.1 ad /*
1796 1.1 ad * Print status information from a failure reply frame.
1797 1.1 ad */
1798 1.1 ad static void
1799 1.1 ad iop_reply_print(struct iop_softc *sc, struct iop_msg *im,
1800 1.1 ad struct i2o_reply *rb)
1801 1.1 ad {
1802 1.5 ad u_int function, detail;
1803 1.1 ad #ifdef I2OVERBOSE
1804 1.1 ad const char *statusstr;
1805 1.1 ad #endif
1806 1.1 ad
1807 1.5 ad if (im != NULL && (im->im_flags & IM_REPLIED) == 0)
1808 1.1 ad panic("iop_msg_print_status: %p not replied to", im);
1809 1.1 ad
1810 1.5 ad function = (le32toh(rb->msgfunc) >> 24) & 0xff;
1811 1.1 ad detail = le16toh(rb->detail);
1812 1.1 ad
1813 1.5 ad printf("%s: reply:\n", sc->sc_dv.dv_xname);
1814 1.5 ad
1815 1.1 ad #ifdef I2OVERBOSE
1816 1.1 ad if (rb->reqstatus < sizeof(iop_status) / sizeof(iop_status[0]))
1817 1.1 ad statusstr = iop_status[rb->reqstatus];
1818 1.1 ad else
1819 1.1 ad statusstr = "undefined error code";
1820 1.1 ad
1821 1.5 ad printf("%s: function=0x%02x status=0x%02x (%s)\n",
1822 1.5 ad sc->sc_dv.dv_xname, function, rb->reqstatus, statusstr);
1823 1.1 ad #else
1824 1.5 ad printf("%s: function=0x%02x status=0x%02x\n",
1825 1.5 ad sc->sc_dv.dv_xname, function, rb->reqstatus);
1826 1.1 ad #endif
1827 1.5 ad printf("%s: detail=0x%04x ictx=0x%08x tctx=0x%08x\n",
1828 1.5 ad sc->sc_dv.dv_xname, detail, le32toh(rb->msgictx),
1829 1.5 ad le32toh(rb->msgtctx));
1830 1.5 ad printf("%s: tidi=%d tidt=%d flags=0x%02x\n", sc->sc_dv.dv_xname,
1831 1.5 ad (le32toh(rb->msgfunc) >> 12) & 4095, le32toh(rb->msgfunc) & 4095,
1832 1.5 ad (le32toh(rb->msgflags) >> 8) & 0xff);
1833 1.1 ad }
1834 1.1 ad #endif
1835 1.1 ad
1836 1.1 ad /*
1837 1.5 ad * Translate an I2O ASCII field into a C string.
1838 1.1 ad */
1839 1.1 ad void
1840 1.5 ad iop_strvis(struct iop_softc *sc, const char *src, int slen, char *dst, int dlen)
1841 1.1 ad {
1842 1.5 ad int hc, lc, i, nit;
1843 1.1 ad
1844 1.1 ad dlen--;
1845 1.1 ad lc = 0;
1846 1.1 ad hc = 0;
1847 1.1 ad i = 0;
1848 1.5 ad
1849 1.5 ad /*
1850 1.5 ad * DPT use NUL as a space, whereas AMI use it as a terminator. The
1851 1.5 ad * spec has nothing to say about it. Since AMI fields are usually
1852 1.5 ad * filled with junk after the terminator, ...
1853 1.5 ad */
1854 1.5 ad nit = (le16toh(sc->sc_status.orgid) != I2O_ORG_DPT);
1855 1.5 ad
1856 1.5 ad while (slen-- != 0 && dlen-- != 0) {
1857 1.5 ad if (nit && *src == '\0')
1858 1.5 ad break;
1859 1.5 ad else if (*src <= 0x20 || *src >= 0x7f) {
1860 1.1 ad if (hc)
1861 1.1 ad dst[i++] = ' ';
1862 1.1 ad } else {
1863 1.1 ad hc = 1;
1864 1.1 ad dst[i++] = *src;
1865 1.1 ad lc = i;
1866 1.1 ad }
1867 1.1 ad src++;
1868 1.1 ad }
1869 1.1 ad
1870 1.1 ad dst[lc] = '\0';
1871 1.1 ad }
1872 1.1 ad
1873 1.1 ad /*
1874 1.5 ad * Claim or unclaim the specified TID.
1875 1.1 ad */
1876 1.1 ad int
1877 1.5 ad iop_util_claim(struct iop_softc *sc, struct iop_initiator *ii, int release,
1878 1.5 ad int flags)
1879 1.1 ad {
1880 1.5 ad struct iop_msg *im;
1881 1.5 ad struct i2o_util_claim *mb;
1882 1.5 ad int rv, func;
1883 1.5 ad
1884 1.5 ad func = release ? I2O_UTIL_CLAIM_RELEASE : I2O_UTIL_CLAIM;
1885 1.5 ad
1886 1.5 ad if ((rv = iop_msg_alloc(sc, ii, &im, IM_NOINTR)) != 0)
1887 1.5 ad return (rv);
1888 1.5 ad
1889 1.5 ad /* We can use the same structure, as both are identical. */
1890 1.5 ad mb = (struct i2o_util_claim *)im->im_msg;
1891 1.5 ad mb->msgflags = I2O_MSGFLAGS(i2o_util_claim);
1892 1.5 ad mb->msgfunc = I2O_MSGFUNC(ii->ii_tid, func);
1893 1.5 ad mb->msgictx = ii->ii_ictx;
1894 1.5 ad mb->msgtctx = im->im_tctx;
1895 1.5 ad mb->flags = flags;
1896 1.5 ad
1897 1.5 ad rv = iop_msg_enqueue(sc, im, 5000);
1898 1.5 ad iop_msg_free(sc, ii, im);
1899 1.5 ad return (rv);
1900 1.5 ad }
1901 1.5 ad
1902 1.5 ad /*
1903 1.5 ad * Perform an abort.
1904 1.5 ad */
1905 1.5 ad int iop_util_abort(struct iop_softc *sc, struct iop_initiator *ii, int func,
1906 1.5 ad int tctxabort, int flags)
1907 1.5 ad {
1908 1.5 ad struct iop_msg *im;
1909 1.5 ad struct i2o_util_abort *mb;
1910 1.5 ad int rv;
1911 1.5 ad
1912 1.5 ad if ((rv = iop_msg_alloc(sc, ii, &im, IM_NOINTR)) != 0)
1913 1.5 ad return (rv);
1914 1.1 ad
1915 1.5 ad mb = (struct i2o_util_abort *)im->im_msg;
1916 1.5 ad mb->msgflags = I2O_MSGFLAGS(i2o_util_abort);
1917 1.5 ad mb->msgfunc = I2O_MSGFUNC(ii->ii_tid, I2O_UTIL_ABORT);
1918 1.5 ad mb->msgictx = ii->ii_ictx;
1919 1.5 ad mb->msgtctx = im->im_tctx;
1920 1.5 ad mb->flags = (func << 24) | flags;
1921 1.5 ad mb->tctxabort = tctxabort;
1922 1.1 ad
1923 1.5 ad rv = iop_msg_enqueue(sc, im, 5000);
1924 1.5 ad iop_msg_free(sc, ii, im);
1925 1.5 ad return (rv);
1926 1.1 ad }
1927 1.1 ad
1928 1.1 ad /*
1929 1.5 ad * Enable or disable event types for the specified device.
1930 1.1 ad */
1931 1.5 ad int iop_util_eventreg(struct iop_softc *sc, struct iop_initiator *ii, int mask)
1932 1.5 ad {
1933 1.5 ad struct iop_msg *im;
1934 1.5 ad struct i2o_util_event_register *mb;
1935 1.5 ad int rv;
1936 1.5 ad
1937 1.5 ad if ((rv = iop_msg_alloc(sc, ii, &im, 0)) != 0)
1938 1.5 ad return (rv);
1939 1.5 ad
1940 1.5 ad mb = (struct i2o_util_event_register *)im->im_msg;
1941 1.5 ad mb->msgflags = I2O_MSGFLAGS(i2o_util_event_register);
1942 1.5 ad mb->msgfunc = I2O_MSGFUNC(ii->ii_tid, I2O_UTIL_EVENT_REGISTER);
1943 1.5 ad mb->msgictx = ii->ii_ictx;
1944 1.5 ad mb->msgtctx = im->im_tctx;
1945 1.5 ad mb->eventmask = mask;
1946 1.5 ad
1947 1.5 ad return (iop_msg_enqueue(sc, im, 0));
1948 1.5 ad }
1949 1.5 ad
1950 1.1 ad int
1951 1.5 ad iopopen(dev_t dev, int flag, int mode, struct proc *p)
1952 1.1 ad {
1953 1.5 ad struct iop_softc *sc;
1954 1.5 ad int unit, error;
1955 1.5 ad
1956 1.5 ad unit = minor(dev);
1957 1.5 ad
1958 1.5 ad sc = device_lookup(&iop_cd, minor(dev));
1959 1.5 ad if ((sc = iop_cd.cd_devs[unit]) == NULL)
1960 1.1 ad return (ENXIO);
1961 1.5 ad if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1962 1.5 ad return (error);
1963 1.5 ad
1964 1.5 ad if ((sc->sc_flags & IOP_OPEN) != 0)
1965 1.5 ad return (EBUSY);
1966 1.5 ad if ((sc->sc_flags & IOP_ONLINE) == 0)
1967 1.5 ad return (EIO);
1968 1.5 ad sc->sc_flags |= IOP_OPEN;
1969 1.5 ad
1970 1.5 ad return (0);
1971 1.1 ad }
1972 1.1 ad
1973 1.5 ad int
1974 1.5 ad iopclose(dev_t dev, int flag, int mode, struct proc *p)
1975 1.1 ad {
1976 1.5 ad struct iop_softc *sc;
1977 1.1 ad
1978 1.5 ad sc = device_lookup(&iop_cd, minor(dev));
1979 1.5 ad sc->sc_flags &= ~IOP_OPEN;
1980 1.5 ad return (0);
1981 1.1 ad }
1982 1.1 ad
1983 1.1 ad int
1984 1.5 ad iopioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
1985 1.1 ad {
1986 1.5 ad struct iop_softc *sc;
1987 1.5 ad struct iovec *iov;
1988 1.5 ad struct ioppt *pt;
1989 1.1 ad struct iop_msg *im;
1990 1.5 ad struct i2o_msg *mb;
1991 1.5 ad struct i2o_reply *rb;
1992 1.5 ad int rv, i;
1993 1.5 ad
1994 1.5 ad if (securelevel >= 2)
1995 1.5 ad return (EPERM);
1996 1.5 ad
1997 1.5 ad sc = device_lookup(&iop_cd, minor(dev));
1998 1.5 ad
1999 1.5 ad switch (cmd) {
2000 1.5 ad case IOPIOCPT:
2001 1.5 ad pt = (struct ioppt *)data;
2002 1.5 ad
2003 1.5 ad if (pt->pt_msglen > IOP_MAX_MSG_SIZE ||
2004 1.5 ad pt->pt_msglen < sizeof(struct i2o_msg) ||
2005 1.5 ad pt->pt_nbufs > IOP_MAX_MSG_XFERS ||
2006 1.5 ad pt->pt_nbufs < 0 ||
2007 1.5 ad pt->pt_replylen < 0 ||
2008 1.5 ad pt->pt_timo < 1000 ||
2009 1.5 ad pt->pt_timo > 5*60*1000) {
2010 1.5 ad rv = EINVAL;
2011 1.5 ad break;
2012 1.5 ad }
2013 1.5 ad
2014 1.5 ad rv = iop_msg_alloc(sc, NULL, &im, IM_NOINTR | IM_NOSTATUS);
2015 1.5 ad if (rv != 0)
2016 1.5 ad break;
2017 1.5 ad
2018 1.5 ad if ((rv = copyin(pt->pt_msg, im->im_msg, pt->pt_msglen)) != 0) {
2019 1.5 ad iop_msg_free(sc, NULL, im);
2020 1.5 ad break;
2021 1.5 ad }
2022 1.5 ad
2023 1.5 ad mb = (struct i2o_msg *)im->im_msg;
2024 1.5 ad mb->msgictx = IOP_ICTX;
2025 1.5 ad mb->msgtctx = im->im_tctx;
2026 1.5 ad
2027 1.5 ad for (i = 0; i < pt->pt_nbufs; i++) {
2028 1.5 ad rv = iop_msg_map(sc, im, pt->pt_bufs[i].ptb_data,
2029 1.5 ad pt->pt_bufs[i].ptb_datalen,
2030 1.5 ad pt->pt_bufs[i].ptb_out != 0);
2031 1.5 ad if (rv != 0) {
2032 1.5 ad iop_msg_free(sc, NULL, im);
2033 1.5 ad return (rv);
2034 1.5 ad }
2035 1.5 ad }
2036 1.5 ad
2037 1.5 ad if ((rv = iop_msg_enqueue(sc, im, pt->pt_timo)) == 0) {
2038 1.5 ad rb = (struct i2o_reply *)im->im_msg;
2039 1.5 ad i = (le32toh(rb->msgflags) >> 14) & ~3; /* XXX */
2040 1.5 ad if (i > IOP_MAX_REPLY_SIZE)
2041 1.5 ad i = IOP_MAX_REPLY_SIZE;
2042 1.5 ad if (i > pt->pt_replylen)
2043 1.5 ad i = pt->pt_replylen;
2044 1.5 ad rv = copyout(rb, pt->pt_reply, i);
2045 1.5 ad }
2046 1.5 ad
2047 1.5 ad iop_msg_free(sc, NULL, im);
2048 1.5 ad break;
2049 1.1 ad
2050 1.5 ad case IOPIOCGLCT:
2051 1.5 ad iov = (struct iovec *)data;
2052 1.5 ad rv = lockmgr(&sc->sc_conflock, LK_EXCLUSIVE, NULL);
2053 1.5 ad if (rv == 0) {
2054 1.5 ad i = le16toh(sc->sc_lct->tablesize) << 2;
2055 1.5 ad if (i > iov->iov_len)
2056 1.5 ad i = iov->iov_len;
2057 1.5 ad else
2058 1.5 ad iov->iov_len = i;
2059 1.5 ad rv = copyout(sc->sc_lct, iov->iov_base, i);
2060 1.5 ad lockmgr(&sc->sc_conflock, LK_RELEASE, NULL);
2061 1.5 ad }
2062 1.5 ad break;
2063 1.1 ad
2064 1.5 ad case IOPIOCGSTATUS:
2065 1.5 ad iov = (struct iovec *)data;
2066 1.5 ad i = sizeof(struct i2o_status);
2067 1.5 ad if (i > iov->iov_len)
2068 1.5 ad i = iov->iov_len;
2069 1.5 ad else
2070 1.5 ad iov->iov_len = i;
2071 1.5 ad if ((rv = iop_status_get(sc)) == 0)
2072 1.5 ad rv = copyout(&sc->sc_status, iov->iov_base, i);
2073 1.5 ad break;
2074 1.5 ad
2075 1.5 ad case IOPIOCRECONFIG:
2076 1.5 ad rv = iop_reconfigure(sc, 0);
2077 1.5 ad break;
2078 1.5 ad
2079 1.5 ad default:
2080 1.5 ad #if defined(DIAGNOSTIC) || defined(I2ODEBUG)
2081 1.5 ad printf("%s: unknown ioctl %lx\n", sc->sc_dv.dv_xname, cmd);
2082 1.5 ad #endif
2083 1.5 ad rv = ENOTTY;
2084 1.5 ad break;
2085 1.5 ad }
2086 1.1 ad
2087 1.1 ad return (rv);
2088 1.5 ad }
2089