sfp-bus.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. #include <linux/export.h>
  2. #include <linux/kref.h>
  3. #include <linux/list.h>
  4. #include <linux/mutex.h>
  5. #include <linux/phylink.h>
  6. #include <linux/rtnetlink.h>
  7. #include <linux/slab.h>
  8. #include "sfp.h"
  9. struct sfp_bus {
  10. struct kref kref;
  11. struct list_head node;
  12. struct device_node *device_node;
  13. const struct sfp_socket_ops *socket_ops;
  14. struct device *sfp_dev;
  15. struct sfp *sfp;
  16. const struct sfp_upstream_ops *upstream_ops;
  17. void *upstream;
  18. struct net_device *netdev;
  19. struct phy_device *phydev;
  20. bool registered;
  21. bool started;
  22. };
  23. int sfp_parse_port(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
  24. unsigned long *support)
  25. {
  26. int port;
  27. /* port is the physical connector, set this from the connector field. */
  28. switch (id->base.connector) {
  29. case SFP_CONNECTOR_SC:
  30. case SFP_CONNECTOR_FIBERJACK:
  31. case SFP_CONNECTOR_LC:
  32. case SFP_CONNECTOR_MT_RJ:
  33. case SFP_CONNECTOR_MU:
  34. case SFP_CONNECTOR_OPTICAL_PIGTAIL:
  35. if (support)
  36. phylink_set(support, FIBRE);
  37. port = PORT_FIBRE;
  38. break;
  39. case SFP_CONNECTOR_RJ45:
  40. if (support)
  41. phylink_set(support, TP);
  42. port = PORT_TP;
  43. break;
  44. case SFP_CONNECTOR_UNSPEC:
  45. if (id->base.e1000_base_t) {
  46. if (support)
  47. phylink_set(support, TP);
  48. port = PORT_TP;
  49. break;
  50. }
  51. /* fallthrough */
  52. case SFP_CONNECTOR_SG: /* guess */
  53. case SFP_CONNECTOR_MPO_1X12:
  54. case SFP_CONNECTOR_MPO_2X16:
  55. case SFP_CONNECTOR_HSSDC_II:
  56. case SFP_CONNECTOR_COPPER_PIGTAIL:
  57. case SFP_CONNECTOR_NOSEPARATE:
  58. case SFP_CONNECTOR_MXC_2X16:
  59. port = PORT_OTHER;
  60. break;
  61. default:
  62. dev_warn(bus->sfp_dev, "SFP: unknown connector id 0x%02x\n",
  63. id->base.connector);
  64. port = PORT_OTHER;
  65. break;
  66. }
  67. return port;
  68. }
  69. EXPORT_SYMBOL_GPL(sfp_parse_port);
  70. phy_interface_t sfp_parse_interface(struct sfp_bus *bus,
  71. const struct sfp_eeprom_id *id)
  72. {
  73. phy_interface_t iface;
  74. /* Setting the serdes link mode is guesswork: there's no field in
  75. * the EEPROM which indicates what mode should be used.
  76. *
  77. * If the module wants 64b66b, then it must be >= 10G.
  78. *
  79. * If it's a gigabit-only fiber module, it probably does not have
  80. * a PHY, so switch to 802.3z negotiation mode. Otherwise, switch
  81. * to SGMII mode (which is required to support non-gigabit speeds).
  82. */
  83. switch (id->base.encoding) {
  84. case SFP_ENCODING_8472_64B66B:
  85. iface = PHY_INTERFACE_MODE_10GKR;
  86. break;
  87. case SFP_ENCODING_8B10B:
  88. if (!id->base.e1000_base_t &&
  89. !id->base.e100_base_lx &&
  90. !id->base.e100_base_fx)
  91. iface = PHY_INTERFACE_MODE_1000BASEX;
  92. else
  93. iface = PHY_INTERFACE_MODE_SGMII;
  94. break;
  95. default:
  96. iface = PHY_INTERFACE_MODE_NA;
  97. dev_err(bus->sfp_dev,
  98. "SFP module encoding does not support 8b10b nor 64b66b\n");
  99. break;
  100. }
  101. return iface;
  102. }
  103. EXPORT_SYMBOL_GPL(sfp_parse_interface);
  104. void sfp_parse_support(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
  105. unsigned long *support)
  106. {
  107. phylink_set(support, Autoneg);
  108. phylink_set(support, Pause);
  109. phylink_set(support, Asym_Pause);
  110. /* Set ethtool support from the compliance fields. */
  111. if (id->base.e10g_base_sr)
  112. phylink_set(support, 10000baseSR_Full);
  113. if (id->base.e10g_base_lr)
  114. phylink_set(support, 10000baseLR_Full);
  115. if (id->base.e10g_base_lrm)
  116. phylink_set(support, 10000baseLRM_Full);
  117. if (id->base.e10g_base_er)
  118. phylink_set(support, 10000baseER_Full);
  119. if (id->base.e1000_base_sx ||
  120. id->base.e1000_base_lx ||
  121. id->base.e1000_base_cx)
  122. phylink_set(support, 1000baseX_Full);
  123. if (id->base.e1000_base_t) {
  124. phylink_set(support, 1000baseT_Half);
  125. phylink_set(support, 1000baseT_Full);
  126. }
  127. switch (id->base.extended_cc) {
  128. case 0x00: /* Unspecified */
  129. break;
  130. case 0x02: /* 100Gbase-SR4 or 25Gbase-SR */
  131. phylink_set(support, 100000baseSR4_Full);
  132. phylink_set(support, 25000baseSR_Full);
  133. break;
  134. case 0x03: /* 100Gbase-LR4 or 25Gbase-LR */
  135. case 0x04: /* 100Gbase-ER4 or 25Gbase-ER */
  136. phylink_set(support, 100000baseLR4_ER4_Full);
  137. break;
  138. case 0x0b: /* 100Gbase-CR4 or 25Gbase-CR CA-L */
  139. case 0x0c: /* 25Gbase-CR CA-S */
  140. case 0x0d: /* 25Gbase-CR CA-N */
  141. phylink_set(support, 100000baseCR4_Full);
  142. phylink_set(support, 25000baseCR_Full);
  143. break;
  144. default:
  145. dev_warn(bus->sfp_dev,
  146. "Unknown/unsupported extended compliance code: 0x%02x\n",
  147. id->base.extended_cc);
  148. break;
  149. }
  150. /* For fibre channel SFP, derive possible BaseX modes */
  151. if (id->base.fc_speed_100 ||
  152. id->base.fc_speed_200 ||
  153. id->base.fc_speed_400) {
  154. if (id->base.br_nominal >= 31)
  155. phylink_set(support, 2500baseX_Full);
  156. if (id->base.br_nominal >= 12)
  157. phylink_set(support, 1000baseX_Full);
  158. }
  159. switch (id->base.connector) {
  160. case SFP_CONNECTOR_SC:
  161. case SFP_CONNECTOR_FIBERJACK:
  162. case SFP_CONNECTOR_LC:
  163. case SFP_CONNECTOR_MT_RJ:
  164. case SFP_CONNECTOR_MU:
  165. case SFP_CONNECTOR_OPTICAL_PIGTAIL:
  166. break;
  167. case SFP_CONNECTOR_UNSPEC:
  168. if (id->base.e1000_base_t)
  169. break;
  170. case SFP_CONNECTOR_SG: /* guess */
  171. case SFP_CONNECTOR_MPO_1X12:
  172. case SFP_CONNECTOR_MPO_2X16:
  173. case SFP_CONNECTOR_HSSDC_II:
  174. case SFP_CONNECTOR_COPPER_PIGTAIL:
  175. case SFP_CONNECTOR_NOSEPARATE:
  176. case SFP_CONNECTOR_MXC_2X16:
  177. default:
  178. /* a guess at the supported link modes */
  179. dev_warn(bus->sfp_dev,
  180. "Guessing link modes, please report...\n");
  181. phylink_set(support, 1000baseT_Half);
  182. phylink_set(support, 1000baseT_Full);
  183. break;
  184. }
  185. }
  186. EXPORT_SYMBOL_GPL(sfp_parse_support);
  187. static LIST_HEAD(sfp_buses);
  188. static DEFINE_MUTEX(sfp_mutex);
  189. static const struct sfp_upstream_ops *sfp_get_upstream_ops(struct sfp_bus *bus)
  190. {
  191. return bus->registered ? bus->upstream_ops : NULL;
  192. }
  193. static struct sfp_bus *sfp_bus_get(struct device_node *np)
  194. {
  195. struct sfp_bus *sfp, *new, *found = NULL;
  196. new = kzalloc(sizeof(*new), GFP_KERNEL);
  197. mutex_lock(&sfp_mutex);
  198. list_for_each_entry(sfp, &sfp_buses, node) {
  199. if (sfp->device_node == np) {
  200. kref_get(&sfp->kref);
  201. found = sfp;
  202. break;
  203. }
  204. }
  205. if (!found && new) {
  206. kref_init(&new->kref);
  207. new->device_node = np;
  208. list_add(&new->node, &sfp_buses);
  209. found = new;
  210. new = NULL;
  211. }
  212. mutex_unlock(&sfp_mutex);
  213. kfree(new);
  214. return found;
  215. }
  216. static void sfp_bus_release(struct kref *kref) __releases(sfp_mutex)
  217. {
  218. struct sfp_bus *bus = container_of(kref, struct sfp_bus, kref);
  219. list_del(&bus->node);
  220. mutex_unlock(&sfp_mutex);
  221. kfree(bus);
  222. }
  223. static void sfp_bus_put(struct sfp_bus *bus)
  224. {
  225. kref_put_mutex(&bus->kref, sfp_bus_release, &sfp_mutex);
  226. }
  227. static int sfp_register_bus(struct sfp_bus *bus)
  228. {
  229. const struct sfp_upstream_ops *ops = bus->upstream_ops;
  230. int ret;
  231. if (ops) {
  232. if (ops->link_down)
  233. ops->link_down(bus->upstream);
  234. if (ops->connect_phy && bus->phydev) {
  235. ret = ops->connect_phy(bus->upstream, bus->phydev);
  236. if (ret)
  237. return ret;
  238. }
  239. }
  240. if (bus->started)
  241. bus->socket_ops->start(bus->sfp);
  242. bus->registered = true;
  243. return 0;
  244. }
  245. static void sfp_unregister_bus(struct sfp_bus *bus)
  246. {
  247. const struct sfp_upstream_ops *ops = bus->upstream_ops;
  248. if (bus->registered) {
  249. if (bus->started)
  250. bus->socket_ops->stop(bus->sfp);
  251. if (bus->phydev && ops && ops->disconnect_phy)
  252. ops->disconnect_phy(bus->upstream);
  253. }
  254. bus->registered = false;
  255. }
  256. int sfp_get_module_info(struct sfp_bus *bus, struct ethtool_modinfo *modinfo)
  257. {
  258. if (!bus->registered)
  259. return -ENOIOCTLCMD;
  260. return bus->socket_ops->module_info(bus->sfp, modinfo);
  261. }
  262. EXPORT_SYMBOL_GPL(sfp_get_module_info);
  263. int sfp_get_module_eeprom(struct sfp_bus *bus, struct ethtool_eeprom *ee,
  264. u8 *data)
  265. {
  266. if (!bus->registered)
  267. return -ENOIOCTLCMD;
  268. return bus->socket_ops->module_eeprom(bus->sfp, ee, data);
  269. }
  270. EXPORT_SYMBOL_GPL(sfp_get_module_eeprom);
  271. void sfp_upstream_start(struct sfp_bus *bus)
  272. {
  273. if (bus->registered)
  274. bus->socket_ops->start(bus->sfp);
  275. bus->started = true;
  276. }
  277. EXPORT_SYMBOL_GPL(sfp_upstream_start);
  278. void sfp_upstream_stop(struct sfp_bus *bus)
  279. {
  280. if (bus->registered)
  281. bus->socket_ops->stop(bus->sfp);
  282. bus->started = false;
  283. }
  284. EXPORT_SYMBOL_GPL(sfp_upstream_stop);
  285. struct sfp_bus *sfp_register_upstream(struct device_node *np,
  286. struct net_device *ndev, void *upstream,
  287. const struct sfp_upstream_ops *ops)
  288. {
  289. struct sfp_bus *bus = sfp_bus_get(np);
  290. int ret = 0;
  291. if (bus) {
  292. rtnl_lock();
  293. bus->upstream_ops = ops;
  294. bus->upstream = upstream;
  295. bus->netdev = ndev;
  296. if (bus->sfp)
  297. ret = sfp_register_bus(bus);
  298. rtnl_unlock();
  299. }
  300. if (ret) {
  301. sfp_bus_put(bus);
  302. bus = NULL;
  303. }
  304. return bus;
  305. }
  306. EXPORT_SYMBOL_GPL(sfp_register_upstream);
  307. void sfp_unregister_upstream(struct sfp_bus *bus)
  308. {
  309. rtnl_lock();
  310. sfp_unregister_bus(bus);
  311. bus->upstream = NULL;
  312. bus->netdev = NULL;
  313. rtnl_unlock();
  314. sfp_bus_put(bus);
  315. }
  316. EXPORT_SYMBOL_GPL(sfp_unregister_upstream);
  317. /* Socket driver entry points */
  318. int sfp_add_phy(struct sfp_bus *bus, struct phy_device *phydev)
  319. {
  320. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  321. int ret = 0;
  322. if (ops && ops->connect_phy)
  323. ret = ops->connect_phy(bus->upstream, phydev);
  324. if (ret == 0)
  325. bus->phydev = phydev;
  326. return ret;
  327. }
  328. EXPORT_SYMBOL_GPL(sfp_add_phy);
  329. void sfp_remove_phy(struct sfp_bus *bus)
  330. {
  331. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  332. if (ops && ops->disconnect_phy)
  333. ops->disconnect_phy(bus->upstream);
  334. bus->phydev = NULL;
  335. }
  336. EXPORT_SYMBOL_GPL(sfp_remove_phy);
  337. void sfp_link_up(struct sfp_bus *bus)
  338. {
  339. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  340. if (ops && ops->link_up)
  341. ops->link_up(bus->upstream);
  342. }
  343. EXPORT_SYMBOL_GPL(sfp_link_up);
  344. void sfp_link_down(struct sfp_bus *bus)
  345. {
  346. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  347. if (ops && ops->link_down)
  348. ops->link_down(bus->upstream);
  349. }
  350. EXPORT_SYMBOL_GPL(sfp_link_down);
  351. int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id)
  352. {
  353. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  354. int ret = 0;
  355. if (ops && ops->module_insert)
  356. ret = ops->module_insert(bus->upstream, id);
  357. return ret;
  358. }
  359. EXPORT_SYMBOL_GPL(sfp_module_insert);
  360. void sfp_module_remove(struct sfp_bus *bus)
  361. {
  362. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  363. if (ops && ops->module_remove)
  364. ops->module_remove(bus->upstream);
  365. }
  366. EXPORT_SYMBOL_GPL(sfp_module_remove);
  367. struct sfp_bus *sfp_register_socket(struct device *dev, struct sfp *sfp,
  368. const struct sfp_socket_ops *ops)
  369. {
  370. struct sfp_bus *bus = sfp_bus_get(dev->of_node);
  371. int ret = 0;
  372. if (bus) {
  373. rtnl_lock();
  374. bus->sfp_dev = dev;
  375. bus->sfp = sfp;
  376. bus->socket_ops = ops;
  377. if (bus->netdev)
  378. ret = sfp_register_bus(bus);
  379. rtnl_unlock();
  380. }
  381. if (ret) {
  382. sfp_bus_put(bus);
  383. bus = NULL;
  384. }
  385. return bus;
  386. }
  387. EXPORT_SYMBOL_GPL(sfp_register_socket);
  388. void sfp_unregister_socket(struct sfp_bus *bus)
  389. {
  390. rtnl_lock();
  391. sfp_unregister_bus(bus);
  392. bus->sfp_dev = NULL;
  393. bus->sfp = NULL;
  394. bus->socket_ops = NULL;
  395. rtnl_unlock();
  396. sfp_bus_put(bus);
  397. }
  398. EXPORT_SYMBOL_GPL(sfp_unregister_socket);