1
2#include <linux/ctype.h>
3#include <linux/delay.h>
4#include <linux/gpio/consumer.h>
5#include <linux/hwmon.h>
6#include <linux/i2c.h>
7#include <linux/interrupt.h>
8#include <linux/jiffies.h>
9#include <linux/module.h>
10#include <linux/mutex.h>
11#include <linux/of.h>
12#include <linux/phy.h>
13#include <linux/platform_device.h>
14#include <linux/rtnetlink.h>
15#include <linux/slab.h>
16#include <linux/workqueue.h>
17
18#include "mdio-i2c.h"
19#include "sfp.h"
20#include "swphy.h"
21
22enum {
23 GPIO_MODDEF0,
24 GPIO_LOS,
25 GPIO_TX_FAULT,
26 GPIO_TX_DISABLE,
27 GPIO_RATE_SELECT,
28 GPIO_MAX,
29
30 SFP_F_PRESENT = BIT(GPIO_MODDEF0),
31 SFP_F_LOS = BIT(GPIO_LOS),
32 SFP_F_TX_FAULT = BIT(GPIO_TX_FAULT),
33 SFP_F_TX_DISABLE = BIT(GPIO_TX_DISABLE),
34 SFP_F_RATE_SELECT = BIT(GPIO_RATE_SELECT),
35
36 SFP_E_INSERT = 0,
37 SFP_E_REMOVE,
38 SFP_E_DEV_DOWN,
39 SFP_E_DEV_UP,
40 SFP_E_TX_FAULT,
41 SFP_E_TX_CLEAR,
42 SFP_E_LOS_HIGH,
43 SFP_E_LOS_LOW,
44 SFP_E_TIMEOUT,
45
46 SFP_MOD_EMPTY = 0,
47 SFP_MOD_PROBE,
48 SFP_MOD_HPOWER,
49 SFP_MOD_PRESENT,
50 SFP_MOD_ERROR,
51
52 SFP_DEV_DOWN = 0,
53 SFP_DEV_UP,
54
55 SFP_S_DOWN = 0,
56 SFP_S_INIT,
57 SFP_S_WAIT_LOS,
58 SFP_S_LINK_UP,
59 SFP_S_TX_FAULT,
60 SFP_S_REINIT,
61 SFP_S_TX_DISABLE,
62};
63
64static const char * const mod_state_strings[] = {
65 [SFP_MOD_EMPTY] = "empty",
66 [SFP_MOD_PROBE] = "probe",
67 [SFP_MOD_HPOWER] = "hpower",
68 [SFP_MOD_PRESENT] = "present",
69 [SFP_MOD_ERROR] = "error",
70};
71
72static const char *mod_state_to_str(unsigned short mod_state)
73{
74 if (mod_state >= ARRAY_SIZE(mod_state_strings))
75 return "Unknown module state";
76 return mod_state_strings[mod_state];
77}
78
79static const char * const dev_state_strings[] = {
80 [SFP_DEV_DOWN] = "down",
81 [SFP_DEV_UP] = "up",
82};
83
84static const char *dev_state_to_str(unsigned short dev_state)
85{
86 if (dev_state >= ARRAY_SIZE(dev_state_strings))
87 return "Unknown device state";
88 return dev_state_strings[dev_state];
89}
90
91static const char * const event_strings[] = {
92 [SFP_E_INSERT] = "insert",
93 [SFP_E_REMOVE] = "remove",
94 [SFP_E_DEV_DOWN] = "dev_down",
95 [SFP_E_DEV_UP] = "dev_up",
96 [SFP_E_TX_FAULT] = "tx_fault",
97 [SFP_E_TX_CLEAR] = "tx_clear",
98 [SFP_E_LOS_HIGH] = "los_high",
99 [SFP_E_LOS_LOW] = "los_low",
100 [SFP_E_TIMEOUT] = "timeout",
101};
102
103static const char *event_to_str(unsigned short event)
104{
105 if (event >= ARRAY_SIZE(event_strings))
106 return "Unknown event";
107 return event_strings[event];
108}
109
110static const char * const sm_state_strings[] = {
111 [SFP_S_DOWN] = "down",
112 [SFP_S_INIT] = "init",
113 [SFP_S_WAIT_LOS] = "wait_los",
114 [SFP_S_LINK_UP] = "link_up",
115 [SFP_S_TX_FAULT] = "tx_fault",
116 [SFP_S_REINIT] = "reinit",
117 [SFP_S_TX_DISABLE] = "rx_disable",
118};
119
120static const char *sm_state_to_str(unsigned short sm_state)
121{
122 if (sm_state >= ARRAY_SIZE(sm_state_strings))
123 return "Unknown state";
124 return sm_state_strings[sm_state];
125}
126
127static const char *gpio_of_names[] = {
128 "mod-def0",
129 "los",
130 "tx-fault",
131 "tx-disable",
132 "rate-select0",
133};
134
135static const enum gpiod_flags gpio_flags[] = {
136 GPIOD_IN,
137 GPIOD_IN,
138 GPIOD_IN,
139 GPIOD_ASIS,
140 GPIOD_ASIS,
141};
142
143#define T_INIT_JIFFIES msecs_to_jiffies(300)
144#define T_RESET_US 10
145#define T_FAULT_RECOVER msecs_to_jiffies(1000)
146
147
148
149
150
151
152
153
154
155#define T_PROBE_INIT msecs_to_jiffies(300)
156#define T_HPOWER_LEVEL msecs_to_jiffies(300)
157#define T_PROBE_RETRY msecs_to_jiffies(100)
158
159
160
161
162#define SFP_PHY_ADDR 22
163
164
165#define T_PHY_RESET_MS 50
166
167struct sff_data {
168 unsigned int gpios;
169 bool (*module_supported)(const struct sfp_eeprom_id *id);
170};
171
172struct sfp {
173 struct device *dev;
174 struct i2c_adapter *i2c;
175 struct mii_bus *i2c_mii;
176 struct sfp_bus *sfp_bus;
177 struct phy_device *mod_phy;
178 const struct sff_data *type;
179 u32 max_power_mW;
180
181 unsigned int (*get_state)(struct sfp *);
182 void (*set_state)(struct sfp *, unsigned int);
183 int (*read)(struct sfp *, bool, u8, void *, size_t);
184 int (*write)(struct sfp *, bool, u8, void *, size_t);
185
186 struct gpio_desc *gpio[GPIO_MAX];
187
188 bool attached;
189 unsigned int state;
190 struct delayed_work poll;
191 struct delayed_work timeout;
192 struct mutex sm_mutex;
193 unsigned char sm_mod_state;
194 unsigned char sm_dev_state;
195 unsigned short sm_state;
196 unsigned int sm_retries;
197
198 struct sfp_eeprom_id id;
199#if IS_ENABLED(CONFIG_HWMON)
200 struct sfp_diag diag;
201 struct device *hwmon_dev;
202 char *hwmon_name;
203#endif
204
205};
206
207static bool sff_module_supported(const struct sfp_eeprom_id *id)
208{
209 return id->base.phys_id == SFP_PHYS_ID_SFF &&
210 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
211}
212
213static const struct sff_data sff_data = {
214 .gpios = SFP_F_LOS | SFP_F_TX_FAULT | SFP_F_TX_DISABLE,
215 .module_supported = sff_module_supported,
216};
217
218static bool sfp_module_supported(const struct sfp_eeprom_id *id)
219{
220 return id->base.phys_id == SFP_PHYS_ID_SFP &&
221 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
222}
223
224static const struct sff_data sfp_data = {
225 .gpios = SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT |
226 SFP_F_TX_DISABLE | SFP_F_RATE_SELECT,
227 .module_supported = sfp_module_supported,
228};
229
230static const struct of_device_id sfp_of_match[] = {
231 { .compatible = "sff,sff", .data = &sff_data, },
232 { .compatible = "sff,sfp", .data = &sfp_data, },
233 { },
234};
235MODULE_DEVICE_TABLE(of, sfp_of_match);
236
237static unsigned long poll_jiffies;
238
239static unsigned int sfp_gpio_get_state(struct sfp *sfp)
240{
241 unsigned int i, state, v;
242
243 for (i = state = 0; i < GPIO_MAX; i++) {
244 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
245 continue;
246
247 v = gpiod_get_value_cansleep(sfp->gpio[i]);
248 if (v)
249 state |= BIT(i);
250 }
251
252 return state;
253}
254
255static unsigned int sff_gpio_get_state(struct sfp *sfp)
256{
257 return sfp_gpio_get_state(sfp) | SFP_F_PRESENT;
258}
259
260static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state)
261{
262 if (state & SFP_F_PRESENT) {
263
264 if (sfp->gpio[GPIO_TX_DISABLE])
265 gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE],
266 state & SFP_F_TX_DISABLE);
267 if (state & SFP_F_RATE_SELECT)
268 gpiod_direction_output(sfp->gpio[GPIO_RATE_SELECT],
269 state & SFP_F_RATE_SELECT);
270 } else {
271
272 if (sfp->gpio[GPIO_TX_DISABLE])
273 gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]);
274 if (state & SFP_F_RATE_SELECT)
275 gpiod_direction_input(sfp->gpio[GPIO_RATE_SELECT]);
276 }
277}
278
279static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
280 size_t len)
281{
282 struct i2c_msg msgs[2];
283 u8 bus_addr = a2 ? 0x51 : 0x50;
284 size_t this_len;
285 int ret;
286
287 msgs[0].addr = bus_addr;
288 msgs[0].flags = 0;
289 msgs[0].len = 1;
290 msgs[0].buf = &dev_addr;
291 msgs[1].addr = bus_addr;
292 msgs[1].flags = I2C_M_RD;
293 msgs[1].len = len;
294 msgs[1].buf = buf;
295
296 while (len) {
297 this_len = len;
298 if (this_len > 16)
299 this_len = 16;
300
301 msgs[1].len = this_len;
302
303 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
304 if (ret < 0)
305 return ret;
306
307 if (ret != ARRAY_SIZE(msgs))
308 break;
309
310 msgs[1].buf += this_len;
311 dev_addr += this_len;
312 len -= this_len;
313 }
314
315 return msgs[1].buf - (u8 *)buf;
316}
317
318static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
319 size_t len)
320{
321 struct i2c_msg msgs[1];
322 u8 bus_addr = a2 ? 0x51 : 0x50;
323 int ret;
324
325 msgs[0].addr = bus_addr;
326 msgs[0].flags = 0;
327 msgs[0].len = 1 + len;
328 msgs[0].buf = kmalloc(1 + len, GFP_KERNEL);
329 if (!msgs[0].buf)
330 return -ENOMEM;
331
332 msgs[0].buf[0] = dev_addr;
333 memcpy(&msgs[0].buf[1], buf, len);
334
335 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
336
337 kfree(msgs[0].buf);
338
339 if (ret < 0)
340 return ret;
341
342 return ret == ARRAY_SIZE(msgs) ? len : 0;
343}
344
345static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
346{
347 struct mii_bus *i2c_mii;
348 int ret;
349
350 if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
351 return -EINVAL;
352
353 sfp->i2c = i2c;
354 sfp->read = sfp_i2c_read;
355 sfp->write = sfp_i2c_write;
356
357 i2c_mii = mdio_i2c_alloc(sfp->dev, i2c);
358 if (IS_ERR(i2c_mii))
359 return PTR_ERR(i2c_mii);
360
361 i2c_mii->name = "SFP I2C Bus";
362 i2c_mii->phy_mask = ~0;
363
364 ret = mdiobus_register(i2c_mii);
365 if (ret < 0) {
366 mdiobus_free(i2c_mii);
367 return ret;
368 }
369
370 sfp->i2c_mii = i2c_mii;
371
372 return 0;
373}
374
375
376static unsigned int sfp_get_state(struct sfp *sfp)
377{
378 return sfp->get_state(sfp);
379}
380
381static void sfp_set_state(struct sfp *sfp, unsigned int state)
382{
383 sfp->set_state(sfp, state);
384}
385
386static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
387{
388 return sfp->read(sfp, a2, addr, buf, len);
389}
390
391static int sfp_write(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
392{
393 return sfp->write(sfp, a2, addr, buf, len);
394}
395
396static unsigned int sfp_check(void *buf, size_t len)
397{
398 u8 *p, check;
399
400 for (p = buf, check = 0; len; p++, len--)
401 check += *p;
402
403 return check;
404}
405
406
407#if IS_ENABLED(CONFIG_HWMON)
408static umode_t sfp_hwmon_is_visible(const void *data,
409 enum hwmon_sensor_types type,
410 u32 attr, int channel)
411{
412 const struct sfp *sfp = data;
413
414 switch (type) {
415 case hwmon_temp:
416 switch (attr) {
417 case hwmon_temp_min_alarm:
418 case hwmon_temp_max_alarm:
419 case hwmon_temp_lcrit_alarm:
420 case hwmon_temp_crit_alarm:
421 case hwmon_temp_min:
422 case hwmon_temp_max:
423 case hwmon_temp_lcrit:
424 case hwmon_temp_crit:
425 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
426 return 0;
427
428 case hwmon_temp_input:
429 return 0444;
430 default:
431 return 0;
432 }
433 case hwmon_in:
434 switch (attr) {
435 case hwmon_in_min_alarm:
436 case hwmon_in_max_alarm:
437 case hwmon_in_lcrit_alarm:
438 case hwmon_in_crit_alarm:
439 case hwmon_in_min:
440 case hwmon_in_max:
441 case hwmon_in_lcrit:
442 case hwmon_in_crit:
443 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
444 return 0;
445
446 case hwmon_in_input:
447 return 0444;
448 default:
449 return 0;
450 }
451 case hwmon_curr:
452 switch (attr) {
453 case hwmon_curr_min_alarm:
454 case hwmon_curr_max_alarm:
455 case hwmon_curr_lcrit_alarm:
456 case hwmon_curr_crit_alarm:
457 case hwmon_curr_min:
458 case hwmon_curr_max:
459 case hwmon_curr_lcrit:
460 case hwmon_curr_crit:
461 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
462 return 0;
463
464 case hwmon_curr_input:
465 return 0444;
466 default:
467 return 0;
468 }
469 case hwmon_power:
470
471
472
473
474
475
476 if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL &&
477 channel == 1)
478 return 0;
479 switch (attr) {
480 case hwmon_power_min_alarm:
481 case hwmon_power_max_alarm:
482 case hwmon_power_lcrit_alarm:
483 case hwmon_power_crit_alarm:
484 case hwmon_power_min:
485 case hwmon_power_max:
486 case hwmon_power_lcrit:
487 case hwmon_power_crit:
488 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
489 return 0;
490
491 case hwmon_power_input:
492 return 0444;
493 default:
494 return 0;
495 }
496 default:
497 return 0;
498 }
499}
500
501static int sfp_hwmon_read_sensor(struct sfp *sfp, int reg, long *value)
502{
503 __be16 val;
504 int err;
505
506 err = sfp_read(sfp, true, reg, &val, sizeof(val));
507 if (err < 0)
508 return err;
509
510 *value = be16_to_cpu(val);
511
512 return 0;
513}
514
515static void sfp_hwmon_to_rx_power(long *value)
516{
517 *value = DIV_ROUND_CLOSEST(*value, 100);
518}
519
520static void sfp_hwmon_calibrate(struct sfp *sfp, unsigned int slope, int offset,
521 long *value)
522{
523 if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL)
524 *value = DIV_ROUND_CLOSEST(*value * slope, 256) + offset;
525}
526
527static void sfp_hwmon_calibrate_temp(struct sfp *sfp, long *value)
528{
529 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_t_slope),
530 be16_to_cpu(sfp->diag.cal_t_offset), value);
531
532 if (*value >= 0x8000)
533 *value -= 0x10000;
534
535 *value = DIV_ROUND_CLOSEST(*value * 1000, 256);
536}
537
538static void sfp_hwmon_calibrate_vcc(struct sfp *sfp, long *value)
539{
540 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_v_slope),
541 be16_to_cpu(sfp->diag.cal_v_offset), value);
542
543 *value = DIV_ROUND_CLOSEST(*value, 10);
544}
545
546static void sfp_hwmon_calibrate_bias(struct sfp *sfp, long *value)
547{
548 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txi_slope),
549 be16_to_cpu(sfp->diag.cal_txi_offset), value);
550
551 *value = DIV_ROUND_CLOSEST(*value, 500);
552}
553
554static void sfp_hwmon_calibrate_tx_power(struct sfp *sfp, long *value)
555{
556 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txpwr_slope),
557 be16_to_cpu(sfp->diag.cal_txpwr_offset), value);
558
559 *value = DIV_ROUND_CLOSEST(*value, 10);
560}
561
562static int sfp_hwmon_read_temp(struct sfp *sfp, int reg, long *value)
563{
564 int err;
565
566 err = sfp_hwmon_read_sensor(sfp, reg, value);
567 if (err < 0)
568 return err;
569
570 sfp_hwmon_calibrate_temp(sfp, value);
571
572 return 0;
573}
574
575static int sfp_hwmon_read_vcc(struct sfp *sfp, int reg, long *value)
576{
577 int err;
578
579 err = sfp_hwmon_read_sensor(sfp, reg, value);
580 if (err < 0)
581 return err;
582
583 sfp_hwmon_calibrate_vcc(sfp, value);
584
585 return 0;
586}
587
588static int sfp_hwmon_read_bias(struct sfp *sfp, int reg, long *value)
589{
590 int err;
591
592 err = sfp_hwmon_read_sensor(sfp, reg, value);
593 if (err < 0)
594 return err;
595
596 sfp_hwmon_calibrate_bias(sfp, value);
597
598 return 0;
599}
600
601static int sfp_hwmon_read_tx_power(struct sfp *sfp, int reg, long *value)
602{
603 int err;
604
605 err = sfp_hwmon_read_sensor(sfp, reg, value);
606 if (err < 0)
607 return err;
608
609 sfp_hwmon_calibrate_tx_power(sfp, value);
610
611 return 0;
612}
613
614static int sfp_hwmon_read_rx_power(struct sfp *sfp, int reg, long *value)
615{
616 int err;
617
618 err = sfp_hwmon_read_sensor(sfp, reg, value);
619 if (err < 0)
620 return err;
621
622 sfp_hwmon_to_rx_power(value);
623
624 return 0;
625}
626
627static int sfp_hwmon_temp(struct sfp *sfp, u32 attr, long *value)
628{
629 u8 status;
630 int err;
631
632 switch (attr) {
633 case hwmon_temp_input:
634 return sfp_hwmon_read_temp(sfp, SFP_TEMP, value);
635
636 case hwmon_temp_lcrit:
637 *value = be16_to_cpu(sfp->diag.temp_low_alarm);
638 sfp_hwmon_calibrate_temp(sfp, value);
639 return 0;
640
641 case hwmon_temp_min:
642 *value = be16_to_cpu(sfp->diag.temp_low_warn);
643 sfp_hwmon_calibrate_temp(sfp, value);
644 return 0;
645 case hwmon_temp_max:
646 *value = be16_to_cpu(sfp->diag.temp_high_warn);
647 sfp_hwmon_calibrate_temp(sfp, value);
648 return 0;
649
650 case hwmon_temp_crit:
651 *value = be16_to_cpu(sfp->diag.temp_high_alarm);
652 sfp_hwmon_calibrate_temp(sfp, value);
653 return 0;
654
655 case hwmon_temp_lcrit_alarm:
656 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
657 if (err < 0)
658 return err;
659
660 *value = !!(status & SFP_ALARM0_TEMP_LOW);
661 return 0;
662
663 case hwmon_temp_min_alarm:
664 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
665 if (err < 0)
666 return err;
667
668 *value = !!(status & SFP_WARN0_TEMP_LOW);
669 return 0;
670
671 case hwmon_temp_max_alarm:
672 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
673 if (err < 0)
674 return err;
675
676 *value = !!(status & SFP_WARN0_TEMP_HIGH);
677 return 0;
678
679 case hwmon_temp_crit_alarm:
680 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
681 if (err < 0)
682 return err;
683
684 *value = !!(status & SFP_ALARM0_TEMP_HIGH);
685 return 0;
686 default:
687 return -EOPNOTSUPP;
688 }
689
690 return -EOPNOTSUPP;
691}
692
693static int sfp_hwmon_vcc(struct sfp *sfp, u32 attr, long *value)
694{
695 u8 status;
696 int err;
697
698 switch (attr) {
699 case hwmon_in_input:
700 return sfp_hwmon_read_vcc(sfp, SFP_VCC, value);
701
702 case hwmon_in_lcrit:
703 *value = be16_to_cpu(sfp->diag.volt_low_alarm);
704 sfp_hwmon_calibrate_vcc(sfp, value);
705 return 0;
706
707 case hwmon_in_min:
708 *value = be16_to_cpu(sfp->diag.volt_low_warn);
709 sfp_hwmon_calibrate_vcc(sfp, value);
710 return 0;
711
712 case hwmon_in_max:
713 *value = be16_to_cpu(sfp->diag.volt_high_warn);
714 sfp_hwmon_calibrate_vcc(sfp, value);
715 return 0;
716
717 case hwmon_in_crit:
718 *value = be16_to_cpu(sfp->diag.volt_high_alarm);
719 sfp_hwmon_calibrate_vcc(sfp, value);
720 return 0;
721
722 case hwmon_in_lcrit_alarm:
723 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
724 if (err < 0)
725 return err;
726
727 *value = !!(status & SFP_ALARM0_VCC_LOW);
728 return 0;
729
730 case hwmon_in_min_alarm:
731 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
732 if (err < 0)
733 return err;
734
735 *value = !!(status & SFP_WARN0_VCC_LOW);
736 return 0;
737
738 case hwmon_in_max_alarm:
739 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
740 if (err < 0)
741 return err;
742
743 *value = !!(status & SFP_WARN0_VCC_HIGH);
744 return 0;
745
746 case hwmon_in_crit_alarm:
747 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
748 if (err < 0)
749 return err;
750
751 *value = !!(status & SFP_ALARM0_VCC_HIGH);
752 return 0;
753 default:
754 return -EOPNOTSUPP;
755 }
756
757 return -EOPNOTSUPP;
758}
759
760static int sfp_hwmon_bias(struct sfp *sfp, u32 attr, long *value)
761{
762 u8 status;
763 int err;
764
765 switch (attr) {
766 case hwmon_curr_input:
767 return sfp_hwmon_read_bias(sfp, SFP_TX_BIAS, value);
768
769 case hwmon_curr_lcrit:
770 *value = be16_to_cpu(sfp->diag.bias_low_alarm);
771 sfp_hwmon_calibrate_bias(sfp, value);
772 return 0;
773
774 case hwmon_curr_min:
775 *value = be16_to_cpu(sfp->diag.bias_low_warn);
776 sfp_hwmon_calibrate_bias(sfp, value);
777 return 0;
778
779 case hwmon_curr_max:
780 *value = be16_to_cpu(sfp->diag.bias_high_warn);
781 sfp_hwmon_calibrate_bias(sfp, value);
782 return 0;
783
784 case hwmon_curr_crit:
785 *value = be16_to_cpu(sfp->diag.bias_high_alarm);
786 sfp_hwmon_calibrate_bias(sfp, value);
787 return 0;
788
789 case hwmon_curr_lcrit_alarm:
790 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
791 if (err < 0)
792 return err;
793
794 *value = !!(status & SFP_ALARM0_TX_BIAS_LOW);
795 return 0;
796
797 case hwmon_curr_min_alarm:
798 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
799 if (err < 0)
800 return err;
801
802 *value = !!(status & SFP_WARN0_TX_BIAS_LOW);
803 return 0;
804
805 case hwmon_curr_max_alarm:
806 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
807 if (err < 0)
808 return err;
809
810 *value = !!(status & SFP_WARN0_TX_BIAS_HIGH);
811 return 0;
812
813 case hwmon_curr_crit_alarm:
814 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
815 if (err < 0)
816 return err;
817
818 *value = !!(status & SFP_ALARM0_TX_BIAS_HIGH);
819 return 0;
820 default:
821 return -EOPNOTSUPP;
822 }
823
824 return -EOPNOTSUPP;
825}
826
827static int sfp_hwmon_tx_power(struct sfp *sfp, u32 attr, long *value)
828{
829 u8 status;
830 int err;
831
832 switch (attr) {
833 case hwmon_power_input:
834 return sfp_hwmon_read_tx_power(sfp, SFP_TX_POWER, value);
835
836 case hwmon_power_lcrit:
837 *value = be16_to_cpu(sfp->diag.txpwr_low_alarm);
838 sfp_hwmon_calibrate_tx_power(sfp, value);
839 return 0;
840
841 case hwmon_power_min:
842 *value = be16_to_cpu(sfp->diag.txpwr_low_warn);
843 sfp_hwmon_calibrate_tx_power(sfp, value);
844 return 0;
845
846 case hwmon_power_max:
847 *value = be16_to_cpu(sfp->diag.txpwr_high_warn);
848 sfp_hwmon_calibrate_tx_power(sfp, value);
849 return 0;
850
851 case hwmon_power_crit:
852 *value = be16_to_cpu(sfp->diag.txpwr_high_alarm);
853 sfp_hwmon_calibrate_tx_power(sfp, value);
854 return 0;
855
856 case hwmon_power_lcrit_alarm:
857 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
858 if (err < 0)
859 return err;
860
861 *value = !!(status & SFP_ALARM0_TXPWR_LOW);
862 return 0;
863
864 case hwmon_power_min_alarm:
865 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
866 if (err < 0)
867 return err;
868
869 *value = !!(status & SFP_WARN0_TXPWR_LOW);
870 return 0;
871
872 case hwmon_power_max_alarm:
873 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
874 if (err < 0)
875 return err;
876
877 *value = !!(status & SFP_WARN0_TXPWR_HIGH);
878 return 0;
879
880 case hwmon_power_crit_alarm:
881 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
882 if (err < 0)
883 return err;
884
885 *value = !!(status & SFP_ALARM0_TXPWR_HIGH);
886 return 0;
887 default:
888 return -EOPNOTSUPP;
889 }
890
891 return -EOPNOTSUPP;
892}
893
894static int sfp_hwmon_rx_power(struct sfp *sfp, u32 attr, long *value)
895{
896 u8 status;
897 int err;
898
899 switch (attr) {
900 case hwmon_power_input:
901 return sfp_hwmon_read_rx_power(sfp, SFP_RX_POWER, value);
902
903 case hwmon_power_lcrit:
904 *value = be16_to_cpu(sfp->diag.rxpwr_low_alarm);
905 sfp_hwmon_to_rx_power(value);
906 return 0;
907
908 case hwmon_power_min:
909 *value = be16_to_cpu(sfp->diag.rxpwr_low_warn);
910 sfp_hwmon_to_rx_power(value);
911 return 0;
912
913 case hwmon_power_max:
914 *value = be16_to_cpu(sfp->diag.rxpwr_high_warn);
915 sfp_hwmon_to_rx_power(value);
916 return 0;
917
918 case hwmon_power_crit:
919 *value = be16_to_cpu(sfp->diag.rxpwr_high_alarm);
920 sfp_hwmon_to_rx_power(value);
921 return 0;
922
923 case hwmon_power_lcrit_alarm:
924 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
925 if (err < 0)
926 return err;
927
928 *value = !!(status & SFP_ALARM1_RXPWR_LOW);
929 return 0;
930
931 case hwmon_power_min_alarm:
932 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
933 if (err < 0)
934 return err;
935
936 *value = !!(status & SFP_WARN1_RXPWR_LOW);
937 return 0;
938
939 case hwmon_power_max_alarm:
940 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
941 if (err < 0)
942 return err;
943
944 *value = !!(status & SFP_WARN1_RXPWR_HIGH);
945 return 0;
946
947 case hwmon_power_crit_alarm:
948 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
949 if (err < 0)
950 return err;
951
952 *value = !!(status & SFP_ALARM1_RXPWR_HIGH);
953 return 0;
954 default:
955 return -EOPNOTSUPP;
956 }
957
958 return -EOPNOTSUPP;
959}
960
961static int sfp_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
962 u32 attr, int channel, long *value)
963{
964 struct sfp *sfp = dev_get_drvdata(dev);
965
966 switch (type) {
967 case hwmon_temp:
968 return sfp_hwmon_temp(sfp, attr, value);
969 case hwmon_in:
970 return sfp_hwmon_vcc(sfp, attr, value);
971 case hwmon_curr:
972 return sfp_hwmon_bias(sfp, attr, value);
973 case hwmon_power:
974 switch (channel) {
975 case 0:
976 return sfp_hwmon_tx_power(sfp, attr, value);
977 case 1:
978 return sfp_hwmon_rx_power(sfp, attr, value);
979 default:
980 return -EOPNOTSUPP;
981 }
982 default:
983 return -EOPNOTSUPP;
984 }
985}
986
987static const struct hwmon_ops sfp_hwmon_ops = {
988 .is_visible = sfp_hwmon_is_visible,
989 .read = sfp_hwmon_read,
990};
991
992static u32 sfp_hwmon_chip_config[] = {
993 HWMON_C_REGISTER_TZ,
994 0,
995};
996
997static const struct hwmon_channel_info sfp_hwmon_chip = {
998 .type = hwmon_chip,
999 .config = sfp_hwmon_chip_config,
1000};
1001
1002static u32 sfp_hwmon_temp_config[] = {
1003 HWMON_T_INPUT |
1004 HWMON_T_MAX | HWMON_T_MIN |
1005 HWMON_T_MAX_ALARM | HWMON_T_MIN_ALARM |
1006 HWMON_T_CRIT | HWMON_T_LCRIT |
1007 HWMON_T_CRIT_ALARM | HWMON_T_LCRIT_ALARM,
1008 0,
1009};
1010
1011static const struct hwmon_channel_info sfp_hwmon_temp_channel_info = {
1012 .type = hwmon_temp,
1013 .config = sfp_hwmon_temp_config,
1014};
1015
1016static u32 sfp_hwmon_vcc_config[] = {
1017 HWMON_I_INPUT |
1018 HWMON_I_MAX | HWMON_I_MIN |
1019 HWMON_I_MAX_ALARM | HWMON_I_MIN_ALARM |
1020 HWMON_I_CRIT | HWMON_I_LCRIT |
1021 HWMON_I_CRIT_ALARM | HWMON_I_LCRIT_ALARM,
1022 0,
1023};
1024
1025static const struct hwmon_channel_info sfp_hwmon_vcc_channel_info = {
1026 .type = hwmon_in,
1027 .config = sfp_hwmon_vcc_config,
1028};
1029
1030static u32 sfp_hwmon_bias_config[] = {
1031 HWMON_C_INPUT |
1032 HWMON_C_MAX | HWMON_C_MIN |
1033 HWMON_C_MAX_ALARM | HWMON_C_MIN_ALARM |
1034 HWMON_C_CRIT | HWMON_C_LCRIT |
1035 HWMON_C_CRIT_ALARM | HWMON_C_LCRIT_ALARM,
1036 0,
1037};
1038
1039static const struct hwmon_channel_info sfp_hwmon_bias_channel_info = {
1040 .type = hwmon_curr,
1041 .config = sfp_hwmon_bias_config,
1042};
1043
1044static u32 sfp_hwmon_power_config[] = {
1045
1046 HWMON_P_INPUT |
1047 HWMON_P_MAX | HWMON_P_MIN |
1048 HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
1049 HWMON_P_CRIT | HWMON_P_LCRIT |
1050 HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM,
1051
1052 HWMON_P_INPUT |
1053 HWMON_P_MAX | HWMON_P_MIN |
1054 HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
1055 HWMON_P_CRIT | HWMON_P_LCRIT |
1056 HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM,
1057 0,
1058};
1059
1060static const struct hwmon_channel_info sfp_hwmon_power_channel_info = {
1061 .type = hwmon_power,
1062 .config = sfp_hwmon_power_config,
1063};
1064
1065static const struct hwmon_channel_info *sfp_hwmon_info[] = {
1066 &sfp_hwmon_chip,
1067 &sfp_hwmon_vcc_channel_info,
1068 &sfp_hwmon_temp_channel_info,
1069 &sfp_hwmon_bias_channel_info,
1070 &sfp_hwmon_power_channel_info,
1071 NULL,
1072};
1073
1074static const struct hwmon_chip_info sfp_hwmon_chip_info = {
1075 .ops = &sfp_hwmon_ops,
1076 .info = sfp_hwmon_info,
1077};
1078
1079static int sfp_hwmon_insert(struct sfp *sfp)
1080{
1081 int err, i;
1082
1083 if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE)
1084 return 0;
1085
1086 if (!(sfp->id.ext.diagmon & SFP_DIAGMON_DDM))
1087 return 0;
1088
1089 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
1090
1091
1092
1093 return 0;
1094
1095 err = sfp_read(sfp, true, 0, &sfp->diag, sizeof(sfp->diag));
1096 if (err < 0)
1097 return err;
1098
1099 sfp->hwmon_name = kstrdup(dev_name(sfp->dev), GFP_KERNEL);
1100 if (!sfp->hwmon_name)
1101 return -ENODEV;
1102
1103 for (i = 0; sfp->hwmon_name[i]; i++)
1104 if (hwmon_is_bad_char(sfp->hwmon_name[i]))
1105 sfp->hwmon_name[i] = '_';
1106
1107 sfp->hwmon_dev = hwmon_device_register_with_info(sfp->dev,
1108 sfp->hwmon_name, sfp,
1109 &sfp_hwmon_chip_info,
1110 NULL);
1111
1112 return PTR_ERR_OR_ZERO(sfp->hwmon_dev);
1113}
1114
1115static void sfp_hwmon_remove(struct sfp *sfp)
1116{
1117 if (!IS_ERR_OR_NULL(sfp->hwmon_dev)) {
1118 hwmon_device_unregister(sfp->hwmon_dev);
1119 sfp->hwmon_dev = NULL;
1120 kfree(sfp->hwmon_name);
1121 }
1122}
1123#else
1124static int sfp_hwmon_insert(struct sfp *sfp)
1125{
1126 return 0;
1127}
1128
1129static void sfp_hwmon_remove(struct sfp *sfp)
1130{
1131}
1132#endif
1133
1134
1135static void sfp_module_tx_disable(struct sfp *sfp)
1136{
1137 dev_dbg(sfp->dev, "tx disable %u -> %u\n",
1138 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1);
1139 sfp->state |= SFP_F_TX_DISABLE;
1140 sfp_set_state(sfp, sfp->state);
1141}
1142
1143static void sfp_module_tx_enable(struct sfp *sfp)
1144{
1145 dev_dbg(sfp->dev, "tx disable %u -> %u\n",
1146 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0);
1147 sfp->state &= ~SFP_F_TX_DISABLE;
1148 sfp_set_state(sfp, sfp->state);
1149}
1150
1151static void sfp_module_tx_fault_reset(struct sfp *sfp)
1152{
1153 unsigned int state = sfp->state;
1154
1155 if (state & SFP_F_TX_DISABLE)
1156 return;
1157
1158 sfp_set_state(sfp, state | SFP_F_TX_DISABLE);
1159
1160 udelay(T_RESET_US);
1161
1162 sfp_set_state(sfp, state);
1163}
1164
1165
1166static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout)
1167{
1168 if (timeout)
1169 mod_delayed_work(system_power_efficient_wq, &sfp->timeout,
1170 timeout);
1171 else
1172 cancel_delayed_work(&sfp->timeout);
1173}
1174
1175static void sfp_sm_next(struct sfp *sfp, unsigned int state,
1176 unsigned int timeout)
1177{
1178 sfp->sm_state = state;
1179 sfp_sm_set_timer(sfp, timeout);
1180}
1181
1182static void sfp_sm_ins_next(struct sfp *sfp, unsigned int state,
1183 unsigned int timeout)
1184{
1185 sfp->sm_mod_state = state;
1186 sfp_sm_set_timer(sfp, timeout);
1187}
1188
1189static void sfp_sm_phy_detach(struct sfp *sfp)
1190{
1191 phy_stop(sfp->mod_phy);
1192 sfp_remove_phy(sfp->sfp_bus);
1193 phy_device_remove(sfp->mod_phy);
1194 phy_device_free(sfp->mod_phy);
1195 sfp->mod_phy = NULL;
1196}
1197
1198static void sfp_sm_probe_phy(struct sfp *sfp)
1199{
1200 struct phy_device *phy;
1201 int err;
1202
1203 msleep(T_PHY_RESET_MS);
1204
1205 phy = mdiobus_scan(sfp->i2c_mii, SFP_PHY_ADDR);
1206 if (phy == ERR_PTR(-ENODEV)) {
1207 dev_info(sfp->dev, "no PHY detected\n");
1208 return;
1209 }
1210 if (IS_ERR(phy)) {
1211 dev_err(sfp->dev, "mdiobus scan returned %ld\n", PTR_ERR(phy));
1212 return;
1213 }
1214
1215 err = sfp_add_phy(sfp->sfp_bus, phy);
1216 if (err) {
1217 phy_device_remove(phy);
1218 phy_device_free(phy);
1219 dev_err(sfp->dev, "sfp_add_phy failed: %d\n", err);
1220 return;
1221 }
1222
1223 sfp->mod_phy = phy;
1224 phy_start(phy);
1225}
1226
1227static void sfp_sm_link_up(struct sfp *sfp)
1228{
1229 sfp_link_up(sfp->sfp_bus);
1230 sfp_sm_next(sfp, SFP_S_LINK_UP, 0);
1231}
1232
1233static void sfp_sm_link_down(struct sfp *sfp)
1234{
1235 sfp_link_down(sfp->sfp_bus);
1236}
1237
1238static void sfp_sm_link_check_los(struct sfp *sfp)
1239{
1240 unsigned int los = sfp->state & SFP_F_LOS;
1241
1242
1243
1244
1245 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED))
1246 los ^= SFP_F_LOS;
1247 else if (!(sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL)))
1248 los = 0;
1249
1250 if (los)
1251 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
1252 else
1253 sfp_sm_link_up(sfp);
1254}
1255
1256static bool sfp_los_event_active(struct sfp *sfp, unsigned int event)
1257{
1258 return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) &&
1259 event == SFP_E_LOS_LOW) ||
1260 (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) &&
1261 event == SFP_E_LOS_HIGH);
1262}
1263
1264static bool sfp_los_event_inactive(struct sfp *sfp, unsigned int event)
1265{
1266 return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) &&
1267 event == SFP_E_LOS_HIGH) ||
1268 (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) &&
1269 event == SFP_E_LOS_LOW);
1270}
1271
1272static void sfp_sm_fault(struct sfp *sfp, bool warn)
1273{
1274 if (sfp->sm_retries && !--sfp->sm_retries) {
1275 dev_err(sfp->dev,
1276 "module persistently indicates fault, disabling\n");
1277 sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0);
1278 } else {
1279 if (warn)
1280 dev_err(sfp->dev, "module transmit fault indicated\n");
1281
1282 sfp_sm_next(sfp, SFP_S_TX_FAULT, T_FAULT_RECOVER);
1283 }
1284}
1285
1286static void sfp_sm_mod_init(struct sfp *sfp)
1287{
1288 sfp_module_tx_enable(sfp);
1289
1290
1291
1292
1293
1294 sfp_sm_next(sfp, SFP_S_INIT, T_INIT_JIFFIES);
1295 sfp->sm_retries = 5;
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306 if (sfp->id.base.e1000_base_t ||
1307 sfp->id.base.e100_base_lx ||
1308 sfp->id.base.e100_base_fx)
1309 sfp_sm_probe_phy(sfp);
1310}
1311
1312static int sfp_sm_mod_hpower(struct sfp *sfp)
1313{
1314 u32 power;
1315 u8 val;
1316 int err;
1317
1318 power = 1000;
1319 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_POWER_DECL))
1320 power = 1500;
1321 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL))
1322 power = 2000;
1323
1324 if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE &&
1325 (sfp->id.ext.diagmon & (SFP_DIAGMON_DDM | SFP_DIAGMON_ADDRMODE)) !=
1326 SFP_DIAGMON_DDM) {
1327
1328
1329
1330
1331 if (power > sfp->max_power_mW) {
1332 dev_err(sfp->dev,
1333 "Host does not support %u.%uW modules\n",
1334 power / 1000, (power / 100) % 10);
1335 return -EINVAL;
1336 }
1337 return 0;
1338 }
1339
1340 if (power > sfp->max_power_mW) {
1341 dev_warn(sfp->dev,
1342 "Host does not support %u.%uW modules, module left in power mode 1\n",
1343 power / 1000, (power / 100) % 10);
1344 return 0;
1345 }
1346
1347 if (power <= 1000)
1348 return 0;
1349
1350 err = sfp_read(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
1351 if (err != sizeof(val)) {
1352 dev_err(sfp->dev, "Failed to read EEPROM: %d\n", err);
1353 err = -EAGAIN;
1354 goto err;
1355 }
1356
1357 val |= BIT(0);
1358
1359 err = sfp_write(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
1360 if (err != sizeof(val)) {
1361 dev_err(sfp->dev, "Failed to write EEPROM: %d\n", err);
1362 err = -EAGAIN;
1363 goto err;
1364 }
1365
1366 dev_info(sfp->dev, "Module switched to %u.%uW power level\n",
1367 power / 1000, (power / 100) % 10);
1368 return T_HPOWER_LEVEL;
1369
1370err:
1371 return err;
1372}
1373
1374static int sfp_sm_mod_probe(struct sfp *sfp)
1375{
1376
1377 struct sfp_eeprom_id id;
1378 bool cotsworks;
1379 u8 check;
1380 int ret;
1381
1382 ret = sfp_read(sfp, false, 0, &id, sizeof(id));
1383 if (ret < 0) {
1384 dev_err(sfp->dev, "failed to read EEPROM: %d\n", ret);
1385 return -EAGAIN;
1386 }
1387
1388 if (ret != sizeof(id)) {
1389 dev_err(sfp->dev, "EEPROM short read: %d\n", ret);
1390 return -EAGAIN;
1391 }
1392
1393
1394
1395
1396
1397 cotsworks = !memcmp(id.base.vendor_name, "COTSWORKS ", 16);
1398
1399
1400 check = sfp_check(&id.base, sizeof(id.base) - 1);
1401 if (check != id.base.cc_base) {
1402 if (cotsworks) {
1403 dev_warn(sfp->dev,
1404 "EEPROM base structure checksum failure (0x%02x != 0x%02x)\n",
1405 check, id.base.cc_base);
1406 } else {
1407 dev_err(sfp->dev,
1408 "EEPROM base structure checksum failure: 0x%02x != 0x%02x\n",
1409 check, id.base.cc_base);
1410 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
1411 16, 1, &id, sizeof(id), true);
1412 return -EINVAL;
1413 }
1414 }
1415
1416 check = sfp_check(&id.ext, sizeof(id.ext) - 1);
1417 if (check != id.ext.cc_ext) {
1418 if (cotsworks) {
1419 dev_warn(sfp->dev,
1420 "EEPROM extended structure checksum failure (0x%02x != 0x%02x)\n",
1421 check, id.ext.cc_ext);
1422 } else {
1423 dev_err(sfp->dev,
1424 "EEPROM extended structure checksum failure: 0x%02x != 0x%02x\n",
1425 check, id.ext.cc_ext);
1426 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
1427 16, 1, &id, sizeof(id), true);
1428 memset(&id.ext, 0, sizeof(id.ext));
1429 }
1430 }
1431
1432 sfp->id = id;
1433
1434 dev_info(sfp->dev, "module %.*s %.*s rev %.*s sn %.*s dc %.*s\n",
1435 (int)sizeof(id.base.vendor_name), id.base.vendor_name,
1436 (int)sizeof(id.base.vendor_pn), id.base.vendor_pn,
1437 (int)sizeof(id.base.vendor_rev), id.base.vendor_rev,
1438 (int)sizeof(id.ext.vendor_sn), id.ext.vendor_sn,
1439 (int)sizeof(id.ext.datecode), id.ext.datecode);
1440
1441
1442 if (!sfp->type->module_supported(&sfp->id)) {
1443 dev_err(sfp->dev,
1444 "module is not supported - phys id 0x%02x 0x%02x\n",
1445 sfp->id.base.phys_id, sfp->id.base.phys_ext_id);
1446 return -EINVAL;
1447 }
1448
1449
1450 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
1451 dev_warn(sfp->dev,
1452 "module address swap to access page 0xA2 is not supported.\n");
1453
1454 ret = sfp_hwmon_insert(sfp);
1455 if (ret < 0)
1456 return ret;
1457
1458 ret = sfp_module_insert(sfp->sfp_bus, &sfp->id);
1459 if (ret < 0)
1460 return ret;
1461
1462 return sfp_sm_mod_hpower(sfp);
1463}
1464
1465static void sfp_sm_mod_remove(struct sfp *sfp)
1466{
1467 sfp_module_remove(sfp->sfp_bus);
1468
1469 sfp_hwmon_remove(sfp);
1470
1471 if (sfp->mod_phy)
1472 sfp_sm_phy_detach(sfp);
1473
1474 sfp_module_tx_disable(sfp);
1475
1476 memset(&sfp->id, 0, sizeof(sfp->id));
1477
1478 dev_info(sfp->dev, "module removed\n");
1479}
1480
1481static void sfp_sm_event(struct sfp *sfp, unsigned int event)
1482{
1483 mutex_lock(&sfp->sm_mutex);
1484
1485 dev_dbg(sfp->dev, "SM: enter %s:%s:%s event %s\n",
1486 mod_state_to_str(sfp->sm_mod_state),
1487 dev_state_to_str(sfp->sm_dev_state),
1488 sm_state_to_str(sfp->sm_state),
1489 event_to_str(event));
1490
1491
1492
1493
1494 switch (sfp->sm_mod_state) {
1495 default:
1496 if (event == SFP_E_INSERT && sfp->attached) {
1497 sfp_module_tx_disable(sfp);
1498 sfp_sm_ins_next(sfp, SFP_MOD_PROBE, T_PROBE_INIT);
1499 }
1500 break;
1501
1502 case SFP_MOD_PROBE:
1503 if (event == SFP_E_REMOVE) {
1504 sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
1505 } else if (event == SFP_E_TIMEOUT) {
1506 int val = sfp_sm_mod_probe(sfp);
1507
1508 if (val == 0)
1509 sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
1510 else if (val > 0)
1511 sfp_sm_ins_next(sfp, SFP_MOD_HPOWER, val);
1512 else if (val != -EAGAIN)
1513 sfp_sm_ins_next(sfp, SFP_MOD_ERROR, 0);
1514 else
1515 sfp_sm_set_timer(sfp, T_PROBE_RETRY);
1516 }
1517 break;
1518
1519 case SFP_MOD_HPOWER:
1520 if (event == SFP_E_TIMEOUT) {
1521 sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
1522 break;
1523 }
1524
1525 case SFP_MOD_PRESENT:
1526 case SFP_MOD_ERROR:
1527 if (event == SFP_E_REMOVE) {
1528 sfp_sm_mod_remove(sfp);
1529 sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
1530 }
1531 break;
1532 }
1533
1534
1535 switch (sfp->sm_dev_state) {
1536 default:
1537 if (event == SFP_E_DEV_UP)
1538 sfp->sm_dev_state = SFP_DEV_UP;
1539 break;
1540
1541 case SFP_DEV_UP:
1542 if (event == SFP_E_DEV_DOWN) {
1543
1544
1545
1546
1547 if (!sfp->mod_phy)
1548 sfp_module_tx_disable(sfp);
1549 sfp->sm_dev_state = SFP_DEV_DOWN;
1550 }
1551 break;
1552 }
1553
1554
1555 if (sfp->sm_state != SFP_S_DOWN &&
1556 (sfp->sm_mod_state != SFP_MOD_PRESENT ||
1557 sfp->sm_dev_state != SFP_DEV_UP)) {
1558 if (sfp->sm_state == SFP_S_LINK_UP &&
1559 sfp->sm_dev_state == SFP_DEV_UP)
1560 sfp_sm_link_down(sfp);
1561 if (sfp->mod_phy)
1562 sfp_sm_phy_detach(sfp);
1563 sfp_sm_next(sfp, SFP_S_DOWN, 0);
1564 mutex_unlock(&sfp->sm_mutex);
1565 return;
1566 }
1567
1568
1569 switch (sfp->sm_state) {
1570 case SFP_S_DOWN:
1571 if (sfp->sm_mod_state == SFP_MOD_PRESENT &&
1572 sfp->sm_dev_state == SFP_DEV_UP)
1573 sfp_sm_mod_init(sfp);
1574 break;
1575
1576 case SFP_S_INIT:
1577 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT)
1578 sfp_sm_fault(sfp, true);
1579 else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR)
1580 sfp_sm_link_check_los(sfp);
1581 break;
1582
1583 case SFP_S_WAIT_LOS:
1584 if (event == SFP_E_TX_FAULT)
1585 sfp_sm_fault(sfp, true);
1586 else if (sfp_los_event_inactive(sfp, event))
1587 sfp_sm_link_up(sfp);
1588 break;
1589
1590 case SFP_S_LINK_UP:
1591 if (event == SFP_E_TX_FAULT) {
1592 sfp_sm_link_down(sfp);
1593 sfp_sm_fault(sfp, true);
1594 } else if (sfp_los_event_active(sfp, event)) {
1595 sfp_sm_link_down(sfp);
1596 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
1597 }
1598 break;
1599
1600 case SFP_S_TX_FAULT:
1601 if (event == SFP_E_TIMEOUT) {
1602 sfp_module_tx_fault_reset(sfp);
1603 sfp_sm_next(sfp, SFP_S_REINIT, T_INIT_JIFFIES);
1604 }
1605 break;
1606
1607 case SFP_S_REINIT:
1608 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
1609 sfp_sm_fault(sfp, false);
1610 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
1611 dev_info(sfp->dev, "module transmit fault recovered\n");
1612 sfp_sm_link_check_los(sfp);
1613 }
1614 break;
1615
1616 case SFP_S_TX_DISABLE:
1617 break;
1618 }
1619
1620 dev_dbg(sfp->dev, "SM: exit %s:%s:%s\n",
1621 mod_state_to_str(sfp->sm_mod_state),
1622 dev_state_to_str(sfp->sm_dev_state),
1623 sm_state_to_str(sfp->sm_state));
1624
1625 mutex_unlock(&sfp->sm_mutex);
1626}
1627
1628static void sfp_attach(struct sfp *sfp)
1629{
1630 sfp->attached = true;
1631 if (sfp->state & SFP_F_PRESENT)
1632 sfp_sm_event(sfp, SFP_E_INSERT);
1633}
1634
1635static void sfp_detach(struct sfp *sfp)
1636{
1637 sfp->attached = false;
1638 sfp_sm_event(sfp, SFP_E_REMOVE);
1639}
1640
1641static void sfp_start(struct sfp *sfp)
1642{
1643 sfp_sm_event(sfp, SFP_E_DEV_UP);
1644}
1645
1646static void sfp_stop(struct sfp *sfp)
1647{
1648 sfp_sm_event(sfp, SFP_E_DEV_DOWN);
1649}
1650
1651static int sfp_module_info(struct sfp *sfp, struct ethtool_modinfo *modinfo)
1652{
1653
1654
1655 if (sfp->id.ext.sff8472_compliance &&
1656 !(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)) {
1657 modinfo->type = ETH_MODULE_SFF_8472;
1658 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1659 } else {
1660 modinfo->type = ETH_MODULE_SFF_8079;
1661 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
1662 }
1663 return 0;
1664}
1665
1666static int sfp_module_eeprom(struct sfp *sfp, struct ethtool_eeprom *ee,
1667 u8 *data)
1668{
1669 unsigned int first, last, len;
1670 int ret;
1671
1672 if (ee->len == 0)
1673 return -EINVAL;
1674
1675 first = ee->offset;
1676 last = ee->offset + ee->len;
1677 if (first < ETH_MODULE_SFF_8079_LEN) {
1678 len = min_t(unsigned int, last, ETH_MODULE_SFF_8079_LEN);
1679 len -= first;
1680
1681 ret = sfp_read(sfp, false, first, data, len);
1682 if (ret < 0)
1683 return ret;
1684
1685 first += len;
1686 data += len;
1687 }
1688 if (first < ETH_MODULE_SFF_8472_LEN && last > ETH_MODULE_SFF_8079_LEN) {
1689 len = min_t(unsigned int, last, ETH_MODULE_SFF_8472_LEN);
1690 len -= first;
1691 first -= ETH_MODULE_SFF_8079_LEN;
1692
1693 ret = sfp_read(sfp, true, first, data, len);
1694 if (ret < 0)
1695 return ret;
1696 }
1697 return 0;
1698}
1699
1700static const struct sfp_socket_ops sfp_module_ops = {
1701 .attach = sfp_attach,
1702 .detach = sfp_detach,
1703 .start = sfp_start,
1704 .stop = sfp_stop,
1705 .module_info = sfp_module_info,
1706 .module_eeprom = sfp_module_eeprom,
1707};
1708
1709static void sfp_timeout(struct work_struct *work)
1710{
1711 struct sfp *sfp = container_of(work, struct sfp, timeout.work);
1712
1713 rtnl_lock();
1714 sfp_sm_event(sfp, SFP_E_TIMEOUT);
1715 rtnl_unlock();
1716}
1717
1718static void sfp_check_state(struct sfp *sfp)
1719{
1720 unsigned int state, i, changed;
1721
1722 state = sfp_get_state(sfp);
1723 changed = state ^ sfp->state;
1724 changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT;
1725
1726 for (i = 0; i < GPIO_MAX; i++)
1727 if (changed & BIT(i))
1728 dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_of_names[i],
1729 !!(sfp->state & BIT(i)), !!(state & BIT(i)));
1730
1731 state |= sfp->state & (SFP_F_TX_DISABLE | SFP_F_RATE_SELECT);
1732 sfp->state = state;
1733
1734 rtnl_lock();
1735 if (changed & SFP_F_PRESENT)
1736 sfp_sm_event(sfp, state & SFP_F_PRESENT ?
1737 SFP_E_INSERT : SFP_E_REMOVE);
1738
1739 if (changed & SFP_F_TX_FAULT)
1740 sfp_sm_event(sfp, state & SFP_F_TX_FAULT ?
1741 SFP_E_TX_FAULT : SFP_E_TX_CLEAR);
1742
1743 if (changed & SFP_F_LOS)
1744 sfp_sm_event(sfp, state & SFP_F_LOS ?
1745 SFP_E_LOS_HIGH : SFP_E_LOS_LOW);
1746 rtnl_unlock();
1747}
1748
1749static irqreturn_t sfp_irq(int irq, void *data)
1750{
1751 struct sfp *sfp = data;
1752
1753 sfp_check_state(sfp);
1754
1755 return IRQ_HANDLED;
1756}
1757
1758static void sfp_poll(struct work_struct *work)
1759{
1760 struct sfp *sfp = container_of(work, struct sfp, poll.work);
1761
1762 sfp_check_state(sfp);
1763 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
1764}
1765
1766static struct sfp *sfp_alloc(struct device *dev)
1767{
1768 struct sfp *sfp;
1769
1770 sfp = kzalloc(sizeof(*sfp), GFP_KERNEL);
1771 if (!sfp)
1772 return ERR_PTR(-ENOMEM);
1773
1774 sfp->dev = dev;
1775
1776 mutex_init(&sfp->sm_mutex);
1777 INIT_DELAYED_WORK(&sfp->poll, sfp_poll);
1778 INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout);
1779
1780 return sfp;
1781}
1782
1783static void sfp_cleanup(void *data)
1784{
1785 struct sfp *sfp = data;
1786
1787 cancel_delayed_work_sync(&sfp->poll);
1788 cancel_delayed_work_sync(&sfp->timeout);
1789 if (sfp->i2c_mii) {
1790 mdiobus_unregister(sfp->i2c_mii);
1791 mdiobus_free(sfp->i2c_mii);
1792 }
1793 if (sfp->i2c)
1794 i2c_put_adapter(sfp->i2c);
1795 kfree(sfp);
1796}
1797
1798static int sfp_probe(struct platform_device *pdev)
1799{
1800 const struct sff_data *sff;
1801 struct sfp *sfp;
1802 bool poll = false;
1803 int irq, err, i;
1804
1805 sfp = sfp_alloc(&pdev->dev);
1806 if (IS_ERR(sfp))
1807 return PTR_ERR(sfp);
1808
1809 platform_set_drvdata(pdev, sfp);
1810
1811 err = devm_add_action(sfp->dev, sfp_cleanup, sfp);
1812 if (err < 0)
1813 return err;
1814
1815 sff = sfp->type = &sfp_data;
1816
1817 if (pdev->dev.of_node) {
1818 struct device_node *node = pdev->dev.of_node;
1819 const struct of_device_id *id;
1820 struct i2c_adapter *i2c;
1821 struct device_node *np;
1822
1823 id = of_match_node(sfp_of_match, node);
1824 if (WARN_ON(!id))
1825 return -EINVAL;
1826
1827 sff = sfp->type = id->data;
1828
1829 np = of_parse_phandle(node, "i2c-bus", 0);
1830 if (!np) {
1831 dev_err(sfp->dev, "missing 'i2c-bus' property\n");
1832 return -ENODEV;
1833 }
1834
1835 i2c = of_find_i2c_adapter_by_node(np);
1836 of_node_put(np);
1837 if (!i2c)
1838 return -EPROBE_DEFER;
1839
1840 err = sfp_i2c_configure(sfp, i2c);
1841 if (err < 0) {
1842 i2c_put_adapter(i2c);
1843 return err;
1844 }
1845 }
1846
1847 for (i = 0; i < GPIO_MAX; i++)
1848 if (sff->gpios & BIT(i)) {
1849 sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev,
1850 gpio_of_names[i], gpio_flags[i]);
1851 if (IS_ERR(sfp->gpio[i]))
1852 return PTR_ERR(sfp->gpio[i]);
1853 }
1854
1855 sfp->get_state = sfp_gpio_get_state;
1856 sfp->set_state = sfp_gpio_set_state;
1857
1858
1859 if (!(sfp->gpio[GPIO_MODDEF0]))
1860 sfp->get_state = sff_gpio_get_state;
1861
1862 device_property_read_u32(&pdev->dev, "maximum-power-milliwatt",
1863 &sfp->max_power_mW);
1864 if (!sfp->max_power_mW)
1865 sfp->max_power_mW = 1000;
1866
1867 dev_info(sfp->dev, "Host maximum power %u.%uW\n",
1868 sfp->max_power_mW / 1000, (sfp->max_power_mW / 100) % 10);
1869
1870
1871
1872
1873 sfp->state = sfp_get_state(sfp) | SFP_F_TX_DISABLE;
1874
1875 if (sfp->gpio[GPIO_RATE_SELECT] &&
1876 gpiod_get_value_cansleep(sfp->gpio[GPIO_RATE_SELECT]))
1877 sfp->state |= SFP_F_RATE_SELECT;
1878 sfp_set_state(sfp, sfp->state);
1879 sfp_module_tx_disable(sfp);
1880
1881 for (i = 0; i < GPIO_MAX; i++) {
1882 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
1883 continue;
1884
1885 irq = gpiod_to_irq(sfp->gpio[i]);
1886 if (!irq) {
1887 poll = true;
1888 continue;
1889 }
1890
1891 err = devm_request_threaded_irq(sfp->dev, irq, NULL, sfp_irq,
1892 IRQF_ONESHOT |
1893 IRQF_TRIGGER_RISING |
1894 IRQF_TRIGGER_FALLING,
1895 dev_name(sfp->dev), sfp);
1896 if (err)
1897 poll = true;
1898 }
1899
1900 if (poll)
1901 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
1902
1903
1904
1905
1906
1907
1908 if (!sfp->gpio[GPIO_TX_DISABLE])
1909 dev_warn(sfp->dev,
1910 "No tx_disable pin: SFP modules will always be emitting.\n");
1911
1912 sfp->sfp_bus = sfp_register_socket(sfp->dev, sfp, &sfp_module_ops);
1913 if (!sfp->sfp_bus)
1914 return -ENOMEM;
1915
1916 return 0;
1917}
1918
1919static int sfp_remove(struct platform_device *pdev)
1920{
1921 struct sfp *sfp = platform_get_drvdata(pdev);
1922
1923 sfp_unregister_socket(sfp->sfp_bus);
1924
1925 return 0;
1926}
1927
1928static struct platform_driver sfp_driver = {
1929 .probe = sfp_probe,
1930 .remove = sfp_remove,
1931 .driver = {
1932 .name = "sfp",
1933 .of_match_table = sfp_of_match,
1934 },
1935};
1936
1937static int sfp_init(void)
1938{
1939 poll_jiffies = msecs_to_jiffies(100);
1940
1941 return platform_driver_register(&sfp_driver);
1942}
1943module_init(sfp_init);
1944
1945static void sfp_exit(void)
1946{
1947 platform_driver_unregister(&sfp_driver);
1948}
1949module_exit(sfp_exit);
1950
1951MODULE_ALIAS("platform:sfp");
1952MODULE_AUTHOR("Russell King");
1953MODULE_LICENSE("GPL v2");
1954