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