blob: 316ff719857bbb439ded9db95983dfad85c54021 [file] [log] [blame]
Andrew Lunna2443fd2019-01-21 19:05:50 +01001// SPDX-License-Identifier: GPL-2.0
Ruslan Babayev7ce236fa2019-05-28 16:02:33 -07002#include <linux/acpi.h>
Andrew Lunn13230612018-07-17 21:48:13 +02003#include <linux/ctype.h>
Russell King73970052017-07-25 15:03:39 +01004#include <linux/delay.h>
Florian Fainelli54a2fc62017-10-30 21:42:58 -07005#include <linux/gpio/consumer.h>
Andrew Lunn13230612018-07-17 21:48:13 +02006#include <linux/hwmon.h>
Russell King73970052017-07-25 15:03:39 +01007#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_DOWN,
40 SFP_E_DEV_UP,
41 SFP_E_TX_FAULT,
42 SFP_E_TX_CLEAR,
43 SFP_E_LOS_HIGH,
44 SFP_E_LOS_LOW,
45 SFP_E_TIMEOUT,
46
47 SFP_MOD_EMPTY = 0,
48 SFP_MOD_PROBE,
Jon Nettleton3bb35262018-02-27 15:53:12 +000049 SFP_MOD_HPOWER,
Russell King73970052017-07-25 15:03:39 +010050 SFP_MOD_PRESENT,
51 SFP_MOD_ERROR,
52
53 SFP_DEV_DOWN = 0,
54 SFP_DEV_UP,
55
56 SFP_S_DOWN = 0,
Russell Kingeefa6f12019-11-10 14:06:59 +000057 SFP_S_WAIT,
Russell King73970052017-07-25 15:03:39 +010058 SFP_S_INIT,
59 SFP_S_WAIT_LOS,
60 SFP_S_LINK_UP,
61 SFP_S_TX_FAULT,
62 SFP_S_REINIT,
63 SFP_S_TX_DISABLE,
64};
65
Andrew Lunn4005a7c2018-08-08 20:54:12 +020066static const char * const mod_state_strings[] = {
67 [SFP_MOD_EMPTY] = "empty",
68 [SFP_MOD_PROBE] = "probe",
69 [SFP_MOD_HPOWER] = "hpower",
70 [SFP_MOD_PRESENT] = "present",
71 [SFP_MOD_ERROR] = "error",
72};
73
74static const char *mod_state_to_str(unsigned short mod_state)
75{
76 if (mod_state >= ARRAY_SIZE(mod_state_strings))
77 return "Unknown module state";
78 return mod_state_strings[mod_state];
79}
80
81static const char * const dev_state_strings[] = {
82 [SFP_DEV_DOWN] = "down",
83 [SFP_DEV_UP] = "up",
84};
85
86static const char *dev_state_to_str(unsigned short dev_state)
87{
88 if (dev_state >= ARRAY_SIZE(dev_state_strings))
89 return "Unknown device state";
90 return dev_state_strings[dev_state];
91}
92
93static const char * const event_strings[] = {
94 [SFP_E_INSERT] = "insert",
95 [SFP_E_REMOVE] = "remove",
96 [SFP_E_DEV_DOWN] = "dev_down",
97 [SFP_E_DEV_UP] = "dev_up",
98 [SFP_E_TX_FAULT] = "tx_fault",
99 [SFP_E_TX_CLEAR] = "tx_clear",
100 [SFP_E_LOS_HIGH] = "los_high",
101 [SFP_E_LOS_LOW] = "los_low",
102 [SFP_E_TIMEOUT] = "timeout",
103};
104
105static const char *event_to_str(unsigned short event)
106{
107 if (event >= ARRAY_SIZE(event_strings))
108 return "Unknown event";
109 return event_strings[event];
110}
111
112static const char * const sm_state_strings[] = {
113 [SFP_S_DOWN] = "down",
Russell Kingeefa6f12019-11-10 14:06:59 +0000114 [SFP_S_WAIT] = "wait",
Andrew Lunn4005a7c2018-08-08 20:54:12 +0200115 [SFP_S_INIT] = "init",
116 [SFP_S_WAIT_LOS] = "wait_los",
117 [SFP_S_LINK_UP] = "link_up",
118 [SFP_S_TX_FAULT] = "tx_fault",
119 [SFP_S_REINIT] = "reinit",
120 [SFP_S_TX_DISABLE] = "rx_disable",
121};
122
123static const char *sm_state_to_str(unsigned short sm_state)
124{
125 if (sm_state >= ARRAY_SIZE(sm_state_strings))
126 return "Unknown state";
127 return sm_state_strings[sm_state];
128}
129
Russell King73970052017-07-25 15:03:39 +0100130static const char *gpio_of_names[] = {
Baruch Siach25ee0792017-09-07 12:25:50 +0300131 "mod-def0",
Russell King73970052017-07-25 15:03:39 +0100132 "los",
133 "tx-fault",
134 "tx-disable",
Baruch Siach25ee0792017-09-07 12:25:50 +0300135 "rate-select0",
Russell King73970052017-07-25 15:03:39 +0100136};
137
138static const enum gpiod_flags gpio_flags[] = {
139 GPIOD_IN,
140 GPIOD_IN,
141 GPIOD_IN,
142 GPIOD_ASIS,
143 GPIOD_ASIS,
144};
145
Russell Kingeefa6f12019-11-10 14:06:59 +0000146#define T_WAIT msecs_to_jiffies(50)
Russell King73970052017-07-25 15:03:39 +0100147#define T_INIT_JIFFIES msecs_to_jiffies(300)
148#define T_RESET_US 10
149#define T_FAULT_RECOVER msecs_to_jiffies(1000)
150
151/* SFP module presence detection is poor: the three MOD DEF signals are
152 * the same length on the PCB, which means it's possible for MOD DEF 0 to
153 * connect before the I2C bus on MOD DEF 1/2.
154 *
Russell Kingd9009542019-11-10 14:06:33 +0000155 * The SFF-8472 specifies t_serial ("Time from power on until module is
156 * ready for data transmission over the two wire serial bus.") as 300ms.
Russell King73970052017-07-25 15:03:39 +0100157 */
Russell Kingd9009542019-11-10 14:06:33 +0000158#define T_SERIAL msecs_to_jiffies(300)
Jon Nettleton3bb35262018-02-27 15:53:12 +0000159#define T_HPOWER_LEVEL msecs_to_jiffies(300)
Russell King73970052017-07-25 15:03:39 +0100160#define T_PROBE_RETRY msecs_to_jiffies(100)
161
Florian Fainelli516b29e2017-10-30 21:42:57 -0700162/* SFP modules appear to always have their PHY configured for bus address
Russell King73970052017-07-25 15:03:39 +0100163 * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
164 */
165#define SFP_PHY_ADDR 22
166
Russell King259c8612017-12-14 10:27:47 +0000167struct sff_data {
168 unsigned int gpios;
169 bool (*module_supported)(const struct sfp_eeprom_id *id);
170};
171
Russell King73970052017-07-25 15:03:39 +0100172struct 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;
Russell King259c8612017-12-14 10:27:47 +0000178 const struct sff_data *type;
Jon Nettleton3bb35262018-02-27 15:53:12 +0000179 u32 max_power_mW;
Russell King73970052017-07-25 15:03:39 +0100180
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);
Jon Nettleton3bb35262018-02-27 15:53:12 +0000184 int (*write)(struct sfp *, bool, u8, void *, size_t);
Russell King73970052017-07-25 15:03:39 +0100185
186 struct gpio_desc *gpio[GPIO_MAX];
Robert Hancock257c2552019-06-07 10:42:35 -0600187 int gpio_irq[GPIO_MAX];
Russell King73970052017-07-25 15:03:39 +0100188
Russell Kingb5bfc212019-02-06 10:52:30 +0000189 bool attached;
Robert Hancock2158e852019-06-07 10:42:36 -0600190 struct mutex st_mutex; /* Protects state */
Russell King73970052017-07-25 15:03:39 +0100191 unsigned int state;
192 struct delayed_work poll;
193 struct delayed_work timeout;
Robert Hancock2158e852019-06-07 10:42:36 -0600194 struct mutex sm_mutex; /* Protects state machine */
Russell King73970052017-07-25 15:03:39 +0100195 unsigned char sm_mod_state;
196 unsigned char sm_dev_state;
197 unsigned short sm_state;
198 unsigned int sm_retries;
199
200 struct sfp_eeprom_id id;
Russell Kinged32abb2019-11-10 14:06:39 +0000201 unsigned int module_power_mW;
202
Andrew Lunn13230612018-07-17 21:48:13 +0200203#if IS_ENABLED(CONFIG_HWMON)
204 struct sfp_diag diag;
205 struct device *hwmon_dev;
206 char *hwmon_name;
207#endif
208
Russell King73970052017-07-25 15:03:39 +0100209};
210
Russell King259c8612017-12-14 10:27:47 +0000211static bool sff_module_supported(const struct sfp_eeprom_id *id)
212{
213 return id->base.phys_id == SFP_PHYS_ID_SFF &&
214 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
215}
216
217static const struct sff_data sff_data = {
218 .gpios = SFP_F_LOS | SFP_F_TX_FAULT | SFP_F_TX_DISABLE,
219 .module_supported = sff_module_supported,
220};
221
222static bool sfp_module_supported(const struct sfp_eeprom_id *id)
223{
224 return id->base.phys_id == SFP_PHYS_ID_SFP &&
225 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
226}
227
228static const struct sff_data sfp_data = {
229 .gpios = SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT |
230 SFP_F_TX_DISABLE | SFP_F_RATE_SELECT,
231 .module_supported = sfp_module_supported,
232};
233
234static const struct of_device_id sfp_of_match[] = {
235 { .compatible = "sff,sff", .data = &sff_data, },
236 { .compatible = "sff,sfp", .data = &sfp_data, },
237 { },
238};
239MODULE_DEVICE_TABLE(of, sfp_of_match);
240
Russell King73970052017-07-25 15:03:39 +0100241static unsigned long poll_jiffies;
242
243static unsigned int sfp_gpio_get_state(struct sfp *sfp)
244{
245 unsigned int i, state, v;
246
247 for (i = state = 0; i < GPIO_MAX; i++) {
248 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
249 continue;
250
251 v = gpiod_get_value_cansleep(sfp->gpio[i]);
252 if (v)
253 state |= BIT(i);
254 }
255
256 return state;
257}
258
Russell King259c8612017-12-14 10:27:47 +0000259static unsigned int sff_gpio_get_state(struct sfp *sfp)
260{
261 return sfp_gpio_get_state(sfp) | SFP_F_PRESENT;
262}
263
Russell King73970052017-07-25 15:03:39 +0100264static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state)
265{
266 if (state & SFP_F_PRESENT) {
267 /* If the module is present, drive the signals */
268 if (sfp->gpio[GPIO_TX_DISABLE])
269 gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE],
Florian Fainelli516b29e2017-10-30 21:42:57 -0700270 state & SFP_F_TX_DISABLE);
Russell King73970052017-07-25 15:03:39 +0100271 if (state & SFP_F_RATE_SELECT)
272 gpiod_direction_output(sfp->gpio[GPIO_RATE_SELECT],
Florian Fainelli516b29e2017-10-30 21:42:57 -0700273 state & SFP_F_RATE_SELECT);
Russell King73970052017-07-25 15:03:39 +0100274 } else {
275 /* Otherwise, let them float to the pull-ups */
276 if (sfp->gpio[GPIO_TX_DISABLE])
277 gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]);
278 if (state & SFP_F_RATE_SELECT)
279 gpiod_direction_input(sfp->gpio[GPIO_RATE_SELECT]);
280 }
281}
282
Jon Nettleton3bb35262018-02-27 15:53:12 +0000283static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
284 size_t len)
Russell King73970052017-07-25 15:03:39 +0100285{
286 struct i2c_msg msgs[2];
Jon Nettleton3bb35262018-02-27 15:53:12 +0000287 u8 bus_addr = a2 ? 0x51 : 0x50;
Russell King28e74a72019-06-02 15:13:00 +0100288 size_t this_len;
Russell King73970052017-07-25 15:03:39 +0100289 int ret;
290
291 msgs[0].addr = bus_addr;
292 msgs[0].flags = 0;
293 msgs[0].len = 1;
294 msgs[0].buf = &dev_addr;
295 msgs[1].addr = bus_addr;
296 msgs[1].flags = I2C_M_RD;
297 msgs[1].len = len;
298 msgs[1].buf = buf;
299
Russell King28e74a72019-06-02 15:13:00 +0100300 while (len) {
301 this_len = len;
302 if (this_len > 16)
303 this_len = 16;
Russell King73970052017-07-25 15:03:39 +0100304
Russell King28e74a72019-06-02 15:13:00 +0100305 msgs[1].len = this_len;
306
307 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
308 if (ret < 0)
309 return ret;
310
311 if (ret != ARRAY_SIZE(msgs))
312 break;
313
314 msgs[1].buf += this_len;
315 dev_addr += this_len;
316 len -= this_len;
317 }
318
319 return msgs[1].buf - (u8 *)buf;
Russell King73970052017-07-25 15:03:39 +0100320}
321
Jon Nettleton3bb35262018-02-27 15:53:12 +0000322static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
323 size_t len)
Russell King73970052017-07-25 15:03:39 +0100324{
Jon Nettleton3bb35262018-02-27 15:53:12 +0000325 struct i2c_msg msgs[1];
326 u8 bus_addr = a2 ? 0x51 : 0x50;
327 int ret;
328
329 msgs[0].addr = bus_addr;
330 msgs[0].flags = 0;
331 msgs[0].len = 1 + len;
332 msgs[0].buf = kmalloc(1 + len, GFP_KERNEL);
333 if (!msgs[0].buf)
334 return -ENOMEM;
335
336 msgs[0].buf[0] = dev_addr;
337 memcpy(&msgs[0].buf[1], buf, len);
338
339 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
340
341 kfree(msgs[0].buf);
342
343 if (ret < 0)
344 return ret;
345
346 return ret == ARRAY_SIZE(msgs) ? len : 0;
Russell King73970052017-07-25 15:03:39 +0100347}
348
349static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
350{
351 struct mii_bus *i2c_mii;
352 int ret;
353
354 if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
355 return -EINVAL;
356
357 sfp->i2c = i2c;
358 sfp->read = sfp_i2c_read;
Jon Nettleton3bb35262018-02-27 15:53:12 +0000359 sfp->write = sfp_i2c_write;
Russell King73970052017-07-25 15:03:39 +0100360
361 i2c_mii = mdio_i2c_alloc(sfp->dev, i2c);
362 if (IS_ERR(i2c_mii))
363 return PTR_ERR(i2c_mii);
364
365 i2c_mii->name = "SFP I2C Bus";
366 i2c_mii->phy_mask = ~0;
367
368 ret = mdiobus_register(i2c_mii);
369 if (ret < 0) {
370 mdiobus_free(i2c_mii);
371 return ret;
372 }
373
374 sfp->i2c_mii = i2c_mii;
375
376 return 0;
377}
378
Russell King73970052017-07-25 15:03:39 +0100379/* Interface */
380static unsigned int sfp_get_state(struct sfp *sfp)
381{
382 return sfp->get_state(sfp);
383}
384
385static void sfp_set_state(struct sfp *sfp, unsigned int state)
386{
387 sfp->set_state(sfp, state);
388}
389
390static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
391{
392 return sfp->read(sfp, a2, addr, buf, len);
393}
394
Jon Nettleton3bb35262018-02-27 15:53:12 +0000395static int sfp_write(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
396{
397 return sfp->write(sfp, a2, addr, buf, len);
398}
399
Russell King73970052017-07-25 15:03:39 +0100400static unsigned int sfp_check(void *buf, size_t len)
401{
402 u8 *p, check;
403
404 for (p = buf, check = 0; len; p++, len--)
405 check += *p;
406
407 return check;
408}
409
Andrew Lunn13230612018-07-17 21:48:13 +0200410/* hwmon */
411#if IS_ENABLED(CONFIG_HWMON)
412static umode_t sfp_hwmon_is_visible(const void *data,
413 enum hwmon_sensor_types type,
414 u32 attr, int channel)
415{
416 const struct sfp *sfp = data;
417
418 switch (type) {
419 case hwmon_temp:
420 switch (attr) {
Andrew Lunn13230612018-07-17 21:48:13 +0200421 case hwmon_temp_min_alarm:
422 case hwmon_temp_max_alarm:
423 case hwmon_temp_lcrit_alarm:
424 case hwmon_temp_crit_alarm:
425 case hwmon_temp_min:
426 case hwmon_temp_max:
427 case hwmon_temp_lcrit:
428 case hwmon_temp_crit:
Andrew Lunna33710b2018-09-04 04:23:56 +0200429 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
430 return 0;
431 /* fall through */
432 case hwmon_temp_input:
Andrew Lunnc1236972019-08-25 01:04:17 +0200433 case hwmon_temp_label:
Andrew Lunn13230612018-07-17 21:48:13 +0200434 return 0444;
435 default:
436 return 0;
437 }
438 case hwmon_in:
439 switch (attr) {
Andrew Lunn13230612018-07-17 21:48:13 +0200440 case hwmon_in_min_alarm:
441 case hwmon_in_max_alarm:
442 case hwmon_in_lcrit_alarm:
443 case hwmon_in_crit_alarm:
444 case hwmon_in_min:
445 case hwmon_in_max:
446 case hwmon_in_lcrit:
447 case hwmon_in_crit:
Andrew Lunna33710b2018-09-04 04:23:56 +0200448 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
449 return 0;
450 /* fall through */
451 case hwmon_in_input:
Andrew Lunnc1236972019-08-25 01:04:17 +0200452 case hwmon_in_label:
Andrew Lunn13230612018-07-17 21:48:13 +0200453 return 0444;
454 default:
455 return 0;
456 }
457 case hwmon_curr:
458 switch (attr) {
Andrew Lunn13230612018-07-17 21:48:13 +0200459 case hwmon_curr_min_alarm:
460 case hwmon_curr_max_alarm:
461 case hwmon_curr_lcrit_alarm:
462 case hwmon_curr_crit_alarm:
463 case hwmon_curr_min:
464 case hwmon_curr_max:
465 case hwmon_curr_lcrit:
466 case hwmon_curr_crit:
Andrew Lunna33710b2018-09-04 04:23:56 +0200467 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
468 return 0;
469 /* fall through */
470 case hwmon_curr_input:
Andrew Lunnc1236972019-08-25 01:04:17 +0200471 case hwmon_curr_label:
Andrew Lunn13230612018-07-17 21:48:13 +0200472 return 0444;
473 default:
474 return 0;
475 }
476 case hwmon_power:
477 /* External calibration of receive power requires
478 * floating point arithmetic. Doing that in the kernel
479 * is not easy, so just skip it. If the module does
480 * not require external calibration, we can however
481 * show receiver power, since FP is then not needed.
482 */
483 if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL &&
484 channel == 1)
485 return 0;
486 switch (attr) {
Andrew Lunn13230612018-07-17 21:48:13 +0200487 case hwmon_power_min_alarm:
488 case hwmon_power_max_alarm:
489 case hwmon_power_lcrit_alarm:
490 case hwmon_power_crit_alarm:
491 case hwmon_power_min:
492 case hwmon_power_max:
493 case hwmon_power_lcrit:
494 case hwmon_power_crit:
Andrew Lunna33710b2018-09-04 04:23:56 +0200495 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
496 return 0;
497 /* fall through */
498 case hwmon_power_input:
Andrew Lunnc1236972019-08-25 01:04:17 +0200499 case hwmon_power_label:
Andrew Lunn13230612018-07-17 21:48:13 +0200500 return 0444;
501 default:
502 return 0;
503 }
504 default:
505 return 0;
506 }
507}
508
509static int sfp_hwmon_read_sensor(struct sfp *sfp, int reg, long *value)
510{
511 __be16 val;
512 int err;
513
514 err = sfp_read(sfp, true, reg, &val, sizeof(val));
515 if (err < 0)
516 return err;
517
518 *value = be16_to_cpu(val);
519
520 return 0;
521}
522
523static void sfp_hwmon_to_rx_power(long *value)
524{
Andrew Lunn0cea0e12019-07-21 18:50:08 +0200525 *value = DIV_ROUND_CLOSEST(*value, 10);
Andrew Lunn13230612018-07-17 21:48:13 +0200526}
527
528static void sfp_hwmon_calibrate(struct sfp *sfp, unsigned int slope, int offset,
529 long *value)
530{
531 if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL)
532 *value = DIV_ROUND_CLOSEST(*value * slope, 256) + offset;
533}
534
535static void sfp_hwmon_calibrate_temp(struct sfp *sfp, long *value)
536{
537 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_t_slope),
538 be16_to_cpu(sfp->diag.cal_t_offset), value);
539
540 if (*value >= 0x8000)
541 *value -= 0x10000;
542
543 *value = DIV_ROUND_CLOSEST(*value * 1000, 256);
544}
545
546static void sfp_hwmon_calibrate_vcc(struct sfp *sfp, long *value)
547{
548 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_v_slope),
549 be16_to_cpu(sfp->diag.cal_v_offset), value);
550
551 *value = DIV_ROUND_CLOSEST(*value, 10);
552}
553
554static void sfp_hwmon_calibrate_bias(struct sfp *sfp, long *value)
555{
556 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txi_slope),
557 be16_to_cpu(sfp->diag.cal_txi_offset), value);
558
559 *value = DIV_ROUND_CLOSEST(*value, 500);
560}
561
562static void sfp_hwmon_calibrate_tx_power(struct sfp *sfp, long *value)
563{
564 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txpwr_slope),
565 be16_to_cpu(sfp->diag.cal_txpwr_offset), value);
566
567 *value = DIV_ROUND_CLOSEST(*value, 10);
568}
569
570static int sfp_hwmon_read_temp(struct sfp *sfp, int reg, long *value)
571{
572 int err;
573
574 err = sfp_hwmon_read_sensor(sfp, reg, value);
575 if (err < 0)
576 return err;
577
578 sfp_hwmon_calibrate_temp(sfp, value);
579
580 return 0;
581}
582
583static int sfp_hwmon_read_vcc(struct sfp *sfp, int reg, long *value)
584{
585 int err;
586
587 err = sfp_hwmon_read_sensor(sfp, reg, value);
588 if (err < 0)
589 return err;
590
591 sfp_hwmon_calibrate_vcc(sfp, value);
592
593 return 0;
594}
595
596static int sfp_hwmon_read_bias(struct sfp *sfp, int reg, long *value)
597{
598 int err;
599
600 err = sfp_hwmon_read_sensor(sfp, reg, value);
601 if (err < 0)
602 return err;
603
604 sfp_hwmon_calibrate_bias(sfp, value);
605
606 return 0;
607}
608
609static int sfp_hwmon_read_tx_power(struct sfp *sfp, int reg, long *value)
610{
611 int err;
612
613 err = sfp_hwmon_read_sensor(sfp, reg, value);
614 if (err < 0)
615 return err;
616
617 sfp_hwmon_calibrate_tx_power(sfp, value);
618
619 return 0;
620}
621
622static int sfp_hwmon_read_rx_power(struct sfp *sfp, int reg, long *value)
623{
624 int err;
625
626 err = sfp_hwmon_read_sensor(sfp, reg, value);
627 if (err < 0)
628 return err;
629
630 sfp_hwmon_to_rx_power(value);
631
632 return 0;
633}
634
635static int sfp_hwmon_temp(struct sfp *sfp, u32 attr, long *value)
636{
637 u8 status;
638 int err;
639
640 switch (attr) {
641 case hwmon_temp_input:
642 return sfp_hwmon_read_temp(sfp, SFP_TEMP, value);
643
644 case hwmon_temp_lcrit:
645 *value = be16_to_cpu(sfp->diag.temp_low_alarm);
646 sfp_hwmon_calibrate_temp(sfp, value);
647 return 0;
648
649 case hwmon_temp_min:
650 *value = be16_to_cpu(sfp->diag.temp_low_warn);
651 sfp_hwmon_calibrate_temp(sfp, value);
652 return 0;
653 case hwmon_temp_max:
654 *value = be16_to_cpu(sfp->diag.temp_high_warn);
655 sfp_hwmon_calibrate_temp(sfp, value);
656 return 0;
657
658 case hwmon_temp_crit:
659 *value = be16_to_cpu(sfp->diag.temp_high_alarm);
660 sfp_hwmon_calibrate_temp(sfp, value);
661 return 0;
662
663 case hwmon_temp_lcrit_alarm:
664 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
665 if (err < 0)
666 return err;
667
668 *value = !!(status & SFP_ALARM0_TEMP_LOW);
669 return 0;
670
671 case hwmon_temp_min_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_LOW);
677 return 0;
678
679 case hwmon_temp_max_alarm:
680 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
681 if (err < 0)
682 return err;
683
684 *value = !!(status & SFP_WARN0_TEMP_HIGH);
685 return 0;
686
687 case hwmon_temp_crit_alarm:
688 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
689 if (err < 0)
690 return err;
691
692 *value = !!(status & SFP_ALARM0_TEMP_HIGH);
693 return 0;
694 default:
695 return -EOPNOTSUPP;
696 }
697
698 return -EOPNOTSUPP;
699}
700
701static int sfp_hwmon_vcc(struct sfp *sfp, u32 attr, long *value)
702{
703 u8 status;
704 int err;
705
706 switch (attr) {
707 case hwmon_in_input:
708 return sfp_hwmon_read_vcc(sfp, SFP_VCC, value);
709
710 case hwmon_in_lcrit:
711 *value = be16_to_cpu(sfp->diag.volt_low_alarm);
712 sfp_hwmon_calibrate_vcc(sfp, value);
713 return 0;
714
715 case hwmon_in_min:
716 *value = be16_to_cpu(sfp->diag.volt_low_warn);
717 sfp_hwmon_calibrate_vcc(sfp, value);
718 return 0;
719
720 case hwmon_in_max:
721 *value = be16_to_cpu(sfp->diag.volt_high_warn);
722 sfp_hwmon_calibrate_vcc(sfp, value);
723 return 0;
724
725 case hwmon_in_crit:
726 *value = be16_to_cpu(sfp->diag.volt_high_alarm);
727 sfp_hwmon_calibrate_vcc(sfp, value);
728 return 0;
729
730 case hwmon_in_lcrit_alarm:
731 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
732 if (err < 0)
733 return err;
734
735 *value = !!(status & SFP_ALARM0_VCC_LOW);
736 return 0;
737
738 case hwmon_in_min_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_LOW);
744 return 0;
745
746 case hwmon_in_max_alarm:
747 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
748 if (err < 0)
749 return err;
750
751 *value = !!(status & SFP_WARN0_VCC_HIGH);
752 return 0;
753
754 case hwmon_in_crit_alarm:
755 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
756 if (err < 0)
757 return err;
758
759 *value = !!(status & SFP_ALARM0_VCC_HIGH);
760 return 0;
761 default:
762 return -EOPNOTSUPP;
763 }
764
765 return -EOPNOTSUPP;
766}
767
768static int sfp_hwmon_bias(struct sfp *sfp, u32 attr, long *value)
769{
770 u8 status;
771 int err;
772
773 switch (attr) {
774 case hwmon_curr_input:
775 return sfp_hwmon_read_bias(sfp, SFP_TX_BIAS, value);
776
777 case hwmon_curr_lcrit:
778 *value = be16_to_cpu(sfp->diag.bias_low_alarm);
779 sfp_hwmon_calibrate_bias(sfp, value);
780 return 0;
781
782 case hwmon_curr_min:
783 *value = be16_to_cpu(sfp->diag.bias_low_warn);
784 sfp_hwmon_calibrate_bias(sfp, value);
785 return 0;
786
787 case hwmon_curr_max:
788 *value = be16_to_cpu(sfp->diag.bias_high_warn);
789 sfp_hwmon_calibrate_bias(sfp, value);
790 return 0;
791
792 case hwmon_curr_crit:
793 *value = be16_to_cpu(sfp->diag.bias_high_alarm);
794 sfp_hwmon_calibrate_bias(sfp, value);
795 return 0;
796
797 case hwmon_curr_lcrit_alarm:
798 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
799 if (err < 0)
800 return err;
801
802 *value = !!(status & SFP_ALARM0_TX_BIAS_LOW);
803 return 0;
804
805 case hwmon_curr_min_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_LOW);
811 return 0;
812
813 case hwmon_curr_max_alarm:
814 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
815 if (err < 0)
816 return err;
817
818 *value = !!(status & SFP_WARN0_TX_BIAS_HIGH);
819 return 0;
820
821 case hwmon_curr_crit_alarm:
822 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
823 if (err < 0)
824 return err;
825
826 *value = !!(status & SFP_ALARM0_TX_BIAS_HIGH);
827 return 0;
828 default:
829 return -EOPNOTSUPP;
830 }
831
832 return -EOPNOTSUPP;
833}
834
835static int sfp_hwmon_tx_power(struct sfp *sfp, u32 attr, long *value)
836{
837 u8 status;
838 int err;
839
840 switch (attr) {
841 case hwmon_power_input:
842 return sfp_hwmon_read_tx_power(sfp, SFP_TX_POWER, value);
843
844 case hwmon_power_lcrit:
845 *value = be16_to_cpu(sfp->diag.txpwr_low_alarm);
846 sfp_hwmon_calibrate_tx_power(sfp, value);
847 return 0;
848
849 case hwmon_power_min:
850 *value = be16_to_cpu(sfp->diag.txpwr_low_warn);
851 sfp_hwmon_calibrate_tx_power(sfp, value);
852 return 0;
853
854 case hwmon_power_max:
855 *value = be16_to_cpu(sfp->diag.txpwr_high_warn);
856 sfp_hwmon_calibrate_tx_power(sfp, value);
857 return 0;
858
859 case hwmon_power_crit:
860 *value = be16_to_cpu(sfp->diag.txpwr_high_alarm);
861 sfp_hwmon_calibrate_tx_power(sfp, value);
862 return 0;
863
864 case hwmon_power_lcrit_alarm:
865 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
866 if (err < 0)
867 return err;
868
869 *value = !!(status & SFP_ALARM0_TXPWR_LOW);
870 return 0;
871
872 case hwmon_power_min_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_LOW);
878 return 0;
879
880 case hwmon_power_max_alarm:
881 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
882 if (err < 0)
883 return err;
884
885 *value = !!(status & SFP_WARN0_TXPWR_HIGH);
886 return 0;
887
888 case hwmon_power_crit_alarm:
889 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
890 if (err < 0)
891 return err;
892
893 *value = !!(status & SFP_ALARM0_TXPWR_HIGH);
894 return 0;
895 default:
896 return -EOPNOTSUPP;
897 }
898
899 return -EOPNOTSUPP;
900}
901
902static int sfp_hwmon_rx_power(struct sfp *sfp, u32 attr, long *value)
903{
904 u8 status;
905 int err;
906
907 switch (attr) {
908 case hwmon_power_input:
909 return sfp_hwmon_read_rx_power(sfp, SFP_RX_POWER, value);
910
911 case hwmon_power_lcrit:
912 *value = be16_to_cpu(sfp->diag.rxpwr_low_alarm);
913 sfp_hwmon_to_rx_power(value);
914 return 0;
915
916 case hwmon_power_min:
917 *value = be16_to_cpu(sfp->diag.rxpwr_low_warn);
918 sfp_hwmon_to_rx_power(value);
919 return 0;
920
921 case hwmon_power_max:
922 *value = be16_to_cpu(sfp->diag.rxpwr_high_warn);
923 sfp_hwmon_to_rx_power(value);
924 return 0;
925
926 case hwmon_power_crit:
927 *value = be16_to_cpu(sfp->diag.rxpwr_high_alarm);
928 sfp_hwmon_to_rx_power(value);
929 return 0;
930
931 case hwmon_power_lcrit_alarm:
932 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
933 if (err < 0)
934 return err;
935
936 *value = !!(status & SFP_ALARM1_RXPWR_LOW);
937 return 0;
938
939 case hwmon_power_min_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_LOW);
945 return 0;
946
947 case hwmon_power_max_alarm:
948 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
949 if (err < 0)
950 return err;
951
952 *value = !!(status & SFP_WARN1_RXPWR_HIGH);
953 return 0;
954
955 case hwmon_power_crit_alarm:
956 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
957 if (err < 0)
958 return err;
959
960 *value = !!(status & SFP_ALARM1_RXPWR_HIGH);
961 return 0;
962 default:
963 return -EOPNOTSUPP;
964 }
965
966 return -EOPNOTSUPP;
967}
968
969static int sfp_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
970 u32 attr, int channel, long *value)
971{
972 struct sfp *sfp = dev_get_drvdata(dev);
973
974 switch (type) {
975 case hwmon_temp:
976 return sfp_hwmon_temp(sfp, attr, value);
977 case hwmon_in:
978 return sfp_hwmon_vcc(sfp, attr, value);
979 case hwmon_curr:
980 return sfp_hwmon_bias(sfp, attr, value);
981 case hwmon_power:
982 switch (channel) {
983 case 0:
984 return sfp_hwmon_tx_power(sfp, attr, value);
985 case 1:
986 return sfp_hwmon_rx_power(sfp, attr, value);
987 default:
988 return -EOPNOTSUPP;
989 }
990 default:
991 return -EOPNOTSUPP;
992 }
993}
994
Andrew Lunnc1236972019-08-25 01:04:17 +0200995static const char *const sfp_hwmon_power_labels[] = {
996 "TX_power",
997 "RX_power",
998};
999
1000static int sfp_hwmon_read_string(struct device *dev,
1001 enum hwmon_sensor_types type,
1002 u32 attr, int channel, const char **str)
1003{
1004 switch (type) {
1005 case hwmon_curr:
1006 switch (attr) {
1007 case hwmon_curr_label:
1008 *str = "bias";
1009 return 0;
1010 default:
1011 return -EOPNOTSUPP;
1012 }
1013 break;
1014 case hwmon_temp:
1015 switch (attr) {
1016 case hwmon_temp_label:
1017 *str = "temperature";
1018 return 0;
1019 default:
1020 return -EOPNOTSUPP;
1021 }
1022 break;
1023 case hwmon_in:
1024 switch (attr) {
1025 case hwmon_in_label:
1026 *str = "VCC";
1027 return 0;
1028 default:
1029 return -EOPNOTSUPP;
1030 }
1031 break;
1032 case hwmon_power:
1033 switch (attr) {
1034 case hwmon_power_label:
1035 *str = sfp_hwmon_power_labels[channel];
1036 return 0;
1037 default:
1038 return -EOPNOTSUPP;
1039 }
1040 break;
1041 default:
1042 return -EOPNOTSUPP;
1043 }
1044
1045 return -EOPNOTSUPP;
1046}
1047
Andrew Lunn13230612018-07-17 21:48:13 +02001048static const struct hwmon_ops sfp_hwmon_ops = {
1049 .is_visible = sfp_hwmon_is_visible,
1050 .read = sfp_hwmon_read,
Andrew Lunnc1236972019-08-25 01:04:17 +02001051 .read_string = sfp_hwmon_read_string,
Andrew Lunn13230612018-07-17 21:48:13 +02001052};
1053
1054static u32 sfp_hwmon_chip_config[] = {
1055 HWMON_C_REGISTER_TZ,
1056 0,
1057};
1058
1059static const struct hwmon_channel_info sfp_hwmon_chip = {
1060 .type = hwmon_chip,
1061 .config = sfp_hwmon_chip_config,
1062};
1063
1064static u32 sfp_hwmon_temp_config[] = {
1065 HWMON_T_INPUT |
1066 HWMON_T_MAX | HWMON_T_MIN |
1067 HWMON_T_MAX_ALARM | HWMON_T_MIN_ALARM |
1068 HWMON_T_CRIT | HWMON_T_LCRIT |
Andrew Lunnc1236972019-08-25 01:04:17 +02001069 HWMON_T_CRIT_ALARM | HWMON_T_LCRIT_ALARM |
1070 HWMON_T_LABEL,
Andrew Lunn13230612018-07-17 21:48:13 +02001071 0,
1072};
1073
1074static const struct hwmon_channel_info sfp_hwmon_temp_channel_info = {
1075 .type = hwmon_temp,
1076 .config = sfp_hwmon_temp_config,
1077};
1078
1079static u32 sfp_hwmon_vcc_config[] = {
1080 HWMON_I_INPUT |
1081 HWMON_I_MAX | HWMON_I_MIN |
1082 HWMON_I_MAX_ALARM | HWMON_I_MIN_ALARM |
1083 HWMON_I_CRIT | HWMON_I_LCRIT |
Andrew Lunnc1236972019-08-25 01:04:17 +02001084 HWMON_I_CRIT_ALARM | HWMON_I_LCRIT_ALARM |
1085 HWMON_I_LABEL,
Andrew Lunn13230612018-07-17 21:48:13 +02001086 0,
1087};
1088
1089static const struct hwmon_channel_info sfp_hwmon_vcc_channel_info = {
1090 .type = hwmon_in,
1091 .config = sfp_hwmon_vcc_config,
1092};
1093
1094static u32 sfp_hwmon_bias_config[] = {
1095 HWMON_C_INPUT |
1096 HWMON_C_MAX | HWMON_C_MIN |
1097 HWMON_C_MAX_ALARM | HWMON_C_MIN_ALARM |
1098 HWMON_C_CRIT | HWMON_C_LCRIT |
Andrew Lunnc1236972019-08-25 01:04:17 +02001099 HWMON_C_CRIT_ALARM | HWMON_C_LCRIT_ALARM |
1100 HWMON_C_LABEL,
Andrew Lunn13230612018-07-17 21:48:13 +02001101 0,
1102};
1103
1104static const struct hwmon_channel_info sfp_hwmon_bias_channel_info = {
1105 .type = hwmon_curr,
1106 .config = sfp_hwmon_bias_config,
1107};
1108
1109static u32 sfp_hwmon_power_config[] = {
1110 /* Transmit power */
1111 HWMON_P_INPUT |
1112 HWMON_P_MAX | HWMON_P_MIN |
1113 HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
1114 HWMON_P_CRIT | HWMON_P_LCRIT |
Andrew Lunnc1236972019-08-25 01:04:17 +02001115 HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM |
1116 HWMON_P_LABEL,
Andrew Lunn13230612018-07-17 21:48:13 +02001117 /* Receive power */
1118 HWMON_P_INPUT |
1119 HWMON_P_MAX | HWMON_P_MIN |
1120 HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
1121 HWMON_P_CRIT | HWMON_P_LCRIT |
Andrew Lunnc1236972019-08-25 01:04:17 +02001122 HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM |
1123 HWMON_P_LABEL,
Andrew Lunn13230612018-07-17 21:48:13 +02001124 0,
1125};
1126
1127static const struct hwmon_channel_info sfp_hwmon_power_channel_info = {
1128 .type = hwmon_power,
1129 .config = sfp_hwmon_power_config,
1130};
1131
1132static const struct hwmon_channel_info *sfp_hwmon_info[] = {
1133 &sfp_hwmon_chip,
1134 &sfp_hwmon_vcc_channel_info,
1135 &sfp_hwmon_temp_channel_info,
1136 &sfp_hwmon_bias_channel_info,
1137 &sfp_hwmon_power_channel_info,
1138 NULL,
1139};
1140
1141static const struct hwmon_chip_info sfp_hwmon_chip_info = {
1142 .ops = &sfp_hwmon_ops,
1143 .info = sfp_hwmon_info,
1144};
1145
1146static int sfp_hwmon_insert(struct sfp *sfp)
1147{
1148 int err, i;
1149
1150 if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE)
1151 return 0;
1152
1153 if (!(sfp->id.ext.diagmon & SFP_DIAGMON_DDM))
1154 return 0;
1155
1156 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
1157 /* This driver in general does not support address
1158 * change.
1159 */
1160 return 0;
1161
1162 err = sfp_read(sfp, true, 0, &sfp->diag, sizeof(sfp->diag));
1163 if (err < 0)
1164 return err;
1165
1166 sfp->hwmon_name = kstrdup(dev_name(sfp->dev), GFP_KERNEL);
1167 if (!sfp->hwmon_name)
1168 return -ENODEV;
1169
1170 for (i = 0; sfp->hwmon_name[i]; i++)
1171 if (hwmon_is_bad_char(sfp->hwmon_name[i]))
1172 sfp->hwmon_name[i] = '_';
1173
1174 sfp->hwmon_dev = hwmon_device_register_with_info(sfp->dev,
1175 sfp->hwmon_name, sfp,
1176 &sfp_hwmon_chip_info,
1177 NULL);
1178
1179 return PTR_ERR_OR_ZERO(sfp->hwmon_dev);
1180}
1181
1182static void sfp_hwmon_remove(struct sfp *sfp)
1183{
Andrew Lunn3e322472018-09-25 01:50:00 +02001184 if (!IS_ERR_OR_NULL(sfp->hwmon_dev)) {
1185 hwmon_device_unregister(sfp->hwmon_dev);
1186 sfp->hwmon_dev = NULL;
1187 kfree(sfp->hwmon_name);
1188 }
Andrew Lunn13230612018-07-17 21:48:13 +02001189}
1190#else
1191static int sfp_hwmon_insert(struct sfp *sfp)
1192{
1193 return 0;
1194}
1195
1196static void sfp_hwmon_remove(struct sfp *sfp)
1197{
1198}
1199#endif
1200
Russell King73970052017-07-25 15:03:39 +01001201/* Helpers */
1202static void sfp_module_tx_disable(struct sfp *sfp)
1203{
1204 dev_dbg(sfp->dev, "tx disable %u -> %u\n",
1205 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1);
1206 sfp->state |= SFP_F_TX_DISABLE;
1207 sfp_set_state(sfp, sfp->state);
1208}
1209
1210static void sfp_module_tx_enable(struct sfp *sfp)
1211{
1212 dev_dbg(sfp->dev, "tx disable %u -> %u\n",
1213 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0);
1214 sfp->state &= ~SFP_F_TX_DISABLE;
1215 sfp_set_state(sfp, sfp->state);
1216}
1217
1218static void sfp_module_tx_fault_reset(struct sfp *sfp)
1219{
1220 unsigned int state = sfp->state;
1221
1222 if (state & SFP_F_TX_DISABLE)
1223 return;
1224
1225 sfp_set_state(sfp, state | SFP_F_TX_DISABLE);
1226
1227 udelay(T_RESET_US);
1228
1229 sfp_set_state(sfp, state);
1230}
1231
1232/* SFP state machine */
1233static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout)
1234{
1235 if (timeout)
1236 mod_delayed_work(system_power_efficient_wq, &sfp->timeout,
1237 timeout);
1238 else
1239 cancel_delayed_work(&sfp->timeout);
1240}
1241
1242static void sfp_sm_next(struct sfp *sfp, unsigned int state,
1243 unsigned int timeout)
1244{
1245 sfp->sm_state = state;
1246 sfp_sm_set_timer(sfp, timeout);
1247}
1248
Russell King0936ebc2019-11-10 14:06:23 +00001249static void sfp_sm_mod_next(struct sfp *sfp, unsigned int state,
Florian Fainelli516b29e2017-10-30 21:42:57 -07001250 unsigned int timeout)
Russell King73970052017-07-25 15:03:39 +01001251{
1252 sfp->sm_mod_state = state;
1253 sfp_sm_set_timer(sfp, timeout);
1254}
1255
1256static void sfp_sm_phy_detach(struct sfp *sfp)
1257{
1258 phy_stop(sfp->mod_phy);
1259 sfp_remove_phy(sfp->sfp_bus);
1260 phy_device_remove(sfp->mod_phy);
1261 phy_device_free(sfp->mod_phy);
1262 sfp->mod_phy = NULL;
1263}
1264
1265static void sfp_sm_probe_phy(struct sfp *sfp)
1266{
1267 struct phy_device *phy;
1268 int err;
1269
Russell King73970052017-07-25 15:03:39 +01001270 phy = mdiobus_scan(sfp->i2c_mii, SFP_PHY_ADDR);
Russell King20b56ed2017-12-15 16:09:36 +00001271 if (phy == ERR_PTR(-ENODEV)) {
1272 dev_info(sfp->dev, "no PHY detected\n");
Russell King73970052017-07-25 15:03:39 +01001273 return;
1274 }
Russell King20b56ed2017-12-15 16:09:36 +00001275 if (IS_ERR(phy)) {
1276 dev_err(sfp->dev, "mdiobus scan returned %ld\n", PTR_ERR(phy));
Russell King73970052017-07-25 15:03:39 +01001277 return;
1278 }
1279
1280 err = sfp_add_phy(sfp->sfp_bus, phy);
1281 if (err) {
1282 phy_device_remove(phy);
1283 phy_device_free(phy);
1284 dev_err(sfp->dev, "sfp_add_phy failed: %d\n", err);
1285 return;
1286 }
1287
1288 sfp->mod_phy = phy;
1289 phy_start(phy);
1290}
1291
1292static void sfp_sm_link_up(struct sfp *sfp)
1293{
1294 sfp_link_up(sfp->sfp_bus);
1295 sfp_sm_next(sfp, SFP_S_LINK_UP, 0);
1296}
1297
1298static void sfp_sm_link_down(struct sfp *sfp)
1299{
1300 sfp_link_down(sfp->sfp_bus);
1301}
1302
1303static void sfp_sm_link_check_los(struct sfp *sfp)
1304{
1305 unsigned int los = sfp->state & SFP_F_LOS;
1306
Russell King710dfbb2017-11-30 13:59:16 +00001307 /* If neither SFP_OPTIONS_LOS_INVERTED nor SFP_OPTIONS_LOS_NORMAL
1308 * are set, we assume that no LOS signal is available.
Russell King73970052017-07-25 15:03:39 +01001309 */
Russell Kingacf1c022017-11-30 13:59:11 +00001310 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED))
Russell King73970052017-07-25 15:03:39 +01001311 los ^= SFP_F_LOS;
Russell King710dfbb2017-11-30 13:59:16 +00001312 else if (!(sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL)))
1313 los = 0;
Russell King73970052017-07-25 15:03:39 +01001314
1315 if (los)
1316 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
1317 else
1318 sfp_sm_link_up(sfp);
1319}
1320
Russell King710dfbb2017-11-30 13:59:16 +00001321static bool sfp_los_event_active(struct sfp *sfp, unsigned int event)
1322{
1323 return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) &&
1324 event == SFP_E_LOS_LOW) ||
1325 (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) &&
1326 event == SFP_E_LOS_HIGH);
1327}
1328
1329static bool sfp_los_event_inactive(struct sfp *sfp, unsigned int event)
1330{
1331 return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) &&
1332 event == SFP_E_LOS_HIGH) ||
1333 (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) &&
1334 event == SFP_E_LOS_LOW);
1335}
1336
Russell King73970052017-07-25 15:03:39 +01001337static void sfp_sm_fault(struct sfp *sfp, bool warn)
1338{
1339 if (sfp->sm_retries && !--sfp->sm_retries) {
Florian Fainelli516b29e2017-10-30 21:42:57 -07001340 dev_err(sfp->dev,
1341 "module persistently indicates fault, disabling\n");
Russell King73970052017-07-25 15:03:39 +01001342 sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0);
1343 } else {
1344 if (warn)
1345 dev_err(sfp->dev, "module transmit fault indicated\n");
1346
1347 sfp_sm_next(sfp, SFP_S_TX_FAULT, T_FAULT_RECOVER);
1348 }
1349}
1350
1351static void sfp_sm_mod_init(struct sfp *sfp)
1352{
1353 sfp_module_tx_enable(sfp);
Russell King181f29d2019-11-10 14:06:54 +00001354}
Russell King73970052017-07-25 15:03:39 +01001355
Russell King181f29d2019-11-10 14:06:54 +00001356static void sfp_sm_probe_for_phy(struct sfp *sfp)
1357{
Russell King73970052017-07-25 15:03:39 +01001358 /* Setting the serdes link mode is guesswork: there's no
1359 * field in the EEPROM which indicates what mode should
1360 * be used.
1361 *
1362 * If it's a gigabit-only fiber module, it probably does
1363 * not have a PHY, so switch to 802.3z negotiation mode.
1364 * Otherwise, switch to SGMII mode (which is required to
1365 * support non-gigabit speeds) and probe for a PHY.
1366 */
1367 if (sfp->id.base.e1000_base_t ||
1368 sfp->id.base.e100_base_lx ||
1369 sfp->id.base.e100_base_fx)
1370 sfp_sm_probe_phy(sfp);
1371}
1372
Russell Kinged32abb2019-11-10 14:06:39 +00001373static int sfp_module_parse_power(struct sfp *sfp)
Jon Nettleton3bb35262018-02-27 15:53:12 +00001374{
Russell Kinged32abb2019-11-10 14:06:39 +00001375 u32 power_mW = 1000;
Jon Nettleton3bb35262018-02-27 15:53:12 +00001376
Jon Nettleton3bb35262018-02-27 15:53:12 +00001377 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_POWER_DECL))
Russell Kinged32abb2019-11-10 14:06:39 +00001378 power_mW = 1500;
Jon Nettleton3bb35262018-02-27 15:53:12 +00001379 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL))
Russell Kinged32abb2019-11-10 14:06:39 +00001380 power_mW = 2000;
Jon Nettleton3bb35262018-02-27 15:53:12 +00001381
Russell King7cfa9c92019-11-10 14:06:44 +00001382 if (power_mW > sfp->max_power_mW) {
1383 /* Module power specification exceeds the allowed maximum. */
1384 if (sfp->id.ext.sff8472_compliance ==
1385 SFP_SFF8472_COMPLIANCE_NONE &&
1386 !(sfp->id.ext.diagmon & SFP_DIAGMON_DDM)) {
1387 /* The module appears not to implement bus address
1388 * 0xa2, so assume that the module powers up in the
1389 * indicated mode.
1390 */
Jon Nettleton3bb35262018-02-27 15:53:12 +00001391 dev_err(sfp->dev,
1392 "Host does not support %u.%uW modules\n",
Russell Kinged32abb2019-11-10 14:06:39 +00001393 power_mW / 1000, (power_mW / 100) % 10);
Jon Nettleton3bb35262018-02-27 15:53:12 +00001394 return -EINVAL;
Russell King7cfa9c92019-11-10 14:06:44 +00001395 } else {
1396 dev_warn(sfp->dev,
1397 "Host does not support %u.%uW modules, module left in power mode 1\n",
1398 power_mW / 1000, (power_mW / 100) % 10);
1399 return 0;
Jon Nettleton3bb35262018-02-27 15:53:12 +00001400 }
Jon Nettleton3bb35262018-02-27 15:53:12 +00001401 }
1402
Russell King7cfa9c92019-11-10 14:06:44 +00001403 /* If the module requires a higher power mode, but also requires
1404 * an address change sequence, warn the user that the module may
1405 * not be functional.
1406 */
1407 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE && power_mW > 1000) {
Jon Nettleton3bb35262018-02-27 15:53:12 +00001408 dev_warn(sfp->dev,
Russell King7cfa9c92019-11-10 14:06:44 +00001409 "Address Change Sequence not supported but module requies %u.%uW, module may not be functional\n",
Russell Kinged32abb2019-11-10 14:06:39 +00001410 power_mW / 1000, (power_mW / 100) % 10);
Jon Nettleton3bb35262018-02-27 15:53:12 +00001411 return 0;
1412 }
1413
Russell Kinged32abb2019-11-10 14:06:39 +00001414 sfp->module_power_mW = power_mW;
1415
1416 return 0;
1417}
1418
1419static int sfp_sm_mod_hpower(struct sfp *sfp)
1420{
1421 u8 val;
1422 int err;
1423
1424 if (sfp->module_power_mW <= 1000)
Jon Nettleton3bb35262018-02-27 15:53:12 +00001425 return 0;
1426
1427 err = sfp_read(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
1428 if (err != sizeof(val)) {
1429 dev_err(sfp->dev, "Failed to read EEPROM: %d\n", err);
1430 err = -EAGAIN;
1431 goto err;
1432 }
1433
1434 val |= BIT(0);
1435
1436 err = sfp_write(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
1437 if (err != sizeof(val)) {
1438 dev_err(sfp->dev, "Failed to write EEPROM: %d\n", err);
1439 err = -EAGAIN;
1440 goto err;
1441 }
1442
1443 dev_info(sfp->dev, "Module switched to %u.%uW power level\n",
Russell Kinged32abb2019-11-10 14:06:39 +00001444 sfp->module_power_mW / 1000,
1445 (sfp->module_power_mW / 100) % 10);
Jon Nettleton3bb35262018-02-27 15:53:12 +00001446 return T_HPOWER_LEVEL;
1447
1448err:
1449 return err;
1450}
1451
Russell King73970052017-07-25 15:03:39 +01001452static int sfp_sm_mod_probe(struct sfp *sfp)
1453{
1454 /* SFP module inserted - read I2C data */
1455 struct sfp_eeprom_id id;
Russell King981f1f82018-03-28 11:18:25 +01001456 bool cotsworks;
Russell King73970052017-07-25 15:03:39 +01001457 u8 check;
Jon Nettleton3bb35262018-02-27 15:53:12 +00001458 int ret;
Russell King73970052017-07-25 15:03:39 +01001459
Jon Nettleton3bb35262018-02-27 15:53:12 +00001460 ret = sfp_read(sfp, false, 0, &id, sizeof(id));
1461 if (ret < 0) {
1462 dev_err(sfp->dev, "failed to read EEPROM: %d\n", ret);
Russell King73970052017-07-25 15:03:39 +01001463 return -EAGAIN;
1464 }
1465
Jon Nettleton3bb35262018-02-27 15:53:12 +00001466 if (ret != sizeof(id)) {
1467 dev_err(sfp->dev, "EEPROM short read: %d\n", ret);
Russell King73970052017-07-25 15:03:39 +01001468 return -EAGAIN;
1469 }
1470
Russell King981f1f82018-03-28 11:18:25 +01001471 /* Cotsworks do not seem to update the checksums when they
1472 * do the final programming with the final module part number,
1473 * serial number and date code.
1474 */
1475 cotsworks = !memcmp(id.base.vendor_name, "COTSWORKS ", 16);
1476
Russell King73970052017-07-25 15:03:39 +01001477 /* Validate the checksum over the base structure */
1478 check = sfp_check(&id.base, sizeof(id.base) - 1);
1479 if (check != id.base.cc_base) {
Russell King981f1f82018-03-28 11:18:25 +01001480 if (cotsworks) {
1481 dev_warn(sfp->dev,
1482 "EEPROM base structure checksum failure (0x%02x != 0x%02x)\n",
1483 check, id.base.cc_base);
1484 } else {
1485 dev_err(sfp->dev,
1486 "EEPROM base structure checksum failure: 0x%02x != 0x%02x\n",
1487 check, id.base.cc_base);
1488 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
1489 16, 1, &id, sizeof(id), true);
1490 return -EINVAL;
1491 }
Russell King73970052017-07-25 15:03:39 +01001492 }
1493
1494 check = sfp_check(&id.ext, sizeof(id.ext) - 1);
1495 if (check != id.ext.cc_ext) {
Russell King981f1f82018-03-28 11:18:25 +01001496 if (cotsworks) {
1497 dev_warn(sfp->dev,
1498 "EEPROM extended structure checksum failure (0x%02x != 0x%02x)\n",
1499 check, id.ext.cc_ext);
1500 } else {
1501 dev_err(sfp->dev,
1502 "EEPROM extended structure checksum failure: 0x%02x != 0x%02x\n",
1503 check, id.ext.cc_ext);
1504 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
1505 16, 1, &id, sizeof(id), true);
1506 memset(&id.ext, 0, sizeof(id.ext));
1507 }
Russell King73970052017-07-25 15:03:39 +01001508 }
1509
1510 sfp->id = id;
1511
Russell Kinga2f247e2017-12-29 12:15:12 +00001512 dev_info(sfp->dev, "module %.*s %.*s rev %.*s sn %.*s dc %.*s\n",
1513 (int)sizeof(id.base.vendor_name), id.base.vendor_name,
1514 (int)sizeof(id.base.vendor_pn), id.base.vendor_pn,
1515 (int)sizeof(id.base.vendor_rev), id.base.vendor_rev,
1516 (int)sizeof(id.ext.vendor_sn), id.ext.vendor_sn,
1517 (int)sizeof(id.ext.datecode), id.ext.datecode);
Russell King73970052017-07-25 15:03:39 +01001518
Russell King259c8612017-12-14 10:27:47 +00001519 /* Check whether we support this module */
1520 if (!sfp->type->module_supported(&sfp->id)) {
1521 dev_err(sfp->dev,
1522 "module is not supported - phys id 0x%02x 0x%02x\n",
Russell King73970052017-07-25 15:03:39 +01001523 sfp->id.base.phys_id, sfp->id.base.phys_ext_id);
1524 return -EINVAL;
1525 }
1526
Russell Kingec7681b2017-11-30 13:59:21 +00001527 /* If the module requires address swap mode, warn about it */
1528 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
1529 dev_warn(sfp->dev,
1530 "module address swap to access page 0xA2 is not supported.\n");
1531
Russell Kinged32abb2019-11-10 14:06:39 +00001532 /* Parse the module power requirement */
1533 ret = sfp_module_parse_power(sfp);
1534 if (ret < 0)
1535 return ret;
1536
Andrew Lunn13230612018-07-17 21:48:13 +02001537 ret = sfp_hwmon_insert(sfp);
1538 if (ret < 0)
1539 return ret;
1540
Jon Nettleton3bb35262018-02-27 15:53:12 +00001541 ret = sfp_module_insert(sfp->sfp_bus, &sfp->id);
1542 if (ret < 0)
1543 return ret;
1544
1545 return sfp_sm_mod_hpower(sfp);
Russell King73970052017-07-25 15:03:39 +01001546}
1547
1548static void sfp_sm_mod_remove(struct sfp *sfp)
1549{
1550 sfp_module_remove(sfp->sfp_bus);
1551
Andrew Lunn13230612018-07-17 21:48:13 +02001552 sfp_hwmon_remove(sfp);
1553
Russell King73970052017-07-25 15:03:39 +01001554 memset(&sfp->id, 0, sizeof(sfp->id));
Russell Kinged32abb2019-11-10 14:06:39 +00001555 sfp->module_power_mW = 0;
Russell King73970052017-07-25 15:03:39 +01001556
1557 dev_info(sfp->dev, "module removed\n");
1558}
1559
Russell Kinge85d81a2019-11-10 14:06:13 +00001560/* This state machine tracks the netdev up/down state */
1561static void sfp_sm_device(struct sfp *sfp, unsigned int event)
Russell King73970052017-07-25 15:03:39 +01001562{
Russell Kinge85d81a2019-11-10 14:06:13 +00001563 switch (sfp->sm_dev_state) {
1564 default:
1565 if (event == SFP_E_DEV_UP)
1566 sfp->sm_dev_state = SFP_DEV_UP;
1567 break;
Russell King73970052017-07-25 15:03:39 +01001568
Russell Kinge85d81a2019-11-10 14:06:13 +00001569 case SFP_DEV_UP:
Russell King1539e0d2019-11-10 14:06:18 +00001570 if (event == SFP_E_DEV_DOWN)
Russell Kinge85d81a2019-11-10 14:06:13 +00001571 sfp->sm_dev_state = SFP_DEV_DOWN;
Russell Kinge85d81a2019-11-10 14:06:13 +00001572 break;
1573 }
1574}
Russell King73970052017-07-25 15:03:39 +01001575
Russell Kingd9009542019-11-10 14:06:33 +00001576/* This state machine tracks the insert/remove state of the module, probes
1577 * the on-board EEPROM, and sets up the power level.
Russell Kinge85d81a2019-11-10 14:06:13 +00001578 */
1579static void sfp_sm_module(struct sfp *sfp, unsigned int event)
1580{
Russell Kingd2e816c2019-11-10 14:06:28 +00001581 /* Handle remove event globally, it resets this state machine */
1582 if (event == SFP_E_REMOVE) {
1583 if (sfp->sm_mod_state > SFP_MOD_PROBE)
1584 sfp_sm_mod_remove(sfp);
1585 sfp_sm_mod_next(sfp, SFP_MOD_EMPTY, 0);
1586 return;
1587 }
1588
Russell King73970052017-07-25 15:03:39 +01001589 switch (sfp->sm_mod_state) {
1590 default:
Russell King8e210b62019-11-10 14:06:49 +00001591 if (event == SFP_E_INSERT && sfp->attached)
Russell Kingd9009542019-11-10 14:06:33 +00001592 sfp_sm_mod_next(sfp, SFP_MOD_PROBE, T_SERIAL);
Russell King73970052017-07-25 15:03:39 +01001593 break;
1594
1595 case SFP_MOD_PROBE:
Russell Kingd2e816c2019-11-10 14:06:28 +00001596 if (event == SFP_E_TIMEOUT) {
Jon Nettleton3bb35262018-02-27 15:53:12 +00001597 int val = sfp_sm_mod_probe(sfp);
Russell King73970052017-07-25 15:03:39 +01001598
Jon Nettleton3bb35262018-02-27 15:53:12 +00001599 if (val == 0)
Russell King0936ebc2019-11-10 14:06:23 +00001600 sfp_sm_mod_next(sfp, SFP_MOD_PRESENT, 0);
Jon Nettleton3bb35262018-02-27 15:53:12 +00001601 else if (val > 0)
Russell King0936ebc2019-11-10 14:06:23 +00001602 sfp_sm_mod_next(sfp, SFP_MOD_HPOWER, val);
Jon Nettleton3bb35262018-02-27 15:53:12 +00001603 else if (val != -EAGAIN)
Russell King0936ebc2019-11-10 14:06:23 +00001604 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
Jon Nettleton3bb35262018-02-27 15:53:12 +00001605 else
1606 sfp_sm_set_timer(sfp, T_PROBE_RETRY);
Russell King73970052017-07-25 15:03:39 +01001607 }
1608 break;
1609
Jon Nettleton3bb35262018-02-27 15:53:12 +00001610 case SFP_MOD_HPOWER:
1611 if (event == SFP_E_TIMEOUT) {
Russell King0936ebc2019-11-10 14:06:23 +00001612 sfp_sm_mod_next(sfp, SFP_MOD_PRESENT, 0);
Jon Nettleton3bb35262018-02-27 15:53:12 +00001613 break;
1614 }
1615 /* fallthrough */
Russell King73970052017-07-25 15:03:39 +01001616 case SFP_MOD_PRESENT:
1617 case SFP_MOD_ERROR:
Russell King73970052017-07-25 15:03:39 +01001618 break;
1619 }
Russell Kinge85d81a2019-11-10 14:06:13 +00001620}
Russell King73970052017-07-25 15:03:39 +01001621
Russell Kinge85d81a2019-11-10 14:06:13 +00001622static void sfp_sm_main(struct sfp *sfp, unsigned int event)
1623{
Russell Kingeefa6f12019-11-10 14:06:59 +00001624 unsigned long timeout;
1625
Russell King73970052017-07-25 15:03:39 +01001626 /* Some events are global */
1627 if (sfp->sm_state != SFP_S_DOWN &&
1628 (sfp->sm_mod_state != SFP_MOD_PRESENT ||
1629 sfp->sm_dev_state != SFP_DEV_UP)) {
1630 if (sfp->sm_state == SFP_S_LINK_UP &&
1631 sfp->sm_dev_state == SFP_DEV_UP)
1632 sfp_sm_link_down(sfp);
1633 if (sfp->mod_phy)
1634 sfp_sm_phy_detach(sfp);
Russell King1539e0d2019-11-10 14:06:18 +00001635 sfp_module_tx_disable(sfp);
Russell King73970052017-07-25 15:03:39 +01001636 sfp_sm_next(sfp, SFP_S_DOWN, 0);
Russell King73970052017-07-25 15:03:39 +01001637 return;
1638 }
1639
1640 /* The main state machine */
1641 switch (sfp->sm_state) {
1642 case SFP_S_DOWN:
Russell Kingeefa6f12019-11-10 14:06:59 +00001643 if (sfp->sm_mod_state != SFP_MOD_PRESENT ||
1644 sfp->sm_dev_state != SFP_DEV_UP)
1645 break;
Russell King181f29d2019-11-10 14:06:54 +00001646
Russell Kingeefa6f12019-11-10 14:06:59 +00001647 sfp_sm_mod_init(sfp);
1648
1649 /* Initialise the fault clearance retries */
1650 sfp->sm_retries = 5;
1651
1652 /* We need to check the TX_FAULT state, which is not defined
1653 * while TX_DISABLE is asserted. The earliest we want to do
1654 * anything (such as probe for a PHY) is 50ms.
1655 */
1656 sfp_sm_next(sfp, SFP_S_WAIT, T_WAIT);
1657 break;
1658
1659 case SFP_S_WAIT:
1660 if (event != SFP_E_TIMEOUT)
1661 break;
1662
1663 sfp_sm_probe_for_phy(sfp);
1664
1665 if (sfp->state & SFP_F_TX_FAULT) {
Russell King181f29d2019-11-10 14:06:54 +00001666 /* Wait t_init before indicating that the link is up,
1667 * provided the current state indicates no TX_FAULT. If
1668 * TX_FAULT clears before this time, that's fine too.
1669 */
Russell Kingeefa6f12019-11-10 14:06:59 +00001670 timeout = T_INIT_JIFFIES;
1671 if (timeout > T_WAIT)
1672 timeout -= T_WAIT;
1673 else
1674 timeout = 1;
1675
1676 sfp_sm_next(sfp, SFP_S_INIT, timeout);
1677 } else {
1678 /* TX_FAULT is not asserted, assume the module has
1679 * finished initialising.
1680 */
1681 goto init_done;
Russell King181f29d2019-11-10 14:06:54 +00001682 }
Russell King73970052017-07-25 15:03:39 +01001683 break;
1684
1685 case SFP_S_INIT:
1686 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT)
1687 sfp_sm_fault(sfp, true);
1688 else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR)
Russell Kingeefa6f12019-11-10 14:06:59 +00001689 init_done: sfp_sm_link_check_los(sfp);
Russell King73970052017-07-25 15:03:39 +01001690 break;
1691
1692 case SFP_S_WAIT_LOS:
1693 if (event == SFP_E_TX_FAULT)
1694 sfp_sm_fault(sfp, true);
Russell King710dfbb2017-11-30 13:59:16 +00001695 else if (sfp_los_event_inactive(sfp, event))
Russell King73970052017-07-25 15:03:39 +01001696 sfp_sm_link_up(sfp);
1697 break;
1698
1699 case SFP_S_LINK_UP:
1700 if (event == SFP_E_TX_FAULT) {
1701 sfp_sm_link_down(sfp);
1702 sfp_sm_fault(sfp, true);
Russell King710dfbb2017-11-30 13:59:16 +00001703 } else if (sfp_los_event_active(sfp, event)) {
Russell King73970052017-07-25 15:03:39 +01001704 sfp_sm_link_down(sfp);
1705 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
1706 }
1707 break;
1708
1709 case SFP_S_TX_FAULT:
1710 if (event == SFP_E_TIMEOUT) {
1711 sfp_module_tx_fault_reset(sfp);
1712 sfp_sm_next(sfp, SFP_S_REINIT, T_INIT_JIFFIES);
1713 }
1714 break;
1715
1716 case SFP_S_REINIT:
1717 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
1718 sfp_sm_fault(sfp, false);
1719 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
1720 dev_info(sfp->dev, "module transmit fault recovered\n");
1721 sfp_sm_link_check_los(sfp);
1722 }
1723 break;
1724
1725 case SFP_S_TX_DISABLE:
1726 break;
1727 }
Russell Kinge85d81a2019-11-10 14:06:13 +00001728}
1729
1730static void sfp_sm_event(struct sfp *sfp, unsigned int event)
1731{
1732 mutex_lock(&sfp->sm_mutex);
1733
1734 dev_dbg(sfp->dev, "SM: enter %s:%s:%s event %s\n",
1735 mod_state_to_str(sfp->sm_mod_state),
1736 dev_state_to_str(sfp->sm_dev_state),
1737 sm_state_to_str(sfp->sm_state),
1738 event_to_str(event));
1739
1740 sfp_sm_module(sfp, event);
1741 sfp_sm_device(sfp, event);
1742 sfp_sm_main(sfp, event);
Russell King73970052017-07-25 15:03:39 +01001743
Andrew Lunn4005a7c2018-08-08 20:54:12 +02001744 dev_dbg(sfp->dev, "SM: exit %s:%s:%s\n",
1745 mod_state_to_str(sfp->sm_mod_state),
1746 dev_state_to_str(sfp->sm_dev_state),
1747 sm_state_to_str(sfp->sm_state));
Russell King73970052017-07-25 15:03:39 +01001748
1749 mutex_unlock(&sfp->sm_mutex);
1750}
1751
Russell Kingb5bfc212019-02-06 10:52:30 +00001752static void sfp_attach(struct sfp *sfp)
1753{
1754 sfp->attached = true;
1755 if (sfp->state & SFP_F_PRESENT)
1756 sfp_sm_event(sfp, SFP_E_INSERT);
1757}
1758
1759static void sfp_detach(struct sfp *sfp)
1760{
1761 sfp->attached = false;
1762 sfp_sm_event(sfp, SFP_E_REMOVE);
1763}
1764
Russell King73970052017-07-25 15:03:39 +01001765static void sfp_start(struct sfp *sfp)
1766{
1767 sfp_sm_event(sfp, SFP_E_DEV_UP);
1768}
1769
1770static void sfp_stop(struct sfp *sfp)
1771{
1772 sfp_sm_event(sfp, SFP_E_DEV_DOWN);
1773}
1774
1775static int sfp_module_info(struct sfp *sfp, struct ethtool_modinfo *modinfo)
1776{
1777 /* locking... and check module is present */
1778
Russell Kingec7681b2017-11-30 13:59:21 +00001779 if (sfp->id.ext.sff8472_compliance &&
1780 !(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)) {
Russell King73970052017-07-25 15:03:39 +01001781 modinfo->type = ETH_MODULE_SFF_8472;
1782 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1783 } else {
1784 modinfo->type = ETH_MODULE_SFF_8079;
1785 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
1786 }
1787 return 0;
1788}
1789
1790static int sfp_module_eeprom(struct sfp *sfp, struct ethtool_eeprom *ee,
Florian Fainelli516b29e2017-10-30 21:42:57 -07001791 u8 *data)
Russell King73970052017-07-25 15:03:39 +01001792{
1793 unsigned int first, last, len;
1794 int ret;
1795
1796 if (ee->len == 0)
1797 return -EINVAL;
1798
1799 first = ee->offset;
1800 last = ee->offset + ee->len;
1801 if (first < ETH_MODULE_SFF_8079_LEN) {
1802 len = min_t(unsigned int, last, ETH_MODULE_SFF_8079_LEN);
1803 len -= first;
1804
Russell King2794ffc2017-12-15 16:09:41 +00001805 ret = sfp_read(sfp, false, first, data, len);
Russell King73970052017-07-25 15:03:39 +01001806 if (ret < 0)
1807 return ret;
1808
1809 first += len;
1810 data += len;
1811 }
Russell King2794ffc2017-12-15 16:09:41 +00001812 if (first < ETH_MODULE_SFF_8472_LEN && last > ETH_MODULE_SFF_8079_LEN) {
Russell King73970052017-07-25 15:03:39 +01001813 len = min_t(unsigned int, last, ETH_MODULE_SFF_8472_LEN);
1814 len -= first;
1815 first -= ETH_MODULE_SFF_8079_LEN;
1816
Russell King2794ffc2017-12-15 16:09:41 +00001817 ret = sfp_read(sfp, true, first, data, len);
Russell King73970052017-07-25 15:03:39 +01001818 if (ret < 0)
1819 return ret;
1820 }
1821 return 0;
1822}
1823
1824static const struct sfp_socket_ops sfp_module_ops = {
Russell Kingb5bfc212019-02-06 10:52:30 +00001825 .attach = sfp_attach,
1826 .detach = sfp_detach,
Russell King73970052017-07-25 15:03:39 +01001827 .start = sfp_start,
1828 .stop = sfp_stop,
1829 .module_info = sfp_module_info,
1830 .module_eeprom = sfp_module_eeprom,
1831};
1832
1833static void sfp_timeout(struct work_struct *work)
1834{
1835 struct sfp *sfp = container_of(work, struct sfp, timeout.work);
1836
1837 rtnl_lock();
1838 sfp_sm_event(sfp, SFP_E_TIMEOUT);
1839 rtnl_unlock();
1840}
1841
1842static void sfp_check_state(struct sfp *sfp)
1843{
1844 unsigned int state, i, changed;
1845
Robert Hancock2158e852019-06-07 10:42:36 -06001846 mutex_lock(&sfp->st_mutex);
Russell King73970052017-07-25 15:03:39 +01001847 state = sfp_get_state(sfp);
1848 changed = state ^ sfp->state;
1849 changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT;
1850
1851 for (i = 0; i < GPIO_MAX; i++)
1852 if (changed & BIT(i))
1853 dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_of_names[i],
1854 !!(sfp->state & BIT(i)), !!(state & BIT(i)));
1855
1856 state |= sfp->state & (SFP_F_TX_DISABLE | SFP_F_RATE_SELECT);
1857 sfp->state = state;
1858
1859 rtnl_lock();
1860 if (changed & SFP_F_PRESENT)
1861 sfp_sm_event(sfp, state & SFP_F_PRESENT ?
1862 SFP_E_INSERT : SFP_E_REMOVE);
1863
1864 if (changed & SFP_F_TX_FAULT)
1865 sfp_sm_event(sfp, state & SFP_F_TX_FAULT ?
1866 SFP_E_TX_FAULT : SFP_E_TX_CLEAR);
1867
1868 if (changed & SFP_F_LOS)
1869 sfp_sm_event(sfp, state & SFP_F_LOS ?
1870 SFP_E_LOS_HIGH : SFP_E_LOS_LOW);
1871 rtnl_unlock();
Robert Hancock2158e852019-06-07 10:42:36 -06001872 mutex_unlock(&sfp->st_mutex);
Russell King73970052017-07-25 15:03:39 +01001873}
1874
1875static irqreturn_t sfp_irq(int irq, void *data)
1876{
1877 struct sfp *sfp = data;
1878
1879 sfp_check_state(sfp);
1880
1881 return IRQ_HANDLED;
1882}
1883
1884static void sfp_poll(struct work_struct *work)
1885{
1886 struct sfp *sfp = container_of(work, struct sfp, poll.work);
1887
1888 sfp_check_state(sfp);
1889 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
1890}
1891
1892static struct sfp *sfp_alloc(struct device *dev)
1893{
1894 struct sfp *sfp;
1895
1896 sfp = kzalloc(sizeof(*sfp), GFP_KERNEL);
1897 if (!sfp)
1898 return ERR_PTR(-ENOMEM);
1899
1900 sfp->dev = dev;
1901
1902 mutex_init(&sfp->sm_mutex);
Robert Hancock2158e852019-06-07 10:42:36 -06001903 mutex_init(&sfp->st_mutex);
Russell King73970052017-07-25 15:03:39 +01001904 INIT_DELAYED_WORK(&sfp->poll, sfp_poll);
1905 INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout);
1906
1907 return sfp;
1908}
1909
1910static void sfp_cleanup(void *data)
1911{
1912 struct sfp *sfp = data;
1913
1914 cancel_delayed_work_sync(&sfp->poll);
1915 cancel_delayed_work_sync(&sfp->timeout);
1916 if (sfp->i2c_mii) {
1917 mdiobus_unregister(sfp->i2c_mii);
1918 mdiobus_free(sfp->i2c_mii);
1919 }
1920 if (sfp->i2c)
1921 i2c_put_adapter(sfp->i2c);
1922 kfree(sfp);
1923}
1924
1925static int sfp_probe(struct platform_device *pdev)
1926{
Russell King259c8612017-12-14 10:27:47 +00001927 const struct sff_data *sff;
Ruslan Babayev7ce236fa2019-05-28 16:02:33 -07001928 struct i2c_adapter *i2c;
Russell King73970052017-07-25 15:03:39 +01001929 struct sfp *sfp;
1930 bool poll = false;
Robert Hancock257c2552019-06-07 10:42:35 -06001931 int err, i;
Russell King73970052017-07-25 15:03:39 +01001932
1933 sfp = sfp_alloc(&pdev->dev);
1934 if (IS_ERR(sfp))
1935 return PTR_ERR(sfp);
1936
1937 platform_set_drvdata(pdev, sfp);
1938
1939 err = devm_add_action(sfp->dev, sfp_cleanup, sfp);
1940 if (err < 0)
1941 return err;
1942
Russell King259c8612017-12-14 10:27:47 +00001943 sff = sfp->type = &sfp_data;
1944
Russell King73970052017-07-25 15:03:39 +01001945 if (pdev->dev.of_node) {
1946 struct device_node *node = pdev->dev.of_node;
Russell King259c8612017-12-14 10:27:47 +00001947 const struct of_device_id *id;
Russell King73970052017-07-25 15:03:39 +01001948 struct device_node *np;
1949
Russell King259c8612017-12-14 10:27:47 +00001950 id = of_match_node(sfp_of_match, node);
1951 if (WARN_ON(!id))
1952 return -EINVAL;
1953
1954 sff = sfp->type = id->data;
1955
Russell King73970052017-07-25 15:03:39 +01001956 np = of_parse_phandle(node, "i2c-bus", 0);
Antoine Tenart66ede1f2018-05-22 12:18:00 +02001957 if (!np) {
1958 dev_err(sfp->dev, "missing 'i2c-bus' property\n");
1959 return -ENODEV;
1960 }
Russell King73970052017-07-25 15:03:39 +01001961
Antoine Tenart66ede1f2018-05-22 12:18:00 +02001962 i2c = of_find_i2c_adapter_by_node(np);
1963 of_node_put(np);
Ruslan Babayev7ce236fa2019-05-28 16:02:33 -07001964 } else if (has_acpi_companion(&pdev->dev)) {
1965 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
1966 struct fwnode_handle *fw = acpi_fwnode_handle(adev);
1967 struct fwnode_reference_args args;
1968 struct acpi_handle *acpi_handle;
1969 int ret;
Russell King73970052017-07-25 15:03:39 +01001970
Ruslan Babayev7ce236fa2019-05-28 16:02:33 -07001971 ret = acpi_node_get_property_reference(fw, "i2c-bus", 0, &args);
Dan Carpenter1086ca32019-06-13 09:51:02 +03001972 if (ret || !is_acpi_device_node(args.fwnode)) {
Ruslan Babayev7ce236fa2019-05-28 16:02:33 -07001973 dev_err(&pdev->dev, "missing 'i2c-bus' property\n");
1974 return -ENODEV;
Russell King73970052017-07-25 15:03:39 +01001975 }
Ruslan Babayev7ce236fa2019-05-28 16:02:33 -07001976
1977 acpi_handle = ACPI_HANDLE_FWNODE(args.fwnode);
1978 i2c = i2c_acpi_find_adapter_by_handle(acpi_handle);
1979 } else {
1980 return -EINVAL;
1981 }
1982
1983 if (!i2c)
1984 return -EPROBE_DEFER;
1985
1986 err = sfp_i2c_configure(sfp, i2c);
1987 if (err < 0) {
1988 i2c_put_adapter(i2c);
1989 return err;
Russell King259c8612017-12-14 10:27:47 +00001990 }
Russell King73970052017-07-25 15:03:39 +01001991
Russell King259c8612017-12-14 10:27:47 +00001992 for (i = 0; i < GPIO_MAX; i++)
1993 if (sff->gpios & BIT(i)) {
Russell King73970052017-07-25 15:03:39 +01001994 sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev,
1995 gpio_of_names[i], gpio_flags[i]);
1996 if (IS_ERR(sfp->gpio[i]))
1997 return PTR_ERR(sfp->gpio[i]);
1998 }
1999
Russell King259c8612017-12-14 10:27:47 +00002000 sfp->get_state = sfp_gpio_get_state;
2001 sfp->set_state = sfp_gpio_set_state;
2002
2003 /* Modules that have no detect signal are always present */
2004 if (!(sfp->gpio[GPIO_MODDEF0]))
2005 sfp->get_state = sff_gpio_get_state;
Russell King73970052017-07-25 15:03:39 +01002006
Jon Nettleton3bb35262018-02-27 15:53:12 +00002007 device_property_read_u32(&pdev->dev, "maximum-power-milliwatt",
2008 &sfp->max_power_mW);
2009 if (!sfp->max_power_mW)
2010 sfp->max_power_mW = 1000;
2011
2012 dev_info(sfp->dev, "Host maximum power %u.%uW\n",
2013 sfp->max_power_mW / 1000, (sfp->max_power_mW / 100) % 10);
2014
Russell King73970052017-07-25 15:03:39 +01002015 /* Get the initial state, and always signal TX disable,
2016 * since the network interface will not be up.
2017 */
2018 sfp->state = sfp_get_state(sfp) | SFP_F_TX_DISABLE;
2019
2020 if (sfp->gpio[GPIO_RATE_SELECT] &&
2021 gpiod_get_value_cansleep(sfp->gpio[GPIO_RATE_SELECT]))
2022 sfp->state |= SFP_F_RATE_SELECT;
2023 sfp_set_state(sfp, sfp->state);
2024 sfp_module_tx_disable(sfp);
Russell King73970052017-07-25 15:03:39 +01002025
2026 for (i = 0; i < GPIO_MAX; i++) {
2027 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
2028 continue;
2029
Robert Hancock257c2552019-06-07 10:42:35 -06002030 sfp->gpio_irq[i] = gpiod_to_irq(sfp->gpio[i]);
2031 if (!sfp->gpio_irq[i]) {
Russell King73970052017-07-25 15:03:39 +01002032 poll = true;
2033 continue;
2034 }
2035
Robert Hancock257c2552019-06-07 10:42:35 -06002036 err = devm_request_threaded_irq(sfp->dev, sfp->gpio_irq[i],
2037 NULL, sfp_irq,
Russell King73970052017-07-25 15:03:39 +01002038 IRQF_ONESHOT |
2039 IRQF_TRIGGER_RISING |
2040 IRQF_TRIGGER_FALLING,
2041 dev_name(sfp->dev), sfp);
Robert Hancock257c2552019-06-07 10:42:35 -06002042 if (err) {
2043 sfp->gpio_irq[i] = 0;
Russell King73970052017-07-25 15:03:39 +01002044 poll = true;
Robert Hancock257c2552019-06-07 10:42:35 -06002045 }
Russell King73970052017-07-25 15:03:39 +01002046 }
2047
2048 if (poll)
2049 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
2050
Antoine Tenarta1f5d1f2018-05-22 12:17:59 +02002051 /* We could have an issue in cases no Tx disable pin is available or
2052 * wired as modules using a laser as their light source will continue to
2053 * be active when the fiber is removed. This could be a safety issue and
2054 * we should at least warn the user about that.
2055 */
2056 if (!sfp->gpio[GPIO_TX_DISABLE])
2057 dev_warn(sfp->dev,
2058 "No tx_disable pin: SFP modules will always be emitting.\n");
2059
Russell Kingb5bfc212019-02-06 10:52:30 +00002060 sfp->sfp_bus = sfp_register_socket(sfp->dev, sfp, &sfp_module_ops);
2061 if (!sfp->sfp_bus)
2062 return -ENOMEM;
2063
Russell King73970052017-07-25 15:03:39 +01002064 return 0;
2065}
2066
2067static int sfp_remove(struct platform_device *pdev)
2068{
2069 struct sfp *sfp = platform_get_drvdata(pdev);
2070
2071 sfp_unregister_socket(sfp->sfp_bus);
2072
2073 return 0;
2074}
2075
Robert Hancock257c2552019-06-07 10:42:35 -06002076static void sfp_shutdown(struct platform_device *pdev)
2077{
2078 struct sfp *sfp = platform_get_drvdata(pdev);
2079 int i;
2080
2081 for (i = 0; i < GPIO_MAX; i++) {
2082 if (!sfp->gpio_irq[i])
2083 continue;
2084
2085 devm_free_irq(sfp->dev, sfp->gpio_irq[i], sfp);
2086 }
2087
2088 cancel_delayed_work_sync(&sfp->poll);
2089 cancel_delayed_work_sync(&sfp->timeout);
2090}
2091
Russell King73970052017-07-25 15:03:39 +01002092static struct platform_driver sfp_driver = {
2093 .probe = sfp_probe,
2094 .remove = sfp_remove,
Robert Hancock257c2552019-06-07 10:42:35 -06002095 .shutdown = sfp_shutdown,
Russell King73970052017-07-25 15:03:39 +01002096 .driver = {
2097 .name = "sfp",
2098 .of_match_table = sfp_of_match,
2099 },
2100};
2101
2102static int sfp_init(void)
2103{
2104 poll_jiffies = msecs_to_jiffies(100);
2105
2106 return platform_driver_register(&sfp_driver);
2107}
2108module_init(sfp_init);
2109
2110static void sfp_exit(void)
2111{
2112 platform_driver_unregister(&sfp_driver);
2113}
2114module_exit(sfp_exit);
2115
2116MODULE_ALIAS("platform:sfp");
2117MODULE_AUTHOR("Russell King");
2118MODULE_LICENSE("GPL v2");