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