-
Notifications
You must be signed in to change notification settings - Fork 32
/
svc.c
1486 lines (1216 loc) · 37.1 KB
/
svc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SVC Greybus driver.
*
* Copyright 2015 Google Inc.
* Copyright 2015 Linaro Ltd.
*
* Released under the GPLv2 only.
*/
#include <linux/debugfs.h>
#include <linux/workqueue.h>
#include "greybus.h"
#define SVC_INTF_EJECT_TIMEOUT 9000
#define SVC_INTF_ACTIVATE_TIMEOUT 6000
#define SVC_INTF_RESUME_TIMEOUT 3000
struct gb_svc_deferred_request {
struct work_struct work;
struct gb_operation *operation;
};
static int gb_svc_queue_deferred_request(struct gb_operation *operation);
static ssize_t endo_id_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct gb_svc *svc = to_gb_svc(dev);
return sprintf(buf, "0x%04x\n", svc->endo_id);
}
static DEVICE_ATTR_RO(endo_id);
static ssize_t ap_intf_id_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct gb_svc *svc = to_gb_svc(dev);
return sprintf(buf, "%u\n", svc->ap_intf_id);
}
static DEVICE_ATTR_RO(ap_intf_id);
// FIXME
// This is a hack, we need to do this "right" and clean the interface up
// properly, not just forcibly yank the thing out of the system and hope for the
// best. But for now, people want their modules to come out without having to
// throw the thing to the ground or get out a screwdriver.
static ssize_t intf_eject_store(struct device *dev,
struct device_attribute *attr, const char *buf,
size_t len)
{
struct gb_svc *svc = to_gb_svc(dev);
unsigned short intf_id;
int ret;
ret = kstrtou16(buf, 10, &intf_id);
if (ret < 0)
return ret;
dev_warn(dev, "Forcibly trying to eject interface %d\n", intf_id);
ret = gb_svc_intf_eject(svc, intf_id);
if (ret < 0)
return ret;
return len;
}
static DEVICE_ATTR_WO(intf_eject);
static ssize_t watchdog_show(struct device *dev, struct device_attribute *attr,
char *buf)
{
struct gb_svc *svc = to_gb_svc(dev);
return sprintf(buf, "%s\n",
gb_svc_watchdog_enabled(svc) ? "enabled" : "disabled");
}
static ssize_t watchdog_store(struct device *dev,
struct device_attribute *attr, const char *buf,
size_t len)
{
struct gb_svc *svc = to_gb_svc(dev);
int retval;
bool user_request;
retval = strtobool(buf, &user_request);
if (retval)
return retval;
if (user_request)
retval = gb_svc_watchdog_enable(svc);
else
retval = gb_svc_watchdog_disable(svc);
if (retval)
return retval;
return len;
}
static DEVICE_ATTR_RW(watchdog);
static ssize_t watchdog_action_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct gb_svc *svc = to_gb_svc(dev);
if (svc->action == GB_SVC_WATCHDOG_BITE_PANIC_KERNEL)
return sprintf(buf, "panic\n");
else if (svc->action == GB_SVC_WATCHDOG_BITE_RESET_UNIPRO)
return sprintf(buf, "reset\n");
return -EINVAL;
}
static ssize_t watchdog_action_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t len)
{
struct gb_svc *svc = to_gb_svc(dev);
if (sysfs_streq(buf, "panic"))
svc->action = GB_SVC_WATCHDOG_BITE_PANIC_KERNEL;
else if (sysfs_streq(buf, "reset"))
svc->action = GB_SVC_WATCHDOG_BITE_RESET_UNIPRO;
else
return -EINVAL;
return len;
}
static DEVICE_ATTR_RW(watchdog_action);
static int gb_svc_pwrmon_rail_count_get(struct gb_svc *svc, u8 *value)
{
struct gb_svc_pwrmon_rail_count_get_response response;
int ret;
ret = gb_operation_sync(svc->connection,
GB_SVC_TYPE_PWRMON_RAIL_COUNT_GET, NULL, 0,
&response, sizeof(response));
if (ret) {
dev_err(&svc->dev, "failed to get rail count: %d\n", ret);
return ret;
}
*value = response.rail_count;
return 0;
}
static int gb_svc_pwrmon_rail_names_get(struct gb_svc *svc,
struct gb_svc_pwrmon_rail_names_get_response *response,
size_t bufsize)
{
int ret;
ret = gb_operation_sync(svc->connection,
GB_SVC_TYPE_PWRMON_RAIL_NAMES_GET, NULL, 0,
response, bufsize);
if (ret) {
dev_err(&svc->dev, "failed to get rail names: %d\n", ret);
return ret;
}
if (response->status != GB_SVC_OP_SUCCESS) {
dev_err(&svc->dev,
"SVC error while getting rail names: %u\n",
response->status);
return -EREMOTEIO;
}
return 0;
}
static int gb_svc_pwrmon_sample_get(struct gb_svc *svc, u8 rail_id,
u8 measurement_type, u32 *value)
{
struct gb_svc_pwrmon_sample_get_request request;
struct gb_svc_pwrmon_sample_get_response response;
int ret;
request.rail_id = rail_id;
request.measurement_type = measurement_type;
ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_PWRMON_SAMPLE_GET,
&request, sizeof(request),
&response, sizeof(response));
if (ret) {
dev_err(&svc->dev, "failed to get rail sample: %d\n", ret);
return ret;
}
if (response.result) {
dev_err(&svc->dev,
"UniPro error while getting rail power sample (%d %d): %d\n",
rail_id, measurement_type, response.result);
switch (response.result) {
case GB_SVC_PWRMON_GET_SAMPLE_INVAL:
return -EINVAL;
case GB_SVC_PWRMON_GET_SAMPLE_NOSUPP:
return -ENOMSG;
default:
return -EREMOTEIO;
}
}
*value = le32_to_cpu(response.measurement);
return 0;
}
int gb_svc_pwrmon_intf_sample_get(struct gb_svc *svc, u8 intf_id,
u8 measurement_type, u32 *value)
{
struct gb_svc_pwrmon_intf_sample_get_request request;
struct gb_svc_pwrmon_intf_sample_get_response response;
int ret;
request.intf_id = intf_id;
request.measurement_type = measurement_type;
ret = gb_operation_sync(svc->connection,
GB_SVC_TYPE_PWRMON_INTF_SAMPLE_GET,
&request, sizeof(request),
&response, sizeof(response));
if (ret) {
dev_err(&svc->dev, "failed to get intf sample: %d\n", ret);
return ret;
}
if (response.result) {
dev_err(&svc->dev,
"UniPro error while getting intf power sample (%d %d): %d\n",
intf_id, measurement_type, response.result);
switch (response.result) {
case GB_SVC_PWRMON_GET_SAMPLE_INVAL:
return -EINVAL;
case GB_SVC_PWRMON_GET_SAMPLE_NOSUPP:
return -ENOMSG;
default:
return -EREMOTEIO;
}
}
*value = le32_to_cpu(response.measurement);
return 0;
}
static struct attribute *svc_attrs[] = {
&dev_attr_endo_id.attr,
&dev_attr_ap_intf_id.attr,
&dev_attr_intf_eject.attr,
&dev_attr_watchdog.attr,
&dev_attr_watchdog_action.attr,
NULL,
};
ATTRIBUTE_GROUPS(svc);
int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
{
struct gb_svc_intf_device_id_request request;
request.intf_id = intf_id;
request.device_id = device_id;
return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID,
&request, sizeof(request), NULL, 0);
}
int gb_svc_intf_eject(struct gb_svc *svc, u8 intf_id)
{
struct gb_svc_intf_eject_request request;
int ret;
request.intf_id = intf_id;
/*
* The pulse width for module release in svc is long so we need to
* increase the timeout so the operation will not return to soon.
*/
ret = gb_operation_sync_timeout(svc->connection,
GB_SVC_TYPE_INTF_EJECT, &request,
sizeof(request), NULL, 0,
SVC_INTF_EJECT_TIMEOUT);
if (ret) {
dev_err(&svc->dev, "failed to eject interface %u\n", intf_id);
return ret;
}
return 0;
}
int gb_svc_intf_vsys_set(struct gb_svc *svc, u8 intf_id, bool enable)
{
struct gb_svc_intf_vsys_request request;
struct gb_svc_intf_vsys_response response;
int type, ret;
request.intf_id = intf_id;
if (enable)
type = GB_SVC_TYPE_INTF_VSYS_ENABLE;
else
type = GB_SVC_TYPE_INTF_VSYS_DISABLE;
ret = gb_operation_sync(svc->connection, type,
&request, sizeof(request),
&response, sizeof(response));
if (ret < 0)
return ret;
if (response.result_code != GB_SVC_INTF_VSYS_OK)
return -EREMOTEIO;
return 0;
}
int gb_svc_intf_refclk_set(struct gb_svc *svc, u8 intf_id, bool enable)
{
struct gb_svc_intf_refclk_request request;
struct gb_svc_intf_refclk_response response;
int type, ret;
request.intf_id = intf_id;
if (enable)
type = GB_SVC_TYPE_INTF_REFCLK_ENABLE;
else
type = GB_SVC_TYPE_INTF_REFCLK_DISABLE;
ret = gb_operation_sync(svc->connection, type,
&request, sizeof(request),
&response, sizeof(response));
if (ret < 0)
return ret;
if (response.result_code != GB_SVC_INTF_REFCLK_OK)
return -EREMOTEIO;
return 0;
}
int gb_svc_intf_unipro_set(struct gb_svc *svc, u8 intf_id, bool enable)
{
struct gb_svc_intf_unipro_request request;
struct gb_svc_intf_unipro_response response;
int type, ret;
request.intf_id = intf_id;
if (enable)
type = GB_SVC_TYPE_INTF_UNIPRO_ENABLE;
else
type = GB_SVC_TYPE_INTF_UNIPRO_DISABLE;
ret = gb_operation_sync(svc->connection, type,
&request, sizeof(request),
&response, sizeof(response));
if (ret < 0)
return ret;
if (response.result_code != GB_SVC_INTF_UNIPRO_OK)
return -EREMOTEIO;
return 0;
}
int gb_svc_intf_activate(struct gb_svc *svc, u8 intf_id, u8 *intf_type)
{
struct gb_svc_intf_activate_request request;
struct gb_svc_intf_activate_response response;
int ret;
request.intf_id = intf_id;
ret = gb_operation_sync_timeout(svc->connection,
GB_SVC_TYPE_INTF_ACTIVATE,
&request, sizeof(request),
&response, sizeof(response),
SVC_INTF_ACTIVATE_TIMEOUT);
if (ret < 0)
return ret;
if (response.status != GB_SVC_OP_SUCCESS) {
dev_err(&svc->dev, "failed to activate interface %u: %u\n",
intf_id, response.status);
return -EREMOTEIO;
}
*intf_type = response.intf_type;
return 0;
}
int gb_svc_intf_resume(struct gb_svc *svc, u8 intf_id)
{
struct gb_svc_intf_resume_request request;
struct gb_svc_intf_resume_response response;
int ret;
request.intf_id = intf_id;
ret = gb_operation_sync_timeout(svc->connection,
GB_SVC_TYPE_INTF_RESUME,
&request, sizeof(request),
&response, sizeof(response),
SVC_INTF_RESUME_TIMEOUT);
if (ret < 0) {
dev_err(&svc->dev, "failed to send interface resume %u: %d\n",
intf_id, ret);
return ret;
}
if (response.status != GB_SVC_OP_SUCCESS) {
dev_err(&svc->dev, "failed to resume interface %u: %u\n",
intf_id, response.status);
return -EREMOTEIO;
}
return 0;
}
int gb_svc_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
u32 *value)
{
struct gb_svc_dme_peer_get_request request;
struct gb_svc_dme_peer_get_response response;
u16 result;
int ret;
request.intf_id = intf_id;
request.attr = cpu_to_le16(attr);
request.selector = cpu_to_le16(selector);
ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_GET,
&request, sizeof(request),
&response, sizeof(response));
if (ret) {
dev_err(&svc->dev, "failed to get DME attribute (%u 0x%04x %u): %d\n",
intf_id, attr, selector, ret);
return ret;
}
result = le16_to_cpu(response.result_code);
if (result) {
dev_err(&svc->dev, "UniPro error while getting DME attribute (%u 0x%04x %u): %u\n",
intf_id, attr, selector, result);
return -EREMOTEIO;
}
if (value)
*value = le32_to_cpu(response.attr_value);
return 0;
}
int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
u32 value)
{
struct gb_svc_dme_peer_set_request request;
struct gb_svc_dme_peer_set_response response;
u16 result;
int ret;
request.intf_id = intf_id;
request.attr = cpu_to_le16(attr);
request.selector = cpu_to_le16(selector);
request.value = cpu_to_le32(value);
ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_SET,
&request, sizeof(request),
&response, sizeof(response));
if (ret) {
dev_err(&svc->dev, "failed to set DME attribute (%u 0x%04x %u %u): %d\n",
intf_id, attr, selector, value, ret);
return ret;
}
result = le16_to_cpu(response.result_code);
if (result) {
dev_err(&svc->dev, "UniPro error while setting DME attribute (%u 0x%04x %u %u): %u\n",
intf_id, attr, selector, value, result);
return -EREMOTEIO;
}
return 0;
}
int gb_svc_connection_create(struct gb_svc *svc,
u8 intf1_id, u16 cport1_id,
u8 intf2_id, u16 cport2_id,
u8 cport_flags)
{
struct gb_svc_conn_create_request request;
request.intf1_id = intf1_id;
request.cport1_id = cpu_to_le16(cport1_id);
request.intf2_id = intf2_id;
request.cport2_id = cpu_to_le16(cport2_id);
request.tc = 0; /* TC0 */
request.flags = cport_flags;
return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
&request, sizeof(request), NULL, 0);
}
void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
u8 intf2_id, u16 cport2_id)
{
struct gb_svc_conn_destroy_request request;
struct gb_connection *connection = svc->connection;
int ret;
request.intf1_id = intf1_id;
request.cport1_id = cpu_to_le16(cport1_id);
request.intf2_id = intf2_id;
request.cport2_id = cpu_to_le16(cport2_id);
ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY,
&request, sizeof(request), NULL, 0);
if (ret) {
dev_err(&svc->dev, "failed to destroy connection (%u:%u %u:%u): %d\n",
intf1_id, cport1_id, intf2_id, cport2_id, ret);
}
}
int gb_svc_timesync_enable(struct gb_svc *svc, u8 count, u64 frame_time,
u32 strobe_delay, u32 refclk)
{
struct gb_connection *connection = svc->connection;
struct gb_svc_timesync_enable_request request;
request.count = count;
request.frame_time = cpu_to_le64(frame_time);
request.strobe_delay = cpu_to_le32(strobe_delay);
request.refclk = cpu_to_le32(refclk);
return gb_operation_sync(connection,
GB_SVC_TYPE_TIMESYNC_ENABLE,
&request, sizeof(request), NULL, 0);
}
int gb_svc_timesync_disable(struct gb_svc *svc)
{
struct gb_connection *connection = svc->connection;
return gb_operation_sync(connection,
GB_SVC_TYPE_TIMESYNC_DISABLE,
NULL, 0, NULL, 0);
}
int gb_svc_timesync_authoritative(struct gb_svc *svc, u64 *frame_time)
{
struct gb_connection *connection = svc->connection;
struct gb_svc_timesync_authoritative_response response;
int ret, i;
ret = gb_operation_sync(connection,
GB_SVC_TYPE_TIMESYNC_AUTHORITATIVE, NULL, 0,
&response, sizeof(response));
if (ret < 0)
return ret;
for (i = 0; i < GB_TIMESYNC_MAX_STROBES; i++)
frame_time[i] = le64_to_cpu(response.frame_time[i]);
return 0;
}
int gb_svc_timesync_ping(struct gb_svc *svc, u64 *frame_time)
{
struct gb_connection *connection = svc->connection;
struct gb_svc_timesync_ping_response response;
int ret;
ret = gb_operation_sync(connection,
GB_SVC_TYPE_TIMESYNC_PING,
NULL, 0,
&response, sizeof(response));
if (ret < 0)
return ret;
*frame_time = le64_to_cpu(response.frame_time);
return 0;
}
int gb_svc_timesync_wake_pins_acquire(struct gb_svc *svc, u32 strobe_mask)
{
struct gb_connection *connection = svc->connection;
struct gb_svc_timesync_wake_pins_acquire_request request;
request.strobe_mask = cpu_to_le32(strobe_mask);
return gb_operation_sync(connection,
GB_SVC_TYPE_TIMESYNC_WAKE_PINS_ACQUIRE,
&request, sizeof(request),
NULL, 0);
}
int gb_svc_timesync_wake_pins_release(struct gb_svc *svc)
{
struct gb_connection *connection = svc->connection;
return gb_operation_sync(connection,
GB_SVC_TYPE_TIMESYNC_WAKE_PINS_RELEASE,
NULL, 0, NULL, 0);
}
/* Creates bi-directional routes between the devices */
int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
u8 intf2_id, u8 dev2_id)
{
struct gb_svc_route_create_request request;
request.intf1_id = intf1_id;
request.dev1_id = dev1_id;
request.intf2_id = intf2_id;
request.dev2_id = dev2_id;
return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
&request, sizeof(request), NULL, 0);
}
/* Destroys bi-directional routes between the devices */
void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id)
{
struct gb_svc_route_destroy_request request;
int ret;
request.intf1_id = intf1_id;
request.intf2_id = intf2_id;
ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY,
&request, sizeof(request), NULL, 0);
if (ret) {
dev_err(&svc->dev, "failed to destroy route (%u %u): %d\n",
intf1_id, intf2_id, ret);
}
}
int gb_svc_intf_set_power_mode(struct gb_svc *svc, u8 intf_id, u8 hs_series,
u8 tx_mode, u8 tx_gear, u8 tx_nlanes,
u8 tx_amplitude, u8 tx_hs_equalizer,
u8 rx_mode, u8 rx_gear, u8 rx_nlanes,
u8 flags, u32 quirks,
struct gb_svc_l2_timer_cfg *local,
struct gb_svc_l2_timer_cfg *remote)
{
struct gb_svc_intf_set_pwrm_request request;
struct gb_svc_intf_set_pwrm_response response;
int ret;
u16 result_code;
memset(&request, 0, sizeof(request));
request.intf_id = intf_id;
request.hs_series = hs_series;
request.tx_mode = tx_mode;
request.tx_gear = tx_gear;
request.tx_nlanes = tx_nlanes;
request.tx_amplitude = tx_amplitude;
request.tx_hs_equalizer = tx_hs_equalizer;
request.rx_mode = rx_mode;
request.rx_gear = rx_gear;
request.rx_nlanes = rx_nlanes;
request.flags = flags;
request.quirks = cpu_to_le32(quirks);
if (local)
request.local_l2timerdata = *local;
if (remote)
request.remote_l2timerdata = *remote;
ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_SET_PWRM,
&request, sizeof(request),
&response, sizeof(response));
if (ret < 0)
return ret;
result_code = response.result_code;
if (result_code != GB_SVC_SETPWRM_PWR_LOCAL) {
dev_err(&svc->dev, "set power mode = %d\n", result_code);
return -EIO;
}
return 0;
}
EXPORT_SYMBOL_GPL(gb_svc_intf_set_power_mode);
int gb_svc_intf_set_power_mode_hibernate(struct gb_svc *svc, u8 intf_id)
{
struct gb_svc_intf_set_pwrm_request request;
struct gb_svc_intf_set_pwrm_response response;
int ret;
u16 result_code;
memset(&request, 0, sizeof(request));
request.intf_id = intf_id;
request.hs_series = GB_SVC_UNIPRO_HS_SERIES_A;
request.tx_mode = GB_SVC_UNIPRO_HIBERNATE_MODE;
request.rx_mode = GB_SVC_UNIPRO_HIBERNATE_MODE;
ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_SET_PWRM,
&request, sizeof(request),
&response, sizeof(response));
if (ret < 0) {
dev_err(&svc->dev,
"failed to send set power mode operation to interface %u: %d\n",
intf_id, ret);
return ret;
}
result_code = response.result_code;
if (result_code != GB_SVC_SETPWRM_PWR_OK) {
dev_err(&svc->dev,
"failed to hibernate the link for interface %u: %u\n",
intf_id, result_code);
return -EIO;
}
return 0;
}
int gb_svc_ping(struct gb_svc *svc)
{
return gb_operation_sync_timeout(svc->connection, GB_SVC_TYPE_PING,
NULL, 0, NULL, 0,
GB_OPERATION_TIMEOUT_DEFAULT * 2);
}
static int gb_svc_version_request(struct gb_operation *op)
{
struct gb_connection *connection = op->connection;
struct gb_svc *svc = gb_connection_get_data(connection);
struct gb_svc_version_request *request;
struct gb_svc_version_response *response;
if (op->request->payload_size < sizeof(*request)) {
dev_err(&svc->dev, "short version request (%zu < %zu)\n",
op->request->payload_size,
sizeof(*request));
return -EINVAL;
}
request = op->request->payload;
if (request->major > GB_SVC_VERSION_MAJOR) {
dev_warn(&svc->dev, "unsupported major version (%u > %u)\n",
request->major, GB_SVC_VERSION_MAJOR);
return -ENOTSUPP;
}
svc->protocol_major = request->major;
svc->protocol_minor = request->minor;
if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL))
return -ENOMEM;
response = op->response->payload;
response->major = svc->protocol_major;
response->minor = svc->protocol_minor;
return 0;
}
static ssize_t pwr_debugfs_voltage_read(struct file *file, char __user *buf,
size_t len, loff_t *offset)
{
struct svc_debugfs_pwrmon_rail *pwrmon_rails = file->f_inode->i_private;
struct gb_svc *svc = pwrmon_rails->svc;
int ret, desc;
u32 value;
char buff[16];
ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
GB_SVC_PWRMON_TYPE_VOL, &value);
if (ret) {
dev_err(&svc->dev,
"failed to get voltage sample %u: %d\n",
pwrmon_rails->id, ret);
return ret;
}
desc = scnprintf(buff, sizeof(buff), "%u\n", value);
return simple_read_from_buffer(buf, len, offset, buff, desc);
}
static ssize_t pwr_debugfs_current_read(struct file *file, char __user *buf,
size_t len, loff_t *offset)
{
struct svc_debugfs_pwrmon_rail *pwrmon_rails = file->f_inode->i_private;
struct gb_svc *svc = pwrmon_rails->svc;
int ret, desc;
u32 value;
char buff[16];
ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
GB_SVC_PWRMON_TYPE_CURR, &value);
if (ret) {
dev_err(&svc->dev,
"failed to get current sample %u: %d\n",
pwrmon_rails->id, ret);
return ret;
}
desc = scnprintf(buff, sizeof(buff), "%u\n", value);
return simple_read_from_buffer(buf, len, offset, buff, desc);
}
static ssize_t pwr_debugfs_power_read(struct file *file, char __user *buf,
size_t len, loff_t *offset)
{
struct svc_debugfs_pwrmon_rail *pwrmon_rails = file->f_inode->i_private;
struct gb_svc *svc = pwrmon_rails->svc;
int ret, desc;
u32 value;
char buff[16];
ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
GB_SVC_PWRMON_TYPE_PWR, &value);
if (ret) {
dev_err(&svc->dev, "failed to get power sample %u: %d\n",
pwrmon_rails->id, ret);
return ret;
}
desc = scnprintf(buff, sizeof(buff), "%u\n", value);
return simple_read_from_buffer(buf, len, offset, buff, desc);
}
static const struct file_operations pwrmon_debugfs_voltage_fops = {
.read = pwr_debugfs_voltage_read,
};
static const struct file_operations pwrmon_debugfs_current_fops = {
.read = pwr_debugfs_current_read,
};
static const struct file_operations pwrmon_debugfs_power_fops = {
.read = pwr_debugfs_power_read,
};
static void gb_svc_pwrmon_debugfs_init(struct gb_svc *svc)
{
int i;
size_t bufsize;
struct dentry *dent;
struct gb_svc_pwrmon_rail_names_get_response *rail_names;
u8 rail_count;
dent = debugfs_create_dir("pwrmon", svc->debugfs_dentry);
if (IS_ERR_OR_NULL(dent))
return;
if (gb_svc_pwrmon_rail_count_get(svc, &rail_count))
goto err_pwrmon_debugfs;
if (!rail_count || rail_count > GB_SVC_PWRMON_MAX_RAIL_COUNT)
goto err_pwrmon_debugfs;
bufsize = sizeof(*rail_names) +
GB_SVC_PWRMON_RAIL_NAME_BUFSIZE * rail_count;
rail_names = kzalloc(bufsize, GFP_KERNEL);
if (!rail_names)
goto err_pwrmon_debugfs;
svc->pwrmon_rails = kcalloc(rail_count, sizeof(*svc->pwrmon_rails),
GFP_KERNEL);
if (!svc->pwrmon_rails)
goto err_pwrmon_debugfs_free;
if (gb_svc_pwrmon_rail_names_get(svc, rail_names, bufsize))
goto err_pwrmon_debugfs_free;
for (i = 0; i < rail_count; i++) {
struct dentry *dir;
struct svc_debugfs_pwrmon_rail *rail = &svc->pwrmon_rails[i];
char fname[GB_SVC_PWRMON_RAIL_NAME_BUFSIZE];
snprintf(fname, sizeof(fname), "%s",
(char *)&rail_names->name[i]);
rail->id = i;
rail->svc = svc;
dir = debugfs_create_dir(fname, dent);
debugfs_create_file("voltage_now", S_IRUGO, dir, rail,
&pwrmon_debugfs_voltage_fops);
debugfs_create_file("current_now", S_IRUGO, dir, rail,
&pwrmon_debugfs_current_fops);
debugfs_create_file("power_now", S_IRUGO, dir, rail,
&pwrmon_debugfs_power_fops);
}
kfree(rail_names);
return;
err_pwrmon_debugfs_free:
kfree(rail_names);
kfree(svc->pwrmon_rails);
svc->pwrmon_rails = NULL;
err_pwrmon_debugfs:
debugfs_remove(dent);
}
static void gb_svc_debugfs_init(struct gb_svc *svc)
{
svc->debugfs_dentry = debugfs_create_dir(dev_name(&svc->dev),
gb_debugfs_get());
gb_svc_pwrmon_debugfs_init(svc);
}
static void gb_svc_debugfs_exit(struct gb_svc *svc)
{
debugfs_remove_recursive(svc->debugfs_dentry);
kfree(svc->pwrmon_rails);
svc->pwrmon_rails = NULL;
}
static int gb_svc_hello(struct gb_operation *op)
{
struct gb_connection *connection = op->connection;
struct gb_svc *svc = gb_connection_get_data(connection);
struct gb_svc_hello_request *hello_request;
int ret;
if (op->request->payload_size < sizeof(*hello_request)) {
dev_warn(&svc->dev, "short hello request (%zu < %zu)\n",
op->request->payload_size,
sizeof(*hello_request));
return -EINVAL;
}
hello_request = op->request->payload;
svc->endo_id = le16_to_cpu(hello_request->endo_id);
svc->ap_intf_id = hello_request->interface_id;
ret = device_add(&svc->dev);
if (ret) {
dev_err(&svc->dev, "failed to register svc device: %d\n", ret);
return ret;
}
ret = gb_svc_watchdog_create(svc);
if (ret) {
dev_err(&svc->dev, "failed to create watchdog: %d\n", ret);
goto err_unregister_device;
}
gb_svc_debugfs_init(svc);
ret = gb_timesync_svc_add(svc);
if (ret) {
dev_err(&svc->dev, "failed to add SVC to timesync: %d\n", ret);
gb_svc_debugfs_exit(svc);
goto err_unregister_device;
}
return gb_svc_queue_deferred_request(op);
err_unregister_device:
gb_svc_watchdog_destroy(svc);
device_del(&svc->dev);
return ret;
}
static struct gb_interface *gb_svc_interface_lookup(struct gb_svc *svc,
u8 intf_id)
{
struct gb_host_device *hd = svc->hd;
struct gb_module *module;
size_t num_interfaces;
u8 module_id;
list_for_each_entry(module, &hd->modules, hd_node) {
module_id = module->module_id;
num_interfaces = module->num_interfaces;
if (intf_id >= module_id &&
intf_id < module_id + num_interfaces) {
return module->interfaces[intf_id - module_id];
}
}
return NULL;
}
static struct gb_module *gb_svc_module_lookup(struct gb_svc *svc, u8 module_id)
{
struct gb_host_device *hd = svc->hd;
struct gb_module *module;
list_for_each_entry(module, &hd->modules, hd_node) {
if (module->module_id == module_id)
return module;
}
return NULL;
}
static void gb_svc_process_hello_deferred(struct gb_operation *operation)
{
struct gb_connection *connection = operation->connection;
struct gb_svc *svc = gb_connection_get_data(connection);