-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet.c
executable file
·880 lines (676 loc) · 23.3 KB
/
net.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
/* maple_bus.c - The simplest kernel module.
*/
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/io.h>
#include <linux/interrupt.h>
#include <linux/dmaengine.h>
#include <linux/dma-contiguous.h>
#include <linux/amba/xilinx_dma.h>
#include <linux/netdevice.h>
#include <linux/if_arp.h>
#include <linux/of_address.h>
#include <linux/of_device.h>
#include <linux/of_platform.h>
#include <asm/uaccess.h>
#include <linux/cdev.h>
#include <linux/spinlock.h>
#include <linux/fs.h> /* everything... */
#include "hw.h"
#include "utils.h"
#include "dma.h"
/* Standard module information, edit as appropriate */
MODULE_LICENSE("GPL");
MODULE_AUTHOR
("Raul Rangel - ismell.org <[email protected]>");
MODULE_DESCRIPTION
("maple_bus - Sega Dreamcast Maple Bus Driver");
#define DRIVER_NAME "maple_bus"
/* Simple example of how to receive command line parameters to your module.
Delete if you don't need them */
unsigned pause = 0x0;
unsigned loopback = 0x1;
char *mystr = "default";
module_param(pause, int, S_IRUGO);
module_param(loopback, int, S_IRUGO);
module_param(mystr, charp, S_IRUGO);
// Module Globals
static struct maple_bus_global *gp;
static int maple_bus_start_rx(struct net_device *netdev);
static void maple_bus_print_status(struct maple_bus_local *lp) {
u32 control, status;
struct maple_bus_chan_count rx_count, tx_count;
struct maple_bus_chan_totals totals;
control = maple_bus_read(lp, MAPLE_BUS_CONTROL_REG);
status = maple_bus_read(lp, MAPLE_BUS_STATUS_REG);
maple_bus_get_count(lp, MAPLE_BUS_TX_COUNT_REG, &tx_count);
maple_bus_get_count(lp, MAPLE_BUS_RX_COUNT_REG, &rx_count);
maple_bus_get_totals(lp, &totals);
dev_dbg(lp->dev,"maple_bus at 0x%08x with irq %d",
(unsigned int __force)lp->base_addr,
lp->irq
);
dev_dbg(lp->dev,
"Control Register: 0x%08x\n"
" TX Enabled: %d, RX Enabled: %d, Loopback Enabled: %d\n"
" Reset TX: %d, Reset RX: %d\n"
" TX IRQ Enabled: %d, RX IRQ Enabled: %d\n",
control,
!!(control & MAPLE_BUS_ENABLE_TX),
!!(control & MAPLE_BUS_ENABLE_RX),
!!(control & MAPLE_BUS_ENABLE_LOOPBACK),
!!(control & MAPLE_BUS_RESET_TX),
!!(control & MAPLE_BUS_RESET_RX),
!!(control & MAPLE_BUS_ENABLE_TX_IRQ),
!!(control & MAPLE_BUS_ENABLE_RX_IRQ)
);
dev_dbg(lp->dev,
"Status Register: 0x%08x\n"
" TX IRQ: %d, RX IRQ: %d\n"
" TX Buffered Packets: %d, TX Buffer Length: %d\n"
" RX Buffered Packets: %d, RX Buffer Length: %d\n"
" Received Packets: %d, Transmitted Packets: %d\n",
status,
!!(status & MAPLE_BUS_TX_IRQ_STATUS),
!!(status & MAPLE_BUS_RX_IRQ_STATUS),
tx_count.packets, tx_count.length,
rx_count.packets, rx_count.length,
totals.rx_packets, totals.tx_packets
);
}
struct maple_bus_async_dma_desc {
struct net_device *netdev;
struct dma_async_tx_descriptor *txd;
dma_cookie_t cookie;
struct sg_table sgt;
struct sk_buff *skb;
enum dma_transfer_direction direction;
};
/* Maps a skb into a scatterlist
*
* returns the number of scatterlist entries
*/
static int maple_bus_sbk_to_sgt(struct net_device *netdev, struct sk_buff *skb,
struct sg_table *sgt) {
struct maple_bus_local *lp = netdev_priv(netdev);
int sg_n = 0;
struct scatterlist *sg;
unsigned int i;
struct skb_frag_struct *frag;
dev_dbg(lp->dev, "Mapping scatter list for skb 0x%p\n", skb);
//skb_has_frag_list
sg_n = 1 + skb_shinfo(skb)->nr_frags;
dev_dbg(lp->dev, "total number of sgs: %d\n", sg_n);
if (sg_alloc_table(sgt, sg_n, GFP_ATOMIC)) {
dev_dbg(lp->dev, "Failed to allocate scatterlist\n");
return -ENOMEM;
}
for_each_sg(sgt->sgl, sg, sgt->orig_nents, i) {
if (i == 0) {
dev_dbg(lp->dev, "Setting buffer 0x%p with length %d\n", skb->data,
sg_n == 1 ? skb->len : skb->data_len);
sg_set_buf(sg, skb->data, sg_n == 1 ? skb->len : skb->data_len);
} else {
frag = &skb_shinfo(skb)->frags[i - 1];
dev_dbg(lp->dev, "Setting page 0x%p with offset %d and size %d\n", skb_frag_page(frag),
frag->page_offset, skb_frag_size(frag));
sg_set_page(sg, skb_frag_page(frag), frag->page_offset, skb_frag_size(frag));
}
}
dev_dbg(lp->dev, "Mapped scatter list for skb 0x%p\n", skb);
return 0;
}
/* DMA map a scatter list.
*
*/
static int maple_bus_map_sgt(struct net_device *netdev, struct sg_table *sgt,
enum dma_transfer_direction direction) {
struct maple_bus_local *lp = netdev_priv(netdev);
struct device *dev;
dev_dbg(lp->dev, "maple_bus_map_sgt starting\n");
if (direction == DMA_MEM_TO_DEV)
dev = lp->global->tx_chan->device->dev;
else if (direction == DMA_DEV_TO_MEM)
dev = lp->global->rx_chan->device->dev;
else
return -EINVAL;
sgt->nents = dma_map_sg(dev, sgt->sgl, sgt->orig_nents, direction);
dev_dbg(lp->dev, "maple_bus_map_sgt mapped %d ents\n", sgt->nents);
return sgt->nents;
}
static int maple_bus_unmap_sgt(struct net_device *netdev, struct sg_table *sgt,
enum dma_transfer_direction direction) {
struct maple_bus_local *lp = netdev_priv(netdev);
struct device *dev;
dev_dbg(lp->dev, "maple_bus_unmap_sgt starting\n");
if (direction == DMA_MEM_TO_DEV)
dev = lp->global->tx_chan->device->dev;
else if (direction == DMA_DEV_TO_MEM)
dev = lp->global->rx_chan->device->dev;
else
return -EINVAL;
//TODO: nents or orig_nents?
dma_unmap_sg(dev, sgt->sgl, sgt->nents, direction);
dev_dbg(lp->dev, "maple_bus_unmap_sgt unmapped\n");
return 0;
}
static int maple_bus_submit_dmadesc(struct net_device *netdev,
struct maple_bus_async_dma_desc *dmadesc) {
struct maple_bus_local *lp = netdev_priv(netdev);
int err;
dev_dbg(lp->dev, "maple_bus_submit_dmadesc submitting txd 0x%p", dmadesc->txd);
dmadesc->cookie = dmaengine_submit(dmadesc->txd);
if ((err = dma_submit_error(dmadesc->cookie))) {
dev_dbg(lp->dev, "maple_bus_submit_dmadesc failed to submit txd");
return err;
}
dev_dbg(lp->dev, "maple_bus_submit_dmadesc submitted txd 0x%p", dmadesc->txd);
return 0;
}
static int maple_bus_free_dmadesc(struct net_device *netdev, struct maple_bus_async_dma_desc *dmadesc) {
struct maple_bus_local *lp = netdev_priv(netdev);
int err;
dev_dbg(lp->dev, "maple_bus_free_dmadesc starting\n");
if ((err = maple_bus_unmap_sgt(netdev, &dmadesc->sgt, dmadesc->direction))) {
dev_dbg(lp->dev, "maple_bus_free_dmadesc failed to unmap sgt\n");
return err;
}
sg_free_table(&dmadesc->sgt);
kfree(dmadesc);
dev_dbg(lp->dev, "maple_bus_free_dmadesc complete\n");
return 0;
}
// Create the DMA descriptor
static int maple_bus_alloc_dmadesc(struct maple_bus_async_dma_desc **dmadescp, struct net_device *netdev,
struct sk_buff *skb, enum dma_transfer_direction direction, dma_async_tx_callback callback) {
struct maple_bus_local *lp = netdev_priv(netdev);
struct maple_bus_async_dma_desc *dmadesc;
struct sg_table *sgt;
int err = -EFAULT;
struct dma_chan *chan;
dev_dbg(lp->dev, "maple_bus_alloc_dmadesc starting\n");
if (direction == DMA_MEM_TO_DEV)
chan = lp->global->tx_chan;
else if (direction == DMA_DEV_TO_MEM)
chan = lp->global->rx_chan;
else
return -EINVAL;
if (!(dmadesc = kzalloc(sizeof(*dmadesc), GFP_ATOMIC))) {
dev_dbg(lp->dev, "Failed to allocate dma desc");
return -ENOMEM;
}
dmadesc->netdev = netdev;
dmadesc->skb = skb;
dmadesc->direction = direction;
sgt = &dmadesc->sgt;
// Map SKB to scattertable
if ((err = maple_bus_sbk_to_sgt(netdev, skb, sgt))) {
goto free_dmadesc;
}
// DMA Map sg_table
if ((err = maple_bus_map_sgt(netdev, &dmadesc->sgt, dmadesc->direction) <= 0)) {
dev_dbg(lp->dev, "Failed to map dma desc");
goto free_sgt;
}
// Allocate txd
dev_dbg(lp->dev, "Prepping SG list");
if (!(dmadesc->txd = dmaengine_prep_slave_sg(chan, sgt->sgl, sgt->nents,
dmadesc->direction, DMA_CTRL_ACK | DMA_PREP_INTERRUPT))) {
dev_dbg(lp->dev, "Failed to prep sg list");
goto free_mapping; //TODO: I don't know how to free a list
}
dmadesc->txd->callback = callback;
dmadesc->txd->callback_param = dmadesc;
*dmadescp = dmadesc;
return 0;
free_mapping:
maple_bus_unmap_sgt(netdev, &dmadesc->sgt, dmadesc->direction);
free_sgt:
sg_free_table(&dmadesc->sgt);
free_dmadesc:
kfree(dmadesc);
return err;
}
// @context tasklet
static void maple_bus_dma_rx_callback(void *arg)
{
struct maple_bus_async_dma_desc *dmadesc = arg;
struct net_device *netdev = dmadesc->netdev;
struct maple_bus_local *lp = netdev_priv(netdev);
struct sk_buff *skb = dmadesc->skb;
enum dma_status status;
dev_dbg(lp->dev, "maple_bus_dma_rx_callback starting\n");
spin_lock(&lp->global->rx_chan_lock);
//TODO: We don't have way of calculating the total number of bytes actually transfered :(
dev_dbg(lp->dev, "maple_bus_dma_rx_callback receive skb 0x%p with data 0x%p of size %d\n", skb,
skb->data, skb->len);
//skb->mac.raw = netdev->dev_addr;
skb->pkt_type = PACKET_HOST;
skb->protocol = htons(ETH_P_MAPLE_BUS);
skb->ip_summed = CHECKSUM_UNNECESSARY; /* don't check it */
// Increment Stats
netdev->stats.rx_packets++;
netdev->stats.rx_bytes += dmadesc->skb->len;
netif_receive_skb(skb);
dmadesc->skb = NULL;
if (maple_bus_free_dmadesc(netdev, dmadesc)) {
dev_dbg(lp->dev, "maple_bus_dma_rx_callback failed to unmap dmadesc\n");
}
spin_lock(&lp->rx_lock);
lp->pending_rx--;
maple_bus_start_rx(netdev);
spin_unlock(&lp->rx_lock);
dev_dbg(lp->dev, "maple_bus_dma_rx_callback complete\n");
unlock:
spin_unlock(&lp->global->rx_chan_lock);
}
static int maple_bus_enqueue_rx(struct net_device *netdev, int size) {
struct maple_bus_local *lp = netdev_priv(netdev);
struct sk_buff *skb;
struct maple_bus_async_dma_desc *dmadesc;
int err;
dev_dbg(lp->dev, "maple_bus_enqueue_rx starting\n");
skb = netdev_alloc_skb(netdev, size);
if (!skb) {
dev_dbg(lp->dev, "maple_bus_enqueue_rx failed to alloc skb\n");
return -ENOMEM;
}
//size += 37; // DMA enqueues 37 bytes!!!!
skb_put(skb, size);
err = maple_bus_alloc_dmadesc(&dmadesc, netdev, skb, DMA_DEV_TO_MEM,
maple_bus_dma_rx_callback);
if (err) {
dev_dbg(lp->dev, "maple_bus_enqueue_rx failed to alloc dmadesc\n");
goto free_skb;
}
skb_tx_timestamp(skb);
if ((err = maple_bus_submit_dmadesc(netdev, dmadesc))) {
goto free_dmadesc;
}
dev_dbg(lp->dev, "maple_bus_enqueue_rx complete\n");
return 0;
free_dmadesc:
//TODO: I don't know how to free the txd
maple_bus_free_dmadesc(netdev, dmadesc);
free_skb:
kfree_skb(skb);
return err;
}
// This is locked by the lp->rx_lock and lp->global->rx_chan_lock
// Enqueue all dma transactions
static int maple_bus_start_rx(struct net_device *netdev) {
struct maple_bus_local *lp = netdev_priv(netdev);
struct maple_bus_chan_totals totals;
struct maple_bus_chan_count counts;
int i = 0, err = 0, enqueued = 0, diff = 0, size = 0;
dev_dbg(lp->dev, "maple_bus_start_rx starting\n");
maple_bus_get_totals(lp, &totals);
maple_bus_get_count(lp, MAPLE_BUS_RX_COUNT_REG, &counts);
if (totals.rx_packets < lp->total_rx) { // We overflowed
diff = (lp->total_rx - 0x0000FFFF) + totals.rx_packets;
} else {
diff = totals.rx_packets - lp->total_rx;
}
dev_dbg(lp->dev, "maple_bus_start_rx enqueuing %d transactions out of (%d/%d).\n", diff,
lp->total_rx, totals.rx_packets);
// The DMA engine fills its buffer with 37 bytes before we have a chance to read the length.
size = counts.length + 40;
for (i = 0; i < diff; ++i) {
if ((err = maple_bus_enqueue_rx(netdev, size))) {
dev_dbg(lp->dev, "maple_bus_start_rx failed to enqueue rx transaction\n");
break;
}
enqueued += 1;
lp->pending_rx++;
}
lp->total_rx = (lp->total_rx + enqueued) % 0x0000FFFF;
dev_dbg(lp->dev, "maple_bus_start_rx issuing %d rx transactions\n", enqueued);
if (enqueued) {
dma_async_issue_pending(lp->global->rx_chan);
}
dev_dbg(lp->dev, "maple_bus_start_rx done with %d total and %d pending rx transactions\n",
lp->total_rx, lp->pending_rx);
return 0;
}
// @context tasklet
static void maple_bus_dma_tx_callback(void *arg)
{
struct maple_bus_async_dma_desc *dmadesc = arg;
struct net_device *netdev = dmadesc->netdev;
struct maple_bus_local *lp = netdev_priv(netdev);
struct sg_table *sgt = &dmadesc->sgt;
enum dma_status status;
struct scatterlist *sg;
int total_len = 0, i = 0;
dev_dbg(lp->dev, "maple_bus_dma_tx_callback starting\n");
spin_lock(&lp->global->tx_chan_lock);
//netif_printk(lp, tx_done, KERN_DEBUG, netdev,
// "cb[%d]->status = 0x%04X\n",
// (int)(((void*)cb - (void*)nic->cbs)/sizeof(struct cb)),
// cb->status);
for_each_sg(sgt->sgl, sg, sgt->orig_nents, i) {
total_len += sg_dma_len(sg);
}
netdev->stats.tx_packets++;
netdev->stats.tx_bytes += total_len;
dev_dbg(lp->dev, "maple_bus_dma_tx_callback consume skb 0x%p\n", dmadesc->skb);
dev_consume_skb_any(dmadesc->skb);
dmadesc->skb = NULL;
maple_bus_free_dmadesc(netdev, dmadesc);
/* Recover from running out of Tx resources in xmit_frame */
if (unlikely(netif_queue_stopped(netdev)))
netif_wake_queue(netdev);
maple_bus_print_status(lp);
dev_dbg(lp->dev, "maple_bus_dma_tx_callback complete\n");
unlock:
spin_unlock(&lp->global->tx_chan_lock);
}
// locked by lp->global->tx_chan_lock
static int maple_bus_xmit_prepare(struct net_device *netdev, struct sk_buff *skb)
{
struct maple_bus_local *lp = netdev_priv(netdev);
struct maple_bus_async_dma_desc *dmadesc;
int err;
dev_dbg(lp->dev, "maple_bus_xmit_prepare starting\n");
err = maple_bus_alloc_dmadesc(&dmadesc, netdev, skb, DMA_MEM_TO_DEV,
maple_bus_dma_tx_callback);
if (err) {
dev_dbg(lp->dev, "maple_bus_xmit_prepare failed to alloc dmadesc\n");
return err;
}
skb_tx_timestamp(skb);
if ((err = maple_bus_submit_dmadesc(netdev, dmadesc))) {
goto free_dmadesc;
}
dev_dbg(lp->dev, "maple_bus_xmit_prepare complete\n");
return 0;
free_dmadesc:
//TODO: I don't know how to free the txd
maple_bus_free_dmadesc(netdev, dmadesc);
return err;
}
static netdev_tx_t maple_bus_net_xmit_frame(struct sk_buff *skb,
struct net_device *netdev)
{
struct maple_bus_local *lp = netdev_priv(netdev);
int err;
dev_dbg(lp->dev, "maple_bus_net_xmit_frame starting transmit of skb 0x%p\n", skb);
maple_bus_print_status(lp);
spin_lock(&lp->global->tx_chan_lock);
err = maple_bus_xmit_prepare(netdev, skb);
spin_unlock(&lp->global->tx_chan_lock);
switch (err) {
case 0:
netif_printk(lp, tx_err, KERN_DEBUG, netdev,
"Packet enqueued\n");
break;
case -ENOSPC:
/* We queued the skb, but now we're out of space. */
netif_printk(lp, tx_err, KERN_DEBUG, netdev,
"No space for CB\n");
netif_stop_queue(netdev);
break;
case -ENOMEM:
/* This is a hard error - log it. */
netif_printk(lp, tx_err, KERN_DEBUG, netdev,
"Out of Tx resources, returning skb\n");
netif_stop_queue(netdev);
return NETDEV_TX_BUSY;
default:
netif_printk(lp, tx_err, KERN_DEBUG, netdev,
"Could not enqueue packet for an unknown reason\n");
netif_stop_queue(netdev);
return NETDEV_TX_BUSY;
}
spin_lock(&lp->global->tx_chan_lock);
dma_async_issue_pending(lp->global->tx_chan);
spin_unlock(&lp->global->tx_chan_lock);
dev_dbg(lp->dev, "maple_bus_net_xmit_frame completed transmit of skb 0x%p\n", skb);
return NETDEV_TX_OK;
}
// @context tasklet
static void maple_bus_irq_rx_callback(unsigned long args)
{
struct net_device *netdev = (struct net_device*)args;
struct maple_bus_local *lp = netdev_priv(netdev);
spin_lock(&lp->global->rx_chan_lock);
spin_lock(&lp->rx_lock); // This is actually kinda dumb. The tasklet will only run once
maple_bus_start_rx(netdev);
/* Unlock the device and we are done */
spin_unlock(&lp->rx_lock);
spin_unlock(&lp->global->rx_chan_lock);
}
static irqreturn_t maple_bus_irq(int irq, void *args)
{
struct net_device *netdev = args;
struct maple_bus_local *lp = netdev_priv(netdev);
u32 status, clear = 0;
irqreturn_t rc = IRQ_NONE;
dev_dbg(lp->dev, "maple_bus_irq starting\n");
maple_bus_print_status(lp);
status = maple_bus_read(lp, MAPLE_BUS_STATUS_REG);
if (status & MAPLE_BUS_TX_IRQ_STATUS) {
dev_dbg(lp->dev, "maple_bus_irq tx IRQ received\n");
clear |= MAPLE_BUS_CLEAR_TX_IRQ;
}
if (status & MAPLE_BUS_RX_IRQ_STATUS) {
dev_dbg(lp->dev, "maple_bus_irq rx IRQ received\n");
clear |= MAPLE_BUS_CLEAR_RX_IRQ;
tasklet_schedule(&lp->tasklet);
}
if (clear) {
dev_dbg(lp->dev, "maple_bus_irq clearing IRQ: 0x%08x\n", clear);
maple_bus_write(lp, MAPLE_BUS_STATUS_REG, clear);
rc = IRQ_HANDLED;
}
return rc;
}
static int maple_bus_up(struct net_device *netdev)
{
struct maple_bus_local *lp = netdev_priv(netdev);
int err;
u32 control = 0;
dev_dbg(lp->dev, "maple_bus_up starting\n");
maple_bus_reset_hw(lp);
dev_dbg(lp->dev, "maple_bus_up requesting irq %d\n", lp->irq);
if ((err = request_irq(lp->irq, maple_bus_irq, 0, netdev->name, netdev))) {
dev_dbg(lp->dev, "Could not allocate interrupt %d.\n", lp->irq);
return -EBUSY;
}
control |= MAPLE_BUS_ENABLE_RX | MAPLE_BUS_ENABLE_TX;
control |= MAPLE_BUS_ENABLE_TX_IRQ | MAPLE_BUS_ENABLE_RX_IRQ;
if (lp->global->loopback) {
control |= MAPLE_BUS_ENABLE_LOOPBACK;
}
maple_bus_write(lp, MAPLE_BUS_CONTROL_REG, control);
maple_bus_print_status(lp);
dev_dbg(lp->dev, "maple_bus_up starting queue\n");
netif_wake_queue(netdev);
dev_dbg(lp->dev, "maple_bus_up started\n");
return 0;
}
static void maple_bus_down(struct net_device *netdev)
{
struct maple_bus_local *lp = netdev_priv(netdev);
dev_dbg(lp->dev, "maple_bus_down starting\n");
maple_bus_print_status(lp);
netif_stop_queue(netdev);
dev_dbg(lp->dev, "Killing tasklet \n");
tasklet_kill(&lp->tasklet);
maple_bus_reset_hw(lp);
dev_dbg(lp->dev, "maple_bus_down freeing irq %d\n", lp->irq);
free_irq(lp->irq, netdev);
//del_timer_sync(&nic->watchdog);
netif_carrier_off(netdev);
maple_bus_print_status(lp);
dev_dbg(lp->dev, "maple_bus_down done\n");
}
static int maple_bus_net_open(struct net_device *netdev)
{
struct maple_bus_local *lp = netdev_priv(netdev);
int err = 0;
//netif_carrier_off(netdev); //TODO: We might need this once we get sense
if ((err = maple_bus_up(netdev)))
netif_err(lp, ifup, netdev, "Cannot open interface, aborting\n");
return err;
}
static int maple_bus_net_stop(struct net_device *netdev)
{
maple_bus_down(netdev);
return 0;
}
static int maple_bus_net_validate_addr(struct net_device *netdev) {
struct maple_bus_local *lp = netdev_priv(netdev);
dev_dbg(lp->dev, "Validating mac address\n");
return 0;
}
static const struct net_device_ops maple_bus_netdev_ops = {
.ndo_open = maple_bus_net_open,
.ndo_stop = maple_bus_net_stop,
.ndo_start_xmit = maple_bus_net_xmit_frame,
.ndo_validate_addr = maple_bus_net_validate_addr,
//.ndo_set_rx_mode = e100_set_multicast_list,
//.ndo_set_mac_address = e100_set_mac_address,
//.ndo_change_mtu = e100_change_mtu,
//.ndo_do_ioctl = e100_do_ioctl,
//.ndo_tx_timeout = e100_tx_timeout,
//.ndo_poll_controller = e100_netpoll,
//.ndo_set_features = e100_set_features,
};
static void maple_bus_net_init(struct net_device *netdev) {
netdev->tx_queue_len = 10;
netdev->mtu = MAPLE_BUS_DATA_LEN;
netdev->hard_header_len = MAPLE_BUS_HLEN;
netdev->addr_len = MAPLE_BUS_ALEN;
netdev->type = ARPHRD_NONE;
/* keep the default flags, just add NOARP */
netdev->flags |= IFF_NOARP;
netdev->features |= NETIF_F_HW_CSUM | NETIF_F_HIGHDMA; // | NETIF_F_SG
netdev->netdev_ops = &maple_bus_netdev_ops;
//TODO(rrangel): netdev->header_ops = &maple_bus_header_ops;
}
static int maple_bus_probe(struct platform_device *pdev)
{
struct resource *r_irq; /* Interrupt resources */
struct resource *r_mem; /* IO mem resources */
int err;
struct device *dev = &pdev->dev;
struct net_device *netdev;
struct maple_bus_local *lp;
dev_dbg(dev, "Device Tree Probing\n");
netdev = devm_alloc_netdev(dev, sizeof(*lp), "maplebus%d", maple_bus_net_init);
if (IS_ERR(netdev))
return PTR_ERR(netdev);
memset(netdev->dev_addr, 0, MAPLE_BUS_ALEN);
dev_set_drvdata(dev, netdev);
lp = netdev_priv(netdev);
lp->dev = dev;
lp->magic = MAPLE_BUS_LOCAL_MAGIC;
lp->global = gp;
lp->msg_enable = 0x0000;
spin_lock_init(&lp->cmd_lock);
spin_lock_init(&lp->rx_lock);
tasklet_init(&lp->tasklet, maple_bus_irq_rx_callback, (unsigned long)netdev);
/* Get iospace for the device */
if (!(r_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0))) {
dev_dbg(dev, "invalid address\n");
return -ENODEV;
}
/* Remap IO for device */
lp->base_addr = devm_ioremap_resource(dev, r_mem);
if (IS_ERR(lp->base_addr)) {
dev_dbg(dev, "Failed to remap IO\n");
return PTR_ERR(lp->base_addr);
}
/* Make sure we have the correct device */
if ((err = maple_bus_verify_magic(lp))) {
dev_dbg(dev, "failed to validate hw magic\n");
return err;
}
/* Get IRQ for the device */
if (!(r_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0))) {
dev_dbg(dev, "no IRQ found\n");
return -ENODEV;
}
lp->irq = r_irq->start;
maple_bus_print_status(lp);
if ((err = devm_register_netdev(dev, netdev))) {
netif_err(lp, probe, netdev, "Cannot register net device, aborting\n");
return err;
}
return 0;
}
static int maple_bus_remove(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
// All the freeing is handled by devm
dev_set_drvdata(dev, NULL);
return 0;
}
#ifdef CONFIG_OF
static struct of_device_id maple_bus_of_match[] = {
{ .compatible = "ismell.org,rxMapleBus-1.0", },
{ /* end of list */ },
};
MODULE_DEVICE_TABLE(of, maple_bus_of_match);
#else
# define maple_bus_of_match
#endif
static struct platform_driver maple_bus_driver = {
.driver = {
.name = DRIVER_NAME,
.owner = THIS_MODULE,
.of_match_table = maple_bus_of_match,
},
.probe = maple_bus_probe,
.remove = maple_bus_remove,
};
static int __init maple_bus_init(void)
{
int err;
printk("<1>Hello module world.\n");
printk("<1>Module parameters were pause (0x%08x), loopback (0x%08x) and \"%s\"\n", pause, loopback,
mystr);
gp = kzalloc(sizeof(*gp), GFP_ATOMIC);
if (!gp) {
printk(KERN_ALERT "Failed to alloc module buffer\n");
return -EFAULT;
}
spin_lock_init(&gp->rx_chan_lock);
spin_lock_init(&gp->tx_chan_lock);
// TODO: We should find a way to find the DMA IP linked to the maple bus
if ((err = maple_bus_find_dma_channels(gp, 0))) {
printk(KERN_ALERT "Failed to find DMA channels\n");
goto free_global;
}
gp->loopback = loopback;
gp->pause = pause;
if ((err = platform_driver_register(&maple_bus_driver))) {
printk(KERN_ALERT "Failed to register maple_bus_driver\n");
goto free_dma;
}
return 0;
free_dma:
maple_bus_free_dma_channels(gp);
free_global:
kfree(gp);
gp = NULL;
return -EFAULT;
}
static void __exit maple_bus_exit(void)
{
printk(KERN_ALERT "Starting maple bus unload\n");
if (gp) {
printk(KERN_ALERT "unloading platform devices\n");
platform_driver_unregister(&maple_bus_driver);
printk(KERN_ALERT "Freeing DMA channels\n");
maple_bus_free_dma_channels(gp);
kfree(gp);
gp = NULL;
}
printk(KERN_ALERT "Goodbye module world.\n");
}
module_init(maple_bus_init);
module_exit(maple_bus_exit);