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