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