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
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
|
/* Copyright (c) 2013-2017, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that 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.
*/
#include <linux/debugfs.h>
#include <linux/errno.h>
#include <linux/etherdevice.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/skbuff.h>
#include <linux/sched.h>
#include <linux/atomic.h>
#include <linux/ecm_ipa.h>
#define DRIVER_NAME "ecm_ipa"
#define ECM_IPA_IPV4_HDR_NAME "ecm_eth_ipv4"
#define ECM_IPA_IPV6_HDR_NAME "ecm_eth_ipv6"
#define INACTIVITY_MSEC_DELAY 100
#define DEFAULT_OUTSTANDING_HIGH 64
#define DEFAULT_OUTSTANDING_LOW 32
#define DEBUGFS_TEMP_BUF_SIZE 4
#define TX_TIMEOUT (5 * HZ)
#define ECM_IPA_DEBUG(fmt, args...) \
pr_debug("ctx:%s: "\
fmt, current->comm, ## args)
#define ECM_IPA_INFO(fmt, args...) \
pr_err(DRIVER_NAME "@%s@%d@ctx:%s: "\
fmt, __func__, __LINE__, current->comm, ## args)
#define ECM_IPA_ERROR(fmt, args...) \
pr_err(DRIVER_NAME "@%s@%d@ctx:%s: "\
fmt, __func__, __LINE__, current->comm, ## args)
#define NULL_CHECK(ptr) \
do { \
if (!(ptr)) { \
ECM_IPA_ERROR("null pointer #ptr\n"); \
return -EINVAL; \
} \
} \
while (0)
#define ECM_IPA_LOG_ENTRY() ECM_IPA_DEBUG("begin\n")
#define ECM_IPA_LOG_EXIT() ECM_IPA_DEBUG("end\n")
/**
* enum ecm_ipa_state - specify the current driver internal state
* which is guarded by a state machine.
*
* The driver internal state changes due to its external API usage.
* The driver saves its internal state to guard from caller illegal
* call sequence.
* states:
* UNLOADED is the first state which is the default one and is also the state
* after the driver gets unloaded(cleanup).
* INITIALIZED is the driver state once it finished registering
* the network device and all internal data struct were initialized
* CONNECTED is the driver state once the USB pipes were connected to IPA
* UP is the driver state after the interface mode was set to UP but the
* pipes are not connected yet - this state is meta-stable state.
* CONNECTED_AND_UP is the driver state when the pipe were connected and
* the interface got UP request from the network stack. this is the driver
* idle operation state which allows it to transmit/receive data.
* INVALID is a state which is not allowed.
*/
enum ecm_ipa_state {
ECM_IPA_UNLOADED = 0,
ECM_IPA_INITIALIZED,
ECM_IPA_CONNECTED,
ECM_IPA_UP,
ECM_IPA_CONNECTED_AND_UP,
ECM_IPA_INVALID,
};
/**
* enum ecm_ipa_operation - enumerations used to descibe the API operation
*
* Those enums are used as input for the driver state machine.
*/
enum ecm_ipa_operation {
ECM_IPA_INITIALIZE,
ECM_IPA_CONNECT,
ECM_IPA_OPEN,
ECM_IPA_STOP,
ECM_IPA_DISCONNECT,
ECM_IPA_CLEANUP,
};
#define ECM_IPA_STATE_DEBUG(ecm_ipa_ctx) \
ECM_IPA_DEBUG("Driver state - %s\n",\
ecm_ipa_state_string(ecm_ipa_ctx->state));
/**
* struct ecm_ipa_dev - main driver context parameters
* @net: network interface struct implemented by this driver
* @directory: debugfs directory for various debuging switches
* @eth_ipv4_hdr_hdl: saved handle for ipv4 header-insertion table
* @eth_ipv6_hdr_hdl: saved handle for ipv6 header-insertion table
* @usb_to_ipa_hdl: save handle for IPA pipe operations
* @ipa_to_usb_hdl: save handle for IPA pipe operations
* @outstanding_pkts: number of packets sent to IPA without TX complete ACKed
* @outstanding_high: number of outstanding packets allowed
* @outstanding_low: number of outstanding packets which shall cause
* to netdev queue start (after stopped due to outstanding_high reached)
* @state: current state of ecm_ipa driver
* @device_ready_notify: callback supplied by USB core driver
* This callback shall be called by the Netdev once the Netdev internal
* state is changed to RNDIS_IPA_CONNECTED_AND_UP
* @ipa_to_usb_client: consumer client
* @usb_to_ipa_client: producer client
* @ipa_rm_resource_name_prod: IPA resource manager producer resource
* @ipa_rm_resource_name_cons: IPA resource manager consumer resource
*/
struct ecm_ipa_dev {
struct net_device *net;
struct dentry *directory;
uint32_t eth_ipv4_hdr_hdl;
uint32_t eth_ipv6_hdr_hdl;
u32 usb_to_ipa_hdl;
u32 ipa_to_usb_hdl;
atomic_t outstanding_pkts;
u8 outstanding_high;
u8 outstanding_low;
enum ecm_ipa_state state;
void (*device_ready_notify)(void);
enum ipa_client_type ipa_to_usb_client;
enum ipa_client_type usb_to_ipa_client;
enum ipa_rm_resource_name ipa_rm_resource_name_prod;
enum ipa_rm_resource_name ipa_rm_resource_name_cons;
};
static int ecm_ipa_open(struct net_device *net);
static void ecm_ipa_packet_receive_notify(void *priv,
enum ipa_dp_evt_type evt, unsigned long data);
static void ecm_ipa_tx_complete_notify(void *priv,
enum ipa_dp_evt_type evt, unsigned long data);
static void ecm_ipa_tx_timeout(struct net_device *net);
static int ecm_ipa_stop(struct net_device *net);
static void ecm_ipa_enable_data_path(struct ecm_ipa_dev *ecm_ipa_ctx);
static int ecm_ipa_rules_cfg(struct ecm_ipa_dev *ecm_ipa_ctx,
const void *dst_mac, const void *src_mac);
static void ecm_ipa_rules_destroy(struct ecm_ipa_dev *ecm_ipa_ctx);
static int ecm_ipa_register_properties(struct ecm_ipa_dev *ecm_ipa_ctx);
static void ecm_ipa_deregister_properties(void);
static void ecm_ipa_rm_notify(void *user_data, enum ipa_rm_event event,
unsigned long data);
static struct net_device_stats *ecm_ipa_get_stats(struct net_device *net);
static int ecm_ipa_create_rm_resource(struct ecm_ipa_dev *ecm_ipa_ctx);
static void ecm_ipa_destory_rm_resource(struct ecm_ipa_dev *ecm_ipa_ctx);
static int resource_request(struct ecm_ipa_dev *ecm_ipa_ctx);
static void resource_release(struct ecm_ipa_dev *ecm_ipa_ctx);
static netdev_tx_t ecm_ipa_start_xmit(struct sk_buff *skb,
struct net_device *net);
static int ecm_ipa_debugfs_atomic_open(struct inode *inode, struct file *file);
static ssize_t ecm_ipa_debugfs_atomic_read(struct file *file,
char __user *ubuf, size_t count, loff_t *ppos);
static void ecm_ipa_debugfs_init(struct ecm_ipa_dev *ecm_ipa_ctx);
static void ecm_ipa_debugfs_destroy(struct ecm_ipa_dev *ecm_ipa_ctx);
static int ecm_ipa_ep_registers_cfg(u32 usb_to_ipa_hdl, u32 ipa_to_usb_hdl);
static int ecm_ipa_set_device_ethernet_addr(u8 *dev_ethaddr,
u8 device_ethaddr[]);
static enum ecm_ipa_state ecm_ipa_next_state(enum ecm_ipa_state current_state,
enum ecm_ipa_operation operation);
static const char *ecm_ipa_state_string(enum ecm_ipa_state state);
static int ecm_ipa_init_module(void);
static void ecm_ipa_cleanup_module(void);
static const struct net_device_ops ecm_ipa_netdev_ops = {
.ndo_open = ecm_ipa_open,
.ndo_stop = ecm_ipa_stop,
.ndo_start_xmit = ecm_ipa_start_xmit,
.ndo_set_mac_address = eth_mac_addr,
.ndo_tx_timeout = ecm_ipa_tx_timeout,
.ndo_get_stats = ecm_ipa_get_stats,
};
const struct file_operations ecm_ipa_debugfs_atomic_ops = {
.open = ecm_ipa_debugfs_atomic_open,
.read = ecm_ipa_debugfs_atomic_read,
};
static void ecm_ipa_msg_free_cb(void *buff, u32 len, u32 type)
{
kfree(buff);
}
/**
* ecm_ipa_init() - create network device and initializes internal
* data structures
* @params: in/out parameters required for ecm_ipa initialization
*
* Shall be called prior to pipe connection.
* The out parameters (the callbacks) shall be supplied to ipa_connect.
* Detailed description:
* - allocate the network device
* - set default values for driver internals
* - create debugfs folder and files
* - create IPA resource manager client
* - add header insertion rules for IPA driver (based on host/device
* Ethernet addresses given in input params)
* - register tx/rx properties to IPA driver (will be later used
* by IPA configuration manager to configure reset of the IPA rules)
* - set the carrier state to "off" (until ecm_ipa_connect is called)
* - register the network device
* - set the out parameters
*
* Returns negative errno, or zero on success
*/
int ecm_ipa_init(struct ecm_ipa_params *params)
{
int result = 0;
struct net_device *net;
struct ecm_ipa_dev *ecm_ipa_ctx;
ECM_IPA_LOG_ENTRY();
ECM_IPA_DEBUG("%s initializing\n", DRIVER_NAME);
NULL_CHECK(params);
ECM_IPA_DEBUG("host_ethaddr=%pM, device_ethaddr=%pM\n",
params->host_ethaddr,
params->device_ethaddr);
net = alloc_etherdev(sizeof(struct ecm_ipa_dev));
if (!net) {
result = -ENOMEM;
ECM_IPA_ERROR("fail to allocate etherdev\n");
goto fail_alloc_etherdev;
}
ECM_IPA_DEBUG("network device was successfully allocated\n");
ecm_ipa_ctx = netdev_priv(net);
if (!ecm_ipa_ctx) {
ECM_IPA_ERROR("fail to extract netdev priv\n");
result = -ENOMEM;
goto fail_netdev_priv;
}
memset(ecm_ipa_ctx, 0, sizeof(*ecm_ipa_ctx));
ECM_IPA_DEBUG("ecm_ipa_ctx (private) = %p\n", ecm_ipa_ctx);
ecm_ipa_ctx->net = net;
ecm_ipa_ctx->outstanding_high = DEFAULT_OUTSTANDING_HIGH;
ecm_ipa_ctx->outstanding_low = DEFAULT_OUTSTANDING_LOW;
atomic_set(&ecm_ipa_ctx->outstanding_pkts, 0);
snprintf(net->name, sizeof(net->name), "%s%%d", "ecm");
net->netdev_ops = &ecm_ipa_netdev_ops;
net->watchdog_timeo = TX_TIMEOUT;
ECM_IPA_DEBUG("internal data structures were intialized\n");
if (!params->device_ready_notify)
ECM_IPA_DEBUG("device_ready_notify() was not supplied");
ecm_ipa_ctx->device_ready_notify = params->device_ready_notify;
ecm_ipa_debugfs_init(ecm_ipa_ctx);
result = ecm_ipa_set_device_ethernet_addr(net->dev_addr,
params->device_ethaddr);
if (result) {
ECM_IPA_ERROR("set device MAC failed\n");
goto fail_set_device_ethernet;
}
ECM_IPA_DEBUG("Device Ethernet address set %pM\n", net->dev_addr);
result = ecm_ipa_rules_cfg(ecm_ipa_ctx, params->host_ethaddr,
params->device_ethaddr);
if (result) {
ECM_IPA_ERROR("fail on ipa rules set\n");
goto fail_rules_cfg;
}
ECM_IPA_DEBUG("Ethernet header insertion set\n");
netif_carrier_off(net);
ECM_IPA_DEBUG("netif_carrier_off() was called\n");
netif_stop_queue(ecm_ipa_ctx->net);
ECM_IPA_DEBUG("netif_stop_queue() was called");
result = register_netdev(net);
if (result) {
ECM_IPA_ERROR("register_netdev failed: %d\n", result);
goto fail_register_netdev;
}
ECM_IPA_DEBUG("register_netdev succeeded\n");
params->ecm_ipa_rx_dp_notify = ecm_ipa_packet_receive_notify;
params->ecm_ipa_tx_dp_notify = ecm_ipa_tx_complete_notify;
params->private = (void *)ecm_ipa_ctx;
params->skip_ep_cfg = false;
ecm_ipa_ctx->state = ECM_IPA_INITIALIZED;
ECM_IPA_STATE_DEBUG(ecm_ipa_ctx);
ECM_IPA_INFO("ECM_IPA was initialized successfully\n");
ECM_IPA_LOG_EXIT();
return 0;
fail_register_netdev:
ecm_ipa_rules_destroy(ecm_ipa_ctx);
fail_set_device_ethernet:
fail_rules_cfg:
ecm_ipa_debugfs_destroy(ecm_ipa_ctx);
fail_netdev_priv:
free_netdev(net);
fail_alloc_etherdev:
return result;
}
EXPORT_SYMBOL(ecm_ipa_init);
/**
* ecm_ipa_connect() - notify ecm_ipa for IPA<->USB pipes connection
* @usb_to_ipa_hdl: handle of IPA driver client for USB->IPA
* @ipa_to_usb_hdl: handle of IPA driver client for IPA->USB
* @priv: same value that was set by ecm_ipa_init(), this
* parameter holds the network device pointer.
*
* Once USB driver finishes the pipe connection between IPA core
* and USB core this method shall be called in order to
* allow ecm_ipa complete the data path configurations.
* Caller should make sure that it is calling this function
* from a context that allows it to handle device_ready_notify().
* Detailed description:
* - configure the IPA end-points register
* - notify the Linux kernel for "carrier_on"
* After this function is done the driver state changes to "Connected".
* This API is expected to be called after ecm_ipa_init() or
* after a call to ecm_ipa_disconnect.
*/
int ecm_ipa_connect(u32 usb_to_ipa_hdl, u32 ipa_to_usb_hdl, void *priv)
{
struct ecm_ipa_dev *ecm_ipa_ctx = priv;
int next_state;
struct ipa_ecm_msg *ecm_msg;
struct ipa_msg_meta msg_meta;
int retval;
ECM_IPA_LOG_ENTRY();
NULL_CHECK(priv);
ECM_IPA_DEBUG("usb_to_ipa_hdl = %d, ipa_to_usb_hdl = %d, priv=0x%p\n",
usb_to_ipa_hdl, ipa_to_usb_hdl, priv);
next_state = ecm_ipa_next_state(ecm_ipa_ctx->state, ECM_IPA_CONNECT);
if (next_state == ECM_IPA_INVALID) {
ECM_IPA_ERROR("can't call connect before calling initialize\n");
return -EPERM;
}
ecm_ipa_ctx->state = next_state;
ECM_IPA_STATE_DEBUG(ecm_ipa_ctx);
if (!ipa_is_client_handle_valid(usb_to_ipa_hdl)) {
ECM_IPA_ERROR("usb_to_ipa_hdl(%d) is not a valid ipa handle\n",
usb_to_ipa_hdl);
return -EINVAL;
}
if (!ipa_is_client_handle_valid(ipa_to_usb_hdl)) {
ECM_IPA_ERROR("ipa_to_usb_hdl(%d) is not a valid ipa handle\n",
ipa_to_usb_hdl);
return -EINVAL;
}
ecm_ipa_ctx->ipa_to_usb_hdl = ipa_to_usb_hdl;
ecm_ipa_ctx->usb_to_ipa_hdl = usb_to_ipa_hdl;
ecm_ipa_ctx->ipa_to_usb_client = ipa_get_client_mapping(ipa_to_usb_hdl);
if (ecm_ipa_ctx->ipa_to_usb_client < 0) {
ECM_IPA_ERROR(
"Error getting IPA->USB client from handle %d\n",
ecm_ipa_ctx->ipa_to_usb_client);
return -EINVAL;
}
ECM_IPA_DEBUG("ipa_to_usb_client = %d\n",
ecm_ipa_ctx->ipa_to_usb_client);
ecm_ipa_ctx->usb_to_ipa_client = ipa_get_client_mapping(usb_to_ipa_hdl);
if (ecm_ipa_ctx->usb_to_ipa_client < 0) {
ECM_IPA_ERROR(
"Error getting USB->IPA client from handle %d\n",
ecm_ipa_ctx->usb_to_ipa_client);
return -EINVAL;
}
ECM_IPA_DEBUG("usb_to_ipa_client = %d\n",
ecm_ipa_ctx->usb_to_ipa_client);
ecm_ipa_ctx->ipa_rm_resource_name_cons =
ipa_get_rm_resource_from_ep(ipa_to_usb_hdl);
if (ecm_ipa_ctx->ipa_rm_resource_name_cons < 0) {
ECM_IPA_ERROR("Error getting CONS RM resource from handle %d\n",
ecm_ipa_ctx->ipa_rm_resource_name_cons);
return -EINVAL;
}
ECM_IPA_DEBUG("ipa_rm_resource_name_cons = %d\n",
ecm_ipa_ctx->ipa_rm_resource_name_cons);
ecm_ipa_ctx->ipa_rm_resource_name_prod =
ipa_get_rm_resource_from_ep(usb_to_ipa_hdl);
if (ecm_ipa_ctx->ipa_rm_resource_name_prod < 0) {
ECM_IPA_ERROR("Error getting PROD RM resource from handle %d\n",
ecm_ipa_ctx->ipa_rm_resource_name_prod);
return -EINVAL;
}
ECM_IPA_DEBUG("ipa_rm_resource_name_prod = %d\n",
ecm_ipa_ctx->ipa_rm_resource_name_prod);
retval = ecm_ipa_create_rm_resource(ecm_ipa_ctx);
if (retval) {
ECM_IPA_ERROR("fail on RM create\n");
goto fail_create_rm;
}
ECM_IPA_DEBUG("RM resource was created\n");
retval = ecm_ipa_register_properties(ecm_ipa_ctx);
if (retval) {
ECM_IPA_ERROR("fail on properties set\n");
goto fail_create_rm;
}
ECM_IPA_DEBUG("ecm_ipa 2 Tx and 2 Rx properties were registered\n");
retval = ecm_ipa_ep_registers_cfg(usb_to_ipa_hdl, ipa_to_usb_hdl);
if (retval) {
ECM_IPA_ERROR("fail on ep cfg\n");
goto fail;
}
ECM_IPA_DEBUG("end-point configured\n");
netif_carrier_on(ecm_ipa_ctx->net);
ecm_msg = kzalloc(sizeof(struct ipa_ecm_msg), GFP_KERNEL);
if (!ecm_msg) {
ECM_IPA_ERROR("can't alloc msg mem\n");
retval = -ENOMEM;
goto fail;
}
memset(&msg_meta, 0, sizeof(struct ipa_msg_meta));
msg_meta.msg_type = ECM_CONNECT;
msg_meta.msg_len = sizeof(struct ipa_ecm_msg);
strlcpy(ecm_msg->name, ecm_ipa_ctx->net->name,
IPA_RESOURCE_NAME_MAX);
ecm_msg->ifindex = ecm_ipa_ctx->net->ifindex;
retval = ipa_send_msg(&msg_meta, ecm_msg, ecm_ipa_msg_free_cb);
if (retval) {
ECM_IPA_ERROR("fail to send ECM_CONNECT message\n");
kfree(ecm_msg);
goto fail;
}
if (!netif_carrier_ok(ecm_ipa_ctx->net)) {
ECM_IPA_ERROR("netif_carrier_ok error\n");
retval = -EBUSY;
goto fail;
}
ECM_IPA_DEBUG("carrier_on notified\n");
if (ecm_ipa_ctx->state == ECM_IPA_CONNECTED_AND_UP)
ecm_ipa_enable_data_path(ecm_ipa_ctx);
else
ECM_IPA_DEBUG("data path was not enabled yet\n");
ECM_IPA_INFO("ECM_IPA was connected successfully\n");
ECM_IPA_LOG_EXIT();
return 0;
fail:
ecm_ipa_deregister_properties();
fail_create_rm:
ecm_ipa_destory_rm_resource(ecm_ipa_ctx);
return retval;
}
EXPORT_SYMBOL(ecm_ipa_connect);
/**
* ecm_ipa_open() - notify Linux network stack to start sending packets
* @net: the network interface supplied by the network stack
*
* Linux uses this API to notify the driver that the network interface
* transitions to the up state.
* The driver will instruct the Linux network stack to start
* delivering data packets.
*/
static int ecm_ipa_open(struct net_device *net)
{
struct ecm_ipa_dev *ecm_ipa_ctx;
int next_state;
ECM_IPA_LOG_ENTRY();
ecm_ipa_ctx = netdev_priv(net);
next_state = ecm_ipa_next_state(ecm_ipa_ctx->state, ECM_IPA_OPEN);
if (next_state == ECM_IPA_INVALID) {
ECM_IPA_ERROR("can't bring driver up before initialize\n");
return -EPERM;
}
ecm_ipa_ctx->state = next_state;
ECM_IPA_STATE_DEBUG(ecm_ipa_ctx);
if (ecm_ipa_ctx->state == ECM_IPA_CONNECTED_AND_UP)
ecm_ipa_enable_data_path(ecm_ipa_ctx);
else
ECM_IPA_DEBUG("data path was not enabled yet\n");
ECM_IPA_LOG_EXIT();
return 0;
}
/**
* ecm_ipa_start_xmit() - send data from APPs to USB core via IPA core
* @skb: packet received from Linux network stack
* @net: the network device being used to send this packet
*
* Several conditions needed in order to send the packet to IPA:
* - Transmit queue for the network driver is currently
* in "send" state
* - The driver internal state is in "UP" state.
* - Filter Tx switch is turned off
* - The IPA resource manager state for the driver producer client
* is "Granted" which implies that all the resources in the dependency
* graph are valid for data flow.
* - outstanding high boundary did not reach.
*
* In case all of the above conditions are met, the network driver will
* send the packet by using the IPA API for Tx.
* In case the outstanding packet high boundary is reached, the driver will
* stop the send queue until enough packet were proceeded by the IPA core.
*/
static netdev_tx_t ecm_ipa_start_xmit(struct sk_buff *skb,
struct net_device *net)
{
int ret;
netdev_tx_t status = NETDEV_TX_BUSY;
struct ecm_ipa_dev *ecm_ipa_ctx = netdev_priv(net);
net->trans_start = jiffies;
ECM_IPA_DEBUG("Tx, len=%d, skb->protocol=%d, outstanding=%d\n",
skb->len, skb->protocol,
atomic_read(&ecm_ipa_ctx->outstanding_pkts));
if (unlikely(netif_queue_stopped(net))) {
ECM_IPA_ERROR("interface queue is stopped\n");
goto out;
}
if (unlikely(ecm_ipa_ctx->state != ECM_IPA_CONNECTED_AND_UP)) {
ECM_IPA_ERROR("Missing pipe connected and/or iface up\n");
return NETDEV_TX_BUSY;
}
ret = resource_request(ecm_ipa_ctx);
if (ret) {
ECM_IPA_DEBUG("Waiting to resource\n");
netif_stop_queue(net);
goto resource_busy;
}
if (atomic_read(&ecm_ipa_ctx->outstanding_pkts) >=
ecm_ipa_ctx->outstanding_high) {
ECM_IPA_DEBUG("outstanding high (%d)- stopping\n",
ecm_ipa_ctx->outstanding_high);
netif_stop_queue(net);
status = NETDEV_TX_BUSY;
goto out;
}
ret = ipa_tx_dp(ecm_ipa_ctx->ipa_to_usb_client, skb, NULL);
if (ret) {
ECM_IPA_ERROR("ipa transmit failed (%d)\n", ret);
goto fail_tx_packet;
}
atomic_inc(&ecm_ipa_ctx->outstanding_pkts);
status = NETDEV_TX_OK;
goto out;
fail_tx_packet:
out:
resource_release(ecm_ipa_ctx);
resource_busy:
return status;
}
/**
* ecm_ipa_packet_receive_notify() - Rx notify
*
* @priv: ecm driver context
* @evt: event type
* @data: data provided with event
*
* IPA will pass a packet to the Linux network stack with skb->data pointing
* to Ethernet packet frame.
*/
static void ecm_ipa_packet_receive_notify(void *priv,
enum ipa_dp_evt_type evt,
unsigned long data)
{
struct sk_buff *skb = (struct sk_buff *)data;
struct ecm_ipa_dev *ecm_ipa_ctx = priv;
int result;
unsigned int packet_len;
if (!skb) {
ECM_IPA_ERROR("Bad SKB received from IPA driver\n");
return;
}
packet_len = skb->len;
ECM_IPA_DEBUG("packet RX, len=%d\n", skb->len);
if (unlikely(ecm_ipa_ctx->state != ECM_IPA_CONNECTED_AND_UP)) {
ECM_IPA_DEBUG("Missing pipe connected and/or iface up\n");
return;
}
if (evt != IPA_RECEIVE) {
ECM_IPA_ERROR("A none IPA_RECEIVE event in ecm_ipa_receive\n");
return;
}
skb->dev = ecm_ipa_ctx->net;
skb->protocol = eth_type_trans(skb, ecm_ipa_ctx->net);
result = netif_rx(skb);
if (result)
ECM_IPA_ERROR("fail on netif_rx\n");
ecm_ipa_ctx->net->stats.rx_packets++;
ecm_ipa_ctx->net->stats.rx_bytes += packet_len;
return;
}
/** ecm_ipa_stop() - called when network device transitions to the down
* state.
* @net: the network device being stopped.
*
* This API is used by Linux network stack to notify the network driver that
* its state was changed to "down"
* The driver will stop the "send" queue and change its internal
* state to "Connected".
*/
static int ecm_ipa_stop(struct net_device *net)
{
struct ecm_ipa_dev *ecm_ipa_ctx = netdev_priv(net);
int next_state;
ECM_IPA_LOG_ENTRY();
next_state = ecm_ipa_next_state(ecm_ipa_ctx->state, ECM_IPA_STOP);
if (next_state == ECM_IPA_INVALID) {
ECM_IPA_ERROR("can't do network interface down without up\n");
return -EPERM;
}
ecm_ipa_ctx->state = next_state;
ECM_IPA_STATE_DEBUG(ecm_ipa_ctx);
netif_stop_queue(net);
ECM_IPA_DEBUG("network device stopped\n");
ECM_IPA_LOG_EXIT();
return 0;
}
/** ecm_ipa_disconnect() - called when the USB cable is unplugged.
* @priv: same value that was set by ecm_ipa_init(), this
* parameter holds the network device pointer.
*
* Once the USB cable is unplugged the USB driver will notify the network
* interface driver.
* The internal driver state will returned to its initialized state and
* Linux network stack will be informed for carrier off and the send queue
* will be stopped.
*/
int ecm_ipa_disconnect(void *priv)
{
struct ecm_ipa_dev *ecm_ipa_ctx = priv;
int next_state;
struct ipa_ecm_msg *ecm_msg;
struct ipa_msg_meta msg_meta;
int retval;
int outstanding_dropped_pkts;
ECM_IPA_LOG_ENTRY();
NULL_CHECK(ecm_ipa_ctx);
ECM_IPA_DEBUG("priv=0x%p\n", priv);
next_state = ecm_ipa_next_state(ecm_ipa_ctx->state, ECM_IPA_DISCONNECT);
if (next_state == ECM_IPA_INVALID) {
ECM_IPA_ERROR("can't disconnect before connect\n");
return -EPERM;
}
ecm_ipa_ctx->state = next_state;
ECM_IPA_STATE_DEBUG(ecm_ipa_ctx);
netif_carrier_off(ecm_ipa_ctx->net);
ECM_IPA_DEBUG("carrier_off notifcation was sent\n");
ecm_msg = kzalloc(sizeof(struct ipa_ecm_msg), GFP_KERNEL);
if (!ecm_msg) {
ECM_IPA_ERROR("can't alloc msg mem\n");
return -ENOMEM;
}
memset(&msg_meta, 0, sizeof(struct ipa_msg_meta));
msg_meta.msg_type = ECM_DISCONNECT;
msg_meta.msg_len = sizeof(struct ipa_ecm_msg);
strlcpy(ecm_msg->name, ecm_ipa_ctx->net->name,
IPA_RESOURCE_NAME_MAX);
ecm_msg->ifindex = ecm_ipa_ctx->net->ifindex;
retval = ipa_send_msg(&msg_meta, ecm_msg, ecm_ipa_msg_free_cb);
if (retval) {
ECM_IPA_ERROR("fail to send ECM_DISCONNECT message\n");
kfree(ecm_msg);
return -EPERM;
}
netif_stop_queue(ecm_ipa_ctx->net);
ECM_IPA_DEBUG("queue stopped\n");
ecm_ipa_destory_rm_resource(ecm_ipa_ctx);
outstanding_dropped_pkts =
atomic_read(&ecm_ipa_ctx->outstanding_pkts);
ecm_ipa_ctx->net->stats.tx_errors += outstanding_dropped_pkts;
atomic_set(&ecm_ipa_ctx->outstanding_pkts, 0);
ECM_IPA_INFO("ECM_IPA was disconnected successfully\n");
ECM_IPA_LOG_EXIT();
return 0;
}
EXPORT_SYMBOL(ecm_ipa_disconnect);
/**
* ecm_ipa_cleanup() - unregister the network interface driver and free
* internal data structs.
* @priv: same value that was set by ecm_ipa_init(), this
* parameter holds the network device pointer.
*
* This function shall be called once the network interface is not
* needed anymore, e.g: when the USB composition does not support ECM.
* This function shall be called after the pipes were disconnected.
* Detailed description:
* - delete the driver dependency defined for IPA resource manager and
* destroy the producer resource.
* - remove the debugfs entries
* - deregister the network interface from Linux network stack
* - free all internal data structs
*/
void ecm_ipa_cleanup(void *priv)
{
struct ecm_ipa_dev *ecm_ipa_ctx = priv;
int next_state;
ECM_IPA_LOG_ENTRY();
ECM_IPA_DEBUG("priv=0x%p\n", priv);
if (!ecm_ipa_ctx) {
ECM_IPA_ERROR("ecm_ipa_ctx NULL pointer\n");
return;
}
next_state = ecm_ipa_next_state(ecm_ipa_ctx->state, ECM_IPA_CLEANUP);
if (next_state == ECM_IPA_INVALID) {
ECM_IPA_ERROR("can't clean driver without cable disconnect\n");
return;
}
ecm_ipa_ctx->state = next_state;
ECM_IPA_STATE_DEBUG(ecm_ipa_ctx);
ecm_ipa_rules_destroy(ecm_ipa_ctx);
ecm_ipa_debugfs_destroy(ecm_ipa_ctx);
unregister_netdev(ecm_ipa_ctx->net);
free_netdev(ecm_ipa_ctx->net);
ECM_IPA_INFO("ECM_IPA was destroyed successfully\n");
ECM_IPA_LOG_EXIT();
return;
}
EXPORT_SYMBOL(ecm_ipa_cleanup);
static void ecm_ipa_enable_data_path(struct ecm_ipa_dev *ecm_ipa_ctx)
{
if (ecm_ipa_ctx->device_ready_notify) {
ecm_ipa_ctx->device_ready_notify();
ECM_IPA_DEBUG("USB device_ready_notify() was called\n");
} else {
ECM_IPA_DEBUG("device_ready_notify() not supplied\n");
}
netif_start_queue(ecm_ipa_ctx->net);
ECM_IPA_DEBUG("queue started\n");
}
/**
* ecm_ipa_rules_cfg() - set header insertion and register Tx/Rx properties
* Headers will be commited to HW
* @ecm_ipa_ctx: main driver context parameters
* @dst_mac: destination MAC address
* @src_mac: source MAC address
*
* Returns negative errno, or zero on success
*/
static int ecm_ipa_rules_cfg(struct ecm_ipa_dev *ecm_ipa_ctx,
const void *dst_mac, const void *src_mac)
{
struct ipa_ioc_add_hdr *hdrs;
struct ipa_hdr_add *ipv4_hdr;
struct ipa_hdr_add *ipv6_hdr;
struct ethhdr *eth_ipv4;
struct ethhdr *eth_ipv6;
int result = 0;
ECM_IPA_LOG_ENTRY();
hdrs = kzalloc(sizeof(*hdrs) + sizeof(*ipv4_hdr) + sizeof(*ipv6_hdr),
GFP_KERNEL);
if (!hdrs) {
result = -ENOMEM;
goto out;
}
ipv4_hdr = &hdrs->hdr[0];
eth_ipv4 = (struct ethhdr *)ipv4_hdr->hdr;
ipv6_hdr = &hdrs->hdr[1];
eth_ipv6 = (struct ethhdr *)ipv6_hdr->hdr;
strlcpy(ipv4_hdr->name, ECM_IPA_IPV4_HDR_NAME, IPA_RESOURCE_NAME_MAX);
memcpy(eth_ipv4->h_dest, dst_mac, ETH_ALEN);
memcpy(eth_ipv4->h_source, src_mac, ETH_ALEN);
eth_ipv4->h_proto = htons(ETH_P_IP);
ipv4_hdr->hdr_len = ETH_HLEN;
ipv4_hdr->is_partial = 0;
ipv4_hdr->is_eth2_ofst_valid = true;
ipv4_hdr->eth2_ofst = 0;
ipv4_hdr->type = IPA_HDR_L2_ETHERNET_II;
strlcpy(ipv6_hdr->name, ECM_IPA_IPV6_HDR_NAME, IPA_RESOURCE_NAME_MAX);
memcpy(eth_ipv6->h_dest, dst_mac, ETH_ALEN);
memcpy(eth_ipv6->h_source, src_mac, ETH_ALEN);
eth_ipv6->h_proto = htons(ETH_P_IPV6);
ipv6_hdr->hdr_len = ETH_HLEN;
ipv6_hdr->is_partial = 0;
ipv6_hdr->is_eth2_ofst_valid = true;
ipv6_hdr->eth2_ofst = 0;
ipv6_hdr->type = IPA_HDR_L2_ETHERNET_II;
hdrs->commit = 1;
hdrs->num_hdrs = 2;
result = ipa_add_hdr(hdrs);
if (result) {
ECM_IPA_ERROR("Fail on Header-Insertion(%d)\n", result);
goto out_free_mem;
}
if (ipv4_hdr->status) {
ECM_IPA_ERROR("Fail on Header-Insertion ipv4(%d)\n",
ipv4_hdr->status);
result = ipv4_hdr->status;
goto out_free_mem;
}
if (ipv6_hdr->status) {
ECM_IPA_ERROR("Fail on Header-Insertion ipv6(%d)\n",
ipv6_hdr->status);
result = ipv6_hdr->status;
goto out_free_mem;
}
ecm_ipa_ctx->eth_ipv4_hdr_hdl = ipv4_hdr->hdr_hdl;
ecm_ipa_ctx->eth_ipv6_hdr_hdl = ipv6_hdr->hdr_hdl;
ECM_IPA_LOG_EXIT();
out_free_mem:
kfree(hdrs);
out:
return result;
}
/**
* ecm_ipa_rules_destroy() - remove the IPA core configuration done for
* the driver data path.
* @ecm_ipa_ctx: the driver context
*
* Revert the work done on ecm_ipa_rules_cfg.
*/
static void ecm_ipa_rules_destroy(struct ecm_ipa_dev *ecm_ipa_ctx)
{
struct ipa_ioc_del_hdr *del_hdr;
struct ipa_hdr_del *ipv4;
struct ipa_hdr_del *ipv6;
int result;
del_hdr = kzalloc(sizeof(*del_hdr) + sizeof(*ipv4) +
sizeof(*ipv6), GFP_KERNEL);
if (!del_hdr)
return;
del_hdr->commit = 1;
del_hdr->num_hdls = 2;
ipv4 = &del_hdr->hdl[0];
ipv4->hdl = ecm_ipa_ctx->eth_ipv4_hdr_hdl;
ipv6 = &del_hdr->hdl[1];
ipv6->hdl = ecm_ipa_ctx->eth_ipv6_hdr_hdl;
result = ipa_del_hdr(del_hdr);
if (result || ipv4->status || ipv6->status)
ECM_IPA_ERROR("ipa_del_hdr failed\n");
kfree(del_hdr);
}
/* ecm_ipa_register_properties() - set Tx/Rx properties for ipacm
*
* Register ecm0 interface with 2 Tx properties and 2 Rx properties:
* The 2 Tx properties are for data flowing from IPA to USB, they
* have Header-Insertion properties both for Ipv4 and Ipv6 Ethernet framing.
* The 2 Rx properties are for data flowing from USB to IPA, they have
* simple rule which always "hit".
*
*/
static int ecm_ipa_register_properties(struct ecm_ipa_dev *ecm_ipa_ctx)
{
struct ipa_tx_intf tx_properties = {0};
struct ipa_ioc_tx_intf_prop properties[2] = { {0}, {0} };
struct ipa_ioc_tx_intf_prop *ipv4_property;
struct ipa_ioc_tx_intf_prop *ipv6_property;
struct ipa_ioc_rx_intf_prop rx_ioc_properties[2] = { {0}, {0} };
struct ipa_rx_intf rx_properties = {0};
struct ipa_ioc_rx_intf_prop *rx_ipv4_property;
struct ipa_ioc_rx_intf_prop *rx_ipv6_property;
int result = 0;
ECM_IPA_LOG_ENTRY();
tx_properties.prop = properties;
ipv4_property = &tx_properties.prop[0];
ipv4_property->ip = IPA_IP_v4;
ipv4_property->dst_pipe = ecm_ipa_ctx->ipa_to_usb_client;
strlcpy(ipv4_property->hdr_name, ECM_IPA_IPV4_HDR_NAME,
IPA_RESOURCE_NAME_MAX);
ipv4_property->hdr_l2_type = IPA_HDR_L2_ETHERNET_II;
ipv6_property = &tx_properties.prop[1];
ipv6_property->ip = IPA_IP_v6;
ipv6_property->dst_pipe = ecm_ipa_ctx->ipa_to_usb_client;
ipv6_property->hdr_l2_type = IPA_HDR_L2_ETHERNET_II;
strlcpy(ipv6_property->hdr_name, ECM_IPA_IPV6_HDR_NAME,
IPA_RESOURCE_NAME_MAX);
tx_properties.num_props = 2;
rx_properties.prop = rx_ioc_properties;
rx_ipv4_property = &rx_properties.prop[0];
rx_ipv4_property->ip = IPA_IP_v4;
rx_ipv4_property->attrib.attrib_mask = 0;
rx_ipv4_property->src_pipe = ecm_ipa_ctx->usb_to_ipa_client;
rx_ipv4_property->hdr_l2_type = IPA_HDR_L2_ETHERNET_II;
rx_ipv6_property = &rx_properties.prop[1];
rx_ipv6_property->ip = IPA_IP_v6;
rx_ipv6_property->attrib.attrib_mask = 0;
rx_ipv6_property->src_pipe = ecm_ipa_ctx->usb_to_ipa_client;
rx_ipv6_property->hdr_l2_type = IPA_HDR_L2_ETHERNET_II;
rx_properties.num_props = 2;
result = ipa_register_intf("ecm0", &tx_properties, &rx_properties);
if (result)
ECM_IPA_ERROR("fail on Tx/Rx properties registration\n");
ECM_IPA_LOG_EXIT();
return result;
}
static void ecm_ipa_deregister_properties(void)
{
int result;
ECM_IPA_LOG_ENTRY();
result = ipa_deregister_intf("ecm0");
if (result)
ECM_IPA_DEBUG("Fail on Tx prop deregister\n");
ECM_IPA_LOG_EXIT();
return;
}
/**
* ecm_ipa_configure() - make IPA core end-point specific configuration
* @usb_to_ipa_hdl: handle of usb_to_ipa end-point for IPA driver
* @ipa_to_usb_hdl: handle of ipa_to_usb end-point for IPA driver
* @host_ethaddr: host Ethernet address in network order
* @device_ethaddr: device Ethernet address in network order
*
* Configure the usb_to_ipa and ipa_to_usb end-point registers
* - USB->IPA end-point: disable de-aggregation, enable link layer
* header removal (Ethernet removal), source NATing and default routing.
* - IPA->USB end-point: disable aggregation, add link layer header (Ethernet)
* - allocate Ethernet device
* - register to Linux network stack
*
* Returns negative errno, or zero on success
*/
static void ecm_ipa_rm_notify(void *user_data, enum ipa_rm_event event,
unsigned long data)
{
struct ecm_ipa_dev *ecm_ipa_ctx = user_data;
ECM_IPA_LOG_ENTRY();
if (event == IPA_RM_RESOURCE_GRANTED &&
netif_queue_stopped(ecm_ipa_ctx->net)) {
ECM_IPA_DEBUG("Resource Granted - starting queue\n");
netif_start_queue(ecm_ipa_ctx->net);
} else {
ECM_IPA_DEBUG("Resource released\n");
}
ECM_IPA_LOG_EXIT();
}
static struct net_device_stats *ecm_ipa_get_stats(struct net_device *net)
{
return &net->stats;
}
static int ecm_ipa_create_rm_resource(struct ecm_ipa_dev *ecm_ipa_ctx)
{
struct ipa_rm_create_params create_params = {0};
struct ipa_rm_perf_profile profile;
int result;
ECM_IPA_LOG_ENTRY();
create_params.name = IPA_RM_RESOURCE_STD_ECM_PROD;
create_params.reg_params.user_data = ecm_ipa_ctx;
create_params.reg_params.notify_cb = ecm_ipa_rm_notify;
result = ipa_rm_create_resource(&create_params);
if (result) {
ECM_IPA_ERROR("Fail on ipa_rm_create_resource\n");
goto fail_rm_create;
}
ECM_IPA_DEBUG("rm client was created");
profile.max_supported_bandwidth_mbps = IPA_APPS_MAX_BW_IN_MBPS;
ipa_rm_set_perf_profile(IPA_RM_RESOURCE_STD_ECM_PROD, &profile);
result = ipa_rm_inactivity_timer_init(IPA_RM_RESOURCE_STD_ECM_PROD,
INACTIVITY_MSEC_DELAY);
if (result) {
ECM_IPA_ERROR("Fail on ipa_rm_inactivity_timer_init\n");
goto fail_it;
}
ECM_IPA_DEBUG("rm_it client was created");
result = ipa_rm_add_dependency_sync(IPA_RM_RESOURCE_STD_ECM_PROD,
ecm_ipa_ctx->ipa_rm_resource_name_cons);
if (result && result != -EINPROGRESS)
ECM_IPA_ERROR("unable to add ECM/USB dependency (%d)\n",
result);
result = ipa_rm_add_dependency_sync(
ecm_ipa_ctx->ipa_rm_resource_name_prod,
IPA_RM_RESOURCE_APPS_CONS);
if (result && result != -EINPROGRESS)
ECM_IPA_ERROR("unable to add USB/APPS dependency (%d)\n",
result);
ECM_IPA_DEBUG("rm dependency was set\n");
ECM_IPA_LOG_EXIT();
return 0;
fail_it:
fail_rm_create:
return result;
}
static void ecm_ipa_destory_rm_resource(struct ecm_ipa_dev *ecm_ipa_ctx)
{
int result;
ECM_IPA_LOG_ENTRY();
ipa_rm_delete_dependency(IPA_RM_RESOURCE_STD_ECM_PROD,
ecm_ipa_ctx->ipa_rm_resource_name_cons);
ipa_rm_delete_dependency(ecm_ipa_ctx->ipa_rm_resource_name_prod,
IPA_RM_RESOURCE_APPS_CONS);
ipa_rm_inactivity_timer_destroy(IPA_RM_RESOURCE_STD_ECM_PROD);
result = ipa_rm_delete_resource(IPA_RM_RESOURCE_STD_ECM_PROD);
if (result)
ECM_IPA_ERROR("resource deletion failed\n");
ECM_IPA_LOG_EXIT();
}
static int resource_request(struct ecm_ipa_dev *ecm_ipa_ctx)
{
return ipa_rm_inactivity_timer_request_resource(
IPA_RM_RESOURCE_STD_ECM_PROD);
}
static void resource_release(struct ecm_ipa_dev *ecm_ipa_ctx)
{
ipa_rm_inactivity_timer_release_resource(IPA_RM_RESOURCE_STD_ECM_PROD);
}
/**
* ecm_ipa_tx_complete_notify() - Rx notify
*
* @priv: ecm driver context
* @evt: event type
* @data: data provided with event
*
* Check that the packet is the one we sent and release it
* This function will be called in defered context in IPA wq.
*/
static void ecm_ipa_tx_complete_notify(void *priv,
enum ipa_dp_evt_type evt,
unsigned long data)
{
struct sk_buff *skb = (struct sk_buff *)data;
struct ecm_ipa_dev *ecm_ipa_ctx = priv;
if (!skb) {
ECM_IPA_ERROR("Bad SKB received from IPA driver\n");
return;
}
if (!ecm_ipa_ctx) {
ECM_IPA_ERROR("ecm_ipa_ctx is NULL pointer\n");
return;
}
ECM_IPA_DEBUG("Tx-complete, len=%d, skb->prot=%d, outstanding=%d\n",
skb->len, skb->protocol,
atomic_read(&ecm_ipa_ctx->outstanding_pkts));
if (evt != IPA_WRITE_DONE) {
ECM_IPA_ERROR("unsupported event on Tx callback\n");
return;
}
if (unlikely(ecm_ipa_ctx->state != ECM_IPA_CONNECTED_AND_UP)) {
ECM_IPA_DEBUG("dropping Tx-complete pkt, state=%s",
ecm_ipa_state_string(ecm_ipa_ctx->state));
goto out;
}
ecm_ipa_ctx->net->stats.tx_packets++;
ecm_ipa_ctx->net->stats.tx_bytes += skb->len;
atomic_dec(&ecm_ipa_ctx->outstanding_pkts);
if (netif_queue_stopped(ecm_ipa_ctx->net) &&
netif_carrier_ok(ecm_ipa_ctx->net) &&
atomic_read(&ecm_ipa_ctx->outstanding_pkts) <
(ecm_ipa_ctx->outstanding_low)) {
ECM_IPA_DEBUG("outstanding low (%d) - waking up queue\n",
ecm_ipa_ctx->outstanding_low);
netif_wake_queue(ecm_ipa_ctx->net);
}
out:
dev_kfree_skb_any(skb);
return;
}
static void ecm_ipa_tx_timeout(struct net_device *net)
{
struct ecm_ipa_dev *ecm_ipa_ctx = netdev_priv(net);
ECM_IPA_ERROR("possible IPA stall was detected, %d outstanding",
atomic_read(&ecm_ipa_ctx->outstanding_pkts));
net->stats.tx_errors++;
}
static int ecm_ipa_debugfs_atomic_open(struct inode *inode, struct file *file)
{
struct ecm_ipa_dev *ecm_ipa_ctx = inode->i_private;
ECM_IPA_LOG_ENTRY();
file->private_data = &(ecm_ipa_ctx->outstanding_pkts);
ECM_IPA_LOG_EXIT();
return 0;
}
static ssize_t ecm_ipa_debugfs_atomic_read(struct file *file,
char __user *ubuf, size_t count, loff_t *ppos)
{
int nbytes;
u8 atomic_str[DEBUGFS_TEMP_BUF_SIZE] = {0};
atomic_t *atomic_var = file->private_data;
nbytes = scnprintf(atomic_str, sizeof(atomic_str), "%d\n",
atomic_read(atomic_var));
return simple_read_from_buffer(ubuf, count, ppos, atomic_str, nbytes);
}
#ifdef CONFIG_DEBUG_FS
static void ecm_ipa_debugfs_init(struct ecm_ipa_dev *ecm_ipa_ctx)
{
const mode_t flags_read_write = S_IRUGO | S_IWUGO;
const mode_t flags_read_only = S_IRUGO;
struct dentry *file;
ECM_IPA_LOG_ENTRY();
if (!ecm_ipa_ctx)
return;
ecm_ipa_ctx->directory = debugfs_create_dir("ecm_ipa", NULL);
if (!ecm_ipa_ctx->directory) {
ECM_IPA_ERROR("could not create debugfs directory entry\n");
goto fail_directory;
}
file = debugfs_create_u8("outstanding_high", flags_read_write,
ecm_ipa_ctx->directory, &ecm_ipa_ctx->outstanding_high);
if (!file) {
ECM_IPA_ERROR("could not create outstanding_high file\n");
goto fail_file;
}
file = debugfs_create_u8("outstanding_low", flags_read_write,
ecm_ipa_ctx->directory, &ecm_ipa_ctx->outstanding_low);
if (!file) {
ECM_IPA_ERROR("could not create outstanding_low file\n");
goto fail_file;
}
file = debugfs_create_file("outstanding", flags_read_only,
ecm_ipa_ctx->directory,
ecm_ipa_ctx, &ecm_ipa_debugfs_atomic_ops);
if (!file) {
ECM_IPA_ERROR("could not create outstanding file\n");
goto fail_file;
}
ECM_IPA_DEBUG("debugfs entries were created\n");
ECM_IPA_LOG_EXIT();
return;
fail_file:
debugfs_remove_recursive(ecm_ipa_ctx->directory);
fail_directory:
return;
}
static void ecm_ipa_debugfs_destroy(struct ecm_ipa_dev *ecm_ipa_ctx)
{
debugfs_remove_recursive(ecm_ipa_ctx->directory);
}
#else /* !CONFIG_DEBUG_FS*/
static void ecm_ipa_debugfs_init(struct ecm_ipa_dev *ecm_ipa_ctx) {}
static void ecm_ipa_debugfs_destroy(struct ecm_ipa_dev *ecm_ipa_ctx) {}
#endif /* CONFIG_DEBUG_FS */
/**
* ecm_ipa_ep_cfg() - configure the USB endpoints for ECM
*
*usb_to_ipa_hdl: handle received from ipa_connect
*ipa_to_usb_hdl: handle received from ipa_connect
*
* USB to IPA pipe:
* - No de-aggregation
* - Remove Ethernet header
* - SRC NAT
* - Default routing(0)
* IPA to USB Pipe:
* - No aggregation
* - Add Ethernet header
*/
static int ecm_ipa_ep_registers_cfg(u32 usb_to_ipa_hdl, u32 ipa_to_usb_hdl)
{
int result = 0;
struct ipa_ep_cfg usb_to_ipa_ep_cfg;
struct ipa_ep_cfg ipa_to_usb_ep_cfg;
ECM_IPA_LOG_ENTRY();
memset(&usb_to_ipa_ep_cfg, 0 , sizeof(struct ipa_ep_cfg));
usb_to_ipa_ep_cfg.aggr.aggr_en = IPA_BYPASS_AGGR;
usb_to_ipa_ep_cfg.hdr.hdr_len = ETH_HLEN;
usb_to_ipa_ep_cfg.nat.nat_en = IPA_SRC_NAT;
usb_to_ipa_ep_cfg.route.rt_tbl_hdl = 0;
usb_to_ipa_ep_cfg.mode.dst = IPA_CLIENT_A5_LAN_WAN_CONS;
usb_to_ipa_ep_cfg.mode.mode = IPA_BASIC;
result = ipa_cfg_ep(usb_to_ipa_hdl, &usb_to_ipa_ep_cfg);
if (result) {
ECM_IPA_ERROR("failed to configure USB to IPA point\n");
goto out;
}
memset(&ipa_to_usb_ep_cfg, 0 , sizeof(struct ipa_ep_cfg));
ipa_to_usb_ep_cfg.aggr.aggr_en = IPA_BYPASS_AGGR;
ipa_to_usb_ep_cfg.hdr.hdr_len = ETH_HLEN;
ipa_to_usb_ep_cfg.nat.nat_en = IPA_BYPASS_NAT;
result = ipa_cfg_ep(ipa_to_usb_hdl, &ipa_to_usb_ep_cfg);
if (result) {
ECM_IPA_ERROR("failed to configure IPA to USB end-point\n");
goto out;
}
ECM_IPA_DEBUG("end-point registers successfully configured\n");
out:
ECM_IPA_LOG_EXIT();
return result;
}
/**
* ecm_ipa_set_device_ethernet_addr() - set device etherenet address
* @dev_ethaddr: device etherenet address
*
* Returns 0 for success, negative otherwise
*/
static int ecm_ipa_set_device_ethernet_addr(u8 *dev_ethaddr,
u8 device_ethaddr[])
{
if (!is_valid_ether_addr(device_ethaddr))
return -EINVAL;
memcpy(dev_ethaddr, device_ethaddr, ETH_ALEN);
ECM_IPA_DEBUG("device ethernet address: %pM\n", dev_ethaddr);
return 0;
}
/** ecm_ipa_next_state - return the next state of the driver
* @current_state: the current state of the driver
* @operation: an enum which represent the operation being made on the driver
* by its API.
*
* This function implements the driver internal state machine.
* Its decisions are based on the driver current state and the operation
* being made.
* In case the operation is invalid this state machine will return
* the value ECM_IPA_INVALID to inform the caller for a forbidden sequence.
*/
static enum ecm_ipa_state ecm_ipa_next_state(enum ecm_ipa_state current_state,
enum ecm_ipa_operation operation)
{
int next_state = ECM_IPA_INVALID;
switch (current_state) {
case ECM_IPA_UNLOADED:
if (operation == ECM_IPA_INITIALIZE)
next_state = ECM_IPA_INITIALIZED;
break;
case ECM_IPA_INITIALIZED:
if (operation == ECM_IPA_CONNECT)
next_state = ECM_IPA_CONNECTED;
else if (operation == ECM_IPA_OPEN)
next_state = ECM_IPA_UP;
else if (operation == ECM_IPA_CLEANUP)
next_state = ECM_IPA_UNLOADED;
break;
case ECM_IPA_CONNECTED:
if (operation == ECM_IPA_DISCONNECT)
next_state = ECM_IPA_INITIALIZED;
else if (operation == ECM_IPA_OPEN)
next_state = ECM_IPA_CONNECTED_AND_UP;
break;
case ECM_IPA_UP:
if (operation == ECM_IPA_STOP)
next_state = ECM_IPA_INITIALIZED;
else if (operation == ECM_IPA_CONNECT)
next_state = ECM_IPA_CONNECTED_AND_UP;
else if (operation == ECM_IPA_CLEANUP)
next_state = ECM_IPA_UNLOADED;
break;
case ECM_IPA_CONNECTED_AND_UP:
if (operation == ECM_IPA_STOP)
next_state = ECM_IPA_CONNECTED;
else if (operation == ECM_IPA_DISCONNECT)
next_state = ECM_IPA_UP;
break;
default:
ECM_IPA_ERROR("State is not supported\n");
break;
}
ECM_IPA_DEBUG("state transition ( %s -> %s )- %s\n",
ecm_ipa_state_string(current_state),
ecm_ipa_state_string(next_state) ,
next_state == ECM_IPA_INVALID ?
"Forbidden" : "Allowed");
return next_state;
}
/**
* ecm_ipa_state_string - return the state string representation
* @state: enum which describe the state
*/
static const char *ecm_ipa_state_string(enum ecm_ipa_state state)
{
switch (state) {
case ECM_IPA_UNLOADED:
return "ECM_IPA_UNLOADED";
case ECM_IPA_INITIALIZED:
return "ECM_IPA_INITIALIZED";
case ECM_IPA_CONNECTED:
return "ECM_IPA_CONNECTED";
case ECM_IPA_UP:
return "ECM_IPA_UP";
case ECM_IPA_CONNECTED_AND_UP:
return "ECM_IPA_CONNECTED_AND_UP";
default:
return "Not supported";
}
}
/**
* ecm_ipa_init_module() - module initialization
*
*/
static int ecm_ipa_init_module(void)
{
ECM_IPA_LOG_ENTRY();
ECM_IPA_LOG_EXIT();
return 0;
}
/**
* ecm_ipa_cleanup_module() - module cleanup
*
*/
static void ecm_ipa_cleanup_module(void)
{
ECM_IPA_LOG_ENTRY();
ECM_IPA_LOG_EXIT();
return;
}
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("ECM IPA network interface");
late_initcall(ecm_ipa_init_module);
module_exit(ecm_ipa_cleanup_module);
|