Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame^] | 1 | #include <linux/delay.h> |
| 2 | #include <linux/gpio.h> |
| 3 | #include <linux/i2c.h> |
| 4 | #include <linux/interrupt.h> |
| 5 | #include <linux/jiffies.h> |
| 6 | #include <linux/module.h> |
| 7 | #include <linux/mutex.h> |
| 8 | #include <linux/of.h> |
| 9 | #include <linux/phy.h> |
| 10 | #include <linux/platform_device.h> |
| 11 | #include <linux/rtnetlink.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/workqueue.h> |
| 14 | |
| 15 | #include "mdio-i2c.h" |
| 16 | #include "sfp.h" |
| 17 | #include "swphy.h" |
| 18 | |
| 19 | enum { |
| 20 | GPIO_MODDEF0, |
| 21 | GPIO_LOS, |
| 22 | GPIO_TX_FAULT, |
| 23 | GPIO_TX_DISABLE, |
| 24 | GPIO_RATE_SELECT, |
| 25 | GPIO_MAX, |
| 26 | |
| 27 | SFP_F_PRESENT = BIT(GPIO_MODDEF0), |
| 28 | SFP_F_LOS = BIT(GPIO_LOS), |
| 29 | SFP_F_TX_FAULT = BIT(GPIO_TX_FAULT), |
| 30 | SFP_F_TX_DISABLE = BIT(GPIO_TX_DISABLE), |
| 31 | SFP_F_RATE_SELECT = BIT(GPIO_RATE_SELECT), |
| 32 | |
| 33 | SFP_E_INSERT = 0, |
| 34 | SFP_E_REMOVE, |
| 35 | SFP_E_DEV_DOWN, |
| 36 | SFP_E_DEV_UP, |
| 37 | SFP_E_TX_FAULT, |
| 38 | SFP_E_TX_CLEAR, |
| 39 | SFP_E_LOS_HIGH, |
| 40 | SFP_E_LOS_LOW, |
| 41 | SFP_E_TIMEOUT, |
| 42 | |
| 43 | SFP_MOD_EMPTY = 0, |
| 44 | SFP_MOD_PROBE, |
| 45 | SFP_MOD_PRESENT, |
| 46 | SFP_MOD_ERROR, |
| 47 | |
| 48 | SFP_DEV_DOWN = 0, |
| 49 | SFP_DEV_UP, |
| 50 | |
| 51 | SFP_S_DOWN = 0, |
| 52 | SFP_S_INIT, |
| 53 | SFP_S_WAIT_LOS, |
| 54 | SFP_S_LINK_UP, |
| 55 | SFP_S_TX_FAULT, |
| 56 | SFP_S_REINIT, |
| 57 | SFP_S_TX_DISABLE, |
| 58 | }; |
| 59 | |
| 60 | static const char *gpio_of_names[] = { |
| 61 | "moddef0", |
| 62 | "los", |
| 63 | "tx-fault", |
| 64 | "tx-disable", |
| 65 | "rate-select", |
| 66 | }; |
| 67 | |
| 68 | static const enum gpiod_flags gpio_flags[] = { |
| 69 | GPIOD_IN, |
| 70 | GPIOD_IN, |
| 71 | GPIOD_IN, |
| 72 | GPIOD_ASIS, |
| 73 | GPIOD_ASIS, |
| 74 | }; |
| 75 | |
| 76 | #define T_INIT_JIFFIES msecs_to_jiffies(300) |
| 77 | #define T_RESET_US 10 |
| 78 | #define T_FAULT_RECOVER msecs_to_jiffies(1000) |
| 79 | |
| 80 | /* SFP module presence detection is poor: the three MOD DEF signals are |
| 81 | * the same length on the PCB, which means it's possible for MOD DEF 0 to |
| 82 | * connect before the I2C bus on MOD DEF 1/2. |
| 83 | * |
| 84 | * The SFP MSA specifies 300ms as t_init (the time taken for TX_FAULT to |
| 85 | * be deasserted) but makes no mention of the earliest time before we can |
| 86 | * access the I2C EEPROM. However, Avago modules require 300ms. |
| 87 | */ |
| 88 | #define T_PROBE_INIT msecs_to_jiffies(300) |
| 89 | #define T_PROBE_RETRY msecs_to_jiffies(100) |
| 90 | |
| 91 | /* |
| 92 | * SFP modules appear to always have their PHY configured for bus address |
| 93 | * 0x56 (which with mdio-i2c, translates to a PHY address of 22). |
| 94 | */ |
| 95 | #define SFP_PHY_ADDR 22 |
| 96 | |
| 97 | /* |
| 98 | * Give this long for the PHY to reset. |
| 99 | */ |
| 100 | #define T_PHY_RESET_MS 50 |
| 101 | |
| 102 | static DEFINE_MUTEX(sfp_mutex); |
| 103 | |
| 104 | struct sfp { |
| 105 | struct device *dev; |
| 106 | struct i2c_adapter *i2c; |
| 107 | struct mii_bus *i2c_mii; |
| 108 | struct sfp_bus *sfp_bus; |
| 109 | struct phy_device *mod_phy; |
| 110 | |
| 111 | unsigned int (*get_state)(struct sfp *); |
| 112 | void (*set_state)(struct sfp *, unsigned int); |
| 113 | int (*read)(struct sfp *, bool, u8, void *, size_t); |
| 114 | |
| 115 | struct gpio_desc *gpio[GPIO_MAX]; |
| 116 | |
| 117 | unsigned int state; |
| 118 | struct delayed_work poll; |
| 119 | struct delayed_work timeout; |
| 120 | struct mutex sm_mutex; |
| 121 | unsigned char sm_mod_state; |
| 122 | unsigned char sm_dev_state; |
| 123 | unsigned short sm_state; |
| 124 | unsigned int sm_retries; |
| 125 | |
| 126 | struct sfp_eeprom_id id; |
| 127 | }; |
| 128 | |
| 129 | static unsigned long poll_jiffies; |
| 130 | |
| 131 | static unsigned int sfp_gpio_get_state(struct sfp *sfp) |
| 132 | { |
| 133 | unsigned int i, state, v; |
| 134 | |
| 135 | for (i = state = 0; i < GPIO_MAX; i++) { |
| 136 | if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i]) |
| 137 | continue; |
| 138 | |
| 139 | v = gpiod_get_value_cansleep(sfp->gpio[i]); |
| 140 | if (v) |
| 141 | state |= BIT(i); |
| 142 | } |
| 143 | |
| 144 | return state; |
| 145 | } |
| 146 | |
| 147 | static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state) |
| 148 | { |
| 149 | if (state & SFP_F_PRESENT) { |
| 150 | /* If the module is present, drive the signals */ |
| 151 | if (sfp->gpio[GPIO_TX_DISABLE]) |
| 152 | gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE], |
| 153 | state & SFP_F_TX_DISABLE); |
| 154 | if (state & SFP_F_RATE_SELECT) |
| 155 | gpiod_direction_output(sfp->gpio[GPIO_RATE_SELECT], |
| 156 | state & SFP_F_RATE_SELECT); |
| 157 | } else { |
| 158 | /* Otherwise, let them float to the pull-ups */ |
| 159 | if (sfp->gpio[GPIO_TX_DISABLE]) |
| 160 | gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]); |
| 161 | if (state & SFP_F_RATE_SELECT) |
| 162 | gpiod_direction_input(sfp->gpio[GPIO_RATE_SELECT]); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | static int sfp__i2c_read(struct i2c_adapter *i2c, u8 bus_addr, u8 dev_addr, |
| 167 | void *buf, size_t len) |
| 168 | { |
| 169 | struct i2c_msg msgs[2]; |
| 170 | int ret; |
| 171 | |
| 172 | msgs[0].addr = bus_addr; |
| 173 | msgs[0].flags = 0; |
| 174 | msgs[0].len = 1; |
| 175 | msgs[0].buf = &dev_addr; |
| 176 | msgs[1].addr = bus_addr; |
| 177 | msgs[1].flags = I2C_M_RD; |
| 178 | msgs[1].len = len; |
| 179 | msgs[1].buf = buf; |
| 180 | |
| 181 | ret = i2c_transfer(i2c, msgs, ARRAY_SIZE(msgs)); |
| 182 | if (ret < 0) |
| 183 | return ret; |
| 184 | |
| 185 | return ret == ARRAY_SIZE(msgs) ? len : 0; |
| 186 | } |
| 187 | |
| 188 | static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 addr, void *buf, |
| 189 | size_t len) |
| 190 | { |
| 191 | return sfp__i2c_read(sfp->i2c, a2 ? 0x51 : 0x50, addr, buf, len); |
| 192 | } |
| 193 | |
| 194 | static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c) |
| 195 | { |
| 196 | struct mii_bus *i2c_mii; |
| 197 | int ret; |
| 198 | |
| 199 | if (!i2c_check_functionality(i2c, I2C_FUNC_I2C)) |
| 200 | return -EINVAL; |
| 201 | |
| 202 | sfp->i2c = i2c; |
| 203 | sfp->read = sfp_i2c_read; |
| 204 | |
| 205 | i2c_mii = mdio_i2c_alloc(sfp->dev, i2c); |
| 206 | if (IS_ERR(i2c_mii)) |
| 207 | return PTR_ERR(i2c_mii); |
| 208 | |
| 209 | i2c_mii->name = "SFP I2C Bus"; |
| 210 | i2c_mii->phy_mask = ~0; |
| 211 | |
| 212 | ret = mdiobus_register(i2c_mii); |
| 213 | if (ret < 0) { |
| 214 | mdiobus_free(i2c_mii); |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | sfp->i2c_mii = i2c_mii; |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | /* Interface */ |
| 225 | static unsigned int sfp_get_state(struct sfp *sfp) |
| 226 | { |
| 227 | return sfp->get_state(sfp); |
| 228 | } |
| 229 | |
| 230 | static void sfp_set_state(struct sfp *sfp, unsigned int state) |
| 231 | { |
| 232 | sfp->set_state(sfp, state); |
| 233 | } |
| 234 | |
| 235 | static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len) |
| 236 | { |
| 237 | return sfp->read(sfp, a2, addr, buf, len); |
| 238 | } |
| 239 | |
| 240 | static unsigned int sfp_check(void *buf, size_t len) |
| 241 | { |
| 242 | u8 *p, check; |
| 243 | |
| 244 | for (p = buf, check = 0; len; p++, len--) |
| 245 | check += *p; |
| 246 | |
| 247 | return check; |
| 248 | } |
| 249 | |
| 250 | /* Helpers */ |
| 251 | static void sfp_module_tx_disable(struct sfp *sfp) |
| 252 | { |
| 253 | dev_dbg(sfp->dev, "tx disable %u -> %u\n", |
| 254 | sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1); |
| 255 | sfp->state |= SFP_F_TX_DISABLE; |
| 256 | sfp_set_state(sfp, sfp->state); |
| 257 | } |
| 258 | |
| 259 | static void sfp_module_tx_enable(struct sfp *sfp) |
| 260 | { |
| 261 | dev_dbg(sfp->dev, "tx disable %u -> %u\n", |
| 262 | sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0); |
| 263 | sfp->state &= ~SFP_F_TX_DISABLE; |
| 264 | sfp_set_state(sfp, sfp->state); |
| 265 | } |
| 266 | |
| 267 | static void sfp_module_tx_fault_reset(struct sfp *sfp) |
| 268 | { |
| 269 | unsigned int state = sfp->state; |
| 270 | |
| 271 | if (state & SFP_F_TX_DISABLE) |
| 272 | return; |
| 273 | |
| 274 | sfp_set_state(sfp, state | SFP_F_TX_DISABLE); |
| 275 | |
| 276 | udelay(T_RESET_US); |
| 277 | |
| 278 | sfp_set_state(sfp, state); |
| 279 | } |
| 280 | |
| 281 | /* SFP state machine */ |
| 282 | static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout) |
| 283 | { |
| 284 | if (timeout) |
| 285 | mod_delayed_work(system_power_efficient_wq, &sfp->timeout, |
| 286 | timeout); |
| 287 | else |
| 288 | cancel_delayed_work(&sfp->timeout); |
| 289 | } |
| 290 | |
| 291 | static void sfp_sm_next(struct sfp *sfp, unsigned int state, |
| 292 | unsigned int timeout) |
| 293 | { |
| 294 | sfp->sm_state = state; |
| 295 | sfp_sm_set_timer(sfp, timeout); |
| 296 | } |
| 297 | |
| 298 | static void sfp_sm_ins_next(struct sfp *sfp, unsigned int state, unsigned int timeout) |
| 299 | { |
| 300 | sfp->sm_mod_state = state; |
| 301 | sfp_sm_set_timer(sfp, timeout); |
| 302 | } |
| 303 | |
| 304 | static void sfp_sm_phy_detach(struct sfp *sfp) |
| 305 | { |
| 306 | phy_stop(sfp->mod_phy); |
| 307 | sfp_remove_phy(sfp->sfp_bus); |
| 308 | phy_device_remove(sfp->mod_phy); |
| 309 | phy_device_free(sfp->mod_phy); |
| 310 | sfp->mod_phy = NULL; |
| 311 | } |
| 312 | |
| 313 | static void sfp_sm_probe_phy(struct sfp *sfp) |
| 314 | { |
| 315 | struct phy_device *phy; |
| 316 | int err; |
| 317 | |
| 318 | msleep(T_PHY_RESET_MS); |
| 319 | |
| 320 | phy = mdiobus_scan(sfp->i2c_mii, SFP_PHY_ADDR); |
| 321 | if (IS_ERR(phy)) { |
| 322 | dev_err(sfp->dev, "mdiobus scan returned %ld\n", PTR_ERR(phy)); |
| 323 | return; |
| 324 | } |
| 325 | if (!phy) { |
| 326 | dev_info(sfp->dev, "no PHY detected\n"); |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | err = sfp_add_phy(sfp->sfp_bus, phy); |
| 331 | if (err) { |
| 332 | phy_device_remove(phy); |
| 333 | phy_device_free(phy); |
| 334 | dev_err(sfp->dev, "sfp_add_phy failed: %d\n", err); |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | sfp->mod_phy = phy; |
| 339 | phy_start(phy); |
| 340 | } |
| 341 | |
| 342 | static void sfp_sm_link_up(struct sfp *sfp) |
| 343 | { |
| 344 | sfp_link_up(sfp->sfp_bus); |
| 345 | sfp_sm_next(sfp, SFP_S_LINK_UP, 0); |
| 346 | } |
| 347 | |
| 348 | static void sfp_sm_link_down(struct sfp *sfp) |
| 349 | { |
| 350 | sfp_link_down(sfp->sfp_bus); |
| 351 | } |
| 352 | |
| 353 | static void sfp_sm_link_check_los(struct sfp *sfp) |
| 354 | { |
| 355 | unsigned int los = sfp->state & SFP_F_LOS; |
| 356 | |
| 357 | /* FIXME: what if neither SFP_OPTIONS_LOS_INVERTED nor |
| 358 | * SFP_OPTIONS_LOS_NORMAL are set? For now, we assume |
| 359 | * the same as SFP_OPTIONS_LOS_NORMAL set. |
| 360 | */ |
| 361 | if (sfp->id.ext.options & SFP_OPTIONS_LOS_INVERTED) |
| 362 | los ^= SFP_F_LOS; |
| 363 | |
| 364 | if (los) |
| 365 | sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0); |
| 366 | else |
| 367 | sfp_sm_link_up(sfp); |
| 368 | } |
| 369 | |
| 370 | static void sfp_sm_fault(struct sfp *sfp, bool warn) |
| 371 | { |
| 372 | if (sfp->sm_retries && !--sfp->sm_retries) { |
| 373 | dev_err(sfp->dev, "module persistently indicates fault, disabling\n"); |
| 374 | sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0); |
| 375 | } else { |
| 376 | if (warn) |
| 377 | dev_err(sfp->dev, "module transmit fault indicated\n"); |
| 378 | |
| 379 | sfp_sm_next(sfp, SFP_S_TX_FAULT, T_FAULT_RECOVER); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | static void sfp_sm_mod_init(struct sfp *sfp) |
| 384 | { |
| 385 | sfp_module_tx_enable(sfp); |
| 386 | |
| 387 | /* Wait t_init before indicating that the link is up, provided the |
| 388 | * current state indicates no TX_FAULT. If TX_FAULT clears before |
| 389 | * this time, that's fine too. |
| 390 | */ |
| 391 | sfp_sm_next(sfp, SFP_S_INIT, T_INIT_JIFFIES); |
| 392 | sfp->sm_retries = 5; |
| 393 | |
| 394 | /* Setting the serdes link mode is guesswork: there's no |
| 395 | * field in the EEPROM which indicates what mode should |
| 396 | * be used. |
| 397 | * |
| 398 | * If it's a gigabit-only fiber module, it probably does |
| 399 | * not have a PHY, so switch to 802.3z negotiation mode. |
| 400 | * Otherwise, switch to SGMII mode (which is required to |
| 401 | * support non-gigabit speeds) and probe for a PHY. |
| 402 | */ |
| 403 | if (sfp->id.base.e1000_base_t || |
| 404 | sfp->id.base.e100_base_lx || |
| 405 | sfp->id.base.e100_base_fx) |
| 406 | sfp_sm_probe_phy(sfp); |
| 407 | } |
| 408 | |
| 409 | static int sfp_sm_mod_probe(struct sfp *sfp) |
| 410 | { |
| 411 | /* SFP module inserted - read I2C data */ |
| 412 | struct sfp_eeprom_id id; |
| 413 | char vendor[17]; |
| 414 | char part[17]; |
| 415 | char sn[17]; |
| 416 | char date[9]; |
| 417 | char rev[5]; |
| 418 | u8 check; |
| 419 | int err; |
| 420 | |
| 421 | err = sfp_read(sfp, false, 0, &id, sizeof(id)); |
| 422 | if (err < 0) { |
| 423 | dev_err(sfp->dev, "failed to read EEPROM: %d\n", err); |
| 424 | return -EAGAIN; |
| 425 | } |
| 426 | |
| 427 | if (err != sizeof(id)) { |
| 428 | dev_err(sfp->dev, "EEPROM short read: %d\n", err); |
| 429 | return -EAGAIN; |
| 430 | } |
| 431 | |
| 432 | /* Validate the checksum over the base structure */ |
| 433 | check = sfp_check(&id.base, sizeof(id.base) - 1); |
| 434 | if (check != id.base.cc_base) { |
| 435 | dev_err(sfp->dev, |
| 436 | "EEPROM base structure checksum failure: 0x%02x\n", |
| 437 | check); |
| 438 | print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET, |
| 439 | 16, 1, &id, sizeof(id.base) - 1, true); |
| 440 | return -EINVAL; |
| 441 | } |
| 442 | |
| 443 | check = sfp_check(&id.ext, sizeof(id.ext) - 1); |
| 444 | if (check != id.ext.cc_ext) { |
| 445 | dev_err(sfp->dev, |
| 446 | "EEPROM extended structure checksum failure: 0x%02x\n", |
| 447 | check); |
| 448 | memset(&id.ext, 0, sizeof(id.ext)); |
| 449 | } |
| 450 | |
| 451 | sfp->id = id; |
| 452 | |
| 453 | memcpy(vendor, sfp->id.base.vendor_name, 16); |
| 454 | vendor[16] = '\0'; |
| 455 | memcpy(part, sfp->id.base.vendor_pn, 16); |
| 456 | part[16] = '\0'; |
| 457 | memcpy(rev, sfp->id.base.vendor_rev, 4); |
| 458 | rev[4] = '\0'; |
| 459 | memcpy(sn, sfp->id.ext.vendor_sn, 16); |
| 460 | sn[16] = '\0'; |
| 461 | memcpy(date, sfp->id.ext.datecode, 8); |
| 462 | date[8] = '\0'; |
| 463 | |
| 464 | dev_info(sfp->dev, "module %s %s rev %s sn %s dc %s\n", vendor, part, rev, sn, date); |
| 465 | |
| 466 | /* We only support SFP modules, not the legacy GBIC modules. */ |
| 467 | if (sfp->id.base.phys_id != SFP_PHYS_ID_SFP || |
| 468 | sfp->id.base.phys_ext_id != SFP_PHYS_EXT_ID_SFP) { |
| 469 | dev_err(sfp->dev, "module is not SFP - phys id 0x%02x 0x%02x\n", |
| 470 | sfp->id.base.phys_id, sfp->id.base.phys_ext_id); |
| 471 | return -EINVAL; |
| 472 | } |
| 473 | |
| 474 | return sfp_module_insert(sfp->sfp_bus, &sfp->id); |
| 475 | } |
| 476 | |
| 477 | static void sfp_sm_mod_remove(struct sfp *sfp) |
| 478 | { |
| 479 | sfp_module_remove(sfp->sfp_bus); |
| 480 | |
| 481 | if (sfp->mod_phy) |
| 482 | sfp_sm_phy_detach(sfp); |
| 483 | |
| 484 | sfp_module_tx_disable(sfp); |
| 485 | |
| 486 | memset(&sfp->id, 0, sizeof(sfp->id)); |
| 487 | |
| 488 | dev_info(sfp->dev, "module removed\n"); |
| 489 | } |
| 490 | |
| 491 | static void sfp_sm_event(struct sfp *sfp, unsigned int event) |
| 492 | { |
| 493 | mutex_lock(&sfp->sm_mutex); |
| 494 | |
| 495 | dev_dbg(sfp->dev, "SM: enter %u:%u:%u event %u\n", |
| 496 | sfp->sm_mod_state, sfp->sm_dev_state, sfp->sm_state, event); |
| 497 | |
| 498 | /* This state machine tracks the insert/remove state of |
| 499 | * the module, and handles probing the on-board EEPROM. |
| 500 | */ |
| 501 | switch (sfp->sm_mod_state) { |
| 502 | default: |
| 503 | if (event == SFP_E_INSERT) { |
| 504 | sfp_module_tx_disable(sfp); |
| 505 | sfp_sm_ins_next(sfp, SFP_MOD_PROBE, T_PROBE_INIT); |
| 506 | } |
| 507 | break; |
| 508 | |
| 509 | case SFP_MOD_PROBE: |
| 510 | if (event == SFP_E_REMOVE) { |
| 511 | sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0); |
| 512 | } else if (event == SFP_E_TIMEOUT) { |
| 513 | int err = sfp_sm_mod_probe(sfp); |
| 514 | |
| 515 | if (err == 0) |
| 516 | sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0); |
| 517 | else if (err == -EAGAIN) |
| 518 | sfp_sm_set_timer(sfp, T_PROBE_RETRY); |
| 519 | else |
| 520 | sfp_sm_ins_next(sfp, SFP_MOD_ERROR, 0); |
| 521 | } |
| 522 | break; |
| 523 | |
| 524 | case SFP_MOD_PRESENT: |
| 525 | case SFP_MOD_ERROR: |
| 526 | if (event == SFP_E_REMOVE) { |
| 527 | sfp_sm_mod_remove(sfp); |
| 528 | sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0); |
| 529 | } |
| 530 | break; |
| 531 | } |
| 532 | |
| 533 | /* This state machine tracks the netdev up/down state */ |
| 534 | switch (sfp->sm_dev_state) { |
| 535 | default: |
| 536 | if (event == SFP_E_DEV_UP) |
| 537 | sfp->sm_dev_state = SFP_DEV_UP; |
| 538 | break; |
| 539 | |
| 540 | case SFP_DEV_UP: |
| 541 | if (event == SFP_E_DEV_DOWN) { |
| 542 | /* If the module has a PHY, avoid raising TX disable |
| 543 | * as this resets the PHY. Otherwise, raise it to |
| 544 | * turn the laser off. |
| 545 | */ |
| 546 | if (!sfp->mod_phy) |
| 547 | sfp_module_tx_disable(sfp); |
| 548 | sfp->sm_dev_state = SFP_DEV_DOWN; |
| 549 | } |
| 550 | break; |
| 551 | } |
| 552 | |
| 553 | /* Some events are global */ |
| 554 | if (sfp->sm_state != SFP_S_DOWN && |
| 555 | (sfp->sm_mod_state != SFP_MOD_PRESENT || |
| 556 | sfp->sm_dev_state != SFP_DEV_UP)) { |
| 557 | if (sfp->sm_state == SFP_S_LINK_UP && |
| 558 | sfp->sm_dev_state == SFP_DEV_UP) |
| 559 | sfp_sm_link_down(sfp); |
| 560 | if (sfp->mod_phy) |
| 561 | sfp_sm_phy_detach(sfp); |
| 562 | sfp_sm_next(sfp, SFP_S_DOWN, 0); |
| 563 | mutex_unlock(&sfp->sm_mutex); |
| 564 | return; |
| 565 | } |
| 566 | |
| 567 | /* The main state machine */ |
| 568 | switch (sfp->sm_state) { |
| 569 | case SFP_S_DOWN: |
| 570 | if (sfp->sm_mod_state == SFP_MOD_PRESENT && |
| 571 | sfp->sm_dev_state == SFP_DEV_UP) |
| 572 | sfp_sm_mod_init(sfp); |
| 573 | break; |
| 574 | |
| 575 | case SFP_S_INIT: |
| 576 | if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) |
| 577 | sfp_sm_fault(sfp, true); |
| 578 | else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) |
| 579 | sfp_sm_link_check_los(sfp); |
| 580 | break; |
| 581 | |
| 582 | case SFP_S_WAIT_LOS: |
| 583 | if (event == SFP_E_TX_FAULT) |
| 584 | sfp_sm_fault(sfp, true); |
| 585 | else if (event == |
| 586 | (sfp->id.ext.options & SFP_OPTIONS_LOS_INVERTED ? |
| 587 | SFP_E_LOS_HIGH : SFP_E_LOS_LOW)) |
| 588 | sfp_sm_link_up(sfp); |
| 589 | break; |
| 590 | |
| 591 | case SFP_S_LINK_UP: |
| 592 | if (event == SFP_E_TX_FAULT) { |
| 593 | sfp_sm_link_down(sfp); |
| 594 | sfp_sm_fault(sfp, true); |
| 595 | } else if (event == |
| 596 | (sfp->id.ext.options & SFP_OPTIONS_LOS_INVERTED ? |
| 597 | SFP_E_LOS_LOW : SFP_E_LOS_HIGH)) { |
| 598 | sfp_sm_link_down(sfp); |
| 599 | sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0); |
| 600 | } |
| 601 | break; |
| 602 | |
| 603 | case SFP_S_TX_FAULT: |
| 604 | if (event == SFP_E_TIMEOUT) { |
| 605 | sfp_module_tx_fault_reset(sfp); |
| 606 | sfp_sm_next(sfp, SFP_S_REINIT, T_INIT_JIFFIES); |
| 607 | } |
| 608 | break; |
| 609 | |
| 610 | case SFP_S_REINIT: |
| 611 | if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) { |
| 612 | sfp_sm_fault(sfp, false); |
| 613 | } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) { |
| 614 | dev_info(sfp->dev, "module transmit fault recovered\n"); |
| 615 | sfp_sm_link_check_los(sfp); |
| 616 | } |
| 617 | break; |
| 618 | |
| 619 | case SFP_S_TX_DISABLE: |
| 620 | break; |
| 621 | } |
| 622 | |
| 623 | dev_dbg(sfp->dev, "SM: exit %u:%u:%u\n", |
| 624 | sfp->sm_mod_state, sfp->sm_dev_state, sfp->sm_state); |
| 625 | |
| 626 | mutex_unlock(&sfp->sm_mutex); |
| 627 | } |
| 628 | |
| 629 | static void sfp_start(struct sfp *sfp) |
| 630 | { |
| 631 | sfp_sm_event(sfp, SFP_E_DEV_UP); |
| 632 | } |
| 633 | |
| 634 | static void sfp_stop(struct sfp *sfp) |
| 635 | { |
| 636 | sfp_sm_event(sfp, SFP_E_DEV_DOWN); |
| 637 | } |
| 638 | |
| 639 | static int sfp_module_info(struct sfp *sfp, struct ethtool_modinfo *modinfo) |
| 640 | { |
| 641 | /* locking... and check module is present */ |
| 642 | |
| 643 | if (sfp->id.ext.sff8472_compliance) { |
| 644 | modinfo->type = ETH_MODULE_SFF_8472; |
| 645 | modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN; |
| 646 | } else { |
| 647 | modinfo->type = ETH_MODULE_SFF_8079; |
| 648 | modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN; |
| 649 | } |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | static int sfp_module_eeprom(struct sfp *sfp, struct ethtool_eeprom *ee, |
| 654 | u8 *data) |
| 655 | { |
| 656 | unsigned int first, last, len; |
| 657 | int ret; |
| 658 | |
| 659 | if (ee->len == 0) |
| 660 | return -EINVAL; |
| 661 | |
| 662 | first = ee->offset; |
| 663 | last = ee->offset + ee->len; |
| 664 | if (first < ETH_MODULE_SFF_8079_LEN) { |
| 665 | len = min_t(unsigned int, last, ETH_MODULE_SFF_8079_LEN); |
| 666 | len -= first; |
| 667 | |
| 668 | ret = sfp->read(sfp, false, first, data, len); |
| 669 | if (ret < 0) |
| 670 | return ret; |
| 671 | |
| 672 | first += len; |
| 673 | data += len; |
| 674 | } |
| 675 | if (first >= ETH_MODULE_SFF_8079_LEN && |
| 676 | first < ETH_MODULE_SFF_8472_LEN) { |
| 677 | len = min_t(unsigned int, last, ETH_MODULE_SFF_8472_LEN); |
| 678 | len -= first; |
| 679 | first -= ETH_MODULE_SFF_8079_LEN; |
| 680 | |
| 681 | ret = sfp->read(sfp, true, first, data, len); |
| 682 | if (ret < 0) |
| 683 | return ret; |
| 684 | } |
| 685 | return 0; |
| 686 | } |
| 687 | |
| 688 | static const struct sfp_socket_ops sfp_module_ops = { |
| 689 | .start = sfp_start, |
| 690 | .stop = sfp_stop, |
| 691 | .module_info = sfp_module_info, |
| 692 | .module_eeprom = sfp_module_eeprom, |
| 693 | }; |
| 694 | |
| 695 | static void sfp_timeout(struct work_struct *work) |
| 696 | { |
| 697 | struct sfp *sfp = container_of(work, struct sfp, timeout.work); |
| 698 | |
| 699 | rtnl_lock(); |
| 700 | sfp_sm_event(sfp, SFP_E_TIMEOUT); |
| 701 | rtnl_unlock(); |
| 702 | } |
| 703 | |
| 704 | static void sfp_check_state(struct sfp *sfp) |
| 705 | { |
| 706 | unsigned int state, i, changed; |
| 707 | |
| 708 | state = sfp_get_state(sfp); |
| 709 | changed = state ^ sfp->state; |
| 710 | changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT; |
| 711 | |
| 712 | for (i = 0; i < GPIO_MAX; i++) |
| 713 | if (changed & BIT(i)) |
| 714 | dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_of_names[i], |
| 715 | !!(sfp->state & BIT(i)), !!(state & BIT(i))); |
| 716 | |
| 717 | state |= sfp->state & (SFP_F_TX_DISABLE | SFP_F_RATE_SELECT); |
| 718 | sfp->state = state; |
| 719 | |
| 720 | rtnl_lock(); |
| 721 | if (changed & SFP_F_PRESENT) |
| 722 | sfp_sm_event(sfp, state & SFP_F_PRESENT ? |
| 723 | SFP_E_INSERT : SFP_E_REMOVE); |
| 724 | |
| 725 | if (changed & SFP_F_TX_FAULT) |
| 726 | sfp_sm_event(sfp, state & SFP_F_TX_FAULT ? |
| 727 | SFP_E_TX_FAULT : SFP_E_TX_CLEAR); |
| 728 | |
| 729 | if (changed & SFP_F_LOS) |
| 730 | sfp_sm_event(sfp, state & SFP_F_LOS ? |
| 731 | SFP_E_LOS_HIGH : SFP_E_LOS_LOW); |
| 732 | rtnl_unlock(); |
| 733 | } |
| 734 | |
| 735 | static irqreturn_t sfp_irq(int irq, void *data) |
| 736 | { |
| 737 | struct sfp *sfp = data; |
| 738 | |
| 739 | sfp_check_state(sfp); |
| 740 | |
| 741 | return IRQ_HANDLED; |
| 742 | } |
| 743 | |
| 744 | static void sfp_poll(struct work_struct *work) |
| 745 | { |
| 746 | struct sfp *sfp = container_of(work, struct sfp, poll.work); |
| 747 | |
| 748 | sfp_check_state(sfp); |
| 749 | mod_delayed_work(system_wq, &sfp->poll, poll_jiffies); |
| 750 | } |
| 751 | |
| 752 | static struct sfp *sfp_alloc(struct device *dev) |
| 753 | { |
| 754 | struct sfp *sfp; |
| 755 | |
| 756 | sfp = kzalloc(sizeof(*sfp), GFP_KERNEL); |
| 757 | if (!sfp) |
| 758 | return ERR_PTR(-ENOMEM); |
| 759 | |
| 760 | sfp->dev = dev; |
| 761 | |
| 762 | mutex_init(&sfp->sm_mutex); |
| 763 | INIT_DELAYED_WORK(&sfp->poll, sfp_poll); |
| 764 | INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout); |
| 765 | |
| 766 | return sfp; |
| 767 | } |
| 768 | |
| 769 | static void sfp_cleanup(void *data) |
| 770 | { |
| 771 | struct sfp *sfp = data; |
| 772 | |
| 773 | cancel_delayed_work_sync(&sfp->poll); |
| 774 | cancel_delayed_work_sync(&sfp->timeout); |
| 775 | if (sfp->i2c_mii) { |
| 776 | mdiobus_unregister(sfp->i2c_mii); |
| 777 | mdiobus_free(sfp->i2c_mii); |
| 778 | } |
| 779 | if (sfp->i2c) |
| 780 | i2c_put_adapter(sfp->i2c); |
| 781 | kfree(sfp); |
| 782 | } |
| 783 | |
| 784 | static int sfp_probe(struct platform_device *pdev) |
| 785 | { |
| 786 | struct sfp *sfp; |
| 787 | bool poll = false; |
| 788 | int irq, err, i; |
| 789 | |
| 790 | sfp = sfp_alloc(&pdev->dev); |
| 791 | if (IS_ERR(sfp)) |
| 792 | return PTR_ERR(sfp); |
| 793 | |
| 794 | platform_set_drvdata(pdev, sfp); |
| 795 | |
| 796 | err = devm_add_action(sfp->dev, sfp_cleanup, sfp); |
| 797 | if (err < 0) |
| 798 | return err; |
| 799 | |
| 800 | if (pdev->dev.of_node) { |
| 801 | struct device_node *node = pdev->dev.of_node; |
| 802 | struct device_node *np; |
| 803 | |
| 804 | np = of_parse_phandle(node, "i2c-bus", 0); |
| 805 | if (np) { |
| 806 | struct i2c_adapter *i2c; |
| 807 | |
| 808 | i2c = of_find_i2c_adapter_by_node(np); |
| 809 | of_node_put(np); |
| 810 | if (!i2c) |
| 811 | return -EPROBE_DEFER; |
| 812 | |
| 813 | err = sfp_i2c_configure(sfp, i2c); |
| 814 | if (err < 0) { |
| 815 | i2c_put_adapter(i2c); |
| 816 | return err; |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | for (i = 0; i < GPIO_MAX; i++) { |
| 821 | sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev, |
| 822 | gpio_of_names[i], gpio_flags[i]); |
| 823 | if (IS_ERR(sfp->gpio[i])) |
| 824 | return PTR_ERR(sfp->gpio[i]); |
| 825 | } |
| 826 | |
| 827 | sfp->get_state = sfp_gpio_get_state; |
| 828 | sfp->set_state = sfp_gpio_set_state; |
| 829 | } |
| 830 | |
| 831 | sfp->sfp_bus = sfp_register_socket(sfp->dev, sfp, &sfp_module_ops); |
| 832 | if (!sfp->sfp_bus) |
| 833 | return -ENOMEM; |
| 834 | |
| 835 | /* Get the initial state, and always signal TX disable, |
| 836 | * since the network interface will not be up. |
| 837 | */ |
| 838 | sfp->state = sfp_get_state(sfp) | SFP_F_TX_DISABLE; |
| 839 | |
| 840 | if (sfp->gpio[GPIO_RATE_SELECT] && |
| 841 | gpiod_get_value_cansleep(sfp->gpio[GPIO_RATE_SELECT])) |
| 842 | sfp->state |= SFP_F_RATE_SELECT; |
| 843 | sfp_set_state(sfp, sfp->state); |
| 844 | sfp_module_tx_disable(sfp); |
| 845 | rtnl_lock(); |
| 846 | if (sfp->state & SFP_F_PRESENT) |
| 847 | sfp_sm_event(sfp, SFP_E_INSERT); |
| 848 | rtnl_unlock(); |
| 849 | |
| 850 | for (i = 0; i < GPIO_MAX; i++) { |
| 851 | if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i]) |
| 852 | continue; |
| 853 | |
| 854 | irq = gpiod_to_irq(sfp->gpio[i]); |
| 855 | if (!irq) { |
| 856 | poll = true; |
| 857 | continue; |
| 858 | } |
| 859 | |
| 860 | err = devm_request_threaded_irq(sfp->dev, irq, NULL, sfp_irq, |
| 861 | IRQF_ONESHOT | |
| 862 | IRQF_TRIGGER_RISING | |
| 863 | IRQF_TRIGGER_FALLING, |
| 864 | dev_name(sfp->dev), sfp); |
| 865 | if (err) |
| 866 | poll = true; |
| 867 | } |
| 868 | |
| 869 | if (poll) |
| 870 | mod_delayed_work(system_wq, &sfp->poll, poll_jiffies); |
| 871 | |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | static int sfp_remove(struct platform_device *pdev) |
| 876 | { |
| 877 | struct sfp *sfp = platform_get_drvdata(pdev); |
| 878 | |
| 879 | sfp_unregister_socket(sfp->sfp_bus); |
| 880 | |
| 881 | return 0; |
| 882 | } |
| 883 | |
| 884 | static const struct of_device_id sfp_of_match[] = { |
| 885 | { .compatible = "sff,sfp", }, |
| 886 | { }, |
| 887 | }; |
| 888 | MODULE_DEVICE_TABLE(of, sfp_of_match); |
| 889 | |
| 890 | static struct platform_driver sfp_driver = { |
| 891 | .probe = sfp_probe, |
| 892 | .remove = sfp_remove, |
| 893 | .driver = { |
| 894 | .name = "sfp", |
| 895 | .of_match_table = sfp_of_match, |
| 896 | }, |
| 897 | }; |
| 898 | |
| 899 | static int sfp_init(void) |
| 900 | { |
| 901 | poll_jiffies = msecs_to_jiffies(100); |
| 902 | |
| 903 | return platform_driver_register(&sfp_driver); |
| 904 | } |
| 905 | module_init(sfp_init); |
| 906 | |
| 907 | static void sfp_exit(void) |
| 908 | { |
| 909 | platform_driver_unregister(&sfp_driver); |
| 910 | } |
| 911 | module_exit(sfp_exit); |
| 912 | |
| 913 | MODULE_ALIAS("platform:sfp"); |
| 914 | MODULE_AUTHOR("Russell King"); |
| 915 | MODULE_LICENSE("GPL v2"); |