xgbe-drv.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227
  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/platform_device.h>
  117. #include <linux/spinlock.h>
  118. #include <linux/tcp.h>
  119. #include <linux/if_vlan.h>
  120. #include <net/busy_poll.h>
  121. #include <linux/clk.h>
  122. #include <linux/if_ether.h>
  123. #include <linux/net_tstamp.h>
  124. #include <linux/phy.h>
  125. #include "xgbe.h"
  126. #include "xgbe-common.h"
  127. static int xgbe_one_poll(struct napi_struct *, int);
  128. static int xgbe_all_poll(struct napi_struct *, int);
  129. static int xgbe_alloc_channels(struct xgbe_prv_data *pdata)
  130. {
  131. struct xgbe_channel *channel_mem, *channel;
  132. struct xgbe_ring *tx_ring, *rx_ring;
  133. unsigned int count, i;
  134. int ret = -ENOMEM;
  135. count = max_t(unsigned int, pdata->tx_ring_count, pdata->rx_ring_count);
  136. channel_mem = kcalloc(count, sizeof(struct xgbe_channel), GFP_KERNEL);
  137. if (!channel_mem)
  138. goto err_channel;
  139. tx_ring = kcalloc(pdata->tx_ring_count, sizeof(struct xgbe_ring),
  140. GFP_KERNEL);
  141. if (!tx_ring)
  142. goto err_tx_ring;
  143. rx_ring = kcalloc(pdata->rx_ring_count, sizeof(struct xgbe_ring),
  144. GFP_KERNEL);
  145. if (!rx_ring)
  146. goto err_rx_ring;
  147. for (i = 0, channel = channel_mem; i < count; i++, channel++) {
  148. snprintf(channel->name, sizeof(channel->name), "channel-%d", i);
  149. channel->pdata = pdata;
  150. channel->queue_index = i;
  151. channel->dma_regs = pdata->xgmac_regs + DMA_CH_BASE +
  152. (DMA_CH_INC * i);
  153. if (pdata->per_channel_irq) {
  154. /* Get the DMA interrupt (offset 1) */
  155. ret = platform_get_irq(pdata->pdev, i + 1);
  156. if (ret < 0) {
  157. netdev_err(pdata->netdev,
  158. "platform_get_irq %u failed\n",
  159. i + 1);
  160. goto err_irq;
  161. }
  162. channel->dma_irq = ret;
  163. }
  164. if (i < pdata->tx_ring_count) {
  165. spin_lock_init(&tx_ring->lock);
  166. channel->tx_ring = tx_ring++;
  167. }
  168. if (i < pdata->rx_ring_count) {
  169. spin_lock_init(&rx_ring->lock);
  170. channel->rx_ring = rx_ring++;
  171. }
  172. DBGPR(" %s: queue=%u, dma_regs=%p, dma_irq=%d, tx=%p, rx=%p\n",
  173. channel->name, channel->queue_index, channel->dma_regs,
  174. channel->dma_irq, channel->tx_ring, channel->rx_ring);
  175. }
  176. pdata->channel = channel_mem;
  177. pdata->channel_count = count;
  178. return 0;
  179. err_irq:
  180. kfree(rx_ring);
  181. err_rx_ring:
  182. kfree(tx_ring);
  183. err_tx_ring:
  184. kfree(channel_mem);
  185. err_channel:
  186. return ret;
  187. }
  188. static void xgbe_free_channels(struct xgbe_prv_data *pdata)
  189. {
  190. if (!pdata->channel)
  191. return;
  192. kfree(pdata->channel->rx_ring);
  193. kfree(pdata->channel->tx_ring);
  194. kfree(pdata->channel);
  195. pdata->channel = NULL;
  196. pdata->channel_count = 0;
  197. }
  198. static inline unsigned int xgbe_tx_avail_desc(struct xgbe_ring *ring)
  199. {
  200. return (ring->rdesc_count - (ring->cur - ring->dirty));
  201. }
  202. static inline unsigned int xgbe_rx_dirty_desc(struct xgbe_ring *ring)
  203. {
  204. return (ring->cur - ring->dirty);
  205. }
  206. static int xgbe_maybe_stop_tx_queue(struct xgbe_channel *channel,
  207. struct xgbe_ring *ring, unsigned int count)
  208. {
  209. struct xgbe_prv_data *pdata = channel->pdata;
  210. if (count > xgbe_tx_avail_desc(ring)) {
  211. DBGPR(" Tx queue stopped, not enough descriptors available\n");
  212. netif_stop_subqueue(pdata->netdev, channel->queue_index);
  213. ring->tx.queue_stopped = 1;
  214. /* If we haven't notified the hardware because of xmit_more
  215. * support, tell it now
  216. */
  217. if (ring->tx.xmit_more)
  218. pdata->hw_if.tx_start_xmit(channel, ring);
  219. return NETDEV_TX_BUSY;
  220. }
  221. return 0;
  222. }
  223. static int xgbe_calc_rx_buf_size(struct net_device *netdev, unsigned int mtu)
  224. {
  225. unsigned int rx_buf_size;
  226. if (mtu > XGMAC_JUMBO_PACKET_MTU) {
  227. netdev_alert(netdev, "MTU exceeds maximum supported value\n");
  228. return -EINVAL;
  229. }
  230. rx_buf_size = mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  231. rx_buf_size = clamp_val(rx_buf_size, XGBE_RX_MIN_BUF_SIZE, PAGE_SIZE);
  232. rx_buf_size = (rx_buf_size + XGBE_RX_BUF_ALIGN - 1) &
  233. ~(XGBE_RX_BUF_ALIGN - 1);
  234. return rx_buf_size;
  235. }
  236. static void xgbe_enable_rx_tx_ints(struct xgbe_prv_data *pdata)
  237. {
  238. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  239. struct xgbe_channel *channel;
  240. enum xgbe_int int_id;
  241. unsigned int i;
  242. channel = pdata->channel;
  243. for (i = 0; i < pdata->channel_count; i++, channel++) {
  244. if (channel->tx_ring && channel->rx_ring)
  245. int_id = XGMAC_INT_DMA_CH_SR_TI_RI;
  246. else if (channel->tx_ring)
  247. int_id = XGMAC_INT_DMA_CH_SR_TI;
  248. else if (channel->rx_ring)
  249. int_id = XGMAC_INT_DMA_CH_SR_RI;
  250. else
  251. continue;
  252. hw_if->enable_int(channel, int_id);
  253. }
  254. }
  255. static void xgbe_disable_rx_tx_ints(struct xgbe_prv_data *pdata)
  256. {
  257. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  258. struct xgbe_channel *channel;
  259. enum xgbe_int int_id;
  260. unsigned int i;
  261. channel = pdata->channel;
  262. for (i = 0; i < pdata->channel_count; i++, channel++) {
  263. if (channel->tx_ring && channel->rx_ring)
  264. int_id = XGMAC_INT_DMA_CH_SR_TI_RI;
  265. else if (channel->tx_ring)
  266. int_id = XGMAC_INT_DMA_CH_SR_TI;
  267. else if (channel->rx_ring)
  268. int_id = XGMAC_INT_DMA_CH_SR_RI;
  269. else
  270. continue;
  271. hw_if->disable_int(channel, int_id);
  272. }
  273. }
  274. static irqreturn_t xgbe_isr(int irq, void *data)
  275. {
  276. struct xgbe_prv_data *pdata = data;
  277. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  278. struct xgbe_channel *channel;
  279. unsigned int dma_isr, dma_ch_isr;
  280. unsigned int mac_isr, mac_tssr;
  281. unsigned int i;
  282. /* The DMA interrupt status register also reports MAC and MTL
  283. * interrupts. So for polling mode, we just need to check for
  284. * this register to be non-zero
  285. */
  286. dma_isr = XGMAC_IOREAD(pdata, DMA_ISR);
  287. if (!dma_isr)
  288. goto isr_done;
  289. DBGPR(" DMA_ISR = %08x\n", dma_isr);
  290. for (i = 0; i < pdata->channel_count; i++) {
  291. if (!(dma_isr & (1 << i)))
  292. continue;
  293. channel = pdata->channel + i;
  294. dma_ch_isr = XGMAC_DMA_IOREAD(channel, DMA_CH_SR);
  295. DBGPR(" DMA_CH%u_ISR = %08x\n", i, dma_ch_isr);
  296. /* The TI or RI interrupt bits may still be set even if using
  297. * per channel DMA interrupts. Check to be sure those are not
  298. * enabled before using the private data napi structure.
  299. */
  300. if (!pdata->per_channel_irq &&
  301. (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, TI) ||
  302. XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, RI))) {
  303. if (napi_schedule_prep(&pdata->napi)) {
  304. /* Disable Tx and Rx interrupts */
  305. xgbe_disable_rx_tx_ints(pdata);
  306. /* Turn on polling */
  307. __napi_schedule(&pdata->napi);
  308. }
  309. }
  310. /* Restart the device on a Fatal Bus Error */
  311. if (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, FBE))
  312. schedule_work(&pdata->restart_work);
  313. /* Clear all interrupt signals */
  314. XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_ch_isr);
  315. }
  316. if (XGMAC_GET_BITS(dma_isr, DMA_ISR, MACIS)) {
  317. mac_isr = XGMAC_IOREAD(pdata, MAC_ISR);
  318. if (XGMAC_GET_BITS(mac_isr, MAC_ISR, MMCTXIS))
  319. hw_if->tx_mmc_int(pdata);
  320. if (XGMAC_GET_BITS(mac_isr, MAC_ISR, MMCRXIS))
  321. hw_if->rx_mmc_int(pdata);
  322. if (XGMAC_GET_BITS(mac_isr, MAC_ISR, TSIS)) {
  323. mac_tssr = XGMAC_IOREAD(pdata, MAC_TSSR);
  324. if (XGMAC_GET_BITS(mac_tssr, MAC_TSSR, TXTSC)) {
  325. /* Read Tx Timestamp to clear interrupt */
  326. pdata->tx_tstamp =
  327. hw_if->get_tx_tstamp(pdata);
  328. schedule_work(&pdata->tx_tstamp_work);
  329. }
  330. }
  331. }
  332. DBGPR(" DMA_ISR = %08x\n", XGMAC_IOREAD(pdata, DMA_ISR));
  333. isr_done:
  334. return IRQ_HANDLED;
  335. }
  336. static irqreturn_t xgbe_dma_isr(int irq, void *data)
  337. {
  338. struct xgbe_channel *channel = data;
  339. /* Per channel DMA interrupts are enabled, so we use the per
  340. * channel napi structure and not the private data napi structure
  341. */
  342. if (napi_schedule_prep(&channel->napi)) {
  343. /* Disable Tx and Rx interrupts */
  344. disable_irq_nosync(channel->dma_irq);
  345. /* Turn on polling */
  346. __napi_schedule(&channel->napi);
  347. }
  348. return IRQ_HANDLED;
  349. }
  350. static void xgbe_tx_timer(unsigned long data)
  351. {
  352. struct xgbe_channel *channel = (struct xgbe_channel *)data;
  353. struct xgbe_prv_data *pdata = channel->pdata;
  354. struct napi_struct *napi;
  355. DBGPR("-->xgbe_tx_timer\n");
  356. napi = (pdata->per_channel_irq) ? &channel->napi : &pdata->napi;
  357. if (napi_schedule_prep(napi)) {
  358. /* Disable Tx and Rx interrupts */
  359. if (pdata->per_channel_irq)
  360. disable_irq_nosync(channel->dma_irq);
  361. else
  362. xgbe_disable_rx_tx_ints(pdata);
  363. /* Turn on polling */
  364. __napi_schedule(napi);
  365. }
  366. channel->tx_timer_active = 0;
  367. DBGPR("<--xgbe_tx_timer\n");
  368. }
  369. static void xgbe_init_tx_timers(struct xgbe_prv_data *pdata)
  370. {
  371. struct xgbe_channel *channel;
  372. unsigned int i;
  373. DBGPR("-->xgbe_init_tx_timers\n");
  374. channel = pdata->channel;
  375. for (i = 0; i < pdata->channel_count; i++, channel++) {
  376. if (!channel->tx_ring)
  377. break;
  378. DBGPR(" %s adding tx timer\n", channel->name);
  379. setup_timer(&channel->tx_timer, xgbe_tx_timer,
  380. (unsigned long)channel);
  381. }
  382. DBGPR("<--xgbe_init_tx_timers\n");
  383. }
  384. static void xgbe_stop_tx_timers(struct xgbe_prv_data *pdata)
  385. {
  386. struct xgbe_channel *channel;
  387. unsigned int i;
  388. DBGPR("-->xgbe_stop_tx_timers\n");
  389. channel = pdata->channel;
  390. for (i = 0; i < pdata->channel_count; i++, channel++) {
  391. if (!channel->tx_ring)
  392. break;
  393. DBGPR(" %s deleting tx timer\n", channel->name);
  394. del_timer_sync(&channel->tx_timer);
  395. }
  396. DBGPR("<--xgbe_stop_tx_timers\n");
  397. }
  398. void xgbe_get_all_hw_features(struct xgbe_prv_data *pdata)
  399. {
  400. unsigned int mac_hfr0, mac_hfr1, mac_hfr2;
  401. struct xgbe_hw_features *hw_feat = &pdata->hw_feat;
  402. DBGPR("-->xgbe_get_all_hw_features\n");
  403. mac_hfr0 = XGMAC_IOREAD(pdata, MAC_HWF0R);
  404. mac_hfr1 = XGMAC_IOREAD(pdata, MAC_HWF1R);
  405. mac_hfr2 = XGMAC_IOREAD(pdata, MAC_HWF2R);
  406. memset(hw_feat, 0, sizeof(*hw_feat));
  407. hw_feat->version = XGMAC_IOREAD(pdata, MAC_VR);
  408. /* Hardware feature register 0 */
  409. hw_feat->gmii = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, GMIISEL);
  410. hw_feat->vlhash = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, VLHASH);
  411. hw_feat->sma = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SMASEL);
  412. hw_feat->rwk = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RWKSEL);
  413. hw_feat->mgk = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MGKSEL);
  414. hw_feat->mmc = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MMCSEL);
  415. hw_feat->aoe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, ARPOFFSEL);
  416. hw_feat->ts = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSEL);
  417. hw_feat->eee = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, EEESEL);
  418. hw_feat->tx_coe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TXCOESEL);
  419. hw_feat->rx_coe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RXCOESEL);
  420. hw_feat->addn_mac = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R,
  421. ADDMACADRSEL);
  422. hw_feat->ts_src = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSTSSEL);
  423. hw_feat->sa_vlan_ins = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SAVLANINS);
  424. /* Hardware feature register 1 */
  425. hw_feat->rx_fifo_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
  426. RXFIFOSIZE);
  427. hw_feat->tx_fifo_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
  428. TXFIFOSIZE);
  429. hw_feat->dma_width = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, ADDR64);
  430. hw_feat->dcb = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DCBEN);
  431. hw_feat->sph = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, SPHEN);
  432. hw_feat->tso = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, TSOEN);
  433. hw_feat->dma_debug = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DBGMEMA);
  434. hw_feat->rss = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, RSSEN);
  435. hw_feat->tc_cnt = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, NUMTC);
  436. hw_feat->hash_table_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
  437. HASHTBLSZ);
  438. hw_feat->l3l4_filter_num = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
  439. L3L4FNUM);
  440. /* Hardware feature register 2 */
  441. hw_feat->rx_q_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXQCNT);
  442. hw_feat->tx_q_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXQCNT);
  443. hw_feat->rx_ch_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXCHCNT);
  444. hw_feat->tx_ch_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXCHCNT);
  445. hw_feat->pps_out_num = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, PPSOUTNUM);
  446. hw_feat->aux_snap_num = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, AUXSNAPNUM);
  447. /* Translate the Hash Table size into actual number */
  448. switch (hw_feat->hash_table_size) {
  449. case 0:
  450. break;
  451. case 1:
  452. hw_feat->hash_table_size = 64;
  453. break;
  454. case 2:
  455. hw_feat->hash_table_size = 128;
  456. break;
  457. case 3:
  458. hw_feat->hash_table_size = 256;
  459. break;
  460. }
  461. /* Translate the address width setting into actual number */
  462. switch (hw_feat->dma_width) {
  463. case 0:
  464. hw_feat->dma_width = 32;
  465. break;
  466. case 1:
  467. hw_feat->dma_width = 40;
  468. break;
  469. case 2:
  470. hw_feat->dma_width = 48;
  471. break;
  472. default:
  473. hw_feat->dma_width = 32;
  474. }
  475. /* The Queue, Channel and TC counts are zero based so increment them
  476. * to get the actual number
  477. */
  478. hw_feat->rx_q_cnt++;
  479. hw_feat->tx_q_cnt++;
  480. hw_feat->rx_ch_cnt++;
  481. hw_feat->tx_ch_cnt++;
  482. hw_feat->tc_cnt++;
  483. DBGPR("<--xgbe_get_all_hw_features\n");
  484. }
  485. static void xgbe_napi_enable(struct xgbe_prv_data *pdata, unsigned int add)
  486. {
  487. struct xgbe_channel *channel;
  488. unsigned int i;
  489. if (pdata->per_channel_irq) {
  490. channel = pdata->channel;
  491. for (i = 0; i < pdata->channel_count; i++, channel++) {
  492. if (add)
  493. netif_napi_add(pdata->netdev, &channel->napi,
  494. xgbe_one_poll, NAPI_POLL_WEIGHT);
  495. napi_enable(&channel->napi);
  496. }
  497. } else {
  498. if (add)
  499. netif_napi_add(pdata->netdev, &pdata->napi,
  500. xgbe_all_poll, NAPI_POLL_WEIGHT);
  501. napi_enable(&pdata->napi);
  502. }
  503. }
  504. static void xgbe_napi_disable(struct xgbe_prv_data *pdata, unsigned int del)
  505. {
  506. struct xgbe_channel *channel;
  507. unsigned int i;
  508. if (pdata->per_channel_irq) {
  509. channel = pdata->channel;
  510. for (i = 0; i < pdata->channel_count; i++, channel++) {
  511. napi_disable(&channel->napi);
  512. if (del)
  513. netif_napi_del(&channel->napi);
  514. }
  515. } else {
  516. napi_disable(&pdata->napi);
  517. if (del)
  518. netif_napi_del(&pdata->napi);
  519. }
  520. }
  521. static int xgbe_request_irqs(struct xgbe_prv_data *pdata)
  522. {
  523. struct xgbe_channel *channel;
  524. struct net_device *netdev = pdata->netdev;
  525. unsigned int i;
  526. int ret;
  527. ret = devm_request_irq(pdata->dev, pdata->dev_irq, xgbe_isr, 0,
  528. netdev->name, pdata);
  529. if (ret) {
  530. netdev_alert(netdev, "error requesting irq %d\n",
  531. pdata->dev_irq);
  532. return ret;
  533. }
  534. if (!pdata->per_channel_irq)
  535. return 0;
  536. channel = pdata->channel;
  537. for (i = 0; i < pdata->channel_count; i++, channel++) {
  538. snprintf(channel->dma_irq_name,
  539. sizeof(channel->dma_irq_name) - 1,
  540. "%s-TxRx-%u", netdev_name(netdev),
  541. channel->queue_index);
  542. ret = devm_request_irq(pdata->dev, channel->dma_irq,
  543. xgbe_dma_isr, 0,
  544. channel->dma_irq_name, channel);
  545. if (ret) {
  546. netdev_alert(netdev, "error requesting irq %d\n",
  547. channel->dma_irq);
  548. goto err_irq;
  549. }
  550. }
  551. return 0;
  552. err_irq:
  553. /* Using an unsigned int, 'i' will go to UINT_MAX and exit */
  554. for (i--, channel--; i < pdata->channel_count; i--, channel--)
  555. devm_free_irq(pdata->dev, channel->dma_irq, channel);
  556. devm_free_irq(pdata->dev, pdata->dev_irq, pdata);
  557. return ret;
  558. }
  559. static void xgbe_free_irqs(struct xgbe_prv_data *pdata)
  560. {
  561. struct xgbe_channel *channel;
  562. unsigned int i;
  563. devm_free_irq(pdata->dev, pdata->dev_irq, pdata);
  564. if (!pdata->per_channel_irq)
  565. return;
  566. channel = pdata->channel;
  567. for (i = 0; i < pdata->channel_count; i++, channel++)
  568. devm_free_irq(pdata->dev, channel->dma_irq, channel);
  569. }
  570. void xgbe_init_tx_coalesce(struct xgbe_prv_data *pdata)
  571. {
  572. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  573. DBGPR("-->xgbe_init_tx_coalesce\n");
  574. pdata->tx_usecs = XGMAC_INIT_DMA_TX_USECS;
  575. pdata->tx_frames = XGMAC_INIT_DMA_TX_FRAMES;
  576. hw_if->config_tx_coalesce(pdata);
  577. DBGPR("<--xgbe_init_tx_coalesce\n");
  578. }
  579. void xgbe_init_rx_coalesce(struct xgbe_prv_data *pdata)
  580. {
  581. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  582. DBGPR("-->xgbe_init_rx_coalesce\n");
  583. pdata->rx_riwt = hw_if->usec_to_riwt(pdata, XGMAC_INIT_DMA_RX_USECS);
  584. pdata->rx_usecs = XGMAC_INIT_DMA_RX_USECS;
  585. pdata->rx_frames = XGMAC_INIT_DMA_RX_FRAMES;
  586. hw_if->config_rx_coalesce(pdata);
  587. DBGPR("<--xgbe_init_rx_coalesce\n");
  588. }
  589. static void xgbe_free_tx_data(struct xgbe_prv_data *pdata)
  590. {
  591. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  592. struct xgbe_channel *channel;
  593. struct xgbe_ring *ring;
  594. struct xgbe_ring_data *rdata;
  595. unsigned int i, j;
  596. DBGPR("-->xgbe_free_tx_data\n");
  597. channel = pdata->channel;
  598. for (i = 0; i < pdata->channel_count; i++, channel++) {
  599. ring = channel->tx_ring;
  600. if (!ring)
  601. break;
  602. for (j = 0; j < ring->rdesc_count; j++) {
  603. rdata = XGBE_GET_DESC_DATA(ring, j);
  604. desc_if->unmap_rdata(pdata, rdata);
  605. }
  606. }
  607. DBGPR("<--xgbe_free_tx_data\n");
  608. }
  609. static void xgbe_free_rx_data(struct xgbe_prv_data *pdata)
  610. {
  611. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  612. struct xgbe_channel *channel;
  613. struct xgbe_ring *ring;
  614. struct xgbe_ring_data *rdata;
  615. unsigned int i, j;
  616. DBGPR("-->xgbe_free_rx_data\n");
  617. channel = pdata->channel;
  618. for (i = 0; i < pdata->channel_count; i++, channel++) {
  619. ring = channel->rx_ring;
  620. if (!ring)
  621. break;
  622. for (j = 0; j < ring->rdesc_count; j++) {
  623. rdata = XGBE_GET_DESC_DATA(ring, j);
  624. desc_if->unmap_rdata(pdata, rdata);
  625. }
  626. }
  627. DBGPR("<--xgbe_free_rx_data\n");
  628. }
  629. static void xgbe_adjust_link(struct net_device *netdev)
  630. {
  631. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  632. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  633. struct phy_device *phydev = pdata->phydev;
  634. int new_state = 0;
  635. if (!phydev)
  636. return;
  637. if (phydev->link) {
  638. /* Flow control support */
  639. if (pdata->pause_autoneg) {
  640. if (phydev->pause || phydev->asym_pause) {
  641. pdata->tx_pause = 1;
  642. pdata->rx_pause = 1;
  643. } else {
  644. pdata->tx_pause = 0;
  645. pdata->rx_pause = 0;
  646. }
  647. }
  648. if (pdata->tx_pause != pdata->phy_tx_pause) {
  649. hw_if->config_tx_flow_control(pdata);
  650. pdata->phy_tx_pause = pdata->tx_pause;
  651. }
  652. if (pdata->rx_pause != pdata->phy_rx_pause) {
  653. hw_if->config_rx_flow_control(pdata);
  654. pdata->phy_rx_pause = pdata->rx_pause;
  655. }
  656. /* Speed support */
  657. if (phydev->speed != pdata->phy_speed) {
  658. new_state = 1;
  659. switch (phydev->speed) {
  660. case SPEED_10000:
  661. hw_if->set_xgmii_speed(pdata);
  662. break;
  663. case SPEED_2500:
  664. hw_if->set_gmii_2500_speed(pdata);
  665. break;
  666. case SPEED_1000:
  667. hw_if->set_gmii_speed(pdata);
  668. break;
  669. }
  670. pdata->phy_speed = phydev->speed;
  671. }
  672. if (phydev->link != pdata->phy_link) {
  673. new_state = 1;
  674. pdata->phy_link = 1;
  675. }
  676. } else if (pdata->phy_link) {
  677. new_state = 1;
  678. pdata->phy_link = 0;
  679. pdata->phy_speed = SPEED_UNKNOWN;
  680. }
  681. if (new_state)
  682. phy_print_status(phydev);
  683. }
  684. static int xgbe_phy_init(struct xgbe_prv_data *pdata)
  685. {
  686. struct net_device *netdev = pdata->netdev;
  687. struct phy_device *phydev = pdata->phydev;
  688. int ret;
  689. pdata->phy_link = -1;
  690. pdata->phy_speed = SPEED_UNKNOWN;
  691. pdata->phy_tx_pause = pdata->tx_pause;
  692. pdata->phy_rx_pause = pdata->rx_pause;
  693. ret = phy_connect_direct(netdev, phydev, &xgbe_adjust_link,
  694. pdata->phy_mode);
  695. if (ret) {
  696. netdev_err(netdev, "phy_connect_direct failed\n");
  697. return ret;
  698. }
  699. if (!phydev->drv || (phydev->drv->phy_id == 0)) {
  700. netdev_err(netdev, "phy_id not valid\n");
  701. ret = -ENODEV;
  702. goto err_phy_connect;
  703. }
  704. DBGPR(" phy_connect_direct succeeded for PHY %s, link=%d\n",
  705. dev_name(&phydev->dev), phydev->link);
  706. return 0;
  707. err_phy_connect:
  708. phy_disconnect(phydev);
  709. return ret;
  710. }
  711. static void xgbe_phy_exit(struct xgbe_prv_data *pdata)
  712. {
  713. if (!pdata->phydev)
  714. return;
  715. phy_disconnect(pdata->phydev);
  716. }
  717. int xgbe_powerdown(struct net_device *netdev, unsigned int caller)
  718. {
  719. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  720. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  721. unsigned long flags;
  722. DBGPR("-->xgbe_powerdown\n");
  723. if (!netif_running(netdev) ||
  724. (caller == XGMAC_IOCTL_CONTEXT && pdata->power_down)) {
  725. netdev_alert(netdev, "Device is already powered down\n");
  726. DBGPR("<--xgbe_powerdown\n");
  727. return -EINVAL;
  728. }
  729. spin_lock_irqsave(&pdata->lock, flags);
  730. if (caller == XGMAC_DRIVER_CONTEXT)
  731. netif_device_detach(netdev);
  732. netif_tx_stop_all_queues(netdev);
  733. hw_if->powerdown_tx(pdata);
  734. hw_if->powerdown_rx(pdata);
  735. xgbe_napi_disable(pdata, 0);
  736. phy_stop(pdata->phydev);
  737. pdata->power_down = 1;
  738. spin_unlock_irqrestore(&pdata->lock, flags);
  739. DBGPR("<--xgbe_powerdown\n");
  740. return 0;
  741. }
  742. int xgbe_powerup(struct net_device *netdev, unsigned int caller)
  743. {
  744. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  745. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  746. unsigned long flags;
  747. DBGPR("-->xgbe_powerup\n");
  748. if (!netif_running(netdev) ||
  749. (caller == XGMAC_IOCTL_CONTEXT && !pdata->power_down)) {
  750. netdev_alert(netdev, "Device is already powered up\n");
  751. DBGPR("<--xgbe_powerup\n");
  752. return -EINVAL;
  753. }
  754. spin_lock_irqsave(&pdata->lock, flags);
  755. pdata->power_down = 0;
  756. phy_start(pdata->phydev);
  757. xgbe_napi_enable(pdata, 0);
  758. hw_if->powerup_tx(pdata);
  759. hw_if->powerup_rx(pdata);
  760. if (caller == XGMAC_DRIVER_CONTEXT)
  761. netif_device_attach(netdev);
  762. netif_tx_start_all_queues(netdev);
  763. spin_unlock_irqrestore(&pdata->lock, flags);
  764. DBGPR("<--xgbe_powerup\n");
  765. return 0;
  766. }
  767. static int xgbe_start(struct xgbe_prv_data *pdata)
  768. {
  769. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  770. struct net_device *netdev = pdata->netdev;
  771. int ret;
  772. DBGPR("-->xgbe_start\n");
  773. hw_if->init(pdata);
  774. phy_start(pdata->phydev);
  775. xgbe_napi_enable(pdata, 1);
  776. ret = xgbe_request_irqs(pdata);
  777. if (ret)
  778. goto err_napi;
  779. hw_if->enable_tx(pdata);
  780. hw_if->enable_rx(pdata);
  781. xgbe_init_tx_timers(pdata);
  782. netif_tx_start_all_queues(netdev);
  783. DBGPR("<--xgbe_start\n");
  784. return 0;
  785. err_napi:
  786. xgbe_napi_disable(pdata, 1);
  787. phy_stop(pdata->phydev);
  788. hw_if->exit(pdata);
  789. return ret;
  790. }
  791. static void xgbe_stop(struct xgbe_prv_data *pdata)
  792. {
  793. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  794. struct xgbe_channel *channel;
  795. struct net_device *netdev = pdata->netdev;
  796. struct netdev_queue *txq;
  797. unsigned int i;
  798. DBGPR("-->xgbe_stop\n");
  799. netif_tx_stop_all_queues(netdev);
  800. xgbe_stop_tx_timers(pdata);
  801. hw_if->disable_tx(pdata);
  802. hw_if->disable_rx(pdata);
  803. xgbe_free_irqs(pdata);
  804. xgbe_napi_disable(pdata, 1);
  805. phy_stop(pdata->phydev);
  806. hw_if->exit(pdata);
  807. channel = pdata->channel;
  808. for (i = 0; i < pdata->channel_count; i++, channel++) {
  809. if (!channel->tx_ring)
  810. continue;
  811. txq = netdev_get_tx_queue(netdev, channel->queue_index);
  812. netdev_tx_reset_queue(txq);
  813. }
  814. DBGPR("<--xgbe_stop\n");
  815. }
  816. static void xgbe_restart_dev(struct xgbe_prv_data *pdata)
  817. {
  818. DBGPR("-->xgbe_restart_dev\n");
  819. /* If not running, "restart" will happen on open */
  820. if (!netif_running(pdata->netdev))
  821. return;
  822. xgbe_stop(pdata);
  823. xgbe_free_tx_data(pdata);
  824. xgbe_free_rx_data(pdata);
  825. xgbe_start(pdata);
  826. DBGPR("<--xgbe_restart_dev\n");
  827. }
  828. static void xgbe_restart(struct work_struct *work)
  829. {
  830. struct xgbe_prv_data *pdata = container_of(work,
  831. struct xgbe_prv_data,
  832. restart_work);
  833. rtnl_lock();
  834. xgbe_restart_dev(pdata);
  835. rtnl_unlock();
  836. }
  837. static void xgbe_tx_tstamp(struct work_struct *work)
  838. {
  839. struct xgbe_prv_data *pdata = container_of(work,
  840. struct xgbe_prv_data,
  841. tx_tstamp_work);
  842. struct skb_shared_hwtstamps hwtstamps;
  843. u64 nsec;
  844. unsigned long flags;
  845. if (pdata->tx_tstamp) {
  846. nsec = timecounter_cyc2time(&pdata->tstamp_tc,
  847. pdata->tx_tstamp);
  848. memset(&hwtstamps, 0, sizeof(hwtstamps));
  849. hwtstamps.hwtstamp = ns_to_ktime(nsec);
  850. skb_tstamp_tx(pdata->tx_tstamp_skb, &hwtstamps);
  851. }
  852. dev_kfree_skb_any(pdata->tx_tstamp_skb);
  853. spin_lock_irqsave(&pdata->tstamp_lock, flags);
  854. pdata->tx_tstamp_skb = NULL;
  855. spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
  856. }
  857. static int xgbe_get_hwtstamp_settings(struct xgbe_prv_data *pdata,
  858. struct ifreq *ifreq)
  859. {
  860. if (copy_to_user(ifreq->ifr_data, &pdata->tstamp_config,
  861. sizeof(pdata->tstamp_config)))
  862. return -EFAULT;
  863. return 0;
  864. }
  865. static int xgbe_set_hwtstamp_settings(struct xgbe_prv_data *pdata,
  866. struct ifreq *ifreq)
  867. {
  868. struct hwtstamp_config config;
  869. unsigned int mac_tscr;
  870. if (copy_from_user(&config, ifreq->ifr_data, sizeof(config)))
  871. return -EFAULT;
  872. if (config.flags)
  873. return -EINVAL;
  874. mac_tscr = 0;
  875. switch (config.tx_type) {
  876. case HWTSTAMP_TX_OFF:
  877. break;
  878. case HWTSTAMP_TX_ON:
  879. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  880. break;
  881. default:
  882. return -ERANGE;
  883. }
  884. switch (config.rx_filter) {
  885. case HWTSTAMP_FILTER_NONE:
  886. break;
  887. case HWTSTAMP_FILTER_ALL:
  888. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENALL, 1);
  889. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  890. break;
  891. /* PTP v2, UDP, any kind of event packet */
  892. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  893. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
  894. /* PTP v1, UDP, any kind of event packet */
  895. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  896. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
  897. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
  898. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
  899. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  900. break;
  901. /* PTP v2, UDP, Sync packet */
  902. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  903. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
  904. /* PTP v1, UDP, Sync packet */
  905. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  906. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
  907. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
  908. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
  909. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  910. break;
  911. /* PTP v2, UDP, Delay_req packet */
  912. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  913. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
  914. /* PTP v1, UDP, Delay_req packet */
  915. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  916. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
  917. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
  918. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
  919. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
  920. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  921. break;
  922. /* 802.AS1, Ethernet, any kind of event packet */
  923. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  924. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
  925. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
  926. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  927. break;
  928. /* 802.AS1, Ethernet, Sync packet */
  929. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  930. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
  931. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
  932. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  933. break;
  934. /* 802.AS1, Ethernet, Delay_req packet */
  935. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  936. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
  937. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
  938. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
  939. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  940. break;
  941. /* PTP v2/802.AS1, any layer, any kind of event packet */
  942. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  943. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
  944. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
  945. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
  946. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
  947. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
  948. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  949. break;
  950. /* PTP v2/802.AS1, any layer, Sync packet */
  951. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  952. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
  953. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
  954. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
  955. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
  956. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
  957. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  958. break;
  959. /* PTP v2/802.AS1, any layer, Delay_req packet */
  960. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  961. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
  962. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
  963. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
  964. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
  965. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
  966. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
  967. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  968. break;
  969. default:
  970. return -ERANGE;
  971. }
  972. pdata->hw_if.config_tstamp(pdata, mac_tscr);
  973. memcpy(&pdata->tstamp_config, &config, sizeof(config));
  974. return 0;
  975. }
  976. static void xgbe_prep_tx_tstamp(struct xgbe_prv_data *pdata,
  977. struct sk_buff *skb,
  978. struct xgbe_packet_data *packet)
  979. {
  980. unsigned long flags;
  981. if (XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP)) {
  982. spin_lock_irqsave(&pdata->tstamp_lock, flags);
  983. if (pdata->tx_tstamp_skb) {
  984. /* Another timestamp in progress, ignore this one */
  985. XGMAC_SET_BITS(packet->attributes,
  986. TX_PACKET_ATTRIBUTES, PTP, 0);
  987. } else {
  988. pdata->tx_tstamp_skb = skb_get(skb);
  989. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  990. }
  991. spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
  992. }
  993. if (!XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP))
  994. skb_tx_timestamp(skb);
  995. }
  996. static void xgbe_prep_vlan(struct sk_buff *skb, struct xgbe_packet_data *packet)
  997. {
  998. if (skb_vlan_tag_present(skb))
  999. packet->vlan_ctag = skb_vlan_tag_get(skb);
  1000. }
  1001. static int xgbe_prep_tso(struct sk_buff *skb, struct xgbe_packet_data *packet)
  1002. {
  1003. int ret;
  1004. if (!XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1005. TSO_ENABLE))
  1006. return 0;
  1007. ret = skb_cow_head(skb, 0);
  1008. if (ret)
  1009. return ret;
  1010. packet->header_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1011. packet->tcp_header_len = tcp_hdrlen(skb);
  1012. packet->tcp_payload_len = skb->len - packet->header_len;
  1013. packet->mss = skb_shinfo(skb)->gso_size;
  1014. DBGPR(" packet->header_len=%u\n", packet->header_len);
  1015. DBGPR(" packet->tcp_header_len=%u, packet->tcp_payload_len=%u\n",
  1016. packet->tcp_header_len, packet->tcp_payload_len);
  1017. DBGPR(" packet->mss=%u\n", packet->mss);
  1018. /* Update the number of packets that will ultimately be transmitted
  1019. * along with the extra bytes for each extra packet
  1020. */
  1021. packet->tx_packets = skb_shinfo(skb)->gso_segs;
  1022. packet->tx_bytes += (packet->tx_packets - 1) * packet->header_len;
  1023. return 0;
  1024. }
  1025. static int xgbe_is_tso(struct sk_buff *skb)
  1026. {
  1027. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1028. return 0;
  1029. if (!skb_is_gso(skb))
  1030. return 0;
  1031. DBGPR(" TSO packet to be processed\n");
  1032. return 1;
  1033. }
  1034. static void xgbe_packet_info(struct xgbe_prv_data *pdata,
  1035. struct xgbe_ring *ring, struct sk_buff *skb,
  1036. struct xgbe_packet_data *packet)
  1037. {
  1038. struct skb_frag_struct *frag;
  1039. unsigned int context_desc;
  1040. unsigned int len;
  1041. unsigned int i;
  1042. packet->skb = skb;
  1043. context_desc = 0;
  1044. packet->rdesc_count = 0;
  1045. packet->tx_packets = 1;
  1046. packet->tx_bytes = skb->len;
  1047. if (xgbe_is_tso(skb)) {
  1048. /* TSO requires an extra descriptor if mss is different */
  1049. if (skb_shinfo(skb)->gso_size != ring->tx.cur_mss) {
  1050. context_desc = 1;
  1051. packet->rdesc_count++;
  1052. }
  1053. /* TSO requires an extra descriptor for TSO header */
  1054. packet->rdesc_count++;
  1055. XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1056. TSO_ENABLE, 1);
  1057. XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1058. CSUM_ENABLE, 1);
  1059. } else if (skb->ip_summed == CHECKSUM_PARTIAL)
  1060. XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1061. CSUM_ENABLE, 1);
  1062. if (skb_vlan_tag_present(skb)) {
  1063. /* VLAN requires an extra descriptor if tag is different */
  1064. if (skb_vlan_tag_get(skb) != ring->tx.cur_vlan_ctag)
  1065. /* We can share with the TSO context descriptor */
  1066. if (!context_desc) {
  1067. context_desc = 1;
  1068. packet->rdesc_count++;
  1069. }
  1070. XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1071. VLAN_CTAG, 1);
  1072. }
  1073. if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
  1074. (pdata->tstamp_config.tx_type == HWTSTAMP_TX_ON))
  1075. XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1076. PTP, 1);
  1077. for (len = skb_headlen(skb); len;) {
  1078. packet->rdesc_count++;
  1079. len -= min_t(unsigned int, len, XGBE_TX_MAX_BUF_SIZE);
  1080. }
  1081. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1082. frag = &skb_shinfo(skb)->frags[i];
  1083. for (len = skb_frag_size(frag); len; ) {
  1084. packet->rdesc_count++;
  1085. len -= min_t(unsigned int, len, XGBE_TX_MAX_BUF_SIZE);
  1086. }
  1087. }
  1088. }
  1089. static int xgbe_open(struct net_device *netdev)
  1090. {
  1091. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1092. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  1093. int ret;
  1094. DBGPR("-->xgbe_open\n");
  1095. /* Initialize the phy */
  1096. ret = xgbe_phy_init(pdata);
  1097. if (ret)
  1098. return ret;
  1099. /* Enable the clocks */
  1100. ret = clk_prepare_enable(pdata->sysclk);
  1101. if (ret) {
  1102. netdev_alert(netdev, "dma clk_prepare_enable failed\n");
  1103. goto err_phy_init;
  1104. }
  1105. ret = clk_prepare_enable(pdata->ptpclk);
  1106. if (ret) {
  1107. netdev_alert(netdev, "ptp clk_prepare_enable failed\n");
  1108. goto err_sysclk;
  1109. }
  1110. /* Calculate the Rx buffer size before allocating rings */
  1111. ret = xgbe_calc_rx_buf_size(netdev, netdev->mtu);
  1112. if (ret < 0)
  1113. goto err_ptpclk;
  1114. pdata->rx_buf_size = ret;
  1115. /* Allocate the channel and ring structures */
  1116. ret = xgbe_alloc_channels(pdata);
  1117. if (ret)
  1118. goto err_ptpclk;
  1119. /* Allocate the ring descriptors and buffers */
  1120. ret = desc_if->alloc_ring_resources(pdata);
  1121. if (ret)
  1122. goto err_channels;
  1123. /* Initialize the device restart and Tx timestamp work struct */
  1124. INIT_WORK(&pdata->restart_work, xgbe_restart);
  1125. INIT_WORK(&pdata->tx_tstamp_work, xgbe_tx_tstamp);
  1126. ret = xgbe_start(pdata);
  1127. if (ret)
  1128. goto err_rings;
  1129. DBGPR("<--xgbe_open\n");
  1130. return 0;
  1131. err_rings:
  1132. desc_if->free_ring_resources(pdata);
  1133. err_channels:
  1134. xgbe_free_channels(pdata);
  1135. err_ptpclk:
  1136. clk_disable_unprepare(pdata->ptpclk);
  1137. err_sysclk:
  1138. clk_disable_unprepare(pdata->sysclk);
  1139. err_phy_init:
  1140. xgbe_phy_exit(pdata);
  1141. return ret;
  1142. }
  1143. static int xgbe_close(struct net_device *netdev)
  1144. {
  1145. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1146. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  1147. DBGPR("-->xgbe_close\n");
  1148. /* Stop the device */
  1149. xgbe_stop(pdata);
  1150. /* Free the ring descriptors and buffers */
  1151. desc_if->free_ring_resources(pdata);
  1152. /* Free the channel and ring structures */
  1153. xgbe_free_channels(pdata);
  1154. /* Disable the clocks */
  1155. clk_disable_unprepare(pdata->ptpclk);
  1156. clk_disable_unprepare(pdata->sysclk);
  1157. /* Release the phy */
  1158. xgbe_phy_exit(pdata);
  1159. DBGPR("<--xgbe_close\n");
  1160. return 0;
  1161. }
  1162. static int xgbe_xmit(struct sk_buff *skb, struct net_device *netdev)
  1163. {
  1164. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1165. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1166. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  1167. struct xgbe_channel *channel;
  1168. struct xgbe_ring *ring;
  1169. struct xgbe_packet_data *packet;
  1170. struct netdev_queue *txq;
  1171. int ret;
  1172. DBGPR("-->xgbe_xmit: skb->len = %d\n", skb->len);
  1173. channel = pdata->channel + skb->queue_mapping;
  1174. txq = netdev_get_tx_queue(netdev, channel->queue_index);
  1175. ring = channel->tx_ring;
  1176. packet = &ring->packet_data;
  1177. ret = NETDEV_TX_OK;
  1178. if (skb->len == 0) {
  1179. netdev_err(netdev, "empty skb received from stack\n");
  1180. dev_kfree_skb_any(skb);
  1181. goto tx_netdev_return;
  1182. }
  1183. /* Calculate preliminary packet info */
  1184. memset(packet, 0, sizeof(*packet));
  1185. xgbe_packet_info(pdata, ring, skb, packet);
  1186. /* Check that there are enough descriptors available */
  1187. ret = xgbe_maybe_stop_tx_queue(channel, ring, packet->rdesc_count);
  1188. if (ret)
  1189. goto tx_netdev_return;
  1190. ret = xgbe_prep_tso(skb, packet);
  1191. if (ret) {
  1192. netdev_err(netdev, "error processing TSO packet\n");
  1193. dev_kfree_skb_any(skb);
  1194. goto tx_netdev_return;
  1195. }
  1196. xgbe_prep_vlan(skb, packet);
  1197. if (!desc_if->map_tx_skb(channel, skb)) {
  1198. dev_kfree_skb_any(skb);
  1199. goto tx_netdev_return;
  1200. }
  1201. xgbe_prep_tx_tstamp(pdata, skb, packet);
  1202. /* Report on the actual number of bytes (to be) sent */
  1203. netdev_tx_sent_queue(txq, packet->tx_bytes);
  1204. /* Configure required descriptor fields for transmission */
  1205. hw_if->dev_xmit(channel);
  1206. #ifdef XGMAC_ENABLE_TX_PKT_DUMP
  1207. xgbe_print_pkt(netdev, skb, true);
  1208. #endif
  1209. /* Stop the queue in advance if there may not be enough descriptors */
  1210. xgbe_maybe_stop_tx_queue(channel, ring, XGBE_TX_MAX_DESCS);
  1211. ret = NETDEV_TX_OK;
  1212. tx_netdev_return:
  1213. return ret;
  1214. }
  1215. static void xgbe_set_rx_mode(struct net_device *netdev)
  1216. {
  1217. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1218. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1219. DBGPR("-->xgbe_set_rx_mode\n");
  1220. hw_if->config_rx_mode(pdata);
  1221. DBGPR("<--xgbe_set_rx_mode\n");
  1222. }
  1223. static int xgbe_set_mac_address(struct net_device *netdev, void *addr)
  1224. {
  1225. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1226. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1227. struct sockaddr *saddr = addr;
  1228. DBGPR("-->xgbe_set_mac_address\n");
  1229. if (!is_valid_ether_addr(saddr->sa_data))
  1230. return -EADDRNOTAVAIL;
  1231. memcpy(netdev->dev_addr, saddr->sa_data, netdev->addr_len);
  1232. hw_if->set_mac_address(pdata, netdev->dev_addr);
  1233. DBGPR("<--xgbe_set_mac_address\n");
  1234. return 0;
  1235. }
  1236. static int xgbe_ioctl(struct net_device *netdev, struct ifreq *ifreq, int cmd)
  1237. {
  1238. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1239. int ret;
  1240. switch (cmd) {
  1241. case SIOCGHWTSTAMP:
  1242. ret = xgbe_get_hwtstamp_settings(pdata, ifreq);
  1243. break;
  1244. case SIOCSHWTSTAMP:
  1245. ret = xgbe_set_hwtstamp_settings(pdata, ifreq);
  1246. break;
  1247. default:
  1248. ret = -EOPNOTSUPP;
  1249. }
  1250. return ret;
  1251. }
  1252. static int xgbe_change_mtu(struct net_device *netdev, int mtu)
  1253. {
  1254. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1255. int ret;
  1256. DBGPR("-->xgbe_change_mtu\n");
  1257. ret = xgbe_calc_rx_buf_size(netdev, mtu);
  1258. if (ret < 0)
  1259. return ret;
  1260. pdata->rx_buf_size = ret;
  1261. netdev->mtu = mtu;
  1262. xgbe_restart_dev(pdata);
  1263. DBGPR("<--xgbe_change_mtu\n");
  1264. return 0;
  1265. }
  1266. static void xgbe_tx_timeout(struct net_device *netdev)
  1267. {
  1268. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1269. netdev_warn(netdev, "tx timeout, device restarting\n");
  1270. schedule_work(&pdata->restart_work);
  1271. }
  1272. static struct rtnl_link_stats64 *xgbe_get_stats64(struct net_device *netdev,
  1273. struct rtnl_link_stats64 *s)
  1274. {
  1275. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1276. struct xgbe_mmc_stats *pstats = &pdata->mmc_stats;
  1277. DBGPR("-->%s\n", __func__);
  1278. pdata->hw_if.read_mmc_stats(pdata);
  1279. s->rx_packets = pstats->rxframecount_gb;
  1280. s->rx_bytes = pstats->rxoctetcount_gb;
  1281. s->rx_errors = pstats->rxframecount_gb -
  1282. pstats->rxbroadcastframes_g -
  1283. pstats->rxmulticastframes_g -
  1284. pstats->rxunicastframes_g;
  1285. s->multicast = pstats->rxmulticastframes_g;
  1286. s->rx_length_errors = pstats->rxlengtherror;
  1287. s->rx_crc_errors = pstats->rxcrcerror;
  1288. s->rx_fifo_errors = pstats->rxfifooverflow;
  1289. s->tx_packets = pstats->txframecount_gb;
  1290. s->tx_bytes = pstats->txoctetcount_gb;
  1291. s->tx_errors = pstats->txframecount_gb - pstats->txframecount_g;
  1292. s->tx_dropped = netdev->stats.tx_dropped;
  1293. DBGPR("<--%s\n", __func__);
  1294. return s;
  1295. }
  1296. static int xgbe_vlan_rx_add_vid(struct net_device *netdev, __be16 proto,
  1297. u16 vid)
  1298. {
  1299. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1300. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1301. DBGPR("-->%s\n", __func__);
  1302. set_bit(vid, pdata->active_vlans);
  1303. hw_if->update_vlan_hash_table(pdata);
  1304. DBGPR("<--%s\n", __func__);
  1305. return 0;
  1306. }
  1307. static int xgbe_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto,
  1308. u16 vid)
  1309. {
  1310. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1311. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1312. DBGPR("-->%s\n", __func__);
  1313. clear_bit(vid, pdata->active_vlans);
  1314. hw_if->update_vlan_hash_table(pdata);
  1315. DBGPR("<--%s\n", __func__);
  1316. return 0;
  1317. }
  1318. #ifdef CONFIG_NET_POLL_CONTROLLER
  1319. static void xgbe_poll_controller(struct net_device *netdev)
  1320. {
  1321. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1322. struct xgbe_channel *channel;
  1323. unsigned int i;
  1324. DBGPR("-->xgbe_poll_controller\n");
  1325. if (pdata->per_channel_irq) {
  1326. channel = pdata->channel;
  1327. for (i = 0; i < pdata->channel_count; i++, channel++)
  1328. xgbe_dma_isr(channel->dma_irq, channel);
  1329. } else {
  1330. disable_irq(pdata->dev_irq);
  1331. xgbe_isr(pdata->dev_irq, pdata);
  1332. enable_irq(pdata->dev_irq);
  1333. }
  1334. DBGPR("<--xgbe_poll_controller\n");
  1335. }
  1336. #endif /* End CONFIG_NET_POLL_CONTROLLER */
  1337. static int xgbe_setup_tc(struct net_device *netdev, u8 tc)
  1338. {
  1339. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1340. unsigned int offset, queue;
  1341. u8 i;
  1342. if (tc && (tc != pdata->hw_feat.tc_cnt))
  1343. return -EINVAL;
  1344. if (tc) {
  1345. netdev_set_num_tc(netdev, tc);
  1346. for (i = 0, queue = 0, offset = 0; i < tc; i++) {
  1347. while ((queue < pdata->tx_q_count) &&
  1348. (pdata->q2tc_map[queue] == i))
  1349. queue++;
  1350. DBGPR(" TC%u using TXq%u-%u\n", i, offset, queue - 1);
  1351. netdev_set_tc_queue(netdev, i, queue - offset, offset);
  1352. offset = queue;
  1353. }
  1354. } else {
  1355. netdev_reset_tc(netdev);
  1356. }
  1357. return 0;
  1358. }
  1359. static int xgbe_set_features(struct net_device *netdev,
  1360. netdev_features_t features)
  1361. {
  1362. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1363. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1364. netdev_features_t rxhash, rxcsum, rxvlan, rxvlan_filter;
  1365. int ret = 0;
  1366. rxhash = pdata->netdev_features & NETIF_F_RXHASH;
  1367. rxcsum = pdata->netdev_features & NETIF_F_RXCSUM;
  1368. rxvlan = pdata->netdev_features & NETIF_F_HW_VLAN_CTAG_RX;
  1369. rxvlan_filter = pdata->netdev_features & NETIF_F_HW_VLAN_CTAG_FILTER;
  1370. if ((features & NETIF_F_RXHASH) && !rxhash)
  1371. ret = hw_if->enable_rss(pdata);
  1372. else if (!(features & NETIF_F_RXHASH) && rxhash)
  1373. ret = hw_if->disable_rss(pdata);
  1374. if (ret)
  1375. return ret;
  1376. if ((features & NETIF_F_RXCSUM) && !rxcsum)
  1377. hw_if->enable_rx_csum(pdata);
  1378. else if (!(features & NETIF_F_RXCSUM) && rxcsum)
  1379. hw_if->disable_rx_csum(pdata);
  1380. if ((features & NETIF_F_HW_VLAN_CTAG_RX) && !rxvlan)
  1381. hw_if->enable_rx_vlan_stripping(pdata);
  1382. else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) && rxvlan)
  1383. hw_if->disable_rx_vlan_stripping(pdata);
  1384. if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) && !rxvlan_filter)
  1385. hw_if->enable_rx_vlan_filtering(pdata);
  1386. else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) && rxvlan_filter)
  1387. hw_if->disable_rx_vlan_filtering(pdata);
  1388. pdata->netdev_features = features;
  1389. DBGPR("<--xgbe_set_features\n");
  1390. return 0;
  1391. }
  1392. static const struct net_device_ops xgbe_netdev_ops = {
  1393. .ndo_open = xgbe_open,
  1394. .ndo_stop = xgbe_close,
  1395. .ndo_start_xmit = xgbe_xmit,
  1396. .ndo_set_rx_mode = xgbe_set_rx_mode,
  1397. .ndo_set_mac_address = xgbe_set_mac_address,
  1398. .ndo_validate_addr = eth_validate_addr,
  1399. .ndo_do_ioctl = xgbe_ioctl,
  1400. .ndo_change_mtu = xgbe_change_mtu,
  1401. .ndo_tx_timeout = xgbe_tx_timeout,
  1402. .ndo_get_stats64 = xgbe_get_stats64,
  1403. .ndo_vlan_rx_add_vid = xgbe_vlan_rx_add_vid,
  1404. .ndo_vlan_rx_kill_vid = xgbe_vlan_rx_kill_vid,
  1405. #ifdef CONFIG_NET_POLL_CONTROLLER
  1406. .ndo_poll_controller = xgbe_poll_controller,
  1407. #endif
  1408. .ndo_setup_tc = xgbe_setup_tc,
  1409. .ndo_set_features = xgbe_set_features,
  1410. };
  1411. struct net_device_ops *xgbe_get_netdev_ops(void)
  1412. {
  1413. return (struct net_device_ops *)&xgbe_netdev_ops;
  1414. }
  1415. static void xgbe_rx_refresh(struct xgbe_channel *channel)
  1416. {
  1417. struct xgbe_prv_data *pdata = channel->pdata;
  1418. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1419. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  1420. struct xgbe_ring *ring = channel->rx_ring;
  1421. struct xgbe_ring_data *rdata;
  1422. while (ring->dirty != ring->cur) {
  1423. rdata = XGBE_GET_DESC_DATA(ring, ring->dirty);
  1424. /* Reset rdata values */
  1425. desc_if->unmap_rdata(pdata, rdata);
  1426. if (desc_if->map_rx_buffer(pdata, ring, rdata))
  1427. break;
  1428. hw_if->rx_desc_reset(pdata, rdata, ring->dirty);
  1429. ring->dirty++;
  1430. }
  1431. /* Make sure everything is written before the register write */
  1432. wmb();
  1433. /* Update the Rx Tail Pointer Register with address of
  1434. * the last cleaned entry */
  1435. rdata = XGBE_GET_DESC_DATA(ring, ring->dirty - 1);
  1436. XGMAC_DMA_IOWRITE(channel, DMA_CH_RDTR_LO,
  1437. lower_32_bits(rdata->rdesc_dma));
  1438. }
  1439. static struct sk_buff *xgbe_create_skb(struct napi_struct *napi,
  1440. struct xgbe_ring_data *rdata,
  1441. unsigned int *len)
  1442. {
  1443. struct sk_buff *skb;
  1444. u8 *packet;
  1445. unsigned int copy_len;
  1446. skb = napi_alloc_skb(napi, rdata->rx.hdr.dma_len);
  1447. if (!skb)
  1448. return NULL;
  1449. packet = page_address(rdata->rx.hdr.pa.pages) +
  1450. rdata->rx.hdr.pa.pages_offset;
  1451. copy_len = (rdata->rx.hdr_len) ? rdata->rx.hdr_len : *len;
  1452. copy_len = min(rdata->rx.hdr.dma_len, copy_len);
  1453. skb_copy_to_linear_data(skb, packet, copy_len);
  1454. skb_put(skb, copy_len);
  1455. *len -= copy_len;
  1456. return skb;
  1457. }
  1458. static int xgbe_tx_poll(struct xgbe_channel *channel)
  1459. {
  1460. struct xgbe_prv_data *pdata = channel->pdata;
  1461. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1462. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  1463. struct xgbe_ring *ring = channel->tx_ring;
  1464. struct xgbe_ring_data *rdata;
  1465. struct xgbe_ring_desc *rdesc;
  1466. struct net_device *netdev = pdata->netdev;
  1467. struct netdev_queue *txq;
  1468. int processed = 0;
  1469. unsigned int tx_packets = 0, tx_bytes = 0;
  1470. DBGPR("-->xgbe_tx_poll\n");
  1471. /* Nothing to do if there isn't a Tx ring for this channel */
  1472. if (!ring)
  1473. return 0;
  1474. txq = netdev_get_tx_queue(netdev, channel->queue_index);
  1475. while ((processed < XGBE_TX_DESC_MAX_PROC) &&
  1476. (ring->dirty != ring->cur)) {
  1477. rdata = XGBE_GET_DESC_DATA(ring, ring->dirty);
  1478. rdesc = rdata->rdesc;
  1479. if (!hw_if->tx_complete(rdesc))
  1480. break;
  1481. /* Make sure descriptor fields are read after reading the OWN
  1482. * bit */
  1483. dma_rmb();
  1484. #ifdef XGMAC_ENABLE_TX_DESC_DUMP
  1485. xgbe_dump_tx_desc(ring, ring->dirty, 1, 0);
  1486. #endif
  1487. if (hw_if->is_last_desc(rdesc)) {
  1488. tx_packets += rdata->tx.packets;
  1489. tx_bytes += rdata->tx.bytes;
  1490. }
  1491. /* Free the SKB and reset the descriptor for re-use */
  1492. desc_if->unmap_rdata(pdata, rdata);
  1493. hw_if->tx_desc_reset(rdata);
  1494. processed++;
  1495. ring->dirty++;
  1496. }
  1497. if (!processed)
  1498. return 0;
  1499. netdev_tx_completed_queue(txq, tx_packets, tx_bytes);
  1500. if ((ring->tx.queue_stopped == 1) &&
  1501. (xgbe_tx_avail_desc(ring) > XGBE_TX_DESC_MIN_FREE)) {
  1502. ring->tx.queue_stopped = 0;
  1503. netif_tx_wake_queue(txq);
  1504. }
  1505. DBGPR("<--xgbe_tx_poll: processed=%d\n", processed);
  1506. return processed;
  1507. }
  1508. static int xgbe_rx_poll(struct xgbe_channel *channel, int budget)
  1509. {
  1510. struct xgbe_prv_data *pdata = channel->pdata;
  1511. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1512. struct xgbe_ring *ring = channel->rx_ring;
  1513. struct xgbe_ring_data *rdata;
  1514. struct xgbe_packet_data *packet;
  1515. struct net_device *netdev = pdata->netdev;
  1516. struct napi_struct *napi;
  1517. struct sk_buff *skb;
  1518. struct skb_shared_hwtstamps *hwtstamps;
  1519. unsigned int incomplete, error, context_next, context;
  1520. unsigned int len, put_len, max_len;
  1521. unsigned int received = 0;
  1522. int packet_count = 0;
  1523. DBGPR("-->xgbe_rx_poll: budget=%d\n", budget);
  1524. /* Nothing to do if there isn't a Rx ring for this channel */
  1525. if (!ring)
  1526. return 0;
  1527. napi = (pdata->per_channel_irq) ? &channel->napi : &pdata->napi;
  1528. rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
  1529. packet = &ring->packet_data;
  1530. while (packet_count < budget) {
  1531. DBGPR(" cur = %d\n", ring->cur);
  1532. /* First time in loop see if we need to restore state */
  1533. if (!received && rdata->state_saved) {
  1534. incomplete = rdata->state.incomplete;
  1535. context_next = rdata->state.context_next;
  1536. skb = rdata->state.skb;
  1537. error = rdata->state.error;
  1538. len = rdata->state.len;
  1539. } else {
  1540. memset(packet, 0, sizeof(*packet));
  1541. incomplete = 0;
  1542. context_next = 0;
  1543. skb = NULL;
  1544. error = 0;
  1545. len = 0;
  1546. }
  1547. read_again:
  1548. rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
  1549. if (xgbe_rx_dirty_desc(ring) > (XGBE_RX_DESC_CNT >> 3))
  1550. xgbe_rx_refresh(channel);
  1551. if (hw_if->dev_read(channel))
  1552. break;
  1553. received++;
  1554. ring->cur++;
  1555. incomplete = XGMAC_GET_BITS(packet->attributes,
  1556. RX_PACKET_ATTRIBUTES,
  1557. INCOMPLETE);
  1558. context_next = XGMAC_GET_BITS(packet->attributes,
  1559. RX_PACKET_ATTRIBUTES,
  1560. CONTEXT_NEXT);
  1561. context = XGMAC_GET_BITS(packet->attributes,
  1562. RX_PACKET_ATTRIBUTES,
  1563. CONTEXT);
  1564. /* Earlier error, just drain the remaining data */
  1565. if ((incomplete || context_next) && error)
  1566. goto read_again;
  1567. if (error || packet->errors) {
  1568. if (packet->errors)
  1569. DBGPR("Error in received packet\n");
  1570. dev_kfree_skb(skb);
  1571. goto next_packet;
  1572. }
  1573. if (!context) {
  1574. put_len = rdata->rx.len - len;
  1575. len += put_len;
  1576. if (!skb) {
  1577. dma_sync_single_for_cpu(pdata->dev,
  1578. rdata->rx.hdr.dma,
  1579. rdata->rx.hdr.dma_len,
  1580. DMA_FROM_DEVICE);
  1581. skb = xgbe_create_skb(napi, rdata, &put_len);
  1582. if (!skb) {
  1583. error = 1;
  1584. goto skip_data;
  1585. }
  1586. }
  1587. if (put_len) {
  1588. dma_sync_single_for_cpu(pdata->dev,
  1589. rdata->rx.buf.dma,
  1590. rdata->rx.buf.dma_len,
  1591. DMA_FROM_DEVICE);
  1592. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
  1593. rdata->rx.buf.pa.pages,
  1594. rdata->rx.buf.pa.pages_offset,
  1595. put_len, rdata->rx.buf.dma_len);
  1596. rdata->rx.buf.pa.pages = NULL;
  1597. }
  1598. }
  1599. skip_data:
  1600. if (incomplete || context_next)
  1601. goto read_again;
  1602. if (!skb)
  1603. goto next_packet;
  1604. /* Be sure we don't exceed the configured MTU */
  1605. max_len = netdev->mtu + ETH_HLEN;
  1606. if (!(netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
  1607. (skb->protocol == htons(ETH_P_8021Q)))
  1608. max_len += VLAN_HLEN;
  1609. if (skb->len > max_len) {
  1610. DBGPR("packet length exceeds configured MTU\n");
  1611. dev_kfree_skb(skb);
  1612. goto next_packet;
  1613. }
  1614. #ifdef XGMAC_ENABLE_RX_PKT_DUMP
  1615. xgbe_print_pkt(netdev, skb, false);
  1616. #endif
  1617. skb_checksum_none_assert(skb);
  1618. if (XGMAC_GET_BITS(packet->attributes,
  1619. RX_PACKET_ATTRIBUTES, CSUM_DONE))
  1620. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1621. if (XGMAC_GET_BITS(packet->attributes,
  1622. RX_PACKET_ATTRIBUTES, VLAN_CTAG))
  1623. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  1624. packet->vlan_ctag);
  1625. if (XGMAC_GET_BITS(packet->attributes,
  1626. RX_PACKET_ATTRIBUTES, RX_TSTAMP)) {
  1627. u64 nsec;
  1628. nsec = timecounter_cyc2time(&pdata->tstamp_tc,
  1629. packet->rx_tstamp);
  1630. hwtstamps = skb_hwtstamps(skb);
  1631. hwtstamps->hwtstamp = ns_to_ktime(nsec);
  1632. }
  1633. if (XGMAC_GET_BITS(packet->attributes,
  1634. RX_PACKET_ATTRIBUTES, RSS_HASH))
  1635. skb_set_hash(skb, packet->rss_hash,
  1636. packet->rss_hash_type);
  1637. skb->dev = netdev;
  1638. skb->protocol = eth_type_trans(skb, netdev);
  1639. skb_record_rx_queue(skb, channel->queue_index);
  1640. skb_mark_napi_id(skb, napi);
  1641. netdev->last_rx = jiffies;
  1642. napi_gro_receive(napi, skb);
  1643. next_packet:
  1644. packet_count++;
  1645. }
  1646. /* Check if we need to save state before leaving */
  1647. if (received && (incomplete || context_next)) {
  1648. rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
  1649. rdata->state_saved = 1;
  1650. rdata->state.incomplete = incomplete;
  1651. rdata->state.context_next = context_next;
  1652. rdata->state.skb = skb;
  1653. rdata->state.len = len;
  1654. rdata->state.error = error;
  1655. }
  1656. DBGPR("<--xgbe_rx_poll: packet_count = %d\n", packet_count);
  1657. return packet_count;
  1658. }
  1659. static int xgbe_one_poll(struct napi_struct *napi, int budget)
  1660. {
  1661. struct xgbe_channel *channel = container_of(napi, struct xgbe_channel,
  1662. napi);
  1663. int processed = 0;
  1664. DBGPR("-->xgbe_one_poll: budget=%d\n", budget);
  1665. /* Cleanup Tx ring first */
  1666. xgbe_tx_poll(channel);
  1667. /* Process Rx ring next */
  1668. processed = xgbe_rx_poll(channel, budget);
  1669. /* If we processed everything, we are done */
  1670. if (processed < budget) {
  1671. /* Turn off polling */
  1672. napi_complete(napi);
  1673. /* Enable Tx and Rx interrupts */
  1674. enable_irq(channel->dma_irq);
  1675. }
  1676. DBGPR("<--xgbe_one_poll: received = %d\n", processed);
  1677. return processed;
  1678. }
  1679. static int xgbe_all_poll(struct napi_struct *napi, int budget)
  1680. {
  1681. struct xgbe_prv_data *pdata = container_of(napi, struct xgbe_prv_data,
  1682. napi);
  1683. struct xgbe_channel *channel;
  1684. int ring_budget;
  1685. int processed, last_processed;
  1686. unsigned int i;
  1687. DBGPR("-->xgbe_all_poll: budget=%d\n", budget);
  1688. processed = 0;
  1689. ring_budget = budget / pdata->rx_ring_count;
  1690. do {
  1691. last_processed = processed;
  1692. channel = pdata->channel;
  1693. for (i = 0; i < pdata->channel_count; i++, channel++) {
  1694. /* Cleanup Tx ring first */
  1695. xgbe_tx_poll(channel);
  1696. /* Process Rx ring next */
  1697. if (ring_budget > (budget - processed))
  1698. ring_budget = budget - processed;
  1699. processed += xgbe_rx_poll(channel, ring_budget);
  1700. }
  1701. } while ((processed < budget) && (processed != last_processed));
  1702. /* If we processed everything, we are done */
  1703. if (processed < budget) {
  1704. /* Turn off polling */
  1705. napi_complete(napi);
  1706. /* Enable Tx and Rx interrupts */
  1707. xgbe_enable_rx_tx_ints(pdata);
  1708. }
  1709. DBGPR("<--xgbe_all_poll: received = %d\n", processed);
  1710. return processed;
  1711. }
  1712. void xgbe_dump_tx_desc(struct xgbe_ring *ring, unsigned int idx,
  1713. unsigned int count, unsigned int flag)
  1714. {
  1715. struct xgbe_ring_data *rdata;
  1716. struct xgbe_ring_desc *rdesc;
  1717. while (count--) {
  1718. rdata = XGBE_GET_DESC_DATA(ring, idx);
  1719. rdesc = rdata->rdesc;
  1720. pr_alert("TX_NORMAL_DESC[%d %s] = %08x:%08x:%08x:%08x\n", idx,
  1721. (flag == 1) ? "QUEUED FOR TX" : "TX BY DEVICE",
  1722. le32_to_cpu(rdesc->desc0), le32_to_cpu(rdesc->desc1),
  1723. le32_to_cpu(rdesc->desc2), le32_to_cpu(rdesc->desc3));
  1724. idx++;
  1725. }
  1726. }
  1727. void xgbe_dump_rx_desc(struct xgbe_ring *ring, struct xgbe_ring_desc *desc,
  1728. unsigned int idx)
  1729. {
  1730. pr_alert("RX_NORMAL_DESC[%d RX BY DEVICE] = %08x:%08x:%08x:%08x\n", idx,
  1731. le32_to_cpu(desc->desc0), le32_to_cpu(desc->desc1),
  1732. le32_to_cpu(desc->desc2), le32_to_cpu(desc->desc3));
  1733. }
  1734. void xgbe_print_pkt(struct net_device *netdev, struct sk_buff *skb, bool tx_rx)
  1735. {
  1736. struct ethhdr *eth = (struct ethhdr *)skb->data;
  1737. unsigned char *buf = skb->data;
  1738. unsigned char buffer[128];
  1739. unsigned int i, j;
  1740. netdev_alert(netdev, "\n************** SKB dump ****************\n");
  1741. netdev_alert(netdev, "%s packet of %d bytes\n",
  1742. (tx_rx ? "TX" : "RX"), skb->len);
  1743. netdev_alert(netdev, "Dst MAC addr: %pM\n", eth->h_dest);
  1744. netdev_alert(netdev, "Src MAC addr: %pM\n", eth->h_source);
  1745. netdev_alert(netdev, "Protocol: 0x%04hx\n", ntohs(eth->h_proto));
  1746. for (i = 0, j = 0; i < skb->len;) {
  1747. j += snprintf(buffer + j, sizeof(buffer) - j, "%02hhx",
  1748. buf[i++]);
  1749. if ((i % 32) == 0) {
  1750. netdev_alert(netdev, " 0x%04x: %s\n", i - 32, buffer);
  1751. j = 0;
  1752. } else if ((i % 16) == 0) {
  1753. buffer[j++] = ' ';
  1754. buffer[j++] = ' ';
  1755. } else if ((i % 4) == 0) {
  1756. buffer[j++] = ' ';
  1757. }
  1758. }
  1759. if (i % 32)
  1760. netdev_alert(netdev, " 0x%04x: %s\n", i - (i % 32), buffer);
  1761. netdev_alert(netdev, "\n************** SKB dump ****************\n");
  1762. }