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