xgbe-drv.c 50 KB

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