Home | History | Annotate | Line # | Download | only in pci
ld_amr.c revision 1.10
      1 /*	$NetBSD: ld_amr.c,v 1.10 2006/07/23 12:01:26 bouyer 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.
      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  * AMI RAID controller front-end for ld(4) driver.
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: ld_amr.c,v 1.10 2006/07/23 12:01:26 bouyer Exp $");
     45 
     46 #include "rnd.h"
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/kernel.h>
     51 #include <sys/device.h>
     52 #include <sys/buf.h>
     53 #include <sys/bufq.h>
     54 #include <sys/endian.h>
     55 #include <sys/dkio.h>
     56 #include <sys/disk.h>
     57 #if NRND > 0
     58 #include <sys/rnd.h>
     59 #endif
     60 
     61 #include <uvm/uvm_extern.h>
     62 
     63 #include <machine/bus.h>
     64 
     65 #include <dev/ldvar.h>
     66 
     67 #include <dev/pci/pcireg.h>
     68 #include <dev/pci/pcivar.h>
     69 #include <dev/pci/amrreg.h>
     70 #include <dev/pci/amrvar.h>
     71 
     72 struct ld_amr_softc {
     73 	struct	ld_softc sc_ld;
     74 	int	sc_hwunit;
     75 };
     76 
     77 static int	ld_amr_dobio(struct ld_amr_softc *, void *, int, int, int,
     78 			     struct buf *);
     79 static int	ld_amr_dump(struct ld_softc *, void *, int, int);
     80 static void	ld_amr_handler(struct amr_ccb *);
     81 static int	ld_amr_start(struct ld_softc *, struct buf *);
     82 
     83 static int
     84 ld_amr_match(struct device *parent, struct cfdata *match, void *aux)
     85 {
     86 
     87 	return (1);
     88 }
     89 
     90 static void
     91 ld_amr_attach(struct device *parent, struct device *self, void *aux)
     92 {
     93 	struct amr_attach_args *amra;
     94 	struct ld_amr_softc *sc;
     95 	struct ld_softc *ld;
     96 	struct amr_softc *amr;
     97 	const char *statestr;
     98 	int happy;
     99 
    100 	sc = (struct ld_amr_softc *)self;
    101 	ld = &sc->sc_ld;
    102 	amr = (struct amr_softc *)parent;
    103 	amra = aux;
    104 
    105 	sc->sc_hwunit = amra->amra_unit;
    106 	ld->sc_maxxfer = amr_max_xfer;
    107 	ld->sc_maxqueuecnt = (amr->amr_maxqueuecnt - AMR_NCCB_RESV)
    108 	    / amr->amr_numdrives;
    109 	ld->sc_secperunit = amr->amr_drive[sc->sc_hwunit].al_size;
    110 	ld->sc_secsize = AMR_SECTOR_SIZE;
    111 	ld->sc_start = ld_amr_start;
    112 	ld->sc_dump = ld_amr_dump;
    113 
    114 	if (ld->sc_maxqueuecnt > AMR_MAX_CMDS_PU)
    115 		ld->sc_maxqueuecnt = AMR_MAX_CMDS_PU;
    116 
    117 	/*
    118 	 * Print status information and attach to the ld driver proper.
    119 	 */
    120 	statestr = amr_drive_state(amr->amr_drive[sc->sc_hwunit].al_state,
    121 	    &happy);
    122 	if (happy)
    123 		ld->sc_flags = LDF_ENABLED;
    124 	aprint_normal(": RAID %d, %s\n",
    125 	    amr->amr_drive[sc->sc_hwunit].al_properties & AMR_DRV_RAID_MASK,
    126 	    statestr);
    127 
    128 	ldattach(ld);
    129 }
    130 
    131 CFATTACH_DECL(ld_amr, sizeof(struct ld_amr_softc),
    132     ld_amr_match, ld_amr_attach, NULL, NULL);
    133 
    134 static int
    135 ld_amr_dobio(struct ld_amr_softc *sc, void *data, int datasize,
    136 	     int blkno, int dowrite, struct buf *bp)
    137 {
    138 	struct amr_ccb *ac;
    139 	struct amr_softc *amr;
    140 	struct amr_mailbox_cmd *mb;
    141 	int s, rv;
    142 
    143 	amr = (struct amr_softc *)device_parent(&sc->sc_ld.sc_dv);
    144 
    145 	if ((rv = amr_ccb_alloc(amr, &ac)) != 0)
    146 		return (rv);
    147 
    148 	mb = &ac->ac_cmd;
    149 	mb->mb_command = (dowrite ? AMR_CMD_LWRITE : AMR_CMD_LREAD);
    150 	mb->mb_drive = sc->sc_hwunit;
    151 	mb->mb_blkcount = htole16(datasize / AMR_SECTOR_SIZE);
    152 	mb->mb_lba = htole32(blkno);
    153 
    154 	rv = amr_ccb_map(amr, ac, data, datasize,
    155 	    (dowrite ? AC_XFER_OUT : AC_XFER_IN));
    156 	if (rv != 0) {
    157 		amr_ccb_free(amr, ac);
    158 		return (rv);
    159 	}
    160 
    161 	if (bp == NULL) {
    162 		/*
    163 		 * Polled commands must not sit on the software queue.  Wait
    164 		 * up to 30 seconds for the command to complete.
    165 		 */
    166 		s = splbio();
    167 		rv = amr_ccb_poll(amr, ac, 30000);
    168 		splx(s);
    169 		amr_ccb_unmap(amr, ac);
    170 		amr_ccb_free(amr, ac);
    171 	} else {
    172 		ac->ac_handler = ld_amr_handler;
    173 		ac->ac_context = bp;
    174 		ac->ac_dv = (struct device *)sc;
    175 		amr_ccb_enqueue(amr, ac);
    176 		rv = 0;
    177 	}
    178 
    179 	return (rv);
    180 }
    181 
    182 static int
    183 ld_amr_start(struct ld_softc *ld, struct buf *bp)
    184 {
    185 
    186 	return (ld_amr_dobio((struct ld_amr_softc *)ld, bp->b_data,
    187 	    bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp));
    188 }
    189 
    190 static void
    191 ld_amr_handler(struct amr_ccb *ac)
    192 {
    193 	struct buf *bp;
    194 	struct ld_amr_softc *sc;
    195 	struct amr_softc *amr;
    196 
    197 	bp = ac->ac_context;
    198 	sc = (struct ld_amr_softc *)ac->ac_dv;
    199 	amr = (struct amr_softc *)device_parent(&sc->sc_ld.sc_dv);
    200 
    201 	if (ac->ac_status != AMR_STATUS_SUCCESS) {
    202 		printf("%s: cmd status 0x%02x\n", sc->sc_ld.sc_dv.dv_xname,
    203 		    ac->ac_status);
    204 
    205 		bp->b_flags |= B_ERROR;
    206 		bp->b_error = EIO;
    207 		bp->b_resid = bp->b_bcount;
    208 	} else
    209 		bp->b_resid = 0;
    210 
    211 	amr_ccb_unmap(amr, ac);
    212 	amr_ccb_free(amr, ac);
    213 	lddone(&sc->sc_ld, bp);
    214 }
    215 
    216 static int
    217 ld_amr_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
    218 {
    219 	struct ld_amr_softc *sc;
    220 
    221 	sc = (struct ld_amr_softc *)ld;
    222 
    223 	return (ld_amr_dobio(sc, data, blkcnt * ld->sc_secsize, blkno, 1,
    224 	    NULL));
    225 }
    226