1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 1999 Michael Smith 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #include <sys/lock.h> 32 #include <sys/mutex.h> 33 34 /* 35 * Debugging levels: 36 * 0 - quiet, only emit warnings 37 * 1 - noisy, emit major function points and things done 38 * 2 - extremely noisy, emit trace items in loops, etc. 39 */ 40 #ifdef MLX_DEBUG 41 #define debug(level, fmt, args...) do { if (level <= MLX_DEBUG) printf("%s: " fmt "\n", __func__ , ##args); } while(0) 42 #define debug_called(level) do { if (level <= MLX_DEBUG) printf(__func__ ": called\n"); } while(0) 43 #else 44 #define debug(level, fmt, args...) 45 #define debug_called(level) 46 #endif 47 48 /* 49 * Regardless of the actual capacity of the controller, we will allocate space 50 * for 64 s/g entries. Typically controllers support 17 or 33 entries (64k or 51 * 128k maximum transfer assuming 4k page size and non-optimal alignment), but 52 * making that fit cleanly without crossing page boundaries requires rounding up 53 * to the next power of two. 54 */ 55 #define MLX_MAXPHYS (128 * 1024) 56 #define MLX_NSEG 64 57 58 #define MLX_NSLOTS 256 /* max number of command slots */ 59 60 #define MLX_MAXDRIVES 32 /* max number of system drives */ 61 62 /* 63 * Structure describing a System Drive as attached to the controller. 64 */ 65 struct mlx_sysdrive 66 { 67 /* from MLX_CMD_ENQSYSDRIVE */ 68 u_int32_t ms_size; 69 int ms_state; 70 int ms_raidlevel; 71 72 /* synthetic geometry */ 73 int ms_cylinders; 74 int ms_heads; 75 int ms_sectors; 76 77 /* handle for attached driver */ 78 device_t ms_disk; 79 }; 80 81 /* 82 * Per-command control structure. 83 */ 84 struct mlx_command 85 { 86 TAILQ_ENTRY(mlx_command) mc_link; /* list linkage */ 87 88 struct mlx_softc *mc_sc; /* controller that owns us */ 89 u_int8_t mc_slot; /* command slot we occupy */ 90 u_int16_t mc_status; /* command completion status */ 91 time_t mc_timeout; /* when this command expires */ 92 u_int8_t mc_mailbox[16]; /* command mailbox */ 93 u_int32_t mc_sgphys; /* physical address of s/g array in controller space */ 94 int mc_nsgent; /* number of entries in s/g map */ 95 int mc_flags; 96 #define MLX_CMD_DATAIN (1<<0) 97 #define MLX_CMD_DATAOUT (1<<1) 98 #define MLX_CMD_PRIORITY (1<<2) /* high-priority command */ 99 100 void *mc_data; /* data buffer */ 101 size_t mc_length; 102 bus_dmamap_t mc_dmamap; /* DMA map for data */ 103 u_int32_t mc_dataphys; /* data buffer base address controller space */ 104 105 void (* mc_complete)(struct mlx_command *mc); /* completion handler */ 106 void *mc_private; /* submitter-private data or wait channel */ 107 int mc_command; 108 }; 109 110 /* 111 * Per-controller structure. 112 */ 113 struct mlx_softc 114 { 115 /* bus connections */ 116 device_t mlx_dev; 117 struct cdev *mlx_dev_t; 118 struct resource *mlx_mem; /* mailbox interface window */ 119 int mlx_mem_rid; 120 int mlx_mem_type; 121 bus_dma_tag_t mlx_parent_dmat;/* parent DMA tag */ 122 bus_dma_tag_t mlx_buffer_dmat;/* data buffer DMA tag */ 123 struct resource *mlx_irq; /* interrupt */ 124 void *mlx_intr; /* interrupt handle */ 125 126 /* scatter/gather lists and their controller-visible mappings */ 127 struct mlx_sgentry *mlx_sgtable; /* s/g lists */ 128 u_int32_t mlx_sgbusaddr; /* s/g table base address in bus space */ 129 bus_dma_tag_t mlx_sg_dmat; /* s/g buffer DMA tag */ 130 bus_dmamap_t mlx_sg_dmamap; /* map for s/g buffers */ 131 132 /* controller limits and features */ 133 struct mlx_enquiry2 *mlx_enq2; 134 int mlx_feature; /* controller features/quirks */ 135 #define MLX_FEAT_PAUSEWORKS (1<<0) /* channel pause works as expected */ 136 137 /* controller queues and arrays */ 138 TAILQ_HEAD(, mlx_command) mlx_freecmds; /* command structures available for reuse */ 139 TAILQ_HEAD(, mlx_command) mlx_work; /* active commands */ 140 struct mlx_command *mlx_busycmd[MLX_NSLOTS]; /* busy commands */ 141 int mlx_busycmds; /* count of busy commands */ 142 struct mlx_sysdrive mlx_sysdrive[MLX_MAXDRIVES]; /* system drives */ 143 struct bio_queue_head mlx_bioq; /* outstanding I/O operations */ 144 int mlx_waitbufs; /* number of bufs awaiting commands */ 145 146 /* controller status */ 147 int mlx_geom; 148 #define MLX_GEOM_128_32 0 /* geoemetry translation modes */ 149 #define MLX_GEOM_256_63 1 150 int mlx_state; 151 #define MLX_STATE_INTEN (1<<0) /* interrupts have been enabled */ 152 #define MLX_STATE_SHUTDOWN (1<<1) /* controller is shut down */ 153 #define MLX_STATE_OPEN (1<<2) /* control device is open */ 154 #define MLX_STATE_SUSPEND (1<<3) /* controller is suspended */ 155 #define MLX_STATE_QFROZEN (1<<4) /* bio queue frozen */ 156 struct mtx mlx_io_lock; 157 struct sx mlx_config_lock; 158 struct callout mlx_timeout; /* periodic status monitor */ 159 time_t mlx_lastpoll; /* last time_second we polled for status */ 160 u_int16_t mlx_lastevent; /* sequence number of the last event we recorded */ 161 int mlx_currevent; /* sequence number last time we looked */ 162 int mlx_background; /* if != 0 rebuild or check is in progress */ 163 #define MLX_BACKGROUND_CHECK 1 /* we started a check */ 164 #define MLX_BACKGROUND_REBUILD 2 /* we started a rebuild */ 165 #define MLX_BACKGROUND_SPONTANEOUS 3 /* it just happened somehow */ 166 struct mlx_rebuild_status mlx_rebuildstat; /* last rebuild status */ 167 struct mlx_pause mlx_pause; /* pending pause operation details */ 168 169 int mlx_flags; 170 #define MLX_SPINUP_REPORTED (1<<0) /* "spinning up drives" message displayed */ 171 #define MLX_EVENTLOG_BUSY (1<<1) /* currently reading event log */ 172 173 /* interface-specific accessor functions */ 174 int mlx_iftype; /* interface protocol */ 175 #define MLX_IFTYPE_2 2 176 #define MLX_IFTYPE_3 3 177 #define MLX_IFTYPE_4 4 178 #define MLX_IFTYPE_5 5 179 int (* mlx_tryqueue)(struct mlx_softc *sc, struct mlx_command *mc); 180 int (* mlx_findcomplete)(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status); 181 void (* mlx_intaction)(struct mlx_softc *sc, int action); 182 int (* mlx_fw_handshake)(struct mlx_softc *sc, int *error, int *param1, int *param2, int first); 183 #define MLX_INTACTION_DISABLE 0 184 #define MLX_INTACTION_ENABLE 1 185 }; 186 187 #define MLX_IO_LOCK(sc) mtx_lock(&(sc)->mlx_io_lock) 188 #define MLX_IO_UNLOCK(sc) mtx_unlock(&(sc)->mlx_io_lock) 189 #define MLX_IO_ASSERT_LOCKED(sc) mtx_assert(&(sc)->mlx_io_lock, MA_OWNED) 190 #define MLX_CONFIG_LOCK(sc) sx_xlock(&(sc)->mlx_config_lock) 191 #define MLX_CONFIG_UNLOCK(sc) sx_xunlock(&(sc)->mlx_config_lock) 192 #define MLX_CONFIG_ASSERT_LOCKED(sc) sx_assert(&(sc)->mlx_config_lock, SA_XLOCKED) 193 194 /* 195 * Interface between bus connections and driver core. 196 */ 197 extern void mlx_free(struct mlx_softc *sc); 198 extern int mlx_attach(struct mlx_softc *sc); 199 extern void mlx_startup(struct mlx_softc *sc); 200 extern void mlx_intr(void *data); 201 extern int mlx_detach(device_t dev); 202 extern int mlx_shutdown(device_t dev); 203 extern int mlx_suspend(device_t dev); 204 extern int mlx_resume(device_t dev); 205 extern d_open_t mlx_open; 206 extern d_close_t mlx_close; 207 extern d_ioctl_t mlx_ioctl; 208 209 /* 210 * Mylex System Disk driver 211 */ 212 struct mlxd_softc 213 { 214 device_t mlxd_dev; 215 struct mlx_softc *mlxd_controller; 216 struct mlx_sysdrive *mlxd_drive; 217 struct disk *mlxd_disk; 218 int mlxd_unit; 219 int mlxd_flags; 220 #define MLXD_OPEN (1<<0) /* drive is open (can't shut down) */ 221 }; 222 223 /* 224 * Interface between driver core and disk driver (should be using a bus?) 225 */ 226 extern int mlx_submit_buf(struct mlx_softc *sc, struct bio *bp); 227 extern int mlx_submit_ioctl(struct mlx_softc *sc, 228 struct mlx_sysdrive *drive, u_long cmd, 229 caddr_t addr, int32_t flag, struct thread *td); 230 extern void mlxd_intr(struct bio *bp); 231 232 233