Home | History | Annotate | Line # | Download | only in pci
      1 /*	$NetBSD: ld_twe.c,v 1.41 2025/04/13 02:34:03 rin Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000, 2001, 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; and by Jason R. Thorpe of Wasabi Systems, Inc.
      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 /*
     33  * 3ware "Escalade" RAID controller front-end for ld(4) driver.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: ld_twe.c,v 1.41 2025/04/13 02:34:03 rin Exp $");
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/kernel.h>
     42 #include <sys/device.h>
     43 #include <sys/buf.h>
     44 #include <sys/bufq.h>
     45 #include <sys/endian.h>
     46 #include <sys/dkio.h>
     47 #include <sys/disk.h>
     48 #include <sys/proc.h>
     49 #include <sys/module.h>
     50 #include <sys/bus.h>
     51 
     52 #include <dev/ldvar.h>
     53 
     54 #include <dev/pci/twereg.h>
     55 #include <dev/pci/twevar.h>
     56 
     57 #include "ioconf.h"
     58 
     59 struct ld_twe_softc {
     60 	struct	ld_softc sc_ld;
     61 	int	sc_hwunit;
     62 };
     63 
     64 static void	ld_twe_attach(device_t, device_t, void *);
     65 static int	ld_twe_detach(device_t, int);
     66 static int	ld_twe_dobio(struct ld_twe_softc *, void *, int, int, int,
     67 			     struct buf *);
     68 static int	ld_twe_dump(struct ld_softc *, void *, daddr_t, int);
     69 static int	ld_twe_flush(struct ld_softc *, bool);
     70 static int	ld_twe_ioctl(struct ld_softc *, u_long, void *, int32_t, bool);
     71 static void	ld_twe_handler(struct twe_ccb *, int);
     72 static int	ld_twe_match(device_t, cfdata_t, void *);
     73 static int	ld_twe_start(struct ld_softc *, struct buf *);
     74 
     75 static void	ld_twe_adjqparam(device_t, int);
     76 
     77 CFATTACH_DECL_NEW(ld_twe, sizeof(struct ld_twe_softc),
     78     ld_twe_match, ld_twe_attach, ld_twe_detach, NULL);
     79 
     80 static const struct twe_callbacks ld_twe_callbacks = {
     81 	ld_twe_adjqparam,
     82 };
     83 
     84 static int
     85 ld_twe_match(device_t parent, cfdata_t match, void *aux)
     86 {
     87 
     88 	return (1);
     89 }
     90 
     91 static void
     92 ld_twe_attach(device_t parent, device_t self, void *aux)
     93 {
     94 	struct twe_attach_args *twea = aux;
     95 	struct ld_twe_softc *sc = device_private(self);
     96 	struct ld_softc *ld = &sc->sc_ld;
     97 	struct twe_softc *twe = device_private(parent);
     98 	struct twe_drive *td = &twe->sc_units[twea->twea_unit];
     99 	const char *typestr, *stripestr, *statstr;
    100 	char unktype[16], stripebuf[32], unkstat[32];
    101 	int error;
    102 	uint8_t status;
    103 
    104 	ld->sc_dv = self;
    105 
    106 	twe_register_callbacks(twe, twea->twea_unit, &ld_twe_callbacks);
    107 
    108 	sc->sc_hwunit = twea->twea_unit;
    109 	ld->sc_flags = LDF_ENABLED;
    110 	ld->sc_maxxfer = twe_get_maxxfer(twe_get_maxsegs());
    111 	ld->sc_secperunit = td->td_size;
    112 	ld->sc_secsize = TWE_SECTOR_SIZE;
    113 	ld->sc_maxqueuecnt = twe->sc_openings;
    114 	ld->sc_start = ld_twe_start;
    115 	ld->sc_dump = ld_twe_dump;
    116 	ld->sc_ioctl = ld_twe_ioctl;
    117 
    118 	typestr = twe_describe_code(twe_table_unittype, td->td_type);
    119 	if (typestr == NULL) {
    120 		snprintf(unktype, sizeof(unktype), "<0x%02x>", td->td_type);
    121 		typestr = unktype;
    122 	}
    123 	switch (td->td_type) {
    124 	case TWE_AD_CONFIG_RAID0:
    125 	case TWE_AD_CONFIG_RAID5:
    126 	case TWE_AD_CONFIG_RAID10:
    127 		stripestr = twe_describe_code(twe_table_stripedepth,
    128 		    td->td_stripe);
    129 		if (stripestr == NULL)
    130 			snprintf(stripebuf, sizeof(stripebuf),
    131 			    "<stripe code 0x%02x> ", td->td_stripe);
    132 		else
    133 			snprintf(stripebuf, sizeof(stripebuf), "%s stripe ",
    134 			    stripestr);
    135 		break;
    136 	default:
    137 		stripebuf[0] = '\0';
    138 	}
    139 
    140 	error = twe_param_get_1(twe, TWE_PARAM_UNITINFO + twea->twea_unit,
    141 	    TWE_PARAM_UNITINFO_Status, &status);
    142 	status &= TWE_PARAM_UNITSTATUS_MASK;
    143 	if (error) {
    144 		snprintf(unkstat, sizeof(unkstat), "<unknown>");
    145 		statstr = unkstat;
    146 	} else if ((statstr =
    147 		    twe_describe_code(twe_table_unitstate, status)) == NULL) {
    148 		snprintf(unkstat, sizeof(unkstat), "<status code 0x%02x>",
    149 		    status);
    150 		statstr = unkstat;
    151 	}
    152 
    153 	aprint_normal(": %s%s, status: %s\n", stripebuf, typestr, statstr);
    154 	ldattach(ld, BUFQ_DISK_DEFAULT_STRAT);
    155 }
    156 
    157 static int
    158 ld_twe_detach(device_t self, int flags)
    159 {
    160 	struct ld_twe_softc *sc = device_private(self);
    161 	struct ld_softc *ld = &sc->sc_ld;
    162 	int rv;
    163 
    164 	if ((rv = ldbegindetach(ld, flags)) != 0)
    165 		return (rv);
    166 	ldenddetach(ld);
    167 
    168 	return (0);
    169 }
    170 
    171 static int
    172 ld_twe_dobio(struct ld_twe_softc *sc, void *data, int datasize, int blkno,
    173 	     int dowrite, struct buf *bp)
    174 {
    175 	struct twe_ccb *ccb;
    176 	struct twe_cmd *tc;
    177 	struct twe_softc *twe;
    178 	int s, rv, flags;
    179 
    180 	twe = device_private(device_parent(sc->sc_ld.sc_dv));
    181 
    182 	flags = (dowrite ? TWE_CCB_DATA_OUT : TWE_CCB_DATA_IN);
    183 	if ((ccb = twe_ccb_alloc(twe, flags)) == NULL)
    184 		return (EAGAIN);
    185 
    186 	ccb->ccb_data = data;
    187 	ccb->ccb_datasize = datasize;
    188 	tc = ccb->ccb_cmd;
    189 
    190 	/* Build the command. */
    191 	tc->tc_size = 3;
    192 	tc->tc_unit = sc->sc_hwunit;
    193 	tc->tc_count = htole16(datasize / TWE_SECTOR_SIZE);
    194 	tc->tc_args.io.lba = htole32(blkno);
    195 
    196 	if (dowrite)
    197 		tc->tc_opcode = TWE_OP_WRITE | (tc->tc_size << 5);
    198 	else
    199 		tc->tc_opcode = TWE_OP_READ | (tc->tc_size << 5);
    200 
    201 	/* Map the data transfer. */
    202 	if ((rv = twe_ccb_map(twe, ccb)) != 0) {
    203 		twe_ccb_free(twe, ccb);
    204 		return (rv);
    205 	}
    206 
    207 	if (bp == NULL) {
    208 		/*
    209 		 * Polled commands must not sit on the software queue.  Wait
    210 		 * up to 2 seconds for the command to complete.
    211 		 */
    212 		s = splbio();
    213 		rv = twe_ccb_poll(twe, ccb, 2000);
    214 		twe_ccb_unmap(twe, ccb);
    215 		twe_ccb_free(twe, ccb);
    216 		splx(s);
    217 	} else {
    218 		ccb->ccb_tx.tx_handler = ld_twe_handler;
    219 		ccb->ccb_tx.tx_context = bp;
    220 		ccb->ccb_tx.tx_dv = sc->sc_ld.sc_dv;
    221 		twe_ccb_enqueue(twe, ccb);
    222 		rv = 0;
    223 	}
    224 
    225 	return (rv);
    226 }
    227 
    228 static int
    229 ld_twe_start(struct ld_softc *ld, struct buf *bp)
    230 {
    231 
    232 	return (ld_twe_dobio((struct ld_twe_softc *)ld, bp->b_data,
    233 	    bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp));
    234 }
    235 
    236 static void
    237 ld_twe_handler(struct twe_ccb *ccb, int error)
    238 {
    239 	struct buf *bp;
    240 	struct twe_context *tx;
    241 	struct ld_twe_softc *sc;
    242 	struct twe_softc *twe;
    243 
    244 	tx = &ccb->ccb_tx;
    245 	bp = tx->tx_context;
    246 	sc = device_private(tx->tx_dv);
    247 	twe = device_private(device_parent(sc->sc_ld.sc_dv));
    248 
    249 	twe_ccb_unmap(twe, ccb);
    250 	twe_ccb_free(twe, ccb);
    251 
    252 	if (error) {
    253 		bp->b_error = error;
    254 		bp->b_resid = bp->b_bcount;
    255 	} else
    256 		bp->b_resid = 0;
    257 
    258 	lddone(&sc->sc_ld, bp);
    259 }
    260 
    261 static int
    262 ld_twe_dump(struct ld_softc *ld, void *data, daddr_t blkno, int blkcnt)
    263 {
    264 
    265 	/* ld_twe_dobio() takes only an 'int' as a disk address */
    266 	if (blkno + blkcnt - 1 > INT_MAX)
    267 		return (EIO);
    268 
    269 	return (ld_twe_dobio((struct ld_twe_softc *)ld, data,
    270 	    blkcnt * ld->sc_secsize, blkno, 1, NULL));
    271 }
    272 
    273 static int
    274 ld_twe_flush(struct ld_softc *ld, bool poll)
    275 {
    276 	struct ld_twe_softc *sc = (void *) ld;
    277 	struct twe_softc *twe = device_private(device_parent(ld->sc_dv));
    278 	struct twe_ccb *ccb;
    279 	struct twe_cmd *tc;
    280 	int s, rv;
    281 
    282 	ccb = twe_ccb_alloc_wait(twe, 0);
    283 	KASSERT(ccb != NULL);
    284 
    285 	ccb->ccb_data = NULL;
    286 	ccb->ccb_datasize = 0;
    287 
    288 	tc = ccb->ccb_cmd;
    289 	tc->tc_size = 2;
    290 	tc->tc_opcode = TWE_OP_FLUSH;
    291 	tc->tc_unit = sc->sc_hwunit;
    292 	tc->tc_count = 0;
    293 
    294 	if (poll) {
    295 		/*
    296 		 * Polled commands must not sit on the software queue.  Wait
    297 		 * up to 2 seconds for the command to complete.
    298 		 */
    299 		s = splbio();
    300 		rv = twe_ccb_poll(twe, ccb, 2000);
    301 		twe_ccb_unmap(twe, ccb);
    302 		twe_ccb_free(twe, ccb);
    303 		splx(s);
    304 	} else {
    305 		ccb->ccb_tx.tx_handler = twe_ccb_wait_handler;
    306 		ccb->ccb_tx.tx_context = NULL;
    307 		ccb->ccb_tx.tx_dv = ld->sc_dv;
    308 		twe_ccb_enqueue(twe, ccb);
    309 
    310 		rv = 0;
    311 		s = splbio();
    312 		while ((ccb->ccb_flags & TWE_CCB_COMPLETE) == 0)
    313 			if ((rv = tsleep(ccb, PRIBIO, "tweflush",
    314 			    60 * hz)) != 0)
    315 				break;
    316 		twe_ccb_free(twe, ccb);
    317 		splx(s);
    318 	}
    319 
    320 	return (rv);
    321 }
    322 
    323 static int
    324 ld_twe_ioctl(struct ld_softc *ld, u_long cmd, void *addr, int32_t flag, bool poll)
    325 {
    326         int error;
    327 
    328         switch (cmd) {
    329         case DIOCCACHESYNC:
    330 		error = ld_twe_flush(ld, poll);
    331 		break;
    332 
    333 	default:
    334 		error = EPASSTHROUGH;
    335 		break;
    336 	}
    337 
    338 	return error;
    339 }
    340 
    341 static void
    342 ld_twe_adjqparam(device_t self, int openings)
    343 {
    344 	struct ld_twe_softc *sc = device_private(self);
    345 	struct ld_softc *ld = &sc->sc_ld;
    346 
    347 	ldadjqparam(ld, openings);
    348 }
    349 
    350 MODULE(MODULE_CLASS_DRIVER, ld_twe, "ld,twe");
    351 
    352 #ifdef _MODULE
    353 /*
    354  * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd"
    355  * XXX it will be defined in the common-code module
    356  */
    357 #undef  CFDRIVER_DECL
    358 #define CFDRIVER_DECL(name, class, attr)
    359 #include "ioconf.c"
    360 #endif
    361 
    362 static int
    363 ld_twe_modcmd(modcmd_t cmd, void *opaque)
    364 {
    365 #ifdef _MODULE
    366 	/*
    367 	 * We ignore the cfdriver_vec[] that ioconf provides, since
    368 	 * the cfdrivers are attached already.
    369 	 */
    370 	static struct cfdriver * const no_cfdriver_vec[] = { NULL };
    371 #endif
    372 	int error = 0;
    373 
    374 #ifdef _MODULE
    375 	switch (cmd) {
    376 	case MODULE_CMD_INIT:
    377 		error = config_init_component(no_cfdriver_vec,
    378 		    cfattach_ioconf_ld_twe, cfdata_ioconf_ld_twe);
    379 		break;
    380 	case MODULE_CMD_FINI:
    381 		error = config_fini_component(no_cfdriver_vec,
    382 		    cfattach_ioconf_ld_twe, cfdata_ioconf_ld_twe);
    383 		break;
    384 	default:
    385 		error = ENOTTY;
    386 		break;
    387 	}
    388 #endif
    389 
    390 	return error;
    391 }
    392