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