sfp.c 20 KB

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