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 STAILQ_HEAD(fw_xferlist, fw_xfer); 46 47 struct fw_device { 48 uint16_t dst; 49 struct fw_eui64 eui; 50 uint8_t speed; 51 uint8_t maxrec; 52 uint8_t nport; 53 uint8_t power; 54 #define CSRROMOFF 0x400 55 #define CSRROMSIZE 0x400 56 int rommax; /* offset from 0xffff f000 0000 */ 57 uint32_t csrrom[CSRROMSIZE / 4]; 58 int rcnt; 59 int rom_changed; 60 struct firewire_comm *fc; 61 uint32_t status; 62 #define FWDEVINIT 1 63 #define FWDEVATTACHED 2 64 #define FWDEVINVAL 3 65 STAILQ_HEAD(, fw_unit) units; 66 STAILQ_ENTRY(fw_device) link; 67 }; 68 69 struct fw_unit { 70 device_t dev; 71 struct fw_device *fwdev; 72 uint32_t spec_id; 73 uint32_t sw_version; 74 uint32_t dir_offset; 75 STAILQ_ENTRY(fw_unit) link; 76 }; 77 78 enum fw_child_type { 79 FW_CHILD_BUS, 80 FW_CHILD_UNIT, 81 }; 82 83 struct fw_child_ivars { 84 enum fw_child_type type; 85 union { 86 struct firewire_comm *fc; 87 struct fw_unit *unit; 88 } u; 89 }; 90 91 struct firewire_softc { 92 struct cdev *dev; 93 struct firewire_comm *fc; 94 int watchdog_clock; 95 }; 96 97 #define FW_MAX_DMACH 0x20 98 #define FW_MAX_DEVCH FW_MAX_DMACH 99 #define FW_XFERTIMEOUT 1 100 101 struct firewire_dev_comm { 102 device_t dev; 103 struct firewire_comm *fc; 104 void (*post_busreset) (void *); 105 void (*post_explore) (void *); 106 }; 107 108 struct tcode_info { 109 u_char hdr_len; /* IEEE1394 header length */ 110 u_char flag; 111 #define FWTI_REQ (1 << 0) 112 #define FWTI_RES (1 << 1) 113 #define FWTI_TLABEL (1 << 2) 114 #define FWTI_BLOCK_STR (1 << 3) 115 #define FWTI_BLOCK_ASY (1 << 4) 116 u_char valid_res; 117 }; 118 119 struct firewire_comm { 120 device_t dev; 121 device_t bdev; 122 uint16_t busid:10, 123 nodeid:6; 124 u_int mode; 125 u_int nport; 126 u_int speed; 127 u_int maxrec; 128 u_int irm; 129 u_int max_node; 130 u_int max_hop; 131 #define FWPHYASYST (1 << 0) 132 uint32_t status; 133 #define FWBUSDETACH (-2) 134 #define FWBUSNOTREADY (-1) 135 #define FWBUSRESET 0 136 #define FWBUSINIT 1 137 #define FWBUSCYMELECT 2 138 #define FWBUSMGRELECT 3 139 #define FWBUSMGRDONE 4 140 #define FWBUSEXPLORE 5 141 #define FWBUSPHYCONF 6 142 #define FWBUSEXPDONE 7 143 #define FWBUSCOMPLETION 10 144 int nisodma; 145 struct fw_eui64 eui; 146 struct fw_xferq 147 *arq, *atq, *ars, *ats, *it[FW_MAX_DMACH],*ir[FW_MAX_DMACH]; 148 struct fw_xferlist tlabels[0x40]; 149 u_char last_tlabel[0x40]; 150 struct mtx tlabel_lock; 151 STAILQ_HEAD(, fw_bind) binds; 152 STAILQ_HEAD(, fw_device) devices; 153 u_int sid_cnt; 154 #define CSRSIZE 0x4000 155 uint32_t csr_arc[CSRSIZE / 4]; 156 #define CROMSIZE 0x400 157 uint32_t *config_rom; 158 struct crom_src_buf *crom_src_buf; 159 struct crom_src *crom_src; 160 struct crom_chunk *crom_root; 161 struct fw_topology_map *topology_map; 162 struct fw_speed_map *speed_map; 163 struct callout busprobe_callout; 164 struct callout bmr_callout; 165 struct callout timeout_callout; 166 struct task task_timeout; 167 uint32_t (*cyctimer) (struct firewire_comm *); 168 void (*ibr) (struct firewire_comm *); 169 uint32_t (*set_bmr) (struct firewire_comm *, uint32_t); 170 int (*ioctl) (struct cdev *, u_long, caddr_t, int, fw_proc *); 171 int (*irx_enable) (struct firewire_comm *, int); 172 int (*irx_disable) (struct firewire_comm *, int); 173 int (*itx_enable) (struct firewire_comm *, int); 174 int (*itx_disable) (struct firewire_comm *, int); 175 void (*timeout) (void *); 176 void (*poll) (struct firewire_comm *, int, int); 177 void (*set_intr) (struct firewire_comm *, int); 178 void (*irx_post) (struct firewire_comm *, uint32_t *); 179 void (*itx_post) (struct firewire_comm *, uint32_t *); 180 struct tcode_info *tcode; 181 bus_dma_tag_t dmat; 182 struct mtx mtx; 183 struct mtx wait_lock; 184 struct taskqueue *taskqueue; 185 struct proc *probe_thread; 186 }; 187 #define CSRARC(sc, offset) ((sc)->csr_arc[(offset) / 4]) 188 189 #define FW_GMTX(fc) (&(fc)->mtx) 190 #define FW_GLOCK(fc) mtx_lock(FW_GMTX(fc)) 191 #define FW_GUNLOCK(fc) mtx_unlock(FW_GMTX(fc)) 192 #define FW_GLOCK_ASSERT(fc) mtx_assert(FW_GMTX(fc), MA_OWNED) 193 194 struct fw_xferq { 195 int flag; 196 #define FWXFERQ_CHTAGMASK 0xff 197 #define FWXFERQ_RUNNING (1 << 8) 198 #define FWXFERQ_STREAM (1 << 9) 199 200 #define FWXFERQ_BULK (1 << 11) 201 #define FWXFERQ_MODEMASK (7 << 10) 202 203 #define FWXFERQ_EXTBUF (1 << 13) 204 #define FWXFERQ_OPEN (1 << 14) 205 206 #define FWXFERQ_HANDLER (1 << 16) 207 #define FWXFERQ_WAKEUP (1 << 17) 208 void (*start) (struct firewire_comm *); 209 int dmach; 210 struct fw_xferlist q; 211 u_int queued; 212 u_int maxq; 213 u_int psize; 214 struct fwdma_alloc_multi *buf; 215 u_int bnchunk; 216 u_int bnpacket; 217 struct fw_bulkxfer *bulkxfer; 218 STAILQ_HEAD(, fw_bulkxfer) stvalid; 219 STAILQ_HEAD(, fw_bulkxfer) stfree; 220 STAILQ_HEAD(, fw_bulkxfer) stdma; 221 struct fw_bulkxfer *stproc; 222 struct selinfo rsel; 223 caddr_t sc; 224 void (*hand) (struct fw_xferq *); 225 }; 226 227 struct fw_bulkxfer { 228 int poffset; 229 struct mbuf *mbuf; 230 STAILQ_ENTRY(fw_bulkxfer) link; 231 caddr_t start; 232 caddr_t end; 233 int resp; 234 }; 235 236 struct fw_bind { 237 u_int64_t start; 238 u_int64_t end; 239 struct fw_xferlist xferlist; 240 STAILQ_ENTRY(fw_bind) fclist; 241 STAILQ_ENTRY(fw_bind) chlist; 242 void *sc; 243 }; 244 245 struct fw_xfer { 246 caddr_t sc; 247 struct firewire_comm *fc; 248 struct fw_xferq *q; 249 struct timeval tv; 250 int8_t resp; 251 #define FWXF_INIT 0x00 252 #define FWXF_INQ 0x01 253 #define FWXF_START 0x02 254 #define FWXF_SENT 0x04 255 #define FWXF_SENTERR 0x08 256 #define FWXF_BUSY 0x10 257 #define FWXF_RCVD 0x20 258 259 #define FWXF_WAKE 0x80 260 uint8_t flag; 261 int8_t tl; 262 void (*hand) (struct fw_xfer *); 263 struct { 264 struct fw_pkt hdr; 265 uint32_t *payload; 266 uint16_t pay_len; 267 uint8_t spd; 268 } send, recv; 269 struct mbuf *mbuf; 270 STAILQ_ENTRY(fw_xfer) link; 271 STAILQ_ENTRY(fw_xfer) tlabel; 272 struct malloc_type *malloc; 273 }; 274 275 struct fw_rcv_buf { 276 struct firewire_comm *fc; 277 struct fw_xfer *xfer; 278 struct iovec *vec; 279 u_int nvec; 280 uint8_t spd; 281 }; 282 283 void fw_sidrcv (struct firewire_comm *, uint32_t *, u_int); 284 void fw_rcv (struct fw_rcv_buf *); 285 void fw_xfer_unload (struct fw_xfer *); 286 void fw_xfer_free_buf (struct fw_xfer *); 287 void fw_xfer_free (struct fw_xfer*); 288 struct fw_xfer *fw_xfer_alloc (struct malloc_type *); 289 struct fw_xfer *fw_xfer_alloc_buf (struct malloc_type *, int, int); 290 void fw_init (struct firewire_comm *); 291 int fw_tbuf_update (struct firewire_comm *, int, int); 292 int fw_rbuf_update (struct firewire_comm *, int, int); 293 int fw_bindadd (struct firewire_comm *, struct fw_bind *); 294 int fw_bindremove (struct firewire_comm *, struct fw_bind *); 295 int fw_xferlist_add (struct fw_xferlist *, struct malloc_type *, int, int, int, 296 struct firewire_comm *, void *, void (*)(struct fw_xfer *)); 297 void fw_xferlist_remove (struct fw_xferlist *); 298 int fw_asyreq (struct firewire_comm *, int, struct fw_xfer *); 299 void fw_busreset (struct firewire_comm *, uint32_t); 300 uint16_t fw_crc16 (uint32_t *, uint32_t); 301 void fw_xfer_timeout (void *); 302 void fw_xfer_done (struct fw_xfer *); 303 void fw_xferwake (struct fw_xfer *); 304 int fw_xferwait (struct fw_xfer *); 305 void fw_asy_callback_free (struct fw_xfer *); 306 struct fw_device *fw_noderesolve_nodeid (struct firewire_comm *, int); 307 struct fw_device *fw_noderesolve_eui64 (struct firewire_comm *, struct fw_eui64 *); 308 struct fw_bind *fw_bindlookup (struct firewire_comm *, uint16_t, uint32_t); 309 void fw_drain_txq (struct firewire_comm *); 310 int fwdev_makedev (struct firewire_softc *); 311 int fwdev_destroydev (struct firewire_softc *); 312 void fwdev_clone (void *, struct ucred *, char *, int, struct cdev **); 313 int fw_open_isodma(struct firewire_comm *, int); 314 315 extern int firewire_debug; 316 extern devclass_t firewire_devclass; 317 extern int firewire_phydma_enable; 318 319 static __inline struct fw_child_ivars * 320 fw_get_ivars(device_t dev) 321 { 322 323 return ((struct fw_child_ivars *)device_get_ivars(dev)); 324 } 325 326 static __inline struct firewire_comm * 327 fw_get_comm(device_t dev) 328 { 329 struct fw_child_ivars *iv; 330 331 iv = fw_get_ivars(dev); 332 if (iv->type == FW_CHILD_BUS) 333 return (iv->u.fc); 334 return (iv->u.unit->fwdev->fc); 335 } 336 337 static __inline struct fw_unit * 338 fw_get_unit(device_t dev) 339 { 340 struct fw_child_ivars *iv; 341 342 iv = fw_get_ivars(dev); 343 if (iv->type == FW_CHILD_UNIT) 344 return (iv->u.unit); 345 return (NULL); 346 } 347 348 #define FWPRI (PWAIT | PCATCH) 349 350 #define CALLOUT_INIT(x) callout_init(x, 1 /* mpsafe */) 351 352 MALLOC_DECLARE(M_FW); 353 MALLOC_DECLARE(M_FWXFER); 354