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