sfp-bus.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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. const 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_upstream_ops *upstream_ops;
  23. void *upstream;
  24. struct phy_device *phydev;
  25. bool registered;
  26. bool started;
  27. struct sfp_module_caps caps;
  28. };
  29. const struct sfp_module_caps *sfp_get_module_caps(struct sfp_bus *bus)
  30. {
  31. return &bus->caps;
  32. }
  33. EXPORT_SYMBOL_GPL(sfp_get_module_caps);
  34. static void sfp_module_parse_port(struct sfp_bus *bus,
  35. const struct sfp_eeprom_id *id)
  36. {
  37. int port;
  38. /* port is the physical connector, set this from the connector field. */
  39. switch (id->base.connector) {
  40. case SFF8024_CONNECTOR_SC:
  41. case SFF8024_CONNECTOR_FIBERJACK:
  42. case SFF8024_CONNECTOR_LC:
  43. case SFF8024_CONNECTOR_MT_RJ:
  44. case SFF8024_CONNECTOR_MU:
  45. case SFF8024_CONNECTOR_OPTICAL_PIGTAIL:
  46. case SFF8024_CONNECTOR_MPO_1X12:
  47. case SFF8024_CONNECTOR_MPO_2X16:
  48. port = PORT_FIBRE;
  49. break;
  50. case SFF8024_CONNECTOR_RJ45:
  51. port = PORT_TP;
  52. break;
  53. case SFF8024_CONNECTOR_COPPER_PIGTAIL:
  54. port = PORT_DA;
  55. break;
  56. case SFF8024_CONNECTOR_UNSPEC:
  57. if (id->base.e1000_base_t) {
  58. port = PORT_TP;
  59. break;
  60. }
  61. fallthrough;
  62. case SFF8024_CONNECTOR_SG: /* guess */
  63. case SFF8024_CONNECTOR_HSSDC_II:
  64. case SFF8024_CONNECTOR_NOSEPARATE:
  65. case SFF8024_CONNECTOR_MXC_2X16:
  66. port = PORT_OTHER;
  67. break;
  68. default:
  69. dev_warn(bus->sfp_dev, "SFP: unknown connector id 0x%02x\n",
  70. id->base.connector);
  71. port = PORT_OTHER;
  72. break;
  73. }
  74. switch (port) {
  75. case PORT_FIBRE:
  76. phylink_set(bus->caps.link_modes, FIBRE);
  77. break;
  78. case PORT_TP:
  79. phylink_set(bus->caps.link_modes, TP);
  80. break;
  81. }
  82. bus->caps.port = port;
  83. }
  84. static void sfp_module_parse_may_have_phy(struct sfp_bus *bus,
  85. const struct sfp_eeprom_id *id)
  86. {
  87. if (id->base.e1000_base_t) {
  88. bus->caps.may_have_phy = true;
  89. return;
  90. }
  91. if (id->base.phys_id != SFF8024_ID_DWDM_SFP) {
  92. switch (id->base.extended_cc) {
  93. case SFF8024_ECC_10GBASE_T_SFI:
  94. case SFF8024_ECC_10GBASE_T_SR:
  95. case SFF8024_ECC_5GBASE_T:
  96. case SFF8024_ECC_2_5GBASE_T:
  97. bus->caps.may_have_phy = true;
  98. return;
  99. }
  100. }
  101. bus->caps.may_have_phy = false;
  102. }
  103. static void sfp_module_parse_support(struct sfp_bus *bus,
  104. const struct sfp_eeprom_id *id)
  105. {
  106. unsigned long *interfaces = bus->caps.interfaces;
  107. unsigned long *modes = bus->caps.link_modes;
  108. unsigned int br_min, br_nom, br_max;
  109. /* Decode the bitrate information to MBd */
  110. br_min = br_nom = br_max = 0;
  111. if (id->base.br_nominal) {
  112. if (id->base.br_nominal != 255) {
  113. br_nom = id->base.br_nominal * 100;
  114. br_min = br_nom - id->base.br_nominal * id->ext.br_min;
  115. br_max = br_nom + id->base.br_nominal * id->ext.br_max;
  116. } else if (id->ext.br_max) {
  117. br_nom = 250 * id->ext.br_max;
  118. br_max = br_nom + br_nom * id->ext.br_min / 100;
  119. br_min = br_nom - br_nom * id->ext.br_min / 100;
  120. }
  121. /* When using passive cables, in case neither BR,min nor BR,max
  122. * are specified, set br_min to 0 as the nominal value is then
  123. * used as the maximum.
  124. */
  125. if (br_min == br_max && id->base.sfp_ct_passive)
  126. br_min = 0;
  127. }
  128. /* Set ethtool support from the compliance fields. */
  129. if (id->base.e10g_base_sr) {
  130. phylink_set(modes, 10000baseSR_Full);
  131. __set_bit(PHY_INTERFACE_MODE_10GBASER, interfaces);
  132. }
  133. if (id->base.e10g_base_lr) {
  134. phylink_set(modes, 10000baseLR_Full);
  135. __set_bit(PHY_INTERFACE_MODE_10GBASER, interfaces);
  136. }
  137. if (id->base.e10g_base_lrm) {
  138. phylink_set(modes, 10000baseLRM_Full);
  139. __set_bit(PHY_INTERFACE_MODE_10GBASER, interfaces);
  140. }
  141. if (id->base.e10g_base_er) {
  142. phylink_set(modes, 10000baseER_Full);
  143. __set_bit(PHY_INTERFACE_MODE_10GBASER, interfaces);
  144. }
  145. if (id->base.e1000_base_sx ||
  146. id->base.e1000_base_lx ||
  147. id->base.e1000_base_cx) {
  148. phylink_set(modes, 1000baseX_Full);
  149. __set_bit(PHY_INTERFACE_MODE_1000BASEX, interfaces);
  150. }
  151. if (id->base.e1000_base_t) {
  152. phylink_set(modes, 1000baseT_Half);
  153. phylink_set(modes, 1000baseT_Full);
  154. __set_bit(PHY_INTERFACE_MODE_1000BASEX, interfaces);
  155. __set_bit(PHY_INTERFACE_MODE_SGMII, interfaces);
  156. }
  157. /* 1000Base-PX or 1000Base-BX10 */
  158. if ((id->base.e_base_px || id->base.e_base_bx10) &&
  159. br_min <= 1300 && br_max >= 1200) {
  160. phylink_set(modes, 1000baseX_Full);
  161. __set_bit(PHY_INTERFACE_MODE_1000BASEX, interfaces);
  162. }
  163. /* 100Base-FX, 100Base-LX, 100Base-PX, 100Base-BX10 */
  164. if (id->base.e100_base_fx || id->base.e100_base_lx) {
  165. phylink_set(modes, 100baseFX_Full);
  166. __set_bit(PHY_INTERFACE_MODE_100BASEX, interfaces);
  167. }
  168. if ((id->base.e_base_px || id->base.e_base_bx10) && br_nom == 100) {
  169. phylink_set(modes, 100baseFX_Full);
  170. __set_bit(PHY_INTERFACE_MODE_100BASEX, interfaces);
  171. }
  172. /* For active or passive cables, select the link modes
  173. * based on the bit rates and the cable compliance bytes.
  174. */
  175. if ((id->base.sfp_ct_passive || id->base.sfp_ct_active) && br_nom) {
  176. /* This may look odd, but some manufacturers use 12000MBd */
  177. if (br_min <= 12000 && br_max >= 10300) {
  178. phylink_set(modes, 10000baseCR_Full);
  179. __set_bit(PHY_INTERFACE_MODE_10GBASER, interfaces);
  180. }
  181. if (br_min <= 3200 && br_max >= 3100) {
  182. phylink_set(modes, 2500baseX_Full);
  183. __set_bit(PHY_INTERFACE_MODE_2500BASEX, interfaces);
  184. }
  185. if (br_min <= 1300 && br_max >= 1200) {
  186. phylink_set(modes, 1000baseX_Full);
  187. __set_bit(PHY_INTERFACE_MODE_1000BASEX, interfaces);
  188. }
  189. }
  190. if (id->base.sfp_ct_passive) {
  191. if (id->base.passive.sff8431_app_e) {
  192. phylink_set(modes, 10000baseCR_Full);
  193. __set_bit(PHY_INTERFACE_MODE_10GBASER, interfaces);
  194. }
  195. }
  196. if (id->base.sfp_ct_active) {
  197. if (id->base.active.sff8431_app_e ||
  198. id->base.active.sff8431_lim) {
  199. phylink_set(modes, 10000baseCR_Full);
  200. __set_bit(PHY_INTERFACE_MODE_10GBASER, interfaces);
  201. }
  202. }
  203. switch (id->base.extended_cc) {
  204. case SFF8024_ECC_UNSPEC:
  205. break;
  206. case SFF8024_ECC_100G_25GAUI_C2M_AOC:
  207. if (br_min <= 28000 && br_max >= 25000) {
  208. /* 25GBASE-R, possibly with FEC */
  209. __set_bit(PHY_INTERFACE_MODE_25GBASER, interfaces);
  210. /* There is currently no link mode for 25000base
  211. * with unspecified range, reuse SR.
  212. */
  213. phylink_set(modes, 25000baseSR_Full);
  214. }
  215. break;
  216. case SFF8024_ECC_100GBASE_SR4_25GBASE_SR:
  217. phylink_set(modes, 100000baseSR4_Full);
  218. phylink_set(modes, 25000baseSR_Full);
  219. __set_bit(PHY_INTERFACE_MODE_25GBASER, interfaces);
  220. break;
  221. case SFF8024_ECC_100GBASE_LR4_25GBASE_LR:
  222. case SFF8024_ECC_100GBASE_ER4_25GBASE_ER:
  223. phylink_set(modes, 100000baseLR4_ER4_Full);
  224. break;
  225. case SFF8024_ECC_100GBASE_CR4:
  226. phylink_set(modes, 100000baseCR4_Full);
  227. fallthrough;
  228. case SFF8024_ECC_25GBASE_CR_S:
  229. case SFF8024_ECC_25GBASE_CR_N:
  230. phylink_set(modes, 25000baseCR_Full);
  231. __set_bit(PHY_INTERFACE_MODE_25GBASER, interfaces);
  232. break;
  233. case SFF8024_ECC_10GBASE_T_SFI:
  234. case SFF8024_ECC_10GBASE_T_SR:
  235. phylink_set(modes, 10000baseT_Full);
  236. __set_bit(PHY_INTERFACE_MODE_10GBASER, interfaces);
  237. break;
  238. case SFF8024_ECC_5GBASE_T:
  239. phylink_set(modes, 5000baseT_Full);
  240. __set_bit(PHY_INTERFACE_MODE_5GBASER, interfaces);
  241. break;
  242. case SFF8024_ECC_2_5GBASE_T:
  243. phylink_set(modes, 2500baseT_Full);
  244. __set_bit(PHY_INTERFACE_MODE_2500BASEX, interfaces);
  245. break;
  246. default:
  247. dev_warn(bus->sfp_dev,
  248. "Unknown/unsupported extended compliance code: 0x%02x\n",
  249. id->base.extended_cc);
  250. break;
  251. }
  252. /* For fibre channel SFP, derive possible BaseX modes */
  253. if (id->base.fc_speed_100 ||
  254. id->base.fc_speed_200 ||
  255. id->base.fc_speed_400) {
  256. if (id->base.br_nominal >= 31) {
  257. phylink_set(modes, 2500baseX_Full);
  258. __set_bit(PHY_INTERFACE_MODE_2500BASEX, interfaces);
  259. }
  260. if (id->base.br_nominal >= 12) {
  261. phylink_set(modes, 1000baseX_Full);
  262. __set_bit(PHY_INTERFACE_MODE_1000BASEX, interfaces);
  263. }
  264. }
  265. /* If we haven't discovered any modes that this module supports, try
  266. * the bitrate to determine supported modes. Some BiDi modules (eg,
  267. * 1310nm/1550nm) are not 1000BASE-BX compliant due to the differing
  268. * wavelengths, so do not set any transceiver bits.
  269. *
  270. * Do the same for modules supporting 2500BASE-X. Note that some
  271. * modules use 2500Mbaud rather than 3100 or 3200Mbaud for
  272. * 2500BASE-X, so we allow some slack here.
  273. */
  274. if (linkmode_empty(modes) && br_nom) {
  275. if (br_min <= 1300 && br_max >= 1200) {
  276. phylink_set(modes, 1000baseX_Full);
  277. __set_bit(PHY_INTERFACE_MODE_1000BASEX, interfaces);
  278. }
  279. if (br_min <= 3200 && br_max >= 2500) {
  280. phylink_set(modes, 2500baseX_Full);
  281. __set_bit(PHY_INTERFACE_MODE_2500BASEX, interfaces);
  282. }
  283. }
  284. phylink_set(modes, Autoneg);
  285. phylink_set(modes, Pause);
  286. phylink_set(modes, Asym_Pause);
  287. }
  288. static void sfp_init_module(struct sfp_bus *bus,
  289. const struct sfp_eeprom_id *id,
  290. const struct sfp_quirk *quirk)
  291. {
  292. memset(&bus->caps, 0, sizeof(bus->caps));
  293. sfp_module_parse_support(bus, id);
  294. sfp_module_parse_port(bus, id);
  295. sfp_module_parse_may_have_phy(bus, id);
  296. if (quirk && quirk->support)
  297. quirk->support(id, &bus->caps);
  298. }
  299. /**
  300. * sfp_select_interface() - Select appropriate phy_interface_t mode
  301. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  302. * @link_modes: ethtool link modes mask
  303. *
  304. * Derive the phy_interface_t mode for the SFP module from the link
  305. * modes mask.
  306. */
  307. phy_interface_t sfp_select_interface(struct sfp_bus *bus,
  308. const unsigned long *link_modes)
  309. {
  310. if (phylink_test(link_modes, 25000baseCR_Full) ||
  311. phylink_test(link_modes, 25000baseKR_Full) ||
  312. phylink_test(link_modes, 25000baseSR_Full))
  313. return PHY_INTERFACE_MODE_25GBASER;
  314. if (phylink_test(link_modes, 10000baseCR_Full) ||
  315. phylink_test(link_modes, 10000baseSR_Full) ||
  316. phylink_test(link_modes, 10000baseLR_Full) ||
  317. phylink_test(link_modes, 10000baseLRM_Full) ||
  318. phylink_test(link_modes, 10000baseER_Full) ||
  319. phylink_test(link_modes, 10000baseT_Full))
  320. return PHY_INTERFACE_MODE_10GBASER;
  321. if (phylink_test(link_modes, 5000baseT_Full))
  322. return PHY_INTERFACE_MODE_5GBASER;
  323. if (phylink_test(link_modes, 2500baseX_Full) ||
  324. phylink_test(link_modes, 2500baseT_Full))
  325. return PHY_INTERFACE_MODE_2500BASEX;
  326. if (phylink_test(link_modes, 1000baseT_Half) ||
  327. phylink_test(link_modes, 1000baseT_Full))
  328. return PHY_INTERFACE_MODE_SGMII;
  329. if (phylink_test(link_modes, 1000baseX_Full))
  330. return PHY_INTERFACE_MODE_1000BASEX;
  331. if (phylink_test(link_modes, 100baseFX_Full))
  332. return PHY_INTERFACE_MODE_100BASEX;
  333. dev_warn(bus->sfp_dev, "Unable to ascertain link mode\n");
  334. return PHY_INTERFACE_MODE_NA;
  335. }
  336. EXPORT_SYMBOL_GPL(sfp_select_interface);
  337. static LIST_HEAD(sfp_buses);
  338. static DEFINE_MUTEX(sfp_mutex);
  339. static const struct sfp_upstream_ops *sfp_get_upstream_ops(struct sfp_bus *bus)
  340. {
  341. return bus->registered ? bus->upstream_ops : NULL;
  342. }
  343. static struct sfp_bus *sfp_bus_get(const struct fwnode_handle *fwnode)
  344. {
  345. struct sfp_bus *sfp, *new, *found = NULL;
  346. new = kzalloc_obj(*new);
  347. mutex_lock(&sfp_mutex);
  348. list_for_each_entry(sfp, &sfp_buses, node) {
  349. if (sfp->fwnode == fwnode) {
  350. kref_get(&sfp->kref);
  351. found = sfp;
  352. break;
  353. }
  354. }
  355. if (!found && new) {
  356. kref_init(&new->kref);
  357. new->fwnode = fwnode;
  358. list_add(&new->node, &sfp_buses);
  359. found = new;
  360. new = NULL;
  361. }
  362. mutex_unlock(&sfp_mutex);
  363. kfree(new);
  364. return found;
  365. }
  366. static void sfp_bus_release(struct kref *kref)
  367. {
  368. struct sfp_bus *bus = container_of(kref, struct sfp_bus, kref);
  369. list_del(&bus->node);
  370. mutex_unlock(&sfp_mutex);
  371. kfree(bus);
  372. }
  373. /**
  374. * sfp_bus_put() - put a reference on the &struct sfp_bus
  375. * @bus: the &struct sfp_bus found via sfp_bus_find_fwnode()
  376. *
  377. * Put a reference on the &struct sfp_bus and free the underlying structure
  378. * if this was the last reference.
  379. */
  380. void sfp_bus_put(struct sfp_bus *bus)
  381. {
  382. if (bus)
  383. kref_put_mutex(&bus->kref, sfp_bus_release, &sfp_mutex);
  384. }
  385. EXPORT_SYMBOL_GPL(sfp_bus_put);
  386. static int sfp_register_bus(struct sfp_bus *bus)
  387. {
  388. const struct sfp_upstream_ops *ops = bus->upstream_ops;
  389. int ret;
  390. if (ops) {
  391. if (ops->link_down)
  392. ops->link_down(bus->upstream);
  393. if (ops->connect_phy && bus->phydev) {
  394. ret = ops->connect_phy(bus->upstream, bus->phydev);
  395. if (ret)
  396. return ret;
  397. }
  398. }
  399. bus->registered = true;
  400. bus->socket_ops->attach(bus->sfp);
  401. if (bus->started)
  402. bus->socket_ops->start(bus->sfp);
  403. bus->upstream_ops->attach(bus->upstream, bus);
  404. return 0;
  405. }
  406. static void sfp_unregister_bus(struct sfp_bus *bus)
  407. {
  408. const struct sfp_upstream_ops *ops = bus->upstream_ops;
  409. if (bus->registered) {
  410. bus->upstream_ops->detach(bus->upstream, bus);
  411. if (bus->started)
  412. bus->socket_ops->stop(bus->sfp);
  413. bus->socket_ops->detach(bus->sfp);
  414. if (bus->phydev && ops && ops->disconnect_phy)
  415. ops->disconnect_phy(bus->upstream, bus->phydev);
  416. }
  417. bus->registered = false;
  418. }
  419. /**
  420. * sfp_get_module_info() - Get the ethtool_modinfo for a SFP module
  421. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  422. * @modinfo: a &struct ethtool_modinfo
  423. *
  424. * Fill in the type and eeprom_len parameters in @modinfo for a module on
  425. * the sfp bus specified by @bus.
  426. *
  427. * Returns 0 on success or a negative errno number.
  428. */
  429. int sfp_get_module_info(struct sfp_bus *bus, struct ethtool_modinfo *modinfo)
  430. {
  431. return bus->socket_ops->module_info(bus->sfp, modinfo);
  432. }
  433. EXPORT_SYMBOL_GPL(sfp_get_module_info);
  434. /**
  435. * sfp_get_module_eeprom() - Read the SFP module EEPROM
  436. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  437. * @ee: a &struct ethtool_eeprom
  438. * @data: buffer to contain the EEPROM data (must be at least @ee->len bytes)
  439. *
  440. * Read the EEPROM as specified by the supplied @ee. See the documentation
  441. * for &struct ethtool_eeprom for the region to be read.
  442. *
  443. * Returns 0 on success or a negative errno number.
  444. */
  445. int sfp_get_module_eeprom(struct sfp_bus *bus, struct ethtool_eeprom *ee,
  446. u8 *data)
  447. {
  448. return bus->socket_ops->module_eeprom(bus->sfp, ee, data);
  449. }
  450. EXPORT_SYMBOL_GPL(sfp_get_module_eeprom);
  451. /**
  452. * sfp_get_module_eeprom_by_page() - Read a page from the SFP module EEPROM
  453. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  454. * @page: a &struct ethtool_module_eeprom
  455. * @extack: extack for reporting problems
  456. *
  457. * Read an EEPROM page as specified by the supplied @page. See the
  458. * documentation for &struct ethtool_module_eeprom for the page to be read.
  459. *
  460. * Returns 0 on success or a negative errno number. More error
  461. * information might be provided via extack
  462. */
  463. int sfp_get_module_eeprom_by_page(struct sfp_bus *bus,
  464. const struct ethtool_module_eeprom *page,
  465. struct netlink_ext_ack *extack)
  466. {
  467. return bus->socket_ops->module_eeprom_by_page(bus->sfp, page, extack);
  468. }
  469. EXPORT_SYMBOL_GPL(sfp_get_module_eeprom_by_page);
  470. /**
  471. * sfp_upstream_start() - Inform the SFP that the network device is up
  472. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  473. *
  474. * Inform the SFP socket that the network device is now up, so that the
  475. * module can be enabled by allowing TX_DISABLE to be deasserted. This
  476. * should be called from the network device driver's &struct net_device_ops
  477. * ndo_open() method.
  478. */
  479. void sfp_upstream_start(struct sfp_bus *bus)
  480. {
  481. if (bus->registered)
  482. bus->socket_ops->start(bus->sfp);
  483. bus->started = true;
  484. }
  485. EXPORT_SYMBOL_GPL(sfp_upstream_start);
  486. /**
  487. * sfp_upstream_stop() - Inform the SFP that the network device is down
  488. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  489. *
  490. * Inform the SFP socket that the network device is now up, so that the
  491. * module can be disabled by asserting TX_DISABLE, disabling the laser
  492. * in optical modules. This should be called from the network device
  493. * driver's &struct net_device_ops ndo_stop() method.
  494. */
  495. void sfp_upstream_stop(struct sfp_bus *bus)
  496. {
  497. if (bus->registered)
  498. bus->socket_ops->stop(bus->sfp);
  499. bus->started = false;
  500. }
  501. EXPORT_SYMBOL_GPL(sfp_upstream_stop);
  502. static void sfp_upstream_clear(struct sfp_bus *bus)
  503. {
  504. bus->upstream_ops = NULL;
  505. bus->upstream = NULL;
  506. }
  507. /**
  508. * sfp_upstream_set_signal_rate() - set data signalling rate
  509. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  510. * @rate_kbd: signalling rate in units of 1000 baud
  511. *
  512. * Configure the rate select settings on the SFP module for the signalling
  513. * rate (not the same as the data rate).
  514. *
  515. * Locks that may be held:
  516. * Phylink's state_mutex
  517. * rtnl lock
  518. * SFP's sm_mutex
  519. */
  520. void sfp_upstream_set_signal_rate(struct sfp_bus *bus, unsigned int rate_kbd)
  521. {
  522. if (bus->registered)
  523. bus->socket_ops->set_signal_rate(bus->sfp, rate_kbd);
  524. }
  525. EXPORT_SYMBOL_GPL(sfp_upstream_set_signal_rate);
  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(const 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. /**
  630. * sfp_get_name() - Get the SFP device name
  631. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  632. *
  633. * Gets the SFP device's name, if @bus has a registered socket. Callers must
  634. * hold RTNL, and the returned name is only valid until RTNL is released.
  635. *
  636. * Returns:
  637. * - The name of the SFP device registered with sfp_register_socket()
  638. * - %NULL if no device was registered on @bus
  639. */
  640. const char *sfp_get_name(struct sfp_bus *bus)
  641. {
  642. ASSERT_RTNL();
  643. if (bus->sfp_dev)
  644. return dev_name(bus->sfp_dev);
  645. return NULL;
  646. }
  647. EXPORT_SYMBOL_GPL(sfp_get_name);
  648. /* Socket driver entry points */
  649. int sfp_add_phy(struct sfp_bus *bus, struct phy_device *phydev)
  650. {
  651. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  652. int ret = 0;
  653. if (ops && ops->connect_phy)
  654. ret = ops->connect_phy(bus->upstream, phydev);
  655. if (ret == 0)
  656. bus->phydev = phydev;
  657. return ret;
  658. }
  659. EXPORT_SYMBOL_GPL(sfp_add_phy);
  660. void sfp_remove_phy(struct sfp_bus *bus)
  661. {
  662. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  663. if (ops && ops->disconnect_phy)
  664. ops->disconnect_phy(bus->upstream, bus->phydev);
  665. bus->phydev = NULL;
  666. }
  667. EXPORT_SYMBOL_GPL(sfp_remove_phy);
  668. void sfp_link_up(struct sfp_bus *bus)
  669. {
  670. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  671. if (ops && ops->link_up)
  672. ops->link_up(bus->upstream);
  673. }
  674. EXPORT_SYMBOL_GPL(sfp_link_up);
  675. void sfp_link_down(struct sfp_bus *bus)
  676. {
  677. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  678. if (ops && ops->link_down)
  679. ops->link_down(bus->upstream);
  680. }
  681. EXPORT_SYMBOL_GPL(sfp_link_down);
  682. int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
  683. const struct sfp_quirk *quirk)
  684. {
  685. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  686. int ret = 0;
  687. sfp_init_module(bus, id, quirk);
  688. if (ops && ops->module_insert)
  689. ret = ops->module_insert(bus->upstream, id);
  690. return ret;
  691. }
  692. EXPORT_SYMBOL_GPL(sfp_module_insert);
  693. void sfp_module_remove(struct sfp_bus *bus)
  694. {
  695. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  696. if (ops && ops->module_remove)
  697. ops->module_remove(bus->upstream);
  698. }
  699. EXPORT_SYMBOL_GPL(sfp_module_remove);
  700. int sfp_module_start(struct sfp_bus *bus)
  701. {
  702. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  703. int ret = 0;
  704. if (ops && ops->module_start)
  705. ret = ops->module_start(bus->upstream);
  706. return ret;
  707. }
  708. EXPORT_SYMBOL_GPL(sfp_module_start);
  709. void sfp_module_stop(struct sfp_bus *bus)
  710. {
  711. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  712. if (ops && ops->module_stop)
  713. ops->module_stop(bus->upstream);
  714. }
  715. EXPORT_SYMBOL_GPL(sfp_module_stop);
  716. static void sfp_socket_clear(struct sfp_bus *bus)
  717. {
  718. bus->sfp_dev = NULL;
  719. bus->sfp = NULL;
  720. bus->socket_ops = NULL;
  721. }
  722. struct sfp_bus *sfp_register_socket(struct device *dev, struct sfp *sfp,
  723. const struct sfp_socket_ops *ops)
  724. {
  725. struct sfp_bus *bus = sfp_bus_get(dev->fwnode);
  726. int ret = 0;
  727. if (bus) {
  728. rtnl_lock();
  729. bus->sfp_dev = dev;
  730. bus->sfp = sfp;
  731. bus->socket_ops = ops;
  732. if (bus->upstream_ops) {
  733. ret = sfp_register_bus(bus);
  734. if (ret)
  735. sfp_socket_clear(bus);
  736. }
  737. rtnl_unlock();
  738. }
  739. if (ret) {
  740. sfp_bus_put(bus);
  741. bus = NULL;
  742. }
  743. return bus;
  744. }
  745. EXPORT_SYMBOL_GPL(sfp_register_socket);
  746. void sfp_unregister_socket(struct sfp_bus *bus)
  747. {
  748. rtnl_lock();
  749. if (bus->upstream_ops)
  750. sfp_unregister_bus(bus);
  751. sfp_socket_clear(bus);
  752. rtnl_unlock();
  753. sfp_bus_put(bus);
  754. }
  755. EXPORT_SYMBOL_GPL(sfp_unregister_socket);