Home | History | Annotate | Line # | Download | only in ic
ld_aac.c revision 1.13
      1 /*	$NetBSD: ld_aac.c,v 1.13 2006/11/16 01:32:51 christos 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 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: ld_aac.c,v 1.13 2006/11/16 01:32:51 christos Exp $");
     41 
     42 #include "rnd.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/kernel.h>
     47 #include <sys/device.h>
     48 #include <sys/buf.h>
     49 #include <sys/bufq.h>
     50 #include <sys/endian.h>
     51 #include <sys/dkio.h>
     52 #include <sys/disk.h>
     53 #if NRND > 0
     54 #include <sys/rnd.h>
     55 #endif
     56 
     57 #include <machine/bus.h>
     58 
     59 #include <uvm/uvm_extern.h>
     60 
     61 #include <dev/ldvar.h>
     62 
     63 #include <dev/ic/aacreg.h>
     64 #include <dev/ic/aacvar.h>
     65 
     66 struct ld_aac_softc {
     67 	struct	ld_softc sc_ld;
     68 	int	sc_hwunit;
     69 };
     70 
     71 static void	ld_aac_attach(struct device *, struct device *, void *);
     72 static void	ld_aac_intr(struct aac_ccb *);
     73 static int	ld_aac_dobio(struct ld_aac_softc *, void *, int, int, int,
     74 			     struct buf *);
     75 static int	ld_aac_dump(struct ld_softc *, void *, int, int);
     76 static int	ld_aac_match(struct device *, struct cfdata *, void *);
     77 static int	ld_aac_start(struct ld_softc *, struct buf *);
     78 
     79 CFATTACH_DECL(ld_aac, sizeof(struct ld_aac_softc),
     80     ld_aac_match, ld_aac_attach, NULL, NULL);
     81 
     82 static int
     83 ld_aac_match(struct device *parent, struct cfdata *match,
     84     void *aux)
     85 {
     86 
     87 	return (1);
     88 }
     89 
     90 static void
     91 ld_aac_attach(struct device *parent, struct device *self, void *aux)
     92 {
     93 	struct aac_attach_args *aaca;
     94 	struct aac_drive *hdr;
     95 	struct ld_aac_softc *sc;
     96 	struct ld_softc *ld;
     97 	struct aac_softc *aac;
     98 
     99 	aaca = aux;
    100 	aac = (struct aac_softc *)parent;
    101 	sc = (struct ld_aac_softc *)self;
    102 	ld = &sc->sc_ld;
    103 	hdr = &aac->sc_hdr[aaca->aaca_unit];
    104 
    105 	sc->sc_hwunit = aaca->aaca_unit;
    106 	ld->sc_flags = LDF_ENABLED;
    107 	ld->sc_maxxfer = AAC_MAX_XFER;
    108 	ld->sc_secperunit = hdr->hd_size;
    109 	ld->sc_secsize = AAC_SECTOR_SIZE;
    110 	ld->sc_maxqueuecnt = (AAC_NCCBS - AAC_NCCBS_RESERVE) / aac->sc_nunits;
    111 	ld->sc_start = ld_aac_start;
    112 	ld->sc_dump = ld_aac_dump;
    113 
    114 	aprint_normal(": %s\n",
    115 	    aac_describe_code(aac_container_types, hdr->hd_devtype));
    116 	ldattach(ld);
    117 }
    118 
    119 static int
    120 ld_aac_dobio(struct ld_aac_softc *sc, void *data, int datasize, int blkno,
    121 	     int dowrite, struct buf *bp)
    122 {
    123 	struct aac_blockread_response *brr;
    124 	struct aac_blockwrite_response *bwr;
    125 	struct aac_ccb *ac;
    126 	struct aac_softc *aac;
    127 	struct aac_blockread *br;
    128 	struct aac_blockwrite *bw;
    129 	struct aac_sg_entry *sge;
    130 	struct aac_sg_table *sgt;
    131 	struct aac_fib *fib;
    132 	bus_dmamap_t xfer;
    133 	u_int32_t status;
    134 	u_int16_t size;
    135 	int s, rv, i;
    136 
    137 	aac = (struct aac_softc *)device_parent(&sc->sc_ld.sc_dv);
    138 
    139 	/*
    140 	 * Allocate a command control block and map the data transfer.
    141 	 */
    142 	ac = aac_ccb_alloc(aac, (dowrite ? AAC_CCB_DATA_OUT : AAC_CCB_DATA_IN));
    143 	ac->ac_data = data;
    144 	ac->ac_datalen = datasize;
    145 
    146 	if ((rv = aac_ccb_map(aac, ac)) != 0) {
    147 		aac_ccb_free(aac, ac);
    148 		return (rv);
    149 	}
    150 
    151 	/*
    152 	 * Build the command.
    153 	 */
    154 	fib = ac->ac_fib;
    155 
    156         fib->Header.XferState = htole32(AAC_FIBSTATE_HOSTOWNED |
    157 	    AAC_FIBSTATE_INITIALISED | AAC_FIBSTATE_FROMHOST |
    158 	    AAC_FIBSTATE_REXPECTED | AAC_FIBSTATE_NORM);
    159 	fib->Header.Command = htole16(ContainerCommand);
    160 
    161 	if (dowrite) {
    162 		bw = (struct aac_blockwrite *)&fib->data[0];
    163 		bw->Command = htole32(VM_CtBlockWrite);
    164 		bw->ContainerId = htole32(sc->sc_hwunit);
    165 		bw->BlockNumber = htole32(blkno);
    166 		bw->ByteCount = htole32(datasize);
    167 		bw->Stable = htole32(CUNSTABLE); /* XXX what's appropriate here? */
    168 
    169 		size = sizeof(struct aac_blockwrite);
    170 		sgt = &bw->SgMap;
    171 	} else {
    172 		br = (struct aac_blockread *)&fib->data[0];
    173 		br->Command = htole32(VM_CtBlockRead);
    174 		br->ContainerId = htole32(sc->sc_hwunit);
    175 		br->BlockNumber = htole32(blkno);
    176 		br->ByteCount = htole32(datasize);
    177 
    178 		size = sizeof(struct aac_blockread);
    179 		sgt = &br->SgMap;
    180 	}
    181 
    182 	xfer = ac->ac_dmamap_xfer;
    183 	sgt->SgCount = xfer->dm_nsegs;
    184 	sge = sgt->SgEntry;
    185 
    186 	for (i = 0; i < xfer->dm_nsegs; i++, sge++) {
    187 		sge->SgAddress = htole32(xfer->dm_segs[i].ds_addr);
    188 		sge->SgByteCount = htole32(xfer->dm_segs[i].ds_len);
    189 		AAC_DPRINTF(AAC_D_IO,
    190 		    ("#%d va %p pa %lx len %lx\n", i, data,
    191 		    (u_long)xfer->dm_segs[i].ds_addr,
    192 		    (u_long)xfer->dm_segs[i].ds_len));
    193 	}
    194 
    195 	size += xfer->dm_nsegs * sizeof(struct aac_sg_entry);
    196 	size = htole16(sizeof(fib->Header) + size);
    197 	fib->Header.Size = htole16(size);
    198 
    199 	if (bp == NULL) {
    200 		/*
    201 		 * Polled commands must not sit on the software queue.  Wait
    202 		 * up to 30 seconds for the command to complete.
    203 		 */
    204 		s = splbio();
    205 		rv = aac_ccb_poll(aac, ac, 30000);
    206 		aac_ccb_unmap(aac, ac);
    207 		aac_ccb_free(aac, ac);
    208 		splx(s);
    209 
    210 		if (rv == 0) {
    211 			if (dowrite) {
    212 				bwr = (struct aac_blockwrite_response *)
    213 				    &ac->ac_fib->data[0];
    214 				status = le32toh(bwr->Status);
    215 			} else {
    216 				brr = (struct aac_blockread_response *)
    217 				    &ac->ac_fib->data[0];
    218 				status = le32toh(brr->Status);
    219 			}
    220 
    221 			if (status != ST_OK) {
    222 				printf("%s: I/O error: %s\n",
    223 				    sc->sc_ld.sc_dv.dv_xname,
    224 				    aac_describe_code(aac_command_status_table,
    225 				    status));
    226 				rv = EIO;
    227 			}
    228 		}
    229 	} else {
    230 		ac->ac_device = (struct device *)sc;
    231 		ac->ac_context = bp;
    232 		ac->ac_intr = ld_aac_intr;
    233 		aac_ccb_enqueue(aac, ac);
    234 		rv = 0;
    235 	}
    236 
    237 	return (rv);
    238 }
    239 
    240 static int
    241 ld_aac_start(struct ld_softc *ld, struct buf *bp)
    242 {
    243 
    244 	return (ld_aac_dobio((struct ld_aac_softc *)ld, bp->b_data,
    245 	    bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp));
    246 }
    247 
    248 static void
    249 ld_aac_intr(struct aac_ccb *ac)
    250 {
    251 	struct aac_blockread_response *brr;
    252 	struct aac_blockwrite_response *bwr;
    253 	struct ld_aac_softc *sc;
    254 	struct aac_softc *aac;
    255 	struct buf *bp;
    256 	u_int32_t status;
    257 
    258 	bp = ac->ac_context;
    259 	sc = (struct ld_aac_softc *)ac->ac_device;
    260 	aac = (struct aac_softc *)device_parent(&sc->sc_ld.sc_dv);
    261 
    262 	if ((bp->b_flags & B_READ) != 0) {
    263 		brr = (struct aac_blockread_response *)&ac->ac_fib->data[0];
    264 		status = le32toh(brr->Status);
    265 	} else {
    266 		bwr = (struct aac_blockwrite_response *)&ac->ac_fib->data[0];
    267 		status = le32toh(bwr->Status);
    268 	}
    269 
    270 	aac_ccb_unmap(aac, ac);
    271 	aac_ccb_free(aac, ac);
    272 
    273 	if (status != ST_OK) {
    274 		bp->b_flags |= B_ERROR;
    275 		bp->b_error = EIO;
    276 		bp->b_resid = bp->b_bcount;
    277 
    278 		printf("%s: I/O error: %s\n", sc->sc_ld.sc_dv.dv_xname,
    279 		    aac_describe_code(aac_command_status_table, status));
    280 	} else
    281 		bp->b_resid = 0;
    282 
    283 	lddone(&sc->sc_ld, bp);
    284 }
    285 
    286 static int
    287 ld_aac_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
    288 {
    289 
    290 	return (ld_aac_dobio((struct ld_aac_softc *)ld, data,
    291 	    blkcnt * ld->sc_secsize, blkno, 1, NULL));
    292 }
    293