1 /* $OpenBSD: oosiopvar.h,v 1.6 2020/07/22 13:16:04 krw Exp $ */ 2 /* $NetBSD: oosiopvar.h,v 1.2 2003/05/03 18:11:23 wiz Exp $ */ 3 4 /* 5 * Copyright (c) 2001 Shuichiro URATA. 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 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include <uvm/uvm_extern.h> 31 32 #define OOSIOP_NTGT 8 /* Max targets */ 33 #define OOSIOP_NCB 32 /* Initial command buffers */ 34 #define OOSIOP_NSG (MIN(atop(MAXPHYS) + 1, 32)) /* Max S/G operation */ 35 #define OOSIOP_MAX_XFER ptoa(OOSIOP_NSG - 1) 36 37 struct oosiop_xfer { 38 /* script for scatter/gather DMA (move*nsg+jump) */ 39 u_int32_t datain_scr[(OOSIOP_NSG + 1) * 2]; 40 u_int32_t dataout_scr[(OOSIOP_NSG + 1) * 2]; 41 42 u_int8_t msgin[8]; 43 u_int8_t msgout[8]; 44 u_int8_t status; 45 u_int8_t pad[7]; 46 } __packed; 47 48 #define SCSI_OOSIOP_NOSTATUS 0xff /* device didn't report status */ 49 50 #define OOSIOP_XFEROFF(x) offsetof(struct oosiop_xfer, x) 51 #define OOSIOP_DINSCROFF OOSIOP_XFEROFF(datain_scr[0]) 52 #define OOSIOP_DOUTSCROFF OOSIOP_XFEROFF(dataout_scr[0]) 53 #define OOSIOP_MSGINOFF OOSIOP_XFEROFF(msgin[0]) 54 #define OOSIOP_MSGOUTOFF OOSIOP_XFEROFF(msgout[0]) 55 56 #define OOSIOP_XFERSCR_SYNC(sc, cb, ops) \ 57 bus_dmamap_sync((sc)->sc_dmat, (cb)->xferdma, OOSIOP_DINSCROFF, \ 58 OOSIOP_MSGINOFF - OOSIOP_DINSCROFF, (ops)) 59 #define OOSIOP_DINSCR_SYNC(sc, cb, ops) \ 60 bus_dmamap_sync((sc)->sc_dmat, (cb)->xferdma, OOSIOP_DINSCROFF, \ 61 OOSIOP_DOUTSCROFF - OOSIOP_DINSCROFF, (ops)) 62 #define OOSIOP_DOUTSCR_SYNC(sc, cb, ops) \ 63 bus_dmamap_sync((sc)->sc_dmat, (cb)->xferdma, OOSIOP_DOUTSCROFF,\ 64 OOSIOP_MSGINOFF - OOSIOP_DOUTSCROFF, (ops)) 65 #define OOSIOP_XFERMSG_SYNC(sc, cb, ops) \ 66 bus_dmamap_sync((sc)->sc_dmat, (cb)->xferdma, OOSIOP_MSGINOFF, \ 67 sizeof(struct oosiop_xfer) - OOSIOP_MSGINOFF, (ops)) 68 69 #define OOSIOP_SCRIPT_SYNC(sc, ops) \ 70 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_scrdma, \ 71 0, sizeof(oosiop_script), (ops)) 72 73 struct oosiop_cb { 74 TAILQ_ENTRY(oosiop_cb) chain; 75 76 struct scsi_xfer *xs; /* SCSI xfer ctrl block from above */ 77 int flags; 78 int id; /* target scsi id */ 79 int lun; /* target lun */ 80 81 bus_dmamap_t cmddma; /* DMA map for command out */ 82 bus_dmamap_t datadma; /* DMA map for data I/O */ 83 bus_dmamap_t xferdma; /* DMA map for xfer block */ 84 85 int curdp; /* current data pointer */ 86 int savedp; /* saved data pointer */ 87 int msgoutlen; 88 89 int xsflags; /* copy of xs->flags */ 90 int datalen; /* copy of xs->datalen */ 91 int cmdlen; /* copy of xs->cmdlen */ 92 93 struct oosiop_xfer *xfer; /* DMA xfer block */ 94 }; 95 96 /* oosiop_cb flags */ 97 #define CBF_SELTOUT 0x01 /* Selection timeout */ 98 #define CBF_TIMEOUT 0x02 /* Command timeout */ 99 #define CBF_AUTOSENSE 0x04 /* Request sense due to SCSI_CHECK */ 100 101 struct oosiop_target { 102 struct oosiop_cb *nexus; 103 int flags; 104 u_int8_t scf; /* synchronous clock divisor */ 105 u_int8_t sxfer; /* synchronous period and offset */ 106 }; 107 108 /* target flags */ 109 #define TGTF_SYNCNEG 0x01 /* Trigger synchronous negotiation */ 110 #define TGTF_WAITSDTR 0x02 /* Waiting SDTR from target */ 111 112 struct oosiop_softc { 113 struct device sc_dev; 114 115 bus_space_tag_t sc_bst; /* bus space tag */ 116 bus_space_handle_t sc_bsh; /* bus space handle */ 117 118 bus_dma_tag_t sc_dmat; /* bus DMA tag */ 119 bus_dmamap_t sc_scrdma; /* script DMA map */ 120 121 bus_addr_t sc_scrbase; /* script DMA base address */ 122 u_int32_t *sc_scr; /* ptr to script memory */ 123 124 int sc_chip; /* 700 or 700-66 */ 125 #define OOSIOP_700 0 126 #define OOSIOP_700_66 1 127 128 int sc_id; /* SCSI ID of this interface */ 129 int sc_freq; /* SCLK frequency */ 130 int sc_ccf; /* asynchronous divisor (*10) */ 131 u_int8_t sc_dcntl; 132 u_int8_t sc_minperiod; 133 134 struct oosiop_target sc_tgt[OOSIOP_NTGT]; 135 136 /* Lists of command blocks */ 137 TAILQ_HEAD(oosiop_cb_queue, oosiop_cb) sc_free_cb, 138 sc_cbq; 139 140 struct mutex sc_cb_mtx; 141 struct scsi_iopool sc_iopool; 142 143 struct oosiop_cb *sc_curcb; /* current command */ 144 struct oosiop_cb *sc_lastcb; /* last activated command */ 145 146 bus_addr_t sc_reselbuf; /* msgin buffer for reselection */ 147 int sc_resid; /* reselected target id */ 148 149 int sc_active; 150 int sc_nextdsp; 151 152 uint8_t sc_scntl0; 153 uint8_t sc_dmode; 154 uint8_t sc_dwt; 155 uint8_t sc_ctest7; 156 }; 157 158 #define oosiop_read_1(sc, addr) \ 159 bus_space_read_1((sc)->sc_bst, (sc)->sc_bsh, (addr)) 160 #define oosiop_write_1(sc, addr, data) \ 161 bus_space_write_1((sc)->sc_bst, (sc)->sc_bsh, (addr), (data)) 162 /* XXX byte swapping should be handled by MD bus_space(9)? */ 163 #define oosiop_read_4(sc, addr) \ 164 letoh32(bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (addr))) 165 #define oosiop_write_4(sc, addr, data) \ 166 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (addr), htole32(data)) 167 168 void oosiop_attach(struct oosiop_softc *); 169 int oosiop_intr(struct oosiop_softc *); 170