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 * We could actually use all 33 segments, but using only 32 means that 311ac4b82bSMike Smith * each scatter/gather map is 256 bytes in size, and thus we don't have to worry about 321ac4b82bSMike Smith * maps crossing page boundaries. 331ac4b82bSMike Smith */ 341ac4b82bSMike Smith #define MLX_NSEG 32 /* max scatter/gather segments we use */ 351ac4b82bSMike Smith #define MLX_NSLOTS 256 /* max number of command slots */ 361ac4b82bSMike Smith 371ac4b82bSMike Smith #define MLX_MAXDRIVES 32 381ac4b82bSMike Smith 391ac4b82bSMike Smith /* 401ac4b82bSMike Smith * Structure describing a System Drive as attached to the controller. 411ac4b82bSMike Smith */ 421ac4b82bSMike Smith struct mlx_sysdrive 431ac4b82bSMike Smith { 441ac4b82bSMike Smith /* from MLX_CMD_ENQSYSDRIVE */ 451ac4b82bSMike Smith u_int32_t ms_size; 461ac4b82bSMike Smith int ms_state; 471ac4b82bSMike Smith int ms_raidlevel; 481ac4b82bSMike Smith 491ac4b82bSMike Smith /* synthetic geometry */ 501ac4b82bSMike Smith int ms_cylinders; 511ac4b82bSMike Smith int ms_heads; 521ac4b82bSMike Smith int ms_sectors; 531ac4b82bSMike Smith 541ac4b82bSMike Smith /* handle for attached driver */ 551ac4b82bSMike Smith device_t ms_disk; 561ac4b82bSMike Smith }; 571ac4b82bSMike Smith 581ac4b82bSMike Smith /* 591ac4b82bSMike Smith * Per-command control structure. 601ac4b82bSMike Smith */ 611ac4b82bSMike Smith struct mlx_command 621ac4b82bSMike Smith { 631ac4b82bSMike Smith TAILQ_ENTRY(mlx_command) mc_link; /* list linkage */ 641ac4b82bSMike Smith 651ac4b82bSMike Smith struct mlx_softc *mc_sc; /* controller that owns us */ 661ac4b82bSMike Smith u_int8_t mc_slot; /* command slot we occupy */ 671ac4b82bSMike Smith u_int16_t mc_status; /* command completion status */ 685792b7feSMike Smith time_t mc_timeout; /* when this command expires */ 691ac4b82bSMike Smith u_int8_t mc_mailbox[16]; /* command mailbox */ 701ac4b82bSMike Smith u_int32_t mc_sgphys; /* physical address of s/g array in controller space */ 711ac4b82bSMike Smith int mc_nsgent; /* number of entries in s/g map */ 721ac4b82bSMike Smith int mc_flags; 731ac4b82bSMike Smith #define MLX_CMD_DATAIN (1<<0) 741ac4b82bSMike Smith #define MLX_CMD_DATAOUT (1<<1) 751ac4b82bSMike Smith #define MLX_CMD_PRIORITY (1<<2) /* high-priority command */ 761ac4b82bSMike Smith 771ac4b82bSMike Smith void *mc_data; /* data buffer */ 781ac4b82bSMike Smith size_t mc_length; 791ac4b82bSMike Smith bus_dmamap_t mc_dmamap; /* DMA map for data */ 801ac4b82bSMike Smith u_int32_t mc_dataphys; /* data buffer base address controller space */ 811ac4b82bSMike Smith 821ac4b82bSMike Smith void (* mc_complete)(struct mlx_command *mc); /* completion handler */ 831ac4b82bSMike Smith void *mc_private; /* submitter-private data or wait channel */ 841ac4b82bSMike Smith }; 851ac4b82bSMike Smith 861ac4b82bSMike Smith /* 871ac4b82bSMike Smith * Per-controller structure. 881ac4b82bSMike Smith */ 891ac4b82bSMike Smith struct mlx_softc 901ac4b82bSMike Smith { 911ac4b82bSMike Smith /* bus connections */ 921ac4b82bSMike Smith device_t mlx_dev; 931ac4b82bSMike Smith struct resource *mlx_mem; /* mailbox interface window */ 941ac4b82bSMike Smith bus_space_handle_t mlx_bhandle; /* bus space handle */ 951ac4b82bSMike Smith bus_space_tag_t mlx_btag; /* bus space tag */ 961ac4b82bSMike Smith bus_dma_tag_t mlx_parent_dmat;/* parent DMA tag */ 971ac4b82bSMike Smith bus_dma_tag_t mlx_buffer_dmat;/* data buffer DMA tag */ 981ac4b82bSMike Smith struct resource *mlx_irq; /* interrupt */ 991ac4b82bSMike Smith void *mlx_intr; /* interrupt handle */ 1001ac4b82bSMike Smith 1011ac4b82bSMike Smith /* scatter/gather lists and their controller-visible mappings */ 1021ac4b82bSMike Smith struct mlx_sgentry *mlx_sgtable; /* s/g lists */ 1031ac4b82bSMike Smith u_int32_t mlx_sgbusaddr; /* s/g table base address in bus space */ 1041ac4b82bSMike Smith bus_dma_tag_t mlx_sg_dmat; /* s/g buffer DMA tag */ 1051ac4b82bSMike Smith bus_dmamap_t mlx_sg_dmamap; /* map for s/g buffers */ 1061ac4b82bSMike Smith 1071ac4b82bSMike Smith /* controller limits and features */ 1089eee27f1SMike Smith struct mlx_enquiry2 *mlx_enq2; 1099eee27f1SMike Smith int mlx_maxiop; /* hard maximum number of commands */ 1101ac4b82bSMike Smith int mlx_feature; /* controller features/quirks */ 1111ac4b82bSMike Smith #define MLX_FEAT_PAUSEWORKS (1<<0) /* channel pause works as expected */ 1121ac4b82bSMike Smith 1131ac4b82bSMike Smith /* controller queues and arrays */ 1141ac4b82bSMike Smith TAILQ_HEAD(, mlx_command) mlx_freecmds; /* command structures available for reuse */ 1154b006d7bSMike Smith TAILQ_HEAD(, mlx_command) mlx_work; /* active commands */ 1161ac4b82bSMike Smith struct mlx_command *mlx_busycmd[MLX_NSLOTS]; /* busy commands */ 1171ac4b82bSMike Smith int mlx_busycmds; /* count of busy commands */ 1181ac4b82bSMike Smith struct mlx_sysdrive mlx_sysdrive[MLX_MAXDRIVES]; /* system drives */ 1191ac4b82bSMike Smith struct buf_queue_head mlx_bufq; /* outstanding I/O operations */ 1201ac4b82bSMike Smith int mlx_waitbufs; /* number of bufs awaiting commands */ 1211ac4b82bSMike Smith 1221ac4b82bSMike Smith /* controller status */ 1231ac4b82bSMike Smith int mlx_geom; 1241ac4b82bSMike Smith #define MLX_GEOM_128_32 0 /* geoemetry translation modes */ 1251ac4b82bSMike Smith #define MLX_GEOM_256_63 1 1261ac4b82bSMike Smith int mlx_state; 1271ac4b82bSMike Smith #define MLX_STATE_INTEN (1<<0) /* interrupts have been enabled */ 1281ac4b82bSMike Smith #define MLX_STATE_SHUTDOWN (1<<1) /* controller is shut down */ 1291ac4b82bSMike Smith #define MLX_STATE_OPEN (1<<2) /* control device is open */ 1301ac4b82bSMike Smith #define MLX_STATE_SUSPEND (1<<3) /* controller is suspended */ 1311ac4b82bSMike Smith struct callout_handle mlx_timeout; /* periodic status monitor */ 1321ac4b82bSMike Smith time_t mlx_lastpoll; /* last time_second we polled for status */ 1339eee27f1SMike Smith int mlx_lastevent; /* sequence number of the last event we recorded */ 1349eee27f1SMike Smith int mlx_currevent; /* sequence number last time we looked */ 1351ac4b82bSMike Smith int mlx_rebuild; /* if >= 0, drive is being rebuilt */ 1361ac4b82bSMike Smith u_int32_t mlx_rebuildstat;/* blocks left to rebuild if active */ 1371ac4b82bSMike Smith int mlx_check; /* if >= 0, drive is being checked */ 1381ac4b82bSMike Smith struct mlx_pause mlx_pause; /* pending pause operation details */ 1391ac4b82bSMike Smith 1405792b7feSMike Smith int mlx_locks; /* reentrancy avoidance */ 1415792b7feSMike Smith 1421ac4b82bSMike Smith /* interface-specific accessor functions */ 1431ac4b82bSMike Smith int mlx_iftype; /* interface protocol */ 1441ac4b82bSMike Smith #define MLX_IFTYPE_3 3 1451ac4b82bSMike Smith #define MLX_IFTYPE_4 4 1461ac4b82bSMike Smith #define MLX_IFTYPE_5 5 1471ac4b82bSMike Smith int (* mlx_tryqueue)(struct mlx_softc *sc, struct mlx_command *mc); 1481ac4b82bSMike Smith int (* mlx_findcomplete)(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status); 1491ac4b82bSMike Smith void (* mlx_intaction)(struct mlx_softc *sc, int action); 1501ac4b82bSMike Smith #define MLX_INTACTION_DISABLE 0 1511ac4b82bSMike Smith #define MLX_INTACTION_ENABLE 1 1521ac4b82bSMike Smith }; 1531ac4b82bSMike Smith 1541ac4b82bSMike Smith /* 1555792b7feSMike Smith * Simple (stupid) locks. 1565792b7feSMike Smith * 1575792b7feSMike Smith * Note that these are designed to avoid reentrancy, not concurrency, and will 1585792b7feSMike Smith * need to be replaced with something better. 1595792b7feSMike Smith */ 1605792b7feSMike Smith #define MLX_LOCK_COMPLETING (1<<0) 1615792b7feSMike Smith #define MLX_LOCK_STARTING (1<<1) 1625792b7feSMike Smith 1635792b7feSMike Smith static __inline int 1645792b7feSMike Smith mlx_lock_tas(struct mlx_softc *sc, int lock) 1655792b7feSMike Smith { 1665792b7feSMike Smith if ((sc)->mlx_locks & (lock)) 1675792b7feSMike Smith return(1); 1685792b7feSMike Smith atomic_set_int(&sc->mlx_locks, lock); 1695792b7feSMike Smith return(0); 1705792b7feSMike Smith } 1715792b7feSMike Smith 1725792b7feSMike Smith static __inline void 1735792b7feSMike Smith mlx_lock_clr(struct mlx_softc *sc, int lock) 1745792b7feSMike Smith { 1755792b7feSMike Smith atomic_clear_int(&sc->mlx_locks, lock); 1765792b7feSMike Smith } 1775792b7feSMike Smith 1785792b7feSMike Smith /* 1791ac4b82bSMike Smith * Interface between bus connections and driver core. 1801ac4b82bSMike Smith */ 1811ac4b82bSMike Smith extern void mlx_free(struct mlx_softc *sc); 1821ac4b82bSMike Smith extern int mlx_attach(struct mlx_softc *sc); 1831ac4b82bSMike Smith extern void mlx_startup(struct mlx_softc *sc); 1841ac4b82bSMike Smith extern void mlx_intr(void *data); 1851ac4b82bSMike Smith extern int mlx_detach(device_t dev); 1861ac4b82bSMike Smith extern int mlx_shutdown(device_t dev); 1871ac4b82bSMike Smith extern int mlx_suspend(device_t dev); 1881ac4b82bSMike Smith extern int mlx_resume(device_t dev); 1891ac4b82bSMike Smith extern d_open_t mlx_open; 1901ac4b82bSMike Smith extern d_close_t mlx_close; 1911ac4b82bSMike Smith extern d_ioctl_t mlx_ioctl; 1921ac4b82bSMike Smith 1931ac4b82bSMike Smith extern devclass_t mlx_devclass; 1941ac4b82bSMike Smith 1951ac4b82bSMike Smith /* 1961ac4b82bSMike Smith * Mylex System Disk driver 1971ac4b82bSMike Smith */ 1981ac4b82bSMike Smith struct mlxd_softc 1991ac4b82bSMike Smith { 2001ac4b82bSMike Smith device_t mlxd_dev; 2011ac4b82bSMike Smith struct mlx_softc *mlxd_controller; 2021ac4b82bSMike Smith struct mlx_sysdrive *mlxd_drive; 2031ac4b82bSMike Smith struct disk mlxd_disk; 2041ac4b82bSMike Smith struct devstat mlxd_stats; 2051ac4b82bSMike Smith struct disklabel mlxd_label; 2061ac4b82bSMike Smith int mlxd_unit; 2071ac4b82bSMike Smith int mlxd_flags; 2081ac4b82bSMike Smith #define MLXD_OPEN (1<<0) /* drive is open (can't shut down) */ 2091ac4b82bSMike Smith }; 2101ac4b82bSMike Smith 2111ac4b82bSMike Smith /* 2121ac4b82bSMike Smith * Interface between driver core and disk driver (should be using a bus?) 2131ac4b82bSMike Smith */ 2141ac4b82bSMike Smith extern int mlx_submit_buf(struct mlx_softc *sc, struct buf *bp); 2151ac4b82bSMike Smith extern int mlx_submit_ioctl(struct mlx_softc *sc, struct mlx_sysdrive *drive, u_long cmd, 2161ac4b82bSMike Smith caddr_t addr, int32_t flag, struct proc *p); 2171ac4b82bSMike Smith extern void mlxd_intr(void *data); 2181ac4b82bSMike Smith 2191ac4b82bSMike Smith 220