Home | History | Annotate | Line # | Download | only in ic
ld_aac.c revision 1.21
      1 /*	$NetBSD: ld_aac.c,v 1.21 2008/09/09 12:45:39 tron 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  *
     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 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: ld_aac.c,v 1.21 2008/09/09 12:45:39 tron Exp $");
     34 
     35 #include "rnd.h"
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/kernel.h>
     40 #include <sys/device.h>
     41 #include <sys/buf.h>
     42 #include <sys/bufq.h>
     43 #include <sys/endian.h>
     44 #include <sys/dkio.h>
     45 #include <sys/disk.h>
     46 #if NRND > 0
     47 #include <sys/rnd.h>
     48 #endif
     49 
     50 #include <sys/bus.h>
     51 
     52 #include <uvm/uvm_extern.h>
     53 
     54 #include <dev/ldvar.h>
     55 
     56 #include <dev/ic/aacreg.h>
     57 #include <dev/ic/aacvar.h>
     58 
     59 struct ld_aac_softc {
     60 	struct	ld_softc sc_ld;
     61 	int	sc_hwunit;
     62 };
     63 
     64 static void	ld_aac_attach(device_t, device_t, void *);
     65 static void	ld_aac_intr(struct aac_ccb *);
     66 static int	ld_aac_dobio(struct ld_aac_softc *, void *, int, int, int,
     67 			     struct buf *);
     68 static int	ld_aac_dump(struct ld_softc *, void *, int, int);
     69 static int	ld_aac_match(device_t, cfdata_t, void *);
     70 static int	ld_aac_start(struct ld_softc *, struct buf *);
     71 
     72 CFATTACH_DECL_NEW(ld_aac, sizeof(struct ld_aac_softc),
     73     ld_aac_match, ld_aac_attach, NULL, NULL);
     74 
     75 static int
     76 ld_aac_match(device_t parent, cfdata_t match, void *aux)
     77 {
     78 
     79 	return (1);
     80 }
     81 
     82 static void
     83 ld_aac_attach(device_t parent, device_t self, void *aux)
     84 {
     85 	struct aac_attach_args *aaca = aux;
     86 	struct ld_aac_softc *sc = device_private(self);
     87 	struct ld_softc *ld = &sc->sc_ld;
     88 	struct aac_softc *aac = device_private(parent);
     89 	struct aac_drive *hdr = &aac->sc_hdr[aaca->aaca_unit];
     90 
     91 	ld->sc_dv = self;
     92 
     93 	sc->sc_hwunit = aaca->aaca_unit;
     94 	ld->sc_flags = LDF_ENABLED;
     95 	ld->sc_maxxfer = AAC_MAX_XFER(aac);
     96 	ld->sc_secperunit = hdr->hd_size;
     97 	ld->sc_secsize = AAC_SECTOR_SIZE;
     98 	ld->sc_maxqueuecnt =
     99 	    (aac->sc_max_fibs - AAC_NCCBS_RESERVE) / aac->sc_nunits;
    100 	ld->sc_start = ld_aac_start;
    101 	ld->sc_dump = ld_aac_dump;
    102 
    103 	aprint_normal(": %s\n",
    104 	    aac_describe_code(aac_container_types, hdr->hd_devtype));
    105 	ldattach(ld);
    106 }
    107 
    108 static int
    109 ld_aac_dobio(struct ld_aac_softc *sc, void *data, int datasize, int blkno,
    110 	     int dowrite, struct buf *bp)
    111 {
    112 	struct aac_blockread_response *brr;
    113 	struct aac_blockwrite_response *bwr;
    114 	struct aac_ccb *ac;
    115 	struct aac_softc *aac;
    116 	struct aac_fib *fib;
    117 	bus_dmamap_t xfer;
    118 	u_int32_t status;
    119 	u_int16_t size;
    120 	int s, rv, i;
    121 
    122 	aac = device_private(device_parent(sc->sc_ld.sc_dv));
    123 
    124 	/*
    125 	 * Allocate a command control block and map the data transfer.
    126 	 */
    127 	ac = aac_ccb_alloc(aac, (dowrite ? AAC_CCB_DATA_OUT : AAC_CCB_DATA_IN));
    128 	if (ac == NULL)
    129 		return EBUSY;
    130 	ac->ac_data = data;
    131 	ac->ac_datalen = datasize;
    132 
    133 	if ((rv = aac_ccb_map(aac, ac)) != 0) {
    134 		aac_ccb_free(aac, ac);
    135 		return (rv);
    136 	}
    137 
    138 	/*
    139 	 * Build the command.
    140 	 */
    141 	fib = ac->ac_fib;
    142 
    143         fib->Header.XferState = htole32(AAC_FIBSTATE_HOSTOWNED |
    144 	    AAC_FIBSTATE_INITIALISED | AAC_FIBSTATE_FROMHOST |
    145 	    AAC_FIBSTATE_REXPECTED | AAC_FIBSTATE_NORM |
    146 	    AAC_FIBSTATE_ASYNC | AAC_FIBSTATE_FAST_RESPONSE );
    147 
    148 	if ((aac->sc_quirks & AAC_QUIRK_SG_64BIT) == 0) {
    149 		struct aac_blockread *br;
    150 		struct aac_blockwrite *bw;
    151 		struct aac_sg_entry *sge;
    152 		struct aac_sg_table *sgt;
    153 
    154 		fib->Header.Command = htole16(ContainerCommand);
    155 		if (dowrite) {
    156 			bw = (struct aac_blockwrite *)&fib->data[0];
    157 			bw->Command = htole32(VM_CtBlockWrite);
    158 			bw->ContainerId = htole32(sc->sc_hwunit);
    159 			bw->BlockNumber = htole32(blkno);
    160 			bw->ByteCount = htole32(datasize);
    161 			bw->Stable = htole32(CUNSTABLE);
    162 			/* CSTABLE sometimes?  FUA? */
    163 
    164 			size = sizeof(struct aac_blockwrite);
    165 			sgt = &bw->SgMap;
    166 		} else {
    167 			br = (struct aac_blockread *)&fib->data[0];
    168 			br->Command = htole32(VM_CtBlockRead);
    169 			br->ContainerId = htole32(sc->sc_hwunit);
    170 			br->BlockNumber = htole32(blkno);
    171 			br->ByteCount = htole32(datasize);
    172 
    173 			size = sizeof(struct aac_blockread);
    174 			sgt = &br->SgMap;
    175 		}
    176 
    177 		xfer = ac->ac_dmamap_xfer;
    178 		sgt->SgCount = xfer->dm_nsegs;
    179 		sge = sgt->SgEntry;
    180 
    181 		for (i = 0; i < xfer->dm_nsegs; i++, sge++) {
    182 			sge->SgAddress = htole32(xfer->dm_segs[i].ds_addr);
    183 			sge->SgByteCount = htole32(xfer->dm_segs[i].ds_len);
    184 			AAC_DPRINTF(AAC_D_IO,
    185 			    ("#%d va %p pa %lx len %lx\n", i, data,
    186 			    (u_long)xfer->dm_segs[i].ds_addr,
    187 			    (u_long)xfer->dm_segs[i].ds_len));
    188 		}
    189 
    190 		size += xfer->dm_nsegs * sizeof(struct aac_sg_entry);
    191 		size = htole16(sizeof(fib->Header) + size);
    192 		fib->Header.Size = htole16(size);
    193 	} else {
    194 		struct aac_blockread64 *br;
    195 		struct aac_blockwrite64 *bw;
    196 		struct aac_sg_entry64 *sge;
    197 		struct aac_sg_table64 *sgt;
    198 
    199 		fib->Header.Command = htole16(ContainerCommand64);
    200 		if (dowrite) {
    201 			bw = (struct aac_blockwrite64 *)&fib->data[0];
    202 			bw->Command = htole32(VM_CtHostWrite64);
    203 			bw->BlockNumber = htole32(blkno);
    204 			bw->ContainerId = htole16(sc->sc_hwunit);
    205 			bw->SectorCount = htole16(datasize / AAC_BLOCK_SIZE);
    206 			bw->Pad = 0;
    207 			bw->Flags = 0;
    208 
    209 			size = sizeof(struct aac_blockwrite64);
    210 			sgt = &bw->SgMap64;
    211 		} else {
    212 			br = (struct aac_blockread64 *)&fib->data[0];
    213 			br->Command = htole32(VM_CtHostRead64);
    214 			br->BlockNumber = htole32(blkno);
    215 			br->ContainerId = htole16(sc->sc_hwunit);
    216 			br->SectorCount = htole16(datasize / AAC_BLOCK_SIZE);
    217 			br->Pad = 0;
    218 			br->Flags = 0;
    219 
    220 			size = sizeof(struct aac_blockread64);
    221 			sgt = &br->SgMap64;
    222 		}
    223 
    224 		xfer = ac->ac_dmamap_xfer;
    225 		sgt->SgCount = xfer->dm_nsegs;
    226 		sge = sgt->SgEntry64;
    227 
    228 		for (i = 0; i < xfer->dm_nsegs; i++, sge++) {
    229 			/*
    230 			 * XXX - This is probably an alignment issue on non-x86
    231 			 * platforms since this is a packed array of 64/32-bit
    232 			 * tuples, so every other SgAddress is 32-bit, but not
    233 			 * 64-bit aligned.
    234 			 */
    235 			sge->SgAddress = htole64(xfer->dm_segs[i].ds_addr);
    236 			sge->SgByteCount = htole32(xfer->dm_segs[i].ds_len);
    237 			AAC_DPRINTF(AAC_D_IO,
    238 			    ("#%d va %p pa %llx len %lx\n", i, data,
    239 			    (u_int64_t)xfer->dm_segs[i].ds_addr,
    240 			    (u_long)xfer->dm_segs[i].ds_len));
    241 		}
    242 		size += xfer->dm_nsegs * sizeof(struct aac_sg_entry64);
    243 		size = htole16(sizeof(fib->Header) + size);
    244 		fib->Header.Size = htole16(size);
    245 	}
    246 
    247 	if (bp == NULL) {
    248 		/*
    249 		 * Polled commands must not sit on the software queue.  Wait
    250 		 * up to 30 seconds for the command to complete.
    251 		 */
    252 		s = splbio();
    253 		rv = aac_ccb_poll(aac, ac, 30000);
    254 		aac_ccb_unmap(aac, ac);
    255 		aac_ccb_free(aac, ac);
    256 		splx(s);
    257 
    258 		if (rv == 0) {
    259 			if (dowrite) {
    260 				bwr = (struct aac_blockwrite_response *)
    261 				    &ac->ac_fib->data[0];
    262 				status = le32toh(bwr->Status);
    263 			} else {
    264 				brr = (struct aac_blockread_response *)
    265 				    &ac->ac_fib->data[0];
    266 				status = le32toh(brr->Status);
    267 			}
    268 
    269 			if (status != ST_OK) {
    270 				aprint_error_dev(sc->sc_ld.sc_dv,
    271 				    "I/O error: %s\n",
    272 				    aac_describe_code(aac_command_status_table,
    273 				    status));
    274 				rv = EIO;
    275 			}
    276 		}
    277 	} else {
    278 		ac->ac_device = (struct device *)sc;
    279 		ac->ac_context = bp;
    280 		ac->ac_intr = ld_aac_intr;
    281 		aac_ccb_enqueue(aac, ac);
    282 		rv = 0;
    283 	}
    284 
    285 	return (rv);
    286 }
    287 
    288 static int
    289 ld_aac_start(struct ld_softc *ld, struct buf *bp)
    290 {
    291 
    292 	return (ld_aac_dobio((struct ld_aac_softc *)ld, bp->b_data,
    293 	    bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp));
    294 }
    295 
    296 static void
    297 ld_aac_intr(struct aac_ccb *ac)
    298 {
    299 	struct aac_blockread_response *brr;
    300 	struct aac_blockwrite_response *bwr;
    301 	struct ld_aac_softc *sc;
    302 	struct aac_softc *aac;
    303 	struct buf *bp;
    304 	u_int32_t status;
    305 
    306 	bp = ac->ac_context;
    307 	sc = (struct ld_aac_softc *)ac->ac_device;
    308 	aac = device_private(device_parent(sc->sc_ld.sc_dv));
    309 
    310 	if ((bp->b_flags & B_READ) != 0) {
    311 		brr = (struct aac_blockread_response *)&ac->ac_fib->data[0];
    312 		status = le32toh(brr->Status);
    313 	} else {
    314 		bwr = (struct aac_blockwrite_response *)&ac->ac_fib->data[0];
    315 		status = le32toh(bwr->Status);
    316 	}
    317 
    318 	aac_ccb_unmap(aac, ac);
    319 	aac_ccb_free(aac, ac);
    320 
    321 	if (status != ST_OK) {
    322 		bp->b_error = EIO;
    323 		bp->b_resid = bp->b_bcount;
    324 
    325 		aprint_error_dev(sc->sc_ld.sc_dv, "I/O error: %s\n",
    326 		    aac_describe_code(aac_command_status_table, status));
    327 	} else
    328 		bp->b_resid = 0;
    329 
    330 	lddone(&sc->sc_ld, bp);
    331 }
    332 
    333 static int
    334 ld_aac_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
    335 {
    336 
    337 	return (ld_aac_dobio((struct ld_aac_softc *)ld, data,
    338 	    blkcnt * ld->sc_secsize, blkno, 1, NULL));
    339 }
    340