Home | History | Annotate | Line # | Download | only in ic
icp.c revision 1.5
      1 /*	$NetBSD: icp.c,v 1.5 2002/10/22 01:25:28 simonb Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002 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.
      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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1999, 2000 Niklas Hallqvist.  All rights reserved.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *	This product includes software developed by Niklas Hallqvist.
     53  * 4. The name of the author may not be used to endorse or promote products
     54  *    derived from this software without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     57  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     58  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     59  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     60  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     61   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     62  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     63  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     64  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     65  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     66  *
     67  * from OpenBSD: gdt_common.c,v 1.12 2001/07/04 06:43:18 niklas Exp
     68  */
     69 
     70 /*
     71  * This driver would not have written if it was not for the hardware donations
     72  * from both ICP-Vortex and ko.neT.  I want to thank them for their support.
     73  *
     74  * Re-worked for NetBSD by Andrew Doran.  Test hardware kindly supplied by
     75  * Intel.
     76  */
     77 
     78 #include <sys/cdefs.h>
     79 __KERNEL_RCSID(0, "$NetBSD: icp.c,v 1.5 2002/10/22 01:25:28 simonb 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 <uvm/uvm_extern.h>
     93 
     94 #include <machine/bswap.h>
     95 #include <machine/bus.h>
     96 
     97 #include <dev/pci/pcireg.h>
     98 #include <dev/pci/pcivar.h>
     99 #include <dev/pci/pcidevs.h>
    100 
    101 #include <dev/ic/icpreg.h>
    102 #include <dev/ic/icpvar.h>
    103 
    104 int	icp_async_event(struct icp_softc *, int);
    105 void	icp_ccb_submit(struct icp_softc *icp, struct icp_ccb *ic);
    106 void	icp_chain(struct icp_softc *);
    107 int	icp_print(void *, const char *);
    108 int	icp_submatch(struct device *, struct cfdata *, void *);
    109 void	icp_watchdog(void *);
    110 
    111 int
    112 icp_init(struct icp_softc *icp, const char *intrstr)
    113 {
    114 	struct icp_attach_args icpa;
    115 	struct icp_binfo binfo;
    116 	struct icp_ccb *ic;
    117 	u_int16_t cdev_cnt;
    118 	int i, j, state, feat, nsegs, rv, noscsi, nocache;
    119 
    120 	state = 0;
    121 	noscsi = 0;
    122 	nocache = 0;
    123 
    124 	if (intrstr != NULL)
    125 		printf("%s: interrupting at %s\n", icp->icp_dv.dv_xname,
    126 		    intrstr);
    127 
    128 	SIMPLEQ_INIT(&icp->icp_ccb_queue);
    129 	SIMPLEQ_INIT(&icp->icp_ccb_freelist);
    130 	callout_init(&icp->icp_wdog_callout);
    131 
    132 	/*
    133 	 * Allocate a scratch area.
    134 	 */
    135 	if (bus_dmamap_create(icp->icp_dmat, ICP_SCRATCH_SIZE, 1,
    136 	    ICP_SCRATCH_SIZE, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    137 	    &icp->icp_scr_dmamap) != 0) {
    138 		printf("%s: cannot create scratch dmamap\n",
    139 		    icp->icp_dv.dv_xname);
    140 		return (1);
    141 	}
    142 	state++;
    143 
    144 	if (bus_dmamem_alloc(icp->icp_dmat, ICP_SCRATCH_SIZE, PAGE_SIZE, 0,
    145 	    icp->icp_scr_seg, 1, &nsegs, BUS_DMA_NOWAIT) != 0) {
    146 		printf("%s: cannot alloc scratch dmamem\n",
    147 		    icp->icp_dv.dv_xname);
    148 		goto bail_out;
    149 	}
    150 	state++;
    151 
    152 	if (bus_dmamem_map(icp->icp_dmat, icp->icp_scr_seg, nsegs,
    153 	    ICP_SCRATCH_SIZE, &icp->icp_scr, 0)) {
    154 		printf("%s: cannot map scratch dmamem\n", icp->icp_dv.dv_xname);
    155 		goto bail_out;
    156 	}
    157 	state++;
    158 
    159 	if (bus_dmamap_load(icp->icp_dmat, icp->icp_scr_dmamap, icp->icp_scr,
    160 	    ICP_SCRATCH_SIZE, NULL, BUS_DMA_NOWAIT)) {
    161 		printf("%s: cannot load scratch dmamap\n", icp->icp_dv.dv_xname);
    162 		goto bail_out;
    163 	}
    164 	state++;
    165 
    166 	/*
    167 	 * Allocate and initialize the command control blocks.
    168 	 */
    169 	ic = malloc(sizeof(*ic) * ICP_NCCBS, M_DEVBUF, M_NOWAIT | M_ZERO);
    170 	if ((icp->icp_ccbs = ic) == NULL) {
    171 		printf("%s: malloc() failed\n", icp->icp_dv.dv_xname);
    172 		goto bail_out;
    173 	}
    174 	state++;
    175 
    176 	for (i = 0; i < ICP_NCCBS; i++, ic++) {
    177 		/*
    178 		 * The first two command indexes have special meanings, so
    179 		 * we can't use them.
    180 		 */
    181 		ic->ic_ident = i + 2;
    182 		rv = bus_dmamap_create(icp->icp_dmat, ICP_MAX_XFER,
    183 		    ICP_MAXSG, ICP_MAX_XFER, 0,
    184 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    185 		    &ic->ic_xfer_map);
    186 		if (rv != 0)
    187 			break;
    188 		icp->icp_nccbs++;
    189 		icp_ccb_free(icp, ic);
    190 	}
    191 #ifdef DIAGNOSTIC
    192 	if (icp->icp_nccbs != ICP_NCCBS)
    193 		printf("%s: %d/%d CCBs usable\n", icp->icp_dv.dv_xname,
    194 		    icp->icp_nccbs, ICP_NCCBS);
    195 #endif
    196 
    197 	/*
    198 	 * Initalize the controller.
    199 	 */
    200 	if (!icp_cmd(icp, ICP_SCREENSERVICE, ICP_INIT, 0, 0, 0)) {
    201 		printf("%s: screen service init error %d\n",
    202 		    icp->icp_dv.dv_xname, icp->icp_status);
    203 		goto bail_out;
    204 	}
    205 
    206 	if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_INIT, ICP_LINUX_OS, 0, 0)) {
    207 		printf("%s: cache service init error %d\n",
    208 		    icp->icp_dv.dv_xname, icp->icp_status);
    209 		goto bail_out;
    210 	}
    211 
    212 	icp_cmd(icp, ICP_CACHESERVICE, ICP_UNFREEZE_IO, 0, 0, 0);
    213 
    214 	if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_MOUNT, 0xffff, 1, 0)) {
    215 		printf("%s: cache service mount error %d\n",
    216 		    icp->icp_dv.dv_xname, icp->icp_status);
    217 		goto bail_out;
    218 	}
    219 
    220 	if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_INIT, ICP_LINUX_OS, 0, 0)) {
    221 		printf("%s: cache service post-mount init error %d\n",
    222 		    icp->icp_dv.dv_xname, icp->icp_status);
    223 		goto bail_out;
    224 	}
    225 	cdev_cnt = (u_int16_t)icp->icp_info;
    226 
    227 	if (!icp_cmd(icp, ICP_SCSIRAWSERVICE, ICP_INIT, 0, 0, 0)) {
    228 		printf("%s: raw service init error %d\n",
    229 		    icp->icp_dv.dv_xname, icp->icp_status);
    230 		goto bail_out;
    231 	}
    232 
    233 	/*
    234 	 * Set/get raw service features (scatter/gather).
    235 	 */
    236 	feat = 0;
    237 	if (icp_cmd(icp, ICP_SCSIRAWSERVICE, ICP_SET_FEAT, ICP_SCATTER_GATHER,
    238 	    0, 0))
    239 		if (icp_cmd(icp, ICP_SCSIRAWSERVICE, ICP_GET_FEAT, 0, 0, 0))
    240 			feat = icp->icp_info;
    241 
    242 	if ((feat & ICP_SCATTER_GATHER) == 0) {
    243 #ifdef DIAGNOSTIC
    244 		printf("%s: scatter/gather not supported (raw service)\n",
    245 		    icp->icp_dv.dv_xname);
    246 #endif
    247 		noscsi = 1;
    248 	}
    249 
    250 	/*
    251 	 * Set/get cache service features (scatter/gather).
    252 	 */
    253 	feat = 0;
    254 	if (icp_cmd(icp, ICP_CACHESERVICE, ICP_SET_FEAT, 0,
    255 	    ICP_SCATTER_GATHER, 0))
    256 		if (icp_cmd(icp, ICP_CACHESERVICE, ICP_GET_FEAT, 0, 0, 0))
    257 			feat = icp->icp_info;
    258 
    259 	if ((feat & ICP_SCATTER_GATHER) == 0) {
    260 #ifdef DIAGNOSTIC
    261 		printf("%s: scatter/gather not supported (cache service)\n",
    262 		    icp->icp_dv.dv_xname);
    263 #endif
    264 		nocache = 1;
    265 	}
    266 
    267 	/*
    268 	 * Pull some information from the board and dump.
    269 	 */
    270 	if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_IOCTL, ICP_BOARD_INFO,
    271 	    ICP_INVALID_CHANNEL, sizeof(struct icp_binfo))) {
    272 		printf("%s: unable to retrive board info\n",
    273 		    icp->icp_dv.dv_xname);
    274 		goto bail_out;
    275 	}
    276 	memcpy(&binfo, icp->icp_scr, sizeof(binfo));
    277 
    278 	printf("%s: model <%s>, firmware <%s>, %d channel(s), %dMB memory\n",
    279 	    icp->icp_dv.dv_xname, binfo.bi_type_string, binfo.bi_raid_string,
    280 	    binfo.bi_chan_count, le32toh(binfo.bi_memsize) >> 20);
    281 
    282 	/*
    283 	 * Determine the number of devices, and number of openings per
    284 	 * device.
    285 	 */
    286 	if (!nocache) {
    287 		for (j = 0; j < cdev_cnt && j < ICP_MAX_HDRIVES; j++) {
    288 			if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_INFO, j, 0,
    289 			    0))
    290 				continue;
    291 
    292 			icp->icp_cdr[j].cd_size = icp->icp_info;
    293 			icp->icp_ndevs++;
    294 
    295 			if (icp_cmd(icp, ICP_CACHESERVICE, ICP_DEVTYPE, j, 0,
    296 			    0))
    297 				icp->icp_cdr[j].cd_type = icp->icp_info;
    298 		}
    299 	}
    300 
    301 	if (!noscsi)
    302 		icp->icp_ndevs += binfo.bi_chan_count;
    303 
    304 	if (icp->icp_ndevs != 0)
    305 		icp->icp_openings =
    306 		    (icp->icp_nccbs - ICP_NCCB_RESERVE) / icp->icp_ndevs;
    307 #ifdef ICP_DEBUG
    308 	printf("%s: %d openings per device\n", icp->icp_dv.dv_xname,
    309 	    icp->icp_openings);
    310 #endif
    311 
    312 	/*
    313 	 * Attach SCSI channels.
    314 	 */
    315 	if (!noscsi) {
    316 		struct icp_ioc_version *iv;
    317 		struct icp_rawioc *ri;
    318 		struct icp_getch *gc;
    319 
    320 		iv = (struct icp_ioc_version *)icp->icp_scr;
    321 		iv->iv_version = htole32(ICP_IOC_NEWEST);
    322 		iv->iv_listents = ICP_MAXBUS;
    323 		iv->iv_firstchan = 0;
    324 		iv->iv_lastchan = ICP_MAXBUS - 1;
    325 		iv->iv_listoffset = htole32(sizeof(*iv));
    326 
    327 		if (icp_cmd(icp, ICP_CACHESERVICE, ICP_IOCTL,
    328 		    ICP_IOCHAN_RAW_DESC, ICP_INVALID_CHANNEL,
    329 		    sizeof(*iv) + ICP_MAXBUS * sizeof(*ri))) {
    330 			ri = (struct icp_rawioc *)(iv + 1);
    331 			for (j = 0; j < binfo.bi_chan_count; j++, ri++)
    332 				icp->icp_bus_id[j] = ri->ri_procid;
    333 		} else {
    334 			/*
    335 			 * Fall back to the old method.
    336 			 */
    337 			gc = (struct icp_getch *)icp->icp_scr;
    338 
    339 			for (i = 0; j < binfo.bi_chan_count; j++) {
    340 				if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_IOCTL,
    341 				    ICP_SCSI_CHAN_CNT | ICP_L_CTRL_PATTERN,
    342 				    ICP_IO_CHANNEL | ICP_INVALID_CHANNEL,
    343 				    sizeof(*gc))) {
    344 				    	printf("%s: unable to get chan info",
    345 				    	    icp->icp_dv.dv_xname);
    346 					goto bail_out;
    347 				}
    348 				icp->icp_bus_id[j] = gc->gc_scsiid;
    349 			}
    350 		}
    351 
    352 		for (j = 0; j < binfo.bi_chan_count; j++) {
    353 			if (icp->icp_bus_id[j] > ICP_MAXID_FC)
    354 				icp->icp_bus_id[j] = ICP_MAXID_FC;
    355 
    356 			icpa.icpa_unit = j + ICPA_UNIT_SCSI;
    357 			config_found_sm(&icp->icp_dv, &icpa, icp_print,
    358 			    icp_submatch);
    359 		}
    360 	}
    361 
    362 	/*
    363 	 * Attach cache devices.
    364 	 */
    365 	if (!nocache) {
    366 		for (j = 0; j < cdev_cnt && j < ICP_MAX_HDRIVES; j++) {
    367 			if (icp->icp_cdr[j].cd_size == 0)
    368 				continue;
    369 
    370 			icpa.icpa_unit = j;
    371 			config_found_sm(&icp->icp_dv, &icpa, icp_print,
    372 			    icp_submatch);
    373 		}
    374 	}
    375 
    376 	/*
    377 	 * Start the watchdog.
    378 	 */
    379 	icp_watchdog(icp);
    380 	return (0);
    381 
    382  bail_out:
    383 	if (state > 4)
    384 		for (j = 0; j < i; j++)
    385 			bus_dmamap_destroy(icp->icp_dmat,
    386 			    icp->icp_ccbs[j].ic_xfer_map);
    387  	if (state > 3)
    388 		free(icp->icp_ccbs, M_DEVBUF);
    389 	if (state > 2)
    390 		bus_dmamap_unload(icp->icp_dmat, icp->icp_scr_dmamap);
    391 	if (state > 1)
    392 		bus_dmamem_unmap(icp->icp_dmat, icp->icp_scr,
    393 		    ICP_SCRATCH_SIZE);
    394 	if (state > 0)
    395 		bus_dmamem_free(icp->icp_dmat, icp->icp_scr_seg, nsegs);
    396 	bus_dmamap_destroy(icp->icp_dmat, icp->icp_scr_dmamap);
    397 
    398 	return (1);
    399 }
    400 
    401 void
    402 icp_watchdog(void *cookie)
    403 {
    404 	struct icp_softc *icp;
    405 	int s;
    406 
    407 	icp = cookie;
    408 
    409 	s = splbio();
    410 	icp_intr(icp);
    411 	if (! SIMPLEQ_EMPTY(&icp->icp_ccb_queue))
    412 		icp_ccb_enqueue(icp, NULL);
    413 	splx(s);
    414 
    415 	callout_reset(&icp->icp_wdog_callout, hz * ICP_WATCHDOG_FREQ,
    416 	    icp_watchdog, icp);
    417 }
    418 
    419 int
    420 icp_print(void *aux, const char *pnp)
    421 {
    422 	struct icp_attach_args *icpa;
    423 	const char *str;
    424 
    425 	icpa = (struct icp_attach_args *)aux;
    426 
    427 	if (pnp != NULL) {
    428 		if (icpa->icpa_unit < ICPA_UNIT_SCSI)
    429 			str = "block device";
    430 		else
    431 			str = "SCSI channel";
    432 		printf("%s at %s", str, pnp);
    433 	}
    434 	printf(" unit %d", icpa->icpa_unit);
    435 
    436 	return (UNCONF);
    437 }
    438 
    439 int
    440 icp_submatch(struct device *parent, struct cfdata *cf, void *aux)
    441 {
    442 	struct icp_attach_args *icpa;
    443 
    444 	icpa = (struct icp_attach_args *)aux;
    445 
    446 	if (cf->icpacf_unit != ICPCF_UNIT_DEFAULT &&
    447 	    cf->icpacf_unit != icpa->icpa_unit)
    448 		return (0);
    449 
    450 	return (config_match(parent, cf, aux));
    451 }
    452 
    453 int
    454 icp_async_event(struct icp_softc *icp, int val)
    455 {
    456 
    457 	/* XXX */
    458 	return (1);
    459 }
    460 
    461 int
    462 icp_intr(void *cookie)
    463 {
    464 	struct icp_softc *icp;
    465 	struct icp_intr_ctx ctx;
    466 	struct icp_ccb *ic;
    467 
    468 	icp = cookie;
    469 
    470 	ctx.istatus = (*icp->icp_get_status)(icp);
    471 	if (!ctx.istatus) {
    472 		icp->icp_status = ICP_S_NO_STATUS;
    473 		return (0);
    474 	}
    475 
    476 	(*icp->icp_intr)(icp, &ctx);
    477 
    478 	icp->icp_status = ctx.cmd_status;
    479 	icp->icp_info = ctx.info;
    480 	icp->icp_info2 = ctx.info2;
    481 
    482 	switch (ctx.istatus) {
    483 	case ICP_ASYNCINDEX:
    484 		icp_async_event(icp, ctx.service);
    485 		return (1);
    486 
    487 	case ICP_SPEZINDEX:
    488 		printf("%s: uninitialized or unknown service (%d/%d)\n",
    489 		    icp->icp_dv.dv_xname, ctx.info, ctx.info2);
    490 		return (1);
    491 	}
    492 
    493 	if ((ctx.istatus - 2) > icp->icp_nccbs)
    494 		panic("icp_intr: bad command index returned");
    495 
    496 	ic = &icp->icp_ccbs[ctx.istatus - 2];
    497 	ic->ic_status = icp->icp_status;
    498 
    499 	if ((ic->ic_flags & IC_ALLOCED) == 0)
    500 		panic("icp_intr: inactive CCB identified");
    501 
    502 	switch (icp->icp_status) {
    503 	case ICP_S_BSY:
    504 #ifdef ICP_DEBUG
    505 		printf("%s: ICP_S_BSY received\n", icp->icp_dv.dv_xname);
    506 #endif
    507 		SIMPLEQ_INSERT_HEAD(&icp->icp_ccb_queue, ic, ic_chain);
    508 		break;
    509 
    510 	default:
    511 		ic->ic_flags |= IC_COMPLETE;
    512 
    513 		if ((ic->ic_flags & IC_WAITING) != 0)
    514 			wakeup(ic);
    515 		else if (ic->ic_intr != NULL)
    516 			(*ic->ic_intr)(ic);
    517 
    518 		if (! SIMPLEQ_EMPTY(&icp->icp_ccb_queue))
    519 			icp_ccb_enqueue(icp, NULL);
    520 
    521 		break;
    522 	}
    523 
    524 	return (1);
    525 }
    526 
    527 int
    528 icp_cmd(struct icp_softc *icp, u_int8_t service, u_int16_t opcode,
    529 	u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
    530 {
    531 	struct icp_ioctlcmd *icmd;
    532 	struct icp_cachecmd *cc;
    533 	struct icp_rawcmd *rc;
    534 	int retries, rv;
    535 	struct icp_ccb *ic;
    536 
    537 	retries = ICP_RETRIES;
    538 
    539 	do {
    540 		ic = icp_ccb_alloc(icp);
    541 		memset(&ic->ic_cmd, 0, sizeof(ic->ic_cmd));
    542 		ic->ic_cmd.cmd_opcode = htole16(opcode);
    543 
    544 		switch (service) {
    545 		case ICP_CACHESERVICE:
    546 			if (opcode == ICP_IOCTL) {
    547 				icmd = &ic->ic_cmd.cmd_packet.ic;
    548 				icmd->ic_subfunc = htole16(arg1);
    549 				icmd->ic_channel = htole32(arg2);
    550 				icmd->ic_bufsize = htole32(arg3);
    551 				icmd->ic_addr =
    552 				    htole32(icp->icp_scr_seg[0].ds_addr);
    553 
    554 				bus_dmamap_sync(icp->icp_dmat,
    555 				    icp->icp_scr_dmamap, 0, arg3,
    556 				    BUS_DMASYNC_PREWRITE |
    557 				    BUS_DMASYNC_PREREAD);
    558 			} else {
    559 				cc = &ic->ic_cmd.cmd_packet.cc;
    560 				cc->cc_deviceno = htole16(arg1);
    561 				cc->cc_blockno = htole32(arg2);
    562 			}
    563 			break;
    564 
    565 		case ICP_SCSIRAWSERVICE:
    566 			rc = &ic->ic_cmd.cmd_packet.rc;
    567 			rc->rc_direction = htole32(arg1);
    568 			rc->rc_bus = arg2;
    569 			rc->rc_target = arg3;
    570 			rc->rc_lun = arg3 >> 8;
    571 			break;
    572 		}
    573 
    574 		ic->ic_service = service;
    575 		ic->ic_cmdlen = sizeof(ic->ic_cmd);
    576 		rv = icp_ccb_poll(icp, ic, 10000);
    577 
    578 		switch (service) {
    579 		case ICP_CACHESERVICE:
    580 			if (opcode == ICP_IOCTL) {
    581 				bus_dmamap_sync(icp->icp_dmat,
    582 				    icp->icp_scr_dmamap, 0, arg3,
    583 				    BUS_DMASYNC_POSTWRITE |
    584 				    BUS_DMASYNC_POSTREAD);
    585 			}
    586 			break;
    587 		}
    588 
    589 		icp_ccb_free(icp, ic);
    590 	} while (rv != 0 && --retries > 0);
    591 
    592 	return (icp->icp_status == ICP_S_OK);
    593 }
    594 
    595 struct icp_ccb *
    596 icp_ccb_alloc(struct icp_softc *icp)
    597 {
    598 	struct icp_ccb *ic;
    599 	int s;
    600 
    601 	s = splbio();
    602 	ic = SIMPLEQ_FIRST(&icp->icp_ccb_freelist);
    603 	SIMPLEQ_REMOVE_HEAD(&icp->icp_ccb_freelist, ic_chain);
    604 	splx(s);
    605 
    606 	ic->ic_flags = IC_ALLOCED;
    607 	return (ic);
    608 }
    609 
    610 void
    611 icp_ccb_free(struct icp_softc *icp, struct icp_ccb *ic)
    612 {
    613 	int s;
    614 
    615 	s = splbio();
    616 	ic->ic_flags = 0;
    617 	ic->ic_intr = NULL;
    618 	SIMPLEQ_INSERT_HEAD(&icp->icp_ccb_freelist, ic, ic_chain);
    619 	splx(s);
    620 }
    621 
    622 void
    623 icp_ccb_enqueue(struct icp_softc *icp, struct icp_ccb *ic)
    624 {
    625 	int s;
    626 
    627 	s = splbio();
    628 
    629 	if (ic != NULL)
    630 		SIMPLEQ_INSERT_TAIL(&icp->icp_ccb_queue, ic, ic_chain);
    631 
    632 	while ((ic = SIMPLEQ_FIRST(&icp->icp_ccb_queue)) != NULL) {
    633 		if ((*icp->icp_test_busy)(icp))
    634 			break;
    635 		icp_ccb_submit(icp, ic);
    636 		SIMPLEQ_REMOVE_HEAD(&icp->icp_ccb_queue, ic_chain);
    637 	}
    638 
    639 	splx(s);
    640 }
    641 
    642 int
    643 icp_ccb_map(struct icp_softc *icp, struct icp_ccb *ic, void *data, int size,
    644 	    int dir)
    645 {
    646 	struct icp_sg *sg;
    647 	int nsegs, i, rv;
    648 	bus_dmamap_t xfer;
    649 
    650 	xfer = ic->ic_xfer_map;
    651 
    652 	rv = bus_dmamap_load(icp->icp_dmat, xfer, data, size, NULL,
    653 	    BUS_DMA_NOWAIT | BUS_DMA_STREAMING |
    654 	    ((dir & IC_XFER_IN) ? BUS_DMA_READ : BUS_DMA_WRITE));
    655 	if (rv != 0)
    656 		return (rv);
    657 
    658 	nsegs = xfer->dm_nsegs;
    659 	ic->ic_xfer_size = size;
    660 	ic->ic_nsgent = nsegs;
    661 	ic->ic_flags |= dir;
    662 	sg = ic->ic_sg;
    663 
    664 	if (sg != NULL) {
    665 		for (i = 0; i < nsegs; i++, sg++) {
    666 			sg->sg_addr = htole32(xfer->dm_segs[i].ds_addr);
    667 			sg->sg_len = htole32(xfer->dm_segs[i].ds_len);
    668 		}
    669 	} else if (nsegs > 1)
    670 		panic("icp_ccb_map: no SG list specified, but nsegs > 1");
    671 
    672 	if ((dir & IC_XFER_OUT) != 0)
    673 		i = BUS_DMASYNC_PREWRITE;
    674 	else /* if ((dir & IC_XFER_IN) != 0) */
    675 		i = BUS_DMASYNC_PREREAD;
    676 
    677 	bus_dmamap_sync(icp->icp_dmat, xfer, 0, ic->ic_xfer_size, i);
    678 	return (0);
    679 }
    680 
    681 void
    682 icp_ccb_unmap(struct icp_softc *icp, struct icp_ccb *ic)
    683 {
    684 	int i;
    685 
    686 	if ((ic->ic_flags & IC_XFER_OUT) != 0)
    687 		i = BUS_DMASYNC_POSTWRITE;
    688 	else /* if ((ic->ic_flags & IC_XFER_IN) != 0) */
    689 		i = BUS_DMASYNC_POSTREAD;
    690 
    691 	bus_dmamap_sync(icp->icp_dmat, ic->ic_xfer_map, 0, ic->ic_xfer_size, i);
    692 	bus_dmamap_unload(icp->icp_dmat, ic->ic_xfer_map);
    693 }
    694 
    695 int
    696 icp_ccb_poll(struct icp_softc *icp, struct icp_ccb *ic, int timo)
    697 {
    698 	int rv;
    699 
    700 	for (timo = ICP_BUSY_WAIT_MS * 100; timo != 0; timo--) {
    701 		if (!(*icp->icp_test_busy)(icp))
    702 			break;
    703 		DELAY(10);
    704 	}
    705 	if (timo == 0) {
    706 		printf("%s: submit: busy\n", icp->icp_dv.dv_xname);
    707 		return (EAGAIN);
    708 	}
    709 
    710 	icp_ccb_submit(icp, ic);
    711 
    712 	for (timo *= 10; timo != 0; timo--) {
    713 		DELAY(100);
    714 		icp_intr(icp);
    715 		if ((ic->ic_flags & IC_COMPLETE) != 0)
    716 			break;
    717 	}
    718 
    719 	if (timo != 0) {
    720 		if (ic->ic_status != ICP_S_OK) {
    721 #ifdef ICP_DEBUG
    722 			printf("%s: request failed; status=0x%04x\n",
    723 			    icp->icp_dv.dv_xname, ic->ic_status);
    724 #endif
    725 			rv = EIO;
    726 		} else
    727 			rv = 0;
    728 	} else {
    729 		printf("%s: command timed out\n", icp->icp_dv.dv_xname);
    730 		rv = EIO;
    731 	}
    732 
    733 	while ((*icp->icp_test_busy)(icp) != 0)
    734 		DELAY(10);
    735 
    736 	return (rv);
    737 }
    738 
    739 int
    740 icp_ccb_wait(struct icp_softc *icp, struct icp_ccb *ic, int timo)
    741 {
    742 	int s, rv;
    743 
    744 	ic->ic_flags |= IC_WAITING;
    745 
    746 	s = splbio();
    747 	icp_ccb_enqueue(icp, ic);
    748 	if ((rv = tsleep(ic, PRIBIO, "icpwccb", mstohz(timo))) != 0) {
    749 		splx(s);
    750 		return (rv);
    751 	}
    752 	splx(s);
    753 
    754 	if (ic->ic_status != ICP_S_OK) {
    755 		printf("%s: command failed; status=%x\n", icp->icp_dv.dv_xname,
    756 		    ic->ic_status);
    757 		return (EIO);
    758 	}
    759 
    760 	return (0);
    761 }
    762 
    763 void
    764 icp_ccb_submit(struct icp_softc *icp, struct icp_ccb *ic)
    765 {
    766 
    767 	ic->ic_cmdlen = (ic->ic_cmdlen + 3) & ~3;
    768 
    769 	(*icp->icp_set_sema0)(icp);
    770 	DELAY(10);
    771 
    772 	ic->ic_cmd.cmd_boardnode = htole32(ICP_LOCALBOARD);
    773 	ic->ic_cmd.cmd_cmdindex = htole32(ic->ic_ident);
    774 
    775 	(*icp->icp_copy_cmd)(icp, ic);
    776 	(*icp->icp_release_event)(icp, ic);
    777 }
    778