132-sfp-add-phylink-based-SFP-module-support.patch 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  1. From bf0a000960234c0e773fadea47240c3cda0cab02 Mon Sep 17 00:00:00 2001
  2. From: Russell King <rmk+kernel@arm.linux.org.uk>
  3. Date: Sat, 12 Sep 2015 18:43:39 +0100
  4. Subject: [PATCH 720/744] sfp: add phylink based SFP module support
  5. Add support for SFP hotpluggable modules via phylink. This supports
  6. both copper and optical SFP modules, which require different Serdes
  7. modes in order to properly negotiate the link.
  8. Optical SFP modules typically require the Serdes link to be talking
  9. 1000base-X mode - this is the gigabit ethernet mode defined by the
  10. 802.3 standard.
  11. Copper SFP modules typically integrate a PHY in the module to convert
  12. from Serdes to copper, and the PHY will be configured by the vendor
  13. to either present a 1000base-X Serdes link (for fixed 1000base-T) or
  14. a SGMII Serdes link. However, this is vendor defined, so we instead
  15. detect the PHY, switch the link to SGMII mode, and use traditional
  16. PHY based negotiation.
  17. Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
  18. ---
  19. drivers/net/phy/Kconfig | 5 +
  20. drivers/net/phy/Makefile | 1 +
  21. drivers/net/phy/sfp.c | 986 +++++++++++++++++++++++++++++++++++++++++++++++
  22. include/linux/sfp.h | 339 ++++++++++++++++
  23. 4 files changed, 1331 insertions(+)
  24. create mode 100644 drivers/net/phy/sfp.c
  25. create mode 100644 include/linux/sfp.h
  26. --- a/drivers/net/phy/Kconfig
  27. +++ b/drivers/net/phy/Kconfig
  28. @@ -225,6 +225,11 @@ config FIXED_PHY
  29. Currently tested with mpc866ads and mpc8349e-mitx.
  30. +config SFP
  31. + tristate "SFP cage support"
  32. + depends on I2C && PHYLINK
  33. + select MDIO_I2C
  34. +
  35. config MDIO_BITBANG
  36. tristate "Support for bitbanged MDIO buses"
  37. help
  38. --- a/drivers/net/phy/Makefile
  39. +++ b/drivers/net/phy/Makefile
  40. @@ -61,3 +61,4 @@ obj-$(CONFIG_MDIO_MOXART) += mdio-moxart
  41. obj-$(CONFIG_MDIO_BCM_UNIMAC) += mdio-bcm-unimac.o
  42. obj-$(CONFIG_MICROCHIP_PHY) += microchip.o
  43. obj-$(CONFIG_MDIO_BCM_IPROC) += mdio-bcm-iproc.o
  44. +obj-$(CONFIG_SFP) += sfp.o
  45. --- /dev/null
  46. +++ b/drivers/net/phy/sfp.c
  47. @@ -0,0 +1,986 @@
  48. +#include <linux/delay.h>
  49. +#include <linux/gpio.h>
  50. +#include <linux/i2c.h>
  51. +#include <linux/interrupt.h>
  52. +#include <linux/jiffies.h>
  53. +#include <linux/module.h>
  54. +#include <linux/mutex.h>
  55. +#include <linux/netdevice.h>
  56. +#include <linux/of.h>
  57. +#include <linux/of_net.h>
  58. +#include <linux/phylink.h>
  59. +#include <linux/platform_device.h>
  60. +#include <linux/sfp.h>
  61. +#include <linux/slab.h>
  62. +#include <linux/workqueue.h>
  63. +
  64. +#include "mdio-i2c.h"
  65. +#include "swphy.h"
  66. +
  67. +enum {
  68. + GPIO_MODDEF0,
  69. + GPIO_LOS,
  70. + GPIO_TX_FAULT,
  71. + GPIO_TX_DISABLE,
  72. + GPIO_RATE_SELECT,
  73. + GPIO_MAX,
  74. +
  75. + SFP_F_PRESENT = BIT(GPIO_MODDEF0),
  76. + SFP_F_LOS = BIT(GPIO_LOS),
  77. + SFP_F_TX_FAULT = BIT(GPIO_TX_FAULT),
  78. + SFP_F_TX_DISABLE = BIT(GPIO_TX_DISABLE),
  79. + SFP_F_RATE_SELECT = BIT(GPIO_RATE_SELECT),
  80. +
  81. + SFP_E_INSERT = 0,
  82. + SFP_E_REMOVE,
  83. + SFP_E_DEV_DOWN,
  84. + SFP_E_DEV_UP,
  85. + SFP_E_TX_FAULT,
  86. + SFP_E_TX_CLEAR,
  87. + SFP_E_LOS_HIGH,
  88. + SFP_E_LOS_LOW,
  89. + SFP_E_TIMEOUT,
  90. +
  91. + SFP_MOD_EMPTY = 0,
  92. + SFP_MOD_PROBE,
  93. + SFP_MOD_PRESENT,
  94. + SFP_MOD_ERROR,
  95. +
  96. + SFP_DEV_DOWN = 0,
  97. + SFP_DEV_UP,
  98. +
  99. + SFP_S_DOWN = 0,
  100. + SFP_S_INIT,
  101. + SFP_S_WAIT_LOS,
  102. + SFP_S_LINK_UP,
  103. + SFP_S_TX_FAULT,
  104. + SFP_S_REINIT,
  105. + SFP_S_TX_DISABLE,
  106. +};
  107. +
  108. +static const char *gpio_of_names[] = {
  109. + "moddef0",
  110. + "los",
  111. + "tx-fault",
  112. + "tx-disable",
  113. + "rate-select",
  114. +};
  115. +
  116. +static const enum gpiod_flags gpio_flags[] = {
  117. + GPIOD_IN,
  118. + GPIOD_IN,
  119. + GPIOD_IN,
  120. + GPIOD_ASIS,
  121. + GPIOD_ASIS,
  122. +};
  123. +
  124. +#define T_INIT_JIFFIES msecs_to_jiffies(300)
  125. +#define T_RESET_US 10
  126. +#define T_FAULT_RECOVER msecs_to_jiffies(1000)
  127. +
  128. +/* SFP module presence detection is poor: the three MOD DEF signals are
  129. + * the same length on the PCB, which means it's possible for MOD DEF 0 to
  130. + * connect before the I2C bus on MOD DEF 1/2. Try to work around this
  131. + * design bug by waiting 50ms before probing, and then retry every 250ms.
  132. + */
  133. +#define T_PROBE_INIT msecs_to_jiffies(50)
  134. +#define T_PROBE_RETRY msecs_to_jiffies(250)
  135. +
  136. +/*
  137. + * SFP modules appear to always have their PHY configured for bus address
  138. + * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
  139. + */
  140. +#define SFP_PHY_ADDR 22
  141. +
  142. +/*
  143. + * Give this long for the PHY to reset.
  144. + */
  145. +#define T_PHY_RESET_MS 50
  146. +
  147. +static DEFINE_MUTEX(sfp_mutex);
  148. +
  149. +struct sfp {
  150. + struct device *dev;
  151. + struct i2c_adapter *i2c;
  152. + struct mii_bus *i2c_mii;
  153. + struct net_device *ndev;
  154. + struct phylink *phylink;
  155. + struct phy_device *mod_phy;
  156. +
  157. + unsigned int (*get_state)(struct sfp *);
  158. + void (*set_state)(struct sfp *, unsigned int);
  159. + int (*read)(struct sfp *, bool, u8, void *, size_t);
  160. +
  161. + struct gpio_desc *gpio[GPIO_MAX];
  162. +
  163. + unsigned int state;
  164. + struct delayed_work poll;
  165. + struct delayed_work timeout;
  166. + struct mutex sm_mutex;
  167. + unsigned char sm_mod_state;
  168. + unsigned char sm_dev_state;
  169. + unsigned short sm_state;
  170. + unsigned int sm_retries;
  171. +
  172. + struct sfp_eeprom_id id;
  173. +
  174. + struct notifier_block netdev_nb;
  175. +};
  176. +
  177. +static unsigned long poll_jiffies;
  178. +
  179. +static unsigned int sfp_gpio_get_state(struct sfp *sfp)
  180. +{
  181. + unsigned int i, state, v;
  182. +
  183. + for (i = state = 0; i < GPIO_MAX; i++) {
  184. + if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
  185. + continue;
  186. +
  187. + v = gpiod_get_value_cansleep(sfp->gpio[i]);
  188. + if (v)
  189. + state |= BIT(i);
  190. + }
  191. +
  192. + return state;
  193. +}
  194. +
  195. +static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state)
  196. +{
  197. + if (state & SFP_F_PRESENT) {
  198. + /* If the module is present, drive the signals */
  199. + if (sfp->gpio[GPIO_TX_DISABLE])
  200. + gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE],
  201. + state & SFP_F_TX_DISABLE);
  202. + if (state & SFP_F_RATE_SELECT)
  203. + gpiod_direction_output(sfp->gpio[GPIO_RATE_SELECT],
  204. + state & SFP_F_RATE_SELECT);
  205. + } else {
  206. + /* Otherwise, let them float to the pull-ups */
  207. + if (sfp->gpio[GPIO_TX_DISABLE])
  208. + gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]);
  209. + if (state & SFP_F_RATE_SELECT)
  210. + gpiod_direction_input(sfp->gpio[GPIO_RATE_SELECT]);
  211. + }
  212. +}
  213. +
  214. +static int sfp__i2c_read(struct i2c_adapter *i2c, u8 bus_addr, u8 dev_addr,
  215. + void *buf, size_t len)
  216. +{
  217. + struct i2c_msg msgs[2];
  218. + int ret;
  219. +
  220. + msgs[0].addr = bus_addr;
  221. + msgs[0].flags = 0;
  222. + msgs[0].len = 1;
  223. + msgs[0].buf = &dev_addr;
  224. + msgs[1].addr = bus_addr;
  225. + msgs[1].flags = I2C_M_RD;
  226. + msgs[1].len = len;
  227. + msgs[1].buf = buf;
  228. +
  229. + ret = i2c_transfer(i2c, msgs, ARRAY_SIZE(msgs));
  230. + if (ret < 0)
  231. + return ret;
  232. +
  233. + return ret == ARRAY_SIZE(msgs) ? len : 0;
  234. +}
  235. +
  236. +static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 addr, void *buf,
  237. + size_t len)
  238. +{
  239. + return sfp__i2c_read(sfp->i2c, a2 ? 0x51 : 0x50, addr, buf, len);
  240. +}
  241. +
  242. +static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
  243. +{
  244. + struct mii_bus *i2c_mii;
  245. + int ret;
  246. +
  247. + if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
  248. + return -EINVAL;
  249. +
  250. + sfp->i2c = i2c;
  251. + sfp->read = sfp_i2c_read;
  252. +
  253. + i2c_mii = mdio_i2c_alloc(sfp->dev, i2c);
  254. + if (IS_ERR(i2c_mii))
  255. + return PTR_ERR(i2c_mii);
  256. +
  257. + i2c_mii->name = "SFP I2C Bus";
  258. + i2c_mii->phy_mask = ~0;
  259. +
  260. + ret = mdiobus_register(i2c_mii);
  261. + if (ret < 0) {
  262. + mdiobus_free(i2c_mii);
  263. + return ret;
  264. + }
  265. +
  266. + sfp->i2c_mii = i2c_mii;
  267. +
  268. + return 0;
  269. +}
  270. +
  271. +
  272. +/* Interface */
  273. +static unsigned int sfp_get_state(struct sfp *sfp)
  274. +{
  275. + return sfp->get_state(sfp);
  276. +}
  277. +
  278. +static void sfp_set_state(struct sfp *sfp, unsigned int state)
  279. +{
  280. + sfp->set_state(sfp, state);
  281. +}
  282. +
  283. +static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
  284. +{
  285. + return sfp->read(sfp, a2, addr, buf, len);
  286. +}
  287. +
  288. +static unsigned int sfp_check(void *buf, size_t len)
  289. +{
  290. + u8 *p, check;
  291. +
  292. + for (p = buf, check = 0; len; p++, len--)
  293. + check += *p;
  294. +
  295. + return check;
  296. +}
  297. +
  298. +/* Helpers */
  299. +static void sfp_module_tx_disable(struct sfp *sfp)
  300. +{
  301. + dev_dbg(sfp->dev, "tx disable %u -> %u\n",
  302. + sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1);
  303. + sfp->state |= SFP_F_TX_DISABLE;
  304. + sfp_set_state(sfp, sfp->state);
  305. +}
  306. +
  307. +static void sfp_module_tx_enable(struct sfp *sfp)
  308. +{
  309. + dev_dbg(sfp->dev, "tx disable %u -> %u\n",
  310. + sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0);
  311. + sfp->state &= ~SFP_F_TX_DISABLE;
  312. + sfp_set_state(sfp, sfp->state);
  313. +}
  314. +
  315. +static void sfp_module_tx_fault_reset(struct sfp *sfp)
  316. +{
  317. + unsigned int state = sfp->state;
  318. +
  319. + if (state & SFP_F_TX_DISABLE)
  320. + return;
  321. +
  322. + sfp_set_state(sfp, state | SFP_F_TX_DISABLE);
  323. +
  324. + udelay(T_RESET_US);
  325. +
  326. + sfp_set_state(sfp, state);
  327. +}
  328. +
  329. +/* SFP state machine */
  330. +static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout)
  331. +{
  332. + if (timeout)
  333. + mod_delayed_work(system_power_efficient_wq, &sfp->timeout,
  334. + timeout);
  335. + else
  336. + cancel_delayed_work(&sfp->timeout);
  337. +}
  338. +
  339. +static void sfp_sm_next(struct sfp *sfp, unsigned int state,
  340. + unsigned int timeout)
  341. +{
  342. + sfp->sm_state = state;
  343. + sfp_sm_set_timer(sfp, timeout);
  344. +}
  345. +
  346. +static void sfp_sm_ins_next(struct sfp *sfp, unsigned int state, unsigned int timeout)
  347. +{
  348. + sfp->sm_mod_state = state;
  349. + sfp_sm_set_timer(sfp, timeout);
  350. +}
  351. +
  352. +static void sfp_sm_phy_detach(struct sfp *sfp)
  353. +{
  354. + phy_stop(sfp->mod_phy);
  355. + if (sfp->phylink)
  356. + phylink_disconnect_phy(sfp->phylink);
  357. + phy_device_remove(sfp->mod_phy);
  358. + phy_device_free(sfp->mod_phy);
  359. + sfp->mod_phy = NULL;
  360. +}
  361. +
  362. +static void sfp_sm_probe_phy(struct sfp *sfp)
  363. +{
  364. + struct phy_device *phy;
  365. + int err;
  366. +
  367. + msleep(T_PHY_RESET_MS);
  368. +
  369. + phy = mdiobus_scan(sfp->i2c_mii, SFP_PHY_ADDR);
  370. + if (IS_ERR(phy)) {
  371. + dev_err(sfp->dev, "mdiobus scan returned %ld\n", PTR_ERR(phy));
  372. + return;
  373. + }
  374. + if (!phy) {
  375. + dev_info(sfp->dev, "no PHY detected\n");
  376. + return;
  377. + }
  378. +
  379. + err = phylink_connect_phy(sfp->phylink, phy);
  380. + if (err) {
  381. + phy_device_remove(phy);
  382. + phy_device_free(phy);
  383. + dev_err(sfp->dev, "phylink_connect_phy failed: %d\n", err);
  384. + return;
  385. + }
  386. +
  387. + sfp->mod_phy = phy;
  388. + phy_start(phy);
  389. +}
  390. +
  391. +static void sfp_sm_link_up(struct sfp *sfp)
  392. +{
  393. + if (sfp->phylink)
  394. + phylink_enable(sfp->phylink);
  395. +
  396. + sfp_sm_next(sfp, SFP_S_LINK_UP, 0);
  397. +}
  398. +
  399. +static void sfp_sm_link_down(struct sfp *sfp)
  400. +{
  401. + if (sfp->phylink)
  402. + phylink_disable(sfp->phylink);
  403. +}
  404. +
  405. +static void sfp_sm_link_check_los(struct sfp *sfp)
  406. +{
  407. + unsigned int los = sfp->state & SFP_F_LOS;
  408. +
  409. + /* FIXME: what if neither SFP_OPTIONS_LOS_INVERTED nor
  410. + * SFP_OPTIONS_LOS_NORMAL are set? For now, we assume
  411. + * the same as SFP_OPTIONS_LOS_NORMAL set.
  412. + */
  413. + if (sfp->id.ext.options & SFP_OPTIONS_LOS_INVERTED)
  414. + los ^= SFP_F_LOS;
  415. +
  416. + if (los)
  417. + sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
  418. + else
  419. + sfp_sm_link_up(sfp);
  420. +}
  421. +
  422. +static void sfp_sm_fault(struct sfp *sfp, bool warn)
  423. +{
  424. + if (sfp->sm_retries && !--sfp->sm_retries) {
  425. + dev_err(sfp->dev, "module persistently indicates fault, disabling\n");
  426. + sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0);
  427. + } else {
  428. + if (warn)
  429. + dev_err(sfp->dev, "module transmit fault indicated\n");
  430. +
  431. + sfp_sm_next(sfp, SFP_S_TX_FAULT, T_FAULT_RECOVER);
  432. + }
  433. +}
  434. +
  435. +static void sfp_sm_mod_init(struct sfp *sfp)
  436. +{
  437. + sfp_module_tx_enable(sfp);
  438. +
  439. + /* Wait t_init before indicating that the link is up, provided the
  440. + * current state indicates no TX_FAULT. If TX_FAULT clears before
  441. + * this time, that's fine too.
  442. + */
  443. + sfp_sm_next(sfp, SFP_S_INIT, T_INIT_JIFFIES);
  444. + sfp->sm_retries = 5;
  445. +
  446. + if (sfp->phylink) {
  447. + /* Setting the serdes link mode is guesswork: there's no
  448. + * field in the EEPROM which indicates what mode should
  449. + * be used.
  450. + *
  451. + * If it's a gigabit-only fiber module, it probably does
  452. + * not have a PHY, so switch to 802.3z negotiation mode.
  453. + * Otherwise, switch to SGMII mode (which is required to
  454. + * support non-gigabit speeds) and probe for a PHY.
  455. + */
  456. + if (!sfp->id.base.e1000_base_t &&
  457. + !sfp->id.base.e100_base_lx &&
  458. + !sfp->id.base.e100_base_fx) {
  459. + phylink_set_link_an_mode(sfp->phylink, MLO_AN_8023Z);
  460. + } else {
  461. + phylink_set_link_an_mode(sfp->phylink, MLO_AN_SGMII);
  462. + sfp_sm_probe_phy(sfp);
  463. + }
  464. + }
  465. +}
  466. +
  467. +static int sfp_sm_mod_probe(struct sfp *sfp)
  468. +{
  469. + /* SFP module inserted - read I2C data */
  470. + struct sfp_eeprom_id id;
  471. + char vendor[17];
  472. + char part[17];
  473. + char sn[17];
  474. + char date[9];
  475. + char rev[5];
  476. + u8 check;
  477. + int err;
  478. +
  479. + err = sfp_read(sfp, false, 0, &id, sizeof(id));
  480. + if (err < 0) {
  481. + dev_err(sfp->dev, "failed to read EEPROM: %d\n", err);
  482. + return -EAGAIN;
  483. + }
  484. +
  485. + /* Validate the checksum over the base structure */
  486. + check = sfp_check(&id.base, sizeof(id.base) - 1);
  487. + if (check != id.base.cc_base) {
  488. + dev_err(sfp->dev,
  489. + "EEPROM base structure checksum failure: 0x%02x\n",
  490. + check);
  491. + return -EINVAL;
  492. + }
  493. +
  494. + check = sfp_check(&id.ext, sizeof(id.ext) - 1);
  495. + if (check != id.ext.cc_ext) {
  496. + dev_err(sfp->dev,
  497. + "EEPROM extended structure checksum failure: 0x%02x\n",
  498. + check);
  499. + memset(&id.ext, 0, sizeof(id.ext));
  500. + }
  501. +
  502. + sfp->id = id;
  503. +
  504. + memcpy(vendor, sfp->id.base.vendor_name, 16);
  505. + vendor[16] = '\0';
  506. + memcpy(part, sfp->id.base.vendor_pn, 16);
  507. + part[16] = '\0';
  508. + memcpy(rev, sfp->id.base.vendor_rev, 4);
  509. + rev[4] = '\0';
  510. + memcpy(sn, sfp->id.ext.vendor_sn, 16);
  511. + sn[16] = '\0';
  512. + memcpy(date, sfp->id.ext.datecode, 8);
  513. + date[8] = '\0';
  514. +
  515. + dev_info(sfp->dev, "module %s %s rev %s sn %s dc %s\n", vendor, part, rev, sn, date);
  516. +
  517. + /* We only support SFP modules, not the legacy GBIC modules. */
  518. + if (sfp->id.base.phys_id != SFP_PHYS_ID_SFP ||
  519. + sfp->id.base.phys_ext_id != SFP_PHYS_EXT_ID_SFP) {
  520. + dev_err(sfp->dev, "module is not SFP - phys id 0x%02x 0x%02x\n",
  521. + sfp->id.base.phys_id, sfp->id.base.phys_ext_id);
  522. + return -EINVAL;
  523. + }
  524. +
  525. + /*
  526. + * What isn't clear from the SFP documentation is whether this
  527. + * specifies the encoding expected on the TD/RD lines, or whether
  528. + * the TD/RD lines are always 8b10b encoded, but the transceiver
  529. + * converts. Eg, think of a copper SFP supporting 1G/100M/10M
  530. + * ethernet: this requires 8b10b encoding for 1G, 4b5b for 100M,
  531. + * and manchester for 10M.
  532. + */
  533. + /* 1Gbit ethernet requires 8b10b encoding */
  534. + if (sfp->id.base.encoding != SFP_ENCODING_8B10B) {
  535. + dev_err(sfp->dev, "module does not support 8B10B encoding\n");
  536. + return -EINVAL;
  537. + }
  538. +
  539. + if (sfp->phylink) {
  540. + u32 support;
  541. + u8 port;
  542. +
  543. + if (sfp->id.base.e1000_base_t) {
  544. + support = SUPPORTED_TP;
  545. + port = PORT_TP;
  546. + } else {
  547. + support = SUPPORTED_FIBRE;
  548. + port = PORT_FIBRE;
  549. + }
  550. + phylink_set_link_port(sfp->phylink, support, port);
  551. + }
  552. +
  553. + return 0;
  554. +}
  555. +
  556. +static void sfp_sm_mod_remove(struct sfp *sfp)
  557. +{
  558. + if (sfp->mod_phy)
  559. + sfp_sm_phy_detach(sfp);
  560. +
  561. + sfp_module_tx_disable(sfp);
  562. +
  563. + memset(&sfp->id, 0, sizeof(sfp->id));
  564. +
  565. + dev_info(sfp->dev, "module removed\n");
  566. +}
  567. +
  568. +static void sfp_sm_event(struct sfp *sfp, unsigned int event)
  569. +{
  570. + mutex_lock(&sfp->sm_mutex);
  571. +
  572. + dev_dbg(sfp->dev, "SM: enter %u:%u:%u event %u\n",
  573. + sfp->sm_mod_state, sfp->sm_dev_state, sfp->sm_state, event);
  574. +
  575. + /* This state machine tracks the insert/remove state of
  576. + * the module, and handles probing the on-board EEPROM.
  577. + */
  578. + switch (sfp->sm_mod_state) {
  579. + default:
  580. + if (event == SFP_E_INSERT) {
  581. + sfp_module_tx_disable(sfp);
  582. + sfp_sm_ins_next(sfp, SFP_MOD_PROBE, T_PROBE_INIT);
  583. + }
  584. + break;
  585. +
  586. + case SFP_MOD_PROBE:
  587. + if (event == SFP_E_REMOVE) {
  588. + sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
  589. + } else if (event == SFP_E_TIMEOUT) {
  590. + int err = sfp_sm_mod_probe(sfp);
  591. +
  592. + if (err == 0)
  593. + sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
  594. + else if (err == -EAGAIN)
  595. + sfp_sm_set_timer(sfp, T_PROBE_RETRY);
  596. + else
  597. + sfp_sm_ins_next(sfp, SFP_MOD_ERROR, 0);
  598. + }
  599. + break;
  600. +
  601. + case SFP_MOD_PRESENT:
  602. + case SFP_MOD_ERROR:
  603. + if (event == SFP_E_REMOVE) {
  604. + sfp_sm_mod_remove(sfp);
  605. + sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
  606. + }
  607. + break;
  608. + }
  609. +
  610. + /* This state machine tracks the netdev up/down state */
  611. + switch (sfp->sm_dev_state) {
  612. + default:
  613. + if (event == SFP_E_DEV_UP)
  614. + sfp->sm_dev_state = SFP_DEV_UP;
  615. + break;
  616. +
  617. + case SFP_DEV_UP:
  618. + if (event == SFP_E_DEV_DOWN) {
  619. + /* If the module has a PHY, avoid raising TX disable
  620. + * as this resets the PHY. Otherwise, raise it to
  621. + * turn the laser off.
  622. + */
  623. + if (!sfp->mod_phy)
  624. + sfp_module_tx_disable(sfp);
  625. + sfp->sm_dev_state = SFP_DEV_DOWN;
  626. + }
  627. + break;
  628. + }
  629. +
  630. + /* Some events are global */
  631. + if (sfp->sm_state != SFP_S_DOWN &&
  632. + (sfp->sm_mod_state != SFP_MOD_PRESENT ||
  633. + sfp->sm_dev_state != SFP_DEV_UP)) {
  634. + if (sfp->sm_state == SFP_S_LINK_UP &&
  635. + sfp->sm_dev_state == SFP_DEV_UP)
  636. + sfp_sm_link_down(sfp);
  637. + if (sfp->mod_phy)
  638. + sfp_sm_phy_detach(sfp);
  639. + sfp_sm_next(sfp, SFP_S_DOWN, 0);
  640. + mutex_unlock(&sfp->sm_mutex);
  641. + return;
  642. + }
  643. +
  644. + /* The main state machine */
  645. + switch (sfp->sm_state) {
  646. + case SFP_S_DOWN:
  647. + if (sfp->sm_mod_state == SFP_MOD_PRESENT &&
  648. + sfp->sm_dev_state == SFP_DEV_UP)
  649. + sfp_sm_mod_init(sfp);
  650. + break;
  651. +
  652. + case SFP_S_INIT:
  653. + if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT)
  654. + sfp_sm_fault(sfp, true);
  655. + else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR)
  656. + sfp_sm_link_check_los(sfp);
  657. + break;
  658. +
  659. + case SFP_S_WAIT_LOS:
  660. + if (event == SFP_E_TX_FAULT)
  661. + sfp_sm_fault(sfp, true);
  662. + else if (event ==
  663. + (sfp->id.ext.options & SFP_OPTIONS_LOS_INVERTED ?
  664. + SFP_E_LOS_HIGH : SFP_E_LOS_LOW))
  665. + sfp_sm_link_up(sfp);
  666. + break;
  667. +
  668. + case SFP_S_LINK_UP:
  669. + if (event == SFP_E_TX_FAULT) {
  670. + sfp_sm_link_down(sfp);
  671. + sfp_sm_fault(sfp, true);
  672. + } else if (event ==
  673. + (sfp->id.ext.options & SFP_OPTIONS_LOS_INVERTED ?
  674. + SFP_E_LOS_LOW : SFP_E_LOS_HIGH)) {
  675. + sfp_sm_link_down(sfp);
  676. + sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
  677. + }
  678. + break;
  679. +
  680. + case SFP_S_TX_FAULT:
  681. + if (event == SFP_E_TIMEOUT) {
  682. + sfp_module_tx_fault_reset(sfp);
  683. + sfp_sm_next(sfp, SFP_S_REINIT, T_INIT_JIFFIES);
  684. + }
  685. + break;
  686. +
  687. + case SFP_S_REINIT:
  688. + if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
  689. + sfp_sm_fault(sfp, false);
  690. + } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
  691. + dev_info(sfp->dev, "module transmit fault recovered\n");
  692. + sfp_sm_link_check_los(sfp);
  693. + }
  694. + break;
  695. +
  696. + case SFP_S_TX_DISABLE:
  697. + break;
  698. + }
  699. +
  700. + dev_dbg(sfp->dev, "SM: exit %u:%u:%u\n",
  701. + sfp->sm_mod_state, sfp->sm_dev_state, sfp->sm_state);
  702. +
  703. + mutex_unlock(&sfp->sm_mutex);
  704. +}
  705. +
  706. +#if 0
  707. +static int sfp_phy_module_info(struct phy_device *phy,
  708. + struct ethtool_modinfo *modinfo)
  709. +{
  710. + struct sfp *sfp = phy->priv;
  711. +
  712. + /* locking... and check module is present */
  713. +
  714. + if (sfp->id.ext.sff8472_compliance) {
  715. + modinfo->type = ETH_MODULE_SFF_8472;
  716. + modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
  717. + } else {
  718. + modinfo->type = ETH_MODULE_SFF_8079;
  719. + modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
  720. + }
  721. + return 0;
  722. +}
  723. +
  724. +static int sfp_phy_module_eeprom(struct phy_device *phy,
  725. + struct ethtool_eeprom *ee, u8 *data)
  726. +{
  727. + struct sfp *sfp = phy->priv;
  728. + unsigned int first, last, len;
  729. + int ret;
  730. +
  731. + if (ee->len == 0)
  732. + return -EINVAL;
  733. +
  734. + first = ee->offset;
  735. + last = ee->offset + ee->len;
  736. + if (first < ETH_MODULE_SFF_8079_LEN) {
  737. + len = last;
  738. + if (len > ETH_MODULE_SFF_8079_LEN)
  739. + len = ETH_MODULE_SFF_8079_LEN;
  740. + len -= first;
  741. +
  742. + ret = sfp->read(sfp, false, first, data, len);
  743. + if (ret < 0)
  744. + return ret;
  745. +
  746. + first += len;
  747. + data += len;
  748. + }
  749. + if (first >= ETH_MODULE_SFF_8079_LEN && last > first) {
  750. + len = last - first;
  751. +
  752. + ret = sfp->read(sfp, true, first, data, len);
  753. + if (ret < 0)
  754. + return ret;
  755. + }
  756. + return 0;
  757. +}
  758. +#endif
  759. +
  760. +static void sfp_timeout(struct work_struct *work)
  761. +{
  762. + struct sfp *sfp = container_of(work, struct sfp, timeout.work);
  763. +
  764. + sfp_sm_event(sfp, SFP_E_TIMEOUT);
  765. +}
  766. +
  767. +static void sfp_check_state(struct sfp *sfp)
  768. +{
  769. + unsigned int state, i, changed;
  770. +
  771. + state = sfp_get_state(sfp);
  772. + changed = state ^ sfp->state;
  773. + changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT;
  774. +
  775. + for (i = 0; i < GPIO_MAX; i++)
  776. + if (changed & BIT(i))
  777. + dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_of_names[i],
  778. + !!(sfp->state & BIT(i)), !!(state & BIT(i)));
  779. +
  780. + state |= sfp->state & (SFP_F_TX_DISABLE | SFP_F_RATE_SELECT);
  781. + sfp->state = state;
  782. +
  783. + if (changed & SFP_F_PRESENT)
  784. + sfp_sm_event(sfp, state & SFP_F_PRESENT ?
  785. + SFP_E_INSERT : SFP_E_REMOVE);
  786. +
  787. + if (changed & SFP_F_TX_FAULT)
  788. + sfp_sm_event(sfp, state & SFP_F_TX_FAULT ?
  789. + SFP_E_TX_FAULT : SFP_E_TX_CLEAR);
  790. +
  791. + if (changed & SFP_F_LOS)
  792. + sfp_sm_event(sfp, state & SFP_F_LOS ?
  793. + SFP_E_LOS_HIGH : SFP_E_LOS_LOW);
  794. +}
  795. +
  796. +static irqreturn_t sfp_irq(int irq, void *data)
  797. +{
  798. + struct sfp *sfp = data;
  799. +
  800. + sfp_check_state(sfp);
  801. +
  802. + return IRQ_HANDLED;
  803. +}
  804. +
  805. +static void sfp_poll(struct work_struct *work)
  806. +{
  807. + struct sfp *sfp = container_of(work, struct sfp, poll.work);
  808. +
  809. + sfp_check_state(sfp);
  810. + mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
  811. +}
  812. +
  813. +static int sfp_netdev_notify(struct notifier_block *nb, unsigned long act, void *data)
  814. +{
  815. + struct sfp *sfp = container_of(nb, struct sfp, netdev_nb);
  816. + struct netdev_notifier_info *info = data;
  817. + struct net_device *ndev = info->dev;
  818. +
  819. + if (!sfp->ndev || ndev != sfp->ndev)
  820. + return NOTIFY_DONE;
  821. +
  822. + switch (act) {
  823. + case NETDEV_UP:
  824. + sfp_sm_event(sfp, SFP_E_DEV_UP);
  825. + break;
  826. +
  827. + case NETDEV_GOING_DOWN:
  828. + sfp_sm_event(sfp, SFP_E_DEV_DOWN);
  829. + break;
  830. +
  831. + case NETDEV_UNREGISTER:
  832. + if (sfp->mod_phy && sfp->phylink)
  833. + phylink_disconnect_phy(sfp->phylink);
  834. + sfp->phylink = NULL;
  835. + dev_put(sfp->ndev);
  836. + sfp->ndev = NULL;
  837. + break;
  838. + }
  839. + return NOTIFY_OK;
  840. +}
  841. +
  842. +static struct sfp *sfp_alloc(struct device *dev)
  843. +{
  844. + struct sfp *sfp;
  845. +
  846. + sfp = kzalloc(sizeof(*sfp), GFP_KERNEL);
  847. + if (!sfp)
  848. + return ERR_PTR(-ENOMEM);
  849. +
  850. + sfp->dev = dev;
  851. +
  852. + mutex_init(&sfp->sm_mutex);
  853. + INIT_DELAYED_WORK(&sfp->poll, sfp_poll);
  854. + INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout);
  855. +
  856. + sfp->netdev_nb.notifier_call = sfp_netdev_notify;
  857. +
  858. + return sfp;
  859. +}
  860. +
  861. +static void sfp_destroy(struct sfp *sfp)
  862. +{
  863. + cancel_delayed_work_sync(&sfp->poll);
  864. + cancel_delayed_work_sync(&sfp->timeout);
  865. + if (sfp->i2c_mii) {
  866. + mdiobus_unregister(sfp->i2c_mii);
  867. + mdiobus_free(sfp->i2c_mii);
  868. + }
  869. + if (sfp->i2c)
  870. + i2c_put_adapter(sfp->i2c);
  871. + of_node_put(sfp->dev->of_node);
  872. + kfree(sfp);
  873. +}
  874. +
  875. +static void sfp_cleanup(void *data)
  876. +{
  877. + struct sfp *sfp = data;
  878. +
  879. + sfp_destroy(sfp);
  880. +}
  881. +
  882. +static int sfp_probe(struct platform_device *pdev)
  883. +{
  884. + struct sfp *sfp;
  885. + bool poll = false;
  886. + int irq, err, i;
  887. +
  888. + sfp = sfp_alloc(&pdev->dev);
  889. + if (IS_ERR(sfp))
  890. + return PTR_ERR(sfp);
  891. +
  892. + platform_set_drvdata(pdev, sfp);
  893. +
  894. + err = devm_add_action(sfp->dev, sfp_cleanup, sfp);
  895. + if (err < 0)
  896. + return err;
  897. +
  898. + if (pdev->dev.of_node) {
  899. + struct device_node *node = pdev->dev.of_node;
  900. + struct device_node *np;
  901. +
  902. + np = of_parse_phandle(node, "i2c-bus", 0);
  903. + if (np) {
  904. + struct i2c_adapter *i2c;
  905. +
  906. + i2c = of_find_i2c_adapter_by_node(np);
  907. + of_node_put(np);
  908. + if (!i2c)
  909. + return -EPROBE_DEFER;
  910. +
  911. + err = sfp_i2c_configure(sfp, i2c);
  912. + if (err < 0) {
  913. + i2c_put_adapter(i2c);
  914. + return err;
  915. + }
  916. + }
  917. +
  918. + for (i = 0; i < GPIO_MAX; i++) {
  919. + sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev,
  920. + gpio_of_names[i], gpio_flags[i]);
  921. + if (IS_ERR(sfp->gpio[i]))
  922. + return PTR_ERR(sfp->gpio[i]);
  923. + }
  924. +
  925. + sfp->get_state = sfp_gpio_get_state;
  926. + sfp->set_state = sfp_gpio_set_state;
  927. +
  928. + np = of_parse_phandle(node, "sfp,ethernet", 0);
  929. + if (!np) {
  930. + dev_err(sfp->dev, "missing sfp,ethernet property\n");
  931. + return -EINVAL;
  932. + }
  933. +
  934. + sfp->ndev = of_find_net_device_by_node(np);
  935. + if (!sfp->ndev) {
  936. + dev_err(sfp->dev, "ethernet device not found\n");
  937. + return -EPROBE_DEFER;
  938. + }
  939. +
  940. + dev_hold(sfp->ndev);
  941. + put_device(&sfp->ndev->dev);
  942. +
  943. + sfp->phylink = phylink_lookup_by_netdev(sfp->ndev);
  944. + if (!sfp->phylink) {
  945. + dev_err(sfp->dev, "ethernet device not found\n");
  946. + return -EPROBE_DEFER;
  947. + }
  948. +
  949. + phylink_disable(sfp->phylink);
  950. + }
  951. +
  952. + sfp->state = sfp_get_state(sfp);
  953. + if (sfp->gpio[GPIO_TX_DISABLE] &&
  954. + gpiod_get_value_cansleep(sfp->gpio[GPIO_TX_DISABLE]))
  955. + sfp->state |= SFP_F_TX_DISABLE;
  956. + if (sfp->gpio[GPIO_RATE_SELECT] &&
  957. + gpiod_get_value_cansleep(sfp->gpio[GPIO_RATE_SELECT]))
  958. + sfp->state |= SFP_F_RATE_SELECT;
  959. + sfp_set_state(sfp, sfp->state);
  960. + sfp_module_tx_disable(sfp);
  961. + if (sfp->state & SFP_F_PRESENT)
  962. + sfp_sm_event(sfp, SFP_E_INSERT);
  963. +
  964. + for (i = 0; i < GPIO_MAX; i++) {
  965. + if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
  966. + continue;
  967. +
  968. + irq = gpiod_to_irq(sfp->gpio[i]);
  969. + if (!irq) {
  970. + poll = true;
  971. + continue;
  972. + }
  973. +
  974. + err = devm_request_threaded_irq(sfp->dev, irq, NULL, sfp_irq,
  975. + IRQF_ONESHOT |
  976. + IRQF_TRIGGER_RISING |
  977. + IRQF_TRIGGER_FALLING,
  978. + dev_name(sfp->dev), sfp);
  979. + if (err)
  980. + poll = true;
  981. + }
  982. +
  983. + if (poll)
  984. + mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
  985. +
  986. + register_netdevice_notifier(&sfp->netdev_nb);
  987. +
  988. + return 0;
  989. +}
  990. +
  991. +static int sfp_remove(struct platform_device *pdev)
  992. +{
  993. + struct sfp *sfp = platform_get_drvdata(pdev);
  994. +
  995. + unregister_netdevice_notifier(&sfp->netdev_nb);
  996. + if (sfp->ndev)
  997. + dev_put(sfp->ndev);
  998. +
  999. + return 0;
  1000. +}
  1001. +
  1002. +static const struct of_device_id sfp_of_match[] = {
  1003. + { .compatible = "sff,sfp", },
  1004. + { },
  1005. +};
  1006. +MODULE_DEVICE_TABLE(of, sfp_of_match);
  1007. +
  1008. +static struct platform_driver sfp_driver = {
  1009. + .probe = sfp_probe,
  1010. + .remove = sfp_remove,
  1011. + .driver = {
  1012. + .name = "sfp",
  1013. + .of_match_table = sfp_of_match,
  1014. + },
  1015. +};
  1016. +
  1017. +static int sfp_init(void)
  1018. +{
  1019. + poll_jiffies = msecs_to_jiffies(100);
  1020. +
  1021. + return platform_driver_register(&sfp_driver);
  1022. +}
  1023. +module_init(sfp_init);
  1024. +
  1025. +static void sfp_exit(void)
  1026. +{
  1027. + platform_driver_unregister(&sfp_driver);
  1028. +}
  1029. +module_exit(sfp_exit);
  1030. +
  1031. +MODULE_ALIAS("platform:sfp");
  1032. +MODULE_AUTHOR("Russell King");
  1033. +MODULE_LICENSE("GPL v2");
  1034. --- /dev/null
  1035. +++ b/include/linux/sfp.h
  1036. @@ -0,0 +1,339 @@
  1037. +#ifndef LINUX_SFP_H
  1038. +#define LINUX_SFP_H
  1039. +
  1040. +struct __packed sfp_eeprom_base {
  1041. + u8 phys_id;
  1042. + u8 phys_ext_id;
  1043. + u8 connector;
  1044. +#if defined __BIG_ENDIAN_BITFIELD
  1045. + u8 e10g_base_er:1;
  1046. + u8 e10g_base_lrm:1;
  1047. + u8 e10g_base_lr:1;
  1048. + u8 e10g_base_sr:1;
  1049. + u8 if_1x_sx:1;
  1050. + u8 if_1x_lx:1;
  1051. + u8 if_1x_copper_active:1;
  1052. + u8 if_1x_copper_passive:1;
  1053. +
  1054. + u8 escon_mmf_1310_led:1;
  1055. + u8 escon_smf_1310_laser:1;
  1056. + u8 sonet_oc192_short_reach:1;
  1057. + u8 sonet_reach_bit1:1;
  1058. + u8 sonet_reach_bit2:1;
  1059. + u8 sonet_oc48_long_reach:1;
  1060. + u8 sonet_oc48_intermediate_reach:1;
  1061. + u8 sonet_oc48_short_reach:1;
  1062. +
  1063. + u8 unallocated_5_7:1;
  1064. + u8 sonet_oc12_smf_long_reach:1;
  1065. + u8 sonet_oc12_smf_intermediate_reach:1;
  1066. + u8 sonet_oc12_short_reach:1;
  1067. + u8 unallocated_5_3:1;
  1068. + u8 sonet_oc3_smf_long_reach:1;
  1069. + u8 sonet_oc3_smf_intermediate_reach:1;
  1070. + u8 sonet_oc3_short_reach:1;
  1071. +
  1072. + u8 e_base_px:1;
  1073. + u8 e_base_bx10:1;
  1074. + u8 e100_base_fx:1;
  1075. + u8 e100_base_lx:1;
  1076. + u8 e1000_base_t:1;
  1077. + u8 e1000_base_cx:1;
  1078. + u8 e1000_base_lx:1;
  1079. + u8 e1000_base_sx:1;
  1080. +
  1081. + u8 fc_ll_v:1;
  1082. + u8 fc_ll_s:1;
  1083. + u8 fc_ll_i:1;
  1084. + u8 fc_ll_l:1;
  1085. + u8 fc_ll_m:1;
  1086. + u8 fc_tech_sa:1;
  1087. + u8 fc_tech_lc:1;
  1088. + u8 fc_tech_electrical_inter_enclosure:1;
  1089. +
  1090. + u8 fc_tech_electrical_intra_enclosure:1;
  1091. + u8 fc_tech_sn:1;
  1092. + u8 fc_tech_sl:1;
  1093. + u8 fc_tech_ll:1;
  1094. + u8 sfp_ct_active:1;
  1095. + u8 sfp_ct_passive:1;
  1096. + u8 unallocated_8_1:1;
  1097. + u8 unallocated_8_0:1;
  1098. +
  1099. + u8 fc_media_tw:1;
  1100. + u8 fc_media_tp:1;
  1101. + u8 fc_media_mi:1;
  1102. + u8 fc_media_tv:1;
  1103. + u8 fc_media_m6:1;
  1104. + u8 fc_media_m5:1;
  1105. + u8 unallocated_9_1:1;
  1106. + u8 fc_media_sm:1;
  1107. +
  1108. + u8 fc_speed_1200:1;
  1109. + u8 fc_speed_800:1;
  1110. + u8 fc_speed_1600:1;
  1111. + u8 fc_speed_400:1;
  1112. + u8 fc_speed_3200:1;
  1113. + u8 fc_speed_200:1;
  1114. + u8 unallocated_10_1:1;
  1115. + u8 fc_speed_100:1;
  1116. +#elif defined __LITTLE_ENDIAN_BITFIELD
  1117. + u8 if_1x_copper_passive:1;
  1118. + u8 if_1x_copper_active:1;
  1119. + u8 if_1x_lx:1;
  1120. + u8 if_1x_sx:1;
  1121. + u8 e10g_base_sr:1;
  1122. + u8 e10g_base_lr:1;
  1123. + u8 e10g_base_lrm:1;
  1124. + u8 e10g_base_er:1;
  1125. +
  1126. + u8 sonet_oc3_short_reach:1;
  1127. + u8 sonet_oc3_smf_intermediate_reach:1;
  1128. + u8 sonet_oc3_smf_long_reach:1;
  1129. + u8 unallocated_5_3:1;
  1130. + u8 sonet_oc12_short_reach:1;
  1131. + u8 sonet_oc12_smf_intermediate_reach:1;
  1132. + u8 sonet_oc12_smf_long_reach:1;
  1133. + u8 unallocated_5_7:1;
  1134. +
  1135. + u8 sonet_oc48_short_reach:1;
  1136. + u8 sonet_oc48_intermediate_reach:1;
  1137. + u8 sonet_oc48_long_reach:1;
  1138. + u8 sonet_reach_bit2:1;
  1139. + u8 sonet_reach_bit1:1;
  1140. + u8 sonet_oc192_short_reach:1;
  1141. + u8 escon_smf_1310_laser:1;
  1142. + u8 escon_mmf_1310_led:1;
  1143. +
  1144. + u8 e1000_base_sx:1;
  1145. + u8 e1000_base_lx:1;
  1146. + u8 e1000_base_cx:1;
  1147. + u8 e1000_base_t:1;
  1148. + u8 e100_base_lx:1;
  1149. + u8 e100_base_fx:1;
  1150. + u8 e_base_bx10:1;
  1151. + u8 e_base_px:1;
  1152. +
  1153. + u8 fc_tech_electrical_inter_enclosure:1;
  1154. + u8 fc_tech_lc:1;
  1155. + u8 fc_tech_sa:1;
  1156. + u8 fc_ll_m:1;
  1157. + u8 fc_ll_l:1;
  1158. + u8 fc_ll_i:1;
  1159. + u8 fc_ll_s:1;
  1160. + u8 fc_ll_v:1;
  1161. +
  1162. + u8 unallocated_8_0:1;
  1163. + u8 unallocated_8_1:1;
  1164. + u8 sfp_ct_passive:1;
  1165. + u8 sfp_ct_active:1;
  1166. + u8 fc_tech_ll:1;
  1167. + u8 fc_tech_sl:1;
  1168. + u8 fc_tech_sn:1;
  1169. + u8 fc_tech_electrical_intra_enclosure:1;
  1170. +
  1171. + u8 fc_media_sm:1;
  1172. + u8 unallocated_9_1:1;
  1173. + u8 fc_media_m5:1;
  1174. + u8 fc_media_m6:1;
  1175. + u8 fc_media_tv:1;
  1176. + u8 fc_media_mi:1;
  1177. + u8 fc_media_tp:1;
  1178. + u8 fc_media_tw:1;
  1179. +
  1180. + u8 fc_speed_100:1;
  1181. + u8 unallocated_10_1:1;
  1182. + u8 fc_speed_200:1;
  1183. + u8 fc_speed_3200:1;
  1184. + u8 fc_speed_400:1;
  1185. + u8 fc_speed_1600:1;
  1186. + u8 fc_speed_800:1;
  1187. + u8 fc_speed_1200:1;
  1188. +#else
  1189. +#error Unknown Endian
  1190. +#endif
  1191. + u8 encoding;
  1192. + u8 br_nominal;
  1193. + u8 rate_id;
  1194. + u8 link_len[6];
  1195. + char vendor_name[16];
  1196. + u8 reserved36;
  1197. + char vendor_oui[3];
  1198. + char vendor_pn[16];
  1199. + char vendor_rev[4];
  1200. + union {
  1201. + __be16 optical_wavelength;
  1202. + u8 cable_spec;
  1203. + };
  1204. + u8 reserved62;
  1205. + u8 cc_base;
  1206. +};
  1207. +
  1208. +struct __packed sfp_eeprom_ext {
  1209. + __be16 options;
  1210. + u8 br_max;
  1211. + u8 br_min;
  1212. + char vendor_sn[16];
  1213. + char datecode[8];
  1214. + u8 diagmon;
  1215. + u8 enhopts;
  1216. + u8 sff8472_compliance;
  1217. + u8 cc_ext;
  1218. +};
  1219. +
  1220. +struct __packed sfp_eeprom_id {
  1221. + struct sfp_eeprom_base base;
  1222. + struct sfp_eeprom_ext ext;
  1223. +};
  1224. +
  1225. +/* SFP EEPROM registers */
  1226. +enum {
  1227. + SFP_PHYS_ID = 0x00,
  1228. + SFP_PHYS_EXT_ID = 0x01,
  1229. + SFP_CONNECTOR = 0x02,
  1230. + SFP_COMPLIANCE = 0x03,
  1231. + SFP_ENCODING = 0x0b,
  1232. + SFP_BR_NOMINAL = 0x0c,
  1233. + SFP_RATE_ID = 0x0d,
  1234. + SFP_LINK_LEN_SM_KM = 0x0e,
  1235. + SFP_LINK_LEN_SM_100M = 0x0f,
  1236. + SFP_LINK_LEN_50UM_OM2_10M = 0x10,
  1237. + SFP_LINK_LEN_62_5UM_OM1_10M = 0x11,
  1238. + SFP_LINK_LEN_COPPER_1M = 0x12,
  1239. + SFP_LINK_LEN_50UM_OM4_10M = 0x12,
  1240. + SFP_LINK_LEN_50UM_OM3_10M = 0x13,
  1241. + SFP_VENDOR_NAME = 0x14,
  1242. + SFP_VENDOR_OUI = 0x25,
  1243. + SFP_VENDOR_PN = 0x28,
  1244. + SFP_VENDOR_REV = 0x38,
  1245. + SFP_OPTICAL_WAVELENGTH_MSB = 0x3c,
  1246. + SFP_OPTICAL_WAVELENGTH_LSB = 0x3d,
  1247. + SFP_CABLE_SPEC = 0x3c,
  1248. + SFP_CC_BASE = 0x3f,
  1249. + SFP_OPTIONS = 0x40, /* 2 bytes, MSB, LSB */
  1250. + SFP_BR_MAX = 0x42,
  1251. + SFP_BR_MIN = 0x43,
  1252. + SFP_VENDOR_SN = 0x44,
  1253. + SFP_DATECODE = 0x54,
  1254. + SFP_DIAGMON = 0x5c,
  1255. + SFP_ENHOPTS = 0x5d,
  1256. + SFP_SFF8472_COMPLIANCE = 0x5e,
  1257. + SFP_CC_EXT = 0x5f,
  1258. +
  1259. + SFP_PHYS_ID_SFP = 0x03,
  1260. + SFP_PHYS_EXT_ID_SFP = 0x04,
  1261. + SFP_CONNECTOR_UNSPEC = 0x00,
  1262. + /* codes 01-05 not supportable on SFP, but some modules have single SC */
  1263. + SFP_CONNECTOR_SC = 0x01,
  1264. + SFP_CONNECTOR_FIBERJACK = 0x06,
  1265. + SFP_CONNECTOR_LC = 0x07,
  1266. + SFP_CONNECTOR_MT_RJ = 0x08,
  1267. + SFP_CONNECTOR_MU = 0x09,
  1268. + SFP_CONNECTOR_SG = 0x0a,
  1269. + SFP_CONNECTOR_OPTICAL_PIGTAIL = 0x0b,
  1270. + SFP_CONNECTOR_HSSDC_II = 0x20,
  1271. + SFP_CONNECTOR_COPPER_PIGTAIL = 0x21,
  1272. + SFP_ENCODING_UNSPEC = 0x00,
  1273. + SFP_ENCODING_8B10B = 0x01,
  1274. + SFP_ENCODING_4B5B = 0x02,
  1275. + SFP_ENCODING_NRZ = 0x03,
  1276. + SFP_ENCODING_MANCHESTER = 0x04,
  1277. + SFP_OPTIONS_HIGH_POWER_LEVEL = BIT(13),
  1278. + SFP_OPTIONS_PAGING_A2 = BIT(12),
  1279. + SFP_OPTIONS_RETIMER = BIT(11),
  1280. + SFP_OPTIONS_COOLED_XCVR = BIT(10),
  1281. + SFP_OPTIONS_POWER_DECL = BIT(9),
  1282. + SFP_OPTIONS_RX_LINEAR_OUT = BIT(8),
  1283. + SFP_OPTIONS_RX_DECISION_THRESH = BIT(7),
  1284. + SFP_OPTIONS_TUNABLE_TX = BIT(6),
  1285. + SFP_OPTIONS_RATE_SELECT = BIT(5),
  1286. + SFP_OPTIONS_TX_DISABLE = BIT(4),
  1287. + SFP_OPTIONS_TX_FAULT = BIT(3),
  1288. + SFP_OPTIONS_LOS_INVERTED = BIT(2),
  1289. + SFP_OPTIONS_LOS_NORMAL = BIT(1),
  1290. + SFP_DIAGMON_DDM = BIT(6),
  1291. + SFP_DIAGMON_INT_CAL = BIT(5),
  1292. + SFP_DIAGMON_EXT_CAL = BIT(4),
  1293. + SFP_DIAGMON_RXPWR_AVG = BIT(3),
  1294. + SFP_DIAGMON_ADDRMODE = BIT(2),
  1295. + SFP_ENHOPTS_ALARMWARN = BIT(7),
  1296. + SFP_ENHOPTS_SOFT_TX_DISABLE = BIT(6),
  1297. + SFP_ENHOPTS_SOFT_TX_FAULT = BIT(5),
  1298. + SFP_ENHOPTS_SOFT_RX_LOS = BIT(4),
  1299. + SFP_ENHOPTS_SOFT_RATE_SELECT = BIT(3),
  1300. + SFP_ENHOPTS_APP_SELECT_SFF8079 = BIT(2),
  1301. + SFP_ENHOPTS_SOFT_RATE_SFF8431 = BIT(1),
  1302. + SFP_SFF8472_COMPLIANCE_NONE = 0x00,
  1303. + SFP_SFF8472_COMPLIANCE_REV9_3 = 0x01,
  1304. + SFP_SFF8472_COMPLIANCE_REV9_5 = 0x02,
  1305. + SFP_SFF8472_COMPLIANCE_REV10_2 = 0x03,
  1306. + SFP_SFF8472_COMPLIANCE_REV10_4 = 0x04,
  1307. + SFP_SFF8472_COMPLIANCE_REV11_0 = 0x05,
  1308. + SFP_SFF8472_COMPLIANCE_REV11_3 = 0x06,
  1309. + SFP_SFF8472_COMPLIANCE_REV11_4 = 0x07,
  1310. + SFP_SFF8472_COMPLIANCE_REV12_0 = 0x08,
  1311. +};
  1312. +
  1313. +/* SFP Diagnostics */
  1314. +enum {
  1315. + /* Alarm and warnings stored MSB at lower address then LSB */
  1316. + SFP_TEMP_HIGH_ALARM = 0x00,
  1317. + SFP_TEMP_LOW_ALARM = 0x02,
  1318. + SFP_TEMP_HIGH_WARN = 0x04,
  1319. + SFP_TEMP_LOW_WARN = 0x06,
  1320. + SFP_VOLT_HIGH_ALARM = 0x08,
  1321. + SFP_VOLT_LOW_ALARM = 0x0a,
  1322. + SFP_VOLT_HIGH_WARN = 0x0c,
  1323. + SFP_VOLT_LOW_WARN = 0x0e,
  1324. + SFP_BIAS_HIGH_ALARM = 0x10,
  1325. + SFP_BIAS_LOW_ALARM = 0x12,
  1326. + SFP_BIAS_HIGH_WARN = 0x14,
  1327. + SFP_BIAS_LOW_WARN = 0x16,
  1328. + SFP_TXPWR_HIGH_ALARM = 0x18,
  1329. + SFP_TXPWR_LOW_ALARM = 0x1a,
  1330. + SFP_TXPWR_HIGH_WARN = 0x1c,
  1331. + SFP_TXPWR_LOW_WARN = 0x1e,
  1332. + SFP_RXPWR_HIGH_ALARM = 0x20,
  1333. + SFP_RXPWR_LOW_ALARM = 0x22,
  1334. + SFP_RXPWR_HIGH_WARN = 0x24,
  1335. + SFP_RXPWR_LOW_WARN = 0x26,
  1336. + SFP_LASER_TEMP_HIGH_ALARM = 0x28,
  1337. + SFP_LASER_TEMP_LOW_ALARM = 0x2a,
  1338. + SFP_LASER_TEMP_HIGH_WARN = 0x2c,
  1339. + SFP_LASER_TEMP_LOW_WARN = 0x2e,
  1340. + SFP_TEC_CUR_HIGH_ALARM = 0x30,
  1341. + SFP_TEC_CUR_LOW_ALARM = 0x32,
  1342. + SFP_TEC_CUR_HIGH_WARN = 0x34,
  1343. + SFP_TEC_CUR_LOW_WARN = 0x36,
  1344. + SFP_CAL_RXPWR4 = 0x38,
  1345. + SFP_CAL_RXPWR3 = 0x3c,
  1346. + SFP_CAL_RXPWR2 = 0x40,
  1347. + SFP_CAL_RXPWR1 = 0x44,
  1348. + SFP_CAL_RXPWR0 = 0x48,
  1349. + SFP_CAL_TXI_SLOPE = 0x4c,
  1350. + SFP_CAL_TXI_OFFSET = 0x4e,
  1351. + SFP_CAL_TXPWR_SLOPE = 0x50,
  1352. + SFP_CAL_TXPWR_OFFSET = 0x52,
  1353. + SFP_CAL_T_SLOPE = 0x54,
  1354. + SFP_CAL_T_OFFSET = 0x56,
  1355. + SFP_CAL_V_SLOPE = 0x58,
  1356. + SFP_CAL_V_OFFSET = 0x5a,
  1357. + SFP_CHKSUM = 0x5f,
  1358. +
  1359. + SFP_TEMP = 0x60,
  1360. + SFP_VCC = 0x62,
  1361. + SFP_TX_BIAS = 0x64,
  1362. + SFP_TX_POWER = 0x66,
  1363. + SFP_RX_POWER = 0x68,
  1364. + SFP_LASER_TEMP = 0x6a,
  1365. + SFP_TEC_CUR = 0x6c,
  1366. +
  1367. + SFP_STATUS = 0x6e,
  1368. + SFP_ALARM = 0x70,
  1369. +
  1370. + SFP_EXT_STATUS = 0x76,
  1371. + SFP_VSL = 0x78,
  1372. + SFP_PAGE = 0x7f,
  1373. +};
  1374. +
  1375. +#endif