sfp-bus.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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/rtnetlink.h>
  8. #include <linux/slab.h>
  9. #include "sfp.h"
  10. struct sfp_quirk {
  11. const char *vendor;
  12. const char *part;
  13. void (*modes)(const struct sfp_eeprom_id *id, unsigned long *modes);
  14. };
  15. /**
  16. * struct sfp_bus - internal representation of a sfp bus
  17. */
  18. struct sfp_bus {
  19. /* private: */
  20. struct kref kref;
  21. struct list_head node;
  22. struct fwnode_handle *fwnode;
  23. const struct sfp_socket_ops *socket_ops;
  24. struct device *sfp_dev;
  25. struct sfp *sfp;
  26. const struct sfp_quirk *sfp_quirk;
  27. const struct sfp_upstream_ops *upstream_ops;
  28. void *upstream;
  29. struct phy_device *phydev;
  30. bool registered;
  31. bool started;
  32. };
  33. static void sfp_quirk_2500basex(const struct sfp_eeprom_id *id,
  34. unsigned long *modes)
  35. {
  36. phylink_set(modes, 2500baseX_Full);
  37. }
  38. static const struct sfp_quirk sfp_quirks[] = {
  39. {
  40. // Alcatel Lucent G-010S-P can operate at 2500base-X, but
  41. // incorrectly report 2500MBd NRZ in their EEPROM
  42. .vendor = "ALCATELLUCENT",
  43. .part = "G010SP",
  44. .modes = sfp_quirk_2500basex,
  45. }, {
  46. // Alcatel Lucent G-010S-A can operate at 2500base-X, but
  47. // report 3.2GBd NRZ in their EEPROM
  48. .vendor = "ALCATELLUCENT",
  49. .part = "3FE46541AA",
  50. .modes = sfp_quirk_2500basex,
  51. }, {
  52. // Huawei MA5671A can operate at 2500base-X, but report 1.2GBd
  53. // NRZ in their EEPROM
  54. .vendor = "HUAWEI",
  55. .part = "MA5671A",
  56. .modes = sfp_quirk_2500basex,
  57. },
  58. };
  59. static size_t sfp_strlen(const char *str, size_t maxlen)
  60. {
  61. size_t size, i;
  62. /* Trailing characters should be filled with space chars */
  63. for (i = 0, size = 0; i < maxlen; i++)
  64. if (str[i] != ' ')
  65. size = i + 1;
  66. return size;
  67. }
  68. static bool sfp_match(const char *qs, const char *str, size_t len)
  69. {
  70. if (!qs)
  71. return true;
  72. if (strlen(qs) != len)
  73. return false;
  74. return !strncmp(qs, str, len);
  75. }
  76. static const struct sfp_quirk *sfp_lookup_quirk(const struct sfp_eeprom_id *id)
  77. {
  78. const struct sfp_quirk *q;
  79. unsigned int i;
  80. size_t vs, ps;
  81. vs = sfp_strlen(id->base.vendor_name, ARRAY_SIZE(id->base.vendor_name));
  82. ps = sfp_strlen(id->base.vendor_pn, ARRAY_SIZE(id->base.vendor_pn));
  83. for (i = 0, q = sfp_quirks; i < ARRAY_SIZE(sfp_quirks); i++, q++)
  84. if (sfp_match(q->vendor, id->base.vendor_name, vs) &&
  85. sfp_match(q->part, id->base.vendor_pn, ps))
  86. return q;
  87. return NULL;
  88. }
  89. /**
  90. * sfp_parse_port() - Parse the EEPROM base ID, setting the port type
  91. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  92. * @id: a pointer to the module's &struct sfp_eeprom_id
  93. * @support: optional pointer to an array of unsigned long for the
  94. * ethtool support mask
  95. *
  96. * Parse the EEPROM identification given in @id, and return one of
  97. * %PORT_TP, %PORT_FIBRE or %PORT_OTHER. If @support is non-%NULL,
  98. * also set the ethtool %ETHTOOL_LINK_MODE_xxx_BIT corresponding with
  99. * the connector type.
  100. *
  101. * If the port type is not known, returns %PORT_OTHER.
  102. */
  103. int sfp_parse_port(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
  104. unsigned long *support)
  105. {
  106. int port;
  107. /* port is the physical connector, set this from the connector field. */
  108. switch (id->base.connector) {
  109. case SFP_CONNECTOR_SC:
  110. case SFP_CONNECTOR_FIBERJACK:
  111. case SFP_CONNECTOR_LC:
  112. case SFP_CONNECTOR_MT_RJ:
  113. case SFP_CONNECTOR_MU:
  114. case SFP_CONNECTOR_OPTICAL_PIGTAIL:
  115. port = PORT_FIBRE;
  116. break;
  117. case SFP_CONNECTOR_RJ45:
  118. port = PORT_TP;
  119. break;
  120. case SFP_CONNECTOR_COPPER_PIGTAIL:
  121. port = PORT_DA;
  122. break;
  123. case SFP_CONNECTOR_UNSPEC:
  124. if (id->base.e1000_base_t) {
  125. port = PORT_TP;
  126. break;
  127. }
  128. /* fallthrough */
  129. case SFP_CONNECTOR_SG: /* guess */
  130. case SFP_CONNECTOR_MPO_1X12:
  131. case SFP_CONNECTOR_MPO_2X16:
  132. case SFP_CONNECTOR_HSSDC_II:
  133. case SFP_CONNECTOR_NOSEPARATE:
  134. case SFP_CONNECTOR_MXC_2X16:
  135. port = PORT_OTHER;
  136. break;
  137. default:
  138. dev_warn(bus->sfp_dev, "SFP: unknown connector id 0x%02x\n",
  139. id->base.connector);
  140. port = PORT_OTHER;
  141. break;
  142. }
  143. if (support) {
  144. switch (port) {
  145. case PORT_FIBRE:
  146. phylink_set(support, FIBRE);
  147. break;
  148. case PORT_TP:
  149. phylink_set(support, TP);
  150. break;
  151. }
  152. }
  153. return port;
  154. }
  155. EXPORT_SYMBOL_GPL(sfp_parse_port);
  156. /**
  157. * sfp_parse_support() - Parse the eeprom id for supported link modes
  158. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  159. * @id: a pointer to the module's &struct sfp_eeprom_id
  160. * @support: pointer to an array of unsigned long for the ethtool support mask
  161. *
  162. * Parse the EEPROM identification information and derive the supported
  163. * ethtool link modes for the module.
  164. */
  165. void sfp_parse_support(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
  166. unsigned long *support)
  167. {
  168. unsigned int br_min, br_nom, br_max;
  169. __ETHTOOL_DECLARE_LINK_MODE_MASK(modes) = { 0, };
  170. /* Decode the bitrate information to MBd */
  171. br_min = br_nom = br_max = 0;
  172. if (id->base.br_nominal) {
  173. if (id->base.br_nominal != 255) {
  174. br_nom = id->base.br_nominal * 100;
  175. br_min = br_nom - id->base.br_nominal * id->ext.br_min;
  176. br_max = br_nom + id->base.br_nominal * id->ext.br_max;
  177. } else if (id->ext.br_max) {
  178. br_nom = 250 * id->ext.br_max;
  179. br_max = br_nom + br_nom * id->ext.br_min / 100;
  180. br_min = br_nom - br_nom * id->ext.br_min / 100;
  181. }
  182. /* When using passive cables, in case neither BR,min nor BR,max
  183. * are specified, set br_min to 0 as the nominal value is then
  184. * used as the maximum.
  185. */
  186. if (br_min == br_max && id->base.sfp_ct_passive)
  187. br_min = 0;
  188. }
  189. /* Set ethtool support from the compliance fields. */
  190. if (id->base.e10g_base_sr)
  191. phylink_set(modes, 10000baseSR_Full);
  192. if (id->base.e10g_base_lr)
  193. phylink_set(modes, 10000baseLR_Full);
  194. if (id->base.e10g_base_lrm)
  195. phylink_set(modes, 10000baseLRM_Full);
  196. if (id->base.e10g_base_er)
  197. phylink_set(modes, 10000baseER_Full);
  198. if (id->base.e1000_base_sx ||
  199. id->base.e1000_base_lx ||
  200. id->base.e1000_base_cx)
  201. phylink_set(modes, 1000baseX_Full);
  202. if (id->base.e1000_base_t) {
  203. phylink_set(modes, 1000baseT_Half);
  204. phylink_set(modes, 1000baseT_Full);
  205. }
  206. /* 1000Base-PX or 1000Base-BX10 */
  207. if ((id->base.e_base_px || id->base.e_base_bx10) &&
  208. br_min <= 1300 && br_max >= 1200)
  209. phylink_set(modes, 1000baseX_Full);
  210. /* For active or passive cables, select the link modes
  211. * based on the bit rates and the cable compliance bytes.
  212. */
  213. if ((id->base.sfp_ct_passive || id->base.sfp_ct_active) && br_nom) {
  214. /* This may look odd, but some manufacturers use 12000MBd */
  215. if (br_min <= 12000 && br_max >= 10300)
  216. phylink_set(modes, 10000baseCR_Full);
  217. if (br_min <= 3200 && br_max >= 3100)
  218. phylink_set(modes, 2500baseX_Full);
  219. if (br_min <= 1300 && br_max >= 1200)
  220. phylink_set(modes, 1000baseX_Full);
  221. }
  222. if (id->base.sfp_ct_passive) {
  223. if (id->base.passive.sff8431_app_e)
  224. phylink_set(modes, 10000baseCR_Full);
  225. }
  226. if (id->base.sfp_ct_active) {
  227. if (id->base.active.sff8431_app_e ||
  228. id->base.active.sff8431_lim) {
  229. phylink_set(modes, 10000baseCR_Full);
  230. }
  231. }
  232. switch (id->base.extended_cc) {
  233. case 0x00: /* Unspecified */
  234. break;
  235. case 0x02: /* 100Gbase-SR4 or 25Gbase-SR */
  236. phylink_set(modes, 100000baseSR4_Full);
  237. phylink_set(modes, 25000baseSR_Full);
  238. break;
  239. case 0x03: /* 100Gbase-LR4 or 25Gbase-LR */
  240. case 0x04: /* 100Gbase-ER4 or 25Gbase-ER */
  241. phylink_set(modes, 100000baseLR4_ER4_Full);
  242. break;
  243. case 0x0b: /* 100Gbase-CR4 or 25Gbase-CR CA-L */
  244. case 0x0c: /* 25Gbase-CR CA-S */
  245. case 0x0d: /* 25Gbase-CR CA-N */
  246. phylink_set(modes, 100000baseCR4_Full);
  247. phylink_set(modes, 25000baseCR_Full);
  248. break;
  249. default:
  250. dev_warn(bus->sfp_dev,
  251. "Unknown/unsupported extended compliance code: 0x%02x\n",
  252. id->base.extended_cc);
  253. break;
  254. }
  255. /* For fibre channel SFP, derive possible BaseX modes */
  256. if (id->base.fc_speed_100 ||
  257. id->base.fc_speed_200 ||
  258. id->base.fc_speed_400) {
  259. if (id->base.br_nominal >= 31)
  260. phylink_set(modes, 2500baseX_Full);
  261. if (id->base.br_nominal >= 12)
  262. phylink_set(modes, 1000baseX_Full);
  263. }
  264. /* If we haven't discovered any modes that this module supports, try
  265. * the encoding and bitrate to determine supported modes. Some BiDi
  266. * modules (eg, 1310nm/1550nm) are not 1000BASE-BX compliant due to
  267. * the differing wavelengths, so do not set any transceiver bits.
  268. */
  269. if (bitmap_empty(modes, __ETHTOOL_LINK_MODE_MASK_NBITS)) {
  270. /* If the encoding and bit rate allows 1000baseX */
  271. if (id->base.encoding == SFP_ENCODING_8B10B && br_nom &&
  272. br_min <= 1300 && br_max >= 1200)
  273. phylink_set(modes, 1000baseX_Full);
  274. }
  275. if (bus->sfp_quirk)
  276. bus->sfp_quirk->modes(id, modes);
  277. bitmap_or(support, support, modes, __ETHTOOL_LINK_MODE_MASK_NBITS);
  278. phylink_set(support, Autoneg);
  279. phylink_set(support, Pause);
  280. phylink_set(support, Asym_Pause);
  281. }
  282. EXPORT_SYMBOL_GPL(sfp_parse_support);
  283. /**
  284. * sfp_select_interface() - Select appropriate phy_interface_t mode
  285. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  286. * @id: a pointer to the module's &struct sfp_eeprom_id
  287. * @link_modes: ethtool link modes mask
  288. *
  289. * Derive the phy_interface_t mode for the information found in the
  290. * module's identifying EEPROM and the link modes mask. There is no
  291. * standard or defined way to derive this information, so we decide
  292. * based upon the link mode mask.
  293. */
  294. phy_interface_t sfp_select_interface(struct sfp_bus *bus,
  295. const struct sfp_eeprom_id *id,
  296. unsigned long *link_modes)
  297. {
  298. if (phylink_test(link_modes, 10000baseCR_Full) ||
  299. phylink_test(link_modes, 10000baseSR_Full) ||
  300. phylink_test(link_modes, 10000baseLR_Full) ||
  301. phylink_test(link_modes, 10000baseLRM_Full) ||
  302. phylink_test(link_modes, 10000baseER_Full))
  303. return PHY_INTERFACE_MODE_10GKR;
  304. if (phylink_test(link_modes, 2500baseX_Full))
  305. return PHY_INTERFACE_MODE_2500BASEX;
  306. if (id->base.e1000_base_t ||
  307. id->base.e100_base_lx ||
  308. id->base.e100_base_fx)
  309. return PHY_INTERFACE_MODE_SGMII;
  310. if (phylink_test(link_modes, 1000baseX_Full))
  311. return PHY_INTERFACE_MODE_1000BASEX;
  312. dev_warn(bus->sfp_dev, "Unable to ascertain link mode\n");
  313. return PHY_INTERFACE_MODE_NA;
  314. }
  315. EXPORT_SYMBOL_GPL(sfp_select_interface);
  316. static LIST_HEAD(sfp_buses);
  317. static DEFINE_MUTEX(sfp_mutex);
  318. static const struct sfp_upstream_ops *sfp_get_upstream_ops(struct sfp_bus *bus)
  319. {
  320. return bus->registered ? bus->upstream_ops : NULL;
  321. }
  322. static struct sfp_bus *sfp_bus_get(struct fwnode_handle *fwnode)
  323. {
  324. struct sfp_bus *sfp, *new, *found = NULL;
  325. new = kzalloc(sizeof(*new), GFP_KERNEL);
  326. mutex_lock(&sfp_mutex);
  327. list_for_each_entry(sfp, &sfp_buses, node) {
  328. if (sfp->fwnode == fwnode) {
  329. kref_get(&sfp->kref);
  330. found = sfp;
  331. break;
  332. }
  333. }
  334. if (!found && new) {
  335. kref_init(&new->kref);
  336. new->fwnode = fwnode;
  337. list_add(&new->node, &sfp_buses);
  338. found = new;
  339. new = NULL;
  340. }
  341. mutex_unlock(&sfp_mutex);
  342. kfree(new);
  343. return found;
  344. }
  345. static void sfp_bus_release(struct kref *kref)
  346. {
  347. struct sfp_bus *bus = container_of(kref, struct sfp_bus, kref);
  348. list_del(&bus->node);
  349. mutex_unlock(&sfp_mutex);
  350. kfree(bus);
  351. }
  352. static void sfp_bus_put(struct sfp_bus *bus)
  353. {
  354. kref_put_mutex(&bus->kref, sfp_bus_release, &sfp_mutex);
  355. }
  356. static int sfp_register_bus(struct sfp_bus *bus)
  357. {
  358. const struct sfp_upstream_ops *ops = bus->upstream_ops;
  359. int ret;
  360. if (ops) {
  361. if (ops->link_down)
  362. ops->link_down(bus->upstream);
  363. if (ops->connect_phy && bus->phydev) {
  364. ret = ops->connect_phy(bus->upstream, bus->phydev);
  365. if (ret)
  366. return ret;
  367. }
  368. }
  369. bus->socket_ops->attach(bus->sfp);
  370. if (bus->started)
  371. bus->socket_ops->start(bus->sfp);
  372. bus->upstream_ops->attach(bus->upstream, bus);
  373. bus->registered = true;
  374. return 0;
  375. }
  376. static void sfp_unregister_bus(struct sfp_bus *bus)
  377. {
  378. const struct sfp_upstream_ops *ops = bus->upstream_ops;
  379. if (bus->registered) {
  380. bus->upstream_ops->detach(bus->upstream, bus);
  381. if (bus->started)
  382. bus->socket_ops->stop(bus->sfp);
  383. bus->socket_ops->detach(bus->sfp);
  384. if (bus->phydev && ops && ops->disconnect_phy)
  385. ops->disconnect_phy(bus->upstream);
  386. }
  387. bus->registered = false;
  388. }
  389. /**
  390. * sfp_get_module_info() - Get the ethtool_modinfo for a SFP module
  391. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  392. * @modinfo: a &struct ethtool_modinfo
  393. *
  394. * Fill in the type and eeprom_len parameters in @modinfo for a module on
  395. * the sfp bus specified by @bus.
  396. *
  397. * Returns 0 on success or a negative errno number.
  398. */
  399. int sfp_get_module_info(struct sfp_bus *bus, struct ethtool_modinfo *modinfo)
  400. {
  401. return bus->socket_ops->module_info(bus->sfp, modinfo);
  402. }
  403. EXPORT_SYMBOL_GPL(sfp_get_module_info);
  404. /**
  405. * sfp_get_module_eeprom() - Read the SFP module EEPROM
  406. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  407. * @ee: a &struct ethtool_eeprom
  408. * @data: buffer to contain the EEPROM data (must be at least @ee->len bytes)
  409. *
  410. * Read the EEPROM as specified by the supplied @ee. See the documentation
  411. * for &struct ethtool_eeprom for the region to be read.
  412. *
  413. * Returns 0 on success or a negative errno number.
  414. */
  415. int sfp_get_module_eeprom(struct sfp_bus *bus, struct ethtool_eeprom *ee,
  416. u8 *data)
  417. {
  418. return bus->socket_ops->module_eeprom(bus->sfp, ee, data);
  419. }
  420. EXPORT_SYMBOL_GPL(sfp_get_module_eeprom);
  421. /**
  422. * sfp_upstream_start() - Inform the SFP that the network device is up
  423. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  424. *
  425. * Inform the SFP socket that the network device is now up, so that the
  426. * module can be enabled by allowing TX_DISABLE to be deasserted. This
  427. * should be called from the network device driver's &struct net_device_ops
  428. * ndo_open() method.
  429. */
  430. void sfp_upstream_start(struct sfp_bus *bus)
  431. {
  432. if (bus->registered)
  433. bus->socket_ops->start(bus->sfp);
  434. bus->started = true;
  435. }
  436. EXPORT_SYMBOL_GPL(sfp_upstream_start);
  437. /**
  438. * sfp_upstream_stop() - Inform the SFP that the network device is down
  439. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  440. *
  441. * Inform the SFP socket that the network device is now up, so that the
  442. * module can be disabled by asserting TX_DISABLE, disabling the laser
  443. * in optical modules. This should be called from the network device
  444. * driver's &struct net_device_ops ndo_stop() method.
  445. */
  446. void sfp_upstream_stop(struct sfp_bus *bus)
  447. {
  448. if (bus->registered)
  449. bus->socket_ops->stop(bus->sfp);
  450. bus->started = false;
  451. }
  452. EXPORT_SYMBOL_GPL(sfp_upstream_stop);
  453. static void sfp_upstream_clear(struct sfp_bus *bus)
  454. {
  455. bus->upstream_ops = NULL;
  456. bus->upstream = NULL;
  457. }
  458. /**
  459. * sfp_register_upstream() - Register the neighbouring device
  460. * @fwnode: firmware node for the SFP bus
  461. * @upstream: the upstream private data
  462. * @ops: the upstream's &struct sfp_upstream_ops
  463. *
  464. * Register the upstream device (eg, PHY) with the SFP bus. MAC drivers
  465. * should use phylink, which will call this function for them. Returns
  466. * a pointer to the allocated &struct sfp_bus.
  467. *
  468. * On error, returns %NULL.
  469. */
  470. struct sfp_bus *sfp_register_upstream(struct fwnode_handle *fwnode,
  471. void *upstream,
  472. const struct sfp_upstream_ops *ops)
  473. {
  474. struct sfp_bus *bus = sfp_bus_get(fwnode);
  475. int ret = 0;
  476. if (bus) {
  477. rtnl_lock();
  478. bus->upstream_ops = ops;
  479. bus->upstream = upstream;
  480. if (bus->sfp) {
  481. ret = sfp_register_bus(bus);
  482. if (ret)
  483. sfp_upstream_clear(bus);
  484. }
  485. rtnl_unlock();
  486. }
  487. if (ret) {
  488. sfp_bus_put(bus);
  489. bus = NULL;
  490. }
  491. return bus;
  492. }
  493. EXPORT_SYMBOL_GPL(sfp_register_upstream);
  494. /**
  495. * sfp_unregister_upstream() - Unregister sfp bus
  496. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  497. *
  498. * Unregister a previously registered upstream connection for the SFP
  499. * module. @bus is returned from sfp_register_upstream().
  500. */
  501. void sfp_unregister_upstream(struct sfp_bus *bus)
  502. {
  503. rtnl_lock();
  504. if (bus->sfp)
  505. sfp_unregister_bus(bus);
  506. sfp_upstream_clear(bus);
  507. rtnl_unlock();
  508. sfp_bus_put(bus);
  509. }
  510. EXPORT_SYMBOL_GPL(sfp_unregister_upstream);
  511. /* Socket driver entry points */
  512. int sfp_add_phy(struct sfp_bus *bus, struct phy_device *phydev)
  513. {
  514. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  515. int ret = 0;
  516. if (ops && ops->connect_phy)
  517. ret = ops->connect_phy(bus->upstream, phydev);
  518. if (ret == 0)
  519. bus->phydev = phydev;
  520. return ret;
  521. }
  522. EXPORT_SYMBOL_GPL(sfp_add_phy);
  523. void sfp_remove_phy(struct sfp_bus *bus)
  524. {
  525. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  526. if (ops && ops->disconnect_phy)
  527. ops->disconnect_phy(bus->upstream);
  528. bus->phydev = NULL;
  529. }
  530. EXPORT_SYMBOL_GPL(sfp_remove_phy);
  531. void sfp_link_up(struct sfp_bus *bus)
  532. {
  533. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  534. if (ops && ops->link_up)
  535. ops->link_up(bus->upstream);
  536. }
  537. EXPORT_SYMBOL_GPL(sfp_link_up);
  538. void sfp_link_down(struct sfp_bus *bus)
  539. {
  540. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  541. if (ops && ops->link_down)
  542. ops->link_down(bus->upstream);
  543. }
  544. EXPORT_SYMBOL_GPL(sfp_link_down);
  545. int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id)
  546. {
  547. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  548. int ret = 0;
  549. bus->sfp_quirk = sfp_lookup_quirk(id);
  550. if (ops && ops->module_insert)
  551. ret = ops->module_insert(bus->upstream, id);
  552. return ret;
  553. }
  554. EXPORT_SYMBOL_GPL(sfp_module_insert);
  555. void sfp_module_remove(struct sfp_bus *bus)
  556. {
  557. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  558. if (ops && ops->module_remove)
  559. ops->module_remove(bus->upstream);
  560. bus->sfp_quirk = NULL;
  561. }
  562. EXPORT_SYMBOL_GPL(sfp_module_remove);
  563. static void sfp_socket_clear(struct sfp_bus *bus)
  564. {
  565. bus->sfp_dev = NULL;
  566. bus->sfp = NULL;
  567. bus->socket_ops = NULL;
  568. }
  569. struct sfp_bus *sfp_register_socket(struct device *dev, struct sfp *sfp,
  570. const struct sfp_socket_ops *ops)
  571. {
  572. struct sfp_bus *bus = sfp_bus_get(dev->fwnode);
  573. int ret = 0;
  574. if (bus) {
  575. rtnl_lock();
  576. bus->sfp_dev = dev;
  577. bus->sfp = sfp;
  578. bus->socket_ops = ops;
  579. if (bus->upstream_ops) {
  580. ret = sfp_register_bus(bus);
  581. if (ret)
  582. sfp_socket_clear(bus);
  583. }
  584. rtnl_unlock();
  585. }
  586. if (ret) {
  587. sfp_bus_put(bus);
  588. bus = NULL;
  589. }
  590. return bus;
  591. }
  592. EXPORT_SYMBOL_GPL(sfp_register_socket);
  593. void sfp_unregister_socket(struct sfp_bus *bus)
  594. {
  595. rtnl_lock();
  596. if (bus->upstream_ops)
  597. sfp_unregister_bus(bus);
  598. sfp_socket_clear(bus);
  599. rtnl_unlock();
  600. sfp_bus_put(bus);
  601. }
  602. EXPORT_SYMBOL_GPL(sfp_unregister_socket);