1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 1998, 2001 Nicolas Souchu 5 * 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 /* 31 * I2C bus IP driver 32 */ 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/mbuf.h> 37 #include <sys/socket.h> 38 #include <sys/filio.h> 39 #include <sys/sockio.h> 40 #include <sys/kernel.h> 41 #include <sys/lock.h> 42 #include <sys/module.h> 43 #include <sys/mutex.h> 44 #include <sys/bus.h> 45 #include <sys/time.h> 46 #include <sys/malloc.h> 47 48 #include <net/if.h> 49 #include <net/if_var.h> 50 #include <net/if_types.h> 51 #include <net/netisr.h> 52 53 #include <net/route.h> 54 #include <netinet/in.h> 55 #include <netinet/in_systm.h> 56 #include <netinet/in_var.h> 57 #include <netinet/ip.h> 58 #include <netinet/if_ether.h> 59 60 #include <net/bpf.h> 61 62 #include <dev/iicbus/iiconf.h> 63 #include <dev/iicbus/iicbus.h> 64 65 #include "iicbus_if.h" 66 67 #define PCF_MASTER_ADDRESS 0xaa 68 69 #define ICHDRLEN sizeof(u_int32_t) 70 #define ICMTU 1500 /* default mtu */ 71 72 struct ic_softc { 73 if_t ic_ifp; 74 device_t ic_dev; 75 76 u_char ic_addr; /* peer I2C address */ 77 78 int ic_flags; 79 80 char *ic_obuf; 81 char *ic_ifbuf; 82 char *ic_cp; 83 84 int ic_xfercnt; 85 86 int ic_iferrs; 87 88 struct mtx ic_lock; 89 }; 90 91 #define IC_SENDING 0x0001 92 #define IC_OBUF_BUSY 0x0002 93 #define IC_IFBUF_BUSY 0x0004 94 #define IC_BUFFERS_BUSY (IC_OBUF_BUSY | IC_IFBUF_BUSY) 95 #define IC_BUFFER_WAITER 0x0004 96 97 static int icprobe(device_t); 98 static int icattach(device_t); 99 100 static int icioctl(if_t, u_long, caddr_t); 101 static int icoutput(if_t, struct mbuf *, const struct sockaddr *, 102 struct route *); 103 104 static int icintr(device_t, int, char *); 105 106 static device_method_t ic_methods[] = { 107 /* device interface */ 108 DEVMETHOD(device_probe, icprobe), 109 DEVMETHOD(device_attach, icattach), 110 111 /* iicbus interface */ 112 DEVMETHOD(iicbus_intr, icintr), 113 114 { 0, 0 } 115 }; 116 117 static driver_t ic_driver = { 118 "ic", 119 ic_methods, 120 sizeof(struct ic_softc), 121 }; 122 123 static void 124 ic_alloc_buffers(struct ic_softc *sc, int mtu) 125 { 126 char *obuf, *ifbuf; 127 128 obuf = malloc(mtu + ICHDRLEN, M_DEVBUF, M_WAITOK); 129 ifbuf = malloc(mtu + ICHDRLEN, M_DEVBUF, M_WAITOK); 130 131 mtx_lock(&sc->ic_lock); 132 while (sc->ic_flags & IC_BUFFERS_BUSY) { 133 sc->ic_flags |= IC_BUFFER_WAITER; 134 mtx_sleep(sc, &sc->ic_lock, 0, "icalloc", 0); 135 sc->ic_flags &= ~IC_BUFFER_WAITER; 136 } 137 138 free(sc->ic_obuf, M_DEVBUF); 139 free(sc->ic_ifbuf, M_DEVBUF); 140 sc->ic_obuf = obuf; 141 sc->ic_ifbuf = ifbuf; 142 if_setmtu(sc->ic_ifp, mtu); 143 mtx_unlock(&sc->ic_lock); 144 } 145 146 /* 147 * icprobe() 148 */ 149 static int 150 icprobe(device_t dev) 151 { 152 return (BUS_PROBE_NOWILDCARD); 153 } 154 155 /* 156 * icattach() 157 */ 158 static int 159 icattach(device_t dev) 160 { 161 struct ic_softc *sc = (struct ic_softc *)device_get_softc(dev); 162 if_t ifp; 163 164 ifp = sc->ic_ifp = if_alloc(IFT_PARA); 165 166 mtx_init(&sc->ic_lock, device_get_nameunit(dev), MTX_NETWORK_LOCK, 167 MTX_DEF); 168 sc->ic_addr = PCF_MASTER_ADDRESS; /* XXX only PCF masters */ 169 sc->ic_dev = dev; 170 171 if_setsoftc(ifp, sc); 172 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 173 if_setflags(ifp, IFF_SIMPLEX | IFF_POINTOPOINT | IFF_MULTICAST); 174 if_setioctlfn(ifp, icioctl); 175 if_setoutputfn(ifp, icoutput); 176 if_setifheaderlen(ifp, 0); 177 if_setsendqlen(ifp, ifqmaxlen); 178 179 ic_alloc_buffers(sc, ICMTU); 180 181 if_attach(ifp); 182 183 bpfattach(ifp, DLT_NULL, ICHDRLEN); 184 185 return (0); 186 } 187 188 /* 189 * iciotcl() 190 */ 191 static int 192 icioctl(if_t ifp, u_long cmd, caddr_t data) 193 { 194 struct ic_softc *sc = if_getsoftc(ifp); 195 device_t icdev = sc->ic_dev; 196 device_t parent = device_get_parent(icdev); 197 struct ifaddr *ifa = (struct ifaddr *)data; 198 struct ifreq *ifr = (struct ifreq *)data; 199 int error; 200 201 switch (cmd) { 202 203 case SIOCAIFADDR: 204 case SIOCSIFADDR: 205 if (ifa->ifa_addr->sa_family != AF_INET) 206 return (EAFNOSUPPORT); 207 mtx_lock(&sc->ic_lock); 208 if_setflagbits(ifp, IFF_UP, 0); 209 goto locked; 210 case SIOCSIFFLAGS: 211 mtx_lock(&sc->ic_lock); 212 locked: 213 if ((!(if_getflags(ifp) & IFF_UP)) && 214 (if_getdrvflags(ifp) & IFF_DRV_RUNNING)) { 215 216 /* XXX disable PCF */ 217 if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING); 218 mtx_unlock(&sc->ic_lock); 219 220 /* IFF_UP is not set, try to release the bus anyway */ 221 iicbus_release_bus(parent, icdev); 222 break; 223 } 224 if (((if_getflags(ifp) & IFF_UP)) && 225 (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))) { 226 mtx_unlock(&sc->ic_lock); 227 if ((error = iicbus_request_bus(parent, icdev, 228 IIC_WAIT | IIC_INTR))) 229 return (error); 230 mtx_lock(&sc->ic_lock); 231 iicbus_reset(parent, IIC_FASTEST, 0, NULL); 232 if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0); 233 } 234 mtx_unlock(&sc->ic_lock); 235 break; 236 237 case SIOCSIFMTU: 238 ic_alloc_buffers(sc, ifr->ifr_mtu); 239 break; 240 241 case SIOCGIFMTU: 242 mtx_lock(&sc->ic_lock); 243 ifr->ifr_mtu = if_getmtu(sc->ic_ifp); 244 mtx_unlock(&sc->ic_lock); 245 break; 246 247 case SIOCADDMULTI: 248 case SIOCDELMULTI: 249 if (ifr == NULL) 250 return (EAFNOSUPPORT); /* XXX */ 251 switch (ifr->ifr_addr.sa_family) { 252 case AF_INET: 253 break; 254 default: 255 return (EAFNOSUPPORT); 256 } 257 break; 258 default: 259 return (EINVAL); 260 } 261 return (0); 262 } 263 264 /* 265 * icintr() 266 */ 267 static int 268 icintr(device_t dev, int event, char *ptr) 269 { 270 struct ic_softc *sc = (struct ic_softc *)device_get_softc(dev); 271 struct mbuf *top; 272 int len; 273 274 mtx_lock(&sc->ic_lock); 275 276 switch (event) { 277 278 case INTR_GENERAL: 279 case INTR_START: 280 sc->ic_cp = sc->ic_ifbuf; 281 sc->ic_xfercnt = 0; 282 sc->ic_flags |= IC_IFBUF_BUSY; 283 break; 284 285 case INTR_STOP: 286 287 /* if any error occurred during transfert, 288 * drop the packet */ 289 sc->ic_flags &= ~IC_IFBUF_BUSY; 290 if ((sc->ic_flags & (IC_BUFFERS_BUSY | IC_BUFFER_WAITER)) == 291 IC_BUFFER_WAITER) 292 wakeup(&sc); 293 if (sc->ic_iferrs) 294 goto err; 295 if ((len = sc->ic_xfercnt) == 0) 296 break; /* ignore */ 297 if (len <= ICHDRLEN) 298 goto err; 299 len -= ICHDRLEN; 300 if_inc_counter(sc->ic_ifp, IFCOUNTER_IPACKETS, 1); 301 if_inc_counter(sc->ic_ifp, IFCOUNTER_IBYTES, len); 302 BPF_TAP(sc->ic_ifp, sc->ic_ifbuf, len + ICHDRLEN); 303 top = m_devget(sc->ic_ifbuf + ICHDRLEN, len, 0, sc->ic_ifp, 0); 304 if (top) { 305 struct epoch_tracker et; 306 307 mtx_unlock(&sc->ic_lock); 308 M_SETFIB(top, if_getfib(sc->ic_ifp)); 309 NET_EPOCH_ENTER(et); 310 netisr_dispatch(NETISR_IP, top); 311 NET_EPOCH_EXIT(et); 312 mtx_lock(&sc->ic_lock); 313 } 314 break; 315 err: 316 if_printf(sc->ic_ifp, "errors (%d)!\n", sc->ic_iferrs); 317 sc->ic_iferrs = 0; /* reset error count */ 318 if_inc_counter(sc->ic_ifp, IFCOUNTER_IERRORS, 1); 319 break; 320 321 case INTR_RECEIVE: 322 if (sc->ic_xfercnt >= if_getmtu(sc->ic_ifp) + ICHDRLEN) { 323 sc->ic_iferrs++; 324 } else { 325 *sc->ic_cp++ = *ptr; 326 sc->ic_xfercnt++; 327 } 328 break; 329 330 case INTR_NOACK: /* xfer terminated by master */ 331 break; 332 333 case INTR_TRANSMIT: 334 *ptr = 0xff; /* XXX */ 335 break; 336 337 case INTR_ERROR: 338 sc->ic_iferrs++; 339 break; 340 341 default: 342 panic("%s: unknown event (%d)!", __func__, event); 343 } 344 345 mtx_unlock(&sc->ic_lock); 346 return (0); 347 } 348 349 /* 350 * icoutput() 351 */ 352 static int 353 icoutput(if_t ifp, struct mbuf *m, const struct sockaddr *dst, 354 struct route *ro) 355 { 356 struct ic_softc *sc = if_getsoftc(ifp); 357 device_t icdev = sc->ic_dev; 358 device_t parent = device_get_parent(icdev); 359 int len, sent; 360 struct mbuf *mm; 361 u_char *cp; 362 u_int32_t hdr; 363 364 /* BPF writes need to be handled specially. */ 365 if (dst->sa_family == AF_UNSPEC || dst->sa_family == pseudo_AF_HDRCMPLT) 366 bcopy(dst->sa_data, &hdr, sizeof(hdr)); 367 else 368 hdr = RO_GET_FAMILY(ro, dst); 369 370 mtx_lock(&sc->ic_lock); 371 if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0); 372 373 /* already sending? */ 374 if (sc->ic_flags & IC_SENDING) { 375 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 376 goto error; 377 } 378 379 /* insert header */ 380 bcopy ((char *)&hdr, sc->ic_obuf, ICHDRLEN); 381 382 cp = sc->ic_obuf + ICHDRLEN; 383 len = 0; 384 mm = m; 385 do { 386 if (len + mm->m_len > if_getmtu(sc->ic_ifp)) { 387 /* packet too large */ 388 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 389 goto error; 390 } 391 392 bcopy(mtod(mm,char *), cp, mm->m_len); 393 cp += mm->m_len; 394 len += mm->m_len; 395 396 } while ((mm = mm->m_next)); 397 398 BPF_MTAP2(ifp, &hdr, sizeof(hdr), m); 399 400 sc->ic_flags |= (IC_SENDING | IC_OBUF_BUSY); 401 402 m_freem(m); 403 mtx_unlock(&sc->ic_lock); 404 405 /* send the packet */ 406 if (iicbus_block_write(parent, sc->ic_addr, sc->ic_obuf, 407 len + ICHDRLEN, &sent)) 408 409 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 410 else { 411 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 412 if_inc_counter(ifp, IFCOUNTER_OBYTES, len); 413 } 414 415 mtx_lock(&sc->ic_lock); 416 sc->ic_flags &= ~(IC_SENDING | IC_OBUF_BUSY); 417 if ((sc->ic_flags & (IC_BUFFERS_BUSY | IC_BUFFER_WAITER)) == 418 IC_BUFFER_WAITER) 419 wakeup(&sc); 420 mtx_unlock(&sc->ic_lock); 421 422 return (0); 423 424 error: 425 m_freem(m); 426 mtx_unlock(&sc->ic_lock); 427 428 return(0); 429 } 430 431 DRIVER_MODULE(ic, iicbus, ic_driver, 0, 0); 432 MODULE_DEPEND(ic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER); 433 MODULE_VERSION(ic, 1); 434