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 /* 30da8bb3a3SMike Smith * Debugging levels: 31da8bb3a3SMike Smith * 0 - quiet, only emit warnings 32da8bb3a3SMike Smith * 1 - noisy, emit major function points and things done 33da8bb3a3SMike Smith * 2 - extremely noisy, emit trace items in loops, etc. 34da8bb3a3SMike Smith */ 35da8bb3a3SMike Smith #ifdef MLX_DEBUG 36da8bb3a3SMike Smith #define debug(level, fmt, args...) do { if (level <= MLX_DEBUG) printf("%s: " fmt "\n", __FUNCTION__ , ##args); } while(0) 37da8bb3a3SMike Smith #define debug_called(level) do { if (level <= MLX_DEBUG) printf(__FUNCTION__ ": called\n"); } while(0) 38da8bb3a3SMike Smith #else 39da8bb3a3SMike Smith #define debug(level, fmt, args...) 40da8bb3a3SMike Smith #define debug_called(level) 41da8bb3a3SMike Smith #endif 42da8bb3a3SMike Smith 43da8bb3a3SMike Smith 44da8bb3a3SMike Smith /* 45da8bb3a3SMike Smith * We could actually use all 17/33 segments, but using only 16/32 means that 46da8bb3a3SMike Smith * each scatter/gather map is 128/256 bytes in size, and thus we don't have to worry about 471ac4b82bSMike Smith * maps crossing page boundaries. 481ac4b82bSMike Smith */ 49da8bb3a3SMike Smith #define MLX_NSEG_OLD 16 /* max scatter/gather segments we use 3.x and earlier */ 50da8bb3a3SMike Smith #define MLX_NSEG_NEW 32 /* max scatter/gather segments we use 4.x and later */ 511ac4b82bSMike Smith #define MLX_NSLOTS 256 /* max number of command slots */ 521ac4b82bSMike Smith 53da8bb3a3SMike Smith #define MLX_MAXDRIVES 32 /* max number of system drives */ 541ac4b82bSMike Smith 551ac4b82bSMike Smith /* 561ac4b82bSMike Smith * Structure describing a System Drive as attached to the controller. 571ac4b82bSMike Smith */ 581ac4b82bSMike Smith struct mlx_sysdrive 591ac4b82bSMike Smith { 601ac4b82bSMike Smith /* from MLX_CMD_ENQSYSDRIVE */ 611ac4b82bSMike Smith u_int32_t ms_size; 621ac4b82bSMike Smith int ms_state; 631ac4b82bSMike Smith int ms_raidlevel; 641ac4b82bSMike Smith 651ac4b82bSMike Smith /* synthetic geometry */ 661ac4b82bSMike Smith int ms_cylinders; 671ac4b82bSMike Smith int ms_heads; 681ac4b82bSMike Smith int ms_sectors; 691ac4b82bSMike Smith 701ac4b82bSMike Smith /* handle for attached driver */ 711ac4b82bSMike Smith device_t ms_disk; 721ac4b82bSMike Smith }; 731ac4b82bSMike Smith 741ac4b82bSMike Smith /* 751ac4b82bSMike Smith * Per-command control structure. 761ac4b82bSMike Smith */ 771ac4b82bSMike Smith struct mlx_command 781ac4b82bSMike Smith { 791ac4b82bSMike Smith TAILQ_ENTRY(mlx_command) mc_link; /* list linkage */ 801ac4b82bSMike Smith 811ac4b82bSMike Smith struct mlx_softc *mc_sc; /* controller that owns us */ 821ac4b82bSMike Smith u_int8_t mc_slot; /* command slot we occupy */ 831ac4b82bSMike Smith u_int16_t mc_status; /* command completion status */ 845792b7feSMike Smith time_t mc_timeout; /* when this command expires */ 851ac4b82bSMike Smith u_int8_t mc_mailbox[16]; /* command mailbox */ 861ac4b82bSMike Smith u_int32_t mc_sgphys; /* physical address of s/g array in controller space */ 871ac4b82bSMike Smith int mc_nsgent; /* number of entries in s/g map */ 881ac4b82bSMike Smith int mc_flags; 891ac4b82bSMike Smith #define MLX_CMD_DATAIN (1<<0) 901ac4b82bSMike Smith #define MLX_CMD_DATAOUT (1<<1) 911ac4b82bSMike Smith #define MLX_CMD_PRIORITY (1<<2) /* high-priority command */ 921ac4b82bSMike Smith 931ac4b82bSMike Smith void *mc_data; /* data buffer */ 941ac4b82bSMike Smith size_t mc_length; 951ac4b82bSMike Smith bus_dmamap_t mc_dmamap; /* DMA map for data */ 961ac4b82bSMike Smith u_int32_t mc_dataphys; /* data buffer base address controller space */ 971ac4b82bSMike Smith 981ac4b82bSMike Smith void (* mc_complete)(struct mlx_command *mc); /* completion handler */ 991ac4b82bSMike Smith void *mc_private; /* submitter-private data or wait channel */ 1001ac4b82bSMike Smith }; 1011ac4b82bSMike Smith 1021ac4b82bSMike Smith /* 1031ac4b82bSMike Smith * Per-controller structure. 1041ac4b82bSMike Smith */ 1051ac4b82bSMike Smith struct mlx_softc 1061ac4b82bSMike Smith { 1071ac4b82bSMike Smith /* bus connections */ 1081ac4b82bSMike Smith device_t mlx_dev; 109da8bb3a3SMike Smith dev_t mlx_dev_t; 1101ac4b82bSMike Smith struct resource *mlx_mem; /* mailbox interface window */ 1111ac4b82bSMike Smith bus_space_handle_t mlx_bhandle; /* bus space handle */ 1121ac4b82bSMike Smith bus_space_tag_t mlx_btag; /* bus space tag */ 1131ac4b82bSMike Smith bus_dma_tag_t mlx_parent_dmat;/* parent DMA tag */ 1141ac4b82bSMike Smith bus_dma_tag_t mlx_buffer_dmat;/* data buffer DMA tag */ 1151ac4b82bSMike Smith struct resource *mlx_irq; /* interrupt */ 1161ac4b82bSMike Smith void *mlx_intr; /* interrupt handle */ 1171ac4b82bSMike Smith 1181ac4b82bSMike Smith /* scatter/gather lists and their controller-visible mappings */ 1191ac4b82bSMike Smith struct mlx_sgentry *mlx_sgtable; /* s/g lists */ 1201ac4b82bSMike Smith u_int32_t mlx_sgbusaddr; /* s/g table base address in bus space */ 1211ac4b82bSMike Smith bus_dma_tag_t mlx_sg_dmat; /* s/g buffer DMA tag */ 1221ac4b82bSMike Smith bus_dmamap_t mlx_sg_dmamap; /* map for s/g buffers */ 123da8bb3a3SMike Smith int mlx_sg_nseg; /* max number of s/g entries */ 1241ac4b82bSMike Smith 1251ac4b82bSMike Smith /* controller limits and features */ 1269eee27f1SMike Smith struct mlx_enquiry2 *mlx_enq2; 1279eee27f1SMike Smith int mlx_maxiop; /* hard maximum number of commands */ 1281ac4b82bSMike Smith int mlx_feature; /* controller features/quirks */ 1291ac4b82bSMike Smith #define MLX_FEAT_PAUSEWORKS (1<<0) /* channel pause works as expected */ 1301ac4b82bSMike Smith 1311ac4b82bSMike Smith /* controller queues and arrays */ 1321ac4b82bSMike Smith TAILQ_HEAD(, mlx_command) mlx_freecmds; /* command structures available for reuse */ 1334b006d7bSMike Smith TAILQ_HEAD(, mlx_command) mlx_work; /* active commands */ 1341ac4b82bSMike Smith struct mlx_command *mlx_busycmd[MLX_NSLOTS]; /* busy commands */ 1351ac4b82bSMike Smith int mlx_busycmds; /* count of busy commands */ 1361ac4b82bSMike Smith struct mlx_sysdrive mlx_sysdrive[MLX_MAXDRIVES]; /* system drives */ 1371ac4b82bSMike Smith struct buf_queue_head mlx_bufq; /* outstanding I/O operations */ 1381ac4b82bSMike Smith int mlx_waitbufs; /* number of bufs awaiting commands */ 1391ac4b82bSMike Smith 1401ac4b82bSMike Smith /* controller status */ 1411ac4b82bSMike Smith int mlx_geom; 1421ac4b82bSMike Smith #define MLX_GEOM_128_32 0 /* geoemetry translation modes */ 1431ac4b82bSMike Smith #define MLX_GEOM_256_63 1 1441ac4b82bSMike Smith int mlx_state; 1451ac4b82bSMike Smith #define MLX_STATE_INTEN (1<<0) /* interrupts have been enabled */ 1461ac4b82bSMike Smith #define MLX_STATE_SHUTDOWN (1<<1) /* controller is shut down */ 1471ac4b82bSMike Smith #define MLX_STATE_OPEN (1<<2) /* control device is open */ 1481ac4b82bSMike Smith #define MLX_STATE_SUSPEND (1<<3) /* controller is suspended */ 1491ac4b82bSMike Smith struct callout_handle mlx_timeout; /* periodic status monitor */ 1501ac4b82bSMike Smith time_t mlx_lastpoll; /* last time_second we polled for status */ 151421f2f7dSMike Smith u_int16_t mlx_lastevent; /* sequence number of the last event we recorded */ 1529eee27f1SMike Smith int mlx_currevent; /* sequence number last time we looked */ 153421f2f7dSMike Smith int mlx_background; /* if != 0 rebuild or check is in progress */ 154421f2f7dSMike Smith #define MLX_BACKGROUND_CHECK 1 /* we started a check */ 155421f2f7dSMike Smith #define MLX_BACKGROUND_REBUILD 2 /* we started a rebuild */ 156421f2f7dSMike Smith #define MLX_BACKGROUND_SPONTANEOUS 3 /* it just happened somehow */ 157421f2f7dSMike Smith struct mlx_rebuild_status mlx_rebuildstat; /* last rebuild status */ 1581ac4b82bSMike Smith struct mlx_pause mlx_pause; /* pending pause operation details */ 1591ac4b82bSMike Smith 1605792b7feSMike Smith int mlx_locks; /* reentrancy avoidance */ 161da8bb3a3SMike Smith int mlx_flags; 162da8bb3a3SMike Smith #define MLX_SPINUP_REPORTED (1<<0) /* "spinning up drives" message displayed */ 1635792b7feSMike Smith 1641ac4b82bSMike Smith /* interface-specific accessor functions */ 1651ac4b82bSMike Smith int mlx_iftype; /* interface protocol */ 166da8bb3a3SMike Smith #define MLX_IFTYPE_2 2 1671ac4b82bSMike Smith #define MLX_IFTYPE_3 3 1681ac4b82bSMike Smith #define MLX_IFTYPE_4 4 1691ac4b82bSMike Smith #define MLX_IFTYPE_5 5 1701ac4b82bSMike Smith int (* mlx_tryqueue)(struct mlx_softc *sc, struct mlx_command *mc); 1711ac4b82bSMike Smith int (* mlx_findcomplete)(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status); 1721ac4b82bSMike Smith void (* mlx_intaction)(struct mlx_softc *sc, int action); 173da8bb3a3SMike Smith int (* mlx_fw_handshake)(struct mlx_softc *sc, int *error, int *param1, int *param2); 1741ac4b82bSMike Smith #define MLX_INTACTION_DISABLE 0 1751ac4b82bSMike Smith #define MLX_INTACTION_ENABLE 1 1761ac4b82bSMike Smith }; 1771ac4b82bSMike Smith 1781ac4b82bSMike Smith /* 1795792b7feSMike Smith * Simple (stupid) locks. 1805792b7feSMike Smith * 1815792b7feSMike Smith * Note that these are designed to avoid reentrancy, not concurrency, and will 1825792b7feSMike Smith * need to be replaced with something better. 1835792b7feSMike Smith */ 1845792b7feSMike Smith #define MLX_LOCK_COMPLETING (1<<0) 1855792b7feSMike Smith #define MLX_LOCK_STARTING (1<<1) 1865792b7feSMike Smith 1875792b7feSMike Smith static __inline int 1885792b7feSMike Smith mlx_lock_tas(struct mlx_softc *sc, int lock) 1895792b7feSMike Smith { 1905792b7feSMike Smith if ((sc)->mlx_locks & (lock)) 1915792b7feSMike Smith return(1); 1925792b7feSMike Smith atomic_set_int(&sc->mlx_locks, lock); 1935792b7feSMike Smith return(0); 1945792b7feSMike Smith } 1955792b7feSMike Smith 1965792b7feSMike Smith static __inline void 1975792b7feSMike Smith mlx_lock_clr(struct mlx_softc *sc, int lock) 1985792b7feSMike Smith { 1995792b7feSMike Smith atomic_clear_int(&sc->mlx_locks, lock); 2005792b7feSMike Smith } 2015792b7feSMike Smith 2025792b7feSMike Smith /* 2031ac4b82bSMike Smith * Interface between bus connections and driver core. 2041ac4b82bSMike Smith */ 2051ac4b82bSMike Smith extern void mlx_free(struct mlx_softc *sc); 2061ac4b82bSMike Smith extern int mlx_attach(struct mlx_softc *sc); 2071ac4b82bSMike Smith extern void mlx_startup(struct mlx_softc *sc); 2081ac4b82bSMike Smith extern void mlx_intr(void *data); 2091ac4b82bSMike Smith extern int mlx_detach(device_t dev); 2101ac4b82bSMike Smith extern int mlx_shutdown(device_t dev); 2111ac4b82bSMike Smith extern int mlx_suspend(device_t dev); 2121ac4b82bSMike Smith extern int mlx_resume(device_t dev); 2131ac4b82bSMike Smith extern d_open_t mlx_open; 2141ac4b82bSMike Smith extern d_close_t mlx_close; 2151ac4b82bSMike Smith extern d_ioctl_t mlx_ioctl; 2161ac4b82bSMike Smith 2171ac4b82bSMike Smith extern devclass_t mlx_devclass; 218421f2f7dSMike Smith extern devclass_t mlxd_devclass; 2191ac4b82bSMike Smith 2201ac4b82bSMike Smith /* 2211ac4b82bSMike Smith * Mylex System Disk driver 2221ac4b82bSMike Smith */ 2231ac4b82bSMike Smith struct mlxd_softc 2241ac4b82bSMike Smith { 2251ac4b82bSMike Smith device_t mlxd_dev; 226421f2f7dSMike Smith dev_t mlxd_dev_t; 2271ac4b82bSMike Smith struct mlx_softc *mlxd_controller; 2281ac4b82bSMike Smith struct mlx_sysdrive *mlxd_drive; 2291ac4b82bSMike Smith struct disk mlxd_disk; 2301ac4b82bSMike Smith struct devstat mlxd_stats; 2311ac4b82bSMike Smith struct disklabel mlxd_label; 2321ac4b82bSMike Smith int mlxd_unit; 2331ac4b82bSMike Smith int mlxd_flags; 2341ac4b82bSMike Smith #define MLXD_OPEN (1<<0) /* drive is open (can't shut down) */ 2351ac4b82bSMike Smith }; 2361ac4b82bSMike Smith 2371ac4b82bSMike Smith /* 2381ac4b82bSMike Smith * Interface between driver core and disk driver (should be using a bus?) 2391ac4b82bSMike Smith */ 2401ac4b82bSMike Smith extern int mlx_submit_buf(struct mlx_softc *sc, struct buf *bp); 2411ac4b82bSMike Smith extern int mlx_submit_ioctl(struct mlx_softc *sc, struct mlx_sysdrive *drive, u_long cmd, 2421ac4b82bSMike Smith caddr_t addr, int32_t flag, struct proc *p); 2431ac4b82bSMike Smith extern void mlxd_intr(void *data); 2441ac4b82bSMike Smith 2451ac4b82bSMike Smith 246