xgbe-drv.c 78 KB

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