xgbe-drv.c 79 KB

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