aboutsummaryrefslogtreecommitdiffstats
path: root/net/tipc/eth_media.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/tipc/eth_media.c')
-rw-r--r--net/tipc/eth_media.c296
1 files changed, 296 insertions, 0 deletions
diff --git a/net/tipc/eth_media.c b/net/tipc/eth_media.c
new file mode 100644
index 000000000000..b634d7a5640e
--- /dev/null
+++ b/net/tipc/eth_media.c
@@ -0,0 +1,296 @@
1/*
2 * net/tipc/eth_media.c: Ethernet bearer support for TIPC
3 *
4 * Copyright (c) 2003-2005, Ericsson Research Canada
5 * Copyright (c) 2005, Wind River Systems
6 * Copyright (c) 2005-2006, Ericsson AB
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 * Redistributions of source code must retain the above copyright notice, this
13 * list of conditions and the following disclaimer.
14 * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * Neither the names of the copyright holders nor the names of its
18 * contributors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include <net/tipc/tipc.h>
35#include <net/tipc/tipc_bearer.h>
36#include <net/tipc/tipc_msg.h>
37#include <linux/netdevice.h>
38#include <linux/version.h>
39
40#define MAX_ETH_BEARERS 2
41#define TIPC_PROTOCOL 0x88ca
42#define ETH_LINK_PRIORITY 10
43#define ETH_LINK_TOLERANCE TIPC_DEF_LINK_TOL
44
45
46/**
47 * struct eth_bearer - Ethernet bearer data structure
48 * @bearer: ptr to associated "generic" bearer structure
49 * @dev: ptr to associated Ethernet network device
50 * @tipc_packet_type: used in binding TIPC to Ethernet driver
51 */
52
53struct eth_bearer {
54 struct tipc_bearer *bearer;
55 struct net_device *dev;
56 struct packet_type tipc_packet_type;
57};
58
59static struct eth_bearer eth_bearers[MAX_ETH_BEARERS];
60static int eth_started = 0;
61static struct notifier_block notifier;
62
63/**
64 * send_msg - send a TIPC message out over an Ethernet interface
65 */
66
67static int send_msg(struct sk_buff *buf, struct tipc_bearer *tb_ptr,
68 struct tipc_media_addr *dest)
69{
70 struct sk_buff *clone;
71 struct net_device *dev;
72
73 clone = skb_clone(buf, GFP_ATOMIC);
74 if (clone) {
75 clone->nh.raw = clone->data;
76 dev = ((struct eth_bearer *)(tb_ptr->usr_handle))->dev;
77 clone->dev = dev;
78 dev->hard_header(clone, dev, TIPC_PROTOCOL,
79 &dest->dev_addr.eth_addr,
80 dev->dev_addr, clone->len);
81 dev_queue_xmit(clone);
82 }
83 return TIPC_OK;
84}
85
86/**
87 * recv_msg - handle incoming TIPC message from an Ethernet interface
88 *
89 * Routine truncates any Ethernet padding/CRC appended to the message,
90 * and ensures message size matches actual length
91 */
92
93static int recv_msg(struct sk_buff *buf, struct net_device *dev,
94 struct packet_type *pt, struct net_device *orig_dev)
95{
96 struct eth_bearer *eb_ptr = (struct eth_bearer *)pt->af_packet_priv;
97 u32 size;
98
99 if (likely(eb_ptr->bearer)) {
100 size = msg_size((struct tipc_msg *)buf->data);
101 skb_trim(buf, size);
102 if (likely(buf->len == size)) {
103 buf->next = NULL;
104 tipc_recv_msg(buf, eb_ptr->bearer);
105 } else {
106 kfree_skb(buf);
107 }
108 } else {
109 kfree_skb(buf);
110 }
111 return TIPC_OK;
112}
113
114/**
115 * enable_bearer - attach TIPC bearer to an Ethernet interface
116 */
117
118static int enable_bearer(struct tipc_bearer *tb_ptr)
119{
120 struct net_device *dev = dev_base;
121 struct eth_bearer *eb_ptr = &eth_bearers[0];
122 struct eth_bearer *stop = &eth_bearers[MAX_ETH_BEARERS];
123 char *driver_name = strchr((const char *)tb_ptr->name, ':') + 1;
124
125 /* Find device with specified name */
126
127 while (dev && dev->name &&
128 (memcmp(dev->name, driver_name, strlen(dev->name)))) {
129 dev = dev->next;
130 }
131 if (!dev)
132 return -ENODEV;
133
134 /* Find Ethernet bearer for device (or create one) */
135
136 for (;(eb_ptr != stop) && eb_ptr->dev && (eb_ptr->dev != dev); eb_ptr++);
137 if (eb_ptr == stop)
138 return -EDQUOT;
139 if (!eb_ptr->dev) {
140 eb_ptr->dev = dev;
141 eb_ptr->tipc_packet_type.type = __constant_htons(TIPC_PROTOCOL);
142 eb_ptr->tipc_packet_type.dev = dev;
143 eb_ptr->tipc_packet_type.func = recv_msg;
144 eb_ptr->tipc_packet_type.af_packet_priv = eb_ptr;
145 INIT_LIST_HEAD(&(eb_ptr->tipc_packet_type.list));
146 dev_hold(dev);
147 dev_add_pack(&eb_ptr->tipc_packet_type);
148 }
149
150 /* Associate TIPC bearer with Ethernet bearer */
151
152 eb_ptr->bearer = tb_ptr;
153 tb_ptr->usr_handle = (void *)eb_ptr;
154 tb_ptr->mtu = dev->mtu;
155 tb_ptr->blocked = 0;
156 tb_ptr->addr.type = htonl(TIPC_MEDIA_TYPE_ETH);
157 memcpy(&tb_ptr->addr.dev_addr, &dev->dev_addr, ETH_ALEN);
158 return 0;
159}
160
161/**
162 * disable_bearer - detach TIPC bearer from an Ethernet interface
163 *
164 * We really should do dev_remove_pack() here, but this function can not be
165 * called at tasklet level. => Use eth_bearer->bearer as a flag to throw away
166 * incoming buffers, & postpone dev_remove_pack() to eth_media_stop() on exit.
167 */
168
169static void disable_bearer(struct tipc_bearer *tb_ptr)
170{
171 ((struct eth_bearer *)tb_ptr->usr_handle)->bearer = 0;
172}
173
174/**
175 * recv_notification - handle device updates from OS
176 *
177 * Change the state of the Ethernet bearer (if any) associated with the
178 * specified device.
179 */
180
181static int recv_notification(struct notifier_block *nb, unsigned long evt,
182 void *dv)
183{
184 struct net_device *dev = (struct net_device *)dv;
185 struct eth_bearer *eb_ptr = &eth_bearers[0];
186 struct eth_bearer *stop = &eth_bearers[MAX_ETH_BEARERS];
187
188 while ((eb_ptr->dev != dev)) {
189 if (++eb_ptr == stop)
190 return NOTIFY_DONE; /* couldn't find device */
191 }
192 if (!eb_ptr->bearer)
193 return NOTIFY_DONE; /* bearer had been disabled */
194
195 eb_ptr->bearer->mtu = dev->mtu;
196
197 switch (evt) {
198 case NETDEV_CHANGE:
199 if (netif_carrier_ok(dev))
200 tipc_continue(eb_ptr->bearer);
201 else
202 tipc_block_bearer(eb_ptr->bearer->name);
203 break;
204 case NETDEV_UP:
205 tipc_continue(eb_ptr->bearer);
206 break;
207 case NETDEV_DOWN:
208 tipc_block_bearer(eb_ptr->bearer->name);
209 break;
210 case NETDEV_CHANGEMTU:
211 case NETDEV_CHANGEADDR:
212 tipc_block_bearer(eb_ptr->bearer->name);
213 tipc_continue(eb_ptr->bearer);
214 break;
215 case NETDEV_UNREGISTER:
216 case NETDEV_CHANGENAME:
217 tipc_disable_bearer(eb_ptr->bearer->name);
218 break;
219 }
220 return NOTIFY_OK;
221}
222
223/**
224 * eth_addr2str - convert Ethernet address to string
225 */
226
227static char *eth_addr2str(struct tipc_media_addr *a, char *str_buf, int str_size)
228{
229 unchar *addr = (unchar *)&a->dev_addr;
230
231 if (str_size < 18)
232 *str_buf = '\0';
233 else
234 sprintf(str_buf, "%02x:%02x:%02x:%02x:%02x:%02x",
235 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
236 return str_buf;
237}
238
239/**
240 * eth_media_start - activate Ethernet bearer support
241 *
242 * Register Ethernet media type with TIPC bearer code. Also register
243 * with OS for notifications about device state changes.
244 */
245
246int eth_media_start(void)
247{
248 struct tipc_media_addr bcast_addr;
249 int res;
250
251 if (eth_started)
252 return -EINVAL;
253
254 memset(&bcast_addr, 0xff, sizeof(bcast_addr));
255 memset(eth_bearers, 0, sizeof(eth_bearers));
256
257 res = tipc_register_media(TIPC_MEDIA_TYPE_ETH, "eth",
258 enable_bearer, disable_bearer, send_msg,
259 eth_addr2str, &bcast_addr, ETH_LINK_PRIORITY,
260 ETH_LINK_TOLERANCE, TIPC_DEF_LINK_WIN);
261 if (res)
262 return res;
263
264 notifier.notifier_call = &recv_notification;
265 notifier.priority = 0;
266 res = register_netdevice_notifier(&notifier);
267 if (!res)
268 eth_started = 1;
269 return res;
270}
271
272/**
273 * eth_media_stop - deactivate Ethernet bearer support
274 */
275
276void eth_media_stop(void)
277{
278 int i;
279
280 if (!eth_started)
281 return;
282
283 unregister_netdevice_notifier(&notifier);
284 for (i = 0; i < MAX_ETH_BEARERS ; i++) {
285 if (eth_bearers[i].bearer) {
286 eth_bearers[i].bearer->blocked = 1;
287 eth_bearers[i].bearer = 0;
288 }
289 if (eth_bearers[i].dev) {
290 dev_remove_pack(&eth_bearers[i].tipc_packet_type);
291 dev_put(eth_bearers[i].dev);
292 }
293 }
294 memset(&eth_bearers, 0, sizeof(eth_bearers));
295 eth_started = 0;
296}
os: fix checkpatch error' href='/cgit/cgit.cgi/nvidia-tegra-modules.git/commit/drivers/net/ethernet/nvidia/eqos/ethtool.c?h=gpu-paging&id=f530d7a963d41b46bcd9b24b2c8349ba86092e33'>f530d7a96
bc46fbc60
402305809
c5d3999ae

bc46fbc60
4448caec7
bc46fbc60


2c1e06660

bc46fbc60
c5d3999ae







4448caec7
bc46fbc60

















402305809
f530d7a96
bc46fbc60
c5d3999ae




402305809
c5d3999ae

bc46fbc60
4448caec7
bc46fbc60











c5d3999ae
bc46fbc60

c5d3999ae





















bc46fbc60
c5d3999ae
bc46fbc60
b25b3e89f
c5d3999ae
bc46fbc60
402305809
bc46fbc60

402305809
f530d7a96

bc46fbc60
c5d3999ae
bc46fbc60
b25b3e89f
f530d7a96
bc46fbc60

c5d3999ae

















256e984ed

c5d3999ae


























bc46fbc60

77bc11b77



f530d7a96
402305809
2c1e06660
c5d3999ae
2c1e06660

bc46fbc60
c5d3999ae






bc46fbc60
4448caec7
bc46fbc60



bc46fbc60










402305809
f530d7a96
bc46fbc60
402305809
bc46fbc60

4448caec7
bc46fbc60

402305809
bc46fbc60
402305809
f530d7a96
bc46fbc60
402305809
f530d7a96
bc46fbc60


402305809
f530d7a96
402305809
f530d7a96
bc46fbc60

4448caec7
bc46fbc60

bc46fbc60









402305809
bc46fbc60
402305809
bc46fbc60


4448caec7
bc46fbc60
f530d7a96
bc46fbc60

402305809

f530d7a96
bc46fbc60



402305809

f530d7a96
bc46fbc60






4448caec7
bc46fbc60

bc46fbc60











402305809
bc46fbc60
402305809
bc46fbc60

4448caec7
bc46fbc60
f530d7a96
bc46fbc60

402305809

bc46fbc60




4448caec7
bc46fbc60



bc46fbc60











402305809
bc46fbc60
402305809
bc46fbc60
4448caec7
bc46fbc60

f530d7a96
bc46fbc60





4448caec7
bc46fbc60



bc46fbc60










402305809
bc46fbc60
402305809
bc46fbc60
4448caec7
bc46fbc60

f530d7a96
bc46fbc60
4448caec7
bc46fbc60



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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019




























                                                                              

                                                                            
  
                                                                     









                                                                            
                        




                            
                   





                                          












                                                        

                                  


































                                                     
                              
























                                            
                 



                                                     
                          






                                                 

                                              

















                                               
  
 
                                                           

                                               


                                                                 
 
                                             
                             
























                                                    

                             




                                               
                                        

















                                                    

                 

                                            

                  




                                         

                  


                                                

                       





                                       

                  




                                                

                  


                                         

                       





                                              
  
 
                                               
 
                                                   
                                                         
 




















                                                                         

 
                                                    
                                        

                                              







                                                    






                                                      

  
                                              
 
                                                       

 









                                                                           
                                                       
                                                                 
 
                                                       



                                                     
                                             






                                                       
                                                 









                                                                
                                                                        

                                    
                                                                        

                                    
                                             













                                                                           
                                                      
                                                                
 
                                                       

                                                     
                                           


                          


                                                                
                                           





                                                                
                                                 


                                                             
                                                                



                                       
                                               
                            
                                               






                                                             
                                                





                                                             
                                                        


                 
                                             



                   
                                                          

                                                     
                  
 
                                                  
 
                                                                        
                                             
            
                                              

                                                                


                                                                          

                                                                
                

                                                                



                                               
                                                  

 


























                                                                            












                                                                            
                                                                            
 
                                                       

                    

                                
 

                               
 




                                                   
















                                                                            
                                                                            
 
                                                       
                                

                    



                                                                                   

         


                                                   


                   
      










                                                                              
                                                                             
 
                                                       
 
                           
                         
 

                                                        













                                                                       
                                                                            
 
                                                       
                
 

                                 
 



                                                      





                                      


                                            

 
                                                         


                    
                                        

              
                                                                          

                                                                     

                                                           
           
 
                                                        
 
                                        



                   
                                                                


                    
                                        
 
                                                 
                                                        
 
                                        

















                                                                       
                                                    
                                                         
 
                                                       

                                                          
 
                                           


                                                       

                                                                         
 







                                                                         
                                           

















                                                                         
                                                    
                                                         
 




                                                     
                                                       

                                                          
 
                                           











                                                                                
                                                                             

                                   





















                                                                                                        
            
                                                      
 
                                              
                                                               
 
                                                               

                                               
                                          

                                                                                
                               
              
                                                        
                                                                       
                                                  

                               

















                                                                                                       

                               


























                                                                                                             

                                                                  



                                                                      
           
                                                          
                                                     
                                                  

                                                                       
         






                                                                       
 
                                           



                 










                                                            
                                                          
                                                                          
 
                                                       

                     
                                                

                                     
                                           
 
                                                          
                                                                          
 
                                                               
                                                                                


                 
                                                   
                                                                             
                                                                  
                                                                        

         
                                                

 









                                                                       
                                                                             
 
                                                       


                     
                                          
 
                            

                                             

                                                                  
                                                        



                                                     

                                                                     
                                                






                                             
                                          

 











                                                                  
                                                                
 
                                                       

                    
                                             
 
                       

                                           

                                                 




                                  
                                             



                   











                                                        
                                                         
 
                                                       
 
                                      

                                       
                                   





                                              
                                      



                 










                                                                 
                                               
 
                                                       
 
                                      

                                       
                         
 
                                      



                                                    
/* =========================================================================
 * The Synopsys DWC ETHER QOS Software Driver and documentation (hereinafter
 * "Software") is an unsupported proprietary work of Synopsys, Inc. unless
 * otherwise expressly agreed to in writing between Synopsys and you.
 *
 * The Software IS NOT an item of Licensed Software or Licensed Product under
 * any End User Software License Agreement or Agreement for Licensed Product
 * with Synopsys or any supplement thereto.  Permission is hereby granted,
 * free of charge, to any person obtaining a copy of this software annotated
 * with this license and the Software, to deal in the Software without
 * restriction, including without limitation the rights to use, copy, modify,
 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so, subject
 * to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS 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.
 * =========================================================================
 */
/*
 * Copyright (c) 2015-2021, NVIDIA CORPORATION.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 */
/*!@file: eqos_ethtool.c
 * @brief: Driver functions.
 */
#include "yheader.h"
#include "ethtool.h"

struct eqos_stats {
	char stat_string[ETH_GSTRING_LEN];
	int sizeof_stat;
	int stat_offset;
};

/* HW extra status */
#define EQOS_EXTRA_STAT(m) \
	{#m, FIELD_SIZEOF(struct eqos_extra_stats, m), \
	offsetof(struct eqos_prv_data, xstats.m)}

static const struct eqos_stats eqos_gstrings_stats[] = {
	EQOS_EXTRA_STAT(q_re_alloc_rx_buf_failed[0]),
	EQOS_EXTRA_STAT(q_re_alloc_rx_buf_failed[1]),
	EQOS_EXTRA_STAT(q_re_alloc_rx_buf_failed[2]),
	EQOS_EXTRA_STAT(q_re_alloc_rx_buf_failed[3]),
	EQOS_EXTRA_STAT(q_re_alloc_rx_buf_failed[4]),
	EQOS_EXTRA_STAT(q_re_alloc_rx_buf_failed[5]),
	EQOS_EXTRA_STAT(q_re_alloc_rx_buf_failed[6]),
	EQOS_EXTRA_STAT(q_re_alloc_rx_buf_failed[7]),

	/* Tx/Rx IRQ error info */
	EQOS_EXTRA_STAT(tx_process_stopped_irq_n[0]),
	EQOS_EXTRA_STAT(tx_process_stopped_irq_n[1]),
	EQOS_EXTRA_STAT(tx_process_stopped_irq_n[2]),
	EQOS_EXTRA_STAT(tx_process_stopped_irq_n[3]),
	EQOS_EXTRA_STAT(tx_process_stopped_irq_n[4]),
	EQOS_EXTRA_STAT(tx_process_stopped_irq_n[5]),
	EQOS_EXTRA_STAT(tx_process_stopped_irq_n[6]),
	EQOS_EXTRA_STAT(tx_process_stopped_irq_n[7]),
	EQOS_EXTRA_STAT(rx_process_stopped_irq_n[0]),
	EQOS_EXTRA_STAT(rx_process_stopped_irq_n[1]),
	EQOS_EXTRA_STAT(rx_process_stopped_irq_n[2]),
	EQOS_EXTRA_STAT(rx_process_stopped_irq_n[3]),
	EQOS_EXTRA_STAT(rx_process_stopped_irq_n[4]),
	EQOS_EXTRA_STAT(rx_process_stopped_irq_n[5]),
	EQOS_EXTRA_STAT(rx_process_stopped_irq_n[6]),
	EQOS_EXTRA_STAT(rx_process_stopped_irq_n[7]),
	EQOS_EXTRA_STAT(tx_buf_unavailable_irq_n[0]),
	EQOS_EXTRA_STAT(tx_buf_unavailable_irq_n[1]),
	EQOS_EXTRA_STAT(tx_buf_unavailable_irq_n[2]),
	EQOS_EXTRA_STAT(tx_buf_unavailable_irq_n[3]),
	EQOS_EXTRA_STAT(tx_buf_unavailable_irq_n[4]),
	EQOS_EXTRA_STAT(tx_buf_unavailable_irq_n[5]),
	EQOS_EXTRA_STAT(tx_buf_unavailable_irq_n[6]),
	EQOS_EXTRA_STAT(tx_buf_unavailable_irq_n[7]),
	EQOS_EXTRA_STAT(rx_buf_unavailable_irq_n[0]),
	EQOS_EXTRA_STAT(rx_buf_unavailable_irq_n[1]),
	EQOS_EXTRA_STAT(rx_buf_unavailable_irq_n[2]),
	EQOS_EXTRA_STAT(rx_buf_unavailable_irq_n[3]),
	EQOS_EXTRA_STAT(rx_buf_unavailable_irq_n[4]),
	EQOS_EXTRA_STAT(rx_buf_unavailable_irq_n[5]),
	EQOS_EXTRA_STAT(rx_buf_unavailable_irq_n[6]),
	EQOS_EXTRA_STAT(rx_buf_unavailable_irq_n[7]),
	EQOS_EXTRA_STAT(rx_watchdog_irq_n),
	EQOS_EXTRA_STAT(fatal_bus_error_irq_n),
	EQOS_EXTRA_STAT(pmt_irq_n),
	/* Tx/Rx IRQ Events */
	EQOS_EXTRA_STAT(tx_normal_irq_n[0]),
	EQOS_EXTRA_STAT(tx_normal_irq_n[1]),
	EQOS_EXTRA_STAT(tx_normal_irq_n[2]),
	EQOS_EXTRA_STAT(tx_normal_irq_n[3]),
	EQOS_EXTRA_STAT(tx_normal_irq_n[4]),
	EQOS_EXTRA_STAT(tx_normal_irq_n[5]),
	EQOS_EXTRA_STAT(tx_normal_irq_n[6]),
	EQOS_EXTRA_STAT(tx_normal_irq_n[7]),
	EQOS_EXTRA_STAT(rx_normal_irq_n[0]),
	EQOS_EXTRA_STAT(rx_normal_irq_n[1]),
	EQOS_EXTRA_STAT(rx_normal_irq_n[2]),
	EQOS_EXTRA_STAT(rx_normal_irq_n[3]),
	EQOS_EXTRA_STAT(rx_normal_irq_n[4]),
	EQOS_EXTRA_STAT(rx_normal_irq_n[5]),
	EQOS_EXTRA_STAT(rx_normal_irq_n[6]),
	EQOS_EXTRA_STAT(rx_normal_irq_n[7]),
	EQOS_EXTRA_STAT(napi_poll_n),
	EQOS_EXTRA_STAT(tx_clean_n[0]),
	EQOS_EXTRA_STAT(tx_clean_n[1]),
	EQOS_EXTRA_STAT(tx_clean_n[2]),
	EQOS_EXTRA_STAT(tx_clean_n[3]),
	EQOS_EXTRA_STAT(tx_clean_n[4]),
	EQOS_EXTRA_STAT(tx_clean_n[5]),
	EQOS_EXTRA_STAT(tx_clean_n[6]),
	EQOS_EXTRA_STAT(tx_clean_n[7]),
	/* EEE */
	EQOS_EXTRA_STAT(tx_path_in_lpi_mode_irq_n),
	EQOS_EXTRA_STAT(tx_path_exit_lpi_mode_irq_n),
	EQOS_EXTRA_STAT(rx_path_in_lpi_mode_irq_n),
	EQOS_EXTRA_STAT(rx_path_exit_lpi_mode_irq_n),
	/* Tx/Rx frames */
	EQOS_EXTRA_STAT(tx_pkt_n),
	EQOS_EXTRA_STAT(rx_pkt_n),
	EQOS_EXTRA_STAT(tx_vlan_pkt_n),
	EQOS_EXTRA_STAT(rx_vlan_pkt_n),
	EQOS_EXTRA_STAT(tx_timestamp_captured_n),
	EQOS_EXTRA_STAT(rx_timestamp_captured_n),
	EQOS_EXTRA_STAT(tx_tso_pkt_n),

	/* Tx/Rx frames per channels/queues */
	EQOS_EXTRA_STAT(q_tx_pkt_n[0]),
	EQOS_EXTRA_STAT(q_tx_pkt_n[1]),
	EQOS_EXTRA_STAT(q_tx_pkt_n[2]),
	EQOS_EXTRA_STAT(q_tx_pkt_n[3]),
	EQOS_EXTRA_STAT(q_tx_pkt_n[4]),
	EQOS_EXTRA_STAT(q_tx_pkt_n[5]),
	EQOS_EXTRA_STAT(q_tx_pkt_n[6]),
	EQOS_EXTRA_STAT(q_tx_pkt_n[7]),
	EQOS_EXTRA_STAT(q_rx_pkt_n[0]),
	EQOS_EXTRA_STAT(q_rx_pkt_n[1]),
	EQOS_EXTRA_STAT(q_rx_pkt_n[2]),
	EQOS_EXTRA_STAT(q_rx_pkt_n[3]),
	EQOS_EXTRA_STAT(q_rx_pkt_n[4]),
	EQOS_EXTRA_STAT(q_rx_pkt_n[5]),
	EQOS_EXTRA_STAT(q_rx_pkt_n[6]),
	EQOS_EXTRA_STAT(q_rx_pkt_n[7]),
	EQOS_EXTRA_STAT(link_disconnect_count),
	EQOS_EXTRA_STAT(link_connect_count),
};

#define EQOS_EXTRA_STAT_LEN ARRAY_SIZE(eqos_gstrings_stats)

/* HW MAC Management counters (if supported) */
#define EQOS_MMC_STAT(m)	\
	{ #m, FIELD_SIZEOF(struct eqos_mmc_counters, m),	\
	offsetof(struct eqos_prv_data, mmc.m)}

static const struct eqos_stats eqos_mmc[] = {
	/* MMC TX counters */
	EQOS_MMC_STAT(mmc_tx_octetcount_gb),
	EQOS_MMC_STAT(mmc_tx_framecount_gb),
	EQOS_MMC_STAT(mmc_tx_broadcastframe_g),
	EQOS_MMC_STAT(mmc_tx_multicastframe_g),
	EQOS_MMC_STAT(mmc_tx_64_octets_gb),
	EQOS_MMC_STAT(mmc_tx_65_to_127_octets_gb),
	EQOS_MMC_STAT(mmc_tx_128_to_255_octets_gb),
	EQOS_MMC_STAT(mmc_tx_256_to_511_octets_gb),
	EQOS_MMC_STAT(mmc_tx_512_to_1023_octets_gb),
	EQOS_MMC_STAT(mmc_tx_1024_to_max_octets_gb),
	EQOS_MMC_STAT(mmc_tx_unicast_gb),
	EQOS_MMC_STAT(mmc_tx_multicast_gb),
	EQOS_MMC_STAT(mmc_tx_broadcast_gb),
	EQOS_MMC_STAT(mmc_tx_underflow_error),
	EQOS_MMC_STAT(mmc_tx_singlecol_g),
	EQOS_MMC_STAT(mmc_tx_multicol_g),
	EQOS_MMC_STAT(mmc_tx_deferred),
	EQOS_MMC_STAT(mmc_tx_latecol),
	EQOS_MMC_STAT(mmc_tx_exesscol),
	EQOS_MMC_STAT(mmc_tx_carrier_error),
	EQOS_MMC_STAT(mmc_tx_octetcount_g),
	EQOS_MMC_STAT(mmc_tx_framecount_g),
	EQOS_MMC_STAT(mmc_tx_excessdef),
	EQOS_MMC_STAT(mmc_tx_pause_frame),
	EQOS_MMC_STAT(mmc_tx_vlan_frame_g),

	/* MMC RX counters */
	EQOS_MMC_STAT(mmc_rx_framecount_gb),
	EQOS_MMC_STAT(mmc_rx_octetcount_gb),
	EQOS_MMC_STAT(mmc_rx_octetcount_g),
	EQOS_MMC_STAT(mmc_rx_broadcastframe_g),
	EQOS_MMC_STAT(mmc_rx_multicastframe_g),
	EQOS_MMC_STAT(mmc_rx_crc_error),
	EQOS_MMC_STAT(mmc_rx_align_error),
	EQOS_MMC_STAT(mmc_rx_run_error),
	EQOS_MMC_STAT(mmc_rx_jabber_error),
	EQOS_MMC_STAT(mmc_rx_undersize_g),
	EQOS_MMC_STAT(mmc_rx_oversize_g),
	EQOS_MMC_STAT(mmc_rx_64_octets_gb),
	EQOS_MMC_STAT(mmc_rx_65_to_127_octets_gb),
	EQOS_MMC_STAT(mmc_rx_128_to_255_octets_gb),
	EQOS_MMC_STAT(mmc_rx_256_to_511_octets_gb),
	EQOS_MMC_STAT(mmc_rx_512_to_1023_octets_gb),
	EQOS_MMC_STAT(mmc_rx_1024_to_max_octets_gb),
	EQOS_MMC_STAT(mmc_rx_unicast_g),
	EQOS_MMC_STAT(mmc_rx_length_error),
	EQOS_MMC_STAT(mmc_rx_outofrangetype),
	EQOS_MMC_STAT(mmc_rx_pause_frames),
	EQOS_MMC_STAT(mmc_rx_fifo_overflow),
	EQOS_MMC_STAT(mmc_rx_vlan_frames_gb),
	EQOS_MMC_STAT(mmc_rx_watchdog_error),

	/* IPC */
	EQOS_MMC_STAT(mmc_rx_ipc_intr_mask),
	EQOS_MMC_STAT(mmc_rx_ipc_intr),

	/* IPv4 */
	EQOS_MMC_STAT(mmc_rx_ipv4_gd),
	EQOS_MMC_STAT(mmc_rx_ipv4_hderr),
	EQOS_MMC_STAT(mmc_rx_ipv4_nopay),
	EQOS_MMC_STAT(mmc_rx_ipv4_frag),
	EQOS_MMC_STAT(mmc_rx_ipv4_udsbl),

	/* IPV6 */
	EQOS_MMC_STAT(mmc_rx_ipv6_gd_octets),
	EQOS_MMC_STAT(mmc_rx_ipv6_hderr_octets),
	EQOS_MMC_STAT(mmc_rx_ipv6_nopay_octets),

	/* Protocols */
	EQOS_MMC_STAT(mmc_rx_udp_gd),
	EQOS_MMC_STAT(mmc_rx_udp_err),
	EQOS_MMC_STAT(mmc_rx_tcp_gd),
	EQOS_MMC_STAT(mmc_rx_tcp_err),
	EQOS_MMC_STAT(mmc_rx_icmp_gd),
	EQOS_MMC_STAT(mmc_rx_icmp_err),

	/* IPv4 */
	EQOS_MMC_STAT(mmc_rx_ipv4_gd_octets),
	EQOS_MMC_STAT(mmc_rx_ipv4_hderr_octets),
	EQOS_MMC_STAT(mmc_rx_ipv4_nopay_octets),
	EQOS_MMC_STAT(mmc_rx_ipv4_frag_octets),
	EQOS_MMC_STAT(mmc_rx_ipv4_udsbl_octets),

	/* IPV6 */
	EQOS_MMC_STAT(mmc_rx_ipv6_gd),
	EQOS_MMC_STAT(mmc_rx_ipv6_hderr),
	EQOS_MMC_STAT(mmc_rx_ipv6_nopay),

	/* Protocols */
	EQOS_MMC_STAT(mmc_rx_udp_gd_octets),
	EQOS_MMC_STAT(mmc_rx_udp_err_octets),
	EQOS_MMC_STAT(mmc_rx_tcp_gd_octets),
	EQOS_MMC_STAT(mmc_rx_tcp_err_octets),
	EQOS_MMC_STAT(mmc_rx_icmp_gd_octets),
	EQOS_MMC_STAT(mmc_rx_icmp_err_octets),
};

#define EQOS_MMC_STATS_LEN ARRAY_SIZE(eqos_mmc)

static int eqos_get_ts_info(struct net_device *net,
			    struct ethtool_ts_info *info)
{
	info->so_timestamping =
	    SOF_TIMESTAMPING_TX_SOFTWARE |
	    SOF_TIMESTAMPING_RX_SOFTWARE |
	    SOF_TIMESTAMPING_SOFTWARE |
	    SOF_TIMESTAMPING_TX_HARDWARE |
	    SOF_TIMESTAMPING_RX_HARDWARE | SOF_TIMESTAMPING_RAW_HARDWARE;
	info->phc_index = 0;

	info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);

	info->rx_filters = 1 << HWTSTAMP_FILTER_NONE;
	info->rx_filters |=
	    (1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC) |
	    (1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) |
	    (1 << HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
	    (1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
	    (1 << HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) |
	    (1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) |
	    (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);

	return 0;
}

static const struct ethtool_ops eqos_ethtool_ops = {
	.get_link = ethtool_op_get_link,
	.get_pauseparam = eqos_get_pauseparam,
	.set_pauseparam = eqos_set_pauseparam,
	.get_wol = eqos_get_wol,
	.set_wol = eqos_set_wol,
	.get_coalesce = eqos_get_coalesce,
	.set_coalesce = eqos_set_coalesce,
	.get_ethtool_stats = eqos_get_ethtool_stats,
	.get_strings = eqos_get_strings,
	.get_sset_count = eqos_get_sset_count,
	.get_ts_info = eqos_get_ts_info,
#if LINUX_VERSION_CODE > KERNEL_VERSION(4, 9, 0)
	.get_link_ksettings = eqos_get_link_ksettings,
	.set_link_ksettings = eqos_set_link_ksettings,
#else
	.get_settings = eqos_getsettings,
	.set_settings = eqos_setsettings,
#endif
};

struct ethtool_ops *eqos_get_ethtool_ops(void)
{
	return (struct ethtool_ops *)&eqos_ethtool_ops;
}

/*!
 * \details This function is invoked by kernel when user request to get the
 * pause parameters through standard ethtool command.
 *
 * \param[in] dev – pointer to net device structure.
 * \param[in] Pause – pointer to ethtool_pauseparam structure.
 *
 * \return void
 */

static void eqos_get_pauseparam(struct net_device *dev,
				struct ethtool_pauseparam *pause)
{
	struct eqos_prv_data *pdata = netdev_priv(dev);
	struct hw_if_struct *hw_if = &(pdata->hw_if);
	struct phy_device *phydev = pdata->phydev;
	unsigned int data;

	pr_debug("-->eqos_get_pauseparam\n");

	pause->rx_pause = 0;
	pause->tx_pause = 0;

	if (pdata->hw_feat.pcs_sel) {
		pause->autoneg = 1;
		data = hw_if->get_an_adv_pause_param();
		if (!(data == 1) && !(data == 2))
			return;
	} else {
		pause->autoneg = pdata->phydev->autoneg;

		/* return if PHY doesn't support FLOW ctrl */
		if (!(phydev->supported & SUPPORTED_Pause) ||
		    !(phydev->supported & SUPPORTED_Asym_Pause))
			return;
	}

	if ((pdata->flow_ctrl & EQOS_FLOW_CTRL_RX) == EQOS_FLOW_CTRL_RX)
		pause->rx_pause = 1;

	if ((pdata->flow_ctrl & EQOS_FLOW_CTRL_TX) == EQOS_FLOW_CTRL_TX)
		pause->tx_pause = 1;

	pr_debug("<--eqos_get_pauseparam\n");
}

/*!
 * \details This function is invoked by kernel when user request to set the
 * pause parameters through standard ethtool command.
 *
 * \param[in] dev – pointer to net device structure.
 * \param[in] pause – pointer to ethtool_pauseparam structure.
 *
 * \return int
 *
 * \retval zero on success and -ve number on failure.
 */

static int eqos_set_pauseparam(struct net_device *dev,
			       struct ethtool_pauseparam *pause)
{
	struct eqos_prv_data *pdata = netdev_priv(dev);
	struct hw_if_struct *hw_if = &(pdata->hw_if);
	struct phy_device *phydev = pdata->phydev;
	int new_pause = EQOS_FLOW_CTRL_OFF;
	unsigned int data;
	int ret = 0;

	if (pdata->dt_cfg.pause_frames == PAUSE_FRAMES_DISABLED)
		return -EOPNOTSUPP;

	pr_debug("-->eqos_set_pauseparam: "
	      "autoneg = %d tx_pause = %d rx_pause = %d\n",
	      pause->autoneg, pause->tx_pause, pause->rx_pause);

	/* return if PHY doesn't support FLOW ctrl */
	if (pdata->hw_feat.pcs_sel) {
		data = hw_if->get_an_adv_pause_param();
		if (!(data == 1) && !(data == 2))
			return -EINVAL;
	} else {
		if (!(phydev->supported & SUPPORTED_Pause) ||
		    !(phydev->supported & SUPPORTED_Asym_Pause))
			return -EINVAL;
	}

	if (pause->rx_pause)
		new_pause |= EQOS_FLOW_CTRL_RX;
	if (pause->tx_pause)
		new_pause |= EQOS_FLOW_CTRL_TX;

	if (new_pause == pdata->flow_ctrl && !pause->autoneg)
		return -EINVAL;

	pdata->flow_ctrl = new_pause;

	if (pdata->hw_feat.pcs_sel) {
		eqos_configure_flow_ctrl(pdata);
	} else {
		phydev->autoneg = pause->autoneg;
		if (phydev->autoneg) {
			if (netif_running(dev))
				ret = phy_start_aneg(phydev);
		} else {
			eqos_configure_flow_ctrl(pdata);
		}
	}

	pr_debug("<--eqos_set_pauseparam\n");

	return ret;
}

void eqos_configure_flow_ctrl(struct eqos_prv_data *pdata)
{
	struct hw_if_struct *hw_if = &(pdata->hw_if);
	UINT qinx;

	pr_debug("-->eqos_configure_flow_ctrl\n");

	if ((pdata->flow_ctrl & EQOS_FLOW_CTRL_RX) == EQOS_FLOW_CTRL_RX)
		hw_if->enable_rx_flow_ctrl();
	else
		hw_if->disable_rx_flow_ctrl();

	/* As ethtool does not provide queue level configuration
	 * Tx flow control is disabled/enabled for all transmit queues
	 */
	if ((pdata->flow_ctrl & EQOS_FLOW_CTRL_TX) == EQOS_FLOW_CTRL_TX) {
		for (qinx = 0; qinx < EQOS_TX_QUEUE_CNT; qinx++)
			hw_if->enable_tx_flow_ctrl(qinx);
	} else {
		for (qinx = 0; qinx < EQOS_TX_QUEUE_CNT; qinx++)
			hw_if->disable_tx_flow_ctrl(qinx);
	}

	pdata->oldflow_ctrl = pdata->flow_ctrl;

	pr_debug("<--eqos_configure_flow_ctrl\n");
}

#if LINUX_VERSION_CODE > KERNEL_VERSION(4, 9, 0)
static int eqos_get_link_ksettings(struct net_device *dev,
                                   struct ethtool_link_ksettings *cmd)
{
        struct eqos_prv_data *pdata = netdev_priv(dev);

        if (!netif_running(dev))
                return -EINVAL;

        if (!pdata->phydev)
                return -ENODEV;

        phy_ethtool_ksettings_get(pdata->phydev, cmd);

        return 0;
}

static int eqos_set_link_ksettings(struct net_device *dev,
                                   const struct ethtool_link_ksettings *cmd)
{
        struct eqos_prv_data *pdata = netdev_priv(dev);

        return phy_ethtool_ksettings_set(pdata->phydev, cmd);
}

#else

/*!
 * \details This function is invoked by kernel when user request to get the
 * various device settings through standard ethtool command. This function
 * support to get the PHY related settings like link status, interface type,
 * auto-negotiation parameters and pause parameters etc.
 *
 * \param[in] dev – pointer to net device structure.
 * \param[in] cmd – pointer to ethtool_cmd structure.
 *
 * \return int
 *
 * \retval zero on success and -ve number on failure.
 */
static int eqos_getsettings(struct net_device *dev, struct ethtool_cmd *cmd)
{
	struct eqos_prv_data *pdata = netdev_priv(dev);
	int ret = 0;

	if (!netif_running(dev))
		return -EINVAL;

	if (!pdata->phydev)
		return -ENODEV;

	cmd->transceiver = XCVR_EXTERNAL;

	spin_lock_irq(&pdata->lock);
	ret = phy_ethtool_gset(pdata->phydev, cmd);
	spin_unlock_irq(&pdata->lock);

	return ret;
}

/*!
 * \details This function is invoked by kernel when user request to set the
 * various device settings through standard ethtool command. This function
 * support to set the PHY related settings like link status, interface type,
 * auto-negotiation parameters and pause parameters etc.
 *
 * \param[in] dev – pointer to net device structure.
 * \param[in] cmd – pointer to ethtool_cmd structure.
 *
 * \return int
 *
 * \retval zero on success and -ve number on failure.
 */
static int eqos_setsettings(struct net_device *dev, struct ethtool_cmd *cmd)
{
	struct eqos_prv_data *pdata = netdev_priv(dev);
	u8 duplex = cmd->duplex;
	int ret = 0;

	if (pdata->num_chans == MAX_CHANS &&
	    duplex == DUPLEX_HALF) {
		netdev_err(dev, "Half duplex mode not allowed in multi-channel\n");
		return -ENOTSUPP;
	}

	spin_lock_irq(&pdata->lock);
	ret = phy_ethtool_sset(pdata->phydev, cmd);
	spin_unlock_irq(&pdata->lock);

	return ret;
}
#endif

/*!
 * \details This function is invoked by kernel when user request to get report
 * whether wake-on-lan is enable or not.
 *
 * \param[in] dev – pointer to net device structure.
 * \param[in] wol – pointer to ethtool_wolinfo structure.
 *
 * \return void
 */

static void eqos_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
{
	struct eqos_prv_data *pdata = netdev_priv(dev);

	wol->supported = 0;
	wol->wolopts = 0;

	if (pdata->phydev)
		phy_ethtool_get_wol(pdata->phydev, wol);
}

/*!
 * \details This function is invoked by kernel when user request to set
 * pmt parameters for remote wakeup or magic wakeup
 *
 * \param[in] dev – pointer to net device structure.
 * \param[in] wol – pointer to ethtool_wolinfo structure.
 *
 * \return int
 *
 * \retval zero on success and -ve number on failure.
 */

static int eqos_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
{
	struct eqos_prv_data *pdata = netdev_priv(dev);
	int ret;

	if (!pdata->phydev)
		return -ENOTSUPP;

	ret = phy_ethtool_set_wol(pdata->phydev, wol);
	if (ret < 0)
		return ret;

	/* Save WoL state */
	if (wol->wolopts & WAKE_MAGIC)
		pdata->wolopts = 1;
	else
		pdata->wolopts = 0;

	device_init_wakeup(&dev->dev, true);

	return ret;
}

u32 eqos_usec2riwt(u32 usec, struct eqos_prv_data *pdata)
{
	u32 ret = 0;

	pr_debug("-->eqos_usec2riwt\n");

	/* Eg:
	 * AXI System clock is 125 MHz, each clock cycle would then be 8ns
	 * For value 0x1 in watchdog timer, device would wait for 256
	 * clock cycles,
	 * ie, (8ns x 256) => 2.048us (rounding off to 2us)
	 * So below is the formula with above values
	 */

	ret = (usec * (EQOS_AXI_CLOCK / 1000000)) / 256;

	pr_debug("<--eqos_usec2riwt\n");

	return ret;
}

static u32 eqos_riwt2usec(u32 riwt, struct eqos_prv_data *pdata)
{
	u32 ret = 0;

	pr_debug("-->eqos_riwt2usec\n");

	/* using formula from 'eqos_usec2riwt' */
	ret = (riwt * 256) / (EQOS_AXI_CLOCK / 1000000);

	pr_debug("<--eqos_riwt2usec\n");

	return ret;
}

/*!
 * \details This function is invoked by kernel when user request to get
 * interrupt coalescing parameters. As coalescing parameters are same
 * for all the channels, so this function will get coalescing
 * details from channel zero and return.
 *
 * \param[in] dev – pointer to net device structure.
 * \param[in] wol – pointer to ethtool_coalesce structure.
 *
 * \return int
 *
 * \retval 0
 */

static int eqos_get_coalesce(struct net_device *dev,
			     struct ethtool_coalesce *ec)
{
	struct eqos_prv_data *pdata = netdev_priv(dev);
	struct rx_ring *prx_ring = GET_RX_WRAPPER_DESC(0);
	struct tx_ring *ptx_ring = GET_TX_WRAPPER_DESC(0);

	pr_debug("-->eqos_get_coalesce\n");

	memset(ec, 0, sizeof(struct ethtool_coalesce));

	ec->rx_coalesce_usecs = eqos_riwt2usec(prx_ring->rx_riwt, pdata);
	ec->rx_max_coalesced_frames = prx_ring->rx_coal_frames;

	ec->rx_coalesce_usecs = eqos_riwt2usec(prx_ring->rx_riwt, pdata);
	ec->rx_max_coalesced_frames = prx_ring->rx_coal_frames;

	if (ptx_ring->use_tx_usecs)
		ec->tx_coalesce_usecs = ptx_ring->tx_usecs;
	if (ptx_ring->use_tx_frames)
		ec->tx_max_coalesced_frames = ptx_ring->tx_coal_frames;

	pr_debug("<--eqos_get_coalesce\n");

	return 0;
}

/*!
 * \details This function is invoked by kernel when user request to set
 * interrupt coalescing parameters. This driver maintains same coalescing
 * parameters for all the channels, hence same changes will be applied to
 * all the channels.
 *
 * \param[in] dev – pointer to net device structure.
 * \param[in] wol – pointer to ethtool_coalesce structure.
 *
 * \return int
 *
 * \retval zero on success and -ve number on failure.
 */

static int eqos_set_coalesce(struct net_device *dev,
			     struct ethtool_coalesce *ec)
{
	unsigned int rx_riwt, rx_usec, qinx;
	bool use_tx_usecs = EQOS_COAELSCING_DISABLE;
	bool use_tx_frames = EQOS_COAELSCING_DISABLE;
	bool use_rx_usecs = EQOS_COAELSCING_DISABLE;
	bool use_rx_frames = EQOS_COAELSCING_DISABLE;
	struct eqos_prv_data *pdata = netdev_priv(dev);
	struct rx_ring *prx_ring = GET_RX_WRAPPER_DESC(0);
	struct tx_ring *ptx_ring = GET_TX_WRAPPER_DESC(0);

	pr_debug("-->eqos_set_coalesce\n");

	/* Check for not supported parameters  */
	if ((ec->rx_coalesce_usecs_irq) ||
	    (ec->rx_max_coalesced_frames_irq) || (ec->tx_coalesce_usecs_irq) ||
	    (ec->use_adaptive_rx_coalesce) || (ec->use_adaptive_tx_coalesce) ||
	    (ec->pkt_rate_low) || (ec->rx_coalesce_usecs_low) ||
	    (ec->rx_max_coalesced_frames_low) || (ec->tx_coalesce_usecs_high) ||
	    (ec->tx_max_coalesced_frames_low) || (ec->pkt_rate_high) ||
	    (ec->tx_coalesce_usecs_low) || (ec->rx_coalesce_usecs_high) ||
	    (ec->rx_max_coalesced_frames_high) ||
	    (ec->tx_max_coalesced_frames_irq) ||
	    (ec->stats_block_coalesce_usecs) ||
	    (ec->tx_max_coalesced_frames_high) || (ec->rate_sample_interval))
		return -EOPNOTSUPP;

	/* check if we are changing the parameters when interface is already up */
	if (prx_ring->rx_coal_frames != ec->rx_max_coalesced_frames
	    && netif_running(dev)) {
		DBGPR_ETHTOOL("Coalesce frame parameter can be changed only if interface is down\n");
		return -EINVAL;
	}

	if (ec->tx_coalesce_usecs !=  ptx_ring->tx_usecs &&
	    netif_running(dev)) {
		DBGPR_ETHTOOL("Coalesce Tx usec parameter can be changed only if interface is down\n");
		return -EINVAL;
	}

	if (ec->tx_max_coalesced_frames !=  ptx_ring->tx_coal_frames &&
	    netif_running(dev)) {
		DBGPR_ETHTOOL("Coalesce Tx frame parameter can be changed only if interface is down\n");
		return -EINVAL;
	}

	/* Enable Rx usec coalesing only if Rx-usecs is more than 3 usecs. */
	if (ec->rx_coalesce_usecs <= EQOS_MIN_RX_COALESCE_USEC)
		use_rx_usecs = EQOS_COAELSCING_DISABLE;
	else
		use_rx_usecs = EQOS_COAELSCING_ENABLE;

	DBGPR_ETHTOOL("RX COALESCING is %s\n",
		      (use_rx_usecs ? "ENABLED" : "DISABLED"));

	rx_riwt = eqos_usec2riwt(ec->rx_coalesce_usecs, pdata);

	/* Check the bounds of values for RX */
	if (rx_riwt > EQOS_MAX_DMA_RIWT) {
		rx_usec = eqos_riwt2usec(EQOS_MAX_DMA_RIWT, pdata);
		DBGPR_ETHTOOL("RX Coalesing is limited to %d usecs\n", rx_usec);
		return -EINVAL;
	} else
	if (ec->rx_max_coalesced_frames > RX_DESC_CNT) {
		DBGPR_ETHTOOL("RX Coalesing is limited to %d frames\n",
			      EQOS_RX_MAX_FRAMES);
		return -EINVAL;
	}
	if (ec->rx_max_coalesced_frames < EQOS_MIN_RX_COALESCE_FRAMES)
		use_rx_frames = EQOS_COAELSCING_DISABLE;
	else
		use_rx_frames = EQOS_COAELSCING_ENABLE;

	/*  On Rx side we support Rx_usecs and Rx-frames together only  */
	if (use_rx_frames && use_rx_usecs) {
		DBGPR_ETHTOOL("RX COALESCING is Enabled\n");
	} else if (!use_rx_frames && !use_rx_usecs) {
		DBGPR_ETHTOOL("RX COALESCING is Disabled\n");
	} else {
		DBGPR_ETHTOOL("Both Rx-frames and Rx-usecs need to be enabled or disabled together\n");
		return -EINVAL;
	}

	if (ec->tx_coalesce_usecs > EQOS_MAX_TX_COALESCE_USEC) {
		DBGPR_ETHTOOL("TX Coalesing is limited to %d usecs\n",
			      EQOS_MAX_TX_COALESCE_USEC);
		return -EINVAL;
	}

	if (ec->tx_max_coalesced_frames > EQOS_TX_MAX_FRAME) {
		DBGPR_ETHTOOL("TX Coalesing is limited to %d frames\n",
			      EQOS_TX_MAX_FRAME);
		return -EINVAL;
	}

	if (ec->tx_max_coalesced_frames < EQOS_MIN_TX_COALESCE_FRAMES) {
		DBGPR_ETHTOOL("TX-frames COALESCING is disabled\n");
		use_tx_frames = EQOS_COAELSCING_DISABLE;
	} else {
		use_tx_frames = EQOS_COAELSCING_ENABLE;
	}

	if (ec->tx_coalesce_usecs < EQOS_MIN_TX_COALESCE_USEC) {
		DBGPR_ETHTOOL("TX-usecs COALESCING is disabled\n");
		use_tx_usecs = EQOS_COAELSCING_DISABLE;
	} else {
		use_tx_usecs = EQOS_COAELSCING_ENABLE;
	}

	if (use_tx_frames && !use_tx_usecs) {
		DBGPR_ETHTOOL("Tx-usecs coalescing needs to be enabled if Tx-frames coalescing is enabled\n",
			      EQOS_TX_MAX_FRAME);
		return -EINVAL;
	}

	/* The selected parameters are applied to all the
	 * receive queues equally, so all the queue configurations
	 * are in sync. Update software data structure here. We cannot
	 * update hardware here since interface is down at this point.
	 * Hardware will be updated on interface getting up using
	 * "ifconfig eth0 up" after this setting is done.
	 */
	for (qinx = 0; qinx < EQOS_RX_QUEUE_CNT; qinx++) {
		prx_ring = GET_RX_WRAPPER_DESC(qinx);
		prx_ring->use_riwt = use_rx_usecs;
		prx_ring->rx_riwt = rx_riwt;
		prx_ring->rx_coal_frames = ec->rx_max_coalesced_frames;
	}
	for (qinx = 0; qinx < EQOS_TX_QUEUE_CNT; qinx++) {
		ptx_ring = GET_TX_WRAPPER_DESC(qinx);
		ptx_ring->tx_usecs = ec->tx_coalesce_usecs;
		ptx_ring->tx_coal_frames = ec->tx_max_coalesced_frames;
		ptx_ring->use_tx_usecs = use_tx_usecs;
		ptx_ring->use_tx_frames = use_tx_frames;
	}

	pr_debug("<--eqos_set_coalesce\n");

	return 0;
}

/*!
 * \details This function is invoked by kernel when user
 * requests to get the extended statistics about the device.
 *
 * \param[in] dev – pointer to net device structure.
 * \param[in] data – pointer in which extended statistics
 *                   should be put.
 *
 * \return void
 */

static void eqos_get_ethtool_stats(struct net_device *dev,
				   struct ethtool_stats *dummy, u64 *data)
{
	struct eqos_prv_data *pdata = netdev_priv(dev);
	int i, j = 0;

	pr_debug("-->eqos_get_ethtool_stats\n");

	if (pdata->hw_feat.mmc_sel) {
		eqos_mmc_read(&pdata->mmc);

		for (i = 0; i < EQOS_MMC_STATS_LEN; i++) {
			char *p = (char *)pdata + eqos_mmc[i].stat_offset;

			data[j++] = (eqos_mmc[i].sizeof_stat ==
				     sizeof(u64)) ? (*(u64 *) p) : (*(u32 *) p);
		}
	}

	for (i = 0; i < EQOS_EXTRA_STAT_LEN; i++) {
		char *p = (char *)pdata + eqos_gstrings_stats[i].stat_offset;
		data[j++] = (eqos_gstrings_stats[i].sizeof_stat ==
			     sizeof(u64)) ? (*(u64 *) p) : (*(u32 *) p);
	}

	pr_debug("<--eqos_get_ethtool_stats\n");
}

/*!
 * \details This function returns a set of strings that describe
 * the requested objects.
 *
 * \param[in] dev – pointer to net device structure.
 * \param[in] data – pointer in which requested string should be put.
 *
 * \return void
 */

static void eqos_get_strings(struct net_device *dev, u32 stringset, u8 *data)
{
	struct eqos_prv_data *pdata = netdev_priv(dev);
	int i;
	u8 *p = data;

	pr_debug("-->eqos_get_strings\n");

	switch (stringset) {
	case ETH_SS_STATS:
		if (pdata->hw_feat.mmc_sel) {
			for (i = 0; i < EQOS_MMC_STATS_LEN; i++) {
				memcpy(p, eqos_mmc[i].stat_string,
				       ETH_GSTRING_LEN);
				p += ETH_GSTRING_LEN;
			}
		}

		for (i = 0; i < EQOS_EXTRA_STAT_LEN; i++) {
			memcpy(p, eqos_gstrings_stats[i].stat_string,
			       ETH_GSTRING_LEN);
			p += ETH_GSTRING_LEN;
		}
		break;
	default:
		WARN_ON(1);
	}

	pr_debug("<--eqos_get_strings\n");
}

/*!
 * \details This function gets number of strings that @get_strings
 * will write.
 *
 * \param[in] dev – pointer to net device structure.
 *
 * \return int
 *
 * \retval +ve(>0) on success, 0 if that string is not
 * defined and -ve on failure.
 */

static int eqos_get_sset_count(struct net_device *dev, int sset)
{
	struct eqos_prv_data *pdata = netdev_priv(dev);
	int len = 0;

	pr_debug("-->eqos_get_sset_count\n");

	switch (sset) {
	case ETH_SS_STATS:
		if (pdata->hw_feat.mmc_sel)
			len = EQOS_MMC_STATS_LEN;
		len += EQOS_EXTRA_STAT_LEN;
		break;
	default:
		len = -EOPNOTSUPP;
	}

	pr_debug("<--eqos_get_sset_count\n");

	return len;
}

/*!
 * \details This function is invoked by kernel when user
 * request to enable/disable tso feature.
 *
 * \param[in] dev – pointer to net device structure.
 * \param[in] data – 1/0 for enabling/disabling tso.
 *
 * \return int
 *
 * \retval 0 on success and -ve on failure.
 */
#if 0
static int eqos_set_tso(struct net_device *dev, u32 data)
{
	struct eqos_prv_data *pdata = netdev_priv(dev);

	pr_debug("-->eqos_set_tso\n");

	if (pdata->hw_feat.tso_en == 0)
		return -EOPNOTSUPP;

	if (data)
		dev->features |= NETIF_F_TSO;
	else
		dev->features &= ~NETIF_F_TSO;

	pr_debug("<--eqos_set_tso\n");

	return 0;
}

/*!
 * \details This function is invoked by kernel when user
 * request to get report whether tso feature is enabled/disabled.
 *
 * \param[in] dev – pointer to net device structure.
 *
 * \return unsigned int
 *
 * \retval  +ve no. on success and -ve no. on failure.
 */

static u32 eqos_get_tso(struct net_device *dev)
{
	struct eqos_prv_data *pdata = netdev_priv(dev);

	pr_debug("-->eqos_get_tso\n");

	if (pdata->hw_feat.tso_en == 0)
		return 0;

	pr_debug("<--eqos_get_tso\n");

	return ((dev->features & NETIF_F_TSO) != 0);
}
#endif