xgbe-main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * AMD 10Gb Ethernet driver
  3. *
  4. * This file is available to you under your choice of the following two
  5. * licenses:
  6. *
  7. * License 1: GPLv2
  8. *
  9. * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
  10. *
  11. * This file is free software; you may copy, redistribute and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 2 of the License, or (at
  14. * your option) any later version.
  15. *
  16. * This file is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. * This file incorporates work covered by the following copyright and
  25. * permission notice:
  26. * The Synopsys DWC ETHER XGMAC Software Driver and documentation
  27. * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
  28. * Inc. unless otherwise expressly agreed to in writing between Synopsys
  29. * and you.
  30. *
  31. * The Software IS NOT an item of Licensed Software or Licensed Product
  32. * under any End User Software License Agreement or Agreement for Licensed
  33. * Product with Synopsys or any supplement thereto. Permission is hereby
  34. * granted, free of charge, to any person obtaining a copy of this software
  35. * annotated with this license and the Software, to deal in the Software
  36. * without restriction, including without limitation the rights to use,
  37. * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  38. * of the Software, and to permit persons to whom the Software is furnished
  39. * to do so, subject to the following conditions:
  40. *
  41. * The above copyright notice and this permission notice shall be included
  42. * in all copies or substantial portions of the Software.
  43. *
  44. * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  45. * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  46. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  47. * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
  48. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  49. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  50. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  51. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  52. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  53. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  54. * THE POSSIBILITY OF SUCH DAMAGE.
  55. *
  56. *
  57. * License 2: Modified BSD
  58. *
  59. * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
  60. * All rights reserved.
  61. *
  62. * Redistribution and use in source and binary forms, with or without
  63. * modification, are permitted provided that the following conditions are met:
  64. * * Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. * * Redistributions in binary form must reproduce the above copyright
  67. * notice, this list of conditions and the following disclaimer in the
  68. * documentation and/or other materials provided with the distribution.
  69. * * Neither the name of Advanced Micro Devices, Inc. nor the
  70. * names of its contributors may be used to endorse or promote products
  71. * derived from this software without specific prior written permission.
  72. *
  73. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  74. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  75. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  76. * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  77. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  78. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  79. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  80. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  81. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  82. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  83. *
  84. * This file incorporates work covered by the following copyright and
  85. * permission notice:
  86. * The Synopsys DWC ETHER XGMAC Software Driver and documentation
  87. * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
  88. * Inc. unless otherwise expressly agreed to in writing between Synopsys
  89. * and you.
  90. *
  91. * The Software IS NOT an item of Licensed Software or Licensed Product
  92. * under any End User Software License Agreement or Agreement for Licensed
  93. * Product with Synopsys or any supplement thereto. Permission is hereby
  94. * granted, free of charge, to any person obtaining a copy of this software
  95. * annotated with this license and the Software, to deal in the Software
  96. * without restriction, including without limitation the rights to use,
  97. * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  98. * of the Software, and to permit persons to whom the Software is furnished
  99. * to do so, subject to the following conditions:
  100. *
  101. * The above copyright notice and this permission notice shall be included
  102. * in all copies or substantial portions of the Software.
  103. *
  104. * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  105. * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  106. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  107. * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
  108. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  109. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  110. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  111. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  112. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  113. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  114. * THE POSSIBILITY OF SUCH DAMAGE.
  115. */
  116. #include <linux/module.h>
  117. #include <linux/device.h>
  118. #include <linux/spinlock.h>
  119. #include <linux/netdevice.h>
  120. #include <linux/etherdevice.h>
  121. #include <linux/io.h>
  122. #include <linux/notifier.h>
  123. #include "xgbe.h"
  124. #include "xgbe-common.h"
  125. MODULE_AUTHOR("Tom Lendacky <[email protected]>");
  126. MODULE_LICENSE("Dual BSD/GPL");
  127. MODULE_DESCRIPTION(XGBE_DRV_DESC);
  128. static int debug = -1;
  129. module_param(debug, int, 0644);
  130. MODULE_PARM_DESC(debug, " Network interface message level setting");
  131. static const u32 default_msg_level = (NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
  132. NETIF_MSG_IFUP);
  133. static void xgbe_default_config(struct xgbe_prv_data *pdata)
  134. {
  135. DBGPR("-->xgbe_default_config\n");
  136. pdata->blen = DMA_SBMR_BLEN_64;
  137. pdata->pbl = DMA_PBL_128;
  138. pdata->aal = 1;
  139. pdata->rd_osr_limit = 8;
  140. pdata->wr_osr_limit = 8;
  141. pdata->tx_sf_mode = MTL_TSF_ENABLE;
  142. pdata->tx_threshold = MTL_TX_THRESHOLD_64;
  143. pdata->tx_osp_mode = DMA_OSP_ENABLE;
  144. pdata->rx_sf_mode = MTL_RSF_DISABLE;
  145. pdata->rx_threshold = MTL_RX_THRESHOLD_64;
  146. pdata->pause_autoneg = 1;
  147. pdata->tx_pause = 1;
  148. pdata->rx_pause = 1;
  149. pdata->phy_speed = SPEED_UNKNOWN;
  150. pdata->power_down = 0;
  151. DBGPR("<--xgbe_default_config\n");
  152. }
  153. static void xgbe_init_all_fptrs(struct xgbe_prv_data *pdata)
  154. {
  155. xgbe_init_function_ptrs_dev(&pdata->hw_if);
  156. xgbe_init_function_ptrs_phy(&pdata->phy_if);
  157. xgbe_init_function_ptrs_i2c(&pdata->i2c_if);
  158. xgbe_init_function_ptrs_desc(&pdata->desc_if);
  159. pdata->vdata->init_function_ptrs_phy_impl(&pdata->phy_if);
  160. }
  161. struct xgbe_prv_data *xgbe_alloc_pdata(struct device *dev)
  162. {
  163. struct xgbe_prv_data *pdata;
  164. struct net_device *netdev;
  165. netdev = alloc_etherdev_mq(sizeof(struct xgbe_prv_data),
  166. XGBE_MAX_DMA_CHANNELS);
  167. if (!netdev) {
  168. dev_err(dev, "alloc_etherdev_mq failed\n");
  169. return ERR_PTR(-ENOMEM);
  170. }
  171. SET_NETDEV_DEV(netdev, dev);
  172. pdata = netdev_priv(netdev);
  173. pdata->netdev = netdev;
  174. pdata->dev = dev;
  175. spin_lock_init(&pdata->lock);
  176. spin_lock_init(&pdata->xpcs_lock);
  177. mutex_init(&pdata->rss_mutex);
  178. spin_lock_init(&pdata->tstamp_lock);
  179. mutex_init(&pdata->i2c_mutex);
  180. init_completion(&pdata->i2c_complete);
  181. init_completion(&pdata->mdio_complete);
  182. pdata->msg_enable = netif_msg_init(debug, default_msg_level);
  183. set_bit(XGBE_DOWN, &pdata->dev_state);
  184. set_bit(XGBE_STOPPED, &pdata->dev_state);
  185. return pdata;
  186. }
  187. void xgbe_free_pdata(struct xgbe_prv_data *pdata)
  188. {
  189. struct net_device *netdev = pdata->netdev;
  190. free_netdev(netdev);
  191. }
  192. void xgbe_set_counts(struct xgbe_prv_data *pdata)
  193. {
  194. /* Set all the function pointers */
  195. xgbe_init_all_fptrs(pdata);
  196. /* Populate the hardware features */
  197. xgbe_get_all_hw_features(pdata);
  198. /* Set default max values if not provided */
  199. if (!pdata->tx_max_channel_count)
  200. pdata->tx_max_channel_count = pdata->hw_feat.tx_ch_cnt;
  201. if (!pdata->rx_max_channel_count)
  202. pdata->rx_max_channel_count = pdata->hw_feat.rx_ch_cnt;
  203. if (!pdata->tx_max_q_count)
  204. pdata->tx_max_q_count = pdata->hw_feat.tx_q_cnt;
  205. if (!pdata->rx_max_q_count)
  206. pdata->rx_max_q_count = pdata->hw_feat.rx_q_cnt;
  207. /* Calculate the number of Tx and Rx rings to be created
  208. * -Tx (DMA) Channels map 1-to-1 to Tx Queues so set
  209. * the number of Tx queues to the number of Tx channels
  210. * enabled
  211. * -Rx (DMA) Channels do not map 1-to-1 so use the actual
  212. * number of Rx queues or maximum allowed
  213. */
  214. pdata->tx_ring_count = min_t(unsigned int, num_online_cpus(),
  215. pdata->hw_feat.tx_ch_cnt);
  216. pdata->tx_ring_count = min_t(unsigned int, pdata->tx_ring_count,
  217. pdata->tx_max_channel_count);
  218. pdata->tx_ring_count = min_t(unsigned int, pdata->tx_ring_count,
  219. pdata->tx_max_q_count);
  220. pdata->tx_q_count = pdata->tx_ring_count;
  221. pdata->rx_ring_count = min_t(unsigned int, num_online_cpus(),
  222. pdata->hw_feat.rx_ch_cnt);
  223. pdata->rx_ring_count = min_t(unsigned int, pdata->rx_ring_count,
  224. pdata->rx_max_channel_count);
  225. pdata->rx_q_count = min_t(unsigned int, pdata->hw_feat.rx_q_cnt,
  226. pdata->rx_max_q_count);
  227. if (netif_msg_probe(pdata)) {
  228. dev_dbg(pdata->dev, "TX/RX DMA channel count = %u/%u\n",
  229. pdata->tx_ring_count, pdata->rx_ring_count);
  230. dev_dbg(pdata->dev, "TX/RX hardware queue count = %u/%u\n",
  231. pdata->tx_q_count, pdata->rx_q_count);
  232. }
  233. }
  234. int xgbe_config_netdev(struct xgbe_prv_data *pdata)
  235. {
  236. struct net_device *netdev = pdata->netdev;
  237. struct device *dev = pdata->dev;
  238. int ret;
  239. netdev->irq = pdata->dev_irq;
  240. netdev->base_addr = (unsigned long)pdata->xgmac_regs;
  241. eth_hw_addr_set(netdev, pdata->mac_addr);
  242. /* Initialize ECC timestamps */
  243. pdata->tx_sec_period = jiffies;
  244. pdata->tx_ded_period = jiffies;
  245. pdata->rx_sec_period = jiffies;
  246. pdata->rx_ded_period = jiffies;
  247. pdata->desc_sec_period = jiffies;
  248. pdata->desc_ded_period = jiffies;
  249. /* Issue software reset to device */
  250. ret = pdata->hw_if.exit(pdata);
  251. if (ret) {
  252. dev_err(dev, "software reset failed\n");
  253. return ret;
  254. }
  255. /* Set default configuration data */
  256. xgbe_default_config(pdata);
  257. /* Set the DMA mask */
  258. ret = dma_set_mask_and_coherent(dev,
  259. DMA_BIT_MASK(pdata->hw_feat.dma_width));
  260. if (ret) {
  261. dev_err(dev, "dma_set_mask_and_coherent failed\n");
  262. return ret;
  263. }
  264. /* Set default max values if not provided */
  265. if (!pdata->tx_max_fifo_size)
  266. pdata->tx_max_fifo_size = pdata->hw_feat.tx_fifo_size;
  267. if (!pdata->rx_max_fifo_size)
  268. pdata->rx_max_fifo_size = pdata->hw_feat.rx_fifo_size;
  269. /* Set and validate the number of descriptors for a ring */
  270. BUILD_BUG_ON_NOT_POWER_OF_2(XGBE_TX_DESC_CNT);
  271. pdata->tx_desc_count = XGBE_TX_DESC_CNT;
  272. BUILD_BUG_ON_NOT_POWER_OF_2(XGBE_RX_DESC_CNT);
  273. pdata->rx_desc_count = XGBE_RX_DESC_CNT;
  274. /* Adjust the number of queues based on interrupts assigned */
  275. if (pdata->channel_irq_count) {
  276. pdata->tx_ring_count = min_t(unsigned int, pdata->tx_ring_count,
  277. pdata->channel_irq_count);
  278. pdata->rx_ring_count = min_t(unsigned int, pdata->rx_ring_count,
  279. pdata->channel_irq_count);
  280. if (netif_msg_probe(pdata))
  281. dev_dbg(pdata->dev,
  282. "adjusted TX/RX DMA channel count = %u/%u\n",
  283. pdata->tx_ring_count, pdata->rx_ring_count);
  284. }
  285. /* Initialize RSS hash key */
  286. netdev_rss_key_fill(pdata->rss_key, sizeof(pdata->rss_key));
  287. XGMAC_SET_BITS(pdata->rss_options, MAC_RSSCR, IP2TE, 1);
  288. XGMAC_SET_BITS(pdata->rss_options, MAC_RSSCR, TCP4TE, 1);
  289. XGMAC_SET_BITS(pdata->rss_options, MAC_RSSCR, UDP4TE, 1);
  290. /* Call MDIO/PHY initialization routine */
  291. pdata->debugfs_an_cdr_workaround = pdata->vdata->an_cdr_workaround;
  292. ret = pdata->phy_if.phy_init(pdata);
  293. if (ret)
  294. return ret;
  295. /* Set device operations */
  296. netdev->netdev_ops = xgbe_get_netdev_ops();
  297. netdev->ethtool_ops = xgbe_get_ethtool_ops();
  298. #ifdef CONFIG_AMD_XGBE_DCB
  299. netdev->dcbnl_ops = xgbe_get_dcbnl_ops();
  300. #endif
  301. /* Set device features */
  302. netdev->hw_features = NETIF_F_SG |
  303. NETIF_F_IP_CSUM |
  304. NETIF_F_IPV6_CSUM |
  305. NETIF_F_RXCSUM |
  306. NETIF_F_TSO |
  307. NETIF_F_TSO6 |
  308. NETIF_F_GRO |
  309. NETIF_F_HW_VLAN_CTAG_RX |
  310. NETIF_F_HW_VLAN_CTAG_TX |
  311. NETIF_F_HW_VLAN_CTAG_FILTER;
  312. if (pdata->hw_feat.rss)
  313. netdev->hw_features |= NETIF_F_RXHASH;
  314. if (pdata->hw_feat.vxn) {
  315. netdev->hw_enc_features = NETIF_F_SG |
  316. NETIF_F_IP_CSUM |
  317. NETIF_F_IPV6_CSUM |
  318. NETIF_F_RXCSUM |
  319. NETIF_F_TSO |
  320. NETIF_F_TSO6 |
  321. NETIF_F_GRO |
  322. NETIF_F_GSO_UDP_TUNNEL |
  323. NETIF_F_GSO_UDP_TUNNEL_CSUM;
  324. netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
  325. NETIF_F_GSO_UDP_TUNNEL_CSUM;
  326. netdev->udp_tunnel_nic_info = xgbe_get_udp_tunnel_info();
  327. }
  328. netdev->vlan_features |= NETIF_F_SG |
  329. NETIF_F_IP_CSUM |
  330. NETIF_F_IPV6_CSUM |
  331. NETIF_F_TSO |
  332. NETIF_F_TSO6;
  333. netdev->features |= netdev->hw_features;
  334. pdata->netdev_features = netdev->features;
  335. netdev->priv_flags |= IFF_UNICAST_FLT;
  336. netdev->min_mtu = 0;
  337. netdev->max_mtu = XGMAC_JUMBO_PACKET_MTU;
  338. /* Use default watchdog timeout */
  339. netdev->watchdog_timeo = 0;
  340. xgbe_init_rx_coalesce(pdata);
  341. xgbe_init_tx_coalesce(pdata);
  342. netif_carrier_off(netdev);
  343. ret = register_netdev(netdev);
  344. if (ret) {
  345. dev_err(dev, "net device registration failed\n");
  346. return ret;
  347. }
  348. if (IS_REACHABLE(CONFIG_PTP_1588_CLOCK))
  349. xgbe_ptp_register(pdata);
  350. xgbe_debugfs_init(pdata);
  351. netif_dbg(pdata, drv, pdata->netdev, "%u Tx software queues\n",
  352. pdata->tx_ring_count);
  353. netif_dbg(pdata, drv, pdata->netdev, "%u Rx software queues\n",
  354. pdata->rx_ring_count);
  355. return 0;
  356. }
  357. void xgbe_deconfig_netdev(struct xgbe_prv_data *pdata)
  358. {
  359. struct net_device *netdev = pdata->netdev;
  360. xgbe_debugfs_exit(pdata);
  361. if (IS_REACHABLE(CONFIG_PTP_1588_CLOCK))
  362. xgbe_ptp_unregister(pdata);
  363. unregister_netdev(netdev);
  364. pdata->phy_if.phy_exit(pdata);
  365. }
  366. static int xgbe_netdev_event(struct notifier_block *nb, unsigned long event,
  367. void *data)
  368. {
  369. struct net_device *netdev = netdev_notifier_info_to_dev(data);
  370. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  371. if (netdev->netdev_ops != xgbe_get_netdev_ops())
  372. goto out;
  373. switch (event) {
  374. case NETDEV_CHANGENAME:
  375. xgbe_debugfs_rename(pdata);
  376. break;
  377. default:
  378. break;
  379. }
  380. out:
  381. return NOTIFY_DONE;
  382. }
  383. static struct notifier_block xgbe_netdev_notifier = {
  384. .notifier_call = xgbe_netdev_event,
  385. };
  386. static int __init xgbe_mod_init(void)
  387. {
  388. int ret;
  389. ret = register_netdevice_notifier(&xgbe_netdev_notifier);
  390. if (ret)
  391. return ret;
  392. ret = xgbe_platform_init();
  393. if (ret)
  394. goto err_platform_init;
  395. ret = xgbe_pci_init();
  396. if (ret)
  397. goto err_pci_init;
  398. return 0;
  399. err_pci_init:
  400. xgbe_platform_exit();
  401. err_platform_init:
  402. unregister_netdevice_notifier(&xgbe_netdev_notifier);
  403. return ret;
  404. }
  405. static void __exit xgbe_mod_exit(void)
  406. {
  407. xgbe_pci_exit();
  408. xgbe_platform_exit();
  409. unregister_netdevice_notifier(&xgbe_netdev_notifier);
  410. }
  411. module_init(xgbe_mod_init);
  412. module_exit(xgbe_mod_exit);