blob: 27360d1840b23c4ec794caad282d5f7c3fd726ac [file] [log] [blame]
Andrew Lunna2443fd2019-01-21 19:05:50 +01001// SPDX-License-Identifier: GPL-2.0
Ruslan Babayev7ce236fa2019-05-28 16:02:33 -07002#include <linux/acpi.h>
Andrew Lunn13230612018-07-17 21:48:13 +02003#include <linux/ctype.h>
Russell King73970052017-07-25 15:03:39 +01004#include <linux/delay.h>
Florian Fainelli54a2fc62017-10-30 21:42:58 -07005#include <linux/gpio/consumer.h>
Andrew Lunn13230612018-07-17 21:48:13 +02006#include <linux/hwmon.h>
Russell King73970052017-07-25 15:03:39 +01007#include <linux/i2c.h>
8#include <linux/interrupt.h>
9#include <linux/jiffies.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 "mdio-i2c.h"
20#include "sfp.h"
21#include "swphy.h"
22
23enum {
24 GPIO_MODDEF0,
25 GPIO_LOS,
26 GPIO_TX_FAULT,
27 GPIO_TX_DISABLE,
28 GPIO_RATE_SELECT,
29 GPIO_MAX,
30
31 SFP_F_PRESENT = BIT(GPIO_MODDEF0),
32 SFP_F_LOS = BIT(GPIO_LOS),
33 SFP_F_TX_FAULT = BIT(GPIO_TX_FAULT),
34 SFP_F_TX_DISABLE = BIT(GPIO_TX_DISABLE),
35 SFP_F_RATE_SELECT = BIT(GPIO_RATE_SELECT),
36
37 SFP_E_INSERT = 0,
38 SFP_E_REMOVE,
Russell King6b0da5c2019-11-10 14:07:14 +000039 SFP_E_DEV_ATTACH,
40 SFP_E_DEV_DETACH,
Russell King73970052017-07-25 15:03:39 +010041 SFP_E_DEV_DOWN,
42 SFP_E_DEV_UP,
43 SFP_E_TX_FAULT,
44 SFP_E_TX_CLEAR,
45 SFP_E_LOS_HIGH,
46 SFP_E_LOS_LOW,
47 SFP_E_TIMEOUT,
48
49 SFP_MOD_EMPTY = 0,
Russell King73f5e842019-11-10 14:07:25 +000050 SFP_MOD_ERROR,
Russell King73970052017-07-25 15:03:39 +010051 SFP_MOD_PROBE,
Russell King73f5e842019-11-10 14:07:25 +000052 SFP_MOD_WAITDEV,
Jon Nettleton3bb35262018-02-27 15:53:12 +000053 SFP_MOD_HPOWER,
Russell Kingb036a552019-11-10 14:07:20 +000054 SFP_MOD_WAITPWR,
Russell King73970052017-07-25 15:03:39 +010055 SFP_MOD_PRESENT,
Russell King73970052017-07-25 15:03:39 +010056
Russell King6b0da5c2019-11-10 14:07:14 +000057 SFP_DEV_DETACHED = 0,
58 SFP_DEV_DOWN,
Russell King73970052017-07-25 15:03:39 +010059 SFP_DEV_UP,
60
61 SFP_S_DOWN = 0,
Russell Kingeefa6f12019-11-10 14:06:59 +000062 SFP_S_WAIT,
Russell King73970052017-07-25 15:03:39 +010063 SFP_S_INIT,
Russell Kingd23751a2019-11-10 14:07:09 +000064 SFP_S_INIT_TX_FAULT,
Russell King73970052017-07-25 15:03:39 +010065 SFP_S_WAIT_LOS,
66 SFP_S_LINK_UP,
67 SFP_S_TX_FAULT,
68 SFP_S_REINIT,
69 SFP_S_TX_DISABLE,
70};
71
Andrew Lunn4005a7c2018-08-08 20:54:12 +020072static const char * const mod_state_strings[] = {
73 [SFP_MOD_EMPTY] = "empty",
Russell King73f5e842019-11-10 14:07:25 +000074 [SFP_MOD_ERROR] = "error",
Andrew Lunn4005a7c2018-08-08 20:54:12 +020075 [SFP_MOD_PROBE] = "probe",
Russell King73f5e842019-11-10 14:07:25 +000076 [SFP_MOD_WAITDEV] = "waitdev",
Andrew Lunn4005a7c2018-08-08 20:54:12 +020077 [SFP_MOD_HPOWER] = "hpower",
Russell Kingb036a552019-11-10 14:07:20 +000078 [SFP_MOD_WAITPWR] = "waitpwr",
Andrew Lunn4005a7c2018-08-08 20:54:12 +020079 [SFP_MOD_PRESENT] = "present",
Andrew Lunn4005a7c2018-08-08 20:54:12 +020080};
81
82static const char *mod_state_to_str(unsigned short mod_state)
83{
84 if (mod_state >= ARRAY_SIZE(mod_state_strings))
85 return "Unknown module state";
86 return mod_state_strings[mod_state];
87}
88
89static const char * const dev_state_strings[] = {
Russell King6b0da5c2019-11-10 14:07:14 +000090 [SFP_DEV_DETACHED] = "detached",
Andrew Lunn4005a7c2018-08-08 20:54:12 +020091 [SFP_DEV_DOWN] = "down",
92 [SFP_DEV_UP] = "up",
93};
94
95static const char *dev_state_to_str(unsigned short dev_state)
96{
97 if (dev_state >= ARRAY_SIZE(dev_state_strings))
98 return "Unknown device state";
99 return dev_state_strings[dev_state];
100}
101
102static const char * const event_strings[] = {
103 [SFP_E_INSERT] = "insert",
104 [SFP_E_REMOVE] = "remove",
Russell King6b0da5c2019-11-10 14:07:14 +0000105 [SFP_E_DEV_ATTACH] = "dev_attach",
106 [SFP_E_DEV_DETACH] = "dev_detach",
Andrew Lunn4005a7c2018-08-08 20:54:12 +0200107 [SFP_E_DEV_DOWN] = "dev_down",
108 [SFP_E_DEV_UP] = "dev_up",
109 [SFP_E_TX_FAULT] = "tx_fault",
110 [SFP_E_TX_CLEAR] = "tx_clear",
111 [SFP_E_LOS_HIGH] = "los_high",
112 [SFP_E_LOS_LOW] = "los_low",
113 [SFP_E_TIMEOUT] = "timeout",
114};
115
116static const char *event_to_str(unsigned short event)
117{
118 if (event >= ARRAY_SIZE(event_strings))
119 return "Unknown event";
120 return event_strings[event];
121}
122
123static const char * const sm_state_strings[] = {
124 [SFP_S_DOWN] = "down",
Russell Kingeefa6f12019-11-10 14:06:59 +0000125 [SFP_S_WAIT] = "wait",
Andrew Lunn4005a7c2018-08-08 20:54:12 +0200126 [SFP_S_INIT] = "init",
Russell Kingd23751a2019-11-10 14:07:09 +0000127 [SFP_S_INIT_TX_FAULT] = "init_tx_fault",
Andrew Lunn4005a7c2018-08-08 20:54:12 +0200128 [SFP_S_WAIT_LOS] = "wait_los",
129 [SFP_S_LINK_UP] = "link_up",
130 [SFP_S_TX_FAULT] = "tx_fault",
131 [SFP_S_REINIT] = "reinit",
132 [SFP_S_TX_DISABLE] = "rx_disable",
133};
134
135static const char *sm_state_to_str(unsigned short sm_state)
136{
137 if (sm_state >= ARRAY_SIZE(sm_state_strings))
138 return "Unknown state";
139 return sm_state_strings[sm_state];
140}
141
Russell King73970052017-07-25 15:03:39 +0100142static const char *gpio_of_names[] = {
Baruch Siach25ee0792017-09-07 12:25:50 +0300143 "mod-def0",
Russell King73970052017-07-25 15:03:39 +0100144 "los",
145 "tx-fault",
146 "tx-disable",
Baruch Siach25ee0792017-09-07 12:25:50 +0300147 "rate-select0",
Russell King73970052017-07-25 15:03:39 +0100148};
149
150static const enum gpiod_flags gpio_flags[] = {
151 GPIOD_IN,
152 GPIOD_IN,
153 GPIOD_IN,
154 GPIOD_ASIS,
155 GPIOD_ASIS,
156};
157
Russell King26c97a22019-12-09 13:40:23 +0000158/* t_start_up (SFF-8431) or t_init (SFF-8472) is the time required for a
159 * non-cooled module to initialise its laser safety circuitry. We wait
160 * an initial T_WAIT period before we check the tx fault to give any PHY
161 * on board (for a copper SFP) time to initialise.
162 */
163#define T_WAIT msecs_to_jiffies(50)
164#define T_START_UP msecs_to_jiffies(300)
165#define T_START_UP_BAD_GPON msecs_to_jiffies(60000)
166
167/* t_reset is the time required to assert the TX_DISABLE signal to reset
168 * an indicated TX_FAULT.
169 */
170#define T_RESET_US 10
171#define T_FAULT_RECOVER msecs_to_jiffies(1000)
Russell King73970052017-07-25 15:03:39 +0100172
173/* SFP module presence detection is poor: the three MOD DEF signals are
174 * the same length on the PCB, which means it's possible for MOD DEF 0 to
175 * connect before the I2C bus on MOD DEF 1/2.
176 *
Russell Kingd9009542019-11-10 14:06:33 +0000177 * The SFF-8472 specifies t_serial ("Time from power on until module is
178 * ready for data transmission over the two wire serial bus.") as 300ms.
Russell King73970052017-07-25 15:03:39 +0100179 */
Russell Kinge117be72019-11-10 14:07:30 +0000180#define T_SERIAL msecs_to_jiffies(300)
181#define T_HPOWER_LEVEL msecs_to_jiffies(300)
182#define T_PROBE_RETRY_INIT msecs_to_jiffies(100)
183#define R_PROBE_RETRY_INIT 10
184#define T_PROBE_RETRY_SLOW msecs_to_jiffies(5000)
185#define R_PROBE_RETRY_SLOW 12
Russell King73970052017-07-25 15:03:39 +0100186
Florian Fainelli516b29e2017-10-30 21:42:57 -0700187/* SFP modules appear to always have their PHY configured for bus address
Russell King73970052017-07-25 15:03:39 +0100188 * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
189 */
190#define SFP_PHY_ADDR 22
191
Russell King259c8612017-12-14 10:27:47 +0000192struct sff_data {
193 unsigned int gpios;
194 bool (*module_supported)(const struct sfp_eeprom_id *id);
195};
196
Russell King73970052017-07-25 15:03:39 +0100197struct sfp {
198 struct device *dev;
199 struct i2c_adapter *i2c;
200 struct mii_bus *i2c_mii;
201 struct sfp_bus *sfp_bus;
202 struct phy_device *mod_phy;
Russell King259c8612017-12-14 10:27:47 +0000203 const struct sff_data *type;
Jon Nettleton3bb35262018-02-27 15:53:12 +0000204 u32 max_power_mW;
Russell King73970052017-07-25 15:03:39 +0100205
206 unsigned int (*get_state)(struct sfp *);
207 void (*set_state)(struct sfp *, unsigned int);
208 int (*read)(struct sfp *, bool, u8, void *, size_t);
Jon Nettleton3bb35262018-02-27 15:53:12 +0000209 int (*write)(struct sfp *, bool, u8, void *, size_t);
Russell King73970052017-07-25 15:03:39 +0100210
211 struct gpio_desc *gpio[GPIO_MAX];
Robert Hancock257c2552019-06-07 10:42:35 -0600212 int gpio_irq[GPIO_MAX];
Russell King73970052017-07-25 15:03:39 +0100213
Russell Kingf3c9a662019-11-20 12:29:59 +0000214 bool need_poll;
215
Robert Hancock2158e852019-06-07 10:42:36 -0600216 struct mutex st_mutex; /* Protects state */
Russell Kingf3c9a662019-11-20 12:29:59 +0000217 unsigned int state_soft_mask;
Russell King73970052017-07-25 15:03:39 +0100218 unsigned int state;
219 struct delayed_work poll;
220 struct delayed_work timeout;
Robert Hancock2158e852019-06-07 10:42:36 -0600221 struct mutex sm_mutex; /* Protects state machine */
Russell King73970052017-07-25 15:03:39 +0100222 unsigned char sm_mod_state;
Russell Kinge117be72019-11-10 14:07:30 +0000223 unsigned char sm_mod_tries_init;
224 unsigned char sm_mod_tries;
Russell King73970052017-07-25 15:03:39 +0100225 unsigned char sm_dev_state;
226 unsigned short sm_state;
227 unsigned int sm_retries;
228
229 struct sfp_eeprom_id id;
Russell Kinged32abb2019-11-10 14:06:39 +0000230 unsigned int module_power_mW;
Russell King26c97a22019-12-09 13:40:23 +0000231 unsigned int module_t_start_up;
Russell Kinged32abb2019-11-10 14:06:39 +0000232
Andrew Lunn13230612018-07-17 21:48:13 +0200233#if IS_ENABLED(CONFIG_HWMON)
234 struct sfp_diag diag;
Russell King139d3a22019-11-10 14:07:35 +0000235 struct delayed_work hwmon_probe;
236 unsigned int hwmon_tries;
Andrew Lunn13230612018-07-17 21:48:13 +0200237 struct device *hwmon_dev;
238 char *hwmon_name;
239#endif
240
Russell King73970052017-07-25 15:03:39 +0100241};
242
Russell King259c8612017-12-14 10:27:47 +0000243static bool sff_module_supported(const struct sfp_eeprom_id *id)
244{
245 return id->base.phys_id == SFP_PHYS_ID_SFF &&
246 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
247}
248
249static const struct sff_data sff_data = {
250 .gpios = SFP_F_LOS | SFP_F_TX_FAULT | SFP_F_TX_DISABLE,
251 .module_supported = sff_module_supported,
252};
253
254static bool sfp_module_supported(const struct sfp_eeprom_id *id)
255{
256 return id->base.phys_id == SFP_PHYS_ID_SFP &&
257 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
258}
259
260static const struct sff_data sfp_data = {
261 .gpios = SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT |
262 SFP_F_TX_DISABLE | SFP_F_RATE_SELECT,
263 .module_supported = sfp_module_supported,
264};
265
266static const struct of_device_id sfp_of_match[] = {
267 { .compatible = "sff,sff", .data = &sff_data, },
268 { .compatible = "sff,sfp", .data = &sfp_data, },
269 { },
270};
271MODULE_DEVICE_TABLE(of, sfp_of_match);
272
Russell King73970052017-07-25 15:03:39 +0100273static unsigned long poll_jiffies;
274
275static unsigned int sfp_gpio_get_state(struct sfp *sfp)
276{
277 unsigned int i, state, v;
278
279 for (i = state = 0; i < GPIO_MAX; i++) {
280 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
281 continue;
282
283 v = gpiod_get_value_cansleep(sfp->gpio[i]);
284 if (v)
285 state |= BIT(i);
286 }
287
288 return state;
289}
290
Russell King259c8612017-12-14 10:27:47 +0000291static unsigned int sff_gpio_get_state(struct sfp *sfp)
292{
293 return sfp_gpio_get_state(sfp) | SFP_F_PRESENT;
294}
295
Russell King73970052017-07-25 15:03:39 +0100296static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state)
297{
298 if (state & SFP_F_PRESENT) {
299 /* If the module is present, drive the signals */
300 if (sfp->gpio[GPIO_TX_DISABLE])
301 gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE],
Florian Fainelli516b29e2017-10-30 21:42:57 -0700302 state & SFP_F_TX_DISABLE);
Russell King73970052017-07-25 15:03:39 +0100303 if (state & SFP_F_RATE_SELECT)
304 gpiod_direction_output(sfp->gpio[GPIO_RATE_SELECT],
Florian Fainelli516b29e2017-10-30 21:42:57 -0700305 state & SFP_F_RATE_SELECT);
Russell King73970052017-07-25 15:03:39 +0100306 } else {
307 /* Otherwise, let them float to the pull-ups */
308 if (sfp->gpio[GPIO_TX_DISABLE])
309 gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]);
310 if (state & SFP_F_RATE_SELECT)
311 gpiod_direction_input(sfp->gpio[GPIO_RATE_SELECT]);
312 }
313}
314
Jon Nettleton3bb35262018-02-27 15:53:12 +0000315static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
316 size_t len)
Russell King73970052017-07-25 15:03:39 +0100317{
318 struct i2c_msg msgs[2];
Jon Nettleton3bb35262018-02-27 15:53:12 +0000319 u8 bus_addr = a2 ? 0x51 : 0x50;
Russell King28e74a72019-06-02 15:13:00 +0100320 size_t this_len;
Russell King73970052017-07-25 15:03:39 +0100321 int ret;
322
323 msgs[0].addr = bus_addr;
324 msgs[0].flags = 0;
325 msgs[0].len = 1;
326 msgs[0].buf = &dev_addr;
327 msgs[1].addr = bus_addr;
328 msgs[1].flags = I2C_M_RD;
329 msgs[1].len = len;
330 msgs[1].buf = buf;
331
Russell King28e74a72019-06-02 15:13:00 +0100332 while (len) {
333 this_len = len;
334 if (this_len > 16)
335 this_len = 16;
Russell King73970052017-07-25 15:03:39 +0100336
Russell King28e74a72019-06-02 15:13:00 +0100337 msgs[1].len = this_len;
338
339 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
340 if (ret < 0)
341 return ret;
342
343 if (ret != ARRAY_SIZE(msgs))
344 break;
345
346 msgs[1].buf += this_len;
347 dev_addr += this_len;
348 len -= this_len;
349 }
350
351 return msgs[1].buf - (u8 *)buf;
Russell King73970052017-07-25 15:03:39 +0100352}
353
Jon Nettleton3bb35262018-02-27 15:53:12 +0000354static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
355 size_t len)
Russell King73970052017-07-25 15:03:39 +0100356{
Jon Nettleton3bb35262018-02-27 15:53:12 +0000357 struct i2c_msg msgs[1];
358 u8 bus_addr = a2 ? 0x51 : 0x50;
359 int ret;
360
361 msgs[0].addr = bus_addr;
362 msgs[0].flags = 0;
363 msgs[0].len = 1 + len;
364 msgs[0].buf = kmalloc(1 + len, GFP_KERNEL);
365 if (!msgs[0].buf)
366 return -ENOMEM;
367
368 msgs[0].buf[0] = dev_addr;
369 memcpy(&msgs[0].buf[1], buf, len);
370
371 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
372
373 kfree(msgs[0].buf);
374
375 if (ret < 0)
376 return ret;
377
378 return ret == ARRAY_SIZE(msgs) ? len : 0;
Russell King73970052017-07-25 15:03:39 +0100379}
380
381static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
382{
383 struct mii_bus *i2c_mii;
384 int ret;
385
386 if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
387 return -EINVAL;
388
389 sfp->i2c = i2c;
390 sfp->read = sfp_i2c_read;
Jon Nettleton3bb35262018-02-27 15:53:12 +0000391 sfp->write = sfp_i2c_write;
Russell King73970052017-07-25 15:03:39 +0100392
393 i2c_mii = mdio_i2c_alloc(sfp->dev, i2c);
394 if (IS_ERR(i2c_mii))
395 return PTR_ERR(i2c_mii);
396
397 i2c_mii->name = "SFP I2C Bus";
398 i2c_mii->phy_mask = ~0;
399
400 ret = mdiobus_register(i2c_mii);
401 if (ret < 0) {
402 mdiobus_free(i2c_mii);
403 return ret;
404 }
405
406 sfp->i2c_mii = i2c_mii;
407
408 return 0;
409}
410
Russell King73970052017-07-25 15:03:39 +0100411/* Interface */
Russell King73970052017-07-25 15:03:39 +0100412static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
413{
414 return sfp->read(sfp, a2, addr, buf, len);
415}
416
Jon Nettleton3bb35262018-02-27 15:53:12 +0000417static int sfp_write(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
418{
419 return sfp->write(sfp, a2, addr, buf, len);
420}
421
Russell Kingf3c9a662019-11-20 12:29:59 +0000422static unsigned int sfp_soft_get_state(struct sfp *sfp)
423{
424 unsigned int state = 0;
425 u8 status;
426
427 if (sfp_read(sfp, true, SFP_STATUS, &status, sizeof(status)) ==
428 sizeof(status)) {
429 if (status & SFP_STATUS_RX_LOS)
430 state |= SFP_F_LOS;
431 if (status & SFP_STATUS_TX_FAULT)
432 state |= SFP_F_TX_FAULT;
433 }
434
435 return state & sfp->state_soft_mask;
436}
437
438static void sfp_soft_set_state(struct sfp *sfp, unsigned int state)
439{
440 u8 status;
441
442 if (sfp_read(sfp, true, SFP_STATUS, &status, sizeof(status)) ==
443 sizeof(status)) {
444 if (state & SFP_F_TX_DISABLE)
445 status |= SFP_STATUS_TX_DISABLE_FORCE;
446 else
447 status &= ~SFP_STATUS_TX_DISABLE_FORCE;
448
449 sfp_write(sfp, true, SFP_STATUS, &status, sizeof(status));
450 }
451}
452
453static void sfp_soft_start_poll(struct sfp *sfp)
454{
455 const struct sfp_eeprom_id *id = &sfp->id;
456
457 sfp->state_soft_mask = 0;
458 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_DISABLE &&
459 !sfp->gpio[GPIO_TX_DISABLE])
460 sfp->state_soft_mask |= SFP_F_TX_DISABLE;
461 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_FAULT &&
462 !sfp->gpio[GPIO_TX_FAULT])
463 sfp->state_soft_mask |= SFP_F_TX_FAULT;
464 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_RX_LOS &&
465 !sfp->gpio[GPIO_LOS])
466 sfp->state_soft_mask |= SFP_F_LOS;
467
468 if (sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT) &&
469 !sfp->need_poll)
470 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
471}
472
473static void sfp_soft_stop_poll(struct sfp *sfp)
474{
475 sfp->state_soft_mask = 0;
476}
477
478static unsigned int sfp_get_state(struct sfp *sfp)
479{
480 unsigned int state = sfp->get_state(sfp);
481
482 if (state & SFP_F_PRESENT &&
483 sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT))
484 state |= sfp_soft_get_state(sfp);
485
486 return state;
487}
488
489static void sfp_set_state(struct sfp *sfp, unsigned int state)
490{
491 sfp->set_state(sfp, state);
492
493 if (state & SFP_F_PRESENT &&
494 sfp->state_soft_mask & SFP_F_TX_DISABLE)
495 sfp_soft_set_state(sfp, state);
496}
497
Russell King73970052017-07-25 15:03:39 +0100498static unsigned int sfp_check(void *buf, size_t len)
499{
500 u8 *p, check;
501
502 for (p = buf, check = 0; len; p++, len--)
503 check += *p;
504
505 return check;
506}
507
Andrew Lunn13230612018-07-17 21:48:13 +0200508/* hwmon */
509#if IS_ENABLED(CONFIG_HWMON)
510static umode_t sfp_hwmon_is_visible(const void *data,
511 enum hwmon_sensor_types type,
512 u32 attr, int channel)
513{
514 const struct sfp *sfp = data;
515
516 switch (type) {
517 case hwmon_temp:
518 switch (attr) {
Andrew Lunn13230612018-07-17 21:48:13 +0200519 case hwmon_temp_min_alarm:
520 case hwmon_temp_max_alarm:
521 case hwmon_temp_lcrit_alarm:
522 case hwmon_temp_crit_alarm:
523 case hwmon_temp_min:
524 case hwmon_temp_max:
525 case hwmon_temp_lcrit:
526 case hwmon_temp_crit:
Andrew Lunna33710b2018-09-04 04:23:56 +0200527 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
528 return 0;
529 /* fall through */
530 case hwmon_temp_input:
Andrew Lunnc1236972019-08-25 01:04:17 +0200531 case hwmon_temp_label:
Andrew Lunn13230612018-07-17 21:48:13 +0200532 return 0444;
533 default:
534 return 0;
535 }
536 case hwmon_in:
537 switch (attr) {
Andrew Lunn13230612018-07-17 21:48:13 +0200538 case hwmon_in_min_alarm:
539 case hwmon_in_max_alarm:
540 case hwmon_in_lcrit_alarm:
541 case hwmon_in_crit_alarm:
542 case hwmon_in_min:
543 case hwmon_in_max:
544 case hwmon_in_lcrit:
545 case hwmon_in_crit:
Andrew Lunna33710b2018-09-04 04:23:56 +0200546 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
547 return 0;
548 /* fall through */
549 case hwmon_in_input:
Andrew Lunnc1236972019-08-25 01:04:17 +0200550 case hwmon_in_label:
Andrew Lunn13230612018-07-17 21:48:13 +0200551 return 0444;
552 default:
553 return 0;
554 }
555 case hwmon_curr:
556 switch (attr) {
Andrew Lunn13230612018-07-17 21:48:13 +0200557 case hwmon_curr_min_alarm:
558 case hwmon_curr_max_alarm:
559 case hwmon_curr_lcrit_alarm:
560 case hwmon_curr_crit_alarm:
561 case hwmon_curr_min:
562 case hwmon_curr_max:
563 case hwmon_curr_lcrit:
564 case hwmon_curr_crit:
Andrew Lunna33710b2018-09-04 04:23:56 +0200565 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
566 return 0;
567 /* fall through */
568 case hwmon_curr_input:
Andrew Lunnc1236972019-08-25 01:04:17 +0200569 case hwmon_curr_label:
Andrew Lunn13230612018-07-17 21:48:13 +0200570 return 0444;
571 default:
572 return 0;
573 }
574 case hwmon_power:
575 /* External calibration of receive power requires
576 * floating point arithmetic. Doing that in the kernel
577 * is not easy, so just skip it. If the module does
578 * not require external calibration, we can however
579 * show receiver power, since FP is then not needed.
580 */
581 if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL &&
582 channel == 1)
583 return 0;
584 switch (attr) {
Andrew Lunn13230612018-07-17 21:48:13 +0200585 case hwmon_power_min_alarm:
586 case hwmon_power_max_alarm:
587 case hwmon_power_lcrit_alarm:
588 case hwmon_power_crit_alarm:
589 case hwmon_power_min:
590 case hwmon_power_max:
591 case hwmon_power_lcrit:
592 case hwmon_power_crit:
Andrew Lunna33710b2018-09-04 04:23:56 +0200593 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
594 return 0;
595 /* fall through */
596 case hwmon_power_input:
Andrew Lunnc1236972019-08-25 01:04:17 +0200597 case hwmon_power_label:
Andrew Lunn13230612018-07-17 21:48:13 +0200598 return 0444;
599 default:
600 return 0;
601 }
602 default:
603 return 0;
604 }
605}
606
607static int sfp_hwmon_read_sensor(struct sfp *sfp, int reg, long *value)
608{
609 __be16 val;
610 int err;
611
612 err = sfp_read(sfp, true, reg, &val, sizeof(val));
613 if (err < 0)
614 return err;
615
616 *value = be16_to_cpu(val);
617
618 return 0;
619}
620
621static void sfp_hwmon_to_rx_power(long *value)
622{
Andrew Lunn0cea0e12019-07-21 18:50:08 +0200623 *value = DIV_ROUND_CLOSEST(*value, 10);
Andrew Lunn13230612018-07-17 21:48:13 +0200624}
625
626static void sfp_hwmon_calibrate(struct sfp *sfp, unsigned int slope, int offset,
627 long *value)
628{
629 if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL)
630 *value = DIV_ROUND_CLOSEST(*value * slope, 256) + offset;
631}
632
633static void sfp_hwmon_calibrate_temp(struct sfp *sfp, long *value)
634{
635 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_t_slope),
636 be16_to_cpu(sfp->diag.cal_t_offset), value);
637
638 if (*value >= 0x8000)
639 *value -= 0x10000;
640
641 *value = DIV_ROUND_CLOSEST(*value * 1000, 256);
642}
643
644static void sfp_hwmon_calibrate_vcc(struct sfp *sfp, long *value)
645{
646 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_v_slope),
647 be16_to_cpu(sfp->diag.cal_v_offset), value);
648
649 *value = DIV_ROUND_CLOSEST(*value, 10);
650}
651
652static void sfp_hwmon_calibrate_bias(struct sfp *sfp, long *value)
653{
654 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txi_slope),
655 be16_to_cpu(sfp->diag.cal_txi_offset), value);
656
657 *value = DIV_ROUND_CLOSEST(*value, 500);
658}
659
660static void sfp_hwmon_calibrate_tx_power(struct sfp *sfp, long *value)
661{
662 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txpwr_slope),
663 be16_to_cpu(sfp->diag.cal_txpwr_offset), value);
664
665 *value = DIV_ROUND_CLOSEST(*value, 10);
666}
667
668static int sfp_hwmon_read_temp(struct sfp *sfp, int reg, long *value)
669{
670 int err;
671
672 err = sfp_hwmon_read_sensor(sfp, reg, value);
673 if (err < 0)
674 return err;
675
676 sfp_hwmon_calibrate_temp(sfp, value);
677
678 return 0;
679}
680
681static int sfp_hwmon_read_vcc(struct sfp *sfp, int reg, long *value)
682{
683 int err;
684
685 err = sfp_hwmon_read_sensor(sfp, reg, value);
686 if (err < 0)
687 return err;
688
689 sfp_hwmon_calibrate_vcc(sfp, value);
690
691 return 0;
692}
693
694static int sfp_hwmon_read_bias(struct sfp *sfp, int reg, long *value)
695{
696 int err;
697
698 err = sfp_hwmon_read_sensor(sfp, reg, value);
699 if (err < 0)
700 return err;
701
702 sfp_hwmon_calibrate_bias(sfp, value);
703
704 return 0;
705}
706
707static int sfp_hwmon_read_tx_power(struct sfp *sfp, int reg, long *value)
708{
709 int err;
710
711 err = sfp_hwmon_read_sensor(sfp, reg, value);
712 if (err < 0)
713 return err;
714
715 sfp_hwmon_calibrate_tx_power(sfp, value);
716
717 return 0;
718}
719
720static int sfp_hwmon_read_rx_power(struct sfp *sfp, int reg, long *value)
721{
722 int err;
723
724 err = sfp_hwmon_read_sensor(sfp, reg, value);
725 if (err < 0)
726 return err;
727
728 sfp_hwmon_to_rx_power(value);
729
730 return 0;
731}
732
733static int sfp_hwmon_temp(struct sfp *sfp, u32 attr, long *value)
734{
735 u8 status;
736 int err;
737
738 switch (attr) {
739 case hwmon_temp_input:
740 return sfp_hwmon_read_temp(sfp, SFP_TEMP, value);
741
742 case hwmon_temp_lcrit:
743 *value = be16_to_cpu(sfp->diag.temp_low_alarm);
744 sfp_hwmon_calibrate_temp(sfp, value);
745 return 0;
746
747 case hwmon_temp_min:
748 *value = be16_to_cpu(sfp->diag.temp_low_warn);
749 sfp_hwmon_calibrate_temp(sfp, value);
750 return 0;
751 case hwmon_temp_max:
752 *value = be16_to_cpu(sfp->diag.temp_high_warn);
753 sfp_hwmon_calibrate_temp(sfp, value);
754 return 0;
755
756 case hwmon_temp_crit:
757 *value = be16_to_cpu(sfp->diag.temp_high_alarm);
758 sfp_hwmon_calibrate_temp(sfp, value);
759 return 0;
760
761 case hwmon_temp_lcrit_alarm:
762 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
763 if (err < 0)
764 return err;
765
766 *value = !!(status & SFP_ALARM0_TEMP_LOW);
767 return 0;
768
769 case hwmon_temp_min_alarm:
770 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
771 if (err < 0)
772 return err;
773
774 *value = !!(status & SFP_WARN0_TEMP_LOW);
775 return 0;
776
777 case hwmon_temp_max_alarm:
778 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
779 if (err < 0)
780 return err;
781
782 *value = !!(status & SFP_WARN0_TEMP_HIGH);
783 return 0;
784
785 case hwmon_temp_crit_alarm:
786 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
787 if (err < 0)
788 return err;
789
790 *value = !!(status & SFP_ALARM0_TEMP_HIGH);
791 return 0;
792 default:
793 return -EOPNOTSUPP;
794 }
795
796 return -EOPNOTSUPP;
797}
798
799static int sfp_hwmon_vcc(struct sfp *sfp, u32 attr, long *value)
800{
801 u8 status;
802 int err;
803
804 switch (attr) {
805 case hwmon_in_input:
806 return sfp_hwmon_read_vcc(sfp, SFP_VCC, value);
807
808 case hwmon_in_lcrit:
809 *value = be16_to_cpu(sfp->diag.volt_low_alarm);
810 sfp_hwmon_calibrate_vcc(sfp, value);
811 return 0;
812
813 case hwmon_in_min:
814 *value = be16_to_cpu(sfp->diag.volt_low_warn);
815 sfp_hwmon_calibrate_vcc(sfp, value);
816 return 0;
817
818 case hwmon_in_max:
819 *value = be16_to_cpu(sfp->diag.volt_high_warn);
820 sfp_hwmon_calibrate_vcc(sfp, value);
821 return 0;
822
823 case hwmon_in_crit:
824 *value = be16_to_cpu(sfp->diag.volt_high_alarm);
825 sfp_hwmon_calibrate_vcc(sfp, value);
826 return 0;
827
828 case hwmon_in_lcrit_alarm:
829 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
830 if (err < 0)
831 return err;
832
833 *value = !!(status & SFP_ALARM0_VCC_LOW);
834 return 0;
835
836 case hwmon_in_min_alarm:
837 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
838 if (err < 0)
839 return err;
840
841 *value = !!(status & SFP_WARN0_VCC_LOW);
842 return 0;
843
844 case hwmon_in_max_alarm:
845 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
846 if (err < 0)
847 return err;
848
849 *value = !!(status & SFP_WARN0_VCC_HIGH);
850 return 0;
851
852 case hwmon_in_crit_alarm:
853 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
854 if (err < 0)
855 return err;
856
857 *value = !!(status & SFP_ALARM0_VCC_HIGH);
858 return 0;
859 default:
860 return -EOPNOTSUPP;
861 }
862
863 return -EOPNOTSUPP;
864}
865
866static int sfp_hwmon_bias(struct sfp *sfp, u32 attr, long *value)
867{
868 u8 status;
869 int err;
870
871 switch (attr) {
872 case hwmon_curr_input:
873 return sfp_hwmon_read_bias(sfp, SFP_TX_BIAS, value);
874
875 case hwmon_curr_lcrit:
876 *value = be16_to_cpu(sfp->diag.bias_low_alarm);
877 sfp_hwmon_calibrate_bias(sfp, value);
878 return 0;
879
880 case hwmon_curr_min:
881 *value = be16_to_cpu(sfp->diag.bias_low_warn);
882 sfp_hwmon_calibrate_bias(sfp, value);
883 return 0;
884
885 case hwmon_curr_max:
886 *value = be16_to_cpu(sfp->diag.bias_high_warn);
887 sfp_hwmon_calibrate_bias(sfp, value);
888 return 0;
889
890 case hwmon_curr_crit:
891 *value = be16_to_cpu(sfp->diag.bias_high_alarm);
892 sfp_hwmon_calibrate_bias(sfp, value);
893 return 0;
894
895 case hwmon_curr_lcrit_alarm:
896 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
897 if (err < 0)
898 return err;
899
900 *value = !!(status & SFP_ALARM0_TX_BIAS_LOW);
901 return 0;
902
903 case hwmon_curr_min_alarm:
904 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
905 if (err < 0)
906 return err;
907
908 *value = !!(status & SFP_WARN0_TX_BIAS_LOW);
909 return 0;
910
911 case hwmon_curr_max_alarm:
912 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
913 if (err < 0)
914 return err;
915
916 *value = !!(status & SFP_WARN0_TX_BIAS_HIGH);
917 return 0;
918
919 case hwmon_curr_crit_alarm:
920 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
921 if (err < 0)
922 return err;
923
924 *value = !!(status & SFP_ALARM0_TX_BIAS_HIGH);
925 return 0;
926 default:
927 return -EOPNOTSUPP;
928 }
929
930 return -EOPNOTSUPP;
931}
932
933static int sfp_hwmon_tx_power(struct sfp *sfp, u32 attr, long *value)
934{
935 u8 status;
936 int err;
937
938 switch (attr) {
939 case hwmon_power_input:
940 return sfp_hwmon_read_tx_power(sfp, SFP_TX_POWER, value);
941
942 case hwmon_power_lcrit:
943 *value = be16_to_cpu(sfp->diag.txpwr_low_alarm);
944 sfp_hwmon_calibrate_tx_power(sfp, value);
945 return 0;
946
947 case hwmon_power_min:
948 *value = be16_to_cpu(sfp->diag.txpwr_low_warn);
949 sfp_hwmon_calibrate_tx_power(sfp, value);
950 return 0;
951
952 case hwmon_power_max:
953 *value = be16_to_cpu(sfp->diag.txpwr_high_warn);
954 sfp_hwmon_calibrate_tx_power(sfp, value);
955 return 0;
956
957 case hwmon_power_crit:
958 *value = be16_to_cpu(sfp->diag.txpwr_high_alarm);
959 sfp_hwmon_calibrate_tx_power(sfp, value);
960 return 0;
961
962 case hwmon_power_lcrit_alarm:
963 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
964 if (err < 0)
965 return err;
966
967 *value = !!(status & SFP_ALARM0_TXPWR_LOW);
968 return 0;
969
970 case hwmon_power_min_alarm:
971 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
972 if (err < 0)
973 return err;
974
975 *value = !!(status & SFP_WARN0_TXPWR_LOW);
976 return 0;
977
978 case hwmon_power_max_alarm:
979 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
980 if (err < 0)
981 return err;
982
983 *value = !!(status & SFP_WARN0_TXPWR_HIGH);
984 return 0;
985
986 case hwmon_power_crit_alarm:
987 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
988 if (err < 0)
989 return err;
990
991 *value = !!(status & SFP_ALARM0_TXPWR_HIGH);
992 return 0;
993 default:
994 return -EOPNOTSUPP;
995 }
996
997 return -EOPNOTSUPP;
998}
999
1000static int sfp_hwmon_rx_power(struct sfp *sfp, u32 attr, long *value)
1001{
1002 u8 status;
1003 int err;
1004
1005 switch (attr) {
1006 case hwmon_power_input:
1007 return sfp_hwmon_read_rx_power(sfp, SFP_RX_POWER, value);
1008
1009 case hwmon_power_lcrit:
1010 *value = be16_to_cpu(sfp->diag.rxpwr_low_alarm);
1011 sfp_hwmon_to_rx_power(value);
1012 return 0;
1013
1014 case hwmon_power_min:
1015 *value = be16_to_cpu(sfp->diag.rxpwr_low_warn);
1016 sfp_hwmon_to_rx_power(value);
1017 return 0;
1018
1019 case hwmon_power_max:
1020 *value = be16_to_cpu(sfp->diag.rxpwr_high_warn);
1021 sfp_hwmon_to_rx_power(value);
1022 return 0;
1023
1024 case hwmon_power_crit:
1025 *value = be16_to_cpu(sfp->diag.rxpwr_high_alarm);
1026 sfp_hwmon_to_rx_power(value);
1027 return 0;
1028
1029 case hwmon_power_lcrit_alarm:
1030 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
1031 if (err < 0)
1032 return err;
1033
1034 *value = !!(status & SFP_ALARM1_RXPWR_LOW);
1035 return 0;
1036
1037 case hwmon_power_min_alarm:
1038 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
1039 if (err < 0)
1040 return err;
1041
1042 *value = !!(status & SFP_WARN1_RXPWR_LOW);
1043 return 0;
1044
1045 case hwmon_power_max_alarm:
1046 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
1047 if (err < 0)
1048 return err;
1049
1050 *value = !!(status & SFP_WARN1_RXPWR_HIGH);
1051 return 0;
1052
1053 case hwmon_power_crit_alarm:
1054 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
1055 if (err < 0)
1056 return err;
1057
1058 *value = !!(status & SFP_ALARM1_RXPWR_HIGH);
1059 return 0;
1060 default:
1061 return -EOPNOTSUPP;
1062 }
1063
1064 return -EOPNOTSUPP;
1065}
1066
1067static int sfp_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
1068 u32 attr, int channel, long *value)
1069{
1070 struct sfp *sfp = dev_get_drvdata(dev);
1071
1072 switch (type) {
1073 case hwmon_temp:
1074 return sfp_hwmon_temp(sfp, attr, value);
1075 case hwmon_in:
1076 return sfp_hwmon_vcc(sfp, attr, value);
1077 case hwmon_curr:
1078 return sfp_hwmon_bias(sfp, attr, value);
1079 case hwmon_power:
1080 switch (channel) {
1081 case 0:
1082 return sfp_hwmon_tx_power(sfp, attr, value);
1083 case 1:
1084 return sfp_hwmon_rx_power(sfp, attr, value);
1085 default:
1086 return -EOPNOTSUPP;
1087 }
1088 default:
1089 return -EOPNOTSUPP;
1090 }
1091}
1092
Andrew Lunnc1236972019-08-25 01:04:17 +02001093static const char *const sfp_hwmon_power_labels[] = {
1094 "TX_power",
1095 "RX_power",
1096};
1097
1098static int sfp_hwmon_read_string(struct device *dev,
1099 enum hwmon_sensor_types type,
1100 u32 attr, int channel, const char **str)
1101{
1102 switch (type) {
1103 case hwmon_curr:
1104 switch (attr) {
1105 case hwmon_curr_label:
1106 *str = "bias";
1107 return 0;
1108 default:
1109 return -EOPNOTSUPP;
1110 }
1111 break;
1112 case hwmon_temp:
1113 switch (attr) {
1114 case hwmon_temp_label:
1115 *str = "temperature";
1116 return 0;
1117 default:
1118 return -EOPNOTSUPP;
1119 }
1120 break;
1121 case hwmon_in:
1122 switch (attr) {
1123 case hwmon_in_label:
1124 *str = "VCC";
1125 return 0;
1126 default:
1127 return -EOPNOTSUPP;
1128 }
1129 break;
1130 case hwmon_power:
1131 switch (attr) {
1132 case hwmon_power_label:
1133 *str = sfp_hwmon_power_labels[channel];
1134 return 0;
1135 default:
1136 return -EOPNOTSUPP;
1137 }
1138 break;
1139 default:
1140 return -EOPNOTSUPP;
1141 }
1142
1143 return -EOPNOTSUPP;
1144}
1145
Andrew Lunn13230612018-07-17 21:48:13 +02001146static const struct hwmon_ops sfp_hwmon_ops = {
1147 .is_visible = sfp_hwmon_is_visible,
1148 .read = sfp_hwmon_read,
Andrew Lunnc1236972019-08-25 01:04:17 +02001149 .read_string = sfp_hwmon_read_string,
Andrew Lunn13230612018-07-17 21:48:13 +02001150};
1151
1152static u32 sfp_hwmon_chip_config[] = {
1153 HWMON_C_REGISTER_TZ,
1154 0,
1155};
1156
1157static const struct hwmon_channel_info sfp_hwmon_chip = {
1158 .type = hwmon_chip,
1159 .config = sfp_hwmon_chip_config,
1160};
1161
1162static u32 sfp_hwmon_temp_config[] = {
1163 HWMON_T_INPUT |
1164 HWMON_T_MAX | HWMON_T_MIN |
1165 HWMON_T_MAX_ALARM | HWMON_T_MIN_ALARM |
1166 HWMON_T_CRIT | HWMON_T_LCRIT |
Andrew Lunnc1236972019-08-25 01:04:17 +02001167 HWMON_T_CRIT_ALARM | HWMON_T_LCRIT_ALARM |
1168 HWMON_T_LABEL,
Andrew Lunn13230612018-07-17 21:48:13 +02001169 0,
1170};
1171
1172static const struct hwmon_channel_info sfp_hwmon_temp_channel_info = {
1173 .type = hwmon_temp,
1174 .config = sfp_hwmon_temp_config,
1175};
1176
1177static u32 sfp_hwmon_vcc_config[] = {
1178 HWMON_I_INPUT |
1179 HWMON_I_MAX | HWMON_I_MIN |
1180 HWMON_I_MAX_ALARM | HWMON_I_MIN_ALARM |
1181 HWMON_I_CRIT | HWMON_I_LCRIT |
Andrew Lunnc1236972019-08-25 01:04:17 +02001182 HWMON_I_CRIT_ALARM | HWMON_I_LCRIT_ALARM |
1183 HWMON_I_LABEL,
Andrew Lunn13230612018-07-17 21:48:13 +02001184 0,
1185};
1186
1187static const struct hwmon_channel_info sfp_hwmon_vcc_channel_info = {
1188 .type = hwmon_in,
1189 .config = sfp_hwmon_vcc_config,
1190};
1191
1192static u32 sfp_hwmon_bias_config[] = {
1193 HWMON_C_INPUT |
1194 HWMON_C_MAX | HWMON_C_MIN |
1195 HWMON_C_MAX_ALARM | HWMON_C_MIN_ALARM |
1196 HWMON_C_CRIT | HWMON_C_LCRIT |
Andrew Lunnc1236972019-08-25 01:04:17 +02001197 HWMON_C_CRIT_ALARM | HWMON_C_LCRIT_ALARM |
1198 HWMON_C_LABEL,
Andrew Lunn13230612018-07-17 21:48:13 +02001199 0,
1200};
1201
1202static const struct hwmon_channel_info sfp_hwmon_bias_channel_info = {
1203 .type = hwmon_curr,
1204 .config = sfp_hwmon_bias_config,
1205};
1206
1207static u32 sfp_hwmon_power_config[] = {
1208 /* Transmit power */
1209 HWMON_P_INPUT |
1210 HWMON_P_MAX | HWMON_P_MIN |
1211 HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
1212 HWMON_P_CRIT | HWMON_P_LCRIT |
Andrew Lunnc1236972019-08-25 01:04:17 +02001213 HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM |
1214 HWMON_P_LABEL,
Andrew Lunn13230612018-07-17 21:48:13 +02001215 /* Receive power */
1216 HWMON_P_INPUT |
1217 HWMON_P_MAX | HWMON_P_MIN |
1218 HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
1219 HWMON_P_CRIT | HWMON_P_LCRIT |
Andrew Lunnc1236972019-08-25 01:04:17 +02001220 HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM |
1221 HWMON_P_LABEL,
Andrew Lunn13230612018-07-17 21:48:13 +02001222 0,
1223};
1224
1225static const struct hwmon_channel_info sfp_hwmon_power_channel_info = {
1226 .type = hwmon_power,
1227 .config = sfp_hwmon_power_config,
1228};
1229
1230static const struct hwmon_channel_info *sfp_hwmon_info[] = {
1231 &sfp_hwmon_chip,
1232 &sfp_hwmon_vcc_channel_info,
1233 &sfp_hwmon_temp_channel_info,
1234 &sfp_hwmon_bias_channel_info,
1235 &sfp_hwmon_power_channel_info,
1236 NULL,
1237};
1238
1239static const struct hwmon_chip_info sfp_hwmon_chip_info = {
1240 .ops = &sfp_hwmon_ops,
1241 .info = sfp_hwmon_info,
1242};
1243
Russell King139d3a22019-11-10 14:07:35 +00001244static void sfp_hwmon_probe(struct work_struct *work)
Andrew Lunn13230612018-07-17 21:48:13 +02001245{
Russell King139d3a22019-11-10 14:07:35 +00001246 struct sfp *sfp = container_of(work, struct sfp, hwmon_probe.work);
Andrew Lunn13230612018-07-17 21:48:13 +02001247 int err, i;
1248
Russell King139d3a22019-11-10 14:07:35 +00001249 err = sfp_read(sfp, true, 0, &sfp->diag, sizeof(sfp->diag));
1250 if (err < 0) {
1251 if (sfp->hwmon_tries--) {
1252 mod_delayed_work(system_wq, &sfp->hwmon_probe,
1253 T_PROBE_RETRY_SLOW);
1254 } else {
1255 dev_warn(sfp->dev, "hwmon probe failed: %d\n", err);
1256 }
1257 return;
1258 }
1259
1260 sfp->hwmon_name = kstrdup(dev_name(sfp->dev), GFP_KERNEL);
1261 if (!sfp->hwmon_name) {
1262 dev_err(sfp->dev, "out of memory for hwmon name\n");
1263 return;
1264 }
1265
1266 for (i = 0; sfp->hwmon_name[i]; i++)
1267 if (hwmon_is_bad_char(sfp->hwmon_name[i]))
1268 sfp->hwmon_name[i] = '_';
1269
1270 sfp->hwmon_dev = hwmon_device_register_with_info(sfp->dev,
1271 sfp->hwmon_name, sfp,
1272 &sfp_hwmon_chip_info,
1273 NULL);
1274 if (IS_ERR(sfp->hwmon_dev))
1275 dev_err(sfp->dev, "failed to register hwmon device: %ld\n",
1276 PTR_ERR(sfp->hwmon_dev));
1277}
1278
1279static int sfp_hwmon_insert(struct sfp *sfp)
1280{
Andrew Lunn13230612018-07-17 21:48:13 +02001281 if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE)
1282 return 0;
1283
1284 if (!(sfp->id.ext.diagmon & SFP_DIAGMON_DDM))
1285 return 0;
1286
1287 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
1288 /* This driver in general does not support address
1289 * change.
1290 */
1291 return 0;
1292
Russell King139d3a22019-11-10 14:07:35 +00001293 mod_delayed_work(system_wq, &sfp->hwmon_probe, 1);
1294 sfp->hwmon_tries = R_PROBE_RETRY_SLOW;
Andrew Lunn13230612018-07-17 21:48:13 +02001295
Russell King139d3a22019-11-10 14:07:35 +00001296 return 0;
Andrew Lunn13230612018-07-17 21:48:13 +02001297}
1298
1299static void sfp_hwmon_remove(struct sfp *sfp)
1300{
Russell King139d3a22019-11-10 14:07:35 +00001301 cancel_delayed_work_sync(&sfp->hwmon_probe);
Andrew Lunn3e322472018-09-25 01:50:00 +02001302 if (!IS_ERR_OR_NULL(sfp->hwmon_dev)) {
1303 hwmon_device_unregister(sfp->hwmon_dev);
1304 sfp->hwmon_dev = NULL;
1305 kfree(sfp->hwmon_name);
1306 }
Andrew Lunn13230612018-07-17 21:48:13 +02001307}
Russell King139d3a22019-11-10 14:07:35 +00001308
1309static int sfp_hwmon_init(struct sfp *sfp)
1310{
1311 INIT_DELAYED_WORK(&sfp->hwmon_probe, sfp_hwmon_probe);
1312
1313 return 0;
1314}
1315
1316static void sfp_hwmon_exit(struct sfp *sfp)
1317{
1318 cancel_delayed_work_sync(&sfp->hwmon_probe);
1319}
Andrew Lunn13230612018-07-17 21:48:13 +02001320#else
1321static int sfp_hwmon_insert(struct sfp *sfp)
1322{
1323 return 0;
1324}
1325
1326static void sfp_hwmon_remove(struct sfp *sfp)
1327{
1328}
Russell King139d3a22019-11-10 14:07:35 +00001329
1330static int sfp_hwmon_init(struct sfp *sfp)
1331{
1332 return 0;
1333}
1334
1335static void sfp_hwmon_exit(struct sfp *sfp)
1336{
1337}
Andrew Lunn13230612018-07-17 21:48:13 +02001338#endif
1339
Russell King73970052017-07-25 15:03:39 +01001340/* Helpers */
1341static void sfp_module_tx_disable(struct sfp *sfp)
1342{
1343 dev_dbg(sfp->dev, "tx disable %u -> %u\n",
1344 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1);
1345 sfp->state |= SFP_F_TX_DISABLE;
1346 sfp_set_state(sfp, sfp->state);
1347}
1348
1349static void sfp_module_tx_enable(struct sfp *sfp)
1350{
1351 dev_dbg(sfp->dev, "tx disable %u -> %u\n",
1352 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0);
1353 sfp->state &= ~SFP_F_TX_DISABLE;
1354 sfp_set_state(sfp, sfp->state);
1355}
1356
1357static void sfp_module_tx_fault_reset(struct sfp *sfp)
1358{
1359 unsigned int state = sfp->state;
1360
1361 if (state & SFP_F_TX_DISABLE)
1362 return;
1363
1364 sfp_set_state(sfp, state | SFP_F_TX_DISABLE);
1365
1366 udelay(T_RESET_US);
1367
1368 sfp_set_state(sfp, state);
1369}
1370
1371/* SFP state machine */
1372static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout)
1373{
1374 if (timeout)
1375 mod_delayed_work(system_power_efficient_wq, &sfp->timeout,
1376 timeout);
1377 else
1378 cancel_delayed_work(&sfp->timeout);
1379}
1380
1381static void sfp_sm_next(struct sfp *sfp, unsigned int state,
1382 unsigned int timeout)
1383{
1384 sfp->sm_state = state;
1385 sfp_sm_set_timer(sfp, timeout);
1386}
1387
Russell King0936ebc2019-11-10 14:06:23 +00001388static void sfp_sm_mod_next(struct sfp *sfp, unsigned int state,
Florian Fainelli516b29e2017-10-30 21:42:57 -07001389 unsigned int timeout)
Russell King73970052017-07-25 15:03:39 +01001390{
1391 sfp->sm_mod_state = state;
1392 sfp_sm_set_timer(sfp, timeout);
1393}
1394
1395static void sfp_sm_phy_detach(struct sfp *sfp)
1396{
1397 phy_stop(sfp->mod_phy);
1398 sfp_remove_phy(sfp->sfp_bus);
1399 phy_device_remove(sfp->mod_phy);
1400 phy_device_free(sfp->mod_phy);
1401 sfp->mod_phy = NULL;
1402}
1403
1404static void sfp_sm_probe_phy(struct sfp *sfp)
1405{
1406 struct phy_device *phy;
1407 int err;
1408
Russell King73970052017-07-25 15:03:39 +01001409 phy = mdiobus_scan(sfp->i2c_mii, SFP_PHY_ADDR);
Russell King20b56ed2017-12-15 16:09:36 +00001410 if (phy == ERR_PTR(-ENODEV)) {
1411 dev_info(sfp->dev, "no PHY detected\n");
Russell King73970052017-07-25 15:03:39 +01001412 return;
1413 }
Russell King20b56ed2017-12-15 16:09:36 +00001414 if (IS_ERR(phy)) {
1415 dev_err(sfp->dev, "mdiobus scan returned %ld\n", PTR_ERR(phy));
Russell King73970052017-07-25 15:03:39 +01001416 return;
1417 }
1418
1419 err = sfp_add_phy(sfp->sfp_bus, phy);
1420 if (err) {
1421 phy_device_remove(phy);
1422 phy_device_free(phy);
1423 dev_err(sfp->dev, "sfp_add_phy failed: %d\n", err);
1424 return;
1425 }
1426
1427 sfp->mod_phy = phy;
1428 phy_start(phy);
1429}
1430
1431static void sfp_sm_link_up(struct sfp *sfp)
1432{
1433 sfp_link_up(sfp->sfp_bus);
1434 sfp_sm_next(sfp, SFP_S_LINK_UP, 0);
1435}
1436
1437static void sfp_sm_link_down(struct sfp *sfp)
1438{
1439 sfp_link_down(sfp->sfp_bus);
1440}
1441
1442static void sfp_sm_link_check_los(struct sfp *sfp)
1443{
1444 unsigned int los = sfp->state & SFP_F_LOS;
1445
Russell King710dfbb2017-11-30 13:59:16 +00001446 /* If neither SFP_OPTIONS_LOS_INVERTED nor SFP_OPTIONS_LOS_NORMAL
1447 * are set, we assume that no LOS signal is available.
Russell King73970052017-07-25 15:03:39 +01001448 */
Russell Kingacf1c022017-11-30 13:59:11 +00001449 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED))
Russell King73970052017-07-25 15:03:39 +01001450 los ^= SFP_F_LOS;
Russell King710dfbb2017-11-30 13:59:16 +00001451 else if (!(sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL)))
1452 los = 0;
Russell King73970052017-07-25 15:03:39 +01001453
1454 if (los)
1455 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
1456 else
1457 sfp_sm_link_up(sfp);
1458}
1459
Russell King710dfbb2017-11-30 13:59:16 +00001460static bool sfp_los_event_active(struct sfp *sfp, unsigned int event)
1461{
1462 return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) &&
1463 event == SFP_E_LOS_LOW) ||
1464 (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) &&
1465 event == SFP_E_LOS_HIGH);
1466}
1467
1468static bool sfp_los_event_inactive(struct sfp *sfp, unsigned int event)
1469{
1470 return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) &&
1471 event == SFP_E_LOS_HIGH) ||
1472 (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) &&
1473 event == SFP_E_LOS_LOW);
1474}
1475
Russell King63ec1c72019-11-10 14:07:04 +00001476static void sfp_sm_fault(struct sfp *sfp, unsigned int next_state, bool warn)
Russell King73970052017-07-25 15:03:39 +01001477{
1478 if (sfp->sm_retries && !--sfp->sm_retries) {
Florian Fainelli516b29e2017-10-30 21:42:57 -07001479 dev_err(sfp->dev,
1480 "module persistently indicates fault, disabling\n");
Russell King73970052017-07-25 15:03:39 +01001481 sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0);
1482 } else {
1483 if (warn)
1484 dev_err(sfp->dev, "module transmit fault indicated\n");
1485
Russell King63ec1c72019-11-10 14:07:04 +00001486 sfp_sm_next(sfp, next_state, T_FAULT_RECOVER);
Russell King73970052017-07-25 15:03:39 +01001487 }
1488}
1489
Russell King181f29d2019-11-10 14:06:54 +00001490static void sfp_sm_probe_for_phy(struct sfp *sfp)
1491{
Russell King73970052017-07-25 15:03:39 +01001492 /* Setting the serdes link mode is guesswork: there's no
1493 * field in the EEPROM which indicates what mode should
1494 * be used.
1495 *
1496 * If it's a gigabit-only fiber module, it probably does
1497 * not have a PHY, so switch to 802.3z negotiation mode.
1498 * Otherwise, switch to SGMII mode (which is required to
1499 * support non-gigabit speeds) and probe for a PHY.
1500 */
1501 if (sfp->id.base.e1000_base_t ||
1502 sfp->id.base.e100_base_lx ||
1503 sfp->id.base.e100_base_fx)
1504 sfp_sm_probe_phy(sfp);
1505}
1506
Russell Kinged32abb2019-11-10 14:06:39 +00001507static int sfp_module_parse_power(struct sfp *sfp)
Jon Nettleton3bb35262018-02-27 15:53:12 +00001508{
Russell Kinged32abb2019-11-10 14:06:39 +00001509 u32 power_mW = 1000;
Jon Nettleton3bb35262018-02-27 15:53:12 +00001510
Jon Nettleton3bb35262018-02-27 15:53:12 +00001511 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_POWER_DECL))
Russell Kinged32abb2019-11-10 14:06:39 +00001512 power_mW = 1500;
Jon Nettleton3bb35262018-02-27 15:53:12 +00001513 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL))
Russell Kinged32abb2019-11-10 14:06:39 +00001514 power_mW = 2000;
Jon Nettleton3bb35262018-02-27 15:53:12 +00001515
Russell King7cfa9c92019-11-10 14:06:44 +00001516 if (power_mW > sfp->max_power_mW) {
1517 /* Module power specification exceeds the allowed maximum. */
1518 if (sfp->id.ext.sff8472_compliance ==
1519 SFP_SFF8472_COMPLIANCE_NONE &&
1520 !(sfp->id.ext.diagmon & SFP_DIAGMON_DDM)) {
1521 /* The module appears not to implement bus address
1522 * 0xa2, so assume that the module powers up in the
1523 * indicated mode.
1524 */
Jon Nettleton3bb35262018-02-27 15:53:12 +00001525 dev_err(sfp->dev,
1526 "Host does not support %u.%uW modules\n",
Russell Kinged32abb2019-11-10 14:06:39 +00001527 power_mW / 1000, (power_mW / 100) % 10);
Jon Nettleton3bb35262018-02-27 15:53:12 +00001528 return -EINVAL;
Russell King7cfa9c92019-11-10 14:06:44 +00001529 } else {
1530 dev_warn(sfp->dev,
1531 "Host does not support %u.%uW modules, module left in power mode 1\n",
1532 power_mW / 1000, (power_mW / 100) % 10);
1533 return 0;
Jon Nettleton3bb35262018-02-27 15:53:12 +00001534 }
Jon Nettleton3bb35262018-02-27 15:53:12 +00001535 }
1536
Russell King7cfa9c92019-11-10 14:06:44 +00001537 /* If the module requires a higher power mode, but also requires
1538 * an address change sequence, warn the user that the module may
1539 * not be functional.
1540 */
1541 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE && power_mW > 1000) {
Jon Nettleton3bb35262018-02-27 15:53:12 +00001542 dev_warn(sfp->dev,
Colin Ian King07f23d92019-11-13 09:55:48 +00001543 "Address Change Sequence not supported but module requires %u.%uW, module may not be functional\n",
Russell Kinged32abb2019-11-10 14:06:39 +00001544 power_mW / 1000, (power_mW / 100) % 10);
Jon Nettleton3bb35262018-02-27 15:53:12 +00001545 return 0;
1546 }
1547
Russell Kinged32abb2019-11-10 14:06:39 +00001548 sfp->module_power_mW = power_mW;
1549
1550 return 0;
1551}
1552
Russell Kingb036a552019-11-10 14:07:20 +00001553static int sfp_sm_mod_hpower(struct sfp *sfp, bool enable)
Russell Kinged32abb2019-11-10 14:06:39 +00001554{
1555 u8 val;
1556 int err;
1557
Jon Nettleton3bb35262018-02-27 15:53:12 +00001558 err = sfp_read(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
1559 if (err != sizeof(val)) {
1560 dev_err(sfp->dev, "Failed to read EEPROM: %d\n", err);
Russell Kingb036a552019-11-10 14:07:20 +00001561 return -EAGAIN;
Jon Nettleton3bb35262018-02-27 15:53:12 +00001562 }
1563
Russell Kingb036a552019-11-10 14:07:20 +00001564 if (enable)
1565 val |= BIT(0);
1566 else
1567 val &= ~BIT(0);
Jon Nettleton3bb35262018-02-27 15:53:12 +00001568
1569 err = sfp_write(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
1570 if (err != sizeof(val)) {
1571 dev_err(sfp->dev, "Failed to write EEPROM: %d\n", err);
Russell Kingb036a552019-11-10 14:07:20 +00001572 return -EAGAIN;
Jon Nettleton3bb35262018-02-27 15:53:12 +00001573 }
1574
Russell Kingb036a552019-11-10 14:07:20 +00001575 if (enable)
1576 dev_info(sfp->dev, "Module switched to %u.%uW power level\n",
1577 sfp->module_power_mW / 1000,
1578 (sfp->module_power_mW / 100) % 10);
Jon Nettleton3bb35262018-02-27 15:53:12 +00001579
Russell Kingb036a552019-11-10 14:07:20 +00001580 return 0;
Jon Nettleton3bb35262018-02-27 15:53:12 +00001581}
1582
Russell Kinge117be72019-11-10 14:07:30 +00001583static int sfp_sm_mod_probe(struct sfp *sfp, bool report)
Russell King73970052017-07-25 15:03:39 +01001584{
1585 /* SFP module inserted - read I2C data */
1586 struct sfp_eeprom_id id;
Russell King981f1f82018-03-28 11:18:25 +01001587 bool cotsworks;
Russell King73970052017-07-25 15:03:39 +01001588 u8 check;
Jon Nettleton3bb35262018-02-27 15:53:12 +00001589 int ret;
Russell King73970052017-07-25 15:03:39 +01001590
Jon Nettleton3bb35262018-02-27 15:53:12 +00001591 ret = sfp_read(sfp, false, 0, &id, sizeof(id));
1592 if (ret < 0) {
Russell Kinge117be72019-11-10 14:07:30 +00001593 if (report)
1594 dev_err(sfp->dev, "failed to read EEPROM: %d\n", ret);
Russell King73970052017-07-25 15:03:39 +01001595 return -EAGAIN;
1596 }
1597
Jon Nettleton3bb35262018-02-27 15:53:12 +00001598 if (ret != sizeof(id)) {
1599 dev_err(sfp->dev, "EEPROM short read: %d\n", ret);
Russell King73970052017-07-25 15:03:39 +01001600 return -EAGAIN;
1601 }
1602
Russell King981f1f82018-03-28 11:18:25 +01001603 /* Cotsworks do not seem to update the checksums when they
1604 * do the final programming with the final module part number,
1605 * serial number and date code.
1606 */
1607 cotsworks = !memcmp(id.base.vendor_name, "COTSWORKS ", 16);
1608
Russell King73970052017-07-25 15:03:39 +01001609 /* Validate the checksum over the base structure */
1610 check = sfp_check(&id.base, sizeof(id.base) - 1);
1611 if (check != id.base.cc_base) {
Russell King981f1f82018-03-28 11:18:25 +01001612 if (cotsworks) {
1613 dev_warn(sfp->dev,
1614 "EEPROM base structure checksum failure (0x%02x != 0x%02x)\n",
1615 check, id.base.cc_base);
1616 } else {
1617 dev_err(sfp->dev,
1618 "EEPROM base structure checksum failure: 0x%02x != 0x%02x\n",
1619 check, id.base.cc_base);
1620 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
1621 16, 1, &id, sizeof(id), true);
1622 return -EINVAL;
1623 }
Russell King73970052017-07-25 15:03:39 +01001624 }
1625
1626 check = sfp_check(&id.ext, sizeof(id.ext) - 1);
1627 if (check != id.ext.cc_ext) {
Russell King981f1f82018-03-28 11:18:25 +01001628 if (cotsworks) {
1629 dev_warn(sfp->dev,
1630 "EEPROM extended structure checksum failure (0x%02x != 0x%02x)\n",
1631 check, id.ext.cc_ext);
1632 } else {
1633 dev_err(sfp->dev,
1634 "EEPROM extended structure checksum failure: 0x%02x != 0x%02x\n",
1635 check, id.ext.cc_ext);
1636 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
1637 16, 1, &id, sizeof(id), true);
1638 memset(&id.ext, 0, sizeof(id.ext));
1639 }
Russell King73970052017-07-25 15:03:39 +01001640 }
1641
1642 sfp->id = id;
1643
Russell Kinga2f247e2017-12-29 12:15:12 +00001644 dev_info(sfp->dev, "module %.*s %.*s rev %.*s sn %.*s dc %.*s\n",
1645 (int)sizeof(id.base.vendor_name), id.base.vendor_name,
1646 (int)sizeof(id.base.vendor_pn), id.base.vendor_pn,
1647 (int)sizeof(id.base.vendor_rev), id.base.vendor_rev,
1648 (int)sizeof(id.ext.vendor_sn), id.ext.vendor_sn,
1649 (int)sizeof(id.ext.datecode), id.ext.datecode);
Russell King73970052017-07-25 15:03:39 +01001650
Russell King259c8612017-12-14 10:27:47 +00001651 /* Check whether we support this module */
Russell Kingf3c9a662019-11-20 12:29:59 +00001652 if (!sfp->type->module_supported(&id)) {
Russell King259c8612017-12-14 10:27:47 +00001653 dev_err(sfp->dev,
1654 "module is not supported - phys id 0x%02x 0x%02x\n",
Russell King73970052017-07-25 15:03:39 +01001655 sfp->id.base.phys_id, sfp->id.base.phys_ext_id);
1656 return -EINVAL;
1657 }
1658
Russell Kingec7681b2017-11-30 13:59:21 +00001659 /* If the module requires address swap mode, warn about it */
1660 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
1661 dev_warn(sfp->dev,
1662 "module address swap to access page 0xA2 is not supported.\n");
1663
Russell Kinged32abb2019-11-10 14:06:39 +00001664 /* Parse the module power requirement */
1665 ret = sfp_module_parse_power(sfp);
1666 if (ret < 0)
1667 return ret;
1668
Russell King26c97a22019-12-09 13:40:23 +00001669 if (!memcmp(id.base.vendor_name, "ALCATELLUCENT ", 16) &&
1670 !memcmp(id.base.vendor_pn, "3FE46541AA ", 16))
1671 sfp->module_t_start_up = T_START_UP_BAD_GPON;
1672 else
1673 sfp->module_t_start_up = T_START_UP;
1674
Russell Kingb036a552019-11-10 14:07:20 +00001675 return 0;
Russell King73970052017-07-25 15:03:39 +01001676}
1677
1678static void sfp_sm_mod_remove(struct sfp *sfp)
1679{
Russell King73f5e842019-11-10 14:07:25 +00001680 if (sfp->sm_mod_state > SFP_MOD_WAITDEV)
1681 sfp_module_remove(sfp->sfp_bus);
Russell King73970052017-07-25 15:03:39 +01001682
Andrew Lunn13230612018-07-17 21:48:13 +02001683 sfp_hwmon_remove(sfp);
1684
Russell King73970052017-07-25 15:03:39 +01001685 memset(&sfp->id, 0, sizeof(sfp->id));
Russell Kinged32abb2019-11-10 14:06:39 +00001686 sfp->module_power_mW = 0;
Russell King73970052017-07-25 15:03:39 +01001687
1688 dev_info(sfp->dev, "module removed\n");
1689}
1690
Russell King6b0da5c2019-11-10 14:07:14 +00001691/* This state machine tracks the upstream's state */
Russell Kinge85d81a2019-11-10 14:06:13 +00001692static void sfp_sm_device(struct sfp *sfp, unsigned int event)
Russell King73970052017-07-25 15:03:39 +01001693{
Russell Kinge85d81a2019-11-10 14:06:13 +00001694 switch (sfp->sm_dev_state) {
1695 default:
Russell King6b0da5c2019-11-10 14:07:14 +00001696 if (event == SFP_E_DEV_ATTACH)
1697 sfp->sm_dev_state = SFP_DEV_DOWN;
1698 break;
1699
1700 case SFP_DEV_DOWN:
1701 if (event == SFP_E_DEV_DETACH)
1702 sfp->sm_dev_state = SFP_DEV_DETACHED;
1703 else if (event == SFP_E_DEV_UP)
Russell Kinge85d81a2019-11-10 14:06:13 +00001704 sfp->sm_dev_state = SFP_DEV_UP;
1705 break;
Russell King73970052017-07-25 15:03:39 +01001706
Russell Kinge85d81a2019-11-10 14:06:13 +00001707 case SFP_DEV_UP:
Russell King6b0da5c2019-11-10 14:07:14 +00001708 if (event == SFP_E_DEV_DETACH)
1709 sfp->sm_dev_state = SFP_DEV_DETACHED;
1710 else if (event == SFP_E_DEV_DOWN)
Russell Kinge85d81a2019-11-10 14:06:13 +00001711 sfp->sm_dev_state = SFP_DEV_DOWN;
Russell Kinge85d81a2019-11-10 14:06:13 +00001712 break;
1713 }
1714}
Russell King73970052017-07-25 15:03:39 +01001715
Russell Kingd9009542019-11-10 14:06:33 +00001716/* This state machine tracks the insert/remove state of the module, probes
1717 * the on-board EEPROM, and sets up the power level.
Russell Kinge85d81a2019-11-10 14:06:13 +00001718 */
1719static void sfp_sm_module(struct sfp *sfp, unsigned int event)
1720{
Russell Kingb036a552019-11-10 14:07:20 +00001721 int err;
1722
1723 /* Handle remove event globally, it resets this state machine */
1724 if (event == SFP_E_REMOVE) {
Russell Kingd2e816c2019-11-10 14:06:28 +00001725 if (sfp->sm_mod_state > SFP_MOD_PROBE)
1726 sfp_sm_mod_remove(sfp);
Russell Kingb036a552019-11-10 14:07:20 +00001727 sfp_sm_mod_next(sfp, SFP_MOD_EMPTY, 0);
1728 return;
1729 }
1730
1731 /* Handle device detach globally */
Russell King73f5e842019-11-10 14:07:25 +00001732 if (sfp->sm_dev_state < SFP_DEV_DOWN &&
1733 sfp->sm_mod_state > SFP_MOD_WAITDEV) {
Russell Kingb036a552019-11-10 14:07:20 +00001734 if (sfp->module_power_mW > 1000 &&
1735 sfp->sm_mod_state > SFP_MOD_HPOWER)
1736 sfp_sm_mod_hpower(sfp, false);
Russell King73f5e842019-11-10 14:07:25 +00001737 sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0);
Russell Kingd2e816c2019-11-10 14:06:28 +00001738 return;
1739 }
1740
Russell King73970052017-07-25 15:03:39 +01001741 switch (sfp->sm_mod_state) {
1742 default:
Russell Kinge117be72019-11-10 14:07:30 +00001743 if (event == SFP_E_INSERT) {
Russell Kingd9009542019-11-10 14:06:33 +00001744 sfp_sm_mod_next(sfp, SFP_MOD_PROBE, T_SERIAL);
Russell Kinge117be72019-11-10 14:07:30 +00001745 sfp->sm_mod_tries_init = R_PROBE_RETRY_INIT;
1746 sfp->sm_mod_tries = R_PROBE_RETRY_SLOW;
1747 }
Russell King73970052017-07-25 15:03:39 +01001748 break;
1749
1750 case SFP_MOD_PROBE:
Russell King73f5e842019-11-10 14:07:25 +00001751 /* Wait for T_PROBE_INIT to time out */
Russell Kingb036a552019-11-10 14:07:20 +00001752 if (event != SFP_E_TIMEOUT)
1753 break;
Russell King73970052017-07-25 15:03:39 +01001754
Russell Kinge117be72019-11-10 14:07:30 +00001755 err = sfp_sm_mod_probe(sfp, sfp->sm_mod_tries == 1);
Russell Kingb036a552019-11-10 14:07:20 +00001756 if (err == -EAGAIN) {
Russell Kinge117be72019-11-10 14:07:30 +00001757 if (sfp->sm_mod_tries_init &&
1758 --sfp->sm_mod_tries_init) {
1759 sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT);
1760 break;
1761 } else if (sfp->sm_mod_tries && --sfp->sm_mod_tries) {
1762 if (sfp->sm_mod_tries == R_PROBE_RETRY_SLOW - 1)
1763 dev_warn(sfp->dev,
1764 "please wait, module slow to respond\n");
1765 sfp_sm_set_timer(sfp, T_PROBE_RETRY_SLOW);
1766 break;
1767 }
Jon Nettleton3bb35262018-02-27 15:53:12 +00001768 }
Russell Kingb036a552019-11-10 14:07:20 +00001769 if (err < 0) {
1770 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
1771 break;
1772 }
1773
Russell King38ecd702019-12-03 23:51:28 +00001774 err = sfp_hwmon_insert(sfp);
1775 if (err)
1776 dev_warn(sfp->dev, "hwmon probe failed: %d\n", err);
1777
Russell King73f5e842019-11-10 14:07:25 +00001778 sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0);
1779 /* fall through */
1780 case SFP_MOD_WAITDEV:
1781 /* Ensure that the device is attached before proceeding */
1782 if (sfp->sm_dev_state < SFP_DEV_DOWN)
1783 break;
1784
1785 /* Report the module insertion to the upstream device */
1786 err = sfp_module_insert(sfp->sfp_bus, &sfp->id);
1787 if (err < 0) {
1788 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
1789 break;
1790 }
1791
Russell Kingb036a552019-11-10 14:07:20 +00001792 /* If this is a power level 1 module, we are done */
1793 if (sfp->module_power_mW <= 1000)
1794 goto insert;
1795
1796 sfp_sm_mod_next(sfp, SFP_MOD_HPOWER, 0);
1797 /* fall through */
1798 case SFP_MOD_HPOWER:
1799 /* Enable high power mode */
1800 err = sfp_sm_mod_hpower(sfp, true);
Russell King73f5e842019-11-10 14:07:25 +00001801 if (err < 0) {
1802 if (err != -EAGAIN) {
1803 sfp_module_remove(sfp->sfp_bus);
1804 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
1805 } else {
Russell Kinge117be72019-11-10 14:07:30 +00001806 sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT);
Russell King73f5e842019-11-10 14:07:25 +00001807 }
1808 break;
1809 }
1810
1811 sfp_sm_mod_next(sfp, SFP_MOD_WAITPWR, T_HPOWER_LEVEL);
Russell Kingb036a552019-11-10 14:07:20 +00001812 break;
1813
1814 case SFP_MOD_WAITPWR:
1815 /* Wait for T_HPOWER_LEVEL to time out */
1816 if (event != SFP_E_TIMEOUT)
1817 break;
1818
1819 insert:
1820 sfp_sm_mod_next(sfp, SFP_MOD_PRESENT, 0);
1821 break;
1822
Russell King73970052017-07-25 15:03:39 +01001823 case SFP_MOD_PRESENT:
1824 case SFP_MOD_ERROR:
Russell King73970052017-07-25 15:03:39 +01001825 break;
1826 }
Russell Kinge85d81a2019-11-10 14:06:13 +00001827}
Russell King73970052017-07-25 15:03:39 +01001828
Russell Kinge85d81a2019-11-10 14:06:13 +00001829static void sfp_sm_main(struct sfp *sfp, unsigned int event)
1830{
Russell Kingeefa6f12019-11-10 14:06:59 +00001831 unsigned long timeout;
1832
Russell King73970052017-07-25 15:03:39 +01001833 /* Some events are global */
1834 if (sfp->sm_state != SFP_S_DOWN &&
1835 (sfp->sm_mod_state != SFP_MOD_PRESENT ||
1836 sfp->sm_dev_state != SFP_DEV_UP)) {
1837 if (sfp->sm_state == SFP_S_LINK_UP &&
1838 sfp->sm_dev_state == SFP_DEV_UP)
1839 sfp_sm_link_down(sfp);
1840 if (sfp->mod_phy)
1841 sfp_sm_phy_detach(sfp);
Russell King1539e0d2019-11-10 14:06:18 +00001842 sfp_module_tx_disable(sfp);
Russell Kingf3c9a662019-11-20 12:29:59 +00001843 sfp_soft_stop_poll(sfp);
Russell King73970052017-07-25 15:03:39 +01001844 sfp_sm_next(sfp, SFP_S_DOWN, 0);
Russell King73970052017-07-25 15:03:39 +01001845 return;
1846 }
1847
1848 /* The main state machine */
1849 switch (sfp->sm_state) {
1850 case SFP_S_DOWN:
Russell Kingeefa6f12019-11-10 14:06:59 +00001851 if (sfp->sm_mod_state != SFP_MOD_PRESENT ||
1852 sfp->sm_dev_state != SFP_DEV_UP)
1853 break;
Russell King181f29d2019-11-10 14:06:54 +00001854
Russell Kingf3c9a662019-11-20 12:29:59 +00001855 if (!(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE))
1856 sfp_soft_start_poll(sfp);
1857
1858 sfp_module_tx_enable(sfp);
Russell Kingeefa6f12019-11-10 14:06:59 +00001859
1860 /* Initialise the fault clearance retries */
1861 sfp->sm_retries = 5;
1862
1863 /* We need to check the TX_FAULT state, which is not defined
1864 * while TX_DISABLE is asserted. The earliest we want to do
1865 * anything (such as probe for a PHY) is 50ms.
1866 */
1867 sfp_sm_next(sfp, SFP_S_WAIT, T_WAIT);
1868 break;
1869
1870 case SFP_S_WAIT:
1871 if (event != SFP_E_TIMEOUT)
1872 break;
1873
Russell Kingeefa6f12019-11-10 14:06:59 +00001874 if (sfp->state & SFP_F_TX_FAULT) {
Russell King26c97a22019-12-09 13:40:23 +00001875 /* Wait up to t_init (SFF-8472) or t_start_up (SFF-8431)
1876 * from the TX_DISABLE deassertion for the module to
1877 * initialise, which is indicated by TX_FAULT
1878 * deasserting.
Russell King181f29d2019-11-10 14:06:54 +00001879 */
Russell King26c97a22019-12-09 13:40:23 +00001880 timeout = sfp->module_t_start_up;
Russell Kingeefa6f12019-11-10 14:06:59 +00001881 if (timeout > T_WAIT)
1882 timeout -= T_WAIT;
1883 else
1884 timeout = 1;
1885
1886 sfp_sm_next(sfp, SFP_S_INIT, timeout);
1887 } else {
1888 /* TX_FAULT is not asserted, assume the module has
1889 * finished initialising.
1890 */
1891 goto init_done;
Russell King181f29d2019-11-10 14:06:54 +00001892 }
Russell King73970052017-07-25 15:03:39 +01001893 break;
1894
1895 case SFP_S_INIT:
Russell Kingd23751a2019-11-10 14:07:09 +00001896 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
Russell King26c97a22019-12-09 13:40:23 +00001897 /* TX_FAULT is still asserted after t_init or
1898 * or t_start_up, so assume there is a fault.
Russell Kingd23751a2019-11-10 14:07:09 +00001899 */
1900 sfp_sm_fault(sfp, SFP_S_INIT_TX_FAULT,
1901 sfp->sm_retries == 5);
1902 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
1903 init_done: /* TX_FAULT deasserted or we timed out with TX_FAULT
1904 * clear. Probe for the PHY and check the LOS state.
1905 */
1906 sfp_sm_probe_for_phy(sfp);
1907 sfp_sm_link_check_los(sfp);
1908
1909 /* Reset the fault retry count */
1910 sfp->sm_retries = 5;
1911 }
1912 break;
1913
1914 case SFP_S_INIT_TX_FAULT:
1915 if (event == SFP_E_TIMEOUT) {
1916 sfp_module_tx_fault_reset(sfp);
Russell King26c97a22019-12-09 13:40:23 +00001917 sfp_sm_next(sfp, SFP_S_INIT, sfp->module_t_start_up);
Russell Kingd23751a2019-11-10 14:07:09 +00001918 }
Russell King73970052017-07-25 15:03:39 +01001919 break;
1920
1921 case SFP_S_WAIT_LOS:
1922 if (event == SFP_E_TX_FAULT)
Russell King63ec1c72019-11-10 14:07:04 +00001923 sfp_sm_fault(sfp, SFP_S_TX_FAULT, true);
Russell King710dfbb2017-11-30 13:59:16 +00001924 else if (sfp_los_event_inactive(sfp, event))
Russell King73970052017-07-25 15:03:39 +01001925 sfp_sm_link_up(sfp);
1926 break;
1927
1928 case SFP_S_LINK_UP:
1929 if (event == SFP_E_TX_FAULT) {
1930 sfp_sm_link_down(sfp);
Russell King63ec1c72019-11-10 14:07:04 +00001931 sfp_sm_fault(sfp, SFP_S_TX_FAULT, true);
Russell King710dfbb2017-11-30 13:59:16 +00001932 } else if (sfp_los_event_active(sfp, event)) {
Russell King73970052017-07-25 15:03:39 +01001933 sfp_sm_link_down(sfp);
1934 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
1935 }
1936 break;
1937
1938 case SFP_S_TX_FAULT:
1939 if (event == SFP_E_TIMEOUT) {
1940 sfp_module_tx_fault_reset(sfp);
Russell King26c97a22019-12-09 13:40:23 +00001941 sfp_sm_next(sfp, SFP_S_REINIT, sfp->module_t_start_up);
Russell King73970052017-07-25 15:03:39 +01001942 }
1943 break;
1944
1945 case SFP_S_REINIT:
1946 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
Russell King63ec1c72019-11-10 14:07:04 +00001947 sfp_sm_fault(sfp, SFP_S_TX_FAULT, false);
Russell King73970052017-07-25 15:03:39 +01001948 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
1949 dev_info(sfp->dev, "module transmit fault recovered\n");
1950 sfp_sm_link_check_los(sfp);
1951 }
1952 break;
1953
1954 case SFP_S_TX_DISABLE:
1955 break;
1956 }
Russell Kinge85d81a2019-11-10 14:06:13 +00001957}
1958
1959static void sfp_sm_event(struct sfp *sfp, unsigned int event)
1960{
1961 mutex_lock(&sfp->sm_mutex);
1962
1963 dev_dbg(sfp->dev, "SM: enter %s:%s:%s event %s\n",
1964 mod_state_to_str(sfp->sm_mod_state),
1965 dev_state_to_str(sfp->sm_dev_state),
1966 sm_state_to_str(sfp->sm_state),
1967 event_to_str(event));
1968
Russell Kinge85d81a2019-11-10 14:06:13 +00001969 sfp_sm_device(sfp, event);
Russell King6b0da5c2019-11-10 14:07:14 +00001970 sfp_sm_module(sfp, event);
Russell Kinge85d81a2019-11-10 14:06:13 +00001971 sfp_sm_main(sfp, event);
Russell King73970052017-07-25 15:03:39 +01001972
Andrew Lunn4005a7c2018-08-08 20:54:12 +02001973 dev_dbg(sfp->dev, "SM: exit %s:%s:%s\n",
1974 mod_state_to_str(sfp->sm_mod_state),
1975 dev_state_to_str(sfp->sm_dev_state),
1976 sm_state_to_str(sfp->sm_state));
Russell King73970052017-07-25 15:03:39 +01001977
1978 mutex_unlock(&sfp->sm_mutex);
1979}
1980
Russell Kingb5bfc212019-02-06 10:52:30 +00001981static void sfp_attach(struct sfp *sfp)
1982{
Russell King6b0da5c2019-11-10 14:07:14 +00001983 sfp_sm_event(sfp, SFP_E_DEV_ATTACH);
Russell Kingb5bfc212019-02-06 10:52:30 +00001984}
1985
1986static void sfp_detach(struct sfp *sfp)
1987{
Russell King6b0da5c2019-11-10 14:07:14 +00001988 sfp_sm_event(sfp, SFP_E_DEV_DETACH);
Russell Kingb5bfc212019-02-06 10:52:30 +00001989}
1990
Russell King73970052017-07-25 15:03:39 +01001991static void sfp_start(struct sfp *sfp)
1992{
1993 sfp_sm_event(sfp, SFP_E_DEV_UP);
1994}
1995
1996static void sfp_stop(struct sfp *sfp)
1997{
1998 sfp_sm_event(sfp, SFP_E_DEV_DOWN);
1999}
2000
2001static int sfp_module_info(struct sfp *sfp, struct ethtool_modinfo *modinfo)
2002{
2003 /* locking... and check module is present */
2004
Russell Kingec7681b2017-11-30 13:59:21 +00002005 if (sfp->id.ext.sff8472_compliance &&
2006 !(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)) {
Russell King73970052017-07-25 15:03:39 +01002007 modinfo->type = ETH_MODULE_SFF_8472;
2008 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
2009 } else {
2010 modinfo->type = ETH_MODULE_SFF_8079;
2011 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
2012 }
2013 return 0;
2014}
2015
2016static int sfp_module_eeprom(struct sfp *sfp, struct ethtool_eeprom *ee,
Florian Fainelli516b29e2017-10-30 21:42:57 -07002017 u8 *data)
Russell King73970052017-07-25 15:03:39 +01002018{
2019 unsigned int first, last, len;
2020 int ret;
2021
2022 if (ee->len == 0)
2023 return -EINVAL;
2024
2025 first = ee->offset;
2026 last = ee->offset + ee->len;
2027 if (first < ETH_MODULE_SFF_8079_LEN) {
2028 len = min_t(unsigned int, last, ETH_MODULE_SFF_8079_LEN);
2029 len -= first;
2030
Russell King2794ffc2017-12-15 16:09:41 +00002031 ret = sfp_read(sfp, false, first, data, len);
Russell King73970052017-07-25 15:03:39 +01002032 if (ret < 0)
2033 return ret;
2034
2035 first += len;
2036 data += len;
2037 }
Russell King2794ffc2017-12-15 16:09:41 +00002038 if (first < ETH_MODULE_SFF_8472_LEN && last > ETH_MODULE_SFF_8079_LEN) {
Russell King73970052017-07-25 15:03:39 +01002039 len = min_t(unsigned int, last, ETH_MODULE_SFF_8472_LEN);
2040 len -= first;
2041 first -= ETH_MODULE_SFF_8079_LEN;
2042
Russell King2794ffc2017-12-15 16:09:41 +00002043 ret = sfp_read(sfp, true, first, data, len);
Russell King73970052017-07-25 15:03:39 +01002044 if (ret < 0)
2045 return ret;
2046 }
2047 return 0;
2048}
2049
2050static const struct sfp_socket_ops sfp_module_ops = {
Russell Kingb5bfc212019-02-06 10:52:30 +00002051 .attach = sfp_attach,
2052 .detach = sfp_detach,
Russell King73970052017-07-25 15:03:39 +01002053 .start = sfp_start,
2054 .stop = sfp_stop,
2055 .module_info = sfp_module_info,
2056 .module_eeprom = sfp_module_eeprom,
2057};
2058
2059static void sfp_timeout(struct work_struct *work)
2060{
2061 struct sfp *sfp = container_of(work, struct sfp, timeout.work);
2062
2063 rtnl_lock();
2064 sfp_sm_event(sfp, SFP_E_TIMEOUT);
2065 rtnl_unlock();
2066}
2067
2068static void sfp_check_state(struct sfp *sfp)
2069{
2070 unsigned int state, i, changed;
2071
Robert Hancock2158e852019-06-07 10:42:36 -06002072 mutex_lock(&sfp->st_mutex);
Russell King73970052017-07-25 15:03:39 +01002073 state = sfp_get_state(sfp);
2074 changed = state ^ sfp->state;
2075 changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT;
2076
2077 for (i = 0; i < GPIO_MAX; i++)
2078 if (changed & BIT(i))
2079 dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_of_names[i],
2080 !!(sfp->state & BIT(i)), !!(state & BIT(i)));
2081
2082 state |= sfp->state & (SFP_F_TX_DISABLE | SFP_F_RATE_SELECT);
2083 sfp->state = state;
2084
2085 rtnl_lock();
2086 if (changed & SFP_F_PRESENT)
2087 sfp_sm_event(sfp, state & SFP_F_PRESENT ?
2088 SFP_E_INSERT : SFP_E_REMOVE);
2089
2090 if (changed & SFP_F_TX_FAULT)
2091 sfp_sm_event(sfp, state & SFP_F_TX_FAULT ?
2092 SFP_E_TX_FAULT : SFP_E_TX_CLEAR);
2093
2094 if (changed & SFP_F_LOS)
2095 sfp_sm_event(sfp, state & SFP_F_LOS ?
2096 SFP_E_LOS_HIGH : SFP_E_LOS_LOW);
2097 rtnl_unlock();
Robert Hancock2158e852019-06-07 10:42:36 -06002098 mutex_unlock(&sfp->st_mutex);
Russell King73970052017-07-25 15:03:39 +01002099}
2100
2101static irqreturn_t sfp_irq(int irq, void *data)
2102{
2103 struct sfp *sfp = data;
2104
2105 sfp_check_state(sfp);
2106
2107 return IRQ_HANDLED;
2108}
2109
2110static void sfp_poll(struct work_struct *work)
2111{
2112 struct sfp *sfp = container_of(work, struct sfp, poll.work);
2113
2114 sfp_check_state(sfp);
Russell Kingf3c9a662019-11-20 12:29:59 +00002115
2116 if (sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT) ||
2117 sfp->need_poll)
2118 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
Russell King73970052017-07-25 15:03:39 +01002119}
2120
2121static struct sfp *sfp_alloc(struct device *dev)
2122{
2123 struct sfp *sfp;
2124
2125 sfp = kzalloc(sizeof(*sfp), GFP_KERNEL);
2126 if (!sfp)
2127 return ERR_PTR(-ENOMEM);
2128
2129 sfp->dev = dev;
2130
2131 mutex_init(&sfp->sm_mutex);
Robert Hancock2158e852019-06-07 10:42:36 -06002132 mutex_init(&sfp->st_mutex);
Russell King73970052017-07-25 15:03:39 +01002133 INIT_DELAYED_WORK(&sfp->poll, sfp_poll);
2134 INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout);
2135
Russell King139d3a22019-11-10 14:07:35 +00002136 sfp_hwmon_init(sfp);
2137
Russell King73970052017-07-25 15:03:39 +01002138 return sfp;
2139}
2140
2141static void sfp_cleanup(void *data)
2142{
2143 struct sfp *sfp = data;
2144
Russell King139d3a22019-11-10 14:07:35 +00002145 sfp_hwmon_exit(sfp);
2146
Russell King73970052017-07-25 15:03:39 +01002147 cancel_delayed_work_sync(&sfp->poll);
2148 cancel_delayed_work_sync(&sfp->timeout);
2149 if (sfp->i2c_mii) {
2150 mdiobus_unregister(sfp->i2c_mii);
2151 mdiobus_free(sfp->i2c_mii);
2152 }
2153 if (sfp->i2c)
2154 i2c_put_adapter(sfp->i2c);
2155 kfree(sfp);
2156}
2157
2158static int sfp_probe(struct platform_device *pdev)
2159{
Russell King259c8612017-12-14 10:27:47 +00002160 const struct sff_data *sff;
Ruslan Babayev7ce236fa2019-05-28 16:02:33 -07002161 struct i2c_adapter *i2c;
Russell King73970052017-07-25 15:03:39 +01002162 struct sfp *sfp;
Robert Hancock257c2552019-06-07 10:42:35 -06002163 int err, i;
Russell King73970052017-07-25 15:03:39 +01002164
2165 sfp = sfp_alloc(&pdev->dev);
2166 if (IS_ERR(sfp))
2167 return PTR_ERR(sfp);
2168
2169 platform_set_drvdata(pdev, sfp);
2170
2171 err = devm_add_action(sfp->dev, sfp_cleanup, sfp);
2172 if (err < 0)
2173 return err;
2174
Russell King259c8612017-12-14 10:27:47 +00002175 sff = sfp->type = &sfp_data;
2176
Russell King73970052017-07-25 15:03:39 +01002177 if (pdev->dev.of_node) {
2178 struct device_node *node = pdev->dev.of_node;
Russell King259c8612017-12-14 10:27:47 +00002179 const struct of_device_id *id;
Russell King73970052017-07-25 15:03:39 +01002180 struct device_node *np;
2181
Russell King259c8612017-12-14 10:27:47 +00002182 id = of_match_node(sfp_of_match, node);
2183 if (WARN_ON(!id))
2184 return -EINVAL;
2185
2186 sff = sfp->type = id->data;
2187
Russell King73970052017-07-25 15:03:39 +01002188 np = of_parse_phandle(node, "i2c-bus", 0);
Antoine Tenart66ede1f2018-05-22 12:18:00 +02002189 if (!np) {
2190 dev_err(sfp->dev, "missing 'i2c-bus' property\n");
2191 return -ENODEV;
2192 }
Russell King73970052017-07-25 15:03:39 +01002193
Antoine Tenart66ede1f2018-05-22 12:18:00 +02002194 i2c = of_find_i2c_adapter_by_node(np);
2195 of_node_put(np);
Ruslan Babayev7ce236fa2019-05-28 16:02:33 -07002196 } else if (has_acpi_companion(&pdev->dev)) {
2197 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
2198 struct fwnode_handle *fw = acpi_fwnode_handle(adev);
2199 struct fwnode_reference_args args;
2200 struct acpi_handle *acpi_handle;
2201 int ret;
Russell King73970052017-07-25 15:03:39 +01002202
Ruslan Babayev7ce236fa2019-05-28 16:02:33 -07002203 ret = acpi_node_get_property_reference(fw, "i2c-bus", 0, &args);
Dan Carpenter1086ca32019-06-13 09:51:02 +03002204 if (ret || !is_acpi_device_node(args.fwnode)) {
Ruslan Babayev7ce236fa2019-05-28 16:02:33 -07002205 dev_err(&pdev->dev, "missing 'i2c-bus' property\n");
2206 return -ENODEV;
Russell King73970052017-07-25 15:03:39 +01002207 }
Ruslan Babayev7ce236fa2019-05-28 16:02:33 -07002208
2209 acpi_handle = ACPI_HANDLE_FWNODE(args.fwnode);
2210 i2c = i2c_acpi_find_adapter_by_handle(acpi_handle);
2211 } else {
2212 return -EINVAL;
2213 }
2214
2215 if (!i2c)
2216 return -EPROBE_DEFER;
2217
2218 err = sfp_i2c_configure(sfp, i2c);
2219 if (err < 0) {
2220 i2c_put_adapter(i2c);
2221 return err;
Russell King259c8612017-12-14 10:27:47 +00002222 }
Russell King73970052017-07-25 15:03:39 +01002223
Russell King259c8612017-12-14 10:27:47 +00002224 for (i = 0; i < GPIO_MAX; i++)
2225 if (sff->gpios & BIT(i)) {
Russell King73970052017-07-25 15:03:39 +01002226 sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev,
2227 gpio_of_names[i], gpio_flags[i]);
2228 if (IS_ERR(sfp->gpio[i]))
2229 return PTR_ERR(sfp->gpio[i]);
2230 }
2231
Russell King259c8612017-12-14 10:27:47 +00002232 sfp->get_state = sfp_gpio_get_state;
2233 sfp->set_state = sfp_gpio_set_state;
2234
2235 /* Modules that have no detect signal are always present */
2236 if (!(sfp->gpio[GPIO_MODDEF0]))
2237 sfp->get_state = sff_gpio_get_state;
Russell King73970052017-07-25 15:03:39 +01002238
Jon Nettleton3bb35262018-02-27 15:53:12 +00002239 device_property_read_u32(&pdev->dev, "maximum-power-milliwatt",
2240 &sfp->max_power_mW);
2241 if (!sfp->max_power_mW)
2242 sfp->max_power_mW = 1000;
2243
2244 dev_info(sfp->dev, "Host maximum power %u.%uW\n",
2245 sfp->max_power_mW / 1000, (sfp->max_power_mW / 100) % 10);
2246
Russell King73970052017-07-25 15:03:39 +01002247 /* Get the initial state, and always signal TX disable,
2248 * since the network interface will not be up.
2249 */
2250 sfp->state = sfp_get_state(sfp) | SFP_F_TX_DISABLE;
2251
2252 if (sfp->gpio[GPIO_RATE_SELECT] &&
2253 gpiod_get_value_cansleep(sfp->gpio[GPIO_RATE_SELECT]))
2254 sfp->state |= SFP_F_RATE_SELECT;
2255 sfp_set_state(sfp, sfp->state);
2256 sfp_module_tx_disable(sfp);
Russell King73f5e842019-11-10 14:07:25 +00002257 if (sfp->state & SFP_F_PRESENT) {
2258 rtnl_lock();
2259 sfp_sm_event(sfp, SFP_E_INSERT);
2260 rtnl_unlock();
2261 }
Russell King73970052017-07-25 15:03:39 +01002262
2263 for (i = 0; i < GPIO_MAX; i++) {
2264 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
2265 continue;
2266
Robert Hancock257c2552019-06-07 10:42:35 -06002267 sfp->gpio_irq[i] = gpiod_to_irq(sfp->gpio[i]);
2268 if (!sfp->gpio_irq[i]) {
Russell Kingf3c9a662019-11-20 12:29:59 +00002269 sfp->need_poll = true;
Russell King73970052017-07-25 15:03:39 +01002270 continue;
2271 }
2272
Robert Hancock257c2552019-06-07 10:42:35 -06002273 err = devm_request_threaded_irq(sfp->dev, sfp->gpio_irq[i],
2274 NULL, sfp_irq,
Russell King73970052017-07-25 15:03:39 +01002275 IRQF_ONESHOT |
2276 IRQF_TRIGGER_RISING |
2277 IRQF_TRIGGER_FALLING,
2278 dev_name(sfp->dev), sfp);
Robert Hancock257c2552019-06-07 10:42:35 -06002279 if (err) {
2280 sfp->gpio_irq[i] = 0;
Russell Kingf3c9a662019-11-20 12:29:59 +00002281 sfp->need_poll = true;
Robert Hancock257c2552019-06-07 10:42:35 -06002282 }
Russell King73970052017-07-25 15:03:39 +01002283 }
2284
Russell Kingf3c9a662019-11-20 12:29:59 +00002285 if (sfp->need_poll)
Russell King73970052017-07-25 15:03:39 +01002286 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
2287
Antoine Tenarta1f5d1f2018-05-22 12:17:59 +02002288 /* We could have an issue in cases no Tx disable pin is available or
2289 * wired as modules using a laser as their light source will continue to
2290 * be active when the fiber is removed. This could be a safety issue and
2291 * we should at least warn the user about that.
2292 */
2293 if (!sfp->gpio[GPIO_TX_DISABLE])
2294 dev_warn(sfp->dev,
2295 "No tx_disable pin: SFP modules will always be emitting.\n");
2296
Russell Kingb5bfc212019-02-06 10:52:30 +00002297 sfp->sfp_bus = sfp_register_socket(sfp->dev, sfp, &sfp_module_ops);
2298 if (!sfp->sfp_bus)
2299 return -ENOMEM;
2300
Russell King73970052017-07-25 15:03:39 +01002301 return 0;
2302}
2303
2304static int sfp_remove(struct platform_device *pdev)
2305{
2306 struct sfp *sfp = platform_get_drvdata(pdev);
2307
2308 sfp_unregister_socket(sfp->sfp_bus);
2309
Russell King0cb96b52019-12-03 23:51:22 +00002310 rtnl_lock();
2311 sfp_sm_event(sfp, SFP_E_REMOVE);
2312 rtnl_unlock();
2313
Russell King73970052017-07-25 15:03:39 +01002314 return 0;
2315}
2316
Robert Hancock257c2552019-06-07 10:42:35 -06002317static void sfp_shutdown(struct platform_device *pdev)
2318{
2319 struct sfp *sfp = platform_get_drvdata(pdev);
2320 int i;
2321
2322 for (i = 0; i < GPIO_MAX; i++) {
2323 if (!sfp->gpio_irq[i])
2324 continue;
2325
2326 devm_free_irq(sfp->dev, sfp->gpio_irq[i], sfp);
2327 }
2328
2329 cancel_delayed_work_sync(&sfp->poll);
2330 cancel_delayed_work_sync(&sfp->timeout);
2331}
2332
Russell King73970052017-07-25 15:03:39 +01002333static struct platform_driver sfp_driver = {
2334 .probe = sfp_probe,
2335 .remove = sfp_remove,
Robert Hancock257c2552019-06-07 10:42:35 -06002336 .shutdown = sfp_shutdown,
Russell King73970052017-07-25 15:03:39 +01002337 .driver = {
2338 .name = "sfp",
2339 .of_match_table = sfp_of_match,
2340 },
2341};
2342
2343static int sfp_init(void)
2344{
2345 poll_jiffies = msecs_to_jiffies(100);
2346
2347 return platform_driver_register(&sfp_driver);
2348}
2349module_init(sfp_init);
2350
2351static void sfp_exit(void)
2352{
2353 platform_driver_unregister(&sfp_driver);
2354}
2355module_exit(sfp_exit);
2356
2357MODULE_ALIAS("platform:sfp");
2358MODULE_AUTHOR("Russell King");
2359MODULE_LICENSE("GPL v2");