xgbe-drv.c 79 KB

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