-
Notifications
You must be signed in to change notification settings - Fork 20
/
mlnx-asic-drv.c
2570 lines (2274 loc) · 83.6 KB
/
mlnx-asic-drv.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
/**
*
* Copyright (C) Mellanox Technologies Ltd. 2001-2015. 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 as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <linux/module.h>
#include <linux/version.h>
#include <linux/types.h>
#include <linux/acpi.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/dmi.h>
#include <linux/capability.h>
#include <linux/mutex.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/i2c.h>
#include <linux/leds.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/thermal.h>
#include "mlnx-common-drv.h"
#include "mlx_sx/kernel_user.h"
#include "device.h"
#define TEMP_POLLING_INTERVAL (30 * 1000) /* interval between two temperature checks is 30 seconds */
#define TEMP_PASSIVE_INTERVAL (30 * 1000) /* interval to wait between polls when performing passive cooling is 30 seconds */
#define MAX_PWM_DUTY_CYCLE 255
#define PWM_DUTY_CYCLE_STEP 10
#define MAX_ASIC_TEMP (105 * 1000) /* maximum allowed temperature for ASIC is 105 degrees */
#define ASIC_GROUP_NUM 4
#define FAN_NUM 12
#define TACH_PWM_MAP 16
#define FAN_TACH_NUM 4
#define QSFP_MODULE_NUM 64
#define FAN_ATTR_NUM 5
#define TEMP_ATTR_NUM 4
#define CPLD_ATTR_NUM 1
#define QSFP_ATTR_NUM 6
#define QSFP_DATA_VALID_TIME (120 * 1000) /* 120 seconds */
#define ENTRY_DATA_VALID_TIME (3 * 1000) /* 3 seconds */
#define QSFP_PAGE_NUM 5
#define QSFP_SUB_PAGE_NUM 3
#define QSFP_PAGE_SIZE 128
#define QSFP_SUB_PAGE_SIZE 48
#define QSFP_LAST_SUB_PAGE_SIZE 32
enum fan_contol_mode {
FAN_CTRL_KERNEL,
FAN_CTRL_US_LEGACY,
FAN_CTRL_US_PRIVATE,
};
typedef enum qsfp_module_status {
qsfp_good = 0x00,
qsfp_no_eeprrom = 0x01, /* No response from module's EEPROM. */
qsfp_not_supported = 0x02, /* Module type not supported by the device. */
qsfp_not_connected = 0x03, /* No module present indication.*/
qsfp_type_invalid = 0x04, /* if the module is not qsfp or sfp bus.*/
qsfp_not_accessiable = 0x05, /* Not accessable module */
qsfp_i2c_error = 0x09, /* Error occurred while trying to access the module's EEPROM using i2c */
qsfp_disable = 0x10, /* module is disabled by disable command */
} qsfp_module_status_t;
static inline const char *qsfp_status_2string(qsfp_module_status_t status)
{
switch (status) {
case qsfp_good:
return "good";
case qsfp_no_eeprrom:
return "no_eeprrom";
case qsfp_not_connected:
return "not_connected";
case qsfp_type_invalid:
return "type_invalid";
case qsfp_not_accessiable:
return "not_accessiable";
case qsfp_i2c_error:
return "i2c_error";
case qsfp_disable:
return "disable";
default:
return "not exist";
}
}
typedef enum temp_module_attr {
temp_input,
temp_min,
temp_max,
temp_crit,
temp_conf,
} temp_module_attr_t;
typedef enum fan_module_attr {
fan_power,
fan_speed_tacho0,
fan_speed_tacho1,
fan_speed_tacho2,
fan_speed_tacho3,
fan_speed_min,
fan_speed_max,
fan_enable,
fan_conf,
} fan_module_attr_t;
typedef enum qsfp_module_attr {
qsfp_status,
qsfp_event,
qsfp_temp_input,
qsfp_temp_min,
qsfp_temp_max,
qsfp_temp_crit,
} qsfp_module_attr_t;
typedef enum cpld_attr {
cpld_version,
} cpld_attr_t;
struct fan_config {
struct mlnx_bsp_entry entry; /* Entry id */
u8 num_tachos; /* Tachometers' number */
u8 tacho_id[FAN_TACH_NUM]; /* Fan tachometer index */
u8 pwm_id; /* PWM tachometer index */
u16 speed[FAN_TACH_NUM]; /* Fan speed (Round Per Minute) calculated based on the time measurement between n */
/* fan pulses. Note that in order for the RPM to be correct, the n value should */
/* correspond to the number of tachometer pulses per rotation measured by the tachometer */
u16 speed_min[FAN_TACH_NUM]; /* Fan speed minimum (Round Per Minute) */
u16 speed_max[FAN_TACH_NUM]; /* Fan speed maximum (Round Per Minute) */
u8 enable[FAN_TACH_NUM]; /* Software enable state */
u8 pwm_duty_cycle; /* Controls the duty cycle of the PWM. Value range from 0..255 */
};
struct temp_config {
struct mlnx_bsp_entry entry; /* Entry id */
u8 sensor_index; /* Sensors index to access */
u32 temperature; /* Temperature reading from the sensor. Reading in 0.125 Celsius degrees */
u8 mte; /* Enables measuring the max temperature on a sensor */
u8 mtr; /* Clears the value of the max temperature register */
u32 max_temperature; /* The highest measured temperature from the sensor */
u8 tee; /* Temperature Event Enable */
u32 temperature_threshold; /* Generate event if sensor temperature measurement is above the threshold and events enabled */
};
struct qsfp_config {
struct mlnx_bsp_entry entry; /* Entry id */
u8 module_index; /* QSFP modules index to access */
u8 lock; /* Lock bit. Setting this bit will lock the access to the specific cable */
u8 status; /* module status (GOOD, NO_EEPROM_MODULES, MODULE_NOT_CONNECTED, I2C_ERROR, MODULE_DISABLED) */
};
struct cpld_config {
struct mlnx_bsp_entry entry; /* Entry id */
u8 index; /* CPLD index to access */
u32 version; /* CPLD version */
};
struct temp_config_params {
u8 num_sensors;
u8 sensor_active; /* Indicates number of connected temprature sensors */
struct temp_config *sensor;
};
struct fan_config_params {
struct mlnx_bsp_entry entry; /* Entry id */
u8 num_fan;
u8 pwm_frequency; /* Controls the frequency of the PWM signal */
u16 pwm_active; /* Indicates which of the PWM control is active (bit per PWM) */
u16 tacho_active; /* Indicates which of the tachometer is active (bit per tachometer) */
u8 num_cooling_levels; /* pwm trip levels number */
u16 *cooling_levels; /* pwm trip levels */
s16 cooling_cur_level; /* pwm current level */
struct fan_config *fan;
};
struct qsfp_config_params {
struct mlnx_bsp_entry entry; /* Entry id */
u8 num_modules;
u32 presence_bitmap[8];
unsigned long presence_bitmap_valid;
struct qsfp_config *module;
struct bin_attribute *eeprom;
struct bin_attribute **eeprom_attr_list;
};
struct cpld_config_params {
u8 num_cpld;
struct cpld_config *cpld;
};
#define LED_OFF_COLOR 0x0000
#define LED_INFINITY_COLOR 0xffff
#define LED_TYPE_UID 1
#define LED_TYPE_PORT 2
struct port_led_pdata {
struct led_classdev cdev;
struct switchdev_if *devif;
const char *name;
int index;
u8 led_type;
spinlock_t lock;
};
#define cdev_to_priv(c) container_of(c, struct port_led_pdata, cdev)
struct port_led_params {
struct platform_device *pdev;
struct switchdev_if *devif;
int num_led_instances;
struct port_led_pdata *led;
};
enum ports_capabilty {
none_drv,
asic_drv_32_ports,
asic_drv_64_ports,
asic_drv_54_ports,
asic_drv_36_ports,
asic_drv_16_ports,
asic_drv_56_ports,
};
enum chips {
any_chip,
switchx2,
spectrum,
};
struct switchdev_if {
struct mutex access_lock;
u8 dev_id;
int (*REG_MFSC)(struct sx_dev *dev, struct ku_access_mfsc_reg *reg_data);
int (*REG_MFSM)(struct sx_dev *dev, struct ku_access_mfsm_reg *reg_data);
int (*REG_MTMP)(struct sx_dev *dev, struct ku_access_mtmp_reg *reg_data);
int (*REG_MTCAP)(struct sx_dev *dev, struct ku_access_mtcap_reg *reg_data);
int (*REG_MCIA)(struct sx_dev *dev, struct ku_access_mcia_reg *reg_data);
int (*REG_PMPC)(struct sx_dev *dev, struct ku_access_pmpc_reg *reg_data);
int (*REG_MSCI)(struct sx_dev *dev, struct ku_access_msci_reg *reg_data);
int (*REG_MJTAG)(struct sx_dev *dev, struct ku_access_mjtag_reg *reg_data);
int (*REG_PMAOS)(struct sx_dev *dev, struct ku_access_pmaos_reg *reg_data);
int (*REG_MFCR)(struct sx_dev *dev, struct ku_access_mfcr_reg *reg_data);
int (*REG_MGIR)(struct sx_dev *dev, struct ku_access_mgir_reg *reg_data);
int (*REG_MLCR)(struct sx_dev *dev, struct ku_access_mlcr_reg *reg_data);
int (*REG_PMLP)(struct sx_dev *dev, struct ku_access_pmlp_reg *reg_data);
void *(*DEV_CONTEXT)(void);
};
struct asic_data {
struct list_head list;
struct kref kref;
struct i2c_client *client;
enum ports_capabilty port_cap;
enum chips kind;
struct device *hwmon_dev;
const char *name;
struct mutex access_lock;
struct temp_config_params temp_config;
struct fan_config_params fan_config;
struct cpld_config_params cpld_config;
struct qsfp_config_params qsfp_config;
struct port_led_params led_config;
struct attribute_group group[ASIC_GROUP_NUM];
const struct attribute_group *groups[ASIC_GROUP_NUM + 1];
u8 asic_id;
struct switchdev_if switchdevif;
struct thermal_cooling_device *tcdev;
struct thermal_zone_device *tzdev;
};
#define ASIC_DRV_VERSION "0.0.1 24/08/2015"
#define ASIC_DRV_DESCRIPTION "Mellanox ASIC BSP driver. Build:" " "__DATE__" "__TIME__
MODULE_AUTHOR("Vadim Pasternak ([email protected])");
MODULE_DESCRIPTION(ASIC_DRV_DESCRIPTION);
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("mlnx-asic");
static unsigned short num_cpld = 3;
module_param(num_cpld, ushort, 0);
MODULE_PARM_DESC(num_cpld, "Number of CPLD, default is 3");
static unsigned short num_tachos = 2;
module_param(num_tachos, ushort, 0);
MODULE_PARM_DESC(num_tachos, "Number of tachometers per fan, default is 2");
static unsigned short tacho_flat = 1;
module_param(tacho_flat, ushort, 0);
MODULE_PARM_DESC(tacho_flat, "Each tachometer is presented as fan, default is 1");
static unsigned short speed_min = 10500;
module_param(speed_min, ushort, 0);
MODULE_PARM_DESC(speed, "fan minimum speed (round per minute), default is 10500 RPM");
static unsigned short speed_max = 23000;
module_param(speed_max, ushort, 0);
MODULE_PARM_DESC(speed_max, "fan maximum speed (round per minute), default is 23000 (100 percent)");
static unsigned short pwm_duty_cycle = 153;
module_param(pwm_duty_cycle, ushort, 0);
MODULE_PARM_DESC(pwm_duty_cycle, "Duty cycle of the PWM, value range from 0..255, default is 153");
static unsigned short asic_dev_id = 255;
module_param(asic_dev_id, ushort, 0);
MODULE_PARM_DESC(asic_dev_id, "ASIC device Id, default is 255");
static unsigned short mte = 1;
module_param(mte, ushort, 0);
MODULE_PARM_DESC(mte, "Enable measuring the max temperature, default is enable (1)");
static unsigned short mtr = 0;
module_param(mtr, ushort, 0);
MODULE_PARM_DESC(mte, "Clear the value of the max temperature, default is not clear (0)");
static unsigned short tee = 0;
module_param(tee, ushort, 0);
MODULE_PARM_DESC(tee, "Enable temperature event, default is not disable (0)");
static unsigned short temp_threshold = 80;
module_param(temp_threshold, ushort, 0);
MODULE_PARM_DESC(temp_threshold, "Temprature threshold, default is 80");
static unsigned short qsfp_map[QSFP_MODULE_NUM] = { 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 };
module_param_array(qsfp_map, ushort, NULL, 0644);
MODULE_PARM_DESC(qsfp_map, "Module status offsets vector (default)");
static unsigned short qsfp_eeprom_i2c_addr = 0x50;
module_param(qsfp_eeprom_i2c_addr, ushort, 0);
MODULE_PARM_DESC(qsfp_eeprom_i2c_addr, "I2C address of qsfp module eeprom, default is 0x50");
static unsigned short auto_thermal_control = 0;
module_param(auto_thermal_control, ushort, 0);
MODULE_PARM_DESC(auto_thermal_control, "Automatic thermal control is enable, default is no");
static unsigned short port_led_control = 0;
module_param(port_led_control, ushort, 0);
MODULE_PARM_DESC(port_led_control, "Port led control is enable, default is no");
static ssize_t store_bind_sx_core(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t len);
static struct kobj_attribute bind_sx_core_attr = __ATTR(bind_sx_core, S_IWUSR, NULL, store_bind_sx_core);
static struct switchdev_if *g_switchdev_if = NULL;
int mlxsw_local_port_mapping[] = {
0x2d, 0x2f, 0x2a, 0x2b, 0x26, 0x28, 0x23, 0x25, 0x01, 0x21, 0x05, 0x03,
0x08, 0x06, 0x0b, 0x0a, 0x0f, 0x0d, 0x1d, 0x1f, 0x19, 0x1b, 0x15, 0x17,
0x12, 0x14, 0x30, 0x10, 0x34, 0x32, 0x37, 0x35, 0x3b, 0x39, 0x3f, 0x3d,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
#define REG_QUERY (1)
#define REG_WRITE (2)
#define SET_REG_TEMPLATE(reg_data, regid, method, devif) \
reg_data.op_tlv.type = 1; \
reg_data.op_tlv.length = 4; \
reg_data.op_tlv.dr = 0; \
reg_data.op_tlv.status = 0; \
reg_data.op_tlv.register_id = regid; \
reg_data.op_tlv.r = 0; \
reg_data.op_tlv.method = method; \
reg_data.op_tlv.op_class = 1; \
reg_data.op_tlv.tid = 0; \
reg_data.dev_id = devif->dev_id;
#define REG_ACCESS(devif, REGID, reg_data, err, ret_on_null_ptr) \
mutex_lock(&devif->access_lock); \
if (!devif->REG_##REGID || !devif->DEV_CONTEXT) { \
mutex_unlock(&devif->access_lock); \
return ret_on_null_ptr; \
} \
err = devif->REG_##REGID((struct sx_dev *)devif->DEV_CONTEXT(), ®_data); \
mutex_unlock(&devif->access_lock); \
if (err) \
return err;
#define ENTRY_DATA_VALID(entry, refresh) \
if (time_before(jiffies, entry.last_updated + refresh) && entry.valid) \
return 0;
#define GET_ONE_SX_CORE_FUNC(func_name) \
if (!(g_switchdev_if->func_name)) { \
g_switchdev_if->func_name = __symbol_get("sx_ACCESS_"#func_name); \
}
#define PUT_ONE_SX_CORE_FUNC(func_name) \
if (g_switchdev_if->func_name) { \
__symbol_put("sx_ACCESS_"#func_name); \
g_switchdev_if->func_name = NULL; \
}
static ssize_t store_bind_sx_core(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t len)
{
int ret;
int value;
ret = kstrtoint(buf, 10, &value);
if (ret) {
printk(KERN_INFO "sysfs entry bind_sx_core got invalid value\n");
return ret;
}
if (value) {
mutex_lock(&(g_switchdev_if->access_lock));
if (!g_switchdev_if->DEV_CONTEXT) {
g_switchdev_if->DEV_CONTEXT = __symbol_get("sx_get_dev_context");
}
GET_ONE_SX_CORE_FUNC(REG_MFSC);
GET_ONE_SX_CORE_FUNC(REG_MFSM);
GET_ONE_SX_CORE_FUNC(REG_MTMP);
GET_ONE_SX_CORE_FUNC(REG_MTCAP);
GET_ONE_SX_CORE_FUNC(REG_MCIA);
GET_ONE_SX_CORE_FUNC(REG_PMPC);
GET_ONE_SX_CORE_FUNC(REG_MSCI);
GET_ONE_SX_CORE_FUNC(REG_MJTAG);
GET_ONE_SX_CORE_FUNC(REG_PMAOS);
GET_ONE_SX_CORE_FUNC(REG_MFCR);
GET_ONE_SX_CORE_FUNC(REG_MGIR);
GET_ONE_SX_CORE_FUNC(REG_MLCR);
GET_ONE_SX_CORE_FUNC(REG_PMLP);
mutex_unlock(&(g_switchdev_if->access_lock));
} else {
mutex_lock(&(g_switchdev_if->access_lock));
if (g_switchdev_if->DEV_CONTEXT) {
__symbol_put("sx_get_dev_context");
g_switchdev_if->DEV_CONTEXT = NULL;
}
PUT_ONE_SX_CORE_FUNC(REG_MFSC);
PUT_ONE_SX_CORE_FUNC(REG_MFSM);
PUT_ONE_SX_CORE_FUNC(REG_MTMP);
PUT_ONE_SX_CORE_FUNC(REG_MTCAP);
PUT_ONE_SX_CORE_FUNC(REG_MCIA);
PUT_ONE_SX_CORE_FUNC(REG_PMPC);
PUT_ONE_SX_CORE_FUNC(REG_MSCI);
PUT_ONE_SX_CORE_FUNC(REG_MJTAG);
PUT_ONE_SX_CORE_FUNC(REG_PMAOS);
PUT_ONE_SX_CORE_FUNC(REG_MFCR);
PUT_ONE_SX_CORE_FUNC(REG_MGIR);
PUT_ONE_SX_CORE_FUNC(REG_MLCR);
PUT_ONE_SX_CORE_FUNC(REG_PMLP);
mutex_unlock(&(g_switchdev_if->access_lock));
}
return len;
}
static int fan_get_power(struct switchdev_if *devif, struct fan_config *fan, u8 cache_drop)
{
int err = 0;
u8 method = REG_QUERY;
struct ku_access_mfsc_reg reg_data;
if (!cache_drop)
ENTRY_DATA_VALID(fan->entry, ENTRY_DATA_VALID_TIME);
memset(®_data, 0, sizeof(struct ku_access_mfsc_reg));
SET_REG_TEMPLATE(reg_data, MFSC_REG_ID, method, devif);
reg_data.mfsc_reg.pwm = fan->pwm_id; /* Will affect all FANs */
REG_ACCESS(devif, MFSC, reg_data, err, err);
fan->entry.last_updated = jiffies;
fan->entry.valid = 1;
fan->pwm_duty_cycle = (reg_data.mfsc_reg.pwm_duty_cycle);
return err;
}
static int fan_set_power(struct switchdev_if *devif, struct fan_config *fan)
{
int err = 0;
u8 method = REG_WRITE;
struct ku_access_mfsc_reg reg_data;
memset(®_data, 0, sizeof(struct ku_access_mfsc_reg));
SET_REG_TEMPLATE(reg_data, MFSC_REG_ID, method, devif);
reg_data.mfsc_reg.pwm = fan->pwm_id; /* Will affect all FANs */
reg_data.mfsc_reg.pwm_duty_cycle = fan->pwm_duty_cycle;
REG_ACCESS(devif, MFSC, reg_data, err, err);
return err;
}
static int fan_get_speed(struct switchdev_if *devif, struct fan_config *fan, char *buf, int tacho_id)
{
int err = 0;
u8 method = REG_QUERY;
struct ku_access_mfsm_reg reg_data;
ENTRY_DATA_VALID(fan->entry, ENTRY_DATA_VALID_TIME);
memset(®_data, 0, sizeof(struct ku_access_mfsm_reg));
SET_REG_TEMPLATE(reg_data, MFSM_REG_ID, method, devif);
reg_data.mfsm_reg.tacho = fan->tacho_id[tacho_id];
REG_ACCESS(devif, MFSM, reg_data, err, ENODEV);
fan->entry.last_updated = jiffies;
fan->entry.valid = 1;
fan->speed[tacho_id] = reg_data.mfsm_reg.rpm;
return err;
}
static int fan_set_enable(struct asic_data *asicdata, int index, u8 enable)
{
struct fan_config *fan;
int err = 0;
fan = &asicdata->fan_config.fan[index];
switch (enable) {
case FAN_CTRL_KERNEL:
fan->pwm_duty_cycle = pwm_duty_cycle;
err = fan_set_power(&asicdata->switchdevif, fan);
if (err)
return err;
if (auto_thermal_control) {
asicdata->fan_config.cooling_cur_level = 0;
asicdata->tzdev->polling_delay = TEMP_POLLING_INTERVAL;
asicdata->tzdev->passive_delay = TEMP_PASSIVE_INTERVAL;
thermal_zone_device_update(asicdata->tzdev);
pr_notice("kernel mode fan control ON\n");
}
break;
case FAN_CTRL_US_LEGACY:
case FAN_CTRL_US_PRIVATE:
if (auto_thermal_control) {
asicdata->tzdev->polling_delay = 0;
asicdata->tzdev->passive_delay = 0;
thermal_zone_device_update(asicdata->tzdev);
pr_notice("kernel mode fan control OFF\n");
}
break;
default:
fan->pwm_duty_cycle = MAX_PWM_DUTY_CYCLE;
err = fan_set_power(&asicdata->switchdevif, fan);
if (err)
return err;
if (auto_thermal_control) {
asicdata->tzdev->polling_delay = 0;
asicdata->tzdev->passive_delay = 0;
thermal_zone_device_update(asicdata->tzdev);
pr_notice("kernel mode fan control OFF\n");
}
break;
}
return err;
}
static int fan_get_config(struct switchdev_if *devif, struct fan_config_params *fan_config)
{
int err = 0;
u8 method = REG_QUERY;
struct ku_access_mfcr_reg reg_data;
ENTRY_DATA_VALID(fan_config->entry, ENTRY_DATA_VALID_TIME);
memset(®_data, 0, sizeof(struct ku_access_mfcr_reg));
SET_REG_TEMPLATE(reg_data, MFCR_REG_ID, method, devif);
REG_ACCESS(devif, MFCR, reg_data, err, ENODEV);
fan_config->entry.last_updated = jiffies;
fan_config->entry.valid = 1;
fan_config->pwm_frequency = reg_data.mfcr_reg.pwm_frequency;
fan_config->pwm_active = reg_data.mfcr_reg.pwm_active;
fan_config->tacho_active = reg_data.mfcr_reg.tacho_active;
return err;
}
static int temp_get(struct switchdev_if *devif, struct temp_config *temp, u8 id, u8 cache_drop)
{
int err = 0;
u8 method = REG_QUERY;
struct ku_access_mtmp_reg reg_data;
if (!cache_drop)
ENTRY_DATA_VALID(temp->entry, ENTRY_DATA_VALID_TIME);
memset(®_data, 0, sizeof(struct ku_access_mtmp_reg));
SET_REG_TEMPLATE(reg_data, MTMP_REG_ID, method, devif);
reg_data.mtmp_reg.sensor_index = temp->sensor_index + id; /* Sensors index to access */
REG_ACCESS(devif, MTMP, reg_data, err, ENODEV);
temp->entry.last_updated = jiffies;
temp->entry.valid = 1;
/* For temp->temperature < 0 consider to set:
temp->temperature = 0xffff + ((s16)temp->temperature) + 1;
*/
temp->temperature = reg_data.mtmp_reg.temperature * 100; /* temp1_input */
temp->max_temperature = reg_data.mtmp_reg.max_temperature * 100; /* temp1_max */
temp->temperature_threshold = reg_data.mtmp_reg.temperature_threshold * 100; /* temp1_crit */
/* temp->temp1_min_hyst = reg_data.temperature_threshold_lo; Not implemented */
/* temp->temp1_max_hyst = reg_data.temperature_threshold_hi; Not implemented */
return err;
}
static int temp_get_config(struct switchdev_if *devif, struct temp_config_params *temp_config)
{
int err = 0;
u8 method = REG_QUERY;
struct ku_access_mtcap_reg reg_data;
memset(®_data, 0, sizeof(struct ku_access_mtcap_reg));
SET_REG_TEMPLATE(reg_data, MTCAP_REG_ID, method, devif);
REG_ACCESS(devif, MTCAP, reg_data, err, ENODEV);
temp_config->sensor_active = reg_data.mtcap_reg.sensor_count;
return err;
}
static int qsfp_get(struct switchdev_if *devif, struct qsfp_config *qsfp)
{
int err = 0;
u8 method = REG_QUERY;
struct ku_access_mcia_reg reg_data;
ENTRY_DATA_VALID(qsfp->entry, ENTRY_DATA_VALID_TIME);
memset(®_data, 0, sizeof(struct ku_access_mcia_reg));
SET_REG_TEMPLATE(reg_data, MCIA_REG_ID, method, devif);
reg_data.mcia_reg.module = qsfp->module_index;
reg_data.mcia_reg.l = qsfp->lock;
reg_data.mcia_reg.page_number = 0;
reg_data.mcia_reg.size = QSFP_SUB_PAGE_SIZE;
REG_ACCESS(devif, MCIA, reg_data, err, ENODEV);
qsfp->entry.last_updated = jiffies;
qsfp->entry.valid = 1;
qsfp->status = reg_data.mcia_reg.status;
return err;
}
static int qsfp_get_eeprom(struct switchdev_if *devif, struct qsfp_config *qsfp,
char *buf, loff_t off, size_t count)
{
int err = 0, res = 0, i, j, k, size, page = 0, subpage = 0, page_off = 0, subpage_off = 0;
u8 method = REG_QUERY;
struct ku_access_mcia_reg reg_data;
u32 tbuf[12];
u32 *rbuf;
u8 page_number[QSFP_PAGE_NUM] = { 0xa0, 0x00, 0x01, 0x02, 0x03 }; /* ftp://ftp.seagate.com/sff/SFF-8436.PDF */
u16 page_shift[QSFP_PAGE_NUM + 1] = { 0x00, 0x80, 0x80, 0x80, 0x80, 0x00 };
u16 sub_page_size[QSFP_SUB_PAGE_NUM] = { QSFP_SUB_PAGE_SIZE, QSFP_SUB_PAGE_SIZE, QSFP_LAST_SUB_PAGE_SIZE};
u16 copysize;
memset(®_data, 0, sizeof(struct ku_access_mcia_reg));
SET_REG_TEMPLATE(reg_data, MCIA_REG_ID, method, devif);
reg_data.mcia_reg.i2c_device_address = qsfp_eeprom_i2c_addr;
reg_data.mcia_reg.device_address = 0;
reg_data.mcia_reg.module = qsfp->module_index;
reg_data.mcia_reg.l = qsfp->lock;
/* Map offset to correct page number, subpage number and device internal offset */
page = off / QSFP_PAGE_SIZE;
page_off = off % QSFP_PAGE_SIZE;
subpage = page_off / QSFP_SUB_PAGE_SIZE;
subpage_off = page_off % QSFP_SUB_PAGE_SIZE;
reg_data.mcia_reg.device_address = subpage_off + page_shift[page];
for (i = page; i < QSFP_PAGE_NUM; i++) {
for (j = subpage; j < QSFP_SUB_PAGE_NUM; j++) {
reg_data.mcia_reg.page_number = page_number[i];
if (j == subpage)
reg_data.mcia_reg.size = sub_page_size[j] - subpage_off;
else
reg_data.mcia_reg.size = sub_page_size[j];
REG_ACCESS(devif, MCIA, reg_data, err, ENODEV);
if (reg_data.mcia_reg.status)
return err;
rbuf = ®_data.mcia_reg.dword_0;
size = ((reg_data.mcia_reg.size % 4) == 0) ? (reg_data.mcia_reg.size / 4) :
(reg_data.mcia_reg.size / 4) + 1;
for (k = 0; k < size; k++, rbuf++) {
tbuf[k] = ntohl(*rbuf);
}
if (count > reg_data.mcia_reg.size)
copysize = reg_data.mcia_reg.size;
else
copysize = count;
memcpy(buf, tbuf, copysize);
buf += copysize;
off += copysize;
count -= copysize;
res += copysize;
reg_data.mcia_reg.device_address += copysize;
if (count <= 0)
return res;
}
reg_data.mcia_reg.device_address = page_shift[i + 1];
}
return res;
}
static int qsfp_get_event(struct switchdev_if *devif, struct qsfp_config_params *qsfp_config)
{
int err = 0, i;
u8 method = REG_QUERY;
struct ku_access_pmpc_reg reg_data;
ENTRY_DATA_VALID(qsfp_config->entry, ENTRY_DATA_VALID_TIME);
memset(®_data, 0, sizeof(struct ku_access_pmpc_reg));
SET_REG_TEMPLATE(reg_data, PMPC_REG_ID, method, devif);
REG_ACCESS(devif, PMPC, reg_data, err, ENODEV);
qsfp_config->entry.last_updated = jiffies;
qsfp_config->entry.valid = 1;
for (i = 0; i < 8; i++)
qsfp_config->presence_bitmap[7 - i] = reg_data.pmpc_reg.module_state_updated_bitmap[i];
return err;
}
static int qsfp_set_event(struct switchdev_if *devif, u32 *bitmap)
{
int err = 0;
u8 method = REG_WRITE;
struct ku_access_pmpc_reg reg_data;
memset(®_data, 0, sizeof(struct ku_access_pmpc_reg));
SET_REG_TEMPLATE(reg_data, PMPC_REG_ID, method, devif);
memcpy(reg_data.pmpc_reg.module_state_updated_bitmap, bitmap,
sizeof(reg_data.pmpc_reg.module_state_updated_bitmap[0] * 8));
REG_ACCESS(devif, PMPC, reg_data, err, ENODEV);
return err;
}
#define MSCI_REG_ID 0x902A /* Missed defenition */
static int cpld_get(struct switchdev_if *devif, struct cpld_config *cpld)
{
int err = 0;
u8 method = REG_QUERY;
struct ku_access_msci_reg reg_data;
memset(®_data, 0, sizeof(struct ku_access_msci_reg));
SET_REG_TEMPLATE(reg_data, MSCI_REG_ID, method, devif);
reg_data.msci_reg.index = cpld->index;
REG_ACCESS(devif, MSCI, reg_data, err, err);
cpld->entry.last_updated = jiffies;
cpld->entry.valid = 1;
cpld->version = reg_data.msci_reg.version;
return err;
}
static int mgir_get(struct switchdev_if *devif, u16 *device_id)
{
int err = 0;
u8 method = REG_QUERY;
struct ku_access_mgir_reg reg_data;
memset(®_data, 0, sizeof(struct ku_access_mgir_reg));
SET_REG_TEMPLATE(reg_data, MGIR_REG_ID, method, devif);
REG_ACCESS(devif, MGIR, reg_data, err, err);
*device_id = reg_data.mgir_reg.hw_info.device_id;
return err;
}
static int pmlp_get(struct switchdev_if *devif, u8 local_port, u8 *width)
{
int err = 0;
u8 method = REG_QUERY;
struct ku_access_pmlp_reg reg_data;
memset(®_data, 0, sizeof(struct ku_access_mgir_reg));
SET_REG_TEMPLATE(reg_data, PMLP_REG_ID, method, devif);
reg_data.pmlp_reg.local_port = local_port;
REG_ACCESS(devif, PMLP, reg_data, err, err);
*width = reg_data.pmlp_reg.width;
return err;
}
static ssize_t store_temp(struct device *dev,
struct device_attribute *devattr,
const char *buf,
size_t count)
{
struct asic_data *asicdata = i2c_get_clientdata(to_i2c_client(dev));
int index = to_sensor_dev_attr_2(devattr)->index;
int nr = to_sensor_dev_attr_2(devattr)->nr;
struct temp_config *temp = NULL;
temp = &asicdata->temp_config.sensor[index];
if (!temp)
return -EEXIST;
switch (nr) {
case temp_min:
break;
default:
return -EEXIST;
}
return count;
}
static ssize_t show_temp(struct device *dev,
struct device_attribute *devattr,
char *buf)
{
struct asic_data *asicdata = i2c_get_clientdata(to_i2c_client(dev));
int index = to_sensor_dev_attr_2(devattr)->index;
int nr = to_sensor_dev_attr_2(devattr)->nr;
struct temp_config *temp = NULL;
int err = 0, res = 0;
temp = &asicdata->temp_config.sensor[index];
if (!temp)
return -EEXIST;
switch (nr) {
case temp_input:
err = temp_get(&asicdata->switchdevif, temp, 0, 0);
if (err)
return -EEXIST;
res = temp->temperature;
break;
case temp_min:
break;
case temp_max:
res = MAX_ASIC_TEMP;
break;
case temp_crit:
err = temp_get(&asicdata->switchdevif, temp, 0, 0);
if (err)
return -EEXIST;
res = temp->temperature_threshold;
break;
case temp_conf:
if (!temp_get_config(&asicdata->switchdevif, &asicdata->temp_config))
res = asicdata->temp_config.sensor_active;
else
goto req_err;
break;
default:
return -EEXIST;
}
return sprintf(buf, "%d\n", res);
req_err:
if (err != ENODEV)
return err;
else
return sprintf(buf, "%d\n", res);
}
static ssize_t store_fan(struct device *dev,
struct device_attribute *devattr,
const char *buf,
size_t count)
{
struct asic_data *asicdata = i2c_get_clientdata(to_i2c_client(dev));
int index = to_sensor_dev_attr_2(devattr)->index;
int nr = to_sensor_dev_attr_2(devattr)->nr;
struct fan_config *fan = NULL;
int err;
fan = &asicdata->fan_config.fan[index];
if (!fan)
return -EEXIST;
switch (nr) {
case fan_power:
fan->pwm_duty_cycle = simple_strtoul(buf, NULL, 10);
if (fan->pwm_duty_cycle < pwm_duty_cycle)
fan->pwm_duty_cycle = pwm_duty_cycle;
err = fan_set_power(&asicdata->switchdevif, fan);
break;
case fan_speed_min:
fan->speed_min[0] = simple_strtoul(buf, NULL, 10);
break;
case fan_speed_max:
fan->speed_max[0] = simple_strtoul(buf, NULL, 10);
break;
case fan_enable:
fan->enable[0] = simple_strtoul(buf, NULL, 10);
err = fan_set_enable(asicdata, index, fan->enable[0]);
if (err)
return -EEXIST;
break;
default:
return -EEXIST;
}
return count;
}
static ssize_t show_fan(struct device *dev,
struct device_attribute *devattr,
char *buf)
{
struct asic_data *asicdata = i2c_get_clientdata(to_i2c_client(dev));
int index = to_sensor_dev_attr_2(devattr)->index;
int nr = to_sensor_dev_attr_2(devattr)->nr;
struct fan_config *fan = NULL;
int err = 0, res = 0;
fan = &asicdata->fan_config.fan[index];
if (!fan)
return -EEXIST;
switch (nr) {
case fan_power:
err = fan_get_power(&asicdata->switchdevif, fan, 0);
if (err)
return -EEXIST;
res = fan->pwm_duty_cycle;
break;
case fan_speed_tacho0:
if (!fan_get_speed(&asicdata->switchdevif, fan, buf, 0))
res = fan->speed[0];
else
goto req_err;
break;
case fan_speed_tacho1:
err = fan_get_speed(&asicdata->switchdevif, fan, buf, 1);
if (err)
return -EEXIST;
res = fan->speed[1];
break;
case fan_speed_tacho2:
err = fan_get_speed(&asicdata->switchdevif, fan, buf, 2);
if (err)
return -EEXIST;
res = fan->speed[2];
break;
case fan_speed_tacho3:
err = fan_get_speed(&asicdata->switchdevif, fan, buf, 3);
if (err)
return -EEXIST;
res = fan->speed[3];
break;
case fan_speed_min:
res = fan->speed_min[0];
break;
case fan_speed_max:
res = fan->speed_max[0];
break;
case fan_enable:
res = fan->enable[0];
break;
case fan_conf:
if (!fan_get_config(&asicdata->switchdevif, &asicdata->fan_config))
res = asicdata->fan_config.tacho_active;
else
goto req_err;
break;
default:
return -EEXIST;
}
return sprintf(buf, "%d\n", res);
req_err:
if (err != ENODEV)
return err;
else
return sprintf(buf, "%d\n", res);
}