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