Home | History | Annotate | Line # | Download | only in pci
xmm7360.c revision 1.1
      1 // vim: noet ts=8 sts=8 sw=8
      2 /*
      3  * Device driver for Intel XMM7360 LTE modems, eg. Fibocom L850-GL.
      4  * Written by James Wah
      5  * james (at) laird-wah.net
      6  *
      7  * Development of this driver was supported by genua GmbH
      8  *
      9 * Copyright (c) 2020 genua GmbH <info (at) genua.de>
     10 *Copyright (c) 2020 James Wah <james (at) laird-wah.net>
     11  *Copyright (c) 2020 Jaromir Dolecek <jdolecek (at) NetBSD.org>
     12  *
     13  * OpenBSD port written by Jaromir Dolecek for genua GmbH
     14  *
     15  * Permission to use, copy, modify, and/or distribute this software for any
     16  * purpose with or without fee is hereby granted, provided that the above
     17  * copyright notice and this permission notice appear in all copies.
     18  *
     19  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     20  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES ON
     21  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     22  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGE
     23  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     24  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     25  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     26  */
     27 
     28 #ifdef __linux__
     29 
     30 #include <linux/init.h>
     31 #include <linux/interrupt.h>
     32 #include <linux/kernel.h>
     33 #include <linux/module.h>
     34 #include <linux/pci.h>
     35 #include <linux/delay.h>
     36 #include <linux/uaccess.h>
     37 #include <linux/cdev.h>
     38 #include <linux/wait.h>
     39 #include <linux/tty.h>
     40 #include <linux/tty_flip.h>
     41 #include <linux/poll.h>
     42 #include <linux/skbuff.h>
     43 #include <linux/netdevice.h>
     44 #include <linux/if.h>
     45 #include <linux/if_arp.h>
     46 #include <net/rtnetlink.h>
     47 #include <linux/hrtimer.h>
     48 #include <linux/workqueue.h>
     49 
     50 MODULE_LICENSE("Dual BSD/GPL");
     51 
     52 static const struct pci_device_id xmm7360_ids[] = {
     53 	{ PCI_DEVICE(0x8086, 0x7360), },
     54 	{ 0, }
     55 };
     56 MODULE_DEVICE_TABLE(pci, xmm7360_ids);
     57 
     58 /* Actually this ioctl not used for xmm0/rpc device by python code */
     59 #define XMM7360_IOCTL_GET_PAGE_SIZE _IOC(_IOC_READ, 'x', 0xc0, sizeof(u32))
     60 
     61 #define xmm7360_os_msleep(msec)		msleep(msec)
     62 
     63 #define __unused			/* nothing */
     64 
     65 #endif
     66 
     67 #if defined(__OpenBSD__) || defined(__NetBSD__)
     68 
     69 #ifdef __OpenBSD__
     70 #include "bpfilter.h"
     71 #endif
     72 #ifdef __NetBSD__
     73 #include "opt_inet.h"
     74 #include "opt_gateway.h"
     75 
     76 #include <sys/cdefs.h>
     77 __KERNEL_RCSID(0, "$NetBSD: xmm7360.c,v 1.1 2020/07/26 14:51:18 jdolecek Exp $");
     78 #endif
     79 
     80 #include <sys/param.h>
     81 #include <sys/systm.h>
     82 #include <sys/sockio.h>
     83 #include <sys/mbuf.h>
     84 #include <sys/kernel.h>
     85 #include <sys/device.h>
     86 #include <sys/socket.h>
     87 #include <sys/mutex.h>
     88 #include <sys/tty.h>
     89 #include <sys/conf.h>
     90 #include <sys/kthread.h>
     91 #include <sys/poll.h>
     92 #include <sys/fcntl.h>		/* for FREAD/FWRITE */
     93 #include <sys/vnode.h>
     94 #include <uvm/uvm_param.h>
     95 
     96 #include <dev/pci/pcireg.h>
     97 #include <dev/pci/pcivar.h>
     98 #include <dev/pci/pcidevs.h>
     99 
    100 #include <net/if.h>
    101 #include <net/if_types.h>
    102 
    103 #include <netinet/in.h>
    104 #include <netinet/ip.h>
    105 #include <netinet/ip6.h>
    106 
    107 #ifdef __OpenBSD__
    108 #include <netinet/if_ether.h>
    109 #include <sys/timeout.h>
    110 #include <machine/bus.h>
    111 #endif
    112 
    113 #if NBPFILTER > 0 || defined(__NetBSD__)
    114 #include <net/bpf.h>
    115 #endif
    116 
    117 #ifdef __NetBSD__
    118 #include "ioconf.h"
    119 #include <sys/cpu.h>
    120 #endif
    121 
    122 #ifdef INET
    123 #include <netinet/in_var.h>
    124 #endif
    125 #ifdef INET6
    126 #include <netinet6/in6_var.h>
    127 #endif
    128 
    129 typedef uint8_t u8;
    130 typedef uint16_t u16;
    131 typedef uint32_t u32;
    132 typedef bus_addr_t dma_addr_t;
    133 typedef void * wait_queue_head_t;	/* just address for tsleep() */
    134 
    135 #define WWAN_BAR0	PCI_MAPREG_START
    136 #define WWAN_BAR1	(PCI_MAPREG_START + 4)
    137 #define WWAN_BAR2	(PCI_MAPREG_START + 8)
    138 
    139 #define BUG_ON(never_true)	KASSERT(!(never_true))
    140 #define WARN_ON(x)		/* nothing */
    141 
    142 #ifdef __OpenBSD__
    143 typedef struct mutex spinlock_t;
    144 #define dev_err(devp, fmt, ...)		\
    145 	printf("%s: " fmt, (devp)->dv_xname, ##__VA_ARGS__)
    146 #define dev_info(devp, fmt, ...)	\
    147 	printf("%s: " fmt, (devp)->dv_xname, ##__VA_ARGS__)
    148 #define	kzalloc(size, flags)	malloc(size, M_DEVBUF, M_WAITOK | M_ZERO)
    149 #define kfree(addr)		free(addr, M_DEVBUF, 0)
    150 #define mutex_init(lock)	mtx_init(lock, IPL_TTY)
    151 #define mutex_lock(lock)	mtx_enter(lock)
    152 #define mutex_unlock(lock)	mtx_leave(lock)
    153 /* In OpenBSD every mutex is spin mutex, and it must not be held on sleep */
    154 #define spin_lock_irqsave(lock, flags)		mtx_enter(lock)
    155 #define spin_unlock_irqrestore(lock, flags)	mtx_leave(lock)
    156 
    157 /* Compat defines for NetBSD API */
    158 #define curlwp			curproc
    159 #define LINESW(tp)				(linesw[(tp)->t_line])
    160 #define selnotify(sel, band, note)		selwakeup(sel)
    161 #define cfdata_t				void *
    162 #define device_lookup_private(cdp, unit)	\
    163 	(unit < (*cdp).cd_ndevs) ? (*cdp).cd_devs[unit] : NULL
    164 #define IFQ_SET_READY(ifq)			/* nothing */
    165 #define device_private(devt)			(void *)devt;
    166 #define if_deferred_start_init(ifp, arg)	/* nothing */
    167 #define IF_OUTPUT_CONST				/* nothing */
    168 #define tty_lock()				int s = spltty()
    169 #define tty_unlock()				splx(s)
    170 #define tty_locked()				/* nothing */
    171 #define pmf_device_deregister(dev)		/* nothing */
    172 #if NBPFILTER > 0
    173 #define BPF_MTAP_OUT(ifp, m)						\
    174                 if (ifp->if_bpf) {					\
    175                         bpf_mtap_af(ifp->if_bpf, m->m_pkthdr.ph_family,	\
    176 			    m, BPF_DIRECTION_OUT);			\
    177 		}
    178 #else
    179 #define BPF_MTAP_OUT(ifp, m)			/* nothing */
    180 #endif
    181 
    182 /* Copied from NetBSD <lib/libkern/libkern.h> */
    183 #define __validate_container_of(PTR, TYPE, FIELD)			\
    184     (0 * sizeof((PTR) - &((TYPE *)(((char *)(PTR)) -			\
    185     offsetof(TYPE, FIELD)))->FIELD))
    186 #define	container_of(PTR, TYPE, FIELD)					\
    187     ((TYPE *)(((char *)(PTR)) - offsetof(TYPE, FIELD))			\
    188 	+ __validate_container_of(PTR, TYPE, FIELD))
    189 
    190 /* Copied from NetBSD <sys/cdefs.h> */
    191 #define __UNVOLATILE(a)		((void *)(unsigned long)(volatile void *)(a))
    192 
    193 #if OpenBSD <= 201911
    194 /* Backward compat with OpenBSD 6.6 */
    195 #define klist_insert(klist, kn)		\
    196 		SLIST_INSERT_HEAD(klist, kn, kn_selnext)
    197 #define klist_remove(klist, kn)		\
    198 		SLIST_REMOVE(klist, kn, knote, kn_selnext)
    199 #define XMM_KQ_ISFD_INITIALIZER		.f_isfd = 1
    200 #else
    201 #define XMM_KQ_ISFD_INITIALIZER		.f_flags = FILTEROP_ISFD
    202 #endif /* OpenBSD <= 201911 */
    203 
    204 #endif
    205 
    206 #ifdef __NetBSD__
    207 typedef struct kmutex spinlock_t;
    208 #define dev_err			aprint_error_dev
    209 #define dev_info		aprint_normal_dev
    210 #define mutex			kmutex
    211 #define kzalloc(size, flags)	malloc(size, M_DEVBUF, M_WAITOK | M_ZERO)
    212 #define kfree(addr)		free(addr, M_DEVBUF)
    213 #define mutex_init(lock)	mutex_init(lock, MUTEX_DEFAULT, IPL_TTY)
    214 #define mutex_lock(lock)	mutex_enter(lock)
    215 #define mutex_unlock(lock)	mutex_exit(lock)
    216 #define spin_lock_irqsave(lock, flags)	mutex_enter(lock)
    217 #define spin_unlock_irqrestore(lock, flags)	mutex_exit(lock)
    218 
    219 /* Compat defines with OpenBSD API */
    220 #define caddr_t			void *
    221 #define proc			lwp
    222 #define LINESW(tp)		(*tp->t_linesw)
    223 #define ttymalloc(speed)	tty_alloc()
    224 #define ttyfree(tp)		tty_free(tp)
    225 #define l_open(dev, tp, p)	l_open(dev, tp)
    226 #define l_close(tp, flag, p)	l_close(tp, flag)
    227 #define ttkqfilter(dev, kn)	ttykqfilter(dev, kn)
    228 #define msleep(ident, lock, prio, wmesg, timo) \
    229 		mtsleep(ident, prio, wmesg, timo, lock)
    230 #define pci_mapreg_map(pa, reg, type, busfl, tp, hp, bp, szp, maxsize) \
    231 	pci_mapreg_map(pa, reg, type, busfl, tp, hp, bp, szp)
    232 #define pci_intr_establish(pc, ih, lvl, func, arg, name) \
    233 	pci_intr_establish_xname(pc, ih, lvl, func, arg, name)
    234 #define suser(l)					\
    235 	kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)
    236 #define kthread_create(func, arg, lwpp, name)		\
    237 	kthread_create(0, 0, NULL, func, arg, lwpp, "%s", name)
    238 #define MUTEX_ASSERT_LOCKED(lock)	KASSERT(mutex_owned(lock))
    239 #define MCLGETI(m, how, m0, sz)		MCLGET(m, how)
    240 #define m_copyback(m, off, sz, buf, how)		\
    241 					m_copyback(m, off, sz, buf)
    242 #define ifq_deq_begin(ifq)		({		\
    243 		struct mbuf *m0;			\
    244 		IFQ_DEQUEUE(ifq, m0);			\
    245 		m0;					\
    246 })
    247 #define ifq_deq_rollback(ifq, m)	m_freem(m)
    248 #define ifq_deq_commit(ifq, m)		/* nothing to do */
    249 #define ifq_is_oactive(ifq)		true	/* always restart queue */
    250 #define ifq_clr_oactive(ifq)		/* nothing to do */
    251 #define ifq_empty(ifq)			IFQ_IS_EMPTY(ifq)
    252 #define ifq_purge(ifq)			IF_PURGE(ifq)
    253 #define if_enqueue(ifp, m)		ifq_enqueue(ifp, m)
    254 #define if_ih_insert(ifp, func, arg)	(ifp)->_if_input = (func)
    255 #define if_ih_remove(ifp, func, arg)	/* nothing to do */
    256 #define if_hardmtu			if_mtu
    257 #define IF_OUTPUT_CONST			const
    258 #define si_note				sel_klist
    259 #define klist_insert(klist, kn)		\
    260 		SLIST_INSERT_HEAD(klist, kn, kn_selnext)
    261 #define klist_remove(klist, kn)		\
    262 		SLIST_REMOVE(klist, kn, knote, kn_selnext)
    263 #define XMM_KQ_ISFD_INITIALIZER		.f_isfd = 1
    264 #define tty_lock()			mutex_spin_enter(&tty_lock)
    265 #define tty_unlock()			mutex_spin_exit(&tty_lock)
    266 #define tty_locked()			KASSERT(mutex_owned(&tty_lock))
    267 #define bpfattach(bpf, ifp, dlt, sz)	bpf_attach(ifp, dlt, sz)
    268 #define NBPFILTER			1
    269 #define BPF_MTAP_OUT(ifp, m)		bpf_mtap(ifp, m, BPF_D_OUT)
    270 #endif /* __NetBSD__ */
    271 
    272 #define __user				/* nothing */
    273 #define copy_from_user(kbuf, userbuf, sz)		\
    274 ({							\
    275 	int __ret = 0;					\
    276 	int error = copyin(userbuf, kbuf, sz);		\
    277 	if (error != 0)					\
    278 		return -error;				\
    279 	__ret;						\
    280 })
    281 #define copy_to_user(kbuf, userbuf, sz)			\
    282 ({							\
    283 	int __ret = 0;					\
    284 	int error = copyout(userbuf, kbuf, sz);		\
    285 	if (error != 0)					\
    286 		return -error;				\
    287 	__ret;						\
    288 })
    289 #define xmm7360_os_msleep(msec)				\
    290 		KASSERT(!cold);				\
    291 		tsleep(xmm, 0, "wwancsl", msec * hz / 1000)
    292 
    293 static void *dma_alloc_coherent(struct device *, size_t, dma_addr_t *, int);
    294 static void dma_free_coherent(struct device *, size_t, volatile void *, dma_addr_t);
    295 
    296 #ifndef PCI_PRODUCT_INTEL_XMM7360
    297 #define PCI_PRODUCT_INTEL_XMM7360	0x7360
    298 #endif
    299 
    300 #define init_waitqueue_head(wqp)	*(wqp) = (wqp)
    301 #define wait_event_interruptible(wq, cond)				\
    302 ({									\
    303 	int __ret = 1;							\
    304 	while (!(cond)) {						\
    305 		KASSERT(!cold);						\
    306 		int error = tsleep(wq, PCATCH, "xmmwq", 0);		\
    307 		if (error) {						\
    308 			__ret = (cond) ? 1				\
    309 			    : ((error != ERESTART) ? -error : error);	\
    310 			break;						\
    311 		}							\
    312 	}								\
    313 	__ret;								\
    314 })
    315 
    316 #define msecs_to_jiffies(msec)						\
    317 ({									\
    318 	KASSERT(hz < 1000);						\
    319 	KASSERT(msec > (1000 / hz));					\
    320 	msec * hz / 1000;						\
    321 })
    322 
    323 #define wait_event_interruptible_timeout(wq, cond, jiffies)		\
    324 ({									\
    325 	int __ret = 1;							\
    326 	while (!(cond)) {						\
    327 		if (cold) {						\
    328 			for (int loop = 0; loop < 10; loop++) {		\
    329 				delay(jiffies * 1000 * 1000 / hz / 10);	\
    330 				if (cond)				\
    331 					break;				\
    332 			}						\
    333 			__ret = (cond) ? 1 : 0;				\
    334 			break;						\
    335 		}							\
    336 		int error = tsleep(wq, PCATCH, "xmmwq", jiffies);	\
    337 		if (error) {						\
    338 			__ret = (cond) ? 1				\
    339 			    : ((error != ERESTART) ? -error : error);	\
    340 			break;						\
    341 		}							\
    342 	}								\
    343 	__ret;								\
    344 })
    345 
    346 #define GFP_KERNEL			0
    347 
    348 #endif /* __OpenBSD__ || __NetBSD__ */
    349 
    350 /*
    351  * The XMM7360 communicates via DMA ring buffers. It has one
    352  * command ring, plus sixteen transfer descriptor (TD)
    353  * rings. The command ring is mainly used to configure and
    354  * deconfigure the TD rings.
    355  *
    356  * The 16 TD rings form 8 queue pairs (QP). For example, QP
    357  * 0 uses ring 0 for host->device, and ring 1 for
    358  * device->host.
    359  *
    360  * The known queue pair functions are as follows:
    361  *
    362  * 0:	Mux (Raw IP packets, amongst others)
    363  * 1:	RPC (funky command protocol based in part on ASN.1 BER)
    364  * 2:	AT trace? port; does not accept commands after init
    365  * 4:	AT command port
    366  * 7:	AT command port
    367  *
    368  */
    369 
    370 /* Command ring, which is used to configure the queue pairs */
    371 struct cmd_ring_entry {
    372 	dma_addr_t ptr;
    373 	u16 len;
    374 	u8 parm;
    375 	u8 cmd;
    376 	u32 extra;
    377 	u32 unk, flags;
    378 };
    379 
    380 #define CMD_RING_OPEN	1
    381 #define CMD_RING_CLOSE	2
    382 #define CMD_RING_FLUSH	3
    383 #define CMD_WAKEUP	4
    384 
    385 #define CMD_FLAG_DONE	1
    386 #define CMD_FLAG_READY	2
    387 
    388 /* Transfer descriptors used on the Tx and Rx rings of each queue pair */
    389 struct td_ring_entry {
    390 	dma_addr_t addr;
    391 	u16 length;
    392 	u16 flags;
    393 	u32 unk;
    394 };
    395 
    396 #define TD_FLAG_COMPLETE 0x200
    397 
    398 /* Root configuration object. This contains pointers to all of the control
    399  * structures that the modem will interact with.
    400  */
    401 struct control {
    402 	dma_addr_t status;
    403 	dma_addr_t s_wptr, s_rptr;
    404 	dma_addr_t c_wptr, c_rptr;
    405 	dma_addr_t c_ring;
    406 	u16 c_ring_size;
    407 	u16 unk;
    408 };
    409 
    410 struct status {
    411 	u32 code;
    412 	u32 mode;
    413 	u32 asleep;
    414 	u32 pad;
    415 };
    416 
    417 #define CMD_RING_SIZE 0x80
    418 
    419 /* All of the control structures can be packed into one page of RAM. */
    420 struct control_page {
    421 	struct control ctl;
    422 	// Status words - written by modem.
    423 	volatile struct status status;
    424 	// Slave ring write/read pointers.
    425 	volatile u32 s_wptr[16], s_rptr[16];
    426 	// Command ring write/read pointers.
    427 	volatile u32 c_wptr, c_rptr;
    428 	// Command ring entries.
    429 	volatile struct cmd_ring_entry c_ring[CMD_RING_SIZE];
    430 };
    431 
    432 #define BAR0_MODE	0x0c
    433 #define BAR0_DOORBELL	0x04
    434 #define BAR0_WAKEUP	0x14
    435 
    436 #define DOORBELL_TD	0
    437 #define DOORBELL_CMD	1
    438 
    439 #define BAR2_STATUS	0x00
    440 #define BAR2_MODE	0x18
    441 #define BAR2_CONTROL	0x19
    442 #define BAR2_CONTROLH	0x1a
    443 
    444 #define BAR2_BLANK0	0x1b
    445 #define BAR2_BLANK1	0x1c
    446 #define BAR2_BLANK2	0x1d
    447 #define BAR2_BLANK3	0x1e
    448 
    449 #define XMM_MODEM_BOOTING	0xfeedb007
    450 #define XMM_MODEM_READY		0x600df00d
    451 
    452 #define XMM_TAG_ACBH		0x41434248	// 'ACBH'
    453 #define XMM_TAG_CMDH		0x434d4448	// 'CMDH'
    454 #define XMM_TAG_ADBH		0x41444248	// 'ADBH'
    455 #define XMM_TAG_ADTH		0x41445448	// 'ADTH'
    456 
    457 /* There are 16 TD rings: a Tx and Rx ring for each queue pair */
    458 struct td_ring {
    459 	u8 depth;
    460 	u8 last_handled;
    461 	u16 page_size;
    462 
    463 	struct td_ring_entry *tds;
    464 	dma_addr_t tds_phys;
    465 
    466 	// One page of page_size per td
    467 	void **pages;
    468 	dma_addr_t *pages_phys;
    469 };
    470 
    471 #define TD_MAX_PAGE_SIZE 16384
    472 
    473 struct queue_pair {
    474 	struct xmm_dev *xmm;
    475 	u8 depth;
    476 	u16 page_size;
    477 	int tty_index;
    478 	int tty_needs_wake;
    479 	struct device dev;
    480 	int num;
    481 	int open;
    482 	struct mutex lock;
    483 	unsigned char user_buf[TD_MAX_PAGE_SIZE];
    484 	wait_queue_head_t wq;
    485 
    486 #ifdef __linux__
    487 	struct cdev cdev;
    488 	struct tty_port port;
    489 #endif
    490 #if defined(__OpenBSD__) || defined(__NetBSD__)
    491 	struct selinfo selr, selw;
    492 #endif
    493 };
    494 
    495 #define XMM_QP_COUNT	8
    496 
    497 struct xmm_dev {
    498 	struct device *dev;
    499 
    500 	volatile uint32_t *bar0, *bar2;
    501 
    502 	volatile struct control_page *cp;
    503 	dma_addr_t cp_phys;
    504 
    505 	struct td_ring td_ring[2 * XMM_QP_COUNT];
    506 
    507 	struct queue_pair qp[XMM_QP_COUNT];
    508 
    509 	struct xmm_net *net;
    510 	struct net_device *netdev;
    511 
    512 	int error;
    513 	int card_num;
    514 	int num_ttys;
    515 	wait_queue_head_t wq;
    516 
    517 #ifdef __linux__
    518 	struct pci_dev *pci_dev;
    519 
    520 	int irq;
    521 
    522 	struct work_struct init_work;	// XXX work not actually scheduled
    523 #endif
    524 };
    525 
    526 struct mux_bounds {
    527 	uint32_t offset;
    528 	uint32_t length;
    529 };
    530 
    531 struct mux_first_header {
    532 	uint32_t tag;
    533 	uint16_t unknown;
    534 	uint16_t sequence;
    535 	uint16_t length;
    536 	uint16_t extra;
    537 	uint16_t next;
    538 	uint16_t pad;
    539 };
    540 
    541 struct mux_next_header {
    542 	uint32_t tag;
    543 	uint16_t length;
    544 	uint16_t extra;
    545 	uint16_t next;
    546 	uint16_t pad;
    547 };
    548 
    549 #define MUX_MAX_PACKETS	64
    550 
    551 struct mux_frame {
    552 	int n_packets, n_bytes, max_size, sequence;
    553 	uint16_t *last_tag_length, *last_tag_next;
    554 	struct mux_bounds bounds[MUX_MAX_PACKETS];
    555 	uint8_t data[TD_MAX_PAGE_SIZE];
    556 };
    557 
    558 struct xmm_net {
    559 	struct xmm_dev *xmm;
    560 	struct queue_pair *qp;
    561 	int channel;
    562 
    563 #ifdef __linux__
    564 	struct sk_buff_head queue;
    565 	struct hrtimer deadline;
    566 #endif
    567 	int queued_packets, queued_bytes;
    568 
    569 	int sequence;
    570 	spinlock_t lock;
    571 	struct mux_frame frame;
    572 };
    573 
    574 static void xmm7360_os_handle_net_frame(struct xmm_dev *, const u8 *, size_t);
    575 static void xmm7360_os_handle_net_dequeue(struct xmm_net *, struct mux_frame *);
    576 static void xmm7360_os_handle_net_txwake(struct xmm_net *);
    577 static void xmm7360_os_handle_tty_idata(struct queue_pair *, const u8 *, size_t);
    578 
    579 static void xmm7360_poll(struct xmm_dev *xmm)
    580 {
    581 	if (xmm->cp->status.code == 0xbadc0ded) {
    582 		dev_err(xmm->dev, "crashed but dma up\n");
    583 		xmm->error = -ENODEV;
    584 	}
    585 	if (xmm->bar2[BAR2_STATUS] != XMM_MODEM_READY) {
    586 		dev_err(xmm->dev, "bad status %x\n",xmm->bar2[BAR2_STATUS]);
    587 		xmm->error = -ENODEV;
    588 	}
    589 }
    590 
    591 static void xmm7360_ding(struct xmm_dev *xmm, int bell)
    592 {
    593 	if (xmm->cp->status.asleep)
    594 		xmm->bar0[BAR0_WAKEUP] = 1;
    595 	xmm->bar0[BAR0_DOORBELL] = bell;
    596 	xmm7360_poll(xmm);
    597 }
    598 
    599 static int xmm7360_cmd_ring_wait(struct xmm_dev *xmm)
    600 {
    601 	// Wait for all commands to complete
    602 	// XXX locking?
    603 	int ret = wait_event_interruptible_timeout(xmm->wq, (xmm->cp->c_rptr == xmm->cp->c_wptr) || xmm->error, msecs_to_jiffies(1000));
    604 	if (ret == 0)
    605 		return -ETIMEDOUT;
    606 	if (ret < 0)
    607 		return ret;
    608 	return xmm->error;
    609 }
    610 
    611 static int xmm7360_cmd_ring_execute(struct xmm_dev *xmm, u8 cmd, u8 parm, u16 len, dma_addr_t ptr, u32 extra)
    612 {
    613 	u8 wptr = xmm->cp->c_wptr;
    614 	u8 new_wptr = (wptr + 1) % CMD_RING_SIZE;
    615 	if (xmm->error)
    616 		return xmm->error;
    617 	if (new_wptr == xmm->cp->c_rptr)	// ring full
    618 		return -EAGAIN;
    619 
    620 	xmm->cp->c_ring[wptr].ptr = ptr;
    621 	xmm->cp->c_ring[wptr].cmd = cmd;
    622 	xmm->cp->c_ring[wptr].parm = parm;
    623 	xmm->cp->c_ring[wptr].len = len;
    624 	xmm->cp->c_ring[wptr].extra = extra;
    625 	xmm->cp->c_ring[wptr].unk = 0;
    626 	xmm->cp->c_ring[wptr].flags = CMD_FLAG_READY;
    627 
    628 	xmm->cp->c_wptr = new_wptr;
    629 
    630 	xmm7360_ding(xmm, DOORBELL_CMD);
    631 	return xmm7360_cmd_ring_wait(xmm);
    632 }
    633 
    634 static int xmm7360_cmd_ring_init(struct xmm_dev *xmm) {
    635 	int timeout;
    636 	int ret;
    637 
    638 	xmm->cp = dma_alloc_coherent(xmm->dev, sizeof(struct control_page), &xmm->cp_phys, GFP_KERNEL);
    639 	BUG_ON(xmm->cp == NULL);
    640 
    641 	xmm->cp->ctl.status = xmm->cp_phys + offsetof(struct control_page, status);
    642 	xmm->cp->ctl.s_wptr = xmm->cp_phys + offsetof(struct control_page, s_wptr);
    643 	xmm->cp->ctl.s_rptr = xmm->cp_phys + offsetof(struct control_page, s_rptr);
    644 	xmm->cp->ctl.c_wptr = xmm->cp_phys + offsetof(struct control_page, c_wptr);
    645 	xmm->cp->ctl.c_rptr = xmm->cp_phys + offsetof(struct control_page, c_rptr);
    646 	xmm->cp->ctl.c_ring = xmm->cp_phys + offsetof(struct control_page, c_ring);
    647 	xmm->cp->ctl.c_ring_size = CMD_RING_SIZE;
    648 
    649 	xmm->bar2[BAR2_CONTROL] = xmm->cp_phys;
    650 	xmm->bar2[BAR2_CONTROLH] = xmm->cp_phys >> 32;
    651 
    652 	xmm->bar0[BAR0_MODE] = 1;
    653 
    654 	timeout = 100;
    655 	while (xmm->bar2[BAR2_MODE] == 0 && --timeout)
    656 		xmm7360_os_msleep(10);
    657 
    658 	if (!timeout)
    659 		return -ETIMEDOUT;
    660 
    661 	xmm->bar2[BAR2_BLANK0] = 0;
    662 	xmm->bar2[BAR2_BLANK1] = 0;
    663 	xmm->bar2[BAR2_BLANK2] = 0;
    664 	xmm->bar2[BAR2_BLANK3] = 0;
    665 
    666 	xmm->bar0[BAR0_MODE] = 2;	// enable intrs?
    667 
    668 	timeout = 100;
    669 	while (xmm->bar2[BAR2_MODE] != 2 && --timeout)
    670 		xmm7360_os_msleep(10);
    671 
    672 	if (!timeout)
    673 		return -ETIMEDOUT;
    674 
    675 	// enable going to sleep when idle
    676 	ret = xmm7360_cmd_ring_execute(xmm, CMD_WAKEUP, 0, 1, 0, 0);
    677 	if (ret)
    678 		return ret;
    679 
    680 	return 0;
    681 }
    682 
    683 static void xmm7360_cmd_ring_free(struct xmm_dev *xmm) {
    684 	if (xmm->bar0)
    685 		xmm->bar0[BAR0_MODE] = 0;
    686 	if (xmm->cp)
    687 		dma_free_coherent(xmm->dev, sizeof(struct control_page), (volatile void *)xmm->cp, xmm->cp_phys);
    688 	xmm->cp = NULL;
    689 	return;
    690 }
    691 
    692 static void xmm7360_td_ring_activate(struct xmm_dev *xmm, u8 ring_id)
    693 {
    694 	struct td_ring *ring = &xmm->td_ring[ring_id];
    695 	int ret;
    696 
    697 	xmm->cp->s_rptr[ring_id] = xmm->cp->s_wptr[ring_id] = 0;
    698 	ring->last_handled = 0;
    699 	ret = xmm7360_cmd_ring_execute(xmm, CMD_RING_OPEN, ring_id, ring->depth, ring->tds_phys, 0x60);
    700 	BUG_ON(ret);
    701 }
    702 
    703 static void xmm7360_td_ring_create(struct xmm_dev *xmm, u8 ring_id, u8 depth, u16 page_size)
    704 {
    705 	struct td_ring *ring = &xmm->td_ring[ring_id];
    706 	int i;
    707 
    708 	BUG_ON(ring->depth);
    709 	BUG_ON(depth & (depth-1));
    710 	BUG_ON(page_size > TD_MAX_PAGE_SIZE);
    711 
    712 	memset(ring, 0, sizeof(struct td_ring));
    713 	ring->depth = depth;
    714 	ring->page_size = page_size;
    715 	ring->tds = dma_alloc_coherent(xmm->dev, sizeof(struct td_ring_entry)*depth, &ring->tds_phys, GFP_KERNEL);
    716 
    717 	ring->pages = kzalloc(sizeof(void*)*depth, GFP_KERNEL);
    718 	ring->pages_phys = kzalloc(sizeof(dma_addr_t)*depth, GFP_KERNEL);
    719 
    720 	for (i=0; i<depth; i++) {
    721 		ring->pages[i] = dma_alloc_coherent(xmm->dev, ring->page_size, &ring->pages_phys[i], GFP_KERNEL);
    722 		ring->tds[i].addr = ring->pages_phys[i];
    723 	}
    724 
    725 	xmm7360_td_ring_activate(xmm, ring_id);
    726 }
    727 
    728 static void xmm7360_td_ring_deactivate(struct xmm_dev *xmm, u8 ring_id)
    729 {
    730 	xmm7360_cmd_ring_execute(xmm, CMD_RING_CLOSE, ring_id, 0, 0, 0);
    731 }
    732 
    733 static void xmm7360_td_ring_destroy(struct xmm_dev *xmm, u8 ring_id)
    734 {
    735 	struct td_ring *ring = &xmm->td_ring[ring_id];
    736 	int i, depth=ring->depth;
    737 
    738 	if (!depth) {
    739 		WARN_ON(1);
    740 		dev_err(xmm->dev, "Tried destroying empty ring!\n");
    741 		return;
    742 	}
    743 
    744 	xmm7360_td_ring_deactivate(xmm, ring_id);
    745 
    746 	for (i=0; i<depth; i++) {
    747 		dma_free_coherent(xmm->dev, ring->page_size, ring->pages[i], ring->pages_phys[i]);
    748 	}
    749 
    750 	kfree(ring->pages_phys);
    751 	kfree(ring->pages);
    752 
    753 	dma_free_coherent(xmm->dev, sizeof(struct td_ring_entry)*depth, ring->tds, ring->tds_phys);
    754 
    755 	ring->depth = 0;
    756 }
    757 
    758 static void xmm7360_td_ring_write(struct xmm_dev *xmm, u8 ring_id, const void *buf, int len)
    759 {
    760 	struct td_ring *ring = &xmm->td_ring[ring_id];
    761 	u8 wptr = xmm->cp->s_wptr[ring_id];
    762 
    763 	BUG_ON(!ring->depth);
    764 	BUG_ON(len > ring->page_size);
    765 	BUG_ON(ring_id & 1);
    766 
    767 	memcpy(ring->pages[wptr], buf, len);
    768 	ring->tds[wptr].length = len;
    769 	ring->tds[wptr].flags = 0;
    770 	ring->tds[wptr].unk = 0;
    771 
    772 	wptr = (wptr + 1) & (ring->depth - 1);
    773 	BUG_ON(wptr == xmm->cp->s_rptr[ring_id]);
    774 
    775 	xmm->cp->s_wptr[ring_id] = wptr;
    776 }
    777 
    778 static int xmm7360_td_ring_full(struct xmm_dev *xmm, u8 ring_id)
    779 {
    780 	struct td_ring *ring = &xmm->td_ring[ring_id];
    781 	u8 wptr = xmm->cp->s_wptr[ring_id];
    782 	wptr = (wptr + 1) & (ring->depth - 1);
    783 	return wptr == xmm->cp->s_rptr[ring_id];
    784 }
    785 
    786 static void xmm7360_td_ring_read(struct xmm_dev *xmm, u8 ring_id)
    787 {
    788 	struct td_ring *ring = &xmm->td_ring[ring_id];
    789 	u8 wptr = xmm->cp->s_wptr[ring_id];
    790 
    791 	if (!ring->depth) {
    792 		dev_err(xmm->dev, "read on disabled ring\n");
    793 		WARN_ON(1);
    794 		return;
    795 	}
    796 	if (!(ring_id & 1)) {
    797 		dev_err(xmm->dev, "read on write ring\n");
    798 		WARN_ON(1);
    799 		return;
    800 	}
    801 
    802 	ring->tds[wptr].length = ring->page_size;
    803 	ring->tds[wptr].flags = 0;
    804 	ring->tds[wptr].unk = 0;
    805 
    806 	wptr = (wptr + 1) & (ring->depth - 1);
    807 	BUG_ON(wptr == xmm->cp->s_rptr[ring_id]);
    808 
    809 	xmm->cp->s_wptr[ring_id] = wptr;
    810 }
    811 
    812 static struct queue_pair * xmm7360_init_qp(struct xmm_dev *xmm, int num, u8 depth, u16 page_size)
    813 {
    814 	struct queue_pair *qp = &xmm->qp[num];
    815 
    816 	qp->xmm = xmm;
    817 	qp->num = num;
    818 	qp->open = 0;
    819 	qp->depth = depth;
    820 	qp->page_size = page_size;
    821 
    822 	mutex_init(&qp->lock);
    823 	init_waitqueue_head(&qp->wq);
    824 	return qp;
    825 }
    826 
    827 static void xmm7360_qp_arm(struct xmm_dev *xmm, struct queue_pair *qp)
    828 {
    829 	while (!xmm7360_td_ring_full(xmm, qp->num*2+1))
    830 		xmm7360_td_ring_read(xmm, qp->num*2+1);
    831 	xmm7360_ding(xmm, DOORBELL_TD);
    832 }
    833 
    834 static int xmm7360_qp_start(struct queue_pair *qp)
    835 {
    836 	struct xmm_dev *xmm = qp->xmm;
    837 	int ret;
    838 
    839 	mutex_lock(&qp->lock);
    840 	if (qp->open) {
    841 		ret = -EBUSY;
    842 	} else {
    843 		ret = 0;
    844 		qp->open = 1;
    845 	}
    846 	mutex_unlock(&qp->lock);
    847 
    848 	if (ret == 0) {
    849 		xmm7360_td_ring_create(xmm, qp->num*2, qp->depth, qp->page_size);
    850 		xmm7360_td_ring_create(xmm, qp->num*2+1, qp->depth, qp->page_size);
    851 		xmm7360_qp_arm(xmm, qp);
    852 	}
    853 
    854 	return ret;
    855 }
    856 
    857 static void xmm7360_qp_resume(struct queue_pair *qp)
    858 {
    859 	struct xmm_dev *xmm = qp->xmm;
    860 
    861 	BUG_ON(!qp->open);
    862 	xmm7360_td_ring_activate(xmm, qp->num*2);
    863 	xmm7360_td_ring_activate(xmm, qp->num*2+1);
    864 	xmm7360_qp_arm(xmm, qp);
    865 }
    866 
    867 static int xmm7360_qp_stop(struct queue_pair *qp)
    868 {
    869 	struct xmm_dev *xmm = qp->xmm;
    870 	int ret = 0;
    871 
    872 	mutex_lock(&qp->lock);
    873 	if (!qp->open) {
    874 		ret = -ENODEV;
    875 	} else {
    876 		ret = 0;
    877 		/* still holding qp->open to prevent concurrent access */
    878 	}
    879 	mutex_unlock(&qp->lock);
    880 
    881 	if (ret == 0) {
    882 		xmm7360_td_ring_destroy(xmm, qp->num*2);
    883 		xmm7360_td_ring_destroy(xmm, qp->num*2+1);
    884 
    885 		mutex_lock(&qp->lock);
    886 		qp->open = 0;
    887 		mutex_unlock(&qp->lock);
    888 	}
    889 
    890 	return ret;
    891 }
    892 
    893 static void xmm7360_qp_suspend(struct queue_pair *qp)
    894 {
    895 	struct xmm_dev *xmm = qp->xmm;
    896 
    897 	BUG_ON(!qp->open);
    898 	xmm7360_td_ring_deactivate(xmm, qp->num*2);
    899 }
    900 
    901 static int xmm7360_qp_can_write(struct queue_pair *qp)
    902 {
    903 	struct xmm_dev *xmm = qp->xmm;
    904 	return !xmm7360_td_ring_full(xmm, qp->num*2);
    905 }
    906 
    907 static ssize_t xmm7360_qp_write(struct queue_pair *qp, const char *buf, size_t size)
    908 {
    909 	struct xmm_dev *xmm = qp->xmm;
    910 	int page_size = qp->xmm->td_ring[qp->num*2].page_size;
    911 	if (xmm->error)
    912 		return xmm->error;
    913 	if (!xmm7360_qp_can_write(qp))
    914 		return 0;
    915 	if (size > page_size)
    916 		size = page_size;
    917 	xmm7360_td_ring_write(xmm, qp->num*2, buf, size);
    918 	xmm7360_ding(xmm, DOORBELL_TD);
    919 	return size;
    920 }
    921 
    922 static ssize_t xmm7360_qp_write_user(struct queue_pair *qp, const char __user *buf, size_t size)
    923 {
    924 	int page_size = qp->xmm->td_ring[qp->num*2].page_size;
    925 	int ret;
    926 
    927 	if (size > page_size)
    928 		size = page_size;
    929 
    930 	ret = copy_from_user(qp->user_buf, buf, size);
    931 	size = size - ret;
    932 	if (!size)
    933 		return 0;
    934 	return xmm7360_qp_write(qp, qp->user_buf, size);
    935 }
    936 
    937 static int xmm7360_qp_has_data(struct queue_pair *qp)
    938 {
    939 	struct xmm_dev *xmm = qp->xmm;
    940 	struct td_ring *ring = &xmm->td_ring[qp->num*2+1];
    941 
    942 	return (xmm->cp->s_rptr[qp->num*2+1] != ring->last_handled);
    943 }
    944 
    945 static ssize_t xmm7360_qp_read_user(struct queue_pair *qp, char __user *buf, size_t size)
    946 {
    947 	struct xmm_dev *xmm = qp->xmm;
    948 	struct td_ring *ring = &xmm->td_ring[qp->num*2+1];
    949 	int idx, nread, ret;
    950 	// XXX locking?
    951 	ret = wait_event_interruptible(qp->wq, xmm7360_qp_has_data(qp) || xmm->error);
    952 	if (ret < 0)
    953 		return ret;
    954 	if (xmm->error)
    955 		return xmm->error;
    956 
    957 	idx = ring->last_handled;
    958 	nread = ring->tds[idx].length;
    959 	if (nread > size)
    960 		nread = size;
    961 	ret = copy_to_user(buf, ring->pages[idx], nread);
    962 	nread -= ret;
    963 	if (nread == 0)
    964 		return 0;
    965 
    966 	// XXX all data not fitting into buf+size is discarded
    967 	xmm7360_td_ring_read(xmm, qp->num*2+1);
    968 	xmm7360_ding(xmm, DOORBELL_TD);
    969 	ring->last_handled = (idx + 1) & (ring->depth - 1);
    970 
    971 	return nread;
    972 }
    973 
    974 static void xmm7360_tty_poll_qp(struct queue_pair *qp)
    975 {
    976 	struct xmm_dev *xmm = qp->xmm;
    977 	struct td_ring *ring = &xmm->td_ring[qp->num*2+1];
    978 	int idx, nread;
    979 	while (xmm7360_qp_has_data(qp)) {
    980 		idx = ring->last_handled;
    981 		nread = ring->tds[idx].length;
    982 		xmm7360_os_handle_tty_idata(qp, ring->pages[idx], nread);
    983 
    984 		xmm7360_td_ring_read(xmm, qp->num*2+1);
    985 		xmm7360_ding(xmm, DOORBELL_TD);
    986 		ring->last_handled = (idx + 1) & (ring->depth - 1);
    987 	}
    988 }
    989 
    990 #ifdef __linux__
    991 
    992 static void xmm7360_os_handle_tty_idata(struct queue_pair *qp, const u8 *data, size_t nread)
    993 {
    994 	tty_insert_flip_string(&qp->port, data, nread);
    995 	tty_flip_buffer_push(&qp->port);
    996 }
    997 
    998 int xmm7360_cdev_open (struct inode *inode, struct file *file)
    999 {
   1000 	struct queue_pair *qp = container_of(inode->i_cdev, struct queue_pair, cdev);
   1001 	file->private_data = qp;
   1002 	return xmm7360_qp_start(qp);
   1003 }
   1004 
   1005 int xmm7360_cdev_release (struct inode *inode, struct file *file)
   1006 {
   1007 	struct queue_pair *qp = file->private_data;
   1008 	return xmm7360_qp_stop(qp);
   1009 }
   1010 
   1011 ssize_t xmm7360_cdev_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
   1012 {
   1013 	struct queue_pair *qp = file->private_data;
   1014 	int ret;
   1015 
   1016 	ret = xmm7360_qp_write_user(qp, buf, size);
   1017 	if (ret < 0)
   1018 		return ret;
   1019 
   1020 	*offset += ret;
   1021 	return ret;
   1022 }
   1023 
   1024 ssize_t xmm7360_cdev_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
   1025 {
   1026 	struct queue_pair *qp = file->private_data;
   1027 	int ret;
   1028 
   1029 	ret = xmm7360_qp_read_user(qp, buf, size);
   1030 	if (ret < 0)
   1031 		return ret;
   1032 
   1033 	*offset += ret;
   1034 	return ret;
   1035 }
   1036 
   1037 static unsigned int xmm7360_cdev_poll(struct file *file, poll_table *wait)
   1038 {
   1039 	struct queue_pair *qp = file->private_data;
   1040 	unsigned int mask = 0;
   1041 
   1042 	poll_wait(file, &qp->wq, wait);
   1043 
   1044 	if (qp->xmm->error)
   1045 		return POLLHUP;
   1046 
   1047 	if (xmm7360_qp_has_data(qp))
   1048 		mask |= POLLIN | POLLRDNORM;
   1049 
   1050 	if (xmm7360_qp_can_write(qp))
   1051 		mask |= POLLOUT | POLLWRNORM;
   1052 
   1053 	return mask;
   1054 }
   1055 
   1056 static long xmm7360_cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
   1057 {
   1058 	struct queue_pair *qp = file->private_data;
   1059 
   1060 	u32 val;
   1061 
   1062 	switch (cmd) {
   1063 		case XMM7360_IOCTL_GET_PAGE_SIZE:
   1064 			val = qp->xmm->td_ring[qp->num*2].page_size;
   1065 			if (copy_to_user((u32*)arg, &val, sizeof(u32)))
   1066 				return -EFAULT;
   1067 			return 0;
   1068 	}
   1069 
   1070 	return -ENOTTY;
   1071 }
   1072 
   1073 static struct file_operations xmm7360_fops = {
   1074 	.read		= xmm7360_cdev_read,
   1075 	.write		= xmm7360_cdev_write,
   1076 	.poll		= xmm7360_cdev_poll,
   1077 	.unlocked_ioctl	= xmm7360_cdev_ioctl,
   1078 	.open		= xmm7360_cdev_open,
   1079 	.release	= xmm7360_cdev_release
   1080 };
   1081 
   1082 #endif /* __linux__ */
   1083 
   1084 static void xmm7360_mux_frame_init(struct xmm_net *xn, struct mux_frame *frame, int sequence)
   1085 {
   1086 	frame->sequence = xn->sequence;
   1087 	frame->max_size = xn->xmm->td_ring[0].page_size;
   1088 	frame->n_packets = 0;
   1089 	frame->n_bytes = 0;
   1090 	frame->last_tag_next = NULL;
   1091 	frame->last_tag_length = NULL;
   1092 }
   1093 
   1094 static void xmm7360_mux_frame_add_tag(struct mux_frame *frame, uint32_t tag, uint16_t extra, void *data, int data_len)
   1095 {
   1096 	int total_length;
   1097 	if (frame->n_bytes == 0)
   1098 		total_length = sizeof(struct mux_first_header) + data_len;
   1099 	else
   1100 		total_length = sizeof(struct mux_next_header) + data_len;
   1101 
   1102 	while (frame->n_bytes & 3)
   1103 		frame->n_bytes++;
   1104 
   1105 	BUG_ON(frame->n_bytes + total_length > frame->max_size);
   1106 
   1107 	if (frame->last_tag_next)
   1108 		*frame->last_tag_next = frame->n_bytes;
   1109 
   1110 	if (frame->n_bytes == 0) {
   1111 		struct mux_first_header *hdr = (struct mux_first_header *)frame->data;
   1112 		memset(hdr, 0, sizeof(struct mux_first_header));
   1113 		hdr->tag = htonl(tag);
   1114 		hdr->sequence = frame->sequence;
   1115 		hdr->length = total_length;
   1116 		hdr->extra = extra;
   1117 		frame->last_tag_length = &hdr->length;
   1118 		frame->last_tag_next = &hdr->next;
   1119 		frame->n_bytes += sizeof(struct mux_first_header);
   1120 	} else {
   1121 		struct mux_next_header *hdr = (struct mux_next_header *)(&frame->data[frame->n_bytes]);
   1122 		memset(hdr, 0, sizeof(struct mux_next_header));
   1123 		hdr->tag = htonl(tag);
   1124 		hdr->length = total_length;
   1125 		hdr->extra = extra;
   1126 		frame->last_tag_length = &hdr->length;
   1127 		frame->last_tag_next = &hdr->next;
   1128 		frame->n_bytes += sizeof(struct mux_next_header);
   1129 	}
   1130 
   1131 	if (data_len) {
   1132 		memcpy(&frame->data[frame->n_bytes], data, data_len);
   1133 		frame->n_bytes += data_len;
   1134 	}
   1135 }
   1136 
   1137 static void xmm7360_mux_frame_append_data(struct mux_frame *frame, const void *data, int data_len)
   1138 {
   1139 	BUG_ON(frame->n_bytes + data_len > frame->max_size);
   1140 	BUG_ON(!frame->last_tag_length);
   1141 
   1142 	memcpy(&frame->data[frame->n_bytes], data, data_len);
   1143 	*frame->last_tag_length += data_len;
   1144 	frame->n_bytes += data_len;
   1145 }
   1146 
   1147 static int xmm7360_mux_frame_append_packet(struct mux_frame *frame, const void *data, int data_len)
   1148 {
   1149 	int expected_adth_size = sizeof(struct mux_next_header) + 4 + (frame->n_packets+1)*sizeof(struct mux_bounds);
   1150 	uint8_t pad[16];
   1151 
   1152 	if (frame->n_packets >= MUX_MAX_PACKETS)
   1153 		return -1;
   1154 
   1155 	if (frame->n_bytes + data_len + 16 + expected_adth_size > frame->max_size)
   1156 		return -1;
   1157 
   1158 	BUG_ON(!frame->last_tag_length);
   1159 
   1160 	frame->bounds[frame->n_packets].offset = frame->n_bytes;
   1161 	frame->bounds[frame->n_packets].length = data_len + 16;
   1162 	frame->n_packets++;
   1163 
   1164 	memset(pad, 0, sizeof(pad));
   1165 	xmm7360_mux_frame_append_data(frame, pad, 16);
   1166 	xmm7360_mux_frame_append_data(frame, data, data_len);
   1167 	return 0;
   1168 }
   1169 
   1170 static int xmm7360_mux_frame_push(struct xmm_dev *xmm, struct mux_frame *frame)
   1171 {
   1172 	struct mux_first_header *hdr = (void*)&frame->data[0];
   1173 	int ret;
   1174 	hdr->length = frame->n_bytes;
   1175 
   1176 	ret = xmm7360_qp_write(xmm->net->qp, frame->data, frame->n_bytes);
   1177 	if (ret < 0)
   1178 		return ret;
   1179 	return 0;
   1180 }
   1181 
   1182 static int xmm7360_mux_control(struct xmm_net *xn, u32 arg1, u32 arg2, u32 arg3, u32 arg4)
   1183 {
   1184 	struct mux_frame *frame = &xn->frame;
   1185 	int ret;
   1186 	uint32_t cmdh_args[] = {arg1, arg2, arg3, arg4};
   1187 	unsigned long flags __unused;
   1188 
   1189 	spin_lock_irqsave(&xn->lock, flags);
   1190 
   1191 	xmm7360_mux_frame_init(xn, frame, 0);
   1192 	xmm7360_mux_frame_add_tag(frame, XMM_TAG_ACBH, 0, NULL, 0);
   1193 	xmm7360_mux_frame_add_tag(frame, XMM_TAG_CMDH, xn->channel, cmdh_args, sizeof(cmdh_args));
   1194 	ret = xmm7360_mux_frame_push(xn->xmm, frame);
   1195 
   1196 	spin_unlock_irqrestore(&xn->lock, flags);
   1197 
   1198 	return ret;
   1199 }
   1200 
   1201 static void xmm7360_net_flush(struct xmm_net *xn)
   1202 {
   1203 	struct mux_frame *frame = &xn->frame;
   1204 	int ret;
   1205 	u32 unknown = 0;
   1206 
   1207 #ifdef __linux__
   1208 	/* Never called with empty queue */
   1209 	BUG_ON(skb_queue_empty(&xn->queue));
   1210 #endif
   1211 	BUG_ON(!xmm7360_qp_can_write(xn->qp));
   1212 
   1213 	xmm7360_mux_frame_init(xn, frame, xn->sequence++);
   1214 	xmm7360_mux_frame_add_tag(frame, XMM_TAG_ADBH, 0, NULL, 0);
   1215 
   1216 	xmm7360_os_handle_net_dequeue(xn, frame);
   1217 	xn->queued_packets = xn->queued_bytes = 0;
   1218 
   1219 	xmm7360_mux_frame_add_tag(frame, XMM_TAG_ADTH, xn->channel, &unknown, sizeof(uint32_t));
   1220 	xmm7360_mux_frame_append_data(frame, &frame->bounds[0], sizeof(struct mux_bounds)*frame->n_packets);
   1221 
   1222 	ret = xmm7360_mux_frame_push(xn->xmm, frame);
   1223 	if (ret)
   1224 		goto drop;
   1225 
   1226 	return;
   1227 
   1228 drop:
   1229 	dev_err(xn->xmm->dev, "Failed to ship coalesced frame");
   1230 }
   1231 
   1232 static int xmm7360_base_init(struct xmm_dev *xmm)
   1233 {
   1234 	int ret, i;
   1235 	u32 status;
   1236 
   1237 	xmm->error = 0;
   1238 	xmm->num_ttys = 0;
   1239 
   1240 	status = xmm->bar2[BAR2_STATUS];
   1241 	if (status == XMM_MODEM_BOOTING) {
   1242 		dev_info(xmm->dev, "modem still booting, waiting...\n");
   1243 		for (i=0; i<100; i++) {
   1244 			status = xmm->bar2[BAR2_STATUS];
   1245 			if (status != XMM_MODEM_BOOTING)
   1246 				break;
   1247 			xmm7360_os_msleep(200);
   1248 		}
   1249 	}
   1250 
   1251 	if (status != XMM_MODEM_READY) {
   1252 		dev_err(xmm->dev, "unknown modem status: 0x%08x\n", status);
   1253 		return -EINVAL;
   1254 	}
   1255 
   1256 	dev_info(xmm->dev, "modem is ready\n");
   1257 
   1258 	ret = xmm7360_cmd_ring_init(xmm);
   1259 	if (ret) {
   1260 		dev_err(xmm->dev, "Could not bring up command ring %d\n",
   1261 		    ret);
   1262 		return ret;
   1263 	}
   1264 
   1265 	return 0;
   1266 }
   1267 
   1268 static void xmm7360_net_mux_handle_frame(struct xmm_net *xn, u8 *data, int len)
   1269 {
   1270 	struct mux_first_header *first;
   1271 	struct mux_next_header *adth;
   1272 	int n_packets, i;
   1273 	struct mux_bounds *bounds;
   1274 
   1275 	first = (void*)data;
   1276 	if (ntohl(first->tag) == XMM_TAG_ACBH)
   1277 		return;
   1278 
   1279 	if (ntohl(first->tag) != XMM_TAG_ADBH) {
   1280 		dev_info(xn->xmm->dev, "Unexpected tag %x\n", first->tag);
   1281 		return;
   1282 	}
   1283 
   1284 	adth = (void*)(&data[first->next]);
   1285 	if (ntohl(adth->tag) != XMM_TAG_ADTH) {
   1286 		dev_err(xn->xmm->dev, "Unexpected tag %x, expected ADTH\n", adth->tag);
   1287 		return;
   1288 	}
   1289 
   1290 	n_packets = (adth->length - sizeof(struct mux_next_header) - 4) / sizeof(struct mux_bounds);
   1291 
   1292 	bounds = (void*)&data[first->next + sizeof(struct mux_next_header) + 4];
   1293 
   1294 	for (i=0; i<n_packets; i++) {
   1295 		if (!bounds[i].length)
   1296 			continue;
   1297 
   1298 		xmm7360_os_handle_net_frame(xn->xmm,
   1299 		    &data[bounds[i].offset], bounds[i].length);
   1300 	}
   1301 }
   1302 
   1303 static void xmm7360_net_poll(struct xmm_dev *xmm)
   1304 {
   1305 	struct queue_pair *qp;
   1306 	struct td_ring *ring;
   1307 	int idx, nread;
   1308 	struct xmm_net *xn = xmm->net;
   1309 	unsigned long flags __unused;
   1310 
   1311 	BUG_ON(!xn);
   1312 
   1313 	qp = xn->qp;
   1314 	ring = &xmm->td_ring[qp->num*2+1];
   1315 
   1316 	spin_lock_irqsave(&xn->lock, flags);
   1317 
   1318 	if (xmm7360_qp_can_write(qp))
   1319 		xmm7360_os_handle_net_txwake(xn);
   1320 
   1321 	while (xmm7360_qp_has_data(qp)) {
   1322 		idx = ring->last_handled;
   1323 		nread = ring->tds[idx].length;
   1324 		xmm7360_net_mux_handle_frame(xn, ring->pages[idx], nread);
   1325 
   1326 		xmm7360_td_ring_read(xmm, qp->num*2+1);
   1327 		xmm7360_ding(xmm, DOORBELL_TD);
   1328 		ring->last_handled = (idx + 1) & (ring->depth - 1);
   1329 	}
   1330 
   1331 	spin_unlock_irqrestore(&xn->lock, flags);
   1332 }
   1333 
   1334 #ifdef __linux__
   1335 
   1336 static void xmm7360_net_uninit(struct net_device *dev)
   1337 {
   1338 }
   1339 
   1340 static int xmm7360_net_open(struct net_device *dev)
   1341 {
   1342 	struct xmm_net *xn = netdev_priv(dev);
   1343 	xn->queued_packets = xn->queued_bytes = 0;
   1344 	skb_queue_purge(&xn->queue);
   1345 	netif_start_queue(dev);
   1346 	return xmm7360_mux_control(xn, 1, 0, 0, 0);
   1347 }
   1348 
   1349 static int xmm7360_net_close(struct net_device *dev)
   1350 {
   1351 	netif_stop_queue(dev);
   1352 	return 0;
   1353 }
   1354 
   1355 static int xmm7360_net_must_flush(struct xmm_net *xn, int new_packet_bytes)
   1356 {
   1357 	int frame_size;
   1358 	if (xn->queued_packets >= MUX_MAX_PACKETS)
   1359 		return 1;
   1360 
   1361 	frame_size = sizeof(struct mux_first_header) + xn->queued_bytes + sizeof(struct mux_next_header) + 4 + sizeof(struct mux_bounds)*xn->queued_packets;
   1362 
   1363 	frame_size += 16 + new_packet_bytes + sizeof(struct mux_bounds);
   1364 
   1365 	return frame_size > xn->frame.max_size;
   1366 }
   1367 
   1368 static enum hrtimer_restart xmm7360_net_deadline_cb(struct hrtimer *t)
   1369 {
   1370 	struct xmm_net *xn = container_of(t, struct xmm_net, deadline);
   1371 	unsigned long flags;
   1372 	spin_lock_irqsave(&xn->lock, flags);
   1373 	if (!skb_queue_empty(&xn->queue) && xmm7360_qp_can_write(xn->qp))
   1374 		xmm7360_net_flush(xn);
   1375 	spin_unlock_irqrestore(&xn->lock, flags);
   1376 	return HRTIMER_NORESTART;
   1377 }
   1378 
   1379 static netdev_tx_t xmm7360_net_xmit(struct sk_buff *skb, struct net_device *dev)
   1380 {
   1381 	struct xmm_net *xn = netdev_priv(dev);
   1382 	ktime_t kt;
   1383 	unsigned long flags;
   1384 
   1385 	if (netif_queue_stopped(dev))
   1386 		return NETDEV_TX_BUSY;
   1387 
   1388 	skb_orphan(skb);
   1389 
   1390 	spin_lock_irqsave(&xn->lock, flags);
   1391 	if (xmm7360_net_must_flush(xn, skb->len)) {
   1392 		if (xmm7360_qp_can_write(xn->qp)) {
   1393 			xmm7360_net_flush(xn);
   1394 		} else {
   1395 			netif_stop_queue(dev);
   1396 			spin_unlock_irqrestore(&xn->lock, flags);
   1397 			return NETDEV_TX_BUSY;
   1398 		}
   1399 	}
   1400 
   1401 	xn->queued_packets++;
   1402 	xn->queued_bytes += 16 + skb->len;
   1403 	skb_queue_tail(&xn->queue, skb);
   1404 
   1405 	spin_unlock_irqrestore(&xn->lock, flags);
   1406 
   1407 	if (!hrtimer_active(&xn->deadline)) {
   1408 		kt = ktime_set(0, 100000);
   1409 		hrtimer_start(&xn->deadline, kt, HRTIMER_MODE_REL);
   1410 	}
   1411 
   1412 	return NETDEV_TX_OK;
   1413 }
   1414 
   1415 static void xmm7360_os_handle_net_frame(struct xmm_dev *xmm, const u8 *buf, size_t sz)
   1416 {
   1417 	struct sk_buff *skb;
   1418 	void *p;
   1419 	u8 ip_version;
   1420 
   1421 	skb = dev_alloc_skb(sz + NET_IP_ALIGN);
   1422 	if (!skb)
   1423 		return;
   1424 	skb_reserve(skb, NET_IP_ALIGN);
   1425 	p = skb_put(skb, sz);
   1426 	memcpy(p, buf, sz);
   1427 
   1428 	skb->dev = xmm->netdev;
   1429 
   1430 	ip_version = skb->data[0] >> 4;
   1431 	if (ip_version == 4) {
   1432 		skb->protocol = htons(ETH_P_IP);
   1433 	} else if (ip_version == 6) {
   1434 		skb->protocol = htons(ETH_P_IPV6);
   1435 	} else {
   1436 		kfree_skb(skb);
   1437 		return;
   1438 	}
   1439 
   1440 	netif_rx(skb);
   1441 }
   1442 
   1443 static void xmm7360_os_handle_net_dequeue(struct xmm_net *xn, struct mux_frame *frame)
   1444 {
   1445 	struct sk_buff *skb;
   1446 	int ret;
   1447 
   1448 	while ((skb = skb_dequeue(&xn->queue))) {
   1449 		ret = xmm7360_mux_frame_append_packet(frame,
   1450 		    skb->data, skb->len);
   1451 		kfree_skb(skb);
   1452 		if (ret) {
   1453 			/* No more space in the frame */
   1454 			break;
   1455 		}
   1456 	}
   1457 }
   1458 
   1459 static void xmm7360_os_handle_net_txwake(struct xmm_net *xn)
   1460 {
   1461 	BUG_ON(!xmm7360_qp_can_write(xn->qp));
   1462 
   1463 	if (netif_queue_stopped(xn->xmm->netdev))
   1464 		netif_wake_queue(xn->xmm->netdev);
   1465 }
   1466 
   1467 static const struct net_device_ops xmm7360_netdev_ops = {
   1468 	.ndo_uninit		= xmm7360_net_uninit,
   1469 	.ndo_open		= xmm7360_net_open,
   1470 	.ndo_stop		= xmm7360_net_close,
   1471 	.ndo_start_xmit		= xmm7360_net_xmit,
   1472 };
   1473 
   1474 static void xmm7360_net_setup(struct net_device *dev)
   1475 {
   1476 	struct xmm_net *xn = netdev_priv(dev);
   1477 	spin_lock_init(&xn->lock);
   1478 	hrtimer_init(&xn->deadline, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
   1479 	xn->deadline.function = xmm7360_net_deadline_cb;
   1480 	skb_queue_head_init(&xn->queue);
   1481 
   1482 	dev->netdev_ops = &xmm7360_netdev_ops;
   1483 
   1484 	dev->hard_header_len = 0;
   1485 	dev->addr_len = 0;
   1486 	dev->mtu = 1500;
   1487 	dev->min_mtu = 1500;
   1488 	dev->max_mtu = 1500;
   1489 
   1490 	dev->tx_queue_len = 1000;
   1491 
   1492 	dev->type = ARPHRD_NONE;
   1493 	dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
   1494 }
   1495 
   1496 static int xmm7360_create_net(struct xmm_dev *xmm, int num)
   1497 {
   1498 	struct net_device *netdev;
   1499 	struct xmm_net *xn;
   1500 	int ret;
   1501 
   1502 	netdev = alloc_netdev(sizeof(struct xmm_net), "wwan%d", NET_NAME_UNKNOWN, xmm7360_net_setup);
   1503 
   1504 	if (!netdev)
   1505 		return -ENOMEM;
   1506 
   1507 	SET_NETDEV_DEV(netdev, xmm->dev);
   1508 
   1509 	xmm->netdev = netdev;
   1510 
   1511 	xn = netdev_priv(netdev);
   1512 	xn->xmm = xmm;
   1513 	xmm->net = xn;
   1514 
   1515 	rtnl_lock();
   1516 	ret = register_netdevice(netdev);
   1517 	rtnl_unlock();
   1518 
   1519 	xn->qp = xmm7360_init_qp(xmm, num, 128, TD_MAX_PAGE_SIZE);
   1520 
   1521 	if (!ret)
   1522 		ret = xmm7360_qp_start(xn->qp);
   1523 
   1524 	if (ret < 0) {
   1525 		free_netdev(netdev);
   1526 		xmm->netdev = NULL;
   1527 		xmm7360_qp_stop(xn->qp);
   1528 	}
   1529 
   1530 	return ret;
   1531 }
   1532 
   1533 static void xmm7360_destroy_net(struct xmm_dev *xmm)
   1534 {
   1535 	if (xmm->netdev) {
   1536 		xmm7360_qp_stop(xmm->net->qp);
   1537 		rtnl_lock();
   1538 		unregister_netdevice(xmm->netdev);
   1539 		rtnl_unlock();
   1540 		free_netdev(xmm->netdev);
   1541 		xmm->net = NULL;
   1542 		xmm->netdev = NULL;
   1543 	}
   1544 }
   1545 
   1546 static irqreturn_t xmm7360_irq0(int irq, void *dev_id) {
   1547 	struct xmm_dev *xmm = dev_id;
   1548 	struct queue_pair *qp;
   1549 	int id;
   1550 
   1551 	xmm7360_poll(xmm);
   1552 	wake_up(&xmm->wq);
   1553 	if (xmm->td_ring) {
   1554 		if (xmm->net)
   1555 			xmm7360_net_poll(xmm);
   1556 
   1557 		for (id=1; id<XMM_QP_COUNT; id++) {
   1558 			qp = &xmm->qp[id];
   1559 
   1560 			/* wake _cdev_read() */
   1561 			if (qp->open)
   1562 				wake_up(&qp->wq);
   1563 
   1564 			/* tty tasks */
   1565 			if (qp->open && qp->port.ops) {
   1566 				xmm7360_tty_poll_qp(qp);
   1567 				if (qp->tty_needs_wake && xmm7360_qp_can_write(qp) && qp->port.tty) {
   1568 					struct tty_ldisc *ldisc = tty_ldisc_ref(qp->port.tty);
   1569 					if (ldisc) {
   1570 						if (ldisc->ops->write_wakeup)
   1571 							ldisc->ops->write_wakeup(qp->port.tty);
   1572 						tty_ldisc_deref(ldisc);
   1573 					}
   1574 					qp->tty_needs_wake = 0;
   1575 				}
   1576 			}
   1577 		}
   1578 	}
   1579 
   1580 	return IRQ_HANDLED;
   1581 }
   1582 
   1583 static dev_t xmm_base;
   1584 
   1585 static struct tty_driver *xmm7360_tty_driver;
   1586 
   1587 static void xmm7360_dev_deinit(struct xmm_dev *xmm)
   1588 {
   1589 	int i;
   1590 	xmm->error = -ENODEV;
   1591 
   1592 	cancel_work_sync(&xmm->init_work);
   1593 
   1594 	xmm7360_destroy_net(xmm);
   1595 
   1596 	for (i=0; i<XMM_QP_COUNT; i++) {
   1597 		if (xmm->qp[i].xmm) {
   1598 			if (xmm->qp[i].cdev.owner) {
   1599 				cdev_del(&xmm->qp[i].cdev);
   1600 				device_unregister(&xmm->qp[i].dev);
   1601 			}
   1602 			if (xmm->qp[i].port.ops) {
   1603 				tty_unregister_device(xmm7360_tty_driver, xmm->qp[i].tty_index);
   1604 				tty_port_destroy(&xmm->qp[i].port);
   1605 			}
   1606 		}
   1607 		memset(&xmm->qp[i], 0, sizeof(struct queue_pair));
   1608 	}
   1609 	xmm7360_cmd_ring_free(xmm);
   1610 
   1611 }
   1612 
   1613 static void xmm7360_remove(struct pci_dev *dev)
   1614 {
   1615 	struct xmm_dev *xmm = pci_get_drvdata(dev);
   1616 
   1617 	xmm7360_dev_deinit(xmm);
   1618 
   1619 	if (xmm->irq)
   1620 		free_irq(xmm->irq, xmm);
   1621 	pci_free_irq_vectors(dev);
   1622 	pci_release_region(dev, 0);
   1623 	pci_release_region(dev, 2);
   1624 	pci_disable_device(dev);
   1625 	kfree(xmm);
   1626 }
   1627 
   1628 static void xmm7360_cdev_dev_release(struct device *dev)
   1629 {
   1630 }
   1631 
   1632 static int xmm7360_tty_open(struct tty_struct *tty, struct file *filp)
   1633 {
   1634 	struct queue_pair *qp = tty->driver_data;
   1635 	return tty_port_open(&qp->port, tty, filp);
   1636 }
   1637 
   1638 static void xmm7360_tty_close(struct tty_struct *tty, struct file *filp)
   1639 {
   1640 	struct queue_pair *qp = tty->driver_data;
   1641 	if (qp)
   1642 		tty_port_close(&qp->port, tty, filp);
   1643 }
   1644 
   1645 static int xmm7360_tty_write(struct tty_struct *tty, const unsigned char *buffer,
   1646 		      int count)
   1647 {
   1648 	struct queue_pair *qp = tty->driver_data;
   1649 	int written;
   1650 	written = xmm7360_qp_write(qp, buffer, count);
   1651 	if (written < count)
   1652 		qp->tty_needs_wake = 1;
   1653 	return written;
   1654 }
   1655 
   1656 static int xmm7360_tty_write_room(struct tty_struct *tty)
   1657 {
   1658 	struct queue_pair *qp = tty->driver_data;
   1659 	if (!xmm7360_qp_can_write(qp))
   1660 		return 0;
   1661 	else
   1662 		return qp->xmm->td_ring[qp->num*2].page_size;
   1663 }
   1664 
   1665 static int xmm7360_tty_install(struct tty_driver *driver, struct tty_struct *tty)
   1666 {
   1667 	struct queue_pair *qp;
   1668 	int ret;
   1669 
   1670 	ret = tty_standard_install(driver, tty);
   1671 	if (ret)
   1672 		return ret;
   1673 
   1674 	tty->port = driver->ports[tty->index];
   1675 	qp = container_of(tty->port, struct queue_pair, port);
   1676 	tty->driver_data = qp;
   1677 	return 0;
   1678 }
   1679 
   1680 
   1681 static int xmm7360_tty_port_activate(struct tty_port *tport, struct tty_struct *tty)
   1682 {
   1683 	struct queue_pair *qp = tty->driver_data;
   1684 	return xmm7360_qp_start(qp);
   1685 }
   1686 
   1687 static void xmm7360_tty_port_shutdown(struct tty_port *tport)
   1688 {
   1689 	struct queue_pair *qp = tport->tty->driver_data;
   1690 	xmm7360_qp_stop(qp);
   1691 }
   1692 
   1693 
   1694 static const struct tty_port_operations xmm7360_tty_port_ops = {
   1695 	.activate = xmm7360_tty_port_activate,
   1696 	.shutdown = xmm7360_tty_port_shutdown,
   1697 };
   1698 
   1699 static const struct tty_operations xmm7360_tty_ops = {
   1700 	.open = xmm7360_tty_open,
   1701 	.close = xmm7360_tty_close,
   1702 	.write = xmm7360_tty_write,
   1703 	.write_room = xmm7360_tty_write_room,
   1704 	.install = xmm7360_tty_install,
   1705 };
   1706 
   1707 static int xmm7360_create_tty(struct xmm_dev *xmm, int num)
   1708 {
   1709 	struct device *tty_dev;
   1710 	struct queue_pair *qp = xmm7360_init_qp(xmm, num, 8, 4096);
   1711 	int ret;
   1712 	tty_port_init(&qp->port);
   1713 	qp->port.low_latency = 1;
   1714 	qp->port.ops = &xmm7360_tty_port_ops;
   1715 	qp->tty_index = xmm->num_ttys++;
   1716 	tty_dev = tty_port_register_device(&qp->port, xmm7360_tty_driver, qp->tty_index, xmm->dev);
   1717 
   1718 	if (IS_ERR(tty_dev)) {
   1719 		qp->port.ops = NULL;	// prevent calling unregister
   1720 		ret = PTR_ERR(tty_dev);
   1721 		dev_err(xmm->dev, "Could not allocate tty?\n");
   1722 		tty_port_destroy(&qp->port);
   1723 		return ret;
   1724 	}
   1725 
   1726 	return 0;
   1727 }
   1728 
   1729 static int xmm7360_create_cdev(struct xmm_dev *xmm, int num, const char *name, int cardnum)
   1730 {
   1731 	struct queue_pair *qp = xmm7360_init_qp(xmm, num, 16, TD_MAX_PAGE_SIZE);
   1732 	int ret;
   1733 
   1734 	cdev_init(&qp->cdev, &xmm7360_fops);
   1735 	qp->cdev.owner = THIS_MODULE;
   1736 	device_initialize(&qp->dev);
   1737 	qp->dev.devt = MKDEV(MAJOR(xmm_base), num); // XXX multiple cards
   1738 	qp->dev.parent = &xmm->pci_dev->dev;
   1739 	qp->dev.release = xmm7360_cdev_dev_release;
   1740 	dev_set_name(&qp->dev, name, cardnum);
   1741 	dev_set_drvdata(&qp->dev, qp);
   1742 	ret = cdev_device_add(&qp->cdev, &qp->dev);
   1743 	if (ret) {
   1744 		dev_err(xmm->dev, "cdev_device_add: %d\n", ret);
   1745 		return ret;
   1746 	}
   1747 	return 0;
   1748 }
   1749 
   1750 static int xmm7360_dev_init(struct xmm_dev *xmm)
   1751 {
   1752 	int ret;
   1753 
   1754 	ret = xmm7360_base_init(xmm);
   1755 	if (ret)
   1756 		return ret;
   1757 
   1758 	ret = xmm7360_create_cdev(xmm, 1, "xmm%d/rpc", xmm->card_num);
   1759 	if (ret)
   1760 		return ret;
   1761 	ret = xmm7360_create_cdev(xmm, 3, "xmm%d/trace", xmm->card_num);
   1762 	if (ret)
   1763 		return ret;
   1764 	ret = xmm7360_create_tty(xmm, 2);
   1765 	if (ret)
   1766 		return ret;
   1767 	ret = xmm7360_create_tty(xmm, 4);
   1768 	if (ret)
   1769 		return ret;
   1770 	ret = xmm7360_create_tty(xmm, 7);
   1771 	if (ret)
   1772 		return ret;
   1773 	ret = xmm7360_create_net(xmm, 0);
   1774 	if (ret)
   1775 		return ret;
   1776 
   1777 	return 0;
   1778 }
   1779 
   1780 void xmm7360_dev_init_work(struct work_struct *work)
   1781 {
   1782 	struct xmm_dev *xmm = container_of(work, struct xmm_dev, init_work);
   1783 	xmm7360_dev_init(xmm);
   1784 }
   1785 
   1786 static int xmm7360_probe(struct pci_dev *dev, const struct pci_device_id *id)
   1787 {
   1788 	struct xmm_dev *xmm = kzalloc(sizeof(struct xmm_dev), GFP_KERNEL);
   1789 	int ret;
   1790 
   1791 	xmm->pci_dev = dev;
   1792 	xmm->dev = &dev->dev;
   1793 
   1794 	if (!xmm) {
   1795 		dev_err(&(dev->dev), "kzalloc\n");
   1796 		return -ENOMEM;
   1797 	}
   1798 
   1799 	ret = pci_enable_device(dev);
   1800 	if (ret) {
   1801 		dev_err(&(dev->dev), "pci_enable_device\n");
   1802 		goto fail;
   1803 	}
   1804 	pci_set_master(dev);
   1805 
   1806 	ret = pci_set_dma_mask(dev, 0xffffffffffffffff);
   1807 	if (ret) {
   1808 		dev_err(xmm->dev, "Cannot set DMA mask\n");
   1809 		goto fail;
   1810 	}
   1811 	dma_set_coherent_mask(xmm->dev, 0xffffffffffffffff);
   1812 
   1813 
   1814 	ret = pci_request_region(dev, 0, "xmm0");
   1815 	if (ret) {
   1816 		dev_err(&(dev->dev), "pci_request_region(0)\n");
   1817 		goto fail;
   1818 	}
   1819 	xmm->bar0 = pci_iomap(dev, 0, pci_resource_len(dev, 0));
   1820 
   1821 	ret = pci_request_region(dev, 2, "xmm2");
   1822 	if (ret) {
   1823 		dev_err(&(dev->dev), "pci_request_region(2)\n");
   1824 		goto fail;
   1825 	}
   1826 	xmm->bar2 = pci_iomap(dev, 2, pci_resource_len(dev, 2));
   1827 
   1828 	ret = pci_alloc_irq_vectors(dev, 1, 1, PCI_IRQ_MSI | PCI_IRQ_MSIX);
   1829 	if (ret < 0) {
   1830 		dev_err(&(dev->dev), "pci_alloc_irq_vectors\n");
   1831 		goto fail;
   1832 	}
   1833 
   1834 	init_waitqueue_head(&xmm->wq);
   1835 	INIT_WORK(&xmm->init_work, xmm7360_dev_init_work);
   1836 
   1837 	pci_set_drvdata(dev, xmm);
   1838 
   1839 	ret = xmm7360_dev_init(xmm);
   1840 	if (ret)
   1841 		goto fail;
   1842 
   1843 	xmm->irq = pci_irq_vector(dev, 0);
   1844 	ret = request_irq(xmm->irq, xmm7360_irq0, 0, "xmm7360", xmm);
   1845 	if (ret) {
   1846 		dev_err(&(dev->dev), "request_irq\n");
   1847 		goto fail;
   1848 	}
   1849 
   1850 	return ret;
   1851 
   1852 fail:
   1853 	xmm7360_dev_deinit(xmm);
   1854 	xmm7360_remove(dev);
   1855 	return ret;
   1856 }
   1857 
   1858 static struct pci_driver xmm7360_driver = {
   1859 	.name		= "xmm7360",
   1860 	.id_table	= xmm7360_ids,
   1861 	.probe		= xmm7360_probe,
   1862 	.remove		= xmm7360_remove,
   1863 };
   1864 
   1865 static int xmm7360_init(void)
   1866 {
   1867 	int ret;
   1868 	ret = alloc_chrdev_region(&xmm_base, 0, 8, "xmm");
   1869 	if (ret)
   1870 		return ret;
   1871 
   1872 	xmm7360_tty_driver = alloc_tty_driver(8);
   1873 	if (!xmm7360_tty_driver)
   1874 		return -ENOMEM;
   1875 
   1876 	xmm7360_tty_driver->driver_name = "xmm7360";
   1877 	xmm7360_tty_driver->name = "ttyXMM";
   1878 	xmm7360_tty_driver->major = 0;
   1879 	xmm7360_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
   1880 	xmm7360_tty_driver->subtype = SERIAL_TYPE_NORMAL;
   1881 	xmm7360_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
   1882 	xmm7360_tty_driver->init_termios = tty_std_termios;
   1883 	xmm7360_tty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \
   1884 						HUPCL | CLOCAL;
   1885 	xmm7360_tty_driver->init_termios.c_lflag &= ~ECHO;
   1886 	xmm7360_tty_driver->init_termios.c_ispeed = 115200;
   1887 	xmm7360_tty_driver->init_termios.c_ospeed = 115200;
   1888 	tty_set_operations(xmm7360_tty_driver, &xmm7360_tty_ops);
   1889 
   1890 	ret = tty_register_driver(xmm7360_tty_driver);
   1891 	if (ret) {
   1892 		pr_err("xmm7360: failed to register xmm7360_tty driver\n");
   1893 		return ret;
   1894 	}
   1895 
   1896 
   1897 	ret = pci_register_driver(&xmm7360_driver);
   1898 	if (ret)
   1899 		return ret;
   1900 
   1901 	return 0;
   1902 }
   1903 
   1904 static void xmm7360_exit(void)
   1905 {
   1906 	pci_unregister_driver(&xmm7360_driver);
   1907 	unregister_chrdev_region(xmm_base, 8);
   1908 	tty_unregister_driver(xmm7360_tty_driver);
   1909 	put_tty_driver(xmm7360_tty_driver);
   1910 }
   1911 
   1912 module_init(xmm7360_init);
   1913 module_exit(xmm7360_exit);
   1914 
   1915 #endif /* __linux__ */
   1916 
   1917 #if defined(__OpenBSD__) || defined(__NetBSD__)
   1918 
   1919 /*
   1920  * RPC and trace devices behave as regular character device,
   1921  * other devices behave as terminal.
   1922  */
   1923 #define DEVCUA(x)	(minor(x) & 0x80)
   1924 #define DEVUNIT(x)	((minor(x) & 0x70) >> 4)
   1925 #define DEVFUNC_MASK	0x0f
   1926 #define DEVFUNC(x)	(minor(x) & DEVFUNC_MASK)
   1927 #define DEV_IS_TTY(x)	(DEVFUNC(x) == 2 || DEVFUNC(x) > 3)
   1928 
   1929 struct wwanc_softc {
   1930 #ifdef __OpenBSD__
   1931 	struct device		sc_devx;	/* gen. device info storage */
   1932 #endif
   1933 	struct device		*sc_dev;	/* generic device information */
   1934         pci_chipset_tag_t       sc_pc;
   1935         pcitag_t                sc_tag;
   1936 	bus_dma_tag_t		sc_dmat;
   1937 	pci_intr_handle_t	sc_pih;
   1938         void                    *sc_ih;         /* interrupt vectoring */
   1939 
   1940 	bus_space_tag_t		sc_bar0_tag;
   1941 	bus_space_handle_t	sc_bar0_handle;
   1942 	bus_size_t		sc_bar0_sz;
   1943 	bus_space_tag_t		sc_bar2_tag;
   1944 	bus_space_handle_t	sc_bar2_handle;
   1945 	bus_size_t		sc_bar2_sz;
   1946 
   1947 	struct xmm_dev		sc_xmm;
   1948 	struct tty		*sc_tty[XMM_QP_COUNT];
   1949 	struct device		*sc_net;
   1950 	struct selinfo		sc_selr, sc_selw;
   1951 	bool			sc_resume;
   1952 };
   1953 
   1954 struct wwanc_attach_args {
   1955 	enum wwanc_type {
   1956 		WWMC_TYPE_RPC,
   1957 		WWMC_TYPE_TRACE,
   1958 		WWMC_TYPE_TTY,
   1959 		WWMC_TYPE_NET
   1960 	} aa_type;
   1961 };
   1962 
   1963 static int     wwanc_match(struct device *, cfdata_t, void *);
   1964 static void    wwanc_attach(struct device *, struct device *, void *);
   1965 static int     wwanc_detach(struct device *, int);
   1966 
   1967 #ifdef __OpenBSD__
   1968 static int     wwanc_activate(struct device *, int);
   1969 
   1970 struct cfattach wwanc_ca = {
   1971         sizeof(struct wwanc_softc), wwanc_match, wwanc_attach,
   1972         wwanc_detach, wwanc_activate
   1973 };
   1974 
   1975 struct cfdriver wwanc_cd = {
   1976         NULL, "wwanc", DV_DULL
   1977 };
   1978 #endif
   1979 
   1980 #ifdef __NetBSD__
   1981 CFATTACH_DECL3_NEW(wwanc, sizeof(struct wwanc_softc),
   1982    wwanc_match, wwanc_attach, wwanc_detach, NULL,
   1983    NULL, NULL, DVF_DETACH_SHUTDOWN);
   1984 
   1985 static bool wwanc_pmf_suspend(device_t, const pmf_qual_t *);
   1986 static bool wwanc_pmf_resume(device_t, const pmf_qual_t *);
   1987 #endif /* __NetBSD__ */
   1988 
   1989 static int
   1990 wwanc_match(struct device *parent, cfdata_t match, void *aux)
   1991 {
   1992 	struct pci_attach_args *pa = aux;
   1993 
   1994 	return (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_INTEL &&
   1995 		PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_XMM7360);
   1996 }
   1997 
   1998 static int xmm7360_dev_init(struct xmm_dev *xmm)
   1999 {
   2000 	int ret;
   2001 	int depth, page_size;
   2002 
   2003 	ret = xmm7360_base_init(xmm);
   2004 	if (ret)
   2005 		return ret;
   2006 
   2007 	/* Initialize queue pairs for later use */
   2008 	for (int num = 0; num < XMM_QP_COUNT; num++) {
   2009 		switch (num) {
   2010 		case 0:	/* net */
   2011 			depth = 128;
   2012 			page_size = TD_MAX_PAGE_SIZE;
   2013 			break;
   2014 		case 1:	/* rpc */
   2015 		case 3: /* trace */
   2016 			depth = 16;
   2017 			page_size = TD_MAX_PAGE_SIZE;
   2018 			break;
   2019 		default: /* tty */
   2020 			depth = 8;
   2021 			page_size = 4096;
   2022 			break;
   2023 		}
   2024 
   2025 		xmm7360_init_qp(xmm, num, depth, page_size);
   2026 	}
   2027 
   2028 	return 0;
   2029 }
   2030 
   2031 static void xmm7360_dev_deinit(struct xmm_dev *xmm)
   2032 {
   2033 	struct wwanc_softc *sc = device_private(xmm->dev);
   2034 	bool devgone = false;
   2035 	struct tty *tp;
   2036 
   2037 	xmm->error = -ENODEV;
   2038 
   2039 	/* network device should be gone by now */
   2040 	KASSERT(sc->sc_net == NULL);
   2041 	KASSERT(xmm->net == NULL);
   2042 
   2043 	/* free ttys */
   2044 	for (int i=0; i<XMM_QP_COUNT; i++) {
   2045 		tp = sc->sc_tty[i];
   2046 		if (tp) {
   2047 			KASSERT(DEV_IS_TTY(i));
   2048 			if (!devgone) {
   2049 				vdevgone(major(tp->t_dev), 0, DEVFUNC_MASK,
   2050 				    VCHR);
   2051 				devgone = true;
   2052 			}
   2053 			ttyfree(tp);
   2054 			sc->sc_tty[i] = NULL;
   2055 		}
   2056 	}
   2057 
   2058 	xmm7360_cmd_ring_free(xmm);
   2059 }
   2060 
   2061 static void
   2062 wwanc_io_wakeup(struct queue_pair *qp, int flag)
   2063 {
   2064         if (flag & FREAD) {
   2065                 selnotify(&qp->selr, POLLIN|POLLRDNORM, NOTE_SUBMIT);
   2066                 wakeup(qp->wq);
   2067         }
   2068         if (flag & FWRITE) {
   2069                 selnotify(&qp->selw, POLLOUT|POLLWRNORM, NOTE_SUBMIT);
   2070                 wakeup(qp->wq);
   2071         }
   2072 }
   2073 
   2074 static int
   2075 wwanc_intr(void *xsc)
   2076 {
   2077 	struct wwanc_softc *sc = xsc;
   2078 	struct xmm_dev *xmm = &sc->sc_xmm;
   2079 	struct queue_pair *qp;
   2080 
   2081 	xmm7360_poll(xmm);
   2082 	wakeup(&xmm->wq);
   2083 
   2084 	if (xmm->net && xmm->net->qp->open && xmm7360_qp_has_data(xmm->net->qp))
   2085 		xmm7360_net_poll(xmm);
   2086 
   2087 	for (int func = 1; func < XMM_QP_COUNT; func++) {
   2088 		qp = &xmm->qp[func];
   2089 		if (!qp->open)
   2090 			continue;
   2091 
   2092 		/* Check for input, wwancstart()/wwancwrite() does output */
   2093 		if (xmm7360_qp_has_data(qp)) {
   2094 			if (DEV_IS_TTY(func)) {
   2095 				int s = spltty();
   2096 				xmm7360_tty_poll_qp(qp);
   2097 				splx(s);
   2098 			}
   2099 			wwanc_io_wakeup(qp, FREAD);
   2100 		}
   2101 
   2102 		/* Wakeup/notify eventual writers */
   2103 		if (xmm7360_qp_can_write(qp))
   2104 			wwanc_io_wakeup(qp, FWRITE);
   2105 	}
   2106 
   2107 	return 1;
   2108 }
   2109 
   2110 static int
   2111 wwancprint(void *aux, const char *pnp)
   2112 {
   2113 	struct wwanc_attach_args *wa = aux;
   2114 
   2115 	if (pnp)
   2116                 printf("wwanc type %s at %s",
   2117 		    (wa->aa_type == WWMC_TYPE_NET) ? "net" : "unk", pnp);
   2118 	else
   2119 		printf(" type %s",
   2120 		    (wa->aa_type == WWMC_TYPE_NET) ? "net" : "unk");
   2121 
   2122 	return (UNCONF);
   2123 }
   2124 
   2125 static void
   2126 wwanc_attach_finish(struct device *self)
   2127 {
   2128 	struct wwanc_softc *sc = device_private(self);
   2129 
   2130 	if (xmm7360_dev_init(&sc->sc_xmm)) {
   2131 		/* error already printed */
   2132 		return;
   2133 	}
   2134 
   2135 	/* Attach the network device */
   2136 	struct wwanc_attach_args wa;
   2137 	memset(&wa, 0, sizeof(wa));
   2138 	wa.aa_type = WWMC_TYPE_NET;
   2139 	sc->sc_net = config_found(self, &wa, wwancprint);
   2140 }
   2141 
   2142 static void
   2143 wwanc_attach(struct device *parent, struct device *self, void *aux)
   2144 {
   2145 	struct wwanc_softc *sc = device_private(self);
   2146 	struct pci_attach_args *pa = aux;
   2147 	bus_space_tag_t memt;
   2148 	bus_space_handle_t memh;
   2149 	bus_size_t sz;
   2150 	int error;
   2151 	const char *intrstr;
   2152 #ifdef __OpenBSD__
   2153 	pci_intr_handle_t ih;
   2154 #endif
   2155 #ifdef __NetBSD__
   2156 	pci_intr_handle_t *ih;
   2157 	char intrbuf[PCI_INTRSTR_LEN];
   2158 #endif
   2159 
   2160 	sc->sc_dev = self;
   2161 	sc->sc_pc = pa->pa_pc;
   2162 	sc->sc_tag = pa->pa_tag;
   2163 	sc->sc_dmat = pa->pa_dmat;
   2164 
   2165 	/* map the register window, memory mapped 64-bit non-prefetchable */
   2166 	error = pci_mapreg_map(pa, WWAN_BAR0,
   2167 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT,
   2168 	    BUS_SPACE_MAP_LINEAR, &memt, &memh, NULL, &sz, 0);
   2169 	if (error != 0) {
   2170 		printf(": can't map mem space for BAR0 %d\n", error);
   2171 		return;
   2172 	}
   2173 	sc->sc_bar0_tag = memt;
   2174 	sc->sc_bar0_handle = memh;
   2175 	sc->sc_bar0_sz = sz;
   2176 
   2177 	error = pci_mapreg_map(pa, WWAN_BAR2,
   2178 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT,
   2179 	    BUS_SPACE_MAP_LINEAR, &memt, &memh, NULL, &sz, 0);
   2180 	if (error != 0) {
   2181 		bus_space_unmap(sc->sc_bar0_tag, sc->sc_bar0_handle,
   2182 		    sc->sc_bar0_sz);
   2183 		printf(": can't map mem space for BAR2\n");
   2184 		return;
   2185 	}
   2186 	sc->sc_bar2_tag = memt;
   2187 	sc->sc_bar2_handle = memh;
   2188 	sc->sc_bar2_sz = sz;
   2189 
   2190 	/* Set xmm members needed for xmm7360_dev_init() */
   2191 	sc->sc_xmm.dev = self;
   2192 	sc->sc_xmm.bar0 = bus_space_vaddr(sc->sc_bar0_tag, sc->sc_bar0_handle);
   2193 	sc->sc_xmm.bar2 = bus_space_vaddr(sc->sc_bar0_tag, sc->sc_bar2_handle);
   2194 	init_waitqueue_head(&sc->sc_xmm.wq);
   2195 
   2196 #ifdef __OpenBSD__
   2197 	if (pci_intr_map_msi(pa, &ih) && pci_intr_map(pa, &ih)) {
   2198 		printf(": can't map interrupt\n");
   2199 		goto fail;
   2200 	}
   2201 	sc->sc_pih = ih;
   2202 	intrstr = pci_intr_string(sc->sc_pc, ih);
   2203 	printf(": %s\n", intrstr);
   2204 #endif
   2205 #ifdef __NetBSD__
   2206 	if (pci_intr_alloc(pa, &ih, NULL, 0)) {
   2207 		printf(": can't map interrupt\n");
   2208 		goto fail;
   2209 	}
   2210 	sc->sc_pih = ih[0];
   2211 	intrstr = pci_intr_string(pa->pa_pc, ih[0], intrbuf, sizeof(intrbuf));
   2212 	aprint_normal(": LTE modem\n");
   2213 	aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
   2214 #endif
   2215 
   2216 	/* Device initialized, can establish the interrupt now */
   2217 	sc->sc_ih = pci_intr_establish(sc->sc_pc, sc->sc_pih, IPL_NET,
   2218 	    wwanc_intr, sc, sc->sc_dev->dv_xname);
   2219 	if (sc->sc_ih == NULL) {
   2220 		printf("%s: can't establish interrupt\n", self->dv_xname);
   2221 		return;
   2222 	}
   2223 
   2224 #ifdef __NetBSD__
   2225 	if (!pmf_device_register(self, wwanc_pmf_suspend, wwanc_pmf_resume))
   2226 		aprint_error_dev(self, "couldn't establish power handler\n");
   2227 #endif
   2228 
   2229 	/*
   2230 	 * Device initialization requires working interrupts, so need
   2231 	 * to postpone this until they are enabled.
   2232 	 */
   2233 	config_mountroot(self, wwanc_attach_finish);
   2234 	return;
   2235 
   2236 fail:
   2237 	bus_space_unmap(sc->sc_bar0_tag, sc->sc_bar0_handle, sc->sc_bar0_sz);
   2238 	sc->sc_bar0_tag = 0;
   2239 	bus_space_unmap(sc->sc_bar2_tag, sc->sc_bar2_handle, sc->sc_bar2_sz);
   2240 	sc->sc_bar2_tag = 0;
   2241 	return;
   2242 }
   2243 
   2244 static int
   2245 wwanc_detach(struct device *self, int flags)
   2246 {
   2247 	int error;
   2248 	struct wwanc_softc *sc = device_private(self);
   2249 
   2250 	if (sc->sc_ih) {
   2251 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
   2252 		sc->sc_ih = NULL;
   2253 	}
   2254 
   2255 	if (sc->sc_net) {
   2256 		error = config_detach_children(self, flags);
   2257 		if (error)
   2258 			return error;
   2259 		sc->sc_net = NULL;
   2260 	}
   2261 
   2262 	pmf_device_deregister(self);
   2263 
   2264 	xmm7360_dev_deinit(&sc->sc_xmm);
   2265 
   2266 	if (sc->sc_bar0_tag) {
   2267 		bus_space_unmap(sc->sc_bar0_tag, sc->sc_bar0_handle,
   2268 		    sc->sc_bar0_sz);
   2269 		sc->sc_bar0_tag = 0;
   2270 	}
   2271 	sc->sc_xmm.bar0 = NULL;
   2272 
   2273 	if (sc->sc_bar2_tag) {
   2274 		bus_space_unmap(sc->sc_bar2_tag, sc->sc_bar2_handle,
   2275 		    sc->sc_bar2_sz);
   2276 		sc->sc_bar2_tag = 0;
   2277 	}
   2278 	sc->sc_xmm.bar2 = NULL;
   2279 
   2280 	return 0;
   2281 }
   2282 
   2283 static void
   2284 wwanc_suspend(struct device *self)
   2285 {
   2286 	struct wwanc_softc *sc = device_private(self);
   2287 	struct xmm_dev *xmm = &sc->sc_xmm;
   2288 	struct queue_pair *qp;
   2289 
   2290 	KASSERT(!sc->sc_resume);
   2291 	KASSERT(xmm->cp != NULL);
   2292 
   2293 	for (int i = 0; i < XMM_QP_COUNT; i++) {
   2294 		qp = &xmm->qp[i];
   2295 		if (qp->open)
   2296 			xmm7360_qp_suspend(qp);
   2297 	}
   2298 
   2299 	xmm7360_cmd_ring_free(xmm);
   2300 	KASSERT(xmm->cp == NULL);
   2301 }
   2302 
   2303 static void
   2304 wwanc_resume(struct device *self)
   2305 {
   2306 	struct wwanc_softc *sc = device_private(self);
   2307 	struct xmm_dev *xmm = &sc->sc_xmm;
   2308 	struct queue_pair *qp;
   2309 
   2310 	KASSERT(xmm->cp == NULL);
   2311 
   2312 	xmm7360_base_init(xmm);
   2313 
   2314 	for (int i = 0; i < XMM_QP_COUNT; i++) {
   2315 		qp = &xmm->qp[i];
   2316 		if (qp->open)
   2317 			xmm7360_qp_resume(qp);
   2318 	}
   2319 }
   2320 
   2321 #ifdef __OpenBSD__
   2322 
   2323 static void
   2324 wwanc_defer_resume(void *xarg)
   2325 {
   2326 	struct device *self = xarg;
   2327 	struct wwanc_softc *sc = device_private(self);
   2328 
   2329 	tsleep(&sc->sc_resume, 0, "wwancdr", 2 * hz);
   2330 
   2331 	wwanc_resume(self);
   2332 
   2333 	(void)config_activate_children(self, DVACT_RESUME);
   2334 
   2335 	sc->sc_resume = false;
   2336 	kthread_exit(0);
   2337 }
   2338 
   2339 static int
   2340 wwanc_activate(struct device *self, int act)
   2341 {
   2342 	struct wwanc_softc *sc = device_private(self);
   2343 
   2344 	switch (act) {
   2345 	case DVACT_QUIESCE:
   2346 		(void)config_activate_children(self, act);
   2347 		break;
   2348 	case DVACT_SUSPEND:
   2349 		if (sc->sc_resume) {
   2350 			/* Refuse to suspend if resume still ongoing */
   2351 			printf("%s: not suspending, resume still ongoing\n",
   2352 			    self->dv_xname);
   2353 			return EBUSY;
   2354 		}
   2355 
   2356 		(void)config_activate_children(self, act);
   2357 		wwanc_suspend(self);
   2358 		break;
   2359 	case DVACT_RESUME:
   2360 		/*
   2361 		 * Modem reinitialization can take several seconds, defer
   2362 		 * it via kernel thread to avoid blocking the resume.
   2363 		 */
   2364 		sc->sc_resume = true;
   2365 		kthread_create(wwanc_defer_resume, self, NULL, "wwancres");
   2366 		break;
   2367 	default:
   2368 		break;
   2369 	}
   2370 
   2371 	return 0;
   2372 }
   2373 
   2374 cdev_decl(wwanc);
   2375 #endif /* __OpenBSD__ */
   2376 
   2377 #ifdef __NetBSD__
   2378 static bool
   2379 wwanc_pmf_suspend(device_t self, const pmf_qual_t *qual)
   2380 {
   2381 	wwanc_suspend(self);
   2382 	return true;
   2383 }
   2384 
   2385 static bool
   2386 wwanc_pmf_resume(device_t self, const pmf_qual_t *qual)
   2387 {
   2388 	wwanc_resume(self);
   2389 	return true;
   2390 }
   2391 
   2392 static dev_type_open(wwancopen);
   2393 static dev_type_close(wwancclose);
   2394 static dev_type_read(wwancread);
   2395 static dev_type_write(wwancwrite);
   2396 static dev_type_ioctl(wwancioctl);
   2397 static dev_type_poll(wwancpoll);
   2398 static dev_type_kqfilter(wwanckqfilter);
   2399 static dev_type_tty(wwanctty);
   2400 
   2401 const struct cdevsw wwanc_cdevsw = {
   2402 	.d_open = wwancopen,
   2403 	.d_close = wwancclose,
   2404 	.d_read = wwancread,
   2405 	.d_write = wwancwrite,
   2406 	.d_ioctl = wwancioctl,
   2407 	.d_stop = nullstop,
   2408 	.d_tty = wwanctty,
   2409 	.d_poll = wwancpoll,
   2410 	.d_mmap = nommap,
   2411 	.d_kqfilter = wwanckqfilter,
   2412 	.d_discard = nodiscard,
   2413 	.d_flag = D_TTY
   2414 };
   2415 #endif
   2416 
   2417 static int wwancparam(struct tty *, struct termios *);
   2418 static void wwancstart(struct tty *);
   2419 
   2420 static void xmm7360_os_handle_tty_idata(struct queue_pair *qp, const u8 *data, size_t nread)
   2421 {
   2422 	struct xmm_dev *xmm = qp->xmm;
   2423 	struct wwanc_softc *sc = device_private(xmm->dev);
   2424 	int func = qp->num;
   2425 	struct tty *tp = sc->sc_tty[func];
   2426 
   2427 	KASSERT(DEV_IS_TTY(func));
   2428 	KASSERT(tp);
   2429 
   2430 	for (int i = 0; i < nread; i++)
   2431 		LINESW(tp).l_rint(data[i], tp);
   2432 }
   2433 
   2434 int
   2435 wwancopen(dev_t dev, int flags, int mode, struct proc *p)
   2436 {
   2437 	int unit = DEVUNIT(dev);
   2438 	struct wwanc_softc *sc = device_lookup_private(&wwanc_cd, unit);
   2439 	struct tty *tp;
   2440 	int func, error;
   2441 
   2442 	if (sc == NULL)
   2443 		return ENXIO;
   2444 
   2445 	/* Only allow opening the rpc/trace/AT queue pairs */
   2446 	func = DEVFUNC(dev);
   2447 	if (func < 1 || func > 7)
   2448 		return ENXIO;
   2449 
   2450 	if (DEV_IS_TTY(dev)) {
   2451 		if (!sc->sc_tty[func]) {
   2452 			tp = sc->sc_tty[func] = ttymalloc(1000000);
   2453 
   2454 			tp->t_oproc = wwancstart;
   2455 		        tp->t_param = wwancparam;
   2456 			tp->t_dev = dev;
   2457 			tp->t_sc = (void *)sc;
   2458 		} else
   2459 			tp = sc->sc_tty[func];
   2460 
   2461 		if (!ISSET(tp->t_state, TS_ISOPEN)) {
   2462 			ttychars(tp);
   2463 			tp->t_iflag = TTYDEF_IFLAG;
   2464 			tp->t_oflag = TTYDEF_OFLAG;
   2465 			tp->t_lflag = TTYDEF_LFLAG;
   2466 			tp->t_cflag = TTYDEF_CFLAG;
   2467 			tp->t_ispeed = tp->t_ospeed = B115200;
   2468 			SET(tp->t_cflag, CS8 | CREAD | HUPCL | CLOCAL);
   2469 
   2470 			SET(tp->t_state, TS_CARR_ON);
   2471 		} else if (suser(p) != 0) {
   2472 			return EBUSY;
   2473 		}
   2474 
   2475 		error = LINESW(tp).l_open(dev, tp, p);
   2476 		if (error)
   2477 			return error;
   2478 	}
   2479 
   2480 	/* Initialize ring if qp not open yet */
   2481 	xmm7360_qp_start(&sc->sc_xmm.qp[func]);
   2482 
   2483 	return 0;
   2484 }
   2485 
   2486 int
   2487 wwancread(dev_t dev, struct uio *uio, int flag)
   2488 {
   2489 	struct wwanc_softc *sc = device_lookup_private(&wwanc_cd, DEVUNIT(dev));
   2490 	int func = DEVFUNC(dev);
   2491 
   2492 	KASSERT(sc != NULL);
   2493 
   2494 	if (DEV_IS_TTY(dev)) {
   2495 		struct tty *tp = sc->sc_tty[func];
   2496 
   2497 		return (LINESW(tp).l_read(tp, uio, flag));
   2498 	} else {
   2499 		struct queue_pair *qp = &sc->sc_xmm.qp[func];
   2500 		ssize_t ret;
   2501 		char *buf;
   2502 		size_t size, read = 0;
   2503 
   2504 #ifdef __OpenBSD__
   2505 		KASSERT(uio->uio_segflg == UIO_USERSPACE);
   2506 #endif
   2507 
   2508 		for (int i = 0; i < uio->uio_iovcnt; i++) {
   2509 			buf = uio->uio_iov[i].iov_base;
   2510 			size = uio->uio_iov[i].iov_len;
   2511 
   2512 			while (size > 0) {
   2513 				ret = xmm7360_qp_read_user(qp, buf, size);
   2514 				if (ret < 0) {
   2515 					/*
   2516 					 * This shadows -EPERM, but that is
   2517 					 * not returned by the call stack,
   2518 					 * so this condition is safe.
   2519 					 */
   2520 					return (ret == ERESTART) ? ret : -ret;
   2521 				}
   2522 
   2523 				KASSERT(ret > 0 && ret <= size);
   2524 				size -= ret;
   2525 				buf += ret;
   2526 				read += ret;
   2527 
   2528 				/* Reader will re-try if they want more */
   2529 				goto out;
   2530 			}
   2531 		}
   2532 
   2533 out:
   2534 		uio->uio_resid -= read;
   2535 		uio->uio_offset += read;
   2536 
   2537 		return 0;
   2538 	}
   2539 }
   2540 
   2541 int
   2542 wwancwrite(dev_t dev, struct uio *uio, int flag)
   2543 {
   2544 	struct wwanc_softc *sc = device_lookup_private(&wwanc_cd, DEVUNIT(dev));
   2545 	int func = DEVFUNC(dev);
   2546 
   2547 	if (DEV_IS_TTY(dev)) {
   2548 		struct tty *tp = sc->sc_tty[func];
   2549 
   2550 		return (LINESW(tp).l_write(tp, uio, flag));
   2551 	} else {
   2552 		struct queue_pair *qp = &sc->sc_xmm.qp[func];
   2553 		ssize_t ret;
   2554 		const char *buf;
   2555 		size_t size, wrote = 0;
   2556 
   2557 #ifdef __OpenBSD__
   2558 		KASSERT(uio->uio_segflg == UIO_USERSPACE);
   2559 #endif
   2560 
   2561 		for (int i = 0; i < uio->uio_iovcnt; i++) {
   2562 			buf = uio->uio_iov[i].iov_base;
   2563 			size = uio->uio_iov[i].iov_len;
   2564 
   2565 			while (size > 0) {
   2566 				ret = xmm7360_qp_write_user(qp, buf, size);
   2567 				if (ret < 0) {
   2568 					/*
   2569 					 * This shadows -EPERM, but that is
   2570 					 * not returned by the call stack,
   2571 					 * so this condition is safe.
   2572 					 */
   2573 					return (ret == ERESTART) ? ret : -ret;
   2574 				}
   2575 
   2576 				KASSERT(ret > 0 && ret <= size);
   2577 				size -= ret;
   2578 				buf += ret;
   2579 				wrote += ret;
   2580 			}
   2581 		}
   2582 
   2583 		uio->uio_resid -= wrote;
   2584 		uio->uio_offset += wrote;
   2585 
   2586 		return 0;
   2587 	}
   2588 }
   2589 
   2590 int
   2591 wwancioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
   2592 {
   2593 	struct wwanc_softc *sc = device_lookup_private(&wwanc_cd, DEVUNIT(dev));
   2594 	int error;
   2595 
   2596 	if (DEV_IS_TTY(dev)) {
   2597 		struct tty *tp = sc->sc_tty[DEVFUNC(dev)];
   2598 		KASSERT(tp);
   2599 
   2600 		error = LINESW(tp).l_ioctl(tp, cmd, data, flag, p);
   2601 		if (error >= 0)
   2602 			return error;
   2603 		error = ttioctl(tp, cmd, data, flag, p);
   2604 		if (error >= 0)
   2605 			return error;
   2606 	}
   2607 
   2608 	return ENOTTY;
   2609 }
   2610 
   2611 int
   2612 wwancclose(dev_t dev, int flag, int mode, struct proc *p)
   2613 {
   2614 	struct wwanc_softc *sc = device_lookup_private(&wwanc_cd, DEVUNIT(dev));
   2615 	int func = DEVFUNC(dev);
   2616 
   2617 	if (DEV_IS_TTY(dev)) {
   2618 		struct tty *tp = sc->sc_tty[func];
   2619 		KASSERT(tp);
   2620 
   2621 		CLR(tp->t_state, TS_BUSY | TS_FLUSH);
   2622 		LINESW(tp).l_close(tp, flag, p);
   2623 		ttyclose(tp);
   2624 	}
   2625 
   2626 	xmm7360_qp_stop(&sc->sc_xmm.qp[func]);
   2627 
   2628 	return 0;
   2629 }
   2630 
   2631 struct tty *
   2632 wwanctty(dev_t dev)
   2633 {
   2634 	struct wwanc_softc *sc = device_lookup_private(&wwanc_cd, DEVUNIT(dev));
   2635 	struct tty *tp = sc->sc_tty[DEVFUNC(dev)];
   2636 
   2637 	KASSERT(DEV_IS_TTY(dev));
   2638 	KASSERT(tp);
   2639 
   2640 	return tp;
   2641 }
   2642 
   2643 static int
   2644 wwancparam(struct tty *tp, struct termios *t)
   2645 {
   2646 	struct wwanc_softc *sc = (struct wwanc_softc *)tp->t_sc;
   2647 	dev_t dev = tp->t_dev;
   2648 	int func = DEVFUNC(dev);
   2649 
   2650 	KASSERT(DEV_IS_TTY(dev));
   2651 	KASSERT(tp == sc->sc_tty[func]);
   2652 	/* Can't assert tty_locked(), it's not taken when called via ttioctl()*/
   2653 
   2654 	/* Nothing to set on hardware side, just copy values */
   2655 	tp->t_ispeed = t->c_ispeed;
   2656 	tp->t_ospeed = t->c_ospeed;
   2657 	tp->t_cflag = t->c_cflag;
   2658 
   2659 	return 0;
   2660 }
   2661 
   2662 static void
   2663 wwancstart(struct tty *tp)
   2664 {
   2665 	struct wwanc_softc *sc = (struct wwanc_softc *)tp->t_sc;
   2666 	dev_t dev = tp->t_dev;
   2667 	int func = DEVFUNC(dev);
   2668 	struct queue_pair *qp = &sc->sc_xmm.qp[func];
   2669 	int n, written;
   2670 
   2671 	KASSERT(DEV_IS_TTY(dev));
   2672 	KASSERT(tp == sc->sc_tty[func]);
   2673 	tty_locked();
   2674 
   2675 	if (ISSET(tp->t_state, TS_BUSY) || !xmm7360_qp_can_write(qp))
   2676 		return;
   2677 	if (tp->t_outq.c_cc == 0)
   2678 		return;
   2679 
   2680 	/*
   2681 	 * If we can write, we can write full qb page_size amount of data.
   2682 	 * Once q_to_b() is called, the data must be trasmitted - q_to_b()
   2683 	 * removes them from the tty output queue. Partial write is not
   2684 	 * possible.
   2685 	 */
   2686 	KASSERT(sizeof(qp->user_buf) >= qp->page_size);
   2687 	SET(tp->t_state, TS_BUSY);
   2688 	n = q_to_b(&tp->t_outq, qp->user_buf, qp->page_size);
   2689 	KASSERT(n > 0);
   2690 	KASSERT(n <= qp->page_size);
   2691 	written = xmm7360_qp_write(qp, qp->user_buf, n);
   2692 	CLR(tp->t_state, TS_BUSY);
   2693 
   2694 	if (written != n) {
   2695 		dev_err(sc->sc_dev, "xmm7360_qp_write(%d) failed %d != %d\n",
   2696 		    func, written, n);
   2697 		/* nothing to recover, just return */
   2698 	}
   2699 }
   2700 
   2701 int
   2702 wwancpoll(dev_t dev, int events, struct proc *p)
   2703 {
   2704 	struct wwanc_softc *sc = device_lookup_private(&wwanc_cd, DEVUNIT(dev));
   2705 	int func = DEVFUNC(dev);
   2706 	struct queue_pair *qp = &sc->sc_xmm.qp[func];
   2707 	int mask = 0;
   2708 
   2709 	if (DEV_IS_TTY(dev)) {
   2710 #ifdef __OpenBSD__
   2711 		return ttpoll(dev, events, p);
   2712 #endif
   2713 #ifdef __NetBSD__
   2714 		struct tty *tp = sc->sc_tty[func];
   2715 
   2716 		return LINESW(tp).l_poll(tp, events, p);
   2717 #endif
   2718 	}
   2719 
   2720 	KASSERT(!DEV_IS_TTY(dev));
   2721 
   2722 	if (qp->xmm->error) {
   2723 		mask |= POLLHUP;
   2724 		goto out;
   2725 	}
   2726 
   2727 	if (xmm7360_qp_has_data(qp))
   2728 		mask |= POLLIN | POLLRDNORM;
   2729 
   2730 	if (xmm7360_qp_can_write(qp))
   2731 		mask |= POLLOUT | POLLWRNORM;
   2732 
   2733 out:
   2734 	if ((mask & events) == 0) {
   2735 		if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND))
   2736 			selrecord(p, &sc->sc_selr);
   2737                 if (events & (POLLOUT | POLLWRNORM))
   2738                         selrecord(p, &sc->sc_selw);
   2739 	}
   2740 
   2741 	return mask & events;
   2742 }
   2743 
   2744 static void
   2745 filt_wwancrdetach(struct knote *kn)
   2746 {
   2747 	struct queue_pair *qp = (struct queue_pair *)kn->kn_hook;
   2748 
   2749 	tty_lock();
   2750 	klist_remove(&qp->selr.si_note, kn);
   2751 	tty_unlock();
   2752 }
   2753 
   2754 static int
   2755 filt_wwancread(struct knote *kn, long hint)
   2756 {
   2757 	struct queue_pair *qp = (struct queue_pair *)kn->kn_hook;
   2758 
   2759 	kn->kn_data = 0;
   2760 
   2761 	if (!qp->open) {
   2762 		kn->kn_flags |= EV_EOF;
   2763 		return (1);
   2764 	} else {
   2765 		kn->kn_data = xmm7360_qp_has_data(qp) ? 1 : 0;
   2766 	}
   2767 
   2768 	return (kn->kn_data > 0);
   2769 }
   2770 
   2771 static void
   2772 filt_wwancwdetach(struct knote *kn)
   2773 {
   2774 	struct queue_pair *qp = (struct queue_pair *)kn->kn_hook;
   2775 
   2776 	tty_lock();
   2777 	klist_remove(&qp->selw.si_note, kn);
   2778 	tty_unlock();
   2779 }
   2780 
   2781 static int
   2782 filt_wwancwrite(struct knote *kn, long hint)
   2783 {
   2784 	struct queue_pair *qp = (struct queue_pair *)kn->kn_hook;
   2785 
   2786 	kn->kn_data = 0;
   2787 
   2788 	if (qp->open) {
   2789 		if (xmm7360_qp_can_write(qp))
   2790 			kn->kn_data = qp->page_size;
   2791 	}
   2792 
   2793 	return (kn->kn_data > 0);
   2794 }
   2795 
   2796 static const struct filterops wwancread_filtops = {
   2797 	XMM_KQ_ISFD_INITIALIZER,
   2798 	.f_attach	= NULL,
   2799 	.f_detach	= filt_wwancrdetach,
   2800 	.f_event	= filt_wwancread,
   2801 };
   2802 
   2803 static const struct filterops wwancwrite_filtops = {
   2804 	XMM_KQ_ISFD_INITIALIZER,
   2805 	.f_attach	= NULL,
   2806 	.f_detach	= filt_wwancwdetach,
   2807 	.f_event	= filt_wwancwrite,
   2808 };
   2809 
   2810 int
   2811 wwanckqfilter(dev_t dev, struct knote *kn)
   2812 {
   2813 	struct wwanc_softc *sc = device_lookup_private(&wwanc_cd, DEVUNIT(dev));
   2814 	int func = DEVFUNC(dev);
   2815 	struct queue_pair *qp = &sc->sc_xmm.qp[func];
   2816 	struct klist *klist;
   2817 
   2818 	if (DEV_IS_TTY(func))
   2819 		return ttkqfilter(dev, kn);
   2820 
   2821 	KASSERT(!DEV_IS_TTY(func));
   2822 
   2823 	switch (kn->kn_filter) {
   2824 	case EVFILT_READ:
   2825 		klist = &qp->selr.si_note;
   2826 		kn->kn_fop = &wwancread_filtops;
   2827 		break;
   2828 	case EVFILT_WRITE:
   2829 		klist = &qp->selw.si_note;
   2830 		kn->kn_fop = &wwancwrite_filtops;
   2831 		break;
   2832 	default:
   2833 		return (EINVAL);
   2834 	}
   2835 
   2836 	kn->kn_hook = (void *)qp;
   2837 
   2838 	tty_lock();
   2839 	klist_insert(klist, kn);
   2840 	tty_unlock();
   2841 
   2842 	return (0);
   2843 }
   2844 
   2845 static void *
   2846 dma_alloc_coherent(struct device *self, size_t sz, dma_addr_t *physp, int flags)
   2847 {
   2848 	struct wwanc_softc *sc = device_private(self);
   2849 	bus_dma_segment_t seg;
   2850 	int nsegs;
   2851 	int error;
   2852 	caddr_t kva;
   2853 
   2854 	error = bus_dmamem_alloc(sc->sc_dmat, sz, 0, 0, &seg, 1, &nsegs,
   2855 	    BUS_DMA_WAITOK);
   2856 	if (error) {
   2857 		panic("%s: bus_dmamem_alloc(%lu) failed %d\n",
   2858 		    self->dv_xname, (unsigned long)sz, error);
   2859 		/* NOTREACHED */
   2860 	}
   2861 
   2862 	KASSERT(nsegs == 1);
   2863 	KASSERT(seg.ds_len == round_page(sz));
   2864 
   2865 	error = bus_dmamem_map(sc->sc_dmat, &seg, nsegs, sz, &kva,
   2866 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT);
   2867 	if (error) {
   2868 		panic("%s: bus_dmamem_alloc(%lu) failed %d\n",
   2869 		    self->dv_xname, (unsigned long)sz, error);
   2870 		/* NOTREACHED */
   2871 	}
   2872 
   2873 	memset(kva, 0, sz);
   2874 	*physp = seg.ds_addr;
   2875 	return (void *)kva;
   2876 }
   2877 
   2878 static void
   2879 dma_free_coherent(struct device *self, size_t sz, volatile void *vaddr, dma_addr_t phys)
   2880 {
   2881 	struct wwanc_softc *sc = device_private(self);
   2882 	bus_dma_segment_t seg;
   2883 
   2884 	sz = round_page(sz);
   2885 
   2886 	bus_dmamem_unmap(sc->sc_dmat, __UNVOLATILE(vaddr), sz);
   2887 
   2888 	/* this does't need the exact seg returned by bus_dmamem_alloc() */
   2889 	memset(&seg, 0, sizeof(seg));
   2890 	seg.ds_addr = phys;
   2891 	seg.ds_len  = sz;
   2892 	bus_dmamem_free(sc->sc_dmat, &seg, 1);
   2893 }
   2894 
   2895 struct wwan_softc {
   2896 #ifdef __OpenBSD__
   2897 	struct device		sc_devx;	/* gen. device info storage */
   2898 #endif
   2899 	struct device		*sc_dev;	/* generic device */
   2900 	struct wwanc_softc	*sc_parent;	/* parent device */
   2901 	struct ifnet		sc_ifnet;	/* network-visible interface */
   2902 	struct xmm_net		sc_xmm_net;
   2903 };
   2904 
   2905 static void xmm7360_os_handle_net_frame(struct xmm_dev *xmm, const u8 *buf, size_t sz)
   2906 {
   2907 	struct wwanc_softc *sc = device_private(xmm->dev);
   2908 	struct wwan_softc *sc_if = device_private(sc->sc_net);
   2909 	struct ifnet *ifp = &sc_if->sc_ifnet;
   2910 	struct mbuf *m;
   2911 
   2912 	KASSERT(sz <= MCLBYTES);
   2913 
   2914 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   2915 	if (!m)
   2916 		return;
   2917 	if (sz > MHLEN) {
   2918 		MCLGETI(m, M_DONTWAIT, NULL, sz);
   2919 		if ((m->m_flags & M_EXT) == 0) {
   2920 			m_freem(m);
   2921 			return;
   2922 		}
   2923 	}
   2924 	m->m_len = m->m_pkthdr.len = sz;
   2925 
   2926 	/*
   2927 	 * No explicit alignment necessary - there is no ethernet header,
   2928 	 * so IP address is already aligned.
   2929 	 */
   2930 	KASSERT(m->m_pkthdr.len == sz);
   2931 	m_copyback(m, 0, sz, (const void *)buf, M_NOWAIT);
   2932 
   2933 #ifdef __OpenBSD__
   2934 	struct mbuf_list ml = MBUF_LIST_INITIALIZER();
   2935 	ml_enqueue(&ml, m);
   2936 	if_input(ifp, &ml);
   2937 #endif
   2938 #ifdef __NetBSD__
   2939 	if_percpuq_enqueue(ifp->if_percpuq, m);
   2940 #endif
   2941 }
   2942 
   2943 static void
   2944 xmm7360_os_handle_net_dequeue(struct xmm_net *xn, struct mux_frame *frame)
   2945 {
   2946 	struct wwan_softc *sc_if =
   2947 		container_of(xn, struct wwan_softc, sc_xmm_net);
   2948 	struct ifnet *ifp = &sc_if->sc_ifnet;
   2949 	struct mbuf *m;
   2950 	int ret;
   2951 
   2952 	MUTEX_ASSERT_LOCKED(&xn->lock);
   2953 
   2954 	while ((m = ifq_deq_begin(&ifp->if_snd))) {
   2955 		/*
   2956 		 * xmm7360_mux_frame_append_packet() requires single linear
   2957 		 * buffer, so try m_defrag(). Another option would be
   2958 		 * using m_copydata() into an intermediate buffer.
   2959 		 */
   2960 		if (m->m_next) {
   2961 			if (m_defrag(m, M_DONTWAIT) != 0 || m->m_next) {
   2962 				/* Can't defrag, drop and continue */
   2963 				ifq_deq_commit(&ifp->if_snd, m);
   2964 				m_freem(m);
   2965 				continue;
   2966 			}
   2967 		}
   2968 
   2969 		ret = xmm7360_mux_frame_append_packet(frame,
   2970 		    mtod(m, void *), m->m_pkthdr.len);
   2971 		if (ret) {
   2972 			/* No more space in the frame */
   2973 			ifq_deq_rollback(&ifp->if_snd, m);
   2974 			break;
   2975 		}
   2976 		ifq_deq_commit(&ifp->if_snd, m);
   2977 
   2978 		/* Send a copy of the frame to the BPF listener */
   2979 		BPF_MTAP_OUT(ifp, m);
   2980 
   2981 		m_freem(m);
   2982 	}
   2983 }
   2984 
   2985 static void xmm7360_os_handle_net_txwake(struct xmm_net *xn)
   2986 {
   2987 	struct wwan_softc *sc_if =
   2988 		container_of(xn, struct wwan_softc, sc_xmm_net);
   2989 	struct ifnet *ifp = &sc_if->sc_ifnet;
   2990 
   2991 	MUTEX_ASSERT_LOCKED(&xn->lock);
   2992 
   2993 	KASSERT(xmm7360_qp_can_write(xn->qp));
   2994 	if (ifq_is_oactive(&ifp->if_snd)) {
   2995 		ifq_clr_oactive(&ifp->if_snd);
   2996 #ifdef __OpenBSD__
   2997 		ifq_restart(&ifp->if_snd);
   2998 #endif
   2999 #ifdef __NetBSD__
   3000 		if_schedule_deferred_start(ifp);
   3001 #endif
   3002 	}
   3003 }
   3004 
   3005 #ifdef __OpenBSD__
   3006 /*
   3007  * Process received raw IPv4/IPv6 packet. There is no encapsulation.
   3008  */
   3009 static int
   3010 wwan_if_input(struct ifnet *ifp, struct mbuf *m, void *cookie)
   3011 {
   3012 	const uint8_t *data = mtod(m, uint8_t *);
   3013 	void (*input)(struct ifnet *, struct mbuf *);
   3014 	u8 ip_version;
   3015 
   3016 	ip_version = data[0] >> 4;
   3017 
   3018 	switch (ip_version) {
   3019 	case IPVERSION:
   3020 		input = ipv4_input;
   3021 		break;
   3022 	case (IPV6_VERSION >> 4):
   3023 		input = ipv6_input;
   3024 		break;
   3025 	default:
   3026 		/* Unknown protocol, just drop packet */
   3027 		m_freem(m);
   3028 		return 1;
   3029 		/* NOTREACHED */
   3030 	}
   3031 
   3032 	(*input)(ifp, m);
   3033 	return 1;
   3034 }
   3035 #endif /* __OpenBSD__ */
   3036 
   3037 #ifdef __NetBSD__
   3038 static bool wwan_pmf_suspend(device_t, const pmf_qual_t *);
   3039 
   3040 /*
   3041  * Process received raw IPv4/IPv6 packet. There is no encapsulation.
   3042  */
   3043 static void
   3044 wwan_if_input(struct ifnet *ifp, struct mbuf *m)
   3045 {
   3046 	const uint8_t *data = mtod(m, uint8_t *);
   3047 	pktqueue_t *pktq = NULL;
   3048 	u8 ip_version;
   3049 
   3050 	KASSERT(!cpu_intr_p());
   3051 	KASSERT((m->m_flags & M_PKTHDR) != 0);
   3052 
   3053 	if ((ifp->if_flags & IFF_UP) == 0) {
   3054 		m_freem(m);
   3055 		return;
   3056 	}
   3057 
   3058 	if_statadd(ifp, if_ibytes, m->m_pkthdr.len);
   3059 
   3060 	/*
   3061 	 * The interface can't receive packets for other host, so never
   3062 	 * really IFF_PROMISC even if bpf listener is attached.
   3063 	 */
   3064 	if (pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
   3065 		return;
   3066 	if (m == NULL)
   3067 		return;
   3068 
   3069 	ip_version = data[0] >> 4;
   3070 	switch (ip_version) {
   3071 #ifdef INET
   3072 	case IPVERSION:
   3073 #ifdef GATEWAY
   3074 		if (ipflow_fastforward(m))
   3075 			return;
   3076 #endif
   3077 		pktq = ip_pktq;
   3078 		break;
   3079 #endif /* INET */
   3080 #ifdef INET6
   3081 	case (IPV6_VERSION >> 4):
   3082 		if (__predict_false(!in6_present)) {
   3083 			m_freem(m);
   3084 			return;
   3085 		}
   3086 #ifdef GATEWAY
   3087 		if (ip6flow_fastforward(&m))
   3088 			return;
   3089 #endif
   3090 		pktq = ip6_pktq;
   3091 		break;
   3092 #endif /* INET6 */
   3093 	default:
   3094 		/* Unknown protocol, just drop packet */
   3095 		m_freem(m);
   3096 		return;
   3097 		/* NOTREACHED */
   3098 	}
   3099 
   3100 	KASSERT(pktq != NULL);
   3101 
   3102 	/* No errors.  Receive the packet. */
   3103 	m_set_rcvif(m, ifp);
   3104 
   3105 #ifdef NET_MPSAFE
   3106 	const u_int h = curcpu()->ci_index;
   3107 #else
   3108 	const uint32_t h = pktq_rps_hash(m);
   3109 #endif
   3110 	if (__predict_false(!pktq_enqueue(pktq, m, h))) {
   3111 		m_freem(m);
   3112 	}
   3113 }
   3114 #endif
   3115 
   3116 /*
   3117  * Transmit raw IPv4/IPv6 packet. No encapsulation necessary.
   3118  */
   3119 static int
   3120 wwan_if_output(struct ifnet *ifp, struct mbuf *m,
   3121     IF_OUTPUT_CONST struct sockaddr *dst, IF_OUTPUT_CONST struct rtentry *rt)
   3122 {
   3123 	// there is no ethernet frame, this means no bridge(4) handling
   3124 	return (if_enqueue(ifp, m));
   3125 }
   3126 
   3127 static int
   3128 wwan_if_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
   3129 {
   3130 	struct wwan_softc *sc_if = ifp->if_softc;
   3131 	int error = 0;
   3132 	int s;
   3133 
   3134 	s = splnet();
   3135 
   3136 	switch (cmd) {
   3137 #ifdef __NetBSD__
   3138 	case SIOCINITIFADDR:
   3139 #endif
   3140 #ifdef __OpenBSD__
   3141 	case SIOCAIFADDR:
   3142 	case SIOCAIFADDR_IN6:
   3143 	case SIOCSIFADDR:
   3144 #endif
   3145 		/* Make interface ready to run if address is assigned */
   3146 		ifp->if_flags |= IFF_UP;
   3147 		if (!(ifp->if_flags & IFF_RUNNING)) {
   3148 			ifp->if_flags |= IFF_RUNNING;
   3149 			xmm7360_mux_control(&sc_if->sc_xmm_net, 1, 0, 0, 0);
   3150 		}
   3151 		break;
   3152 	case SIOCSIFFLAGS:
   3153 	case SIOCADDMULTI:
   3154 	case SIOCDELMULTI:
   3155 		/* nothing special to do */
   3156 		break;
   3157 	case SIOCSIFMTU:
   3158 		error = ENOTTY;
   3159 		break;
   3160 	default:
   3161 #ifdef __NetBSD__
   3162 		/*
   3163 		 * Call common code for SIOCG* ioctls. In OpenBSD those ioctls
   3164 		 * are handled in ifioctl(), and the if_ioctl is not called
   3165 		 * for them at all.
   3166 		 */
   3167 		error = ifioctl_common(ifp, cmd, data);
   3168 		if (error == ENETRESET)
   3169 			error = 0;
   3170 #endif
   3171 #ifdef __OpenBSD__
   3172 		error = ENOTTY;
   3173 #endif
   3174 		break;
   3175 	}
   3176 
   3177 	splx(s);
   3178 
   3179 	return error;
   3180 }
   3181 
   3182 static void
   3183 wwan_if_start(struct ifnet *ifp)
   3184 {
   3185 	struct wwan_softc *sc = ifp->if_softc;
   3186 
   3187 	mutex_lock(&sc->sc_xmm_net.lock);
   3188 	while (!ifq_empty(&ifp->if_snd)) {
   3189 		if (!xmm7360_qp_can_write(sc->sc_xmm_net.qp)) {
   3190 			break;
   3191 		}
   3192 		xmm7360_net_flush(&sc->sc_xmm_net);
   3193 	}
   3194 	mutex_unlock(&sc->sc_xmm_net.lock);
   3195 }
   3196 
   3197 static int
   3198 wwan_match(struct device *parent, cfdata_t match, void *aux)
   3199 {
   3200 	struct wwanc_attach_args *wa = aux;
   3201 
   3202 	return (wa->aa_type == WWMC_TYPE_NET);
   3203 }
   3204 
   3205 static void
   3206 wwan_attach(struct device *parent, struct device *self, void *aux)
   3207 {
   3208 	struct wwan_softc *sc_if = device_private(self);
   3209 	struct ifnet *ifp = &sc_if->sc_ifnet;
   3210 	struct xmm_dev *xmm;
   3211 	struct xmm_net *xn;
   3212 
   3213 	sc_if->sc_dev = self;
   3214 	sc_if->sc_parent = device_private(parent);
   3215 	xmm = sc_if->sc_xmm_net.xmm = &sc_if->sc_parent->sc_xmm;
   3216 	xn = &sc_if->sc_xmm_net;
   3217 	mutex_init(&xn->lock);
   3218 
   3219 	/* QP already initialized in parent, just set pointers and start */
   3220 	xn->qp = &xmm->qp[0];
   3221 	xmm7360_qp_start(xn->qp);
   3222 	xmm->net = xn;
   3223 
   3224 	ifp->if_softc = sc_if;
   3225 	ifp->if_flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST \
   3226 		| IFF_SIMPLEX;
   3227 	ifp->if_ioctl = wwan_if_ioctl;
   3228 	ifp->if_start = wwan_if_start;
   3229 	ifp->if_mtu = 1500;
   3230 	ifp->if_hardmtu = 1500;
   3231 	ifp->if_type = IFT_OTHER;
   3232 	IFQ_SET_MAXLEN(&ifp->if_snd, xn->qp->depth);
   3233 	IFQ_SET_READY(&ifp->if_snd);
   3234 	bcopy(sc_if->sc_dev->dv_xname, ifp->if_xname, IFNAMSIZ);
   3235 
   3236 	/* Call MI attach routines. */
   3237 	if_attach(ifp);
   3238 
   3239 	/* Hook custom input and output processing, and dummy sadl */
   3240 	ifp->if_output = wwan_if_output;
   3241 	if_ih_insert(ifp, wwan_if_input, NULL);
   3242 	if_deferred_start_init(ifp, NULL);
   3243 	if_alloc_sadl(ifp);
   3244 #if NBPFILTER > 0
   3245 	bpfattach(&ifp->if_bpf, ifp, DLT_RAW, 0);
   3246 #endif
   3247 
   3248 	printf("\n");
   3249 
   3250 #ifdef __NetBSD__
   3251 	if (pmf_device_register(self, wwan_pmf_suspend, NULL))
   3252 		pmf_class_network_register(self, ifp);
   3253 	else
   3254 		aprint_error_dev(self, "couldn't establish power handler\n");
   3255 #endif
   3256 }
   3257 
   3258 static int
   3259 wwan_detach(struct device *self, int flags)
   3260 {
   3261 	struct wwan_softc *sc_if = device_private(self);
   3262 	struct ifnet *ifp = &sc_if->sc_ifnet;
   3263 
   3264 	if (ifp->if_flags & (IFF_UP|IFF_RUNNING))
   3265 		ifp->if_flags &= ~(IFF_UP|IFF_RUNNING);
   3266 
   3267 	pmf_device_deregister(self);
   3268 
   3269 	if_ih_remove(ifp, wwan_if_input, NULL);
   3270 	if_detach(ifp);
   3271 
   3272 	xmm7360_qp_stop(sc_if->sc_xmm_net.qp);
   3273 
   3274 	sc_if->sc_xmm_net.xmm->net = NULL;
   3275 
   3276 	return 0;
   3277 }
   3278 
   3279 static void
   3280 wwan_suspend(struct device *self)
   3281 {
   3282 	struct wwan_softc *sc_if = device_private(self);
   3283 	struct ifnet *ifp = &sc_if->sc_ifnet;
   3284 
   3285 	/*
   3286 	 * Interface is marked down on suspend, and needs to be reconfigured
   3287 	 * after resume.
   3288 	 */
   3289 	if (ifp->if_flags & (IFF_UP|IFF_RUNNING))
   3290 		ifp->if_flags &= ~(IFF_UP|IFF_RUNNING);
   3291 
   3292 	ifq_purge(&ifp->if_snd);
   3293 }
   3294 
   3295 #ifdef __OpenBSD__
   3296 static int
   3297 wwan_activate(struct device *self, int act)
   3298 {
   3299 	switch (act) {
   3300 	case DVACT_QUIESCE:
   3301 	case DVACT_SUSPEND:
   3302 		wwan_suspend(self);
   3303 		break;
   3304 	case DVACT_RESUME:
   3305 		/* Nothing to do */
   3306 		break;
   3307 	}
   3308 
   3309 	return 0;
   3310 }
   3311 
   3312 struct cfattach wwan_ca = {
   3313         sizeof(struct wwan_softc), wwan_match, wwan_attach,
   3314         wwan_detach, wwan_activate
   3315 };
   3316 
   3317 struct cfdriver wwan_cd = {
   3318         NULL, "wwan", DV_IFNET
   3319 };
   3320 #endif /* __OpenBSD__ */
   3321 
   3322 #ifdef __NetBSD__
   3323 static bool
   3324 wwan_pmf_suspend(device_t self, const pmf_qual_t *qual)
   3325 {
   3326 	wwan_suspend(self);
   3327 	return true;
   3328 }
   3329 
   3330 CFATTACH_DECL3_NEW(wwan, sizeof(struct wwan_softc),
   3331    wwan_match, wwan_attach, wwan_detach, NULL,
   3332    NULL, NULL, DVF_DETACH_SHUTDOWN);
   3333 #endif /* __NetBSD__ */
   3334 
   3335 #endif /* __OpenBSD__ || __NetBSD__ */
   3336