forked from xmm7360/xmm7360-pci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmm7360.c
1580 lines (1307 loc) · 36.3 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
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
/*
* Device driver for Intel XMM7360 LTE modems, eg. Fibocom L850-GL.
* Written by James Wah
*
* Development of this driver was supported by genua GmbH
*
* This work is dual-licensed under BSD 3-clause and GPL 2.0.
* You can choose between one of them if you use this work.
*
* Copyright (c) 2020 genua GmbH <[email protected]>
* Copyright (c) 2020 James Wah <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <linux/version.h>
#include <linux/cdev.h>
#include <linux/delay.h>
#include <linux/hrtimer.h>
#include <linux/if.h>
#include <linux/if_arp.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/pci.h>
#include <linux/poll.h>
#include <linux/skbuff.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/uaccess.h>
#include <linux/wait.h>
#include <linux/workqueue.h>
#include <net/rtnetlink.h>
MODULE_LICENSE("Dual BSD/GPL");
static struct pci_device_id xmm7360_ids[] = { {
PCI_DEVICE(0x8086,
0x7360),
},
{
0,
} };
MODULE_DEVICE_TABLE(pci, xmm7360_ids);
#define XMM7360_IOCTL_GET_PAGE_SIZE _IOC(_IOC_READ, 'x', 0xc0, sizeof(u32))
static dev_t xmm_base;
static struct tty_driver *xmm7360_tty_driver;
/*
* 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
/* 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;
struct cdev cdev;
struct tty_port port;
int tty_index;
int tty_needs_wake;
struct device dev;
int num;
int open;
wait_queue_head_t wq;
struct mutex lock;
unsigned char user_buf[TD_MAX_PAGE_SIZE];
};
struct xmm_dev {
struct device *dev;
struct pci_dev *pci_dev;
volatile uint32_t *bar0, *bar2;
int irq;
wait_queue_head_t wq;
struct work_struct init_work;
volatile struct control_page *cp;
dma_addr_t cp_phys;
struct td_ring td_ring[16];
struct queue_pair qp[8];
struct xmm_net *net;
struct net_device *netdev;
int error;
int card_num;
int num_ttys;
};
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;
struct sk_buff_head queue;
struct hrtimer deadline;
int queued_packets, queued_bytes;
int sequence;
spinlock_t lock;
struct mux_frame frame;
};
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] != 0x600df00d) {
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
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);
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)
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)
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),
(void *)xmm->cp, xmm->cp_phys);
xmm->cp = NULL;
return;
}
static int 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;
int ret;
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];
}
xmm->cp->s_rptr[ring_id] = xmm->cp->s_wptr[ring_id] = 0;
ret = xmm7360_cmd_ring_execute(xmm, CMD_RING_OPEN, ring_id, depth,
ring->tds_phys, 0x60);
if (ret)
return ret;
return 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_cmd_ring_execute(xmm, CMD_RING_CLOSE, ring_id, 0, 0, 0);
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 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;
ret = xmm7360_td_ring_create(xmm, qp->num * 2, qp->depth,
qp->page_size);
if (ret)
goto out;
ret = xmm7360_td_ring_create(xmm, qp->num * 2 + 1, qp->depth,
qp->page_size);
if (ret) {
xmm7360_td_ring_destroy(xmm, qp->num * 2);
goto out;
}
while (!xmm7360_td_ring_full(xmm, qp->num * 2 + 1))
xmm7360_td_ring_read(xmm, qp->num * 2 + 1);
xmm7360_ding(xmm, DOORBELL_TD);
}
out:
mutex_unlock(&qp->lock);
return ret;
}
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;
qp->open = 0;
xmm7360_td_ring_destroy(xmm, qp->num * 2);
xmm7360_td_ring_destroy(xmm, qp->num * 2 + 1);
}
mutex_unlock(&qp->lock);
return ret;
}
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 size_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 size_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 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;
tty_insert_flip_string(&qp->port, ring->pages[idx], nread);
tty_flip_buffer_push(&qp->port);
xmm7360_td_ring_read(xmm, qp->num * 2 + 1);
xmm7360_ding(xmm, DOORBELL_TD);
ring->last_handled = (idx + 1) & (ring->depth - 1);
}
}
int xmm7360_cdev_open(struct inode *inode, struct file *file)
{
struct queue_pair *qp =
container_of(inode->i_cdev, struct queue_pair, cdev);
file->private_data = qp;
return xmm7360_qp_start(qp);
}
int xmm7360_cdev_release(struct inode *inode, struct file *file)
{
struct queue_pair *qp = file->private_data;
return xmm7360_qp_stop(qp);
}
ssize_t xmm7360_cdev_write(struct file *file, const char __user *buf,
size_t size, loff_t *offset)
{
struct queue_pair *qp = file->private_data;
int ret;
ret = xmm7360_qp_write_user(qp, buf, size);
if (ret < 0)
return ret;
*offset += ret;
return size;
}
ssize_t xmm7360_cdev_read(struct file *file, char __user *buf, size_t size,
loff_t *offset)
{
struct queue_pair *qp = file->private_data;
struct xmm_dev *xmm = qp->xmm;
struct td_ring *ring = &xmm->td_ring[qp->num * 2 + 1];
int idx, nread, ret;
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;
xmm7360_td_ring_read(xmm, qp->num * 2 + 1);
xmm7360_ding(xmm, DOORBELL_TD);
ring->last_handled = (idx + 1) & (ring->depth - 1);
*offset += nread;
return nread;
}
static unsigned int xmm7360_cdev_poll(struct file *file, poll_table *wait)
{
struct queue_pair *qp = file->private_data;
unsigned int mask = 0;
poll_wait(file, &qp->wq, wait);
if (qp->xmm->error)
return POLLHUP;
if (xmm7360_qp_has_data(qp))
mask |= POLLIN | POLLRDNORM;
if (xmm7360_qp_can_write(qp))
mask |= POLLOUT | POLLWRNORM;
return mask;
}
static long xmm7360_cdev_ioctl(struct file *file, unsigned int cmd,
unsigned long arg)
{
struct queue_pair *qp = file->private_data;
u32 val;
switch (cmd) {
case XMM7360_IOCTL_GET_PAGE_SIZE:
val = qp->xmm->td_ring[qp->num * 2].page_size;
if (copy_to_user((u32 *)arg, &val, sizeof(u32)))
return -EFAULT;
return 0;
}
return -ENOTTY;
}
static struct file_operations xmm7360_fops = {
.read = xmm7360_cdev_read,
.write = xmm7360_cdev_write,
.poll = xmm7360_cdev_poll,
.unlocked_ioctl = xmm7360_cdev_ioctl,
.open = xmm7360_cdev_open,
.release = xmm7360_cdev_release
};
static void xmm7360_mux_frame_init(struct xmm_net *xn, struct mux_frame *frame,
int sequence)
{
frame->sequence = xn->sequence;
frame->max_size = xn->xmm->td_ring[0].page_size;
frame->n_packets = 0;
frame->n_bytes = 0;
frame->last_tag_next = NULL;
frame->last_tag_length = NULL;
}
static int xmm7360_mux_frame_add_tag(struct mux_frame *frame, uint32_t tag,
uint16_t extra, void *data, int data_len)
{
int total_length;
if (frame->n_bytes == 0)
total_length = sizeof(struct mux_first_header) + data_len;
else
total_length = sizeof(struct mux_next_header) + data_len;
while (frame->n_bytes & 3)
frame->n_bytes++;
if (frame->n_bytes + total_length > frame->max_size)
return -1;
if (frame->last_tag_next)
*frame->last_tag_next = frame->n_bytes;
if (frame->n_bytes == 0) {
struct mux_first_header *hdr =
(struct mux_first_header *)frame->data;
memset(hdr, 0, sizeof(struct mux_first_header));
hdr->tag = htonl(tag);
hdr->sequence = frame->sequence;
hdr->length = total_length;
hdr->extra = extra;
frame->last_tag_length = &hdr->length;
frame->last_tag_next = &hdr->next;
frame->n_bytes += sizeof(struct mux_first_header);
} else {
struct mux_next_header *hdr =
(struct mux_next_header *)(&frame->data[frame->n_bytes]);
memset(hdr, 0, sizeof(struct mux_next_header));
hdr->tag = htonl(tag);
hdr->length = total_length;
hdr->extra = extra;
frame->last_tag_length = &hdr->length;
frame->last_tag_next = &hdr->next;
frame->n_bytes += sizeof(struct mux_next_header);
}
if (data_len) {
memcpy(&frame->data[frame->n_bytes], data, data_len);
frame->n_bytes += data_len;
}
return 0;
}
static int xmm7360_mux_frame_append_data(struct mux_frame *frame, void *data,
int data_len)
{
if (frame->n_bytes + data_len > frame->max_size)
return -1;
BUG_ON(!frame->last_tag_length);
memcpy(&frame->data[frame->n_bytes], data, data_len);
*frame->last_tag_length += data_len;
frame->n_bytes += data_len;
return 0;
}
static int xmm7360_mux_frame_append_packet(struct mux_frame *frame,
struct sk_buff *skb)
{
int expected_adth_size =
sizeof(struct mux_next_header) + 4 +
(frame->n_packets + 1) * sizeof(struct mux_bounds);
int ret;
uint8_t pad[16];
if (frame->n_packets >= MUX_MAX_PACKETS)
return -1;
if (frame->n_bytes + skb->len + 16 + expected_adth_size >
frame->max_size)
return -1;
BUG_ON(!frame->last_tag_length);
frame->bounds[frame->n_packets].offset = frame->n_bytes;
frame->bounds[frame->n_packets].length = skb->len + 16;
frame->n_packets++;
memset(pad, 0, sizeof(pad));
ret = xmm7360_mux_frame_append_data(frame, pad, 16);
if (ret)
return ret;
ret = xmm7360_mux_frame_append_data(frame, skb->data, skb->len);
return ret;
}
static int xmm7360_mux_frame_push(struct xmm_dev *xmm, struct mux_frame *frame)
{
struct mux_first_header *hdr = (void *)&frame->data[0];
int ret;
hdr->length = frame->n_bytes;
ret = xmm7360_qp_write(xmm->net->qp, frame->data, frame->n_bytes);
if (ret < 0)
return ret;
return 0;
}
static int xmm7360_mux_control(struct xmm_net *xn, u32 arg1, u32 arg2, u32 arg3,
u32 arg4)
{
struct mux_frame *frame = &xn->frame;
int ret;
uint32_t cmdh_args[] = { arg1, arg2, arg3, arg4 };
unsigned long flags;
spin_lock_irqsave(&xn->lock, flags);
xmm7360_mux_frame_init(xn, frame, 0);
xmm7360_mux_frame_add_tag(frame, 'ACBH', 0, NULL, 0);
xmm7360_mux_frame_add_tag(frame, 'CMDH', xn->channel, cmdh_args,
sizeof(cmdh_args));
ret = xmm7360_mux_frame_push(xn->xmm, frame);
spin_unlock_irqrestore(&xn->lock, flags);
return ret;
}
static void xmm7360_net_uninit(struct net_device *dev)
{
}
static int xmm7360_net_open(struct net_device *dev)
{
struct xmm_net *xn = netdev_priv(dev);
struct sk_buff *skb;
xn->queued_packets = xn->queued_bytes = 0;
while ((skb = skb_dequeue(&xn->queue)))
kfree_skb(skb);
netif_start_queue(dev);
return xmm7360_mux_control(xn, 1, 0, 0, 0);
}
static int xmm7360_net_close(struct net_device *dev)
{
netif_stop_queue(dev);
return 0;
}
static int xmm7360_net_must_flush(struct xmm_net *xn, int new_packet_bytes)
{
int frame_size;
if (xn->queued_packets >= MUX_MAX_PACKETS)
return 1;
frame_size = sizeof(struct mux_first_header) + xn->queued_bytes +
sizeof(struct mux_next_header) + 4 +
sizeof(struct mux_bounds) * xn->queued_packets;
frame_size += 16 + new_packet_bytes + sizeof(struct mux_bounds);
return frame_size > xn->frame.max_size;
}
static void xmm7360_net_flush(struct xmm_net *xn)
{
struct sk_buff *skb;
struct mux_frame *frame = &xn->frame;
int ret;
u32 unknown = 0;
if (skb_queue_empty(&xn->queue))
return;
xmm7360_mux_frame_init(xn, frame, xn->sequence++);
xmm7360_mux_frame_add_tag(frame, 'ADBH', 0, NULL, 0);
while ((skb = skb_dequeue(&xn->queue))) {
ret = xmm7360_mux_frame_append_packet(frame, skb);
if (ret)
goto drop;
}
ret = xmm7360_mux_frame_add_tag(frame, 'ADTH', xn->channel, &unknown,
sizeof(uint32_t));
if (ret)
goto drop;
ret = xmm7360_mux_frame_append_data(frame, &frame->bounds[0],
sizeof(struct mux_bounds) *
frame->n_packets);
if (ret)
goto drop;
ret = xmm7360_mux_frame_push(xn->xmm, frame);
if (ret)
goto drop;
xn->queued_packets = xn->queued_bytes = 0;
return;
drop:
dev_err(xn->xmm->dev, "Failed to ship coalesced frame");
}
static enum hrtimer_restart xmm7360_net_deadline_cb(struct hrtimer *t)
{
struct xmm_net *xn = container_of(t, struct xmm_net, deadline);
unsigned long flags;
spin_lock_irqsave(&xn->lock, flags);
xmm7360_net_flush(xn);
spin_unlock_irqrestore(&xn->lock, flags);
return HRTIMER_NORESTART;
}
static netdev_tx_t xmm7360_net_xmit(struct sk_buff *skb, struct net_device *dev)
{
struct xmm_net *xn = netdev_priv(dev);
ktime_t kt;
unsigned long flags;
if (netif_queue_stopped(dev))
return NETDEV_TX_BUSY;
skb_orphan(skb);
spin_lock_irqsave(&xn->lock, flags);
if (xmm7360_net_must_flush(xn, skb->len)) {
if (xmm7360_qp_can_write(xn->qp)) {
xmm7360_net_flush(xn);
} else {
netif_stop_queue(dev);