1 /*- 2 * Copyright (c) 1999 Michael Smith 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 /* 30 * We could actually use all 33 segments, but using only 32 means that 31 * each scatter/gather map is 256 bytes in size, and thus we don't have to worry about 32 * maps crossing page boundaries. 33 */ 34 #define MLX_NSEG 32 /* max scatter/gather segments we use */ 35 #define MLX_NSLOTS 256 /* max number of command slots */ 36 37 #define MLX_MAXDRIVES 32 38 39 /* 40 * Structure describing a System Drive as attached to the controller. 41 */ 42 struct mlx_sysdrive 43 { 44 /* from MLX_CMD_ENQSYSDRIVE */ 45 u_int32_t ms_size; 46 int ms_state; 47 int ms_raidlevel; 48 49 /* synthetic geometry */ 50 int ms_cylinders; 51 int ms_heads; 52 int ms_sectors; 53 54 /* handle for attached driver */ 55 device_t ms_disk; 56 }; 57 58 /* 59 * Per-command control structure. 60 */ 61 struct mlx_command 62 { 63 TAILQ_ENTRY(mlx_command) mc_link; /* list linkage */ 64 65 struct mlx_softc *mc_sc; /* controller that owns us */ 66 u_int8_t mc_slot; /* command slot we occupy */ 67 u_int16_t mc_status; /* command completion status */ 68 time_t mc_timeout; /* when this command expires */ 69 u_int8_t mc_mailbox[16]; /* command mailbox */ 70 u_int32_t mc_sgphys; /* physical address of s/g array in controller space */ 71 int mc_nsgent; /* number of entries in s/g map */ 72 int mc_flags; 73 #define MLX_CMD_DATAIN (1<<0) 74 #define MLX_CMD_DATAOUT (1<<1) 75 #define MLX_CMD_PRIORITY (1<<2) /* high-priority command */ 76 77 void *mc_data; /* data buffer */ 78 size_t mc_length; 79 bus_dmamap_t mc_dmamap; /* DMA map for data */ 80 u_int32_t mc_dataphys; /* data buffer base address controller space */ 81 82 void (* mc_complete)(struct mlx_command *mc); /* completion handler */ 83 void *mc_private; /* submitter-private data or wait channel */ 84 }; 85 86 /* 87 * Per-controller structure. 88 */ 89 struct mlx_softc 90 { 91 /* bus connections */ 92 device_t mlx_dev; 93 struct resource *mlx_mem; /* mailbox interface window */ 94 bus_space_handle_t mlx_bhandle; /* bus space handle */ 95 bus_space_tag_t mlx_btag; /* bus space tag */ 96 bus_dma_tag_t mlx_parent_dmat;/* parent DMA tag */ 97 bus_dma_tag_t mlx_buffer_dmat;/* data buffer DMA tag */ 98 struct resource *mlx_irq; /* interrupt */ 99 void *mlx_intr; /* interrupt handle */ 100 101 /* scatter/gather lists and their controller-visible mappings */ 102 struct mlx_sgentry *mlx_sgtable; /* s/g lists */ 103 u_int32_t mlx_sgbusaddr; /* s/g table base address in bus space */ 104 bus_dma_tag_t mlx_sg_dmat; /* s/g buffer DMA tag */ 105 bus_dmamap_t mlx_sg_dmamap; /* map for s/g buffers */ 106 107 /* controller limits and features */ 108 int mlx_hwid; /* hardware identifier */ 109 int mlx_maxiop; /* maximum number of I/O operations */ 110 int mlx_nchan; /* number of active channels */ 111 int mlx_maxiosize; /* largest I/O for this controller */ 112 int mlx_maxtarg; /* maximum number of targets per channel */ 113 int mlx_maxtags; /* maximum number of tags per device */ 114 int mlx_scsicap; /* SCSI capabilities */ 115 int mlx_feature; /* controller features/quirks */ 116 #define MLX_FEAT_PAUSEWORKS (1<<0) /* channel pause works as expected */ 117 118 /* controller queues and arrays */ 119 TAILQ_HEAD(, mlx_command) mlx_freecmds; /* command structures available for reuse */ 120 TAILQ_HEAD(, mlx_command) mlx_work; /* active commands */ 121 struct mlx_command *mlx_busycmd[MLX_NSLOTS]; /* busy commands */ 122 int mlx_busycmds; /* count of busy commands */ 123 struct mlx_sysdrive mlx_sysdrive[MLX_MAXDRIVES]; /* system drives */ 124 struct buf_queue_head mlx_bufq; /* outstanding I/O operations */ 125 int mlx_waitbufs; /* number of bufs awaiting commands */ 126 127 /* controller status */ 128 u_int8_t mlx_fwminor; /* firmware revision */ 129 u_int8_t mlx_fwmajor; 130 int mlx_geom; 131 #define MLX_GEOM_128_32 0 /* geoemetry translation modes */ 132 #define MLX_GEOM_256_63 1 133 int mlx_state; 134 #define MLX_STATE_INTEN (1<<0) /* interrupts have been enabled */ 135 #define MLX_STATE_SHUTDOWN (1<<1) /* controller is shut down */ 136 #define MLX_STATE_OPEN (1<<2) /* control device is open */ 137 #define MLX_STATE_SUSPEND (1<<3) /* controller is suspended */ 138 struct callout_handle mlx_timeout; /* periodic status monitor */ 139 time_t mlx_lastpoll; /* last time_second we polled for status */ 140 u_int16_t mlx_lastevent; /* sequence number of the last event we recorded */ 141 u_int16_t mlx_currevent; /* sequence number last time we looked */ 142 int mlx_polling; /* if > 0, polling operations still running */ 143 int mlx_rebuild; /* if >= 0, drive is being rebuilt */ 144 u_int32_t mlx_rebuildstat;/* blocks left to rebuild if active */ 145 int mlx_check; /* if >= 0, drive is being checked */ 146 struct mlx_pause mlx_pause; /* pending pause operation details */ 147 148 int mlx_locks; /* reentrancy avoidance */ 149 150 /* interface-specific accessor functions */ 151 int mlx_iftype; /* interface protocol */ 152 #define MLX_IFTYPE_3 3 153 #define MLX_IFTYPE_4 4 154 #define MLX_IFTYPE_5 5 155 int (* mlx_tryqueue)(struct mlx_softc *sc, struct mlx_command *mc); 156 int (* mlx_findcomplete)(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status); 157 void (* mlx_intaction)(struct mlx_softc *sc, int action); 158 #define MLX_INTACTION_DISABLE 0 159 #define MLX_INTACTION_ENABLE 1 160 }; 161 162 /* 163 * Simple (stupid) locks. 164 * 165 * Note that these are designed to avoid reentrancy, not concurrency, and will 166 * need to be replaced with something better. 167 */ 168 #define MLX_LOCK_COMPLETING (1<<0) 169 #define MLX_LOCK_STARTING (1<<1) 170 171 static __inline int 172 mlx_lock_tas(struct mlx_softc *sc, int lock) 173 { 174 if ((sc)->mlx_locks & (lock)) 175 return(1); 176 atomic_set_int(&sc->mlx_locks, lock); 177 return(0); 178 } 179 180 static __inline void 181 mlx_lock_clr(struct mlx_softc *sc, int lock) 182 { 183 atomic_clear_int(&sc->mlx_locks, lock); 184 } 185 186 /* 187 * Interface between bus connections and driver core. 188 */ 189 extern void mlx_free(struct mlx_softc *sc); 190 extern int mlx_attach(struct mlx_softc *sc); 191 extern void mlx_startup(struct mlx_softc *sc); 192 extern void mlx_intr(void *data); 193 extern int mlx_detach(device_t dev); 194 extern int mlx_shutdown(device_t dev); 195 extern int mlx_suspend(device_t dev); 196 extern int mlx_resume(device_t dev); 197 extern d_open_t mlx_open; 198 extern d_close_t mlx_close; 199 extern d_ioctl_t mlx_ioctl; 200 201 extern devclass_t mlx_devclass; 202 203 /* 204 * Mylex System Disk driver 205 */ 206 struct mlxd_softc 207 { 208 device_t mlxd_dev; 209 struct mlx_softc *mlxd_controller; 210 struct mlx_sysdrive *mlxd_drive; 211 struct disk mlxd_disk; 212 struct devstat mlxd_stats; 213 struct disklabel mlxd_label; 214 int mlxd_unit; 215 int mlxd_flags; 216 #define MLXD_OPEN (1<<0) /* drive is open (can't shut down) */ 217 }; 218 219 /* 220 * Interface between driver core and disk driver (should be using a bus?) 221 */ 222 extern int mlx_submit_buf(struct mlx_softc *sc, struct buf *bp); 223 extern int mlx_submit_ioctl(struct mlx_softc *sc, struct mlx_sysdrive *drive, u_long cmd, 224 caddr_t addr, int32_t flag, struct proc *p); 225 extern void mlxd_intr(void *data); 226 227 228