sfp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. #include <linux/delay.h>
  2. #include <linux/gpio/consumer.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. /* SFP modules appear to always have their PHY configured for bus address
  80. * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
  81. */
  82. #define SFP_PHY_ADDR 22
  83. /* Give this long for the PHY to reset. */
  84. #define T_PHY_RESET_MS 50
  85. static DEFINE_MUTEX(sfp_mutex);
  86. struct sfp {
  87. struct device *dev;
  88. struct i2c_adapter *i2c;
  89. struct mii_bus *i2c_mii;
  90. struct sfp_bus *sfp_bus;
  91. struct phy_device *mod_phy;
  92. unsigned int (*get_state)(struct sfp *);
  93. void (*set_state)(struct sfp *, unsigned int);
  94. int (*read)(struct sfp *, bool, u8, void *, size_t);
  95. struct gpio_desc *gpio[GPIO_MAX];
  96. unsigned int state;
  97. struct delayed_work poll;
  98. struct delayed_work timeout;
  99. struct mutex sm_mutex;
  100. unsigned char sm_mod_state;
  101. unsigned char sm_dev_state;
  102. unsigned short sm_state;
  103. unsigned int sm_retries;
  104. struct sfp_eeprom_id id;
  105. };
  106. static unsigned long poll_jiffies;
  107. static unsigned int sfp_gpio_get_state(struct sfp *sfp)
  108. {
  109. unsigned int i, state, v;
  110. for (i = state = 0; i < GPIO_MAX; i++) {
  111. if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
  112. continue;
  113. v = gpiod_get_value_cansleep(sfp->gpio[i]);
  114. if (v)
  115. state |= BIT(i);
  116. }
  117. return state;
  118. }
  119. static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state)
  120. {
  121. if (state & SFP_F_PRESENT) {
  122. /* If the module is present, drive the signals */
  123. if (sfp->gpio[GPIO_TX_DISABLE])
  124. gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE],
  125. state & SFP_F_TX_DISABLE);
  126. if (state & SFP_F_RATE_SELECT)
  127. gpiod_direction_output(sfp->gpio[GPIO_RATE_SELECT],
  128. state & SFP_F_RATE_SELECT);
  129. } else {
  130. /* Otherwise, let them float to the pull-ups */
  131. if (sfp->gpio[GPIO_TX_DISABLE])
  132. gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]);
  133. if (state & SFP_F_RATE_SELECT)
  134. gpiod_direction_input(sfp->gpio[GPIO_RATE_SELECT]);
  135. }
  136. }
  137. static int sfp__i2c_read(struct i2c_adapter *i2c, u8 bus_addr, u8 dev_addr,
  138. void *buf, size_t len)
  139. {
  140. struct i2c_msg msgs[2];
  141. int ret;
  142. msgs[0].addr = bus_addr;
  143. msgs[0].flags = 0;
  144. msgs[0].len = 1;
  145. msgs[0].buf = &dev_addr;
  146. msgs[1].addr = bus_addr;
  147. msgs[1].flags = I2C_M_RD;
  148. msgs[1].len = len;
  149. msgs[1].buf = buf;
  150. ret = i2c_transfer(i2c, msgs, ARRAY_SIZE(msgs));
  151. if (ret < 0)
  152. return ret;
  153. return ret == ARRAY_SIZE(msgs) ? len : 0;
  154. }
  155. static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 addr, void *buf,
  156. size_t len)
  157. {
  158. return sfp__i2c_read(sfp->i2c, a2 ? 0x51 : 0x50, addr, buf, len);
  159. }
  160. static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
  161. {
  162. struct mii_bus *i2c_mii;
  163. int ret;
  164. if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
  165. return -EINVAL;
  166. sfp->i2c = i2c;
  167. sfp->read = sfp_i2c_read;
  168. i2c_mii = mdio_i2c_alloc(sfp->dev, i2c);
  169. if (IS_ERR(i2c_mii))
  170. return PTR_ERR(i2c_mii);
  171. i2c_mii->name = "SFP I2C Bus";
  172. i2c_mii->phy_mask = ~0;
  173. ret = mdiobus_register(i2c_mii);
  174. if (ret < 0) {
  175. mdiobus_free(i2c_mii);
  176. return ret;
  177. }
  178. sfp->i2c_mii = i2c_mii;
  179. return 0;
  180. }
  181. /* Interface */
  182. static unsigned int sfp_get_state(struct sfp *sfp)
  183. {
  184. return sfp->get_state(sfp);
  185. }
  186. static void sfp_set_state(struct sfp *sfp, unsigned int state)
  187. {
  188. sfp->set_state(sfp, state);
  189. }
  190. static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
  191. {
  192. return sfp->read(sfp, a2, addr, buf, len);
  193. }
  194. static unsigned int sfp_check(void *buf, size_t len)
  195. {
  196. u8 *p, check;
  197. for (p = buf, check = 0; len; p++, len--)
  198. check += *p;
  199. return check;
  200. }
  201. /* Helpers */
  202. static void sfp_module_tx_disable(struct sfp *sfp)
  203. {
  204. dev_dbg(sfp->dev, "tx disable %u -> %u\n",
  205. sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1);
  206. sfp->state |= SFP_F_TX_DISABLE;
  207. sfp_set_state(sfp, sfp->state);
  208. }
  209. static void sfp_module_tx_enable(struct sfp *sfp)
  210. {
  211. dev_dbg(sfp->dev, "tx disable %u -> %u\n",
  212. sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0);
  213. sfp->state &= ~SFP_F_TX_DISABLE;
  214. sfp_set_state(sfp, sfp->state);
  215. }
  216. static void sfp_module_tx_fault_reset(struct sfp *sfp)
  217. {
  218. unsigned int state = sfp->state;
  219. if (state & SFP_F_TX_DISABLE)
  220. return;
  221. sfp_set_state(sfp, state | SFP_F_TX_DISABLE);
  222. udelay(T_RESET_US);
  223. sfp_set_state(sfp, state);
  224. }
  225. /* SFP state machine */
  226. static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout)
  227. {
  228. if (timeout)
  229. mod_delayed_work(system_power_efficient_wq, &sfp->timeout,
  230. timeout);
  231. else
  232. cancel_delayed_work(&sfp->timeout);
  233. }
  234. static void sfp_sm_next(struct sfp *sfp, unsigned int state,
  235. unsigned int timeout)
  236. {
  237. sfp->sm_state = state;
  238. sfp_sm_set_timer(sfp, timeout);
  239. }
  240. static void sfp_sm_ins_next(struct sfp *sfp, unsigned int state,
  241. unsigned int timeout)
  242. {
  243. sfp->sm_mod_state = state;
  244. sfp_sm_set_timer(sfp, timeout);
  245. }
  246. static void sfp_sm_phy_detach(struct sfp *sfp)
  247. {
  248. phy_stop(sfp->mod_phy);
  249. sfp_remove_phy(sfp->sfp_bus);
  250. phy_device_remove(sfp->mod_phy);
  251. phy_device_free(sfp->mod_phy);
  252. sfp->mod_phy = NULL;
  253. }
  254. static void sfp_sm_probe_phy(struct sfp *sfp)
  255. {
  256. struct phy_device *phy;
  257. int err;
  258. msleep(T_PHY_RESET_MS);
  259. phy = mdiobus_scan(sfp->i2c_mii, SFP_PHY_ADDR);
  260. if (IS_ERR(phy)) {
  261. dev_err(sfp->dev, "mdiobus scan returned %ld\n", PTR_ERR(phy));
  262. return;
  263. }
  264. if (!phy) {
  265. dev_info(sfp->dev, "no PHY detected\n");
  266. return;
  267. }
  268. err = sfp_add_phy(sfp->sfp_bus, phy);
  269. if (err) {
  270. phy_device_remove(phy);
  271. phy_device_free(phy);
  272. dev_err(sfp->dev, "sfp_add_phy failed: %d\n", err);
  273. return;
  274. }
  275. sfp->mod_phy = phy;
  276. phy_start(phy);
  277. }
  278. static void sfp_sm_link_up(struct sfp *sfp)
  279. {
  280. sfp_link_up(sfp->sfp_bus);
  281. sfp_sm_next(sfp, SFP_S_LINK_UP, 0);
  282. }
  283. static void sfp_sm_link_down(struct sfp *sfp)
  284. {
  285. sfp_link_down(sfp->sfp_bus);
  286. }
  287. static void sfp_sm_link_check_los(struct sfp *sfp)
  288. {
  289. unsigned int los = sfp->state & SFP_F_LOS;
  290. /* FIXME: what if neither SFP_OPTIONS_LOS_INVERTED nor
  291. * SFP_OPTIONS_LOS_NORMAL are set? For now, we assume
  292. * the same as SFP_OPTIONS_LOS_NORMAL set.
  293. */
  294. if (sfp->id.ext.options & SFP_OPTIONS_LOS_INVERTED)
  295. los ^= SFP_F_LOS;
  296. if (los)
  297. sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
  298. else
  299. sfp_sm_link_up(sfp);
  300. }
  301. static void sfp_sm_fault(struct sfp *sfp, bool warn)
  302. {
  303. if (sfp->sm_retries && !--sfp->sm_retries) {
  304. dev_err(sfp->dev,
  305. "module persistently indicates fault, disabling\n");
  306. sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0);
  307. } else {
  308. if (warn)
  309. dev_err(sfp->dev, "module transmit fault indicated\n");
  310. sfp_sm_next(sfp, SFP_S_TX_FAULT, T_FAULT_RECOVER);
  311. }
  312. }
  313. static void sfp_sm_mod_init(struct sfp *sfp)
  314. {
  315. sfp_module_tx_enable(sfp);
  316. /* Wait t_init before indicating that the link is up, provided the
  317. * current state indicates no TX_FAULT. If TX_FAULT clears before
  318. * this time, that's fine too.
  319. */
  320. sfp_sm_next(sfp, SFP_S_INIT, T_INIT_JIFFIES);
  321. sfp->sm_retries = 5;
  322. /* Setting the serdes link mode is guesswork: there's no
  323. * field in the EEPROM which indicates what mode should
  324. * be used.
  325. *
  326. * If it's a gigabit-only fiber module, it probably does
  327. * not have a PHY, so switch to 802.3z negotiation mode.
  328. * Otherwise, switch to SGMII mode (which is required to
  329. * support non-gigabit speeds) and probe for a PHY.
  330. */
  331. if (sfp->id.base.e1000_base_t ||
  332. sfp->id.base.e100_base_lx ||
  333. sfp->id.base.e100_base_fx)
  334. sfp_sm_probe_phy(sfp);
  335. }
  336. static int sfp_sm_mod_probe(struct sfp *sfp)
  337. {
  338. /* SFP module inserted - read I2C data */
  339. struct sfp_eeprom_id id;
  340. char vendor[17];
  341. char part[17];
  342. char sn[17];
  343. char date[9];
  344. char rev[5];
  345. u8 check;
  346. int err;
  347. err = sfp_read(sfp, false, 0, &id, sizeof(id));
  348. if (err < 0) {
  349. dev_err(sfp->dev, "failed to read EEPROM: %d\n", err);
  350. return -EAGAIN;
  351. }
  352. if (err != sizeof(id)) {
  353. dev_err(sfp->dev, "EEPROM short read: %d\n", err);
  354. return -EAGAIN;
  355. }
  356. /* Validate the checksum over the base structure */
  357. check = sfp_check(&id.base, sizeof(id.base) - 1);
  358. if (check != id.base.cc_base) {
  359. dev_err(sfp->dev,
  360. "EEPROM base structure checksum failure: 0x%02x\n",
  361. check);
  362. print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
  363. 16, 1, &id, sizeof(id.base) - 1, true);
  364. return -EINVAL;
  365. }
  366. check = sfp_check(&id.ext, sizeof(id.ext) - 1);
  367. if (check != id.ext.cc_ext) {
  368. dev_err(sfp->dev,
  369. "EEPROM extended structure checksum failure: 0x%02x\n",
  370. check);
  371. memset(&id.ext, 0, sizeof(id.ext));
  372. }
  373. sfp->id = id;
  374. memcpy(vendor, sfp->id.base.vendor_name, 16);
  375. vendor[16] = '\0';
  376. memcpy(part, sfp->id.base.vendor_pn, 16);
  377. part[16] = '\0';
  378. memcpy(rev, sfp->id.base.vendor_rev, 4);
  379. rev[4] = '\0';
  380. memcpy(sn, sfp->id.ext.vendor_sn, 16);
  381. sn[16] = '\0';
  382. memcpy(date, sfp->id.ext.datecode, 8);
  383. date[8] = '\0';
  384. dev_info(sfp->dev, "module %s %s rev %s sn %s dc %s\n",
  385. 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");