1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/debugfs.h>
3 #include <linux/delay.h>
4 #include <linux/gpio/consumer.h>
5 #include <linux/hwmon.h>
6 #include <linux/i2c.h>
7 #include <linux/interrupt.h>
8 #include <linux/jiffies.h>
9 #include <linux/mdio/mdio-i2c.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 "sfp.h"
20
21 enum {
22 GPIO_MODDEF0,
23 GPIO_LOS,
24 GPIO_TX_FAULT,
25 GPIO_TX_DISABLE,
26 GPIO_RS0,
27 GPIO_RS1,
28 GPIO_MAX,
29
30 SFP_F_PRESENT = BIT(GPIO_MODDEF0),
31 SFP_F_LOS = BIT(GPIO_LOS),
32 SFP_F_TX_FAULT = BIT(GPIO_TX_FAULT),
33 SFP_F_TX_DISABLE = BIT(GPIO_TX_DISABLE),
34 SFP_F_RS0 = BIT(GPIO_RS0),
35 SFP_F_RS1 = BIT(GPIO_RS1),
36
37 SFP_F_OUTPUTS = SFP_F_TX_DISABLE | SFP_F_RS0 | SFP_F_RS1,
38
39 SFP_E_INSERT = 0,
40 SFP_E_REMOVE,
41 SFP_E_DEV_ATTACH,
42 SFP_E_DEV_DETACH,
43 SFP_E_DEV_DOWN,
44 SFP_E_DEV_UP,
45 SFP_E_TX_FAULT,
46 SFP_E_TX_CLEAR,
47 SFP_E_LOS_HIGH,
48 SFP_E_LOS_LOW,
49 SFP_E_TIMEOUT,
50
51 SFP_MOD_EMPTY = 0,
52 SFP_MOD_ERROR,
53 SFP_MOD_PROBE,
54 SFP_MOD_WAITDEV,
55 SFP_MOD_HPOWER,
56 SFP_MOD_WAITPWR,
57 SFP_MOD_PRESENT,
58
59 SFP_DEV_DETACHED = 0,
60 SFP_DEV_DOWN,
61 SFP_DEV_UP,
62
63 SFP_S_DOWN = 0,
64 SFP_S_FAIL,
65 SFP_S_WAIT,
66 SFP_S_INIT,
67 SFP_S_INIT_PHY,
68 SFP_S_INIT_TX_FAULT,
69 SFP_S_WAIT_LOS,
70 SFP_S_LINK_UP,
71 SFP_S_TX_FAULT,
72 SFP_S_REINIT,
73 SFP_S_TX_DISABLE,
74 };
75
76 static const char * const mod_state_strings[] = {
77 [SFP_MOD_EMPTY] = "empty",
78 [SFP_MOD_ERROR] = "error",
79 [SFP_MOD_PROBE] = "probe",
80 [SFP_MOD_WAITDEV] = "waitdev",
81 [SFP_MOD_HPOWER] = "hpower",
82 [SFP_MOD_WAITPWR] = "waitpwr",
83 [SFP_MOD_PRESENT] = "present",
84 };
85
mod_state_to_str(unsigned short mod_state)86 static const char *mod_state_to_str(unsigned short mod_state)
87 {
88 if (mod_state >= ARRAY_SIZE(mod_state_strings))
89 return "Unknown module state";
90 return mod_state_strings[mod_state];
91 }
92
93 static const char * const dev_state_strings[] = {
94 [SFP_DEV_DETACHED] = "detached",
95 [SFP_DEV_DOWN] = "down",
96 [SFP_DEV_UP] = "up",
97 };
98
dev_state_to_str(unsigned short dev_state)99 static const char *dev_state_to_str(unsigned short dev_state)
100 {
101 if (dev_state >= ARRAY_SIZE(dev_state_strings))
102 return "Unknown device state";
103 return dev_state_strings[dev_state];
104 }
105
106 static const char * const event_strings[] = {
107 [SFP_E_INSERT] = "insert",
108 [SFP_E_REMOVE] = "remove",
109 [SFP_E_DEV_ATTACH] = "dev_attach",
110 [SFP_E_DEV_DETACH] = "dev_detach",
111 [SFP_E_DEV_DOWN] = "dev_down",
112 [SFP_E_DEV_UP] = "dev_up",
113 [SFP_E_TX_FAULT] = "tx_fault",
114 [SFP_E_TX_CLEAR] = "tx_clear",
115 [SFP_E_LOS_HIGH] = "los_high",
116 [SFP_E_LOS_LOW] = "los_low",
117 [SFP_E_TIMEOUT] = "timeout",
118 };
119
event_to_str(unsigned short event)120 static const char *event_to_str(unsigned short event)
121 {
122 if (event >= ARRAY_SIZE(event_strings))
123 return "Unknown event";
124 return event_strings[event];
125 }
126
127 static const char * const sm_state_strings[] = {
128 [SFP_S_DOWN] = "down",
129 [SFP_S_FAIL] = "fail",
130 [SFP_S_WAIT] = "wait",
131 [SFP_S_INIT] = "init",
132 [SFP_S_INIT_PHY] = "init_phy",
133 [SFP_S_INIT_TX_FAULT] = "init_tx_fault",
134 [SFP_S_WAIT_LOS] = "wait_los",
135 [SFP_S_LINK_UP] = "link_up",
136 [SFP_S_TX_FAULT] = "tx_fault",
137 [SFP_S_REINIT] = "reinit",
138 [SFP_S_TX_DISABLE] = "tx_disable",
139 };
140
sm_state_to_str(unsigned short sm_state)141 static const char *sm_state_to_str(unsigned short sm_state)
142 {
143 if (sm_state >= ARRAY_SIZE(sm_state_strings))
144 return "Unknown state";
145 return sm_state_strings[sm_state];
146 }
147
148 static const char *gpio_names[] = {
149 "mod-def0",
150 "los",
151 "tx-fault",
152 "tx-disable",
153 "rate-select0",
154 "rate-select1",
155 };
156
157 static const enum gpiod_flags gpio_flags[] = {
158 GPIOD_IN,
159 GPIOD_IN,
160 GPIOD_IN,
161 GPIOD_ASIS,
162 GPIOD_ASIS,
163 GPIOD_ASIS,
164 };
165
166 /* t_start_up (SFF-8431) or t_init (SFF-8472) is the time required for a
167 * non-cooled module to initialise its laser safety circuitry. We wait
168 * an initial T_WAIT period before we check the tx fault to give any PHY
169 * on board (for a copper SFP) time to initialise.
170 */
171 #define T_WAIT msecs_to_jiffies(50)
172 #define T_START_UP msecs_to_jiffies(300)
173 #define T_START_UP_BAD_GPON msecs_to_jiffies(60000)
174
175 /* t_reset is the time required to assert the TX_DISABLE signal to reset
176 * an indicated TX_FAULT.
177 */
178 #define T_RESET_US 10
179 #define T_FAULT_RECOVER msecs_to_jiffies(1000)
180
181 /* N_FAULT_INIT is the number of recovery attempts at module initialisation
182 * time. If the TX_FAULT signal is not deasserted after this number of
183 * attempts at clearing it, we decide that the module is faulty.
184 * N_FAULT is the same but after the module has initialised.
185 */
186 #define N_FAULT_INIT 5
187 #define N_FAULT 5
188
189 /* T_PHY_RETRY is the time interval between attempts to probe the PHY.
190 * R_PHY_RETRY is the number of attempts.
191 */
192 #define T_PHY_RETRY msecs_to_jiffies(50)
193 #define R_PHY_RETRY 25
194
195 /* SFP module presence detection is poor: the three MOD DEF signals are
196 * the same length on the PCB, which means it's possible for MOD DEF 0 to
197 * connect before the I2C bus on MOD DEF 1/2.
198 *
199 * The SFF-8472 specifies t_serial ("Time from power on until module is
200 * ready for data transmission over the two wire serial bus.") as 300ms.
201 */
202 #define T_SERIAL msecs_to_jiffies(300)
203 #define T_HPOWER_LEVEL msecs_to_jiffies(300)
204 #define T_PROBE_RETRY_INIT msecs_to_jiffies(100)
205 #define R_PROBE_RETRY_INIT 10
206 #define T_PROBE_RETRY_SLOW msecs_to_jiffies(5000)
207 #define R_PROBE_RETRY_SLOW 12
208
209 /* SFP modules appear to always have their PHY configured for bus address
210 * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
211 * RollBall SFPs access phy via SFP Enhanced Digital Diagnostic Interface
212 * via address 0x51 (mdio-i2c will use RollBall protocol on this address).
213 */
214 #define SFP_PHY_ADDR 22
215 #define SFP_PHY_ADDR_ROLLBALL 17
216
217 /* SFP_EEPROM_BLOCK_SIZE is the size of data chunk to read the EEPROM
218 * at a time. Some SFP modules and also some Linux I2C drivers do not like
219 * reads longer than 16 bytes.
220 */
221 #define SFP_EEPROM_BLOCK_SIZE 16
222
223 #define SFP_POLL_INTERVAL msecs_to_jiffies(100)
224
225 struct sff_data {
226 unsigned int gpios;
227 bool (*module_supported)(const struct sfp_eeprom_id *id);
228 };
229
230 struct sfp {
231 struct device *dev;
232 struct i2c_adapter *i2c;
233 struct mii_bus *i2c_mii;
234 struct sfp_bus *sfp_bus;
235 enum mdio_i2c_proto mdio_protocol;
236 struct phy_device *mod_phy;
237 const struct sff_data *type;
238 size_t i2c_max_block_size;
239 size_t i2c_block_size;
240 u32 max_power_mW;
241
242 unsigned int (*get_state)(struct sfp *);
243 void (*set_state)(struct sfp *, unsigned int);
244 int (*read)(struct sfp *, bool, u8, void *, size_t);
245 int (*write)(struct sfp *, bool, u8, void *, size_t);
246
247 struct gpio_desc *gpio[GPIO_MAX];
248 int gpio_irq[GPIO_MAX];
249
250 bool need_poll;
251
252 /* Access rules:
253 * state_hw_drive: st_mutex held
254 * state_hw_mask: st_mutex held
255 * state_soft_mask: st_mutex held
256 * state: st_mutex held unless reading input bits
257 */
258 struct mutex st_mutex; /* Protects state */
259 unsigned int state_hw_drive;
260 unsigned int state_hw_mask;
261 unsigned int state_soft_mask;
262 unsigned int state_ignore_mask;
263 unsigned int state;
264
265 struct delayed_work poll;
266 struct delayed_work timeout;
267 struct mutex sm_mutex; /* Protects state machine */
268 unsigned char sm_mod_state;
269 unsigned char sm_mod_tries_init;
270 unsigned char sm_mod_tries;
271 unsigned char sm_dev_state;
272 unsigned short sm_state;
273 unsigned char sm_fault_retries;
274 unsigned char sm_phy_retries;
275
276 struct sfp_eeprom_id id;
277 unsigned int module_power_mW;
278 unsigned int module_t_start_up;
279 unsigned int module_t_wait;
280 unsigned int phy_t_retry;
281
282 unsigned int rate_kbd;
283 unsigned int rs_threshold_kbd;
284 unsigned int rs_state_mask;
285
286 bool have_a2;
287
288 const struct sfp_quirk *quirk;
289
290 #if IS_ENABLED(CONFIG_HWMON)
291 struct sfp_diag diag;
292 struct delayed_work hwmon_probe;
293 unsigned int hwmon_tries;
294 struct device *hwmon_dev;
295 char *hwmon_name;
296 #endif
297
298 #if IS_ENABLED(CONFIG_DEBUG_FS)
299 struct dentry *debugfs_dir;
300 #endif
301 };
302
sfp_schedule_poll(struct sfp * sfp)303 static void sfp_schedule_poll(struct sfp *sfp)
304 {
305 mod_delayed_work(system_percpu_wq, &sfp->poll, SFP_POLL_INTERVAL);
306 }
307
sff_module_supported(const struct sfp_eeprom_id * id)308 static bool sff_module_supported(const struct sfp_eeprom_id *id)
309 {
310 return id->base.phys_id == SFF8024_ID_SFF_8472 &&
311 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
312 }
313
314 static const struct sff_data sff_data = {
315 .gpios = SFP_F_LOS | SFP_F_TX_FAULT | SFP_F_TX_DISABLE,
316 .module_supported = sff_module_supported,
317 };
318
sfp_module_supported(const struct sfp_eeprom_id * id)319 static bool sfp_module_supported(const struct sfp_eeprom_id *id)
320 {
321 if (id->base.phys_id == SFF8024_ID_SFP &&
322 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP)
323 return true;
324
325 /* SFP GPON module Ubiquiti U-Fiber Instant has in its EEPROM stored
326 * phys id SFF instead of SFP. Therefore mark this module explicitly
327 * as supported based on vendor name and pn match.
328 */
329 if (id->base.phys_id == SFF8024_ID_SFF_8472 &&
330 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP &&
331 !memcmp(id->base.vendor_name, "UBNT ", 16) &&
332 !memcmp(id->base.vendor_pn, "UF-INSTANT ", 16))
333 return true;
334
335 return false;
336 }
337
338 static const struct sff_data sfp_data = {
339 .gpios = SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT |
340 SFP_F_TX_DISABLE | SFP_F_RS0 | SFP_F_RS1,
341 .module_supported = sfp_module_supported,
342 };
343
344 static const struct of_device_id sfp_of_match[] = {
345 { .compatible = "sff,sff", .data = &sff_data, },
346 { .compatible = "sff,sfp", .data = &sfp_data, },
347 { },
348 };
349 MODULE_DEVICE_TABLE(of, sfp_of_match);
350
sfp_fixup_long_startup(struct sfp * sfp)351 static void sfp_fixup_long_startup(struct sfp *sfp)
352 {
353 sfp->module_t_start_up = T_START_UP_BAD_GPON;
354 }
355
sfp_fixup_ignore_los(struct sfp * sfp)356 static void sfp_fixup_ignore_los(struct sfp *sfp)
357 {
358 /* This forces LOS to zero, so we ignore transitions */
359 sfp->state_ignore_mask |= SFP_F_LOS;
360 /* Make sure that LOS options are clear */
361 sfp->id.ext.options &= ~cpu_to_be16(SFP_OPTIONS_LOS_INVERTED |
362 SFP_OPTIONS_LOS_NORMAL);
363 }
364
sfp_fixup_ignore_tx_fault(struct sfp * sfp)365 static void sfp_fixup_ignore_tx_fault(struct sfp *sfp)
366 {
367 sfp->state_ignore_mask |= SFP_F_TX_FAULT;
368 }
369
sfp_fixup_ignore_tx_fault_and_los(struct sfp * sfp)370 static void sfp_fixup_ignore_tx_fault_and_los(struct sfp *sfp)
371 {
372 sfp_fixup_ignore_tx_fault(sfp);
373 sfp_fixup_ignore_los(sfp);
374 }
375
sfp_fixup_ignore_hw(struct sfp * sfp,unsigned int mask)376 static void sfp_fixup_ignore_hw(struct sfp *sfp, unsigned int mask)
377 {
378 sfp->state_hw_mask &= ~mask;
379 }
380
sfp_fixup_nokia(struct sfp * sfp)381 static void sfp_fixup_nokia(struct sfp *sfp)
382 {
383 sfp_fixup_long_startup(sfp);
384 sfp_fixup_ignore_los(sfp);
385 }
386
387 // For 10GBASE-T short-reach modules
sfp_fixup_10gbaset_30m(struct sfp * sfp)388 static void sfp_fixup_10gbaset_30m(struct sfp *sfp)
389 {
390 sfp->id.base.connector = SFF8024_CONNECTOR_RJ45;
391 sfp->id.base.extended_cc = SFF8024_ECC_10GBASE_T_SR;
392 }
393
sfp_fixup_rollball(struct sfp * sfp)394 static void sfp_fixup_rollball(struct sfp *sfp)
395 {
396 sfp->mdio_protocol = MDIO_I2C_ROLLBALL;
397
398 /* RollBall modules may disallow access to PHY registers for up to 25
399 * seconds, and the reads return 0xffff before that. Increase the time
400 * between PHY probe retries from 50ms to 1s so that we will wait for
401 * the PHY for a sufficient amount of time.
402 */
403 sfp->phy_t_retry = msecs_to_jiffies(1000);
404 }
405
sfp_fixup_rollball_wait4s(struct sfp * sfp)406 static void sfp_fixup_rollball_wait4s(struct sfp *sfp)
407 {
408 sfp_fixup_rollball(sfp);
409
410 /* The RollBall fixup is not enough for FS modules, the PHY chip inside
411 * them does not return 0xffff for PHY ID registers in all MMDs for the
412 * while initializing. They need a 4 second wait before accessing PHY.
413 */
414 sfp->module_t_wait = msecs_to_jiffies(4000);
415 }
416
sfp_fixup_fs_10gt(struct sfp * sfp)417 static void sfp_fixup_fs_10gt(struct sfp *sfp)
418 {
419 sfp_fixup_10gbaset_30m(sfp);
420 sfp_fixup_rollball_wait4s(sfp);
421 }
422
sfp_fixup_halny_gsfp(struct sfp * sfp)423 static void sfp_fixup_halny_gsfp(struct sfp *sfp)
424 {
425 /* Ignore the TX_FAULT and LOS signals on this module.
426 * these are possibly used for other purposes on this
427 * module, e.g. a serial port.
428 */
429 sfp_fixup_ignore_hw(sfp, SFP_F_TX_FAULT | SFP_F_LOS);
430 }
431
sfp_fixup_potron(struct sfp * sfp)432 static void sfp_fixup_potron(struct sfp *sfp)
433 {
434 /*
435 * The TX_FAULT and LOS pins on this device are used for serial
436 * communication, so ignore them. Additionally, provide extra
437 * time for this device to fully start up.
438 */
439
440 sfp_fixup_long_startup(sfp);
441 sfp_fixup_ignore_hw(sfp, SFP_F_TX_FAULT | SFP_F_LOS);
442 }
443
sfp_fixup_rollball_cc(struct sfp * sfp)444 static void sfp_fixup_rollball_cc(struct sfp *sfp)
445 {
446 sfp_fixup_rollball(sfp);
447
448 /* Some RollBall SFPs may have wrong (zero) extended compliance code
449 * burned in EEPROM. For PHY probing we need the correct one.
450 */
451 sfp->id.base.extended_cc = SFF8024_ECC_10GBASE_T_SFI;
452 }
453
sfp_quirk_2500basex(const struct sfp_eeprom_id * id,struct sfp_module_caps * caps)454 static void sfp_quirk_2500basex(const struct sfp_eeprom_id *id,
455 struct sfp_module_caps *caps)
456 {
457 linkmode_set_bit(ETHTOOL_LINK_MODE_2500baseX_Full_BIT,
458 caps->link_modes);
459 __set_bit(PHY_INTERFACE_MODE_2500BASEX, caps->interfaces);
460 }
461
sfp_quirk_disable_autoneg(const struct sfp_eeprom_id * id,struct sfp_module_caps * caps)462 static void sfp_quirk_disable_autoneg(const struct sfp_eeprom_id *id,
463 struct sfp_module_caps *caps)
464 {
465 linkmode_clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, caps->link_modes);
466 }
467
sfp_quirk_oem_2_5g(const struct sfp_eeprom_id * id,struct sfp_module_caps * caps)468 static void sfp_quirk_oem_2_5g(const struct sfp_eeprom_id *id,
469 struct sfp_module_caps *caps)
470 {
471 /* Copper 2.5G SFP */
472 linkmode_set_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
473 caps->link_modes);
474 __set_bit(PHY_INTERFACE_MODE_2500BASEX, caps->interfaces);
475 sfp_quirk_disable_autoneg(id, caps);
476 }
477
sfp_quirk_ubnt_uf_instant(const struct sfp_eeprom_id * id,struct sfp_module_caps * caps)478 static void sfp_quirk_ubnt_uf_instant(const struct sfp_eeprom_id *id,
479 struct sfp_module_caps *caps)
480 {
481 /* Ubiquiti U-Fiber Instant module claims that support all transceiver
482 * types including 10G Ethernet which is not truth. So clear all claimed
483 * modes and set only one mode which module supports: 1000baseX_Full,
484 * along with the Autoneg and pause bits.
485 */
486 linkmode_zero(caps->link_modes);
487 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
488 caps->link_modes);
489 linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, caps->link_modes);
490 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, caps->link_modes);
491 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, caps->link_modes);
492
493 phy_interface_zero(caps->interfaces);
494 __set_bit(PHY_INTERFACE_MODE_1000BASEX, caps->interfaces);
495 }
496
497 #define SFP_QUIRK(_v, _p, _s, _f) \
498 { .vendor = _v, .part = _p, .support = _s, .fixup = _f, }
499 #define SFP_QUIRK_S(_v, _p, _s) SFP_QUIRK(_v, _p, _s, NULL)
500 #define SFP_QUIRK_F(_v, _p, _f) SFP_QUIRK(_v, _p, NULL, _f)
501
502 static const struct sfp_quirk sfp_quirks[] = {
503 // Alcatel Lucent G-010S-P can operate at 2500base-X, but incorrectly
504 // report 2500MBd NRZ in their EEPROM
505 SFP_QUIRK("ALCATELLUCENT", "G010SP", sfp_quirk_2500basex,
506 sfp_fixup_ignore_tx_fault),
507
508 // Alcatel Lucent G-010S-A can operate at 2500base-X, but report 3.2GBd
509 // NRZ in their EEPROM
510 SFP_QUIRK("ALCATELLUCENT", "3FE46541AA", sfp_quirk_2500basex,
511 sfp_fixup_nokia),
512
513 SFP_QUIRK_F("BIDB", "X-ONU-SFPP", sfp_fixup_potron),
514
515 // FLYPRO SFP-10GT-CS-30M uses Rollball protocol to talk to the PHY.
516 SFP_QUIRK_F("FLYPRO", "SFP-10GT-CS-30M", sfp_fixup_rollball),
517
518 // Fiberstore SFP-10G-T doesn't identify as copper, uses the Rollball
519 // protocol to talk to the PHY and needs 4 sec wait before probing the
520 // PHY.
521 SFP_QUIRK_F("FS", "SFP-10G-T", sfp_fixup_fs_10gt),
522
523 // Fiberstore SFP-2.5G-T and SFP-10GM-T uses Rollball protocol to talk
524 // to the PHY and needs 4 sec wait before probing the PHY.
525 SFP_QUIRK_F("FS", "SFP-2.5G-T", sfp_fixup_rollball_wait4s),
526 SFP_QUIRK_F("FS", "SFP-10GM-T", sfp_fixup_rollball_wait4s),
527
528 // Fiberstore GPON-ONU-34-20BI can operate at 2500base-X, but report 1.2GBd
529 // NRZ in their EEPROM
530 SFP_QUIRK("FS", "GPON-ONU-34-20BI", sfp_quirk_2500basex,
531 sfp_fixup_ignore_tx_fault),
532
533 SFP_QUIRK_F("HALNy", "HL-GSFP", sfp_fixup_halny_gsfp),
534
535 SFP_QUIRK_F("H-COM", "SPP425H-GAB4", sfp_fixup_potron),
536
537 // HG MXPD-483II-F 2.5G supports 2500Base-X, but incorrectly reports
538 // 2600MBd in their EERPOM
539 SFP_QUIRK_S("HG GENUINE", "MXPD-483II", sfp_quirk_2500basex),
540
541 // Huawei MA5671A can operate at 2500base-X, but report 1.2GBd NRZ in
542 // their EEPROM
543 SFP_QUIRK("HUAWEI", "MA5671A", sfp_quirk_2500basex,
544 sfp_fixup_ignore_tx_fault_and_los),
545
546 // Hisense LXT-010S-H is a GPON ONT SFP (sold as LEOX LXT-010S-H) that
547 // can operate at 2500base-X, but reports 1000BASE-LX / 1300MBd in its
548 // EEPROM
549 SFP_QUIRK("Hisense-Leox", "LXT-010S-H", sfp_quirk_2500basex,
550 sfp_fixup_ignore_tx_fault),
551
552 // Hisense ZNID-GPON-2311NA can operate at 2500base-X, but reports
553 // 1000BASE-LX / 1300MBd in its EEPROM
554 SFP_QUIRK("Hisense", "ZNID-GPON-2311NA", sfp_quirk_2500basex,
555 sfp_fixup_ignore_tx_fault),
556
557 // HSGQ HSGQ-XPON-Stick can operate at 2500base-X, but reports
558 // 1000BASE-LX / 1300MBd in its EEPROM
559 SFP_QUIRK("HSGQ", "HSGQ-XPON-Stick", sfp_quirk_2500basex,
560 sfp_fixup_ignore_tx_fault),
561
562 // Lantech 8330-262D-E and 8330-265D can operate at 2500base-X, but
563 // incorrectly report 2500MBd NRZ in their EEPROM.
564 // Some 8330-265D modules have inverted LOS, while all of them report
565 // normal LOS in EEPROM. Therefore we need to ignore LOS entirely.
566 SFP_QUIRK_S("Lantech", "8330-262D-E", sfp_quirk_2500basex),
567 SFP_QUIRK("Lantech", "8330-265D", sfp_quirk_2500basex,
568 sfp_fixup_ignore_los),
569
570 SFP_QUIRK_S("UBNT", "UF-INSTANT", sfp_quirk_ubnt_uf_instant),
571
572 // Walsun HXSX-ATR[CI]-1 don't identify as copper, and use the
573 // Rollball protocol to talk to the PHY.
574 SFP_QUIRK_F("Walsun", "HXSX-ATRC-1", sfp_fixup_fs_10gt),
575 SFP_QUIRK_F("Walsun", "HXSX-ATRI-1", sfp_fixup_fs_10gt),
576
577 SFP_QUIRK_F("YV", "SFP+ONU-XGSPON", sfp_fixup_potron),
578
579 // OEM SFP-GE-T is a 1000Base-T module with broken TX_FAULT indicator
580 SFP_QUIRK_F("OEM", "SFP-GE-T", sfp_fixup_ignore_tx_fault),
581
582 SFP_QUIRK_F("OEM", "SFP-10G-T", sfp_fixup_rollball_cc),
583 SFP_QUIRK_S("OEM", "SFP-2.5G-T", sfp_quirk_oem_2_5g),
584 SFP_QUIRK_S("OEM", "SFP-2.5G-BX10-D", sfp_quirk_2500basex),
585 SFP_QUIRK_S("OEM", "SFP-2.5G-BX10-U", sfp_quirk_2500basex),
586 SFP_QUIRK_F("OEM", "RTSFP-10", sfp_fixup_rollball_cc),
587 SFP_QUIRK_F("OEM", "RTSFP-10G", sfp_fixup_rollball_cc),
588 SFP_QUIRK_F("Turris", "RTSFP-2.5G", sfp_fixup_rollball),
589 SFP_QUIRK_F("Turris", "RTSFP-10", sfp_fixup_rollball),
590 SFP_QUIRK_F("Turris", "RTSFP-10G", sfp_fixup_rollball),
591
592 SFP_QUIRK_S("ZOERAX", "SFP-2.5G-T", sfp_quirk_oem_2_5g),
593 };
594
sfp_strlen(const char * str,size_t maxlen)595 static size_t sfp_strlen(const char *str, size_t maxlen)
596 {
597 size_t size, i;
598
599 /* Trailing characters should be filled with space chars, but
600 * some manufacturers can't read SFF-8472 and use NUL.
601 */
602 for (i = 0, size = 0; i < maxlen; i++)
603 if (str[i] != ' ' && str[i] != '\0')
604 size = i + 1;
605
606 return size;
607 }
608
sfp_match(const char * qs,const char * str,size_t len)609 static bool sfp_match(const char *qs, const char *str, size_t len)
610 {
611 if (!qs)
612 return true;
613 if (strlen(qs) != len)
614 return false;
615 return !strncmp(qs, str, len);
616 }
617
sfp_lookup_quirk(const struct sfp_eeprom_id * id)618 static const struct sfp_quirk *sfp_lookup_quirk(const struct sfp_eeprom_id *id)
619 {
620 const struct sfp_quirk *q;
621 unsigned int i;
622 size_t vs, ps;
623
624 vs = sfp_strlen(id->base.vendor_name, ARRAY_SIZE(id->base.vendor_name));
625 ps = sfp_strlen(id->base.vendor_pn, ARRAY_SIZE(id->base.vendor_pn));
626
627 for (i = 0, q = sfp_quirks; i < ARRAY_SIZE(sfp_quirks); i++, q++)
628 if (sfp_match(q->vendor, id->base.vendor_name, vs) &&
629 sfp_match(q->part, id->base.vendor_pn, ps))
630 return q;
631
632 return NULL;
633 }
634
sfp_gpio_get_state(struct sfp * sfp)635 static unsigned int sfp_gpio_get_state(struct sfp *sfp)
636 {
637 unsigned int i, state, v;
638
639 for (i = state = 0; i < GPIO_MAX; i++) {
640 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
641 continue;
642
643 v = gpiod_get_value_cansleep(sfp->gpio[i]);
644 if (v)
645 state |= BIT(i);
646 }
647
648 return state;
649 }
650
sff_gpio_get_state(struct sfp * sfp)651 static unsigned int sff_gpio_get_state(struct sfp *sfp)
652 {
653 return sfp_gpio_get_state(sfp) | SFP_F_PRESENT;
654 }
655
sfp_gpio_set_state(struct sfp * sfp,unsigned int state)656 static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state)
657 {
658 unsigned int drive;
659
660 if (state & SFP_F_PRESENT)
661 /* If the module is present, drive the requested signals */
662 drive = sfp->state_hw_drive;
663 else
664 /* Otherwise, let them float to the pull-ups */
665 drive = 0;
666
667 if (sfp->gpio[GPIO_TX_DISABLE]) {
668 if (drive & SFP_F_TX_DISABLE)
669 gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE],
670 state & SFP_F_TX_DISABLE);
671 else
672 gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]);
673 }
674
675 if (sfp->gpio[GPIO_RS0]) {
676 if (drive & SFP_F_RS0)
677 gpiod_direction_output(sfp->gpio[GPIO_RS0],
678 state & SFP_F_RS0);
679 else
680 gpiod_direction_input(sfp->gpio[GPIO_RS0]);
681 }
682
683 if (sfp->gpio[GPIO_RS1]) {
684 if (drive & SFP_F_RS1)
685 gpiod_direction_output(sfp->gpio[GPIO_RS1],
686 state & SFP_F_RS1);
687 else
688 gpiod_direction_input(sfp->gpio[GPIO_RS1]);
689 }
690 }
691
sfp_i2c_read(struct sfp * sfp,bool a2,u8 dev_addr,void * buf,size_t len)692 static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
693 size_t len)
694 {
695 struct i2c_msg msgs[2];
696 u8 bus_addr = a2 ? 0x51 : 0x50;
697 size_t block_size = sfp->i2c_block_size;
698 size_t this_len;
699 int ret;
700
701 msgs[0].addr = bus_addr;
702 msgs[0].flags = 0;
703 msgs[0].len = 1;
704 msgs[0].buf = &dev_addr;
705 msgs[1].addr = bus_addr;
706 msgs[1].flags = I2C_M_RD;
707 msgs[1].len = len;
708 msgs[1].buf = buf;
709
710 while (len) {
711 this_len = len;
712 if (this_len > block_size)
713 this_len = block_size;
714
715 msgs[1].len = this_len;
716
717 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
718 if (ret < 0)
719 return ret;
720
721 if (ret != ARRAY_SIZE(msgs))
722 break;
723
724 msgs[1].buf += this_len;
725 dev_addr += this_len;
726 len -= this_len;
727 }
728
729 return msgs[1].buf - (u8 *)buf;
730 }
731
sfp_i2c_write(struct sfp * sfp,bool a2,u8 dev_addr,void * buf,size_t len)732 static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
733 size_t len)
734 {
735 struct i2c_msg msgs[1];
736 u8 bus_addr = a2 ? 0x51 : 0x50;
737 int ret;
738
739 msgs[0].addr = bus_addr;
740 msgs[0].flags = 0;
741 msgs[0].len = 1 + len;
742 msgs[0].buf = kmalloc(1 + len, GFP_KERNEL);
743 if (!msgs[0].buf)
744 return -ENOMEM;
745
746 msgs[0].buf[0] = dev_addr;
747 memcpy(&msgs[0].buf[1], buf, len);
748
749 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
750
751 kfree(msgs[0].buf);
752
753 if (ret < 0)
754 return ret;
755
756 return ret == ARRAY_SIZE(msgs) ? len : 0;
757 }
758
sfp_smbus_byte_read(struct sfp * sfp,bool a2,u8 dev_addr,void * buf,size_t len)759 static int sfp_smbus_byte_read(struct sfp *sfp, bool a2, u8 dev_addr,
760 void *buf, size_t len)
761 {
762 union i2c_smbus_data smbus_data;
763 u8 bus_addr = a2 ? 0x51 : 0x50;
764 u8 *data = buf;
765 int ret;
766
767 while (len) {
768 ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
769 I2C_SMBUS_READ, dev_addr,
770 I2C_SMBUS_BYTE_DATA, &smbus_data);
771 if (ret < 0)
772 return ret;
773
774 *data = smbus_data.byte;
775
776 len--;
777 data++;
778 dev_addr++;
779 }
780
781 return data - (u8 *)buf;
782 }
783
sfp_smbus_byte_write(struct sfp * sfp,bool a2,u8 dev_addr,void * buf,size_t len)784 static int sfp_smbus_byte_write(struct sfp *sfp, bool a2, u8 dev_addr,
785 void *buf, size_t len)
786 {
787 union i2c_smbus_data smbus_data;
788 u8 bus_addr = a2 ? 0x51 : 0x50;
789 u8 *data = buf;
790 int ret;
791
792 while (len) {
793 smbus_data.byte = *data;
794 ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
795 I2C_SMBUS_WRITE, dev_addr,
796 I2C_SMBUS_BYTE_DATA, &smbus_data);
797 if (ret)
798 return ret;
799
800 len--;
801 data++;
802 dev_addr++;
803 }
804
805 return data - (u8 *)buf;
806 }
807
sfp_i2c_configure(struct sfp * sfp,struct i2c_adapter * i2c)808 static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
809 {
810 sfp->i2c = i2c;
811
812 if (i2c_check_functionality(i2c, I2C_FUNC_I2C)) {
813 sfp->read = sfp_i2c_read;
814 sfp->write = sfp_i2c_write;
815 sfp->i2c_max_block_size = SFP_EEPROM_BLOCK_SIZE;
816 } else if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_BYTE_DATA)) {
817 sfp->read = sfp_smbus_byte_read;
818 sfp->write = sfp_smbus_byte_write;
819 sfp->i2c_max_block_size = 1;
820 } else {
821 sfp->i2c = NULL;
822 return -EINVAL;
823 }
824
825 sfp->i2c_block_size = sfp->i2c_max_block_size;
826 return 0;
827 }
828
sfp_i2c_mdiobus_create(struct sfp * sfp)829 static int sfp_i2c_mdiobus_create(struct sfp *sfp)
830 {
831 struct mii_bus *i2c_mii;
832 int ret;
833
834 i2c_mii = mdio_i2c_alloc(sfp->dev, sfp->i2c, sfp->mdio_protocol);
835 if (IS_ERR(i2c_mii))
836 return PTR_ERR(i2c_mii);
837
838 i2c_mii->name = "SFP I2C Bus";
839 i2c_mii->phy_mask = ~0;
840
841 ret = mdiobus_register(i2c_mii);
842 if (ret < 0) {
843 mdiobus_free(i2c_mii);
844 return ret;
845 }
846
847 sfp->i2c_mii = i2c_mii;
848
849 return 0;
850 }
851
sfp_i2c_mdiobus_destroy(struct sfp * sfp)852 static void sfp_i2c_mdiobus_destroy(struct sfp *sfp)
853 {
854 mdiobus_unregister(sfp->i2c_mii);
855 sfp->i2c_mii = NULL;
856 }
857
858 /* Interface */
sfp_read(struct sfp * sfp,bool a2,u8 addr,void * buf,size_t len)859 static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
860 {
861 return sfp->read(sfp, a2, addr, buf, len);
862 }
863
sfp_write(struct sfp * sfp,bool a2,u8 addr,void * buf,size_t len)864 static int sfp_write(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
865 {
866 return sfp->write(sfp, a2, addr, buf, len);
867 }
868
sfp_modify_u8(struct sfp * sfp,bool a2,u8 addr,u8 mask,u8 val)869 static int sfp_modify_u8(struct sfp *sfp, bool a2, u8 addr, u8 mask, u8 val)
870 {
871 int ret;
872 u8 old, v;
873
874 ret = sfp_read(sfp, a2, addr, &old, sizeof(old));
875 if (ret != sizeof(old))
876 return ret;
877
878 v = (old & ~mask) | (val & mask);
879 if (v == old)
880 return sizeof(v);
881
882 return sfp_write(sfp, a2, addr, &v, sizeof(v));
883 }
884
sfp_soft_get_state(struct sfp * sfp)885 static unsigned int sfp_soft_get_state(struct sfp *sfp)
886 {
887 unsigned int state = 0;
888 u8 status;
889 int ret;
890
891 ret = sfp_read(sfp, true, SFP_STATUS, &status, sizeof(status));
892 if (ret == sizeof(status)) {
893 if (status & SFP_STATUS_RX_LOS)
894 state |= SFP_F_LOS;
895 if (status & SFP_STATUS_TX_FAULT)
896 state |= SFP_F_TX_FAULT;
897 } else {
898 dev_err_ratelimited(sfp->dev,
899 "failed to read SFP soft status: %pe\n",
900 ERR_PTR(ret));
901 /* Preserve the current state */
902 state = sfp->state;
903 }
904
905 return state & sfp->state_soft_mask;
906 }
907
sfp_soft_set_state(struct sfp * sfp,unsigned int state,unsigned int soft)908 static void sfp_soft_set_state(struct sfp *sfp, unsigned int state,
909 unsigned int soft)
910 {
911 u8 mask = 0;
912 u8 val = 0;
913
914 if (soft & SFP_F_TX_DISABLE)
915 mask |= SFP_STATUS_TX_DISABLE_FORCE;
916 if (state & SFP_F_TX_DISABLE)
917 val |= SFP_STATUS_TX_DISABLE_FORCE;
918
919 if (soft & SFP_F_RS0)
920 mask |= SFP_STATUS_RS0_SELECT;
921 if (state & SFP_F_RS0)
922 val |= SFP_STATUS_RS0_SELECT;
923
924 if (mask)
925 sfp_modify_u8(sfp, true, SFP_STATUS, mask, val);
926
927 val = mask = 0;
928 if (soft & SFP_F_RS1)
929 mask |= SFP_EXT_STATUS_RS1_SELECT;
930 if (state & SFP_F_RS1)
931 val |= SFP_EXT_STATUS_RS1_SELECT;
932
933 if (mask)
934 sfp_modify_u8(sfp, true, SFP_EXT_STATUS, mask, val);
935 }
936
sfp_soft_start_poll(struct sfp * sfp)937 static void sfp_soft_start_poll(struct sfp *sfp)
938 {
939 const struct sfp_eeprom_id *id = &sfp->id;
940 unsigned int mask = 0;
941
942 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_DISABLE)
943 mask |= SFP_F_TX_DISABLE;
944 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_FAULT)
945 mask |= SFP_F_TX_FAULT;
946 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_RX_LOS)
947 mask |= SFP_F_LOS;
948 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_RATE_SELECT)
949 mask |= sfp->rs_state_mask;
950
951 mutex_lock(&sfp->st_mutex);
952 // Poll the soft state for hardware pins we want to ignore
953 sfp->state_soft_mask = ~sfp->state_hw_mask & ~sfp->state_ignore_mask &
954 mask;
955
956 if (sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT) &&
957 !sfp->need_poll)
958 sfp_schedule_poll(sfp);
959 mutex_unlock(&sfp->st_mutex);
960 }
961
sfp_soft_stop_poll(struct sfp * sfp)962 static void sfp_soft_stop_poll(struct sfp *sfp)
963 {
964 mutex_lock(&sfp->st_mutex);
965 sfp->state_soft_mask = 0;
966 mutex_unlock(&sfp->st_mutex);
967 }
968
969 /* sfp_get_state() - must be called with st_mutex held, or in the
970 * initialisation path.
971 */
sfp_get_state(struct sfp * sfp)972 static unsigned int sfp_get_state(struct sfp *sfp)
973 {
974 unsigned int soft = sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT);
975 unsigned int state;
976
977 state = sfp->get_state(sfp) & sfp->state_hw_mask;
978 if (state & SFP_F_PRESENT && soft)
979 state |= sfp_soft_get_state(sfp);
980
981 return state;
982 }
983
984 /* sfp_set_state() - must be called with st_mutex held, or in the
985 * initialisation path.
986 */
sfp_set_state(struct sfp * sfp,unsigned int state)987 static void sfp_set_state(struct sfp *sfp, unsigned int state)
988 {
989 unsigned int soft;
990
991 sfp->set_state(sfp, state);
992
993 soft = sfp->state_soft_mask & SFP_F_OUTPUTS;
994 if (state & SFP_F_PRESENT && soft)
995 sfp_soft_set_state(sfp, state, soft);
996 }
997
sfp_mod_state(struct sfp * sfp,unsigned int mask,unsigned int set)998 static void sfp_mod_state(struct sfp *sfp, unsigned int mask, unsigned int set)
999 {
1000 mutex_lock(&sfp->st_mutex);
1001 sfp->state = (sfp->state & ~mask) | set;
1002 sfp_set_state(sfp, sfp->state);
1003 mutex_unlock(&sfp->st_mutex);
1004 }
1005
sfp_check(void * buf,size_t len)1006 static unsigned int sfp_check(void *buf, size_t len)
1007 {
1008 u8 *p, check;
1009
1010 for (p = buf, check = 0; len; p++, len--)
1011 check += *p;
1012
1013 return check;
1014 }
1015
1016 /* hwmon */
1017 #if IS_ENABLED(CONFIG_HWMON)
sfp_hwmon_is_visible(const void * data,enum hwmon_sensor_types type,u32 attr,int channel)1018 static umode_t sfp_hwmon_is_visible(const void *data,
1019 enum hwmon_sensor_types type,
1020 u32 attr, int channel)
1021 {
1022 const struct sfp *sfp = data;
1023
1024 switch (type) {
1025 case hwmon_temp:
1026 switch (attr) {
1027 case hwmon_temp_min_alarm:
1028 case hwmon_temp_max_alarm:
1029 case hwmon_temp_lcrit_alarm:
1030 case hwmon_temp_crit_alarm:
1031 case hwmon_temp_min:
1032 case hwmon_temp_max:
1033 case hwmon_temp_lcrit:
1034 case hwmon_temp_crit:
1035 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
1036 return 0;
1037 fallthrough;
1038 case hwmon_temp_input:
1039 case hwmon_temp_label:
1040 return 0444;
1041 default:
1042 return 0;
1043 }
1044 case hwmon_in:
1045 switch (attr) {
1046 case hwmon_in_min_alarm:
1047 case hwmon_in_max_alarm:
1048 case hwmon_in_lcrit_alarm:
1049 case hwmon_in_crit_alarm:
1050 case hwmon_in_min:
1051 case hwmon_in_max:
1052 case hwmon_in_lcrit:
1053 case hwmon_in_crit:
1054 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
1055 return 0;
1056 fallthrough;
1057 case hwmon_in_input:
1058 case hwmon_in_label:
1059 return 0444;
1060 default:
1061 return 0;
1062 }
1063 case hwmon_curr:
1064 switch (attr) {
1065 case hwmon_curr_min_alarm:
1066 case hwmon_curr_max_alarm:
1067 case hwmon_curr_lcrit_alarm:
1068 case hwmon_curr_crit_alarm:
1069 case hwmon_curr_min:
1070 case hwmon_curr_max:
1071 case hwmon_curr_lcrit:
1072 case hwmon_curr_crit:
1073 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
1074 return 0;
1075 fallthrough;
1076 case hwmon_curr_input:
1077 case hwmon_curr_label:
1078 return 0444;
1079 default:
1080 return 0;
1081 }
1082 case hwmon_power:
1083 /* External calibration of receive power requires
1084 * floating point arithmetic. Doing that in the kernel
1085 * is not easy, so just skip it. If the module does
1086 * not require external calibration, we can however
1087 * show receiver power, since FP is then not needed.
1088 */
1089 if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL &&
1090 channel == 1)
1091 return 0;
1092 switch (attr) {
1093 case hwmon_power_min_alarm:
1094 case hwmon_power_max_alarm:
1095 case hwmon_power_lcrit_alarm:
1096 case hwmon_power_crit_alarm:
1097 case hwmon_power_min:
1098 case hwmon_power_max:
1099 case hwmon_power_lcrit:
1100 case hwmon_power_crit:
1101 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
1102 return 0;
1103 fallthrough;
1104 case hwmon_power_input:
1105 case hwmon_power_label:
1106 return 0444;
1107 default:
1108 return 0;
1109 }
1110 default:
1111 return 0;
1112 }
1113 }
1114
sfp_hwmon_read_sensor(struct sfp * sfp,int reg,long * value)1115 static int sfp_hwmon_read_sensor(struct sfp *sfp, int reg, long *value)
1116 {
1117 __be16 val;
1118 int err;
1119
1120 err = sfp_read(sfp, true, reg, &val, sizeof(val));
1121 if (err < 0)
1122 return err;
1123
1124 *value = be16_to_cpu(val);
1125
1126 return 0;
1127 }
1128
sfp_hwmon_to_rx_power(long * value)1129 static void sfp_hwmon_to_rx_power(long *value)
1130 {
1131 *value = DIV_ROUND_CLOSEST(*value, 10);
1132 }
1133
sfp_hwmon_calibrate(struct sfp * sfp,unsigned int slope,int offset,long * value)1134 static void sfp_hwmon_calibrate(struct sfp *sfp, unsigned int slope, int offset,
1135 long *value)
1136 {
1137 if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL)
1138 *value = DIV_ROUND_CLOSEST(*value * slope, 256) + offset;
1139 }
1140
sfp_hwmon_calibrate_temp(struct sfp * sfp,long * value)1141 static void sfp_hwmon_calibrate_temp(struct sfp *sfp, long *value)
1142 {
1143 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_t_slope),
1144 be16_to_cpu(sfp->diag.cal_t_offset), value);
1145
1146 if (*value >= 0x8000)
1147 *value -= 0x10000;
1148
1149 *value = DIV_ROUND_CLOSEST(*value * 1000, 256);
1150 }
1151
sfp_hwmon_calibrate_vcc(struct sfp * sfp,long * value)1152 static void sfp_hwmon_calibrate_vcc(struct sfp *sfp, long *value)
1153 {
1154 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_v_slope),
1155 be16_to_cpu(sfp->diag.cal_v_offset), value);
1156
1157 *value = DIV_ROUND_CLOSEST(*value, 10);
1158 }
1159
sfp_hwmon_calibrate_bias(struct sfp * sfp,long * value)1160 static void sfp_hwmon_calibrate_bias(struct sfp *sfp, long *value)
1161 {
1162 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txi_slope),
1163 be16_to_cpu(sfp->diag.cal_txi_offset), value);
1164
1165 *value = DIV_ROUND_CLOSEST(*value, 500);
1166 }
1167
sfp_hwmon_calibrate_tx_power(struct sfp * sfp,long * value)1168 static void sfp_hwmon_calibrate_tx_power(struct sfp *sfp, long *value)
1169 {
1170 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txpwr_slope),
1171 be16_to_cpu(sfp->diag.cal_txpwr_offset), value);
1172
1173 *value = DIV_ROUND_CLOSEST(*value, 10);
1174 }
1175
sfp_hwmon_read_temp(struct sfp * sfp,int reg,long * value)1176 static int sfp_hwmon_read_temp(struct sfp *sfp, int reg, long *value)
1177 {
1178 int err;
1179
1180 err = sfp_hwmon_read_sensor(sfp, reg, value);
1181 if (err < 0)
1182 return err;
1183
1184 sfp_hwmon_calibrate_temp(sfp, value);
1185
1186 return 0;
1187 }
1188
sfp_hwmon_read_vcc(struct sfp * sfp,int reg,long * value)1189 static int sfp_hwmon_read_vcc(struct sfp *sfp, int reg, long *value)
1190 {
1191 int err;
1192
1193 err = sfp_hwmon_read_sensor(sfp, reg, value);
1194 if (err < 0)
1195 return err;
1196
1197 sfp_hwmon_calibrate_vcc(sfp, value);
1198
1199 return 0;
1200 }
1201
sfp_hwmon_read_bias(struct sfp * sfp,int reg,long * value)1202 static int sfp_hwmon_read_bias(struct sfp *sfp, int reg, long *value)
1203 {
1204 int err;
1205
1206 err = sfp_hwmon_read_sensor(sfp, reg, value);
1207 if (err < 0)
1208 return err;
1209
1210 sfp_hwmon_calibrate_bias(sfp, value);
1211
1212 return 0;
1213 }
1214
sfp_hwmon_read_tx_power(struct sfp * sfp,int reg,long * value)1215 static int sfp_hwmon_read_tx_power(struct sfp *sfp, int reg, long *value)
1216 {
1217 int err;
1218
1219 err = sfp_hwmon_read_sensor(sfp, reg, value);
1220 if (err < 0)
1221 return err;
1222
1223 sfp_hwmon_calibrate_tx_power(sfp, value);
1224
1225 return 0;
1226 }
1227
sfp_hwmon_read_rx_power(struct sfp * sfp,int reg,long * value)1228 static int sfp_hwmon_read_rx_power(struct sfp *sfp, int reg, long *value)
1229 {
1230 int err;
1231
1232 err = sfp_hwmon_read_sensor(sfp, reg, value);
1233 if (err < 0)
1234 return err;
1235
1236 sfp_hwmon_to_rx_power(value);
1237
1238 return 0;
1239 }
1240
sfp_hwmon_temp(struct sfp * sfp,u32 attr,long * value)1241 static int sfp_hwmon_temp(struct sfp *sfp, u32 attr, long *value)
1242 {
1243 u8 status;
1244 int err;
1245
1246 switch (attr) {
1247 case hwmon_temp_input:
1248 return sfp_hwmon_read_temp(sfp, SFP_TEMP, value);
1249
1250 case hwmon_temp_lcrit:
1251 *value = be16_to_cpu(sfp->diag.temp_low_alarm);
1252 sfp_hwmon_calibrate_temp(sfp, value);
1253 return 0;
1254
1255 case hwmon_temp_min:
1256 *value = be16_to_cpu(sfp->diag.temp_low_warn);
1257 sfp_hwmon_calibrate_temp(sfp, value);
1258 return 0;
1259 case hwmon_temp_max:
1260 *value = be16_to_cpu(sfp->diag.temp_high_warn);
1261 sfp_hwmon_calibrate_temp(sfp, value);
1262 return 0;
1263
1264 case hwmon_temp_crit:
1265 *value = be16_to_cpu(sfp->diag.temp_high_alarm);
1266 sfp_hwmon_calibrate_temp(sfp, value);
1267 return 0;
1268
1269 case hwmon_temp_lcrit_alarm:
1270 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
1271 if (err < 0)
1272 return err;
1273
1274 *value = !!(status & SFP_ALARM0_TEMP_LOW);
1275 return 0;
1276
1277 case hwmon_temp_min_alarm:
1278 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
1279 if (err < 0)
1280 return err;
1281
1282 *value = !!(status & SFP_WARN0_TEMP_LOW);
1283 return 0;
1284
1285 case hwmon_temp_max_alarm:
1286 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
1287 if (err < 0)
1288 return err;
1289
1290 *value = !!(status & SFP_WARN0_TEMP_HIGH);
1291 return 0;
1292
1293 case hwmon_temp_crit_alarm:
1294 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
1295 if (err < 0)
1296 return err;
1297
1298 *value = !!(status & SFP_ALARM0_TEMP_HIGH);
1299 return 0;
1300 default:
1301 return -EOPNOTSUPP;
1302 }
1303
1304 return -EOPNOTSUPP;
1305 }
1306
sfp_hwmon_vcc(struct sfp * sfp,u32 attr,long * value)1307 static int sfp_hwmon_vcc(struct sfp *sfp, u32 attr, long *value)
1308 {
1309 u8 status;
1310 int err;
1311
1312 switch (attr) {
1313 case hwmon_in_input:
1314 return sfp_hwmon_read_vcc(sfp, SFP_VCC, value);
1315
1316 case hwmon_in_lcrit:
1317 *value = be16_to_cpu(sfp->diag.volt_low_alarm);
1318 sfp_hwmon_calibrate_vcc(sfp, value);
1319 return 0;
1320
1321 case hwmon_in_min:
1322 *value = be16_to_cpu(sfp->diag.volt_low_warn);
1323 sfp_hwmon_calibrate_vcc(sfp, value);
1324 return 0;
1325
1326 case hwmon_in_max:
1327 *value = be16_to_cpu(sfp->diag.volt_high_warn);
1328 sfp_hwmon_calibrate_vcc(sfp, value);
1329 return 0;
1330
1331 case hwmon_in_crit:
1332 *value = be16_to_cpu(sfp->diag.volt_high_alarm);
1333 sfp_hwmon_calibrate_vcc(sfp, value);
1334 return 0;
1335
1336 case hwmon_in_lcrit_alarm:
1337 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
1338 if (err < 0)
1339 return err;
1340
1341 *value = !!(status & SFP_ALARM0_VCC_LOW);
1342 return 0;
1343
1344 case hwmon_in_min_alarm:
1345 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
1346 if (err < 0)
1347 return err;
1348
1349 *value = !!(status & SFP_WARN0_VCC_LOW);
1350 return 0;
1351
1352 case hwmon_in_max_alarm:
1353 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
1354 if (err < 0)
1355 return err;
1356
1357 *value = !!(status & SFP_WARN0_VCC_HIGH);
1358 return 0;
1359
1360 case hwmon_in_crit_alarm:
1361 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
1362 if (err < 0)
1363 return err;
1364
1365 *value = !!(status & SFP_ALARM0_VCC_HIGH);
1366 return 0;
1367 default:
1368 return -EOPNOTSUPP;
1369 }
1370
1371 return -EOPNOTSUPP;
1372 }
1373
sfp_hwmon_bias(struct sfp * sfp,u32 attr,long * value)1374 static int sfp_hwmon_bias(struct sfp *sfp, u32 attr, long *value)
1375 {
1376 u8 status;
1377 int err;
1378
1379 switch (attr) {
1380 case hwmon_curr_input:
1381 return sfp_hwmon_read_bias(sfp, SFP_TX_BIAS, value);
1382
1383 case hwmon_curr_lcrit:
1384 *value = be16_to_cpu(sfp->diag.bias_low_alarm);
1385 sfp_hwmon_calibrate_bias(sfp, value);
1386 return 0;
1387
1388 case hwmon_curr_min:
1389 *value = be16_to_cpu(sfp->diag.bias_low_warn);
1390 sfp_hwmon_calibrate_bias(sfp, value);
1391 return 0;
1392
1393 case hwmon_curr_max:
1394 *value = be16_to_cpu(sfp->diag.bias_high_warn);
1395 sfp_hwmon_calibrate_bias(sfp, value);
1396 return 0;
1397
1398 case hwmon_curr_crit:
1399 *value = be16_to_cpu(sfp->diag.bias_high_alarm);
1400 sfp_hwmon_calibrate_bias(sfp, value);
1401 return 0;
1402
1403 case hwmon_curr_lcrit_alarm:
1404 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
1405 if (err < 0)
1406 return err;
1407
1408 *value = !!(status & SFP_ALARM0_TX_BIAS_LOW);
1409 return 0;
1410
1411 case hwmon_curr_min_alarm:
1412 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
1413 if (err < 0)
1414 return err;
1415
1416 *value = !!(status & SFP_WARN0_TX_BIAS_LOW);
1417 return 0;
1418
1419 case hwmon_curr_max_alarm:
1420 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
1421 if (err < 0)
1422 return err;
1423
1424 *value = !!(status & SFP_WARN0_TX_BIAS_HIGH);
1425 return 0;
1426
1427 case hwmon_curr_crit_alarm:
1428 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
1429 if (err < 0)
1430 return err;
1431
1432 *value = !!(status & SFP_ALARM0_TX_BIAS_HIGH);
1433 return 0;
1434 default:
1435 return -EOPNOTSUPP;
1436 }
1437
1438 return -EOPNOTSUPP;
1439 }
1440
sfp_hwmon_tx_power(struct sfp * sfp,u32 attr,long * value)1441 static int sfp_hwmon_tx_power(struct sfp *sfp, u32 attr, long *value)
1442 {
1443 u8 status;
1444 int err;
1445
1446 switch (attr) {
1447 case hwmon_power_input:
1448 return sfp_hwmon_read_tx_power(sfp, SFP_TX_POWER, value);
1449
1450 case hwmon_power_lcrit:
1451 *value = be16_to_cpu(sfp->diag.txpwr_low_alarm);
1452 sfp_hwmon_calibrate_tx_power(sfp, value);
1453 return 0;
1454
1455 case hwmon_power_min:
1456 *value = be16_to_cpu(sfp->diag.txpwr_low_warn);
1457 sfp_hwmon_calibrate_tx_power(sfp, value);
1458 return 0;
1459
1460 case hwmon_power_max:
1461 *value = be16_to_cpu(sfp->diag.txpwr_high_warn);
1462 sfp_hwmon_calibrate_tx_power(sfp, value);
1463 return 0;
1464
1465 case hwmon_power_crit:
1466 *value = be16_to_cpu(sfp->diag.txpwr_high_alarm);
1467 sfp_hwmon_calibrate_tx_power(sfp, value);
1468 return 0;
1469
1470 case hwmon_power_lcrit_alarm:
1471 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
1472 if (err < 0)
1473 return err;
1474
1475 *value = !!(status & SFP_ALARM0_TXPWR_LOW);
1476 return 0;
1477
1478 case hwmon_power_min_alarm:
1479 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
1480 if (err < 0)
1481 return err;
1482
1483 *value = !!(status & SFP_WARN0_TXPWR_LOW);
1484 return 0;
1485
1486 case hwmon_power_max_alarm:
1487 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
1488 if (err < 0)
1489 return err;
1490
1491 *value = !!(status & SFP_WARN0_TXPWR_HIGH);
1492 return 0;
1493
1494 case hwmon_power_crit_alarm:
1495 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
1496 if (err < 0)
1497 return err;
1498
1499 *value = !!(status & SFP_ALARM0_TXPWR_HIGH);
1500 return 0;
1501 default:
1502 return -EOPNOTSUPP;
1503 }
1504
1505 return -EOPNOTSUPP;
1506 }
1507
sfp_hwmon_rx_power(struct sfp * sfp,u32 attr,long * value)1508 static int sfp_hwmon_rx_power(struct sfp *sfp, u32 attr, long *value)
1509 {
1510 u8 status;
1511 int err;
1512
1513 switch (attr) {
1514 case hwmon_power_input:
1515 return sfp_hwmon_read_rx_power(sfp, SFP_RX_POWER, value);
1516
1517 case hwmon_power_lcrit:
1518 *value = be16_to_cpu(sfp->diag.rxpwr_low_alarm);
1519 sfp_hwmon_to_rx_power(value);
1520 return 0;
1521
1522 case hwmon_power_min:
1523 *value = be16_to_cpu(sfp->diag.rxpwr_low_warn);
1524 sfp_hwmon_to_rx_power(value);
1525 return 0;
1526
1527 case hwmon_power_max:
1528 *value = be16_to_cpu(sfp->diag.rxpwr_high_warn);
1529 sfp_hwmon_to_rx_power(value);
1530 return 0;
1531
1532 case hwmon_power_crit:
1533 *value = be16_to_cpu(sfp->diag.rxpwr_high_alarm);
1534 sfp_hwmon_to_rx_power(value);
1535 return 0;
1536
1537 case hwmon_power_lcrit_alarm:
1538 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
1539 if (err < 0)
1540 return err;
1541
1542 *value = !!(status & SFP_ALARM1_RXPWR_LOW);
1543 return 0;
1544
1545 case hwmon_power_min_alarm:
1546 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
1547 if (err < 0)
1548 return err;
1549
1550 *value = !!(status & SFP_WARN1_RXPWR_LOW);
1551 return 0;
1552
1553 case hwmon_power_max_alarm:
1554 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
1555 if (err < 0)
1556 return err;
1557
1558 *value = !!(status & SFP_WARN1_RXPWR_HIGH);
1559 return 0;
1560
1561 case hwmon_power_crit_alarm:
1562 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
1563 if (err < 0)
1564 return err;
1565
1566 *value = !!(status & SFP_ALARM1_RXPWR_HIGH);
1567 return 0;
1568 default:
1569 return -EOPNOTSUPP;
1570 }
1571
1572 return -EOPNOTSUPP;
1573 }
1574
sfp_hwmon_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * value)1575 static int sfp_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
1576 u32 attr, int channel, long *value)
1577 {
1578 struct sfp *sfp = dev_get_drvdata(dev);
1579
1580 switch (type) {
1581 case hwmon_temp:
1582 return sfp_hwmon_temp(sfp, attr, value);
1583 case hwmon_in:
1584 return sfp_hwmon_vcc(sfp, attr, value);
1585 case hwmon_curr:
1586 return sfp_hwmon_bias(sfp, attr, value);
1587 case hwmon_power:
1588 switch (channel) {
1589 case 0:
1590 return sfp_hwmon_tx_power(sfp, attr, value);
1591 case 1:
1592 return sfp_hwmon_rx_power(sfp, attr, value);
1593 default:
1594 return -EOPNOTSUPP;
1595 }
1596 default:
1597 return -EOPNOTSUPP;
1598 }
1599 }
1600
1601 static const char *const sfp_hwmon_power_labels[] = {
1602 "TX_power",
1603 "RX_power",
1604 };
1605
sfp_hwmon_read_string(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,const char ** str)1606 static int sfp_hwmon_read_string(struct device *dev,
1607 enum hwmon_sensor_types type,
1608 u32 attr, int channel, const char **str)
1609 {
1610 switch (type) {
1611 case hwmon_curr:
1612 switch (attr) {
1613 case hwmon_curr_label:
1614 *str = "bias";
1615 return 0;
1616 default:
1617 return -EOPNOTSUPP;
1618 }
1619 break;
1620 case hwmon_temp:
1621 switch (attr) {
1622 case hwmon_temp_label:
1623 *str = "temperature";
1624 return 0;
1625 default:
1626 return -EOPNOTSUPP;
1627 }
1628 break;
1629 case hwmon_in:
1630 switch (attr) {
1631 case hwmon_in_label:
1632 *str = "VCC";
1633 return 0;
1634 default:
1635 return -EOPNOTSUPP;
1636 }
1637 break;
1638 case hwmon_power:
1639 switch (attr) {
1640 case hwmon_power_label:
1641 *str = sfp_hwmon_power_labels[channel];
1642 return 0;
1643 default:
1644 return -EOPNOTSUPP;
1645 }
1646 break;
1647 default:
1648 return -EOPNOTSUPP;
1649 }
1650
1651 return -EOPNOTSUPP;
1652 }
1653
1654 static const struct hwmon_ops sfp_hwmon_ops = {
1655 .is_visible = sfp_hwmon_is_visible,
1656 .read = sfp_hwmon_read,
1657 .read_string = sfp_hwmon_read_string,
1658 };
1659
1660 static const struct hwmon_channel_info * const sfp_hwmon_info[] = {
1661 HWMON_CHANNEL_INFO(chip,
1662 HWMON_C_REGISTER_TZ),
1663 HWMON_CHANNEL_INFO(in,
1664 HWMON_I_INPUT |
1665 HWMON_I_MAX | HWMON_I_MIN |
1666 HWMON_I_MAX_ALARM | HWMON_I_MIN_ALARM |
1667 HWMON_I_CRIT | HWMON_I_LCRIT |
1668 HWMON_I_CRIT_ALARM | HWMON_I_LCRIT_ALARM |
1669 HWMON_I_LABEL),
1670 HWMON_CHANNEL_INFO(temp,
1671 HWMON_T_INPUT |
1672 HWMON_T_MAX | HWMON_T_MIN |
1673 HWMON_T_MAX_ALARM | HWMON_T_MIN_ALARM |
1674 HWMON_T_CRIT | HWMON_T_LCRIT |
1675 HWMON_T_CRIT_ALARM | HWMON_T_LCRIT_ALARM |
1676 HWMON_T_LABEL),
1677 HWMON_CHANNEL_INFO(curr,
1678 HWMON_C_INPUT |
1679 HWMON_C_MAX | HWMON_C_MIN |
1680 HWMON_C_MAX_ALARM | HWMON_C_MIN_ALARM |
1681 HWMON_C_CRIT | HWMON_C_LCRIT |
1682 HWMON_C_CRIT_ALARM | HWMON_C_LCRIT_ALARM |
1683 HWMON_C_LABEL),
1684 HWMON_CHANNEL_INFO(power,
1685 /* Transmit power */
1686 HWMON_P_INPUT |
1687 HWMON_P_MAX | HWMON_P_MIN |
1688 HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
1689 HWMON_P_CRIT | HWMON_P_LCRIT |
1690 HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM |
1691 HWMON_P_LABEL,
1692 /* Receive power */
1693 HWMON_P_INPUT |
1694 HWMON_P_MAX | HWMON_P_MIN |
1695 HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
1696 HWMON_P_CRIT | HWMON_P_LCRIT |
1697 HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM |
1698 HWMON_P_LABEL),
1699 NULL,
1700 };
1701
1702 static const struct hwmon_chip_info sfp_hwmon_chip_info = {
1703 .ops = &sfp_hwmon_ops,
1704 .info = sfp_hwmon_info,
1705 };
1706
sfp_hwmon_probe(struct work_struct * work)1707 static void sfp_hwmon_probe(struct work_struct *work)
1708 {
1709 struct sfp *sfp = container_of(work, struct sfp, hwmon_probe.work);
1710 int err;
1711
1712 /* hwmon interface needs to access 16bit registers in atomic way to
1713 * guarantee coherency of the diagnostic monitoring data. If it is not
1714 * possible to guarantee coherency because EEPROM is broken in such way
1715 * that does not support atomic 16bit read operation then we have to
1716 * skip registration of hwmon device.
1717 */
1718 if (sfp->i2c_block_size < 2) {
1719 dev_info(sfp->dev,
1720 "skipping hwmon device registration\n");
1721 dev_info(sfp->dev,
1722 "diagnostic EEPROM area cannot be read atomically to guarantee data coherency\n");
1723 return;
1724 }
1725
1726 err = sfp_read(sfp, true, 0, &sfp->diag, sizeof(sfp->diag));
1727 if (err < 0) {
1728 if (sfp->hwmon_tries--) {
1729 mod_delayed_work(system_percpu_wq, &sfp->hwmon_probe,
1730 T_PROBE_RETRY_SLOW);
1731 } else {
1732 dev_warn(sfp->dev, "hwmon probe failed: %pe\n",
1733 ERR_PTR(err));
1734 }
1735 return;
1736 }
1737
1738 sfp->hwmon_name = hwmon_sanitize_name(dev_name(sfp->dev));
1739 if (IS_ERR(sfp->hwmon_name)) {
1740 dev_err(sfp->dev, "out of memory for hwmon name\n");
1741 return;
1742 }
1743
1744 sfp->hwmon_dev = hwmon_device_register_with_info(sfp->dev,
1745 sfp->hwmon_name, sfp,
1746 &sfp_hwmon_chip_info,
1747 NULL);
1748 if (IS_ERR(sfp->hwmon_dev))
1749 dev_err(sfp->dev, "failed to register hwmon device: %ld\n",
1750 PTR_ERR(sfp->hwmon_dev));
1751 }
1752
sfp_hwmon_insert(struct sfp * sfp)1753 static int sfp_hwmon_insert(struct sfp *sfp)
1754 {
1755 if (sfp->have_a2 && sfp->id.ext.diagmon & SFP_DIAGMON_DDM) {
1756 mod_delayed_work(system_percpu_wq, &sfp->hwmon_probe, 1);
1757 sfp->hwmon_tries = R_PROBE_RETRY_SLOW;
1758 }
1759
1760 return 0;
1761 }
1762
sfp_hwmon_remove(struct sfp * sfp)1763 static void sfp_hwmon_remove(struct sfp *sfp)
1764 {
1765 cancel_delayed_work_sync(&sfp->hwmon_probe);
1766 if (!IS_ERR_OR_NULL(sfp->hwmon_dev)) {
1767 hwmon_device_unregister(sfp->hwmon_dev);
1768 sfp->hwmon_dev = NULL;
1769 kfree(sfp->hwmon_name);
1770 }
1771 }
1772
sfp_hwmon_init(struct sfp * sfp)1773 static int sfp_hwmon_init(struct sfp *sfp)
1774 {
1775 INIT_DELAYED_WORK(&sfp->hwmon_probe, sfp_hwmon_probe);
1776
1777 return 0;
1778 }
1779
sfp_hwmon_exit(struct sfp * sfp)1780 static void sfp_hwmon_exit(struct sfp *sfp)
1781 {
1782 cancel_delayed_work_sync(&sfp->hwmon_probe);
1783 }
1784 #else
sfp_hwmon_insert(struct sfp * sfp)1785 static int sfp_hwmon_insert(struct sfp *sfp)
1786 {
1787 return 0;
1788 }
1789
sfp_hwmon_remove(struct sfp * sfp)1790 static void sfp_hwmon_remove(struct sfp *sfp)
1791 {
1792 }
1793
sfp_hwmon_init(struct sfp * sfp)1794 static int sfp_hwmon_init(struct sfp *sfp)
1795 {
1796 return 0;
1797 }
1798
sfp_hwmon_exit(struct sfp * sfp)1799 static void sfp_hwmon_exit(struct sfp *sfp)
1800 {
1801 }
1802 #endif
1803
1804 /* Helpers */
sfp_module_tx_disable(struct sfp * sfp)1805 static void sfp_module_tx_disable(struct sfp *sfp)
1806 {
1807 dev_dbg(sfp->dev, "tx disable %u -> %u\n",
1808 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1);
1809 sfp_mod_state(sfp, SFP_F_TX_DISABLE, SFP_F_TX_DISABLE);
1810 }
1811
sfp_module_tx_enable(struct sfp * sfp)1812 static void sfp_module_tx_enable(struct sfp *sfp)
1813 {
1814 dev_dbg(sfp->dev, "tx disable %u -> %u\n",
1815 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0);
1816 sfp_mod_state(sfp, SFP_F_TX_DISABLE, 0);
1817 }
1818
1819 #if IS_ENABLED(CONFIG_DEBUG_FS)
sfp_debug_state_show(struct seq_file * s,void * data)1820 static int sfp_debug_state_show(struct seq_file *s, void *data)
1821 {
1822 struct sfp *sfp = s->private;
1823
1824 seq_printf(s, "Module state: %s\n",
1825 mod_state_to_str(sfp->sm_mod_state));
1826 seq_printf(s, "Module probe attempts: %d %d\n",
1827 R_PROBE_RETRY_INIT - sfp->sm_mod_tries_init,
1828 R_PROBE_RETRY_SLOW - sfp->sm_mod_tries);
1829 seq_printf(s, "Device state: %s\n",
1830 dev_state_to_str(sfp->sm_dev_state));
1831 seq_printf(s, "Main state: %s\n",
1832 sm_state_to_str(sfp->sm_state));
1833 seq_printf(s, "Fault recovery remaining retries: %d\n",
1834 sfp->sm_fault_retries);
1835 seq_printf(s, "PHY probe remaining retries: %d\n",
1836 sfp->sm_phy_retries);
1837 seq_printf(s, "Signalling rate: %u kBd\n", sfp->rate_kbd);
1838 seq_printf(s, "Rate select threshold: %u kBd\n",
1839 sfp->rs_threshold_kbd);
1840 seq_printf(s, "moddef0: %d\n", !!(sfp->state & SFP_F_PRESENT));
1841 seq_printf(s, "rx_los: %d\n", !!(sfp->state & SFP_F_LOS));
1842 seq_printf(s, "tx_fault: %d\n", !!(sfp->state & SFP_F_TX_FAULT));
1843 seq_printf(s, "tx_disable: %d\n", !!(sfp->state & SFP_F_TX_DISABLE));
1844 seq_printf(s, "rs0: %d\n", !!(sfp->state & SFP_F_RS0));
1845 seq_printf(s, "rs1: %d\n", !!(sfp->state & SFP_F_RS1));
1846 return 0;
1847 }
1848 DEFINE_SHOW_ATTRIBUTE(sfp_debug_state);
1849
sfp_debugfs_init(struct sfp * sfp)1850 static void sfp_debugfs_init(struct sfp *sfp)
1851 {
1852 sfp->debugfs_dir = debugfs_create_dir(dev_name(sfp->dev), NULL);
1853
1854 debugfs_create_file("state", 0600, sfp->debugfs_dir, sfp,
1855 &sfp_debug_state_fops);
1856 }
1857
sfp_debugfs_exit(struct sfp * sfp)1858 static void sfp_debugfs_exit(struct sfp *sfp)
1859 {
1860 debugfs_remove_recursive(sfp->debugfs_dir);
1861 }
1862 #else
sfp_debugfs_init(struct sfp * sfp)1863 static void sfp_debugfs_init(struct sfp *sfp)
1864 {
1865 }
1866
sfp_debugfs_exit(struct sfp * sfp)1867 static void sfp_debugfs_exit(struct sfp *sfp)
1868 {
1869 }
1870 #endif
1871
sfp_module_tx_fault_reset(struct sfp * sfp)1872 static void sfp_module_tx_fault_reset(struct sfp *sfp)
1873 {
1874 unsigned int state;
1875
1876 mutex_lock(&sfp->st_mutex);
1877 state = sfp->state;
1878 if (!(state & SFP_F_TX_DISABLE)) {
1879 sfp_set_state(sfp, state | SFP_F_TX_DISABLE);
1880
1881 udelay(T_RESET_US);
1882
1883 sfp_set_state(sfp, state);
1884 }
1885 mutex_unlock(&sfp->st_mutex);
1886 }
1887
1888 /* SFP state machine */
sfp_sm_set_timer(struct sfp * sfp,unsigned int timeout)1889 static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout)
1890 {
1891 if (timeout)
1892 mod_delayed_work(system_power_efficient_wq, &sfp->timeout,
1893 timeout);
1894 else
1895 cancel_delayed_work(&sfp->timeout);
1896 }
1897
sfp_sm_next(struct sfp * sfp,unsigned int state,unsigned int timeout)1898 static void sfp_sm_next(struct sfp *sfp, unsigned int state,
1899 unsigned int timeout)
1900 {
1901 sfp->sm_state = state;
1902 sfp_sm_set_timer(sfp, timeout);
1903 }
1904
sfp_sm_mod_next(struct sfp * sfp,unsigned int state,unsigned int timeout)1905 static void sfp_sm_mod_next(struct sfp *sfp, unsigned int state,
1906 unsigned int timeout)
1907 {
1908 sfp->sm_mod_state = state;
1909 sfp_sm_set_timer(sfp, timeout);
1910 }
1911
sfp_sm_phy_detach(struct sfp * sfp)1912 static void sfp_sm_phy_detach(struct sfp *sfp)
1913 {
1914 sfp_remove_phy(sfp->sfp_bus);
1915 phy_device_remove(sfp->mod_phy);
1916 phy_device_free(sfp->mod_phy);
1917 sfp->mod_phy = NULL;
1918 }
1919
sfp_sm_probe_phy(struct sfp * sfp,int addr,bool is_c45)1920 static int sfp_sm_probe_phy(struct sfp *sfp, int addr, bool is_c45)
1921 {
1922 struct phy_device *phy;
1923 int err;
1924
1925 phy = get_phy_device(sfp->i2c_mii, addr, is_c45);
1926 if (phy == ERR_PTR(-ENODEV))
1927 return PTR_ERR(phy);
1928 if (IS_ERR(phy)) {
1929 dev_err(sfp->dev, "mdiobus scan returned %pe\n", phy);
1930 return PTR_ERR(phy);
1931 }
1932
1933 /* Mark this PHY as being on a SFP module */
1934 phy->is_on_sfp_module = true;
1935
1936 err = phy_device_register(phy);
1937 if (err) {
1938 phy_device_free(phy);
1939 dev_err(sfp->dev, "phy_device_register failed: %pe\n",
1940 ERR_PTR(err));
1941 return err;
1942 }
1943
1944 err = sfp_add_phy(sfp->sfp_bus, phy);
1945 if (err) {
1946 phy_device_remove(phy);
1947 phy_device_free(phy);
1948 dev_err(sfp->dev, "sfp_add_phy failed: %pe\n", ERR_PTR(err));
1949 return err;
1950 }
1951
1952 sfp->mod_phy = phy;
1953
1954 return 0;
1955 }
1956
sfp_sm_link_up(struct sfp * sfp)1957 static void sfp_sm_link_up(struct sfp *sfp)
1958 {
1959 sfp_link_up(sfp->sfp_bus);
1960 sfp_sm_next(sfp, SFP_S_LINK_UP, 0);
1961 }
1962
sfp_sm_link_down(struct sfp * sfp)1963 static void sfp_sm_link_down(struct sfp *sfp)
1964 {
1965 sfp_link_down(sfp->sfp_bus);
1966 }
1967
sfp_sm_link_check_los(struct sfp * sfp)1968 static void sfp_sm_link_check_los(struct sfp *sfp)
1969 {
1970 const __be16 los_inverted = cpu_to_be16(SFP_OPTIONS_LOS_INVERTED);
1971 const __be16 los_normal = cpu_to_be16(SFP_OPTIONS_LOS_NORMAL);
1972 __be16 los_options = sfp->id.ext.options & (los_inverted | los_normal);
1973 bool los = false;
1974
1975 /* If neither SFP_OPTIONS_LOS_INVERTED nor SFP_OPTIONS_LOS_NORMAL
1976 * are set, we assume that no LOS signal is available. If both are
1977 * set, we assume LOS is not implemented (and is meaningless.)
1978 */
1979 if (los_options == los_inverted)
1980 los = !(sfp->state & SFP_F_LOS);
1981 else if (los_options == los_normal)
1982 los = !!(sfp->state & SFP_F_LOS);
1983
1984 if (los)
1985 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
1986 else
1987 sfp_sm_link_up(sfp);
1988 }
1989
sfp_los_event_active(struct sfp * sfp,unsigned int event)1990 static bool sfp_los_event_active(struct sfp *sfp, unsigned int event)
1991 {
1992 const __be16 los_inverted = cpu_to_be16(SFP_OPTIONS_LOS_INVERTED);
1993 const __be16 los_normal = cpu_to_be16(SFP_OPTIONS_LOS_NORMAL);
1994 __be16 los_options = sfp->id.ext.options & (los_inverted | los_normal);
1995
1996 return (los_options == los_inverted && event == SFP_E_LOS_LOW) ||
1997 (los_options == los_normal && event == SFP_E_LOS_HIGH);
1998 }
1999
sfp_los_event_inactive(struct sfp * sfp,unsigned int event)2000 static bool sfp_los_event_inactive(struct sfp *sfp, unsigned int event)
2001 {
2002 const __be16 los_inverted = cpu_to_be16(SFP_OPTIONS_LOS_INVERTED);
2003 const __be16 los_normal = cpu_to_be16(SFP_OPTIONS_LOS_NORMAL);
2004 __be16 los_options = sfp->id.ext.options & (los_inverted | los_normal);
2005
2006 return (los_options == los_inverted && event == SFP_E_LOS_HIGH) ||
2007 (los_options == los_normal && event == SFP_E_LOS_LOW);
2008 }
2009
sfp_sm_fault(struct sfp * sfp,unsigned int next_state,bool warn)2010 static void sfp_sm_fault(struct sfp *sfp, unsigned int next_state, bool warn)
2011 {
2012 if (sfp->sm_fault_retries && !--sfp->sm_fault_retries) {
2013 dev_err(sfp->dev,
2014 "module persistently indicates fault, disabling\n");
2015 sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0);
2016 } else {
2017 if (warn)
2018 dev_err(sfp->dev, "module transmit fault indicated\n");
2019
2020 sfp_sm_next(sfp, next_state, T_FAULT_RECOVER);
2021 }
2022 }
2023
sfp_sm_add_mdio_bus(struct sfp * sfp)2024 static int sfp_sm_add_mdio_bus(struct sfp *sfp)
2025 {
2026 if (sfp->mdio_protocol != MDIO_I2C_NONE)
2027 return sfp_i2c_mdiobus_create(sfp);
2028
2029 return 0;
2030 }
2031
2032 /* Probe a SFP for a PHY device if the module supports copper - the PHY
2033 * normally sits at I2C bus address 0x56, and may either be a clause 22
2034 * or clause 45 PHY.
2035 *
2036 * Clause 22 copper SFP modules normally operate in Cisco SGMII mode with
2037 * negotiation enabled, but some may be in 1000base-X - which is for the
2038 * PHY driver to determine.
2039 *
2040 * Clause 45 copper SFP+ modules (10G) appear to switch their interface
2041 * mode according to the negotiated line speed.
2042 */
sfp_sm_probe_for_phy(struct sfp * sfp)2043 static int sfp_sm_probe_for_phy(struct sfp *sfp)
2044 {
2045 int err = 0;
2046
2047 switch (sfp->mdio_protocol) {
2048 case MDIO_I2C_NONE:
2049 break;
2050
2051 case MDIO_I2C_MARVELL_C22:
2052 err = sfp_sm_probe_phy(sfp, SFP_PHY_ADDR, false);
2053 break;
2054
2055 case MDIO_I2C_C45:
2056 err = sfp_sm_probe_phy(sfp, SFP_PHY_ADDR, true);
2057 break;
2058
2059 case MDIO_I2C_ROLLBALL:
2060 err = sfp_sm_probe_phy(sfp, SFP_PHY_ADDR_ROLLBALL, true);
2061 break;
2062 }
2063
2064 return err;
2065 }
2066
sfp_module_parse_power(struct sfp * sfp)2067 static int sfp_module_parse_power(struct sfp *sfp)
2068 {
2069 u32 power_mW = 1000;
2070 bool supports_a2;
2071
2072 if (sfp->id.ext.sff8472_compliance >= SFP_SFF8472_COMPLIANCE_REV10_2 &&
2073 sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_POWER_DECL))
2074 power_mW = 1500;
2075 /* Added in Rev 11.9, but there is no compliance code for this */
2076 if (sfp->id.ext.sff8472_compliance >= SFP_SFF8472_COMPLIANCE_REV11_4 &&
2077 sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL))
2078 power_mW = 2000;
2079
2080 /* Power level 1 modules (max. 1W) are always supported. */
2081 if (power_mW <= 1000) {
2082 sfp->module_power_mW = power_mW;
2083 return 0;
2084 }
2085
2086 supports_a2 = sfp->id.ext.sff8472_compliance !=
2087 SFP_SFF8472_COMPLIANCE_NONE ||
2088 sfp->id.ext.diagmon & SFP_DIAGMON_DDM;
2089
2090 if (power_mW > sfp->max_power_mW) {
2091 /* Module power specification exceeds the allowed maximum. */
2092 if (!supports_a2) {
2093 /* The module appears not to implement bus address
2094 * 0xa2, so assume that the module powers up in the
2095 * indicated mode.
2096 */
2097 dev_err(sfp->dev,
2098 "Host does not support %u.%uW modules\n",
2099 power_mW / 1000, (power_mW / 100) % 10);
2100 return -EINVAL;
2101 } else {
2102 dev_warn(sfp->dev,
2103 "Host does not support %u.%uW modules, module left in power mode 1\n",
2104 power_mW / 1000, (power_mW / 100) % 10);
2105 return 0;
2106 }
2107 }
2108
2109 if (!supports_a2) {
2110 /* The module power level is below the host maximum and the
2111 * module appears not to implement bus address 0xa2, so assume
2112 * that the module powers up in the indicated mode.
2113 */
2114 return 0;
2115 }
2116
2117 /* If the module requires a higher power mode, but also requires
2118 * an address change sequence, warn the user that the module may
2119 * not be functional.
2120 */
2121 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE) {
2122 dev_warn(sfp->dev,
2123 "Address Change Sequence not supported but module requires %u.%uW, module may not be functional\n",
2124 power_mW / 1000, (power_mW / 100) % 10);
2125 return 0;
2126 }
2127
2128 sfp->module_power_mW = power_mW;
2129
2130 return 0;
2131 }
2132
sfp_sm_mod_hpower(struct sfp * sfp,bool enable)2133 static int sfp_sm_mod_hpower(struct sfp *sfp, bool enable)
2134 {
2135 int err;
2136
2137 err = sfp_modify_u8(sfp, true, SFP_EXT_STATUS,
2138 SFP_EXT_STATUS_PWRLVL_SELECT,
2139 enable ? SFP_EXT_STATUS_PWRLVL_SELECT : 0);
2140 if (err != sizeof(u8)) {
2141 dev_err(sfp->dev, "failed to %sable high power: %pe\n",
2142 enable ? "en" : "dis", ERR_PTR(err));
2143 return -EAGAIN;
2144 }
2145
2146 if (enable)
2147 dev_info(sfp->dev, "Module switched to %u.%uW power level\n",
2148 sfp->module_power_mW / 1000,
2149 (sfp->module_power_mW / 100) % 10);
2150
2151 return 0;
2152 }
2153
sfp_module_parse_rate_select(struct sfp * sfp)2154 static void sfp_module_parse_rate_select(struct sfp *sfp)
2155 {
2156 u8 rate_id;
2157
2158 sfp->rs_threshold_kbd = 0;
2159 sfp->rs_state_mask = 0;
2160
2161 if (!(sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_RATE_SELECT)))
2162 /* No support for RateSelect */
2163 return;
2164
2165 /* Default to INF-8074 RateSelect operation. The signalling threshold
2166 * rate is not well specified, so always select "Full Bandwidth", but
2167 * SFF-8079 reveals that it is understood that RS0 will be low for
2168 * 1.0625Gb/s and high for 2.125Gb/s. Choose a value half-way between.
2169 * This method exists prior to SFF-8472.
2170 */
2171 sfp->rs_state_mask = SFP_F_RS0;
2172 sfp->rs_threshold_kbd = 1594;
2173
2174 /* Parse the rate identifier, which is complicated due to history:
2175 * SFF-8472 rev 9.5 marks this field as reserved.
2176 * SFF-8079 references SFF-8472 rev 9.5 and defines bit 0. SFF-8472
2177 * compliance is not required.
2178 * SFF-8472 rev 10.2 defines this field using values 0..4
2179 * SFF-8472 rev 11.0 redefines this field with bit 0 for SFF-8079
2180 * and even values.
2181 */
2182 rate_id = sfp->id.base.rate_id;
2183 if (rate_id == 0)
2184 /* Unspecified */
2185 return;
2186
2187 /* SFF-8472 rev 10.0..10.4 did not account for SFF-8079 using bit 0,
2188 * and allocated value 3 to SFF-8431 independent tx/rx rate select.
2189 * Convert this to a SFF-8472 rev 11.0 rate identifier.
2190 */
2191 if (sfp->id.ext.sff8472_compliance >= SFP_SFF8472_COMPLIANCE_REV10_2 &&
2192 sfp->id.ext.sff8472_compliance < SFP_SFF8472_COMPLIANCE_REV11_0 &&
2193 rate_id == 3)
2194 rate_id = SFF_RID_8431;
2195
2196 if (rate_id & SFF_RID_8079) {
2197 /* SFF-8079 RateSelect / Application Select in conjunction with
2198 * SFF-8472 rev 9.5. SFF-8079 defines rate_id as a bitfield
2199 * with only bit 0 used, which takes precedence over SFF-8472.
2200 */
2201 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_APP_SELECT_SFF8079)) {
2202 /* SFF-8079 Part 1 - rate selection between Fibre
2203 * Channel 1.0625/2.125/4.25 Gbd modes. Note that RS0
2204 * is high for 2125, so we have to subtract 1 to
2205 * include it.
2206 */
2207 sfp->rs_threshold_kbd = 2125 - 1;
2208 sfp->rs_state_mask = SFP_F_RS0;
2209 }
2210 return;
2211 }
2212
2213 /* SFF-8472 rev 9.5 does not define the rate identifier */
2214 if (sfp->id.ext.sff8472_compliance <= SFP_SFF8472_COMPLIANCE_REV9_5)
2215 return;
2216
2217 /* SFF-8472 rev 11.0 defines rate_id as a numerical value which will
2218 * always have bit 0 clear due to SFF-8079's bitfield usage of rate_id.
2219 */
2220 switch (rate_id) {
2221 case SFF_RID_8431_RX_ONLY:
2222 sfp->rs_threshold_kbd = 4250;
2223 sfp->rs_state_mask = SFP_F_RS0;
2224 break;
2225
2226 case SFF_RID_8431_TX_ONLY:
2227 sfp->rs_threshold_kbd = 4250;
2228 sfp->rs_state_mask = SFP_F_RS1;
2229 break;
2230
2231 case SFF_RID_8431:
2232 sfp->rs_threshold_kbd = 4250;
2233 sfp->rs_state_mask = SFP_F_RS0 | SFP_F_RS1;
2234 break;
2235
2236 case SFF_RID_10G8G:
2237 sfp->rs_threshold_kbd = 9000;
2238 sfp->rs_state_mask = SFP_F_RS0 | SFP_F_RS1;
2239 break;
2240 }
2241 }
2242
2243 /* GPON modules based on Realtek RTL8672 and RTL9601C chips (e.g. V-SOL
2244 * V2801F, CarlitoxxPro CPGOS03-0490, Ubiquiti U-Fiber Instant, ...) do
2245 * not support multibyte reads from the EEPROM. Each multi-byte read
2246 * operation returns just one byte of EEPROM followed by zeros. There is
2247 * no way to identify which modules are using Realtek RTL8672 and RTL9601C
2248 * chips. Moreover every OEM of V-SOL V2801F module puts its own vendor
2249 * name and vendor id into EEPROM, so there is even no way to detect if
2250 * module is V-SOL V2801F. Therefore check for those zeros in the read
2251 * data and then based on check switch to reading EEPROM to one byte
2252 * at a time.
2253 */
sfp_id_needs_byte_io(struct sfp * sfp,void * buf,size_t len)2254 static bool sfp_id_needs_byte_io(struct sfp *sfp, void *buf, size_t len)
2255 {
2256 size_t i, block_size = sfp->i2c_block_size;
2257
2258 /* Already using byte IO */
2259 if (block_size == 1)
2260 return false;
2261
2262 for (i = 1; i < len; i += block_size) {
2263 if (memchr_inv(buf + i, '\0', min(block_size - 1, len - i)))
2264 return false;
2265 }
2266 return true;
2267 }
2268
sfp_cotsworks_fixup_check(struct sfp * sfp,struct sfp_eeprom_id * id)2269 static int sfp_cotsworks_fixup_check(struct sfp *sfp, struct sfp_eeprom_id *id)
2270 {
2271 u8 check;
2272 int err;
2273
2274 if (id->base.phys_id != SFF8024_ID_SFF_8472 ||
2275 id->base.phys_ext_id != SFP_PHYS_EXT_ID_SFP ||
2276 id->base.connector != SFF8024_CONNECTOR_LC) {
2277 dev_warn(sfp->dev, "Rewriting fiber module EEPROM with corrected values\n");
2278 id->base.phys_id = SFF8024_ID_SFF_8472;
2279 id->base.phys_ext_id = SFP_PHYS_EXT_ID_SFP;
2280 id->base.connector = SFF8024_CONNECTOR_LC;
2281 err = sfp_write(sfp, false, SFP_PHYS_ID, &id->base, 3);
2282 if (err != 3) {
2283 dev_err(sfp->dev,
2284 "Failed to rewrite module EEPROM: %pe\n",
2285 ERR_PTR(err));
2286 return err;
2287 }
2288
2289 /* Cotsworks modules have been found to require a delay between write operations. */
2290 mdelay(50);
2291
2292 /* Update base structure checksum */
2293 check = sfp_check(&id->base, sizeof(id->base) - 1);
2294 err = sfp_write(sfp, false, SFP_CC_BASE, &check, 1);
2295 if (err != 1) {
2296 dev_err(sfp->dev,
2297 "Failed to update base structure checksum in fiber module EEPROM: %pe\n",
2298 ERR_PTR(err));
2299 return err;
2300 }
2301 }
2302 return 0;
2303 }
2304
sfp_module_parse_sff8472(struct sfp * sfp)2305 static int sfp_module_parse_sff8472(struct sfp *sfp)
2306 {
2307 /* If the module requires address swap mode, warn about it */
2308 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
2309 dev_warn(sfp->dev,
2310 "module address swap to access page 0xA2 is not supported.\n");
2311 else
2312 sfp->have_a2 = true;
2313
2314 return 0;
2315 }
2316
sfp_sm_mod_probe(struct sfp * sfp,bool report)2317 static int sfp_sm_mod_probe(struct sfp *sfp, bool report)
2318 {
2319 /* SFP module inserted - read I2C data */
2320 struct sfp_eeprom_id id;
2321 bool cotsworks_sfbg;
2322 unsigned int mask;
2323 bool cotsworks;
2324 u8 check;
2325 int ret;
2326
2327 sfp->i2c_block_size = sfp->i2c_max_block_size;
2328
2329 ret = sfp_read(sfp, false, 0, &id.base, sizeof(id.base));
2330 if (ret < 0) {
2331 if (report)
2332 dev_err(sfp->dev, "failed to read EEPROM: %pe\n",
2333 ERR_PTR(ret));
2334 return -EAGAIN;
2335 }
2336
2337 if (ret != sizeof(id.base)) {
2338 dev_err(sfp->dev, "EEPROM short read: %pe\n", ERR_PTR(ret));
2339 return -EAGAIN;
2340 }
2341
2342 /* Some SFP modules (e.g. Nokia 3FE46541AA) lock up if read from
2343 * address 0x51 is just one byte at a time. Also SFF-8472 requires
2344 * that EEPROM supports atomic 16bit read operation for diagnostic
2345 * fields, so do not switch to one byte reading at a time unless it
2346 * is really required and we have no other option.
2347 */
2348 if (sfp_id_needs_byte_io(sfp, &id.base, sizeof(id.base))) {
2349 dev_info(sfp->dev,
2350 "Detected broken RTL8672/RTL9601C emulated EEPROM\n");
2351 dev_info(sfp->dev,
2352 "Switching to reading EEPROM to one byte at a time\n");
2353 sfp->i2c_block_size = 1;
2354
2355 ret = sfp_read(sfp, false, 0, &id.base, sizeof(id.base));
2356 if (ret < 0) {
2357 if (report)
2358 dev_err(sfp->dev,
2359 "failed to read EEPROM: %pe\n",
2360 ERR_PTR(ret));
2361 return -EAGAIN;
2362 }
2363
2364 if (ret != sizeof(id.base)) {
2365 dev_err(sfp->dev, "EEPROM short read: %pe\n",
2366 ERR_PTR(ret));
2367 return -EAGAIN;
2368 }
2369 }
2370
2371 /* Cotsworks do not seem to update the checksums when they
2372 * do the final programming with the final module part number,
2373 * serial number and date code.
2374 */
2375 cotsworks = !memcmp(id.base.vendor_name, "COTSWORKS ", 16);
2376 cotsworks_sfbg = !memcmp(id.base.vendor_pn, "SFBG", 4);
2377
2378 /* Cotsworks SFF module EEPROM do not always have valid phys_id,
2379 * phys_ext_id, and connector bytes. Rewrite SFF EEPROM bytes if
2380 * Cotsworks PN matches and bytes are not correct.
2381 */
2382 if (cotsworks && cotsworks_sfbg) {
2383 ret = sfp_cotsworks_fixup_check(sfp, &id);
2384 if (ret < 0)
2385 return ret;
2386 }
2387
2388 /* Validate the checksum over the base structure */
2389 check = sfp_check(&id.base, sizeof(id.base) - 1);
2390 if (check != id.base.cc_base) {
2391 if (cotsworks) {
2392 dev_warn(sfp->dev,
2393 "EEPROM base structure checksum failure (0x%02x != 0x%02x)\n",
2394 check, id.base.cc_base);
2395 } else {
2396 dev_err(sfp->dev,
2397 "EEPROM base structure checksum failure: 0x%02x != 0x%02x\n",
2398 check, id.base.cc_base);
2399 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
2400 16, 1, &id, sizeof(id), true);
2401 return -EINVAL;
2402 }
2403 }
2404
2405 ret = sfp_read(sfp, false, SFP_CC_BASE + 1, &id.ext, sizeof(id.ext));
2406 if (ret < 0) {
2407 if (report)
2408 dev_err(sfp->dev, "failed to read EEPROM: %pe\n",
2409 ERR_PTR(ret));
2410 return -EAGAIN;
2411 }
2412
2413 if (ret != sizeof(id.ext)) {
2414 dev_err(sfp->dev, "EEPROM short read: %pe\n", ERR_PTR(ret));
2415 return -EAGAIN;
2416 }
2417
2418 check = sfp_check(&id.ext, sizeof(id.ext) - 1);
2419 if (check != id.ext.cc_ext) {
2420 if (cotsworks) {
2421 dev_warn(sfp->dev,
2422 "EEPROM extended structure checksum failure (0x%02x != 0x%02x)\n",
2423 check, id.ext.cc_ext);
2424 } else {
2425 dev_err(sfp->dev,
2426 "EEPROM extended structure checksum failure: 0x%02x != 0x%02x\n",
2427 check, id.ext.cc_ext);
2428 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
2429 16, 1, &id, sizeof(id), true);
2430 memset(&id.ext, 0, sizeof(id.ext));
2431 }
2432 }
2433
2434 sfp->id = id;
2435
2436 dev_info(sfp->dev, "module %.*s %.*s rev %.*s sn %.*s dc %.*s\n",
2437 (int)sizeof(id.base.vendor_name), id.base.vendor_name,
2438 (int)sizeof(id.base.vendor_pn), id.base.vendor_pn,
2439 (int)sizeof(id.base.vendor_rev), id.base.vendor_rev,
2440 (int)sizeof(id.ext.vendor_sn), id.ext.vendor_sn,
2441 (int)sizeof(id.ext.datecode), id.ext.datecode);
2442
2443 /* Check whether we support this module */
2444 if (!sfp->type->module_supported(&id)) {
2445 dev_err(sfp->dev,
2446 "module is not supported - phys id 0x%02x 0x%02x\n",
2447 sfp->id.base.phys_id, sfp->id.base.phys_ext_id);
2448 return -EINVAL;
2449 }
2450
2451 if (sfp->id.ext.sff8472_compliance != SFP_SFF8472_COMPLIANCE_NONE) {
2452 ret = sfp_module_parse_sff8472(sfp);
2453 if (ret < 0)
2454 return ret;
2455 }
2456
2457 /* Parse the module power requirement */
2458 ret = sfp_module_parse_power(sfp);
2459 if (ret < 0)
2460 return ret;
2461
2462 sfp_module_parse_rate_select(sfp);
2463
2464 mask = SFP_F_PRESENT;
2465 if (sfp->gpio[GPIO_TX_DISABLE])
2466 mask |= SFP_F_TX_DISABLE;
2467 if (sfp->gpio[GPIO_TX_FAULT])
2468 mask |= SFP_F_TX_FAULT;
2469 if (sfp->gpio[GPIO_LOS])
2470 mask |= SFP_F_LOS;
2471 if (sfp->gpio[GPIO_RS0])
2472 mask |= SFP_F_RS0;
2473 if (sfp->gpio[GPIO_RS1])
2474 mask |= SFP_F_RS1;
2475
2476 sfp->module_t_start_up = T_START_UP;
2477 sfp->module_t_wait = T_WAIT;
2478 sfp->phy_t_retry = T_PHY_RETRY;
2479
2480 sfp->state_ignore_mask = 0;
2481
2482 if (sfp->id.base.extended_cc == SFF8024_ECC_10GBASE_T_SFI ||
2483 sfp->id.base.extended_cc == SFF8024_ECC_10GBASE_T_SR ||
2484 sfp->id.base.extended_cc == SFF8024_ECC_5GBASE_T ||
2485 sfp->id.base.extended_cc == SFF8024_ECC_2_5GBASE_T)
2486 sfp->mdio_protocol = MDIO_I2C_C45;
2487 else if (sfp->id.base.e1000_base_t)
2488 sfp->mdio_protocol = MDIO_I2C_MARVELL_C22;
2489 else
2490 sfp->mdio_protocol = MDIO_I2C_NONE;
2491
2492 sfp->quirk = sfp_lookup_quirk(&id);
2493
2494 mutex_lock(&sfp->st_mutex);
2495 /* Initialise state bits to use from hardware */
2496 sfp->state_hw_mask = mask;
2497
2498 /* We want to drive the rate select pins that the module is using */
2499 sfp->state_hw_drive |= sfp->rs_state_mask;
2500
2501 if (sfp->quirk && sfp->quirk->fixup)
2502 sfp->quirk->fixup(sfp);
2503
2504 sfp->state_hw_mask &= ~sfp->state_ignore_mask;
2505 mutex_unlock(&sfp->st_mutex);
2506
2507 return 0;
2508 }
2509
sfp_sm_mod_remove(struct sfp * sfp)2510 static void sfp_sm_mod_remove(struct sfp *sfp)
2511 {
2512 if (sfp->sm_mod_state > SFP_MOD_WAITDEV)
2513 sfp_module_remove(sfp->sfp_bus);
2514
2515 sfp_hwmon_remove(sfp);
2516
2517 memset(&sfp->id, 0, sizeof(sfp->id));
2518 sfp->module_power_mW = 0;
2519 sfp->state_hw_drive = SFP_F_TX_DISABLE;
2520 sfp->have_a2 = false;
2521
2522 dev_info(sfp->dev, "module removed\n");
2523 }
2524
2525 /* This state machine tracks the upstream's state */
sfp_sm_device(struct sfp * sfp,unsigned int event)2526 static void sfp_sm_device(struct sfp *sfp, unsigned int event)
2527 {
2528 switch (sfp->sm_dev_state) {
2529 default:
2530 if (event == SFP_E_DEV_ATTACH)
2531 sfp->sm_dev_state = SFP_DEV_DOWN;
2532 break;
2533
2534 case SFP_DEV_DOWN:
2535 if (event == SFP_E_DEV_DETACH)
2536 sfp->sm_dev_state = SFP_DEV_DETACHED;
2537 else if (event == SFP_E_DEV_UP)
2538 sfp->sm_dev_state = SFP_DEV_UP;
2539 break;
2540
2541 case SFP_DEV_UP:
2542 if (event == SFP_E_DEV_DETACH)
2543 sfp->sm_dev_state = SFP_DEV_DETACHED;
2544 else if (event == SFP_E_DEV_DOWN)
2545 sfp->sm_dev_state = SFP_DEV_DOWN;
2546 break;
2547 }
2548 }
2549
2550 /* This state machine tracks the insert/remove state of the module, probes
2551 * the on-board EEPROM, and sets up the power level.
2552 */
sfp_sm_module(struct sfp * sfp,unsigned int event)2553 static void sfp_sm_module(struct sfp *sfp, unsigned int event)
2554 {
2555 int err;
2556
2557 /* Handle remove event globally, it resets this state machine */
2558 if (event == SFP_E_REMOVE) {
2559 sfp_sm_mod_remove(sfp);
2560 sfp_sm_mod_next(sfp, SFP_MOD_EMPTY, 0);
2561 return;
2562 }
2563
2564 /* Handle device detach globally */
2565 if (sfp->sm_dev_state < SFP_DEV_DOWN &&
2566 sfp->sm_mod_state > SFP_MOD_WAITDEV) {
2567 if (sfp->module_power_mW > 1000 &&
2568 sfp->sm_mod_state > SFP_MOD_HPOWER)
2569 sfp_sm_mod_hpower(sfp, false);
2570 sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0);
2571 return;
2572 }
2573
2574 switch (sfp->sm_mod_state) {
2575 default:
2576 if (event == SFP_E_INSERT) {
2577 sfp_sm_mod_next(sfp, SFP_MOD_PROBE, T_SERIAL);
2578 sfp->sm_mod_tries_init = R_PROBE_RETRY_INIT;
2579 sfp->sm_mod_tries = R_PROBE_RETRY_SLOW;
2580 }
2581 break;
2582
2583 case SFP_MOD_PROBE:
2584 /* Wait for T_PROBE_INIT to time out */
2585 if (event != SFP_E_TIMEOUT)
2586 break;
2587
2588 err = sfp_sm_mod_probe(sfp, sfp->sm_mod_tries == 1);
2589 if (err == -EAGAIN) {
2590 if (sfp->sm_mod_tries_init &&
2591 --sfp->sm_mod_tries_init) {
2592 sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT);
2593 break;
2594 } else if (sfp->sm_mod_tries && --sfp->sm_mod_tries) {
2595 if (sfp->sm_mod_tries == R_PROBE_RETRY_SLOW - 1)
2596 dev_warn(sfp->dev,
2597 "please wait, module slow to respond\n");
2598 sfp_sm_set_timer(sfp, T_PROBE_RETRY_SLOW);
2599 break;
2600 }
2601 }
2602 if (err < 0) {
2603 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
2604 break;
2605 }
2606
2607 /* Force a poll to re-read the hardware signal state after
2608 * sfp_sm_mod_probe() changed state_hw_mask.
2609 */
2610 mod_delayed_work(system_percpu_wq, &sfp->poll, 1);
2611
2612 err = sfp_hwmon_insert(sfp);
2613 if (err)
2614 dev_warn(sfp->dev, "hwmon probe failed: %pe\n",
2615 ERR_PTR(err));
2616
2617 sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0);
2618 fallthrough;
2619 case SFP_MOD_WAITDEV:
2620 /* Ensure that the device is attached before proceeding */
2621 if (sfp->sm_dev_state < SFP_DEV_DOWN)
2622 break;
2623
2624 /* Report the module insertion to the upstream device */
2625 err = sfp_module_insert(sfp->sfp_bus, &sfp->id,
2626 sfp->quirk);
2627 if (err < 0) {
2628 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
2629 break;
2630 }
2631
2632 /* If this is a power level 1 module, we are done */
2633 if (sfp->module_power_mW <= 1000)
2634 goto insert;
2635
2636 sfp_sm_mod_next(sfp, SFP_MOD_HPOWER, 0);
2637 fallthrough;
2638 case SFP_MOD_HPOWER:
2639 /* Enable high power mode */
2640 err = sfp_sm_mod_hpower(sfp, true);
2641 if (err < 0) {
2642 if (err != -EAGAIN) {
2643 sfp_module_remove(sfp->sfp_bus);
2644 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
2645 } else {
2646 sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT);
2647 }
2648 break;
2649 }
2650
2651 sfp_sm_mod_next(sfp, SFP_MOD_WAITPWR, T_HPOWER_LEVEL);
2652 break;
2653
2654 case SFP_MOD_WAITPWR:
2655 /* Wait for T_HPOWER_LEVEL to time out */
2656 if (event != SFP_E_TIMEOUT)
2657 break;
2658
2659 insert:
2660 sfp_sm_mod_next(sfp, SFP_MOD_PRESENT, 0);
2661 break;
2662
2663 case SFP_MOD_PRESENT:
2664 case SFP_MOD_ERROR:
2665 break;
2666 }
2667 }
2668
sfp_sm_main(struct sfp * sfp,unsigned int event)2669 static void sfp_sm_main(struct sfp *sfp, unsigned int event)
2670 {
2671 unsigned long timeout;
2672 int ret;
2673
2674 /* Some events are global */
2675 if (sfp->sm_state != SFP_S_DOWN &&
2676 (sfp->sm_mod_state != SFP_MOD_PRESENT ||
2677 sfp->sm_dev_state != SFP_DEV_UP)) {
2678 if (sfp->sm_state == SFP_S_LINK_UP &&
2679 sfp->sm_dev_state == SFP_DEV_UP)
2680 sfp_sm_link_down(sfp);
2681 if (sfp->sm_state > SFP_S_INIT)
2682 sfp_module_stop(sfp->sfp_bus);
2683 if (sfp->mod_phy)
2684 sfp_sm_phy_detach(sfp);
2685 if (sfp->i2c_mii)
2686 sfp_i2c_mdiobus_destroy(sfp);
2687 sfp_module_tx_disable(sfp);
2688 sfp_soft_stop_poll(sfp);
2689 sfp_sm_next(sfp, SFP_S_DOWN, 0);
2690 return;
2691 }
2692
2693 /* The main state machine */
2694 switch (sfp->sm_state) {
2695 case SFP_S_DOWN:
2696 if (sfp->sm_mod_state != SFP_MOD_PRESENT ||
2697 sfp->sm_dev_state != SFP_DEV_UP)
2698 break;
2699
2700 /* Only use the soft state bits if we have access to the A2h
2701 * memory, which implies that we have some level of SFF-8472
2702 * compliance.
2703 */
2704 if (sfp->have_a2)
2705 sfp_soft_start_poll(sfp);
2706
2707 sfp_module_tx_enable(sfp);
2708
2709 /* Initialise the fault clearance retries */
2710 sfp->sm_fault_retries = N_FAULT_INIT;
2711
2712 /* We need to check the TX_FAULT state, which is not defined
2713 * while TX_DISABLE is asserted. The earliest we want to do
2714 * anything (such as probe for a PHY) is 50ms (or more on
2715 * specific modules).
2716 */
2717 sfp_sm_next(sfp, SFP_S_WAIT, sfp->module_t_wait);
2718 break;
2719
2720 case SFP_S_WAIT:
2721 if (event != SFP_E_TIMEOUT)
2722 break;
2723
2724 if (sfp->state & SFP_F_TX_FAULT) {
2725 /* Wait up to t_init (SFF-8472) or t_start_up (SFF-8431)
2726 * from the TX_DISABLE deassertion for the module to
2727 * initialise, which is indicated by TX_FAULT
2728 * deasserting.
2729 */
2730 timeout = sfp->module_t_start_up;
2731 if (timeout > sfp->module_t_wait)
2732 timeout -= sfp->module_t_wait;
2733 else
2734 timeout = 1;
2735
2736 sfp_sm_next(sfp, SFP_S_INIT, timeout);
2737 } else {
2738 /* TX_FAULT is not asserted, assume the module has
2739 * finished initialising.
2740 */
2741 goto init_done;
2742 }
2743 break;
2744
2745 case SFP_S_INIT:
2746 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
2747 /* TX_FAULT is still asserted after t_init
2748 * or t_start_up, so assume there is a fault.
2749 */
2750 sfp_sm_fault(sfp, SFP_S_INIT_TX_FAULT,
2751 sfp->sm_fault_retries == N_FAULT_INIT);
2752 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
2753 init_done:
2754 /* Create mdiobus and start trying for PHY */
2755 ret = sfp_sm_add_mdio_bus(sfp);
2756 if (ret < 0) {
2757 sfp_sm_next(sfp, SFP_S_FAIL, 0);
2758 break;
2759 }
2760 sfp->sm_phy_retries = R_PHY_RETRY;
2761 goto phy_probe;
2762 }
2763 break;
2764
2765 case SFP_S_INIT_PHY:
2766 if (event != SFP_E_TIMEOUT)
2767 break;
2768 phy_probe:
2769 /* TX_FAULT deasserted or we timed out with TX_FAULT
2770 * clear. Probe for the PHY and check the LOS state.
2771 */
2772 ret = sfp_sm_probe_for_phy(sfp);
2773 if (ret == -ENODEV) {
2774 if (--sfp->sm_phy_retries) {
2775 sfp_sm_next(sfp, SFP_S_INIT_PHY,
2776 sfp->phy_t_retry);
2777 dev_dbg(sfp->dev,
2778 "no PHY detected, %u tries left\n",
2779 sfp->sm_phy_retries);
2780 break;
2781 } else {
2782 dev_info(sfp->dev, "no PHY detected\n");
2783 }
2784 } else if (ret) {
2785 sfp_sm_next(sfp, SFP_S_FAIL, 0);
2786 break;
2787 }
2788 if (sfp_module_start(sfp->sfp_bus)) {
2789 sfp_sm_next(sfp, SFP_S_FAIL, 0);
2790 break;
2791 }
2792 sfp_sm_link_check_los(sfp);
2793
2794 /* Reset the fault retry count */
2795 sfp->sm_fault_retries = N_FAULT;
2796 break;
2797
2798 case SFP_S_INIT_TX_FAULT:
2799 if (event == SFP_E_TIMEOUT) {
2800 sfp_module_tx_fault_reset(sfp);
2801 sfp_sm_next(sfp, SFP_S_INIT, sfp->module_t_start_up);
2802 }
2803 break;
2804
2805 case SFP_S_WAIT_LOS:
2806 if (event == SFP_E_TX_FAULT)
2807 sfp_sm_fault(sfp, SFP_S_TX_FAULT, true);
2808 else if (sfp_los_event_inactive(sfp, event))
2809 sfp_sm_link_up(sfp);
2810 break;
2811
2812 case SFP_S_LINK_UP:
2813 if (event == SFP_E_TX_FAULT) {
2814 sfp_sm_link_down(sfp);
2815 sfp_sm_fault(sfp, SFP_S_TX_FAULT, true);
2816 } else if (sfp_los_event_active(sfp, event)) {
2817 sfp_sm_link_down(sfp);
2818 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
2819 }
2820 break;
2821
2822 case SFP_S_TX_FAULT:
2823 if (event == SFP_E_TIMEOUT) {
2824 sfp_module_tx_fault_reset(sfp);
2825 sfp_sm_next(sfp, SFP_S_REINIT, sfp->module_t_start_up);
2826 }
2827 break;
2828
2829 case SFP_S_REINIT:
2830 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
2831 sfp_sm_fault(sfp, SFP_S_TX_FAULT, false);
2832 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
2833 dev_info(sfp->dev, "module transmit fault recovered\n");
2834 sfp_sm_link_check_los(sfp);
2835 }
2836 break;
2837
2838 case SFP_S_TX_DISABLE:
2839 break;
2840 }
2841 }
2842
__sfp_sm_event(struct sfp * sfp,unsigned int event)2843 static void __sfp_sm_event(struct sfp *sfp, unsigned int event)
2844 {
2845 dev_dbg(sfp->dev, "SM: enter %s:%s:%s event %s\n",
2846 mod_state_to_str(sfp->sm_mod_state),
2847 dev_state_to_str(sfp->sm_dev_state),
2848 sm_state_to_str(sfp->sm_state),
2849 event_to_str(event));
2850
2851 sfp_sm_device(sfp, event);
2852 sfp_sm_module(sfp, event);
2853 sfp_sm_main(sfp, event);
2854
2855 dev_dbg(sfp->dev, "SM: exit %s:%s:%s\n",
2856 mod_state_to_str(sfp->sm_mod_state),
2857 dev_state_to_str(sfp->sm_dev_state),
2858 sm_state_to_str(sfp->sm_state));
2859 }
2860
sfp_sm_event(struct sfp * sfp,unsigned int event)2861 static void sfp_sm_event(struct sfp *sfp, unsigned int event)
2862 {
2863 mutex_lock(&sfp->sm_mutex);
2864 __sfp_sm_event(sfp, event);
2865 mutex_unlock(&sfp->sm_mutex);
2866 }
2867
sfp_attach(struct sfp * sfp)2868 static void sfp_attach(struct sfp *sfp)
2869 {
2870 sfp_sm_event(sfp, SFP_E_DEV_ATTACH);
2871 }
2872
sfp_detach(struct sfp * sfp)2873 static void sfp_detach(struct sfp *sfp)
2874 {
2875 sfp_sm_event(sfp, SFP_E_DEV_DETACH);
2876 }
2877
sfp_start(struct sfp * sfp)2878 static void sfp_start(struct sfp *sfp)
2879 {
2880 sfp_sm_event(sfp, SFP_E_DEV_UP);
2881 }
2882
sfp_stop(struct sfp * sfp)2883 static void sfp_stop(struct sfp *sfp)
2884 {
2885 sfp_sm_event(sfp, SFP_E_DEV_DOWN);
2886 }
2887
sfp_set_signal_rate(struct sfp * sfp,unsigned int rate_kbd)2888 static void sfp_set_signal_rate(struct sfp *sfp, unsigned int rate_kbd)
2889 {
2890 unsigned int set;
2891
2892 sfp->rate_kbd = rate_kbd;
2893
2894 if (rate_kbd > sfp->rs_threshold_kbd)
2895 set = sfp->rs_state_mask;
2896 else
2897 set = 0;
2898
2899 sfp_mod_state(sfp, SFP_F_RS0 | SFP_F_RS1, set);
2900 }
2901
sfp_module_info(struct sfp * sfp,struct ethtool_modinfo * modinfo)2902 static int sfp_module_info(struct sfp *sfp, struct ethtool_modinfo *modinfo)
2903 {
2904 /* locking... and check module is present */
2905
2906 if (sfp->id.ext.sff8472_compliance &&
2907 !(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)) {
2908 modinfo->type = ETH_MODULE_SFF_8472;
2909 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
2910 } else {
2911 modinfo->type = ETH_MODULE_SFF_8079;
2912 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
2913 }
2914 return 0;
2915 }
2916
sfp_module_eeprom(struct sfp * sfp,struct ethtool_eeprom * ee,u8 * data)2917 static int sfp_module_eeprom(struct sfp *sfp, struct ethtool_eeprom *ee,
2918 u8 *data)
2919 {
2920 unsigned int first, last, len;
2921 int ret;
2922
2923 if (!(sfp->state & SFP_F_PRESENT))
2924 return -ENODEV;
2925
2926 if (ee->len == 0)
2927 return -EINVAL;
2928
2929 first = ee->offset;
2930 last = ee->offset + ee->len;
2931 if (first < ETH_MODULE_SFF_8079_LEN) {
2932 len = min_t(unsigned int, last, ETH_MODULE_SFF_8079_LEN);
2933 len -= first;
2934
2935 ret = sfp_read(sfp, false, first, data, len);
2936 if (ret < 0)
2937 return ret;
2938
2939 first += len;
2940 data += len;
2941 }
2942 if (first < ETH_MODULE_SFF_8472_LEN && last > ETH_MODULE_SFF_8079_LEN) {
2943 len = min_t(unsigned int, last, ETH_MODULE_SFF_8472_LEN);
2944 len -= first;
2945 first -= ETH_MODULE_SFF_8079_LEN;
2946
2947 ret = sfp_read(sfp, true, first, data, len);
2948 if (ret < 0)
2949 return ret;
2950 }
2951 return 0;
2952 }
2953
sfp_module_eeprom_by_page(struct sfp * sfp,const struct ethtool_module_eeprom * page,struct netlink_ext_ack * extack)2954 static int sfp_module_eeprom_by_page(struct sfp *sfp,
2955 const struct ethtool_module_eeprom *page,
2956 struct netlink_ext_ack *extack)
2957 {
2958 if (!(sfp->state & SFP_F_PRESENT))
2959 return -ENODEV;
2960
2961 if (page->bank) {
2962 NL_SET_ERR_MSG(extack, "Banks not supported");
2963 return -EOPNOTSUPP;
2964 }
2965
2966 if (page->page) {
2967 NL_SET_ERR_MSG(extack, "Only page 0 supported");
2968 return -EOPNOTSUPP;
2969 }
2970
2971 if (page->i2c_address != 0x50 &&
2972 page->i2c_address != 0x51) {
2973 NL_SET_ERR_MSG(extack, "Only address 0x50 and 0x51 supported");
2974 return -EOPNOTSUPP;
2975 }
2976
2977 return sfp_read(sfp, page->i2c_address == 0x51, page->offset,
2978 page->data, page->length);
2979 };
2980
2981 static const struct sfp_socket_ops sfp_module_ops = {
2982 .attach = sfp_attach,
2983 .detach = sfp_detach,
2984 .start = sfp_start,
2985 .stop = sfp_stop,
2986 .set_signal_rate = sfp_set_signal_rate,
2987 .module_info = sfp_module_info,
2988 .module_eeprom = sfp_module_eeprom,
2989 .module_eeprom_by_page = sfp_module_eeprom_by_page,
2990 };
2991
sfp_timeout(struct work_struct * work)2992 static void sfp_timeout(struct work_struct *work)
2993 {
2994 struct sfp *sfp = container_of(work, struct sfp, timeout.work);
2995
2996 rtnl_lock();
2997 sfp_sm_event(sfp, SFP_E_TIMEOUT);
2998 rtnl_unlock();
2999 }
3000
sfp_check_state(struct sfp * sfp)3001 static void sfp_check_state(struct sfp *sfp)
3002 {
3003 unsigned int state, i, changed;
3004
3005 rtnl_lock();
3006 mutex_lock(&sfp->st_mutex);
3007 state = sfp_get_state(sfp);
3008 changed = state ^ sfp->state;
3009 changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT;
3010
3011 for (i = 0; i < GPIO_MAX; i++)
3012 if (changed & BIT(i))
3013 dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_names[i],
3014 !!(sfp->state & BIT(i)), !!(state & BIT(i)));
3015
3016 state |= sfp->state & SFP_F_OUTPUTS;
3017 sfp->state = state;
3018 mutex_unlock(&sfp->st_mutex);
3019
3020 mutex_lock(&sfp->sm_mutex);
3021 if (changed & SFP_F_PRESENT)
3022 __sfp_sm_event(sfp, state & SFP_F_PRESENT ?
3023 SFP_E_INSERT : SFP_E_REMOVE);
3024
3025 if (changed & SFP_F_TX_FAULT)
3026 __sfp_sm_event(sfp, state & SFP_F_TX_FAULT ?
3027 SFP_E_TX_FAULT : SFP_E_TX_CLEAR);
3028
3029 if (changed & SFP_F_LOS)
3030 __sfp_sm_event(sfp, state & SFP_F_LOS ?
3031 SFP_E_LOS_HIGH : SFP_E_LOS_LOW);
3032 mutex_unlock(&sfp->sm_mutex);
3033 rtnl_unlock();
3034 }
3035
sfp_irq(int irq,void * data)3036 static irqreturn_t sfp_irq(int irq, void *data)
3037 {
3038 struct sfp *sfp = data;
3039
3040 sfp_check_state(sfp);
3041
3042 return IRQ_HANDLED;
3043 }
3044
sfp_poll(struct work_struct * work)3045 static void sfp_poll(struct work_struct *work)
3046 {
3047 struct sfp *sfp = container_of(work, struct sfp, poll.work);
3048
3049 sfp_check_state(sfp);
3050
3051 // st_mutex doesn't need to be held here for state_soft_mask,
3052 // it's unimportant if we race while reading this.
3053 if (sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT) ||
3054 sfp->need_poll)
3055 sfp_schedule_poll(sfp);
3056 }
3057
sfp_alloc(struct device * dev)3058 static struct sfp *sfp_alloc(struct device *dev)
3059 {
3060 struct sfp *sfp;
3061
3062 sfp = kzalloc_obj(*sfp);
3063 if (!sfp)
3064 return ERR_PTR(-ENOMEM);
3065
3066 sfp->dev = dev;
3067
3068 mutex_init(&sfp->sm_mutex);
3069 mutex_init(&sfp->st_mutex);
3070 INIT_DELAYED_WORK(&sfp->poll, sfp_poll);
3071 INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout);
3072
3073 sfp_hwmon_init(sfp);
3074
3075 return sfp;
3076 }
3077
sfp_cleanup(void * data)3078 static void sfp_cleanup(void *data)
3079 {
3080 struct sfp *sfp = data;
3081
3082 sfp_hwmon_exit(sfp);
3083
3084 cancel_delayed_work_sync(&sfp->poll);
3085 cancel_delayed_work_sync(&sfp->timeout);
3086 if (sfp->i2c_mii) {
3087 mdiobus_unregister(sfp->i2c_mii);
3088 mdiobus_free(sfp->i2c_mii);
3089 }
3090 if (sfp->i2c)
3091 i2c_put_adapter(sfp->i2c);
3092 kfree(sfp);
3093 }
3094
sfp_i2c_get(struct sfp * sfp)3095 static int sfp_i2c_get(struct sfp *sfp)
3096 {
3097 struct fwnode_handle *h;
3098 struct i2c_adapter *i2c;
3099 int err;
3100
3101 h = fwnode_find_reference(dev_fwnode(sfp->dev), "i2c-bus", 0);
3102 if (IS_ERR(h)) {
3103 dev_err(sfp->dev, "missing 'i2c-bus' property\n");
3104 return -ENODEV;
3105 }
3106
3107 i2c = i2c_get_adapter_by_fwnode(h);
3108 if (!i2c) {
3109 err = -EPROBE_DEFER;
3110 goto put;
3111 }
3112
3113 err = sfp_i2c_configure(sfp, i2c);
3114 if (err)
3115 i2c_put_adapter(i2c);
3116 put:
3117 fwnode_handle_put(h);
3118 return err;
3119 }
3120
sfp_probe(struct platform_device * pdev)3121 static int sfp_probe(struct platform_device *pdev)
3122 {
3123 const struct sff_data *sff;
3124 char *sfp_irq_name;
3125 struct sfp *sfp;
3126 int err, i;
3127
3128 sfp = sfp_alloc(&pdev->dev);
3129 if (IS_ERR(sfp))
3130 return PTR_ERR(sfp);
3131
3132 platform_set_drvdata(pdev, sfp);
3133
3134 err = devm_add_action_or_reset(sfp->dev, sfp_cleanup, sfp);
3135 if (err < 0)
3136 return err;
3137
3138 sff = device_get_match_data(sfp->dev);
3139 if (!sff)
3140 sff = &sfp_data;
3141
3142 sfp->type = sff;
3143
3144 err = sfp_i2c_get(sfp);
3145 if (err)
3146 return err;
3147
3148 for (i = 0; i < GPIO_MAX; i++)
3149 if (sff->gpios & BIT(i)) {
3150 sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev,
3151 gpio_names[i], gpio_flags[i]);
3152 if (IS_ERR(sfp->gpio[i]))
3153 return PTR_ERR(sfp->gpio[i]);
3154 }
3155
3156 sfp->state_hw_mask = SFP_F_PRESENT;
3157 sfp->state_hw_drive = SFP_F_TX_DISABLE;
3158
3159 sfp->get_state = sfp_gpio_get_state;
3160 sfp->set_state = sfp_gpio_set_state;
3161
3162 /* Modules that have no detect signal are always present */
3163 if (!(sfp->gpio[GPIO_MODDEF0]))
3164 sfp->get_state = sff_gpio_get_state;
3165
3166 device_property_read_u32(&pdev->dev, "maximum-power-milliwatt",
3167 &sfp->max_power_mW);
3168 if (sfp->max_power_mW < 1000) {
3169 if (sfp->max_power_mW)
3170 dev_warn(sfp->dev,
3171 "Firmware bug: host maximum power should be at least 1W\n");
3172 sfp->max_power_mW = 1000;
3173 }
3174
3175 dev_info(sfp->dev, "Host maximum power %u.%uW\n",
3176 sfp->max_power_mW / 1000, (sfp->max_power_mW / 100) % 10);
3177
3178 /* Get the initial state, and always signal TX disable,
3179 * since the network interface will not be up.
3180 */
3181 sfp->state = sfp_get_state(sfp) | SFP_F_TX_DISABLE;
3182
3183 if (sfp->gpio[GPIO_RS0] &&
3184 gpiod_get_value_cansleep(sfp->gpio[GPIO_RS0]))
3185 sfp->state |= SFP_F_RS0;
3186 sfp_set_state(sfp, sfp->state);
3187 sfp_module_tx_disable(sfp);
3188 if (sfp->state & SFP_F_PRESENT) {
3189 rtnl_lock();
3190 sfp_sm_event(sfp, SFP_E_INSERT);
3191 rtnl_unlock();
3192 }
3193
3194 for (i = 0; i < GPIO_MAX; i++) {
3195 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
3196 continue;
3197
3198 sfp->gpio_irq[i] = gpiod_to_irq(sfp->gpio[i]);
3199 if (sfp->gpio_irq[i] < 0) {
3200 sfp->gpio_irq[i] = 0;
3201 sfp->need_poll = true;
3202 continue;
3203 }
3204
3205 sfp_irq_name = devm_kasprintf(sfp->dev, GFP_KERNEL,
3206 "%s-%s", dev_name(sfp->dev),
3207 gpio_names[i]);
3208
3209 if (!sfp_irq_name)
3210 return -ENOMEM;
3211
3212 err = devm_request_threaded_irq(sfp->dev, sfp->gpio_irq[i],
3213 NULL, sfp_irq,
3214 IRQF_ONESHOT |
3215 IRQF_TRIGGER_RISING |
3216 IRQF_TRIGGER_FALLING,
3217 sfp_irq_name, sfp);
3218 if (err) {
3219 sfp->gpio_irq[i] = 0;
3220 sfp->need_poll = true;
3221 }
3222 }
3223
3224 if (sfp->need_poll)
3225 sfp_schedule_poll(sfp);
3226
3227 /* We could have an issue in cases no Tx disable pin is available or
3228 * wired as modules using a laser as their light source will continue to
3229 * be active when the fiber is removed. This could be a safety issue and
3230 * we should at least warn the user about that.
3231 */
3232 if (!sfp->gpio[GPIO_TX_DISABLE])
3233 dev_warn(sfp->dev,
3234 "No tx_disable pin: SFP modules will always be emitting.\n");
3235
3236 sfp->sfp_bus = sfp_register_socket(sfp->dev, sfp, &sfp_module_ops);
3237 if (!sfp->sfp_bus)
3238 return -ENOMEM;
3239
3240 if (sfp->i2c_max_block_size < 2)
3241 dev_warn(sfp->dev,
3242 "Please note:\n"
3243 "This SFP cage is accessed via an SMBus only capable of single byte\n"
3244 "transactions. Some features are disabled, other may be unreliable or\n"
3245 "sporadically fail. Use with caution. There is nothing that the kernel\n"
3246 "or community can do to fix it, the kernel will try best efforts. Please\n"
3247 "verify any problems on hardware that supports multi-byte I2C transactions.\n");
3248
3249 sfp_debugfs_init(sfp);
3250
3251 return 0;
3252 }
3253
sfp_remove(struct platform_device * pdev)3254 static void sfp_remove(struct platform_device *pdev)
3255 {
3256 struct sfp *sfp = platform_get_drvdata(pdev);
3257
3258 sfp_debugfs_exit(sfp);
3259 sfp_unregister_socket(sfp->sfp_bus);
3260
3261 rtnl_lock();
3262 sfp_sm_event(sfp, SFP_E_REMOVE);
3263 rtnl_unlock();
3264 }
3265
sfp_shutdown(struct platform_device * pdev)3266 static void sfp_shutdown(struct platform_device *pdev)
3267 {
3268 struct sfp *sfp = platform_get_drvdata(pdev);
3269 int i;
3270
3271 for (i = 0; i < GPIO_MAX; i++) {
3272 if (!sfp->gpio_irq[i])
3273 continue;
3274
3275 devm_free_irq(sfp->dev, sfp->gpio_irq[i], sfp);
3276 }
3277
3278 cancel_delayed_work_sync(&sfp->poll);
3279 cancel_delayed_work_sync(&sfp->timeout);
3280 }
3281
3282 static struct platform_driver sfp_driver = {
3283 .probe = sfp_probe,
3284 .remove = sfp_remove,
3285 .shutdown = sfp_shutdown,
3286 .driver = {
3287 .name = "sfp",
3288 .of_match_table = sfp_of_match,
3289 },
3290 };
3291
3292 module_platform_driver(sfp_driver);
3293
3294 MODULE_ALIAS("platform:sfp");
3295 MODULE_AUTHOR("Russell King");
3296 MODULE_LICENSE("GPL v2");
3297 MODULE_DESCRIPTION("SFP cage support");
3298