xgbe-main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 "xgbe.h"
  123. #include "xgbe-common.h"
  124. MODULE_AUTHOR("Tom Lendacky <thomas.lendacky@amd.com>");
  125. MODULE_LICENSE("Dual BSD/GPL");
  126. MODULE_VERSION(XGBE_DRV_VERSION);
  127. MODULE_DESCRIPTION(XGBE_DRV_DESC);
  128. static int debug = -1;
  129. module_param(debug, int, S_IWUSR | S_IRUGO);
  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->pblx8 = DMA_PBL_X8_ENABLE;
  137. pdata->tx_sf_mode = MTL_TSF_ENABLE;
  138. pdata->tx_threshold = MTL_TX_THRESHOLD_64;
  139. pdata->tx_pbl = DMA_PBL_16;
  140. pdata->tx_osp_mode = DMA_OSP_ENABLE;
  141. pdata->rx_sf_mode = MTL_RSF_DISABLE;
  142. pdata->rx_threshold = MTL_RX_THRESHOLD_64;
  143. pdata->rx_pbl = DMA_PBL_16;
  144. pdata->pause_autoneg = 1;
  145. pdata->tx_pause = 1;
  146. pdata->rx_pause = 1;
  147. pdata->phy_speed = SPEED_UNKNOWN;
  148. pdata->power_down = 0;
  149. DBGPR("<--xgbe_default_config\n");
  150. }
  151. static void xgbe_init_all_fptrs(struct xgbe_prv_data *pdata)
  152. {
  153. xgbe_init_function_ptrs_dev(&pdata->hw_if);
  154. xgbe_init_function_ptrs_phy(&pdata->phy_if);
  155. xgbe_init_function_ptrs_i2c(&pdata->i2c_if);
  156. xgbe_init_function_ptrs_desc(&pdata->desc_if);
  157. pdata->vdata->init_function_ptrs_phy_impl(&pdata->phy_if);
  158. }
  159. struct xgbe_prv_data *xgbe_alloc_pdata(struct device *dev)
  160. {
  161. struct xgbe_prv_data *pdata;
  162. struct net_device *netdev;
  163. netdev = alloc_etherdev_mq(sizeof(struct xgbe_prv_data),
  164. XGBE_MAX_DMA_CHANNELS);
  165. if (!netdev) {
  166. dev_err(dev, "alloc_etherdev_mq failed\n");
  167. return ERR_PTR(-ENOMEM);
  168. }
  169. SET_NETDEV_DEV(netdev, dev);
  170. pdata = netdev_priv(netdev);
  171. pdata->netdev = netdev;
  172. pdata->dev = dev;
  173. spin_lock_init(&pdata->lock);
  174. spin_lock_init(&pdata->xpcs_lock);
  175. mutex_init(&pdata->rss_mutex);
  176. spin_lock_init(&pdata->tstamp_lock);
  177. mutex_init(&pdata->i2c_mutex);
  178. init_completion(&pdata->i2c_complete);
  179. init_completion(&pdata->mdio_complete);
  180. pdata->msg_enable = netif_msg_init(debug, default_msg_level);
  181. set_bit(XGBE_DOWN, &pdata->dev_state);
  182. set_bit(XGBE_STOPPED, &pdata->dev_state);
  183. return pdata;
  184. }
  185. void xgbe_free_pdata(struct xgbe_prv_data *pdata)
  186. {
  187. struct net_device *netdev = pdata->netdev;
  188. free_netdev(netdev);
  189. }
  190. void xgbe_set_counts(struct xgbe_prv_data *pdata)
  191. {
  192. /* Set all the function pointers */
  193. xgbe_init_all_fptrs(pdata);
  194. /* Populate the hardware features */
  195. xgbe_get_all_hw_features(pdata);
  196. /* Set default max values if not provided */
  197. if (!pdata->tx_max_channel_count)
  198. pdata->tx_max_channel_count = pdata->hw_feat.tx_ch_cnt;
  199. if (!pdata->rx_max_channel_count)
  200. pdata->rx_max_channel_count = pdata->hw_feat.rx_ch_cnt;
  201. if (!pdata->tx_max_q_count)
  202. pdata->tx_max_q_count = pdata->hw_feat.tx_q_cnt;
  203. if (!pdata->rx_max_q_count)
  204. pdata->rx_max_q_count = pdata->hw_feat.rx_q_cnt;
  205. /* Calculate the number of Tx and Rx rings to be created
  206. * -Tx (DMA) Channels map 1-to-1 to Tx Queues so set
  207. * the number of Tx queues to the number of Tx channels
  208. * enabled
  209. * -Rx (DMA) Channels do not map 1-to-1 so use the actual
  210. * number of Rx queues or maximum allowed
  211. */
  212. pdata->tx_ring_count = min_t(unsigned int, num_online_cpus(),
  213. pdata->hw_feat.tx_ch_cnt);
  214. pdata->tx_ring_count = min_t(unsigned int, pdata->tx_ring_count,
  215. pdata->tx_max_channel_count);
  216. pdata->tx_ring_count = min_t(unsigned int, pdata->tx_ring_count,
  217. pdata->tx_max_q_count);
  218. pdata->tx_q_count = pdata->tx_ring_count;
  219. pdata->rx_ring_count = min_t(unsigned int, num_online_cpus(),
  220. pdata->hw_feat.rx_ch_cnt);
  221. pdata->rx_ring_count = min_t(unsigned int, pdata->rx_ring_count,
  222. pdata->rx_max_channel_count);
  223. pdata->rx_q_count = min_t(unsigned int, pdata->hw_feat.rx_q_cnt,
  224. pdata->rx_max_q_count);
  225. if (netif_msg_probe(pdata)) {
  226. dev_dbg(pdata->dev, "TX/RX DMA channel count = %u/%u\n",
  227. pdata->tx_ring_count, pdata->rx_ring_count);
  228. dev_dbg(pdata->dev, "TX/RX hardware queue count = %u/%u\n",
  229. pdata->tx_q_count, pdata->rx_q_count);
  230. }
  231. }
  232. int xgbe_config_netdev(struct xgbe_prv_data *pdata)
  233. {
  234. struct net_device *netdev = pdata->netdev;
  235. struct device *dev = pdata->dev;
  236. unsigned int i;
  237. int ret;
  238. netdev->irq = pdata->dev_irq;
  239. netdev->base_addr = (unsigned long)pdata->xgmac_regs;
  240. memcpy(netdev->dev_addr, pdata->mac_addr, netdev->addr_len);
  241. /* Initialize ECC timestamps */
  242. pdata->tx_sec_period = jiffies;
  243. pdata->tx_ded_period = jiffies;
  244. pdata->rx_sec_period = jiffies;
  245. pdata->rx_ded_period = jiffies;
  246. pdata->desc_sec_period = jiffies;
  247. pdata->desc_ded_period = jiffies;
  248. /* Issue software reset to device */
  249. pdata->hw_if.exit(pdata);
  250. /* Set default configuration data */
  251. xgbe_default_config(pdata);
  252. /* Set the DMA mask */
  253. ret = dma_set_mask_and_coherent(dev,
  254. DMA_BIT_MASK(pdata->hw_feat.dma_width));
  255. if (ret) {
  256. dev_err(dev, "dma_set_mask_and_coherent failed\n");
  257. return ret;
  258. }
  259. /* Set default max values if not provided */
  260. if (!pdata->tx_max_fifo_size)
  261. pdata->tx_max_fifo_size = pdata->hw_feat.tx_fifo_size;
  262. if (!pdata->rx_max_fifo_size)
  263. pdata->rx_max_fifo_size = pdata->hw_feat.rx_fifo_size;
  264. /* Set and validate the number of descriptors for a ring */
  265. BUILD_BUG_ON_NOT_POWER_OF_2(XGBE_TX_DESC_CNT);
  266. pdata->tx_desc_count = XGBE_TX_DESC_CNT;
  267. BUILD_BUG_ON_NOT_POWER_OF_2(XGBE_RX_DESC_CNT);
  268. pdata->rx_desc_count = XGBE_RX_DESC_CNT;
  269. /* Adjust the number of queues based on interrupts assigned */
  270. if (pdata->channel_irq_count) {
  271. pdata->tx_ring_count = min_t(unsigned int, pdata->tx_ring_count,
  272. pdata->channel_irq_count);
  273. pdata->rx_ring_count = min_t(unsigned int, pdata->rx_ring_count,
  274. pdata->channel_irq_count);
  275. if (netif_msg_probe(pdata))
  276. dev_dbg(pdata->dev,
  277. "adjusted TX/RX DMA channel count = %u/%u\n",
  278. pdata->tx_ring_count, pdata->rx_ring_count);
  279. }
  280. /* Set the number of queues */
  281. ret = netif_set_real_num_tx_queues(netdev, pdata->tx_ring_count);
  282. if (ret) {
  283. dev_err(dev, "error setting real tx queue count\n");
  284. return ret;
  285. }
  286. ret = netif_set_real_num_rx_queues(netdev, pdata->rx_ring_count);
  287. if (ret) {
  288. dev_err(dev, "error setting real rx queue count\n");
  289. return ret;
  290. }
  291. /* Initialize RSS hash key and lookup table */
  292. netdev_rss_key_fill(pdata->rss_key, sizeof(pdata->rss_key));
  293. for (i = 0; i < XGBE_RSS_MAX_TABLE_SIZE; i++)
  294. XGMAC_SET_BITS(pdata->rss_table[i], MAC_RSSDR, DMCH,
  295. i % pdata->rx_ring_count);
  296. XGMAC_SET_BITS(pdata->rss_options, MAC_RSSCR, IP2TE, 1);
  297. XGMAC_SET_BITS(pdata->rss_options, MAC_RSSCR, TCP4TE, 1);
  298. XGMAC_SET_BITS(pdata->rss_options, MAC_RSSCR, UDP4TE, 1);
  299. /* Call MDIO/PHY initialization routine */
  300. ret = pdata->phy_if.phy_init(pdata);
  301. if (ret)
  302. return ret;
  303. /* Set device operations */
  304. netdev->netdev_ops = xgbe_get_netdev_ops();
  305. netdev->ethtool_ops = xgbe_get_ethtool_ops();
  306. #ifdef CONFIG_AMD_XGBE_DCB
  307. netdev->dcbnl_ops = xgbe_get_dcbnl_ops();
  308. #endif
  309. /* Set device features */
  310. netdev->hw_features = NETIF_F_SG |
  311. NETIF_F_IP_CSUM |
  312. NETIF_F_IPV6_CSUM |
  313. NETIF_F_RXCSUM |
  314. NETIF_F_TSO |
  315. NETIF_F_TSO6 |
  316. NETIF_F_GRO |
  317. NETIF_F_HW_VLAN_CTAG_RX |
  318. NETIF_F_HW_VLAN_CTAG_TX |
  319. NETIF_F_HW_VLAN_CTAG_FILTER;
  320. if (pdata->hw_feat.rss)
  321. netdev->hw_features |= NETIF_F_RXHASH;
  322. netdev->vlan_features |= NETIF_F_SG |
  323. NETIF_F_IP_CSUM |
  324. NETIF_F_IPV6_CSUM |
  325. NETIF_F_TSO |
  326. NETIF_F_TSO6;
  327. netdev->features |= netdev->hw_features;
  328. pdata->netdev_features = netdev->features;
  329. netdev->priv_flags |= IFF_UNICAST_FLT;
  330. netdev->min_mtu = 0;
  331. netdev->max_mtu = XGMAC_JUMBO_PACKET_MTU;
  332. /* Use default watchdog timeout */
  333. netdev->watchdog_timeo = 0;
  334. xgbe_init_rx_coalesce(pdata);
  335. xgbe_init_tx_coalesce(pdata);
  336. netif_carrier_off(netdev);
  337. ret = register_netdev(netdev);
  338. if (ret) {
  339. dev_err(dev, "net device registration failed\n");
  340. return ret;
  341. }
  342. /* Create the PHY/ANEG name based on netdev name */
  343. snprintf(pdata->an_name, sizeof(pdata->an_name) - 1, "%s-pcs",
  344. netdev_name(netdev));
  345. /* Create the ECC name based on netdev name */
  346. snprintf(pdata->ecc_name, sizeof(pdata->ecc_name) - 1, "%s-ecc",
  347. netdev_name(netdev));
  348. /* Create the I2C name based on netdev name */
  349. snprintf(pdata->i2c_name, sizeof(pdata->i2c_name) - 1, "%s-i2c",
  350. netdev_name(netdev));
  351. /* Create workqueues */
  352. pdata->dev_workqueue =
  353. create_singlethread_workqueue(netdev_name(netdev));
  354. if (!pdata->dev_workqueue) {
  355. netdev_err(netdev, "device workqueue creation failed\n");
  356. ret = -ENOMEM;
  357. goto err_netdev;
  358. }
  359. pdata->an_workqueue =
  360. create_singlethread_workqueue(pdata->an_name);
  361. if (!pdata->an_workqueue) {
  362. netdev_err(netdev, "phy workqueue creation failed\n");
  363. ret = -ENOMEM;
  364. goto err_wq;
  365. }
  366. if (IS_REACHABLE(CONFIG_PTP_1588_CLOCK))
  367. xgbe_ptp_register(pdata);
  368. xgbe_debugfs_init(pdata);
  369. netif_dbg(pdata, drv, pdata->netdev, "%u Tx software queues\n",
  370. pdata->tx_ring_count);
  371. netif_dbg(pdata, drv, pdata->netdev, "%u Rx software queues\n",
  372. pdata->rx_ring_count);
  373. return 0;
  374. err_wq:
  375. destroy_workqueue(pdata->dev_workqueue);
  376. err_netdev:
  377. unregister_netdev(netdev);
  378. return ret;
  379. }
  380. void xgbe_deconfig_netdev(struct xgbe_prv_data *pdata)
  381. {
  382. struct net_device *netdev = pdata->netdev;
  383. xgbe_debugfs_exit(pdata);
  384. if (IS_REACHABLE(CONFIG_PTP_1588_CLOCK))
  385. xgbe_ptp_unregister(pdata);
  386. pdata->phy_if.phy_exit(pdata);
  387. flush_workqueue(pdata->an_workqueue);
  388. destroy_workqueue(pdata->an_workqueue);
  389. flush_workqueue(pdata->dev_workqueue);
  390. destroy_workqueue(pdata->dev_workqueue);
  391. unregister_netdev(netdev);
  392. }
  393. static int __init xgbe_mod_init(void)
  394. {
  395. int ret;
  396. ret = xgbe_platform_init();
  397. if (ret)
  398. return ret;
  399. ret = xgbe_pci_init();
  400. if (ret)
  401. return ret;
  402. return 0;
  403. }
  404. static void __exit xgbe_mod_exit(void)
  405. {
  406. xgbe_pci_exit();
  407. xgbe_platform_exit();
  408. }
  409. module_init(xgbe_mod_init);
  410. module_exit(xgbe_mod_exit);