sfp.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843
  1. #include <linux/ctype.h>
  2. #include <linux/delay.h>
  3. #include <linux/gpio/consumer.h>
  4. #include <linux/hwmon.h>
  5. #include <linux/i2c.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/jiffies.h>
  8. #include <linux/module.h>
  9. #include <linux/mutex.h>
  10. #include <linux/of.h>
  11. #include <linux/phy.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/rtnetlink.h>
  14. #include <linux/slab.h>
  15. #include <linux/workqueue.h>
  16. #include "mdio-i2c.h"
  17. #include "sfp.h"
  18. #include "swphy.h"
  19. enum {
  20. GPIO_MODDEF0,
  21. GPIO_LOS,
  22. GPIO_TX_FAULT,
  23. GPIO_TX_DISABLE,
  24. GPIO_RATE_SELECT,
  25. GPIO_MAX,
  26. SFP_F_PRESENT = BIT(GPIO_MODDEF0),
  27. SFP_F_LOS = BIT(GPIO_LOS),
  28. SFP_F_TX_FAULT = BIT(GPIO_TX_FAULT),
  29. SFP_F_TX_DISABLE = BIT(GPIO_TX_DISABLE),
  30. SFP_F_RATE_SELECT = BIT(GPIO_RATE_SELECT),
  31. SFP_E_INSERT = 0,
  32. SFP_E_REMOVE,
  33. SFP_E_DEV_DOWN,
  34. SFP_E_DEV_UP,
  35. SFP_E_TX_FAULT,
  36. SFP_E_TX_CLEAR,
  37. SFP_E_LOS_HIGH,
  38. SFP_E_LOS_LOW,
  39. SFP_E_TIMEOUT,
  40. SFP_MOD_EMPTY = 0,
  41. SFP_MOD_PROBE,
  42. SFP_MOD_HPOWER,
  43. SFP_MOD_PRESENT,
  44. SFP_MOD_ERROR,
  45. SFP_DEV_DOWN = 0,
  46. SFP_DEV_UP,
  47. SFP_S_DOWN = 0,
  48. SFP_S_INIT,
  49. SFP_S_WAIT_LOS,
  50. SFP_S_LINK_UP,
  51. SFP_S_TX_FAULT,
  52. SFP_S_REINIT,
  53. SFP_S_TX_DISABLE,
  54. };
  55. static const char *gpio_of_names[] = {
  56. "mod-def0",
  57. "los",
  58. "tx-fault",
  59. "tx-disable",
  60. "rate-select0",
  61. };
  62. static const enum gpiod_flags gpio_flags[] = {
  63. GPIOD_IN,
  64. GPIOD_IN,
  65. GPIOD_IN,
  66. GPIOD_ASIS,
  67. GPIOD_ASIS,
  68. };
  69. #define T_INIT_JIFFIES msecs_to_jiffies(300)
  70. #define T_RESET_US 10
  71. #define T_FAULT_RECOVER msecs_to_jiffies(1000)
  72. /* SFP module presence detection is poor: the three MOD DEF signals are
  73. * the same length on the PCB, which means it's possible for MOD DEF 0 to
  74. * connect before the I2C bus on MOD DEF 1/2.
  75. *
  76. * The SFP MSA specifies 300ms as t_init (the time taken for TX_FAULT to
  77. * be deasserted) but makes no mention of the earliest time before we can
  78. * access the I2C EEPROM. However, Avago modules require 300ms.
  79. */
  80. #define T_PROBE_INIT msecs_to_jiffies(300)
  81. #define T_HPOWER_LEVEL msecs_to_jiffies(300)
  82. #define T_PROBE_RETRY msecs_to_jiffies(100)
  83. /* SFP modules appear to always have their PHY configured for bus address
  84. * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
  85. */
  86. #define SFP_PHY_ADDR 22
  87. /* Give this long for the PHY to reset. */
  88. #define T_PHY_RESET_MS 50
  89. static DEFINE_MUTEX(sfp_mutex);
  90. struct sff_data {
  91. unsigned int gpios;
  92. bool (*module_supported)(const struct sfp_eeprom_id *id);
  93. };
  94. struct sfp {
  95. struct device *dev;
  96. struct i2c_adapter *i2c;
  97. struct mii_bus *i2c_mii;
  98. struct sfp_bus *sfp_bus;
  99. struct phy_device *mod_phy;
  100. const struct sff_data *type;
  101. u32 max_power_mW;
  102. unsigned int (*get_state)(struct sfp *);
  103. void (*set_state)(struct sfp *, unsigned int);
  104. int (*read)(struct sfp *, bool, u8, void *, size_t);
  105. int (*write)(struct sfp *, bool, u8, void *, size_t);
  106. struct gpio_desc *gpio[GPIO_MAX];
  107. unsigned int state;
  108. struct delayed_work poll;
  109. struct delayed_work timeout;
  110. struct mutex sm_mutex;
  111. unsigned char sm_mod_state;
  112. unsigned char sm_dev_state;
  113. unsigned short sm_state;
  114. unsigned int sm_retries;
  115. struct sfp_eeprom_id id;
  116. #if IS_ENABLED(CONFIG_HWMON)
  117. struct sfp_diag diag;
  118. struct device *hwmon_dev;
  119. char *hwmon_name;
  120. #endif
  121. };
  122. static bool sff_module_supported(const struct sfp_eeprom_id *id)
  123. {
  124. return id->base.phys_id == SFP_PHYS_ID_SFF &&
  125. id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
  126. }
  127. static const struct sff_data sff_data = {
  128. .gpios = SFP_F_LOS | SFP_F_TX_FAULT | SFP_F_TX_DISABLE,
  129. .module_supported = sff_module_supported,
  130. };
  131. static bool sfp_module_supported(const struct sfp_eeprom_id *id)
  132. {
  133. return id->base.phys_id == SFP_PHYS_ID_SFP &&
  134. id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
  135. }
  136. static const struct sff_data sfp_data = {
  137. .gpios = SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT |
  138. SFP_F_TX_DISABLE | SFP_F_RATE_SELECT,
  139. .module_supported = sfp_module_supported,
  140. };
  141. static const struct of_device_id sfp_of_match[] = {
  142. { .compatible = "sff,sff", .data = &sff_data, },
  143. { .compatible = "sff,sfp", .data = &sfp_data, },
  144. { },
  145. };
  146. MODULE_DEVICE_TABLE(of, sfp_of_match);
  147. static unsigned long poll_jiffies;
  148. static unsigned int sfp_gpio_get_state(struct sfp *sfp)
  149. {
  150. unsigned int i, state, v;
  151. for (i = state = 0; i < GPIO_MAX; i++) {
  152. if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
  153. continue;
  154. v = gpiod_get_value_cansleep(sfp->gpio[i]);
  155. if (v)
  156. state |= BIT(i);
  157. }
  158. return state;
  159. }
  160. static unsigned int sff_gpio_get_state(struct sfp *sfp)
  161. {
  162. return sfp_gpio_get_state(sfp) | SFP_F_PRESENT;
  163. }
  164. static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state)
  165. {
  166. if (state & SFP_F_PRESENT) {
  167. /* If the module is present, drive the signals */
  168. if (sfp->gpio[GPIO_TX_DISABLE])
  169. gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE],
  170. state & SFP_F_TX_DISABLE);
  171. if (state & SFP_F_RATE_SELECT)
  172. gpiod_direction_output(sfp->gpio[GPIO_RATE_SELECT],
  173. state & SFP_F_RATE_SELECT);
  174. } else {
  175. /* Otherwise, let them float to the pull-ups */
  176. if (sfp->gpio[GPIO_TX_DISABLE])
  177. gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]);
  178. if (state & SFP_F_RATE_SELECT)
  179. gpiod_direction_input(sfp->gpio[GPIO_RATE_SELECT]);
  180. }
  181. }
  182. static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
  183. size_t len)
  184. {
  185. struct i2c_msg msgs[2];
  186. u8 bus_addr = a2 ? 0x51 : 0x50;
  187. int ret;
  188. msgs[0].addr = bus_addr;
  189. msgs[0].flags = 0;
  190. msgs[0].len = 1;
  191. msgs[0].buf = &dev_addr;
  192. msgs[1].addr = bus_addr;
  193. msgs[1].flags = I2C_M_RD;
  194. msgs[1].len = len;
  195. msgs[1].buf = buf;
  196. ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
  197. if (ret < 0)
  198. return ret;
  199. return ret == ARRAY_SIZE(msgs) ? len : 0;
  200. }
  201. static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
  202. size_t len)
  203. {
  204. struct i2c_msg msgs[1];
  205. u8 bus_addr = a2 ? 0x51 : 0x50;
  206. int ret;
  207. msgs[0].addr = bus_addr;
  208. msgs[0].flags = 0;
  209. msgs[0].len = 1 + len;
  210. msgs[0].buf = kmalloc(1 + len, GFP_KERNEL);
  211. if (!msgs[0].buf)
  212. return -ENOMEM;
  213. msgs[0].buf[0] = dev_addr;
  214. memcpy(&msgs[0].buf[1], buf, len);
  215. ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
  216. kfree(msgs[0].buf);
  217. if (ret < 0)
  218. return ret;
  219. return ret == ARRAY_SIZE(msgs) ? len : 0;
  220. }
  221. static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
  222. {
  223. struct mii_bus *i2c_mii;
  224. int ret;
  225. if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
  226. return -EINVAL;
  227. sfp->i2c = i2c;
  228. sfp->read = sfp_i2c_read;
  229. sfp->write = sfp_i2c_write;
  230. i2c_mii = mdio_i2c_alloc(sfp->dev, i2c);
  231. if (IS_ERR(i2c_mii))
  232. return PTR_ERR(i2c_mii);
  233. i2c_mii->name = "SFP I2C Bus";
  234. i2c_mii->phy_mask = ~0;
  235. ret = mdiobus_register(i2c_mii);
  236. if (ret < 0) {
  237. mdiobus_free(i2c_mii);
  238. return ret;
  239. }
  240. sfp->i2c_mii = i2c_mii;
  241. return 0;
  242. }
  243. /* Interface */
  244. static unsigned int sfp_get_state(struct sfp *sfp)
  245. {
  246. return sfp->get_state(sfp);
  247. }
  248. static void sfp_set_state(struct sfp *sfp, unsigned int state)
  249. {
  250. sfp->set_state(sfp, state);
  251. }
  252. static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
  253. {
  254. return sfp->read(sfp, a2, addr, buf, len);
  255. }
  256. static int sfp_write(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
  257. {
  258. return sfp->write(sfp, a2, addr, buf, len);
  259. }
  260. static unsigned int sfp_check(void *buf, size_t len)
  261. {
  262. u8 *p, check;
  263. for (p = buf, check = 0; len; p++, len--)
  264. check += *p;
  265. return check;
  266. }
  267. /* hwmon */
  268. #if IS_ENABLED(CONFIG_HWMON)
  269. static umode_t sfp_hwmon_is_visible(const void *data,
  270. enum hwmon_sensor_types type,
  271. u32 attr, int channel)
  272. {
  273. const struct sfp *sfp = data;
  274. switch (type) {
  275. case hwmon_temp:
  276. switch (attr) {
  277. case hwmon_temp_input:
  278. case hwmon_temp_min_alarm:
  279. case hwmon_temp_max_alarm:
  280. case hwmon_temp_lcrit_alarm:
  281. case hwmon_temp_crit_alarm:
  282. case hwmon_temp_min:
  283. case hwmon_temp_max:
  284. case hwmon_temp_lcrit:
  285. case hwmon_temp_crit:
  286. return 0444;
  287. default:
  288. return 0;
  289. }
  290. case hwmon_in:
  291. switch (attr) {
  292. case hwmon_in_input:
  293. case hwmon_in_min_alarm:
  294. case hwmon_in_max_alarm:
  295. case hwmon_in_lcrit_alarm:
  296. case hwmon_in_crit_alarm:
  297. case hwmon_in_min:
  298. case hwmon_in_max:
  299. case hwmon_in_lcrit:
  300. case hwmon_in_crit:
  301. return 0444;
  302. default:
  303. return 0;
  304. }
  305. case hwmon_curr:
  306. switch (attr) {
  307. case hwmon_curr_input:
  308. case hwmon_curr_min_alarm:
  309. case hwmon_curr_max_alarm:
  310. case hwmon_curr_lcrit_alarm:
  311. case hwmon_curr_crit_alarm:
  312. case hwmon_curr_min:
  313. case hwmon_curr_max:
  314. case hwmon_curr_lcrit:
  315. case hwmon_curr_crit:
  316. return 0444;
  317. default:
  318. return 0;
  319. }
  320. case hwmon_power:
  321. /* External calibration of receive power requires
  322. * floating point arithmetic. Doing that in the kernel
  323. * is not easy, so just skip it. If the module does
  324. * not require external calibration, we can however
  325. * show receiver power, since FP is then not needed.
  326. */
  327. if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL &&
  328. channel == 1)
  329. return 0;
  330. switch (attr) {
  331. case hwmon_power_input:
  332. case hwmon_power_min_alarm:
  333. case hwmon_power_max_alarm:
  334. case hwmon_power_lcrit_alarm:
  335. case hwmon_power_crit_alarm:
  336. case hwmon_power_min:
  337. case hwmon_power_max:
  338. case hwmon_power_lcrit:
  339. case hwmon_power_crit:
  340. return 0444;
  341. default:
  342. return 0;
  343. }
  344. default:
  345. return 0;
  346. }
  347. }
  348. static int sfp_hwmon_read_sensor(struct sfp *sfp, int reg, long *value)
  349. {
  350. __be16 val;
  351. int err;
  352. err = sfp_read(sfp, true, reg, &val, sizeof(val));
  353. if (err < 0)
  354. return err;
  355. *value = be16_to_cpu(val);
  356. return 0;
  357. }
  358. static void sfp_hwmon_to_rx_power(long *value)
  359. {
  360. *value = DIV_ROUND_CLOSEST(*value, 100);
  361. }
  362. static void sfp_hwmon_calibrate(struct sfp *sfp, unsigned int slope, int offset,
  363. long *value)
  364. {
  365. if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL)
  366. *value = DIV_ROUND_CLOSEST(*value * slope, 256) + offset;
  367. }
  368. static void sfp_hwmon_calibrate_temp(struct sfp *sfp, long *value)
  369. {
  370. sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_t_slope),
  371. be16_to_cpu(sfp->diag.cal_t_offset), value);
  372. if (*value >= 0x8000)
  373. *value -= 0x10000;
  374. *value = DIV_ROUND_CLOSEST(*value * 1000, 256);
  375. }
  376. static void sfp_hwmon_calibrate_vcc(struct sfp *sfp, long *value)
  377. {
  378. sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_v_slope),
  379. be16_to_cpu(sfp->diag.cal_v_offset), value);
  380. *value = DIV_ROUND_CLOSEST(*value, 10);
  381. }
  382. static void sfp_hwmon_calibrate_bias(struct sfp *sfp, long *value)
  383. {
  384. sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txi_slope),
  385. be16_to_cpu(sfp->diag.cal_txi_offset), value);
  386. *value = DIV_ROUND_CLOSEST(*value, 500);
  387. }
  388. static void sfp_hwmon_calibrate_tx_power(struct sfp *sfp, long *value)
  389. {
  390. sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txpwr_slope),
  391. be16_to_cpu(sfp->diag.cal_txpwr_offset), value);
  392. *value = DIV_ROUND_CLOSEST(*value, 10);
  393. }
  394. static int sfp_hwmon_read_temp(struct sfp *sfp, int reg, long *value)
  395. {
  396. int err;
  397. err = sfp_hwmon_read_sensor(sfp, reg, value);
  398. if (err < 0)
  399. return err;
  400. sfp_hwmon_calibrate_temp(sfp, value);
  401. return 0;
  402. }
  403. static int sfp_hwmon_read_vcc(struct sfp *sfp, int reg, long *value)
  404. {
  405. int err;
  406. err = sfp_hwmon_read_sensor(sfp, reg, value);
  407. if (err < 0)
  408. return err;
  409. sfp_hwmon_calibrate_vcc(sfp, value);
  410. return 0;
  411. }
  412. static int sfp_hwmon_read_bias(struct sfp *sfp, int reg, long *value)
  413. {
  414. int err;
  415. err = sfp_hwmon_read_sensor(sfp, reg, value);
  416. if (err < 0)
  417. return err;
  418. sfp_hwmon_calibrate_bias(sfp, value);
  419. return 0;
  420. }
  421. static int sfp_hwmon_read_tx_power(struct sfp *sfp, int reg, long *value)
  422. {
  423. int err;
  424. err = sfp_hwmon_read_sensor(sfp, reg, value);
  425. if (err < 0)
  426. return err;
  427. sfp_hwmon_calibrate_tx_power(sfp, value);
  428. return 0;
  429. }
  430. static int sfp_hwmon_read_rx_power(struct sfp *sfp, int reg, long *value)
  431. {
  432. int err;
  433. err = sfp_hwmon_read_sensor(sfp, reg, value);
  434. if (err < 0)
  435. return err;
  436. sfp_hwmon_to_rx_power(value);
  437. return 0;
  438. }
  439. static int sfp_hwmon_temp(struct sfp *sfp, u32 attr, long *value)
  440. {
  441. u8 status;
  442. int err;
  443. switch (attr) {
  444. case hwmon_temp_input:
  445. return sfp_hwmon_read_temp(sfp, SFP_TEMP, value);
  446. case hwmon_temp_lcrit:
  447. *value = be16_to_cpu(sfp->diag.temp_low_alarm);
  448. sfp_hwmon_calibrate_temp(sfp, value);
  449. return 0;
  450. case hwmon_temp_min:
  451. *value = be16_to_cpu(sfp->diag.temp_low_warn);
  452. sfp_hwmon_calibrate_temp(sfp, value);
  453. return 0;
  454. case hwmon_temp_max:
  455. *value = be16_to_cpu(sfp->diag.temp_high_warn);
  456. sfp_hwmon_calibrate_temp(sfp, value);
  457. return 0;
  458. case hwmon_temp_crit:
  459. *value = be16_to_cpu(sfp->diag.temp_high_alarm);
  460. sfp_hwmon_calibrate_temp(sfp, value);
  461. return 0;
  462. case hwmon_temp_lcrit_alarm:
  463. err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
  464. if (err < 0)
  465. return err;
  466. *value = !!(status & SFP_ALARM0_TEMP_LOW);
  467. return 0;
  468. case hwmon_temp_min_alarm:
  469. err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
  470. if (err < 0)
  471. return err;
  472. *value = !!(status & SFP_WARN0_TEMP_LOW);
  473. return 0;
  474. case hwmon_temp_max_alarm:
  475. err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
  476. if (err < 0)
  477. return err;
  478. *value = !!(status & SFP_WARN0_TEMP_HIGH);
  479. return 0;
  480. case hwmon_temp_crit_alarm:
  481. err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
  482. if (err < 0)
  483. return err;
  484. *value = !!(status & SFP_ALARM0_TEMP_HIGH);
  485. return 0;
  486. default:
  487. return -EOPNOTSUPP;
  488. }
  489. return -EOPNOTSUPP;
  490. }
  491. static int sfp_hwmon_vcc(struct sfp *sfp, u32 attr, long *value)
  492. {
  493. u8 status;
  494. int err;
  495. switch (attr) {
  496. case hwmon_in_input:
  497. return sfp_hwmon_read_vcc(sfp, SFP_VCC, value);
  498. case hwmon_in_lcrit:
  499. *value = be16_to_cpu(sfp->diag.volt_low_alarm);
  500. sfp_hwmon_calibrate_vcc(sfp, value);
  501. return 0;
  502. case hwmon_in_min:
  503. *value = be16_to_cpu(sfp->diag.volt_low_warn);
  504. sfp_hwmon_calibrate_vcc(sfp, value);
  505. return 0;
  506. case hwmon_in_max:
  507. *value = be16_to_cpu(sfp->diag.volt_high_warn);
  508. sfp_hwmon_calibrate_vcc(sfp, value);
  509. return 0;
  510. case hwmon_in_crit:
  511. *value = be16_to_cpu(sfp->diag.volt_high_alarm);
  512. sfp_hwmon_calibrate_vcc(sfp, value);
  513. return 0;
  514. case hwmon_in_lcrit_alarm:
  515. err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
  516. if (err < 0)
  517. return err;
  518. *value = !!(status & SFP_ALARM0_VCC_LOW);
  519. return 0;
  520. case hwmon_in_min_alarm:
  521. err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
  522. if (err < 0)
  523. return err;
  524. *value = !!(status & SFP_WARN0_VCC_LOW);
  525. return 0;
  526. case hwmon_in_max_alarm:
  527. err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
  528. if (err < 0)
  529. return err;
  530. *value = !!(status & SFP_WARN0_VCC_HIGH);
  531. return 0;
  532. case hwmon_in_crit_alarm:
  533. err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
  534. if (err < 0)
  535. return err;
  536. *value = !!(status & SFP_ALARM0_VCC_HIGH);
  537. return 0;
  538. default:
  539. return -EOPNOTSUPP;
  540. }
  541. return -EOPNOTSUPP;
  542. }
  543. static int sfp_hwmon_bias(struct sfp *sfp, u32 attr, long *value)
  544. {
  545. u8 status;
  546. int err;
  547. switch (attr) {
  548. case hwmon_curr_input:
  549. return sfp_hwmon_read_bias(sfp, SFP_TX_BIAS, value);
  550. case hwmon_curr_lcrit:
  551. *value = be16_to_cpu(sfp->diag.bias_low_alarm);
  552. sfp_hwmon_calibrate_bias(sfp, value);
  553. return 0;
  554. case hwmon_curr_min:
  555. *value = be16_to_cpu(sfp->diag.bias_low_warn);
  556. sfp_hwmon_calibrate_bias(sfp, value);
  557. return 0;
  558. case hwmon_curr_max:
  559. *value = be16_to_cpu(sfp->diag.bias_high_warn);
  560. sfp_hwmon_calibrate_bias(sfp, value);
  561. return 0;
  562. case hwmon_curr_crit:
  563. *value = be16_to_cpu(sfp->diag.bias_high_alarm);
  564. sfp_hwmon_calibrate_bias(sfp, value);
  565. return 0;
  566. case hwmon_curr_lcrit_alarm:
  567. err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
  568. if (err < 0)
  569. return err;
  570. *value = !!(status & SFP_ALARM0_TX_BIAS_LOW);
  571. return 0;
  572. case hwmon_curr_min_alarm:
  573. err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
  574. if (err < 0)
  575. return err;
  576. *value = !!(status & SFP_WARN0_TX_BIAS_LOW);
  577. return 0;
  578. case hwmon_curr_max_alarm:
  579. err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
  580. if (err < 0)
  581. return err;
  582. *value = !!(status & SFP_WARN0_TX_BIAS_HIGH);
  583. return 0;
  584. case hwmon_curr_crit_alarm:
  585. err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
  586. if (err < 0)
  587. return err;
  588. *value = !!(status & SFP_ALARM0_TX_BIAS_HIGH);
  589. return 0;
  590. default:
  591. return -EOPNOTSUPP;
  592. }
  593. return -EOPNOTSUPP;
  594. }
  595. static int sfp_hwmon_tx_power(struct sfp *sfp, u32 attr, long *value)
  596. {
  597. u8 status;
  598. int err;
  599. switch (attr) {
  600. case hwmon_power_input:
  601. return sfp_hwmon_read_tx_power(sfp, SFP_TX_POWER, value);
  602. case hwmon_power_lcrit:
  603. *value = be16_to_cpu(sfp->diag.txpwr_low_alarm);
  604. sfp_hwmon_calibrate_tx_power(sfp, value);
  605. return 0;
  606. case hwmon_power_min:
  607. *value = be16_to_cpu(sfp->diag.txpwr_low_warn);
  608. sfp_hwmon_calibrate_tx_power(sfp, value);
  609. return 0;
  610. case hwmon_power_max:
  611. *value = be16_to_cpu(sfp->diag.txpwr_high_warn);
  612. sfp_hwmon_calibrate_tx_power(sfp, value);
  613. return 0;
  614. case hwmon_power_crit:
  615. *value = be16_to_cpu(sfp->diag.txpwr_high_alarm);
  616. sfp_hwmon_calibrate_tx_power(sfp, value);
  617. return 0;
  618. case hwmon_power_lcrit_alarm:
  619. err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
  620. if (err < 0)
  621. return err;
  622. *value = !!(status & SFP_ALARM0_TXPWR_LOW);
  623. return 0;
  624. case hwmon_power_min_alarm:
  625. err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
  626. if (err < 0)
  627. return err;
  628. *value = !!(status & SFP_WARN0_TXPWR_LOW);
  629. return 0;
  630. case hwmon_power_max_alarm:
  631. err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
  632. if (err < 0)
  633. return err;
  634. *value = !!(status & SFP_WARN0_TXPWR_HIGH);
  635. return 0;
  636. case hwmon_power_crit_alarm:
  637. err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
  638. if (err < 0)
  639. return err;
  640. *value = !!(status & SFP_ALARM0_TXPWR_HIGH);
  641. return 0;
  642. default:
  643. return -EOPNOTSUPP;
  644. }
  645. return -EOPNOTSUPP;
  646. }
  647. static int sfp_hwmon_rx_power(struct sfp *sfp, u32 attr, long *value)
  648. {
  649. u8 status;
  650. int err;
  651. switch (attr) {
  652. case hwmon_power_input:
  653. return sfp_hwmon_read_rx_power(sfp, SFP_RX_POWER, value);
  654. case hwmon_power_lcrit:
  655. *value = be16_to_cpu(sfp->diag.rxpwr_low_alarm);
  656. sfp_hwmon_to_rx_power(value);
  657. return 0;
  658. case hwmon_power_min:
  659. *value = be16_to_cpu(sfp->diag.rxpwr_low_warn);
  660. sfp_hwmon_to_rx_power(value);
  661. return 0;
  662. case hwmon_power_max:
  663. *value = be16_to_cpu(sfp->diag.rxpwr_high_warn);
  664. sfp_hwmon_to_rx_power(value);
  665. return 0;
  666. case hwmon_power_crit:
  667. *value = be16_to_cpu(sfp->diag.rxpwr_high_alarm);
  668. sfp_hwmon_to_rx_power(value);
  669. return 0;
  670. case hwmon_power_lcrit_alarm:
  671. err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
  672. if (err < 0)
  673. return err;
  674. *value = !!(status & SFP_ALARM1_RXPWR_LOW);
  675. return 0;
  676. case hwmon_power_min_alarm:
  677. err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
  678. if (err < 0)
  679. return err;
  680. *value = !!(status & SFP_WARN1_RXPWR_LOW);
  681. return 0;
  682. case hwmon_power_max_alarm:
  683. err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
  684. if (err < 0)
  685. return err;
  686. *value = !!(status & SFP_WARN1_RXPWR_HIGH);
  687. return 0;
  688. case hwmon_power_crit_alarm:
  689. err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
  690. if (err < 0)
  691. return err;
  692. *value = !!(status & SFP_ALARM1_RXPWR_HIGH);
  693. return 0;
  694. default:
  695. return -EOPNOTSUPP;
  696. }
  697. return -EOPNOTSUPP;
  698. }
  699. static int sfp_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
  700. u32 attr, int channel, long *value)
  701. {
  702. struct sfp *sfp = dev_get_drvdata(dev);
  703. switch (type) {
  704. case hwmon_temp:
  705. return sfp_hwmon_temp(sfp, attr, value);
  706. case hwmon_in:
  707. return sfp_hwmon_vcc(sfp, attr, value);
  708. case hwmon_curr:
  709. return sfp_hwmon_bias(sfp, attr, value);
  710. case hwmon_power:
  711. switch (channel) {
  712. case 0:
  713. return sfp_hwmon_tx_power(sfp, attr, value);
  714. case 1:
  715. return sfp_hwmon_rx_power(sfp, attr, value);
  716. default:
  717. return -EOPNOTSUPP;
  718. }
  719. default:
  720. return -EOPNOTSUPP;
  721. }
  722. }
  723. static const struct hwmon_ops sfp_hwmon_ops = {
  724. .is_visible = sfp_hwmon_is_visible,
  725. .read = sfp_hwmon_read,
  726. };
  727. static u32 sfp_hwmon_chip_config[] = {
  728. HWMON_C_REGISTER_TZ,
  729. 0,
  730. };
  731. static const struct hwmon_channel_info sfp_hwmon_chip = {
  732. .type = hwmon_chip,
  733. .config = sfp_hwmon_chip_config,
  734. };
  735. static u32 sfp_hwmon_temp_config[] = {
  736. HWMON_T_INPUT |
  737. HWMON_T_MAX | HWMON_T_MIN |
  738. HWMON_T_MAX_ALARM | HWMON_T_MIN_ALARM |
  739. HWMON_T_CRIT | HWMON_T_LCRIT |
  740. HWMON_T_CRIT_ALARM | HWMON_T_LCRIT_ALARM,
  741. 0,
  742. };
  743. static const struct hwmon_channel_info sfp_hwmon_temp_channel_info = {
  744. .type = hwmon_temp,
  745. .config = sfp_hwmon_temp_config,
  746. };
  747. static u32 sfp_hwmon_vcc_config[] = {
  748. HWMON_I_INPUT |
  749. HWMON_I_MAX | HWMON_I_MIN |
  750. HWMON_I_MAX_ALARM | HWMON_I_MIN_ALARM |
  751. HWMON_I_CRIT | HWMON_I_LCRIT |
  752. HWMON_I_CRIT_ALARM | HWMON_I_LCRIT_ALARM,
  753. 0,
  754. };
  755. static const struct hwmon_channel_info sfp_hwmon_vcc_channel_info = {
  756. .type = hwmon_in,
  757. .config = sfp_hwmon_vcc_config,
  758. };
  759. static u32 sfp_hwmon_bias_config[] = {
  760. HWMON_C_INPUT |
  761. HWMON_C_MAX | HWMON_C_MIN |
  762. HWMON_C_MAX_ALARM | HWMON_C_MIN_ALARM |
  763. HWMON_C_CRIT | HWMON_C_LCRIT |
  764. HWMON_C_CRIT_ALARM | HWMON_C_LCRIT_ALARM,
  765. 0,
  766. };
  767. static const struct hwmon_channel_info sfp_hwmon_bias_channel_info = {
  768. .type = hwmon_curr,
  769. .config = sfp_hwmon_bias_config,
  770. };
  771. static u32 sfp_hwmon_power_config[] = {
  772. /* Transmit power */
  773. HWMON_P_INPUT |
  774. HWMON_P_MAX | HWMON_P_MIN |
  775. HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
  776. HWMON_P_CRIT | HWMON_P_LCRIT |
  777. HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM,
  778. /* Receive power */
  779. HWMON_P_INPUT |
  780. HWMON_P_MAX | HWMON_P_MIN |
  781. HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
  782. HWMON_P_CRIT | HWMON_P_LCRIT |
  783. HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM,
  784. 0,
  785. };
  786. static const struct hwmon_channel_info sfp_hwmon_power_channel_info = {
  787. .type = hwmon_power,
  788. .config = sfp_hwmon_power_config,
  789. };
  790. static const struct hwmon_channel_info *sfp_hwmon_info[] = {
  791. &sfp_hwmon_chip,
  792. &sfp_hwmon_vcc_channel_info,
  793. &sfp_hwmon_temp_channel_info,
  794. &sfp_hwmon_bias_channel_info,
  795. &sfp_hwmon_power_channel_info,
  796. NULL,
  797. };
  798. static const struct hwmon_chip_info sfp_hwmon_chip_info = {
  799. .ops = &sfp_hwmon_ops,
  800. .info = sfp_hwmon_info,
  801. };
  802. static int sfp_hwmon_insert(struct sfp *sfp)
  803. {
  804. int err, i;
  805. if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE)
  806. return 0;
  807. if (!(sfp->id.ext.diagmon & SFP_DIAGMON_DDM))
  808. return 0;
  809. if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
  810. /* This driver in general does not support address
  811. * change.
  812. */
  813. return 0;
  814. err = sfp_read(sfp, true, 0, &sfp->diag, sizeof(sfp->diag));
  815. if (err < 0)
  816. return err;
  817. sfp->hwmon_name = kstrdup(dev_name(sfp->dev), GFP_KERNEL);
  818. if (!sfp->hwmon_name)
  819. return -ENODEV;
  820. for (i = 0; sfp->hwmon_name[i]; i++)
  821. if (hwmon_is_bad_char(sfp->hwmon_name[i]))
  822. sfp->hwmon_name[i] = '_';
  823. sfp->hwmon_dev = hwmon_device_register_with_info(sfp->dev,
  824. sfp->hwmon_name, sfp,
  825. &sfp_hwmon_chip_info,
  826. NULL);
  827. return PTR_ERR_OR_ZERO(sfp->hwmon_dev);
  828. }
  829. static void sfp_hwmon_remove(struct sfp *sfp)
  830. {
  831. hwmon_device_unregister(sfp->hwmon_dev);
  832. kfree(sfp->hwmon_name);
  833. }
  834. #else
  835. static int sfp_hwmon_insert(struct sfp *sfp)
  836. {
  837. return 0;
  838. }
  839. static void sfp_hwmon_remove(struct sfp *sfp)
  840. {
  841. }
  842. #endif
  843. /* Helpers */
  844. static void sfp_module_tx_disable(struct sfp *sfp)
  845. {
  846. dev_dbg(sfp->dev, "tx disable %u -> %u\n",
  847. sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1);
  848. sfp->state |= SFP_F_TX_DISABLE;
  849. sfp_set_state(sfp, sfp->state);
  850. }
  851. static void sfp_module_tx_enable(struct sfp *sfp)
  852. {
  853. dev_dbg(sfp->dev, "tx disable %u -> %u\n",
  854. sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0);
  855. sfp->state &= ~SFP_F_TX_DISABLE;
  856. sfp_set_state(sfp, sfp->state);
  857. }
  858. static void sfp_module_tx_fault_reset(struct sfp *sfp)
  859. {
  860. unsigned int state = sfp->state;
  861. if (state & SFP_F_TX_DISABLE)
  862. return;
  863. sfp_set_state(sfp, state | SFP_F_TX_DISABLE);
  864. udelay(T_RESET_US);
  865. sfp_set_state(sfp, state);
  866. }
  867. /* SFP state machine */
  868. static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout)
  869. {
  870. if (timeout)
  871. mod_delayed_work(system_power_efficient_wq, &sfp->timeout,
  872. timeout);
  873. else
  874. cancel_delayed_work(&sfp->timeout);
  875. }
  876. static void sfp_sm_next(struct sfp *sfp, unsigned int state,
  877. unsigned int timeout)
  878. {
  879. sfp->sm_state = state;
  880. sfp_sm_set_timer(sfp, timeout);
  881. }
  882. static void sfp_sm_ins_next(struct sfp *sfp, unsigned int state,
  883. unsigned int timeout)
  884. {
  885. sfp->sm_mod_state = state;
  886. sfp_sm_set_timer(sfp, timeout);
  887. }
  888. static void sfp_sm_phy_detach(struct sfp *sfp)
  889. {
  890. phy_stop(sfp->mod_phy);
  891. sfp_remove_phy(sfp->sfp_bus);
  892. phy_device_remove(sfp->mod_phy);
  893. phy_device_free(sfp->mod_phy);
  894. sfp->mod_phy = NULL;
  895. }
  896. static void sfp_sm_probe_phy(struct sfp *sfp)
  897. {
  898. struct phy_device *phy;
  899. int err;
  900. msleep(T_PHY_RESET_MS);
  901. phy = mdiobus_scan(sfp->i2c_mii, SFP_PHY_ADDR);
  902. if (phy == ERR_PTR(-ENODEV)) {
  903. dev_info(sfp->dev, "no PHY detected\n");
  904. return;
  905. }
  906. if (IS_ERR(phy)) {
  907. dev_err(sfp->dev, "mdiobus scan returned %ld\n", PTR_ERR(phy));
  908. return;
  909. }
  910. err = sfp_add_phy(sfp->sfp_bus, phy);
  911. if (err) {
  912. phy_device_remove(phy);
  913. phy_device_free(phy);
  914. dev_err(sfp->dev, "sfp_add_phy failed: %d\n", err);
  915. return;
  916. }
  917. sfp->mod_phy = phy;
  918. phy_start(phy);
  919. }
  920. static void sfp_sm_link_up(struct sfp *sfp)
  921. {
  922. sfp_link_up(sfp->sfp_bus);
  923. sfp_sm_next(sfp, SFP_S_LINK_UP, 0);
  924. }
  925. static void sfp_sm_link_down(struct sfp *sfp)
  926. {
  927. sfp_link_down(sfp->sfp_bus);
  928. }
  929. static void sfp_sm_link_check_los(struct sfp *sfp)
  930. {
  931. unsigned int los = sfp->state & SFP_F_LOS;
  932. /* If neither SFP_OPTIONS_LOS_INVERTED nor SFP_OPTIONS_LOS_NORMAL
  933. * are set, we assume that no LOS signal is available.
  934. */
  935. if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED))
  936. los ^= SFP_F_LOS;
  937. else if (!(sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL)))
  938. los = 0;
  939. if (los)
  940. sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
  941. else
  942. sfp_sm_link_up(sfp);
  943. }
  944. static bool sfp_los_event_active(struct sfp *sfp, unsigned int event)
  945. {
  946. return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) &&
  947. event == SFP_E_LOS_LOW) ||
  948. (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) &&
  949. event == SFP_E_LOS_HIGH);
  950. }
  951. static bool sfp_los_event_inactive(struct sfp *sfp, unsigned int event)
  952. {
  953. return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) &&
  954. event == SFP_E_LOS_HIGH) ||
  955. (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) &&
  956. event == SFP_E_LOS_LOW);
  957. }
  958. static void sfp_sm_fault(struct sfp *sfp, bool warn)
  959. {
  960. if (sfp->sm_retries && !--sfp->sm_retries) {
  961. dev_err(sfp->dev,
  962. "module persistently indicates fault, disabling\n");
  963. sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0);
  964. } else {
  965. if (warn)
  966. dev_err(sfp->dev, "module transmit fault indicated\n");
  967. sfp_sm_next(sfp, SFP_S_TX_FAULT, T_FAULT_RECOVER);
  968. }
  969. }
  970. static void sfp_sm_mod_init(struct sfp *sfp)
  971. {
  972. sfp_module_tx_enable(sfp);
  973. /* Wait t_init before indicating that the link is up, provided the
  974. * current state indicates no TX_FAULT. If TX_FAULT clears before
  975. * this time, that's fine too.
  976. */
  977. sfp_sm_next(sfp, SFP_S_INIT, T_INIT_JIFFIES);
  978. sfp->sm_retries = 5;
  979. /* Setting the serdes link mode is guesswork: there's no
  980. * field in the EEPROM which indicates what mode should
  981. * be used.
  982. *
  983. * If it's a gigabit-only fiber module, it probably does
  984. * not have a PHY, so switch to 802.3z negotiation mode.
  985. * Otherwise, switch to SGMII mode (which is required to
  986. * support non-gigabit speeds) and probe for a PHY.
  987. */
  988. if (sfp->id.base.e1000_base_t ||
  989. sfp->id.base.e100_base_lx ||
  990. sfp->id.base.e100_base_fx)
  991. sfp_sm_probe_phy(sfp);
  992. }
  993. static int sfp_sm_mod_hpower(struct sfp *sfp)
  994. {
  995. u32 power;
  996. u8 val;
  997. int err;
  998. power = 1000;
  999. if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_POWER_DECL))
  1000. power = 1500;
  1001. if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL))
  1002. power = 2000;
  1003. if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE &&
  1004. (sfp->id.ext.diagmon & (SFP_DIAGMON_DDM | SFP_DIAGMON_ADDRMODE)) !=
  1005. SFP_DIAGMON_DDM) {
  1006. /* The module appears not to implement bus address 0xa2,
  1007. * or requires an address change sequence, so assume that
  1008. * the module powers up in the indicated power mode.
  1009. */
  1010. if (power > sfp->max_power_mW) {
  1011. dev_err(sfp->dev,
  1012. "Host does not support %u.%uW modules\n",
  1013. power / 1000, (power / 100) % 10);
  1014. return -EINVAL;
  1015. }
  1016. return 0;
  1017. }
  1018. if (power > sfp->max_power_mW) {
  1019. dev_warn(sfp->dev,
  1020. "Host does not support %u.%uW modules, module left in power mode 1\n",
  1021. power / 1000, (power / 100) % 10);
  1022. return 0;
  1023. }
  1024. if (power <= 1000)
  1025. return 0;
  1026. err = sfp_read(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
  1027. if (err != sizeof(val)) {
  1028. dev_err(sfp->dev, "Failed to read EEPROM: %d\n", err);
  1029. err = -EAGAIN;
  1030. goto err;
  1031. }
  1032. val |= BIT(0);
  1033. err = sfp_write(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
  1034. if (err != sizeof(val)) {
  1035. dev_err(sfp->dev, "Failed to write EEPROM: %d\n", err);
  1036. err = -EAGAIN;
  1037. goto err;
  1038. }
  1039. dev_info(sfp->dev, "Module switched to %u.%uW power level\n",
  1040. power / 1000, (power / 100) % 10);
  1041. return T_HPOWER_LEVEL;
  1042. err:
  1043. return err;
  1044. }
  1045. static int sfp_sm_mod_probe(struct sfp *sfp)
  1046. {
  1047. /* SFP module inserted - read I2C data */
  1048. struct sfp_eeprom_id id;
  1049. bool cotsworks;
  1050. u8 check;
  1051. int ret;
  1052. ret = sfp_read(sfp, false, 0, &id, sizeof(id));
  1053. if (ret < 0) {
  1054. dev_err(sfp->dev, "failed to read EEPROM: %d\n", ret);
  1055. return -EAGAIN;
  1056. }
  1057. if (ret != sizeof(id)) {
  1058. dev_err(sfp->dev, "EEPROM short read: %d\n", ret);
  1059. return -EAGAIN;
  1060. }
  1061. /* Cotsworks do not seem to update the checksums when they
  1062. * do the final programming with the final module part number,
  1063. * serial number and date code.
  1064. */
  1065. cotsworks = !memcmp(id.base.vendor_name, "COTSWORKS ", 16);
  1066. /* Validate the checksum over the base structure */
  1067. check = sfp_check(&id.base, sizeof(id.base) - 1);
  1068. if (check != id.base.cc_base) {
  1069. if (cotsworks) {
  1070. dev_warn(sfp->dev,
  1071. "EEPROM base structure checksum failure (0x%02x != 0x%02x)\n",
  1072. check, id.base.cc_base);
  1073. } else {
  1074. dev_err(sfp->dev,
  1075. "EEPROM base structure checksum failure: 0x%02x != 0x%02x\n",
  1076. check, id.base.cc_base);
  1077. print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
  1078. 16, 1, &id, sizeof(id), true);
  1079. return -EINVAL;
  1080. }
  1081. }
  1082. check = sfp_check(&id.ext, sizeof(id.ext) - 1);
  1083. if (check != id.ext.cc_ext) {
  1084. if (cotsworks) {
  1085. dev_warn(sfp->dev,
  1086. "EEPROM extended structure checksum failure (0x%02x != 0x%02x)\n",
  1087. check, id.ext.cc_ext);
  1088. } else {
  1089. dev_err(sfp->dev,
  1090. "EEPROM extended structure checksum failure: 0x%02x != 0x%02x\n",
  1091. check, id.ext.cc_ext);
  1092. print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
  1093. 16, 1, &id, sizeof(id), true);
  1094. memset(&id.ext, 0, sizeof(id.ext));
  1095. }
  1096. }
  1097. sfp->id = id;
  1098. dev_info(sfp->dev, "module %.*s %.*s rev %.*s sn %.*s dc %.*s\n",
  1099. (int)sizeof(id.base.vendor_name), id.base.vendor_name,
  1100. (int)sizeof(id.base.vendor_pn), id.base.vendor_pn,
  1101. (int)sizeof(id.base.vendor_rev), id.base.vendor_rev,
  1102. (int)sizeof(id.ext.vendor_sn), id.ext.vendor_sn,
  1103. (int)sizeof(id.ext.datecode), id.ext.datecode);
  1104. /* Check whether we support this module */
  1105. if (!sfp->type->module_supported(&sfp->id)) {
  1106. dev_err(sfp->dev,
  1107. "module is not supported - phys id 0x%02x 0x%02x\n",
  1108. sfp->id.base.phys_id, sfp->id.base.phys_ext_id);
  1109. return -EINVAL;
  1110. }
  1111. /* If the module requires address swap mode, warn about it */
  1112. if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
  1113. dev_warn(sfp->dev,
  1114. "module address swap to access page 0xA2 is not supported.\n");
  1115. ret = sfp_hwmon_insert(sfp);
  1116. if (ret < 0)
  1117. return ret;
  1118. ret = sfp_module_insert(sfp->sfp_bus, &sfp->id);
  1119. if (ret < 0)
  1120. return ret;
  1121. return sfp_sm_mod_hpower(sfp);
  1122. }
  1123. static void sfp_sm_mod_remove(struct sfp *sfp)
  1124. {
  1125. sfp_module_remove(sfp->sfp_bus);
  1126. sfp_hwmon_remove(sfp);
  1127. if (sfp->mod_phy)
  1128. sfp_sm_phy_detach(sfp);
  1129. sfp_module_tx_disable(sfp);
  1130. memset(&sfp->id, 0, sizeof(sfp->id));
  1131. dev_info(sfp->dev, "module removed\n");
  1132. }
  1133. static void sfp_sm_event(struct sfp *sfp, unsigned int event)
  1134. {
  1135. mutex_lock(&sfp->sm_mutex);
  1136. dev_dbg(sfp->dev, "SM: enter %u:%u:%u event %u\n",
  1137. sfp->sm_mod_state, sfp->sm_dev_state, sfp->sm_state, event);
  1138. /* This state machine tracks the insert/remove state of
  1139. * the module, and handles probing the on-board EEPROM.
  1140. */
  1141. switch (sfp->sm_mod_state) {
  1142. default:
  1143. if (event == SFP_E_INSERT) {
  1144. sfp_module_tx_disable(sfp);
  1145. sfp_sm_ins_next(sfp, SFP_MOD_PROBE, T_PROBE_INIT);
  1146. }
  1147. break;
  1148. case SFP_MOD_PROBE:
  1149. if (event == SFP_E_REMOVE) {
  1150. sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
  1151. } else if (event == SFP_E_TIMEOUT) {
  1152. int val = sfp_sm_mod_probe(sfp);
  1153. if (val == 0)
  1154. sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
  1155. else if (val > 0)
  1156. sfp_sm_ins_next(sfp, SFP_MOD_HPOWER, val);
  1157. else if (val != -EAGAIN)
  1158. sfp_sm_ins_next(sfp, SFP_MOD_ERROR, 0);
  1159. else
  1160. sfp_sm_set_timer(sfp, T_PROBE_RETRY);
  1161. }
  1162. break;
  1163. case SFP_MOD_HPOWER:
  1164. if (event == SFP_E_TIMEOUT) {
  1165. sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
  1166. break;
  1167. }
  1168. /* fallthrough */
  1169. case SFP_MOD_PRESENT:
  1170. case SFP_MOD_ERROR:
  1171. if (event == SFP_E_REMOVE) {
  1172. sfp_sm_mod_remove(sfp);
  1173. sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
  1174. }
  1175. break;
  1176. }
  1177. /* This state machine tracks the netdev up/down state */
  1178. switch (sfp->sm_dev_state) {
  1179. default:
  1180. if (event == SFP_E_DEV_UP)
  1181. sfp->sm_dev_state = SFP_DEV_UP;
  1182. break;
  1183. case SFP_DEV_UP:
  1184. if (event == SFP_E_DEV_DOWN) {
  1185. /* If the module has a PHY, avoid raising TX disable
  1186. * as this resets the PHY. Otherwise, raise it to
  1187. * turn the laser off.
  1188. */
  1189. if (!sfp->mod_phy)
  1190. sfp_module_tx_disable(sfp);
  1191. sfp->sm_dev_state = SFP_DEV_DOWN;
  1192. }
  1193. break;
  1194. }
  1195. /* Some events are global */
  1196. if (sfp->sm_state != SFP_S_DOWN &&
  1197. (sfp->sm_mod_state != SFP_MOD_PRESENT ||
  1198. sfp->sm_dev_state != SFP_DEV_UP)) {
  1199. if (sfp->sm_state == SFP_S_LINK_UP &&
  1200. sfp->sm_dev_state == SFP_DEV_UP)
  1201. sfp_sm_link_down(sfp);
  1202. if (sfp->mod_phy)
  1203. sfp_sm_phy_detach(sfp);
  1204. sfp_sm_next(sfp, SFP_S_DOWN, 0);
  1205. mutex_unlock(&sfp->sm_mutex);
  1206. return;
  1207. }
  1208. /* The main state machine */
  1209. switch (sfp->sm_state) {
  1210. case SFP_S_DOWN:
  1211. if (sfp->sm_mod_state == SFP_MOD_PRESENT &&
  1212. sfp->sm_dev_state == SFP_DEV_UP)
  1213. sfp_sm_mod_init(sfp);
  1214. break;
  1215. case SFP_S_INIT:
  1216. if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT)
  1217. sfp_sm_fault(sfp, true);
  1218. else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR)
  1219. sfp_sm_link_check_los(sfp);
  1220. break;
  1221. case SFP_S_WAIT_LOS:
  1222. if (event == SFP_E_TX_FAULT)
  1223. sfp_sm_fault(sfp, true);
  1224. else if (sfp_los_event_inactive(sfp, event))
  1225. sfp_sm_link_up(sfp);
  1226. break;
  1227. case SFP_S_LINK_UP:
  1228. if (event == SFP_E_TX_FAULT) {
  1229. sfp_sm_link_down(sfp);
  1230. sfp_sm_fault(sfp, true);
  1231. } else if (sfp_los_event_active(sfp, event)) {
  1232. sfp_sm_link_down(sfp);
  1233. sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
  1234. }
  1235. break;
  1236. case SFP_S_TX_FAULT:
  1237. if (event == SFP_E_TIMEOUT) {
  1238. sfp_module_tx_fault_reset(sfp);
  1239. sfp_sm_next(sfp, SFP_S_REINIT, T_INIT_JIFFIES);
  1240. }
  1241. break;
  1242. case SFP_S_REINIT:
  1243. if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
  1244. sfp_sm_fault(sfp, false);
  1245. } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
  1246. dev_info(sfp->dev, "module transmit fault recovered\n");
  1247. sfp_sm_link_check_los(sfp);
  1248. }
  1249. break;
  1250. case SFP_S_TX_DISABLE:
  1251. break;
  1252. }
  1253. dev_dbg(sfp->dev, "SM: exit %u:%u:%u\n",
  1254. sfp->sm_mod_state, sfp->sm_dev_state, sfp->sm_state);
  1255. mutex_unlock(&sfp->sm_mutex);
  1256. }
  1257. static void sfp_start(struct sfp *sfp)
  1258. {
  1259. sfp_sm_event(sfp, SFP_E_DEV_UP);
  1260. }
  1261. static void sfp_stop(struct sfp *sfp)
  1262. {
  1263. sfp_sm_event(sfp, SFP_E_DEV_DOWN);
  1264. }
  1265. static int sfp_module_info(struct sfp *sfp, struct ethtool_modinfo *modinfo)
  1266. {
  1267. /* locking... and check module is present */
  1268. if (sfp->id.ext.sff8472_compliance &&
  1269. !(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)) {
  1270. modinfo->type = ETH_MODULE_SFF_8472;
  1271. modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
  1272. } else {
  1273. modinfo->type = ETH_MODULE_SFF_8079;
  1274. modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
  1275. }
  1276. return 0;
  1277. }
  1278. static int sfp_module_eeprom(struct sfp *sfp, struct ethtool_eeprom *ee,
  1279. u8 *data)
  1280. {
  1281. unsigned int first, last, len;
  1282. int ret;
  1283. if (ee->len == 0)
  1284. return -EINVAL;
  1285. first = ee->offset;
  1286. last = ee->offset + ee->len;
  1287. if (first < ETH_MODULE_SFF_8079_LEN) {
  1288. len = min_t(unsigned int, last, ETH_MODULE_SFF_8079_LEN);
  1289. len -= first;
  1290. ret = sfp_read(sfp, false, first, data, len);
  1291. if (ret < 0)
  1292. return ret;
  1293. first += len;
  1294. data += len;
  1295. }
  1296. if (first < ETH_MODULE_SFF_8472_LEN && last > ETH_MODULE_SFF_8079_LEN) {
  1297. len = min_t(unsigned int, last, ETH_MODULE_SFF_8472_LEN);
  1298. len -= first;
  1299. first -= ETH_MODULE_SFF_8079_LEN;
  1300. ret = sfp_read(sfp, true, first, data, len);
  1301. if (ret < 0)
  1302. return ret;
  1303. }
  1304. return 0;
  1305. }
  1306. static const struct sfp_socket_ops sfp_module_ops = {
  1307. .start = sfp_start,
  1308. .stop = sfp_stop,
  1309. .module_info = sfp_module_info,
  1310. .module_eeprom = sfp_module_eeprom,
  1311. };
  1312. static void sfp_timeout(struct work_struct *work)
  1313. {
  1314. struct sfp *sfp = container_of(work, struct sfp, timeout.work);
  1315. rtnl_lock();
  1316. sfp_sm_event(sfp, SFP_E_TIMEOUT);
  1317. rtnl_unlock();
  1318. }
  1319. static void sfp_check_state(struct sfp *sfp)
  1320. {
  1321. unsigned int state, i, changed;
  1322. state = sfp_get_state(sfp);
  1323. changed = state ^ sfp->state;
  1324. changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT;
  1325. for (i = 0; i < GPIO_MAX; i++)
  1326. if (changed & BIT(i))
  1327. dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_of_names[i],
  1328. !!(sfp->state & BIT(i)), !!(state & BIT(i)));
  1329. state |= sfp->state & (SFP_F_TX_DISABLE | SFP_F_RATE_SELECT);
  1330. sfp->state = state;
  1331. rtnl_lock();
  1332. if (changed & SFP_F_PRESENT)
  1333. sfp_sm_event(sfp, state & SFP_F_PRESENT ?
  1334. SFP_E_INSERT : SFP_E_REMOVE);
  1335. if (changed & SFP_F_TX_FAULT)
  1336. sfp_sm_event(sfp, state & SFP_F_TX_FAULT ?
  1337. SFP_E_TX_FAULT : SFP_E_TX_CLEAR);
  1338. if (changed & SFP_F_LOS)
  1339. sfp_sm_event(sfp, state & SFP_F_LOS ?
  1340. SFP_E_LOS_HIGH : SFP_E_LOS_LOW);
  1341. rtnl_unlock();
  1342. }
  1343. static irqreturn_t sfp_irq(int irq, void *data)
  1344. {
  1345. struct sfp *sfp = data;
  1346. sfp_check_state(sfp);
  1347. return IRQ_HANDLED;
  1348. }
  1349. static void sfp_poll(struct work_struct *work)
  1350. {
  1351. struct sfp *sfp = container_of(work, struct sfp, poll.work);
  1352. sfp_check_state(sfp);
  1353. mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
  1354. }
  1355. static struct sfp *sfp_alloc(struct device *dev)
  1356. {
  1357. struct sfp *sfp;
  1358. sfp = kzalloc(sizeof(*sfp), GFP_KERNEL);
  1359. if (!sfp)
  1360. return ERR_PTR(-ENOMEM);
  1361. sfp->dev = dev;
  1362. mutex_init(&sfp->sm_mutex);
  1363. INIT_DELAYED_WORK(&sfp->poll, sfp_poll);
  1364. INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout);
  1365. return sfp;
  1366. }
  1367. static void sfp_cleanup(void *data)
  1368. {
  1369. struct sfp *sfp = data;
  1370. cancel_delayed_work_sync(&sfp->poll);
  1371. cancel_delayed_work_sync(&sfp->timeout);
  1372. if (sfp->i2c_mii) {
  1373. mdiobus_unregister(sfp->i2c_mii);
  1374. mdiobus_free(sfp->i2c_mii);
  1375. }
  1376. if (sfp->i2c)
  1377. i2c_put_adapter(sfp->i2c);
  1378. kfree(sfp);
  1379. }
  1380. static int sfp_probe(struct platform_device *pdev)
  1381. {
  1382. const struct sff_data *sff;
  1383. struct sfp *sfp;
  1384. bool poll = false;
  1385. int irq, err, i;
  1386. sfp = sfp_alloc(&pdev->dev);
  1387. if (IS_ERR(sfp))
  1388. return PTR_ERR(sfp);
  1389. platform_set_drvdata(pdev, sfp);
  1390. err = devm_add_action(sfp->dev, sfp_cleanup, sfp);
  1391. if (err < 0)
  1392. return err;
  1393. sff = sfp->type = &sfp_data;
  1394. if (pdev->dev.of_node) {
  1395. struct device_node *node = pdev->dev.of_node;
  1396. const struct of_device_id *id;
  1397. struct i2c_adapter *i2c;
  1398. struct device_node *np;
  1399. id = of_match_node(sfp_of_match, node);
  1400. if (WARN_ON(!id))
  1401. return -EINVAL;
  1402. sff = sfp->type = id->data;
  1403. np = of_parse_phandle(node, "i2c-bus", 0);
  1404. if (!np) {
  1405. dev_err(sfp->dev, "missing 'i2c-bus' property\n");
  1406. return -ENODEV;
  1407. }
  1408. i2c = of_find_i2c_adapter_by_node(np);
  1409. of_node_put(np);
  1410. if (!i2c)
  1411. return -EPROBE_DEFER;
  1412. err = sfp_i2c_configure(sfp, i2c);
  1413. if (err < 0) {
  1414. i2c_put_adapter(i2c);
  1415. return err;
  1416. }
  1417. }
  1418. for (i = 0; i < GPIO_MAX; i++)
  1419. if (sff->gpios & BIT(i)) {
  1420. sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev,
  1421. gpio_of_names[i], gpio_flags[i]);
  1422. if (IS_ERR(sfp->gpio[i]))
  1423. return PTR_ERR(sfp->gpio[i]);
  1424. }
  1425. sfp->get_state = sfp_gpio_get_state;
  1426. sfp->set_state = sfp_gpio_set_state;
  1427. /* Modules that have no detect signal are always present */
  1428. if (!(sfp->gpio[GPIO_MODDEF0]))
  1429. sfp->get_state = sff_gpio_get_state;
  1430. device_property_read_u32(&pdev->dev, "maximum-power-milliwatt",
  1431. &sfp->max_power_mW);
  1432. if (!sfp->max_power_mW)
  1433. sfp->max_power_mW = 1000;
  1434. dev_info(sfp->dev, "Host maximum power %u.%uW\n",
  1435. sfp->max_power_mW / 1000, (sfp->max_power_mW / 100) % 10);
  1436. sfp->sfp_bus = sfp_register_socket(sfp->dev, sfp, &sfp_module_ops);
  1437. if (!sfp->sfp_bus)
  1438. return -ENOMEM;
  1439. /* Get the initial state, and always signal TX disable,
  1440. * since the network interface will not be up.
  1441. */
  1442. sfp->state = sfp_get_state(sfp) | SFP_F_TX_DISABLE;
  1443. if (sfp->gpio[GPIO_RATE_SELECT] &&
  1444. gpiod_get_value_cansleep(sfp->gpio[GPIO_RATE_SELECT]))
  1445. sfp->state |= SFP_F_RATE_SELECT;
  1446. sfp_set_state(sfp, sfp->state);
  1447. sfp_module_tx_disable(sfp);
  1448. rtnl_lock();
  1449. if (sfp->state & SFP_F_PRESENT)
  1450. sfp_sm_event(sfp, SFP_E_INSERT);
  1451. rtnl_unlock();
  1452. for (i = 0; i < GPIO_MAX; i++) {
  1453. if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
  1454. continue;
  1455. irq = gpiod_to_irq(sfp->gpio[i]);
  1456. if (!irq) {
  1457. poll = true;
  1458. continue;
  1459. }
  1460. err = devm_request_threaded_irq(sfp->dev, irq, NULL, sfp_irq,
  1461. IRQF_ONESHOT |
  1462. IRQF_TRIGGER_RISING |
  1463. IRQF_TRIGGER_FALLING,
  1464. dev_name(sfp->dev), sfp);
  1465. if (err)
  1466. poll = true;
  1467. }
  1468. if (poll)
  1469. mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
  1470. /* We could have an issue in cases no Tx disable pin is available or
  1471. * wired as modules using a laser as their light source will continue to
  1472. * be active when the fiber is removed. This could be a safety issue and
  1473. * we should at least warn the user about that.
  1474. */
  1475. if (!sfp->gpio[GPIO_TX_DISABLE])
  1476. dev_warn(sfp->dev,
  1477. "No tx_disable pin: SFP modules will always be emitting.\n");
  1478. return 0;
  1479. }
  1480. static int sfp_remove(struct platform_device *pdev)
  1481. {
  1482. struct sfp *sfp = platform_get_drvdata(pdev);
  1483. sfp_unregister_socket(sfp->sfp_bus);
  1484. return 0;
  1485. }
  1486. static struct platform_driver sfp_driver = {
  1487. .probe = sfp_probe,
  1488. .remove = sfp_remove,
  1489. .driver = {
  1490. .name = "sfp",
  1491. .of_match_table = sfp_of_match,
  1492. },
  1493. };
  1494. static int sfp_init(void)
  1495. {
  1496. poll_jiffies = msecs_to_jiffies(100);
  1497. return platform_driver_register(&sfp_driver);
  1498. }
  1499. module_init(sfp_init);
  1500. static void sfp_exit(void)
  1501. {
  1502. platform_driver_unregister(&sfp_driver);
  1503. }
  1504. module_exit(sfp_exit);
  1505. MODULE_ALIAS("platform:sfp");
  1506. MODULE_AUTHOR("Russell King");
  1507. MODULE_LICENSE("GPL v2");