1 /*- 2 * SPDX-License-Identifier: BSD-4-Clause 3 * 4 * Copyright (c) 2003 Hidetoshi Shimokawa 5 * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the acknowledgement as bellow: 18 * 19 * This product includes software developed by K. Kobayashi and H. Shimokawa 20 * 21 * 4. The name of the author may not be used to endorse or promote products 22 * derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 28 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 33 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 * POSSIBILITY OF SUCH DAMAGE. 35 * 36 */ 37 38 typedef struct thread fw_proc; 39 #include <sys/selinfo.h> 40 41 #include <sys/uio.h> 42 #include <sys/mutex.h> 43 #include <sys/taskqueue.h> 44 45 #define splfw splimp 46 47 STAILQ_HEAD(fw_xferlist, fw_xfer); 48 49 struct fw_device { 50 uint16_t dst; 51 struct fw_eui64 eui; 52 uint8_t speed; 53 uint8_t maxrec; 54 uint8_t nport; 55 uint8_t power; 56 #define CSRROMOFF 0x400 57 #define CSRROMSIZE 0x400 58 int rommax; /* offset from 0xffff f000 0000 */ 59 uint32_t csrrom[CSRROMSIZE / 4]; 60 int rcnt; 61 struct firewire_comm *fc; 62 uint32_t status; 63 #define FWDEVINIT 1 64 #define FWDEVATTACHED 2 65 #define FWDEVINVAL 3 66 STAILQ_ENTRY(fw_device) link; 67 }; 68 69 struct firewire_softc { 70 struct cdev *dev; 71 struct firewire_comm *fc; 72 int watchdog_clock; 73 }; 74 75 #define FW_MAX_DMACH 0x20 76 #define FW_MAX_DEVCH FW_MAX_DMACH 77 #define FW_XFERTIMEOUT 1 78 79 struct firewire_dev_comm { 80 device_t dev; 81 struct firewire_comm *fc; 82 void (*post_busreset) (void *); 83 void (*post_explore) (void *); 84 }; 85 86 struct tcode_info { 87 u_char hdr_len; /* IEEE1394 header length */ 88 u_char flag; 89 #define FWTI_REQ (1 << 0) 90 #define FWTI_RES (1 << 1) 91 #define FWTI_TLABEL (1 << 2) 92 #define FWTI_BLOCK_STR (1 << 3) 93 #define FWTI_BLOCK_ASY (1 << 4) 94 u_char valid_res; 95 }; 96 97 struct firewire_comm { 98 device_t dev; 99 device_t bdev; 100 uint16_t busid:10, 101 nodeid:6; 102 u_int mode; 103 u_int nport; 104 u_int speed; 105 u_int maxrec; 106 u_int irm; 107 u_int max_node; 108 u_int max_hop; 109 #define FWPHYASYST (1 << 0) 110 uint32_t status; 111 #define FWBUSDETACH (-2) 112 #define FWBUSNOTREADY (-1) 113 #define FWBUSRESET 0 114 #define FWBUSINIT 1 115 #define FWBUSCYMELECT 2 116 #define FWBUSMGRELECT 3 117 #define FWBUSMGRDONE 4 118 #define FWBUSEXPLORE 5 119 #define FWBUSPHYCONF 6 120 #define FWBUSEXPDONE 7 121 #define FWBUSCOMPLETION 10 122 int nisodma; 123 struct fw_eui64 eui; 124 struct fw_xferq 125 *arq, *atq, *ars, *ats, *it[FW_MAX_DMACH],*ir[FW_MAX_DMACH]; 126 struct fw_xferlist tlabels[0x40]; 127 u_char last_tlabel[0x40]; 128 struct mtx tlabel_lock; 129 STAILQ_HEAD(, fw_bind) binds; 130 STAILQ_HEAD(, fw_device) devices; 131 u_int sid_cnt; 132 #define CSRSIZE 0x4000 133 uint32_t csr_arc[CSRSIZE / 4]; 134 #define CROMSIZE 0x400 135 uint32_t *config_rom; 136 struct crom_src_buf *crom_src_buf; 137 struct crom_src *crom_src; 138 struct crom_chunk *crom_root; 139 struct fw_topology_map *topology_map; 140 struct fw_speed_map *speed_map; 141 struct callout busprobe_callout; 142 struct callout bmr_callout; 143 struct callout timeout_callout; 144 struct task task_timeout; 145 uint32_t (*cyctimer) (struct firewire_comm *); 146 void (*ibr) (struct firewire_comm *); 147 uint32_t (*set_bmr) (struct firewire_comm *, uint32_t); 148 int (*ioctl) (struct cdev *, u_long, caddr_t, int, fw_proc *); 149 int (*irx_enable) (struct firewire_comm *, int); 150 int (*irx_disable) (struct firewire_comm *, int); 151 int (*itx_enable) (struct firewire_comm *, int); 152 int (*itx_disable) (struct firewire_comm *, int); 153 void (*timeout) (void *); 154 void (*poll) (struct firewire_comm *, int, int); 155 void (*set_intr) (struct firewire_comm *, int); 156 void (*irx_post) (struct firewire_comm *, uint32_t *); 157 void (*itx_post) (struct firewire_comm *, uint32_t *); 158 struct tcode_info *tcode; 159 bus_dma_tag_t dmat; 160 struct mtx mtx; 161 struct mtx wait_lock; 162 struct taskqueue *taskqueue; 163 struct proc *probe_thread; 164 }; 165 #define CSRARC(sc, offset) ((sc)->csr_arc[(offset) / 4]) 166 167 #define FW_GMTX(fc) (&(fc)->mtx) 168 #define FW_GLOCK(fc) mtx_lock(FW_GMTX(fc)) 169 #define FW_GUNLOCK(fc) mtx_unlock(FW_GMTX(fc)) 170 #define FW_GLOCK_ASSERT(fc) mtx_assert(FW_GMTX(fc), MA_OWNED) 171 172 struct fw_xferq { 173 int flag; 174 #define FWXFERQ_CHTAGMASK 0xff 175 #define FWXFERQ_RUNNING (1 << 8) 176 #define FWXFERQ_STREAM (1 << 9) 177 178 #define FWXFERQ_BULK (1 << 11) 179 #define FWXFERQ_MODEMASK (7 << 10) 180 181 #define FWXFERQ_EXTBUF (1 << 13) 182 #define FWXFERQ_OPEN (1 << 14) 183 184 #define FWXFERQ_HANDLER (1 << 16) 185 #define FWXFERQ_WAKEUP (1 << 17) 186 void (*start) (struct firewire_comm *); 187 int dmach; 188 struct fw_xferlist q; 189 u_int queued; 190 u_int maxq; 191 u_int psize; 192 struct fwdma_alloc_multi *buf; 193 u_int bnchunk; 194 u_int bnpacket; 195 struct fw_bulkxfer *bulkxfer; 196 STAILQ_HEAD(, fw_bulkxfer) stvalid; 197 STAILQ_HEAD(, fw_bulkxfer) stfree; 198 STAILQ_HEAD(, fw_bulkxfer) stdma; 199 struct fw_bulkxfer *stproc; 200 struct selinfo rsel; 201 caddr_t sc; 202 void (*hand) (struct fw_xferq *); 203 }; 204 205 struct fw_bulkxfer { 206 int poffset; 207 struct mbuf *mbuf; 208 STAILQ_ENTRY(fw_bulkxfer) link; 209 caddr_t start; 210 caddr_t end; 211 int resp; 212 }; 213 214 struct fw_bind { 215 u_int64_t start; 216 u_int64_t end; 217 struct fw_xferlist xferlist; 218 STAILQ_ENTRY(fw_bind) fclist; 219 STAILQ_ENTRY(fw_bind) chlist; 220 void *sc; 221 }; 222 223 struct fw_xfer { 224 caddr_t sc; 225 struct firewire_comm *fc; 226 struct fw_xferq *q; 227 struct timeval tv; 228 int8_t resp; 229 #define FWXF_INIT 0x00 230 #define FWXF_INQ 0x01 231 #define FWXF_START 0x02 232 #define FWXF_SENT 0x04 233 #define FWXF_SENTERR 0x08 234 #define FWXF_BUSY 0x10 235 #define FWXF_RCVD 0x20 236 237 #define FWXF_WAKE 0x80 238 uint8_t flag; 239 int8_t tl; 240 void (*hand) (struct fw_xfer *); 241 struct { 242 struct fw_pkt hdr; 243 uint32_t *payload; 244 uint16_t pay_len; 245 uint8_t spd; 246 } send, recv; 247 struct mbuf *mbuf; 248 STAILQ_ENTRY(fw_xfer) link; 249 STAILQ_ENTRY(fw_xfer) tlabel; 250 struct malloc_type *malloc; 251 }; 252 253 struct fw_rcv_buf { 254 struct firewire_comm *fc; 255 struct fw_xfer *xfer; 256 struct iovec *vec; 257 u_int nvec; 258 uint8_t spd; 259 }; 260 261 void fw_sidrcv (struct firewire_comm *, uint32_t *, u_int); 262 void fw_rcv (struct fw_rcv_buf *); 263 void fw_xfer_unload (struct fw_xfer *); 264 void fw_xfer_free_buf (struct fw_xfer *); 265 void fw_xfer_free (struct fw_xfer*); 266 struct fw_xfer *fw_xfer_alloc (struct malloc_type *); 267 struct fw_xfer *fw_xfer_alloc_buf (struct malloc_type *, int, int); 268 void fw_init (struct firewire_comm *); 269 int fw_tbuf_update (struct firewire_comm *, int, int); 270 int fw_rbuf_update (struct firewire_comm *, int, int); 271 int fw_bindadd (struct firewire_comm *, struct fw_bind *); 272 int fw_bindremove (struct firewire_comm *, struct fw_bind *); 273 int fw_xferlist_add (struct fw_xferlist *, struct malloc_type *, int, int, int, 274 struct firewire_comm *, void *, void (*)(struct fw_xfer *)); 275 void fw_xferlist_remove (struct fw_xferlist *); 276 int fw_asyreq (struct firewire_comm *, int, struct fw_xfer *); 277 void fw_busreset (struct firewire_comm *, uint32_t); 278 uint16_t fw_crc16 (uint32_t *, uint32_t); 279 void fw_xfer_timeout (void *); 280 void fw_xfer_done (struct fw_xfer *); 281 void fw_xferwake (struct fw_xfer *); 282 int fw_xferwait (struct fw_xfer *); 283 void fw_asy_callback_free (struct fw_xfer *); 284 struct fw_device *fw_noderesolve_nodeid (struct firewire_comm *, int); 285 struct fw_device *fw_noderesolve_eui64 (struct firewire_comm *, struct fw_eui64 *); 286 struct fw_bind *fw_bindlookup (struct firewire_comm *, uint16_t, uint32_t); 287 void fw_drain_txq (struct firewire_comm *); 288 int fwdev_makedev (struct firewire_softc *); 289 int fwdev_destroydev (struct firewire_softc *); 290 void fwdev_clone (void *, struct ucred *, char *, int, struct cdev **); 291 int fw_open_isodma(struct firewire_comm *, int); 292 293 extern int firewire_debug; 294 extern devclass_t firewire_devclass; 295 extern int firewire_phydma_enable; 296 297 #define FWPRI (PWAIT | PCATCH) 298 299 #define CALLOUT_INIT(x) callout_init(x, 1 /* mpsafe */) 300 301 MALLOC_DECLARE(M_FW); 302 MALLOC_DECLARE(M_FWXFER); 303