xgbe-main.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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 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 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/platform_device.h>
  119. #include <linux/spinlock.h>
  120. #include <linux/netdevice.h>
  121. #include <linux/etherdevice.h>
  122. #include <linux/io.h>
  123. #include <linux/of.h>
  124. #include <linux/of_net.h>
  125. #include <linux/of_address.h>
  126. #include <linux/of_platform.h>
  127. #include <linux/clk.h>
  128. #include <linux/property.h>
  129. #include <linux/acpi.h>
  130. #include <linux/mdio.h>
  131. #include "xgbe.h"
  132. #include "xgbe-common.h"
  133. MODULE_AUTHOR("Tom Lendacky <thomas.lendacky@amd.com>");
  134. MODULE_LICENSE("Dual BSD/GPL");
  135. MODULE_VERSION(XGBE_DRV_VERSION);
  136. MODULE_DESCRIPTION(XGBE_DRV_DESC);
  137. static int debug = -1;
  138. module_param(debug, int, S_IWUSR | S_IRUGO);
  139. MODULE_PARM_DESC(debug, " Network interface message level setting");
  140. static const u32 default_msg_level = (NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
  141. NETIF_MSG_IFUP);
  142. static const u32 xgbe_serdes_blwc[] = {
  143. XGBE_SPEED_1000_BLWC,
  144. XGBE_SPEED_2500_BLWC,
  145. XGBE_SPEED_10000_BLWC,
  146. };
  147. static const u32 xgbe_serdes_cdr_rate[] = {
  148. XGBE_SPEED_1000_CDR,
  149. XGBE_SPEED_2500_CDR,
  150. XGBE_SPEED_10000_CDR,
  151. };
  152. static const u32 xgbe_serdes_pq_skew[] = {
  153. XGBE_SPEED_1000_PQ,
  154. XGBE_SPEED_2500_PQ,
  155. XGBE_SPEED_10000_PQ,
  156. };
  157. static const u32 xgbe_serdes_tx_amp[] = {
  158. XGBE_SPEED_1000_TXAMP,
  159. XGBE_SPEED_2500_TXAMP,
  160. XGBE_SPEED_10000_TXAMP,
  161. };
  162. static const u32 xgbe_serdes_dfe_tap_cfg[] = {
  163. XGBE_SPEED_1000_DFE_TAP_CONFIG,
  164. XGBE_SPEED_2500_DFE_TAP_CONFIG,
  165. XGBE_SPEED_10000_DFE_TAP_CONFIG,
  166. };
  167. static const u32 xgbe_serdes_dfe_tap_ena[] = {
  168. XGBE_SPEED_1000_DFE_TAP_ENABLE,
  169. XGBE_SPEED_2500_DFE_TAP_ENABLE,
  170. XGBE_SPEED_10000_DFE_TAP_ENABLE,
  171. };
  172. static void xgbe_default_config(struct xgbe_prv_data *pdata)
  173. {
  174. DBGPR("-->xgbe_default_config\n");
  175. pdata->pblx8 = DMA_PBL_X8_ENABLE;
  176. pdata->tx_sf_mode = MTL_TSF_ENABLE;
  177. pdata->tx_threshold = MTL_TX_THRESHOLD_64;
  178. pdata->tx_pbl = DMA_PBL_16;
  179. pdata->tx_osp_mode = DMA_OSP_ENABLE;
  180. pdata->rx_sf_mode = MTL_RSF_DISABLE;
  181. pdata->rx_threshold = MTL_RX_THRESHOLD_64;
  182. pdata->rx_pbl = DMA_PBL_16;
  183. pdata->pause_autoneg = 1;
  184. pdata->tx_pause = 1;
  185. pdata->rx_pause = 1;
  186. pdata->phy_speed = SPEED_UNKNOWN;
  187. pdata->power_down = 0;
  188. DBGPR("<--xgbe_default_config\n");
  189. }
  190. static void xgbe_init_all_fptrs(struct xgbe_prv_data *pdata)
  191. {
  192. xgbe_init_function_ptrs_dev(&pdata->hw_if);
  193. xgbe_init_function_ptrs_phy(&pdata->phy_if);
  194. xgbe_init_function_ptrs_desc(&pdata->desc_if);
  195. }
  196. #ifdef CONFIG_ACPI
  197. static int xgbe_acpi_support(struct xgbe_prv_data *pdata)
  198. {
  199. struct device *dev = pdata->dev;
  200. u32 property;
  201. int ret;
  202. /* Obtain the system clock setting */
  203. ret = device_property_read_u32(dev, XGBE_ACPI_DMA_FREQ, &property);
  204. if (ret) {
  205. dev_err(dev, "unable to obtain %s property\n",
  206. XGBE_ACPI_DMA_FREQ);
  207. return ret;
  208. }
  209. pdata->sysclk_rate = property;
  210. /* Obtain the PTP clock setting */
  211. ret = device_property_read_u32(dev, XGBE_ACPI_PTP_FREQ, &property);
  212. if (ret) {
  213. dev_err(dev, "unable to obtain %s property\n",
  214. XGBE_ACPI_PTP_FREQ);
  215. return ret;
  216. }
  217. pdata->ptpclk_rate = property;
  218. return 0;
  219. }
  220. #else /* CONFIG_ACPI */
  221. static int xgbe_acpi_support(struct xgbe_prv_data *pdata)
  222. {
  223. return -EINVAL;
  224. }
  225. #endif /* CONFIG_ACPI */
  226. #ifdef CONFIG_OF
  227. static int xgbe_of_support(struct xgbe_prv_data *pdata)
  228. {
  229. struct device *dev = pdata->dev;
  230. /* Obtain the system clock setting */
  231. pdata->sysclk = devm_clk_get(dev, XGBE_DMA_CLOCK);
  232. if (IS_ERR(pdata->sysclk)) {
  233. dev_err(dev, "dma devm_clk_get failed\n");
  234. return PTR_ERR(pdata->sysclk);
  235. }
  236. pdata->sysclk_rate = clk_get_rate(pdata->sysclk);
  237. /* Obtain the PTP clock setting */
  238. pdata->ptpclk = devm_clk_get(dev, XGBE_PTP_CLOCK);
  239. if (IS_ERR(pdata->ptpclk)) {
  240. dev_err(dev, "ptp devm_clk_get failed\n");
  241. return PTR_ERR(pdata->ptpclk);
  242. }
  243. pdata->ptpclk_rate = clk_get_rate(pdata->ptpclk);
  244. return 0;
  245. }
  246. static struct platform_device *xgbe_of_get_phy_pdev(struct xgbe_prv_data *pdata)
  247. {
  248. struct device *dev = pdata->dev;
  249. struct device_node *phy_node;
  250. struct platform_device *phy_pdev;
  251. phy_node = of_parse_phandle(dev->of_node, "phy-handle", 0);
  252. if (phy_node) {
  253. /* Old style device tree:
  254. * The XGBE and PHY resources are separate
  255. */
  256. phy_pdev = of_find_device_by_node(phy_node);
  257. of_node_put(phy_node);
  258. } else {
  259. /* New style device tree:
  260. * The XGBE and PHY resources are grouped together with
  261. * the PHY resources listed last
  262. */
  263. get_device(dev);
  264. phy_pdev = pdata->pdev;
  265. }
  266. return phy_pdev;
  267. }
  268. #else /* CONFIG_OF */
  269. static int xgbe_of_support(struct xgbe_prv_data *pdata)
  270. {
  271. return -EINVAL;
  272. }
  273. static struct platform_device *xgbe_of_get_phy_pdev(struct xgbe_prv_data *pdata)
  274. {
  275. return NULL;
  276. }
  277. #endif /* CONFIG_OF */
  278. static unsigned int xgbe_resource_count(struct platform_device *pdev,
  279. unsigned int type)
  280. {
  281. unsigned int count;
  282. int i;
  283. for (i = 0, count = 0; i < pdev->num_resources; i++) {
  284. struct resource *res = &pdev->resource[i];
  285. if (type == resource_type(res))
  286. count++;
  287. }
  288. return count;
  289. }
  290. static struct platform_device *xgbe_get_phy_pdev(struct xgbe_prv_data *pdata)
  291. {
  292. struct platform_device *phy_pdev;
  293. if (pdata->use_acpi) {
  294. get_device(pdata->dev);
  295. phy_pdev = pdata->pdev;
  296. } else {
  297. phy_pdev = xgbe_of_get_phy_pdev(pdata);
  298. }
  299. return phy_pdev;
  300. }
  301. static int xgbe_probe(struct platform_device *pdev)
  302. {
  303. struct xgbe_prv_data *pdata;
  304. struct net_device *netdev;
  305. struct device *dev = &pdev->dev, *phy_dev;
  306. struct platform_device *phy_pdev;
  307. struct resource *res;
  308. const char *phy_mode;
  309. unsigned int i, phy_memnum, phy_irqnum;
  310. int ret;
  311. DBGPR("--> xgbe_probe\n");
  312. netdev = alloc_etherdev_mq(sizeof(struct xgbe_prv_data),
  313. XGBE_MAX_DMA_CHANNELS);
  314. if (!netdev) {
  315. dev_err(dev, "alloc_etherdev failed\n");
  316. ret = -ENOMEM;
  317. goto err_alloc;
  318. }
  319. SET_NETDEV_DEV(netdev, dev);
  320. pdata = netdev_priv(netdev);
  321. pdata->netdev = netdev;
  322. pdata->pdev = pdev;
  323. pdata->adev = ACPI_COMPANION(dev);
  324. pdata->dev = dev;
  325. platform_set_drvdata(pdev, netdev);
  326. spin_lock_init(&pdata->lock);
  327. mutex_init(&pdata->xpcs_mutex);
  328. mutex_init(&pdata->rss_mutex);
  329. spin_lock_init(&pdata->tstamp_lock);
  330. pdata->msg_enable = netif_msg_init(debug, default_msg_level);
  331. set_bit(XGBE_DOWN, &pdata->dev_state);
  332. /* Check if we should use ACPI or DT */
  333. pdata->use_acpi = (!pdata->adev || acpi_disabled) ? 0 : 1;
  334. phy_pdev = xgbe_get_phy_pdev(pdata);
  335. if (!phy_pdev) {
  336. dev_err(dev, "unable to obtain phy device\n");
  337. ret = -EINVAL;
  338. goto err_phydev;
  339. }
  340. phy_dev = &phy_pdev->dev;
  341. if (pdev == phy_pdev) {
  342. /* New style device tree or ACPI:
  343. * The XGBE and PHY resources are grouped together with
  344. * the PHY resources listed last
  345. */
  346. phy_memnum = xgbe_resource_count(pdev, IORESOURCE_MEM) - 3;
  347. phy_irqnum = xgbe_resource_count(pdev, IORESOURCE_IRQ) - 1;
  348. } else {
  349. /* Old style device tree:
  350. * The XGBE and PHY resources are separate
  351. */
  352. phy_memnum = 0;
  353. phy_irqnum = 0;
  354. }
  355. /* Set and validate the number of descriptors for a ring */
  356. BUILD_BUG_ON_NOT_POWER_OF_2(XGBE_TX_DESC_CNT);
  357. pdata->tx_desc_count = XGBE_TX_DESC_CNT;
  358. if (pdata->tx_desc_count & (pdata->tx_desc_count - 1)) {
  359. dev_err(dev, "tx descriptor count (%d) is not valid\n",
  360. pdata->tx_desc_count);
  361. ret = -EINVAL;
  362. goto err_io;
  363. }
  364. BUILD_BUG_ON_NOT_POWER_OF_2(XGBE_RX_DESC_CNT);
  365. pdata->rx_desc_count = XGBE_RX_DESC_CNT;
  366. if (pdata->rx_desc_count & (pdata->rx_desc_count - 1)) {
  367. dev_err(dev, "rx descriptor count (%d) is not valid\n",
  368. pdata->rx_desc_count);
  369. ret = -EINVAL;
  370. goto err_io;
  371. }
  372. /* Obtain the mmio areas for the device */
  373. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  374. pdata->xgmac_regs = devm_ioremap_resource(dev, res);
  375. if (IS_ERR(pdata->xgmac_regs)) {
  376. dev_err(dev, "xgmac ioremap failed\n");
  377. ret = PTR_ERR(pdata->xgmac_regs);
  378. goto err_io;
  379. }
  380. if (netif_msg_probe(pdata))
  381. dev_dbg(dev, "xgmac_regs = %p\n", pdata->xgmac_regs);
  382. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  383. pdata->xpcs_regs = devm_ioremap_resource(dev, res);
  384. if (IS_ERR(pdata->xpcs_regs)) {
  385. dev_err(dev, "xpcs ioremap failed\n");
  386. ret = PTR_ERR(pdata->xpcs_regs);
  387. goto err_io;
  388. }
  389. if (netif_msg_probe(pdata))
  390. dev_dbg(dev, "xpcs_regs = %p\n", pdata->xpcs_regs);
  391. res = platform_get_resource(phy_pdev, IORESOURCE_MEM, phy_memnum++);
  392. pdata->rxtx_regs = devm_ioremap_resource(dev, res);
  393. if (IS_ERR(pdata->rxtx_regs)) {
  394. dev_err(dev, "rxtx ioremap failed\n");
  395. ret = PTR_ERR(pdata->rxtx_regs);
  396. goto err_io;
  397. }
  398. if (netif_msg_probe(pdata))
  399. dev_dbg(dev, "rxtx_regs = %p\n", pdata->rxtx_regs);
  400. res = platform_get_resource(phy_pdev, IORESOURCE_MEM, phy_memnum++);
  401. pdata->sir0_regs = devm_ioremap_resource(dev, res);
  402. if (IS_ERR(pdata->sir0_regs)) {
  403. dev_err(dev, "sir0 ioremap failed\n");
  404. ret = PTR_ERR(pdata->sir0_regs);
  405. goto err_io;
  406. }
  407. if (netif_msg_probe(pdata))
  408. dev_dbg(dev, "sir0_regs = %p\n", pdata->sir0_regs);
  409. res = platform_get_resource(phy_pdev, IORESOURCE_MEM, phy_memnum++);
  410. pdata->sir1_regs = devm_ioremap_resource(dev, res);
  411. if (IS_ERR(pdata->sir1_regs)) {
  412. dev_err(dev, "sir1 ioremap failed\n");
  413. ret = PTR_ERR(pdata->sir1_regs);
  414. goto err_io;
  415. }
  416. if (netif_msg_probe(pdata))
  417. dev_dbg(dev, "sir1_regs = %p\n", pdata->sir1_regs);
  418. /* Retrieve the MAC address */
  419. ret = device_property_read_u8_array(dev, XGBE_MAC_ADDR_PROPERTY,
  420. pdata->mac_addr,
  421. sizeof(pdata->mac_addr));
  422. if (ret || !is_valid_ether_addr(pdata->mac_addr)) {
  423. dev_err(dev, "invalid %s property\n", XGBE_MAC_ADDR_PROPERTY);
  424. if (!ret)
  425. ret = -EINVAL;
  426. goto err_io;
  427. }
  428. /* Retrieve the PHY mode - it must be "xgmii" */
  429. ret = device_property_read_string(dev, XGBE_PHY_MODE_PROPERTY,
  430. &phy_mode);
  431. if (ret || strcmp(phy_mode, phy_modes(PHY_INTERFACE_MODE_XGMII))) {
  432. dev_err(dev, "invalid %s property\n", XGBE_PHY_MODE_PROPERTY);
  433. if (!ret)
  434. ret = -EINVAL;
  435. goto err_io;
  436. }
  437. pdata->phy_mode = PHY_INTERFACE_MODE_XGMII;
  438. /* Check for per channel interrupt support */
  439. if (device_property_present(dev, XGBE_DMA_IRQS_PROPERTY))
  440. pdata->per_channel_irq = 1;
  441. /* Retrieve the PHY speedset */
  442. ret = device_property_read_u32(phy_dev, XGBE_SPEEDSET_PROPERTY,
  443. &pdata->speed_set);
  444. if (ret) {
  445. dev_err(dev, "invalid %s property\n", XGBE_SPEEDSET_PROPERTY);
  446. goto err_io;
  447. }
  448. switch (pdata->speed_set) {
  449. case XGBE_SPEEDSET_1000_10000:
  450. case XGBE_SPEEDSET_2500_10000:
  451. break;
  452. default:
  453. dev_err(dev, "invalid %s property\n", XGBE_SPEEDSET_PROPERTY);
  454. ret = -EINVAL;
  455. goto err_io;
  456. }
  457. /* Retrieve the PHY configuration properties */
  458. if (device_property_present(phy_dev, XGBE_BLWC_PROPERTY)) {
  459. ret = device_property_read_u32_array(phy_dev,
  460. XGBE_BLWC_PROPERTY,
  461. pdata->serdes_blwc,
  462. XGBE_SPEEDS);
  463. if (ret) {
  464. dev_err(dev, "invalid %s property\n",
  465. XGBE_BLWC_PROPERTY);
  466. goto err_io;
  467. }
  468. } else {
  469. memcpy(pdata->serdes_blwc, xgbe_serdes_blwc,
  470. sizeof(pdata->serdes_blwc));
  471. }
  472. if (device_property_present(phy_dev, XGBE_CDR_RATE_PROPERTY)) {
  473. ret = device_property_read_u32_array(phy_dev,
  474. XGBE_CDR_RATE_PROPERTY,
  475. pdata->serdes_cdr_rate,
  476. XGBE_SPEEDS);
  477. if (ret) {
  478. dev_err(dev, "invalid %s property\n",
  479. XGBE_CDR_RATE_PROPERTY);
  480. goto err_io;
  481. }
  482. } else {
  483. memcpy(pdata->serdes_cdr_rate, xgbe_serdes_cdr_rate,
  484. sizeof(pdata->serdes_cdr_rate));
  485. }
  486. if (device_property_present(phy_dev, XGBE_PQ_SKEW_PROPERTY)) {
  487. ret = device_property_read_u32_array(phy_dev,
  488. XGBE_PQ_SKEW_PROPERTY,
  489. pdata->serdes_pq_skew,
  490. XGBE_SPEEDS);
  491. if (ret) {
  492. dev_err(dev, "invalid %s property\n",
  493. XGBE_PQ_SKEW_PROPERTY);
  494. goto err_io;
  495. }
  496. } else {
  497. memcpy(pdata->serdes_pq_skew, xgbe_serdes_pq_skew,
  498. sizeof(pdata->serdes_pq_skew));
  499. }
  500. if (device_property_present(phy_dev, XGBE_TX_AMP_PROPERTY)) {
  501. ret = device_property_read_u32_array(phy_dev,
  502. XGBE_TX_AMP_PROPERTY,
  503. pdata->serdes_tx_amp,
  504. XGBE_SPEEDS);
  505. if (ret) {
  506. dev_err(dev, "invalid %s property\n",
  507. XGBE_TX_AMP_PROPERTY);
  508. goto err_io;
  509. }
  510. } else {
  511. memcpy(pdata->serdes_tx_amp, xgbe_serdes_tx_amp,
  512. sizeof(pdata->serdes_tx_amp));
  513. }
  514. if (device_property_present(phy_dev, XGBE_DFE_CFG_PROPERTY)) {
  515. ret = device_property_read_u32_array(phy_dev,
  516. XGBE_DFE_CFG_PROPERTY,
  517. pdata->serdes_dfe_tap_cfg,
  518. XGBE_SPEEDS);
  519. if (ret) {
  520. dev_err(dev, "invalid %s property\n",
  521. XGBE_DFE_CFG_PROPERTY);
  522. goto err_io;
  523. }
  524. } else {
  525. memcpy(pdata->serdes_dfe_tap_cfg, xgbe_serdes_dfe_tap_cfg,
  526. sizeof(pdata->serdes_dfe_tap_cfg));
  527. }
  528. if (device_property_present(phy_dev, XGBE_DFE_ENA_PROPERTY)) {
  529. ret = device_property_read_u32_array(phy_dev,
  530. XGBE_DFE_ENA_PROPERTY,
  531. pdata->serdes_dfe_tap_ena,
  532. XGBE_SPEEDS);
  533. if (ret) {
  534. dev_err(dev, "invalid %s property\n",
  535. XGBE_DFE_ENA_PROPERTY);
  536. goto err_io;
  537. }
  538. } else {
  539. memcpy(pdata->serdes_dfe_tap_ena, xgbe_serdes_dfe_tap_ena,
  540. sizeof(pdata->serdes_dfe_tap_ena));
  541. }
  542. /* Obtain device settings unique to ACPI/OF */
  543. if (pdata->use_acpi)
  544. ret = xgbe_acpi_support(pdata);
  545. else
  546. ret = xgbe_of_support(pdata);
  547. if (ret)
  548. goto err_io;
  549. /* Set the DMA coherency values */
  550. pdata->coherent = device_dma_is_coherent(pdata->dev);
  551. if (pdata->coherent) {
  552. pdata->axdomain = XGBE_DMA_OS_AXDOMAIN;
  553. pdata->arcache = XGBE_DMA_OS_ARCACHE;
  554. pdata->awcache = XGBE_DMA_OS_AWCACHE;
  555. } else {
  556. pdata->axdomain = XGBE_DMA_SYS_AXDOMAIN;
  557. pdata->arcache = XGBE_DMA_SYS_ARCACHE;
  558. pdata->awcache = XGBE_DMA_SYS_AWCACHE;
  559. }
  560. /* Get the device interrupt */
  561. ret = platform_get_irq(pdev, 0);
  562. if (ret < 0) {
  563. dev_err(dev, "platform_get_irq 0 failed\n");
  564. goto err_io;
  565. }
  566. pdata->dev_irq = ret;
  567. /* Get the auto-negotiation interrupt */
  568. ret = platform_get_irq(phy_pdev, phy_irqnum++);
  569. if (ret < 0) {
  570. dev_err(dev, "platform_get_irq phy 0 failed\n");
  571. goto err_io;
  572. }
  573. pdata->an_irq = ret;
  574. netdev->irq = pdata->dev_irq;
  575. netdev->base_addr = (unsigned long)pdata->xgmac_regs;
  576. memcpy(netdev->dev_addr, pdata->mac_addr, netdev->addr_len);
  577. /* Set all the function pointers */
  578. xgbe_init_all_fptrs(pdata);
  579. /* Issue software reset to device */
  580. pdata->hw_if.exit(pdata);
  581. /* Populate the hardware features */
  582. xgbe_get_all_hw_features(pdata);
  583. /* Set default configuration data */
  584. xgbe_default_config(pdata);
  585. /* Set the DMA mask */
  586. ret = dma_set_mask_and_coherent(dev,
  587. DMA_BIT_MASK(pdata->hw_feat.dma_width));
  588. if (ret) {
  589. dev_err(dev, "dma_set_mask_and_coherent failed\n");
  590. goto err_io;
  591. }
  592. /* Calculate the number of Tx and Rx rings to be created
  593. * -Tx (DMA) Channels map 1-to-1 to Tx Queues so set
  594. * the number of Tx queues to the number of Tx channels
  595. * enabled
  596. * -Rx (DMA) Channels do not map 1-to-1 so use the actual
  597. * number of Rx queues
  598. */
  599. pdata->tx_ring_count = min_t(unsigned int, num_online_cpus(),
  600. pdata->hw_feat.tx_ch_cnt);
  601. pdata->tx_q_count = pdata->tx_ring_count;
  602. ret = netif_set_real_num_tx_queues(netdev, pdata->tx_ring_count);
  603. if (ret) {
  604. dev_err(dev, "error setting real tx queue count\n");
  605. goto err_io;
  606. }
  607. pdata->rx_ring_count = min_t(unsigned int,
  608. netif_get_num_default_rss_queues(),
  609. pdata->hw_feat.rx_ch_cnt);
  610. pdata->rx_q_count = pdata->hw_feat.rx_q_cnt;
  611. ret = netif_set_real_num_rx_queues(netdev, pdata->rx_ring_count);
  612. if (ret) {
  613. dev_err(dev, "error setting real rx queue count\n");
  614. goto err_io;
  615. }
  616. /* Initialize RSS hash key and lookup table */
  617. netdev_rss_key_fill(pdata->rss_key, sizeof(pdata->rss_key));
  618. for (i = 0; i < XGBE_RSS_MAX_TABLE_SIZE; i++)
  619. XGMAC_SET_BITS(pdata->rss_table[i], MAC_RSSDR, DMCH,
  620. i % pdata->rx_ring_count);
  621. XGMAC_SET_BITS(pdata->rss_options, MAC_RSSCR, IP2TE, 1);
  622. XGMAC_SET_BITS(pdata->rss_options, MAC_RSSCR, TCP4TE, 1);
  623. XGMAC_SET_BITS(pdata->rss_options, MAC_RSSCR, UDP4TE, 1);
  624. /* Call MDIO/PHY initialization routine */
  625. pdata->phy_if.phy_init(pdata);
  626. /* Set device operations */
  627. netdev->netdev_ops = xgbe_get_netdev_ops();
  628. netdev->ethtool_ops = xgbe_get_ethtool_ops();
  629. #ifdef CONFIG_AMD_XGBE_DCB
  630. netdev->dcbnl_ops = xgbe_get_dcbnl_ops();
  631. #endif
  632. /* Set device features */
  633. netdev->hw_features = NETIF_F_SG |
  634. NETIF_F_IP_CSUM |
  635. NETIF_F_IPV6_CSUM |
  636. NETIF_F_RXCSUM |
  637. NETIF_F_TSO |
  638. NETIF_F_TSO6 |
  639. NETIF_F_GRO |
  640. NETIF_F_HW_VLAN_CTAG_RX |
  641. NETIF_F_HW_VLAN_CTAG_TX |
  642. NETIF_F_HW_VLAN_CTAG_FILTER;
  643. if (pdata->hw_feat.rss)
  644. netdev->hw_features |= NETIF_F_RXHASH;
  645. netdev->vlan_features |= NETIF_F_SG |
  646. NETIF_F_IP_CSUM |
  647. NETIF_F_IPV6_CSUM |
  648. NETIF_F_TSO |
  649. NETIF_F_TSO6;
  650. netdev->features |= netdev->hw_features;
  651. pdata->netdev_features = netdev->features;
  652. netdev->priv_flags |= IFF_UNICAST_FLT;
  653. /* Use default watchdog timeout */
  654. netdev->watchdog_timeo = 0;
  655. xgbe_init_rx_coalesce(pdata);
  656. xgbe_init_tx_coalesce(pdata);
  657. netif_carrier_off(netdev);
  658. ret = register_netdev(netdev);
  659. if (ret) {
  660. dev_err(dev, "net device registration failed\n");
  661. goto err_io;
  662. }
  663. /* Create the PHY/ANEG name based on netdev name */
  664. snprintf(pdata->an_name, sizeof(pdata->an_name) - 1, "%s-pcs",
  665. netdev_name(netdev));
  666. /* Create workqueues */
  667. pdata->dev_workqueue =
  668. create_singlethread_workqueue(netdev_name(netdev));
  669. if (!pdata->dev_workqueue) {
  670. netdev_err(netdev, "device workqueue creation failed\n");
  671. ret = -ENOMEM;
  672. goto err_netdev;
  673. }
  674. pdata->an_workqueue =
  675. create_singlethread_workqueue(pdata->an_name);
  676. if (!pdata->an_workqueue) {
  677. netdev_err(netdev, "phy workqueue creation failed\n");
  678. ret = -ENOMEM;
  679. goto err_wq;
  680. }
  681. xgbe_ptp_register(pdata);
  682. xgbe_debugfs_init(pdata);
  683. platform_device_put(phy_pdev);
  684. netdev_notice(netdev, "net device enabled\n");
  685. DBGPR("<-- xgbe_probe\n");
  686. return 0;
  687. err_wq:
  688. destroy_workqueue(pdata->dev_workqueue);
  689. err_netdev:
  690. unregister_netdev(netdev);
  691. err_io:
  692. platform_device_put(phy_pdev);
  693. err_phydev:
  694. free_netdev(netdev);
  695. err_alloc:
  696. dev_notice(dev, "net device not enabled\n");
  697. return ret;
  698. }
  699. static int xgbe_remove(struct platform_device *pdev)
  700. {
  701. struct net_device *netdev = platform_get_drvdata(pdev);
  702. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  703. DBGPR("-->xgbe_remove\n");
  704. xgbe_debugfs_exit(pdata);
  705. xgbe_ptp_unregister(pdata);
  706. flush_workqueue(pdata->an_workqueue);
  707. destroy_workqueue(pdata->an_workqueue);
  708. flush_workqueue(pdata->dev_workqueue);
  709. destroy_workqueue(pdata->dev_workqueue);
  710. unregister_netdev(netdev);
  711. free_netdev(netdev);
  712. DBGPR("<--xgbe_remove\n");
  713. return 0;
  714. }
  715. #ifdef CONFIG_PM
  716. static int xgbe_suspend(struct device *dev)
  717. {
  718. struct net_device *netdev = dev_get_drvdata(dev);
  719. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  720. int ret = 0;
  721. DBGPR("-->xgbe_suspend\n");
  722. if (netif_running(netdev))
  723. ret = xgbe_powerdown(netdev, XGMAC_DRIVER_CONTEXT);
  724. pdata->lpm_ctrl = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL1);
  725. pdata->lpm_ctrl |= MDIO_CTRL1_LPOWER;
  726. XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL1, pdata->lpm_ctrl);
  727. DBGPR("<--xgbe_suspend\n");
  728. return ret;
  729. }
  730. static int xgbe_resume(struct device *dev)
  731. {
  732. struct net_device *netdev = dev_get_drvdata(dev);
  733. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  734. int ret = 0;
  735. DBGPR("-->xgbe_resume\n");
  736. pdata->lpm_ctrl &= ~MDIO_CTRL1_LPOWER;
  737. XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL1, pdata->lpm_ctrl);
  738. if (netif_running(netdev))
  739. ret = xgbe_powerup(netdev, XGMAC_DRIVER_CONTEXT);
  740. DBGPR("<--xgbe_resume\n");
  741. return ret;
  742. }
  743. #endif /* CONFIG_PM */
  744. #ifdef CONFIG_ACPI
  745. static const struct acpi_device_id xgbe_acpi_match[] = {
  746. { "AMDI8001", 0 },
  747. {},
  748. };
  749. MODULE_DEVICE_TABLE(acpi, xgbe_acpi_match);
  750. #endif
  751. #ifdef CONFIG_OF
  752. static const struct of_device_id xgbe_of_match[] = {
  753. { .compatible = "amd,xgbe-seattle-v1a", },
  754. {},
  755. };
  756. MODULE_DEVICE_TABLE(of, xgbe_of_match);
  757. #endif
  758. static SIMPLE_DEV_PM_OPS(xgbe_pm_ops, xgbe_suspend, xgbe_resume);
  759. static struct platform_driver xgbe_driver = {
  760. .driver = {
  761. .name = "amd-xgbe",
  762. #ifdef CONFIG_ACPI
  763. .acpi_match_table = xgbe_acpi_match,
  764. #endif
  765. #ifdef CONFIG_OF
  766. .of_match_table = xgbe_of_match,
  767. #endif
  768. .pm = &xgbe_pm_ops,
  769. },
  770. .probe = xgbe_probe,
  771. .remove = xgbe_remove,
  772. };
  773. module_platform_driver(xgbe_driver);