xref: /netbsd-src/sys/dev/ic/icp.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: icp.c,v 1.34 2020/08/14 09:26:40 chs Exp $	*/
2 
3 /*-
4  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Doran, and by Jason R. Thorpe of Wasabi Systems, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1999, 2000 Niklas Hallqvist.  All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  * 3. All advertising materials mentioning features or use of this software
44  *    must display the following acknowledgement:
45  *	This product includes software developed by Niklas Hallqvist.
46  * 4. The name of the author may not be used to endorse or promote products
47  *    derived from this software without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59  *
60  * from OpenBSD: gdt_common.c,v 1.12 2001/07/04 06:43:18 niklas Exp
61  */
62 
63 /*
64  * This driver would not have written if it was not for the hardware donations
65  * from both ICP-Vortex and �ko.neT.  I want to thank them for their support.
66  *
67  * Re-worked for NetBSD by Andrew Doran.  Test hardware kindly supplied by
68  * Intel.
69  *
70  * Support for the ICP-Vortex management tools added by
71  * Jason R. Thorpe of Wasabi Systems, Inc., based on code
72  * provided by Achim Leubner <achim.leubner@intel.com>.
73  *
74  * Additional support for dynamic rescan of cacheservice drives by
75  * Jason R. Thorpe of Wasabi Systems, Inc.
76  */
77 
78 #include <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: icp.c,v 1.34 2020/08/14 09:26:40 chs Exp $");
80 
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/kernel.h>
84 #include <sys/device.h>
85 #include <sys/queue.h>
86 #include <sys/proc.h>
87 #include <sys/buf.h>
88 #include <sys/endian.h>
89 #include <sys/malloc.h>
90 #include <sys/disk.h>
91 
92 #include <sys/bswap.h>
93 #include <sys/bus.h>
94 
95 #include <dev/pci/pcireg.h>
96 #include <dev/pci/pcivar.h>
97 #include <dev/pci/pcidevs.h>
98 
99 #include <dev/ic/icpreg.h>
100 #include <dev/ic/icpvar.h>
101 
102 #include <dev/scsipi/scsipi_all.h>
103 #include <dev/scsipi/scsiconf.h>
104 
105 #include "locators.h"
106 
107 int	icp_async_event(struct icp_softc *, int);
108 void	icp_ccb_submit(struct icp_softc *icp, struct icp_ccb *ic);
109 void	icp_chain(struct icp_softc *);
110 int	icp_print(void *, const char *);
111 void	icp_watchdog(void *);
112 void	icp_ucmd_intr(struct icp_ccb *);
113 void	icp_recompute_openings(struct icp_softc *);
114 
115 int	icp_count;	/* total # of controllers, for ioctl interface */
116 
117 /*
118  * Statistics for the ioctl interface to query.
119  *
120  * XXX Global.  They should probably be made per-controller
121  * XXX at some point.
122  */
123 gdt_statist_t icp_stats;
124 
125 int
126 icp_init(struct icp_softc *icp, const char *intrstr)
127 {
128 	struct icp_attach_args icpa;
129 	struct icp_binfo binfo;
130 	struct icp_ccb *ic;
131 	u_int16_t cdev_cnt;
132 	int i, j, state, feat, nsegs, rv;
133 	int locs[ICPCF_NLOCS];
134 
135 	state = 0;
136 
137 	if (intrstr != NULL)
138 		aprint_normal_dev(icp->icp_dv, "interrupting at %s\n",
139 		    intrstr);
140 
141 	SIMPLEQ_INIT(&icp->icp_ccb_queue);
142 	SIMPLEQ_INIT(&icp->icp_ccb_freelist);
143 	SIMPLEQ_INIT(&icp->icp_ucmd_queue);
144 	callout_init(&icp->icp_wdog_callout, 0);
145 
146 	/*
147 	 * Allocate a scratch area.
148 	 */
149 	if (bus_dmamap_create(icp->icp_dmat, ICP_SCRATCH_SIZE, 1,
150 	    ICP_SCRATCH_SIZE, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
151 	    &icp->icp_scr_dmamap) != 0) {
152 		aprint_error_dev(icp->icp_dv, "cannot create scratch dmamap\n");
153 		return (1);
154 	}
155 	state++;
156 
157 	if (bus_dmamem_alloc(icp->icp_dmat, ICP_SCRATCH_SIZE, PAGE_SIZE, 0,
158 	    icp->icp_scr_seg, 1, &nsegs, BUS_DMA_NOWAIT) != 0) {
159 		aprint_error_dev(icp->icp_dv, "cannot alloc scratch dmamem\n");
160 		goto bail_out;
161 	}
162 	state++;
163 
164 	if (bus_dmamem_map(icp->icp_dmat, icp->icp_scr_seg, nsegs,
165 	    ICP_SCRATCH_SIZE, &icp->icp_scr, 0)) {
166 		aprint_error_dev(icp->icp_dv, "cannot map scratch dmamem\n");
167 		goto bail_out;
168 	}
169 	state++;
170 
171 	if (bus_dmamap_load(icp->icp_dmat, icp->icp_scr_dmamap, icp->icp_scr,
172 	    ICP_SCRATCH_SIZE, NULL, BUS_DMA_NOWAIT)) {
173 		aprint_error_dev(icp->icp_dv, "cannot load scratch dmamap\n");
174 		goto bail_out;
175 	}
176 	state++;
177 
178 	/*
179 	 * Allocate and initialize the command control blocks.
180 	 */
181 	ic = malloc(sizeof(*ic) * ICP_NCCBS, M_DEVBUF, M_WAITOK | M_ZERO);
182 	icp->icp_ccbs = ic;
183 	state++;
184 
185 	for (i = 0; i < ICP_NCCBS; i++, ic++) {
186 		/*
187 		 * The first two command indexes have special meanings, so
188 		 * we can't use them.
189 		 */
190 		ic->ic_ident = i + 2;
191 		rv = bus_dmamap_create(icp->icp_dmat, ICP_MAX_XFER,
192 		    ICP_MAXSG, ICP_MAX_XFER, 0,
193 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
194 		    &ic->ic_xfer_map);
195 		if (rv != 0)
196 			break;
197 		icp->icp_nccbs++;
198 		icp_ccb_free(icp, ic);
199 	}
200 #ifdef DIAGNOSTIC
201 	if (icp->icp_nccbs != ICP_NCCBS)
202 		aprint_error_dev(icp->icp_dv, "%d/%d CCBs usable\n",
203 		    icp->icp_nccbs, ICP_NCCBS);
204 #endif
205 
206 	/*
207 	 * Initialize the controller.
208 	 */
209 	if (!icp_cmd(icp, ICP_SCREENSERVICE, ICP_INIT, 0, 0, 0)) {
210 		aprint_error_dev(icp->icp_dv, "screen service init error %d\n",
211 		    icp->icp_status);
212 		goto bail_out;
213 	}
214 
215 	if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_INIT, ICP_LINUX_OS, 0, 0)) {
216 		aprint_error_dev(icp->icp_dv, "cache service init error %d\n",
217 		    icp->icp_status);
218 		goto bail_out;
219 	}
220 
221 	icp_cmd(icp, ICP_CACHESERVICE, ICP_UNFREEZE_IO, 0, 0, 0);
222 
223 	if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_MOUNT, 0xffff, 1, 0)) {
224 		aprint_error_dev(icp->icp_dv, "cache service mount error %d\n",
225 		    icp->icp_status);
226 		goto bail_out;
227 	}
228 
229 	if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_INIT, ICP_LINUX_OS, 0, 0)) {
230 		aprint_error_dev(icp->icp_dv, "cache service post-mount init error %d\n",
231 		    icp->icp_status);
232 		goto bail_out;
233 	}
234 	cdev_cnt = (u_int16_t)icp->icp_info;
235 	icp->icp_fw_vers = icp->icp_service;
236 
237 	if (!icp_cmd(icp, ICP_SCSIRAWSERVICE, ICP_INIT, 0, 0, 0)) {
238 		aprint_error_dev(icp->icp_dv, "raw service init error %d\n",
239 		    icp->icp_status);
240 		goto bail_out;
241 	}
242 
243 	/*
244 	 * Set/get raw service features (scatter/gather).
245 	 */
246 	feat = 0;
247 	if (icp_cmd(icp, ICP_SCSIRAWSERVICE, ICP_SET_FEAT, ICP_SCATTER_GATHER,
248 	    0, 0))
249 		if (icp_cmd(icp, ICP_SCSIRAWSERVICE, ICP_GET_FEAT, 0, 0, 0))
250 			feat = icp->icp_info;
251 
252 	if ((feat & ICP_SCATTER_GATHER) == 0) {
253 #ifdef DIAGNOSTIC
254 		aprint_normal_dev(icp->icp_dv,
255 		    "scatter/gather not supported (raw service)\n");
256 #endif
257 	} else
258 		icp->icp_features |= ICP_FEAT_RAWSERVICE;
259 
260 	/*
261 	 * Set/get cache service features (scatter/gather).
262 	 */
263 	feat = 0;
264 	if (icp_cmd(icp, ICP_CACHESERVICE, ICP_SET_FEAT, 0,
265 	    ICP_SCATTER_GATHER, 0))
266 		if (icp_cmd(icp, ICP_CACHESERVICE, ICP_GET_FEAT, 0, 0, 0))
267 			feat = icp->icp_info;
268 
269 	if ((feat & ICP_SCATTER_GATHER) == 0) {
270 #ifdef DIAGNOSTIC
271 		aprint_normal_dev(icp->icp_dv,
272 		    "scatter/gather not supported (cache service)\n");
273 #endif
274 	} else
275 		icp->icp_features |= ICP_FEAT_CACHESERVICE;
276 
277 	/*
278 	 * Pull some information from the board and dump.
279 	 */
280 	if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_IOCTL, ICP_BOARD_INFO,
281 	    ICP_INVALID_CHANNEL, sizeof(struct icp_binfo))) {
282 		aprint_error_dev(icp->icp_dv, "unable to retrive board info\n");
283 		goto bail_out;
284 	}
285 	memcpy(&binfo, icp->icp_scr, sizeof(binfo));
286 
287 	aprint_normal_dev(icp->icp_dv,
288 	    "model <%s>, firmware <%s>, %d channel(s), %dMB memory\n",
289 	    binfo.bi_type_string, binfo.bi_raid_string,
290 	    binfo.bi_chan_count, le32toh(binfo.bi_memsize) >> 20);
291 
292 	/*
293 	 * Determine the number of devices, and number of openings per
294 	 * device.
295 	 */
296 	if (icp->icp_features & ICP_FEAT_CACHESERVICE) {
297 		for (j = 0; j < cdev_cnt && j < ICP_MAX_HDRIVES; j++) {
298 			if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_INFO, j, 0,
299 			    0))
300 				continue;
301 
302 			icp->icp_cdr[j].cd_size = icp->icp_info;
303 			if (icp->icp_cdr[j].cd_size != 0)
304 				icp->icp_ndevs++;
305 
306 			if (icp_cmd(icp, ICP_CACHESERVICE, ICP_DEVTYPE, j, 0,
307 			    0))
308 				icp->icp_cdr[j].cd_type = icp->icp_info;
309 		}
310 	}
311 
312 	if (icp->icp_features & ICP_FEAT_RAWSERVICE) {
313 		icp->icp_nchan = binfo.bi_chan_count;
314 		icp->icp_ndevs += icp->icp_nchan;
315 	}
316 
317 	icp_recompute_openings(icp);
318 
319 	/*
320 	 * Attach SCSI channels.
321 	 */
322 	if (icp->icp_features & ICP_FEAT_RAWSERVICE) {
323 		struct icp_ioc_version *iv;
324 		struct icp_rawioc *ri;
325 		struct icp_getch *gc;
326 
327 		iv = (struct icp_ioc_version *)icp->icp_scr;
328 		iv->iv_version = htole32(ICP_IOC_NEWEST);
329 		iv->iv_listents = ICP_MAXBUS;
330 		iv->iv_firstchan = 0;
331 		iv->iv_lastchan = ICP_MAXBUS - 1;
332 		iv->iv_listoffset = htole32(sizeof(*iv));
333 
334 		if (icp_cmd(icp, ICP_CACHESERVICE, ICP_IOCTL,
335 		    ICP_IOCHAN_RAW_DESC, ICP_INVALID_CHANNEL,
336 		    sizeof(*iv) + ICP_MAXBUS * sizeof(*ri))) {
337 			ri = (struct icp_rawioc *)(iv + 1);
338 			for (j = 0; j < binfo.bi_chan_count; j++, ri++)
339 				icp->icp_bus_id[j] = ri->ri_procid;
340 		} else {
341 			/*
342 			 * Fall back to the old method.
343 			 */
344 			gc = (struct icp_getch *)icp->icp_scr;
345 
346 			for (j = 0; j < binfo.bi_chan_count; j++) {
347 				if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_IOCTL,
348 				    ICP_SCSI_CHAN_CNT | ICP_L_CTRL_PATTERN,
349 				    ICP_IO_CHANNEL | ICP_INVALID_CHANNEL,
350 				    sizeof(*gc))) {
351 				    	aprint_error_dev(icp->icp_dv,
352 					    "unable to get chan info");
353 					goto bail_out;
354 				}
355 				icp->icp_bus_id[j] = gc->gc_scsiid;
356 			}
357 		}
358 
359 		for (j = 0; j < binfo.bi_chan_count; j++) {
360 			if (icp->icp_bus_id[j] > ICP_MAXID_FC)
361 				icp->icp_bus_id[j] = ICP_MAXID_FC;
362 
363 			icpa.icpa_unit = j + ICPA_UNIT_SCSI;
364 
365 			locs[ICPCF_UNIT] = j + ICPA_UNIT_SCSI;
366 
367 			icp->icp_children[icpa.icpa_unit] =
368 				config_found_sm_loc(icp->icp_dv, "icp", locs,
369 					&icpa, icp_print, config_stdsubmatch);
370 		}
371 	}
372 
373 	/*
374 	 * Attach cache devices.
375 	 */
376 	if (icp->icp_features & ICP_FEAT_CACHESERVICE) {
377 		for (j = 0; j < cdev_cnt && j < ICP_MAX_HDRIVES; j++) {
378 			if (icp->icp_cdr[j].cd_size == 0)
379 				continue;
380 
381 			icpa.icpa_unit = j;
382 
383 			locs[ICPCF_UNIT] = j;
384 
385 			icp->icp_children[icpa.icpa_unit] =
386 			    config_found_sm_loc(icp->icp_dv, "icp", locs,
387 				&icpa, icp_print, config_stdsubmatch);
388 		}
389 	}
390 
391 	/*
392 	 * Start the watchdog.
393 	 */
394 	icp_watchdog(icp);
395 
396 	/*
397 	 * Count the controller, and we're done!
398 	 */
399 	if (icp_count++ == 0)
400 		mutex_init(&icp_ioctl_mutex, MUTEX_DEFAULT, IPL_NONE);
401 
402 	return (0);
403 
404  bail_out:
405 	if (state > 4)
406 		for (j = 0; j < i; j++)
407 			bus_dmamap_destroy(icp->icp_dmat,
408 			    icp->icp_ccbs[j].ic_xfer_map);
409  	if (state > 3)
410 		free(icp->icp_ccbs, M_DEVBUF);
411 	if (state > 2)
412 		bus_dmamap_unload(icp->icp_dmat, icp->icp_scr_dmamap);
413 	if (state > 1)
414 		bus_dmamem_unmap(icp->icp_dmat, icp->icp_scr,
415 		    ICP_SCRATCH_SIZE);
416 	if (state > 0)
417 		bus_dmamem_free(icp->icp_dmat, icp->icp_scr_seg, nsegs);
418 	bus_dmamap_destroy(icp->icp_dmat, icp->icp_scr_dmamap);
419 
420 	return (1);
421 }
422 
423 void
424 icp_register_servicecb(struct icp_softc *icp, int unit,
425     const struct icp_servicecb *cb)
426 {
427 
428 	icp->icp_servicecb[unit] = cb;
429 }
430 
431 void
432 icp_rescan(struct icp_softc *icp, int unit)
433 {
434 	struct icp_attach_args icpa;
435 	u_int newsize, newtype;
436 	int locs[ICPCF_NLOCS];
437 
438 	/*
439 	 * NOTE: It is very important that the queue be frozen and not
440 	 * commands running when this is called.  The ioctl mutex must
441 	 * also be held.
442 	 */
443 
444 	KASSERT(icp->icp_qfreeze != 0);
445 	KASSERT(icp->icp_running == 0);
446 	KASSERT(unit < ICP_MAX_HDRIVES);
447 
448 	if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_INFO, unit, 0, 0)) {
449 #ifdef ICP_DEBUG
450 		printf("%s: rescan: unit %d ICP_INFO failed -> 0x%04x\n",
451 		    device_xname(icp->icp_dv), unit, icp->icp_status);
452 #endif
453 		goto gone;
454 	}
455 	if ((newsize = icp->icp_info) == 0) {
456 #ifdef ICP_DEBUG
457 		printf("%s: rescan: unit %d has zero size\n",
458 		    device_xname(icp->icp_dv), unit);
459 #endif
460  gone:
461 		/*
462 		 * Host drive is no longer present; detach if a child
463 		 * is currently there.
464 		 */
465 		if (icp->icp_cdr[unit].cd_size != 0)
466 			icp->icp_ndevs--;
467 		icp->icp_cdr[unit].cd_size = 0;
468 		if (icp->icp_children[unit] != NULL) {
469 			(void) config_detach(icp->icp_children[unit],
470 			    DETACH_FORCE);
471 			icp->icp_children[unit] = NULL;
472 		}
473 		return;
474 	}
475 
476 	if (icp_cmd(icp, ICP_CACHESERVICE, ICP_DEVTYPE, unit, 0, 0))
477 		newtype = icp->icp_info;
478 	else {
479 #ifdef ICP_DEBUG
480 		printf("%s: rescan: unit %d ICP_DEVTYPE failed\n",
481 		    device_xname(icp->icp_dv), unit);
482 #endif
483 		newtype = 0;	/* XXX? */
484 	}
485 
486 #ifdef ICP_DEBUG
487 	printf("%s: rescan: unit %d old %u/%u, new %u/%u\n",
488 	    device_xname(icp->icp_dv), unit, icp->icp_cdr[unit].cd_size,
489 	    icp->icp_cdr[unit].cd_type, newsize, newtype);
490 #endif
491 
492 	/*
493 	 * If the type or size changed, detach any old child (if it exists)
494 	 * and attach a new one.
495 	 */
496 	if (icp->icp_children[unit] == NULL ||
497 	    newsize != icp->icp_cdr[unit].cd_size ||
498 	    newtype != icp->icp_cdr[unit].cd_type) {
499 		if (icp->icp_cdr[unit].cd_size == 0)
500 			icp->icp_ndevs++;
501 		icp->icp_cdr[unit].cd_size = newsize;
502 		icp->icp_cdr[unit].cd_type = newtype;
503 		if (icp->icp_children[unit] != NULL)
504 			(void) config_detach(icp->icp_children[unit],
505 			    DETACH_FORCE);
506 
507 		icpa.icpa_unit = unit;
508 
509 		locs[ICPCF_UNIT] = unit;
510 
511 		icp->icp_children[unit] = config_found_sm_loc(icp->icp_dv,
512 			"icp", locs, &icpa, icp_print, config_stdsubmatch);
513 	}
514 
515 	icp_recompute_openings(icp);
516 }
517 
518 void
519 icp_rescan_all(struct icp_softc *icp)
520 {
521 	int unit;
522 	u_int16_t cdev_cnt;
523 
524 	/*
525 	 * This is the old method of rescanning the host drives.  We
526 	 * start by reinitializing the cache service.
527 	 */
528 	if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_INIT, ICP_LINUX_OS, 0, 0)) {
529 		printf("%s: unable to re-initialize cache service for rescan\n",
530 		    device_xname(icp->icp_dv));
531 		return;
532 	}
533 	cdev_cnt = (u_int16_t) icp->icp_info;
534 
535 	/* For each host drive, do the new-style rescan. */
536 	for (unit = 0; unit < cdev_cnt && unit < ICP_MAX_HDRIVES; unit++)
537 		icp_rescan(icp, unit);
538 
539 	/* Now detach anything in the slots after cdev_cnt. */
540 	for (; unit < ICP_MAX_HDRIVES; unit++) {
541 		if (icp->icp_cdr[unit].cd_size != 0) {
542 #ifdef ICP_DEBUG
543 			printf("%s: rescan all: unit %d < new cdev_cnt (%d)\n",
544 			    device_xname(icp->icp_dv), unit, cdev_cnt);
545 #endif
546 			icp->icp_ndevs--;
547 			icp->icp_cdr[unit].cd_size = 0;
548 			if (icp->icp_children[unit] != NULL) {
549 				(void) config_detach(icp->icp_children[unit],
550 				    DETACH_FORCE);
551 				icp->icp_children[unit] = NULL;
552 			}
553 		}
554 	}
555 
556 	icp_recompute_openings(icp);
557 }
558 
559 void
560 icp_recompute_openings(struct icp_softc *icp)
561 {
562 	int unit, openings;
563 
564 	if (icp->icp_ndevs != 0)
565 		openings =
566 		    (icp->icp_nccbs - ICP_NCCB_RESERVE) / icp->icp_ndevs;
567 	else
568 		openings = 0;
569 	if (openings == icp->icp_openings)
570 		return;
571 	icp->icp_openings = openings;
572 
573 #ifdef ICP_DEBUG
574 	printf("%s: %d device%s, %d openings per device\n",
575 	    device_xname(icp->icp_dv), icp->icp_ndevs,
576 	    icp->icp_ndevs == 1 ? "" : "s", icp->icp_openings);
577 #endif
578 
579 	for (unit = 0; unit < ICP_MAX_HDRIVES + ICP_MAXBUS; unit++) {
580 		if (icp->icp_children[unit] != NULL)
581 			(*icp->icp_servicecb[unit]->iscb_openings)(
582 			    icp->icp_children[unit], icp->icp_openings);
583 	}
584 }
585 
586 void
587 icp_watchdog(void *cookie)
588 {
589 	struct icp_softc *icp;
590 	int s;
591 
592 	icp = cookie;
593 
594 	s = splbio();
595 	icp_intr(icp);
596 	if (ICP_HAS_WORK(icp))
597 		icp_ccb_enqueue(icp, NULL);
598 	splx(s);
599 
600 	callout_reset(&icp->icp_wdog_callout, hz * ICP_WATCHDOG_FREQ,
601 	    icp_watchdog, icp);
602 }
603 
604 int
605 icp_print(void *aux, const char *pnp)
606 {
607 	struct icp_attach_args *icpa;
608 	const char *str;
609 
610 	icpa = (struct icp_attach_args *)aux;
611 
612 	if (pnp != NULL) {
613 		if (icpa->icpa_unit < ICPA_UNIT_SCSI)
614 			str = "block device";
615 		else
616 			str = "SCSI channel";
617 		aprint_normal("%s at %s", str, pnp);
618 	}
619 	aprint_normal(" unit %d", icpa->icpa_unit);
620 
621 	return (UNCONF);
622 }
623 
624 int
625 icp_async_event(struct icp_softc *icp, int service)
626 {
627 
628 	if (service == ICP_SCREENSERVICE) {
629 		if (icp->icp_status == ICP_S_MSG_REQUEST) {
630 			/* XXX */
631 		}
632 	} else {
633 		if ((icp->icp_fw_vers & 0xff) >= 0x1a) {
634 			icp->icp_evt.size = 0;
635 			icp->icp_evt.eu.async.ionode =
636 			    device_unit(icp->icp_dv);
637 			icp->icp_evt.eu.async.status = icp->icp_status;
638 			/*
639 			 * Severity and event string are filled in by the
640 			 * hardware interface interrupt handler.
641 			 */
642 			printf("%s: %s\n", device_xname(icp->icp_dv),
643 			    icp->icp_evt.event_string);
644 		} else {
645 			icp->icp_evt.size = sizeof(icp->icp_evt.eu.async);
646 			icp->icp_evt.eu.async.ionode =
647 			    device_unit(icp->icp_dv);
648 			icp->icp_evt.eu.async.service = service;
649 			icp->icp_evt.eu.async.status = icp->icp_status;
650 			icp->icp_evt.eu.async.info = icp->icp_info;
651 			/* XXXJRT FIX THIS */
652 			*(u_int32_t *) icp->icp_evt.eu.async.scsi_coord =
653 			    icp->icp_info2;
654 		}
655 		icp_store_event(icp, GDT_ES_ASYNC, service, &icp->icp_evt);
656 	}
657 
658 	return (0);
659 }
660 
661 int
662 icp_intr(void *cookie)
663 {
664 	struct icp_softc *icp;
665 	struct icp_intr_ctx ctx;
666 	struct icp_ccb *ic;
667 
668 	icp = cookie;
669 
670 	ctx.istatus = (*icp->icp_get_status)(icp);
671 	if (!ctx.istatus) {
672 		icp->icp_status = ICP_S_NO_STATUS;
673 		return (0);
674 	}
675 
676 	(*icp->icp_intr)(icp, &ctx);
677 
678 	icp->icp_status = ctx.cmd_status;
679 	icp->icp_service = ctx.service;
680 	icp->icp_info = ctx.info;
681 	icp->icp_info2 = ctx.info2;
682 
683 	switch (ctx.istatus) {
684 	case ICP_ASYNCINDEX:
685 		icp_async_event(icp, ctx.service);
686 		return (1);
687 
688 	case ICP_SPEZINDEX:
689 		aprint_error_dev(icp->icp_dv, "uninitialized or unknown service (%d/%d)\n",
690 		    ctx.info, ctx.info2);
691 		icp->icp_evt.size = sizeof(icp->icp_evt.eu.driver);
692 		icp->icp_evt.eu.driver.ionode = device_unit(icp->icp_dv);
693 		icp_store_event(icp, GDT_ES_DRIVER, 4, &icp->icp_evt);
694 		return (1);
695 	}
696 
697 	if ((ctx.istatus - 2) > icp->icp_nccbs)
698 		panic("icp_intr: bad command index returned");
699 
700 	ic = &icp->icp_ccbs[ctx.istatus - 2];
701 	ic->ic_status = icp->icp_status;
702 
703 	if ((ic->ic_flags & IC_ALLOCED) == 0) {
704 		/* XXX ICP's "iir" driver just sends an event here. */
705 		panic("icp_intr: inactive CCB identified");
706 	}
707 
708 	/*
709 	 * Try to protect ourselves from the running command count already
710 	 * being 0 (e.g. if a polled command times out).
711 	 */
712 	KDASSERT(icp->icp_running != 0);
713 	if (--icp->icp_running == 0 &&
714 	    (icp->icp_flags & ICP_F_WAIT_FREEZE) != 0) {
715 		icp->icp_flags &= ~ICP_F_WAIT_FREEZE;
716 		wakeup(&icp->icp_qfreeze);
717 	}
718 
719 	switch (icp->icp_status) {
720 	case ICP_S_BSY:
721 #ifdef ICP_DEBUG
722 		printf("%s: ICP_S_BSY received\n", device_xname(icp->icp_dv));
723 #endif
724 		if (__predict_false((ic->ic_flags & IC_UCMD) != 0))
725 			SIMPLEQ_INSERT_HEAD(&icp->icp_ucmd_queue, ic, ic_chain);
726 		else
727 			SIMPLEQ_INSERT_HEAD(&icp->icp_ccb_queue, ic, ic_chain);
728 		break;
729 
730 	default:
731 		ic->ic_flags |= IC_COMPLETE;
732 
733 		if ((ic->ic_flags & IC_WAITING) != 0)
734 			wakeup(ic);
735 		else if (ic->ic_intr != NULL)
736 			(*ic->ic_intr)(ic);
737 
738 		if (ICP_HAS_WORK(icp))
739 			icp_ccb_enqueue(icp, NULL);
740 
741 		break;
742 	}
743 
744 	return (1);
745 }
746 
747 struct icp_ucmd_ctx {
748 	gdt_ucmd_t *iu_ucmd;
749 	u_int32_t iu_cnt;
750 };
751 
752 void
753 icp_ucmd_intr(struct icp_ccb *ic)
754 {
755 	struct icp_softc *icp = device_private(ic->ic_dv);
756 	struct icp_ucmd_ctx *iu = ic->ic_context;
757 	gdt_ucmd_t *ucmd = iu->iu_ucmd;
758 
759 	ucmd->status = icp->icp_status;
760 	ucmd->info = icp->icp_info;
761 
762 	if (iu->iu_cnt != 0) {
763 		bus_dmamap_sync(icp->icp_dmat,
764 		    icp->icp_scr_dmamap,
765 		    ICP_SCRATCH_UCMD, iu->iu_cnt,
766 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
767 		memcpy(ucmd->data,
768 		    (char *)icp->icp_scr + ICP_SCRATCH_UCMD, iu->iu_cnt);
769 	}
770 
771 	icp->icp_ucmd_ccb = NULL;
772 
773 	ic->ic_flags |= IC_COMPLETE;
774 	wakeup(ic);
775 }
776 
777 /*
778  * NOTE: We assume that it is safe to sleep here!
779  */
780 int
781 icp_cmd(struct icp_softc *icp, u_int8_t service, u_int16_t opcode,
782 	u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
783 {
784 	struct icp_ioctlcmd *icmd;
785 	struct icp_cachecmd *cc;
786 	struct icp_rawcmd *rc;
787 	int retries, rv;
788 	struct icp_ccb *ic;
789 
790 	retries = ICP_RETRIES;
791 
792 	do {
793 		ic = icp_ccb_alloc_wait(icp);
794 		memset(&ic->ic_cmd, 0, sizeof(ic->ic_cmd));
795 		ic->ic_cmd.cmd_opcode = htole16(opcode);
796 
797 		switch (service) {
798 		case ICP_CACHESERVICE:
799 			if (opcode == ICP_IOCTL) {
800 				icmd = &ic->ic_cmd.cmd_packet.ic;
801 				icmd->ic_subfunc = htole16(arg1);
802 				icmd->ic_channel = htole32(arg2);
803 				icmd->ic_bufsize = htole32(arg3);
804 				icmd->ic_addr =
805 				    htole32(icp->icp_scr_seg[0].ds_addr);
806 
807 				bus_dmamap_sync(icp->icp_dmat,
808 				    icp->icp_scr_dmamap, 0, arg3,
809 				    BUS_DMASYNC_PREWRITE |
810 				    BUS_DMASYNC_PREREAD);
811 			} else {
812 				cc = &ic->ic_cmd.cmd_packet.cc;
813 				cc->cc_deviceno = htole16(arg1);
814 				cc->cc_blockno = htole32(arg2);
815 			}
816 			break;
817 
818 		case ICP_SCSIRAWSERVICE:
819 			rc = &ic->ic_cmd.cmd_packet.rc;
820 			rc->rc_direction = htole32(arg1);
821 			rc->rc_bus = arg2;
822 			rc->rc_target = arg3;
823 			rc->rc_lun = arg3 >> 8;
824 			break;
825 		}
826 
827 		ic->ic_service = service;
828 		ic->ic_cmdlen = sizeof(ic->ic_cmd);
829 		rv = icp_ccb_poll(icp, ic, 10000);
830 
831 		switch (service) {
832 		case ICP_CACHESERVICE:
833 			if (opcode == ICP_IOCTL) {
834 				bus_dmamap_sync(icp->icp_dmat,
835 				    icp->icp_scr_dmamap, 0, arg3,
836 				    BUS_DMASYNC_POSTWRITE |
837 				    BUS_DMASYNC_POSTREAD);
838 			}
839 			break;
840 		}
841 
842 		icp_ccb_free(icp, ic);
843 	} while (rv != 0 && --retries > 0);
844 
845 	return (icp->icp_status == ICP_S_OK);
846 }
847 
848 int
849 icp_ucmd(struct icp_softc *icp, gdt_ucmd_t *ucmd)
850 {
851 	struct icp_ccb *ic;
852 	struct icp_ucmd_ctx iu;
853 	u_int32_t cnt;
854 	int error;
855 
856 	if (ucmd->service == ICP_CACHESERVICE) {
857 		if (ucmd->command.cmd_opcode == ICP_IOCTL) {
858 			cnt = ucmd->command.cmd_packet.ic.ic_bufsize;
859 			if (cnt > GDT_SCRATCH_SZ) {
860 				aprint_error_dev(icp->icp_dv, "scratch buffer too small (%d/%d)\n",
861 				    GDT_SCRATCH_SZ, cnt);
862 				return (EINVAL);
863 			}
864 		} else {
865 			cnt = ucmd->command.cmd_packet.cc.cc_blockcnt *
866 			    ICP_SECTOR_SIZE;
867 			if (cnt > GDT_SCRATCH_SZ) {
868 				aprint_error_dev(icp->icp_dv, "scratch buffer too small (%d/%d)\n",
869 				    GDT_SCRATCH_SZ, cnt);
870 				return (EINVAL);
871 			}
872 		}
873 	} else {
874 		cnt = ucmd->command.cmd_packet.rc.rc_sdlen +
875 		    ucmd->command.cmd_packet.rc.rc_sense_len;
876 		if (cnt > GDT_SCRATCH_SZ) {
877 			aprint_error_dev(icp->icp_dv, "scratch buffer too small (%d/%d)\n",
878 			    GDT_SCRATCH_SZ, cnt);
879 			return (EINVAL);
880 		}
881 	}
882 
883 	iu.iu_ucmd = ucmd;
884 	iu.iu_cnt = cnt;
885 
886 	ic = icp_ccb_alloc_wait(icp);
887 	memset(&ic->ic_cmd, 0, sizeof(ic->ic_cmd));
888 	ic->ic_cmd.cmd_opcode = htole16(ucmd->command.cmd_opcode);
889 
890 	if (ucmd->service == ICP_CACHESERVICE) {
891 		if (ucmd->command.cmd_opcode == ICP_IOCTL) {
892 			struct icp_ioctlcmd *icmd, *uicmd;
893 
894 			icmd = &ic->ic_cmd.cmd_packet.ic;
895 			uicmd = &ucmd->command.cmd_packet.ic;
896 
897 			icmd->ic_subfunc = htole16(uicmd->ic_subfunc);
898 			icmd->ic_channel = htole32(uicmd->ic_channel);
899 			icmd->ic_bufsize = htole32(uicmd->ic_bufsize);
900 			icmd->ic_addr =
901 			    htole32(icp->icp_scr_seg[0].ds_addr +
902 				    ICP_SCRATCH_UCMD);
903 		} else {
904 			struct icp_cachecmd *cc, *ucc;
905 
906 			cc = &ic->ic_cmd.cmd_packet.cc;
907 			ucc = &ucmd->command.cmd_packet.cc;
908 
909 			cc->cc_deviceno = htole16(ucc->cc_deviceno);
910 			cc->cc_blockno = htole32(ucc->cc_blockno);
911 			cc->cc_blockcnt = htole32(ucc->cc_blockcnt);
912 			cc->cc_addr = htole32(0xffffffffU);
913 			cc->cc_nsgent = htole32(1);
914 			cc->cc_sg[0].sg_addr =
915 			    htole32(icp->icp_scr_seg[0].ds_addr +
916 				    ICP_SCRATCH_UCMD);
917 			cc->cc_sg[0].sg_len = htole32(cnt);
918 		}
919 	} else {
920 		struct icp_rawcmd *rc, *urc;
921 
922 		rc = &ic->ic_cmd.cmd_packet.rc;
923 		urc = &ucmd->command.cmd_packet.rc;
924 
925 		rc->rc_direction = htole32(urc->rc_direction);
926 		rc->rc_sdata = htole32(0xffffffffU);
927 		rc->rc_sdlen = htole32(urc->rc_sdlen);
928 		rc->rc_clen = htole32(urc->rc_clen);
929 		memcpy(rc->rc_cdb, urc->rc_cdb, sizeof(rc->rc_cdb));
930 		rc->rc_target = urc->rc_target;
931 		rc->rc_lun = urc->rc_lun;
932 		rc->rc_bus = urc->rc_bus;
933 		rc->rc_sense_len = htole32(urc->rc_sense_len);
934 		rc->rc_sense_addr =
935 		    htole32(icp->icp_scr_seg[0].ds_addr +
936 			    ICP_SCRATCH_UCMD + urc->rc_sdlen);
937 		rc->rc_nsgent = htole32(1);
938 		rc->rc_sg[0].sg_addr =
939 		    htole32(icp->icp_scr_seg[0].ds_addr + ICP_SCRATCH_UCMD);
940 		rc->rc_sg[0].sg_len = htole32(cnt - urc->rc_sense_len);
941 	}
942 
943 	ic->ic_service = ucmd->service;
944 	ic->ic_cmdlen = sizeof(ic->ic_cmd);
945 	ic->ic_context = &iu;
946 
947 	/*
948 	 * XXX What units are ucmd->timeout in?  Until we know, we
949 	 * XXX just pull a number out of thin air.
950 	 */
951 	if (__predict_false((error = icp_ccb_wait_user(icp, ic, 30000)) != 0))
952 		aprint_error_dev(icp->icp_dv, "error %d waiting for ucmd to complete\n",
953 		    error);
954 
955 	/* icp_ucmd_intr() has updated ucmd. */
956 	icp_ccb_free(icp, ic);
957 
958 	return (error);
959 }
960 
961 struct icp_ccb *
962 icp_ccb_alloc(struct icp_softc *icp)
963 {
964 	struct icp_ccb *ic;
965 	int s;
966 
967 	s = splbio();
968 	if (__predict_false((ic =
969 			     SIMPLEQ_FIRST(&icp->icp_ccb_freelist)) == NULL)) {
970 		splx(s);
971 		return (NULL);
972 	}
973 	SIMPLEQ_REMOVE_HEAD(&icp->icp_ccb_freelist, ic_chain);
974 	splx(s);
975 
976 	ic->ic_flags = IC_ALLOCED;
977 	return (ic);
978 }
979 
980 struct icp_ccb *
981 icp_ccb_alloc_wait(struct icp_softc *icp)
982 {
983 	struct icp_ccb *ic;
984 	int s;
985 
986 	s = splbio();
987 	while ((ic = SIMPLEQ_FIRST(&icp->icp_ccb_freelist)) == NULL) {
988 		icp->icp_flags |= ICP_F_WAIT_CCB;
989 		(void) tsleep(&icp->icp_ccb_freelist, PRIBIO, "icpccb", 0);
990 	}
991 	SIMPLEQ_REMOVE_HEAD(&icp->icp_ccb_freelist, ic_chain);
992 	splx(s);
993 
994 	ic->ic_flags = IC_ALLOCED;
995 	return (ic);
996 }
997 
998 void
999 icp_ccb_free(struct icp_softc *icp, struct icp_ccb *ic)
1000 {
1001 	int s;
1002 
1003 	s = splbio();
1004 	ic->ic_flags = 0;
1005 	ic->ic_intr = NULL;
1006 	SIMPLEQ_INSERT_HEAD(&icp->icp_ccb_freelist, ic, ic_chain);
1007 	if (__predict_false((icp->icp_flags & ICP_F_WAIT_CCB) != 0)) {
1008 		icp->icp_flags &= ~ICP_F_WAIT_CCB;
1009 		wakeup(&icp->icp_ccb_freelist);
1010 	}
1011 	splx(s);
1012 }
1013 
1014 void
1015 icp_ccb_enqueue(struct icp_softc *icp, struct icp_ccb *ic)
1016 {
1017 	int s;
1018 
1019 	s = splbio();
1020 
1021 	if (ic != NULL) {
1022 		if (__predict_false((ic->ic_flags & IC_UCMD) != 0))
1023 			SIMPLEQ_INSERT_TAIL(&icp->icp_ucmd_queue, ic, ic_chain);
1024 		else
1025 			SIMPLEQ_INSERT_TAIL(&icp->icp_ccb_queue, ic, ic_chain);
1026 	}
1027 
1028 	for (; icp->icp_qfreeze == 0;) {
1029 		if (__predict_false((ic =
1030 			    SIMPLEQ_FIRST(&icp->icp_ucmd_queue)) != NULL)) {
1031 			struct icp_ucmd_ctx *iu = ic->ic_context;
1032 			gdt_ucmd_t *ucmd = iu->iu_ucmd;
1033 
1034 			/*
1035 			 * All user-generated commands share the same
1036 			 * scratch space, so if one is already running,
1037 			 * we have to stall the command queue.
1038 			 */
1039 			if (icp->icp_ucmd_ccb != NULL)
1040 				break;
1041 			if ((*icp->icp_test_busy)(icp))
1042 				break;
1043 			icp->icp_ucmd_ccb = ic;
1044 
1045 			if (iu->iu_cnt != 0) {
1046 				memcpy((char *)icp->icp_scr + ICP_SCRATCH_UCMD,
1047 				    ucmd->data, iu->iu_cnt);
1048 				bus_dmamap_sync(icp->icp_dmat,
1049 				    icp->icp_scr_dmamap,
1050 				    ICP_SCRATCH_UCMD, iu->iu_cnt,
1051 				    BUS_DMASYNC_PREREAD |
1052 				    BUS_DMASYNC_PREWRITE);
1053 			}
1054 		} else if (__predict_true((ic =
1055 				SIMPLEQ_FIRST(&icp->icp_ccb_queue)) != NULL)) {
1056 			if ((*icp->icp_test_busy)(icp))
1057 				break;
1058 		} else {
1059 			/* no command found */
1060 			break;
1061 		}
1062 		icp_ccb_submit(icp, ic);
1063 		if (__predict_false((ic->ic_flags & IC_UCMD) != 0))
1064 			SIMPLEQ_REMOVE_HEAD(&icp->icp_ucmd_queue, ic_chain);
1065 		else
1066 			SIMPLEQ_REMOVE_HEAD(&icp->icp_ccb_queue, ic_chain);
1067 	}
1068 
1069 	splx(s);
1070 }
1071 
1072 int
1073 icp_ccb_map(struct icp_softc *icp, struct icp_ccb *ic, void *data, int size,
1074 	    int dir)
1075 {
1076 	struct icp_sg *sg;
1077 	int nsegs, i, rv;
1078 	bus_dmamap_t xfer;
1079 
1080 	xfer = ic->ic_xfer_map;
1081 
1082 	rv = bus_dmamap_load(icp->icp_dmat, xfer, data, size, NULL,
1083 	    BUS_DMA_NOWAIT | BUS_DMA_STREAMING |
1084 	    ((dir & IC_XFER_IN) ? BUS_DMA_READ : BUS_DMA_WRITE));
1085 	if (rv != 0)
1086 		return (rv);
1087 
1088 	nsegs = xfer->dm_nsegs;
1089 	ic->ic_xfer_size = size;
1090 	ic->ic_nsgent = nsegs;
1091 	ic->ic_flags |= dir;
1092 	sg = ic->ic_sg;
1093 
1094 	if (sg != NULL) {
1095 		for (i = 0; i < nsegs; i++, sg++) {
1096 			sg->sg_addr = htole32(xfer->dm_segs[i].ds_addr);
1097 			sg->sg_len = htole32(xfer->dm_segs[i].ds_len);
1098 		}
1099 	} else if (nsegs > 1)
1100 		panic("icp_ccb_map: no SG list specified, but nsegs > 1");
1101 
1102 	if ((dir & IC_XFER_OUT) != 0)
1103 		i = BUS_DMASYNC_PREWRITE;
1104 	else /* if ((dir & IC_XFER_IN) != 0) */
1105 		i = BUS_DMASYNC_PREREAD;
1106 
1107 	bus_dmamap_sync(icp->icp_dmat, xfer, 0, ic->ic_xfer_size, i);
1108 	return (0);
1109 }
1110 
1111 void
1112 icp_ccb_unmap(struct icp_softc *icp, struct icp_ccb *ic)
1113 {
1114 	int i;
1115 
1116 	if ((ic->ic_flags & IC_XFER_OUT) != 0)
1117 		i = BUS_DMASYNC_POSTWRITE;
1118 	else /* if ((ic->ic_flags & IC_XFER_IN) != 0) */
1119 		i = BUS_DMASYNC_POSTREAD;
1120 
1121 	bus_dmamap_sync(icp->icp_dmat, ic->ic_xfer_map, 0, ic->ic_xfer_size, i);
1122 	bus_dmamap_unload(icp->icp_dmat, ic->ic_xfer_map);
1123 }
1124 
1125 int
1126 icp_ccb_poll(struct icp_softc *icp, struct icp_ccb *ic, int timo)
1127 {
1128 	int s, rv;
1129 
1130 	s = splbio();
1131 
1132 	for (timo = ICP_BUSY_WAIT_MS * 100; timo != 0; timo--) {
1133 		if (!(*icp->icp_test_busy)(icp))
1134 			break;
1135 		DELAY(10);
1136 	}
1137 	if (timo == 0) {
1138 		printf("%s: submit: busy\n", device_xname(icp->icp_dv));
1139 		return (EAGAIN);
1140 	}
1141 
1142 	icp_ccb_submit(icp, ic);
1143 
1144 	if (cold) {
1145 		for (timo *= 10; timo != 0; timo--) {
1146 			DELAY(100);
1147 			icp_intr(icp);
1148 			if ((ic->ic_flags & IC_COMPLETE) != 0)
1149 				break;
1150 		}
1151 	} else {
1152 		ic->ic_flags |= IC_WAITING;
1153 		while ((ic->ic_flags & IC_COMPLETE) == 0) {
1154 			if ((rv = tsleep(ic, PRIBIO, "icpwccb",
1155 					 mstohz(timo))) != 0) {
1156 				timo = 0;
1157 				break;
1158 			}
1159 		}
1160 	}
1161 
1162 	if (timo != 0) {
1163 		if (ic->ic_status != ICP_S_OK) {
1164 #ifdef ICP_DEBUG
1165 			printf("%s: request failed; status=0x%04x\n",
1166 			    device_xname(icp->icp_dv), ic->ic_status);
1167 #endif
1168 			rv = EIO;
1169 		} else
1170 			rv = 0;
1171 	} else {
1172 		aprint_error_dev(icp->icp_dv, "command timed out\n");
1173 		rv = EIO;
1174 	}
1175 
1176 	while ((*icp->icp_test_busy)(icp) != 0)
1177 		DELAY(10);
1178 
1179 	splx(s);
1180 
1181 	return (rv);
1182 }
1183 
1184 int
1185 icp_ccb_wait(struct icp_softc *icp, struct icp_ccb *ic, int timo)
1186 {
1187 	int s, rv;
1188 
1189 	ic->ic_flags |= IC_WAITING;
1190 
1191 	s = splbio();
1192 	icp_ccb_enqueue(icp, ic);
1193 	while ((ic->ic_flags & IC_COMPLETE) == 0) {
1194 		if ((rv = tsleep(ic, PRIBIO, "icpwccb", mstohz(timo))) != 0) {
1195 			splx(s);
1196 			return (rv);
1197 		}
1198 	}
1199 	splx(s);
1200 
1201 	if (ic->ic_status != ICP_S_OK) {
1202 		aprint_error_dev(icp->icp_dv, "command failed; status=%x\n",
1203 		    ic->ic_status);
1204 		return (EIO);
1205 	}
1206 
1207 	return (0);
1208 }
1209 
1210 int
1211 icp_ccb_wait_user(struct icp_softc *icp, struct icp_ccb *ic, int timo)
1212 {
1213 	int s, rv;
1214 
1215 	ic->ic_dv = icp->icp_dv;
1216 	ic->ic_intr = icp_ucmd_intr;
1217 	ic->ic_flags |= IC_UCMD;
1218 
1219 	s = splbio();
1220 	icp_ccb_enqueue(icp, ic);
1221 	while ((ic->ic_flags & IC_COMPLETE) == 0) {
1222 		if ((rv = tsleep(ic, PRIBIO, "icpwuccb", mstohz(timo))) != 0) {
1223 			splx(s);
1224 			return (rv);
1225 		}
1226 	}
1227 	splx(s);
1228 
1229 	return (0);
1230 }
1231 
1232 void
1233 icp_ccb_submit(struct icp_softc *icp, struct icp_ccb *ic)
1234 {
1235 
1236 	ic->ic_cmdlen = (ic->ic_cmdlen + 3) & ~3;
1237 
1238 	(*icp->icp_set_sema0)(icp);
1239 	DELAY(10);
1240 
1241 	ic->ic_cmd.cmd_boardnode = htole32(ICP_LOCALBOARD);
1242 	ic->ic_cmd.cmd_cmdindex = htole32(ic->ic_ident);
1243 
1244 	icp->icp_running++;
1245 
1246 	(*icp->icp_copy_cmd)(icp, ic);
1247 	(*icp->icp_release_event)(icp, ic);
1248 }
1249 
1250 int
1251 icp_freeze(struct icp_softc *icp)
1252 {
1253 	int s, error = 0;
1254 
1255 	s = splbio();
1256 	if (icp->icp_qfreeze++ == 0) {
1257 		while (icp->icp_running != 0) {
1258 			icp->icp_flags |= ICP_F_WAIT_FREEZE;
1259 			error = tsleep(&icp->icp_qfreeze, PRIBIO|PCATCH,
1260 			    "icpqfrz", 0);
1261 			if (error != 0 && --icp->icp_qfreeze == 0 &&
1262 			    ICP_HAS_WORK(icp)) {
1263 				icp_ccb_enqueue(icp, NULL);
1264 				break;
1265 			}
1266 		}
1267 	}
1268 	splx(s);
1269 
1270 	return (error);
1271 }
1272 
1273 void
1274 icp_unfreeze(struct icp_softc *icp)
1275 {
1276 	int s;
1277 
1278 	s = splbio();
1279 	KDASSERT(icp->icp_qfreeze != 0);
1280 	if (--icp->icp_qfreeze == 0 && ICP_HAS_WORK(icp))
1281 		icp_ccb_enqueue(icp, NULL);
1282 	splx(s);
1283 }
1284 
1285 /* XXX Global - should be per-controller? XXX */
1286 static gdt_evt_str icp_event_buffer[ICP_MAX_EVENTS];
1287 static int icp_event_oldidx;
1288 static int icp_event_lastidx;
1289 
1290 gdt_evt_str *
1291 icp_store_event(struct icp_softc *icp, u_int16_t source, u_int16_t idx,
1292     gdt_evt_data *evt)
1293 {
1294 	gdt_evt_str *e;
1295 
1296 	/* no source == no event */
1297 	if (source == 0)
1298 		return (NULL);
1299 
1300 	e = &icp_event_buffer[icp_event_lastidx];
1301 	if (e->event_source == source && e->event_idx == idx &&
1302 	    ((evt->size != 0 && e->event_data.size != 0 &&
1303 	      memcmp(&e->event_data.eu, &evt->eu, evt->size) == 0) ||
1304 	     (evt->size == 0 && e->event_data.size == 0 &&
1305 	      strcmp((char *) e->event_data.event_string,
1306 	      	     (char *) evt->event_string) == 0))) {
1307 		e->last_stamp = time_second;
1308 		e->same_count++;
1309 	} else {
1310 		if (icp_event_buffer[icp_event_lastidx].event_source != 0) {
1311 			icp_event_lastidx++;
1312 			if (icp_event_lastidx == ICP_MAX_EVENTS)
1313 				icp_event_lastidx = 0;
1314 			if (icp_event_lastidx == icp_event_oldidx) {
1315 				icp_event_oldidx++;
1316 				if (icp_event_oldidx == ICP_MAX_EVENTS)
1317 					icp_event_oldidx = 0;
1318 			}
1319 		}
1320 		e = &icp_event_buffer[icp_event_lastidx];
1321 		e->event_source = source;
1322 		e->event_idx = idx;
1323 		e->first_stamp = e->last_stamp = time_second;
1324 		e->same_count = 1;
1325 		e->event_data = *evt;
1326 		e->application = 0;
1327 	}
1328 	return (e);
1329 }
1330 
1331 int
1332 icp_read_event(struct icp_softc *icp, int handle, gdt_evt_str *estr)
1333 {
1334 	gdt_evt_str *e;
1335 	int eindex, s;
1336 
1337 	s = splbio();
1338 
1339 	if (handle == -1)
1340 		eindex = icp_event_oldidx;
1341 	else
1342 		eindex = handle;
1343 
1344 	estr->event_source = 0;
1345 
1346 	if (eindex < 0 || eindex >= ICP_MAX_EVENTS) {
1347 		splx(s);
1348 		return (eindex);
1349 	}
1350 
1351 	e = &icp_event_buffer[eindex];
1352 	if (e->event_source != 0) {
1353 		if (eindex != icp_event_lastidx) {
1354 			eindex++;
1355 			if (eindex == ICP_MAX_EVENTS)
1356 				eindex = 0;
1357 		} else
1358 			eindex = -1;
1359 		memcpy(estr, e, sizeof(gdt_evt_str));
1360 	}
1361 
1362 	splx(s);
1363 
1364 	return (eindex);
1365 }
1366 
1367 void
1368 icp_readapp_event(struct icp_softc *icp, u_int8_t application,
1369     gdt_evt_str *estr)
1370 {
1371 	gdt_evt_str *e;
1372 	int found = 0, eindex, s;
1373 
1374 	s = splbio();
1375 
1376 	eindex = icp_event_oldidx;
1377 	for (;;) {
1378 		e = &icp_event_buffer[eindex];
1379 		if (e->event_source == 0)
1380 			break;
1381 		if ((e->application & application) == 0) {
1382 			e->application |= application;
1383 			found = 1;
1384 			break;
1385 		}
1386 		if (eindex == icp_event_lastidx)
1387 			break;
1388 		eindex++;
1389 		if (eindex == ICP_MAX_EVENTS)
1390 			eindex = 0;
1391 	}
1392 	if (found)
1393 		memcpy(estr, e, sizeof(gdt_evt_str));
1394 	else
1395 		estr->event_source = 0;
1396 
1397 	splx(s);
1398 }
1399 
1400 void
1401 icp_clear_events(struct icp_softc *icp)
1402 {
1403 	int s;
1404 
1405 	s = splbio();
1406 	icp_event_oldidx = icp_event_lastidx = 0;
1407 	memset(icp_event_buffer, 0, sizeof(icp_event_buffer));
1408 	splx(s);
1409 }
1410