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