-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathxmm7360.c
3359 lines (2775 loc) · 77.5 KB
/
xmm7360.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $NetBSD: xmm7360.c,v 1.17 2022/10/27 00:01:07 riastradh Exp $ */
/*
* Device driver for Intel XMM7360 LTE modems, eg. Fibocom L850-GL.
* Written by James Wah
*
* Development of this driver was supported by genua GmbH
*
* Copyright (c) 2020 genua GmbH <[email protected]>
* Copyright (c) 2020 James Wah <[email protected]>
*
* The OpenBSD and NetBSD support was written by Jaromir Dolecek for
* Moritz Systems Technology Company Sp. z o.o.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES ON
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGE
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifdef __linux__
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/delay.h>
#include <linux/uaccess.h>
#include <linux/cdev.h>
#include <linux/wait.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/poll.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/if.h>
#include <linux/if_arp.h>
#include <net/rtnetlink.h>
#include <linux/hrtimer.h>
#include <linux/workqueue.h>
MODULE_LICENSE("Dual BSD/GPL");
static const struct pci_device_id xmm7360_ids[] = {
{ PCI_DEVICE(0x8086, 0x7360), },
{ 0, }
};
MODULE_DEVICE_TABLE(pci, xmm7360_ids);
/* Actually this ioctl not used for xmm0/rpc device by python code */
#define XMM7360_IOCTL_GET_PAGE_SIZE _IOC(_IOC_READ, 'x', 0xc0, sizeof(u32))
#define xmm7360_os_msleep(msec) msleep(msec)
#define __unused /* nothing */
#endif
#if defined(__OpenBSD__) || defined(__NetBSD__)
#ifdef __OpenBSD__
#include "bpfilter.h"
#endif
#ifdef __NetBSD__
#include "opt_inet.h"
#include "opt_gateway.h"
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: xmm7360.c,v 1.17 2022/10/27 00:01:07 riastradh Exp $");
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sockio.h>
#include <sys/mbuf.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/socket.h>
#include <sys/mutex.h>
#include <sys/tty.h>
#include <sys/conf.h>
#include <sys/kthread.h>
#include <sys/poll.h>
#include <sys/fcntl.h> /* for FREAD/FWRITE */
#include <sys/vnode.h>
#include <uvm/uvm_param.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcidevs.h>
#include <net/if.h>
#include <net/if_types.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#ifdef __OpenBSD__
#include <netinet/if_ether.h>
#include <sys/timeout.h>
#include <machine/bus.h>
#endif
#if NBPFILTER > 0 || defined(__NetBSD__)
#include <net/bpf.h>
#endif
#ifdef __NetBSD__
#include "ioconf.h"
#include <sys/cpu.h>
#endif
#ifdef INET
#include <netinet/in_var.h>
#endif
#ifdef INET6
#include <netinet6/in6_var.h>
#endif
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef bus_addr_t dma_addr_t;
typedef void * wait_queue_head_t; /* just address for tsleep() */
#define WWAN_BAR0 PCI_MAPREG_START
#define WWAN_BAR1 (PCI_MAPREG_START + 4)
#define WWAN_BAR2 (PCI_MAPREG_START + 8)
#define BUG_ON(never_true) KASSERT(!(never_true))
#define WARN_ON(x) /* nothing */
#ifdef __OpenBSD__
typedef struct mutex spinlock_t;
#define dev_err(devp, fmt, ...) \
printf("%s: " fmt, device_xname(devp), ##__VA_ARGS__)
#define dev_info(devp, fmt, ...) \
printf("%s: " fmt, device_xname(devp), ##__VA_ARGS__)
#define kzalloc(size, flags) malloc(size, M_DEVBUF, M_WAITOK | M_ZERO)
#define kfree(addr) free(addr, M_DEVBUF, 0)
#define mutex_init(lock) mtx_init(lock, IPL_TTY)
#define mutex_lock(lock) mtx_enter(lock)
#define mutex_unlock(lock) mtx_leave(lock)
/* In OpenBSD every mutex is spin mutex, and it must not be held on sleep */
#define spin_lock_irqsave(lock, flags) mtx_enter(lock)
#define spin_unlock_irqrestore(lock, flags) mtx_leave(lock)
/* Compat defines for NetBSD API */
#define curlwp curproc
#define LINESW(tp) (linesw[(tp)->t_line])
#define selnotify(sel, band, note) selwakeup(sel)
#define cfdata_t void *
#define device_lookup_private(cdp, unit) \
(unit < (*cdp).cd_ndevs) ? (*cdp).cd_devs[unit] : NULL
#define IFQ_SET_READY(ifq) /* nothing */
#define device_private(devt) (void *)devt;
#define if_deferred_start_init(ifp, arg) /* nothing */
#define IF_OUTPUT_CONST /* nothing */
#define knote_set_eof(kn, f) (kn)->kn_flags |= EV_EOF | (f)
#define tty_lock(tp) int s = spltty()
#define tty_unlock(tp) splx(s)
#define tty_locked(tp) /* nothing */
#define pmf_device_deregister(dev) /* nothing */
#if NBPFILTER > 0
#define BPF_MTAP_OUT(ifp, m) \
if (ifp->if_bpf) { \
bpf_mtap_af(ifp->if_bpf, m->m_pkthdr.ph_family, \
m, BPF_DIRECTION_OUT); \
}
#else
#define BPF_MTAP_OUT(ifp, m) /* nothing */
#endif
/* Copied from NetBSD <lib/libkern/libkern.h> */
#define __validate_container_of(PTR, TYPE, FIELD) \
(0 * sizeof((PTR) - &((TYPE *)(((char *)(PTR)) - \
offsetof(TYPE, FIELD)))->FIELD))
#define container_of(PTR, TYPE, FIELD) \
((TYPE *)(((char *)(PTR)) - offsetof(TYPE, FIELD)) \
+ __validate_container_of(PTR, TYPE, FIELD))
/* Copied from NetBSD <sys/cdefs.h> */
#define __UNVOLATILE(a) ((void *)(unsigned long)(volatile void *)(a))
#if OpenBSD <= 201911
/* Backward compat with OpenBSD 6.6 */
#define klist_insert(klist, kn) \
SLIST_INSERT_HEAD(klist, kn, kn_selnext)
#define klist_remove(klist, kn) \
SLIST_REMOVE(klist, kn, knote, kn_selnext)
#define XMM_KQ_ISFD_INITIALIZER .f_isfd = 1
#else
#define XMM_KQ_ISFD_INITIALIZER .f_flags = FILTEROP_ISFD
#endif /* OpenBSD <= 201911 */
#define selrecord_knote(si, kn) \
klist_insert(&(si)->si_note, (kn))
#define selremove_knote(si, kn) \
klist_remove(&(si)->si_note, (kn))
#endif
#ifdef __NetBSD__
typedef struct kmutex spinlock_t;
#define dev_err aprint_error_dev
#define dev_info aprint_normal_dev
#define mutex kmutex
#define kzalloc(size, flags) malloc(size, M_DEVBUF, M_WAITOK | M_ZERO)
#define kfree(addr) free(addr, M_DEVBUF)
#define mutex_init(lock) mutex_init(lock, MUTEX_DEFAULT, IPL_TTY)
#define mutex_lock(lock) mutex_enter(lock)
#define mutex_unlock(lock) mutex_exit(lock)
#define spin_lock_irqsave(lock, flags) mutex_enter(lock)
#define spin_unlock_irqrestore(lock, flags) mutex_exit(lock)
/* Compat defines with OpenBSD API */
#define caddr_t void *
#define proc lwp
#define LINESW(tp) (*tp->t_linesw)
#define ttymalloc(speed) tty_alloc()
#define ttyfree(tp) tty_free(tp)
#define l_open(dev, tp, p) l_open(dev, tp)
#define l_close(tp, flag, p) l_close(tp, flag)
#define ttkqfilter(dev, kn) ttykqfilter(dev, kn)
#define msleep(ident, lock, prio, wmesg, timo) \
mtsleep(ident, prio, wmesg, timo, lock)
#define pci_mapreg_map(pa, reg, type, busfl, tp, hp, bp, szp, maxsize) \
pci_mapreg_map(pa, reg, type, busfl, tp, hp, bp, szp)
#define pci_intr_establish(pc, ih, lvl, func, arg, name) \
pci_intr_establish_xname(pc, ih, lvl, func, arg, name)
#define suser(l) \
kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)
#define kthread_create(func, arg, lwpp, name) \
kthread_create(0, 0, NULL, func, arg, lwpp, "%s", name)
#define MUTEX_ASSERT_LOCKED(lock) KASSERT(mutex_owned(lock))
#define MCLGETI(m, how, m0, sz) MCLGET(m, how)
#define m_copyback(m, off, sz, buf, how) \
m_copyback(m, off, sz, buf)
#define ifq_deq_begin(ifq) ({ \
struct mbuf *m0; \
IFQ_DEQUEUE(ifq, m0); \
m0; \
})
#define ifq_deq_rollback(ifq, m) m_freem(m)
#define ifq_deq_commit(ifq, m) /* nothing to do */
#define ifq_is_oactive(ifq) true /* always restart queue */
#define ifq_clr_oactive(ifq) /* nothing to do */
#define ifq_empty(ifq) IFQ_IS_EMPTY(ifq)
#define ifq_purge(ifq) IF_PURGE(ifq)
#define if_enqueue(ifp, m) ifq_enqueue(ifp, m)
#define if_ih_insert(ifp, func, arg) (ifp)->_if_input = (func)
#define if_ih_remove(ifp, func, arg) /* nothing to do */
#define if_hardmtu if_mtu
#define IF_OUTPUT_CONST const
#define XMM_KQ_ISFD_INITIALIZER .f_flags = FILTEROP_ISFD
#define tty_lock(tp) ttylock(tp)
#define tty_unlock(tp) ttyunlock(tp)
#define tty_locked(tp) KASSERT(ttylocked(tp))
#define bpfattach(bpf, ifp, dlt, sz) bpf_attach(ifp, dlt, sz)
#define NBPFILTER 1
#define BPF_MTAP_OUT(ifp, m) bpf_mtap(ifp, m, BPF_D_OUT)
#endif /* __NetBSD__ */
#define __user /* nothing */
#define copy_from_user(kbuf, userbuf, sz) \
({ \
int __ret = 0; \
int error = copyin(userbuf, kbuf, sz); \
if (error != 0) \
return -error; \
__ret; \
})
#define copy_to_user(kbuf, userbuf, sz) \
({ \
int __ret = 0; \
int error = copyout(userbuf, kbuf, sz); \
if (error != 0) \
return -error; \
__ret; \
})
#define xmm7360_os_msleep(msec) \
do { \
KASSERT(!cold); \
tsleep(xmm, 0, "wwancsl", msec * hz / 1000); \
} while (0)
static pktq_rps_hash_func_t xmm7360_pktq_rps_hash_p;
static void *dma_alloc_coherent(struct device *, size_t, dma_addr_t *, int);
static void dma_free_coherent(struct device *, size_t, volatile void *, dma_addr_t);
#ifndef PCI_PRODUCT_INTEL_XMM7360
#define PCI_PRODUCT_INTEL_XMM7360 0x7360
#endif
#define init_waitqueue_head(wqp) *(wqp) = (wqp)
#define wait_event_interruptible(wq, cond) \
({ \
int __ret = 1; \
while (!(cond)) { \
KASSERT(!cold); \
int error = tsleep(wq, PCATCH, "xmmwq", 0); \
if (error) { \
__ret = (cond) ? 1 \
: ((error != ERESTART) ? -error : error); \
break; \
} \
} \
__ret; \
})
#define msecs_to_jiffies(msec) \
({ \
KASSERT(hz < 1000); \
KASSERT(msec > (1000 / hz)); \
msec * hz / 1000; \
})
#define wait_event_interruptible_timeout(wq, cond, jiffies) \
({ \
int __ret = 1; \
while (!(cond)) { \
if (cold) { \
for (int loop = 0; loop < 10; loop++) { \
delay(jiffies * 1000 * 1000 / hz / 10); \
if (cond) \
break; \
} \
__ret = (cond) ? 1 : 0; \
break; \
} \
int error = tsleep(wq, PCATCH, "xmmwq", jiffies); \
if (error) { \
__ret = (cond) ? 1 \
: ((error != ERESTART) ? -error : error); \
break; \
} \
} \
__ret; \
})
#define GFP_KERNEL 0
#endif /* __OpenBSD__ || __NetBSD__ */
/*
* The XMM7360 communicates via DMA ring buffers. It has one
* command ring, plus sixteen transfer descriptor (TD)
* rings. The command ring is mainly used to configure and
* deconfigure the TD rings.
*
* The 16 TD rings form 8 queue pairs (QP). For example, QP
* 0 uses ring 0 for host->device, and ring 1 for
* device->host.
*
* The known queue pair functions are as follows:
*
* 0: Mux (Raw IP packets, amongst others)
* 1: RPC (funky command protocol based in part on ASN.1 BER)
* 2: AT trace? port; does not accept commands after init
* 4: AT command port
* 7: AT command port
*
*/
/* Command ring, which is used to configure the queue pairs */
struct cmd_ring_entry {
dma_addr_t ptr;
u16 len;
u8 parm;
u8 cmd;
u32 extra;
u32 unk, flags;
};
#define CMD_RING_OPEN 1
#define CMD_RING_CLOSE 2
#define CMD_RING_FLUSH 3
#define CMD_WAKEUP 4
#define CMD_FLAG_DONE 1
#define CMD_FLAG_READY 2
/* Transfer descriptors used on the Tx and Rx rings of each queue pair */
struct td_ring_entry {
dma_addr_t addr;
u16 length;
u16 flags;
u32 unk;
};
#define TD_FLAG_COMPLETE 0x200
/* Root configuration object. This contains pointers to all of the control
* structures that the modem will interact with.
*/
struct control {
dma_addr_t status;
dma_addr_t s_wptr, s_rptr;
dma_addr_t c_wptr, c_rptr;
dma_addr_t c_ring;
u16 c_ring_size;
u16 unk;
};
struct status {
u32 code;
u32 mode;
u32 asleep;
u32 pad;
};
#define CMD_RING_SIZE 0x80
/* All of the control structures can be packed into one page of RAM. */
struct control_page {
struct control ctl;
// Status words - written by modem.
volatile struct status status;
// Slave ring write/read pointers.
volatile u32 s_wptr[16], s_rptr[16];
// Command ring write/read pointers.
volatile u32 c_wptr, c_rptr;
// Command ring entries.
volatile struct cmd_ring_entry c_ring[CMD_RING_SIZE];
};
#define BAR0_MODE 0x0c
#define BAR0_DOORBELL 0x04
#define BAR0_WAKEUP 0x14
#define DOORBELL_TD 0
#define DOORBELL_CMD 1
#define BAR2_STATUS 0x00
#define BAR2_MODE 0x18
#define BAR2_CONTROL 0x19
#define BAR2_CONTROLH 0x1a
#define BAR2_BLANK0 0x1b
#define BAR2_BLANK1 0x1c
#define BAR2_BLANK2 0x1d
#define BAR2_BLANK3 0x1e
#define XMM_MODEM_BOOTING 0xfeedb007
#define XMM_MODEM_READY 0x600df00d
#define XMM_TAG_ACBH 0x41434248 // 'ACBH'
#define XMM_TAG_CMDH 0x434d4448 // 'CMDH'
#define XMM_TAG_ADBH 0x41444248 // 'ADBH'
#define XMM_TAG_ADTH 0x41445448 // 'ADTH'
/* There are 16 TD rings: a Tx and Rx ring for each queue pair */
struct td_ring {
u8 depth;
u8 last_handled;
u16 page_size;
struct td_ring_entry *tds;
dma_addr_t tds_phys;
// One page of page_size per td
void **pages;
dma_addr_t *pages_phys;
};
#define TD_MAX_PAGE_SIZE 16384
struct queue_pair {
struct xmm_dev *xmm;
u8 depth;
u16 page_size;
int tty_index;
int tty_needs_wake;
#ifdef __linux__
struct device dev;
#endif
int num;
int open;
struct mutex lock;
unsigned char user_buf[TD_MAX_PAGE_SIZE];
wait_queue_head_t wq;
#ifdef __linux__
struct cdev cdev;
struct tty_port port;
#endif
#if defined(__OpenBSD__) || defined(__NetBSD__)
struct selinfo selr, selw;
#endif
};
#define XMM_QP_COUNT 8
struct xmm_dev {
struct device *dev;
volatile uint32_t *bar0, *bar2;
volatile struct control_page *cp;
dma_addr_t cp_phys;
struct td_ring td_ring[2 * XMM_QP_COUNT];
struct queue_pair qp[XMM_QP_COUNT];
struct xmm_net *net;
struct net_device *netdev;
int error;
int card_num;
int num_ttys;
wait_queue_head_t wq;
#ifdef __linux__
struct pci_dev *pci_dev;
int irq;
struct work_struct init_work; // XXX work not actually scheduled
#endif
};
struct mux_bounds {
uint32_t offset;
uint32_t length;
};
struct mux_first_header {
uint32_t tag;
uint16_t unknown;
uint16_t sequence;
uint16_t length;
uint16_t extra;
uint16_t next;
uint16_t pad;
};
struct mux_next_header {
uint32_t tag;
uint16_t length;
uint16_t extra;
uint16_t next;
uint16_t pad;
};
#define MUX_MAX_PACKETS 64
struct mux_frame {
int n_packets, n_bytes, max_size, sequence;
uint16_t *last_tag_length, *last_tag_next;
struct mux_bounds bounds[MUX_MAX_PACKETS];
uint8_t data[TD_MAX_PAGE_SIZE];
};
struct xmm_net {
struct xmm_dev *xmm;
struct queue_pair *qp;
int channel;
#ifdef __linux__
struct sk_buff_head queue;
struct hrtimer deadline;
#endif
int queued_packets, queued_bytes;
int sequence;
spinlock_t lock;
struct mux_frame frame;
};
static void xmm7360_os_handle_net_frame(struct xmm_dev *, const u8 *, size_t);
static void xmm7360_os_handle_net_dequeue(struct xmm_net *, struct mux_frame *);
static void xmm7360_os_handle_net_txwake(struct xmm_net *);
static void xmm7360_os_handle_tty_idata(struct queue_pair *, const u8 *, size_t);
static void xmm7360_poll(struct xmm_dev *xmm)
{
if (xmm->cp->status.code == 0xbadc0ded) {
dev_err(xmm->dev, "crashed but dma up\n");
xmm->error = -ENODEV;
}
if (xmm->bar2[BAR2_STATUS] != XMM_MODEM_READY) {
dev_err(xmm->dev, "bad status %x\n",xmm->bar2[BAR2_STATUS]);
xmm->error = -ENODEV;
}
}
static void xmm7360_ding(struct xmm_dev *xmm, int bell)
{
if (xmm->cp->status.asleep)
xmm->bar0[BAR0_WAKEUP] = 1;
xmm->bar0[BAR0_DOORBELL] = bell;
xmm7360_poll(xmm);
}
static int xmm7360_cmd_ring_wait(struct xmm_dev *xmm)
{
// Wait for all commands to complete
// XXX locking?
int ret = wait_event_interruptible_timeout(xmm->wq, (xmm->cp->c_rptr == xmm->cp->c_wptr) || xmm->error, msecs_to_jiffies(1000));
if (ret == 0)
return -ETIMEDOUT;
if (ret < 0)
return ret;
return xmm->error;
}
static int xmm7360_cmd_ring_execute(struct xmm_dev *xmm, u8 cmd, u8 parm, u16 len, dma_addr_t ptr, u32 extra)
{
u8 wptr = xmm->cp->c_wptr;
u8 new_wptr = (wptr + 1) % CMD_RING_SIZE;
if (xmm->error)
return xmm->error;
if (new_wptr == xmm->cp->c_rptr) // ring full
return -EAGAIN;
xmm->cp->c_ring[wptr].ptr = ptr;
xmm->cp->c_ring[wptr].cmd = cmd;
xmm->cp->c_ring[wptr].parm = parm;
xmm->cp->c_ring[wptr].len = len;
xmm->cp->c_ring[wptr].extra = extra;
xmm->cp->c_ring[wptr].unk = 0;
xmm->cp->c_ring[wptr].flags = CMD_FLAG_READY;
xmm->cp->c_wptr = new_wptr;
xmm7360_ding(xmm, DOORBELL_CMD);
return xmm7360_cmd_ring_wait(xmm);
}
static int xmm7360_cmd_ring_init(struct xmm_dev *xmm) {
int timeout;
int ret;
xmm->cp = dma_alloc_coherent(xmm->dev, sizeof(struct control_page), &xmm->cp_phys, GFP_KERNEL);
BUG_ON(xmm->cp == NULL);
xmm->cp->ctl.status = xmm->cp_phys + offsetof(struct control_page, status);
xmm->cp->ctl.s_wptr = xmm->cp_phys + offsetof(struct control_page, s_wptr);
xmm->cp->ctl.s_rptr = xmm->cp_phys + offsetof(struct control_page, s_rptr);
xmm->cp->ctl.c_wptr = xmm->cp_phys + offsetof(struct control_page, c_wptr);
xmm->cp->ctl.c_rptr = xmm->cp_phys + offsetof(struct control_page, c_rptr);
xmm->cp->ctl.c_ring = xmm->cp_phys + offsetof(struct control_page, c_ring);
xmm->cp->ctl.c_ring_size = CMD_RING_SIZE;
xmm->bar2[BAR2_CONTROL] = xmm->cp_phys;
xmm->bar2[BAR2_CONTROLH] = xmm->cp_phys >> 32;
xmm->bar0[BAR0_MODE] = 1;
timeout = 100;
while (xmm->bar2[BAR2_MODE] == 0 && --timeout)
xmm7360_os_msleep(10);
if (!timeout)
return -ETIMEDOUT;
xmm->bar2[BAR2_BLANK0] = 0;
xmm->bar2[BAR2_BLANK1] = 0;
xmm->bar2[BAR2_BLANK2] = 0;
xmm->bar2[BAR2_BLANK3] = 0;
xmm->bar0[BAR0_MODE] = 2; // enable intrs?
timeout = 100;
while (xmm->bar2[BAR2_MODE] != 2 && --timeout)
xmm7360_os_msleep(10);
if (!timeout)
return -ETIMEDOUT;
// enable going to sleep when idle
ret = xmm7360_cmd_ring_execute(xmm, CMD_WAKEUP, 0, 1, 0, 0);
if (ret)
return ret;
return 0;
}
static void xmm7360_cmd_ring_free(struct xmm_dev *xmm) {
if (xmm->bar0)
xmm->bar0[BAR0_MODE] = 0;
if (xmm->cp)
dma_free_coherent(xmm->dev, sizeof(struct control_page), (volatile void *)xmm->cp, xmm->cp_phys);
xmm->cp = NULL;
return;
}
static void xmm7360_td_ring_activate(struct xmm_dev *xmm, u8 ring_id)
{
struct td_ring *ring = &xmm->td_ring[ring_id];
int ret __diagused;
xmm->cp->s_rptr[ring_id] = xmm->cp->s_wptr[ring_id] = 0;
ring->last_handled = 0;
ret = xmm7360_cmd_ring_execute(xmm, CMD_RING_OPEN, ring_id, ring->depth, ring->tds_phys, 0x60);
BUG_ON(ret);
}
static void xmm7360_td_ring_create(struct xmm_dev *xmm, u8 ring_id, u8 depth, u16 page_size)
{
struct td_ring *ring = &xmm->td_ring[ring_id];
int i;
BUG_ON(ring->depth);
BUG_ON(depth & (depth-1));
BUG_ON(page_size > TD_MAX_PAGE_SIZE);
memset(ring, 0, sizeof(struct td_ring));
ring->depth = depth;
ring->page_size = page_size;
ring->tds = dma_alloc_coherent(xmm->dev, sizeof(struct td_ring_entry)*depth, &ring->tds_phys, GFP_KERNEL);
ring->pages = kzalloc(sizeof(void*)*depth, GFP_KERNEL);
ring->pages_phys = kzalloc(sizeof(dma_addr_t)*depth, GFP_KERNEL);
for (i=0; i<depth; i++) {
ring->pages[i] = dma_alloc_coherent(xmm->dev, ring->page_size, &ring->pages_phys[i], GFP_KERNEL);
ring->tds[i].addr = ring->pages_phys[i];
}
xmm7360_td_ring_activate(xmm, ring_id);
}
static void xmm7360_td_ring_deactivate(struct xmm_dev *xmm, u8 ring_id)
{
xmm7360_cmd_ring_execute(xmm, CMD_RING_CLOSE, ring_id, 0, 0, 0);
}
static void xmm7360_td_ring_destroy(struct xmm_dev *xmm, u8 ring_id)
{
struct td_ring *ring = &xmm->td_ring[ring_id];
int i, depth=ring->depth;
if (!depth) {
WARN_ON(1);
dev_err(xmm->dev, "Tried destroying empty ring!\n");
return;
}
xmm7360_td_ring_deactivate(xmm, ring_id);
for (i=0; i<depth; i++) {
dma_free_coherent(xmm->dev, ring->page_size, ring->pages[i], ring->pages_phys[i]);
}
kfree(ring->pages_phys);
kfree(ring->pages);
dma_free_coherent(xmm->dev, sizeof(struct td_ring_entry)*depth, ring->tds, ring->tds_phys);
ring->depth = 0;
}
static void xmm7360_td_ring_write(struct xmm_dev *xmm, u8 ring_id, const void *buf, int len)
{
struct td_ring *ring = &xmm->td_ring[ring_id];
u8 wptr = xmm->cp->s_wptr[ring_id];
BUG_ON(!ring->depth);
BUG_ON(len > ring->page_size);
BUG_ON(ring_id & 1);
memcpy(ring->pages[wptr], buf, len);
ring->tds[wptr].length = len;
ring->tds[wptr].flags = 0;
ring->tds[wptr].unk = 0;
wptr = (wptr + 1) & (ring->depth - 1);
BUG_ON(wptr == xmm->cp->s_rptr[ring_id]);
xmm->cp->s_wptr[ring_id] = wptr;
}
static int xmm7360_td_ring_full(struct xmm_dev *xmm, u8 ring_id)
{
struct td_ring *ring = &xmm->td_ring[ring_id];
u8 wptr = xmm->cp->s_wptr[ring_id];
wptr = (wptr + 1) & (ring->depth - 1);
return wptr == xmm->cp->s_rptr[ring_id];
}
static void xmm7360_td_ring_read(struct xmm_dev *xmm, u8 ring_id)
{
struct td_ring *ring = &xmm->td_ring[ring_id];
u8 wptr = xmm->cp->s_wptr[ring_id];
if (!ring->depth) {
dev_err(xmm->dev, "read on disabled ring\n");
WARN_ON(1);
return;
}
if (!(ring_id & 1)) {
dev_err(xmm->dev, "read on write ring\n");
WARN_ON(1);
return;
}
ring->tds[wptr].length = ring->page_size;
ring->tds[wptr].flags = 0;
ring->tds[wptr].unk = 0;
wptr = (wptr + 1) & (ring->depth - 1);
BUG_ON(wptr == xmm->cp->s_rptr[ring_id]);
xmm->cp->s_wptr[ring_id] = wptr;
}
static struct queue_pair * xmm7360_init_qp(struct xmm_dev *xmm, int num, u8 depth, u16 page_size)
{
struct queue_pair *qp = &xmm->qp[num];
qp->xmm = xmm;
qp->num = num;
qp->open = 0;
qp->depth = depth;
qp->page_size = page_size;
mutex_init(&qp->lock);
init_waitqueue_head(&qp->wq);
return qp;
}
static void xmm7360_qp_arm(struct xmm_dev *xmm, struct queue_pair *qp)
{
while (!xmm7360_td_ring_full(xmm, qp->num*2+1))
xmm7360_td_ring_read(xmm, qp->num*2+1);
xmm7360_ding(xmm, DOORBELL_TD);
}
static int xmm7360_qp_start(struct queue_pair *qp)
{
struct xmm_dev *xmm = qp->xmm;
int ret;
mutex_lock(&qp->lock);
if (qp->open) {
ret = -EBUSY;
} else {
ret = 0;
qp->open = 1;
}
mutex_unlock(&qp->lock);
if (ret == 0) {
xmm7360_td_ring_create(xmm, qp->num*2, qp->depth, qp->page_size);
xmm7360_td_ring_create(xmm, qp->num*2+1, qp->depth, qp->page_size);
xmm7360_qp_arm(xmm, qp);
}
return ret;
}
static void xmm7360_qp_resume(struct queue_pair *qp)
{
struct xmm_dev *xmm = qp->xmm;
BUG_ON(!qp->open);
xmm7360_td_ring_activate(xmm, qp->num*2);
xmm7360_td_ring_activate(xmm, qp->num*2+1);
xmm7360_qp_arm(xmm, qp);
}
static int xmm7360_qp_stop(struct queue_pair *qp)
{
struct xmm_dev *xmm = qp->xmm;
int ret = 0;
mutex_lock(&qp->lock);
if (!qp->open) {
ret = -ENODEV;
} else {
ret = 0;
/* still holding qp->open to prevent concurrent access */
}
mutex_unlock(&qp->lock);
if (ret == 0) {
xmm7360_td_ring_destroy(xmm, qp->num*2);
xmm7360_td_ring_destroy(xmm, qp->num*2+1);
mutex_lock(&qp->lock);
qp->open = 0;
mutex_unlock(&qp->lock);
}
return ret;
}
static void xmm7360_qp_suspend(struct queue_pair *qp)
{
struct xmm_dev *xmm = qp->xmm;
BUG_ON(!qp->open);
xmm7360_td_ring_deactivate(xmm, qp->num*2);
}
static int xmm7360_qp_can_write(struct queue_pair *qp)
{
struct xmm_dev *xmm = qp->xmm;
return !xmm7360_td_ring_full(xmm, qp->num*2);
}
static ssize_t xmm7360_qp_write(struct queue_pair *qp, const char *buf, size_t size)
{
struct xmm_dev *xmm = qp->xmm;
int page_size = qp->xmm->td_ring[qp->num*2].page_size;
if (xmm->error)
return xmm->error;
if (!xmm7360_qp_can_write(qp))
return 0;
if (size > page_size)
size = page_size;
xmm7360_td_ring_write(xmm, qp->num*2, buf, size);
xmm7360_ding(xmm, DOORBELL_TD);
return size;
}
static ssize_t xmm7360_qp_write_user(struct queue_pair *qp, const char __user *buf, size_t size)
{
int page_size = qp->xmm->td_ring[qp->num*2].page_size;
int ret;
if (size > page_size)
size = page_size;
ret = copy_from_user(qp->user_buf, buf, size);
size = size - ret;
if (!size)
return 0;
return xmm7360_qp_write(qp, qp->user_buf, size);
}
static int xmm7360_qp_has_data(struct queue_pair *qp)
{
struct xmm_dev *xmm = qp->xmm;
struct td_ring *ring = &xmm->td_ring[qp->num*2+1];
return (xmm->cp->s_rptr[qp->num*2+1] != ring->last_handled);
}
static ssize_t xmm7360_qp_read_user(struct queue_pair *qp, char __user *buf, size_t size)
{
struct xmm_dev *xmm = qp->xmm;
struct td_ring *ring = &xmm->td_ring[qp->num*2+1];
int idx, nread, ret;
// XXX locking?
ret = wait_event_interruptible(qp->wq, xmm7360_qp_has_data(qp) || xmm->error);
if (ret < 0)
return ret;
if (xmm->error)
return xmm->error;
idx = ring->last_handled;
nread = ring->tds[idx].length;
if (nread > size)
nread = size;
ret = copy_to_user(buf, ring->pages[idx], nread);
nread -= ret;
if (nread == 0)
return 0;
// XXX all data not fitting into buf+size is discarded
xmm7360_td_ring_read(xmm, qp->num*2+1);
xmm7360_ding(xmm, DOORBELL_TD);
ring->last_handled = (idx + 1) & (ring->depth - 1);
return nread;
}
static void xmm7360_tty_poll_qp(struct queue_pair *qp)
{
struct xmm_dev *xmm = qp->xmm;
struct td_ring *ring = &xmm->td_ring[qp->num*2+1];
int idx, nread;
while (xmm7360_qp_has_data(qp)) {
idx = ring->last_handled;
nread = ring->tds[idx].length;
xmm7360_os_handle_tty_idata(qp, ring->pages[idx], nread);
xmm7360_td_ring_read(xmm, qp->num*2+1);
xmm7360_ding(xmm, DOORBELL_TD);
ring->last_handled = (idx + 1) & (ring->depth - 1);
}
}
#ifdef __linux__
static void xmm7360_os_handle_tty_idata(struct queue_pair *qp, const u8 *data, size_t nread)
{