204-net-igb-register-mii_bus-for-SerDes-w-external-phy.patch 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. From 03855caf93f7332a3f320228ba1a0e7baae8a749 Mon Sep 17 00:00:00 2001
  2. From: Tim Harvey <tharvey@gateworks.com>
  3. Date: Thu, 15 May 2014 12:36:23 -0700
  4. Subject: [PATCH] net: igb: register mii_bus for SerDes w/ external phy
  5. If an i210 is configured for 1000BASE-BX link_mode and has an external phy
  6. specified, then register an mii bus using the external phy address as
  7. a mask.
  8. An i210 hooked to an external standard phy will be configured with a link_mo
  9. of SGMII in which case phy ops will be configured and used internall in the
  10. igb driver for link status. However, in certain cases one might be using a
  11. backplane SerDes connection to something that talks on the mdio bus but is
  12. not a standard phy, such as a switch. In this case by registering an mdio
  13. bus a phy driver can manage the device.
  14. Signed-off-by: Tim Harvey <tharvey@gateworks.com>
  15. ---
  16. drivers/net/ethernet/intel/igb/e1000_82575.c | 15 +++
  17. drivers/net/ethernet/intel/igb/e1000_hw.h | 7 ++
  18. drivers/net/ethernet/intel/igb/igb_main.c | 168 ++++++++++++++++++++++++++-
  19. 3 files changed, 185 insertions(+), 5 deletions(-)
  20. --- a/drivers/net/ethernet/intel/igb/e1000_82575.c
  21. +++ b/drivers/net/ethernet/intel/igb/e1000_82575.c
  22. @@ -613,13 +613,25 @@ static s32 igb_get_invariants_82575(stru
  23. switch (link_mode) {
  24. case E1000_CTRL_EXT_LINK_MODE_1000BASE_KX:
  25. hw->phy.media_type = e1000_media_type_internal_serdes;
  26. + if (igb_sgmii_uses_mdio_82575(hw)) {
  27. + u32 mdicnfg = rd32(E1000_MDICNFG);
  28. + mdicnfg &= E1000_MDICNFG_PHY_MASK;
  29. + hw->phy.addr = mdicnfg >> E1000_MDICNFG_PHY_SHIFT;
  30. + hw_dbg("1000BASE_KX w/ external MDIO device at 0x%x\n",
  31. + hw->phy.addr);
  32. + } else {
  33. + hw_dbg("1000BASE_KX");
  34. + }
  35. break;
  36. case E1000_CTRL_EXT_LINK_MODE_SGMII:
  37. /* Get phy control interface type set (MDIO vs. I2C)*/
  38. if (igb_sgmii_uses_mdio_82575(hw)) {
  39. hw->phy.media_type = e1000_media_type_copper;
  40. dev_spec->sgmii_active = true;
  41. + hw_dbg("SGMII with external MDIO PHY");
  42. break;
  43. + } else {
  44. + hw_dbg("SGMII with external I2C PHY");
  45. }
  46. /* fall through for I2C based SGMII */
  47. case E1000_CTRL_EXT_LINK_MODE_PCIE_SERDES:
  48. @@ -636,8 +648,11 @@ static s32 igb_get_invariants_82575(stru
  49. hw->phy.media_type = e1000_media_type_copper;
  50. dev_spec->sgmii_active = true;
  51. }
  52. + hw_dbg("SERDES with external SFP");
  53. break;
  54. + } else {
  55. + hw_dbg("SERDES");
  56. }
  57. /* do not change link mode for 100BaseFX */
  58. --- a/drivers/net/ethernet/intel/igb/e1000_hw.h
  59. +++ b/drivers/net/ethernet/intel/igb/e1000_hw.h
  60. @@ -27,6 +27,7 @@
  61. #include <linux/delay.h>
  62. #include <linux/io.h>
  63. #include <linux/netdevice.h>
  64. +#include <linux/phy.h>
  65. #include "e1000_regs.h"
  66. #include "e1000_defines.h"
  67. @@ -543,6 +544,12 @@ struct e1000_hw {
  68. struct e1000_mbx_info mbx;
  69. struct e1000_host_mng_dhcp_cookie mng_cookie;
  70. +#ifdef CONFIG_PHYLIB
  71. + /* Phylib and MDIO interface */
  72. + struct mii_bus *mii_bus;
  73. + struct phy_device *phy_dev;
  74. + phy_interface_t phy_interface;
  75. +#endif
  76. union {
  77. struct e1000_dev_spec_82575 _82575;
  78. } dev_spec;
  79. --- a/drivers/net/ethernet/intel/igb/igb_main.c
  80. +++ b/drivers/net/ethernet/intel/igb/igb_main.c
  81. @@ -41,6 +41,7 @@
  82. #include <linux/if_vlan.h>
  83. #include <linux/pci.h>
  84. #include <linux/pci-aspm.h>
  85. +#include <linux/phy.h>
  86. #include <linux/delay.h>
  87. #include <linux/interrupt.h>
  88. #include <linux/ip.h>
  89. @@ -2217,6 +2218,126 @@ static s32 igb_init_i2c(struct igb_adapt
  90. return status;
  91. }
  92. +
  93. +#ifdef CONFIG_PHYLIB
  94. +/*
  95. + * MMIO/PHYdev support
  96. + */
  97. +
  98. +static int igb_enet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  99. +{
  100. + struct e1000_hw *hw = bus->priv;
  101. + u16 out;
  102. + int err;
  103. +
  104. + err = igb_read_reg_gs40g(hw, mii_id, regnum, &out);
  105. + if (err)
  106. + return err;
  107. + return out;
  108. +}
  109. +
  110. +static int igb_enet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
  111. + u16 val)
  112. +{
  113. + struct e1000_hw *hw = bus->priv;
  114. +
  115. + return igb_write_reg_gs40g(hw, mii_id, regnum, val);
  116. +}
  117. +
  118. +static int igb_enet_mdio_reset(struct mii_bus *bus)
  119. +{
  120. + udelay(300);
  121. + return 0;
  122. +}
  123. +
  124. +static void igb_enet_mii_link(struct net_device *netdev)
  125. +{
  126. +}
  127. +
  128. +/* Probe the mdio bus for phys and connect them */
  129. +static int igb_enet_mii_probe(struct net_device *netdev)
  130. +{
  131. + struct igb_adapter *adapter = netdev_priv(netdev);
  132. + struct e1000_hw *hw = &adapter->hw;
  133. + struct phy_device *phy_dev = NULL;
  134. + int phy_id;
  135. +
  136. + /* check for attached phy */
  137. + for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) {
  138. + if (hw->mii_bus->phy_map[phy_id]) {
  139. + phy_dev = hw->mii_bus->phy_map[phy_id];
  140. + break;
  141. + }
  142. + }
  143. + if (!phy_dev) {
  144. + netdev_err(netdev, "no PHY found\n");
  145. + return -ENODEV;
  146. + }
  147. +
  148. + hw->phy_interface = PHY_INTERFACE_MODE_RGMII;
  149. + phy_dev = phy_connect(netdev, dev_name(&phy_dev->dev),
  150. + igb_enet_mii_link, hw->phy_interface);
  151. + if (IS_ERR(phy_dev)) {
  152. + netdev_err(netdev, "could not attach to PHY\n");
  153. + return PTR_ERR(phy_dev);
  154. + }
  155. +
  156. + hw->phy_dev = phy_dev;
  157. + netdev_info(netdev, "igb PHY driver [%s] (mii_bus:phy_addr=%s)\n",
  158. + hw->phy_dev->drv->name, dev_name(&hw->phy_dev->dev));
  159. +
  160. + return 0;
  161. +}
  162. +
  163. +/* Create and register mdio bus */
  164. +static int igb_enet_mii_init(struct pci_dev *pdev)
  165. +{
  166. + struct mii_bus *mii_bus;
  167. + struct net_device *netdev = pci_get_drvdata(pdev);
  168. + struct igb_adapter *adapter = netdev_priv(netdev);
  169. + struct e1000_hw *hw = &adapter->hw;
  170. + int err;
  171. +
  172. + mii_bus = mdiobus_alloc();
  173. + if (mii_bus == NULL) {
  174. + err = -ENOMEM;
  175. + goto err_out;
  176. + }
  177. +
  178. + mii_bus->name = "igb_enet_mii_bus";
  179. + mii_bus->read = igb_enet_mdio_read;
  180. + mii_bus->write = igb_enet_mdio_write;
  181. + mii_bus->reset = igb_enet_mdio_reset;
  182. + snprintf(mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  183. + pci_name(pdev), hw->device_id + 1);
  184. + mii_bus->priv = hw;
  185. + mii_bus->parent = &pdev->dev;
  186. + mii_bus->phy_mask = ~(1 << hw->phy.addr);
  187. +
  188. + err = mdiobus_register(mii_bus);
  189. + if (err) {
  190. + printk(KERN_ERR "failed to register mii_bus: %d\n", err);
  191. + goto err_out_free_mdiobus;
  192. + }
  193. + hw->mii_bus = mii_bus;
  194. +
  195. + return 0;
  196. +
  197. +err_out_free_mdiobus:
  198. + mdiobus_free(mii_bus);
  199. +err_out:
  200. + return err;
  201. +}
  202. +
  203. +static void igb_enet_mii_remove(struct e1000_hw *hw)
  204. +{
  205. + if (hw->mii_bus) {
  206. + mdiobus_unregister(hw->mii_bus);
  207. + mdiobus_free(hw->mii_bus);
  208. + }
  209. +}
  210. +#endif /* CONFIG_PHYLIB */
  211. +
  212. /**
  213. * igb_probe - Device Initialization Routine
  214. * @pdev: PCI device information struct
  215. @@ -2641,6 +2762,13 @@ static int igb_probe(struct pci_dev *pde
  216. }
  217. }
  218. pm_runtime_put_noidle(&pdev->dev);
  219. +
  220. +#ifdef CONFIG_PHYLIB
  221. + /* create and register the mdio bus if using ext phy */
  222. + if (rd32(E1000_MDICNFG) & E1000_MDICNFG_EXT_MDIO)
  223. + igb_enet_mii_init(pdev);
  224. +#endif
  225. +
  226. return 0;
  227. err_register:
  228. @@ -2788,6 +2916,10 @@ static void igb_remove(struct pci_dev *p
  229. struct e1000_hw *hw = &adapter->hw;
  230. pm_runtime_get_noresume(&pdev->dev);
  231. +#ifdef CONFIG_PHYLIB
  232. + if (rd32(E1000_MDICNFG) & E1000_MDICNFG_EXT_MDIO)
  233. + igb_enet_mii_remove(hw);
  234. +#endif
  235. #ifdef CONFIG_IGB_HWMON
  236. igb_sysfs_exit(adapter);
  237. #endif
  238. @@ -3113,6 +3245,12 @@ static int __igb_open(struct net_device
  239. if (!resuming)
  240. pm_runtime_put(&pdev->dev);
  241. +#ifdef CONFIG_PHYLIB
  242. + /* Probe and connect to PHY if using ext phy */
  243. + if (rd32(E1000_MDICNFG) & E1000_MDICNFG_EXT_MDIO)
  244. + igb_enet_mii_probe(netdev);
  245. +#endif
  246. +
  247. /* start the watchdog. */
  248. hw->mac.get_link_status = 1;
  249. schedule_work(&adapter->watchdog_task);
  250. @@ -7106,21 +7244,41 @@ void igb_alloc_rx_buffers(struct igb_rin
  251. static int igb_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  252. {
  253. struct igb_adapter *adapter = netdev_priv(netdev);
  254. + struct e1000_hw *hw = &adapter->hw;
  255. struct mii_ioctl_data *data = if_mii(ifr);
  256. - if (adapter->hw.phy.media_type != e1000_media_type_copper)
  257. + if (adapter->hw.phy.media_type != e1000_media_type_copper &&
  258. + !(rd32(E1000_MDICNFG) & E1000_MDICNFG_EXT_MDIO))
  259. return -EOPNOTSUPP;
  260. switch (cmd) {
  261. case SIOCGMIIPHY:
  262. - data->phy_id = adapter->hw.phy.addr;
  263. + data->phy_id = hw->phy.addr;
  264. break;
  265. case SIOCGMIIREG:
  266. - if (igb_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
  267. - &data->val_out))
  268. - return -EIO;
  269. + if (hw->mac.type == e1000_i210 || hw->mac.type == e1000_i211) {
  270. + if (igb_read_reg_gs40g(&adapter->hw, data->phy_id,
  271. + data->reg_num & 0x1F,
  272. + &data->val_out))
  273. + return -EIO;
  274. + } else {
  275. + if (igb_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
  276. + &data->val_out))
  277. + return -EIO;
  278. + }
  279. break;
  280. case SIOCSMIIREG:
  281. + if (hw->mac.type == e1000_i210 || hw->mac.type == e1000_i211) {
  282. + if (igb_write_reg_gs40g(hw, data->phy_id,
  283. + data->reg_num & 0x1F,
  284. + data->val_in))
  285. + return -EIO;
  286. + } else {
  287. + if (igb_write_phy_reg(hw, data->reg_num & 0x1F,
  288. + data->val_in))
  289. + return -EIO;
  290. + }
  291. + break;
  292. default:
  293. return -EOPNOTSUPP;
  294. }