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