Home | History | Annotate | Line # | Download | only in pci
twe.c revision 1.103.4.2
      1  1.103.4.2     skrll /*	$NetBSD: twe.c,v 1.103.4.2 2016/10/05 20:55:55 skrll Exp $	*/
      2        1.1        ad 
      3        1.1        ad /*-
      4       1.55   thorpej  * Copyright (c) 2000, 2001, 2002, 2003, 2004 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.42   thorpej  * by Andrew Doran; and by Jason R. Thorpe of Wasabi Systems, Inc.
      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  *
     19        1.1        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20        1.1        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.1        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.1        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23        1.1        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.1        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.1        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.1        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.1        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.1        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.1        ad  * POSSIBILITY OF SUCH DAMAGE.
     30        1.1        ad  */
     31        1.1        ad 
     32        1.1        ad /*-
     33        1.1        ad  * Copyright (c) 2000 Michael Smith
     34        1.1        ad  * Copyright (c) 2000 BSDi
     35        1.1        ad  * All rights reserved.
     36        1.1        ad  *
     37        1.1        ad  * Redistribution and use in source and binary forms, with or without
     38        1.1        ad  * modification, are permitted provided that the following conditions
     39        1.1        ad  * are met:
     40        1.1        ad  * 1. Redistributions of source code must retain the above copyright
     41        1.1        ad  *    notice, this list of conditions and the following disclaimer.
     42        1.1        ad  * 2. Redistributions in binary form must reproduce the above copyright
     43        1.1        ad  *    notice, this list of conditions and the following disclaimer in the
     44        1.1        ad  *    documentation and/or other materials provided with the distribution.
     45        1.1        ad  *
     46        1.1        ad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     47        1.1        ad  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     48        1.1        ad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     49        1.1        ad  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     50        1.1        ad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     51        1.1        ad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     52        1.1        ad  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     53        1.1        ad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     54        1.1        ad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     55        1.1        ad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     56        1.1        ad  * SUCH DAMAGE.
     57        1.1        ad  *
     58        1.1        ad  * from FreeBSD: twe.c,v 1.1 2000/05/24 23:35:23 msmith Exp
     59        1.1        ad  */
     60        1.1        ad 
     61        1.1        ad /*
     62        1.1        ad  * Driver for the 3ware Escalade family of RAID controllers.
     63        1.1        ad  */
     64       1.21     lukem 
     65       1.21     lukem #include <sys/cdefs.h>
     66  1.103.4.2     skrll __KERNEL_RCSID(0, "$NetBSD: twe.c,v 1.103.4.2 2016/10/05 20:55:55 skrll Exp $");
     67        1.1        ad 
     68        1.1        ad #include <sys/param.h>
     69        1.1        ad #include <sys/systm.h>
     70        1.1        ad #include <sys/kernel.h>
     71        1.1        ad #include <sys/device.h>
     72        1.1        ad #include <sys/queue.h>
     73        1.1        ad #include <sys/proc.h>
     74        1.1        ad #include <sys/buf.h>
     75        1.1        ad #include <sys/endian.h>
     76        1.1        ad #include <sys/malloc.h>
     77       1.33  christos #include <sys/conf.h>
     78        1.1        ad #include <sys/disk.h>
     79       1.62      heas #include <sys/sysctl.h>
     80       1.55   thorpej #include <sys/syslog.h>
     81       1.80      elad #include <sys/kauth.h>
     82  1.103.4.2     skrll #include <sys/module.h>
     83       1.73       dsl #include <sys/bswap.h>
     84       1.84        ad #include <sys/bus.h>
     85        1.1        ad 
     86        1.1        ad #include <dev/pci/pcireg.h>
     87        1.1        ad #include <dev/pci/pcivar.h>
     88        1.1        ad #include <dev/pci/pcidevs.h>
     89        1.1        ad #include <dev/pci/twereg.h>
     90        1.1        ad #include <dev/pci/twevar.h>
     91       1.33  christos #include <dev/pci/tweio.h>
     92        1.1        ad 
     93       1.59  drochner #include "locators.h"
     94  1.103.4.2     skrll #include "ioconf.h"
     95       1.59  drochner 
     96        1.1        ad #define	PCI_CBIO	0x10
     97        1.1        ad 
     98       1.47   thorpej static int	twe_aen_get(struct twe_softc *, uint16_t *);
     99        1.1        ad static void	twe_aen_handler(struct twe_ccb *, int);
    100       1.45   thorpej static void	twe_aen_enqueue(struct twe_softc *sc, uint16_t, int);
    101       1.45   thorpej static uint16_t	twe_aen_dequeue(struct twe_softc *);
    102       1.45   thorpej 
    103       1.89    cegger static void	twe_attach(device_t, device_t, void *);
    104  1.103.4.2     skrll static int	twe_rescan(device_t, const char *, const int *);
    105        1.1        ad static int	twe_init_connection(struct twe_softc *);
    106        1.1        ad static int	twe_intr(void *);
    107       1.89    cegger static int	twe_match(device_t, cfdata_t, void *);
    108       1.33  christos static int	twe_param_set(struct twe_softc *, int, int, size_t, void *);
    109        1.1        ad static void	twe_poll(struct twe_softc *);
    110        1.1        ad static int	twe_print(void *, const char *);
    111        1.1        ad static int	twe_reset(struct twe_softc *);
    112        1.1        ad static int	twe_status_check(struct twe_softc *, u_int);
    113        1.1        ad static int	twe_status_wait(struct twe_softc *, u_int, int);
    114       1.38  jdolecek static void	twe_describe_controller(struct twe_softc *);
    115  1.103.4.2     skrll static void	twe_clear_pci_abort(struct twe_softc *sc);
    116  1.103.4.2     skrll static void	twe_clear_pci_parity_error(struct twe_softc *sc);
    117        1.1        ad 
    118       1.46   thorpej static int	twe_add_unit(struct twe_softc *, int);
    119       1.46   thorpej static int	twe_del_unit(struct twe_softc *, int);
    120       1.78  christos static int	twe_init_connection(struct twe_softc *);
    121       1.46   thorpej 
    122       1.22        ad static inline u_int32_t	twe_inl(struct twe_softc *, int);
    123       1.33  christos static inline void twe_outl(struct twe_softc *, int, u_int32_t);
    124       1.33  christos 
    125       1.63     perry extern struct	cfdriver twe_cd;
    126       1.22        ad 
    127  1.103.4.2     skrll CFATTACH_DECL3_NEW(twe, sizeof(struct twe_softc),
    128  1.103.4.2     skrll     twe_match, twe_attach, NULL, NULL, twe_rescan, NULL, 0);
    129        1.1        ad 
    130       1.62      heas /* FreeBSD driver revision for sysctl expected by the 3ware cli */
    131       1.62      heas const char twever[] = "1.50.01.002";
    132       1.62      heas 
    133       1.40   thorpej /*
    134       1.40   thorpej  * Tables to convert numeric codes to strings.
    135       1.40   thorpej  */
    136       1.40   thorpej const struct twe_code_table twe_table_status[] = {
    137       1.40   thorpej 	{ 0x00,	"successful completion" },
    138       1.40   thorpej 
    139       1.40   thorpej 	/* info */
    140       1.40   thorpej 	{ 0x42,	"command in progress" },
    141       1.40   thorpej 	{ 0x6c,	"retrying interface CRC error from UDMA command" },
    142       1.40   thorpej 
    143       1.40   thorpej 	/* warning */
    144       1.40   thorpej 	{ 0x81,	"redundant/inconsequential request ignored" },
    145       1.40   thorpej 	{ 0x8e,	"failed to write zeroes to LBA 0" },
    146       1.40   thorpej 	{ 0x8f,	"failed to profile TwinStor zones" },
    147       1.40   thorpej 
    148       1.40   thorpej 	/* fatal */
    149       1.40   thorpej 	{ 0xc1,	"aborted due to system command or reconfiguration" },
    150       1.40   thorpej 	{ 0xc4,	"aborted" },
    151       1.40   thorpej 	{ 0xc5,	"access error" },
    152       1.40   thorpej 	{ 0xc6,	"access violation" },
    153       1.40   thorpej 	{ 0xc7,	"device failure" },	/* high byte may be port # */
    154       1.40   thorpej 	{ 0xc8,	"controller error" },
    155       1.40   thorpej 	{ 0xc9,	"timed out" },
    156       1.40   thorpej 	{ 0xcb,	"invalid unit number" },
    157       1.40   thorpej 	{ 0xcf,	"unit not available" },
    158       1.40   thorpej 	{ 0xd2,	"undefined opcode" },
    159       1.40   thorpej 	{ 0xdb,	"request incompatible with unit" },
    160       1.40   thorpej 	{ 0xdc,	"invalid request" },
    161       1.40   thorpej 	{ 0xff,	"firmware error, reset requested" },
    162       1.40   thorpej 
    163       1.40   thorpej 	{ 0,	NULL }
    164       1.40   thorpej };
    165       1.40   thorpej 
    166       1.40   thorpej const struct twe_code_table twe_table_unitstate[] = {
    167       1.40   thorpej 	{ TWE_PARAM_UNITSTATUS_Normal,		"Normal" },
    168       1.40   thorpej 	{ TWE_PARAM_UNITSTATUS_Initialising,	"Initializing" },
    169       1.40   thorpej 	{ TWE_PARAM_UNITSTATUS_Degraded,	"Degraded" },
    170       1.40   thorpej 	{ TWE_PARAM_UNITSTATUS_Rebuilding,	"Rebuilding" },
    171       1.40   thorpej 	{ TWE_PARAM_UNITSTATUS_Verifying,	"Verifying" },
    172       1.40   thorpej 	{ TWE_PARAM_UNITSTATUS_Corrupt,		"Corrupt" },
    173       1.40   thorpej 	{ TWE_PARAM_UNITSTATUS_Missing,		"Missing" },
    174       1.40   thorpej 
    175       1.40   thorpej 	{ 0,					NULL }
    176       1.40   thorpej };
    177       1.40   thorpej 
    178       1.40   thorpej const struct twe_code_table twe_table_unittype[] = {
    179       1.40   thorpej 	/* array descriptor configuration */
    180       1.40   thorpej 	{ TWE_AD_CONFIG_RAID0,			"RAID0" },
    181       1.40   thorpej 	{ TWE_AD_CONFIG_RAID1,			"RAID1" },
    182       1.40   thorpej 	{ TWE_AD_CONFIG_TwinStor,		"TwinStor" },
    183       1.40   thorpej 	{ TWE_AD_CONFIG_RAID5,			"RAID5" },
    184       1.40   thorpej 	{ TWE_AD_CONFIG_RAID10,			"RAID10" },
    185       1.61     lukem 	{ TWE_UD_CONFIG_JBOD,			"JBOD" },
    186       1.40   thorpej 
    187       1.40   thorpej 	{ 0,					NULL }
    188       1.40   thorpej };
    189       1.40   thorpej 
    190       1.40   thorpej const struct twe_code_table twe_table_stripedepth[] = {
    191       1.40   thorpej 	{ TWE_AD_STRIPE_4k,			"4K" },
    192       1.40   thorpej 	{ TWE_AD_STRIPE_8k,			"8K" },
    193       1.40   thorpej 	{ TWE_AD_STRIPE_16k,			"16K" },
    194       1.40   thorpej 	{ TWE_AD_STRIPE_32k,			"32K" },
    195       1.40   thorpej 	{ TWE_AD_STRIPE_64k,			"64K" },
    196       1.61     lukem 	{ TWE_AD_STRIPE_128k,			"128K" },
    197       1.61     lukem 	{ TWE_AD_STRIPE_256k,			"256K" },
    198       1.61     lukem 	{ TWE_AD_STRIPE_512k,			"512K" },
    199       1.61     lukem 	{ TWE_AD_STRIPE_1024k,			"1024K" },
    200       1.40   thorpej 
    201       1.40   thorpej 	{ 0,					NULL }
    202       1.40   thorpej };
    203       1.40   thorpej 
    204       1.45   thorpej /*
    205       1.45   thorpej  * Asynchronous event notification messages are qualified:
    206       1.45   thorpej  *	a - not unit/port specific
    207       1.45   thorpej  *	u - unit specific
    208       1.45   thorpej  *	p - port specific
    209       1.55   thorpej  *
    210       1.55   thorpej  * They are further qualified with a severity:
    211       1.55   thorpej  *	E - LOG_EMERG
    212       1.55   thorpej  *	a - LOG_ALERT
    213       1.55   thorpej  *	c - LOG_CRIT
    214       1.55   thorpej  *	e - LOG_ERR
    215       1.55   thorpej  *	w - LOG_WARNING
    216       1.55   thorpej  *	n - LOG_NOTICE
    217       1.55   thorpej  *	i - LOG_INFO
    218       1.55   thorpej  *	d - LOG_DEBUG
    219       1.55   thorpej  *	blank - just use printf
    220       1.45   thorpej  */
    221       1.45   thorpej const struct twe_code_table twe_table_aen[] = {
    222       1.55   thorpej 	{ 0x00,	"a  queue empty" },
    223       1.55   thorpej 	{ 0x01,	"a  soft reset" },
    224       1.55   thorpej 	{ 0x02,	"uc degraded mode" },
    225       1.55   thorpej 	{ 0x03,	"aa controller error" },
    226       1.55   thorpej 	{ 0x04,	"uE rebuild fail" },
    227       1.55   thorpej 	{ 0x05,	"un rebuild done" },
    228       1.55   thorpej 	{ 0x06,	"ue incomplete unit" },
    229       1.55   thorpej 	{ 0x07,	"un initialization done" },
    230       1.55   thorpej 	{ 0x08,	"uw unclean shutdown detected" },
    231       1.55   thorpej 	{ 0x09,	"pe drive timeout" },
    232       1.55   thorpej 	{ 0x0a,	"pc drive error" },
    233       1.55   thorpej 	{ 0x0b,	"un rebuild started" },
    234       1.55   thorpej 	{ 0x0c,	"un initialization started" },
    235       1.55   thorpej 	{ 0x0d,	"ui logical unit deleted" },
    236       1.55   thorpej 	{ 0x0f,	"pc SMART threshold exceeded" },
    237       1.55   thorpej 	{ 0x15,	"a  table undefined" },	/* XXX: Not in FreeBSD's table */
    238       1.55   thorpej 	{ 0x21,	"pe ATA UDMA downgrade" },
    239       1.55   thorpej 	{ 0x22,	"pi ATA UDMA upgrade" },
    240       1.55   thorpej 	{ 0x23,	"pw sector repair occurred" },
    241       1.55   thorpej 	{ 0x24,	"aa SBUF integrity check failure" },
    242       1.55   thorpej 	{ 0x25,	"pa lost cached write" },
    243       1.55   thorpej 	{ 0x26,	"pa drive ECC error detected" },
    244       1.55   thorpej 	{ 0x27,	"pe DCB checksum error" },
    245       1.55   thorpej 	{ 0x28,	"pn DCB unsupported version" },
    246       1.55   thorpej 	{ 0x29,	"ui verify started" },
    247       1.55   thorpej 	{ 0x2a,	"ua verify failed" },
    248       1.55   thorpej 	{ 0x2b,	"ui verify complete" },
    249       1.55   thorpej 	{ 0x2c,	"pw overwrote bad sector during rebuild" },
    250       1.55   thorpej 	{ 0x2d,	"pa encountered bad sector during rebuild" },
    251       1.55   thorpej 	{ 0x2e,	"pe replacement drive too small" },
    252       1.55   thorpej 	{ 0x2f,	"ue array not previously initialized" },
    253       1.55   thorpej 	{ 0x30,	"p  drive not supported" },
    254       1.55   thorpej 	{ 0xff,	"a  aen queue full" },
    255       1.45   thorpej 
    256       1.45   thorpej 	{ 0,	NULL },
    257       1.45   thorpej };
    258       1.45   thorpej 
    259       1.40   thorpej const char *
    260       1.40   thorpej twe_describe_code(const struct twe_code_table *table, uint32_t code)
    261       1.40   thorpej {
    262       1.40   thorpej 
    263       1.40   thorpej 	for (; table->string != NULL; table++) {
    264       1.40   thorpej 		if (table->code == code)
    265       1.40   thorpej 			return (table->string);
    266       1.40   thorpej 	}
    267       1.40   thorpej 	return (NULL);
    268       1.40   thorpej }
    269       1.40   thorpej 
    270       1.22        ad static inline u_int32_t
    271       1.22        ad twe_inl(struct twe_softc *sc, int off)
    272       1.22        ad {
    273       1.22        ad 
    274       1.22        ad 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, off, 4,
    275       1.22        ad 	    BUS_SPACE_BARRIER_WRITE | BUS_SPACE_BARRIER_READ);
    276       1.22        ad 	return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, off));
    277       1.22        ad }
    278       1.22        ad 
    279       1.22        ad static inline void
    280       1.22        ad twe_outl(struct twe_softc *sc, int off, u_int32_t val)
    281       1.22        ad {
    282       1.22        ad 
    283       1.22        ad 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, off, val);
    284       1.22        ad 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, off, 4,
    285       1.22        ad 	    BUS_SPACE_BARRIER_WRITE);
    286       1.22        ad }
    287       1.22        ad 
    288        1.1        ad /*
    289        1.1        ad  * Match a supported board.
    290        1.1        ad  */
    291        1.1        ad static int
    292       1.89    cegger twe_match(device_t parent, cfdata_t cfdata, void *aux)
    293        1.1        ad {
    294        1.1        ad 	struct pci_attach_args *pa;
    295        1.1        ad 
    296        1.1        ad 	pa = aux;
    297        1.1        ad 
    298       1.63     perry 	return (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_3WARE &&
    299       1.10        ad 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3WARE_ESCALADE ||
    300       1.10        ad 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3WARE_ESCALADE_ASIC));
    301        1.1        ad }
    302        1.1        ad 
    303        1.1        ad /*
    304        1.1        ad  * Attach a supported board.
    305        1.1        ad  *
    306        1.1        ad  * XXX This doesn't fail gracefully.
    307        1.1        ad  */
    308        1.1        ad static void
    309       1.89    cegger twe_attach(device_t parent, device_t self, void *aux)
    310        1.1        ad {
    311        1.1        ad 	struct pci_attach_args *pa;
    312        1.1        ad 	struct twe_softc *sc;
    313        1.1        ad 	pci_chipset_tag_t pc;
    314        1.1        ad 	pci_intr_handle_t ih;
    315        1.1        ad 	pcireg_t csr;
    316        1.1        ad 	const char *intrstr;
    317       1.47   thorpej 	int s, size, i, rv, rseg;
    318       1.23  christos 	size_t max_segs, max_xfer;
    319        1.1        ad 	bus_dma_segment_t seg;
    320       1.93  uebayasi 	const struct sysctlnode *node;
    321        1.1        ad 	struct twe_cmd *tc;
    322        1.1        ad 	struct twe_ccb *ccb;
    323      1.102  christos 	char intrbuf[PCI_INTRSTR_LEN];
    324        1.1        ad 
    325       1.90    cegger 	sc = device_private(self);
    326       1.98       chs 	sc->sc_dev = self;
    327        1.1        ad 	pa = aux;
    328        1.1        ad 	pc = pa->pa_pc;
    329        1.1        ad 	sc->sc_dmat = pa->pa_dmat;
    330        1.1        ad 	SIMPLEQ_INIT(&sc->sc_ccb_queue);
    331        1.1        ad 	SLIST_INIT(&sc->sc_ccb_freelist);
    332        1.1        ad 
    333       1.40   thorpej 	aprint_naive(": RAID controller\n");
    334       1.38  jdolecek 	aprint_normal(": 3ware Escalade\n");
    335        1.1        ad 
    336       1.33  christos 
    337        1.1        ad 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
    338        1.1        ad 	    &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
    339       1.97       chs 		aprint_error_dev(self, "can't map i/o space\n");
    340        1.1        ad 		return;
    341        1.1        ad 	}
    342        1.1        ad 
    343        1.1        ad 	/* Enable the device. */
    344        1.1        ad 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    345        1.1        ad 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    346        1.1        ad 	    csr | PCI_COMMAND_MASTER_ENABLE);
    347        1.1        ad 
    348        1.1        ad 	/* Map and establish the interrupt. */
    349        1.5  sommerfe 	if (pci_intr_map(pa, &ih)) {
    350       1.97       chs 		aprint_error_dev(self, "can't map interrupt\n");
    351        1.1        ad 		return;
    352        1.1        ad 	}
    353       1.38  jdolecek 
    354      1.102  christos 	intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
    355        1.1        ad 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_BIO, twe_intr, sc);
    356        1.1        ad 	if (sc->sc_ih == NULL) {
    357       1.97       chs 		aprint_error_dev(self, "can't establish interrupt%s%s\n",
    358       1.38  jdolecek 			(intrstr) ? " at " : "",
    359       1.38  jdolecek 			(intrstr) ? intrstr : "");
    360        1.1        ad 		return;
    361        1.1        ad 	}
    362       1.38  jdolecek 
    363        1.1        ad 	if (intrstr != NULL)
    364  1.103.4.1     skrll 		aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    365        1.1        ad 
    366        1.1        ad 	/*
    367        1.1        ad 	 * Allocate and initialise the command blocks and CCBs.
    368        1.1        ad 	 */
    369       1.93  uebayasi 	size = sizeof(struct twe_cmd) * TWE_MAX_QUEUECNT;
    370        1.1        ad 
    371       1.63     perry 	if ((rv = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &seg, 1,
    372        1.1        ad 	    &rseg, BUS_DMA_NOWAIT)) != 0) {
    373  1.103.4.2     skrll 		aprint_error_dev(self,
    374  1.103.4.2     skrll 		    "unable to allocate commands, rv = %d\n", rv);
    375        1.1        ad 		return;
    376        1.1        ad 	}
    377        1.1        ad 
    378       1.63     perry 	if ((rv = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
    379       1.83  christos 	    (void **)&sc->sc_cmds,
    380        1.1        ad 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    381  1.103.4.2     skrll 		aprint_error_dev(self,
    382  1.103.4.2     skrll 		    "unable to map commands, rv = %d\n", rv);
    383        1.1        ad 		return;
    384        1.1        ad 	}
    385        1.1        ad 
    386       1.63     perry 	if ((rv = bus_dmamap_create(sc->sc_dmat, size, size, 1, 0,
    387        1.1        ad 	    BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
    388  1.103.4.2     skrll 		aprint_error_dev(self,
    389  1.103.4.2     skrll 		    "unable to create command DMA map, rv = %d\n", rv);
    390        1.1        ad 		return;
    391        1.1        ad 	}
    392        1.1        ad 
    393       1.63     perry 	if ((rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap, sc->sc_cmds,
    394        1.1        ad 	    size, NULL, BUS_DMA_NOWAIT)) != 0) {
    395  1.103.4.2     skrll 		aprint_error_dev(self,
    396  1.103.4.2     skrll 		    "unable to load command DMA map, rv = %d\n", rv);
    397        1.1        ad 		return;
    398        1.1        ad 	}
    399        1.1        ad 
    400       1.75    rpaulo 	ccb = malloc(sizeof(*ccb) * TWE_MAX_QUEUECNT, M_DEVBUF, M_NOWAIT);
    401       1.75    rpaulo 	if (ccb == NULL) {
    402       1.97       chs 		aprint_error_dev(self, "unable to allocate memory for ccbs\n");
    403       1.75    rpaulo 		return;
    404       1.75    rpaulo 	}
    405       1.75    rpaulo 
    406        1.1        ad 	sc->sc_cmds_paddr = sc->sc_dmamap->dm_segs[0].ds_addr;
    407        1.1        ad 	memset(sc->sc_cmds, 0, size);
    408        1.1        ad 
    409        1.1        ad 	sc->sc_ccbs = ccb;
    410        1.1        ad 	tc = (struct twe_cmd *)sc->sc_cmds;
    411       1.24  christos 	max_segs = twe_get_maxsegs();
    412       1.24  christos 	max_xfer = twe_get_maxxfer(max_segs);
    413        1.1        ad 
    414        1.7        ad 	for (i = 0; i < TWE_MAX_QUEUECNT; i++, tc++, ccb++) {
    415        1.1        ad 		ccb->ccb_cmd = tc;
    416        1.1        ad 		ccb->ccb_cmdid = i;
    417        1.1        ad 		ccb->ccb_flags = 0;
    418       1.23  christos 		rv = bus_dmamap_create(sc->sc_dmat, max_xfer,
    419       1.23  christos 		    max_segs, PAGE_SIZE, 0,
    420        1.4   thorpej 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    421        1.1        ad 		    &ccb->ccb_dmamap_xfer);
    422        1.7        ad 		if (rv != 0) {
    423  1.103.4.2     skrll 			aprint_error_dev(self,
    424  1.103.4.2     skrll 			    "can't create dmamap, rv = %d\n", rv);
    425        1.7        ad 			return;
    426        1.7        ad 		}
    427       1.47   thorpej 
    428       1.47   thorpej 		/* Save the first CCB for AEN retrieval. */
    429        1.3        ad 		if (i != 0)
    430        1.3        ad 			SLIST_INSERT_HEAD(&sc->sc_ccb_freelist, ccb,
    431        1.3        ad 			    ccb_chain.slist);
    432        1.3        ad 	}
    433        1.1        ad 
    434        1.1        ad 	/* Wait for the controller to become ready. */
    435        1.1        ad 	if (twe_status_wait(sc, TWE_STS_MICROCONTROLLER_READY, 6)) {
    436       1.97       chs 		aprint_error_dev(self, "microcontroller not ready\n");
    437        1.1        ad 		return;
    438        1.1        ad 	}
    439        1.1        ad 
    440       1.22        ad 	twe_outl(sc, TWE_REG_CTL, TWE_CTL_DISABLE_INTRS);
    441        1.1        ad 
    442        1.1        ad 	/* Reset the controller. */
    443       1.47   thorpej 	s = splbio();
    444       1.47   thorpej 	rv = twe_reset(sc);
    445       1.47   thorpej 	splx(s);
    446       1.47   thorpej 	if (rv) {
    447       1.97       chs 		aprint_error_dev(self, "reset failed\n");
    448        1.1        ad 		return;
    449        1.1        ad 	}
    450        1.1        ad 
    451       1.46   thorpej 	/* Initialise connection with controller. */
    452       1.46   thorpej 	twe_init_connection(sc);
    453       1.46   thorpej 
    454       1.46   thorpej 	twe_describe_controller(sc);
    455       1.46   thorpej 
    456       1.46   thorpej 	/* Find and attach RAID array units. */
    457  1.103.4.2     skrll 	twe_rescan(self, "twe", 0);
    458       1.46   thorpej 
    459       1.46   thorpej 	/* ...and finally, enable interrupts. */
    460       1.46   thorpej 	twe_outl(sc, TWE_REG_CTL, TWE_CTL_CLEAR_ATTN_INTR |
    461       1.46   thorpej 	    TWE_CTL_UNMASK_RESP_INTR |
    462       1.46   thorpej 	    TWE_CTL_ENABLE_INTRS);
    463       1.62      heas 
    464       1.62      heas 	/* sysctl set-up for 3ware cli */
    465       1.62      heas 	if (sysctl_createv(NULL, 0, NULL, &node,
    466       1.97       chs 				0, CTLTYPE_NODE, device_xname(self),
    467       1.93  uebayasi 				SYSCTL_DESCR("twe driver information"),
    468       1.93  uebayasi 				NULL, 0, NULL, 0,
    469       1.62      heas 				CTL_HW, CTL_CREATE, CTL_EOL) != 0) {
    470       1.97       chs 		aprint_error_dev(self, "could not create %s.%s sysctl node\n",
    471  1.103.4.2     skrll 		    "hw", device_xname(self));
    472       1.62      heas 		return;
    473       1.62      heas 	}
    474       1.62      heas 	if ((i = sysctl_createv(NULL, 0, NULL, NULL,
    475       1.93  uebayasi 				0, CTLTYPE_STRING, "driver_version",
    476       1.93  uebayasi 				SYSCTL_DESCR("twe0 driver version"),
    477       1.94     joerg 				NULL, 0, __UNCONST(&twever), 0,
    478       1.62      heas 				CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
    479       1.62      heas 				!= 0) {
    480  1.103.4.2     skrll 		aprint_error_dev(self,
    481  1.103.4.2     skrll 		    "could not create %s.%s.driver_version sysctl\n",
    482  1.103.4.2     skrll 		    "hw", device_xname(self));
    483       1.62      heas 		return;
    484       1.62      heas 	}
    485       1.46   thorpej }
    486       1.46   thorpej 
    487  1.103.4.2     skrll static int
    488  1.103.4.2     skrll twe_rescan(device_t self, const char *attr, const int *flags)
    489  1.103.4.2     skrll {
    490  1.103.4.2     skrll 	struct twe_softc *sc;
    491  1.103.4.2     skrll 	int i;
    492  1.103.4.2     skrll 
    493  1.103.4.2     skrll 	sc = device_private(self);
    494  1.103.4.2     skrll 	sc->sc_nunits = 0;
    495  1.103.4.2     skrll 	for (i = 0; i < TWE_MAX_UNITS; i++)
    496  1.103.4.2     skrll 		(void) twe_add_unit(sc, i);
    497  1.103.4.2     skrll 	return 0;
    498  1.103.4.2     skrll }
    499  1.103.4.2     skrll 
    500  1.103.4.2     skrll 
    501       1.46   thorpej void
    502       1.46   thorpej twe_register_callbacks(struct twe_softc *sc, int unit,
    503       1.46   thorpej     const struct twe_callbacks *tcb)
    504       1.46   thorpej {
    505       1.46   thorpej 
    506       1.46   thorpej 	sc->sc_units[unit].td_callbacks = tcb;
    507       1.46   thorpej }
    508       1.46   thorpej 
    509       1.46   thorpej static void
    510       1.46   thorpej twe_recompute_openings(struct twe_softc *sc)
    511       1.46   thorpej {
    512       1.46   thorpej 	struct twe_drive *td;
    513       1.46   thorpej 	int unit, openings;
    514       1.46   thorpej 
    515       1.46   thorpej 	if (sc->sc_nunits != 0)
    516       1.46   thorpej 		openings = (TWE_MAX_QUEUECNT - 1) / sc->sc_nunits;
    517       1.46   thorpej 	else
    518       1.46   thorpej 		openings = 0;
    519       1.46   thorpej 	if (openings == sc->sc_openings)
    520       1.46   thorpej 		return;
    521       1.46   thorpej 	sc->sc_openings = openings;
    522       1.46   thorpej 
    523       1.46   thorpej #ifdef TWE_DEBUG
    524       1.46   thorpej 	printf("%s: %d array%s, %d openings per array\n",
    525       1.97       chs 	    device_xname(sc->sc_dev), sc->sc_nunits,
    526       1.46   thorpej 	    sc->sc_nunits == 1 ? "" : "s", sc->sc_openings);
    527       1.46   thorpej #endif
    528       1.46   thorpej 
    529       1.46   thorpej 	for (unit = 0; unit < TWE_MAX_UNITS; unit++) {
    530       1.46   thorpej 		td = &sc->sc_units[unit];
    531       1.46   thorpej 		if (td->td_dev != NULL)
    532       1.46   thorpej 			(*td->td_callbacks->tcb_openings)(td->td_dev,
    533       1.46   thorpej 			    sc->sc_openings);
    534       1.46   thorpej 	}
    535       1.46   thorpej }
    536       1.46   thorpej 
    537       1.46   thorpej static int
    538       1.46   thorpej twe_add_unit(struct twe_softc *sc, int unit)
    539       1.46   thorpej {
    540       1.46   thorpej 	struct twe_param *dtp, *atp;
    541       1.46   thorpej 	struct twe_array_descriptor *ad;
    542       1.46   thorpej 	struct twe_drive *td;
    543       1.46   thorpej 	struct twe_attach_args twea;
    544       1.46   thorpej 	uint32_t newsize;
    545       1.46   thorpej 	int rv;
    546       1.46   thorpej 	uint16_t dsize;
    547       1.46   thorpej 	uint8_t newtype, newstripe;
    548       1.69  drochner 	int locs[TWECF_NLOCS];
    549       1.46   thorpej 
    550       1.46   thorpej 	if (unit < 0 || unit >= TWE_MAX_UNITS)
    551       1.46   thorpej 		return (EINVAL);
    552       1.46   thorpej 
    553       1.63     perry 	/* Find attached units. */
    554        1.7        ad 	rv = twe_param_get(sc, TWE_PARAM_UNITSUMMARY,
    555       1.38  jdolecek 	    TWE_PARAM_UNITSUMMARY_Status, TWE_MAX_UNITS, NULL, &dtp);
    556        1.7        ad 	if (rv != 0) {
    557  1.103.4.2     skrll 		aprint_error_dev(sc->sc_dev,
    558  1.103.4.2     skrll 		    "error %d fetching unit summary\n", rv);
    559       1.46   thorpej 		return (rv);
    560        1.1        ad 	}
    561        1.1        ad 
    562        1.1        ad 	/* For each detected unit, collect size and store in an array. */
    563       1.46   thorpej 	td = &sc->sc_units[unit];
    564       1.42   thorpej 
    565       1.46   thorpej 	/* Unit present? */
    566       1.46   thorpej 	if ((dtp->tp_data[unit] & TWE_PARAM_UNITSTATUS_Online) == 0) {
    567       1.46   thorpej 		/*
    568       1.46   thorpej 		 * XXX Should we check to see if a device has been
    569       1.46   thorpej 		 * XXX attached at this index and detach it if it
    570       1.46   thorpej 		 * XXX has?  ("rescan" semantics)
    571       1.46   thorpej 		 */
    572       1.46   thorpej 		rv = 0;
    573       1.46   thorpej 		goto out;
    574       1.46   thorpej    	}
    575        1.1        ad 
    576       1.46   thorpej 	rv = twe_param_get_2(sc, TWE_PARAM_UNITINFO + unit,
    577       1.46   thorpej 	    TWE_PARAM_UNITINFO_DescriptorSize, &dsize);
    578       1.46   thorpej 	if (rv != 0) {
    579  1.103.4.2     skrll 		aprint_error_dev(sc->sc_dev,
    580  1.103.4.2     skrll 		    "error %d fetching descriptor size for unit %d\n",
    581  1.103.4.2     skrll 		    rv, unit);
    582       1.46   thorpej 		goto out;
    583       1.46   thorpej 	}
    584       1.46   thorpej 
    585       1.46   thorpej 	rv = twe_param_get(sc, TWE_PARAM_UNITINFO + unit,
    586       1.46   thorpej 	    TWE_PARAM_UNITINFO_Descriptor, dsize - 3, NULL, &atp);
    587       1.46   thorpej 	if (rv != 0) {
    588  1.103.4.2     skrll 		aprint_error_dev(sc->sc_dev,
    589  1.103.4.2     skrll 		    "error %d fetching array descriptor for unit %d\n",
    590  1.103.4.2     skrll 		    rv, unit);
    591       1.46   thorpej 		goto out;
    592       1.46   thorpej 	}
    593       1.42   thorpej 
    594       1.46   thorpej 	ad = (struct twe_array_descriptor *)atp->tp_data;
    595       1.46   thorpej 	newtype = ad->configuration;
    596       1.46   thorpej 	newstripe = ad->stripe_size;
    597       1.46   thorpej 	free(atp, M_DEVBUF);
    598       1.42   thorpej 
    599       1.46   thorpej 	rv = twe_param_get_4(sc, TWE_PARAM_UNITINFO + unit,
    600       1.46   thorpej 	    TWE_PARAM_UNITINFO_Capacity, &newsize);
    601       1.46   thorpej 	if (rv != 0) {
    602       1.97       chs 		aprint_error_dev(sc->sc_dev,
    603       1.85    cegger 		    "error %d fetching capacity for unit %d\n",
    604       1.85    cegger 		    rv, unit);
    605       1.46   thorpej 		goto out;
    606       1.46   thorpej 	}
    607        1.1        ad 
    608       1.46   thorpej 	/*
    609       1.46   thorpej 	 * Have a device, so we need to attach it.  If there is currently
    610       1.46   thorpej 	 * something sitting at the slot, and the parameters are different,
    611       1.46   thorpej 	 * then we detach the old device before attaching the new one.
    612       1.46   thorpej 	 */
    613       1.46   thorpej 	if (td->td_dev != NULL &&
    614       1.46   thorpej 	    td->td_size == newsize &&
    615       1.46   thorpej 	    td->td_type == newtype &&
    616       1.46   thorpej 	    td->td_stripe == newstripe) {
    617       1.46   thorpej 		/* Same as the old device; just keep using it. */
    618       1.46   thorpej 		rv = 0;
    619       1.46   thorpej 		goto out;
    620       1.46   thorpej 	} else if (td->td_dev != NULL) {
    621       1.46   thorpej 		/* Detach the old device first. */
    622       1.46   thorpej 		(void) config_detach(td->td_dev, DETACH_FORCE);
    623       1.46   thorpej 		td->td_dev = NULL;
    624       1.46   thorpej 	} else if (td->td_size == 0)
    625        1.3        ad 		sc->sc_nunits++;
    626       1.46   thorpej 
    627       1.46   thorpej 	/*
    628       1.46   thorpej 	 * Committed to the new array unit; assign its parameters and
    629       1.46   thorpej 	 * recompute the number of available command openings.
    630       1.46   thorpej 	 */
    631       1.46   thorpej 	td->td_size = newsize;
    632       1.46   thorpej 	td->td_type = newtype;
    633       1.46   thorpej 	td->td_stripe = newstripe;
    634       1.46   thorpej 	twe_recompute_openings(sc);
    635       1.46   thorpej 
    636       1.46   thorpej 	twea.twea_unit = unit;
    637       1.59  drochner 
    638       1.69  drochner 	locs[TWECF_UNIT] = unit;
    639       1.59  drochner 
    640       1.97       chs 	td->td_dev = config_found_sm_loc(sc->sc_dev, "twe", locs, &twea,
    641       1.70  drochner 					 twe_print, config_stdsubmatch);
    642       1.46   thorpej 
    643       1.46   thorpej 	rv = 0;
    644       1.46   thorpej  out:
    645        1.1        ad 	free(dtp, M_DEVBUF);
    646       1.46   thorpej 	return (rv);
    647       1.46   thorpej }
    648        1.1        ad 
    649       1.46   thorpej static int
    650       1.46   thorpej twe_del_unit(struct twe_softc *sc, int unit)
    651       1.46   thorpej {
    652       1.46   thorpej 	struct twe_drive *td;
    653        1.1        ad 
    654       1.46   thorpej 	if (unit < 0 || unit >= TWE_MAX_UNITS)
    655       1.46   thorpej 		return (EINVAL);
    656       1.38  jdolecek 
    657       1.46   thorpej 	td = &sc->sc_units[unit];
    658       1.46   thorpej 	if (td->td_size != 0)
    659       1.46   thorpej 		sc->sc_nunits--;
    660       1.46   thorpej 	td->td_size = 0;
    661       1.46   thorpej 	td->td_type = 0;
    662       1.46   thorpej 	td->td_stripe = 0;
    663       1.46   thorpej 	if (td->td_dev != NULL) {
    664       1.46   thorpej 		(void) config_detach(td->td_dev, DETACH_FORCE);
    665       1.46   thorpej 		td->td_dev = NULL;
    666        1.1        ad 	}
    667       1.46   thorpej 	twe_recompute_openings(sc);
    668       1.46   thorpej 	return (0);
    669        1.1        ad }
    670        1.1        ad 
    671        1.1        ad /*
    672       1.47   thorpej  * Reset the controller.
    673       1.47   thorpej  * MUST BE CALLED AT splbio()!
    674        1.1        ad  */
    675        1.1        ad static int
    676        1.1        ad twe_reset(struct twe_softc *sc)
    677        1.1        ad {
    678       1.41   thorpej 	uint16_t aen;
    679       1.41   thorpej 	u_int status;
    680        1.7        ad 	int got, rv;
    681        1.1        ad 
    682        1.1        ad 	/* Issue a soft reset. */
    683       1.22        ad 	twe_outl(sc, TWE_REG_CTL, TWE_CTL_ISSUE_SOFT_RESET |
    684        1.1        ad 	    TWE_CTL_CLEAR_HOST_INTR |
    685        1.1        ad 	    TWE_CTL_CLEAR_ATTN_INTR |
    686        1.1        ad 	    TWE_CTL_MASK_CMD_INTR |
    687        1.1        ad 	    TWE_CTL_MASK_RESP_INTR |
    688        1.1        ad 	    TWE_CTL_CLEAR_ERROR_STS |
    689        1.1        ad 	    TWE_CTL_DISABLE_INTRS);
    690        1.1        ad 
    691       1.45   thorpej 	/* Wait for attention... */
    692       1.57      heas 	if (twe_status_wait(sc, TWE_STS_ATTN_INTR, 30)) {
    693  1.103.4.2     skrll 		aprint_error_dev(sc->sc_dev,
    694  1.103.4.2     skrll 		    "timeout waiting for attention interrupt\n");
    695        1.1        ad 		return (-1);
    696        1.1        ad 	}
    697        1.1        ad 
    698       1.45   thorpej 	/* ...and ACK it. */
    699       1.45   thorpej 	twe_outl(sc, TWE_REG_CTL, TWE_CTL_CLEAR_ATTN_INTR);
    700       1.45   thorpej 
    701       1.45   thorpej 	/*
    702       1.45   thorpej 	 * Pull AENs out of the controller; look for a soft reset AEN.
    703       1.45   thorpej 	 * Open code this, since we want to detect reset even if the
    704       1.45   thorpej 	 * queue for management tools is full.
    705       1.48   thorpej 	 *
    706       1.48   thorpej 	 * Note that since:
    707       1.48   thorpej 	 *	- interrupts are blocked
    708       1.48   thorpej 	 *	- we have reset the controller
    709       1.48   thorpej 	 *	- acknowledged the pending ATTENTION
    710       1.48   thorpej 	 * that there is no way a pending asynchronous AEN fetch would
    711       1.48   thorpej 	 * finish, so clear the flag.
    712       1.45   thorpej 	 */
    713       1.47   thorpej 	sc->sc_flags &= ~TWEF_AEN;
    714        1.1        ad 	for (got = 0;;) {
    715       1.47   thorpej 		rv = twe_aen_get(sc, &aen);
    716        1.7        ad 		if (rv != 0)
    717       1.45   thorpej 			printf("%s: error %d while draining event queue\n",
    718       1.97       chs 			    device_xname(sc->sc_dev), rv);
    719       1.41   thorpej 		if (TWE_AEN_CODE(aen) == TWE_AEN_QUEUE_EMPTY)
    720        1.1        ad 			break;
    721       1.41   thorpej 		if (TWE_AEN_CODE(aen) == TWE_AEN_SOFT_RESET)
    722        1.1        ad 			got = 1;
    723       1.45   thorpej 		twe_aen_enqueue(sc, aen, 1);
    724        1.1        ad 	}
    725       1.45   thorpej 
    726        1.1        ad 	if (!got) {
    727       1.97       chs 		printf("%s: reset not reported\n", device_xname(sc->sc_dev));
    728        1.1        ad 		return (-1);
    729        1.1        ad 	}
    730        1.1        ad 
    731        1.1        ad 	/* Check controller status. */
    732       1.22        ad 	status = twe_inl(sc, TWE_REG_STS);
    733        1.1        ad 	if (twe_status_check(sc, status)) {
    734        1.1        ad 		printf("%s: controller errors detected\n",
    735       1.97       chs 		    device_xname(sc->sc_dev));
    736        1.1        ad 		return (-1);
    737        1.1        ad 	}
    738        1.1        ad 
    739        1.1        ad 	/* Drain the response queue. */
    740        1.1        ad 	for (;;) {
    741       1.22        ad 		status = twe_inl(sc, TWE_REG_STS);
    742        1.1        ad 		if (twe_status_check(sc, status) != 0) {
    743  1.103.4.2     skrll 			aprint_error_dev(sc->sc_dev,
    744  1.103.4.2     skrll 			    "can't drain response queue\n");
    745        1.1        ad 			return (-1);
    746        1.1        ad 		}
    747        1.1        ad 		if ((status & TWE_STS_RESP_QUEUE_EMPTY) != 0)
    748        1.1        ad 			break;
    749       1.99    martin 		(void)twe_inl(sc, TWE_REG_RESP_QUEUE);
    750        1.1        ad 	}
    751        1.1        ad 
    752        1.1        ad 	return (0);
    753        1.1        ad }
    754        1.1        ad 
    755        1.1        ad /*
    756        1.1        ad  * Print autoconfiguration message for a sub-device.
    757        1.1        ad  */
    758        1.1        ad static int
    759        1.1        ad twe_print(void *aux, const char *pnp)
    760        1.1        ad {
    761        1.1        ad 	struct twe_attach_args *twea;
    762        1.1        ad 
    763        1.1        ad 	twea = aux;
    764        1.1        ad 
    765        1.1        ad 	if (pnp != NULL)
    766       1.35   thorpej 		aprint_normal("block device at %s", pnp);
    767       1.35   thorpej 	aprint_normal(" unit %d", twea->twea_unit);
    768        1.1        ad 	return (UNCONF);
    769        1.1        ad }
    770        1.1        ad 
    771        1.1        ad /*
    772        1.1        ad  * Interrupt service routine.
    773        1.1        ad  */
    774        1.1        ad static int
    775        1.1        ad twe_intr(void *arg)
    776        1.1        ad {
    777        1.1        ad 	struct twe_softc *sc;
    778        1.1        ad 	u_int status;
    779        1.7        ad 	int caught, rv;
    780        1.1        ad 
    781        1.1        ad 	sc = arg;
    782        1.1        ad 	caught = 0;
    783       1.22        ad 	status = twe_inl(sc, TWE_REG_STS);
    784        1.1        ad 	twe_status_check(sc, status);
    785        1.1        ad 
    786        1.1        ad 	/* Host interrupts - purpose unknown. */
    787        1.1        ad 	if ((status & TWE_STS_HOST_INTR) != 0) {
    788       1.38  jdolecek #ifdef DEBUG
    789       1.97       chs 		printf("%s: host interrupt\n", device_xname(sc->sc_dev));
    790        1.1        ad #endif
    791       1.22        ad 		twe_outl(sc, TWE_REG_CTL, TWE_CTL_CLEAR_HOST_INTR);
    792        1.1        ad 		caught = 1;
    793        1.1        ad 	}
    794        1.1        ad 
    795        1.1        ad 	/*
    796        1.1        ad 	 * Attention interrupts, signalled when a controller or child device
    797       1.18       wiz 	 * state change has occurred.
    798        1.1        ad 	 */
    799        1.1        ad 	if ((status & TWE_STS_ATTN_INTR) != 0) {
    800       1.47   thorpej 		rv = twe_aen_get(sc, NULL);
    801       1.45   thorpej 		if (rv != 0)
    802  1.103.4.2     skrll 			aprint_error_dev(sc->sc_dev,
    803  1.103.4.2     skrll 			    "unable to retrieve AEN (%d)\n", rv);
    804       1.45   thorpej 		else
    805       1.45   thorpej 			twe_outl(sc, TWE_REG_CTL, TWE_CTL_CLEAR_ATTN_INTR);
    806        1.1        ad 		caught = 1;
    807        1.1        ad 	}
    808        1.1        ad 
    809        1.1        ad 	/*
    810        1.1        ad 	 * Command interrupts, signalled when the controller can accept more
    811        1.1        ad 	 * commands.  We don't use this; instead, we try to submit commands
    812       1.63     perry 	 * when we receive them, and when other commands have completed.
    813        1.1        ad 	 * Mask it so we don't get another one.
    814        1.1        ad 	 */
    815        1.1        ad 	if ((status & TWE_STS_CMD_INTR) != 0) {
    816       1.38  jdolecek #ifdef DEBUG
    817       1.97       chs 		printf("%s: command interrupt\n", device_xname(sc->sc_dev));
    818        1.1        ad #endif
    819       1.22        ad 		twe_outl(sc, TWE_REG_CTL, TWE_CTL_MASK_CMD_INTR);
    820        1.1        ad 		caught = 1;
    821        1.1        ad 	}
    822        1.1        ad 
    823        1.1        ad 	if ((status & TWE_STS_RESP_INTR) != 0) {
    824        1.1        ad 		twe_poll(sc);
    825        1.1        ad 		caught = 1;
    826        1.1        ad 	}
    827        1.1        ad 
    828        1.1        ad 	return (caught);
    829        1.1        ad }
    830        1.1        ad 
    831        1.1        ad /*
    832       1.47   thorpej  * Fetch an AEN.  Even though this is really like parameter
    833       1.47   thorpej  * retrieval, we handle this specially, because we issue this
    834       1.47   thorpej  * AEN retrieval command from interrupt context, and thus
    835       1.47   thorpej  * reserve a CCB for it to avoid resource shortage.
    836       1.47   thorpej  *
    837       1.47   thorpej  * XXX There are still potential resource shortages we could
    838       1.47   thorpej  * XXX encounter.  Consider pre-allocating all AEN-related
    839       1.47   thorpej  * XXX resources.
    840       1.47   thorpej  *
    841       1.47   thorpej  * MUST BE CALLED AT splbio()!
    842       1.47   thorpej  */
    843       1.47   thorpej static int
    844       1.47   thorpej twe_aen_get(struct twe_softc *sc, uint16_t *aenp)
    845       1.47   thorpej {
    846       1.47   thorpej 	struct twe_ccb *ccb;
    847       1.47   thorpej 	struct twe_cmd *tc;
    848       1.47   thorpej 	struct twe_param *tp;
    849       1.47   thorpej 	int rv;
    850       1.47   thorpej 
    851       1.47   thorpej 	/*
    852       1.47   thorpej 	 * If we're already retrieving an AEN, just wait; another
    853       1.47   thorpej 	 * retrieval will be chained after the current one completes.
    854       1.47   thorpej 	 */
    855       1.47   thorpej 	if (sc->sc_flags & TWEF_AEN) {
    856       1.47   thorpej 		/*
    857       1.47   thorpej 		 * It is a fatal software programming error to attempt
    858       1.47   thorpej 		 * to fetch an AEN synchronously when an AEN fetch is
    859       1.47   thorpej 		 * already pending.
    860       1.47   thorpej 		 */
    861       1.47   thorpej 		KASSERT(aenp == NULL);
    862       1.47   thorpej 		return (0);
    863       1.47   thorpej 	}
    864       1.47   thorpej 
    865       1.47   thorpej 	tp = malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
    866       1.47   thorpej 	if (tp == NULL)
    867       1.47   thorpej 		return (ENOMEM);
    868       1.47   thorpej 
    869       1.49   thorpej 	ccb = twe_ccb_alloc(sc,
    870       1.47   thorpej 	    TWE_CCB_AEN | TWE_CCB_DATA_IN | TWE_CCB_DATA_OUT);
    871       1.49   thorpej 	KASSERT(ccb != NULL);
    872       1.47   thorpej 
    873       1.47   thorpej 	ccb->ccb_data = tp;
    874       1.47   thorpej 	ccb->ccb_datasize = TWE_SECTOR_SIZE;
    875       1.47   thorpej 	ccb->ccb_tx.tx_handler = (aenp == NULL) ? twe_aen_handler : NULL;
    876       1.47   thorpej 	ccb->ccb_tx.tx_context = tp;
    877       1.97       chs 	ccb->ccb_tx.tx_dv = sc->sc_dev;
    878       1.47   thorpej 
    879       1.47   thorpej 	tc = ccb->ccb_cmd;
    880       1.47   thorpej 	tc->tc_size = 2;
    881       1.47   thorpej 	tc->tc_opcode = TWE_OP_GET_PARAM | (tc->tc_size << 5);
    882       1.47   thorpej 	tc->tc_unit = 0;
    883       1.47   thorpej 	tc->tc_count = htole16(1);
    884       1.47   thorpej 
    885       1.47   thorpej 	/* Fill in the outbound parameter data. */
    886       1.47   thorpej 	tp->tp_table_id = htole16(TWE_PARAM_AEN);
    887       1.47   thorpej 	tp->tp_param_id = TWE_PARAM_AEN_UnitCode;
    888       1.47   thorpej 	tp->tp_param_size = 2;
    889       1.47   thorpej 
    890       1.47   thorpej 	/* Map the transfer. */
    891       1.47   thorpej 	if ((rv = twe_ccb_map(sc, ccb)) != 0) {
    892       1.47   thorpej 		twe_ccb_free(sc, ccb);
    893       1.47   thorpej 		goto done;
    894       1.47   thorpej 	}
    895       1.47   thorpej 
    896       1.47   thorpej 	/* Enqueue the command and wait. */
    897       1.47   thorpej 	if (aenp != NULL) {
    898       1.47   thorpej 		rv = twe_ccb_poll(sc, ccb, 5);
    899       1.47   thorpej 		twe_ccb_unmap(sc, ccb);
    900       1.47   thorpej 		twe_ccb_free(sc, ccb);
    901       1.47   thorpej 		if (rv == 0)
    902       1.47   thorpej 			*aenp = le16toh(*(uint16_t *)tp->tp_data);
    903       1.47   thorpej 		free(tp, M_DEVBUF);
    904       1.47   thorpej 	} else {
    905       1.47   thorpej 		sc->sc_flags |= TWEF_AEN;
    906       1.47   thorpej 		twe_ccb_enqueue(sc, ccb);
    907       1.47   thorpej 		rv = 0;
    908       1.47   thorpej 	}
    909       1.47   thorpej 
    910       1.47   thorpej  done:
    911       1.47   thorpej 	return (rv);
    912       1.47   thorpej }
    913       1.47   thorpej 
    914       1.47   thorpej /*
    915        1.1        ad  * Handle an AEN returned by the controller.
    916       1.47   thorpej  * MUST BE CALLED AT splbio()!
    917        1.1        ad  */
    918        1.1        ad static void
    919        1.1        ad twe_aen_handler(struct twe_ccb *ccb, int error)
    920        1.1        ad {
    921        1.1        ad 	struct twe_softc *sc;
    922        1.1        ad 	struct twe_param *tp;
    923       1.45   thorpej 	uint16_t aen;
    924       1.45   thorpej 	int rv;
    925        1.1        ad 
    926       1.90    cegger 	sc = device_private(ccb->ccb_tx.tx_dv);
    927        1.1        ad 	tp = ccb->ccb_tx.tx_context;
    928        1.1        ad 	twe_ccb_unmap(sc, ccb);
    929        1.1        ad 
    930       1.47   thorpej 	sc->sc_flags &= ~TWEF_AEN;
    931       1.47   thorpej 
    932        1.3        ad 	if (error) {
    933       1.97       chs 		aprint_error_dev(sc->sc_dev, "error retrieving AEN\n");
    934        1.3        ad 		aen = TWE_AEN_QUEUE_EMPTY;
    935        1.3        ad 	} else
    936        1.1        ad 		aen = le16toh(*(u_int16_t *)tp->tp_data);
    937        1.3        ad 	free(tp, M_DEVBUF);
    938        1.3        ad 	twe_ccb_free(sc, ccb);
    939        1.3        ad 
    940        1.7        ad 	if (TWE_AEN_CODE(aen) == TWE_AEN_QUEUE_EMPTY) {
    941       1.22        ad 		twe_outl(sc, TWE_REG_CTL, TWE_CTL_CLEAR_ATTN_INTR);
    942        1.7        ad 		return;
    943        1.7        ad 	}
    944        1.7        ad 
    945       1.45   thorpej 	twe_aen_enqueue(sc, aen, 0);
    946        1.3        ad 
    947        1.7        ad 	/*
    948        1.7        ad 	 * Chain another retrieval in case interrupts have been
    949        1.7        ad 	 * coalesced.
    950        1.7        ad 	 */
    951       1.47   thorpej 	rv = twe_aen_get(sc, NULL);
    952        1.7        ad 	if (rv != 0)
    953  1.103.4.2     skrll 		aprint_error_dev(sc->sc_dev,
    954  1.103.4.2     skrll 		    "unable to retrieve AEN (%d)\n", rv);
    955        1.1        ad }
    956        1.1        ad 
    957       1.45   thorpej static void
    958       1.45   thorpej twe_aen_enqueue(struct twe_softc *sc, uint16_t aen, int quiet)
    959       1.45   thorpej {
    960       1.45   thorpej 	const char *str, *msg;
    961       1.55   thorpej 	int s, next, nextnext, level;
    962       1.45   thorpej 
    963       1.45   thorpej 	/*
    964       1.45   thorpej 	 * First report the AEN on the console.  Maybe.
    965       1.45   thorpej 	 */
    966       1.45   thorpej 	if (! quiet) {
    967       1.45   thorpej 		str = twe_describe_code(twe_table_aen, TWE_AEN_CODE(aen));
    968       1.45   thorpej 		if (str == NULL) {
    969  1.103.4.2     skrll 			aprint_error_dev(sc->sc_dev,
    970  1.103.4.2     skrll 			    "unknown AEN 0x%04x\n", aen);
    971       1.45   thorpej 		} else {
    972       1.55   thorpej 			msg = str + 3;
    973       1.55   thorpej 			switch (str[1]) {
    974       1.55   thorpej 			case 'E':	level = LOG_EMERG; break;
    975       1.55   thorpej 			case 'a':	level = LOG_ALERT; break;
    976       1.55   thorpej 			case 'c':	level = LOG_CRIT; break;
    977       1.55   thorpej 			case 'e':	level = LOG_ERR; break;
    978       1.55   thorpej 			case 'w':	level = LOG_WARNING; break;
    979       1.55   thorpej 			case 'n':	level = LOG_NOTICE; break;
    980       1.55   thorpej 			case 'i':	level = LOG_INFO; break;
    981       1.55   thorpej 			case 'd':	level = LOG_DEBUG; break;
    982       1.55   thorpej 			default:
    983       1.55   thorpej 				/* Don't use syslog. */
    984       1.55   thorpej 				level = -1;
    985       1.55   thorpej 			}
    986       1.45   thorpej 
    987       1.55   thorpej 			if (level < 0) {
    988       1.55   thorpej 				switch (str[0]) {
    989       1.55   thorpej 				case 'u':
    990       1.55   thorpej 				case 'p':
    991       1.55   thorpej 					printf("%s: %s %d: %s\n",
    992       1.97       chs 					    device_xname(sc->sc_dev),
    993       1.55   thorpej 					    str[0] == 'u' ? "unit" : "port",
    994       1.55   thorpej 					    TWE_AEN_UNIT(aen), msg);
    995       1.55   thorpej 					break;
    996       1.55   thorpej 
    997       1.55   thorpej 				default:
    998       1.55   thorpej 					printf("%s: %s\n",
    999       1.97       chs 					    device_xname(sc->sc_dev), msg);
   1000       1.55   thorpej 				}
   1001       1.55   thorpej 			} else {
   1002       1.55   thorpej 				switch (str[0]) {
   1003       1.55   thorpej 				case 'u':
   1004       1.55   thorpej 				case 'p':
   1005       1.55   thorpej 					log(level, "%s: %s %d: %s\n",
   1006       1.97       chs 					    device_xname(sc->sc_dev),
   1007       1.55   thorpej 					    str[0] == 'u' ? "unit" : "port",
   1008       1.55   thorpej 					    TWE_AEN_UNIT(aen), msg);
   1009       1.55   thorpej 					break;
   1010       1.55   thorpej 
   1011       1.55   thorpej 				default:
   1012       1.55   thorpej 					log(level, "%s: %s\n",
   1013       1.97       chs 					    device_xname(sc->sc_dev), msg);
   1014       1.55   thorpej 				}
   1015       1.45   thorpej 			}
   1016       1.45   thorpej 		}
   1017       1.45   thorpej 	}
   1018       1.45   thorpej 
   1019       1.45   thorpej 	/* Now enqueue the AEN for mangement tools. */
   1020       1.45   thorpej 	s = splbio();
   1021       1.45   thorpej 
   1022       1.45   thorpej 	next = (sc->sc_aen_head + 1) % TWE_AEN_Q_LENGTH;
   1023       1.45   thorpej 	nextnext = (sc->sc_aen_head + 2) % TWE_AEN_Q_LENGTH;
   1024       1.45   thorpej 
   1025       1.45   thorpej 	/*
   1026       1.45   thorpej 	 * If this is the last free slot, then queue up a "queue
   1027       1.45   thorpej 	 * full" message.
   1028       1.45   thorpej 	 */
   1029       1.45   thorpej 	if (nextnext == sc->sc_aen_tail)
   1030       1.45   thorpej 		aen = TWE_AEN_QUEUE_FULL;
   1031       1.45   thorpej 
   1032       1.45   thorpej 	if (next != sc->sc_aen_tail) {
   1033       1.45   thorpej 		sc->sc_aen_queue[sc->sc_aen_head] = aen;
   1034       1.45   thorpej 		sc->sc_aen_head = next;
   1035       1.45   thorpej 	}
   1036       1.45   thorpej 
   1037       1.45   thorpej 	if (sc->sc_flags & TWEF_AENQ_WAIT) {
   1038       1.45   thorpej 		sc->sc_flags &= ~TWEF_AENQ_WAIT;
   1039       1.45   thorpej 		wakeup(&sc->sc_aen_queue);
   1040       1.45   thorpej 	}
   1041       1.45   thorpej 
   1042       1.45   thorpej 	splx(s);
   1043       1.45   thorpej }
   1044       1.45   thorpej 
   1045       1.45   thorpej /* NOTE: Must be called at splbio(). */
   1046       1.45   thorpej static uint16_t
   1047       1.45   thorpej twe_aen_dequeue(struct twe_softc *sc)
   1048       1.45   thorpej {
   1049       1.45   thorpej 	uint16_t aen;
   1050       1.45   thorpej 
   1051       1.45   thorpej 	if (sc->sc_aen_tail == sc->sc_aen_head)
   1052       1.45   thorpej 		aen = TWE_AEN_QUEUE_EMPTY;
   1053       1.45   thorpej 	else {
   1054       1.45   thorpej 		aen = sc->sc_aen_queue[sc->sc_aen_tail];
   1055       1.60      heas 		sc->sc_aen_tail = (sc->sc_aen_tail + 1) % TWE_AEN_Q_LENGTH;
   1056       1.45   thorpej 	}
   1057       1.45   thorpej 
   1058       1.45   thorpej 	return (aen);
   1059       1.45   thorpej }
   1060       1.45   thorpej 
   1061        1.1        ad /*
   1062       1.41   thorpej  * These are short-hand functions that execute TWE_OP_GET_PARAM to
   1063       1.41   thorpej  * fetch 1, 2, and 4 byte parameter values, respectively.
   1064       1.41   thorpej  */
   1065       1.43   thorpej int
   1066       1.41   thorpej twe_param_get_1(struct twe_softc *sc, int table_id, int param_id,
   1067       1.41   thorpej     uint8_t *valp)
   1068       1.41   thorpej {
   1069       1.41   thorpej 	struct twe_param *tp;
   1070       1.41   thorpej 	int rv;
   1071       1.41   thorpej 
   1072       1.41   thorpej 	rv = twe_param_get(sc, table_id, param_id, 1, NULL, &tp);
   1073       1.41   thorpej 	if (rv != 0)
   1074       1.41   thorpej 		return (rv);
   1075       1.41   thorpej 	*valp = *(uint8_t *)tp->tp_data;
   1076       1.41   thorpej 	free(tp, M_DEVBUF);
   1077       1.41   thorpej 	return (0);
   1078       1.41   thorpej }
   1079       1.41   thorpej 
   1080       1.43   thorpej int
   1081       1.41   thorpej twe_param_get_2(struct twe_softc *sc, int table_id, int param_id,
   1082       1.41   thorpej     uint16_t *valp)
   1083       1.41   thorpej {
   1084       1.41   thorpej 	struct twe_param *tp;
   1085       1.41   thorpej 	int rv;
   1086       1.41   thorpej 
   1087       1.41   thorpej 	rv = twe_param_get(sc, table_id, param_id, 2, NULL, &tp);
   1088       1.41   thorpej 	if (rv != 0)
   1089       1.41   thorpej 		return (rv);
   1090       1.41   thorpej 	*valp = le16toh(*(uint16_t *)tp->tp_data);
   1091       1.41   thorpej 	free(tp, M_DEVBUF);
   1092       1.41   thorpej 	return (0);
   1093       1.41   thorpej }
   1094       1.41   thorpej 
   1095       1.43   thorpej int
   1096       1.41   thorpej twe_param_get_4(struct twe_softc *sc, int table_id, int param_id,
   1097       1.41   thorpej     uint32_t *valp)
   1098       1.41   thorpej {
   1099       1.41   thorpej 	struct twe_param *tp;
   1100       1.41   thorpej 	int rv;
   1101       1.41   thorpej 
   1102       1.41   thorpej 	rv = twe_param_get(sc, table_id, param_id, 4, NULL, &tp);
   1103       1.41   thorpej 	if (rv != 0)
   1104       1.41   thorpej 		return (rv);
   1105       1.41   thorpej 	*valp = le32toh(*(uint32_t *)tp->tp_data);
   1106       1.41   thorpej 	free(tp, M_DEVBUF);
   1107       1.41   thorpej 	return (0);
   1108       1.41   thorpej }
   1109       1.41   thorpej 
   1110       1.41   thorpej /*
   1111        1.1        ad  * Execute a TWE_OP_GET_PARAM command.  If a callback function is provided,
   1112       1.63     perry  * it will be called with generated context when the command has completed.
   1113        1.1        ad  * If no callback is provided, the command will be executed synchronously
   1114        1.3        ad  * and a pointer to a buffer containing the data returned.
   1115        1.1        ad  *
   1116        1.3        ad  * The caller or callback is responsible for freeing the buffer.
   1117       1.49   thorpej  *
   1118       1.49   thorpej  * NOTE: We assume we can sleep here to wait for a CCB to become available.
   1119        1.1        ad  */
   1120       1.43   thorpej int
   1121        1.1        ad twe_param_get(struct twe_softc *sc, int table_id, int param_id, size_t size,
   1122       1.38  jdolecek 	      void (*func)(struct twe_ccb *, int), struct twe_param **pbuf)
   1123        1.1        ad {
   1124        1.1        ad 	struct twe_ccb *ccb;
   1125        1.1        ad 	struct twe_cmd *tc;
   1126        1.1        ad 	struct twe_param *tp;
   1127        1.1        ad 	int rv, s;
   1128        1.1        ad 
   1129       1.33  christos 	tp = malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
   1130       1.33  christos 	if (tp == NULL)
   1131       1.33  christos 		return ENOMEM;
   1132       1.33  christos 
   1133       1.49   thorpej 	ccb = twe_ccb_alloc_wait(sc, TWE_CCB_DATA_IN | TWE_CCB_DATA_OUT);
   1134       1.49   thorpej 	KASSERT(ccb != NULL);
   1135        1.1        ad 
   1136        1.1        ad 	ccb->ccb_data = tp;
   1137        1.1        ad 	ccb->ccb_datasize = TWE_SECTOR_SIZE;
   1138        1.1        ad 	ccb->ccb_tx.tx_handler = func;
   1139        1.1        ad 	ccb->ccb_tx.tx_context = tp;
   1140       1.97       chs 	ccb->ccb_tx.tx_dv = sc->sc_dev;
   1141        1.1        ad 
   1142        1.1        ad 	tc = ccb->ccb_cmd;
   1143        1.1        ad 	tc->tc_size = 2;
   1144        1.1        ad 	tc->tc_opcode = TWE_OP_GET_PARAM | (tc->tc_size << 5);
   1145        1.1        ad 	tc->tc_unit = 0;
   1146        1.1        ad 	tc->tc_count = htole16(1);
   1147        1.1        ad 
   1148        1.1        ad 	/* Fill in the outbound parameter data. */
   1149        1.1        ad 	tp->tp_table_id = htole16(table_id);
   1150        1.1        ad 	tp->tp_param_id = param_id;
   1151        1.1        ad 	tp->tp_param_size = size;
   1152        1.1        ad 
   1153        1.1        ad 	/* Map the transfer. */
   1154        1.7        ad 	if ((rv = twe_ccb_map(sc, ccb)) != 0) {
   1155        1.2        ad 		twe_ccb_free(sc, ccb);
   1156       1.33  christos 		goto done;
   1157        1.1        ad 	}
   1158        1.1        ad 
   1159        1.1        ad 	/* Submit the command and either wait or let the callback handle it. */
   1160        1.1        ad 	if (func == NULL) {
   1161        1.1        ad 		s = splbio();
   1162        1.7        ad 		rv = twe_ccb_poll(sc, ccb, 5);
   1163        1.1        ad 		twe_ccb_unmap(sc, ccb);
   1164        1.2        ad 		twe_ccb_free(sc, ccb);
   1165        1.1        ad 		splx(s);
   1166        1.1        ad 	} else {
   1167       1.38  jdolecek #ifdef DEBUG
   1168       1.33  christos 		if (pbuf != NULL)
   1169       1.33  christos 			panic("both func and pbuf defined");
   1170       1.33  christos #endif
   1171        1.1        ad 		twe_ccb_enqueue(sc, ccb);
   1172       1.33  christos 		return 0;
   1173       1.33  christos 	}
   1174       1.33  christos 
   1175       1.33  christos done:
   1176       1.33  christos 	if (pbuf == NULL || rv != 0)
   1177       1.33  christos 		free(tp, M_DEVBUF);
   1178       1.33  christos 	else if (pbuf != NULL && rv == 0)
   1179       1.33  christos 		*pbuf = tp;
   1180       1.33  christos 	return rv;
   1181       1.33  christos }
   1182       1.33  christos 
   1183       1.33  christos /*
   1184       1.33  christos  * Execute a TWE_OP_SET_PARAM command.
   1185       1.49   thorpej  *
   1186       1.49   thorpej  * NOTE: We assume we can sleep here to wait for a CCB to become available.
   1187       1.33  christos  */
   1188       1.33  christos static int
   1189       1.33  christos twe_param_set(struct twe_softc *sc, int table_id, int param_id, size_t size,
   1190       1.66  christos 	      void *sbuf)
   1191       1.33  christos {
   1192       1.33  christos 	struct twe_ccb *ccb;
   1193       1.33  christos 	struct twe_cmd *tc;
   1194       1.33  christos 	struct twe_param *tp;
   1195       1.33  christos 	int rv, s;
   1196       1.33  christos 
   1197       1.33  christos 	tp = malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
   1198       1.33  christos 	if (tp == NULL)
   1199       1.33  christos 		return ENOMEM;
   1200       1.33  christos 
   1201       1.49   thorpej 	ccb = twe_ccb_alloc_wait(sc, TWE_CCB_DATA_IN | TWE_CCB_DATA_OUT);
   1202       1.49   thorpej 	KASSERT(ccb != NULL);
   1203       1.33  christos 
   1204       1.33  christos 	ccb->ccb_data = tp;
   1205       1.33  christos 	ccb->ccb_datasize = TWE_SECTOR_SIZE;
   1206       1.33  christos 	ccb->ccb_tx.tx_handler = 0;
   1207       1.33  christos 	ccb->ccb_tx.tx_context = tp;
   1208       1.97       chs 	ccb->ccb_tx.tx_dv = sc->sc_dev;
   1209       1.33  christos 
   1210       1.33  christos 	tc = ccb->ccb_cmd;
   1211       1.33  christos 	tc->tc_size = 2;
   1212       1.33  christos 	tc->tc_opcode = TWE_OP_SET_PARAM | (tc->tc_size << 5);
   1213       1.33  christos 	tc->tc_unit = 0;
   1214       1.33  christos 	tc->tc_count = htole16(1);
   1215       1.33  christos 
   1216       1.33  christos 	/* Fill in the outbound parameter data. */
   1217       1.33  christos 	tp->tp_table_id = htole16(table_id);
   1218       1.33  christos 	tp->tp_param_id = param_id;
   1219       1.33  christos 	tp->tp_param_size = size;
   1220       1.66  christos 	memcpy(tp->tp_data, sbuf, size);
   1221       1.33  christos 
   1222       1.33  christos 	/* Map the transfer. */
   1223       1.33  christos 	if ((rv = twe_ccb_map(sc, ccb)) != 0) {
   1224       1.33  christos 		twe_ccb_free(sc, ccb);
   1225       1.33  christos 		goto done;
   1226        1.1        ad 	}
   1227        1.1        ad 
   1228       1.33  christos 	/* Submit the command and wait. */
   1229       1.33  christos 	s = splbio();
   1230       1.33  christos 	rv = twe_ccb_poll(sc, ccb, 5);
   1231       1.33  christos 	twe_ccb_unmap(sc, ccb);
   1232       1.33  christos 	twe_ccb_free(sc, ccb);
   1233       1.33  christos 	splx(s);
   1234       1.33  christos done:
   1235       1.33  christos 	free(tp, M_DEVBUF);
   1236        1.7        ad 	return (rv);
   1237        1.1        ad }
   1238        1.1        ad 
   1239        1.1        ad /*
   1240       1.63     perry  * Execute a TWE_OP_INIT_CONNECTION command.  Return non-zero on error.
   1241        1.1        ad  * Must be called with interrupts blocked.
   1242        1.1        ad  */
   1243        1.1        ad static int
   1244        1.1        ad twe_init_connection(struct twe_softc *sc)
   1245        1.1        ad {
   1246        1.1        ad 	struct twe_ccb *ccb;
   1247        1.1        ad 	struct twe_cmd *tc;
   1248        1.1        ad 	int rv;
   1249        1.1        ad 
   1250       1.49   thorpej 	if ((ccb = twe_ccb_alloc(sc, 0)) == NULL)
   1251       1.49   thorpej 		return (EAGAIN);
   1252        1.1        ad 
   1253        1.1        ad 	/* Build the command. */
   1254        1.1        ad 	tc = ccb->ccb_cmd;
   1255        1.1        ad 	tc->tc_size = 3;
   1256        1.1        ad 	tc->tc_opcode = TWE_OP_INIT_CONNECTION;
   1257        1.1        ad 	tc->tc_unit = 0;
   1258        1.3        ad 	tc->tc_count = htole16(TWE_MAX_CMDS);
   1259        1.1        ad 	tc->tc_args.init_connection.response_queue_pointer = 0;
   1260        1.1        ad 
   1261        1.1        ad 	/* Submit the command for immediate execution. */
   1262        1.7        ad 	rv = twe_ccb_poll(sc, ccb, 5);
   1263        1.2        ad 	twe_ccb_free(sc, ccb);
   1264        1.1        ad 	return (rv);
   1265        1.1        ad }
   1266        1.1        ad 
   1267        1.1        ad /*
   1268        1.1        ad  * Poll the controller for completed commands.  Must be called with
   1269        1.1        ad  * interrupts blocked.
   1270        1.1        ad  */
   1271        1.1        ad static void
   1272        1.1        ad twe_poll(struct twe_softc *sc)
   1273        1.1        ad {
   1274        1.1        ad 	struct twe_ccb *ccb;
   1275        1.1        ad 	int found;
   1276        1.1        ad 	u_int status, cmdid;
   1277        1.1        ad 
   1278        1.1        ad 	found = 0;
   1279        1.1        ad 
   1280        1.1        ad 	for (;;) {
   1281       1.22        ad 		status = twe_inl(sc, TWE_REG_STS);
   1282        1.1        ad 		twe_status_check(sc, status);
   1283        1.1        ad 
   1284        1.1        ad 		if ((status & TWE_STS_RESP_QUEUE_EMPTY))
   1285        1.1        ad 			break;
   1286        1.1        ad 
   1287        1.1        ad 		found = 1;
   1288       1.22        ad 		cmdid = twe_inl(sc, TWE_REG_RESP_QUEUE);
   1289        1.1        ad 		cmdid = (cmdid & TWE_RESP_MASK) >> TWE_RESP_SHIFT;
   1290        1.7        ad 		if (cmdid >= TWE_MAX_QUEUECNT) {
   1291       1.97       chs 			aprint_error_dev(sc->sc_dev, "bad cmdid %d\n", cmdid);
   1292        1.1        ad 			continue;
   1293        1.1        ad 		}
   1294        1.1        ad 
   1295        1.1        ad 		ccb = sc->sc_ccbs + cmdid;
   1296        1.1        ad 		if ((ccb->ccb_flags & TWE_CCB_ACTIVE) == 0) {
   1297       1.50   thorpej 			printf("%s: CCB for cmdid %d not active\n",
   1298       1.97       chs 			    device_xname(sc->sc_dev), cmdid);
   1299        1.1        ad 			continue;
   1300        1.1        ad 		}
   1301        1.1        ad 		ccb->ccb_flags ^= TWE_CCB_COMPLETE | TWE_CCB_ACTIVE;
   1302        1.1        ad 
   1303        1.1        ad 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
   1304       1.83  christos 		    (char *)ccb->ccb_cmd - (char *)sc->sc_cmds,
   1305        1.1        ad 		    sizeof(struct twe_cmd),
   1306        1.1        ad 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1307        1.1        ad 
   1308        1.1        ad 		/* Pass notification to upper layers. */
   1309        1.1        ad 		if (ccb->ccb_tx.tx_handler != NULL)
   1310        1.1        ad 			(*ccb->ccb_tx.tx_handler)(ccb,
   1311        1.1        ad 			    ccb->ccb_cmd->tc_status != 0 ? EIO : 0);
   1312        1.1        ad 	}
   1313        1.1        ad 
   1314        1.1        ad 	/* If any commands have completed, run the software queue. */
   1315        1.1        ad 	if (found)
   1316        1.1        ad 		twe_ccb_enqueue(sc, NULL);
   1317        1.1        ad }
   1318        1.1        ad 
   1319        1.1        ad /*
   1320        1.1        ad  * Wait for `status' to be set in the controller status register.  Return
   1321        1.1        ad  * zero if found, non-zero if the operation timed out.
   1322        1.1        ad  */
   1323        1.1        ad static int
   1324        1.1        ad twe_status_wait(struct twe_softc *sc, u_int32_t status, int timo)
   1325        1.1        ad {
   1326        1.1        ad 
   1327       1.11        ad 	for (timo *= 10; timo != 0; timo--) {
   1328       1.22        ad 		if ((twe_inl(sc, TWE_REG_STS) & status) == status)
   1329        1.1        ad 			break;
   1330        1.1        ad 		delay(100000);
   1331        1.1        ad 	}
   1332        1.1        ad 
   1333        1.1        ad 	return (timo == 0);
   1334        1.1        ad }
   1335        1.1        ad 
   1336        1.1        ad /*
   1337       1.64       erh  * Clear a PCI parity error.
   1338       1.64       erh  */
   1339       1.64       erh static void
   1340       1.64       erh twe_clear_pci_parity_error(struct twe_softc *sc)
   1341       1.64       erh {
   1342  1.103.4.2     skrll 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, 0x0,
   1343  1.103.4.2     skrll 	    TWE_CTL_CLEAR_PARITY_ERROR);
   1344       1.64       erh 
   1345       1.64       erh 	//FreeBSD: pci_write_config(sc->twe_dev, PCIR_STATUS, TWE_PCI_CLEAR_PARITY_ERROR, 2);
   1346       1.64       erh }
   1347       1.64       erh 
   1348       1.64       erh 
   1349       1.64       erh /*
   1350       1.64       erh  * Clear a PCI abort.
   1351       1.64       erh  */
   1352       1.64       erh static void
   1353       1.64       erh twe_clear_pci_abort(struct twe_softc *sc)
   1354       1.64       erh {
   1355       1.64       erh 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, 0x0, TWE_CTL_CLEAR_PCI_ABORT);
   1356       1.64       erh 
   1357       1.64       erh 	//FreeBSD: pci_write_config(sc->twe_dev, PCIR_STATUS, TWE_PCI_CLEAR_PCI_ABORT, 2);
   1358       1.64       erh }
   1359       1.64       erh 
   1360       1.64       erh /*
   1361        1.1        ad  * Complain if the status bits aren't what we expect.
   1362        1.1        ad  */
   1363        1.1        ad static int
   1364        1.1        ad twe_status_check(struct twe_softc *sc, u_int status)
   1365        1.1        ad {
   1366        1.1        ad 	int rv;
   1367        1.1        ad 
   1368        1.1        ad 	rv = 0;
   1369        1.1        ad 
   1370        1.1        ad 	if ((status & TWE_STS_EXPECTED_BITS) != TWE_STS_EXPECTED_BITS) {
   1371       1.97       chs 		aprint_error_dev(sc->sc_dev, "missing status bits: 0x%08x\n",
   1372        1.1        ad 		    status & ~TWE_STS_EXPECTED_BITS);
   1373        1.1        ad 		rv = -1;
   1374        1.1        ad 	}
   1375        1.1        ad 
   1376        1.1        ad 	if ((status & TWE_STS_UNEXPECTED_BITS) != 0) {
   1377       1.97       chs 		aprint_error_dev(sc->sc_dev, "unexpected status bits: 0x%08x\n",
   1378       1.85    cegger 		    status & TWE_STS_UNEXPECTED_BITS);
   1379        1.1        ad 		rv = -1;
   1380       1.64       erh 		if (status & TWE_STS_PCI_PARITY_ERROR) {
   1381  1.103.4.2     skrll 			aprint_error_dev(sc->sc_dev, "PCI parity error: Reseat"
   1382  1.103.4.2     skrll 			    " card, move card or buggy device present.\n");
   1383       1.64       erh 			twe_clear_pci_parity_error(sc);
   1384       1.64       erh 		}
   1385       1.64       erh 		if (status & TWE_STS_PCI_ABORT) {
   1386       1.97       chs 			aprint_error_dev(sc->sc_dev, "PCI abort, clearing.\n");
   1387       1.64       erh 			twe_clear_pci_abort(sc);
   1388       1.64       erh 		}
   1389        1.1        ad 	}
   1390        1.1        ad 
   1391        1.1        ad 	return (rv);
   1392        1.1        ad }
   1393        1.1        ad 
   1394        1.1        ad /*
   1395        1.1        ad  * Allocate and initialise a CCB.
   1396        1.1        ad  */
   1397       1.72     perry static inline void
   1398       1.81  christos twe_ccb_init(struct twe_softc *sc, struct twe_ccb *ccb, int flags)
   1399        1.1        ad {
   1400        1.1        ad 	struct twe_cmd *tc;
   1401       1.49   thorpej 
   1402       1.49   thorpej 	ccb->ccb_tx.tx_handler = NULL;
   1403       1.49   thorpej 	ccb->ccb_flags = flags;
   1404       1.49   thorpej 	tc = ccb->ccb_cmd;
   1405       1.49   thorpej 	tc->tc_status = 0;
   1406       1.49   thorpej 	tc->tc_flags = 0;
   1407       1.49   thorpej 	tc->tc_cmdid = ccb->ccb_cmdid;
   1408       1.49   thorpej }
   1409       1.49   thorpej 
   1410       1.49   thorpej struct twe_ccb *
   1411       1.49   thorpej twe_ccb_alloc(struct twe_softc *sc, int flags)
   1412       1.49   thorpej {
   1413        1.1        ad 	struct twe_ccb *ccb;
   1414        1.1        ad 	int s;
   1415        1.1        ad 
   1416       1.63     perry 	s = splbio();
   1417       1.49   thorpej 	if (__predict_false((flags & TWE_CCB_AEN) != 0)) {
   1418       1.47   thorpej 		/* Use the reserved CCB. */
   1419        1.3        ad 		ccb = sc->sc_ccbs;
   1420       1.47   thorpej 	} else {
   1421        1.3        ad 		/* Allocate a CCB and command block. */
   1422       1.49   thorpej 		if (__predict_false((ccb =
   1423       1.49   thorpej 				SLIST_FIRST(&sc->sc_ccb_freelist)) == NULL)) {
   1424        1.1        ad 			splx(s);
   1425       1.49   thorpej 			return (NULL);
   1426        1.1        ad 		}
   1427        1.3        ad 		SLIST_REMOVE_HEAD(&sc->sc_ccb_freelist, ccb_chain.slist);
   1428        1.1        ad 	}
   1429        1.3        ad #ifdef DIAGNOSTIC
   1430       1.50   thorpej 	if ((long)(ccb - sc->sc_ccbs) == 0 && (flags & TWE_CCB_AEN) == 0)
   1431       1.50   thorpej 		panic("twe_ccb_alloc: got reserved CCB for non-AEN");
   1432        1.3        ad 	if ((ccb->ccb_flags & TWE_CCB_ALLOCED) != 0)
   1433       1.50   thorpej 		panic("twe_ccb_alloc: CCB %ld already allocated",
   1434       1.50   thorpej 		    (long)(ccb - sc->sc_ccbs));
   1435        1.3        ad 	flags |= TWE_CCB_ALLOCED;
   1436        1.3        ad #endif
   1437        1.7        ad 	splx(s);
   1438        1.1        ad 
   1439       1.49   thorpej 	twe_ccb_init(sc, ccb, flags);
   1440       1.49   thorpej 	return (ccb);
   1441       1.49   thorpej }
   1442       1.49   thorpej 
   1443       1.49   thorpej struct twe_ccb *
   1444       1.49   thorpej twe_ccb_alloc_wait(struct twe_softc *sc, int flags)
   1445       1.49   thorpej {
   1446       1.49   thorpej 	struct twe_ccb *ccb;
   1447       1.49   thorpej 	int s;
   1448       1.49   thorpej 
   1449       1.49   thorpej 	KASSERT((flags & TWE_CCB_AEN) == 0);
   1450       1.49   thorpej 
   1451       1.49   thorpej 	s = splbio();
   1452       1.49   thorpej 	while (__predict_false((ccb =
   1453       1.49   thorpej 				SLIST_FIRST(&sc->sc_ccb_freelist)) == NULL)) {
   1454       1.49   thorpej 		sc->sc_flags |= TWEF_WAIT_CCB;
   1455       1.49   thorpej 		(void) tsleep(&sc->sc_ccb_freelist, PRIBIO, "tweccb", 0);
   1456       1.49   thorpej 	}
   1457       1.49   thorpej 	SLIST_REMOVE_HEAD(&sc->sc_ccb_freelist, ccb_chain.slist);
   1458       1.49   thorpej #ifdef DIAGNOSTIC
   1459       1.49   thorpej 	if ((ccb->ccb_flags & TWE_CCB_ALLOCED) != 0)
   1460       1.50   thorpej 		panic("twe_ccb_alloc_wait: CCB %ld already allocated",
   1461       1.50   thorpej 		    (long)(ccb - sc->sc_ccbs));
   1462       1.49   thorpej 	flags |= TWE_CCB_ALLOCED;
   1463       1.49   thorpej #endif
   1464       1.49   thorpej 	splx(s);
   1465        1.1        ad 
   1466       1.49   thorpej 	twe_ccb_init(sc, ccb, flags);
   1467       1.49   thorpej 	return (ccb);
   1468        1.1        ad }
   1469        1.1        ad 
   1470        1.1        ad /*
   1471        1.3        ad  * Free a CCB.
   1472        1.1        ad  */
   1473        1.1        ad void
   1474        1.2        ad twe_ccb_free(struct twe_softc *sc, struct twe_ccb *ccb)
   1475        1.1        ad {
   1476        1.1        ad 	int s;
   1477        1.1        ad 
   1478        1.3        ad 	s = splbio();
   1479       1.49   thorpej 	if ((ccb->ccb_flags & TWE_CCB_AEN) == 0) {
   1480        1.3        ad 		SLIST_INSERT_HEAD(&sc->sc_ccb_freelist, ccb, ccb_chain.slist);
   1481       1.49   thorpej 		if (__predict_false((sc->sc_flags & TWEF_WAIT_CCB) != 0)) {
   1482       1.49   thorpej 			sc->sc_flags &= ~TWEF_WAIT_CCB;
   1483       1.49   thorpej 			wakeup(&sc->sc_ccb_freelist);
   1484       1.49   thorpej 		}
   1485       1.49   thorpej 	}
   1486       1.50   thorpej 	ccb->ccb_flags = 0;
   1487        1.1        ad 	splx(s);
   1488        1.1        ad }
   1489        1.1        ad 
   1490        1.1        ad /*
   1491        1.1        ad  * Map the specified CCB's command block and data buffer (if any) into
   1492        1.1        ad  * controller visible space.  Perform DMA synchronisation.
   1493        1.1        ad  */
   1494        1.1        ad int
   1495        1.1        ad twe_ccb_map(struct twe_softc *sc, struct twe_ccb *ccb)
   1496        1.1        ad {
   1497        1.1        ad 	struct twe_cmd *tc;
   1498       1.99    martin 	int flags, nsegs, i, s, rv;
   1499        1.1        ad 	void *data;
   1500        1.1        ad 
   1501        1.7        ad 	/*
   1502        1.7        ad 	 * The data as a whole must be 512-byte aligned.
   1503        1.7        ad 	 */
   1504        1.1        ad 	if (((u_long)ccb->ccb_data & (TWE_ALIGNMENT - 1)) != 0) {
   1505       1.20        ad 		s = splvm();
   1506       1.20        ad 		/* XXX */
   1507       1.99    martin 		rv = uvm_km_kmem_alloc(kmem_va_arena,
   1508       1.96      para 		    ccb->ccb_datasize, (VM_NOSLEEP | VM_INSTANTFIT),
   1509       1.96      para 		    (vmem_addr_t *)&ccb->ccb_abuf);
   1510       1.20        ad 		splx(s);
   1511       1.20        ad 		data = (void *)ccb->ccb_abuf;
   1512        1.2        ad 		if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
   1513        1.2        ad 			memcpy(data, ccb->ccb_data, ccb->ccb_datasize);
   1514        1.1        ad 	} else {
   1515       1.20        ad 		ccb->ccb_abuf = (vaddr_t)0;
   1516        1.1        ad 		data = ccb->ccb_data;
   1517        1.1        ad 	}
   1518        1.1        ad 
   1519        1.7        ad 	/*
   1520        1.7        ad 	 * Map the data buffer into bus space and build the S/G list.
   1521        1.7        ad 	 */
   1522        1.7        ad 	rv = bus_dmamap_load(sc->sc_dmat, ccb->ccb_dmamap_xfer, data,
   1523       1.16   thorpej 	    ccb->ccb_datasize, NULL, BUS_DMA_NOWAIT | BUS_DMA_STREAMING |
   1524       1.16   thorpej 	    ((ccb->ccb_flags & TWE_CCB_DATA_IN) ?
   1525       1.22        ad 	    BUS_DMA_READ : BUS_DMA_WRITE));
   1526        1.7        ad 	if (rv != 0) {
   1527       1.20        ad 		if (ccb->ccb_abuf != (vaddr_t)0) {
   1528       1.20        ad 			s = splvm();
   1529       1.20        ad 			/* XXX */
   1530       1.96      para 			uvm_km_kmem_free(kmem_va_arena, ccb->ccb_abuf,
   1531       1.96      para 			    ccb->ccb_datasize);
   1532       1.20        ad 			splx(s);
   1533        1.7        ad 		}
   1534        1.7        ad 		return (rv);
   1535        1.7        ad 	}
   1536        1.1        ad 
   1537        1.1        ad 	nsegs = ccb->ccb_dmamap_xfer->dm_nsegs;
   1538        1.1        ad 	tc = ccb->ccb_cmd;
   1539        1.1        ad 	tc->tc_size += 2 * nsegs;
   1540        1.1        ad 
   1541       1.95       wiz 	/* The location of the S/G list is dependent upon command type. */
   1542        1.1        ad 	switch (tc->tc_opcode >> 5) {
   1543        1.1        ad 	case 2:
   1544        1.1        ad 		for (i = 0; i < nsegs; i++) {
   1545        1.1        ad 			tc->tc_args.param.sgl[i].tsg_address =
   1546        1.1        ad 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_addr);
   1547        1.1        ad 			tc->tc_args.param.sgl[i].tsg_length =
   1548        1.1        ad 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_len);
   1549        1.1        ad 		}
   1550        1.1        ad 		/* XXX Needed? */
   1551        1.1        ad 		for (; i < TWE_SG_SIZE; i++) {
   1552        1.1        ad 			tc->tc_args.param.sgl[i].tsg_address = 0;
   1553        1.1        ad 			tc->tc_args.param.sgl[i].tsg_length = 0;
   1554        1.1        ad 		}
   1555        1.1        ad 		break;
   1556        1.1        ad 	case 3:
   1557        1.1        ad 		for (i = 0; i < nsegs; i++) {
   1558        1.1        ad 			tc->tc_args.io.sgl[i].tsg_address =
   1559        1.1        ad 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_addr);
   1560        1.1        ad 			tc->tc_args.io.sgl[i].tsg_length =
   1561        1.1        ad 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_len);
   1562        1.1        ad 		}
   1563        1.1        ad 		/* XXX Needed? */
   1564        1.1        ad 		for (; i < TWE_SG_SIZE; i++) {
   1565        1.1        ad 			tc->tc_args.io.sgl[i].tsg_address = 0;
   1566        1.1        ad 			tc->tc_args.io.sgl[i].tsg_length = 0;
   1567        1.1        ad 		}
   1568        1.1        ad 		break;
   1569        1.1        ad 	default:
   1570       1.58   thorpej 		/*
   1571       1.58   thorpej 		 * In all likelihood, this is a command passed from
   1572       1.58   thorpej 		 * management tools in userspace where no S/G list is
   1573       1.58   thorpej 		 * necessary because no data is being passed.
   1574       1.58   thorpej 		 */
   1575       1.58   thorpej 		break;
   1576        1.1        ad 	}
   1577        1.1        ad 
   1578        1.1        ad 	if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
   1579        1.1        ad 		flags = BUS_DMASYNC_PREREAD;
   1580        1.1        ad 	else
   1581        1.1        ad 		flags = 0;
   1582        1.1        ad 	if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
   1583        1.1        ad 		flags |= BUS_DMASYNC_PREWRITE;
   1584        1.1        ad 
   1585        1.1        ad 	bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0,
   1586        1.1        ad 	    ccb->ccb_datasize, flags);
   1587        1.1        ad 	return (0);
   1588        1.1        ad }
   1589        1.1        ad 
   1590        1.1        ad /*
   1591        1.1        ad  * Unmap the specified CCB's command block and data buffer (if any) and
   1592        1.1        ad  * perform DMA synchronisation.
   1593        1.1        ad  */
   1594        1.1        ad void
   1595        1.1        ad twe_ccb_unmap(struct twe_softc *sc, struct twe_ccb *ccb)
   1596        1.1        ad {
   1597       1.20        ad 	int flags, s;
   1598        1.1        ad 
   1599        1.1        ad 	if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
   1600        1.1        ad 		flags = BUS_DMASYNC_POSTREAD;
   1601        1.1        ad 	else
   1602        1.1        ad 		flags = 0;
   1603        1.1        ad 	if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
   1604        1.1        ad 		flags |= BUS_DMASYNC_POSTWRITE;
   1605        1.1        ad 
   1606        1.1        ad 	bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0,
   1607        1.1        ad 	    ccb->ccb_datasize, flags);
   1608        1.1        ad 	bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap_xfer);
   1609        1.1        ad 
   1610       1.20        ad 	if (ccb->ccb_abuf != (vaddr_t)0) {
   1611        1.2        ad 		if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
   1612       1.20        ad 			memcpy(ccb->ccb_data, (void *)ccb->ccb_abuf,
   1613        1.2        ad 			    ccb->ccb_datasize);
   1614       1.20        ad 		s = splvm();
   1615       1.20        ad 		/* XXX */
   1616       1.96      para 		uvm_km_kmem_free(kmem_va_arena, ccb->ccb_abuf,
   1617       1.96      para 		    ccb->ccb_datasize);
   1618       1.20        ad 		splx(s);
   1619        1.1        ad 	}
   1620        1.1        ad }
   1621        1.1        ad 
   1622        1.1        ad /*
   1623        1.7        ad  * Submit a command to the controller and poll on completion.  Return
   1624        1.7        ad  * non-zero on timeout (but don't check status, as some command types don't
   1625        1.7        ad  * return status).  Must be called with interrupts blocked.
   1626        1.1        ad  */
   1627        1.1        ad int
   1628        1.1        ad twe_ccb_poll(struct twe_softc *sc, struct twe_ccb *ccb, int timo)
   1629        1.1        ad {
   1630        1.7        ad 	int rv;
   1631        1.7        ad 
   1632        1.7        ad 	if ((rv = twe_ccb_submit(sc, ccb)) != 0)
   1633        1.7        ad 		return (rv);
   1634        1.1        ad 
   1635       1.15        ad 	for (timo *= 1000; timo != 0; timo--) {
   1636        1.1        ad 		twe_poll(sc);
   1637        1.1        ad 		if ((ccb->ccb_flags & TWE_CCB_COMPLETE) != 0)
   1638        1.1        ad 			break;
   1639       1.15        ad 		DELAY(100);
   1640        1.1        ad 	}
   1641        1.1        ad 
   1642        1.1        ad 	return (timo == 0);
   1643        1.1        ad }
   1644        1.1        ad 
   1645        1.1        ad /*
   1646        1.1        ad  * If a CCB is specified, enqueue it.  Pull CCBs off the software queue in
   1647        1.1        ad  * the order that they were enqueued and try to submit their command blocks
   1648        1.1        ad  * to the controller for execution.
   1649        1.1        ad  */
   1650        1.1        ad void
   1651        1.1        ad twe_ccb_enqueue(struct twe_softc *sc, struct twe_ccb *ccb)
   1652        1.1        ad {
   1653        1.1        ad 	int s;
   1654        1.1        ad 
   1655        1.1        ad 	s = splbio();
   1656        1.1        ad 
   1657        1.1        ad 	if (ccb != NULL)
   1658        1.1        ad 		SIMPLEQ_INSERT_TAIL(&sc->sc_ccb_queue, ccb, ccb_chain.simpleq);
   1659        1.1        ad 
   1660        1.1        ad 	while ((ccb = SIMPLEQ_FIRST(&sc->sc_ccb_queue)) != NULL) {
   1661        1.1        ad 		if (twe_ccb_submit(sc, ccb))
   1662        1.1        ad 			break;
   1663       1.25     lukem 		SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_queue, ccb_chain.simpleq);
   1664        1.1        ad 	}
   1665        1.1        ad 
   1666        1.1        ad 	splx(s);
   1667        1.1        ad }
   1668        1.1        ad 
   1669        1.1        ad /*
   1670        1.1        ad  * Submit the command block associated with the specified CCB to the
   1671        1.1        ad  * controller for execution.  Must be called with interrupts blocked.
   1672        1.1        ad  */
   1673        1.1        ad int
   1674        1.1        ad twe_ccb_submit(struct twe_softc *sc, struct twe_ccb *ccb)
   1675        1.1        ad {
   1676        1.1        ad 	bus_addr_t pa;
   1677        1.1        ad 	int rv;
   1678        1.1        ad 	u_int status;
   1679        1.1        ad 
   1680        1.1        ad 	/* Check to see if we can post a command. */
   1681       1.22        ad 	status = twe_inl(sc, TWE_REG_STS);
   1682        1.1        ad 	twe_status_check(sc, status);
   1683        1.1        ad 
   1684        1.1        ad 	if ((status & TWE_STS_CMD_QUEUE_FULL) == 0) {
   1685        1.1        ad 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
   1686       1.83  christos 		    (char *)ccb->ccb_cmd - (char *)sc->sc_cmds,
   1687       1.83  christos 		    sizeof(struct twe_cmd),
   1688        1.1        ad 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1689       1.50   thorpej #ifdef DIAGNOSTIC
   1690       1.50   thorpej 		if ((ccb->ccb_flags & TWE_CCB_ALLOCED) == 0)
   1691       1.50   thorpej 			panic("%s: CCB %ld not ALLOCED\n",
   1692       1.97       chs 			    device_xname(sc->sc_dev), (long)(ccb - sc->sc_ccbs));
   1693       1.50   thorpej #endif
   1694        1.1        ad 		ccb->ccb_flags |= TWE_CCB_ACTIVE;
   1695        1.1        ad 		pa = sc->sc_cmds_paddr +
   1696        1.1        ad 		    ccb->ccb_cmdid * sizeof(struct twe_cmd);
   1697       1.22        ad 		twe_outl(sc, TWE_REG_CMD_QUEUE, (u_int32_t)pa);
   1698        1.1        ad 		rv = 0;
   1699        1.1        ad 	} else
   1700        1.1        ad 		rv = EBUSY;
   1701        1.1        ad 
   1702        1.1        ad 	return (rv);
   1703       1.33  christos }
   1704       1.33  christos 
   1705       1.33  christos 
   1706       1.33  christos /*
   1707       1.33  christos  * Accept an open operation on the control device.
   1708       1.33  christos  */
   1709       1.68   thorpej static int
   1710       1.81  christos tweopen(dev_t dev, int flag, int mode, struct lwp *l)
   1711       1.33  christos {
   1712       1.33  christos 	struct twe_softc *twe;
   1713       1.33  christos 
   1714       1.87   tsutsui 	if ((twe = device_lookup_private(&twe_cd, minor(dev))) == NULL)
   1715       1.33  christos 		return (ENXIO);
   1716       1.33  christos 	if ((twe->sc_flags & TWEF_OPEN) != 0)
   1717       1.33  christos 		return (EBUSY);
   1718       1.33  christos 
   1719       1.33  christos 	twe->sc_flags |= TWEF_OPEN;
   1720       1.33  christos 	return (0);
   1721       1.33  christos }
   1722       1.33  christos 
   1723       1.33  christos /*
   1724       1.33  christos  * Accept the last close on the control device.
   1725       1.33  christos  */
   1726       1.68   thorpej static int
   1727       1.81  christos tweclose(dev_t dev, int flag, int mode,
   1728       1.81  christos     struct lwp *l)
   1729       1.33  christos {
   1730       1.33  christos 	struct twe_softc *twe;
   1731       1.33  christos 
   1732       1.87   tsutsui 	twe = device_lookup_private(&twe_cd, minor(dev));
   1733       1.33  christos 	twe->sc_flags &= ~TWEF_OPEN;
   1734       1.33  christos 	return (0);
   1735       1.33  christos }
   1736       1.33  christos 
   1737       1.56   thorpej void
   1738       1.81  christos twe_ccb_wait_handler(struct twe_ccb *ccb, int error)
   1739       1.54   thorpej {
   1740       1.54   thorpej 
   1741       1.54   thorpej 	/* Just wake up the sleeper. */
   1742       1.54   thorpej 	wakeup(ccb);
   1743       1.54   thorpej }
   1744       1.54   thorpej 
   1745       1.33  christos /*
   1746       1.33  christos  * Handle control operations.
   1747       1.33  christos  */
   1748       1.68   thorpej static int
   1749  1.103.4.2     skrll tweioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
   1750       1.33  christos {
   1751       1.33  christos 	struct twe_softc *twe;
   1752       1.33  christos 	struct twe_ccb *ccb;
   1753       1.33  christos 	struct twe_param *param;
   1754       1.33  christos 	struct twe_usercommand *tu;
   1755       1.33  christos 	struct twe_paramcommand *tp;
   1756       1.50   thorpej 	struct twe_drivecommand *td;
   1757       1.33  christos 	void *pdata = NULL;
   1758       1.51   thorpej 	int s, error = 0;
   1759       1.33  christos 	u_int8_t cmdid;
   1760       1.33  christos 
   1761       1.87   tsutsui 	twe = device_lookup_private(&twe_cd, minor(dev));
   1762       1.33  christos 	tu = (struct twe_usercommand *)data;
   1763       1.33  christos 	tp = (struct twe_paramcommand *)data;
   1764       1.50   thorpej 	td = (struct twe_drivecommand *)data;
   1765       1.33  christos 
   1766       1.51   thorpej 	/* This is intended to be compatible with the FreeBSD interface. */
   1767       1.33  christos 	switch (cmd) {
   1768       1.33  christos 	case TWEIO_COMMAND:
   1769       1.82      elad 		error = kauth_authorize_device_passthru(l->l_cred, dev,
   1770       1.82      elad 		    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_ALL, data);
   1771       1.80      elad 		if (error)
   1772       1.80      elad 			return (error);
   1773       1.76  christos 
   1774       1.54   thorpej 		/* XXX mutex */
   1775       1.33  christos 		if (tu->tu_size > 0) {
   1776       1.51   thorpej 			/*
   1777       1.51   thorpej 			 * XXX Handle > TWE_SECTOR_SIZE?  Let's see if
   1778       1.51   thorpej 			 * it's really necessary, first.
   1779       1.51   thorpej 			 */
   1780       1.51   thorpej 			if (tu->tu_size > TWE_SECTOR_SIZE) {
   1781       1.51   thorpej #ifdef TWE_DEBUG
   1782       1.91  jakllsch 				printf("%s: TWEIO_COMMAND: tu_size = %zu\n",
   1783       1.97       chs 				    device_xname(twe->sc_dev), tu->tu_size);
   1784       1.51   thorpej #endif
   1785       1.33  christos 				return EINVAL;
   1786       1.51   thorpej 			}
   1787       1.53   thorpej 			pdata = malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_WAITOK);
   1788       1.33  christos 			error = copyin(tu->tu_data, pdata, tu->tu_size);
   1789       1.33  christos 			if (error != 0)
   1790       1.33  christos 				goto done;
   1791       1.51   thorpej 			ccb = twe_ccb_alloc_wait(twe,
   1792       1.33  christos 			    TWE_CCB_DATA_IN | TWE_CCB_DATA_OUT);
   1793       1.51   thorpej 			KASSERT(ccb != NULL);
   1794       1.33  christos 			ccb->ccb_data = pdata;
   1795       1.33  christos 			ccb->ccb_datasize = TWE_SECTOR_SIZE;
   1796       1.51   thorpej 		} else {
   1797       1.51   thorpej 			ccb = twe_ccb_alloc_wait(twe, 0);
   1798       1.51   thorpej 			KASSERT(ccb != NULL);
   1799       1.33  christos 		}
   1800       1.54   thorpej 
   1801       1.56   thorpej 		ccb->ccb_tx.tx_handler = twe_ccb_wait_handler;
   1802       1.54   thorpej 		ccb->ccb_tx.tx_context = NULL;
   1803       1.97       chs 		ccb->ccb_tx.tx_dv = twe->sc_dev;
   1804       1.54   thorpej 
   1805       1.51   thorpej 		cmdid = ccb->ccb_cmdid;
   1806       1.51   thorpej 		memcpy(ccb->ccb_cmd, &tu->tu_cmd, sizeof(struct twe_cmd));
   1807       1.51   thorpej 		ccb->ccb_cmd->tc_cmdid = cmdid;
   1808       1.51   thorpej 
   1809       1.33  christos 		/* Map the transfer. */
   1810       1.33  christos 		if ((error = twe_ccb_map(twe, ccb)) != 0) {
   1811       1.33  christos 			twe_ccb_free(twe, ccb);
   1812       1.33  christos 			goto done;
   1813       1.33  christos 		}
   1814       1.33  christos 
   1815       1.54   thorpej 		/* Submit the command and wait up to 1 minute. */
   1816       1.54   thorpej 		error = 0;
   1817       1.54   thorpej 		twe_ccb_enqueue(twe, ccb);
   1818       1.33  christos 		s = splbio();
   1819       1.54   thorpej 		while ((ccb->ccb_flags & TWE_CCB_COMPLETE) == 0)
   1820       1.54   thorpej 			if ((error = tsleep(ccb, PRIBIO, "tweioctl",
   1821       1.54   thorpej 					    60 * hz)) != 0)
   1822       1.54   thorpej 				break;
   1823       1.51   thorpej 		splx(s);
   1824       1.51   thorpej 
   1825       1.51   thorpej 		/* Copy the command back to the ioctl argument. */
   1826       1.51   thorpej 		memcpy(&tu->tu_cmd, ccb->ccb_cmd, sizeof(struct twe_cmd));
   1827       1.51   thorpej #ifdef TWE_DEBUG
   1828       1.51   thorpej 		printf("%s: TWEIO_COMMAND: tc_opcode = 0x%02x, "
   1829       1.97       chs 		    "tc_status = 0x%02x\n", device_xname(twe->sc_dev),
   1830       1.51   thorpej 		    tu->tu_cmd.tc_opcode, tu->tu_cmd.tc_status);
   1831       1.51   thorpej #endif
   1832       1.51   thorpej 
   1833       1.51   thorpej 		s = splbio();
   1834       1.33  christos 		twe_ccb_free(twe, ccb);
   1835       1.33  christos 		splx(s);
   1836       1.33  christos 
   1837       1.33  christos 		if (tu->tu_size > 0)
   1838       1.33  christos 			error = copyout(pdata, tu->tu_data, tu->tu_size);
   1839       1.33  christos 		goto done;
   1840       1.33  christos 
   1841       1.33  christos 	case TWEIO_STATS:
   1842       1.33  christos 		return (ENOENT);
   1843       1.33  christos 
   1844       1.33  christos 	case TWEIO_AEN_POLL:
   1845       1.45   thorpej 		s = splbio();
   1846       1.45   thorpej 		*(u_int *)data = twe_aen_dequeue(twe);
   1847       1.45   thorpej 		splx(s);
   1848       1.33  christos 		return (0);
   1849       1.33  christos 
   1850       1.33  christos 	case TWEIO_AEN_WAIT:
   1851       1.33  christos 		s = splbio();
   1852       1.45   thorpej 		while ((*(u_int *)data =
   1853       1.45   thorpej 		    twe_aen_dequeue(twe)) == TWE_AEN_QUEUE_EMPTY) {
   1854       1.45   thorpej 			twe->sc_flags |= TWEF_AENQ_WAIT;
   1855       1.45   thorpej 			error = tsleep(&twe->sc_aen_queue, PRIBIO | PCATCH,
   1856       1.45   thorpej 			    "tweaen", 0);
   1857       1.45   thorpej 			if (error == EINTR) {
   1858       1.45   thorpej 				splx(s);
   1859       1.45   thorpej 				return (error);
   1860       1.45   thorpej 			}
   1861       1.33  christos 		}
   1862       1.33  christos 		splx(s);
   1863       1.33  christos 		return (0);
   1864       1.33  christos 
   1865       1.33  christos 	case TWEIO_GET_PARAM:
   1866       1.33  christos 		error = twe_param_get(twe, tp->tp_table_id, tp->tp_param_id,
   1867       1.39  jdolecek 		    tp->tp_size, 0, &param);
   1868       1.33  christos 		if (error != 0)
   1869       1.33  christos 			return (error);
   1870       1.33  christos 		if (param->tp_param_size > tp->tp_size) {
   1871       1.33  christos 			error = EFAULT;
   1872       1.33  christos 			goto done;
   1873       1.33  christos 		}
   1874       1.63     perry 		error = copyout(param->tp_data, tp->tp_data,
   1875       1.33  christos 		    param->tp_param_size);
   1876       1.74    simonb 		free(param, M_DEVBUF);
   1877       1.33  christos 		goto done;
   1878       1.33  christos 
   1879       1.33  christos 	case TWEIO_SET_PARAM:
   1880       1.33  christos 		pdata = malloc(tp->tp_size, M_DEVBUF, M_WAITOK);
   1881       1.33  christos 		if ((error = copyin(tp->tp_data, pdata, tp->tp_size)) != 0)
   1882       1.33  christos 			goto done;
   1883       1.33  christos 		error = twe_param_set(twe, tp->tp_table_id, tp->tp_param_id,
   1884       1.33  christos 		    tp->tp_size, pdata);
   1885       1.33  christos 		goto done;
   1886       1.33  christos 
   1887       1.33  christos 	case TWEIO_RESET:
   1888       1.47   thorpej 		s = splbio();
   1889       1.33  christos 		twe_reset(twe);
   1890       1.47   thorpej 		splx(s);
   1891       1.33  christos 		return (0);
   1892       1.46   thorpej 
   1893       1.46   thorpej 	case TWEIO_ADD_UNIT:
   1894       1.46   thorpej 		/* XXX mutex */
   1895       1.50   thorpej 		return (twe_add_unit(twe, td->td_unit));
   1896       1.46   thorpej 
   1897       1.46   thorpej 	case TWEIO_DEL_UNIT:
   1898       1.46   thorpej 		/* XXX mutex */
   1899       1.50   thorpej 		return (twe_del_unit(twe, td->td_unit));
   1900       1.33  christos 
   1901       1.33  christos 	default:
   1902       1.33  christos 		return EINVAL;
   1903       1.33  christos 	}
   1904       1.33  christos done:
   1905       1.33  christos 	if (pdata)
   1906       1.33  christos 		free(pdata, M_DEVBUF);
   1907       1.33  christos 	return error;
   1908       1.38  jdolecek }
   1909       1.38  jdolecek 
   1910       1.68   thorpej const struct cdevsw twe_cdevsw = {
   1911      1.101  dholland 	.d_open = tweopen,
   1912      1.101  dholland 	.d_close = tweclose,
   1913      1.101  dholland 	.d_read = noread,
   1914      1.101  dholland 	.d_write = nowrite,
   1915      1.101  dholland 	.d_ioctl = tweioctl,
   1916      1.101  dholland 	.d_stop = nostop,
   1917      1.101  dholland 	.d_tty = notty,
   1918      1.101  dholland 	.d_poll = nopoll,
   1919      1.101  dholland 	.d_mmap = nommap,
   1920      1.101  dholland 	.d_kqfilter = nokqfilter,
   1921      1.103  dholland 	.d_discard = nodiscard,
   1922      1.101  dholland 	.d_flag = D_OTHER
   1923       1.68   thorpej };
   1924       1.68   thorpej 
   1925       1.38  jdolecek /*
   1926       1.38  jdolecek  * Print some information about the controller
   1927       1.38  jdolecek  */
   1928       1.38  jdolecek static void
   1929       1.38  jdolecek twe_describe_controller(struct twe_softc *sc)
   1930       1.38  jdolecek {
   1931       1.41   thorpej 	struct twe_param *p[6];
   1932       1.44   thorpej 	int i, rv = 0;
   1933       1.44   thorpej 	uint32_t dsize;
   1934       1.41   thorpej 	uint8_t ports;
   1935       1.41   thorpej 
   1936       1.77   xtraeme 	ports = 0;
   1937       1.77   xtraeme 
   1938       1.38  jdolecek 	/* get the port count */
   1939       1.41   thorpej 	rv |= twe_param_get_1(sc, TWE_PARAM_CONTROLLER,
   1940       1.41   thorpej 		TWE_PARAM_CONTROLLER_PortCount, &ports);
   1941       1.38  jdolecek 
   1942       1.38  jdolecek 	/* get version strings */
   1943       1.38  jdolecek 	rv |= twe_param_get(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_Mon,
   1944       1.41   thorpej 		16, NULL, &p[0]);
   1945       1.41   thorpej 	rv |= twe_param_get(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_FW,
   1946       1.38  jdolecek 		16, NULL, &p[1]);
   1947       1.41   thorpej 	rv |= twe_param_get(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_BIOS,
   1948       1.38  jdolecek 		16, NULL, &p[2]);
   1949       1.38  jdolecek 	rv |= twe_param_get(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_PCB,
   1950       1.41   thorpej 		8, NULL, &p[3]);
   1951       1.41   thorpej 	rv |= twe_param_get(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_ATA,
   1952       1.38  jdolecek 		8, NULL, &p[4]);
   1953       1.41   thorpej 	rv |= twe_param_get(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_PCI,
   1954       1.38  jdolecek 		8, NULL, &p[5]);
   1955       1.38  jdolecek 
   1956       1.38  jdolecek 	if (rv) {
   1957       1.38  jdolecek 		/* some error occurred */
   1958  1.103.4.2     skrll 		aprint_error_dev(sc->sc_dev,
   1959  1.103.4.2     skrll 		    "failed to fetch version information\n");
   1960       1.38  jdolecek 		return;
   1961       1.38  jdolecek 	}
   1962       1.38  jdolecek 
   1963       1.97       chs 	aprint_normal_dev(sc->sc_dev, "%d ports, Firmware %.16s, BIOS %.16s\n",
   1964  1.103.4.2     skrll 	    ports, p[1]->tp_data, p[2]->tp_data);
   1965       1.38  jdolecek 
   1966  1.103.4.2     skrll 	aprint_verbose_dev(sc->sc_dev,
   1967  1.103.4.2     skrll 	    "Monitor %.16s, PCB %.8s, Achip %.8s, Pchip %.8s\n",
   1968  1.103.4.2     skrll 	    p[0]->tp_data, p[3]->tp_data,
   1969  1.103.4.2     skrll 	    p[4]->tp_data, p[5]->tp_data);
   1970       1.38  jdolecek 
   1971       1.38  jdolecek 	free(p[0], M_DEVBUF);
   1972       1.38  jdolecek 	free(p[1], M_DEVBUF);
   1973       1.38  jdolecek 	free(p[2], M_DEVBUF);
   1974       1.38  jdolecek 	free(p[3], M_DEVBUF);
   1975       1.38  jdolecek 	free(p[4], M_DEVBUF);
   1976       1.38  jdolecek 	free(p[5], M_DEVBUF);
   1977       1.44   thorpej 
   1978       1.44   thorpej 	rv = twe_param_get(sc, TWE_PARAM_DRIVESUMMARY,
   1979       1.44   thorpej 	    TWE_PARAM_DRIVESUMMARY_Status, 16, NULL, &p[0]);
   1980       1.44   thorpej 	if (rv) {
   1981  1.103.4.2     skrll 		aprint_error_dev(sc->sc_dev,
   1982  1.103.4.2     skrll 		    "failed to get drive status summary\n");
   1983       1.44   thorpej 		return;
   1984       1.44   thorpej 	}
   1985       1.44   thorpej 	for (i = 0; i < ports; i++) {
   1986       1.44   thorpej 		if (p[0]->tp_data[i] != TWE_PARAM_DRIVESTATUS_Present)
   1987       1.44   thorpej 			continue;
   1988       1.44   thorpej 		rv = twe_param_get_4(sc, TWE_PARAM_DRIVEINFO + i,
   1989       1.44   thorpej 		    TWE_PARAM_DRIVEINFO_Size, &dsize);
   1990       1.44   thorpej 		if (rv) {
   1991  1.103.4.2     skrll 			aprint_error_dev(sc->sc_dev,
   1992       1.85    cegger 			    "unable to get drive size for port %d\n", i);
   1993       1.44   thorpej 			continue;
   1994       1.44   thorpej 		}
   1995       1.44   thorpej 		rv = twe_param_get(sc, TWE_PARAM_DRIVEINFO + i,
   1996       1.44   thorpej 		    TWE_PARAM_DRIVEINFO_Model, 40, NULL, &p[1]);
   1997       1.44   thorpej 		if (rv) {
   1998       1.97       chs 			aprint_error_dev(sc->sc_dev,
   1999       1.85    cegger 			    "unable to get drive model for port %d\n", i);
   2000       1.44   thorpej 			continue;
   2001       1.44   thorpej 		}
   2002       1.97       chs 		aprint_verbose_dev(sc->sc_dev, "port %d: %.40s %d MB\n",
   2003       1.44   thorpej 		    i, p[1]->tp_data, dsize / 2048);
   2004       1.44   thorpej 		free(p[1], M_DEVBUF);
   2005       1.44   thorpej 	}
   2006       1.44   thorpej 	free(p[0], M_DEVBUF);
   2007        1.1        ad }
   2008  1.103.4.2     skrll 
   2009  1.103.4.2     skrll MODULE(MODULE_CLASS_DRIVER, twe, "pci");
   2010  1.103.4.2     skrll 
   2011  1.103.4.2     skrll #ifdef _MODULE
   2012  1.103.4.2     skrll #include "ioconf.c"
   2013  1.103.4.2     skrll #endif
   2014  1.103.4.2     skrll 
   2015  1.103.4.2     skrll static int
   2016  1.103.4.2     skrll twe_modcmd(modcmd_t cmd, void *opaque)
   2017  1.103.4.2     skrll {
   2018  1.103.4.2     skrll 	int error = 0;
   2019  1.103.4.2     skrll 
   2020  1.103.4.2     skrll #ifdef _MODULE
   2021  1.103.4.2     skrll 	switch (cmd) {
   2022  1.103.4.2     skrll 	case MODULE_CMD_INIT:
   2023  1.103.4.2     skrll 		error = config_init_component(cfdriver_ioconf_twe,
   2024  1.103.4.2     skrll 		    cfattach_ioconf_twe, cfdata_ioconf_twe);
   2025  1.103.4.2     skrll 		break;
   2026  1.103.4.2     skrll 	case MODULE_CMD_FINI:
   2027  1.103.4.2     skrll 		error = config_fini_component(cfdriver_ioconf_twe,
   2028  1.103.4.2     skrll 		    cfattach_ioconf_twe, cfdata_ioconf_twe);
   2029  1.103.4.2     skrll 		break;
   2030  1.103.4.2     skrll 	default:
   2031  1.103.4.2     skrll 		error = ENOTTY;
   2032  1.103.4.2     skrll 		break;
   2033  1.103.4.2     skrll 	}
   2034  1.103.4.2     skrll #endif
   2035  1.103.4.2     skrll 
   2036  1.103.4.2     skrll 	return error;
   2037  1.103.4.2     skrll }
   2038