xgbe-main.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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/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. enum dev_dma_attr attr;
  311. int ret;
  312. DBGPR("--> xgbe_probe\n");
  313. netdev = alloc_etherdev_mq(sizeof(struct xgbe_prv_data),
  314. XGBE_MAX_DMA_CHANNELS);
  315. if (!netdev) {
  316. dev_err(dev, "alloc_etherdev failed\n");
  317. ret = -ENOMEM;
  318. goto err_alloc;
  319. }
  320. SET_NETDEV_DEV(netdev, dev);
  321. pdata = netdev_priv(netdev);
  322. pdata->netdev = netdev;
  323. pdata->pdev = pdev;
  324. pdata->adev = ACPI_COMPANION(dev);
  325. pdata->dev = dev;
  326. platform_set_drvdata(pdev, netdev);
  327. spin_lock_init(&pdata->lock);
  328. spin_lock_init(&pdata->xpcs_lock);
  329. mutex_init(&pdata->rss_mutex);
  330. spin_lock_init(&pdata->tstamp_lock);
  331. pdata->msg_enable = netif_msg_init(debug, default_msg_level);
  332. set_bit(XGBE_DOWN, &pdata->dev_state);
  333. /* Check if we should use ACPI or DT */
  334. pdata->use_acpi = dev->of_node ? 0 : 1;
  335. phy_pdev = xgbe_get_phy_pdev(pdata);
  336. if (!phy_pdev) {
  337. dev_err(dev, "unable to obtain phy device\n");
  338. ret = -EINVAL;
  339. goto err_phydev;
  340. }
  341. phy_dev = &phy_pdev->dev;
  342. if (pdev == phy_pdev) {
  343. /* New style device tree or ACPI:
  344. * The XGBE and PHY resources are grouped together with
  345. * the PHY resources listed last
  346. */
  347. phy_memnum = xgbe_resource_count(pdev, IORESOURCE_MEM) - 3;
  348. phy_irqnum = xgbe_resource_count(pdev, IORESOURCE_IRQ) - 1;
  349. } else {
  350. /* Old style device tree:
  351. * The XGBE and PHY resources are separate
  352. */
  353. phy_memnum = 0;
  354. phy_irqnum = 0;
  355. }
  356. /* Set and validate the number of descriptors for a ring */
  357. BUILD_BUG_ON_NOT_POWER_OF_2(XGBE_TX_DESC_CNT);
  358. pdata->tx_desc_count = XGBE_TX_DESC_CNT;
  359. if (pdata->tx_desc_count & (pdata->tx_desc_count - 1)) {
  360. dev_err(dev, "tx descriptor count (%d) is not valid\n",
  361. pdata->tx_desc_count);
  362. ret = -EINVAL;
  363. goto err_io;
  364. }
  365. BUILD_BUG_ON_NOT_POWER_OF_2(XGBE_RX_DESC_CNT);
  366. pdata->rx_desc_count = XGBE_RX_DESC_CNT;
  367. if (pdata->rx_desc_count & (pdata->rx_desc_count - 1)) {
  368. dev_err(dev, "rx descriptor count (%d) is not valid\n",
  369. pdata->rx_desc_count);
  370. ret = -EINVAL;
  371. goto err_io;
  372. }
  373. /* Obtain the mmio areas for the device */
  374. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  375. pdata->xgmac_regs = devm_ioremap_resource(dev, res);
  376. if (IS_ERR(pdata->xgmac_regs)) {
  377. dev_err(dev, "xgmac ioremap failed\n");
  378. ret = PTR_ERR(pdata->xgmac_regs);
  379. goto err_io;
  380. }
  381. if (netif_msg_probe(pdata))
  382. dev_dbg(dev, "xgmac_regs = %p\n", pdata->xgmac_regs);
  383. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  384. pdata->xpcs_regs = devm_ioremap_resource(dev, res);
  385. if (IS_ERR(pdata->xpcs_regs)) {
  386. dev_err(dev, "xpcs ioremap failed\n");
  387. ret = PTR_ERR(pdata->xpcs_regs);
  388. goto err_io;
  389. }
  390. if (netif_msg_probe(pdata))
  391. dev_dbg(dev, "xpcs_regs = %p\n", pdata->xpcs_regs);
  392. res = platform_get_resource(phy_pdev, IORESOURCE_MEM, phy_memnum++);
  393. pdata->rxtx_regs = devm_ioremap_resource(dev, res);
  394. if (IS_ERR(pdata->rxtx_regs)) {
  395. dev_err(dev, "rxtx ioremap failed\n");
  396. ret = PTR_ERR(pdata->rxtx_regs);
  397. goto err_io;
  398. }
  399. if (netif_msg_probe(pdata))
  400. dev_dbg(dev, "rxtx_regs = %p\n", pdata->rxtx_regs);
  401. res = platform_get_resource(phy_pdev, IORESOURCE_MEM, phy_memnum++);
  402. pdata->sir0_regs = devm_ioremap_resource(dev, res);
  403. if (IS_ERR(pdata->sir0_regs)) {
  404. dev_err(dev, "sir0 ioremap failed\n");
  405. ret = PTR_ERR(pdata->sir0_regs);
  406. goto err_io;
  407. }
  408. if (netif_msg_probe(pdata))
  409. dev_dbg(dev, "sir0_regs = %p\n", pdata->sir0_regs);
  410. res = platform_get_resource(phy_pdev, IORESOURCE_MEM, phy_memnum++);
  411. pdata->sir1_regs = devm_ioremap_resource(dev, res);
  412. if (IS_ERR(pdata->sir1_regs)) {
  413. dev_err(dev, "sir1 ioremap failed\n");
  414. ret = PTR_ERR(pdata->sir1_regs);
  415. goto err_io;
  416. }
  417. if (netif_msg_probe(pdata))
  418. dev_dbg(dev, "sir1_regs = %p\n", pdata->sir1_regs);
  419. /* Retrieve the MAC address */
  420. ret = device_property_read_u8_array(dev, XGBE_MAC_ADDR_PROPERTY,
  421. pdata->mac_addr,
  422. sizeof(pdata->mac_addr));
  423. if (ret || !is_valid_ether_addr(pdata->mac_addr)) {
  424. dev_err(dev, "invalid %s property\n", XGBE_MAC_ADDR_PROPERTY);
  425. if (!ret)
  426. ret = -EINVAL;
  427. goto err_io;
  428. }
  429. /* Retrieve the PHY mode - it must be "xgmii" */
  430. ret = device_property_read_string(dev, XGBE_PHY_MODE_PROPERTY,
  431. &phy_mode);
  432. if (ret || strcmp(phy_mode, phy_modes(PHY_INTERFACE_MODE_XGMII))) {
  433. dev_err(dev, "invalid %s property\n", XGBE_PHY_MODE_PROPERTY);
  434. if (!ret)
  435. ret = -EINVAL;
  436. goto err_io;
  437. }
  438. pdata->phy_mode = PHY_INTERFACE_MODE_XGMII;
  439. /* Check for per channel interrupt support */
  440. if (device_property_present(dev, XGBE_DMA_IRQS_PROPERTY))
  441. pdata->per_channel_irq = 1;
  442. /* Retrieve the PHY speedset */
  443. ret = device_property_read_u32(phy_dev, XGBE_SPEEDSET_PROPERTY,
  444. &pdata->speed_set);
  445. if (ret) {
  446. dev_err(dev, "invalid %s property\n", XGBE_SPEEDSET_PROPERTY);
  447. goto err_io;
  448. }
  449. switch (pdata->speed_set) {
  450. case XGBE_SPEEDSET_1000_10000:
  451. case XGBE_SPEEDSET_2500_10000:
  452. break;
  453. default:
  454. dev_err(dev, "invalid %s property\n", XGBE_SPEEDSET_PROPERTY);
  455. ret = -EINVAL;
  456. goto err_io;
  457. }
  458. /* Retrieve the PHY configuration properties */
  459. if (device_property_present(phy_dev, XGBE_BLWC_PROPERTY)) {
  460. ret = device_property_read_u32_array(phy_dev,
  461. XGBE_BLWC_PROPERTY,
  462. pdata->serdes_blwc,
  463. XGBE_SPEEDS);
  464. if (ret) {
  465. dev_err(dev, "invalid %s property\n",
  466. XGBE_BLWC_PROPERTY);
  467. goto err_io;
  468. }
  469. } else {
  470. memcpy(pdata->serdes_blwc, xgbe_serdes_blwc,
  471. sizeof(pdata->serdes_blwc));
  472. }
  473. if (device_property_present(phy_dev, XGBE_CDR_RATE_PROPERTY)) {
  474. ret = device_property_read_u32_array(phy_dev,
  475. XGBE_CDR_RATE_PROPERTY,
  476. pdata->serdes_cdr_rate,
  477. XGBE_SPEEDS);
  478. if (ret) {
  479. dev_err(dev, "invalid %s property\n",
  480. XGBE_CDR_RATE_PROPERTY);
  481. goto err_io;
  482. }
  483. } else {
  484. memcpy(pdata->serdes_cdr_rate, xgbe_serdes_cdr_rate,
  485. sizeof(pdata->serdes_cdr_rate));
  486. }
  487. if (device_property_present(phy_dev, XGBE_PQ_SKEW_PROPERTY)) {
  488. ret = device_property_read_u32_array(phy_dev,
  489. XGBE_PQ_SKEW_PROPERTY,
  490. pdata->serdes_pq_skew,
  491. XGBE_SPEEDS);
  492. if (ret) {
  493. dev_err(dev, "invalid %s property\n",
  494. XGBE_PQ_SKEW_PROPERTY);
  495. goto err_io;
  496. }
  497. } else {
  498. memcpy(pdata->serdes_pq_skew, xgbe_serdes_pq_skew,
  499. sizeof(pdata->serdes_pq_skew));
  500. }
  501. if (device_property_present(phy_dev, XGBE_TX_AMP_PROPERTY)) {
  502. ret = device_property_read_u32_array(phy_dev,
  503. XGBE_TX_AMP_PROPERTY,
  504. pdata->serdes_tx_amp,
  505. XGBE_SPEEDS);
  506. if (ret) {
  507. dev_err(dev, "invalid %s property\n",
  508. XGBE_TX_AMP_PROPERTY);
  509. goto err_io;
  510. }
  511. } else {
  512. memcpy(pdata->serdes_tx_amp, xgbe_serdes_tx_amp,
  513. sizeof(pdata->serdes_tx_amp));
  514. }
  515. if (device_property_present(phy_dev, XGBE_DFE_CFG_PROPERTY)) {
  516. ret = device_property_read_u32_array(phy_dev,
  517. XGBE_DFE_CFG_PROPERTY,
  518. pdata->serdes_dfe_tap_cfg,
  519. XGBE_SPEEDS);
  520. if (ret) {
  521. dev_err(dev, "invalid %s property\n",
  522. XGBE_DFE_CFG_PROPERTY);
  523. goto err_io;
  524. }
  525. } else {
  526. memcpy(pdata->serdes_dfe_tap_cfg, xgbe_serdes_dfe_tap_cfg,
  527. sizeof(pdata->serdes_dfe_tap_cfg));
  528. }
  529. if (device_property_present(phy_dev, XGBE_DFE_ENA_PROPERTY)) {
  530. ret = device_property_read_u32_array(phy_dev,
  531. XGBE_DFE_ENA_PROPERTY,
  532. pdata->serdes_dfe_tap_ena,
  533. XGBE_SPEEDS);
  534. if (ret) {
  535. dev_err(dev, "invalid %s property\n",
  536. XGBE_DFE_ENA_PROPERTY);
  537. goto err_io;
  538. }
  539. } else {
  540. memcpy(pdata->serdes_dfe_tap_ena, xgbe_serdes_dfe_tap_ena,
  541. sizeof(pdata->serdes_dfe_tap_ena));
  542. }
  543. /* Obtain device settings unique to ACPI/OF */
  544. if (pdata->use_acpi)
  545. ret = xgbe_acpi_support(pdata);
  546. else
  547. ret = xgbe_of_support(pdata);
  548. if (ret)
  549. goto err_io;
  550. /* Set the DMA coherency values */
  551. attr = device_get_dma_attr(dev);
  552. if (attr == DEV_DMA_NOT_SUPPORTED) {
  553. dev_err(dev, "DMA is not supported");
  554. goto err_io;
  555. }
  556. pdata->coherent = (attr == DEV_DMA_COHERENT);
  557. if (pdata->coherent) {
  558. pdata->axdomain = XGBE_DMA_OS_AXDOMAIN;
  559. pdata->arcache = XGBE_DMA_OS_ARCACHE;
  560. pdata->awcache = XGBE_DMA_OS_AWCACHE;
  561. } else {
  562. pdata->axdomain = XGBE_DMA_SYS_AXDOMAIN;
  563. pdata->arcache = XGBE_DMA_SYS_ARCACHE;
  564. pdata->awcache = XGBE_DMA_SYS_AWCACHE;
  565. }
  566. /* Get the device interrupt */
  567. ret = platform_get_irq(pdev, 0);
  568. if (ret < 0) {
  569. dev_err(dev, "platform_get_irq 0 failed\n");
  570. goto err_io;
  571. }
  572. pdata->dev_irq = ret;
  573. /* Get the auto-negotiation interrupt */
  574. ret = platform_get_irq(phy_pdev, phy_irqnum++);
  575. if (ret < 0) {
  576. dev_err(dev, "platform_get_irq phy 0 failed\n");
  577. goto err_io;
  578. }
  579. pdata->an_irq = ret;
  580. netdev->irq = pdata->dev_irq;
  581. netdev->base_addr = (unsigned long)pdata->xgmac_regs;
  582. memcpy(netdev->dev_addr, pdata->mac_addr, netdev->addr_len);
  583. /* Set all the function pointers */
  584. xgbe_init_all_fptrs(pdata);
  585. /* Issue software reset to device */
  586. pdata->hw_if.exit(pdata);
  587. /* Populate the hardware features */
  588. xgbe_get_all_hw_features(pdata);
  589. /* Set default configuration data */
  590. xgbe_default_config(pdata);
  591. /* Set the DMA mask */
  592. ret = dma_set_mask_and_coherent(dev,
  593. DMA_BIT_MASK(pdata->hw_feat.dma_width));
  594. if (ret) {
  595. dev_err(dev, "dma_set_mask_and_coherent failed\n");
  596. goto err_io;
  597. }
  598. /* Calculate the number of Tx and Rx rings to be created
  599. * -Tx (DMA) Channels map 1-to-1 to Tx Queues so set
  600. * the number of Tx queues to the number of Tx channels
  601. * enabled
  602. * -Rx (DMA) Channels do not map 1-to-1 so use the actual
  603. * number of Rx queues
  604. */
  605. pdata->tx_ring_count = min_t(unsigned int, num_online_cpus(),
  606. pdata->hw_feat.tx_ch_cnt);
  607. pdata->tx_q_count = pdata->tx_ring_count;
  608. ret = netif_set_real_num_tx_queues(netdev, pdata->tx_ring_count);
  609. if (ret) {
  610. dev_err(dev, "error setting real tx queue count\n");
  611. goto err_io;
  612. }
  613. pdata->rx_ring_count = min_t(unsigned int,
  614. netif_get_num_default_rss_queues(),
  615. pdata->hw_feat.rx_ch_cnt);
  616. pdata->rx_q_count = pdata->hw_feat.rx_q_cnt;
  617. ret = netif_set_real_num_rx_queues(netdev, pdata->rx_ring_count);
  618. if (ret) {
  619. dev_err(dev, "error setting real rx queue count\n");
  620. goto err_io;
  621. }
  622. /* Initialize RSS hash key and lookup table */
  623. netdev_rss_key_fill(pdata->rss_key, sizeof(pdata->rss_key));
  624. for (i = 0; i < XGBE_RSS_MAX_TABLE_SIZE; i++)
  625. XGMAC_SET_BITS(pdata->rss_table[i], MAC_RSSDR, DMCH,
  626. i % pdata->rx_ring_count);
  627. XGMAC_SET_BITS(pdata->rss_options, MAC_RSSCR, IP2TE, 1);
  628. XGMAC_SET_BITS(pdata->rss_options, MAC_RSSCR, TCP4TE, 1);
  629. XGMAC_SET_BITS(pdata->rss_options, MAC_RSSCR, UDP4TE, 1);
  630. /* Call MDIO/PHY initialization routine */
  631. pdata->phy_if.phy_init(pdata);
  632. /* Set device operations */
  633. netdev->netdev_ops = xgbe_get_netdev_ops();
  634. netdev->ethtool_ops = xgbe_get_ethtool_ops();
  635. #ifdef CONFIG_AMD_XGBE_DCB
  636. netdev->dcbnl_ops = xgbe_get_dcbnl_ops();
  637. #endif
  638. /* Set device features */
  639. netdev->hw_features = NETIF_F_SG |
  640. NETIF_F_IP_CSUM |
  641. NETIF_F_IPV6_CSUM |
  642. NETIF_F_RXCSUM |
  643. NETIF_F_TSO |
  644. NETIF_F_TSO6 |
  645. NETIF_F_GRO |
  646. NETIF_F_HW_VLAN_CTAG_RX |
  647. NETIF_F_HW_VLAN_CTAG_TX |
  648. NETIF_F_HW_VLAN_CTAG_FILTER;
  649. if (pdata->hw_feat.rss)
  650. netdev->hw_features |= NETIF_F_RXHASH;
  651. netdev->vlan_features |= NETIF_F_SG |
  652. NETIF_F_IP_CSUM |
  653. NETIF_F_IPV6_CSUM |
  654. NETIF_F_TSO |
  655. NETIF_F_TSO6;
  656. netdev->features |= netdev->hw_features;
  657. pdata->netdev_features = netdev->features;
  658. netdev->priv_flags |= IFF_UNICAST_FLT;
  659. /* Use default watchdog timeout */
  660. netdev->watchdog_timeo = 0;
  661. xgbe_init_rx_coalesce(pdata);
  662. xgbe_init_tx_coalesce(pdata);
  663. netif_carrier_off(netdev);
  664. ret = register_netdev(netdev);
  665. if (ret) {
  666. dev_err(dev, "net device registration failed\n");
  667. goto err_io;
  668. }
  669. /* Create the PHY/ANEG name based on netdev name */
  670. snprintf(pdata->an_name, sizeof(pdata->an_name) - 1, "%s-pcs",
  671. netdev_name(netdev));
  672. /* Create workqueues */
  673. pdata->dev_workqueue =
  674. create_singlethread_workqueue(netdev_name(netdev));
  675. if (!pdata->dev_workqueue) {
  676. netdev_err(netdev, "device workqueue creation failed\n");
  677. ret = -ENOMEM;
  678. goto err_netdev;
  679. }
  680. pdata->an_workqueue =
  681. create_singlethread_workqueue(pdata->an_name);
  682. if (!pdata->an_workqueue) {
  683. netdev_err(netdev, "phy workqueue creation failed\n");
  684. ret = -ENOMEM;
  685. goto err_wq;
  686. }
  687. xgbe_ptp_register(pdata);
  688. xgbe_debugfs_init(pdata);
  689. platform_device_put(phy_pdev);
  690. netdev_notice(netdev, "net device enabled\n");
  691. DBGPR("<-- xgbe_probe\n");
  692. return 0;
  693. err_wq:
  694. destroy_workqueue(pdata->dev_workqueue);
  695. err_netdev:
  696. unregister_netdev(netdev);
  697. err_io:
  698. platform_device_put(phy_pdev);
  699. err_phydev:
  700. free_netdev(netdev);
  701. err_alloc:
  702. dev_notice(dev, "net device not enabled\n");
  703. return ret;
  704. }
  705. static int xgbe_remove(struct platform_device *pdev)
  706. {
  707. struct net_device *netdev = platform_get_drvdata(pdev);
  708. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  709. DBGPR("-->xgbe_remove\n");
  710. xgbe_debugfs_exit(pdata);
  711. xgbe_ptp_unregister(pdata);
  712. flush_workqueue(pdata->an_workqueue);
  713. destroy_workqueue(pdata->an_workqueue);
  714. flush_workqueue(pdata->dev_workqueue);
  715. destroy_workqueue(pdata->dev_workqueue);
  716. unregister_netdev(netdev);
  717. free_netdev(netdev);
  718. DBGPR("<--xgbe_remove\n");
  719. return 0;
  720. }
  721. #ifdef CONFIG_PM_SLEEP
  722. static int xgbe_suspend(struct device *dev)
  723. {
  724. struct net_device *netdev = dev_get_drvdata(dev);
  725. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  726. int ret = 0;
  727. DBGPR("-->xgbe_suspend\n");
  728. if (netif_running(netdev))
  729. ret = xgbe_powerdown(netdev, XGMAC_DRIVER_CONTEXT);
  730. pdata->lpm_ctrl = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL1);
  731. pdata->lpm_ctrl |= MDIO_CTRL1_LPOWER;
  732. XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL1, pdata->lpm_ctrl);
  733. DBGPR("<--xgbe_suspend\n");
  734. return ret;
  735. }
  736. static int xgbe_resume(struct device *dev)
  737. {
  738. struct net_device *netdev = dev_get_drvdata(dev);
  739. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  740. int ret = 0;
  741. DBGPR("-->xgbe_resume\n");
  742. pdata->lpm_ctrl &= ~MDIO_CTRL1_LPOWER;
  743. XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL1, pdata->lpm_ctrl);
  744. if (netif_running(netdev)) {
  745. ret = xgbe_powerup(netdev, XGMAC_DRIVER_CONTEXT);
  746. /* Schedule a restart in case the link or phy state changed
  747. * while we were powered down.
  748. */
  749. schedule_work(&pdata->restart_work);
  750. }
  751. DBGPR("<--xgbe_resume\n");
  752. return ret;
  753. }
  754. #endif /* CONFIG_PM_SLEEP */
  755. #ifdef CONFIG_ACPI
  756. static const struct acpi_device_id xgbe_acpi_match[] = {
  757. { "AMDI8001", 0 },
  758. {},
  759. };
  760. MODULE_DEVICE_TABLE(acpi, xgbe_acpi_match);
  761. #endif
  762. #ifdef CONFIG_OF
  763. static const struct of_device_id xgbe_of_match[] = {
  764. { .compatible = "amd,xgbe-seattle-v1a", },
  765. {},
  766. };
  767. MODULE_DEVICE_TABLE(of, xgbe_of_match);
  768. #endif
  769. static SIMPLE_DEV_PM_OPS(xgbe_pm_ops, xgbe_suspend, xgbe_resume);
  770. static struct platform_driver xgbe_driver = {
  771. .driver = {
  772. .name = "amd-xgbe",
  773. #ifdef CONFIG_ACPI
  774. .acpi_match_table = xgbe_acpi_match,
  775. #endif
  776. #ifdef CONFIG_OF
  777. .of_match_table = xgbe_of_match,
  778. #endif
  779. .pm = &xgbe_pm_ops,
  780. },
  781. .probe = xgbe_probe,
  782. .remove = xgbe_remove,
  783. };
  784. module_platform_driver(xgbe_driver);