1 /*- 2 * Copyright (c) 2006 IronPort Systems 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 27 #ifndef _MFIVAR_H 28 #define _MFIVAR_H 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 /* 34 * SCSI structures and definitions are used from here, but no linking 35 * requirements are made to CAM. 36 */ 37 #include <cam/scsi/scsi_all.h> 38 39 struct mfi_hwcomms { 40 uint32_t hw_pi; 41 uint32_t hw_ci; 42 uint32_t hw_reply_q[1]; 43 }; 44 45 struct mfi_softc; 46 struct disk; 47 struct ccb_hdr; 48 49 struct mfi_command { 50 TAILQ_ENTRY(mfi_command) cm_link; 51 time_t cm_timestamp; 52 struct mfi_softc *cm_sc; 53 union mfi_frame *cm_frame; 54 uint32_t cm_frame_busaddr; 55 struct mfi_sense *cm_sense; 56 uint32_t cm_sense_busaddr; 57 bus_dmamap_t cm_dmamap; 58 union mfi_sgl *cm_sg; 59 void *cm_data; 60 int cm_len; 61 int cm_total_frame_size; 62 int cm_extra_frames; 63 int cm_flags; 64 #define MFI_CMD_MAPPED (1<<0) 65 #define MFI_CMD_DATAIN (1<<1) 66 #define MFI_CMD_DATAOUT (1<<2) 67 #define MFI_CMD_COMPLETED (1<<3) 68 #define MFI_CMD_POLLED (1<<4) 69 #define MFI_ON_MFIQ_FREE (1<<5) 70 #define MFI_ON_MFIQ_READY (1<<6) 71 #define MFI_ON_MFIQ_BUSY (1<<7) 72 #define MFI_ON_MFIQ_MASK ((1<<5)|(1<<6)|(1<<7)) 73 int cm_aen_abort; 74 void (* cm_complete)(struct mfi_command *cm); 75 void *cm_private; 76 int cm_index; 77 int cm_error; 78 }; 79 80 struct mfi_disk { 81 TAILQ_ENTRY(mfi_disk) ld_link; 82 device_t ld_dev; 83 int ld_id; 84 int ld_unit; 85 struct mfi_softc *ld_controller; 86 struct mfi_ld_info *ld_info; 87 struct disk *ld_disk; 88 int ld_flags; 89 #define MFI_DISK_FLAGS_OPEN 0x01 90 }; 91 92 struct mfi_aen { 93 TAILQ_ENTRY(mfi_aen) aen_link; 94 struct proc *p; 95 }; 96 97 struct mfi_softc { 98 device_t mfi_dev; 99 int mfi_flags; 100 #define MFI_FLAGS_SG64 (1<<0) 101 #define MFI_FLAGS_QFRZN (1<<1) 102 #define MFI_FLAGS_OPEN (1<<2) 103 #define MFI_FLAGS_STOP (1<<3) 104 105 struct mfi_hwcomms *mfi_comms; 106 TAILQ_HEAD(,mfi_command) mfi_free; 107 TAILQ_HEAD(,mfi_command) mfi_ready; 108 TAILQ_HEAD(,mfi_command) mfi_busy; 109 struct bio_queue_head mfi_bioq; 110 struct mfi_qstat mfi_qstat[MFIQ_COUNT]; 111 112 struct resource *mfi_regs_resource; 113 bus_space_handle_t mfi_bhandle; 114 bus_space_tag_t mfi_btag; 115 int mfi_regs_rid; 116 117 bus_dma_tag_t mfi_parent_dmat; 118 bus_dma_tag_t mfi_buffer_dmat; 119 120 bus_dma_tag_t mfi_comms_dmat; 121 bus_dmamap_t mfi_comms_dmamap; 122 uint32_t mfi_comms_busaddr; 123 124 bus_dma_tag_t mfi_frames_dmat; 125 bus_dmamap_t mfi_frames_dmamap; 126 uint32_t mfi_frames_busaddr; 127 union mfi_frame *mfi_frames; 128 129 TAILQ_HEAD(,mfi_aen) mfi_aen_pids; 130 struct mfi_command *mfi_aen_cm; 131 uint32_t mfi_aen_triggered; 132 uint32_t mfi_poll_waiting; 133 struct selinfo mfi_select; 134 135 bus_dma_tag_t mfi_sense_dmat; 136 bus_dmamap_t mfi_sense_dmamap; 137 uint32_t mfi_sense_busaddr; 138 struct mfi_sense *mfi_sense; 139 140 struct resource *mfi_irq; 141 void *mfi_intr; 142 int mfi_irq_rid; 143 144 struct intr_config_hook mfi_ich; 145 eventhandler_tag eh; 146 147 /* 148 * Allocation for the command array. Used as an indexable array to 149 * recover completed commands. 150 */ 151 struct mfi_command *mfi_commands; 152 /* 153 * How many commands were actually allocated 154 */ 155 int mfi_total_cmds; 156 /* 157 * How many commands the firmware can handle. Also how big the reply 158 * queue is, minus 1. 159 */ 160 int mfi_max_fw_cmds; 161 /* 162 * How many S/G elements we'll ever actually use 163 */ 164 int mfi_max_sge; 165 /* 166 * How many bytes a compound frame is, including all of the extra frames 167 * that are used for S/G elements. 168 */ 169 int mfi_cmd_size; 170 /* 171 * How large an S/G element is. Used to calculate the number of single 172 * frames in a command. 173 */ 174 int mfi_sge_size; 175 /* 176 * Max number of sectors that the firmware allows 177 */ 178 uint32_t mfi_max_io; 179 180 TAILQ_HEAD(,mfi_disk) mfi_ld_tqh; 181 eventhandler_tag mfi_eh; 182 struct cdev *mfi_cdev; 183 184 TAILQ_HEAD(, ccb_hdr) mfi_cam_ccbq; 185 struct mfi_command * (* mfi_cam_start)(void *); 186 struct callout mfi_watchdog_callout; 187 struct mtx mfi_io_lock; 188 }; 189 190 extern int mfi_attach(struct mfi_softc *); 191 extern void mfi_free(struct mfi_softc *); 192 extern int mfi_shutdown(struct mfi_softc *); 193 extern void mfi_startio(struct mfi_softc *); 194 extern void mfi_disk_complete(struct bio *); 195 extern int mfi_dump_blocks(struct mfi_softc *, int id, uint64_t, void *, int); 196 197 #define MFIQ_ADD(sc, qname) \ 198 do { \ 199 struct mfi_qstat *qs; \ 200 \ 201 qs = &(sc)->mfi_qstat[qname]; \ 202 qs->q_length++; \ 203 if (qs->q_length > qs->q_max) \ 204 qs->q_max = qs->q_length; \ 205 } while (0) 206 207 #define MFIQ_REMOVE(sc, qname) (sc)->mfi_qstat[qname].q_length-- 208 209 #define MFIQ_INIT(sc, qname) \ 210 do { \ 211 sc->mfi_qstat[qname].q_length = 0; \ 212 sc->mfi_qstat[qname].q_max = 0; \ 213 } while (0) 214 215 #define MFIQ_COMMAND_QUEUE(name, index) \ 216 static __inline void \ 217 mfi_initq_ ## name (struct mfi_softc *sc) \ 218 { \ 219 TAILQ_INIT(&sc->mfi_ ## name); \ 220 MFIQ_INIT(sc, index); \ 221 } \ 222 static __inline void \ 223 mfi_enqueue_ ## name (struct mfi_command *cm) \ 224 { \ 225 if ((cm->cm_flags & MFI_ON_MFIQ_MASK) != 0) { \ 226 printf("command %p is on another queue, " \ 227 "flags = %#x\n", cm, cm->cm_flags); \ 228 panic("command is on another queue"); \ 229 } \ 230 TAILQ_INSERT_TAIL(&cm->cm_sc->mfi_ ## name, cm, cm_link); \ 231 cm->cm_flags |= MFI_ON_ ## index; \ 232 MFIQ_ADD(cm->cm_sc, index); \ 233 } \ 234 static __inline void \ 235 mfi_requeue_ ## name (struct mfi_command *cm) \ 236 { \ 237 if ((cm->cm_flags & MFI_ON_MFIQ_MASK) != 0) { \ 238 printf("command %p is on another queue, " \ 239 "flags = %#x\n", cm, cm->cm_flags); \ 240 panic("command is on another queue"); \ 241 } \ 242 TAILQ_INSERT_HEAD(&cm->cm_sc->mfi_ ## name, cm, cm_link); \ 243 cm->cm_flags |= MFI_ON_ ## index; \ 244 MFIQ_ADD(cm->cm_sc, index); \ 245 } \ 246 static __inline struct mfi_command * \ 247 mfi_dequeue_ ## name (struct mfi_softc *sc) \ 248 { \ 249 struct mfi_command *cm; \ 250 \ 251 if ((cm = TAILQ_FIRST(&sc->mfi_ ## name)) != NULL) { \ 252 if ((cm->cm_flags & MFI_ON_ ## index) == 0) { \ 253 printf("command %p not in queue, " \ 254 "flags = %#x, bit = %#x\n", cm, \ 255 cm->cm_flags, MFI_ON_ ## index); \ 256 panic("command not in queue"); \ 257 } \ 258 TAILQ_REMOVE(&sc->mfi_ ## name, cm, cm_link); \ 259 cm->cm_flags &= ~MFI_ON_ ## index; \ 260 MFIQ_REMOVE(sc, index); \ 261 } \ 262 return (cm); \ 263 } \ 264 static __inline void \ 265 mfi_remove_ ## name (struct mfi_command *cm) \ 266 { \ 267 if ((cm->cm_flags & MFI_ON_ ## index) == 0) { \ 268 printf("command %p not in queue, flags = %#x, " \ 269 "bit = %#x\n", cm, cm->cm_flags, \ 270 MFI_ON_ ## index); \ 271 panic("command not in queue"); \ 272 } \ 273 TAILQ_REMOVE(&cm->cm_sc->mfi_ ## name, cm, cm_link); \ 274 cm->cm_flags &= ~MFI_ON_ ## index; \ 275 MFIQ_REMOVE(cm->cm_sc, index); \ 276 } \ 277 struct hack 278 279 MFIQ_COMMAND_QUEUE(free, MFIQ_FREE); 280 MFIQ_COMMAND_QUEUE(ready, MFIQ_READY); 281 MFIQ_COMMAND_QUEUE(busy, MFIQ_BUSY); 282 283 static __inline void 284 mfi_initq_bio(struct mfi_softc *sc) 285 { 286 bioq_init(&sc->mfi_bioq); 287 MFIQ_INIT(sc, MFIQ_BIO); 288 } 289 290 static __inline void 291 mfi_enqueue_bio(struct mfi_softc *sc, struct bio *bp) 292 { 293 bioq_insert_tail(&sc->mfi_bioq, bp); 294 MFIQ_ADD(sc, MFIQ_BIO); 295 } 296 297 static __inline struct bio * 298 mfi_dequeue_bio(struct mfi_softc *sc) 299 { 300 struct bio *bp; 301 302 if ((bp = bioq_first(&sc->mfi_bioq)) != NULL) { 303 bioq_remove(&sc->mfi_bioq, bp); 304 MFIQ_REMOVE(sc, MFIQ_BIO); 305 } 306 return (bp); 307 } 308 309 static __inline void 310 mfi_print_sense(struct mfi_softc *sc, void *sense) 311 { 312 int error, key, asc, ascq; 313 314 scsi_extract_sense((struct scsi_sense_data *)sense, 315 &error, &key, &asc, &ascq); 316 device_printf(sc->mfi_dev, "sense error %d, sense_key %d, " 317 "asc %d, ascq %d\n", error, key, asc, ascq); 318 } 319 320 321 #define MFI_WRITE4(sc, reg, val) bus_space_write_4((sc)->mfi_btag, \ 322 sc->mfi_bhandle, (reg), (val)) 323 #define MFI_READ4(sc, reg) bus_space_read_4((sc)->mfi_btag, \ 324 (sc)->mfi_bhandle, (reg)) 325 #define MFI_WRITE2(sc, reg, val) bus_space_write_2((sc)->mfi_btag, \ 326 sc->mfi_bhandle, (reg), (val)) 327 #define MFI_READ2(sc, reg) bus_space_read_2((sc)->mfi_btag, \ 328 (sc)->mfi_bhandle, (reg)) 329 #define MFI_WRITE1(sc, reg, val) bus_space_write_1((sc)->mfi_btag, \ 330 sc->mfi_bhandle, (reg), (val)) 331 #define MFI_READ1(sc, reg) bus_space_read_1((sc)->mfi_btag, \ 332 (sc)->mfi_bhandle, (reg)) 333 334 MALLOC_DECLARE(M_MFIBUF); 335 336 #define MFI_CMD_TIMEOUT 30 337 338 #ifdef MFI_DEBUG 339 extern void mfi_print_cmd(struct mfi_command *cm); 340 extern void mfi_dump_cmds(struct mfi_softc *sc); 341 extern void mfi_validate_sg(struct mfi_softc *, struct mfi_command *, const char *, int ); 342 #define MFI_PRINT_CMD(cm) mfi_print_cmd(cm) 343 #define MFI_DUMP_CMDS(sc) mfi_dump_cmds(sc) 344 #define MFI_VALIDATE_CMD(sc, cm) mfi_validate_sg(sc, cm, __FUNCTION__, __LINE__) 345 #else 346 #define MFI_PRINT_CMD(cm) 347 #define MFI_DUMP_CMDS(sc) 348 #define MFI_VALIDATE_CMD(sc, cm) 349 #endif 350 351 extern void mfi_release_command(struct mfi_command *cm); 352 353 #endif /* _MFIVAR_H */ 354