xgbe-drv.c 58 KB

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