xgbe-drv.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813
  1. /*
  2. * AMD 10Gb Ethernet driver
  3. *
  4. * This file is available to you under your choice of the following two
  5. * licenses:
  6. *
  7. * License 1: GPLv2
  8. *
  9. * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
  10. *
  11. * This file is free software; you may copy, redistribute and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 2 of the License, or (at
  14. * your option) any later version.
  15. *
  16. * This file is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. * This file incorporates work covered by the following copyright and
  25. * permission notice:
  26. * The Synopsys DWC ETHER XGMAC Software Driver and documentation
  27. * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
  28. * Inc. unless otherwise expressly agreed to in writing between Synopsys
  29. * and you.
  30. *
  31. * The Software IS NOT an item of Licensed Software or Licensed Product
  32. * under any End User Software License Agreement or Agreement for Licensed
  33. * Product with Synopsys or any supplement thereto. Permission is hereby
  34. * granted, free of charge, to any person obtaining a copy of this software
  35. * annotated with this license and the Software, to deal in the Software
  36. * without restriction, including without limitation the rights to use,
  37. * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  38. * of the Software, and to permit persons to whom the Software is furnished
  39. * to do so, subject to the following conditions:
  40. *
  41. * The above copyright notice and this permission notice shall be included
  42. * in all copies or substantial portions of the Software.
  43. *
  44. * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  45. * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  46. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  47. * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
  48. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  49. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  50. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  51. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  52. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  53. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  54. * THE POSSIBILITY OF SUCH DAMAGE.
  55. *
  56. *
  57. * License 2: Modified BSD
  58. *
  59. * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
  60. * All rights reserved.
  61. *
  62. * Redistribution and use in source and binary forms, with or without
  63. * modification, are permitted provided that the following conditions are met:
  64. * * Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. * * Redistributions in binary form must reproduce the above copyright
  67. * notice, this list of conditions and the following disclaimer in the
  68. * documentation and/or other materials provided with the distribution.
  69. * * Neither the name of Advanced Micro Devices, Inc. nor the
  70. * names of its contributors may be used to endorse or promote products
  71. * derived from this software without specific prior written permission.
  72. *
  73. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  74. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  75. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  76. * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  77. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  78. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  79. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  80. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  81. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  82. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  83. *
  84. * This file incorporates work covered by the following copyright and
  85. * permission notice:
  86. * The Synopsys DWC ETHER XGMAC Software Driver and documentation
  87. * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
  88. * Inc. unless otherwise expressly agreed to in writing between Synopsys
  89. * and you.
  90. *
  91. * The Software IS NOT an item of Licensed Software or Licensed Product
  92. * under any End User Software License Agreement or Agreement for Licensed
  93. * Product with Synopsys or any supplement thereto. Permission is hereby
  94. * granted, free of charge, to any person obtaining a copy of this software
  95. * annotated with this license and the Software, to deal in the Software
  96. * without restriction, including without limitation the rights to use,
  97. * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  98. * of the Software, and to permit persons to whom the Software is furnished
  99. * to do so, subject to the following conditions:
  100. *
  101. * The above copyright notice and this permission notice shall be included
  102. * in all copies or substantial portions of the Software.
  103. *
  104. * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  105. * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  106. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  107. * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
  108. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  109. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  110. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  111. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  112. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  113. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  114. * THE POSSIBILITY OF SUCH DAMAGE.
  115. */
  116. #include <linux/module.h>
  117. #include <linux/spinlock.h>
  118. #include <linux/tcp.h>
  119. #include <linux/if_vlan.h>
  120. #include <linux/interrupt.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 <net/vxlan.h>
  126. #include "xgbe.h"
  127. #include "xgbe-common.h"
  128. static unsigned int ecc_sec_info_threshold = 10;
  129. static unsigned int ecc_sec_warn_threshold = 10000;
  130. static unsigned int ecc_sec_period = 600;
  131. static unsigned int ecc_ded_threshold = 2;
  132. static unsigned int ecc_ded_period = 600;
  133. #ifdef CONFIG_AMD_XGBE_HAVE_ECC
  134. /* Only expose the ECC parameters if supported */
  135. module_param(ecc_sec_info_threshold, uint, 0644);
  136. MODULE_PARM_DESC(ecc_sec_info_threshold,
  137. " ECC corrected error informational threshold setting");
  138. module_param(ecc_sec_warn_threshold, uint, 0644);
  139. MODULE_PARM_DESC(ecc_sec_warn_threshold,
  140. " ECC corrected error warning threshold setting");
  141. module_param(ecc_sec_period, uint, 0644);
  142. MODULE_PARM_DESC(ecc_sec_period, " ECC corrected error period (in seconds)");
  143. module_param(ecc_ded_threshold, uint, 0644);
  144. MODULE_PARM_DESC(ecc_ded_threshold, " ECC detected error threshold setting");
  145. module_param(ecc_ded_period, uint, 0644);
  146. MODULE_PARM_DESC(ecc_ded_period, " ECC detected error period (in seconds)");
  147. #endif
  148. static int xgbe_one_poll(struct napi_struct *, int);
  149. static int xgbe_all_poll(struct napi_struct *, int);
  150. static void xgbe_stop(struct xgbe_prv_data *);
  151. static void *xgbe_alloc_node(size_t size, int node)
  152. {
  153. void *mem;
  154. mem = kzalloc_node(size, GFP_KERNEL, node);
  155. if (!mem)
  156. mem = kzalloc(size, GFP_KERNEL);
  157. return mem;
  158. }
  159. static void xgbe_free_channels(struct xgbe_prv_data *pdata)
  160. {
  161. unsigned int i;
  162. for (i = 0; i < ARRAY_SIZE(pdata->channel); i++) {
  163. if (!pdata->channel[i])
  164. continue;
  165. kfree(pdata->channel[i]->rx_ring);
  166. kfree(pdata->channel[i]->tx_ring);
  167. kfree(pdata->channel[i]);
  168. pdata->channel[i] = NULL;
  169. }
  170. pdata->channel_count = 0;
  171. }
  172. static int xgbe_alloc_channels(struct xgbe_prv_data *pdata)
  173. {
  174. struct xgbe_channel *channel;
  175. struct xgbe_ring *ring;
  176. unsigned int count, i;
  177. unsigned int cpu;
  178. int node;
  179. count = max_t(unsigned int, pdata->tx_ring_count, pdata->rx_ring_count);
  180. for (i = 0; i < count; i++) {
  181. /* Attempt to use a CPU on the node the device is on */
  182. cpu = cpumask_local_spread(i, dev_to_node(pdata->dev));
  183. /* Set the allocation node based on the returned CPU */
  184. node = cpu_to_node(cpu);
  185. channel = xgbe_alloc_node(sizeof(*channel), node);
  186. if (!channel)
  187. goto err_mem;
  188. pdata->channel[i] = channel;
  189. snprintf(channel->name, sizeof(channel->name), "channel-%u", i);
  190. channel->pdata = pdata;
  191. channel->queue_index = i;
  192. channel->dma_regs = pdata->xgmac_regs + DMA_CH_BASE +
  193. (DMA_CH_INC * i);
  194. channel->node = node;
  195. cpumask_set_cpu(cpu, &channel->affinity_mask);
  196. if (pdata->per_channel_irq)
  197. channel->dma_irq = pdata->channel_irq[i];
  198. if (i < pdata->tx_ring_count) {
  199. ring = xgbe_alloc_node(sizeof(*ring), node);
  200. if (!ring)
  201. goto err_mem;
  202. spin_lock_init(&ring->lock);
  203. ring->node = node;
  204. channel->tx_ring = ring;
  205. }
  206. if (i < pdata->rx_ring_count) {
  207. ring = xgbe_alloc_node(sizeof(*ring), node);
  208. if (!ring)
  209. goto err_mem;
  210. spin_lock_init(&ring->lock);
  211. ring->node = node;
  212. channel->rx_ring = ring;
  213. }
  214. netif_dbg(pdata, drv, pdata->netdev,
  215. "%s: cpu=%u, node=%d\n", channel->name, cpu, node);
  216. netif_dbg(pdata, drv, pdata->netdev,
  217. "%s: dma_regs=%p, dma_irq=%d, tx=%p, rx=%p\n",
  218. channel->name, channel->dma_regs, channel->dma_irq,
  219. channel->tx_ring, channel->rx_ring);
  220. }
  221. pdata->channel_count = count;
  222. return 0;
  223. err_mem:
  224. xgbe_free_channels(pdata);
  225. return -ENOMEM;
  226. }
  227. static inline unsigned int xgbe_tx_avail_desc(struct xgbe_ring *ring)
  228. {
  229. return (ring->rdesc_count - (ring->cur - ring->dirty));
  230. }
  231. static inline unsigned int xgbe_rx_dirty_desc(struct xgbe_ring *ring)
  232. {
  233. return (ring->cur - ring->dirty);
  234. }
  235. static int xgbe_maybe_stop_tx_queue(struct xgbe_channel *channel,
  236. struct xgbe_ring *ring, unsigned int count)
  237. {
  238. struct xgbe_prv_data *pdata = channel->pdata;
  239. if (count > xgbe_tx_avail_desc(ring)) {
  240. netif_info(pdata, drv, pdata->netdev,
  241. "Tx queue stopped, not enough descriptors available\n");
  242. netif_stop_subqueue(pdata->netdev, channel->queue_index);
  243. ring->tx.queue_stopped = 1;
  244. /* If we haven't notified the hardware because of xmit_more
  245. * support, tell it now
  246. */
  247. if (ring->tx.xmit_more)
  248. pdata->hw_if.tx_start_xmit(channel, ring);
  249. return NETDEV_TX_BUSY;
  250. }
  251. return 0;
  252. }
  253. static int xgbe_calc_rx_buf_size(struct net_device *netdev, unsigned int mtu)
  254. {
  255. unsigned int rx_buf_size;
  256. rx_buf_size = mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  257. rx_buf_size = clamp_val(rx_buf_size, XGBE_RX_MIN_BUF_SIZE, PAGE_SIZE);
  258. rx_buf_size = (rx_buf_size + XGBE_RX_BUF_ALIGN - 1) &
  259. ~(XGBE_RX_BUF_ALIGN - 1);
  260. return rx_buf_size;
  261. }
  262. static void xgbe_enable_rx_tx_int(struct xgbe_prv_data *pdata,
  263. struct xgbe_channel *channel)
  264. {
  265. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  266. enum xgbe_int int_id;
  267. if (channel->tx_ring && channel->rx_ring)
  268. int_id = XGMAC_INT_DMA_CH_SR_TI_RI;
  269. else if (channel->tx_ring)
  270. int_id = XGMAC_INT_DMA_CH_SR_TI;
  271. else if (channel->rx_ring)
  272. int_id = XGMAC_INT_DMA_CH_SR_RI;
  273. else
  274. return;
  275. hw_if->enable_int(channel, int_id);
  276. }
  277. static void xgbe_enable_rx_tx_ints(struct xgbe_prv_data *pdata)
  278. {
  279. unsigned int i;
  280. for (i = 0; i < pdata->channel_count; i++)
  281. xgbe_enable_rx_tx_int(pdata, pdata->channel[i]);
  282. }
  283. static void xgbe_disable_rx_tx_int(struct xgbe_prv_data *pdata,
  284. struct xgbe_channel *channel)
  285. {
  286. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  287. enum xgbe_int int_id;
  288. if (channel->tx_ring && channel->rx_ring)
  289. int_id = XGMAC_INT_DMA_CH_SR_TI_RI;
  290. else if (channel->tx_ring)
  291. int_id = XGMAC_INT_DMA_CH_SR_TI;
  292. else if (channel->rx_ring)
  293. int_id = XGMAC_INT_DMA_CH_SR_RI;
  294. else
  295. return;
  296. hw_if->disable_int(channel, int_id);
  297. }
  298. static void xgbe_disable_rx_tx_ints(struct xgbe_prv_data *pdata)
  299. {
  300. unsigned int i;
  301. for (i = 0; i < pdata->channel_count; i++)
  302. xgbe_disable_rx_tx_int(pdata, pdata->channel[i]);
  303. }
  304. static bool xgbe_ecc_sec(struct xgbe_prv_data *pdata, unsigned long *period,
  305. unsigned int *count, const char *area)
  306. {
  307. if (time_before(jiffies, *period)) {
  308. (*count)++;
  309. } else {
  310. *period = jiffies + (ecc_sec_period * HZ);
  311. *count = 1;
  312. }
  313. if (*count > ecc_sec_info_threshold)
  314. dev_warn_once(pdata->dev,
  315. "%s ECC corrected errors exceed informational threshold\n",
  316. area);
  317. if (*count > ecc_sec_warn_threshold) {
  318. dev_warn_once(pdata->dev,
  319. "%s ECC corrected errors exceed warning threshold\n",
  320. area);
  321. return true;
  322. }
  323. return false;
  324. }
  325. static bool xgbe_ecc_ded(struct xgbe_prv_data *pdata, unsigned long *period,
  326. unsigned int *count, const char *area)
  327. {
  328. if (time_before(jiffies, *period)) {
  329. (*count)++;
  330. } else {
  331. *period = jiffies + (ecc_ded_period * HZ);
  332. *count = 1;
  333. }
  334. if (*count > ecc_ded_threshold) {
  335. netdev_alert(pdata->netdev,
  336. "%s ECC detected errors exceed threshold\n",
  337. area);
  338. return true;
  339. }
  340. return false;
  341. }
  342. static void xgbe_ecc_isr_task(struct tasklet_struct *t)
  343. {
  344. struct xgbe_prv_data *pdata = from_tasklet(pdata, t, tasklet_ecc);
  345. unsigned int ecc_isr;
  346. bool stop = false;
  347. /* Mask status with only the interrupts we care about */
  348. ecc_isr = XP_IOREAD(pdata, XP_ECC_ISR);
  349. ecc_isr &= XP_IOREAD(pdata, XP_ECC_IER);
  350. netif_dbg(pdata, intr, pdata->netdev, "ECC_ISR=%#010x\n", ecc_isr);
  351. if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, TX_DED)) {
  352. stop |= xgbe_ecc_ded(pdata, &pdata->tx_ded_period,
  353. &pdata->tx_ded_count, "TX fifo");
  354. }
  355. if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, RX_DED)) {
  356. stop |= xgbe_ecc_ded(pdata, &pdata->rx_ded_period,
  357. &pdata->rx_ded_count, "RX fifo");
  358. }
  359. if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, DESC_DED)) {
  360. stop |= xgbe_ecc_ded(pdata, &pdata->desc_ded_period,
  361. &pdata->desc_ded_count,
  362. "descriptor cache");
  363. }
  364. if (stop) {
  365. pdata->hw_if.disable_ecc_ded(pdata);
  366. schedule_work(&pdata->stopdev_work);
  367. goto out;
  368. }
  369. if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, TX_SEC)) {
  370. if (xgbe_ecc_sec(pdata, &pdata->tx_sec_period,
  371. &pdata->tx_sec_count, "TX fifo"))
  372. pdata->hw_if.disable_ecc_sec(pdata, XGBE_ECC_SEC_TX);
  373. }
  374. if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, RX_SEC))
  375. if (xgbe_ecc_sec(pdata, &pdata->rx_sec_period,
  376. &pdata->rx_sec_count, "RX fifo"))
  377. pdata->hw_if.disable_ecc_sec(pdata, XGBE_ECC_SEC_RX);
  378. if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, DESC_SEC))
  379. if (xgbe_ecc_sec(pdata, &pdata->desc_sec_period,
  380. &pdata->desc_sec_count, "descriptor cache"))
  381. pdata->hw_if.disable_ecc_sec(pdata, XGBE_ECC_SEC_DESC);
  382. out:
  383. /* Clear all ECC interrupts */
  384. XP_IOWRITE(pdata, XP_ECC_ISR, ecc_isr);
  385. /* Reissue interrupt if status is not clear */
  386. if (pdata->vdata->irq_reissue_support)
  387. XP_IOWRITE(pdata, XP_INT_REISSUE_EN, 1 << 1);
  388. }
  389. static irqreturn_t xgbe_ecc_isr(int irq, void *data)
  390. {
  391. struct xgbe_prv_data *pdata = data;
  392. if (pdata->isr_as_tasklet)
  393. tasklet_schedule(&pdata->tasklet_ecc);
  394. else
  395. xgbe_ecc_isr_task(&pdata->tasklet_ecc);
  396. return IRQ_HANDLED;
  397. }
  398. static void xgbe_isr_task(struct tasklet_struct *t)
  399. {
  400. struct xgbe_prv_data *pdata = from_tasklet(pdata, t, tasklet_dev);
  401. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  402. struct xgbe_channel *channel;
  403. unsigned int dma_isr, dma_ch_isr;
  404. unsigned int mac_isr, mac_tssr, mac_mdioisr;
  405. unsigned int i;
  406. /* The DMA interrupt status register also reports MAC and MTL
  407. * interrupts. So for polling mode, we just need to check for
  408. * this register to be non-zero
  409. */
  410. dma_isr = XGMAC_IOREAD(pdata, DMA_ISR);
  411. if (!dma_isr)
  412. goto isr_done;
  413. netif_dbg(pdata, intr, pdata->netdev, "DMA_ISR=%#010x\n", dma_isr);
  414. for (i = 0; i < pdata->channel_count; i++) {
  415. if (!(dma_isr & (1 << i)))
  416. continue;
  417. channel = pdata->channel[i];
  418. dma_ch_isr = XGMAC_DMA_IOREAD(channel, DMA_CH_SR);
  419. netif_dbg(pdata, intr, pdata->netdev, "DMA_CH%u_ISR=%#010x\n",
  420. i, dma_ch_isr);
  421. /* The TI or RI interrupt bits may still be set even if using
  422. * per channel DMA interrupts. Check to be sure those are not
  423. * enabled before using the private data napi structure.
  424. */
  425. if (!pdata->per_channel_irq &&
  426. (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, TI) ||
  427. XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, RI))) {
  428. if (napi_schedule_prep(&pdata->napi)) {
  429. /* Disable Tx and Rx interrupts */
  430. xgbe_disable_rx_tx_ints(pdata);
  431. /* Turn on polling */
  432. __napi_schedule(&pdata->napi);
  433. }
  434. } else {
  435. /* Don't clear Rx/Tx status if doing per channel DMA
  436. * interrupts, these will be cleared by the ISR for
  437. * per channel DMA interrupts.
  438. */
  439. XGMAC_SET_BITS(dma_ch_isr, DMA_CH_SR, TI, 0);
  440. XGMAC_SET_BITS(dma_ch_isr, DMA_CH_SR, RI, 0);
  441. }
  442. if (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, RBU))
  443. pdata->ext_stats.rx_buffer_unavailable++;
  444. /* Restart the device on a Fatal Bus Error */
  445. if (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, FBE))
  446. schedule_work(&pdata->restart_work);
  447. /* Clear interrupt signals */
  448. XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_ch_isr);
  449. }
  450. if (XGMAC_GET_BITS(dma_isr, DMA_ISR, MACIS)) {
  451. mac_isr = XGMAC_IOREAD(pdata, MAC_ISR);
  452. netif_dbg(pdata, intr, pdata->netdev, "MAC_ISR=%#010x\n",
  453. mac_isr);
  454. if (XGMAC_GET_BITS(mac_isr, MAC_ISR, MMCTXIS))
  455. hw_if->tx_mmc_int(pdata);
  456. if (XGMAC_GET_BITS(mac_isr, MAC_ISR, MMCRXIS))
  457. hw_if->rx_mmc_int(pdata);
  458. if (XGMAC_GET_BITS(mac_isr, MAC_ISR, TSIS)) {
  459. mac_tssr = XGMAC_IOREAD(pdata, MAC_TSSR);
  460. netif_dbg(pdata, intr, pdata->netdev,
  461. "MAC_TSSR=%#010x\n", mac_tssr);
  462. if (XGMAC_GET_BITS(mac_tssr, MAC_TSSR, TXTSC)) {
  463. /* Read Tx Timestamp to clear interrupt */
  464. pdata->tx_tstamp =
  465. hw_if->get_tx_tstamp(pdata);
  466. queue_work(pdata->dev_workqueue,
  467. &pdata->tx_tstamp_work);
  468. }
  469. }
  470. if (XGMAC_GET_BITS(mac_isr, MAC_ISR, SMI)) {
  471. mac_mdioisr = XGMAC_IOREAD(pdata, MAC_MDIOISR);
  472. netif_dbg(pdata, intr, pdata->netdev,
  473. "MAC_MDIOISR=%#010x\n", mac_mdioisr);
  474. if (XGMAC_GET_BITS(mac_mdioisr, MAC_MDIOISR,
  475. SNGLCOMPINT))
  476. complete(&pdata->mdio_complete);
  477. }
  478. }
  479. isr_done:
  480. /* If there is not a separate AN irq, handle it here */
  481. if (pdata->dev_irq == pdata->an_irq)
  482. pdata->phy_if.an_isr(pdata);
  483. /* If there is not a separate ECC irq, handle it here */
  484. if (pdata->vdata->ecc_support && (pdata->dev_irq == pdata->ecc_irq))
  485. xgbe_ecc_isr_task(&pdata->tasklet_ecc);
  486. /* If there is not a separate I2C irq, handle it here */
  487. if (pdata->vdata->i2c_support && (pdata->dev_irq == pdata->i2c_irq))
  488. pdata->i2c_if.i2c_isr(pdata);
  489. /* Reissue interrupt if status is not clear */
  490. if (pdata->vdata->irq_reissue_support) {
  491. unsigned int reissue_mask;
  492. reissue_mask = 1 << 0;
  493. if (!pdata->per_channel_irq)
  494. reissue_mask |= 0xffff << 4;
  495. XP_IOWRITE(pdata, XP_INT_REISSUE_EN, reissue_mask);
  496. }
  497. }
  498. static irqreturn_t xgbe_isr(int irq, void *data)
  499. {
  500. struct xgbe_prv_data *pdata = data;
  501. if (pdata->isr_as_tasklet)
  502. tasklet_schedule(&pdata->tasklet_dev);
  503. else
  504. xgbe_isr_task(&pdata->tasklet_dev);
  505. return IRQ_HANDLED;
  506. }
  507. static irqreturn_t xgbe_dma_isr(int irq, void *data)
  508. {
  509. struct xgbe_channel *channel = data;
  510. struct xgbe_prv_data *pdata = channel->pdata;
  511. unsigned int dma_status;
  512. /* Per channel DMA interrupts are enabled, so we use the per
  513. * channel napi structure and not the private data napi structure
  514. */
  515. if (napi_schedule_prep(&channel->napi)) {
  516. /* Disable Tx and Rx interrupts */
  517. if (pdata->channel_irq_mode)
  518. xgbe_disable_rx_tx_int(pdata, channel);
  519. else
  520. disable_irq_nosync(channel->dma_irq);
  521. /* Turn on polling */
  522. __napi_schedule_irqoff(&channel->napi);
  523. }
  524. /* Clear Tx/Rx signals */
  525. dma_status = 0;
  526. XGMAC_SET_BITS(dma_status, DMA_CH_SR, TI, 1);
  527. XGMAC_SET_BITS(dma_status, DMA_CH_SR, RI, 1);
  528. XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_status);
  529. return IRQ_HANDLED;
  530. }
  531. static void xgbe_tx_timer(struct timer_list *t)
  532. {
  533. struct xgbe_channel *channel = from_timer(channel, t, tx_timer);
  534. struct xgbe_prv_data *pdata = channel->pdata;
  535. struct napi_struct *napi;
  536. DBGPR("-->xgbe_tx_timer\n");
  537. napi = (pdata->per_channel_irq) ? &channel->napi : &pdata->napi;
  538. if (napi_schedule_prep(napi)) {
  539. /* Disable Tx and Rx interrupts */
  540. if (pdata->per_channel_irq)
  541. if (pdata->channel_irq_mode)
  542. xgbe_disable_rx_tx_int(pdata, channel);
  543. else
  544. disable_irq_nosync(channel->dma_irq);
  545. else
  546. xgbe_disable_rx_tx_ints(pdata);
  547. /* Turn on polling */
  548. __napi_schedule(napi);
  549. }
  550. channel->tx_timer_active = 0;
  551. DBGPR("<--xgbe_tx_timer\n");
  552. }
  553. static void xgbe_service(struct work_struct *work)
  554. {
  555. struct xgbe_prv_data *pdata = container_of(work,
  556. struct xgbe_prv_data,
  557. service_work);
  558. pdata->phy_if.phy_status(pdata);
  559. }
  560. static void xgbe_service_timer(struct timer_list *t)
  561. {
  562. struct xgbe_prv_data *pdata = from_timer(pdata, t, service_timer);
  563. struct xgbe_channel *channel;
  564. unsigned int i;
  565. queue_work(pdata->dev_workqueue, &pdata->service_work);
  566. mod_timer(&pdata->service_timer, jiffies + HZ);
  567. if (!pdata->tx_usecs)
  568. return;
  569. for (i = 0; i < pdata->channel_count; i++) {
  570. channel = pdata->channel[i];
  571. if (!channel->tx_ring || channel->tx_timer_active)
  572. break;
  573. channel->tx_timer_active = 1;
  574. mod_timer(&channel->tx_timer,
  575. jiffies + usecs_to_jiffies(pdata->tx_usecs));
  576. }
  577. }
  578. static void xgbe_init_timers(struct xgbe_prv_data *pdata)
  579. {
  580. struct xgbe_channel *channel;
  581. unsigned int i;
  582. timer_setup(&pdata->service_timer, xgbe_service_timer, 0);
  583. for (i = 0; i < pdata->channel_count; i++) {
  584. channel = pdata->channel[i];
  585. if (!channel->tx_ring)
  586. break;
  587. timer_setup(&channel->tx_timer, xgbe_tx_timer, 0);
  588. }
  589. }
  590. static void xgbe_start_timers(struct xgbe_prv_data *pdata)
  591. {
  592. mod_timer(&pdata->service_timer, jiffies + HZ);
  593. }
  594. static void xgbe_stop_timers(struct xgbe_prv_data *pdata)
  595. {
  596. struct xgbe_channel *channel;
  597. unsigned int i;
  598. del_timer_sync(&pdata->service_timer);
  599. for (i = 0; i < pdata->channel_count; i++) {
  600. channel = pdata->channel[i];
  601. if (!channel->tx_ring)
  602. break;
  603. /* Deactivate the Tx timer */
  604. del_timer_sync(&channel->tx_timer);
  605. channel->tx_timer_active = 0;
  606. }
  607. }
  608. void xgbe_get_all_hw_features(struct xgbe_prv_data *pdata)
  609. {
  610. unsigned int mac_hfr0, mac_hfr1, mac_hfr2;
  611. struct xgbe_hw_features *hw_feat = &pdata->hw_feat;
  612. mac_hfr0 = XGMAC_IOREAD(pdata, MAC_HWF0R);
  613. mac_hfr1 = XGMAC_IOREAD(pdata, MAC_HWF1R);
  614. mac_hfr2 = XGMAC_IOREAD(pdata, MAC_HWF2R);
  615. memset(hw_feat, 0, sizeof(*hw_feat));
  616. hw_feat->version = XGMAC_IOREAD(pdata, MAC_VR);
  617. /* Hardware feature register 0 */
  618. hw_feat->gmii = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, GMIISEL);
  619. hw_feat->vlhash = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, VLHASH);
  620. hw_feat->sma = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SMASEL);
  621. hw_feat->rwk = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RWKSEL);
  622. hw_feat->mgk = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MGKSEL);
  623. hw_feat->mmc = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MMCSEL);
  624. hw_feat->aoe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, ARPOFFSEL);
  625. hw_feat->ts = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSEL);
  626. hw_feat->eee = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, EEESEL);
  627. hw_feat->tx_coe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TXCOESEL);
  628. hw_feat->rx_coe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RXCOESEL);
  629. hw_feat->addn_mac = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R,
  630. ADDMACADRSEL);
  631. hw_feat->ts_src = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSTSSEL);
  632. hw_feat->sa_vlan_ins = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SAVLANINS);
  633. hw_feat->vxn = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, VXN);
  634. /* Hardware feature register 1 */
  635. hw_feat->rx_fifo_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
  636. RXFIFOSIZE);
  637. hw_feat->tx_fifo_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
  638. TXFIFOSIZE);
  639. hw_feat->adv_ts_hi = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, ADVTHWORD);
  640. hw_feat->dma_width = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, ADDR64);
  641. hw_feat->dcb = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DCBEN);
  642. hw_feat->sph = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, SPHEN);
  643. hw_feat->tso = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, TSOEN);
  644. hw_feat->dma_debug = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DBGMEMA);
  645. hw_feat->rss = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, RSSEN);
  646. hw_feat->tc_cnt = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, NUMTC);
  647. hw_feat->hash_table_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
  648. HASHTBLSZ);
  649. hw_feat->l3l4_filter_num = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
  650. L3L4FNUM);
  651. /* Hardware feature register 2 */
  652. hw_feat->rx_q_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXQCNT);
  653. hw_feat->tx_q_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXQCNT);
  654. hw_feat->rx_ch_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXCHCNT);
  655. hw_feat->tx_ch_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXCHCNT);
  656. hw_feat->pps_out_num = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, PPSOUTNUM);
  657. hw_feat->aux_snap_num = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, AUXSNAPNUM);
  658. /* Translate the Hash Table size into actual number */
  659. switch (hw_feat->hash_table_size) {
  660. case 0:
  661. break;
  662. case 1:
  663. hw_feat->hash_table_size = 64;
  664. break;
  665. case 2:
  666. hw_feat->hash_table_size = 128;
  667. break;
  668. case 3:
  669. hw_feat->hash_table_size = 256;
  670. break;
  671. }
  672. /* Translate the address width setting into actual number */
  673. switch (hw_feat->dma_width) {
  674. case 0:
  675. hw_feat->dma_width = 32;
  676. break;
  677. case 1:
  678. hw_feat->dma_width = 40;
  679. break;
  680. case 2:
  681. hw_feat->dma_width = 48;
  682. break;
  683. default:
  684. hw_feat->dma_width = 32;
  685. }
  686. /* The Queue, Channel and TC counts are zero based so increment them
  687. * to get the actual number
  688. */
  689. hw_feat->rx_q_cnt++;
  690. hw_feat->tx_q_cnt++;
  691. hw_feat->rx_ch_cnt++;
  692. hw_feat->tx_ch_cnt++;
  693. hw_feat->tc_cnt++;
  694. /* Translate the fifo sizes into actual numbers */
  695. hw_feat->rx_fifo_size = 1 << (hw_feat->rx_fifo_size + 7);
  696. hw_feat->tx_fifo_size = 1 << (hw_feat->tx_fifo_size + 7);
  697. if (netif_msg_probe(pdata)) {
  698. dev_dbg(pdata->dev, "Hardware features:\n");
  699. /* Hardware feature register 0 */
  700. dev_dbg(pdata->dev, " 1GbE support : %s\n",
  701. hw_feat->gmii ? "yes" : "no");
  702. dev_dbg(pdata->dev, " VLAN hash filter : %s\n",
  703. hw_feat->vlhash ? "yes" : "no");
  704. dev_dbg(pdata->dev, " MDIO interface : %s\n",
  705. hw_feat->sma ? "yes" : "no");
  706. dev_dbg(pdata->dev, " Wake-up packet support : %s\n",
  707. hw_feat->rwk ? "yes" : "no");
  708. dev_dbg(pdata->dev, " Magic packet support : %s\n",
  709. hw_feat->mgk ? "yes" : "no");
  710. dev_dbg(pdata->dev, " Management counters : %s\n",
  711. hw_feat->mmc ? "yes" : "no");
  712. dev_dbg(pdata->dev, " ARP offload : %s\n",
  713. hw_feat->aoe ? "yes" : "no");
  714. dev_dbg(pdata->dev, " IEEE 1588-2008 Timestamp : %s\n",
  715. hw_feat->ts ? "yes" : "no");
  716. dev_dbg(pdata->dev, " Energy Efficient Ethernet : %s\n",
  717. hw_feat->eee ? "yes" : "no");
  718. dev_dbg(pdata->dev, " TX checksum offload : %s\n",
  719. hw_feat->tx_coe ? "yes" : "no");
  720. dev_dbg(pdata->dev, " RX checksum offload : %s\n",
  721. hw_feat->rx_coe ? "yes" : "no");
  722. dev_dbg(pdata->dev, " Additional MAC addresses : %u\n",
  723. hw_feat->addn_mac);
  724. dev_dbg(pdata->dev, " Timestamp source : %s\n",
  725. (hw_feat->ts_src == 1) ? "internal" :
  726. (hw_feat->ts_src == 2) ? "external" :
  727. (hw_feat->ts_src == 3) ? "internal/external" : "n/a");
  728. dev_dbg(pdata->dev, " SA/VLAN insertion : %s\n",
  729. hw_feat->sa_vlan_ins ? "yes" : "no");
  730. dev_dbg(pdata->dev, " VXLAN/NVGRE support : %s\n",
  731. hw_feat->vxn ? "yes" : "no");
  732. /* Hardware feature register 1 */
  733. dev_dbg(pdata->dev, " RX fifo size : %u\n",
  734. hw_feat->rx_fifo_size);
  735. dev_dbg(pdata->dev, " TX fifo size : %u\n",
  736. hw_feat->tx_fifo_size);
  737. dev_dbg(pdata->dev, " IEEE 1588 high word : %s\n",
  738. hw_feat->adv_ts_hi ? "yes" : "no");
  739. dev_dbg(pdata->dev, " DMA width : %u\n",
  740. hw_feat->dma_width);
  741. dev_dbg(pdata->dev, " Data Center Bridging : %s\n",
  742. hw_feat->dcb ? "yes" : "no");
  743. dev_dbg(pdata->dev, " Split header : %s\n",
  744. hw_feat->sph ? "yes" : "no");
  745. dev_dbg(pdata->dev, " TCP Segmentation Offload : %s\n",
  746. hw_feat->tso ? "yes" : "no");
  747. dev_dbg(pdata->dev, " Debug memory interface : %s\n",
  748. hw_feat->dma_debug ? "yes" : "no");
  749. dev_dbg(pdata->dev, " Receive Side Scaling : %s\n",
  750. hw_feat->rss ? "yes" : "no");
  751. dev_dbg(pdata->dev, " Traffic Class count : %u\n",
  752. hw_feat->tc_cnt);
  753. dev_dbg(pdata->dev, " Hash table size : %u\n",
  754. hw_feat->hash_table_size);
  755. dev_dbg(pdata->dev, " L3/L4 Filters : %u\n",
  756. hw_feat->l3l4_filter_num);
  757. /* Hardware feature register 2 */
  758. dev_dbg(pdata->dev, " RX queue count : %u\n",
  759. hw_feat->rx_q_cnt);
  760. dev_dbg(pdata->dev, " TX queue count : %u\n",
  761. hw_feat->tx_q_cnt);
  762. dev_dbg(pdata->dev, " RX DMA channel count : %u\n",
  763. hw_feat->rx_ch_cnt);
  764. dev_dbg(pdata->dev, " TX DMA channel count : %u\n",
  765. hw_feat->rx_ch_cnt);
  766. dev_dbg(pdata->dev, " PPS outputs : %u\n",
  767. hw_feat->pps_out_num);
  768. dev_dbg(pdata->dev, " Auxiliary snapshot inputs : %u\n",
  769. hw_feat->aux_snap_num);
  770. }
  771. }
  772. static int xgbe_vxlan_set_port(struct net_device *netdev, unsigned int table,
  773. unsigned int entry, struct udp_tunnel_info *ti)
  774. {
  775. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  776. pdata->vxlan_port = be16_to_cpu(ti->port);
  777. pdata->hw_if.enable_vxlan(pdata);
  778. return 0;
  779. }
  780. static int xgbe_vxlan_unset_port(struct net_device *netdev, unsigned int table,
  781. unsigned int entry, struct udp_tunnel_info *ti)
  782. {
  783. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  784. pdata->hw_if.disable_vxlan(pdata);
  785. pdata->vxlan_port = 0;
  786. return 0;
  787. }
  788. static const struct udp_tunnel_nic_info xgbe_udp_tunnels = {
  789. .set_port = xgbe_vxlan_set_port,
  790. .unset_port = xgbe_vxlan_unset_port,
  791. .flags = UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
  792. .tables = {
  793. { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, },
  794. },
  795. };
  796. const struct udp_tunnel_nic_info *xgbe_get_udp_tunnel_info(void)
  797. {
  798. return &xgbe_udp_tunnels;
  799. }
  800. static void xgbe_napi_enable(struct xgbe_prv_data *pdata, unsigned int add)
  801. {
  802. struct xgbe_channel *channel;
  803. unsigned int i;
  804. if (pdata->per_channel_irq) {
  805. for (i = 0; i < pdata->channel_count; i++) {
  806. channel = pdata->channel[i];
  807. if (add)
  808. netif_napi_add(pdata->netdev, &channel->napi,
  809. xgbe_one_poll);
  810. napi_enable(&channel->napi);
  811. }
  812. } else {
  813. if (add)
  814. netif_napi_add(pdata->netdev, &pdata->napi,
  815. xgbe_all_poll);
  816. napi_enable(&pdata->napi);
  817. }
  818. }
  819. static void xgbe_napi_disable(struct xgbe_prv_data *pdata, unsigned int del)
  820. {
  821. struct xgbe_channel *channel;
  822. unsigned int i;
  823. if (pdata->per_channel_irq) {
  824. for (i = 0; i < pdata->channel_count; i++) {
  825. channel = pdata->channel[i];
  826. napi_disable(&channel->napi);
  827. if (del)
  828. netif_napi_del(&channel->napi);
  829. }
  830. } else {
  831. napi_disable(&pdata->napi);
  832. if (del)
  833. netif_napi_del(&pdata->napi);
  834. }
  835. }
  836. static int xgbe_request_irqs(struct xgbe_prv_data *pdata)
  837. {
  838. struct xgbe_channel *channel;
  839. struct net_device *netdev = pdata->netdev;
  840. unsigned int i;
  841. int ret;
  842. tasklet_setup(&pdata->tasklet_dev, xgbe_isr_task);
  843. tasklet_setup(&pdata->tasklet_ecc, xgbe_ecc_isr_task);
  844. ret = devm_request_irq(pdata->dev, pdata->dev_irq, xgbe_isr, 0,
  845. netdev_name(netdev), pdata);
  846. if (ret) {
  847. netdev_alert(netdev, "error requesting irq %d\n",
  848. pdata->dev_irq);
  849. return ret;
  850. }
  851. if (pdata->vdata->ecc_support && (pdata->dev_irq != pdata->ecc_irq)) {
  852. ret = devm_request_irq(pdata->dev, pdata->ecc_irq, xgbe_ecc_isr,
  853. 0, pdata->ecc_name, pdata);
  854. if (ret) {
  855. netdev_alert(netdev, "error requesting ecc irq %d\n",
  856. pdata->ecc_irq);
  857. goto err_dev_irq;
  858. }
  859. }
  860. if (!pdata->per_channel_irq)
  861. return 0;
  862. for (i = 0; i < pdata->channel_count; i++) {
  863. channel = pdata->channel[i];
  864. snprintf(channel->dma_irq_name,
  865. sizeof(channel->dma_irq_name) - 1,
  866. "%s-TxRx-%u", netdev_name(netdev),
  867. channel->queue_index);
  868. ret = devm_request_irq(pdata->dev, channel->dma_irq,
  869. xgbe_dma_isr, 0,
  870. channel->dma_irq_name, channel);
  871. if (ret) {
  872. netdev_alert(netdev, "error requesting irq %d\n",
  873. channel->dma_irq);
  874. goto err_dma_irq;
  875. }
  876. irq_set_affinity_hint(channel->dma_irq,
  877. &channel->affinity_mask);
  878. }
  879. return 0;
  880. err_dma_irq:
  881. /* Using an unsigned int, 'i' will go to UINT_MAX and exit */
  882. for (i--; i < pdata->channel_count; i--) {
  883. channel = pdata->channel[i];
  884. irq_set_affinity_hint(channel->dma_irq, NULL);
  885. devm_free_irq(pdata->dev, channel->dma_irq, channel);
  886. }
  887. if (pdata->vdata->ecc_support && (pdata->dev_irq != pdata->ecc_irq))
  888. devm_free_irq(pdata->dev, pdata->ecc_irq, pdata);
  889. err_dev_irq:
  890. devm_free_irq(pdata->dev, pdata->dev_irq, pdata);
  891. return ret;
  892. }
  893. static void xgbe_free_irqs(struct xgbe_prv_data *pdata)
  894. {
  895. struct xgbe_channel *channel;
  896. unsigned int i;
  897. devm_free_irq(pdata->dev, pdata->dev_irq, pdata);
  898. tasklet_kill(&pdata->tasklet_dev);
  899. tasklet_kill(&pdata->tasklet_ecc);
  900. if (pdata->vdata->ecc_support && (pdata->dev_irq != pdata->ecc_irq))
  901. devm_free_irq(pdata->dev, pdata->ecc_irq, pdata);
  902. if (!pdata->per_channel_irq)
  903. return;
  904. for (i = 0; i < pdata->channel_count; i++) {
  905. channel = pdata->channel[i];
  906. irq_set_affinity_hint(channel->dma_irq, NULL);
  907. devm_free_irq(pdata->dev, channel->dma_irq, channel);
  908. }
  909. }
  910. void xgbe_init_tx_coalesce(struct xgbe_prv_data *pdata)
  911. {
  912. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  913. DBGPR("-->xgbe_init_tx_coalesce\n");
  914. pdata->tx_usecs = XGMAC_INIT_DMA_TX_USECS;
  915. pdata->tx_frames = XGMAC_INIT_DMA_TX_FRAMES;
  916. hw_if->config_tx_coalesce(pdata);
  917. DBGPR("<--xgbe_init_tx_coalesce\n");
  918. }
  919. void xgbe_init_rx_coalesce(struct xgbe_prv_data *pdata)
  920. {
  921. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  922. DBGPR("-->xgbe_init_rx_coalesce\n");
  923. pdata->rx_riwt = hw_if->usec_to_riwt(pdata, XGMAC_INIT_DMA_RX_USECS);
  924. pdata->rx_usecs = XGMAC_INIT_DMA_RX_USECS;
  925. pdata->rx_frames = XGMAC_INIT_DMA_RX_FRAMES;
  926. hw_if->config_rx_coalesce(pdata);
  927. DBGPR("<--xgbe_init_rx_coalesce\n");
  928. }
  929. static void xgbe_free_tx_data(struct xgbe_prv_data *pdata)
  930. {
  931. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  932. struct xgbe_ring *ring;
  933. struct xgbe_ring_data *rdata;
  934. unsigned int i, j;
  935. DBGPR("-->xgbe_free_tx_data\n");
  936. for (i = 0; i < pdata->channel_count; i++) {
  937. ring = pdata->channel[i]->tx_ring;
  938. if (!ring)
  939. break;
  940. for (j = 0; j < ring->rdesc_count; j++) {
  941. rdata = XGBE_GET_DESC_DATA(ring, j);
  942. desc_if->unmap_rdata(pdata, rdata);
  943. }
  944. }
  945. DBGPR("<--xgbe_free_tx_data\n");
  946. }
  947. static void xgbe_free_rx_data(struct xgbe_prv_data *pdata)
  948. {
  949. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  950. struct xgbe_ring *ring;
  951. struct xgbe_ring_data *rdata;
  952. unsigned int i, j;
  953. DBGPR("-->xgbe_free_rx_data\n");
  954. for (i = 0; i < pdata->channel_count; i++) {
  955. ring = pdata->channel[i]->rx_ring;
  956. if (!ring)
  957. break;
  958. for (j = 0; j < ring->rdesc_count; j++) {
  959. rdata = XGBE_GET_DESC_DATA(ring, j);
  960. desc_if->unmap_rdata(pdata, rdata);
  961. }
  962. }
  963. DBGPR("<--xgbe_free_rx_data\n");
  964. }
  965. static int xgbe_phy_reset(struct xgbe_prv_data *pdata)
  966. {
  967. pdata->phy_link = -1;
  968. pdata->phy_speed = SPEED_UNKNOWN;
  969. return pdata->phy_if.phy_reset(pdata);
  970. }
  971. int xgbe_powerdown(struct net_device *netdev, unsigned int caller)
  972. {
  973. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  974. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  975. unsigned long flags;
  976. DBGPR("-->xgbe_powerdown\n");
  977. if (!netif_running(netdev) ||
  978. (caller == XGMAC_IOCTL_CONTEXT && pdata->power_down)) {
  979. netdev_alert(netdev, "Device is already powered down\n");
  980. DBGPR("<--xgbe_powerdown\n");
  981. return -EINVAL;
  982. }
  983. spin_lock_irqsave(&pdata->lock, flags);
  984. if (caller == XGMAC_DRIVER_CONTEXT)
  985. netif_device_detach(netdev);
  986. netif_tx_stop_all_queues(netdev);
  987. xgbe_stop_timers(pdata);
  988. flush_workqueue(pdata->dev_workqueue);
  989. hw_if->powerdown_tx(pdata);
  990. hw_if->powerdown_rx(pdata);
  991. xgbe_napi_disable(pdata, 0);
  992. pdata->power_down = 1;
  993. spin_unlock_irqrestore(&pdata->lock, flags);
  994. DBGPR("<--xgbe_powerdown\n");
  995. return 0;
  996. }
  997. int xgbe_powerup(struct net_device *netdev, unsigned int caller)
  998. {
  999. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1000. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1001. unsigned long flags;
  1002. DBGPR("-->xgbe_powerup\n");
  1003. if (!netif_running(netdev) ||
  1004. (caller == XGMAC_IOCTL_CONTEXT && !pdata->power_down)) {
  1005. netdev_alert(netdev, "Device is already powered up\n");
  1006. DBGPR("<--xgbe_powerup\n");
  1007. return -EINVAL;
  1008. }
  1009. spin_lock_irqsave(&pdata->lock, flags);
  1010. pdata->power_down = 0;
  1011. xgbe_napi_enable(pdata, 0);
  1012. hw_if->powerup_tx(pdata);
  1013. hw_if->powerup_rx(pdata);
  1014. if (caller == XGMAC_DRIVER_CONTEXT)
  1015. netif_device_attach(netdev);
  1016. netif_tx_start_all_queues(netdev);
  1017. xgbe_start_timers(pdata);
  1018. spin_unlock_irqrestore(&pdata->lock, flags);
  1019. DBGPR("<--xgbe_powerup\n");
  1020. return 0;
  1021. }
  1022. static void xgbe_free_memory(struct xgbe_prv_data *pdata)
  1023. {
  1024. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  1025. /* Free the ring descriptors and buffers */
  1026. desc_if->free_ring_resources(pdata);
  1027. /* Free the channel and ring structures */
  1028. xgbe_free_channels(pdata);
  1029. }
  1030. static int xgbe_alloc_memory(struct xgbe_prv_data *pdata)
  1031. {
  1032. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  1033. struct net_device *netdev = pdata->netdev;
  1034. int ret;
  1035. if (pdata->new_tx_ring_count) {
  1036. pdata->tx_ring_count = pdata->new_tx_ring_count;
  1037. pdata->tx_q_count = pdata->tx_ring_count;
  1038. pdata->new_tx_ring_count = 0;
  1039. }
  1040. if (pdata->new_rx_ring_count) {
  1041. pdata->rx_ring_count = pdata->new_rx_ring_count;
  1042. pdata->new_rx_ring_count = 0;
  1043. }
  1044. /* Calculate the Rx buffer size before allocating rings */
  1045. pdata->rx_buf_size = xgbe_calc_rx_buf_size(netdev, netdev->mtu);
  1046. /* Allocate the channel and ring structures */
  1047. ret = xgbe_alloc_channels(pdata);
  1048. if (ret)
  1049. return ret;
  1050. /* Allocate the ring descriptors and buffers */
  1051. ret = desc_if->alloc_ring_resources(pdata);
  1052. if (ret)
  1053. goto err_channels;
  1054. /* Initialize the service and Tx timers */
  1055. xgbe_init_timers(pdata);
  1056. return 0;
  1057. err_channels:
  1058. xgbe_free_memory(pdata);
  1059. return ret;
  1060. }
  1061. static int xgbe_start(struct xgbe_prv_data *pdata)
  1062. {
  1063. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1064. struct xgbe_phy_if *phy_if = &pdata->phy_if;
  1065. struct net_device *netdev = pdata->netdev;
  1066. unsigned int i;
  1067. int ret;
  1068. /* Set the number of queues */
  1069. ret = netif_set_real_num_tx_queues(netdev, pdata->tx_ring_count);
  1070. if (ret) {
  1071. netdev_err(netdev, "error setting real tx queue count\n");
  1072. return ret;
  1073. }
  1074. ret = netif_set_real_num_rx_queues(netdev, pdata->rx_ring_count);
  1075. if (ret) {
  1076. netdev_err(netdev, "error setting real rx queue count\n");
  1077. return ret;
  1078. }
  1079. /* Set RSS lookup table data for programming */
  1080. for (i = 0; i < XGBE_RSS_MAX_TABLE_SIZE; i++)
  1081. XGMAC_SET_BITS(pdata->rss_table[i], MAC_RSSDR, DMCH,
  1082. i % pdata->rx_ring_count);
  1083. ret = hw_if->init(pdata);
  1084. if (ret)
  1085. return ret;
  1086. xgbe_napi_enable(pdata, 1);
  1087. ret = xgbe_request_irqs(pdata);
  1088. if (ret)
  1089. goto err_napi;
  1090. ret = phy_if->phy_start(pdata);
  1091. if (ret)
  1092. goto err_irqs;
  1093. hw_if->enable_tx(pdata);
  1094. hw_if->enable_rx(pdata);
  1095. udp_tunnel_nic_reset_ntf(netdev);
  1096. netif_tx_start_all_queues(netdev);
  1097. xgbe_start_timers(pdata);
  1098. queue_work(pdata->dev_workqueue, &pdata->service_work);
  1099. clear_bit(XGBE_STOPPED, &pdata->dev_state);
  1100. return 0;
  1101. err_irqs:
  1102. xgbe_free_irqs(pdata);
  1103. err_napi:
  1104. xgbe_napi_disable(pdata, 1);
  1105. hw_if->exit(pdata);
  1106. return ret;
  1107. }
  1108. static void xgbe_stop(struct xgbe_prv_data *pdata)
  1109. {
  1110. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1111. struct xgbe_phy_if *phy_if = &pdata->phy_if;
  1112. struct xgbe_channel *channel;
  1113. struct net_device *netdev = pdata->netdev;
  1114. struct netdev_queue *txq;
  1115. unsigned int i;
  1116. DBGPR("-->xgbe_stop\n");
  1117. if (test_bit(XGBE_STOPPED, &pdata->dev_state))
  1118. return;
  1119. netif_tx_stop_all_queues(netdev);
  1120. netif_carrier_off(pdata->netdev);
  1121. xgbe_stop_timers(pdata);
  1122. flush_workqueue(pdata->dev_workqueue);
  1123. xgbe_vxlan_unset_port(netdev, 0, 0, NULL);
  1124. hw_if->disable_tx(pdata);
  1125. hw_if->disable_rx(pdata);
  1126. phy_if->phy_stop(pdata);
  1127. xgbe_free_irqs(pdata);
  1128. xgbe_napi_disable(pdata, 1);
  1129. hw_if->exit(pdata);
  1130. for (i = 0; i < pdata->channel_count; i++) {
  1131. channel = pdata->channel[i];
  1132. if (!channel->tx_ring)
  1133. continue;
  1134. txq = netdev_get_tx_queue(netdev, channel->queue_index);
  1135. netdev_tx_reset_queue(txq);
  1136. }
  1137. set_bit(XGBE_STOPPED, &pdata->dev_state);
  1138. DBGPR("<--xgbe_stop\n");
  1139. }
  1140. static void xgbe_stopdev(struct work_struct *work)
  1141. {
  1142. struct xgbe_prv_data *pdata = container_of(work,
  1143. struct xgbe_prv_data,
  1144. stopdev_work);
  1145. rtnl_lock();
  1146. xgbe_stop(pdata);
  1147. xgbe_free_tx_data(pdata);
  1148. xgbe_free_rx_data(pdata);
  1149. rtnl_unlock();
  1150. netdev_alert(pdata->netdev, "device stopped\n");
  1151. }
  1152. void xgbe_full_restart_dev(struct xgbe_prv_data *pdata)
  1153. {
  1154. /* If not running, "restart" will happen on open */
  1155. if (!netif_running(pdata->netdev))
  1156. return;
  1157. xgbe_stop(pdata);
  1158. xgbe_free_memory(pdata);
  1159. xgbe_alloc_memory(pdata);
  1160. xgbe_start(pdata);
  1161. }
  1162. void xgbe_restart_dev(struct xgbe_prv_data *pdata)
  1163. {
  1164. /* If not running, "restart" will happen on open */
  1165. if (!netif_running(pdata->netdev))
  1166. return;
  1167. xgbe_stop(pdata);
  1168. xgbe_free_tx_data(pdata);
  1169. xgbe_free_rx_data(pdata);
  1170. xgbe_start(pdata);
  1171. }
  1172. static void xgbe_restart(struct work_struct *work)
  1173. {
  1174. struct xgbe_prv_data *pdata = container_of(work,
  1175. struct xgbe_prv_data,
  1176. restart_work);
  1177. rtnl_lock();
  1178. xgbe_restart_dev(pdata);
  1179. rtnl_unlock();
  1180. }
  1181. static void xgbe_tx_tstamp(struct work_struct *work)
  1182. {
  1183. struct xgbe_prv_data *pdata = container_of(work,
  1184. struct xgbe_prv_data,
  1185. tx_tstamp_work);
  1186. struct skb_shared_hwtstamps hwtstamps;
  1187. u64 nsec;
  1188. unsigned long flags;
  1189. spin_lock_irqsave(&pdata->tstamp_lock, flags);
  1190. if (!pdata->tx_tstamp_skb)
  1191. goto unlock;
  1192. if (pdata->tx_tstamp) {
  1193. nsec = timecounter_cyc2time(&pdata->tstamp_tc,
  1194. pdata->tx_tstamp);
  1195. memset(&hwtstamps, 0, sizeof(hwtstamps));
  1196. hwtstamps.hwtstamp = ns_to_ktime(nsec);
  1197. skb_tstamp_tx(pdata->tx_tstamp_skb, &hwtstamps);
  1198. }
  1199. dev_kfree_skb_any(pdata->tx_tstamp_skb);
  1200. pdata->tx_tstamp_skb = NULL;
  1201. unlock:
  1202. spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
  1203. }
  1204. static int xgbe_get_hwtstamp_settings(struct xgbe_prv_data *pdata,
  1205. struct ifreq *ifreq)
  1206. {
  1207. if (copy_to_user(ifreq->ifr_data, &pdata->tstamp_config,
  1208. sizeof(pdata->tstamp_config)))
  1209. return -EFAULT;
  1210. return 0;
  1211. }
  1212. static int xgbe_set_hwtstamp_settings(struct xgbe_prv_data *pdata,
  1213. struct ifreq *ifreq)
  1214. {
  1215. struct hwtstamp_config config;
  1216. unsigned int mac_tscr;
  1217. if (copy_from_user(&config, ifreq->ifr_data, sizeof(config)))
  1218. return -EFAULT;
  1219. mac_tscr = 0;
  1220. switch (config.tx_type) {
  1221. case HWTSTAMP_TX_OFF:
  1222. break;
  1223. case HWTSTAMP_TX_ON:
  1224. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  1225. break;
  1226. default:
  1227. return -ERANGE;
  1228. }
  1229. switch (config.rx_filter) {
  1230. case HWTSTAMP_FILTER_NONE:
  1231. break;
  1232. case HWTSTAMP_FILTER_NTP_ALL:
  1233. case HWTSTAMP_FILTER_ALL:
  1234. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENALL, 1);
  1235. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  1236. break;
  1237. /* PTP v2, UDP, any kind of event packet */
  1238. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  1239. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
  1240. fallthrough; /* to PTP v1, UDP, any kind of event packet */
  1241. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  1242. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
  1243. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
  1244. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
  1245. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  1246. break;
  1247. /* PTP v2, UDP, Sync packet */
  1248. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  1249. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
  1250. fallthrough; /* to PTP v1, UDP, Sync packet */
  1251. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  1252. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
  1253. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
  1254. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
  1255. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  1256. break;
  1257. /* PTP v2, UDP, Delay_req packet */
  1258. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  1259. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
  1260. fallthrough; /* to PTP v1, UDP, Delay_req packet */
  1261. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  1262. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
  1263. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
  1264. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
  1265. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
  1266. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  1267. break;
  1268. /* 802.AS1, Ethernet, any kind of event packet */
  1269. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  1270. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
  1271. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
  1272. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  1273. break;
  1274. /* 802.AS1, Ethernet, Sync packet */
  1275. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  1276. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
  1277. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
  1278. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  1279. break;
  1280. /* 802.AS1, Ethernet, Delay_req packet */
  1281. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  1282. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
  1283. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
  1284. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
  1285. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  1286. break;
  1287. /* PTP v2/802.AS1, any layer, any kind of event packet */
  1288. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  1289. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
  1290. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
  1291. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
  1292. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
  1293. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
  1294. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  1295. break;
  1296. /* PTP v2/802.AS1, any layer, Sync packet */
  1297. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  1298. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
  1299. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
  1300. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
  1301. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
  1302. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
  1303. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  1304. break;
  1305. /* PTP v2/802.AS1, any layer, Delay_req packet */
  1306. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  1307. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
  1308. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
  1309. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
  1310. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
  1311. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
  1312. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
  1313. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  1314. break;
  1315. default:
  1316. return -ERANGE;
  1317. }
  1318. pdata->hw_if.config_tstamp(pdata, mac_tscr);
  1319. memcpy(&pdata->tstamp_config, &config, sizeof(config));
  1320. return 0;
  1321. }
  1322. static void xgbe_prep_tx_tstamp(struct xgbe_prv_data *pdata,
  1323. struct sk_buff *skb,
  1324. struct xgbe_packet_data *packet)
  1325. {
  1326. unsigned long flags;
  1327. if (XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP)) {
  1328. spin_lock_irqsave(&pdata->tstamp_lock, flags);
  1329. if (pdata->tx_tstamp_skb) {
  1330. /* Another timestamp in progress, ignore this one */
  1331. XGMAC_SET_BITS(packet->attributes,
  1332. TX_PACKET_ATTRIBUTES, PTP, 0);
  1333. } else {
  1334. pdata->tx_tstamp_skb = skb_get(skb);
  1335. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1336. }
  1337. spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
  1338. }
  1339. skb_tx_timestamp(skb);
  1340. }
  1341. static void xgbe_prep_vlan(struct sk_buff *skb, struct xgbe_packet_data *packet)
  1342. {
  1343. if (skb_vlan_tag_present(skb))
  1344. packet->vlan_ctag = skb_vlan_tag_get(skb);
  1345. }
  1346. static int xgbe_prep_tso(struct sk_buff *skb, struct xgbe_packet_data *packet)
  1347. {
  1348. int ret;
  1349. if (!XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1350. TSO_ENABLE))
  1351. return 0;
  1352. ret = skb_cow_head(skb, 0);
  1353. if (ret)
  1354. return ret;
  1355. if (XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, VXLAN)) {
  1356. packet->header_len = skb_inner_tcp_all_headers(skb);
  1357. packet->tcp_header_len = inner_tcp_hdrlen(skb);
  1358. } else {
  1359. packet->header_len = skb_tcp_all_headers(skb);
  1360. packet->tcp_header_len = tcp_hdrlen(skb);
  1361. }
  1362. packet->tcp_payload_len = skb->len - packet->header_len;
  1363. packet->mss = skb_shinfo(skb)->gso_size;
  1364. DBGPR(" packet->header_len=%u\n", packet->header_len);
  1365. DBGPR(" packet->tcp_header_len=%u, packet->tcp_payload_len=%u\n",
  1366. packet->tcp_header_len, packet->tcp_payload_len);
  1367. DBGPR(" packet->mss=%u\n", packet->mss);
  1368. /* Update the number of packets that will ultimately be transmitted
  1369. * along with the extra bytes for each extra packet
  1370. */
  1371. packet->tx_packets = skb_shinfo(skb)->gso_segs;
  1372. packet->tx_bytes += (packet->tx_packets - 1) * packet->header_len;
  1373. return 0;
  1374. }
  1375. static bool xgbe_is_vxlan(struct sk_buff *skb)
  1376. {
  1377. if (!skb->encapsulation)
  1378. return false;
  1379. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1380. return false;
  1381. switch (skb->protocol) {
  1382. case htons(ETH_P_IP):
  1383. if (ip_hdr(skb)->protocol != IPPROTO_UDP)
  1384. return false;
  1385. break;
  1386. case htons(ETH_P_IPV6):
  1387. if (ipv6_hdr(skb)->nexthdr != IPPROTO_UDP)
  1388. return false;
  1389. break;
  1390. default:
  1391. return false;
  1392. }
  1393. if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
  1394. skb->inner_protocol != htons(ETH_P_TEB) ||
  1395. (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
  1396. sizeof(struct udphdr) + sizeof(struct vxlanhdr)))
  1397. return false;
  1398. return true;
  1399. }
  1400. static int xgbe_is_tso(struct sk_buff *skb)
  1401. {
  1402. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1403. return 0;
  1404. if (!skb_is_gso(skb))
  1405. return 0;
  1406. DBGPR(" TSO packet to be processed\n");
  1407. return 1;
  1408. }
  1409. static void xgbe_packet_info(struct xgbe_prv_data *pdata,
  1410. struct xgbe_ring *ring, struct sk_buff *skb,
  1411. struct xgbe_packet_data *packet)
  1412. {
  1413. skb_frag_t *frag;
  1414. unsigned int context_desc;
  1415. unsigned int len;
  1416. unsigned int i;
  1417. packet->skb = skb;
  1418. context_desc = 0;
  1419. packet->rdesc_count = 0;
  1420. packet->tx_packets = 1;
  1421. packet->tx_bytes = skb->len;
  1422. if (xgbe_is_tso(skb)) {
  1423. /* TSO requires an extra descriptor if mss is different */
  1424. if (skb_shinfo(skb)->gso_size != ring->tx.cur_mss) {
  1425. context_desc = 1;
  1426. packet->rdesc_count++;
  1427. }
  1428. /* TSO requires an extra descriptor for TSO header */
  1429. packet->rdesc_count++;
  1430. XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1431. TSO_ENABLE, 1);
  1432. XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1433. CSUM_ENABLE, 1);
  1434. } else if (skb->ip_summed == CHECKSUM_PARTIAL)
  1435. XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1436. CSUM_ENABLE, 1);
  1437. if (xgbe_is_vxlan(skb))
  1438. XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1439. VXLAN, 1);
  1440. if (skb_vlan_tag_present(skb)) {
  1441. /* VLAN requires an extra descriptor if tag is different */
  1442. if (skb_vlan_tag_get(skb) != ring->tx.cur_vlan_ctag)
  1443. /* We can share with the TSO context descriptor */
  1444. if (!context_desc) {
  1445. context_desc = 1;
  1446. packet->rdesc_count++;
  1447. }
  1448. XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1449. VLAN_CTAG, 1);
  1450. }
  1451. if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
  1452. (pdata->tstamp_config.tx_type == HWTSTAMP_TX_ON))
  1453. XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1454. PTP, 1);
  1455. for (len = skb_headlen(skb); len;) {
  1456. packet->rdesc_count++;
  1457. len -= min_t(unsigned int, len, XGBE_TX_MAX_BUF_SIZE);
  1458. }
  1459. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1460. frag = &skb_shinfo(skb)->frags[i];
  1461. for (len = skb_frag_size(frag); len; ) {
  1462. packet->rdesc_count++;
  1463. len -= min_t(unsigned int, len, XGBE_TX_MAX_BUF_SIZE);
  1464. }
  1465. }
  1466. }
  1467. static int xgbe_open(struct net_device *netdev)
  1468. {
  1469. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1470. int ret;
  1471. /* Create the various names based on netdev name */
  1472. snprintf(pdata->an_name, sizeof(pdata->an_name) - 1, "%s-pcs",
  1473. netdev_name(netdev));
  1474. snprintf(pdata->ecc_name, sizeof(pdata->ecc_name) - 1, "%s-ecc",
  1475. netdev_name(netdev));
  1476. snprintf(pdata->i2c_name, sizeof(pdata->i2c_name) - 1, "%s-i2c",
  1477. netdev_name(netdev));
  1478. /* Create workqueues */
  1479. pdata->dev_workqueue =
  1480. create_singlethread_workqueue(netdev_name(netdev));
  1481. if (!pdata->dev_workqueue) {
  1482. netdev_err(netdev, "device workqueue creation failed\n");
  1483. return -ENOMEM;
  1484. }
  1485. pdata->an_workqueue =
  1486. create_singlethread_workqueue(pdata->an_name);
  1487. if (!pdata->an_workqueue) {
  1488. netdev_err(netdev, "phy workqueue creation failed\n");
  1489. ret = -ENOMEM;
  1490. goto err_dev_wq;
  1491. }
  1492. /* Reset the phy settings */
  1493. ret = xgbe_phy_reset(pdata);
  1494. if (ret)
  1495. goto err_an_wq;
  1496. /* Enable the clocks */
  1497. ret = clk_prepare_enable(pdata->sysclk);
  1498. if (ret) {
  1499. netdev_alert(netdev, "dma clk_prepare_enable failed\n");
  1500. goto err_an_wq;
  1501. }
  1502. ret = clk_prepare_enable(pdata->ptpclk);
  1503. if (ret) {
  1504. netdev_alert(netdev, "ptp clk_prepare_enable failed\n");
  1505. goto err_sysclk;
  1506. }
  1507. INIT_WORK(&pdata->service_work, xgbe_service);
  1508. INIT_WORK(&pdata->restart_work, xgbe_restart);
  1509. INIT_WORK(&pdata->stopdev_work, xgbe_stopdev);
  1510. INIT_WORK(&pdata->tx_tstamp_work, xgbe_tx_tstamp);
  1511. ret = xgbe_alloc_memory(pdata);
  1512. if (ret)
  1513. goto err_ptpclk;
  1514. ret = xgbe_start(pdata);
  1515. if (ret)
  1516. goto err_mem;
  1517. clear_bit(XGBE_DOWN, &pdata->dev_state);
  1518. return 0;
  1519. err_mem:
  1520. xgbe_free_memory(pdata);
  1521. err_ptpclk:
  1522. clk_disable_unprepare(pdata->ptpclk);
  1523. err_sysclk:
  1524. clk_disable_unprepare(pdata->sysclk);
  1525. err_an_wq:
  1526. destroy_workqueue(pdata->an_workqueue);
  1527. err_dev_wq:
  1528. destroy_workqueue(pdata->dev_workqueue);
  1529. return ret;
  1530. }
  1531. static int xgbe_close(struct net_device *netdev)
  1532. {
  1533. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1534. /* Stop the device */
  1535. xgbe_stop(pdata);
  1536. xgbe_free_memory(pdata);
  1537. /* Disable the clocks */
  1538. clk_disable_unprepare(pdata->ptpclk);
  1539. clk_disable_unprepare(pdata->sysclk);
  1540. destroy_workqueue(pdata->an_workqueue);
  1541. destroy_workqueue(pdata->dev_workqueue);
  1542. set_bit(XGBE_DOWN, &pdata->dev_state);
  1543. return 0;
  1544. }
  1545. static netdev_tx_t xgbe_xmit(struct sk_buff *skb, struct net_device *netdev)
  1546. {
  1547. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1548. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1549. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  1550. struct xgbe_channel *channel;
  1551. struct xgbe_ring *ring;
  1552. struct xgbe_packet_data *packet;
  1553. struct netdev_queue *txq;
  1554. netdev_tx_t ret;
  1555. DBGPR("-->xgbe_xmit: skb->len = %d\n", skb->len);
  1556. channel = pdata->channel[skb->queue_mapping];
  1557. txq = netdev_get_tx_queue(netdev, channel->queue_index);
  1558. ring = channel->tx_ring;
  1559. packet = &ring->packet_data;
  1560. ret = NETDEV_TX_OK;
  1561. if (skb->len == 0) {
  1562. netif_err(pdata, tx_err, netdev,
  1563. "empty skb received from stack\n");
  1564. dev_kfree_skb_any(skb);
  1565. goto tx_netdev_return;
  1566. }
  1567. /* Calculate preliminary packet info */
  1568. memset(packet, 0, sizeof(*packet));
  1569. xgbe_packet_info(pdata, ring, skb, packet);
  1570. /* Check that there are enough descriptors available */
  1571. ret = xgbe_maybe_stop_tx_queue(channel, ring, packet->rdesc_count);
  1572. if (ret)
  1573. goto tx_netdev_return;
  1574. ret = xgbe_prep_tso(skb, packet);
  1575. if (ret) {
  1576. netif_err(pdata, tx_err, netdev,
  1577. "error processing TSO packet\n");
  1578. dev_kfree_skb_any(skb);
  1579. goto tx_netdev_return;
  1580. }
  1581. xgbe_prep_vlan(skb, packet);
  1582. if (!desc_if->map_tx_skb(channel, skb)) {
  1583. dev_kfree_skb_any(skb);
  1584. goto tx_netdev_return;
  1585. }
  1586. xgbe_prep_tx_tstamp(pdata, skb, packet);
  1587. /* Report on the actual number of bytes (to be) sent */
  1588. netdev_tx_sent_queue(txq, packet->tx_bytes);
  1589. /* Configure required descriptor fields for transmission */
  1590. hw_if->dev_xmit(channel);
  1591. if (netif_msg_pktdata(pdata))
  1592. xgbe_print_pkt(netdev, skb, true);
  1593. /* Stop the queue in advance if there may not be enough descriptors */
  1594. xgbe_maybe_stop_tx_queue(channel, ring, XGBE_TX_MAX_DESCS);
  1595. ret = NETDEV_TX_OK;
  1596. tx_netdev_return:
  1597. return ret;
  1598. }
  1599. static void xgbe_set_rx_mode(struct net_device *netdev)
  1600. {
  1601. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1602. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1603. DBGPR("-->xgbe_set_rx_mode\n");
  1604. hw_if->config_rx_mode(pdata);
  1605. DBGPR("<--xgbe_set_rx_mode\n");
  1606. }
  1607. static int xgbe_set_mac_address(struct net_device *netdev, void *addr)
  1608. {
  1609. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1610. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1611. struct sockaddr *saddr = addr;
  1612. DBGPR("-->xgbe_set_mac_address\n");
  1613. if (!is_valid_ether_addr(saddr->sa_data))
  1614. return -EADDRNOTAVAIL;
  1615. eth_hw_addr_set(netdev, saddr->sa_data);
  1616. hw_if->set_mac_address(pdata, netdev->dev_addr);
  1617. DBGPR("<--xgbe_set_mac_address\n");
  1618. return 0;
  1619. }
  1620. static int xgbe_ioctl(struct net_device *netdev, struct ifreq *ifreq, int cmd)
  1621. {
  1622. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1623. int ret;
  1624. switch (cmd) {
  1625. case SIOCGHWTSTAMP:
  1626. ret = xgbe_get_hwtstamp_settings(pdata, ifreq);
  1627. break;
  1628. case SIOCSHWTSTAMP:
  1629. ret = xgbe_set_hwtstamp_settings(pdata, ifreq);
  1630. break;
  1631. default:
  1632. ret = -EOPNOTSUPP;
  1633. }
  1634. return ret;
  1635. }
  1636. static int xgbe_change_mtu(struct net_device *netdev, int mtu)
  1637. {
  1638. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1639. int ret;
  1640. DBGPR("-->xgbe_change_mtu\n");
  1641. ret = xgbe_calc_rx_buf_size(netdev, mtu);
  1642. if (ret < 0)
  1643. return ret;
  1644. pdata->rx_buf_size = ret;
  1645. netdev->mtu = mtu;
  1646. xgbe_restart_dev(pdata);
  1647. DBGPR("<--xgbe_change_mtu\n");
  1648. return 0;
  1649. }
  1650. static void xgbe_tx_timeout(struct net_device *netdev, unsigned int txqueue)
  1651. {
  1652. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1653. netdev_warn(netdev, "tx timeout, device restarting\n");
  1654. schedule_work(&pdata->restart_work);
  1655. }
  1656. static void xgbe_get_stats64(struct net_device *netdev,
  1657. struct rtnl_link_stats64 *s)
  1658. {
  1659. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1660. struct xgbe_mmc_stats *pstats = &pdata->mmc_stats;
  1661. DBGPR("-->%s\n", __func__);
  1662. pdata->hw_if.read_mmc_stats(pdata);
  1663. s->rx_packets = pstats->rxframecount_gb;
  1664. s->rx_bytes = pstats->rxoctetcount_gb;
  1665. s->rx_errors = pstats->rxframecount_gb -
  1666. pstats->rxbroadcastframes_g -
  1667. pstats->rxmulticastframes_g -
  1668. pstats->rxunicastframes_g;
  1669. s->multicast = pstats->rxmulticastframes_g;
  1670. s->rx_length_errors = pstats->rxlengtherror;
  1671. s->rx_crc_errors = pstats->rxcrcerror;
  1672. s->rx_fifo_errors = pstats->rxfifooverflow;
  1673. s->tx_packets = pstats->txframecount_gb;
  1674. s->tx_bytes = pstats->txoctetcount_gb;
  1675. s->tx_errors = pstats->txframecount_gb - pstats->txframecount_g;
  1676. s->tx_dropped = netdev->stats.tx_dropped;
  1677. DBGPR("<--%s\n", __func__);
  1678. }
  1679. static int xgbe_vlan_rx_add_vid(struct net_device *netdev, __be16 proto,
  1680. u16 vid)
  1681. {
  1682. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1683. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1684. DBGPR("-->%s\n", __func__);
  1685. set_bit(vid, pdata->active_vlans);
  1686. hw_if->update_vlan_hash_table(pdata);
  1687. DBGPR("<--%s\n", __func__);
  1688. return 0;
  1689. }
  1690. static int xgbe_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto,
  1691. u16 vid)
  1692. {
  1693. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1694. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1695. DBGPR("-->%s\n", __func__);
  1696. clear_bit(vid, pdata->active_vlans);
  1697. hw_if->update_vlan_hash_table(pdata);
  1698. DBGPR("<--%s\n", __func__);
  1699. return 0;
  1700. }
  1701. #ifdef CONFIG_NET_POLL_CONTROLLER
  1702. static void xgbe_poll_controller(struct net_device *netdev)
  1703. {
  1704. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1705. struct xgbe_channel *channel;
  1706. unsigned int i;
  1707. DBGPR("-->xgbe_poll_controller\n");
  1708. if (pdata->per_channel_irq) {
  1709. for (i = 0; i < pdata->channel_count; i++) {
  1710. channel = pdata->channel[i];
  1711. xgbe_dma_isr(channel->dma_irq, channel);
  1712. }
  1713. } else {
  1714. disable_irq(pdata->dev_irq);
  1715. xgbe_isr(pdata->dev_irq, pdata);
  1716. enable_irq(pdata->dev_irq);
  1717. }
  1718. DBGPR("<--xgbe_poll_controller\n");
  1719. }
  1720. #endif /* End CONFIG_NET_POLL_CONTROLLER */
  1721. static int xgbe_setup_tc(struct net_device *netdev, enum tc_setup_type type,
  1722. void *type_data)
  1723. {
  1724. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1725. struct tc_mqprio_qopt *mqprio = type_data;
  1726. u8 tc;
  1727. if (type != TC_SETUP_QDISC_MQPRIO)
  1728. return -EOPNOTSUPP;
  1729. mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
  1730. tc = mqprio->num_tc;
  1731. if (tc > pdata->hw_feat.tc_cnt)
  1732. return -EINVAL;
  1733. pdata->num_tcs = tc;
  1734. pdata->hw_if.config_tc(pdata);
  1735. return 0;
  1736. }
  1737. static netdev_features_t xgbe_fix_features(struct net_device *netdev,
  1738. netdev_features_t features)
  1739. {
  1740. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1741. netdev_features_t vxlan_base;
  1742. vxlan_base = NETIF_F_GSO_UDP_TUNNEL | NETIF_F_RX_UDP_TUNNEL_PORT;
  1743. if (!pdata->hw_feat.vxn)
  1744. return features;
  1745. /* VXLAN CSUM requires VXLAN base */
  1746. if ((features & NETIF_F_GSO_UDP_TUNNEL_CSUM) &&
  1747. !(features & NETIF_F_GSO_UDP_TUNNEL)) {
  1748. netdev_notice(netdev,
  1749. "forcing tx udp tunnel support\n");
  1750. features |= NETIF_F_GSO_UDP_TUNNEL;
  1751. }
  1752. /* Can't do one without doing the other */
  1753. if ((features & vxlan_base) != vxlan_base) {
  1754. netdev_notice(netdev,
  1755. "forcing both tx and rx udp tunnel support\n");
  1756. features |= vxlan_base;
  1757. }
  1758. if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
  1759. if (!(features & NETIF_F_GSO_UDP_TUNNEL_CSUM)) {
  1760. netdev_notice(netdev,
  1761. "forcing tx udp tunnel checksumming on\n");
  1762. features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
  1763. }
  1764. } else {
  1765. if (features & NETIF_F_GSO_UDP_TUNNEL_CSUM) {
  1766. netdev_notice(netdev,
  1767. "forcing tx udp tunnel checksumming off\n");
  1768. features &= ~NETIF_F_GSO_UDP_TUNNEL_CSUM;
  1769. }
  1770. }
  1771. return features;
  1772. }
  1773. static int xgbe_set_features(struct net_device *netdev,
  1774. netdev_features_t features)
  1775. {
  1776. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1777. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1778. netdev_features_t rxhash, rxcsum, rxvlan, rxvlan_filter;
  1779. int ret = 0;
  1780. rxhash = pdata->netdev_features & NETIF_F_RXHASH;
  1781. rxcsum = pdata->netdev_features & NETIF_F_RXCSUM;
  1782. rxvlan = pdata->netdev_features & NETIF_F_HW_VLAN_CTAG_RX;
  1783. rxvlan_filter = pdata->netdev_features & NETIF_F_HW_VLAN_CTAG_FILTER;
  1784. if ((features & NETIF_F_RXHASH) && !rxhash)
  1785. ret = hw_if->enable_rss(pdata);
  1786. else if (!(features & NETIF_F_RXHASH) && rxhash)
  1787. ret = hw_if->disable_rss(pdata);
  1788. if (ret)
  1789. return ret;
  1790. if ((features & NETIF_F_RXCSUM) && !rxcsum)
  1791. hw_if->enable_rx_csum(pdata);
  1792. else if (!(features & NETIF_F_RXCSUM) && rxcsum)
  1793. hw_if->disable_rx_csum(pdata);
  1794. if ((features & NETIF_F_HW_VLAN_CTAG_RX) && !rxvlan)
  1795. hw_if->enable_rx_vlan_stripping(pdata);
  1796. else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) && rxvlan)
  1797. hw_if->disable_rx_vlan_stripping(pdata);
  1798. if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) && !rxvlan_filter)
  1799. hw_if->enable_rx_vlan_filtering(pdata);
  1800. else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) && rxvlan_filter)
  1801. hw_if->disable_rx_vlan_filtering(pdata);
  1802. pdata->netdev_features = features;
  1803. DBGPR("<--xgbe_set_features\n");
  1804. return 0;
  1805. }
  1806. static netdev_features_t xgbe_features_check(struct sk_buff *skb,
  1807. struct net_device *netdev,
  1808. netdev_features_t features)
  1809. {
  1810. features = vlan_features_check(skb, features);
  1811. features = vxlan_features_check(skb, features);
  1812. return features;
  1813. }
  1814. static const struct net_device_ops xgbe_netdev_ops = {
  1815. .ndo_open = xgbe_open,
  1816. .ndo_stop = xgbe_close,
  1817. .ndo_start_xmit = xgbe_xmit,
  1818. .ndo_set_rx_mode = xgbe_set_rx_mode,
  1819. .ndo_set_mac_address = xgbe_set_mac_address,
  1820. .ndo_validate_addr = eth_validate_addr,
  1821. .ndo_eth_ioctl = xgbe_ioctl,
  1822. .ndo_change_mtu = xgbe_change_mtu,
  1823. .ndo_tx_timeout = xgbe_tx_timeout,
  1824. .ndo_get_stats64 = xgbe_get_stats64,
  1825. .ndo_vlan_rx_add_vid = xgbe_vlan_rx_add_vid,
  1826. .ndo_vlan_rx_kill_vid = xgbe_vlan_rx_kill_vid,
  1827. #ifdef CONFIG_NET_POLL_CONTROLLER
  1828. .ndo_poll_controller = xgbe_poll_controller,
  1829. #endif
  1830. .ndo_setup_tc = xgbe_setup_tc,
  1831. .ndo_fix_features = xgbe_fix_features,
  1832. .ndo_set_features = xgbe_set_features,
  1833. .ndo_features_check = xgbe_features_check,
  1834. };
  1835. const struct net_device_ops *xgbe_get_netdev_ops(void)
  1836. {
  1837. return &xgbe_netdev_ops;
  1838. }
  1839. static void xgbe_rx_refresh(struct xgbe_channel *channel)
  1840. {
  1841. struct xgbe_prv_data *pdata = channel->pdata;
  1842. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1843. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  1844. struct xgbe_ring *ring = channel->rx_ring;
  1845. struct xgbe_ring_data *rdata;
  1846. while (ring->dirty != ring->cur) {
  1847. rdata = XGBE_GET_DESC_DATA(ring, ring->dirty);
  1848. /* Reset rdata values */
  1849. desc_if->unmap_rdata(pdata, rdata);
  1850. if (desc_if->map_rx_buffer(pdata, ring, rdata))
  1851. break;
  1852. hw_if->rx_desc_reset(pdata, rdata, ring->dirty);
  1853. ring->dirty++;
  1854. }
  1855. /* Make sure everything is written before the register write */
  1856. wmb();
  1857. /* Update the Rx Tail Pointer Register with address of
  1858. * the last cleaned entry */
  1859. rdata = XGBE_GET_DESC_DATA(ring, ring->dirty - 1);
  1860. XGMAC_DMA_IOWRITE(channel, DMA_CH_RDTR_LO,
  1861. lower_32_bits(rdata->rdesc_dma));
  1862. }
  1863. static struct sk_buff *xgbe_create_skb(struct xgbe_prv_data *pdata,
  1864. struct napi_struct *napi,
  1865. struct xgbe_ring_data *rdata,
  1866. unsigned int len)
  1867. {
  1868. struct sk_buff *skb;
  1869. u8 *packet;
  1870. skb = napi_alloc_skb(napi, rdata->rx.hdr.dma_len);
  1871. if (!skb)
  1872. return NULL;
  1873. /* Pull in the header buffer which may contain just the header
  1874. * or the header plus data
  1875. */
  1876. dma_sync_single_range_for_cpu(pdata->dev, rdata->rx.hdr.dma_base,
  1877. rdata->rx.hdr.dma_off,
  1878. rdata->rx.hdr.dma_len, DMA_FROM_DEVICE);
  1879. packet = page_address(rdata->rx.hdr.pa.pages) +
  1880. rdata->rx.hdr.pa.pages_offset;
  1881. skb_copy_to_linear_data(skb, packet, len);
  1882. skb_put(skb, len);
  1883. return skb;
  1884. }
  1885. static unsigned int xgbe_rx_buf1_len(struct xgbe_ring_data *rdata,
  1886. struct xgbe_packet_data *packet)
  1887. {
  1888. /* Always zero if not the first descriptor */
  1889. if (!XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, FIRST))
  1890. return 0;
  1891. /* First descriptor with split header, return header length */
  1892. if (rdata->rx.hdr_len)
  1893. return rdata->rx.hdr_len;
  1894. /* First descriptor but not the last descriptor and no split header,
  1895. * so the full buffer was used
  1896. */
  1897. if (!XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, LAST))
  1898. return rdata->rx.hdr.dma_len;
  1899. /* First descriptor and last descriptor and no split header, so
  1900. * calculate how much of the buffer was used
  1901. */
  1902. return min_t(unsigned int, rdata->rx.hdr.dma_len, rdata->rx.len);
  1903. }
  1904. static unsigned int xgbe_rx_buf2_len(struct xgbe_ring_data *rdata,
  1905. struct xgbe_packet_data *packet,
  1906. unsigned int len)
  1907. {
  1908. /* Always the full buffer if not the last descriptor */
  1909. if (!XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, LAST))
  1910. return rdata->rx.buf.dma_len;
  1911. /* Last descriptor so calculate how much of the buffer was used
  1912. * for the last bit of data
  1913. */
  1914. return rdata->rx.len - len;
  1915. }
  1916. static int xgbe_tx_poll(struct xgbe_channel *channel)
  1917. {
  1918. struct xgbe_prv_data *pdata = channel->pdata;
  1919. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1920. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  1921. struct xgbe_ring *ring = channel->tx_ring;
  1922. struct xgbe_ring_data *rdata;
  1923. struct xgbe_ring_desc *rdesc;
  1924. struct net_device *netdev = pdata->netdev;
  1925. struct netdev_queue *txq;
  1926. int processed = 0;
  1927. unsigned int tx_packets = 0, tx_bytes = 0;
  1928. unsigned int cur;
  1929. DBGPR("-->xgbe_tx_poll\n");
  1930. /* Nothing to do if there isn't a Tx ring for this channel */
  1931. if (!ring)
  1932. return 0;
  1933. cur = ring->cur;
  1934. /* Be sure we get ring->cur before accessing descriptor data */
  1935. smp_rmb();
  1936. txq = netdev_get_tx_queue(netdev, channel->queue_index);
  1937. while ((processed < XGBE_TX_DESC_MAX_PROC) &&
  1938. (ring->dirty != cur)) {
  1939. rdata = XGBE_GET_DESC_DATA(ring, ring->dirty);
  1940. rdesc = rdata->rdesc;
  1941. if (!hw_if->tx_complete(rdesc))
  1942. break;
  1943. /* Make sure descriptor fields are read after reading the OWN
  1944. * bit */
  1945. dma_rmb();
  1946. if (netif_msg_tx_done(pdata))
  1947. xgbe_dump_tx_desc(pdata, ring, ring->dirty, 1, 0);
  1948. if (hw_if->is_last_desc(rdesc)) {
  1949. tx_packets += rdata->tx.packets;
  1950. tx_bytes += rdata->tx.bytes;
  1951. }
  1952. /* Free the SKB and reset the descriptor for re-use */
  1953. desc_if->unmap_rdata(pdata, rdata);
  1954. hw_if->tx_desc_reset(rdata);
  1955. processed++;
  1956. ring->dirty++;
  1957. }
  1958. if (!processed)
  1959. return 0;
  1960. netdev_tx_completed_queue(txq, tx_packets, tx_bytes);
  1961. if ((ring->tx.queue_stopped == 1) &&
  1962. (xgbe_tx_avail_desc(ring) > XGBE_TX_DESC_MIN_FREE)) {
  1963. ring->tx.queue_stopped = 0;
  1964. netif_tx_wake_queue(txq);
  1965. }
  1966. DBGPR("<--xgbe_tx_poll: processed=%d\n", processed);
  1967. return processed;
  1968. }
  1969. static int xgbe_rx_poll(struct xgbe_channel *channel, int budget)
  1970. {
  1971. struct xgbe_prv_data *pdata = channel->pdata;
  1972. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1973. struct xgbe_ring *ring = channel->rx_ring;
  1974. struct xgbe_ring_data *rdata;
  1975. struct xgbe_packet_data *packet;
  1976. struct net_device *netdev = pdata->netdev;
  1977. struct napi_struct *napi;
  1978. struct sk_buff *skb;
  1979. struct skb_shared_hwtstamps *hwtstamps;
  1980. unsigned int last, error, context_next, context;
  1981. unsigned int len, buf1_len, buf2_len, max_len;
  1982. unsigned int received = 0;
  1983. int packet_count = 0;
  1984. DBGPR("-->xgbe_rx_poll: budget=%d\n", budget);
  1985. /* Nothing to do if there isn't a Rx ring for this channel */
  1986. if (!ring)
  1987. return 0;
  1988. last = 0;
  1989. context_next = 0;
  1990. napi = (pdata->per_channel_irq) ? &channel->napi : &pdata->napi;
  1991. rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
  1992. packet = &ring->packet_data;
  1993. while (packet_count < budget) {
  1994. DBGPR(" cur = %d\n", ring->cur);
  1995. /* First time in loop see if we need to restore state */
  1996. if (!received && rdata->state_saved) {
  1997. skb = rdata->state.skb;
  1998. error = rdata->state.error;
  1999. len = rdata->state.len;
  2000. } else {
  2001. memset(packet, 0, sizeof(*packet));
  2002. skb = NULL;
  2003. error = 0;
  2004. len = 0;
  2005. }
  2006. read_again:
  2007. rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
  2008. if (xgbe_rx_dirty_desc(ring) > (XGBE_RX_DESC_CNT >> 3))
  2009. xgbe_rx_refresh(channel);
  2010. if (hw_if->dev_read(channel))
  2011. break;
  2012. received++;
  2013. ring->cur++;
  2014. last = XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
  2015. LAST);
  2016. context_next = XGMAC_GET_BITS(packet->attributes,
  2017. RX_PACKET_ATTRIBUTES,
  2018. CONTEXT_NEXT);
  2019. context = XGMAC_GET_BITS(packet->attributes,
  2020. RX_PACKET_ATTRIBUTES,
  2021. CONTEXT);
  2022. /* Earlier error, just drain the remaining data */
  2023. if ((!last || context_next) && error)
  2024. goto read_again;
  2025. if (error || packet->errors) {
  2026. if (packet->errors)
  2027. netif_err(pdata, rx_err, netdev,
  2028. "error in received packet\n");
  2029. dev_kfree_skb(skb);
  2030. goto next_packet;
  2031. }
  2032. if (!context) {
  2033. /* Get the data length in the descriptor buffers */
  2034. buf1_len = xgbe_rx_buf1_len(rdata, packet);
  2035. len += buf1_len;
  2036. buf2_len = xgbe_rx_buf2_len(rdata, packet, len);
  2037. len += buf2_len;
  2038. if (buf2_len > rdata->rx.buf.dma_len) {
  2039. /* Hardware inconsistency within the descriptors
  2040. * that has resulted in a length underflow.
  2041. */
  2042. error = 1;
  2043. goto skip_data;
  2044. }
  2045. if (!skb) {
  2046. skb = xgbe_create_skb(pdata, napi, rdata,
  2047. buf1_len);
  2048. if (!skb) {
  2049. error = 1;
  2050. goto skip_data;
  2051. }
  2052. }
  2053. if (buf2_len) {
  2054. dma_sync_single_range_for_cpu(pdata->dev,
  2055. rdata->rx.buf.dma_base,
  2056. rdata->rx.buf.dma_off,
  2057. rdata->rx.buf.dma_len,
  2058. DMA_FROM_DEVICE);
  2059. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
  2060. rdata->rx.buf.pa.pages,
  2061. rdata->rx.buf.pa.pages_offset,
  2062. buf2_len,
  2063. rdata->rx.buf.dma_len);
  2064. rdata->rx.buf.pa.pages = NULL;
  2065. }
  2066. }
  2067. skip_data:
  2068. if (!last || context_next)
  2069. goto read_again;
  2070. if (!skb || error) {
  2071. dev_kfree_skb(skb);
  2072. goto next_packet;
  2073. }
  2074. /* Be sure we don't exceed the configured MTU */
  2075. max_len = netdev->mtu + ETH_HLEN;
  2076. if (!(netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
  2077. (skb->protocol == htons(ETH_P_8021Q)))
  2078. max_len += VLAN_HLEN;
  2079. if (skb->len > max_len) {
  2080. netif_err(pdata, rx_err, netdev,
  2081. "packet length exceeds configured MTU\n");
  2082. dev_kfree_skb(skb);
  2083. goto next_packet;
  2084. }
  2085. if (netif_msg_pktdata(pdata))
  2086. xgbe_print_pkt(netdev, skb, false);
  2087. skb_checksum_none_assert(skb);
  2088. if (XGMAC_GET_BITS(packet->attributes,
  2089. RX_PACKET_ATTRIBUTES, CSUM_DONE))
  2090. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2091. if (XGMAC_GET_BITS(packet->attributes,
  2092. RX_PACKET_ATTRIBUTES, TNP)) {
  2093. skb->encapsulation = 1;
  2094. if (XGMAC_GET_BITS(packet->attributes,
  2095. RX_PACKET_ATTRIBUTES, TNPCSUM_DONE))
  2096. skb->csum_level = 1;
  2097. }
  2098. if (XGMAC_GET_BITS(packet->attributes,
  2099. RX_PACKET_ATTRIBUTES, VLAN_CTAG))
  2100. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  2101. packet->vlan_ctag);
  2102. if (XGMAC_GET_BITS(packet->attributes,
  2103. RX_PACKET_ATTRIBUTES, RX_TSTAMP)) {
  2104. u64 nsec;
  2105. nsec = timecounter_cyc2time(&pdata->tstamp_tc,
  2106. packet->rx_tstamp);
  2107. hwtstamps = skb_hwtstamps(skb);
  2108. hwtstamps->hwtstamp = ns_to_ktime(nsec);
  2109. }
  2110. if (XGMAC_GET_BITS(packet->attributes,
  2111. RX_PACKET_ATTRIBUTES, RSS_HASH))
  2112. skb_set_hash(skb, packet->rss_hash,
  2113. packet->rss_hash_type);
  2114. skb->dev = netdev;
  2115. skb->protocol = eth_type_trans(skb, netdev);
  2116. skb_record_rx_queue(skb, channel->queue_index);
  2117. napi_gro_receive(napi, skb);
  2118. next_packet:
  2119. packet_count++;
  2120. }
  2121. /* Check if we need to save state before leaving */
  2122. if (received && (!last || context_next)) {
  2123. rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
  2124. rdata->state_saved = 1;
  2125. rdata->state.skb = skb;
  2126. rdata->state.len = len;
  2127. rdata->state.error = error;
  2128. }
  2129. DBGPR("<--xgbe_rx_poll: packet_count = %d\n", packet_count);
  2130. return packet_count;
  2131. }
  2132. static int xgbe_one_poll(struct napi_struct *napi, int budget)
  2133. {
  2134. struct xgbe_channel *channel = container_of(napi, struct xgbe_channel,
  2135. napi);
  2136. struct xgbe_prv_data *pdata = channel->pdata;
  2137. int processed = 0;
  2138. DBGPR("-->xgbe_one_poll: budget=%d\n", budget);
  2139. /* Cleanup Tx ring first */
  2140. xgbe_tx_poll(channel);
  2141. /* Process Rx ring next */
  2142. processed = xgbe_rx_poll(channel, budget);
  2143. /* If we processed everything, we are done */
  2144. if ((processed < budget) && napi_complete_done(napi, processed)) {
  2145. /* Enable Tx and Rx interrupts */
  2146. if (pdata->channel_irq_mode)
  2147. xgbe_enable_rx_tx_int(pdata, channel);
  2148. else
  2149. enable_irq(channel->dma_irq);
  2150. }
  2151. DBGPR("<--xgbe_one_poll: received = %d\n", processed);
  2152. return processed;
  2153. }
  2154. static int xgbe_all_poll(struct napi_struct *napi, int budget)
  2155. {
  2156. struct xgbe_prv_data *pdata = container_of(napi, struct xgbe_prv_data,
  2157. napi);
  2158. struct xgbe_channel *channel;
  2159. int ring_budget;
  2160. int processed, last_processed;
  2161. unsigned int i;
  2162. DBGPR("-->xgbe_all_poll: budget=%d\n", budget);
  2163. processed = 0;
  2164. ring_budget = budget / pdata->rx_ring_count;
  2165. do {
  2166. last_processed = processed;
  2167. for (i = 0; i < pdata->channel_count; i++) {
  2168. channel = pdata->channel[i];
  2169. /* Cleanup Tx ring first */
  2170. xgbe_tx_poll(channel);
  2171. /* Process Rx ring next */
  2172. if (ring_budget > (budget - processed))
  2173. ring_budget = budget - processed;
  2174. processed += xgbe_rx_poll(channel, ring_budget);
  2175. }
  2176. } while ((processed < budget) && (processed != last_processed));
  2177. /* If we processed everything, we are done */
  2178. if ((processed < budget) && napi_complete_done(napi, processed)) {
  2179. /* Enable Tx and Rx interrupts */
  2180. xgbe_enable_rx_tx_ints(pdata);
  2181. }
  2182. DBGPR("<--xgbe_all_poll: received = %d\n", processed);
  2183. return processed;
  2184. }
  2185. void xgbe_dump_tx_desc(struct xgbe_prv_data *pdata, struct xgbe_ring *ring,
  2186. unsigned int idx, unsigned int count, unsigned int flag)
  2187. {
  2188. struct xgbe_ring_data *rdata;
  2189. struct xgbe_ring_desc *rdesc;
  2190. while (count--) {
  2191. rdata = XGBE_GET_DESC_DATA(ring, idx);
  2192. rdesc = rdata->rdesc;
  2193. netdev_dbg(pdata->netdev,
  2194. "TX_NORMAL_DESC[%d %s] = %08x:%08x:%08x:%08x\n", idx,
  2195. (flag == 1) ? "QUEUED FOR TX" : "TX BY DEVICE",
  2196. le32_to_cpu(rdesc->desc0),
  2197. le32_to_cpu(rdesc->desc1),
  2198. le32_to_cpu(rdesc->desc2),
  2199. le32_to_cpu(rdesc->desc3));
  2200. idx++;
  2201. }
  2202. }
  2203. void xgbe_dump_rx_desc(struct xgbe_prv_data *pdata, struct xgbe_ring *ring,
  2204. unsigned int idx)
  2205. {
  2206. struct xgbe_ring_data *rdata;
  2207. struct xgbe_ring_desc *rdesc;
  2208. rdata = XGBE_GET_DESC_DATA(ring, idx);
  2209. rdesc = rdata->rdesc;
  2210. netdev_dbg(pdata->netdev,
  2211. "RX_NORMAL_DESC[%d RX BY DEVICE] = %08x:%08x:%08x:%08x\n",
  2212. idx, le32_to_cpu(rdesc->desc0), le32_to_cpu(rdesc->desc1),
  2213. le32_to_cpu(rdesc->desc2), le32_to_cpu(rdesc->desc3));
  2214. }
  2215. void xgbe_print_pkt(struct net_device *netdev, struct sk_buff *skb, bool tx_rx)
  2216. {
  2217. struct ethhdr *eth = (struct ethhdr *)skb->data;
  2218. unsigned char buffer[128];
  2219. unsigned int i;
  2220. netdev_dbg(netdev, "\n************** SKB dump ****************\n");
  2221. netdev_dbg(netdev, "%s packet of %d bytes\n",
  2222. (tx_rx ? "TX" : "RX"), skb->len);
  2223. netdev_dbg(netdev, "Dst MAC addr: %pM\n", eth->h_dest);
  2224. netdev_dbg(netdev, "Src MAC addr: %pM\n", eth->h_source);
  2225. netdev_dbg(netdev, "Protocol: %#06x\n", ntohs(eth->h_proto));
  2226. for (i = 0; i < skb->len; i += 32) {
  2227. unsigned int len = min(skb->len - i, 32U);
  2228. hex_dump_to_buffer(&skb->data[i], len, 32, 1,
  2229. buffer, sizeof(buffer), false);
  2230. netdev_dbg(netdev, " %#06x: %s\n", i, buffer);
  2231. }
  2232. netdev_dbg(netdev, "\n************** SKB dump ****************\n");
  2233. }