11ac4b82bSMike Smith /*- 21ac4b82bSMike Smith * Copyright (c) 1999 Michael Smith 31ac4b82bSMike Smith * All rights reserved. 41ac4b82bSMike Smith * 51ac4b82bSMike Smith * Redistribution and use in source and binary forms, with or without 61ac4b82bSMike Smith * modification, are permitted provided that the following conditions 71ac4b82bSMike Smith * are met: 81ac4b82bSMike Smith * 1. Redistributions of source code must retain the above copyright 91ac4b82bSMike Smith * notice, this list of conditions and the following disclaimer. 101ac4b82bSMike Smith * 2. Redistributions in binary form must reproduce the above copyright 111ac4b82bSMike Smith * notice, this list of conditions and the following disclaimer in the 121ac4b82bSMike Smith * documentation and/or other materials provided with the distribution. 131ac4b82bSMike Smith * 141ac4b82bSMike Smith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 151ac4b82bSMike Smith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 161ac4b82bSMike Smith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 171ac4b82bSMike Smith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 181ac4b82bSMike Smith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 191ac4b82bSMike Smith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 201ac4b82bSMike Smith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 211ac4b82bSMike Smith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 221ac4b82bSMike Smith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 231ac4b82bSMike Smith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 241ac4b82bSMike Smith * SUCH DAMAGE. 251ac4b82bSMike Smith * 261ac4b82bSMike Smith * $FreeBSD$ 271ac4b82bSMike Smith */ 281ac4b82bSMike Smith 291ac4b82bSMike Smith /* 301ac4b82bSMike Smith * Driver for the Mylex DAC960 family of RAID controllers. 311ac4b82bSMike Smith */ 321ac4b82bSMike Smith 331ac4b82bSMike Smith #include <sys/param.h> 341ac4b82bSMike Smith #include <sys/systm.h> 351ac4b82bSMike Smith #include <sys/malloc.h> 361ac4b82bSMike Smith #include <sys/kernel.h> 371ac4b82bSMike Smith 381ac4b82bSMike Smith #include <sys/bus.h> 391ac4b82bSMike Smith #include <sys/conf.h> 40da8bb3a3SMike Smith #include <sys/stat.h> 411ac4b82bSMike Smith 421ac4b82bSMike Smith #include <machine/resource.h> 431ac4b82bSMike Smith #include <machine/bus.h> 441ac4b82bSMike Smith #include <machine/clock.h> 451ac4b82bSMike Smith #include <sys/rman.h> 461ac4b82bSMike Smith 47891619a6SPoul-Henning Kamp #include <geom/geom_disk.h> 48891619a6SPoul-Henning Kamp 4915fd5d22SMike Smith #include <dev/mlx/mlx_compat.h> 501ac4b82bSMike Smith #include <dev/mlx/mlxio.h> 511ac4b82bSMike Smith #include <dev/mlx/mlxvar.h> 521ac4b82bSMike Smith #include <dev/mlx/mlxreg.h> 531ac4b82bSMike Smith 541ac4b82bSMike Smith static struct cdevsw mlx_cdevsw = { 55dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 56dc08ffecSPoul-Henning Kamp .d_flags = D_NEEDGIANT, 577ac40f5fSPoul-Henning Kamp .d_open = mlx_open, 587ac40f5fSPoul-Henning Kamp .d_close = mlx_close, 597ac40f5fSPoul-Henning Kamp .d_ioctl = mlx_ioctl, 607ac40f5fSPoul-Henning Kamp .d_name = "mlx", 611ac4b82bSMike Smith }; 621ac4b82bSMike Smith 631ac4b82bSMike Smith devclass_t mlx_devclass; 641ac4b82bSMike Smith 651ac4b82bSMike Smith /* 661ac4b82bSMike Smith * Per-interface accessor methods 671ac4b82bSMike Smith */ 681ac4b82bSMike Smith static int mlx_v3_tryqueue(struct mlx_softc *sc, struct mlx_command *mc); 691ac4b82bSMike Smith static int mlx_v3_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status); 701ac4b82bSMike Smith static void mlx_v3_intaction(struct mlx_softc *sc, int action); 71da8bb3a3SMike Smith static int mlx_v3_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2); 721ac4b82bSMike Smith 73f6b84b08SMike Smith static int mlx_v4_tryqueue(struct mlx_softc *sc, struct mlx_command *mc); 74f6b84b08SMike Smith static int mlx_v4_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status); 75f6b84b08SMike Smith static void mlx_v4_intaction(struct mlx_softc *sc, int action); 76da8bb3a3SMike Smith static int mlx_v4_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2); 77f6b84b08SMike Smith 785792b7feSMike Smith static int mlx_v5_tryqueue(struct mlx_softc *sc, struct mlx_command *mc); 795792b7feSMike Smith static int mlx_v5_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status); 805792b7feSMike Smith static void mlx_v5_intaction(struct mlx_softc *sc, int action); 81da8bb3a3SMike Smith static int mlx_v5_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2); 825792b7feSMike Smith 831ac4b82bSMike Smith /* 841ac4b82bSMike Smith * Status monitoring 851ac4b82bSMike Smith */ 861ac4b82bSMike Smith static void mlx_periodic(void *data); 871ac4b82bSMike Smith static void mlx_periodic_enquiry(struct mlx_command *mc); 881ac4b82bSMike Smith static void mlx_periodic_eventlog_poll(struct mlx_softc *sc); 891ac4b82bSMike Smith static void mlx_periodic_eventlog_respond(struct mlx_command *mc); 901ac4b82bSMike Smith static void mlx_periodic_rebuild(struct mlx_command *mc); 911ac4b82bSMike Smith 921ac4b82bSMike Smith /* 931ac4b82bSMike Smith * Channel Pause 941ac4b82bSMike Smith */ 951ac4b82bSMike Smith static void mlx_pause_action(struct mlx_softc *sc); 961ac4b82bSMike Smith static void mlx_pause_done(struct mlx_command *mc); 971ac4b82bSMike Smith 981ac4b82bSMike Smith /* 991ac4b82bSMike Smith * Command submission. 1001ac4b82bSMike Smith */ 1011ac4b82bSMike Smith static void *mlx_enquire(struct mlx_softc *sc, int command, size_t bufsize, 1021ac4b82bSMike Smith void (*complete)(struct mlx_command *mc)); 1031ac4b82bSMike Smith static int mlx_flush(struct mlx_softc *sc); 104421f2f7dSMike Smith static int mlx_check(struct mlx_softc *sc, int drive); 1051ac4b82bSMike Smith static int mlx_rebuild(struct mlx_softc *sc, int channel, int target); 1061ac4b82bSMike Smith static int mlx_wait_command(struct mlx_command *mc); 1071ac4b82bSMike Smith static int mlx_poll_command(struct mlx_command *mc); 1081b4404f9SScott Long void mlx_startio_cb(void *arg, 1091b4404f9SScott Long bus_dma_segment_t *segs, 1101b4404f9SScott Long int nsegments, int error); 1111ac4b82bSMike Smith static void mlx_startio(struct mlx_softc *sc); 1121ac4b82bSMike Smith static void mlx_completeio(struct mlx_command *mc); 1131b4404f9SScott Long static int mlx_user_command(struct mlx_softc *sc, 1141b4404f9SScott Long struct mlx_usercommand *mu); 1151b4404f9SScott Long void mlx_user_cb(void *arg, bus_dma_segment_t *segs, 1161b4404f9SScott Long int nsegments, int error); 1171ac4b82bSMike Smith 1181ac4b82bSMike Smith /* 1191ac4b82bSMike Smith * Command buffer allocation. 1201ac4b82bSMike Smith */ 1211ac4b82bSMike Smith static struct mlx_command *mlx_alloccmd(struct mlx_softc *sc); 1221ac4b82bSMike Smith static void mlx_releasecmd(struct mlx_command *mc); 1231ac4b82bSMike Smith static void mlx_freecmd(struct mlx_command *mc); 1241ac4b82bSMike Smith 1251ac4b82bSMike Smith /* 1261ac4b82bSMike Smith * Command management. 1271ac4b82bSMike Smith */ 1281ac4b82bSMike Smith static int mlx_getslot(struct mlx_command *mc); 1291b4404f9SScott Long static void mlx_setup_dmamap(struct mlx_command *mc, 1301b4404f9SScott Long bus_dma_segment_t *segs, 1311b4404f9SScott Long int nsegments, int error); 1321ac4b82bSMike Smith static void mlx_unmapcmd(struct mlx_command *mc); 1331ac4b82bSMike Smith static int mlx_start(struct mlx_command *mc); 1341ac4b82bSMike Smith static int mlx_done(struct mlx_softc *sc); 1351ac4b82bSMike Smith static void mlx_complete(struct mlx_softc *sc); 1361ac4b82bSMike Smith 1371ac4b82bSMike Smith /* 1381ac4b82bSMike Smith * Debugging. 1391ac4b82bSMike Smith */ 1401ac4b82bSMike Smith static char *mlx_diagnose_command(struct mlx_command *mc); 1419eee27f1SMike Smith static void mlx_describe_controller(struct mlx_softc *sc); 142da8bb3a3SMike Smith static int mlx_fw_message(struct mlx_softc *sc, int status, int param1, int param2); 1431ac4b82bSMike Smith 1441ac4b82bSMike Smith /* 1451ac4b82bSMike Smith * Utility functions. 1461ac4b82bSMike Smith */ 1471ac4b82bSMike Smith static struct mlx_sysdrive *mlx_findunit(struct mlx_softc *sc, int unit); 1481ac4b82bSMike Smith 1491ac4b82bSMike Smith /******************************************************************************** 1501ac4b82bSMike Smith ******************************************************************************** 1511ac4b82bSMike Smith Public Interfaces 1521ac4b82bSMike Smith ******************************************************************************** 1531ac4b82bSMike Smith ********************************************************************************/ 1541ac4b82bSMike Smith 1551ac4b82bSMike Smith /******************************************************************************** 1561ac4b82bSMike Smith * Free all of the resources associated with (sc) 1571ac4b82bSMike Smith * 1581ac4b82bSMike Smith * Should not be called if the controller is active. 1591ac4b82bSMike Smith */ 1601ac4b82bSMike Smith void 1611ac4b82bSMike Smith mlx_free(struct mlx_softc *sc) 1621ac4b82bSMike Smith { 1631ac4b82bSMike Smith struct mlx_command *mc; 1641ac4b82bSMike Smith 165da8bb3a3SMike Smith debug_called(1); 1661ac4b82bSMike Smith 1671ac4b82bSMike Smith /* cancel status timeout */ 1681ac4b82bSMike Smith untimeout(mlx_periodic, sc, sc->mlx_timeout); 1691ac4b82bSMike Smith 1701ac4b82bSMike Smith /* throw away any command buffers */ 1711ac4b82bSMike Smith while ((mc = TAILQ_FIRST(&sc->mlx_freecmds)) != NULL) { 1721ac4b82bSMike Smith TAILQ_REMOVE(&sc->mlx_freecmds, mc, mc_link); 1731ac4b82bSMike Smith mlx_freecmd(mc); 1741ac4b82bSMike Smith } 1751ac4b82bSMike Smith 1761ac4b82bSMike Smith /* destroy data-transfer DMA tag */ 1771ac4b82bSMike Smith if (sc->mlx_buffer_dmat) 1781ac4b82bSMike Smith bus_dma_tag_destroy(sc->mlx_buffer_dmat); 1791ac4b82bSMike Smith 1801ac4b82bSMike Smith /* free and destroy DMA memory and tag for s/g lists */ 1811ac4b82bSMike Smith if (sc->mlx_sgtable) 1821ac4b82bSMike Smith bus_dmamem_free(sc->mlx_sg_dmat, sc->mlx_sgtable, sc->mlx_sg_dmamap); 1831ac4b82bSMike Smith if (sc->mlx_sg_dmat) 1841ac4b82bSMike Smith bus_dma_tag_destroy(sc->mlx_sg_dmat); 1851ac4b82bSMike Smith 1861ac4b82bSMike Smith /* disconnect the interrupt handler */ 1871ac4b82bSMike Smith if (sc->mlx_intr) 1881ac4b82bSMike Smith bus_teardown_intr(sc->mlx_dev, sc->mlx_irq, sc->mlx_intr); 1891ac4b82bSMike Smith if (sc->mlx_irq != NULL) 1901ac4b82bSMike Smith bus_release_resource(sc->mlx_dev, SYS_RES_IRQ, 0, sc->mlx_irq); 1911ac4b82bSMike Smith 1921ac4b82bSMike Smith /* destroy the parent DMA tag */ 1931ac4b82bSMike Smith if (sc->mlx_parent_dmat) 1941ac4b82bSMike Smith bus_dma_tag_destroy(sc->mlx_parent_dmat); 1951ac4b82bSMike Smith 1961ac4b82bSMike Smith /* release the register window mapping */ 1971ac4b82bSMike Smith if (sc->mlx_mem != NULL) 1989b11c7baSMatthew N. Dodd bus_release_resource(sc->mlx_dev, sc->mlx_mem_type, sc->mlx_mem_rid, sc->mlx_mem); 1999eee27f1SMike Smith 2009eee27f1SMike Smith /* free controller enquiry data */ 2019eee27f1SMike Smith if (sc->mlx_enq2 != NULL) 2029eee27f1SMike Smith free(sc->mlx_enq2, M_DEVBUF); 203da8bb3a3SMike Smith 204da8bb3a3SMike Smith /* destroy control device */ 20589c9c53dSPoul-Henning Kamp if (sc->mlx_dev_t != (struct cdev *)NULL) 206da8bb3a3SMike Smith destroy_dev(sc->mlx_dev_t); 2071ac4b82bSMike Smith } 2081ac4b82bSMike Smith 2091ac4b82bSMike Smith /******************************************************************************** 2101ac4b82bSMike Smith * Map the scatter/gather table into bus space 2111ac4b82bSMike Smith */ 2121ac4b82bSMike Smith static void 2131ac4b82bSMike Smith mlx_dma_map_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error) 2141ac4b82bSMike Smith { 2151ac4b82bSMike Smith struct mlx_softc *sc = (struct mlx_softc *)arg; 2161ac4b82bSMike Smith 217da8bb3a3SMike Smith debug_called(1); 2181ac4b82bSMike Smith 2191ac4b82bSMike Smith /* save base of s/g table's address in bus space */ 2201ac4b82bSMike Smith sc->mlx_sgbusaddr = segs->ds_addr; 2211ac4b82bSMike Smith } 2221ac4b82bSMike Smith 2231ac4b82bSMike Smith static int 2241ac4b82bSMike Smith mlx_sglist_map(struct mlx_softc *sc) 2251ac4b82bSMike Smith { 2261ac4b82bSMike Smith size_t segsize; 227baff09dbSMike Smith int error, ncmd; 2281ac4b82bSMike Smith 229da8bb3a3SMike Smith debug_called(1); 2301ac4b82bSMike Smith 2311ac4b82bSMike Smith /* destroy any existing mappings */ 2321ac4b82bSMike Smith if (sc->mlx_sgtable) 2331ac4b82bSMike Smith bus_dmamem_free(sc->mlx_sg_dmat, sc->mlx_sgtable, sc->mlx_sg_dmamap); 2341ac4b82bSMike Smith if (sc->mlx_sg_dmat) 2351ac4b82bSMike Smith bus_dma_tag_destroy(sc->mlx_sg_dmat); 2361ac4b82bSMike Smith 2371ac4b82bSMike Smith /* 2381ac4b82bSMike Smith * Create a single tag describing a region large enough to hold all of 239baff09dbSMike Smith * the s/g lists we will need. If we're called early on, we don't know how 240baff09dbSMike Smith * many commands we're going to be asked to support, so only allocate enough 241baff09dbSMike Smith * for a couple. 2421ac4b82bSMike Smith */ 243baff09dbSMike Smith if (sc->mlx_enq2 == NULL) { 244baff09dbSMike Smith ncmd = 2; 245baff09dbSMike Smith } else { 246baff09dbSMike Smith ncmd = sc->mlx_enq2->me_max_commands; 247baff09dbSMike Smith } 248baff09dbSMike Smith segsize = sizeof(struct mlx_sgentry) * MLX_NSEG * ncmd; 2491ac4b82bSMike Smith error = bus_dma_tag_create(sc->mlx_parent_dmat, /* parent */ 2501ac4b82bSMike Smith 1, 0, /* alignment,boundary */ 2511ac4b82bSMike Smith BUS_SPACE_MAXADDR, /* lowaddr */ 2521ac4b82bSMike Smith BUS_SPACE_MAXADDR, /* highaddr */ 2531ac4b82bSMike Smith NULL, NULL, /* filter, filterarg */ 2541ac4b82bSMike Smith segsize, 1, /* maxsize, nsegments */ 2551ac4b82bSMike Smith BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ 2561ac4b82bSMike Smith 0, /* flags */ 257fc3e87b3SScott Long NULL, NULL, /* lockfunc, lockarg */ 2581ac4b82bSMike Smith &sc->mlx_sg_dmat); 2591ac4b82bSMike Smith if (error != 0) { 2601ac4b82bSMike Smith device_printf(sc->mlx_dev, "can't allocate scatter/gather DMA tag\n"); 2611ac4b82bSMike Smith return(ENOMEM); 2621ac4b82bSMike Smith } 2631ac4b82bSMike Smith 2641ac4b82bSMike Smith /* 2651ac4b82bSMike Smith * Allocate enough s/g maps for all commands and permanently map them into 2661ac4b82bSMike Smith * controller-visible space. 2671ac4b82bSMike Smith * 2681ac4b82bSMike Smith * XXX this assumes we can get enough space for all the s/g maps in one 269fc3e87b3SScott Long * contiguous slab. We may need to switch to a more complex arrangement 270fc3e87b3SScott Long * where we allocate in smaller chunks and keep a lookup table from slot 271fc3e87b3SScott Long * to bus address. 2721ac4b82bSMike Smith */ 273fc3e87b3SScott Long error = bus_dmamem_alloc(sc->mlx_sg_dmat, (void **)&sc->mlx_sgtable, 274fc3e87b3SScott Long BUS_DMA_NOWAIT, &sc->mlx_sg_dmamap); 2751ac4b82bSMike Smith if (error) { 2761ac4b82bSMike Smith device_printf(sc->mlx_dev, "can't allocate s/g table\n"); 2771ac4b82bSMike Smith return(ENOMEM); 2781ac4b82bSMike Smith } 2791b4404f9SScott Long (void)bus_dmamap_load(sc->mlx_sg_dmat, sc->mlx_sg_dmamap, sc->mlx_sgtable, 280fc3e87b3SScott Long segsize, mlx_dma_map_sg, sc, 0); 2811ac4b82bSMike Smith return(0); 2821ac4b82bSMike Smith } 2831ac4b82bSMike Smith 2841ac4b82bSMike Smith /******************************************************************************** 2851ac4b82bSMike Smith * Initialise the controller and softc 2861ac4b82bSMike Smith */ 2871ac4b82bSMike Smith int 2881ac4b82bSMike Smith mlx_attach(struct mlx_softc *sc) 2891ac4b82bSMike Smith { 290da8bb3a3SMike Smith struct mlx_enquiry_old *meo; 291da8bb3a3SMike Smith int rid, error, fwminor, hscode, hserror, hsparam1, hsparam2, hsmsg; 2921ac4b82bSMike Smith 293da8bb3a3SMike Smith debug_called(1); 2941ac4b82bSMike Smith 2951ac4b82bSMike Smith /* 2961ac4b82bSMike Smith * Initialise per-controller queues. 2971ac4b82bSMike Smith */ 2984b006d7bSMike Smith TAILQ_INIT(&sc->mlx_work); 2991ac4b82bSMike Smith TAILQ_INIT(&sc->mlx_freecmds); 30015fd5d22SMike Smith MLX_BIO_QINIT(sc->mlx_bioq); 3011ac4b82bSMike Smith 3021ac4b82bSMike Smith /* 3031ac4b82bSMike Smith * Select accessor methods based on controller interface type. 3041ac4b82bSMike Smith */ 3051ac4b82bSMike Smith switch(sc->mlx_iftype) { 306da8bb3a3SMike Smith case MLX_IFTYPE_2: 3071ac4b82bSMike Smith case MLX_IFTYPE_3: 3081ac4b82bSMike Smith sc->mlx_tryqueue = mlx_v3_tryqueue; 3091ac4b82bSMike Smith sc->mlx_findcomplete = mlx_v3_findcomplete; 3101ac4b82bSMike Smith sc->mlx_intaction = mlx_v3_intaction; 311da8bb3a3SMike Smith sc->mlx_fw_handshake = mlx_v3_fw_handshake; 3121ac4b82bSMike Smith break; 313f6b84b08SMike Smith case MLX_IFTYPE_4: 314f6b84b08SMike Smith sc->mlx_tryqueue = mlx_v4_tryqueue; 315f6b84b08SMike Smith sc->mlx_findcomplete = mlx_v4_findcomplete; 316f6b84b08SMike Smith sc->mlx_intaction = mlx_v4_intaction; 317da8bb3a3SMike Smith sc->mlx_fw_handshake = mlx_v4_fw_handshake; 318f6b84b08SMike Smith break; 3195792b7feSMike Smith case MLX_IFTYPE_5: 3205792b7feSMike Smith sc->mlx_tryqueue = mlx_v5_tryqueue; 3215792b7feSMike Smith sc->mlx_findcomplete = mlx_v5_findcomplete; 3225792b7feSMike Smith sc->mlx_intaction = mlx_v5_intaction; 323da8bb3a3SMike Smith sc->mlx_fw_handshake = mlx_v5_fw_handshake; 3245792b7feSMike Smith break; 3251ac4b82bSMike Smith default: 3261ac4b82bSMike Smith return(ENXIO); /* should never happen */ 3271ac4b82bSMike Smith } 3281ac4b82bSMike Smith 3291ac4b82bSMike Smith /* disable interrupts before we start talking to the controller */ 3301ac4b82bSMike Smith sc->mlx_intaction(sc, MLX_INTACTION_DISABLE); 3311ac4b82bSMike Smith 3321ac4b82bSMike Smith /* 333da8bb3a3SMike Smith * Wait for the controller to come ready, handshake with the firmware if required. 334da8bb3a3SMike Smith * This is typically only necessary on platforms where the controller BIOS does not 335da8bb3a3SMike Smith * run. 336da8bb3a3SMike Smith */ 337da8bb3a3SMike Smith hsmsg = 0; 338da8bb3a3SMike Smith DELAY(1000); 339da8bb3a3SMike Smith while ((hscode = sc->mlx_fw_handshake(sc, &hserror, &hsparam1, &hsparam2)) != 0) { 340da8bb3a3SMike Smith /* report first time around... */ 341da8bb3a3SMike Smith if (hsmsg == 0) { 342da8bb3a3SMike Smith device_printf(sc->mlx_dev, "controller initialisation in progress...\n"); 343da8bb3a3SMike Smith hsmsg = 1; 344da8bb3a3SMike Smith } 345da8bb3a3SMike Smith /* did we get a real message? */ 346da8bb3a3SMike Smith if (hscode == 2) { 347da8bb3a3SMike Smith hscode = mlx_fw_message(sc, hserror, hsparam1, hsparam2); 348da8bb3a3SMike Smith /* fatal initialisation error? */ 349da8bb3a3SMike Smith if (hscode != 0) { 350da8bb3a3SMike Smith return(ENXIO); 351da8bb3a3SMike Smith } 352da8bb3a3SMike Smith } 353da8bb3a3SMike Smith } 354da8bb3a3SMike Smith if (hsmsg == 1) 355da8bb3a3SMike Smith device_printf(sc->mlx_dev, "initialisation complete.\n"); 356da8bb3a3SMike Smith 357da8bb3a3SMike Smith /* 3581ac4b82bSMike Smith * Allocate and connect our interrupt. 3591ac4b82bSMike Smith */ 3601ac4b82bSMike Smith rid = 0; 3615f96beb9SNate Lawson sc->mlx_irq = bus_alloc_resource_any(sc->mlx_dev, SYS_RES_IRQ, &rid, 3625f96beb9SNate Lawson RF_SHAREABLE | RF_ACTIVE); 3631ac4b82bSMike Smith if (sc->mlx_irq == NULL) { 364421f2f7dSMike Smith device_printf(sc->mlx_dev, "can't allocate interrupt\n"); 3651ac4b82bSMike Smith return(ENXIO); 3661ac4b82bSMike Smith } 367ef544f63SPaolo Pisati error = bus_setup_intr(sc->mlx_dev, sc->mlx_irq, INTR_TYPE_BIO | INTR_ENTROPY, NULL, mlx_intr, sc, &sc->mlx_intr); 3681ac4b82bSMike Smith if (error) { 369421f2f7dSMike Smith device_printf(sc->mlx_dev, "can't set up interrupt\n"); 3701ac4b82bSMike Smith return(ENXIO); 3711ac4b82bSMike Smith } 3721ac4b82bSMike Smith 3731ac4b82bSMike Smith /* 3741ac4b82bSMike Smith * Create DMA tag for mapping buffers into controller-addressable space. 3751ac4b82bSMike Smith */ 3761ac4b82bSMike Smith error = bus_dma_tag_create(sc->mlx_parent_dmat, /* parent */ 3771b4404f9SScott Long 1, 0, /* align, boundary */ 3781ac4b82bSMike Smith BUS_SPACE_MAXADDR, /* lowaddr */ 3791ac4b82bSMike Smith BUS_SPACE_MAXADDR, /* highaddr */ 3801ac4b82bSMike Smith NULL, NULL, /* filter, filterarg */ 381baff09dbSMike Smith MAXBSIZE, MLX_NSEG, /* maxsize, nsegments */ 3821ac4b82bSMike Smith BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ 3831ac4b82bSMike Smith 0, /* flags */ 384f6b1c44dSScott Long busdma_lock_mutex, /* lockfunc */ 385f6b1c44dSScott Long &Giant, /* lockarg */ 3861ac4b82bSMike Smith &sc->mlx_buffer_dmat); 3871ac4b82bSMike Smith if (error != 0) { 3881ac4b82bSMike Smith device_printf(sc->mlx_dev, "can't allocate buffer DMA tag\n"); 3891ac4b82bSMike Smith return(ENOMEM); 3901ac4b82bSMike Smith } 3911ac4b82bSMike Smith 3921ac4b82bSMike Smith /* 3931b4404f9SScott Long * Create some initial scatter/gather mappings so we can run the probe 3941b4404f9SScott Long * commands. 3951ac4b82bSMike Smith */ 3961ac4b82bSMike Smith error = mlx_sglist_map(sc); 3971ac4b82bSMike Smith if (error != 0) { 398421f2f7dSMike Smith device_printf(sc->mlx_dev, "can't make initial s/g list mapping\n"); 3991ac4b82bSMike Smith return(error); 4001ac4b82bSMike Smith } 4011ac4b82bSMike Smith 402baff09dbSMike Smith /* 403baff09dbSMike Smith * We don't (yet) know where the event log is up to. 404baff09dbSMike Smith */ 405baff09dbSMike Smith sc->mlx_currevent = -1; 406baff09dbSMike Smith 407baff09dbSMike Smith /* 408baff09dbSMike Smith * Obtain controller feature information 409baff09dbSMike Smith */ 4109eee27f1SMike Smith if ((sc->mlx_enq2 = mlx_enquire(sc, MLX_CMD_ENQUIRY2, sizeof(struct mlx_enquiry2), NULL)) == NULL) { 4111ac4b82bSMike Smith device_printf(sc->mlx_dev, "ENQUIRY2 failed\n"); 4121ac4b82bSMike Smith return(ENXIO); 4131ac4b82bSMike Smith } 4141ac4b82bSMike Smith 4159eee27f1SMike Smith /* 4161ac4b82bSMike Smith * Do quirk/feature related things. 4171ac4b82bSMike Smith */ 4189eee27f1SMike Smith fwminor = (sc->mlx_enq2->me_firmware_id >> 8) & 0xff; 4191ac4b82bSMike Smith switch(sc->mlx_iftype) { 420da8bb3a3SMike Smith case MLX_IFTYPE_2: 421da8bb3a3SMike Smith /* These controllers don't report the firmware version in the ENQUIRY2 response */ 422da8bb3a3SMike Smith if ((meo = mlx_enquire(sc, MLX_CMD_ENQUIRY_OLD, sizeof(struct mlx_enquiry_old), NULL)) == NULL) { 423da8bb3a3SMike Smith device_printf(sc->mlx_dev, "ENQUIRY_OLD failed\n"); 424da8bb3a3SMike Smith return(ENXIO); 425da8bb3a3SMike Smith } 426da8bb3a3SMike Smith sc->mlx_enq2->me_firmware_id = ('0' << 24) | (0 << 16) | (meo->me_fwminor << 8) | meo->me_fwmajor; 427da8bb3a3SMike Smith 428da8bb3a3SMike Smith /* XXX require 2.42 or better (PCI) or 2.14 or better (EISA) */ 429da8bb3a3SMike Smith if (meo->me_fwminor < 42) { 430da8bb3a3SMike Smith device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n"); 431da8bb3a3SMike Smith device_printf(sc->mlx_dev, " *** WARNING *** Use revision 2.42 or later\n"); 432da8bb3a3SMike Smith } 433caa32ef5SColin Percival free(meo, M_DEVBUF); 434da8bb3a3SMike Smith break; 4351ac4b82bSMike Smith case MLX_IFTYPE_3: 436f6b84b08SMike Smith /* XXX certify 3.52? */ 4379eee27f1SMike Smith if (fwminor < 51) { 4384b006d7bSMike Smith device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n"); 4394b006d7bSMike Smith device_printf(sc->mlx_dev, " *** WARNING *** Use revision 3.51 or later\n"); 4401ac4b82bSMike Smith } 4411ac4b82bSMike Smith break; 442f6b84b08SMike Smith case MLX_IFTYPE_4: 443f6b84b08SMike Smith /* XXX certify firmware versions? */ 4449eee27f1SMike Smith if (fwminor < 6) { 4454b006d7bSMike Smith device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n"); 4464b006d7bSMike Smith device_printf(sc->mlx_dev, " *** WARNING *** Use revision 4.06 or later\n"); 447f6b84b08SMike Smith } 448f6b84b08SMike Smith break; 4495792b7feSMike Smith case MLX_IFTYPE_5: 4509eee27f1SMike Smith if (fwminor < 7) { 4515792b7feSMike Smith device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n"); 4525792b7feSMike Smith device_printf(sc->mlx_dev, " *** WARNING *** Use revision 5.07 or later\n"); 4535792b7feSMike Smith } 4545792b7feSMike Smith break; 4551ac4b82bSMike Smith default: 4561ac4b82bSMike Smith return(ENXIO); /* should never happen */ 4571ac4b82bSMike Smith } 4581ac4b82bSMike Smith 4591ac4b82bSMike Smith /* 460baff09dbSMike Smith * Create the final scatter/gather mappings now that we have characterised the controller. 4611ac4b82bSMike Smith */ 4621ac4b82bSMike Smith error = mlx_sglist_map(sc); 4631ac4b82bSMike Smith if (error != 0) { 464baff09dbSMike Smith device_printf(sc->mlx_dev, "can't make final s/g list mapping\n"); 4651ac4b82bSMike Smith return(error); 4661ac4b82bSMike Smith } 4671ac4b82bSMike Smith 4681ac4b82bSMike Smith /* 469421f2f7dSMike Smith * No user-requested background operation is in progress. 4701ac4b82bSMike Smith */ 471421f2f7dSMike Smith sc->mlx_background = 0; 472421f2f7dSMike Smith sc->mlx_rebuildstat.rs_code = MLX_REBUILDSTAT_IDLE; 4731ac4b82bSMike Smith 4741ac4b82bSMike Smith /* 475da8bb3a3SMike Smith * Create the control device. 4761ac4b82bSMike Smith */ 477e78a5a3fSEd Schouten sc->mlx_dev_t = make_dev(&mlx_cdevsw, 0, UID_ROOT, GID_OPERATOR, 478da8bb3a3SMike Smith S_IRUSR | S_IWUSR, "mlx%d", device_get_unit(sc->mlx_dev)); 479e78a5a3fSEd Schouten sc->mlx_dev_t->si_drv1 = sc; 4801ac4b82bSMike Smith 4811ac4b82bSMike Smith /* 4821ac4b82bSMike Smith * Start the timeout routine. 4831ac4b82bSMike Smith */ 4841ac4b82bSMike Smith sc->mlx_timeout = timeout(mlx_periodic, sc, hz); 4851ac4b82bSMike Smith 486da8bb3a3SMike Smith /* print a little information about the controller */ 487da8bb3a3SMike Smith mlx_describe_controller(sc); 488da8bb3a3SMike Smith 4891ac4b82bSMike Smith return(0); 4901ac4b82bSMike Smith } 4911ac4b82bSMike Smith 4921ac4b82bSMike Smith /******************************************************************************** 4931ac4b82bSMike Smith * Locate disk resources and attach children to them. 4941ac4b82bSMike Smith */ 4951ac4b82bSMike Smith void 4961ac4b82bSMike Smith mlx_startup(struct mlx_softc *sc) 4971ac4b82bSMike Smith { 4981ac4b82bSMike Smith struct mlx_enq_sys_drive *mes; 4991ac4b82bSMike Smith struct mlx_sysdrive *dr; 5001ac4b82bSMike Smith int i, error; 5011ac4b82bSMike Smith 502da8bb3a3SMike Smith debug_called(1); 5031ac4b82bSMike Smith 5041ac4b82bSMike Smith /* 5051ac4b82bSMike Smith * Scan all the system drives and attach children for those that 5061ac4b82bSMike Smith * don't currently have them. 5071ac4b82bSMike Smith */ 5081ac4b82bSMike Smith mes = mlx_enquire(sc, MLX_CMD_ENQSYSDRIVE, sizeof(*mes) * MLX_MAXDRIVES, NULL); 5091ac4b82bSMike Smith if (mes == NULL) { 5109eee27f1SMike Smith device_printf(sc->mlx_dev, "error fetching drive status\n"); 5111ac4b82bSMike Smith return; 5121ac4b82bSMike Smith } 5131ac4b82bSMike Smith 5141ac4b82bSMike Smith /* iterate over drives returned */ 5151ac4b82bSMike Smith for (i = 0, dr = &sc->mlx_sysdrive[0]; 5161ac4b82bSMike Smith (i < MLX_MAXDRIVES) && (mes[i].sd_size != 0xffffffff); 5171ac4b82bSMike Smith i++, dr++) { 5181ac4b82bSMike Smith /* are we already attached to this drive? */ 5191ac4b82bSMike Smith if (dr->ms_disk == 0) { 5201ac4b82bSMike Smith /* pick up drive information */ 5211ac4b82bSMike Smith dr->ms_size = mes[i].sd_size; 522f6b84b08SMike Smith dr->ms_raidlevel = mes[i].sd_raidlevel & 0xf; 5231ac4b82bSMike Smith dr->ms_state = mes[i].sd_state; 5241ac4b82bSMike Smith 5251ac4b82bSMike Smith /* generate geometry information */ 5261ac4b82bSMike Smith if (sc->mlx_geom == MLX_GEOM_128_32) { 5271ac4b82bSMike Smith dr->ms_heads = 128; 5281ac4b82bSMike Smith dr->ms_sectors = 32; 5291ac4b82bSMike Smith dr->ms_cylinders = dr->ms_size / (128 * 32); 5301ac4b82bSMike Smith } else { /* MLX_GEOM_255/63 */ 5311ac4b82bSMike Smith dr->ms_heads = 255; 5321ac4b82bSMike Smith dr->ms_sectors = 63; 5331ac4b82bSMike Smith dr->ms_cylinders = dr->ms_size / (255 * 63); 5341ac4b82bSMike Smith } 535fe0d4089SMatthew N. Dodd dr->ms_disk = device_add_child(sc->mlx_dev, /*"mlxd"*/NULL, -1); 5361ac4b82bSMike Smith if (dr->ms_disk == 0) 5371ac4b82bSMike Smith device_printf(sc->mlx_dev, "device_add_child failed\n"); 538fe0d4089SMatthew N. Dodd device_set_ivars(dr->ms_disk, dr); 5391ac4b82bSMike Smith } 5401ac4b82bSMike Smith } 5411ac4b82bSMike Smith free(mes, M_DEVBUF); 5421ac4b82bSMike Smith if ((error = bus_generic_attach(sc->mlx_dev)) != 0) 5431ac4b82bSMike Smith device_printf(sc->mlx_dev, "bus_generic_attach returned %d", error); 5441ac4b82bSMike Smith 5451ac4b82bSMike Smith /* mark controller back up */ 5461ac4b82bSMike Smith sc->mlx_state &= ~MLX_STATE_SHUTDOWN; 5471ac4b82bSMike Smith 5481ac4b82bSMike Smith /* enable interrupts */ 5491ac4b82bSMike Smith sc->mlx_intaction(sc, MLX_INTACTION_ENABLE); 5501ac4b82bSMike Smith } 5511ac4b82bSMike Smith 5521ac4b82bSMike Smith /******************************************************************************** 5531ac4b82bSMike Smith * Disconnect from the controller completely, in preparation for unload. 5541ac4b82bSMike Smith */ 5551ac4b82bSMike Smith int 5561ac4b82bSMike Smith mlx_detach(device_t dev) 5571ac4b82bSMike Smith { 5581ac4b82bSMike Smith struct mlx_softc *sc = device_get_softc(dev); 5595792b7feSMike Smith struct mlxd_softc *mlxd; 5605792b7feSMike Smith int i, s, error; 5611ac4b82bSMike Smith 562da8bb3a3SMike Smith debug_called(1); 5631ac4b82bSMike Smith 5645792b7feSMike Smith error = EBUSY; 5655792b7feSMike Smith s = splbio(); 5661ac4b82bSMike Smith if (sc->mlx_state & MLX_STATE_OPEN) 5675792b7feSMike Smith goto out; 5681ac4b82bSMike Smith 5695792b7feSMike Smith for (i = 0; i < MLX_MAXDRIVES; i++) { 5705792b7feSMike Smith if (sc->mlx_sysdrive[i].ms_disk != 0) { 5715792b7feSMike Smith mlxd = device_get_softc(sc->mlx_sysdrive[i].ms_disk); 5725792b7feSMike Smith if (mlxd->mlxd_flags & MLXD_OPEN) { /* drive is mounted, abort detach */ 5735792b7feSMike Smith device_printf(sc->mlx_sysdrive[i].ms_disk, "still open, can't detach\n"); 5745792b7feSMike Smith goto out; 5755792b7feSMike Smith } 5765792b7feSMike Smith } 5775792b7feSMike Smith } 5781ac4b82bSMike Smith if ((error = mlx_shutdown(dev))) 5795792b7feSMike Smith goto out; 5801ac4b82bSMike Smith 5811ac4b82bSMike Smith mlx_free(sc); 5821ac4b82bSMike Smith 5835792b7feSMike Smith error = 0; 5845792b7feSMike Smith out: 5855792b7feSMike Smith splx(s); 5865792b7feSMike Smith return(error); 5871ac4b82bSMike Smith } 5881ac4b82bSMike Smith 5891ac4b82bSMike Smith /******************************************************************************** 5901ac4b82bSMike Smith * Bring the controller down to a dormant state and detach all child devices. 5911ac4b82bSMike Smith * 5921ac4b82bSMike Smith * This function is called before detach, system shutdown, or before performing 5931ac4b82bSMike Smith * an operation which may add or delete system disks. (Call mlx_startup to 5941ac4b82bSMike Smith * resume normal operation.) 5951ac4b82bSMike Smith * 5968177437dSPoul-Henning Kamp * Note that we can assume that the bioq on the controller is empty, as we won't 5971ac4b82bSMike Smith * allow shutdown if any device is open. 5981ac4b82bSMike Smith */ 5991ac4b82bSMike Smith int 6001ac4b82bSMike Smith mlx_shutdown(device_t dev) 6011ac4b82bSMike Smith { 6021ac4b82bSMike Smith struct mlx_softc *sc = device_get_softc(dev); 6031ac4b82bSMike Smith int i, s, error; 6041ac4b82bSMike Smith 605da8bb3a3SMike Smith debug_called(1); 6061ac4b82bSMike Smith 6071ac4b82bSMike Smith s = splbio(); 6081ac4b82bSMike Smith error = 0; 6091ac4b82bSMike Smith 6101ac4b82bSMike Smith sc->mlx_state |= MLX_STATE_SHUTDOWN; 6115792b7feSMike Smith sc->mlx_intaction(sc, MLX_INTACTION_DISABLE); 6121ac4b82bSMike Smith 6131ac4b82bSMike Smith /* flush controller */ 6141ac4b82bSMike Smith device_printf(sc->mlx_dev, "flushing cache..."); 6151ac4b82bSMike Smith if (mlx_flush(sc)) { 6161ac4b82bSMike Smith printf("failed\n"); 6171ac4b82bSMike Smith } else { 6181ac4b82bSMike Smith printf("done\n"); 6191ac4b82bSMike Smith } 6201ac4b82bSMike Smith 6211ac4b82bSMike Smith /* delete all our child devices */ 6221ac4b82bSMike Smith for (i = 0; i < MLX_MAXDRIVES; i++) { 6231ac4b82bSMike Smith if (sc->mlx_sysdrive[i].ms_disk != 0) { 6241ac4b82bSMike Smith if ((error = device_delete_child(sc->mlx_dev, sc->mlx_sysdrive[i].ms_disk)) != 0) 6251ac4b82bSMike Smith goto out; 6261ac4b82bSMike Smith sc->mlx_sysdrive[i].ms_disk = 0; 6271ac4b82bSMike Smith } 6281ac4b82bSMike Smith } 6291ac4b82bSMike Smith 6301ac4b82bSMike Smith out: 6311ac4b82bSMike Smith splx(s); 6321ac4b82bSMike Smith return(error); 6331ac4b82bSMike Smith } 6341ac4b82bSMike Smith 6351ac4b82bSMike Smith /******************************************************************************** 6361ac4b82bSMike Smith * Bring the controller to a quiescent state, ready for system suspend. 6371ac4b82bSMike Smith */ 6381ac4b82bSMike Smith int 6391ac4b82bSMike Smith mlx_suspend(device_t dev) 6401ac4b82bSMike Smith { 6411ac4b82bSMike Smith struct mlx_softc *sc = device_get_softc(dev); 6421ac4b82bSMike Smith int s; 6431ac4b82bSMike Smith 644da8bb3a3SMike Smith debug_called(1); 6451ac4b82bSMike Smith 6461ac4b82bSMike Smith s = splbio(); 6471ac4b82bSMike Smith sc->mlx_state |= MLX_STATE_SUSPEND; 6481ac4b82bSMike Smith 6491ac4b82bSMike Smith /* flush controller */ 6501ac4b82bSMike Smith device_printf(sc->mlx_dev, "flushing cache..."); 6511ac4b82bSMike Smith printf("%s\n", mlx_flush(sc) ? "failed" : "done"); 6521ac4b82bSMike Smith 6531ac4b82bSMike Smith sc->mlx_intaction(sc, MLX_INTACTION_DISABLE); 6541ac4b82bSMike Smith splx(s); 6551ac4b82bSMike Smith 6561ac4b82bSMike Smith return(0); 6571ac4b82bSMike Smith } 6581ac4b82bSMike Smith 6591ac4b82bSMike Smith /******************************************************************************** 6601ac4b82bSMike Smith * Bring the controller back to a state ready for operation. 6611ac4b82bSMike Smith */ 6621ac4b82bSMike Smith int 6631ac4b82bSMike Smith mlx_resume(device_t dev) 6641ac4b82bSMike Smith { 6651ac4b82bSMike Smith struct mlx_softc *sc = device_get_softc(dev); 6661ac4b82bSMike Smith 667da8bb3a3SMike Smith debug_called(1); 6681ac4b82bSMike Smith 6691ac4b82bSMike Smith sc->mlx_state &= ~MLX_STATE_SUSPEND; 6701ac4b82bSMike Smith sc->mlx_intaction(sc, MLX_INTACTION_ENABLE); 6711ac4b82bSMike Smith 6721ac4b82bSMike Smith return(0); 6731ac4b82bSMike Smith } 6741ac4b82bSMike Smith 6751ac4b82bSMike Smith /******************************************************************************* 6761ac4b82bSMike Smith * Take an interrupt, or be poked by other code to look for interrupt-worthy 6771ac4b82bSMike Smith * status. 6781ac4b82bSMike Smith */ 6791ac4b82bSMike Smith void 6801ac4b82bSMike Smith mlx_intr(void *arg) 6811ac4b82bSMike Smith { 6821ac4b82bSMike Smith struct mlx_softc *sc = (struct mlx_softc *)arg; 6831ac4b82bSMike Smith 684da8bb3a3SMike Smith debug_called(1); 6851ac4b82bSMike Smith 6865792b7feSMike Smith /* collect finished commands, queue anything waiting */ 6875792b7feSMike Smith mlx_done(sc); 6881ac4b82bSMike Smith }; 6891ac4b82bSMike Smith 6901ac4b82bSMike Smith /******************************************************************************* 6911ac4b82bSMike Smith * Receive a buf structure from a child device and queue it on a particular 6921ac4b82bSMike Smith * disk resource, then poke the disk resource to start as much work as it can. 6931ac4b82bSMike Smith */ 6941ac4b82bSMike Smith int 69515fd5d22SMike Smith mlx_submit_buf(struct mlx_softc *sc, mlx_bio *bp) 6961ac4b82bSMike Smith { 6974b006d7bSMike Smith int s; 6984b006d7bSMike Smith 699da8bb3a3SMike Smith debug_called(1); 7001ac4b82bSMike Smith 7014b006d7bSMike Smith s = splbio(); 70215fd5d22SMike Smith MLX_BIO_QINSERT(sc->mlx_bioq, bp); 7031ac4b82bSMike Smith sc->mlx_waitbufs++; 7044b006d7bSMike Smith splx(s); 7051ac4b82bSMike Smith mlx_startio(sc); 7061ac4b82bSMike Smith return(0); 7071ac4b82bSMike Smith } 7081ac4b82bSMike Smith 7091ac4b82bSMike Smith /******************************************************************************** 7101ac4b82bSMike Smith * Accept an open operation on the control device. 7111ac4b82bSMike Smith */ 7121ac4b82bSMike Smith int 71389c9c53dSPoul-Henning Kamp mlx_open(struct cdev *dev, int flags, int fmt, struct thread *td) 7141ac4b82bSMike Smith { 715e78a5a3fSEd Schouten struct mlx_softc *sc = dev->si_drv1; 7161ac4b82bSMike Smith 7171ac4b82bSMike Smith sc->mlx_state |= MLX_STATE_OPEN; 7181ac4b82bSMike Smith return(0); 7191ac4b82bSMike Smith } 7201ac4b82bSMike Smith 7211ac4b82bSMike Smith /******************************************************************************** 7221ac4b82bSMike Smith * Accept the last close on the control device. 7231ac4b82bSMike Smith */ 7241ac4b82bSMike Smith int 72589c9c53dSPoul-Henning Kamp mlx_close(struct cdev *dev, int flags, int fmt, struct thread *td) 7261ac4b82bSMike Smith { 727e78a5a3fSEd Schouten struct mlx_softc *sc = dev->si_drv1; 7281ac4b82bSMike Smith 7291ac4b82bSMike Smith sc->mlx_state &= ~MLX_STATE_OPEN; 7301ac4b82bSMike Smith return (0); 7311ac4b82bSMike Smith } 7321ac4b82bSMike Smith 7331ac4b82bSMike Smith /******************************************************************************** 7341ac4b82bSMike Smith * Handle controller-specific control operations. 7351ac4b82bSMike Smith */ 7361ac4b82bSMike Smith int 73789c9c53dSPoul-Henning Kamp mlx_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td) 7381ac4b82bSMike Smith { 739e78a5a3fSEd Schouten struct mlx_softc *sc = dev->si_drv1; 740421f2f7dSMike Smith struct mlx_rebuild_request *rb = (struct mlx_rebuild_request *)addr; 741421f2f7dSMike Smith struct mlx_rebuild_status *rs = (struct mlx_rebuild_status *)addr; 7421ac4b82bSMike Smith int *arg = (int *)addr; 7431ac4b82bSMike Smith struct mlx_pause *mp; 7441ac4b82bSMike Smith struct mlx_sysdrive *dr; 7451ac4b82bSMike Smith struct mlxd_softc *mlxd; 7461ac4b82bSMike Smith int i, error; 7471ac4b82bSMike Smith 7481ac4b82bSMike Smith switch(cmd) { 7491ac4b82bSMike Smith /* 7501ac4b82bSMike Smith * Enumerate connected system drives; returns the first system drive's 7511ac4b82bSMike Smith * unit number if *arg is -1, or the next unit after *arg if it's 7521ac4b82bSMike Smith * a valid unit on this controller. 7531ac4b82bSMike Smith */ 7541ac4b82bSMike Smith case MLX_NEXT_CHILD: 7551ac4b82bSMike Smith /* search system drives */ 7561ac4b82bSMike Smith for (i = 0; i < MLX_MAXDRIVES; i++) { 7571ac4b82bSMike Smith /* is this one attached? */ 7581ac4b82bSMike Smith if (sc->mlx_sysdrive[i].ms_disk != 0) { 7591ac4b82bSMike Smith /* looking for the next one we come across? */ 7601ac4b82bSMike Smith if (*arg == -1) { 76129e6fa3aSStephane E. Potvin *arg = device_get_unit(sc->mlx_sysdrive[i].ms_disk); 7621ac4b82bSMike Smith return(0); 7631ac4b82bSMike Smith } 7641ac4b82bSMike Smith /* we want the one after this one */ 7651ac4b82bSMike Smith if (*arg == device_get_unit(sc->mlx_sysdrive[i].ms_disk)) 7661ac4b82bSMike Smith *arg = -1; 7671ac4b82bSMike Smith } 7681ac4b82bSMike Smith } 7691ac4b82bSMike Smith return(ENOENT); 7701ac4b82bSMike Smith 7711ac4b82bSMike Smith /* 7721ac4b82bSMike Smith * Scan the controller to see whether new drives have appeared. 7731ac4b82bSMike Smith */ 7741ac4b82bSMike Smith case MLX_RESCAN_DRIVES: 7751ac4b82bSMike Smith mlx_startup(sc); 7761ac4b82bSMike Smith return(0); 7771ac4b82bSMike Smith 7781ac4b82bSMike Smith /* 7791ac4b82bSMike Smith * Disconnect from the specified drive; it may be about to go 7801ac4b82bSMike Smith * away. 7811ac4b82bSMike Smith */ 7821ac4b82bSMike Smith case MLX_DETACH_DRIVE: /* detach one drive */ 7831ac4b82bSMike Smith 7841ac4b82bSMike Smith if (((dr = mlx_findunit(sc, *arg)) == NULL) || 7851ac4b82bSMike Smith ((mlxd = device_get_softc(dr->ms_disk)) == NULL)) 7861ac4b82bSMike Smith return(ENOENT); 7871ac4b82bSMike Smith 7881ac4b82bSMike Smith device_printf(dr->ms_disk, "detaching..."); 7891ac4b82bSMike Smith error = 0; 7901ac4b82bSMike Smith if (mlxd->mlxd_flags & MLXD_OPEN) { 7911ac4b82bSMike Smith error = EBUSY; 7921ac4b82bSMike Smith goto detach_out; 7931ac4b82bSMike Smith } 7941ac4b82bSMike Smith 7951ac4b82bSMike Smith /* flush controller */ 7961ac4b82bSMike Smith if (mlx_flush(sc)) { 7971ac4b82bSMike Smith error = EBUSY; 7981ac4b82bSMike Smith goto detach_out; 7991ac4b82bSMike Smith } 8001ac4b82bSMike Smith 8011ac4b82bSMike Smith /* nuke drive */ 8021ac4b82bSMike Smith if ((error = device_delete_child(sc->mlx_dev, dr->ms_disk)) != 0) 8031ac4b82bSMike Smith goto detach_out; 8041ac4b82bSMike Smith dr->ms_disk = 0; 8051ac4b82bSMike Smith 8061ac4b82bSMike Smith detach_out: 8071ac4b82bSMike Smith if (error) { 8081ac4b82bSMike Smith printf("failed\n"); 8091ac4b82bSMike Smith } else { 8101ac4b82bSMike Smith printf("done\n"); 8111ac4b82bSMike Smith } 8121ac4b82bSMike Smith return(error); 8131ac4b82bSMike Smith 8141ac4b82bSMike Smith /* 8151ac4b82bSMike Smith * Pause one or more SCSI channels for a period of time, to assist 8161ac4b82bSMike Smith * in the process of hot-swapping devices. 8171ac4b82bSMike Smith * 8181ac4b82bSMike Smith * Note that at least the 3.51 firmware on the DAC960PL doesn't seem 8191ac4b82bSMike Smith * to do this right. 8201ac4b82bSMike Smith */ 8211ac4b82bSMike Smith case MLX_PAUSE_CHANNEL: /* schedule a channel pause */ 8221ac4b82bSMike Smith /* Does this command work on this firmware? */ 8231ac4b82bSMike Smith if (!(sc->mlx_feature & MLX_FEAT_PAUSEWORKS)) 8241ac4b82bSMike Smith return(EOPNOTSUPP); 8251ac4b82bSMike Smith 8261ac4b82bSMike Smith mp = (struct mlx_pause *)addr; 8271ac4b82bSMike Smith if ((mp->mp_which == MLX_PAUSE_CANCEL) && (sc->mlx_pause.mp_when != 0)) { 8281ac4b82bSMike Smith /* cancel a pending pause operation */ 8291ac4b82bSMike Smith sc->mlx_pause.mp_which = 0; 8301ac4b82bSMike Smith } else { 8311ac4b82bSMike Smith /* fix for legal channels */ 8329eee27f1SMike Smith mp->mp_which &= ((1 << sc->mlx_enq2->me_actual_channels) -1); 8331ac4b82bSMike Smith /* check time values */ 8341ac4b82bSMike Smith if ((mp->mp_when < 0) || (mp->mp_when > 3600)) 8351ac4b82bSMike Smith return(EINVAL); 8361ac4b82bSMike Smith if ((mp->mp_howlong < 1) || (mp->mp_howlong > (0xf * 30))) 8371ac4b82bSMike Smith return(EINVAL); 8381ac4b82bSMike Smith 8391ac4b82bSMike Smith /* check for a pause currently running */ 8401ac4b82bSMike Smith if ((sc->mlx_pause.mp_which != 0) && (sc->mlx_pause.mp_when == 0)) 8411ac4b82bSMike Smith return(EBUSY); 8421ac4b82bSMike Smith 8431ac4b82bSMike Smith /* looks ok, go with it */ 8441ac4b82bSMike Smith sc->mlx_pause.mp_which = mp->mp_which; 8451ac4b82bSMike Smith sc->mlx_pause.mp_when = time_second + mp->mp_when; 8461ac4b82bSMike Smith sc->mlx_pause.mp_howlong = sc->mlx_pause.mp_when + mp->mp_howlong; 8471ac4b82bSMike Smith } 8481ac4b82bSMike Smith return(0); 8491ac4b82bSMike Smith 8501ac4b82bSMike Smith /* 8511ac4b82bSMike Smith * Accept a command passthrough-style. 8521ac4b82bSMike Smith */ 8531ac4b82bSMike Smith case MLX_COMMAND: 8541ac4b82bSMike Smith return(mlx_user_command(sc, (struct mlx_usercommand *)addr)); 8551ac4b82bSMike Smith 856421f2f7dSMike Smith /* 857421f2f7dSMike Smith * Start a rebuild on a given SCSI disk 858421f2f7dSMike Smith */ 859421f2f7dSMike Smith case MLX_REBUILDASYNC: 860421f2f7dSMike Smith if (sc->mlx_background != 0) { 861421f2f7dSMike Smith rb->rr_status = 0x0106; 862421f2f7dSMike Smith return(EBUSY); 863421f2f7dSMike Smith } 864421f2f7dSMike Smith rb->rr_status = mlx_rebuild(sc, rb->rr_channel, rb->rr_target); 865421f2f7dSMike Smith switch (rb->rr_status) { 866421f2f7dSMike Smith case 0: 867421f2f7dSMike Smith error = 0; 868421f2f7dSMike Smith break; 869421f2f7dSMike Smith case 0x10000: 870421f2f7dSMike Smith error = ENOMEM; /* couldn't set up the command */ 871421f2f7dSMike Smith break; 872421f2f7dSMike Smith case 0x0002: 873421f2f7dSMike Smith error = EBUSY; 874421f2f7dSMike Smith break; 875421f2f7dSMike Smith case 0x0104: 876421f2f7dSMike Smith error = EIO; 877421f2f7dSMike Smith break; 878421f2f7dSMike Smith case 0x0105: 879421f2f7dSMike Smith error = ERANGE; 880421f2f7dSMike Smith break; 881421f2f7dSMike Smith case 0x0106: 882421f2f7dSMike Smith error = EBUSY; 883421f2f7dSMike Smith break; 884421f2f7dSMike Smith default: 885421f2f7dSMike Smith error = EINVAL; 886421f2f7dSMike Smith break; 887421f2f7dSMike Smith } 888421f2f7dSMike Smith if (error == 0) 889421f2f7dSMike Smith sc->mlx_background = MLX_BACKGROUND_REBUILD; 890421f2f7dSMike Smith return(error); 891421f2f7dSMike Smith 892421f2f7dSMike Smith /* 893421f2f7dSMike Smith * Get the status of the current rebuild or consistency check. 894421f2f7dSMike Smith */ 895421f2f7dSMike Smith case MLX_REBUILDSTAT: 896421f2f7dSMike Smith *rs = sc->mlx_rebuildstat; 897421f2f7dSMike Smith return(0); 898421f2f7dSMike Smith 899421f2f7dSMike Smith /* 900421f2f7dSMike Smith * Return the per-controller system drive number matching the 901421f2f7dSMike Smith * disk device number in (arg), if it happens to belong to us. 902421f2f7dSMike Smith */ 903421f2f7dSMike Smith case MLX_GET_SYSDRIVE: 904421f2f7dSMike Smith error = ENOENT; 905421f2f7dSMike Smith mlxd = (struct mlxd_softc *)devclass_get_softc(mlxd_devclass, *arg); 906421f2f7dSMike Smith if ((mlxd != NULL) && (mlxd->mlxd_drive >= sc->mlx_sysdrive) && 907421f2f7dSMike Smith (mlxd->mlxd_drive < (sc->mlx_sysdrive + MLX_MAXDRIVES))) { 908421f2f7dSMike Smith error = 0; 909421f2f7dSMike Smith *arg = mlxd->mlxd_drive - sc->mlx_sysdrive; 910421f2f7dSMike Smith } 911421f2f7dSMike Smith return(error); 912421f2f7dSMike Smith 9131ac4b82bSMike Smith default: 9141ac4b82bSMike Smith return(ENOTTY); 9151ac4b82bSMike Smith } 9161ac4b82bSMike Smith } 9171ac4b82bSMike Smith 9181ac4b82bSMike Smith /******************************************************************************** 9191ac4b82bSMike Smith * Handle operations requested by a System Drive connected to this controller. 9201ac4b82bSMike Smith */ 9211ac4b82bSMike Smith int 9221ac4b82bSMike Smith mlx_submit_ioctl(struct mlx_softc *sc, struct mlx_sysdrive *drive, u_long cmd, 923b40ce416SJulian Elischer caddr_t addr, int32_t flag, struct thread *td) 9241ac4b82bSMike Smith { 9251ac4b82bSMike Smith int *arg = (int *)addr; 926421f2f7dSMike Smith int error, result; 9271ac4b82bSMike Smith 9281ac4b82bSMike Smith switch(cmd) { 9291ac4b82bSMike Smith /* 9301ac4b82bSMike Smith * Return the current status of this drive. 9311ac4b82bSMike Smith */ 9321ac4b82bSMike Smith case MLXD_STATUS: 9331ac4b82bSMike Smith *arg = drive->ms_state; 9341ac4b82bSMike Smith return(0); 9351ac4b82bSMike Smith 9361ac4b82bSMike Smith /* 937421f2f7dSMike Smith * Start a background consistency check on this drive. 9381ac4b82bSMike Smith */ 939421f2f7dSMike Smith case MLXD_CHECKASYNC: /* start a background consistency check */ 940421f2f7dSMike Smith if (sc->mlx_background != 0) { 941421f2f7dSMike Smith *arg = 0x0106; 9421ac4b82bSMike Smith return(EBUSY); 943421f2f7dSMike Smith } 944421f2f7dSMike Smith result = mlx_check(sc, drive - &sc->mlx_sysdrive[0]); 945421f2f7dSMike Smith switch (result) { 9461ac4b82bSMike Smith case 0: 9471ac4b82bSMike Smith error = 0; 9481ac4b82bSMike Smith break; 9491ac4b82bSMike Smith case 0x10000: 9501ac4b82bSMike Smith error = ENOMEM; /* couldn't set up the command */ 9511ac4b82bSMike Smith break; 9521ac4b82bSMike Smith case 0x0002: 9531ac4b82bSMike Smith error = EIO; 9541ac4b82bSMike Smith break; 9551ac4b82bSMike Smith case 0x0105: 9561ac4b82bSMike Smith error = ERANGE; 9571ac4b82bSMike Smith break; 958421f2f7dSMike Smith case 0x0106: 959421f2f7dSMike Smith error = EBUSY; 960421f2f7dSMike Smith break; 9611ac4b82bSMike Smith default: 9621ac4b82bSMike Smith error = EINVAL; 9631ac4b82bSMike Smith break; 9641ac4b82bSMike Smith } 965421f2f7dSMike Smith if (error == 0) 966421f2f7dSMike Smith sc->mlx_background = MLX_BACKGROUND_CHECK; 967421f2f7dSMike Smith *arg = result; 9681ac4b82bSMike Smith return(error); 9691ac4b82bSMike Smith 9701ac4b82bSMike Smith } 9711ac4b82bSMike Smith return(ENOIOCTL); 9721ac4b82bSMike Smith } 9731ac4b82bSMike Smith 9741ac4b82bSMike Smith 9751ac4b82bSMike Smith /******************************************************************************** 9761ac4b82bSMike Smith ******************************************************************************** 9771ac4b82bSMike Smith Status Monitoring 9781ac4b82bSMike Smith ******************************************************************************** 9791ac4b82bSMike Smith ********************************************************************************/ 9801ac4b82bSMike Smith 9811ac4b82bSMike Smith /******************************************************************************** 9821ac4b82bSMike Smith * Fire off commands to periodically check the status of connected drives. 9831ac4b82bSMike Smith */ 9841ac4b82bSMike Smith static void 9851ac4b82bSMike Smith mlx_periodic(void *data) 9861ac4b82bSMike Smith { 9871ac4b82bSMike Smith struct mlx_softc *sc = (struct mlx_softc *)data; 9881ac4b82bSMike Smith 989da8bb3a3SMike Smith debug_called(1); 9901ac4b82bSMike Smith 9911ac4b82bSMike Smith /* 9921ac4b82bSMike Smith * Run a bus pause? 9931ac4b82bSMike Smith */ 9941ac4b82bSMike Smith if ((sc->mlx_pause.mp_which != 0) && 9951ac4b82bSMike Smith (sc->mlx_pause.mp_when > 0) && 9961ac4b82bSMike Smith (time_second >= sc->mlx_pause.mp_when)){ 9971ac4b82bSMike Smith 9981ac4b82bSMike Smith mlx_pause_action(sc); /* pause is running */ 9991ac4b82bSMike Smith sc->mlx_pause.mp_when = 0; 10001ac4b82bSMike Smith sysbeep(500, hz); 10011ac4b82bSMike Smith 10021ac4b82bSMike Smith /* 10031ac4b82bSMike Smith * Bus pause still running? 10041ac4b82bSMike Smith */ 10051ac4b82bSMike Smith } else if ((sc->mlx_pause.mp_which != 0) && 10061ac4b82bSMike Smith (sc->mlx_pause.mp_when == 0)) { 10071ac4b82bSMike Smith 10081ac4b82bSMike Smith /* time to stop bus pause? */ 10091ac4b82bSMike Smith if (time_second >= sc->mlx_pause.mp_howlong) { 10101ac4b82bSMike Smith mlx_pause_action(sc); 10111ac4b82bSMike Smith sc->mlx_pause.mp_which = 0; /* pause is complete */ 10121ac4b82bSMike Smith sysbeep(500, hz); 10131ac4b82bSMike Smith } else { 10141ac4b82bSMike Smith sysbeep((time_second % 5) * 100 + 500, hz/8); 10151ac4b82bSMike Smith } 10161ac4b82bSMike Smith 10171ac4b82bSMike Smith /* 10181ac4b82bSMike Smith * Run normal periodic activities? 10191ac4b82bSMike Smith */ 10205792b7feSMike Smith } else if (time_second > (sc->mlx_lastpoll + 10)) { 10211ac4b82bSMike Smith sc->mlx_lastpoll = time_second; 10221ac4b82bSMike Smith 10231ac4b82bSMike Smith /* 10241ac4b82bSMike Smith * Check controller status. 10255792b7feSMike Smith * 10265792b7feSMike Smith * XXX Note that this may not actually launch a command in situations of high load. 10271ac4b82bSMike Smith */ 1028da8bb3a3SMike Smith mlx_enquire(sc, (sc->mlx_iftype == MLX_IFTYPE_2) ? MLX_CMD_ENQUIRY_OLD : MLX_CMD_ENQUIRY, 1029da8bb3a3SMike Smith imax(sizeof(struct mlx_enquiry), sizeof(struct mlx_enquiry_old)), mlx_periodic_enquiry); 10301ac4b82bSMike Smith 10311ac4b82bSMike Smith /* 10321ac4b82bSMike Smith * Check system drive status. 10331ac4b82bSMike Smith * 10341ac4b82bSMike Smith * XXX This might be better left to event-driven detection, eg. I/O to an offline 10351ac4b82bSMike Smith * drive will detect it's offline, rebuilds etc. should detect the drive is back 10361ac4b82bSMike Smith * online. 10371ac4b82bSMike Smith */ 10381ac4b82bSMike Smith mlx_enquire(sc, MLX_CMD_ENQSYSDRIVE, sizeof(struct mlx_enq_sys_drive) * MLX_MAXDRIVES, 10391ac4b82bSMike Smith mlx_periodic_enquiry); 10401ac4b82bSMike Smith 10411ac4b82bSMike Smith } 10421ac4b82bSMike Smith 1043421f2f7dSMike Smith /* get drive rebuild/check status */ 1044421f2f7dSMike Smith /* XXX should check sc->mlx_background if this is only valid while in progress */ 1045421f2f7dSMike Smith mlx_enquire(sc, MLX_CMD_REBUILDSTAT, sizeof(struct mlx_rebuild_stat), mlx_periodic_rebuild); 1046421f2f7dSMike Smith 10475792b7feSMike Smith /* deal with possibly-missed interrupts and timed-out commands */ 10485792b7feSMike Smith mlx_done(sc); 10491ac4b82bSMike Smith 10501ac4b82bSMike Smith /* reschedule another poll next second or so */ 10511ac4b82bSMike Smith sc->mlx_timeout = timeout(mlx_periodic, sc, hz); 10521ac4b82bSMike Smith } 10531ac4b82bSMike Smith 10541ac4b82bSMike Smith /******************************************************************************** 10551ac4b82bSMike Smith * Handle the result of an ENQUIRY command instigated by periodic status polling. 10561ac4b82bSMike Smith */ 10571ac4b82bSMike Smith static void 10581ac4b82bSMike Smith mlx_periodic_enquiry(struct mlx_command *mc) 10591ac4b82bSMike Smith { 10601ac4b82bSMike Smith struct mlx_softc *sc = mc->mc_sc; 10611ac4b82bSMike Smith 1062da8bb3a3SMike Smith debug_called(1); 10631ac4b82bSMike Smith 10641ac4b82bSMike Smith /* Command completed OK? */ 10651ac4b82bSMike Smith if (mc->mc_status != 0) { 1066da8bb3a3SMike Smith device_printf(sc->mlx_dev, "periodic enquiry failed - %s\n", mlx_diagnose_command(mc)); 10671ac4b82bSMike Smith goto out; 10681ac4b82bSMike Smith } 10691ac4b82bSMike Smith 10701ac4b82bSMike Smith /* respond to command */ 10711ac4b82bSMike Smith switch(mc->mc_mailbox[0]) { 10721ac4b82bSMike Smith /* 1073da8bb3a3SMike Smith * This is currently a bit fruitless, as we don't know how to extract the eventlog 1074da8bb3a3SMike Smith * pointer yet. 1075da8bb3a3SMike Smith */ 1076da8bb3a3SMike Smith case MLX_CMD_ENQUIRY_OLD: 1077da8bb3a3SMike Smith { 1078da8bb3a3SMike Smith struct mlx_enquiry *me = (struct mlx_enquiry *)mc->mc_data; 1079da8bb3a3SMike Smith struct mlx_enquiry_old *meo = (struct mlx_enquiry_old *)mc->mc_data; 1080da8bb3a3SMike Smith int i; 1081da8bb3a3SMike Smith 1082da8bb3a3SMike Smith /* convert data in-place to new format */ 1083da8bb3a3SMike Smith for (i = (sizeof(me->me_dead) / sizeof(me->me_dead[0])) - 1; i >= 0; i--) { 1084da8bb3a3SMike Smith me->me_dead[i].dd_chan = meo->me_dead[i].dd_chan; 1085da8bb3a3SMike Smith me->me_dead[i].dd_targ = meo->me_dead[i].dd_targ; 1086da8bb3a3SMike Smith } 1087da8bb3a3SMike Smith me->me_misc_flags = 0; 1088da8bb3a3SMike Smith me->me_rebuild_count = meo->me_rebuild_count; 1089da8bb3a3SMike Smith me->me_dead_count = meo->me_dead_count; 1090da8bb3a3SMike Smith me->me_critical_sd_count = meo->me_critical_sd_count; 1091da8bb3a3SMike Smith me->me_event_log_seq_num = 0; 1092da8bb3a3SMike Smith me->me_offline_sd_count = meo->me_offline_sd_count; 1093da8bb3a3SMike Smith me->me_max_commands = meo->me_max_commands; 1094da8bb3a3SMike Smith me->me_rebuild_flag = meo->me_rebuild_flag; 1095da8bb3a3SMike Smith me->me_fwmajor = meo->me_fwmajor; 1096da8bb3a3SMike Smith me->me_fwminor = meo->me_fwminor; 1097da8bb3a3SMike Smith me->me_status_flags = meo->me_status_flags; 1098da8bb3a3SMike Smith me->me_flash_age = meo->me_flash_age; 1099da8bb3a3SMike Smith for (i = (sizeof(me->me_drvsize) / sizeof(me->me_drvsize[0])) - 1; i >= 0; i--) { 1100da8bb3a3SMike Smith if (i > ((sizeof(meo->me_drvsize) / sizeof(meo->me_drvsize[0])) - 1)) { 1101da8bb3a3SMike Smith me->me_drvsize[i] = 0; /* drive beyond supported range */ 1102da8bb3a3SMike Smith } else { 1103da8bb3a3SMike Smith me->me_drvsize[i] = meo->me_drvsize[i]; 1104da8bb3a3SMike Smith } 1105da8bb3a3SMike Smith } 1106da8bb3a3SMike Smith me->me_num_sys_drvs = meo->me_num_sys_drvs; 1107da8bb3a3SMike Smith } 1108da8bb3a3SMike Smith /* FALLTHROUGH */ 1109da8bb3a3SMike Smith 1110da8bb3a3SMike Smith /* 11111ac4b82bSMike Smith * Generic controller status update. We could do more with this than just 11121ac4b82bSMike Smith * checking the event log. 11131ac4b82bSMike Smith */ 11141ac4b82bSMike Smith case MLX_CMD_ENQUIRY: 11151ac4b82bSMike Smith { 11161ac4b82bSMike Smith struct mlx_enquiry *me = (struct mlx_enquiry *)mc->mc_data; 11171ac4b82bSMike Smith 1118421f2f7dSMike Smith if (sc->mlx_currevent == -1) { 11199eee27f1SMike Smith /* initialise our view of the event log */ 11209eee27f1SMike Smith sc->mlx_currevent = sc->mlx_lastevent = me->me_event_log_seq_num; 11215d278f5cSMike Smith } else if ((me->me_event_log_seq_num != sc->mlx_lastevent) && !(sc->mlx_flags & MLX_EVENTLOG_BUSY)) { 11221ac4b82bSMike Smith /* record where current events are up to */ 11231ac4b82bSMike Smith sc->mlx_currevent = me->me_event_log_seq_num; 1124da8bb3a3SMike Smith debug(1, "event log pointer was %d, now %d\n", sc->mlx_lastevent, sc->mlx_currevent); 11251ac4b82bSMike Smith 11265d278f5cSMike Smith /* mark the event log as busy */ 11275d278f5cSMike Smith atomic_set_int(&sc->mlx_flags, MLX_EVENTLOG_BUSY); 11285d278f5cSMike Smith 11299eee27f1SMike Smith /* drain new eventlog entries */ 11301ac4b82bSMike Smith mlx_periodic_eventlog_poll(sc); 11311ac4b82bSMike Smith } 11321ac4b82bSMike Smith break; 11331ac4b82bSMike Smith } 11341ac4b82bSMike Smith case MLX_CMD_ENQSYSDRIVE: 11351ac4b82bSMike Smith { 11361ac4b82bSMike Smith struct mlx_enq_sys_drive *mes = (struct mlx_enq_sys_drive *)mc->mc_data; 11371ac4b82bSMike Smith struct mlx_sysdrive *dr; 11381ac4b82bSMike Smith int i; 11391ac4b82bSMike Smith 11401ac4b82bSMike Smith for (i = 0, dr = &sc->mlx_sysdrive[0]; 11411ac4b82bSMike Smith (i < MLX_MAXDRIVES) && (mes[i].sd_size != 0xffffffff); 11421ac4b82bSMike Smith i++) { 11431ac4b82bSMike Smith 11441ac4b82bSMike Smith /* has state been changed by controller? */ 11451ac4b82bSMike Smith if (dr->ms_state != mes[i].sd_state) { 11461ac4b82bSMike Smith switch(mes[i].sd_state) { 11471ac4b82bSMike Smith case MLX_SYSD_OFFLINE: 11481ac4b82bSMike Smith device_printf(dr->ms_disk, "drive offline\n"); 11491ac4b82bSMike Smith break; 11501ac4b82bSMike Smith case MLX_SYSD_ONLINE: 11511ac4b82bSMike Smith device_printf(dr->ms_disk, "drive online\n"); 11521ac4b82bSMike Smith break; 11531ac4b82bSMike Smith case MLX_SYSD_CRITICAL: 11541ac4b82bSMike Smith device_printf(dr->ms_disk, "drive critical\n"); 11551ac4b82bSMike Smith break; 11561ac4b82bSMike Smith } 11571ac4b82bSMike Smith /* save new state */ 11581ac4b82bSMike Smith dr->ms_state = mes[i].sd_state; 11591ac4b82bSMike Smith } 11601ac4b82bSMike Smith } 11611ac4b82bSMike Smith break; 11621ac4b82bSMike Smith } 11631ac4b82bSMike Smith default: 11646e551fb6SDavid E. O'Brien device_printf(sc->mlx_dev, "%s: unknown command 0x%x", __func__, mc->mc_mailbox[0]); 11651ac4b82bSMike Smith break; 11661ac4b82bSMike Smith } 11671ac4b82bSMike Smith 11681ac4b82bSMike Smith out: 11691ac4b82bSMike Smith free(mc->mc_data, M_DEVBUF); 11701ac4b82bSMike Smith mlx_releasecmd(mc); 11711ac4b82bSMike Smith } 11721ac4b82bSMike Smith 11731b4404f9SScott Long static void 11741b4404f9SScott Long mlx_eventlog_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error) 11751b4404f9SScott Long { 11761b4404f9SScott Long struct mlx_command *mc; 11771b4404f9SScott Long 11781b4404f9SScott Long mc = (struct mlx_command *)arg; 11791b4404f9SScott Long mlx_setup_dmamap(mc, segs, nsegments, error); 11801b4404f9SScott Long 11811b4404f9SScott Long /* build the command to get one entry */ 11821b4404f9SScott Long mlx_make_type3(mc, MLX_CMD_LOGOP, MLX_LOGOP_GET, 1, 11831b4404f9SScott Long mc->mc_sc->mlx_lastevent, 0, 0, mc->mc_dataphys, 0); 11841b4404f9SScott Long mc->mc_complete = mlx_periodic_eventlog_respond; 11851b4404f9SScott Long mc->mc_private = mc; 11861b4404f9SScott Long 11871b4404f9SScott Long /* start the command */ 11881b4404f9SScott Long if (mlx_start(mc) != 0) { 11891b4404f9SScott Long mlx_releasecmd(mc); 11901b4404f9SScott Long free(mc->mc_data, M_DEVBUF); 11911b4404f9SScott Long mc->mc_data = NULL; 11921b4404f9SScott Long } 11931b4404f9SScott Long 11941b4404f9SScott Long } 11951b4404f9SScott Long 11961ac4b82bSMike Smith /******************************************************************************** 11971ac4b82bSMike Smith * Instigate a poll for one event log message on (sc). 11981ac4b82bSMike Smith * We only poll for one message at a time, to keep our command usage down. 11991ac4b82bSMike Smith */ 12001ac4b82bSMike Smith static void 12011ac4b82bSMike Smith mlx_periodic_eventlog_poll(struct mlx_softc *sc) 12021ac4b82bSMike Smith { 12031ac4b82bSMike Smith struct mlx_command *mc; 12041ac4b82bSMike Smith void *result = NULL; 12051b4404f9SScott Long int error = 0; 12061ac4b82bSMike Smith 1207da8bb3a3SMike Smith debug_called(1); 12081ac4b82bSMike Smith 12091ac4b82bSMike Smith /* get ourselves a command buffer */ 12101ac4b82bSMike Smith error = 1; 12111ac4b82bSMike Smith if ((mc = mlx_alloccmd(sc)) == NULL) 12121ac4b82bSMike Smith goto out; 12131b4404f9SScott Long 12141ac4b82bSMike Smith /* allocate the response structure */ 12151b4404f9SScott Long if ((result = malloc(/*sizeof(struct mlx_eventlog_entry)*/1024, M_DEVBUF, 12161b4404f9SScott Long M_NOWAIT)) == NULL) 12171ac4b82bSMike Smith goto out; 12181b4404f9SScott Long 12191ac4b82bSMike Smith /* get a command slot */ 12201ac4b82bSMike Smith if (mlx_getslot(mc)) 12211ac4b82bSMike Smith goto out; 12221ac4b82bSMike Smith 12231ac4b82bSMike Smith /* map the command so the controller can see it */ 12241ac4b82bSMike Smith mc->mc_data = result; 122533c8cb18SMike Smith mc->mc_length = /*sizeof(struct mlx_eventlog_entry)*/1024; 12261b4404f9SScott Long error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data, 12271b4404f9SScott Long mc->mc_length, mlx_eventlog_cb, mc, BUS_DMA_NOWAIT); 12281ac4b82bSMike Smith 12291ac4b82bSMike Smith out: 123033c8cb18SMike Smith if (error != 0) { 12311ac4b82bSMike Smith if (mc != NULL) 12321ac4b82bSMike Smith mlx_releasecmd(mc); 12331b4404f9SScott Long if ((result != NULL) && (mc->mc_data != NULL)) 12341ac4b82bSMike Smith free(result, M_DEVBUF); 12351ac4b82bSMike Smith } 123633c8cb18SMike Smith } 12371ac4b82bSMike Smith 12381ac4b82bSMike Smith /******************************************************************************** 12391ac4b82bSMike Smith * Handle the result of polling for a log message, generate diagnostic output. 12401ac4b82bSMike Smith * If this wasn't the last message waiting for us, we'll go collect another. 12411ac4b82bSMike Smith */ 12421ac4b82bSMike Smith static char *mlx_sense_messages[] = { 12431ac4b82bSMike Smith "because write recovery failed", 12441ac4b82bSMike Smith "because of SCSI bus reset failure", 12451ac4b82bSMike Smith "because of double check condition", 12461ac4b82bSMike Smith "because it was removed", 12471ac4b82bSMike Smith "because of gross error on SCSI chip", 12481ac4b82bSMike Smith "because of bad tag returned from drive", 12491ac4b82bSMike Smith "because of timeout on SCSI command", 12501ac4b82bSMike Smith "because of reset SCSI command issued from system", 12511ac4b82bSMike Smith "because busy or parity error count exceeded limit", 12521ac4b82bSMike Smith "because of 'kill drive' command from system", 12531ac4b82bSMike Smith "because of selection timeout", 12541ac4b82bSMike Smith "due to SCSI phase sequence error", 12551ac4b82bSMike Smith "due to unknown status" 12561ac4b82bSMike Smith }; 12571ac4b82bSMike Smith 12581ac4b82bSMike Smith static void 12591ac4b82bSMike Smith mlx_periodic_eventlog_respond(struct mlx_command *mc) 12601ac4b82bSMike Smith { 12611ac4b82bSMike Smith struct mlx_softc *sc = mc->mc_sc; 12621ac4b82bSMike Smith struct mlx_eventlog_entry *el = (struct mlx_eventlog_entry *)mc->mc_data; 12631ac4b82bSMike Smith char *reason; 12641ac4b82bSMike Smith 1265da8bb3a3SMike Smith debug_called(1); 12661ac4b82bSMike Smith 12675792b7feSMike Smith sc->mlx_lastevent++; /* next message... */ 12681ac4b82bSMike Smith if (mc->mc_status == 0) { 12691ac4b82bSMike Smith 12701ac4b82bSMike Smith /* handle event log message */ 12711ac4b82bSMike Smith switch(el->el_type) { 12721ac4b82bSMike Smith /* 12731ac4b82bSMike Smith * This is the only sort of message we understand at the moment. 12741ac4b82bSMike Smith * The tests here are probably incomplete. 12751ac4b82bSMike Smith */ 12761ac4b82bSMike Smith case MLX_LOGMSG_SENSE: /* sense data */ 12771ac4b82bSMike Smith /* Mylex vendor-specific message indicating a drive was killed? */ 12781ac4b82bSMike Smith if ((el->el_sensekey == 9) && 12791ac4b82bSMike Smith (el->el_asc == 0x80)) { 12801ac4b82bSMike Smith if (el->el_asq < (sizeof(mlx_sense_messages) / sizeof(mlx_sense_messages[0]))) { 12811ac4b82bSMike Smith reason = mlx_sense_messages[el->el_asq]; 12821ac4b82bSMike Smith } else { 12831ac4b82bSMike Smith reason = "for unknown reason"; 12841ac4b82bSMike Smith } 12851ac4b82bSMike Smith device_printf(sc->mlx_dev, "physical drive %d:%d killed %s\n", 12861ac4b82bSMike Smith el->el_channel, el->el_target, reason); 12871ac4b82bSMike Smith } 12881ac4b82bSMike Smith /* SCSI drive was reset? */ 12891ac4b82bSMike Smith if ((el->el_sensekey == 6) && (el->el_asc == 0x29)) { 12901ac4b82bSMike Smith device_printf(sc->mlx_dev, "physical drive %d:%d reset\n", 12911ac4b82bSMike Smith el->el_channel, el->el_target); 12921ac4b82bSMike Smith } 12931ac4b82bSMike Smith /* SCSI drive error? */ 12941ac4b82bSMike Smith if (!((el->el_sensekey == 0) || 12951ac4b82bSMike Smith ((el->el_sensekey == 2) && 12961ac4b82bSMike Smith (el->el_asc == 0x04) && 12971ac4b82bSMike Smith ((el->el_asq == 0x01) || 12981ac4b82bSMike Smith (el->el_asq == 0x02))))) { 12991ac4b82bSMike Smith device_printf(sc->mlx_dev, "physical drive %d:%d error log: sense = %d asc = %x asq = %x\n", 13001ac4b82bSMike Smith el->el_channel, el->el_target, el->el_sensekey, el->el_asc, el->el_asq); 13011ac4b82bSMike Smith device_printf(sc->mlx_dev, " info %4D csi %4D\n", el->el_information, ":", el->el_csi, ":"); 13021ac4b82bSMike Smith } 13031ac4b82bSMike Smith break; 13041ac4b82bSMike Smith 13051ac4b82bSMike Smith default: 13061ac4b82bSMike Smith device_printf(sc->mlx_dev, "unknown log message type 0x%x\n", el->el_type); 13071ac4b82bSMike Smith break; 13081ac4b82bSMike Smith } 13091ac4b82bSMike Smith } else { 13101ac4b82bSMike Smith device_printf(sc->mlx_dev, "error reading message log - %s\n", mlx_diagnose_command(mc)); 13115d278f5cSMike Smith /* give up on all the outstanding messages, as we may have come unsynched */ 13125d278f5cSMike Smith sc->mlx_lastevent = sc->mlx_currevent; 13131ac4b82bSMike Smith } 13141ac4b82bSMike Smith 13151ac4b82bSMike Smith /* dispose of command and data */ 13161ac4b82bSMike Smith free(mc->mc_data, M_DEVBUF); 13171ac4b82bSMike Smith mlx_releasecmd(mc); 13181ac4b82bSMike Smith 13191ac4b82bSMike Smith /* is there another message to obtain? */ 13205d278f5cSMike Smith if (sc->mlx_lastevent != sc->mlx_currevent) { 13211ac4b82bSMike Smith mlx_periodic_eventlog_poll(sc); 13225d278f5cSMike Smith } else { 13235d278f5cSMike Smith /* clear log-busy status */ 13245d278f5cSMike Smith atomic_clear_int(&sc->mlx_flags, MLX_EVENTLOG_BUSY); 13255d278f5cSMike Smith } 13261ac4b82bSMike Smith } 13271ac4b82bSMike Smith 13281ac4b82bSMike Smith /******************************************************************************** 1329421f2f7dSMike Smith * Handle check/rebuild operations in progress. 13301ac4b82bSMike Smith */ 13311ac4b82bSMike Smith static void 13321ac4b82bSMike Smith mlx_periodic_rebuild(struct mlx_command *mc) 13331ac4b82bSMike Smith { 13341ac4b82bSMike Smith struct mlx_softc *sc = mc->mc_sc; 1335421f2f7dSMike Smith struct mlx_rebuild_status *mr = (struct mlx_rebuild_status *)mc->mc_data; 13361ac4b82bSMike Smith 13371ac4b82bSMike Smith switch(mc->mc_status) { 1338421f2f7dSMike Smith case 0: /* operation running, update stats */ 1339421f2f7dSMike Smith sc->mlx_rebuildstat = *mr; 1340421f2f7dSMike Smith 1341421f2f7dSMike Smith /* spontaneous rebuild/check? */ 1342421f2f7dSMike Smith if (sc->mlx_background == 0) { 1343421f2f7dSMike Smith sc->mlx_background = MLX_BACKGROUND_SPONTANEOUS; 1344421f2f7dSMike Smith device_printf(sc->mlx_dev, "background check/rebuild operation started\n"); 1345421f2f7dSMike Smith } 13461ac4b82bSMike Smith break; 13471ac4b82bSMike Smith 1348421f2f7dSMike Smith case 0x0105: /* nothing running, finalise stats and report */ 1349421f2f7dSMike Smith switch(sc->mlx_background) { 1350421f2f7dSMike Smith case MLX_BACKGROUND_CHECK: 1351421f2f7dSMike Smith device_printf(sc->mlx_dev, "consistency check completed\n"); /* XXX print drive? */ 1352421f2f7dSMike Smith break; 1353421f2f7dSMike Smith case MLX_BACKGROUND_REBUILD: 1354421f2f7dSMike Smith device_printf(sc->mlx_dev, "drive rebuild completed\n"); /* XXX print channel/target? */ 1355421f2f7dSMike Smith break; 1356421f2f7dSMike Smith case MLX_BACKGROUND_SPONTANEOUS: 1357421f2f7dSMike Smith default: 1358421f2f7dSMike Smith /* if we have previously been non-idle, report the transition */ 1359421f2f7dSMike Smith if (sc->mlx_rebuildstat.rs_code != MLX_REBUILDSTAT_IDLE) { 1360421f2f7dSMike Smith device_printf(sc->mlx_dev, "background check/rebuild operation completed\n"); 13611ac4b82bSMike Smith } 1362421f2f7dSMike Smith } 1363421f2f7dSMike Smith sc->mlx_background = 0; 1364421f2f7dSMike Smith sc->mlx_rebuildstat.rs_code = MLX_REBUILDSTAT_IDLE; 13651ac4b82bSMike Smith break; 13661ac4b82bSMike Smith } 13671ac4b82bSMike Smith free(mc->mc_data, M_DEVBUF); 13681ac4b82bSMike Smith mlx_releasecmd(mc); 13691ac4b82bSMike Smith } 13701ac4b82bSMike Smith 13711ac4b82bSMike Smith /******************************************************************************** 13721ac4b82bSMike Smith ******************************************************************************** 13731ac4b82bSMike Smith Channel Pause 13741ac4b82bSMike Smith ******************************************************************************** 13751ac4b82bSMike Smith ********************************************************************************/ 13761ac4b82bSMike Smith 13771ac4b82bSMike Smith /******************************************************************************** 13781ac4b82bSMike Smith * It's time to perform a channel pause action for (sc), either start or stop 13791ac4b82bSMike Smith * the pause. 13801ac4b82bSMike Smith */ 13811ac4b82bSMike Smith static void 13821ac4b82bSMike Smith mlx_pause_action(struct mlx_softc *sc) 13831ac4b82bSMike Smith { 13841ac4b82bSMike Smith struct mlx_command *mc; 13851ac4b82bSMike Smith int failsafe, i, command; 13861ac4b82bSMike Smith 13871ac4b82bSMike Smith /* What are we doing here? */ 13881ac4b82bSMike Smith if (sc->mlx_pause.mp_when == 0) { 13891ac4b82bSMike Smith command = MLX_CMD_STARTCHANNEL; 13901ac4b82bSMike Smith failsafe = 0; 13911ac4b82bSMike Smith 13921ac4b82bSMike Smith } else { 13931ac4b82bSMike Smith command = MLX_CMD_STOPCHANNEL; 13941ac4b82bSMike Smith 13951ac4b82bSMike Smith /* 13961ac4b82bSMike Smith * Channels will always start again after the failsafe period, 13971ac4b82bSMike Smith * which is specified in multiples of 30 seconds. 13981ac4b82bSMike Smith * This constrains us to a maximum pause of 450 seconds. 13991ac4b82bSMike Smith */ 14001ac4b82bSMike Smith failsafe = ((sc->mlx_pause.mp_howlong - time_second) + 5) / 30; 14011ac4b82bSMike Smith if (failsafe > 0xf) { 14021ac4b82bSMike Smith failsafe = 0xf; 14031ac4b82bSMike Smith sc->mlx_pause.mp_howlong = time_second + (0xf * 30) - 5; 14041ac4b82bSMike Smith } 14051ac4b82bSMike Smith } 14061ac4b82bSMike Smith 14071ac4b82bSMike Smith /* build commands for every channel requested */ 14089eee27f1SMike Smith for (i = 0; i < sc->mlx_enq2->me_actual_channels; i++) { 14091ac4b82bSMike Smith if ((1 << i) & sc->mlx_pause.mp_which) { 14101ac4b82bSMike Smith 14111ac4b82bSMike Smith /* get ourselves a command buffer */ 14121ac4b82bSMike Smith if ((mc = mlx_alloccmd(sc)) == NULL) 14131ac4b82bSMike Smith goto fail; 14141ac4b82bSMike Smith /* get a command slot */ 14151ac4b82bSMike Smith mc->mc_flags |= MLX_CMD_PRIORITY; 14161ac4b82bSMike Smith if (mlx_getslot(mc)) 14171ac4b82bSMike Smith goto fail; 14181ac4b82bSMike Smith 14191ac4b82bSMike Smith /* build the command */ 14201ac4b82bSMike Smith mlx_make_type2(mc, command, (failsafe << 4) | i, 0, 0, 0, 0, 0, 0, 0); 14211ac4b82bSMike Smith mc->mc_complete = mlx_pause_done; 14221ac4b82bSMike Smith mc->mc_private = sc; /* XXX not needed */ 14231ac4b82bSMike Smith if (mlx_start(mc)) 14241ac4b82bSMike Smith goto fail; 14251ac4b82bSMike Smith /* command submitted OK */ 14261ac4b82bSMike Smith return; 14271ac4b82bSMike Smith 14281ac4b82bSMike Smith fail: 14291ac4b82bSMike Smith device_printf(sc->mlx_dev, "%s failed for channel %d\n", 14301ac4b82bSMike Smith command == MLX_CMD_STOPCHANNEL ? "pause" : "resume", i); 14311ac4b82bSMike Smith if (mc != NULL) 14321ac4b82bSMike Smith mlx_releasecmd(mc); 14331ac4b82bSMike Smith } 14341ac4b82bSMike Smith } 14351ac4b82bSMike Smith } 14361ac4b82bSMike Smith 14371ac4b82bSMike Smith static void 14381ac4b82bSMike Smith mlx_pause_done(struct mlx_command *mc) 14391ac4b82bSMike Smith { 14401ac4b82bSMike Smith struct mlx_softc *sc = mc->mc_sc; 14411ac4b82bSMike Smith int command = mc->mc_mailbox[0]; 14421ac4b82bSMike Smith int channel = mc->mc_mailbox[2] & 0xf; 14431ac4b82bSMike Smith 14441ac4b82bSMike Smith if (mc->mc_status != 0) { 14451ac4b82bSMike Smith device_printf(sc->mlx_dev, "%s command failed - %s\n", 14461ac4b82bSMike Smith command == MLX_CMD_STOPCHANNEL ? "pause" : "resume", mlx_diagnose_command(mc)); 14471ac4b82bSMike Smith } else if (command == MLX_CMD_STOPCHANNEL) { 14481ac4b82bSMike Smith device_printf(sc->mlx_dev, "channel %d pausing for %ld seconds\n", 144972c10febSPeter Wemm channel, (long)(sc->mlx_pause.mp_howlong - time_second)); 14501ac4b82bSMike Smith } else { 14511ac4b82bSMike Smith device_printf(sc->mlx_dev, "channel %d resuming\n", channel); 14521ac4b82bSMike Smith } 14531ac4b82bSMike Smith mlx_releasecmd(mc); 14541ac4b82bSMike Smith } 14551ac4b82bSMike Smith 14561ac4b82bSMike Smith /******************************************************************************** 14571ac4b82bSMike Smith ******************************************************************************** 14581ac4b82bSMike Smith Command Submission 14591ac4b82bSMike Smith ******************************************************************************** 14601ac4b82bSMike Smith ********************************************************************************/ 14611ac4b82bSMike Smith 14621b4404f9SScott Long static void 14631b4404f9SScott Long mlx_enquire_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error) 14641b4404f9SScott Long { 14651b4404f9SScott Long struct mlx_softc *sc; 14661b4404f9SScott Long struct mlx_command *mc; 14671b4404f9SScott Long 14681b4404f9SScott Long mc = (struct mlx_command *)arg; 14691b4404f9SScott Long if (error) 14701b4404f9SScott Long return; 14711b4404f9SScott Long 14721b4404f9SScott Long mlx_setup_dmamap(mc, segs, nsegments, error); 14731b4404f9SScott Long 14741b4404f9SScott Long /* build an enquiry command */ 14751b4404f9SScott Long sc = mc->mc_sc; 14761b4404f9SScott Long mlx_make_type2(mc, mc->mc_command, 0, 0, 0, 0, 0, 0, mc->mc_dataphys, 0); 14771b4404f9SScott Long 14781b4404f9SScott Long /* do we want a completion callback? */ 14791b4404f9SScott Long if (mc->mc_complete != NULL) { 14801b4404f9SScott Long if ((error = mlx_start(mc)) != 0) 14811b4404f9SScott Long return; 14821b4404f9SScott Long } else { 14831b4404f9SScott Long /* run the command in either polled or wait mode */ 14841b4404f9SScott Long if ((sc->mlx_state & MLX_STATE_INTEN) ? mlx_wait_command(mc) : 14851b4404f9SScott Long mlx_poll_command(mc)) 14861b4404f9SScott Long return; 14871b4404f9SScott Long 14881b4404f9SScott Long /* command completed OK? */ 14891b4404f9SScott Long if (mc->mc_status != 0) { 14901b4404f9SScott Long device_printf(sc->mlx_dev, "ENQUIRY failed - %s\n", 14911b4404f9SScott Long mlx_diagnose_command(mc)); 14921b4404f9SScott Long return; 14931b4404f9SScott Long } 14941b4404f9SScott Long } 14951b4404f9SScott Long } 14961b4404f9SScott Long 14971ac4b82bSMike Smith /******************************************************************************** 14981ac4b82bSMike Smith * Perform an Enquiry command using a type-3 command buffer and a return a single 14991ac4b82bSMike Smith * linear result buffer. If the completion function is specified, it will 15001ac4b82bSMike Smith * be called with the completed command (and the result response will not be 15011ac4b82bSMike Smith * valid until that point). Otherwise, the command will either be busy-waited 15021ac4b82bSMike Smith * for (interrupts not enabled), or slept for. 15031ac4b82bSMike Smith */ 15041ac4b82bSMike Smith static void * 15051ac4b82bSMike Smith mlx_enquire(struct mlx_softc *sc, int command, size_t bufsize, void (* complete)(struct mlx_command *mc)) 15061ac4b82bSMike Smith { 15071ac4b82bSMike Smith struct mlx_command *mc; 15081ac4b82bSMike Smith void *result; 15091ac4b82bSMike Smith int error; 15101ac4b82bSMike Smith 1511da8bb3a3SMike Smith debug_called(1); 15121ac4b82bSMike Smith 15131ac4b82bSMike Smith /* get ourselves a command buffer */ 15141ac4b82bSMike Smith error = 1; 15151ac4b82bSMike Smith result = NULL; 15161ac4b82bSMike Smith if ((mc = mlx_alloccmd(sc)) == NULL) 15171ac4b82bSMike Smith goto out; 15181ac4b82bSMike Smith /* allocate the response structure */ 15191ac4b82bSMike Smith if ((result = malloc(bufsize, M_DEVBUF, M_NOWAIT)) == NULL) 15201ac4b82bSMike Smith goto out; 15211ac4b82bSMike Smith /* get a command slot */ 15221ac4b82bSMike Smith mc->mc_flags |= MLX_CMD_PRIORITY | MLX_CMD_DATAOUT; 15231ac4b82bSMike Smith if (mlx_getslot(mc)) 15241ac4b82bSMike Smith goto out; 15251ac4b82bSMike Smith 15261ac4b82bSMike Smith /* map the command so the controller can see it */ 15271ac4b82bSMike Smith mc->mc_data = result; 15281ac4b82bSMike Smith mc->mc_length = bufsize; 15291b4404f9SScott Long mc->mc_command = command; 15301ac4b82bSMike Smith 15311ac4b82bSMike Smith if (complete != NULL) { 15321ac4b82bSMike Smith mc->mc_complete = complete; 15331ac4b82bSMike Smith mc->mc_private = mc; 15341b4404f9SScott Long } 15351ac4b82bSMike Smith 15361b4404f9SScott Long error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data, 15371b4404f9SScott Long mc->mc_length, mlx_enquire_cb, mc, BUS_DMA_NOWAIT); 15381b4404f9SScott Long 15391ac4b82bSMike Smith out: 15401ac4b82bSMike Smith /* we got a command, but nobody else will free it */ 154123691262SSam Leffler if ((mc != NULL) && (mc->mc_complete == NULL)) 15421ac4b82bSMike Smith mlx_releasecmd(mc); 154333c8cb18SMike Smith /* we got an error, and we allocated a result */ 1544d4881905SScott Long if ((error != 0) && (result != NULL)) { 1545d4881905SScott Long free(result, M_DEVBUF); 1546a7700303SScott Long result = NULL; 15471ac4b82bSMike Smith } 15481ac4b82bSMike Smith return(result); 15491ac4b82bSMike Smith } 15501ac4b82bSMike Smith 15511ac4b82bSMike Smith 15521ac4b82bSMike Smith /******************************************************************************** 15531ac4b82bSMike Smith * Perform a Flush command on the nominated controller. 15541ac4b82bSMike Smith * 15551ac4b82bSMike Smith * May be called with interrupts enabled or disabled; will not return until 15561ac4b82bSMike Smith * the flush operation completes or fails. 15571ac4b82bSMike Smith */ 15581ac4b82bSMike Smith static int 15591ac4b82bSMike Smith mlx_flush(struct mlx_softc *sc) 15601ac4b82bSMike Smith { 15611ac4b82bSMike Smith struct mlx_command *mc; 15621ac4b82bSMike Smith int error; 15631ac4b82bSMike Smith 1564da8bb3a3SMike Smith debug_called(1); 15651ac4b82bSMike Smith 15661ac4b82bSMike Smith /* get ourselves a command buffer */ 15671ac4b82bSMike Smith error = 1; 15681ac4b82bSMike Smith if ((mc = mlx_alloccmd(sc)) == NULL) 15691ac4b82bSMike Smith goto out; 15701ac4b82bSMike Smith /* get a command slot */ 15711ac4b82bSMike Smith if (mlx_getslot(mc)) 15721ac4b82bSMike Smith goto out; 15731ac4b82bSMike Smith 15741ac4b82bSMike Smith /* build a flush command */ 15751ac4b82bSMike Smith mlx_make_type2(mc, MLX_CMD_FLUSH, 0, 0, 0, 0, 0, 0, 0, 0); 15761ac4b82bSMike Smith 15775792b7feSMike Smith /* can't assume that interrupts are going to work here, so play it safe */ 15785792b7feSMike Smith if (mlx_poll_command(mc)) 15791ac4b82bSMike Smith goto out; 15801ac4b82bSMike Smith 15811ac4b82bSMike Smith /* command completed OK? */ 15821ac4b82bSMike Smith if (mc->mc_status != 0) { 15831ac4b82bSMike Smith device_printf(sc->mlx_dev, "FLUSH failed - %s\n", mlx_diagnose_command(mc)); 15841ac4b82bSMike Smith goto out; 15851ac4b82bSMike Smith } 15861ac4b82bSMike Smith 15871ac4b82bSMike Smith error = 0; /* success */ 15881ac4b82bSMike Smith out: 15891ac4b82bSMike Smith if (mc != NULL) 15901ac4b82bSMike Smith mlx_releasecmd(mc); 15911ac4b82bSMike Smith return(error); 15921ac4b82bSMike Smith } 15931ac4b82bSMike Smith 15941ac4b82bSMike Smith /******************************************************************************** 1595421f2f7dSMike Smith * Start a background consistency check on (drive). 1596421f2f7dSMike Smith * 1597421f2f7dSMike Smith * May be called with interrupts enabled or disabled; will return as soon as the 1598421f2f7dSMike Smith * operation has started or been refused. 1599421f2f7dSMike Smith */ 1600421f2f7dSMike Smith static int 1601421f2f7dSMike Smith mlx_check(struct mlx_softc *sc, int drive) 1602421f2f7dSMike Smith { 1603421f2f7dSMike Smith struct mlx_command *mc; 1604421f2f7dSMike Smith int error; 1605421f2f7dSMike Smith 1606421f2f7dSMike Smith debug_called(1); 1607421f2f7dSMike Smith 1608421f2f7dSMike Smith /* get ourselves a command buffer */ 1609421f2f7dSMike Smith error = 0x10000; 1610421f2f7dSMike Smith if ((mc = mlx_alloccmd(sc)) == NULL) 1611421f2f7dSMike Smith goto out; 1612421f2f7dSMike Smith /* get a command slot */ 1613421f2f7dSMike Smith if (mlx_getslot(mc)) 1614421f2f7dSMike Smith goto out; 1615421f2f7dSMike Smith 1616421f2f7dSMike Smith /* build a checkasync command, set the "fix it" flag */ 1617421f2f7dSMike Smith mlx_make_type2(mc, MLX_CMD_CHECKASYNC, 0, 0, 0, 0, 0, drive | 0x80, 0, 0); 1618421f2f7dSMike Smith 1619421f2f7dSMike Smith /* start the command and wait for it to be returned */ 1620421f2f7dSMike Smith if (mlx_wait_command(mc)) 1621421f2f7dSMike Smith goto out; 1622421f2f7dSMike Smith 1623421f2f7dSMike Smith /* command completed OK? */ 1624421f2f7dSMike Smith if (mc->mc_status != 0) { 1625421f2f7dSMike Smith device_printf(sc->mlx_dev, "CHECK ASYNC failed - %s\n", mlx_diagnose_command(mc)); 1626421f2f7dSMike Smith } else { 1627421f2f7dSMike Smith device_printf(sc->mlx_sysdrive[drive].ms_disk, "consistency check started"); 1628421f2f7dSMike Smith } 1629421f2f7dSMike Smith error = mc->mc_status; 1630421f2f7dSMike Smith 1631421f2f7dSMike Smith out: 1632421f2f7dSMike Smith if (mc != NULL) 1633421f2f7dSMike Smith mlx_releasecmd(mc); 1634421f2f7dSMike Smith return(error); 1635421f2f7dSMike Smith } 1636421f2f7dSMike Smith 1637421f2f7dSMike Smith /******************************************************************************** 1638421f2f7dSMike Smith * Start a background rebuild of the physical drive at (channel),(target). 16391ac4b82bSMike Smith * 16401ac4b82bSMike Smith * May be called with interrupts enabled or disabled; will return as soon as the 16411ac4b82bSMike Smith * operation has started or been refused. 16421ac4b82bSMike Smith */ 16431ac4b82bSMike Smith static int 16441ac4b82bSMike Smith mlx_rebuild(struct mlx_softc *sc, int channel, int target) 16451ac4b82bSMike Smith { 16461ac4b82bSMike Smith struct mlx_command *mc; 16471ac4b82bSMike Smith int error; 16481ac4b82bSMike Smith 1649da8bb3a3SMike Smith debug_called(1); 16501ac4b82bSMike Smith 16511ac4b82bSMike Smith /* get ourselves a command buffer */ 16521ac4b82bSMike Smith error = 0x10000; 16531ac4b82bSMike Smith if ((mc = mlx_alloccmd(sc)) == NULL) 16541ac4b82bSMike Smith goto out; 16551ac4b82bSMike Smith /* get a command slot */ 16561ac4b82bSMike Smith if (mlx_getslot(mc)) 16571ac4b82bSMike Smith goto out; 16581ac4b82bSMike Smith 1659421f2f7dSMike Smith /* build a checkasync command, set the "fix it" flag */ 16601ac4b82bSMike Smith mlx_make_type2(mc, MLX_CMD_REBUILDASYNC, channel, target, 0, 0, 0, 0, 0, 0); 16611ac4b82bSMike Smith 1662421f2f7dSMike Smith /* start the command and wait for it to be returned */ 1663421f2f7dSMike Smith if (mlx_wait_command(mc)) 16641ac4b82bSMike Smith goto out; 16651ac4b82bSMike Smith 16661ac4b82bSMike Smith /* command completed OK? */ 16671ac4b82bSMike Smith if (mc->mc_status != 0) { 16681ac4b82bSMike Smith device_printf(sc->mlx_dev, "REBUILD ASYNC failed - %s\n", mlx_diagnose_command(mc)); 16691ac4b82bSMike Smith } else { 1670421f2f7dSMike Smith device_printf(sc->mlx_dev, "drive rebuild started for %d:%d\n", channel, target); 16711ac4b82bSMike Smith } 16721ac4b82bSMike Smith error = mc->mc_status; 16731ac4b82bSMike Smith 16741ac4b82bSMike Smith out: 16751ac4b82bSMike Smith if (mc != NULL) 16761ac4b82bSMike Smith mlx_releasecmd(mc); 16771ac4b82bSMike Smith return(error); 16781ac4b82bSMike Smith } 16791ac4b82bSMike Smith 16801ac4b82bSMike Smith /******************************************************************************** 16811ac4b82bSMike Smith * Run the command (mc) and return when it completes. 16821ac4b82bSMike Smith * 16831ac4b82bSMike Smith * Interrupts need to be enabled; returns nonzero on error. 16841ac4b82bSMike Smith */ 16851ac4b82bSMike Smith static int 16861ac4b82bSMike Smith mlx_wait_command(struct mlx_command *mc) 16871ac4b82bSMike Smith { 16881ac4b82bSMike Smith struct mlx_softc *sc = mc->mc_sc; 16891ac4b82bSMike Smith int error, count; 16901ac4b82bSMike Smith 1691da8bb3a3SMike Smith debug_called(1); 16921ac4b82bSMike Smith 16931ac4b82bSMike Smith mc->mc_complete = NULL; 16941ac4b82bSMike Smith mc->mc_private = mc; /* wake us when you're done */ 16951ac4b82bSMike Smith if ((error = mlx_start(mc)) != 0) 16961ac4b82bSMike Smith return(error); 16971ac4b82bSMike Smith 16981ac4b82bSMike Smith count = 0; 16991ac4b82bSMike Smith /* XXX better timeout? */ 17001ac4b82bSMike Smith while ((mc->mc_status == MLX_STATUS_BUSY) && (count < 30)) { 17011ac4b82bSMike Smith tsleep(mc->mc_private, PRIBIO | PCATCH, "mlxwcmd", hz); 17021ac4b82bSMike Smith } 17031ac4b82bSMike Smith 17041ac4b82bSMike Smith if (mc->mc_status != 0) { 1705da8bb3a3SMike Smith device_printf(sc->mlx_dev, "command failed - %s\n", mlx_diagnose_command(mc)); 17061ac4b82bSMike Smith return(EIO); 17071ac4b82bSMike Smith } 17081ac4b82bSMike Smith return(0); 17091ac4b82bSMike Smith } 17101ac4b82bSMike Smith 17111ac4b82bSMike Smith 17121ac4b82bSMike Smith /******************************************************************************** 17131ac4b82bSMike Smith * Start the command (mc) and busy-wait for it to complete. 17141ac4b82bSMike Smith * 1715da8bb3a3SMike Smith * Should only be used when interrupts can't be relied upon. Returns 0 on 17161ac4b82bSMike Smith * success, nonzero on error. 17171ac4b82bSMike Smith * Successfully completed commands are dequeued. 17181ac4b82bSMike Smith */ 17191ac4b82bSMike Smith static int 17201ac4b82bSMike Smith mlx_poll_command(struct mlx_command *mc) 17211ac4b82bSMike Smith { 17221ac4b82bSMike Smith struct mlx_softc *sc = mc->mc_sc; 17231ac4b82bSMike Smith int error, count, s; 17241ac4b82bSMike Smith 1725da8bb3a3SMike Smith debug_called(1); 17261ac4b82bSMike Smith 17271ac4b82bSMike Smith mc->mc_complete = NULL; 17281ac4b82bSMike Smith mc->mc_private = NULL; /* we will poll for it */ 17291ac4b82bSMike Smith if ((error = mlx_start(mc)) != 0) 17301ac4b82bSMike Smith return(error); 17311ac4b82bSMike Smith 17321ac4b82bSMike Smith count = 0; 17331ac4b82bSMike Smith do { 17341ac4b82bSMike Smith /* poll for completion */ 17351ac4b82bSMike Smith mlx_done(mc->mc_sc); 1736da8bb3a3SMike Smith 1737da8bb3a3SMike Smith } while ((mc->mc_status == MLX_STATUS_BUSY) && (count++ < 15000000)); 17381ac4b82bSMike Smith if (mc->mc_status != MLX_STATUS_BUSY) { 17391ac4b82bSMike Smith s = splbio(); 17404b006d7bSMike Smith TAILQ_REMOVE(&sc->mlx_work, mc, mc_link); 17411ac4b82bSMike Smith splx(s); 17421ac4b82bSMike Smith return(0); 17431ac4b82bSMike Smith } 1744421f2f7dSMike Smith device_printf(sc->mlx_dev, "command failed - %s\n", mlx_diagnose_command(mc)); 17451ac4b82bSMike Smith return(EIO); 17461ac4b82bSMike Smith } 17471ac4b82bSMike Smith 17481b4404f9SScott Long void 17491b4404f9SScott Long mlx_startio_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error) 17501b4404f9SScott Long { 17511b4404f9SScott Long struct mlx_command *mc; 17521b4404f9SScott Long struct mlxd_softc *mlxd; 17531b4404f9SScott Long struct mlx_softc *sc; 17541b4404f9SScott Long mlx_bio *bp; 17551b4404f9SScott Long int blkcount; 17561b4404f9SScott Long int driveno; 17571b4404f9SScott Long int cmd; 17581b4404f9SScott Long 17591b4404f9SScott Long mc = (struct mlx_command *)arg; 17601b4404f9SScott Long mlx_setup_dmamap(mc, segs, nsegments, error); 17611b4404f9SScott Long 17621b4404f9SScott Long sc = mc->mc_sc; 17631b4404f9SScott Long bp = mc->mc_private; 17641b4404f9SScott Long 17651b4404f9SScott Long if (MLX_BIO_IS_READ(bp)) { 17661b4404f9SScott Long mc->mc_flags |= MLX_CMD_DATAIN; 17671b4404f9SScott Long cmd = MLX_CMD_READSG; 17681b4404f9SScott Long } else { 17691b4404f9SScott Long mc->mc_flags |= MLX_CMD_DATAOUT; 17701b4404f9SScott Long cmd = MLX_CMD_WRITESG; 17711b4404f9SScott Long } 17721b4404f9SScott Long 17731b4404f9SScott Long /* build a suitable I/O command (assumes 512-byte rounded transfers) */ 17741b4404f9SScott Long mlxd = (struct mlxd_softc *)MLX_BIO_SOFTC(bp); 17751b4404f9SScott Long driveno = mlxd->mlxd_drive - sc->mlx_sysdrive; 17761b4404f9SScott Long blkcount = (MLX_BIO_LENGTH(bp) + MLX_BLKSIZE - 1) / MLX_BLKSIZE; 17771b4404f9SScott Long 17781b4404f9SScott Long if ((MLX_BIO_LBA(bp) + blkcount) > sc->mlx_sysdrive[driveno].ms_size) 17791b4404f9SScott Long device_printf(sc->mlx_dev, 17801b4404f9SScott Long "I/O beyond end of unit (%lld,%d > %lu)\n", 17811b4404f9SScott Long (long long)MLX_BIO_LBA(bp), blkcount, 17821b4404f9SScott Long (u_long)sc->mlx_sysdrive[driveno].ms_size); 17831b4404f9SScott Long 17841b4404f9SScott Long /* 17851b4404f9SScott Long * Build the I/O command. Note that the SG list type bits are set to zero, 17861b4404f9SScott Long * denoting the format of SG list that we are using. 17871b4404f9SScott Long */ 17881b4404f9SScott Long if (sc->mlx_iftype == MLX_IFTYPE_2) { 17891b4404f9SScott Long mlx_make_type1(mc, (cmd == MLX_CMD_WRITESG) ? MLX_CMD_WRITESG_OLD : 17901b4404f9SScott Long MLX_CMD_READSG_OLD, 17911b4404f9SScott Long blkcount & 0xff, /* xfer length low byte */ 17921b4404f9SScott Long MLX_BIO_LBA(bp), /* physical block number */ 17931b4404f9SScott Long driveno, /* target drive number */ 17941b4404f9SScott Long mc->mc_sgphys, /* location of SG list */ 17951b4404f9SScott Long mc->mc_nsgent & 0x3f); /* size of SG list */ 17961b4404f9SScott Long } else { 17971b4404f9SScott Long mlx_make_type5(mc, cmd, 17981b4404f9SScott Long blkcount & 0xff, /* xfer length low byte */ 17991b4404f9SScott Long (driveno << 3) | ((blkcount >> 8) & 0x07), 18001b4404f9SScott Long /* target+length high 3 bits */ 18011b4404f9SScott Long MLX_BIO_LBA(bp), /* physical block number */ 18021b4404f9SScott Long mc->mc_sgphys, /* location of SG list */ 18031b4404f9SScott Long mc->mc_nsgent & 0x3f); /* size of SG list */ 18041b4404f9SScott Long } 18051b4404f9SScott Long 18061b4404f9SScott Long /* try to give command to controller */ 18071b4404f9SScott Long if (mlx_start(mc) != 0) { 18081b4404f9SScott Long /* fail the command */ 18091b4404f9SScott Long mc->mc_status = MLX_STATUS_WEDGED; 18101b4404f9SScott Long mlx_completeio(mc); 18111b4404f9SScott Long } 18121b4404f9SScott Long } 18131b4404f9SScott Long 18141ac4b82bSMike Smith /******************************************************************************** 18151ac4b82bSMike Smith * Pull as much work off the softc's work queue as possible and give it to the 18161ac4b82bSMike Smith * controller. Leave a couple of slots free for emergencies. 18171ac4b82bSMike Smith * 18181ac4b82bSMike Smith * Must be called at splbio or in an equivalent fashion that prevents 18198177437dSPoul-Henning Kamp * reentry or activity on the bioq. 18201ac4b82bSMike Smith */ 18211ac4b82bSMike Smith static void 18221ac4b82bSMike Smith mlx_startio(struct mlx_softc *sc) 18231ac4b82bSMike Smith { 18241ac4b82bSMike Smith struct mlx_command *mc; 182515fd5d22SMike Smith mlx_bio *bp; 18264b006d7bSMike Smith int s; 18271b4404f9SScott Long int error; 18281ac4b82bSMike Smith 18295792b7feSMike Smith /* avoid reentrancy */ 18305792b7feSMike Smith if (mlx_lock_tas(sc, MLX_LOCK_STARTING)) 18315792b7feSMike Smith return; 18325792b7feSMike Smith 18331ac4b82bSMike Smith /* spin until something prevents us from doing any work */ 18344b006d7bSMike Smith s = splbio(); 18351ac4b82bSMike Smith for (;;) { 18361ac4b82bSMike Smith 18371ac4b82bSMike Smith /* see if there's work to be done */ 183815fd5d22SMike Smith if ((bp = MLX_BIO_QFIRST(sc->mlx_bioq)) == NULL) 18391ac4b82bSMike Smith break; 18401ac4b82bSMike Smith /* get a command */ 18411ac4b82bSMike Smith if ((mc = mlx_alloccmd(sc)) == NULL) 18421ac4b82bSMike Smith break; 18431ac4b82bSMike Smith /* get a slot for the command */ 18441ac4b82bSMike Smith if (mlx_getslot(mc) != 0) { 18451ac4b82bSMike Smith mlx_releasecmd(mc); 18461ac4b82bSMike Smith break; 18471ac4b82bSMike Smith } 18481ac4b82bSMike Smith /* get the buf containing our work */ 184915fd5d22SMike Smith MLX_BIO_QREMOVE(sc->mlx_bioq, bp); 18501ac4b82bSMike Smith sc->mlx_waitbufs--; 18514b006d7bSMike Smith splx(s); 18521ac4b82bSMike Smith 18531ac4b82bSMike Smith /* connect the buf to the command */ 18541ac4b82bSMike Smith mc->mc_complete = mlx_completeio; 18551ac4b82bSMike Smith mc->mc_private = bp; 185615fd5d22SMike Smith mc->mc_data = MLX_BIO_DATA(bp); 185715fd5d22SMike Smith mc->mc_length = MLX_BIO_LENGTH(bp); 18581ac4b82bSMike Smith 18591ac4b82bSMike Smith /* map the command so the controller can work with it */ 18601b4404f9SScott Long error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data, 18611b4404f9SScott Long mc->mc_length, mlx_startio_cb, mc, 0); 18621b4404f9SScott Long if (error == EINPROGRESS) { 18631b4404f9SScott Long break; 1864da8bb3a3SMike Smith } 18651ac4b82bSMike Smith 18664b006d7bSMike Smith s = splbio(); 18671ac4b82bSMike Smith } 18684b006d7bSMike Smith splx(s); 18695792b7feSMike Smith mlx_lock_clr(sc, MLX_LOCK_STARTING); 18701ac4b82bSMike Smith } 18711ac4b82bSMike Smith 18721ac4b82bSMike Smith /******************************************************************************** 18731ac4b82bSMike Smith * Handle completion of an I/O command. 18741ac4b82bSMike Smith */ 18751ac4b82bSMike Smith static void 18761ac4b82bSMike Smith mlx_completeio(struct mlx_command *mc) 18771ac4b82bSMike Smith { 18781ac4b82bSMike Smith struct mlx_softc *sc = mc->mc_sc; 187915fd5d22SMike Smith mlx_bio *bp = (mlx_bio *)mc->mc_private; 188015fd5d22SMike Smith struct mlxd_softc *mlxd = (struct mlxd_softc *)MLX_BIO_SOFTC(bp); 18811ac4b82bSMike Smith 18821ac4b82bSMike Smith if (mc->mc_status != MLX_STATUS_OK) { /* could be more verbose here? */ 188315fd5d22SMike Smith MLX_BIO_SET_ERROR(bp, EIO); 18841ac4b82bSMike Smith 18851ac4b82bSMike Smith switch(mc->mc_status) { 18861ac4b82bSMike Smith case MLX_STATUS_RDWROFFLINE: /* system drive has gone offline */ 18871ac4b82bSMike Smith device_printf(mlxd->mlxd_dev, "drive offline\n"); 1888f6b84b08SMike Smith /* should signal this with a return code */ 18891ac4b82bSMike Smith mlxd->mlxd_drive->ms_state = MLX_SYSD_OFFLINE; 18901ac4b82bSMike Smith break; 18911ac4b82bSMike Smith 18921ac4b82bSMike Smith default: /* other I/O error */ 18931ac4b82bSMike Smith device_printf(sc->mlx_dev, "I/O error - %s\n", mlx_diagnose_command(mc)); 18941ac4b82bSMike Smith #if 0 1895cd4ace0cSMike Smith device_printf(sc->mlx_dev, " b_bcount %ld blkcount %ld b_pblkno %d\n", 189615fd5d22SMike Smith MLX_BIO_LENGTH(bp), MLX_BIO_LENGTH(bp) / MLX_BLKSIZE, MLX_BIO_LBA(bp)); 18971ac4b82bSMike Smith device_printf(sc->mlx_dev, " %13D\n", mc->mc_mailbox, " "); 18981ac4b82bSMike Smith #endif 18991ac4b82bSMike Smith break; 19001ac4b82bSMike Smith } 19011ac4b82bSMike Smith } 19021ac4b82bSMike Smith mlx_releasecmd(mc); 19031ac4b82bSMike Smith mlxd_intr(bp); 19041ac4b82bSMike Smith } 19051ac4b82bSMike Smith 19061b4404f9SScott Long void 19071b4404f9SScott Long mlx_user_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error) 19081b4404f9SScott Long { 19091b4404f9SScott Long struct mlx_usercommand *mu; 19101b4404f9SScott Long struct mlx_command *mc; 19111b4404f9SScott Long struct mlx_dcdb *dcdb; 19121b4404f9SScott Long 19131b4404f9SScott Long mc = (struct mlx_command *)arg; 19141b4404f9SScott Long if (error) 19151b4404f9SScott Long return; 19161b4404f9SScott Long 19171b4404f9SScott Long mlx_setup_dmamap(mc, segs, nsegments, error); 19181b4404f9SScott Long 19191b4404f9SScott Long mu = (struct mlx_usercommand *)mc->mc_private; 19201b4404f9SScott Long dcdb = NULL; 19211b4404f9SScott Long 19221b4404f9SScott Long /* 19231b4404f9SScott Long * If this is a passthrough SCSI command, the DCDB is packed at the 19241b4404f9SScott Long * beginning of the data area. Fix up the DCDB to point to the correct 19251b4404f9SScott Long * physical address and override any bufptr supplied by the caller since 19261b4404f9SScott Long * we know what it's meant to be. 19271b4404f9SScott Long */ 19281b4404f9SScott Long if (mc->mc_mailbox[0] == MLX_CMD_DIRECT_CDB) { 19291b4404f9SScott Long dcdb = (struct mlx_dcdb *)mc->mc_data; 19301b4404f9SScott Long dcdb->dcdb_physaddr = mc->mc_dataphys + sizeof(*dcdb); 19311b4404f9SScott Long mu->mu_bufptr = 8; 19321b4404f9SScott Long } 19331b4404f9SScott Long 19341b4404f9SScott Long /* 19351b4404f9SScott Long * If there's a data buffer, fix up the command's buffer pointer. 19361b4404f9SScott Long */ 19371b4404f9SScott Long if (mu->mu_datasize > 0) { 19381b4404f9SScott Long mc->mc_mailbox[mu->mu_bufptr ] = mc->mc_dataphys & 0xff; 19391b4404f9SScott Long mc->mc_mailbox[mu->mu_bufptr + 1] = (mc->mc_dataphys >> 8) & 0xff; 19401b4404f9SScott Long mc->mc_mailbox[mu->mu_bufptr + 2] = (mc->mc_dataphys >> 16) & 0xff; 19411b4404f9SScott Long mc->mc_mailbox[mu->mu_bufptr + 3] = (mc->mc_dataphys >> 24) & 0xff; 19421b4404f9SScott Long } 19431b4404f9SScott Long debug(0, "command fixup"); 19441b4404f9SScott Long 19451b4404f9SScott Long /* submit the command and wait */ 19461b4404f9SScott Long if (mlx_wait_command(mc) != 0) 19471b4404f9SScott Long return; 19481b4404f9SScott Long 19491b4404f9SScott Long } 19501b4404f9SScott Long 19511ac4b82bSMike Smith /******************************************************************************** 19521ac4b82bSMike Smith * Take a command from user-space and try to run it. 1953da8bb3a3SMike Smith * 1954da8bb3a3SMike Smith * XXX Note that this can't perform very much in the way of error checking, and 1955da8bb3a3SMike Smith * as such, applications _must_ be considered trustworthy. 1956da8bb3a3SMike Smith * XXX Commands using S/G for data are not supported. 19571ac4b82bSMike Smith */ 19581ac4b82bSMike Smith static int 19591ac4b82bSMike Smith mlx_user_command(struct mlx_softc *sc, struct mlx_usercommand *mu) 19601ac4b82bSMike Smith { 19611ac4b82bSMike Smith struct mlx_command *mc; 19621ac4b82bSMike Smith void *kbuf; 19631ac4b82bSMike Smith int error; 19641ac4b82bSMike Smith 1965da8bb3a3SMike Smith debug_called(0); 1966da8bb3a3SMike Smith 19671ac4b82bSMike Smith kbuf = NULL; 19681ac4b82bSMike Smith mc = NULL; 19691ac4b82bSMike Smith error = ENOMEM; 1970da8bb3a3SMike Smith 1971da8bb3a3SMike Smith /* get ourselves a command and copy in from user space */ 19721ac4b82bSMike Smith if ((mc = mlx_alloccmd(sc)) == NULL) 1973001ea8fbSSam Leffler return(error); 19741ac4b82bSMike Smith bcopy(mu->mu_command, mc->mc_mailbox, sizeof(mc->mc_mailbox)); 1975da8bb3a3SMike Smith debug(0, "got command buffer"); 1976da8bb3a3SMike Smith 19771b4404f9SScott Long /* 19781b4404f9SScott Long * if we need a buffer for data transfer, allocate one and copy in its 19791b4404f9SScott Long * initial contents 19801b4404f9SScott Long */ 1981da8bb3a3SMike Smith if (mu->mu_datasize > 0) { 198252c9ce25SScott Long if (mu->mu_datasize > MLX_MAXPHYS) { 1983aa083c3dSSam Leffler error = EINVAL; 1984aa083c3dSSam Leffler goto out; 1985aa083c3dSSam Leffler } 1986a163d034SWarner Losh if (((kbuf = malloc(mu->mu_datasize, M_DEVBUF, M_WAITOK)) == NULL) || 1987da8bb3a3SMike Smith (error = copyin(mu->mu_buf, kbuf, mu->mu_datasize))) 19881ac4b82bSMike Smith goto out; 1989da8bb3a3SMike Smith debug(0, "got kernel buffer"); 1990da8bb3a3SMike Smith } 19911ac4b82bSMike Smith 19921ac4b82bSMike Smith /* get a command slot */ 19931ac4b82bSMike Smith if (mlx_getslot(mc)) 19941ac4b82bSMike Smith goto out; 1995da8bb3a3SMike Smith debug(0, "got a slot"); 19961ac4b82bSMike Smith 1997da8bb3a3SMike Smith if (mu->mu_datasize > 0) { 1998da8bb3a3SMike Smith 1999da8bb3a3SMike Smith /* range check the pointer to physical buffer address */ 20001b4404f9SScott Long if ((mu->mu_bufptr < 0) || (mu->mu_bufptr > (sizeof(mu->mu_command) - 20011b4404f9SScott Long sizeof(u_int32_t)))) { 2002da8bb3a3SMike Smith error = EINVAL; 2003da8bb3a3SMike Smith goto out; 2004da8bb3a3SMike Smith } 2005da8bb3a3SMike Smith } 2006da8bb3a3SMike Smith 20071b4404f9SScott Long /* map the command so the controller can see it */ 20081b4404f9SScott Long mc->mc_data = kbuf; 20091b4404f9SScott Long mc->mc_length = mu->mu_datasize; 20101b4404f9SScott Long mc->mc_private = mu; 20111b4404f9SScott Long error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data, 20121b4404f9SScott Long mc->mc_length, mlx_user_cb, mc, BUS_DMA_NOWAIT); 20131ac4b82bSMike Smith 20141ac4b82bSMike Smith /* copy out status and data */ 20151ac4b82bSMike Smith mu->mu_status = mc->mc_status; 20161b4404f9SScott Long if ((mu->mu_datasize > 0) && 20171b4404f9SScott Long ((error = copyout(kbuf, mu->mu_buf, mu->mu_datasize)))) 20181ac4b82bSMike Smith goto out; 20191b4404f9SScott Long 20201ac4b82bSMike Smith error = 0; 20211ac4b82bSMike Smith 20221ac4b82bSMike Smith out: 20231ac4b82bSMike Smith mlx_releasecmd(mc); 20241ac4b82bSMike Smith if (kbuf != NULL) 20251ac4b82bSMike Smith free(kbuf, M_DEVBUF); 20261ac4b82bSMike Smith return(error); 20271ac4b82bSMike Smith } 20281ac4b82bSMike Smith 20291ac4b82bSMike Smith /******************************************************************************** 20301ac4b82bSMike Smith ******************************************************************************** 20311ac4b82bSMike Smith Command I/O to Controller 20321ac4b82bSMike Smith ******************************************************************************** 20331ac4b82bSMike Smith ********************************************************************************/ 20341ac4b82bSMike Smith 20351ac4b82bSMike Smith /******************************************************************************** 20361ac4b82bSMike Smith * Find a free command slot for (mc). 20371ac4b82bSMike Smith * 20381ac4b82bSMike Smith * Don't hand out a slot to a normal-priority command unless there are at least 20391ac4b82bSMike Smith * 4 slots free for priority commands. 20401ac4b82bSMike Smith */ 20411ac4b82bSMike Smith static int 20421ac4b82bSMike Smith mlx_getslot(struct mlx_command *mc) 20431ac4b82bSMike Smith { 20441ac4b82bSMike Smith struct mlx_softc *sc = mc->mc_sc; 2045baff09dbSMike Smith int s, slot, limit; 20461ac4b82bSMike Smith 2047da8bb3a3SMike Smith debug_called(1); 20481ac4b82bSMike Smith 2049baff09dbSMike Smith /* 2050baff09dbSMike Smith * Enforce slot-usage limit, if we have the required information. 2051baff09dbSMike Smith */ 2052baff09dbSMike Smith if (sc->mlx_enq2 != NULL) { 2053baff09dbSMike Smith limit = sc->mlx_enq2->me_max_commands; 2054baff09dbSMike Smith } else { 2055baff09dbSMike Smith limit = 2; 2056baff09dbSMike Smith } 2057baff09dbSMike Smith if (sc->mlx_busycmds >= ((mc->mc_flags & MLX_CMD_PRIORITY) ? limit : limit - 4)) 20581ac4b82bSMike Smith return(EBUSY); 20591ac4b82bSMike Smith 20601ac4b82bSMike Smith /* 20611ac4b82bSMike Smith * Allocate an outstanding command slot 20621ac4b82bSMike Smith * 20631ac4b82bSMike Smith * XXX linear search is slow 20641ac4b82bSMike Smith */ 20651ac4b82bSMike Smith s = splbio(); 2066baff09dbSMike Smith for (slot = 0; slot < limit; slot++) { 2067da8bb3a3SMike Smith debug(2, "try slot %d", slot); 20681ac4b82bSMike Smith if (sc->mlx_busycmd[slot] == NULL) 20691ac4b82bSMike Smith break; 20701ac4b82bSMike Smith } 2071baff09dbSMike Smith if (slot < limit) { 20721ac4b82bSMike Smith sc->mlx_busycmd[slot] = mc; 20731ac4b82bSMike Smith sc->mlx_busycmds++; 20741ac4b82bSMike Smith } 20751ac4b82bSMike Smith splx(s); 20761ac4b82bSMike Smith 20771ac4b82bSMike Smith /* out of slots? */ 2078baff09dbSMike Smith if (slot >= limit) 20791ac4b82bSMike Smith return(EBUSY); 20801ac4b82bSMike Smith 2081da8bb3a3SMike Smith debug(2, "got slot %d", slot); 20821ac4b82bSMike Smith mc->mc_slot = slot; 20831ac4b82bSMike Smith return(0); 20841ac4b82bSMike Smith } 20851ac4b82bSMike Smith 20861ac4b82bSMike Smith /******************************************************************************** 20871ac4b82bSMike Smith * Map/unmap (mc)'s data in the controller's addressable space. 20881ac4b82bSMike Smith */ 20891ac4b82bSMike Smith static void 20901b4404f9SScott Long mlx_setup_dmamap(struct mlx_command *mc, bus_dma_segment_t *segs, int nsegments, 20911b4404f9SScott Long int error) 20921ac4b82bSMike Smith { 20931ac4b82bSMike Smith struct mlx_softc *sc = mc->mc_sc; 20941ac4b82bSMike Smith struct mlx_sgentry *sg; 20951ac4b82bSMike Smith int i; 20961ac4b82bSMike Smith 2097da8bb3a3SMike Smith debug_called(1); 20981ac4b82bSMike Smith 2099baff09dbSMike Smith /* XXX should be unnecessary */ 2100baff09dbSMike Smith if (sc->mlx_enq2 && (nsegments > sc->mlx_enq2->me_max_sg)) 21011b4404f9SScott Long panic("MLX: too many s/g segments (%d, max %d)", nsegments, 21021b4404f9SScott Long sc->mlx_enq2->me_max_sg); 2103baff09dbSMike Smith 21041ac4b82bSMike Smith /* get base address of s/g table */ 2105baff09dbSMike Smith sg = sc->mlx_sgtable + (mc->mc_slot * MLX_NSEG); 21061ac4b82bSMike Smith 21071ac4b82bSMike Smith /* save s/g table information in command */ 21081ac4b82bSMike Smith mc->mc_nsgent = nsegments; 21091b4404f9SScott Long mc->mc_sgphys = sc->mlx_sgbusaddr + 21101b4404f9SScott Long (mc->mc_slot * MLX_NSEG * sizeof(struct mlx_sgentry)); 21111ac4b82bSMike Smith mc->mc_dataphys = segs[0].ds_addr; 21121ac4b82bSMike Smith 21131ac4b82bSMike Smith /* populate s/g table */ 21141ac4b82bSMike Smith for (i = 0; i < nsegments; i++, sg++) { 21151ac4b82bSMike Smith sg->sg_addr = segs[i].ds_addr; 21161ac4b82bSMike Smith sg->sg_count = segs[i].ds_len; 21171ac4b82bSMike Smith } 21181ac4b82bSMike Smith 21191b4404f9SScott Long /* Make sure the buffers are visible on the bus. */ 21201ac4b82bSMike Smith if (mc->mc_flags & MLX_CMD_DATAIN) 21211b4404f9SScott Long bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, 21221b4404f9SScott Long BUS_DMASYNC_PREREAD); 21231ac4b82bSMike Smith if (mc->mc_flags & MLX_CMD_DATAOUT) 21241b4404f9SScott Long bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, 21251b4404f9SScott Long BUS_DMASYNC_PREWRITE); 21261ac4b82bSMike Smith } 21271ac4b82bSMike Smith 21281ac4b82bSMike Smith static void 21291ac4b82bSMike Smith mlx_unmapcmd(struct mlx_command *mc) 21301ac4b82bSMike Smith { 21311ac4b82bSMike Smith struct mlx_softc *sc = mc->mc_sc; 21321ac4b82bSMike Smith 2133da8bb3a3SMike Smith debug_called(1); 21341ac4b82bSMike Smith 21351ac4b82bSMike Smith /* if the command involved data at all */ 21361ac4b82bSMike Smith if (mc->mc_data != NULL) { 21371ac4b82bSMike Smith 21381ac4b82bSMike Smith if (mc->mc_flags & MLX_CMD_DATAIN) 21391ac4b82bSMike Smith bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_POSTREAD); 21401ac4b82bSMike Smith if (mc->mc_flags & MLX_CMD_DATAOUT) 21411ac4b82bSMike Smith bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_POSTWRITE); 21421ac4b82bSMike Smith 21431ac4b82bSMike Smith bus_dmamap_unload(sc->mlx_buffer_dmat, mc->mc_dmamap); 21441ac4b82bSMike Smith } 21451ac4b82bSMike Smith } 21461ac4b82bSMike Smith 21471ac4b82bSMike Smith /******************************************************************************** 21485792b7feSMike Smith * Try to deliver (mc) to the controller. 21491ac4b82bSMike Smith * 21501ac4b82bSMike Smith * Can be called at any interrupt level, with or without interrupts enabled. 21511ac4b82bSMike Smith */ 21521ac4b82bSMike Smith static int 21531ac4b82bSMike Smith mlx_start(struct mlx_command *mc) 21541ac4b82bSMike Smith { 21551ac4b82bSMike Smith struct mlx_softc *sc = mc->mc_sc; 21565792b7feSMike Smith int i, s, done; 21571ac4b82bSMike Smith 2158da8bb3a3SMike Smith debug_called(1); 21591ac4b82bSMike Smith 21601ac4b82bSMike Smith /* save the slot number as ident so we can handle this command when complete */ 21611ac4b82bSMike Smith mc->mc_mailbox[0x1] = mc->mc_slot; 21621ac4b82bSMike Smith 21634b006d7bSMike Smith /* mark the command as currently being processed */ 21641ac4b82bSMike Smith mc->mc_status = MLX_STATUS_BUSY; 21651ac4b82bSMike Smith 21665792b7feSMike Smith /* set a default 60-second timeout XXX tunable? XXX not currently used */ 21675792b7feSMike Smith mc->mc_timeout = time_second + 60; 21681ac4b82bSMike Smith 21691ac4b82bSMike Smith /* spin waiting for the mailbox */ 21701ac4b82bSMike Smith for (i = 100000, done = 0; (i > 0) && !done; i--) { 21711ac4b82bSMike Smith s = splbio(); 21724b006d7bSMike Smith if (sc->mlx_tryqueue(sc, mc)) { 21734b006d7bSMike Smith done = 1; 21744b006d7bSMike Smith /* move command to work queue */ 21754b006d7bSMike Smith TAILQ_INSERT_TAIL(&sc->mlx_work, mc, mc_link); 21764b006d7bSMike Smith } 21775792b7feSMike Smith splx(s); /* drop spl to allow completion interrupts */ 21781ac4b82bSMike Smith } 21791ac4b82bSMike Smith 21801ac4b82bSMike Smith /* command is enqueued */ 21811ac4b82bSMike Smith if (done) 21821ac4b82bSMike Smith return(0); 21831ac4b82bSMike Smith 21841ac4b82bSMike Smith /* 21851ac4b82bSMike Smith * We couldn't get the controller to take the command. Revoke the slot 21861ac4b82bSMike Smith * that the command was given and return it with a bad status. 21871ac4b82bSMike Smith */ 21881ac4b82bSMike Smith sc->mlx_busycmd[mc->mc_slot] = NULL; 21891ac4b82bSMike Smith device_printf(sc->mlx_dev, "controller wedged (not taking commands)\n"); 21901ac4b82bSMike Smith mc->mc_status = MLX_STATUS_WEDGED; 21915792b7feSMike Smith mlx_complete(sc); 21921ac4b82bSMike Smith return(EIO); 21931ac4b82bSMike Smith } 21941ac4b82bSMike Smith 21951ac4b82bSMike Smith /******************************************************************************** 21965792b7feSMike Smith * Poll the controller (sc) for completed commands. 21975792b7feSMike Smith * Update command status and free slots for reuse. If any slots were freed, 21985792b7feSMike Smith * new commands may be posted. 21991ac4b82bSMike Smith * 22005792b7feSMike Smith * Returns nonzero if one or more commands were completed. 22011ac4b82bSMike Smith */ 22021ac4b82bSMike Smith static int 22031ac4b82bSMike Smith mlx_done(struct mlx_softc *sc) 22041ac4b82bSMike Smith { 22051ac4b82bSMike Smith struct mlx_command *mc; 22065792b7feSMike Smith int s, result; 22071ac4b82bSMike Smith u_int8_t slot; 22081ac4b82bSMike Smith u_int16_t status; 22091ac4b82bSMike Smith 2210da8bb3a3SMike Smith debug_called(2); 22111ac4b82bSMike Smith 22125792b7feSMike Smith result = 0; 22131ac4b82bSMike Smith 22145792b7feSMike Smith /* loop collecting completed commands */ 22154b006d7bSMike Smith s = splbio(); 22165792b7feSMike Smith for (;;) { 22175792b7feSMike Smith /* poll for a completed command's identifier and status */ 22181ac4b82bSMike Smith if (sc->mlx_findcomplete(sc, &slot, &status)) { 22195792b7feSMike Smith result = 1; 22201ac4b82bSMike Smith mc = sc->mlx_busycmd[slot]; /* find command */ 22211ac4b82bSMike Smith if (mc != NULL) { /* paranoia */ 22221ac4b82bSMike Smith if (mc->mc_status == MLX_STATUS_BUSY) { 22231ac4b82bSMike Smith mc->mc_status = status; /* save status */ 22241ac4b82bSMike Smith 22251ac4b82bSMike Smith /* free slot for reuse */ 22261ac4b82bSMike Smith sc->mlx_busycmd[slot] = NULL; 22271ac4b82bSMike Smith sc->mlx_busycmds--; 22281ac4b82bSMike Smith } else { 22291ac4b82bSMike Smith device_printf(sc->mlx_dev, "duplicate done event for slot %d\n", slot); 22301ac4b82bSMike Smith } 22311ac4b82bSMike Smith } else { 22321ac4b82bSMike Smith device_printf(sc->mlx_dev, "done event for nonbusy slot %d\n", slot); 22331ac4b82bSMike Smith } 22345792b7feSMike Smith } else { 22355792b7feSMike Smith break; 22361ac4b82bSMike Smith } 22375792b7feSMike Smith } 2238baff09dbSMike Smith splx(s); 22391ac4b82bSMike Smith 22405792b7feSMike Smith /* if we've completed any commands, try posting some more */ 22415792b7feSMike Smith if (result) 22425792b7feSMike Smith mlx_startio(sc); 22435792b7feSMike Smith 22445792b7feSMike Smith /* handle completion and timeouts */ 22455792b7feSMike Smith mlx_complete(sc); 22465792b7feSMike Smith 22475792b7feSMike Smith return(result); 22481ac4b82bSMike Smith } 22491ac4b82bSMike Smith 22501ac4b82bSMike Smith /******************************************************************************** 22515792b7feSMike Smith * Perform post-completion processing for commands on (sc). 22521ac4b82bSMike Smith */ 22531ac4b82bSMike Smith static void 22541ac4b82bSMike Smith mlx_complete(struct mlx_softc *sc) 22551ac4b82bSMike Smith { 22561ac4b82bSMike Smith struct mlx_command *mc, *nc; 22571ac4b82bSMike Smith int s, count; 22581ac4b82bSMike Smith 2259da8bb3a3SMike Smith debug_called(2); 22601ac4b82bSMike Smith 22615792b7feSMike Smith /* avoid reentrancy XXX might want to signal and request a restart */ 22625792b7feSMike Smith if (mlx_lock_tas(sc, MLX_LOCK_COMPLETING)) 22635792b7feSMike Smith return; 22645792b7feSMike Smith 22651ac4b82bSMike Smith s = splbio(); 22661ac4b82bSMike Smith count = 0; 22671ac4b82bSMike Smith 22685792b7feSMike Smith /* scan the list of busy/done commands */ 22694b006d7bSMike Smith mc = TAILQ_FIRST(&sc->mlx_work); 22701ac4b82bSMike Smith while (mc != NULL) { 22711ac4b82bSMike Smith nc = TAILQ_NEXT(mc, mc_link); 22721ac4b82bSMike Smith 22735792b7feSMike Smith /* Command has been completed in some fashion */ 22744b006d7bSMike Smith if (mc->mc_status != MLX_STATUS_BUSY) { 22754b006d7bSMike Smith 22765792b7feSMike Smith /* unmap the command's data buffer */ 22775792b7feSMike Smith mlx_unmapcmd(mc); 22781ac4b82bSMike Smith /* 22791ac4b82bSMike Smith * Does the command have a completion handler? 22801ac4b82bSMike Smith */ 22811ac4b82bSMike Smith if (mc->mc_complete != NULL) { 22821ac4b82bSMike Smith /* remove from list and give to handler */ 22834b006d7bSMike Smith TAILQ_REMOVE(&sc->mlx_work, mc, mc_link); 22841ac4b82bSMike Smith mc->mc_complete(mc); 22851ac4b82bSMike Smith 22861ac4b82bSMike Smith /* 22871ac4b82bSMike Smith * Is there a sleeper waiting on this command? 22881ac4b82bSMike Smith */ 22891ac4b82bSMike Smith } else if (mc->mc_private != NULL) { /* sleeping caller wants to know about it */ 22901ac4b82bSMike Smith 22911ac4b82bSMike Smith /* remove from list and wake up sleeper */ 22924b006d7bSMike Smith TAILQ_REMOVE(&sc->mlx_work, mc, mc_link); 22931ac4b82bSMike Smith wakeup_one(mc->mc_private); 22941ac4b82bSMike Smith 22951ac4b82bSMike Smith /* 22961ac4b82bSMike Smith * Leave the command for a caller that's polling for it. 22971ac4b82bSMike Smith */ 22981ac4b82bSMike Smith } else { 22991ac4b82bSMike Smith } 23004b006d7bSMike Smith } 23011ac4b82bSMike Smith mc = nc; 23021ac4b82bSMike Smith } 23031ac4b82bSMike Smith splx(s); 23041ac4b82bSMike Smith 23055792b7feSMike Smith mlx_lock_clr(sc, MLX_LOCK_COMPLETING); 23061ac4b82bSMike Smith } 23071ac4b82bSMike Smith 23081ac4b82bSMike Smith /******************************************************************************** 23091ac4b82bSMike Smith ******************************************************************************** 23101ac4b82bSMike Smith Command Buffer Management 23111ac4b82bSMike Smith ******************************************************************************** 23121ac4b82bSMike Smith ********************************************************************************/ 23131ac4b82bSMike Smith 23141ac4b82bSMike Smith /******************************************************************************** 23151ac4b82bSMike Smith * Get a new command buffer. 23161ac4b82bSMike Smith * 23171ac4b82bSMike Smith * This may return NULL in low-memory cases. 23181ac4b82bSMike Smith * 23191ac4b82bSMike Smith * Note that using malloc() is expensive (the command buffer is << 1 page) but 23201ac4b82bSMike Smith * necessary if we are to be a loadable module before the zone allocator is fixed. 23211ac4b82bSMike Smith * 23221ac4b82bSMike Smith * If possible, we recycle a command buffer that's been used before. 23231ac4b82bSMike Smith * 23241ac4b82bSMike Smith * XXX Note that command buffers are not cleaned out - it is the caller's 23251ac4b82bSMike Smith * responsibility to ensure that all required fields are filled in before 23261ac4b82bSMike Smith * using a buffer. 23271ac4b82bSMike Smith */ 23281ac4b82bSMike Smith static struct mlx_command * 23291ac4b82bSMike Smith mlx_alloccmd(struct mlx_softc *sc) 23301ac4b82bSMike Smith { 23311ac4b82bSMike Smith struct mlx_command *mc; 23321ac4b82bSMike Smith int error; 23331ac4b82bSMike Smith int s; 23341ac4b82bSMike Smith 2335da8bb3a3SMike Smith debug_called(1); 23361ac4b82bSMike Smith 23371ac4b82bSMike Smith s = splbio(); 23381ac4b82bSMike Smith if ((mc = TAILQ_FIRST(&sc->mlx_freecmds)) != NULL) 23391ac4b82bSMike Smith TAILQ_REMOVE(&sc->mlx_freecmds, mc, mc_link); 23401ac4b82bSMike Smith splx(s); 23411ac4b82bSMike Smith 23421ac4b82bSMike Smith /* allocate a new command buffer? */ 23431ac4b82bSMike Smith if (mc == NULL) { 2344ca89ee27SDavid Malone mc = (struct mlx_command *)malloc(sizeof(*mc), M_DEVBUF, M_NOWAIT | M_ZERO); 23451ac4b82bSMike Smith if (mc != NULL) { 23461ac4b82bSMike Smith mc->mc_sc = sc; 23471ac4b82bSMike Smith error = bus_dmamap_create(sc->mlx_buffer_dmat, 0, &mc->mc_dmamap); 23481ac4b82bSMike Smith if (error) { 23491ac4b82bSMike Smith free(mc, M_DEVBUF); 23501ac4b82bSMike Smith return(NULL); 23511ac4b82bSMike Smith } 23521ac4b82bSMike Smith } 23531ac4b82bSMike Smith } 23541ac4b82bSMike Smith return(mc); 23551ac4b82bSMike Smith } 23561ac4b82bSMike Smith 23571ac4b82bSMike Smith /******************************************************************************** 23581ac4b82bSMike Smith * Release a command buffer for recycling. 23591ac4b82bSMike Smith * 23601ac4b82bSMike Smith * XXX It might be a good idea to limit the number of commands we save for reuse 23611ac4b82bSMike Smith * if it's shown that this list bloats out massively. 23621ac4b82bSMike Smith */ 23631ac4b82bSMike Smith static void 23641ac4b82bSMike Smith mlx_releasecmd(struct mlx_command *mc) 23651ac4b82bSMike Smith { 23661ac4b82bSMike Smith int s; 23671ac4b82bSMike Smith 2368da8bb3a3SMike Smith debug_called(1); 23691ac4b82bSMike Smith 23701ac4b82bSMike Smith s = splbio(); 23711ac4b82bSMike Smith TAILQ_INSERT_HEAD(&mc->mc_sc->mlx_freecmds, mc, mc_link); 23721ac4b82bSMike Smith splx(s); 23731ac4b82bSMike Smith } 23741ac4b82bSMike Smith 23751ac4b82bSMike Smith /******************************************************************************** 23761ac4b82bSMike Smith * Permanently discard a command buffer. 23771ac4b82bSMike Smith */ 23781ac4b82bSMike Smith static void 23791ac4b82bSMike Smith mlx_freecmd(struct mlx_command *mc) 23801ac4b82bSMike Smith { 23811ac4b82bSMike Smith struct mlx_softc *sc = mc->mc_sc; 23821ac4b82bSMike Smith 2383da8bb3a3SMike Smith debug_called(1); 23841ac4b82bSMike Smith bus_dmamap_destroy(sc->mlx_buffer_dmat, mc->mc_dmamap); 23851ac4b82bSMike Smith free(mc, M_DEVBUF); 23861ac4b82bSMike Smith } 23871ac4b82bSMike Smith 23881ac4b82bSMike Smith 23891ac4b82bSMike Smith /******************************************************************************** 23901ac4b82bSMike Smith ******************************************************************************** 23911ac4b82bSMike Smith Type 3 interface accessor methods 23921ac4b82bSMike Smith ******************************************************************************** 23931ac4b82bSMike Smith ********************************************************************************/ 23941ac4b82bSMike Smith 23951ac4b82bSMike Smith /******************************************************************************** 23961ac4b82bSMike Smith * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure 23971ac4b82bSMike Smith * (the controller is not ready to take a command). 23981ac4b82bSMike Smith * 23991ac4b82bSMike Smith * Must be called at splbio or in a fashion that prevents reentry. 24001ac4b82bSMike Smith */ 24011ac4b82bSMike Smith static int 24021ac4b82bSMike Smith mlx_v3_tryqueue(struct mlx_softc *sc, struct mlx_command *mc) 24031ac4b82bSMike Smith { 24041ac4b82bSMike Smith int i; 24051ac4b82bSMike Smith 2406da8bb3a3SMike Smith debug_called(2); 24071ac4b82bSMike Smith 24081ac4b82bSMike Smith /* ready for our command? */ 24091ac4b82bSMike Smith if (!(MLX_V3_GET_IDBR(sc) & MLX_V3_IDB_FULL)) { 24101ac4b82bSMike Smith /* copy mailbox data to window */ 24111ac4b82bSMike Smith for (i = 0; i < 13; i++) 24121ac4b82bSMike Smith MLX_V3_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]); 24131ac4b82bSMike Smith 24141ac4b82bSMike Smith /* post command */ 2415f6b84b08SMike Smith MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_FULL); 24161ac4b82bSMike Smith return(1); 24171ac4b82bSMike Smith } 24181ac4b82bSMike Smith return(0); 24191ac4b82bSMike Smith } 24201ac4b82bSMike Smith 24211ac4b82bSMike Smith /******************************************************************************** 24221ac4b82bSMike Smith * See if a command has been completed, if so acknowledge its completion 24231ac4b82bSMike Smith * and recover the slot number and status code. 24241ac4b82bSMike Smith * 24251ac4b82bSMike Smith * Must be called at splbio or in a fashion that prevents reentry. 24261ac4b82bSMike Smith */ 24271ac4b82bSMike Smith static int 24281ac4b82bSMike Smith mlx_v3_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status) 24291ac4b82bSMike Smith { 24301ac4b82bSMike Smith 2431da8bb3a3SMike Smith debug_called(2); 24321ac4b82bSMike Smith 24331ac4b82bSMike Smith /* status available? */ 24341ac4b82bSMike Smith if (MLX_V3_GET_ODBR(sc) & MLX_V3_ODB_SAVAIL) { 24351ac4b82bSMike Smith *slot = MLX_V3_GET_STATUS_IDENT(sc); /* get command identifier */ 24361ac4b82bSMike Smith *status = MLX_V3_GET_STATUS(sc); /* get status */ 24371ac4b82bSMike Smith 24381ac4b82bSMike Smith /* acknowledge completion */ 2439f6b84b08SMike Smith MLX_V3_PUT_ODBR(sc, MLX_V3_ODB_SAVAIL); 2440f6b84b08SMike Smith MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_SACK); 24411ac4b82bSMike Smith return(1); 24421ac4b82bSMike Smith } 24431ac4b82bSMike Smith return(0); 24441ac4b82bSMike Smith } 24451ac4b82bSMike Smith 24461ac4b82bSMike Smith /******************************************************************************** 24471ac4b82bSMike Smith * Enable/disable interrupts as requested. (No acknowledge required) 24481ac4b82bSMike Smith * 24491ac4b82bSMike Smith * Must be called at splbio or in a fashion that prevents reentry. 24501ac4b82bSMike Smith */ 24511ac4b82bSMike Smith static void 24521ac4b82bSMike Smith mlx_v3_intaction(struct mlx_softc *sc, int action) 24531ac4b82bSMike Smith { 2454da8bb3a3SMike Smith debug_called(1); 24551ac4b82bSMike Smith 24561ac4b82bSMike Smith switch(action) { 24571ac4b82bSMike Smith case MLX_INTACTION_DISABLE: 24581ac4b82bSMike Smith MLX_V3_PUT_IER(sc, 0); 24591ac4b82bSMike Smith sc->mlx_state &= ~MLX_STATE_INTEN; 24601ac4b82bSMike Smith break; 24611ac4b82bSMike Smith case MLX_INTACTION_ENABLE: 24621ac4b82bSMike Smith MLX_V3_PUT_IER(sc, 1); 24631ac4b82bSMike Smith sc->mlx_state |= MLX_STATE_INTEN; 24641ac4b82bSMike Smith break; 24651ac4b82bSMike Smith } 24661ac4b82bSMike Smith } 24671ac4b82bSMike Smith 2468da8bb3a3SMike Smith /******************************************************************************** 2469da8bb3a3SMike Smith * Poll for firmware error codes during controller initialisation. 2470da8bb3a3SMike Smith * Returns 0 if initialisation is complete, 1 if still in progress but no 2471da8bb3a3SMike Smith * error has been fetched, 2 if an error has been retrieved. 2472da8bb3a3SMike Smith */ 2473da8bb3a3SMike Smith static int 2474da8bb3a3SMike Smith mlx_v3_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2) 2475da8bb3a3SMike Smith { 2476da8bb3a3SMike Smith u_int8_t fwerror; 2477da8bb3a3SMike Smith static int initted = 0; 2478da8bb3a3SMike Smith 2479da8bb3a3SMike Smith debug_called(2); 2480da8bb3a3SMike Smith 2481da8bb3a3SMike Smith /* first time around, clear any hardware completion status */ 2482da8bb3a3SMike Smith if (!initted) { 2483da8bb3a3SMike Smith MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_SACK); 2484da8bb3a3SMike Smith DELAY(1000); 2485da8bb3a3SMike Smith initted = 1; 2486da8bb3a3SMike Smith } 2487da8bb3a3SMike Smith 2488da8bb3a3SMike Smith /* init in progress? */ 2489da8bb3a3SMike Smith if (!(MLX_V3_GET_IDBR(sc) & MLX_V3_IDB_INIT_BUSY)) 2490da8bb3a3SMike Smith return(0); 2491da8bb3a3SMike Smith 2492da8bb3a3SMike Smith /* test error value */ 2493da8bb3a3SMike Smith fwerror = MLX_V3_GET_FWERROR(sc); 2494da8bb3a3SMike Smith if (!(fwerror & MLX_V3_FWERROR_PEND)) 2495da8bb3a3SMike Smith return(1); 2496da8bb3a3SMike Smith 2497da8bb3a3SMike Smith /* mask status pending bit, fetch status */ 2498da8bb3a3SMike Smith *error = fwerror & ~MLX_V3_FWERROR_PEND; 2499da8bb3a3SMike Smith *param1 = MLX_V3_GET_FWERROR_PARAM1(sc); 2500da8bb3a3SMike Smith *param2 = MLX_V3_GET_FWERROR_PARAM2(sc); 2501da8bb3a3SMike Smith 2502da8bb3a3SMike Smith /* acknowledge */ 2503da8bb3a3SMike Smith MLX_V3_PUT_FWERROR(sc, 0); 2504da8bb3a3SMike Smith 2505da8bb3a3SMike Smith return(2); 2506da8bb3a3SMike Smith } 25071ac4b82bSMike Smith 25081ac4b82bSMike Smith /******************************************************************************** 25091ac4b82bSMike Smith ******************************************************************************** 2510f6b84b08SMike Smith Type 4 interface accessor methods 2511f6b84b08SMike Smith ******************************************************************************** 2512f6b84b08SMike Smith ********************************************************************************/ 2513f6b84b08SMike Smith 2514f6b84b08SMike Smith /******************************************************************************** 2515f6b84b08SMike Smith * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure 2516f6b84b08SMike Smith * (the controller is not ready to take a command). 2517f6b84b08SMike Smith * 2518f6b84b08SMike Smith * Must be called at splbio or in a fashion that prevents reentry. 2519f6b84b08SMike Smith */ 2520f6b84b08SMike Smith static int 2521f6b84b08SMike Smith mlx_v4_tryqueue(struct mlx_softc *sc, struct mlx_command *mc) 2522f6b84b08SMike Smith { 2523f6b84b08SMike Smith int i; 2524f6b84b08SMike Smith 2525da8bb3a3SMike Smith debug_called(2); 2526f6b84b08SMike Smith 2527f6b84b08SMike Smith /* ready for our command? */ 2528f6b84b08SMike Smith if (!(MLX_V4_GET_IDBR(sc) & MLX_V4_IDB_FULL)) { 2529f6b84b08SMike Smith /* copy mailbox data to window */ 2530f6b84b08SMike Smith for (i = 0; i < 13; i++) 2531f6b84b08SMike Smith MLX_V4_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]); 2532f6b84b08SMike Smith 2533da8bb3a3SMike Smith /* memory-mapped controller, so issue a write barrier to ensure the mailbox is filled */ 2534da8bb3a3SMike Smith bus_space_barrier(sc->mlx_btag, sc->mlx_bhandle, MLX_V4_MAILBOX, MLX_V4_MAILBOX_LENGTH, 2535da8bb3a3SMike Smith BUS_SPACE_BARRIER_WRITE); 2536da8bb3a3SMike Smith 2537f6b84b08SMike Smith /* post command */ 2538f6b84b08SMike Smith MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_HWMBOX_CMD); 2539f6b84b08SMike Smith return(1); 2540f6b84b08SMike Smith } 2541f6b84b08SMike Smith return(0); 2542f6b84b08SMike Smith } 2543f6b84b08SMike Smith 2544f6b84b08SMike Smith /******************************************************************************** 2545f6b84b08SMike Smith * See if a command has been completed, if so acknowledge its completion 2546f6b84b08SMike Smith * and recover the slot number and status code. 2547f6b84b08SMike Smith * 2548f6b84b08SMike Smith * Must be called at splbio or in a fashion that prevents reentry. 2549f6b84b08SMike Smith */ 2550f6b84b08SMike Smith static int 2551f6b84b08SMike Smith mlx_v4_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status) 2552f6b84b08SMike Smith { 2553f6b84b08SMike Smith 2554da8bb3a3SMike Smith debug_called(2); 2555f6b84b08SMike Smith 2556f6b84b08SMike Smith /* status available? */ 2557f6b84b08SMike Smith if (MLX_V4_GET_ODBR(sc) & MLX_V4_ODB_HWSAVAIL) { 2558f6b84b08SMike Smith *slot = MLX_V4_GET_STATUS_IDENT(sc); /* get command identifier */ 2559f6b84b08SMike Smith *status = MLX_V4_GET_STATUS(sc); /* get status */ 2560f6b84b08SMike Smith 2561f6b84b08SMike Smith /* acknowledge completion */ 2562f6b84b08SMike Smith MLX_V4_PUT_ODBR(sc, MLX_V4_ODB_HWMBOX_ACK); 2563f6b84b08SMike Smith MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_SACK); 2564f6b84b08SMike Smith return(1); 2565f6b84b08SMike Smith } 2566f6b84b08SMike Smith return(0); 2567f6b84b08SMike Smith } 2568f6b84b08SMike Smith 2569f6b84b08SMike Smith /******************************************************************************** 2570f6b84b08SMike Smith * Enable/disable interrupts as requested. 2571f6b84b08SMike Smith * 2572f6b84b08SMike Smith * Must be called at splbio or in a fashion that prevents reentry. 2573f6b84b08SMike Smith */ 2574f6b84b08SMike Smith static void 2575f6b84b08SMike Smith mlx_v4_intaction(struct mlx_softc *sc, int action) 2576f6b84b08SMike Smith { 2577da8bb3a3SMike Smith debug_called(1); 2578f6b84b08SMike Smith 2579f6b84b08SMike Smith switch(action) { 2580f6b84b08SMike Smith case MLX_INTACTION_DISABLE: 2581f6b84b08SMike Smith MLX_V4_PUT_IER(sc, MLX_V4_IER_MASK | MLX_V4_IER_DISINT); 2582f6b84b08SMike Smith sc->mlx_state &= ~MLX_STATE_INTEN; 2583f6b84b08SMike Smith break; 2584f6b84b08SMike Smith case MLX_INTACTION_ENABLE: 2585f6b84b08SMike Smith MLX_V4_PUT_IER(sc, MLX_V4_IER_MASK & ~MLX_V4_IER_DISINT); 2586f6b84b08SMike Smith sc->mlx_state |= MLX_STATE_INTEN; 2587f6b84b08SMike Smith break; 2588f6b84b08SMike Smith } 2589f6b84b08SMike Smith } 2590f6b84b08SMike Smith 2591da8bb3a3SMike Smith /******************************************************************************** 2592da8bb3a3SMike Smith * Poll for firmware error codes during controller initialisation. 2593da8bb3a3SMike Smith * Returns 0 if initialisation is complete, 1 if still in progress but no 2594da8bb3a3SMike Smith * error has been fetched, 2 if an error has been retrieved. 2595da8bb3a3SMike Smith */ 2596da8bb3a3SMike Smith static int 2597da8bb3a3SMike Smith mlx_v4_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2) 2598da8bb3a3SMike Smith { 2599da8bb3a3SMike Smith u_int8_t fwerror; 2600da8bb3a3SMike Smith static int initted = 0; 2601da8bb3a3SMike Smith 2602da8bb3a3SMike Smith debug_called(2); 2603da8bb3a3SMike Smith 2604da8bb3a3SMike Smith /* first time around, clear any hardware completion status */ 2605da8bb3a3SMike Smith if (!initted) { 2606da8bb3a3SMike Smith MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_SACK); 2607da8bb3a3SMike Smith DELAY(1000); 2608da8bb3a3SMike Smith initted = 1; 2609da8bb3a3SMike Smith } 2610da8bb3a3SMike Smith 2611da8bb3a3SMike Smith /* init in progress? */ 2612da8bb3a3SMike Smith if (!(MLX_V4_GET_IDBR(sc) & MLX_V4_IDB_INIT_BUSY)) 2613da8bb3a3SMike Smith return(0); 2614da8bb3a3SMike Smith 2615da8bb3a3SMike Smith /* test error value */ 2616da8bb3a3SMike Smith fwerror = MLX_V4_GET_FWERROR(sc); 2617da8bb3a3SMike Smith if (!(fwerror & MLX_V4_FWERROR_PEND)) 2618da8bb3a3SMike Smith return(1); 2619da8bb3a3SMike Smith 2620da8bb3a3SMike Smith /* mask status pending bit, fetch status */ 2621da8bb3a3SMike Smith *error = fwerror & ~MLX_V4_FWERROR_PEND; 2622da8bb3a3SMike Smith *param1 = MLX_V4_GET_FWERROR_PARAM1(sc); 2623da8bb3a3SMike Smith *param2 = MLX_V4_GET_FWERROR_PARAM2(sc); 2624da8bb3a3SMike Smith 2625da8bb3a3SMike Smith /* acknowledge */ 2626da8bb3a3SMike Smith MLX_V4_PUT_FWERROR(sc, 0); 2627da8bb3a3SMike Smith 2628da8bb3a3SMike Smith return(2); 2629da8bb3a3SMike Smith } 2630f6b84b08SMike Smith 2631f6b84b08SMike Smith /******************************************************************************** 2632f6b84b08SMike Smith ******************************************************************************** 26335792b7feSMike Smith Type 5 interface accessor methods 26345792b7feSMike Smith ******************************************************************************** 26355792b7feSMike Smith ********************************************************************************/ 26365792b7feSMike Smith 26375792b7feSMike Smith /******************************************************************************** 26385792b7feSMike Smith * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure 26395792b7feSMike Smith * (the controller is not ready to take a command). 26405792b7feSMike Smith * 26415792b7feSMike Smith * Must be called at splbio or in a fashion that prevents reentry. 26425792b7feSMike Smith */ 26435792b7feSMike Smith static int 26445792b7feSMike Smith mlx_v5_tryqueue(struct mlx_softc *sc, struct mlx_command *mc) 26455792b7feSMike Smith { 26465792b7feSMike Smith int i; 26475792b7feSMike Smith 2648da8bb3a3SMike Smith debug_called(2); 26495792b7feSMike Smith 26505792b7feSMike Smith /* ready for our command? */ 26515792b7feSMike Smith if (MLX_V5_GET_IDBR(sc) & MLX_V5_IDB_EMPTY) { 26525792b7feSMike Smith /* copy mailbox data to window */ 26535792b7feSMike Smith for (i = 0; i < 13; i++) 26545792b7feSMike Smith MLX_V5_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]); 26555792b7feSMike Smith 26565792b7feSMike Smith /* post command */ 26575792b7feSMike Smith MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_HWMBOX_CMD); 26585792b7feSMike Smith return(1); 26595792b7feSMike Smith } 26605792b7feSMike Smith return(0); 26615792b7feSMike Smith } 26625792b7feSMike Smith 26635792b7feSMike Smith /******************************************************************************** 26645792b7feSMike Smith * See if a command has been completed, if so acknowledge its completion 26655792b7feSMike Smith * and recover the slot number and status code. 26665792b7feSMike Smith * 26675792b7feSMike Smith * Must be called at splbio or in a fashion that prevents reentry. 26685792b7feSMike Smith */ 26695792b7feSMike Smith static int 26705792b7feSMike Smith mlx_v5_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status) 26715792b7feSMike Smith { 26725792b7feSMike Smith 2673da8bb3a3SMike Smith debug_called(2); 26745792b7feSMike Smith 26755792b7feSMike Smith /* status available? */ 26765792b7feSMike Smith if (MLX_V5_GET_ODBR(sc) & MLX_V5_ODB_HWSAVAIL) { 26775792b7feSMike Smith *slot = MLX_V5_GET_STATUS_IDENT(sc); /* get command identifier */ 26785792b7feSMike Smith *status = MLX_V5_GET_STATUS(sc); /* get status */ 26795792b7feSMike Smith 26805792b7feSMike Smith /* acknowledge completion */ 26815792b7feSMike Smith MLX_V5_PUT_ODBR(sc, MLX_V5_ODB_HWMBOX_ACK); 26825792b7feSMike Smith MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_SACK); 26835792b7feSMike Smith return(1); 26845792b7feSMike Smith } 26855792b7feSMike Smith return(0); 26865792b7feSMike Smith } 26875792b7feSMike Smith 26885792b7feSMike Smith /******************************************************************************** 26895792b7feSMike Smith * Enable/disable interrupts as requested. 26905792b7feSMike Smith * 26915792b7feSMike Smith * Must be called at splbio or in a fashion that prevents reentry. 26925792b7feSMike Smith */ 26935792b7feSMike Smith static void 26945792b7feSMike Smith mlx_v5_intaction(struct mlx_softc *sc, int action) 26955792b7feSMike Smith { 2696da8bb3a3SMike Smith debug_called(1); 26975792b7feSMike Smith 26985792b7feSMike Smith switch(action) { 26995792b7feSMike Smith case MLX_INTACTION_DISABLE: 2700da8bb3a3SMike Smith MLX_V5_PUT_IER(sc, 0xff & MLX_V5_IER_DISINT); 27015792b7feSMike Smith sc->mlx_state &= ~MLX_STATE_INTEN; 27025792b7feSMike Smith break; 27035792b7feSMike Smith case MLX_INTACTION_ENABLE: 2704da8bb3a3SMike Smith MLX_V5_PUT_IER(sc, 0xff & ~MLX_V5_IER_DISINT); 27055792b7feSMike Smith sc->mlx_state |= MLX_STATE_INTEN; 27065792b7feSMike Smith break; 27075792b7feSMike Smith } 27085792b7feSMike Smith } 27095792b7feSMike Smith 2710da8bb3a3SMike Smith /******************************************************************************** 2711da8bb3a3SMike Smith * Poll for firmware error codes during controller initialisation. 2712da8bb3a3SMike Smith * Returns 0 if initialisation is complete, 1 if still in progress but no 2713da8bb3a3SMike Smith * error has been fetched, 2 if an error has been retrieved. 2714da8bb3a3SMike Smith */ 2715da8bb3a3SMike Smith static int 2716da8bb3a3SMike Smith mlx_v5_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2) 2717da8bb3a3SMike Smith { 2718da8bb3a3SMike Smith u_int8_t fwerror; 2719da8bb3a3SMike Smith static int initted = 0; 2720da8bb3a3SMike Smith 2721da8bb3a3SMike Smith debug_called(2); 2722da8bb3a3SMike Smith 2723da8bb3a3SMike Smith /* first time around, clear any hardware completion status */ 2724da8bb3a3SMike Smith if (!initted) { 2725da8bb3a3SMike Smith MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_SACK); 2726da8bb3a3SMike Smith DELAY(1000); 2727da8bb3a3SMike Smith initted = 1; 2728da8bb3a3SMike Smith } 2729da8bb3a3SMike Smith 2730da8bb3a3SMike Smith /* init in progress? */ 2731da8bb3a3SMike Smith if (MLX_V5_GET_IDBR(sc) & MLX_V5_IDB_INIT_DONE) 2732da8bb3a3SMike Smith return(0); 2733da8bb3a3SMike Smith 2734da8bb3a3SMike Smith /* test for error value */ 2735da8bb3a3SMike Smith fwerror = MLX_V5_GET_FWERROR(sc); 2736da8bb3a3SMike Smith if (!(fwerror & MLX_V5_FWERROR_PEND)) 2737da8bb3a3SMike Smith return(1); 2738da8bb3a3SMike Smith 2739da8bb3a3SMike Smith /* mask status pending bit, fetch status */ 2740da8bb3a3SMike Smith *error = fwerror & ~MLX_V5_FWERROR_PEND; 2741da8bb3a3SMike Smith *param1 = MLX_V5_GET_FWERROR_PARAM1(sc); 2742da8bb3a3SMike Smith *param2 = MLX_V5_GET_FWERROR_PARAM2(sc); 2743da8bb3a3SMike Smith 2744da8bb3a3SMike Smith /* acknowledge */ 2745da8bb3a3SMike Smith MLX_V5_PUT_FWERROR(sc, 0xff); 2746da8bb3a3SMike Smith 2747da8bb3a3SMike Smith return(2); 2748da8bb3a3SMike Smith } 27495792b7feSMike Smith 27505792b7feSMike Smith /******************************************************************************** 27515792b7feSMike Smith ******************************************************************************** 27521ac4b82bSMike Smith Debugging 27531ac4b82bSMike Smith ******************************************************************************** 27541ac4b82bSMike Smith ********************************************************************************/ 27551ac4b82bSMike Smith 27561ac4b82bSMike Smith /******************************************************************************** 27571ac4b82bSMike Smith * Return a status message describing (mc) 27581ac4b82bSMike Smith */ 27591ac4b82bSMike Smith static char *mlx_status_messages[] = { 27601ac4b82bSMike Smith "normal completion", /* 00 */ 27611ac4b82bSMike Smith "irrecoverable data error", /* 01 */ 27621ac4b82bSMike Smith "drive does not exist, or is offline", /* 02 */ 27631ac4b82bSMike Smith "attempt to write beyond end of drive", /* 03 */ 27641ac4b82bSMike Smith "bad data encountered", /* 04 */ 27651ac4b82bSMike Smith "invalid log entry request", /* 05 */ 27661ac4b82bSMike Smith "attempt to rebuild online drive", /* 06 */ 27671ac4b82bSMike Smith "new disk failed during rebuild", /* 07 */ 27681ac4b82bSMike Smith "invalid channel/target", /* 08 */ 27691ac4b82bSMike Smith "rebuild/check already in progress", /* 09 */ 27701ac4b82bSMike Smith "one or more disks are dead", /* 10 */ 27711ac4b82bSMike Smith "invalid or non-redundant drive", /* 11 */ 27721ac4b82bSMike Smith "channel is busy", /* 12 */ 27731ac4b82bSMike Smith "channel is not stopped", /* 13 */ 2774da8bb3a3SMike Smith "rebuild successfully terminated", /* 14 */ 2775da8bb3a3SMike Smith "unsupported command", /* 15 */ 2776da8bb3a3SMike Smith "check condition received", /* 16 */ 2777da8bb3a3SMike Smith "device is busy", /* 17 */ 2778da8bb3a3SMike Smith "selection or command timeout", /* 18 */ 2779da8bb3a3SMike Smith "command terminated abnormally", /* 19 */ 2780da8bb3a3SMike Smith "" 27811ac4b82bSMike Smith }; 27821ac4b82bSMike Smith 27831ac4b82bSMike Smith static struct 27841ac4b82bSMike Smith { 27851ac4b82bSMike Smith int command; 27861ac4b82bSMike Smith u_int16_t status; 27871ac4b82bSMike Smith int msg; 27881ac4b82bSMike Smith } mlx_messages[] = { 2789da8bb3a3SMike Smith {MLX_CMD_READSG, 0x0001, 1}, 2790da8bb3a3SMike Smith {MLX_CMD_READSG, 0x0002, 1}, 2791da8bb3a3SMike Smith {MLX_CMD_READSG, 0x0105, 3}, 2792da8bb3a3SMike Smith {MLX_CMD_READSG, 0x010c, 4}, 2793da8bb3a3SMike Smith {MLX_CMD_WRITESG, 0x0001, 1}, 2794da8bb3a3SMike Smith {MLX_CMD_WRITESG, 0x0002, 1}, 2795da8bb3a3SMike Smith {MLX_CMD_WRITESG, 0x0105, 3}, 2796da8bb3a3SMike Smith {MLX_CMD_READSG_OLD, 0x0001, 1}, 2797da8bb3a3SMike Smith {MLX_CMD_READSG_OLD, 0x0002, 1}, 2798da8bb3a3SMike Smith {MLX_CMD_READSG_OLD, 0x0105, 3}, 2799da8bb3a3SMike Smith {MLX_CMD_WRITESG_OLD, 0x0001, 1}, 2800da8bb3a3SMike Smith {MLX_CMD_WRITESG_OLD, 0x0002, 1}, 2801da8bb3a3SMike Smith {MLX_CMD_WRITESG_OLD, 0x0105, 3}, 28021ac4b82bSMike Smith {MLX_CMD_LOGOP, 0x0105, 5}, 28031ac4b82bSMike Smith {MLX_CMD_REBUILDASYNC, 0x0002, 6}, 28041ac4b82bSMike Smith {MLX_CMD_REBUILDASYNC, 0x0004, 7}, 28051ac4b82bSMike Smith {MLX_CMD_REBUILDASYNC, 0x0105, 8}, 28061ac4b82bSMike Smith {MLX_CMD_REBUILDASYNC, 0x0106, 9}, 2807da8bb3a3SMike Smith {MLX_CMD_REBUILDASYNC, 0x0107, 14}, 28081ac4b82bSMike Smith {MLX_CMD_CHECKASYNC, 0x0002, 10}, 28091ac4b82bSMike Smith {MLX_CMD_CHECKASYNC, 0x0105, 11}, 28101ac4b82bSMike Smith {MLX_CMD_CHECKASYNC, 0x0106, 9}, 28111ac4b82bSMike Smith {MLX_CMD_STOPCHANNEL, 0x0106, 12}, 28121ac4b82bSMike Smith {MLX_CMD_STOPCHANNEL, 0x0105, 8}, 28131ac4b82bSMike Smith {MLX_CMD_STARTCHANNEL, 0x0005, 13}, 28141ac4b82bSMike Smith {MLX_CMD_STARTCHANNEL, 0x0105, 8}, 2815da8bb3a3SMike Smith {MLX_CMD_DIRECT_CDB, 0x0002, 16}, 2816da8bb3a3SMike Smith {MLX_CMD_DIRECT_CDB, 0x0008, 17}, 2817da8bb3a3SMike Smith {MLX_CMD_DIRECT_CDB, 0x000e, 18}, 2818da8bb3a3SMike Smith {MLX_CMD_DIRECT_CDB, 0x000f, 19}, 2819da8bb3a3SMike Smith {MLX_CMD_DIRECT_CDB, 0x0105, 8}, 2820da8bb3a3SMike Smith 2821da8bb3a3SMike Smith {0, 0x0104, 14}, 28221ac4b82bSMike Smith {-1, 0, 0} 28231ac4b82bSMike Smith }; 28241ac4b82bSMike Smith 28251ac4b82bSMike Smith static char * 28261ac4b82bSMike Smith mlx_diagnose_command(struct mlx_command *mc) 28271ac4b82bSMike Smith { 28281ac4b82bSMike Smith static char unkmsg[80]; 28291ac4b82bSMike Smith int i; 28301ac4b82bSMike Smith 28311ac4b82bSMike Smith /* look up message in table */ 28321ac4b82bSMike Smith for (i = 0; mlx_messages[i].command != -1; i++) 2833da8bb3a3SMike Smith if (((mc->mc_mailbox[0] == mlx_messages[i].command) || (mlx_messages[i].command == 0)) && 2834466454bdSMike Smith (mc->mc_status == mlx_messages[i].status)) 28351ac4b82bSMike Smith return(mlx_status_messages[mlx_messages[i].msg]); 28361ac4b82bSMike Smith 28371ac4b82bSMike Smith sprintf(unkmsg, "unknown response 0x%x for command 0x%x", (int)mc->mc_status, (int)mc->mc_mailbox[0]); 28381ac4b82bSMike Smith return(unkmsg); 28391ac4b82bSMike Smith } 28401ac4b82bSMike Smith 28411ac4b82bSMike Smith /******************************************************************************* 2842da8bb3a3SMike Smith * Print a string describing the controller (sc) 28431ac4b82bSMike Smith */ 28445792b7feSMike Smith static struct 28455792b7feSMike Smith { 28465792b7feSMike Smith int hwid; 28475792b7feSMike Smith char *name; 28485792b7feSMike Smith } mlx_controller_names[] = { 28495792b7feSMike Smith {0x01, "960P/PD"}, 28505792b7feSMike Smith {0x02, "960PL"}, 28515792b7feSMike Smith {0x10, "960PG"}, 28525792b7feSMike Smith {0x11, "960PJ"}, 28539eee27f1SMike Smith {0x12, "960PR"}, 28549eee27f1SMike Smith {0x13, "960PT"}, 28559eee27f1SMike Smith {0x14, "960PTL0"}, 28569eee27f1SMike Smith {0x15, "960PRL"}, 28579eee27f1SMike Smith {0x16, "960PTL1"}, 28589eee27f1SMike Smith {0x20, "1164PVX"}, 28595792b7feSMike Smith {-1, NULL} 28605792b7feSMike Smith }; 28615792b7feSMike Smith 28629eee27f1SMike Smith static void 28639eee27f1SMike Smith mlx_describe_controller(struct mlx_softc *sc) 28641ac4b82bSMike Smith { 28651ac4b82bSMike Smith static char buf[80]; 28665792b7feSMike Smith char *model; 28679eee27f1SMike Smith int i; 28681ac4b82bSMike Smith 28695792b7feSMike Smith for (i = 0, model = NULL; mlx_controller_names[i].name != NULL; i++) { 28709eee27f1SMike Smith if ((sc->mlx_enq2->me_hardware_id & 0xff) == mlx_controller_names[i].hwid) { 28715792b7feSMike Smith model = mlx_controller_names[i].name; 28721ac4b82bSMike Smith break; 28731ac4b82bSMike Smith } 28745792b7feSMike Smith } 28755792b7feSMike Smith if (model == NULL) { 28769eee27f1SMike Smith sprintf(buf, " model 0x%x", sc->mlx_enq2->me_hardware_id & 0xff); 28775792b7feSMike Smith model = buf; 28785792b7feSMike Smith } 2879da8bb3a3SMike Smith device_printf(sc->mlx_dev, "DAC%s, %d channel%s, firmware %d.%02d-%c-%02d, %dMB RAM\n", 28809eee27f1SMike Smith model, 28819eee27f1SMike Smith sc->mlx_enq2->me_actual_channels, 28829eee27f1SMike Smith sc->mlx_enq2->me_actual_channels > 1 ? "s" : "", 28839eee27f1SMike Smith sc->mlx_enq2->me_firmware_id & 0xff, 28849eee27f1SMike Smith (sc->mlx_enq2->me_firmware_id >> 8) & 0xff, 28859eee27f1SMike Smith (sc->mlx_enq2->me_firmware_id >> 24) & 0xff, 2886b9256fe3SMike Smith (sc->mlx_enq2->me_firmware_id >> 16) & 0xff, 28879eee27f1SMike Smith sc->mlx_enq2->me_mem_size / (1024 * 1024)); 28889eee27f1SMike Smith 28899eee27f1SMike Smith if (bootverbose) { 28909eee27f1SMike Smith device_printf(sc->mlx_dev, " Hardware ID 0x%08x\n", sc->mlx_enq2->me_hardware_id); 28919eee27f1SMike Smith device_printf(sc->mlx_dev, " Firmware ID 0x%08x\n", sc->mlx_enq2->me_firmware_id); 28929eee27f1SMike Smith device_printf(sc->mlx_dev, " Configured/Actual channels %d/%d\n", sc->mlx_enq2->me_configured_channels, 28939eee27f1SMike Smith sc->mlx_enq2->me_actual_channels); 28949eee27f1SMike Smith device_printf(sc->mlx_dev, " Max Targets %d\n", sc->mlx_enq2->me_max_targets); 28959eee27f1SMike Smith device_printf(sc->mlx_dev, " Max Tags %d\n", sc->mlx_enq2->me_max_tags); 28969eee27f1SMike Smith device_printf(sc->mlx_dev, " Max System Drives %d\n", sc->mlx_enq2->me_max_sys_drives); 28979eee27f1SMike Smith device_printf(sc->mlx_dev, " Max Arms %d\n", sc->mlx_enq2->me_max_arms); 28989eee27f1SMike Smith device_printf(sc->mlx_dev, " Max Spans %d\n", sc->mlx_enq2->me_max_spans); 28999eee27f1SMike Smith device_printf(sc->mlx_dev, " DRAM/cache/flash/NVRAM size %d/%d/%d/%d\n", sc->mlx_enq2->me_mem_size, 29009eee27f1SMike Smith sc->mlx_enq2->me_cache_size, sc->mlx_enq2->me_flash_size, sc->mlx_enq2->me_nvram_size); 29019eee27f1SMike Smith device_printf(sc->mlx_dev, " DRAM type %d\n", sc->mlx_enq2->me_mem_type); 29029eee27f1SMike Smith device_printf(sc->mlx_dev, " Clock Speed %dns\n", sc->mlx_enq2->me_clock_speed); 29039eee27f1SMike Smith device_printf(sc->mlx_dev, " Hardware Speed %dns\n", sc->mlx_enq2->me_hardware_speed); 29049eee27f1SMike Smith device_printf(sc->mlx_dev, " Max Commands %d\n", sc->mlx_enq2->me_max_commands); 29059eee27f1SMike Smith device_printf(sc->mlx_dev, " Max SG Entries %d\n", sc->mlx_enq2->me_max_sg); 29069eee27f1SMike Smith device_printf(sc->mlx_dev, " Max DP %d\n", sc->mlx_enq2->me_max_dp); 29079eee27f1SMike Smith device_printf(sc->mlx_dev, " Max IOD %d\n", sc->mlx_enq2->me_max_iod); 29089eee27f1SMike Smith device_printf(sc->mlx_dev, " Max Comb %d\n", sc->mlx_enq2->me_max_comb); 29099eee27f1SMike Smith device_printf(sc->mlx_dev, " Latency %ds\n", sc->mlx_enq2->me_latency); 29109eee27f1SMike Smith device_printf(sc->mlx_dev, " SCSI Timeout %ds\n", sc->mlx_enq2->me_scsi_timeout); 29119eee27f1SMike Smith device_printf(sc->mlx_dev, " Min Free Lines %d\n", sc->mlx_enq2->me_min_freelines); 29129eee27f1SMike Smith device_printf(sc->mlx_dev, " Rate Constant %d\n", sc->mlx_enq2->me_rate_const); 29139eee27f1SMike Smith device_printf(sc->mlx_dev, " MAXBLK %d\n", sc->mlx_enq2->me_maxblk); 29149eee27f1SMike Smith device_printf(sc->mlx_dev, " Blocking Factor %d sectors\n", sc->mlx_enq2->me_blocking_factor); 29159eee27f1SMike Smith device_printf(sc->mlx_dev, " Cache Line Size %d blocks\n", sc->mlx_enq2->me_cacheline); 29169eee27f1SMike Smith device_printf(sc->mlx_dev, " SCSI Capability %s%dMHz, %d bit\n", 29179eee27f1SMike Smith sc->mlx_enq2->me_scsi_cap & (1<<4) ? "differential " : "", 29189eee27f1SMike Smith (1 << ((sc->mlx_enq2->me_scsi_cap >> 2) & 3)) * 10, 29199eee27f1SMike Smith 8 << (sc->mlx_enq2->me_scsi_cap & 0x3)); 29209eee27f1SMike Smith device_printf(sc->mlx_dev, " Firmware Build Number %d\n", sc->mlx_enq2->me_firmware_build); 29219eee27f1SMike Smith device_printf(sc->mlx_dev, " Fault Management Type %d\n", sc->mlx_enq2->me_fault_mgmt_type); 29229eee27f1SMike Smith device_printf(sc->mlx_dev, " Features %b\n", sc->mlx_enq2->me_firmware_features, 29239eee27f1SMike Smith "\20\4Background Init\3Read Ahead\2MORE\1Cluster\n"); 29249eee27f1SMike Smith 29259eee27f1SMike Smith } 29261ac4b82bSMike Smith } 29271ac4b82bSMike Smith 2928da8bb3a3SMike Smith /******************************************************************************* 2929da8bb3a3SMike Smith * Emit a string describing the firmware handshake status code, and return a flag 2930da8bb3a3SMike Smith * indicating whether the code represents a fatal error. 2931da8bb3a3SMike Smith * 2932da8bb3a3SMike Smith * Error code interpretations are from the Linux driver, and don't directly match 2933da8bb3a3SMike Smith * the messages printed by Mylex's BIOS. This may change if documentation on the 2934da8bb3a3SMike Smith * codes is forthcoming. 2935da8bb3a3SMike Smith */ 2936da8bb3a3SMike Smith static int 2937da8bb3a3SMike Smith mlx_fw_message(struct mlx_softc *sc, int error, int param1, int param2) 2938da8bb3a3SMike Smith { 2939da8bb3a3SMike Smith switch(error) { 2940da8bb3a3SMike Smith case 0x00: 2941da8bb3a3SMike Smith device_printf(sc->mlx_dev, "physical drive %d:%d not responding\n", param2, param1); 2942da8bb3a3SMike Smith break; 2943da8bb3a3SMike Smith case 0x08: 2944da8bb3a3SMike Smith /* we could be neater about this and give some indication when we receive more of them */ 2945da8bb3a3SMike Smith if (!(sc->mlx_flags & MLX_SPINUP_REPORTED)) { 2946da8bb3a3SMike Smith device_printf(sc->mlx_dev, "spinning up drives...\n"); 2947da8bb3a3SMike Smith sc->mlx_flags |= MLX_SPINUP_REPORTED; 2948da8bb3a3SMike Smith } 2949da8bb3a3SMike Smith break; 2950da8bb3a3SMike Smith case 0x30: 2951da8bb3a3SMike Smith device_printf(sc->mlx_dev, "configuration checksum error\n"); 2952da8bb3a3SMike Smith break; 2953da8bb3a3SMike Smith case 0x60: 2954da8bb3a3SMike Smith device_printf(sc->mlx_dev, "mirror race recovery failed\n"); 2955da8bb3a3SMike Smith break; 2956da8bb3a3SMike Smith case 0x70: 2957da8bb3a3SMike Smith device_printf(sc->mlx_dev, "mirror race recovery in progress\n"); 2958da8bb3a3SMike Smith break; 2959da8bb3a3SMike Smith case 0x90: 2960da8bb3a3SMike Smith device_printf(sc->mlx_dev, "physical drive %d:%d COD mismatch\n", param2, param1); 2961da8bb3a3SMike Smith break; 2962da8bb3a3SMike Smith case 0xa0: 2963da8bb3a3SMike Smith device_printf(sc->mlx_dev, "logical drive installation aborted\n"); 2964da8bb3a3SMike Smith break; 2965da8bb3a3SMike Smith case 0xb0: 2966da8bb3a3SMike Smith device_printf(sc->mlx_dev, "mirror race on a critical system drive\n"); 2967da8bb3a3SMike Smith break; 2968da8bb3a3SMike Smith case 0xd0: 2969da8bb3a3SMike Smith device_printf(sc->mlx_dev, "new controller configuration found\n"); 2970da8bb3a3SMike Smith break; 2971da8bb3a3SMike Smith case 0xf0: 2972da8bb3a3SMike Smith device_printf(sc->mlx_dev, "FATAL MEMORY PARITY ERROR\n"); 2973da8bb3a3SMike Smith return(1); 2974da8bb3a3SMike Smith default: 2975da8bb3a3SMike Smith device_printf(sc->mlx_dev, "unknown firmware initialisation error %02x:%02x:%02x\n", error, param1, param2); 2976da8bb3a3SMike Smith break; 2977da8bb3a3SMike Smith } 2978da8bb3a3SMike Smith return(0); 2979da8bb3a3SMike Smith } 2980da8bb3a3SMike Smith 29811ac4b82bSMike Smith /******************************************************************************** 29821ac4b82bSMike Smith ******************************************************************************** 29831ac4b82bSMike Smith Utility Functions 29841ac4b82bSMike Smith ******************************************************************************** 29851ac4b82bSMike Smith ********************************************************************************/ 29861ac4b82bSMike Smith 29871ac4b82bSMike Smith /******************************************************************************** 29881ac4b82bSMike Smith * Find the disk whose unit number is (unit) on this controller 29891ac4b82bSMike Smith */ 29901ac4b82bSMike Smith static struct mlx_sysdrive * 29911ac4b82bSMike Smith mlx_findunit(struct mlx_softc *sc, int unit) 29921ac4b82bSMike Smith { 29931ac4b82bSMike Smith int i; 29941ac4b82bSMike Smith 29951ac4b82bSMike Smith /* search system drives */ 29961ac4b82bSMike Smith for (i = 0; i < MLX_MAXDRIVES; i++) { 29971ac4b82bSMike Smith /* is this one attached? */ 29981ac4b82bSMike Smith if (sc->mlx_sysdrive[i].ms_disk != 0) { 29991ac4b82bSMike Smith /* is this the one? */ 30001ac4b82bSMike Smith if (unit == device_get_unit(sc->mlx_sysdrive[i].ms_disk)) 30011ac4b82bSMike Smith return(&sc->mlx_sysdrive[i]); 30021ac4b82bSMike Smith } 30031ac4b82bSMike Smith } 30041ac4b82bSMike Smith return(NULL); 30051ac4b82bSMike Smith } 3006