1139749Simp/*- 2113584Ssimokawa * Copyright (c) 2003 Hidetoshi Shimokawa 3103285Sikob * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa 4103285Sikob * All rights reserved. 5103285Sikob * 6103285Sikob * Redistribution and use in source and binary forms, with or without 7103285Sikob * modification, are permitted provided that the following conditions 8103285Sikob * are met: 9103285Sikob * 1. Redistributions of source code must retain the above copyright 10103285Sikob * notice, this list of conditions and the following disclaimer. 11103285Sikob * 2. Redistributions in binary form must reproduce the above copyright 12103285Sikob * notice, this list of conditions and the following disclaimer in the 13103285Sikob * documentation and/or other materials provided with the distribution. 14103285Sikob * 3. All advertising materials mentioning features or use of this software 15103285Sikob * must display the acknowledgement as bellow: 16103285Sikob * 17103285Sikob * This product includes software developed by K. Kobayashi and H. Shimokawa 18103285Sikob * 19103285Sikob * 4. The name of the author may not be used to endorse or promote products 20103285Sikob * derived from this software without specific prior written permission. 21103285Sikob * 22103285Sikob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23103285Sikob * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24103285Sikob * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25103285Sikob * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26103285Sikob * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27103285Sikob * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28103285Sikob * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29103285Sikob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30103285Sikob * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 31103285Sikob * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32103285Sikob * POSSIBILITY OF SUCH DAMAGE. 33272214Skan * 34103285Sikob * $FreeBSD$ 35103285Sikob * 36103285Sikob */ 37103285Sikob 38103285Sikobtypedef struct thread fw_proc; 39103285Sikob#include <sys/selinfo.h> 40103285Sikob 41113584Ssimokawa#include <sys/uio.h> 42170374Ssimokawa#include <sys/mutex.h> 43170374Ssimokawa#include <sys/taskqueue.h> 44113584Ssimokawa 45103285Sikob#define splfw splimp 46103285Sikob 47169130SsimokawaSTAILQ_HEAD(fw_xferlist, fw_xfer); 48169130Ssimokawa 49272214Skanstruct fw_device { 50129585Sdfr uint16_t dst; 51103285Sikob struct fw_eui64 eui; 52129585Sdfr uint8_t speed; 53129585Sdfr uint8_t maxrec; 54129585Sdfr uint8_t nport; 55129585Sdfr uint8_t power; 56103285Sikob#define CSRROMOFF 0x400 57103285Sikob#define CSRROMSIZE 0x400 58103285Sikob int rommax; /* offset from 0xffff f000 0000 */ 59272214Skan uint32_t csrrom[CSRROMSIZE / 4]; 60103285Sikob int rcnt; 61106810Ssimokawa struct firewire_comm *fc; 62129585Sdfr uint32_t status; 63103285Sikob#define FWDEVINIT 1 64103285Sikob#define FWDEVATTACHED 2 65103285Sikob#define FWDEVINVAL 3 66110193Ssimokawa STAILQ_ENTRY(fw_device) link; 67103285Sikob}; 68109645Ssimokawa 69103285Sikobstruct firewire_softc { 70130585Sphk struct cdev *dev; 71103285Sikob struct firewire_comm *fc; 72103285Sikob}; 73109645Ssimokawa 74103285Sikob#define FW_MAX_DMACH 0x20 75103285Sikob#define FW_MAX_DEVCH FW_MAX_DMACH 76103285Sikob#define FW_XFERTIMEOUT 1 77109645Ssimokawa 78103285Sikobstruct firewire_dev_comm { 79103285Sikob device_t dev; 80103285Sikob struct firewire_comm *fc; 81124169Ssimokawa void (*post_busreset) (void *); 82124169Ssimokawa void (*post_explore) (void *); 83103285Sikob}; 84103285Sikob 85103285Sikobstruct tcode_info { 86103285Sikob u_char hdr_len; /* IEEE1394 header length */ 87103285Sikob u_char flag; 88103285Sikob#define FWTI_REQ (1 << 0) 89103285Sikob#define FWTI_RES (1 << 1) 90103285Sikob#define FWTI_TLABEL (1 << 2) 91103285Sikob#define FWTI_BLOCK_STR (1 << 3) 92103285Sikob#define FWTI_BLOCK_ASY (1 << 4) 93170374Ssimokawa u_char valid_res; 94103285Sikob}; 95103285Sikob 96272214Skanstruct firewire_comm { 97103285Sikob device_t dev; 98103285Sikob device_t bdev; 99129585Sdfr uint16_t busid:10, 100272214Skan nodeid:6; 101103285Sikob u_int mode; 102103285Sikob u_int nport; 103103285Sikob u_int speed; 104103285Sikob u_int maxrec; 105103285Sikob u_int irm; 106103285Sikob u_int max_node; 107103285Sikob u_int max_hop; 108103285Sikob#define FWPHYASYST (1 << 0) 109129585Sdfr uint32_t status; 110169806Ssimokawa#define FWBUSDETACH (-2) 111116978Ssimokawa#define FWBUSNOTREADY (-1) 112103285Sikob#define FWBUSRESET 0 113103285Sikob#define FWBUSINIT 1 114103285Sikob#define FWBUSCYMELECT 2 115103285Sikob#define FWBUSMGRELECT 3 116103285Sikob#define FWBUSMGRDONE 4 117103285Sikob#define FWBUSEXPLORE 5 118103285Sikob#define FWBUSPHYCONF 6 119103285Sikob#define FWBUSEXPDONE 7 120103285Sikob#define FWBUSCOMPLETION 10 121103285Sikob int nisodma; 122109814Ssimokawa struct fw_eui64 eui; 123103285Sikob struct fw_xferq 124103285Sikob *arq, *atq, *ars, *ats, *it[FW_MAX_DMACH],*ir[FW_MAX_DMACH]; 125169130Ssimokawa struct fw_xferlist tlabels[0x40]; 126171457Ssimokawa u_char last_tlabel[0x40]; 127171513Ssimokawa struct mtx tlabel_lock; 128103285Sikob STAILQ_HEAD(, fw_bind) binds; 129110193Ssimokawa STAILQ_HEAD(, fw_device) devices; 130103285Sikob u_int sid_cnt; 131103285Sikob#define CSRSIZE 0x4000 132272214Skan uint32_t csr_arc[CSRSIZE / 4]; 133103285Sikob#define CROMSIZE 0x400 134129585Sdfr uint32_t *config_rom; 135116376Ssimokawa struct crom_src_buf *crom_src_buf; 136116376Ssimokawa struct crom_src *crom_src; 137116376Ssimokawa struct crom_chunk *crom_root; 138103285Sikob struct fw_topology_map *topology_map; 139103285Sikob struct fw_speed_map *speed_map; 140108853Ssimokawa struct callout busprobe_callout; 141110193Ssimokawa struct callout bmr_callout; 142110193Ssimokawa struct callout timeout_callout; 143170374Ssimokawa struct task task_timeout; 144272214Skan uint32_t (*cyctimer) (struct firewire_comm *); 145124169Ssimokawa void (*ibr) (struct firewire_comm *); 146129585Sdfr uint32_t (*set_bmr) (struct firewire_comm *, uint32_t); 147130585Sphk int (*ioctl) (struct cdev *, u_long, caddr_t, int, fw_proc *); 148124169Ssimokawa int (*irx_enable) (struct firewire_comm *, int); 149124169Ssimokawa int (*irx_disable) (struct firewire_comm *, int); 150124169Ssimokawa int (*itx_enable) (struct firewire_comm *, int); 151124169Ssimokawa int (*itx_disable) (struct firewire_comm *, int); 152124169Ssimokawa void (*timeout) (void *); 153124169Ssimokawa void (*poll) (struct firewire_comm *, int, int); 154124169Ssimokawa void (*set_intr) (struct firewire_comm *, int); 155129585Sdfr void (*irx_post) (struct firewire_comm *, uint32_t *); 156129585Sdfr void (*itx_post) (struct firewire_comm *, uint32_t *); 157103285Sikob struct tcode_info *tcode; 158113584Ssimokawa bus_dma_tag_t dmat; 159170374Ssimokawa struct mtx mtx; 160170374Ssimokawa struct mtx wait_lock; 161170374Ssimokawa struct taskqueue *taskqueue; 162170374Ssimokawa struct proc *probe_thread; 163103285Sikob}; 164272214Skan#define CSRARC(sc, offset) ((sc)->csr_arc[(offset) / 4]) 165103285Sikob 166170374Ssimokawa#define FW_GMTX(fc) (&(fc)->mtx) 167170374Ssimokawa#define FW_GLOCK(fc) mtx_lock(FW_GMTX(fc)) 168170374Ssimokawa#define FW_GUNLOCK(fc) mtx_unlock(FW_GMTX(fc)) 169170374Ssimokawa#define FW_GLOCK_ASSERT(fc) mtx_assert(FW_GMTX(fc), MA_OWNED) 170170374Ssimokawa 171109645Ssimokawastruct fw_xferq { 172109645Ssimokawa int flag; 173109645Ssimokawa#define FWXFERQ_CHTAGMASK 0xff 174109645Ssimokawa#define FWXFERQ_RUNNING (1 << 8) 175109645Ssimokawa#define FWXFERQ_STREAM (1 << 9) 176109645Ssimokawa 177109645Ssimokawa#define FWXFERQ_BULK (1 << 11) 178109645Ssimokawa#define FWXFERQ_MODEMASK (7 << 10) 179109645Ssimokawa 180109645Ssimokawa#define FWXFERQ_EXTBUF (1 << 13) 181109645Ssimokawa#define FWXFERQ_OPEN (1 << 14) 182109645Ssimokawa 183109645Ssimokawa#define FWXFERQ_HANDLER (1 << 16) 184109645Ssimokawa#define FWXFERQ_WAKEUP (1 << 17) 185272214Skan void (*start) (struct firewire_comm *); 186118293Ssimokawa int dmach; 187169130Ssimokawa struct fw_xferlist q; 188109645Ssimokawa u_int queued; 189109645Ssimokawa u_int maxq; 190109645Ssimokawa u_int psize; 191113584Ssimokawa struct fwdma_alloc_multi *buf; 192109645Ssimokawa u_int bnchunk; 193109645Ssimokawa u_int bnpacket; 194109645Ssimokawa struct fw_bulkxfer *bulkxfer; 195109645Ssimokawa STAILQ_HEAD(, fw_bulkxfer) stvalid; 196109645Ssimokawa STAILQ_HEAD(, fw_bulkxfer) stfree; 197109890Ssimokawa STAILQ_HEAD(, fw_bulkxfer) stdma; 198109645Ssimokawa struct fw_bulkxfer *stproc; 199109645Ssimokawa struct selinfo rsel; 200109645Ssimokawa caddr_t sc; 201124169Ssimokawa void (*hand) (struct fw_xferq *); 202109645Ssimokawa}; 203109645Ssimokawa 204272214Skanstruct fw_bulkxfer { 205113584Ssimokawa int poffset; 206111942Ssimokawa struct mbuf *mbuf; 207109645Ssimokawa STAILQ_ENTRY(fw_bulkxfer) link; 208109645Ssimokawa caddr_t start; 209109645Ssimokawa caddr_t end; 210111942Ssimokawa int resp; 211109645Ssimokawa}; 212109645Ssimokawa 213272214Skanstruct fw_bind { 214120660Ssimokawa u_int64_t start; 215120660Ssimokawa u_int64_t end; 216169130Ssimokawa struct fw_xferlist xferlist; 217109645Ssimokawa STAILQ_ENTRY(fw_bind) fclist; 218109645Ssimokawa STAILQ_ENTRY(fw_bind) chlist; 219169130Ssimokawa void *sc; 220109645Ssimokawa}; 221109645Ssimokawa 222272214Skanstruct fw_xfer { 223103285Sikob caddr_t sc; 224103285Sikob struct firewire_comm *fc; 225103285Sikob struct fw_xferq *q; 226110577Ssimokawa struct timeval tv; 227113584Ssimokawa int8_t resp; 228170374Ssimokawa#define FWXF_INIT 0x00 229170374Ssimokawa#define FWXF_INQ 0x01 230170374Ssimokawa#define FWXF_START 0x02 231170374Ssimokawa#define FWXF_SENT 0x04 232170374Ssimokawa#define FWXF_SENTERR 0x08 233170374Ssimokawa#define FWXF_BUSY 0x10 234170374Ssimokawa#define FWXF_RCVD 0x20 235170374Ssimokawa 236170374Ssimokawa#define FWXF_WAKE 0x80 237170374Ssimokawa uint8_t flag; 238169119Ssimokawa int8_t tl; 239167632Ssimokawa void (*hand) (struct fw_xfer *); 240103285Sikob struct { 241120660Ssimokawa struct fw_pkt hdr; 242129585Sdfr uint32_t *payload; 243129585Sdfr uint16_t pay_len; 244129585Sdfr uint8_t spd; 245103285Sikob } send, recv; 246103285Sikob struct mbuf *mbuf; 247103285Sikob STAILQ_ENTRY(fw_xfer) link; 248169119Ssimokawa STAILQ_ENTRY(fw_xfer) tlabel; 249110269Ssimokawa struct malloc_type *malloc; 250103285Sikob}; 251120660Ssimokawa 252120660Ssimokawastruct fw_rcv_buf { 253120660Ssimokawa struct firewire_comm *fc; 254120660Ssimokawa struct fw_xfer *xfer; 255120660Ssimokawa struct iovec *vec; 256120660Ssimokawa u_int nvec; 257129585Sdfr uint8_t spd; 258120660Ssimokawa}; 259120660Ssimokawa 260129585Sdfrvoid fw_sidrcv (struct firewire_comm *, uint32_t *, u_int); 261124169Ssimokawavoid fw_rcv (struct fw_rcv_buf *); 262272214Skanvoid fw_xfer_unload (struct fw_xfer *); 263272214Skanvoid fw_xfer_free_buf (struct fw_xfer *); 264272214Skanvoid fw_xfer_free (struct fw_xfer*); 265124169Ssimokawastruct fw_xfer *fw_xfer_alloc (struct malloc_type *); 266124169Ssimokawastruct fw_xfer *fw_xfer_alloc_buf (struct malloc_type *, int, int); 267124169Ssimokawavoid fw_init (struct firewire_comm *); 268124169Ssimokawaint fw_tbuf_update (struct firewire_comm *, int, int); 269124169Ssimokawaint fw_rbuf_update (struct firewire_comm *, int, int); 270124169Ssimokawaint fw_bindadd (struct firewire_comm *, struct fw_bind *); 271124169Ssimokawaint fw_bindremove (struct firewire_comm *, struct fw_bind *); 272169130Ssimokawaint fw_xferlist_add (struct fw_xferlist *, struct malloc_type *, int, int, int, 273169130Ssimokawa struct firewire_comm *, void *, void (*)(struct fw_xfer *)); 274169130Ssimokawavoid fw_xferlist_remove (struct fw_xferlist *); 275272214Skanint fw_asyreq (struct firewire_comm *, int, struct fw_xfer *); 276169117Ssimokawavoid fw_busreset (struct firewire_comm *, uint32_t); 277129585Sdfruint16_t fw_crc16 (uint32_t *, uint32_t); 278124169Ssimokawavoid fw_xfer_timeout (void *); 279124169Ssimokawavoid fw_xfer_done (struct fw_xfer *); 280170374Ssimokawavoid fw_xferwake (struct fw_xfer *); 281170374Ssimokawaint fw_xferwait (struct fw_xfer *); 282124169Ssimokawavoid fw_asy_callback_free (struct fw_xfer *); 283124169Ssimokawastruct fw_device *fw_noderesolve_nodeid (struct firewire_comm *, int); 284124169Ssimokawastruct fw_device *fw_noderesolve_eui64 (struct firewire_comm *, struct fw_eui64 *); 285129585Sdfrstruct fw_bind *fw_bindlookup (struct firewire_comm *, uint16_t, uint32_t); 286124169Ssimokawavoid fw_drain_txq (struct firewire_comm *); 287124169Ssimokawaint fwdev_makedev (struct firewire_softc *); 288124169Ssimokawaint fwdev_destroydev (struct firewire_softc *); 289148868Srwatsonvoid fwdev_clone (void *, struct ucred *, char *, int, struct cdev **); 290170374Ssimokawaint fw_open_isodma(struct firewire_comm *, int); 291103285Sikob 292103285Sikobextern int firewire_debug; 293103285Sikobextern devclass_t firewire_devclass; 294170400Ssimokawaextern int firewire_phydma_enable; 295103285Sikob 296272214Skan#define FWPRI ((PZERO + 8) | PCATCH) 297103285Sikob 298170374Ssimokawa#define CALLOUT_INIT(x) callout_init(x, 1 /* mpsafe */) 299111615Ssimokawa 300110195SsimokawaMALLOC_DECLARE(M_FW); 301110269SsimokawaMALLOC_DECLARE(M_FWXFER); 302