sfp-bus.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/export.h>
  3. #include <linux/kref.h>
  4. #include <linux/list.h>
  5. #include <linux/mutex.h>
  6. #include <linux/phylink.h>
  7. #include <linux/property.h>
  8. #include <linux/rtnetlink.h>
  9. #include <linux/slab.h>
  10. #include "sfp.h"
  11. /**
  12. * struct sfp_bus - internal representation of a sfp bus
  13. */
  14. struct sfp_bus {
  15. /* private: */
  16. struct kref kref;
  17. struct list_head node;
  18. struct fwnode_handle *fwnode;
  19. const struct sfp_socket_ops *socket_ops;
  20. struct device *sfp_dev;
  21. struct sfp *sfp;
  22. const struct sfp_quirk *sfp_quirk;
  23. const struct sfp_upstream_ops *upstream_ops;
  24. void *upstream;
  25. struct phy_device *phydev;
  26. bool registered;
  27. bool started;
  28. };
  29. /**
  30. * sfp_parse_port() - Parse the EEPROM base ID, setting the port type
  31. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  32. * @id: a pointer to the module's &struct sfp_eeprom_id
  33. * @support: optional pointer to an array of unsigned long for the
  34. * ethtool support mask
  35. *
  36. * Parse the EEPROM identification given in @id, and return one of
  37. * %PORT_TP, %PORT_FIBRE or %PORT_OTHER. If @support is non-%NULL,
  38. * also set the ethtool %ETHTOOL_LINK_MODE_xxx_BIT corresponding with
  39. * the connector type.
  40. *
  41. * If the port type is not known, returns %PORT_OTHER.
  42. */
  43. int sfp_parse_port(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
  44. unsigned long *support)
  45. {
  46. int port;
  47. /* port is the physical connector, set this from the connector field. */
  48. switch (id->base.connector) {
  49. case SFF8024_CONNECTOR_SC:
  50. case SFF8024_CONNECTOR_FIBERJACK:
  51. case SFF8024_CONNECTOR_LC:
  52. case SFF8024_CONNECTOR_MT_RJ:
  53. case SFF8024_CONNECTOR_MU:
  54. case SFF8024_CONNECTOR_OPTICAL_PIGTAIL:
  55. case SFF8024_CONNECTOR_MPO_1X12:
  56. case SFF8024_CONNECTOR_MPO_2X16:
  57. port = PORT_FIBRE;
  58. break;
  59. case SFF8024_CONNECTOR_RJ45:
  60. port = PORT_TP;
  61. break;
  62. case SFF8024_CONNECTOR_COPPER_PIGTAIL:
  63. port = PORT_DA;
  64. break;
  65. case SFF8024_CONNECTOR_UNSPEC:
  66. if (id->base.e1000_base_t) {
  67. port = PORT_TP;
  68. break;
  69. }
  70. fallthrough;
  71. case SFF8024_CONNECTOR_SG: /* guess */
  72. case SFF8024_CONNECTOR_HSSDC_II:
  73. case SFF8024_CONNECTOR_NOSEPARATE:
  74. case SFF8024_CONNECTOR_MXC_2X16:
  75. port = PORT_OTHER;
  76. break;
  77. default:
  78. dev_warn(bus->sfp_dev, "SFP: unknown connector id 0x%02x\n",
  79. id->base.connector);
  80. port = PORT_OTHER;
  81. break;
  82. }
  83. if (support) {
  84. switch (port) {
  85. case PORT_FIBRE:
  86. phylink_set(support, FIBRE);
  87. break;
  88. case PORT_TP:
  89. phylink_set(support, TP);
  90. break;
  91. }
  92. }
  93. return port;
  94. }
  95. EXPORT_SYMBOL_GPL(sfp_parse_port);
  96. /**
  97. * sfp_may_have_phy() - indicate whether the module may have a PHY
  98. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  99. * @id: a pointer to the module's &struct sfp_eeprom_id
  100. *
  101. * Parse the EEPROM identification given in @id, and return whether
  102. * this module may have a PHY.
  103. */
  104. bool sfp_may_have_phy(struct sfp_bus *bus, const struct sfp_eeprom_id *id)
  105. {
  106. if (id->base.e1000_base_t)
  107. return true;
  108. if (id->base.phys_id != SFF8024_ID_DWDM_SFP) {
  109. switch (id->base.extended_cc) {
  110. case SFF8024_ECC_10GBASE_T_SFI:
  111. case SFF8024_ECC_10GBASE_T_SR:
  112. case SFF8024_ECC_5GBASE_T:
  113. case SFF8024_ECC_2_5GBASE_T:
  114. return true;
  115. }
  116. }
  117. return false;
  118. }
  119. EXPORT_SYMBOL_GPL(sfp_may_have_phy);
  120. /**
  121. * sfp_parse_support() - Parse the eeprom id for supported link modes
  122. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  123. * @id: a pointer to the module's &struct sfp_eeprom_id
  124. * @support: pointer to an array of unsigned long for the ethtool support mask
  125. * @interfaces: pointer to an array of unsigned long for phy interface modes
  126. * mask
  127. *
  128. * Parse the EEPROM identification information and derive the supported
  129. * ethtool link modes for the module.
  130. */
  131. void sfp_parse_support(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
  132. unsigned long *support, unsigned long *interfaces)
  133. {
  134. unsigned int br_min, br_nom, br_max;
  135. __ETHTOOL_DECLARE_LINK_MODE_MASK(modes) = { 0, };
  136. /* Decode the bitrate information to MBd */
  137. br_min = br_nom = br_max = 0;
  138. if (id->base.br_nominal) {
  139. if (id->base.br_nominal != 255) {
  140. br_nom = id->base.br_nominal * 100;
  141. br_min = br_nom - id->base.br_nominal * id->ext.br_min;
  142. br_max = br_nom + id->base.br_nominal * id->ext.br_max;
  143. } else if (id->ext.br_max) {
  144. br_nom = 250 * id->ext.br_max;
  145. br_max = br_nom + br_nom * id->ext.br_min / 100;
  146. br_min = br_nom - br_nom * id->ext.br_min / 100;
  147. }
  148. /* When using passive cables, in case neither BR,min nor BR,max
  149. * are specified, set br_min to 0 as the nominal value is then
  150. * used as the maximum.
  151. */
  152. if (br_min == br_max && id->base.sfp_ct_passive)
  153. br_min = 0;
  154. }
  155. /* Set ethtool support from the compliance fields. */
  156. if (id->base.e10g_base_sr) {
  157. phylink_set(modes, 10000baseSR_Full);
  158. __set_bit(PHY_INTERFACE_MODE_10GBASER, interfaces);
  159. }
  160. if (id->base.e10g_base_lr) {
  161. phylink_set(modes, 10000baseLR_Full);
  162. __set_bit(PHY_INTERFACE_MODE_10GBASER, interfaces);
  163. }
  164. if (id->base.e10g_base_lrm) {
  165. phylink_set(modes, 10000baseLRM_Full);
  166. __set_bit(PHY_INTERFACE_MODE_10GBASER, interfaces);
  167. }
  168. if (id->base.e10g_base_er) {
  169. phylink_set(modes, 10000baseER_Full);
  170. __set_bit(PHY_INTERFACE_MODE_10GBASER, interfaces);
  171. }
  172. if (id->base.e1000_base_sx ||
  173. id->base.e1000_base_lx ||
  174. id->base.e1000_base_cx) {
  175. phylink_set(modes, 1000baseX_Full);
  176. __set_bit(PHY_INTERFACE_MODE_1000BASEX, interfaces);
  177. }
  178. if (id->base.e1000_base_t) {
  179. phylink_set(modes, 1000baseT_Half);
  180. phylink_set(modes, 1000baseT_Full);
  181. __set_bit(PHY_INTERFACE_MODE_1000BASEX, interfaces);
  182. __set_bit(PHY_INTERFACE_MODE_SGMII, interfaces);
  183. }
  184. /* 1000Base-PX or 1000Base-BX10 */
  185. if ((id->base.e_base_px || id->base.e_base_bx10) &&
  186. br_min <= 1300 && br_max >= 1200) {
  187. phylink_set(modes, 1000baseX_Full);
  188. __set_bit(PHY_INTERFACE_MODE_1000BASEX, interfaces);
  189. }
  190. /* 100Base-FX, 100Base-LX, 100Base-PX, 100Base-BX10 */
  191. if (id->base.e100_base_fx || id->base.e100_base_lx) {
  192. phylink_set(modes, 100baseFX_Full);
  193. __set_bit(PHY_INTERFACE_MODE_100BASEX, interfaces);
  194. }
  195. if ((id->base.e_base_px || id->base.e_base_bx10) && br_nom == 100) {
  196. phylink_set(modes, 100baseFX_Full);
  197. __set_bit(PHY_INTERFACE_MODE_100BASEX, interfaces);
  198. }
  199. /* For active or passive cables, select the link modes
  200. * based on the bit rates and the cable compliance bytes.
  201. */
  202. if ((id->base.sfp_ct_passive || id->base.sfp_ct_active) && br_nom) {
  203. /* This may look odd, but some manufacturers use 12000MBd */
  204. if (br_min <= 12000 && br_max >= 10300) {
  205. phylink_set(modes, 10000baseCR_Full);
  206. __set_bit(PHY_INTERFACE_MODE_10GBASER, interfaces);
  207. }
  208. if (br_min <= 3200 && br_max >= 3100) {
  209. phylink_set(modes, 2500baseX_Full);
  210. __set_bit(PHY_INTERFACE_MODE_2500BASEX, interfaces);
  211. }
  212. if (br_min <= 1300 && br_max >= 1200) {
  213. phylink_set(modes, 1000baseX_Full);
  214. __set_bit(PHY_INTERFACE_MODE_1000BASEX, interfaces);
  215. }
  216. }
  217. if (id->base.sfp_ct_passive) {
  218. if (id->base.passive.sff8431_app_e) {
  219. phylink_set(modes, 10000baseCR_Full);
  220. __set_bit(PHY_INTERFACE_MODE_10GBASER, interfaces);
  221. }
  222. }
  223. if (id->base.sfp_ct_active) {
  224. if (id->base.active.sff8431_app_e ||
  225. id->base.active.sff8431_lim) {
  226. phylink_set(modes, 10000baseCR_Full);
  227. __set_bit(PHY_INTERFACE_MODE_10GBASER, interfaces);
  228. }
  229. }
  230. switch (id->base.extended_cc) {
  231. case SFF8024_ECC_UNSPEC:
  232. break;
  233. case SFF8024_ECC_100G_25GAUI_C2M_AOC:
  234. if (br_min <= 28000 && br_max >= 25000) {
  235. /* 25GBASE-R, possibly with FEC */
  236. __set_bit(PHY_INTERFACE_MODE_25GBASER, interfaces);
  237. /* There is currently no link mode for 25000base
  238. * with unspecified range, reuse SR.
  239. */
  240. phylink_set(modes, 25000baseSR_Full);
  241. }
  242. break;
  243. case SFF8024_ECC_100GBASE_SR4_25GBASE_SR:
  244. phylink_set(modes, 100000baseSR4_Full);
  245. phylink_set(modes, 25000baseSR_Full);
  246. __set_bit(PHY_INTERFACE_MODE_25GBASER, interfaces);
  247. break;
  248. case SFF8024_ECC_100GBASE_LR4_25GBASE_LR:
  249. case SFF8024_ECC_100GBASE_ER4_25GBASE_ER:
  250. phylink_set(modes, 100000baseLR4_ER4_Full);
  251. break;
  252. case SFF8024_ECC_100GBASE_CR4:
  253. phylink_set(modes, 100000baseCR4_Full);
  254. fallthrough;
  255. case SFF8024_ECC_25GBASE_CR_S:
  256. case SFF8024_ECC_25GBASE_CR_N:
  257. phylink_set(modes, 25000baseCR_Full);
  258. __set_bit(PHY_INTERFACE_MODE_25GBASER, interfaces);
  259. break;
  260. case SFF8024_ECC_10GBASE_T_SFI:
  261. case SFF8024_ECC_10GBASE_T_SR:
  262. phylink_set(modes, 10000baseT_Full);
  263. __set_bit(PHY_INTERFACE_MODE_10GBASER, interfaces);
  264. break;
  265. case SFF8024_ECC_5GBASE_T:
  266. phylink_set(modes, 5000baseT_Full);
  267. __set_bit(PHY_INTERFACE_MODE_5GBASER, interfaces);
  268. break;
  269. case SFF8024_ECC_2_5GBASE_T:
  270. phylink_set(modes, 2500baseT_Full);
  271. __set_bit(PHY_INTERFACE_MODE_2500BASEX, interfaces);
  272. break;
  273. default:
  274. dev_warn(bus->sfp_dev,
  275. "Unknown/unsupported extended compliance code: 0x%02x\n",
  276. id->base.extended_cc);
  277. break;
  278. }
  279. /* For fibre channel SFP, derive possible BaseX modes */
  280. if (id->base.fc_speed_100 ||
  281. id->base.fc_speed_200 ||
  282. id->base.fc_speed_400) {
  283. if (id->base.br_nominal >= 31) {
  284. phylink_set(modes, 2500baseX_Full);
  285. __set_bit(PHY_INTERFACE_MODE_2500BASEX, interfaces);
  286. }
  287. if (id->base.br_nominal >= 12) {
  288. phylink_set(modes, 1000baseX_Full);
  289. __set_bit(PHY_INTERFACE_MODE_1000BASEX, interfaces);
  290. }
  291. }
  292. /* If we haven't discovered any modes that this module supports, try
  293. * the bitrate to determine supported modes. Some BiDi modules (eg,
  294. * 1310nm/1550nm) are not 1000BASE-BX compliant due to the differing
  295. * wavelengths, so do not set any transceiver bits.
  296. *
  297. * Do the same for modules supporting 2500BASE-X. Note that some
  298. * modules use 2500Mbaud rather than 3100 or 3200Mbaud for
  299. * 2500BASE-X, so we allow some slack here.
  300. */
  301. if (bitmap_empty(modes, __ETHTOOL_LINK_MODE_MASK_NBITS) && br_nom) {
  302. if (br_min <= 1300 && br_max >= 1200) {
  303. phylink_set(modes, 1000baseX_Full);
  304. __set_bit(PHY_INTERFACE_MODE_1000BASEX, interfaces);
  305. }
  306. if (br_min <= 3200 && br_max >= 2500) {
  307. phylink_set(modes, 2500baseX_Full);
  308. __set_bit(PHY_INTERFACE_MODE_2500BASEX, interfaces);
  309. }
  310. }
  311. if (bus->sfp_quirk && bus->sfp_quirk->modes)
  312. bus->sfp_quirk->modes(id, modes, interfaces);
  313. linkmode_or(support, support, modes);
  314. phylink_set(support, Autoneg);
  315. phylink_set(support, Pause);
  316. phylink_set(support, Asym_Pause);
  317. }
  318. EXPORT_SYMBOL_GPL(sfp_parse_support);
  319. /**
  320. * sfp_select_interface() - Select appropriate phy_interface_t mode
  321. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  322. * @link_modes: ethtool link modes mask
  323. *
  324. * Derive the phy_interface_t mode for the SFP module from the link
  325. * modes mask.
  326. */
  327. phy_interface_t sfp_select_interface(struct sfp_bus *bus,
  328. unsigned long *link_modes)
  329. {
  330. if (phylink_test(link_modes, 25000baseCR_Full) ||
  331. phylink_test(link_modes, 25000baseKR_Full) ||
  332. phylink_test(link_modes, 25000baseSR_Full))
  333. return PHY_INTERFACE_MODE_25GBASER;
  334. if (phylink_test(link_modes, 10000baseCR_Full) ||
  335. phylink_test(link_modes, 10000baseSR_Full) ||
  336. phylink_test(link_modes, 10000baseLR_Full) ||
  337. phylink_test(link_modes, 10000baseLRM_Full) ||
  338. phylink_test(link_modes, 10000baseER_Full) ||
  339. phylink_test(link_modes, 10000baseT_Full))
  340. return PHY_INTERFACE_MODE_10GBASER;
  341. if (phylink_test(link_modes, 5000baseT_Full))
  342. return PHY_INTERFACE_MODE_5GBASER;
  343. if (phylink_test(link_modes, 2500baseX_Full))
  344. return PHY_INTERFACE_MODE_2500BASEX;
  345. if (phylink_test(link_modes, 1000baseT_Half) ||
  346. phylink_test(link_modes, 1000baseT_Full))
  347. return PHY_INTERFACE_MODE_SGMII;
  348. if (phylink_test(link_modes, 1000baseX_Full))
  349. return PHY_INTERFACE_MODE_1000BASEX;
  350. if (phylink_test(link_modes, 100baseFX_Full))
  351. return PHY_INTERFACE_MODE_100BASEX;
  352. dev_warn(bus->sfp_dev, "Unable to ascertain link mode\n");
  353. return PHY_INTERFACE_MODE_NA;
  354. }
  355. EXPORT_SYMBOL_GPL(sfp_select_interface);
  356. static LIST_HEAD(sfp_buses);
  357. static DEFINE_MUTEX(sfp_mutex);
  358. static const struct sfp_upstream_ops *sfp_get_upstream_ops(struct sfp_bus *bus)
  359. {
  360. return bus->registered ? bus->upstream_ops : NULL;
  361. }
  362. static struct sfp_bus *sfp_bus_get(struct fwnode_handle *fwnode)
  363. {
  364. struct sfp_bus *sfp, *new, *found = NULL;
  365. new = kzalloc(sizeof(*new), GFP_KERNEL);
  366. mutex_lock(&sfp_mutex);
  367. list_for_each_entry(sfp, &sfp_buses, node) {
  368. if (sfp->fwnode == fwnode) {
  369. kref_get(&sfp->kref);
  370. found = sfp;
  371. break;
  372. }
  373. }
  374. if (!found && new) {
  375. kref_init(&new->kref);
  376. new->fwnode = fwnode;
  377. list_add(&new->node, &sfp_buses);
  378. found = new;
  379. new = NULL;
  380. }
  381. mutex_unlock(&sfp_mutex);
  382. kfree(new);
  383. return found;
  384. }
  385. static void sfp_bus_release(struct kref *kref)
  386. {
  387. struct sfp_bus *bus = container_of(kref, struct sfp_bus, kref);
  388. list_del(&bus->node);
  389. mutex_unlock(&sfp_mutex);
  390. kfree(bus);
  391. }
  392. /**
  393. * sfp_bus_put() - put a reference on the &struct sfp_bus
  394. * @bus: the &struct sfp_bus found via sfp_bus_find_fwnode()
  395. *
  396. * Put a reference on the &struct sfp_bus and free the underlying structure
  397. * if this was the last reference.
  398. */
  399. void sfp_bus_put(struct sfp_bus *bus)
  400. {
  401. if (bus)
  402. kref_put_mutex(&bus->kref, sfp_bus_release, &sfp_mutex);
  403. }
  404. EXPORT_SYMBOL_GPL(sfp_bus_put);
  405. static int sfp_register_bus(struct sfp_bus *bus)
  406. {
  407. const struct sfp_upstream_ops *ops = bus->upstream_ops;
  408. int ret;
  409. if (ops) {
  410. if (ops->link_down)
  411. ops->link_down(bus->upstream);
  412. if (ops->connect_phy && bus->phydev) {
  413. ret = ops->connect_phy(bus->upstream, bus->phydev);
  414. if (ret)
  415. return ret;
  416. }
  417. }
  418. bus->registered = true;
  419. bus->socket_ops->attach(bus->sfp);
  420. if (bus->started)
  421. bus->socket_ops->start(bus->sfp);
  422. bus->upstream_ops->attach(bus->upstream, bus);
  423. return 0;
  424. }
  425. static void sfp_unregister_bus(struct sfp_bus *bus)
  426. {
  427. const struct sfp_upstream_ops *ops = bus->upstream_ops;
  428. if (bus->registered) {
  429. bus->upstream_ops->detach(bus->upstream, bus);
  430. if (bus->started)
  431. bus->socket_ops->stop(bus->sfp);
  432. bus->socket_ops->detach(bus->sfp);
  433. if (bus->phydev && ops && ops->disconnect_phy)
  434. ops->disconnect_phy(bus->upstream);
  435. }
  436. bus->registered = false;
  437. }
  438. /**
  439. * sfp_get_module_info() - Get the ethtool_modinfo for a SFP module
  440. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  441. * @modinfo: a &struct ethtool_modinfo
  442. *
  443. * Fill in the type and eeprom_len parameters in @modinfo for a module on
  444. * the sfp bus specified by @bus.
  445. *
  446. * Returns 0 on success or a negative errno number.
  447. */
  448. int sfp_get_module_info(struct sfp_bus *bus, struct ethtool_modinfo *modinfo)
  449. {
  450. return bus->socket_ops->module_info(bus->sfp, modinfo);
  451. }
  452. EXPORT_SYMBOL_GPL(sfp_get_module_info);
  453. /**
  454. * sfp_get_module_eeprom() - Read the SFP module EEPROM
  455. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  456. * @ee: a &struct ethtool_eeprom
  457. * @data: buffer to contain the EEPROM data (must be at least @ee->len bytes)
  458. *
  459. * Read the EEPROM as specified by the supplied @ee. See the documentation
  460. * for &struct ethtool_eeprom for the region to be read.
  461. *
  462. * Returns 0 on success or a negative errno number.
  463. */
  464. int sfp_get_module_eeprom(struct sfp_bus *bus, struct ethtool_eeprom *ee,
  465. u8 *data)
  466. {
  467. return bus->socket_ops->module_eeprom(bus->sfp, ee, data);
  468. }
  469. EXPORT_SYMBOL_GPL(sfp_get_module_eeprom);
  470. /**
  471. * sfp_get_module_eeprom_by_page() - Read a page from the SFP module EEPROM
  472. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  473. * @page: a &struct ethtool_module_eeprom
  474. * @extack: extack for reporting problems
  475. *
  476. * Read an EEPROM page as specified by the supplied @page. See the
  477. * documentation for &struct ethtool_module_eeprom for the page to be read.
  478. *
  479. * Returns 0 on success or a negative errno number. More error
  480. * information might be provided via extack
  481. */
  482. int sfp_get_module_eeprom_by_page(struct sfp_bus *bus,
  483. const struct ethtool_module_eeprom *page,
  484. struct netlink_ext_ack *extack)
  485. {
  486. return bus->socket_ops->module_eeprom_by_page(bus->sfp, page, extack);
  487. }
  488. EXPORT_SYMBOL_GPL(sfp_get_module_eeprom_by_page);
  489. /**
  490. * sfp_upstream_start() - Inform the SFP that the network device is up
  491. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  492. *
  493. * Inform the SFP socket that the network device is now up, so that the
  494. * module can be enabled by allowing TX_DISABLE to be deasserted. This
  495. * should be called from the network device driver's &struct net_device_ops
  496. * ndo_open() method.
  497. */
  498. void sfp_upstream_start(struct sfp_bus *bus)
  499. {
  500. if (bus->registered)
  501. bus->socket_ops->start(bus->sfp);
  502. bus->started = true;
  503. }
  504. EXPORT_SYMBOL_GPL(sfp_upstream_start);
  505. /**
  506. * sfp_upstream_stop() - Inform the SFP that the network device is down
  507. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  508. *
  509. * Inform the SFP socket that the network device is now up, so that the
  510. * module can be disabled by asserting TX_DISABLE, disabling the laser
  511. * in optical modules. This should be called from the network device
  512. * driver's &struct net_device_ops ndo_stop() method.
  513. */
  514. void sfp_upstream_stop(struct sfp_bus *bus)
  515. {
  516. if (bus->registered)
  517. bus->socket_ops->stop(bus->sfp);
  518. bus->started = false;
  519. }
  520. EXPORT_SYMBOL_GPL(sfp_upstream_stop);
  521. static void sfp_upstream_clear(struct sfp_bus *bus)
  522. {
  523. bus->upstream_ops = NULL;
  524. bus->upstream = NULL;
  525. }
  526. /**
  527. * sfp_bus_find_fwnode() - parse and locate the SFP bus from fwnode
  528. * @fwnode: firmware node for the parent device (MAC or PHY)
  529. *
  530. * Parse the parent device's firmware node for a SFP bus, and locate
  531. * the sfp_bus structure, incrementing its reference count. This must
  532. * be put via sfp_bus_put() when done.
  533. *
  534. * Returns:
  535. * - on success, a pointer to the sfp_bus structure,
  536. * - %NULL if no SFP is specified,
  537. * - on failure, an error pointer value:
  538. *
  539. * - corresponding to the errors detailed for
  540. * fwnode_property_get_reference_args().
  541. * - %-ENOMEM if we failed to allocate the bus.
  542. * - an error from the upstream's connect_phy() method.
  543. */
  544. struct sfp_bus *sfp_bus_find_fwnode(struct fwnode_handle *fwnode)
  545. {
  546. struct fwnode_reference_args ref;
  547. struct sfp_bus *bus;
  548. int ret;
  549. ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL,
  550. 0, 0, &ref);
  551. if (ret == -ENOENT)
  552. return NULL;
  553. else if (ret < 0)
  554. return ERR_PTR(ret);
  555. if (!fwnode_device_is_available(ref.fwnode)) {
  556. fwnode_handle_put(ref.fwnode);
  557. return NULL;
  558. }
  559. bus = sfp_bus_get(ref.fwnode);
  560. fwnode_handle_put(ref.fwnode);
  561. if (!bus)
  562. return ERR_PTR(-ENOMEM);
  563. return bus;
  564. }
  565. EXPORT_SYMBOL_GPL(sfp_bus_find_fwnode);
  566. /**
  567. * sfp_bus_add_upstream() - parse and register the neighbouring device
  568. * @bus: the &struct sfp_bus found via sfp_bus_find_fwnode()
  569. * @upstream: the upstream private data
  570. * @ops: the upstream's &struct sfp_upstream_ops
  571. *
  572. * Add upstream driver for the SFP bus, and if the bus is complete, register
  573. * the SFP bus using sfp_register_upstream(). This takes a reference on the
  574. * bus, so it is safe to put the bus after this call.
  575. *
  576. * Returns:
  577. * - on success, a pointer to the sfp_bus structure,
  578. * - %NULL if no SFP is specified,
  579. * - on failure, an error pointer value:
  580. *
  581. * - corresponding to the errors detailed for
  582. * fwnode_property_get_reference_args().
  583. * - %-ENOMEM if we failed to allocate the bus.
  584. * - an error from the upstream's connect_phy() method.
  585. */
  586. int sfp_bus_add_upstream(struct sfp_bus *bus, void *upstream,
  587. const struct sfp_upstream_ops *ops)
  588. {
  589. int ret;
  590. /* If no bus, return success */
  591. if (!bus)
  592. return 0;
  593. rtnl_lock();
  594. kref_get(&bus->kref);
  595. bus->upstream_ops = ops;
  596. bus->upstream = upstream;
  597. if (bus->sfp) {
  598. ret = sfp_register_bus(bus);
  599. if (ret)
  600. sfp_upstream_clear(bus);
  601. } else {
  602. ret = 0;
  603. }
  604. rtnl_unlock();
  605. if (ret)
  606. sfp_bus_put(bus);
  607. return ret;
  608. }
  609. EXPORT_SYMBOL_GPL(sfp_bus_add_upstream);
  610. /**
  611. * sfp_bus_del_upstream() - Delete a sfp bus
  612. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  613. *
  614. * Delete a previously registered upstream connection for the SFP
  615. * module. @bus should have been added by sfp_bus_add_upstream().
  616. */
  617. void sfp_bus_del_upstream(struct sfp_bus *bus)
  618. {
  619. if (bus) {
  620. rtnl_lock();
  621. if (bus->sfp)
  622. sfp_unregister_bus(bus);
  623. sfp_upstream_clear(bus);
  624. rtnl_unlock();
  625. sfp_bus_put(bus);
  626. }
  627. }
  628. EXPORT_SYMBOL_GPL(sfp_bus_del_upstream);
  629. /* Socket driver entry points */
  630. int sfp_add_phy(struct sfp_bus *bus, struct phy_device *phydev)
  631. {
  632. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  633. int ret = 0;
  634. if (ops && ops->connect_phy)
  635. ret = ops->connect_phy(bus->upstream, phydev);
  636. if (ret == 0)
  637. bus->phydev = phydev;
  638. return ret;
  639. }
  640. EXPORT_SYMBOL_GPL(sfp_add_phy);
  641. void sfp_remove_phy(struct sfp_bus *bus)
  642. {
  643. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  644. if (ops && ops->disconnect_phy)
  645. ops->disconnect_phy(bus->upstream);
  646. bus->phydev = NULL;
  647. }
  648. EXPORT_SYMBOL_GPL(sfp_remove_phy);
  649. void sfp_link_up(struct sfp_bus *bus)
  650. {
  651. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  652. if (ops && ops->link_up)
  653. ops->link_up(bus->upstream);
  654. }
  655. EXPORT_SYMBOL_GPL(sfp_link_up);
  656. void sfp_link_down(struct sfp_bus *bus)
  657. {
  658. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  659. if (ops && ops->link_down)
  660. ops->link_down(bus->upstream);
  661. }
  662. EXPORT_SYMBOL_GPL(sfp_link_down);
  663. int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
  664. const struct sfp_quirk *quirk)
  665. {
  666. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  667. int ret = 0;
  668. bus->sfp_quirk = quirk;
  669. if (ops && ops->module_insert)
  670. ret = ops->module_insert(bus->upstream, id);
  671. return ret;
  672. }
  673. EXPORT_SYMBOL_GPL(sfp_module_insert);
  674. void sfp_module_remove(struct sfp_bus *bus)
  675. {
  676. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  677. if (ops && ops->module_remove)
  678. ops->module_remove(bus->upstream);
  679. bus->sfp_quirk = NULL;
  680. }
  681. EXPORT_SYMBOL_GPL(sfp_module_remove);
  682. int sfp_module_start(struct sfp_bus *bus)
  683. {
  684. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  685. int ret = 0;
  686. if (ops && ops->module_start)
  687. ret = ops->module_start(bus->upstream);
  688. return ret;
  689. }
  690. EXPORT_SYMBOL_GPL(sfp_module_start);
  691. void sfp_module_stop(struct sfp_bus *bus)
  692. {
  693. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  694. if (ops && ops->module_stop)
  695. ops->module_stop(bus->upstream);
  696. }
  697. EXPORT_SYMBOL_GPL(sfp_module_stop);
  698. static void sfp_socket_clear(struct sfp_bus *bus)
  699. {
  700. bus->sfp_dev = NULL;
  701. bus->sfp = NULL;
  702. bus->socket_ops = NULL;
  703. }
  704. struct sfp_bus *sfp_register_socket(struct device *dev, struct sfp *sfp,
  705. const struct sfp_socket_ops *ops)
  706. {
  707. struct sfp_bus *bus = sfp_bus_get(dev->fwnode);
  708. int ret = 0;
  709. if (bus) {
  710. rtnl_lock();
  711. bus->sfp_dev = dev;
  712. bus->sfp = sfp;
  713. bus->socket_ops = ops;
  714. if (bus->upstream_ops) {
  715. ret = sfp_register_bus(bus);
  716. if (ret)
  717. sfp_socket_clear(bus);
  718. }
  719. rtnl_unlock();
  720. }
  721. if (ret) {
  722. sfp_bus_put(bus);
  723. bus = NULL;
  724. }
  725. return bus;
  726. }
  727. EXPORT_SYMBOL_GPL(sfp_register_socket);
  728. void sfp_unregister_socket(struct sfp_bus *bus)
  729. {
  730. rtnl_lock();
  731. if (bus->upstream_ops)
  732. sfp_unregister_bus(bus);
  733. sfp_socket_clear(bus);
  734. rtnl_unlock();
  735. sfp_bus_put(bus);
  736. }
  737. EXPORT_SYMBOL_GPL(sfp_unregister_socket);