12e21a3efSScott Long /*- 22e21a3efSScott Long * Copyright (c) 2006 IronPort Systems 32e21a3efSScott Long * All rights reserved. 42e21a3efSScott Long * 52e21a3efSScott Long * Redistribution and use in source and binary forms, with or without 62e21a3efSScott Long * modification, are permitted provided that the following conditions 72e21a3efSScott Long * are met: 82e21a3efSScott Long * 1. Redistributions of source code must retain the above copyright 92e21a3efSScott Long * notice, this list of conditions and the following disclaimer. 102e21a3efSScott Long * 2. Redistributions in binary form must reproduce the above copyright 112e21a3efSScott Long * notice, this list of conditions and the following disclaimer in the 122e21a3efSScott Long * documentation and/or other materials provided with the distribution. 132e21a3efSScott Long * 142e21a3efSScott Long * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 152e21a3efSScott Long * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 162e21a3efSScott Long * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 172e21a3efSScott Long * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 182e21a3efSScott Long * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 192e21a3efSScott Long * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 202e21a3efSScott Long * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 212e21a3efSScott Long * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 222e21a3efSScott Long * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 232e21a3efSScott Long * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 242e21a3efSScott Long * SUCH DAMAGE. 252e21a3efSScott Long */ 262e21a3efSScott Long 272e21a3efSScott Long #ifndef _MFIVAR_H 282e21a3efSScott Long #define _MFIVAR_H 292e21a3efSScott Long 302e21a3efSScott Long #include <sys/cdefs.h> 312e21a3efSScott Long __FBSDID("$FreeBSD$"); 322e21a3efSScott Long 332e21a3efSScott Long /* 342e21a3efSScott Long * SCSI structures and definitions are used from here, but no linking 352e21a3efSScott Long * requirements are made to CAM. 362e21a3efSScott Long */ 372e21a3efSScott Long #include <cam/scsi/scsi_all.h> 382e21a3efSScott Long 392e21a3efSScott Long struct mfi_hwcomms { 402e21a3efSScott Long uint32_t hw_pi; 412e21a3efSScott Long uint32_t hw_ci; 422e21a3efSScott Long uint32_t hw_reply_q[1]; 432e21a3efSScott Long }; 442e21a3efSScott Long 452e21a3efSScott Long struct mfi_softc; 462e21a3efSScott Long 472e21a3efSScott Long struct mfi_command { 482e21a3efSScott Long TAILQ_ENTRY(mfi_command) cm_link; 492e21a3efSScott Long struct mfi_softc *cm_sc; 502e21a3efSScott Long union mfi_frame *cm_frame; 512e21a3efSScott Long uint32_t cm_frame_busaddr; 522e21a3efSScott Long struct mfi_sense *cm_sense; 532e21a3efSScott Long uint32_t cm_sense_busaddr; 542e21a3efSScott Long bus_dmamap_t cm_dmamap; 552e21a3efSScott Long union mfi_sgl *cm_sg; 562e21a3efSScott Long void *cm_data; 572e21a3efSScott Long int cm_len; 582e21a3efSScott Long int cm_total_frame_size; 592e21a3efSScott Long int cm_extra_frames; 602e21a3efSScott Long int cm_flags; 612e21a3efSScott Long #define MFI_CMD_MAPPED (1<<0) 622e21a3efSScott Long #define MFI_CMD_DATAIN (1<<1) 632e21a3efSScott Long #define MFI_CMD_DATAOUT (1<<2) 642e21a3efSScott Long #define MFI_CMD_COMPLETED (1<<3) 652e21a3efSScott Long #define MFI_CMD_POLLED (1<<4) 662e21a3efSScott Long #define MFI_ON_MFIQ_FREE (1<<5) 672e21a3efSScott Long #define MFI_ON_MFIQ_READY (1<<6) 682e21a3efSScott Long #define MFI_ON_MFIQ_BUSY (1<<7) 692e21a3efSScott Long #define MFI_ON_MFIQ_MASK ((1<<5)|(1<<6)|(1<<7)) 70741367d5SDoug Ambrisko int cm_aen_abort; 712e21a3efSScott Long void (* cm_complete)(struct mfi_command *cm); 722e21a3efSScott Long void *cm_private; 732e21a3efSScott Long }; 742e21a3efSScott Long 752e21a3efSScott Long struct mfi_ld { 762e21a3efSScott Long TAILQ_ENTRY(mfi_ld) ld_link; 772e21a3efSScott Long device_t ld_disk; 78c0b332d1SPaul Saab struct mfi_ld_info *ld_info; 792e21a3efSScott Long int ld_id; 802e21a3efSScott Long }; 812e21a3efSScott Long 82741367d5SDoug Ambrisko struct mfi_aen { 83741367d5SDoug Ambrisko TAILQ_ENTRY(mfi_aen) aen_link; 84741367d5SDoug Ambrisko struct proc *p; 85741367d5SDoug Ambrisko }; 86741367d5SDoug Ambrisko 872e21a3efSScott Long struct mfi_softc { 882e21a3efSScott Long device_t mfi_dev; 892e21a3efSScott Long int mfi_flags; 902e21a3efSScott Long #define MFI_FLAGS_SG64 (1<<0) 912e21a3efSScott Long #define MFI_FLAGS_QFRZN (1<<1) 922e21a3efSScott Long #define MFI_FLAGS_OPEN (1<<2) 932e21a3efSScott Long 942e21a3efSScott Long struct mfi_hwcomms *mfi_comms; 952e21a3efSScott Long TAILQ_HEAD(,mfi_command) mfi_free; 962e21a3efSScott Long TAILQ_HEAD(,mfi_command) mfi_ready; 972e21a3efSScott Long TAILQ_HEAD(,mfi_command) mfi_busy; 982e21a3efSScott Long struct bio_queue_head mfi_bioq; 992e21a3efSScott Long struct mfi_qstat mfi_qstat[MFIQ_COUNT]; 1002e21a3efSScott Long 1012e21a3efSScott Long struct resource *mfi_regs_resource; 1022e21a3efSScott Long bus_space_handle_t mfi_bhandle; 1032e21a3efSScott Long bus_space_tag_t mfi_btag; 1042e21a3efSScott Long int mfi_regs_rid; 1052e21a3efSScott Long 1062e21a3efSScott Long bus_dma_tag_t mfi_parent_dmat; 1072e21a3efSScott Long bus_dma_tag_t mfi_buffer_dmat; 1082e21a3efSScott Long 1092e21a3efSScott Long bus_dma_tag_t mfi_comms_dmat; 1102e21a3efSScott Long bus_dmamap_t mfi_comms_dmamap; 1112e21a3efSScott Long uint32_t mfi_comms_busaddr; 1122e21a3efSScott Long 1132e21a3efSScott Long bus_dma_tag_t mfi_frames_dmat; 1142e21a3efSScott Long bus_dmamap_t mfi_frames_dmamap; 1152e21a3efSScott Long uint32_t mfi_frames_busaddr; 1162e21a3efSScott Long union mfi_frame *mfi_frames; 1172e21a3efSScott Long 118741367d5SDoug Ambrisko TAILQ_HEAD(,mfi_aen) mfi_aen_pids; 119741367d5SDoug Ambrisko struct mfi_command *mfi_aen_cm; 120741367d5SDoug Ambrisko uint32_t mfi_aen_triggered; 121741367d5SDoug Ambrisko uint32_t mfi_poll_waiting; 122741367d5SDoug Ambrisko struct selinfo mfi_select; 123741367d5SDoug Ambrisko 1242e21a3efSScott Long bus_dma_tag_t mfi_sense_dmat; 1252e21a3efSScott Long bus_dmamap_t mfi_sense_dmamap; 1262e21a3efSScott Long uint32_t mfi_sense_busaddr; 1272e21a3efSScott Long struct mfi_sense *mfi_sense; 1282e21a3efSScott Long 1292e21a3efSScott Long struct resource *mfi_irq; 1302e21a3efSScott Long void *mfi_intr; 1312e21a3efSScott Long int mfi_irq_rid; 1322e21a3efSScott Long 1332e21a3efSScott Long struct intr_config_hook mfi_ich; 1342e21a3efSScott Long eventhandler_tag eh; 1352e21a3efSScott Long 1362e21a3efSScott Long /* 1372e21a3efSScott Long * Allocation for the command array. Used as an indexable array to 1382e21a3efSScott Long * recover completed commands. 1392e21a3efSScott Long */ 1402e21a3efSScott Long struct mfi_command *mfi_commands; 1412e21a3efSScott Long /* 1422e21a3efSScott Long * How many commands were actually allocated 1432e21a3efSScott Long */ 1442e21a3efSScott Long int mfi_total_cmds; 1452e21a3efSScott Long /* 1462e21a3efSScott Long * How many commands the firmware can handle. Also how big the reply 1472e21a3efSScott Long * queue is, minus 1. 1482e21a3efSScott Long */ 1492e21a3efSScott Long int mfi_max_fw_cmds; 1502e21a3efSScott Long /* 1512e21a3efSScott Long * How many S/G elements we'll ever actually use 1522e21a3efSScott Long */ 15378e36c27SScott Long int mfi_max_sge; 1542e21a3efSScott Long /* 1552e21a3efSScott Long * How many bytes a compound frame is, including all of the extra frames 1562e21a3efSScott Long * that are used for S/G elements. 1572e21a3efSScott Long */ 15878e36c27SScott Long int mfi_cmd_size; 1592e21a3efSScott Long /* 1602e21a3efSScott Long * How large an S/G element is. Used to calculate the number of single 1612e21a3efSScott Long * frames in a command. 1622e21a3efSScott Long */ 16378e36c27SScott Long int mfi_sge_size; 1642e21a3efSScott Long /* 1652e21a3efSScott Long * Max number of sectors that the firmware allows 1662e21a3efSScott Long */ 1672e21a3efSScott Long uint32_t mfi_max_io; 1682e21a3efSScott Long 1692e21a3efSScott Long TAILQ_HEAD(,mfi_ld) mfi_ld_tqh; 1702e21a3efSScott Long eventhandler_tag mfi_eh; 1712e21a3efSScott Long struct cdev *mfi_cdev; 1722e21a3efSScott Long 1732e21a3efSScott Long struct mtx mfi_io_lock; 1742e21a3efSScott Long }; 1752e21a3efSScott Long 1762e21a3efSScott Long extern int mfi_attach(struct mfi_softc *); 1772e21a3efSScott Long extern void mfi_free(struct mfi_softc *); 1782e21a3efSScott Long extern int mfi_shutdown(struct mfi_softc *); 1792e21a3efSScott Long extern void mfi_startio(struct mfi_softc *); 1802e21a3efSScott Long extern void mfi_disk_complete(struct bio *); 1812e21a3efSScott Long extern int mfi_dump_blocks(struct mfi_softc *, int id, uint64_t, void *, int); 1822e21a3efSScott Long 1832e21a3efSScott Long #define MFIQ_ADD(sc, qname) \ 1842e21a3efSScott Long do { \ 1852e21a3efSScott Long struct mfi_qstat *qs; \ 1862e21a3efSScott Long \ 1872e21a3efSScott Long qs = &(sc)->mfi_qstat[qname]; \ 1882e21a3efSScott Long qs->q_length++; \ 1892e21a3efSScott Long if (qs->q_length > qs->q_max) \ 1902e21a3efSScott Long qs->q_max = qs->q_length; \ 1912e21a3efSScott Long } while (0) 1922e21a3efSScott Long 1932e21a3efSScott Long #define MFIQ_REMOVE(sc, qname) (sc)->mfi_qstat[qname].q_length-- 1942e21a3efSScott Long 1952e21a3efSScott Long #define MFIQ_INIT(sc, qname) \ 1962e21a3efSScott Long do { \ 1972e21a3efSScott Long sc->mfi_qstat[qname].q_length = 0; \ 1982e21a3efSScott Long sc->mfi_qstat[qname].q_max = 0; \ 1992e21a3efSScott Long } while (0) 2002e21a3efSScott Long 2012e21a3efSScott Long #define MFIQ_COMMAND_QUEUE(name, index) \ 2022e21a3efSScott Long static __inline void \ 2032e21a3efSScott Long mfi_initq_ ## name (struct mfi_softc *sc) \ 2042e21a3efSScott Long { \ 2052e21a3efSScott Long TAILQ_INIT(&sc->mfi_ ## name); \ 2062e21a3efSScott Long MFIQ_INIT(sc, index); \ 2072e21a3efSScott Long } \ 2082e21a3efSScott Long static __inline void \ 2092e21a3efSScott Long mfi_enqueue_ ## name (struct mfi_command *cm) \ 2102e21a3efSScott Long { \ 2112e21a3efSScott Long if ((cm->cm_flags & MFI_ON_MFIQ_MASK) != 0) { \ 2122e21a3efSScott Long printf("command %p is on another queue, " \ 2132e21a3efSScott Long "flags = %#x\n", cm, cm->cm_flags); \ 2142e21a3efSScott Long panic("command is on another queue"); \ 2152e21a3efSScott Long } \ 2162e21a3efSScott Long TAILQ_INSERT_TAIL(&cm->cm_sc->mfi_ ## name, cm, cm_link); \ 2172e21a3efSScott Long cm->cm_flags |= MFI_ON_ ## index; \ 2182e21a3efSScott Long MFIQ_ADD(cm->cm_sc, index); \ 2192e21a3efSScott Long } \ 2202e21a3efSScott Long static __inline void \ 2212e21a3efSScott Long mfi_requeue_ ## name (struct mfi_command *cm) \ 2222e21a3efSScott Long { \ 2232e21a3efSScott Long if ((cm->cm_flags & MFI_ON_MFIQ_MASK) != 0) { \ 2242e21a3efSScott Long printf("command %p is on another queue, " \ 2252e21a3efSScott Long "flags = %#x\n", cm, cm->cm_flags); \ 2262e21a3efSScott Long panic("command is on another queue"); \ 2272e21a3efSScott Long } \ 2282e21a3efSScott Long TAILQ_INSERT_HEAD(&cm->cm_sc->mfi_ ## name, cm, cm_link); \ 2292e21a3efSScott Long cm->cm_flags |= MFI_ON_ ## index; \ 2302e21a3efSScott Long MFIQ_ADD(cm->cm_sc, index); \ 2312e21a3efSScott Long } \ 2322e21a3efSScott Long static __inline struct mfi_command * \ 2332e21a3efSScott Long mfi_dequeue_ ## name (struct mfi_softc *sc) \ 2342e21a3efSScott Long { \ 2352e21a3efSScott Long struct mfi_command *cm; \ 2362e21a3efSScott Long \ 2372e21a3efSScott Long if ((cm = TAILQ_FIRST(&sc->mfi_ ## name)) != NULL) { \ 2382e21a3efSScott Long if ((cm->cm_flags & MFI_ON_ ## index) == 0) { \ 2392e21a3efSScott Long printf("command %p not in queue, " \ 2402e21a3efSScott Long "flags = %#x, bit = %#x\n", cm, \ 2412e21a3efSScott Long cm->cm_flags, MFI_ON_ ## index); \ 2422e21a3efSScott Long panic("command not in queue"); \ 2432e21a3efSScott Long } \ 2442e21a3efSScott Long TAILQ_REMOVE(&sc->mfi_ ## name, cm, cm_link); \ 2452e21a3efSScott Long cm->cm_flags &= ~MFI_ON_ ## index; \ 2462e21a3efSScott Long MFIQ_REMOVE(sc, index); \ 2472e21a3efSScott Long } \ 2482e21a3efSScott Long return (cm); \ 2492e21a3efSScott Long } \ 2502e21a3efSScott Long static __inline void \ 2512e21a3efSScott Long mfi_remove_ ## name (struct mfi_command *cm) \ 2522e21a3efSScott Long { \ 2532e21a3efSScott Long if ((cm->cm_flags & MFI_ON_ ## index) == 0) { \ 2542e21a3efSScott Long printf("command %p not in queue, flags = %#x, " \ 2552e21a3efSScott Long "bit = %#x\n", cm, cm->cm_flags, \ 2562e21a3efSScott Long MFI_ON_ ## index); \ 2572e21a3efSScott Long panic("command not in queue"); \ 2582e21a3efSScott Long } \ 2592e21a3efSScott Long TAILQ_REMOVE(&cm->cm_sc->mfi_ ## name, cm, cm_link); \ 2602e21a3efSScott Long cm->cm_flags &= ~MFI_ON_ ## index; \ 2612e21a3efSScott Long MFIQ_REMOVE(cm->cm_sc, index); \ 2622e21a3efSScott Long } \ 2632e21a3efSScott Long struct hack 2642e21a3efSScott Long 2652e21a3efSScott Long MFIQ_COMMAND_QUEUE(free, MFIQ_FREE); 2662e21a3efSScott Long MFIQ_COMMAND_QUEUE(ready, MFIQ_READY); 2672e21a3efSScott Long MFIQ_COMMAND_QUEUE(busy, MFIQ_BUSY); 2682e21a3efSScott Long 2692e21a3efSScott Long static __inline void 2702e21a3efSScott Long mfi_initq_bio(struct mfi_softc *sc) 2712e21a3efSScott Long { 2722e21a3efSScott Long bioq_init(&sc->mfi_bioq); 2732e21a3efSScott Long MFIQ_INIT(sc, MFIQ_BIO); 2742e21a3efSScott Long } 2752e21a3efSScott Long 2762e21a3efSScott Long static __inline void 2772e21a3efSScott Long mfi_enqueue_bio(struct mfi_softc *sc, struct bio *bp) 2782e21a3efSScott Long { 2792e21a3efSScott Long bioq_insert_tail(&sc->mfi_bioq, bp); 2802e21a3efSScott Long MFIQ_ADD(sc, MFIQ_BIO); 2812e21a3efSScott Long } 2822e21a3efSScott Long 2832e21a3efSScott Long static __inline struct bio * 2842e21a3efSScott Long mfi_dequeue_bio(struct mfi_softc *sc) 2852e21a3efSScott Long { 2862e21a3efSScott Long struct bio *bp; 2872e21a3efSScott Long 2882e21a3efSScott Long if ((bp = bioq_first(&sc->mfi_bioq)) != NULL) { 2892e21a3efSScott Long bioq_remove(&sc->mfi_bioq, bp); 2902e21a3efSScott Long MFIQ_REMOVE(sc, MFIQ_BIO); 2912e21a3efSScott Long } 2922e21a3efSScott Long return (bp); 2932e21a3efSScott Long } 2942e21a3efSScott Long 2952e21a3efSScott Long static __inline void 2962e21a3efSScott Long mfi_print_sense(struct mfi_softc *sc, void *sense) 2972e21a3efSScott Long { 2982e21a3efSScott Long int error, key, asc, ascq; 2992e21a3efSScott Long 3002e21a3efSScott Long scsi_extract_sense((struct scsi_sense_data *)sense, 3012e21a3efSScott Long &error, &key, &asc, &ascq); 3022e21a3efSScott Long device_printf(sc->mfi_dev, "sense error %d, sense_key %d, " 3032e21a3efSScott Long "asc %d, ascq %d\n", error, key, asc, ascq); 3042e21a3efSScott Long } 3052e21a3efSScott Long 3062e21a3efSScott Long 3072e21a3efSScott Long #define MFI_WRITE4(sc, reg, val) bus_space_write_4((sc)->mfi_btag, \ 3082e21a3efSScott Long sc->mfi_bhandle, (reg), (val)) 3092e21a3efSScott Long #define MFI_READ4(sc, reg) bus_space_read_4((sc)->mfi_btag, \ 3102e21a3efSScott Long (sc)->mfi_bhandle, (reg)) 3112e21a3efSScott Long #define MFI_WRITE2(sc, reg, val) bus_space_write_2((sc)->mfi_btag, \ 3122e21a3efSScott Long sc->mfi_bhandle, (reg), (val)) 3132e21a3efSScott Long #define MFI_READ2(sc, reg) bus_space_read_2((sc)->mfi_btag, \ 3142e21a3efSScott Long (sc)->mfi_bhandle, (reg)) 3152e21a3efSScott Long #define MFI_WRITE1(sc, reg, val) bus_space_write_1((sc)->mfi_btag, \ 3162e21a3efSScott Long sc->mfi_bhandle, (reg), (val)) 3172e21a3efSScott Long #define MFI_READ1(sc, reg) bus_space_read_1((sc)->mfi_btag, \ 3182e21a3efSScott Long (sc)->mfi_bhandle, (reg)) 3192e21a3efSScott Long 3202e21a3efSScott Long MALLOC_DECLARE(M_MFIBUF); 3212e21a3efSScott Long 3222e21a3efSScott Long #endif /* _MFIVAR_H */ 323