xgbe-drv.c 58 KB

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