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