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)) 702e21a3efSScott Long void (* cm_complete)(struct mfi_command *cm); 712e21a3efSScott Long void *cm_private; 722e21a3efSScott Long }; 732e21a3efSScott Long 742e21a3efSScott Long struct mfi_ld { 752e21a3efSScott Long TAILQ_ENTRY(mfi_ld) ld_link; 762e21a3efSScott Long device_t ld_disk; 772e21a3efSScott Long uint64_t ld_sectors; 782e21a3efSScott Long uint32_t ld_secsize; 792e21a3efSScott Long int ld_id; 802e21a3efSScott Long }; 812e21a3efSScott Long 822e21a3efSScott Long struct mfi_softc { 832e21a3efSScott Long device_t mfi_dev; 842e21a3efSScott Long int mfi_flags; 852e21a3efSScott Long #define MFI_FLAGS_SG64 (1<<0) 862e21a3efSScott Long #define MFI_FLAGS_QFRZN (1<<1) 872e21a3efSScott Long #define MFI_FLAGS_OPEN (1<<2) 882e21a3efSScott Long 892e21a3efSScott Long struct mfi_hwcomms *mfi_comms; 902e21a3efSScott Long TAILQ_HEAD(,mfi_command) mfi_free; 912e21a3efSScott Long TAILQ_HEAD(,mfi_command) mfi_ready; 922e21a3efSScott Long TAILQ_HEAD(,mfi_command) mfi_busy; 932e21a3efSScott Long struct bio_queue_head mfi_bioq; 942e21a3efSScott Long struct mfi_qstat mfi_qstat[MFIQ_COUNT]; 952e21a3efSScott Long 962e21a3efSScott Long struct resource *mfi_regs_resource; 972e21a3efSScott Long bus_space_handle_t mfi_bhandle; 982e21a3efSScott Long bus_space_tag_t mfi_btag; 992e21a3efSScott Long int mfi_regs_rid; 1002e21a3efSScott Long 1012e21a3efSScott Long bus_dma_tag_t mfi_parent_dmat; 1022e21a3efSScott Long bus_dma_tag_t mfi_buffer_dmat; 1032e21a3efSScott Long 1042e21a3efSScott Long bus_dma_tag_t mfi_comms_dmat; 1052e21a3efSScott Long bus_dmamap_t mfi_comms_dmamap; 1062e21a3efSScott Long uint32_t mfi_comms_busaddr; 1072e21a3efSScott Long 1082e21a3efSScott Long bus_dma_tag_t mfi_frames_dmat; 1092e21a3efSScott Long bus_dmamap_t mfi_frames_dmamap; 1102e21a3efSScott Long uint32_t mfi_frames_busaddr; 1112e21a3efSScott Long union mfi_frame *mfi_frames; 1122e21a3efSScott Long 1132e21a3efSScott Long bus_dma_tag_t mfi_sense_dmat; 1142e21a3efSScott Long bus_dmamap_t mfi_sense_dmamap; 1152e21a3efSScott Long uint32_t mfi_sense_busaddr; 1162e21a3efSScott Long struct mfi_sense *mfi_sense; 1172e21a3efSScott Long 1182e21a3efSScott Long struct resource *mfi_irq; 1192e21a3efSScott Long void *mfi_intr; 1202e21a3efSScott Long int mfi_irq_rid; 1212e21a3efSScott Long 1222e21a3efSScott Long struct intr_config_hook mfi_ich; 1232e21a3efSScott Long eventhandler_tag eh; 1242e21a3efSScott Long int mfi_probe_count; 1252e21a3efSScott Long 1262e21a3efSScott Long /* 1272e21a3efSScott Long * Allocation for the command array. Used as an indexable array to 1282e21a3efSScott Long * recover completed commands. 1292e21a3efSScott Long */ 1302e21a3efSScott Long struct mfi_command *mfi_commands; 1312e21a3efSScott Long /* 1322e21a3efSScott Long * How many commands were actually allocated 1332e21a3efSScott Long */ 1342e21a3efSScott Long int mfi_total_cmds; 1352e21a3efSScott Long /* 1362e21a3efSScott Long * How many commands the firmware can handle. Also how big the reply 1372e21a3efSScott Long * queue is, minus 1. 1382e21a3efSScott Long */ 1392e21a3efSScott Long int mfi_max_fw_cmds; 1402e21a3efSScott Long /* 1412e21a3efSScott Long * Max number of S/G elements the firmware can handle 1422e21a3efSScott Long */ 1432e21a3efSScott Long int mfi_max_fw_sgl; 1442e21a3efSScott Long /* 1452e21a3efSScott Long * How many S/G elements we'll ever actually use 1462e21a3efSScott Long */ 1472e21a3efSScott Long int mfi_total_sgl; 1482e21a3efSScott Long /* 1492e21a3efSScott Long * How many bytes a compound frame is, including all of the extra frames 1502e21a3efSScott Long * that are used for S/G elements. 1512e21a3efSScott Long */ 1522e21a3efSScott Long int mfi_frame_size; 1532e21a3efSScott Long /* 1542e21a3efSScott Long * How large an S/G element is. Used to calculate the number of single 1552e21a3efSScott Long * frames in a command. 1562e21a3efSScott Long */ 1572e21a3efSScott Long int mfi_sgsize; 1582e21a3efSScott Long /* 1592e21a3efSScott Long * Max number of sectors that the firmware allows 1602e21a3efSScott Long */ 1612e21a3efSScott Long uint32_t mfi_max_io; 1622e21a3efSScott Long 1632e21a3efSScott Long TAILQ_HEAD(,mfi_ld) mfi_ld_tqh; 1642e21a3efSScott Long eventhandler_tag mfi_eh; 1652e21a3efSScott Long struct cdev *mfi_cdev; 1662e21a3efSScott Long 1672e21a3efSScott Long struct mtx mfi_io_lock; 1682e21a3efSScott Long }; 1692e21a3efSScott Long 1702e21a3efSScott Long extern int mfi_attach(struct mfi_softc *); 1712e21a3efSScott Long extern void mfi_free(struct mfi_softc *); 1722e21a3efSScott Long extern int mfi_shutdown(struct mfi_softc *); 1732e21a3efSScott Long extern void mfi_startio(struct mfi_softc *); 1742e21a3efSScott Long extern void mfi_disk_complete(struct bio *); 1752e21a3efSScott Long extern int mfi_dump_blocks(struct mfi_softc *, int id, uint64_t, void *, int); 1762e21a3efSScott Long 1772e21a3efSScott Long #define MFIQ_ADD(sc, qname) \ 1782e21a3efSScott Long do { \ 1792e21a3efSScott Long struct mfi_qstat *qs; \ 1802e21a3efSScott Long \ 1812e21a3efSScott Long qs = &(sc)->mfi_qstat[qname]; \ 1822e21a3efSScott Long qs->q_length++; \ 1832e21a3efSScott Long if (qs->q_length > qs->q_max) \ 1842e21a3efSScott Long qs->q_max = qs->q_length; \ 1852e21a3efSScott Long } while (0) 1862e21a3efSScott Long 1872e21a3efSScott Long #define MFIQ_REMOVE(sc, qname) (sc)->mfi_qstat[qname].q_length-- 1882e21a3efSScott Long 1892e21a3efSScott Long #define MFIQ_INIT(sc, qname) \ 1902e21a3efSScott Long do { \ 1912e21a3efSScott Long sc->mfi_qstat[qname].q_length = 0; \ 1922e21a3efSScott Long sc->mfi_qstat[qname].q_max = 0; \ 1932e21a3efSScott Long } while (0) 1942e21a3efSScott Long 1952e21a3efSScott Long #define MFIQ_COMMAND_QUEUE(name, index) \ 1962e21a3efSScott Long static __inline void \ 1972e21a3efSScott Long mfi_initq_ ## name (struct mfi_softc *sc) \ 1982e21a3efSScott Long { \ 1992e21a3efSScott Long TAILQ_INIT(&sc->mfi_ ## name); \ 2002e21a3efSScott Long MFIQ_INIT(sc, index); \ 2012e21a3efSScott Long } \ 2022e21a3efSScott Long static __inline void \ 2032e21a3efSScott Long mfi_enqueue_ ## name (struct mfi_command *cm) \ 2042e21a3efSScott Long { \ 2052e21a3efSScott Long if ((cm->cm_flags & MFI_ON_MFIQ_MASK) != 0) { \ 2062e21a3efSScott Long printf("command %p is on another queue, " \ 2072e21a3efSScott Long "flags = %#x\n", cm, cm->cm_flags); \ 2082e21a3efSScott Long panic("command is on another queue"); \ 2092e21a3efSScott Long } \ 2102e21a3efSScott Long TAILQ_INSERT_TAIL(&cm->cm_sc->mfi_ ## name, cm, cm_link); \ 2112e21a3efSScott Long cm->cm_flags |= MFI_ON_ ## index; \ 2122e21a3efSScott Long MFIQ_ADD(cm->cm_sc, index); \ 2132e21a3efSScott Long } \ 2142e21a3efSScott Long static __inline void \ 2152e21a3efSScott Long mfi_requeue_ ## name (struct mfi_command *cm) \ 2162e21a3efSScott Long { \ 2172e21a3efSScott Long if ((cm->cm_flags & MFI_ON_MFIQ_MASK) != 0) { \ 2182e21a3efSScott Long printf("command %p is on another queue, " \ 2192e21a3efSScott Long "flags = %#x\n", cm, cm->cm_flags); \ 2202e21a3efSScott Long panic("command is on another queue"); \ 2212e21a3efSScott Long } \ 2222e21a3efSScott Long TAILQ_INSERT_HEAD(&cm->cm_sc->mfi_ ## name, cm, cm_link); \ 2232e21a3efSScott Long cm->cm_flags |= MFI_ON_ ## index; \ 2242e21a3efSScott Long MFIQ_ADD(cm->cm_sc, index); \ 2252e21a3efSScott Long } \ 2262e21a3efSScott Long static __inline struct mfi_command * \ 2272e21a3efSScott Long mfi_dequeue_ ## name (struct mfi_softc *sc) \ 2282e21a3efSScott Long { \ 2292e21a3efSScott Long struct mfi_command *cm; \ 2302e21a3efSScott Long \ 2312e21a3efSScott Long if ((cm = TAILQ_FIRST(&sc->mfi_ ## name)) != NULL) { \ 2322e21a3efSScott Long if ((cm->cm_flags & MFI_ON_ ## index) == 0) { \ 2332e21a3efSScott Long printf("command %p not in queue, " \ 2342e21a3efSScott Long "flags = %#x, bit = %#x\n", cm, \ 2352e21a3efSScott Long cm->cm_flags, MFI_ON_ ## index); \ 2362e21a3efSScott Long panic("command not in queue"); \ 2372e21a3efSScott Long } \ 2382e21a3efSScott Long TAILQ_REMOVE(&sc->mfi_ ## name, cm, cm_link); \ 2392e21a3efSScott Long cm->cm_flags &= ~MFI_ON_ ## index; \ 2402e21a3efSScott Long MFIQ_REMOVE(sc, index); \ 2412e21a3efSScott Long } \ 2422e21a3efSScott Long return (cm); \ 2432e21a3efSScott Long } \ 2442e21a3efSScott Long static __inline void \ 2452e21a3efSScott Long mfi_remove_ ## name (struct mfi_command *cm) \ 2462e21a3efSScott Long { \ 2472e21a3efSScott Long if ((cm->cm_flags & MFI_ON_ ## index) == 0) { \ 2482e21a3efSScott Long printf("command %p not in queue, flags = %#x, " \ 2492e21a3efSScott Long "bit = %#x\n", cm, cm->cm_flags, \ 2502e21a3efSScott Long MFI_ON_ ## index); \ 2512e21a3efSScott Long panic("command not in queue"); \ 2522e21a3efSScott Long } \ 2532e21a3efSScott Long TAILQ_REMOVE(&cm->cm_sc->mfi_ ## name, cm, cm_link); \ 2542e21a3efSScott Long cm->cm_flags &= ~MFI_ON_ ## index; \ 2552e21a3efSScott Long MFIQ_REMOVE(cm->cm_sc, index); \ 2562e21a3efSScott Long } \ 2572e21a3efSScott Long struct hack 2582e21a3efSScott Long 2592e21a3efSScott Long MFIQ_COMMAND_QUEUE(free, MFIQ_FREE); 2602e21a3efSScott Long MFIQ_COMMAND_QUEUE(ready, MFIQ_READY); 2612e21a3efSScott Long MFIQ_COMMAND_QUEUE(busy, MFIQ_BUSY); 2622e21a3efSScott Long 2632e21a3efSScott Long static __inline void 2642e21a3efSScott Long mfi_initq_bio(struct mfi_softc *sc) 2652e21a3efSScott Long { 2662e21a3efSScott Long bioq_init(&sc->mfi_bioq); 2672e21a3efSScott Long MFIQ_INIT(sc, MFIQ_BIO); 2682e21a3efSScott Long } 2692e21a3efSScott Long 2702e21a3efSScott Long static __inline void 2712e21a3efSScott Long mfi_enqueue_bio(struct mfi_softc *sc, struct bio *bp) 2722e21a3efSScott Long { 2732e21a3efSScott Long bioq_insert_tail(&sc->mfi_bioq, bp); 2742e21a3efSScott Long MFIQ_ADD(sc, MFIQ_BIO); 2752e21a3efSScott Long } 2762e21a3efSScott Long 2772e21a3efSScott Long static __inline struct bio * 2782e21a3efSScott Long mfi_dequeue_bio(struct mfi_softc *sc) 2792e21a3efSScott Long { 2802e21a3efSScott Long struct bio *bp; 2812e21a3efSScott Long 2822e21a3efSScott Long if ((bp = bioq_first(&sc->mfi_bioq)) != NULL) { 2832e21a3efSScott Long bioq_remove(&sc->mfi_bioq, bp); 2842e21a3efSScott Long MFIQ_REMOVE(sc, MFIQ_BIO); 2852e21a3efSScott Long } 2862e21a3efSScott Long return (bp); 2872e21a3efSScott Long } 2882e21a3efSScott Long 2892e21a3efSScott Long static __inline void 2902e21a3efSScott Long mfi_print_sense(struct mfi_softc *sc, void *sense) 2912e21a3efSScott Long { 2922e21a3efSScott Long int error, key, asc, ascq; 2932e21a3efSScott Long 2942e21a3efSScott Long scsi_extract_sense((struct scsi_sense_data *)sense, 2952e21a3efSScott Long &error, &key, &asc, &ascq); 2962e21a3efSScott Long device_printf(sc->mfi_dev, "sense error %d, sense_key %d, " 2972e21a3efSScott Long "asc %d, ascq %d\n", error, key, asc, ascq); 2982e21a3efSScott Long } 2992e21a3efSScott Long 3002e21a3efSScott Long 3012e21a3efSScott Long #define MFI_WRITE4(sc, reg, val) bus_space_write_4((sc)->mfi_btag, \ 3022e21a3efSScott Long sc->mfi_bhandle, (reg), (val)) 3032e21a3efSScott Long #define MFI_READ4(sc, reg) bus_space_read_4((sc)->mfi_btag, \ 3042e21a3efSScott Long (sc)->mfi_bhandle, (reg)) 3052e21a3efSScott Long #define MFI_WRITE2(sc, reg, val) bus_space_write_2((sc)->mfi_btag, \ 3062e21a3efSScott Long sc->mfi_bhandle, (reg), (val)) 3072e21a3efSScott Long #define MFI_READ2(sc, reg) bus_space_read_2((sc)->mfi_btag, \ 3082e21a3efSScott Long (sc)->mfi_bhandle, (reg)) 3092e21a3efSScott Long #define MFI_WRITE1(sc, reg, val) bus_space_write_1((sc)->mfi_btag, \ 3102e21a3efSScott Long sc->mfi_bhandle, (reg), (val)) 3112e21a3efSScott Long #define MFI_READ1(sc, reg) bus_space_read_1((sc)->mfi_btag, \ 3122e21a3efSScott Long (sc)->mfi_bhandle, (reg)) 3132e21a3efSScott Long 3142e21a3efSScott Long MALLOC_DECLARE(M_MFIBUF); 3152e21a3efSScott Long 3162e21a3efSScott Long #endif /* _MFIVAR_H */ 317