xgbe-drv.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401
  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 <net/busy_poll.h>
  121. #include <linux/clk.h>
  122. #include <linux/if_ether.h>
  123. #include <linux/net_tstamp.h>
  124. #include <linux/phy.h>
  125. #include "xgbe.h"
  126. #include "xgbe-common.h"
  127. static unsigned int ecc_sec_info_threshold = 10;
  128. static unsigned int ecc_sec_warn_threshold = 10000;
  129. static unsigned int ecc_sec_period = 600;
  130. static unsigned int ecc_ded_threshold = 2;
  131. static unsigned int ecc_ded_period = 600;
  132. #ifdef CONFIG_AMD_XGBE_HAVE_ECC
  133. /* Only expose the ECC parameters if supported */
  134. module_param(ecc_sec_info_threshold, uint, S_IWUSR | S_IRUGO);
  135. MODULE_PARM_DESC(ecc_sec_info_threshold,
  136. " ECC corrected error informational threshold setting");
  137. module_param(ecc_sec_warn_threshold, uint, S_IWUSR | S_IRUGO);
  138. MODULE_PARM_DESC(ecc_sec_warn_threshold,
  139. " ECC corrected error warning threshold setting");
  140. module_param(ecc_sec_period, uint, S_IWUSR | S_IRUGO);
  141. MODULE_PARM_DESC(ecc_sec_period, " ECC corrected error period (in seconds)");
  142. module_param(ecc_ded_threshold, uint, S_IWUSR | S_IRUGO);
  143. MODULE_PARM_DESC(ecc_ded_threshold, " ECC detected error threshold setting");
  144. module_param(ecc_ded_period, uint, S_IWUSR | S_IRUGO);
  145. MODULE_PARM_DESC(ecc_ded_period, " ECC detected error period (in seconds)");
  146. #endif
  147. static int xgbe_one_poll(struct napi_struct *, int);
  148. static int xgbe_all_poll(struct napi_struct *, int);
  149. static void xgbe_stop(struct xgbe_prv_data *);
  150. static int xgbe_alloc_channels(struct xgbe_prv_data *pdata)
  151. {
  152. struct xgbe_channel *channel_mem, *channel;
  153. struct xgbe_ring *tx_ring, *rx_ring;
  154. unsigned int count, i;
  155. int ret = -ENOMEM;
  156. count = max_t(unsigned int, pdata->tx_ring_count, pdata->rx_ring_count);
  157. channel_mem = kcalloc(count, sizeof(struct xgbe_channel), GFP_KERNEL);
  158. if (!channel_mem)
  159. goto err_channel;
  160. tx_ring = kcalloc(pdata->tx_ring_count, sizeof(struct xgbe_ring),
  161. GFP_KERNEL);
  162. if (!tx_ring)
  163. goto err_tx_ring;
  164. rx_ring = kcalloc(pdata->rx_ring_count, sizeof(struct xgbe_ring),
  165. GFP_KERNEL);
  166. if (!rx_ring)
  167. goto err_rx_ring;
  168. for (i = 0, channel = channel_mem; i < count; i++, channel++) {
  169. snprintf(channel->name, sizeof(channel->name), "channel-%u", i);
  170. channel->pdata = pdata;
  171. channel->queue_index = i;
  172. channel->dma_regs = pdata->xgmac_regs + DMA_CH_BASE +
  173. (DMA_CH_INC * i);
  174. if (pdata->per_channel_irq)
  175. channel->dma_irq = pdata->channel_irq[i];
  176. if (i < pdata->tx_ring_count) {
  177. spin_lock_init(&tx_ring->lock);
  178. channel->tx_ring = tx_ring++;
  179. }
  180. if (i < pdata->rx_ring_count) {
  181. spin_lock_init(&rx_ring->lock);
  182. channel->rx_ring = rx_ring++;
  183. }
  184. netif_dbg(pdata, drv, pdata->netdev,
  185. "%s: dma_regs=%p, dma_irq=%d, tx=%p, rx=%p\n",
  186. channel->name, channel->dma_regs, channel->dma_irq,
  187. channel->tx_ring, channel->rx_ring);
  188. }
  189. pdata->channel = channel_mem;
  190. pdata->channel_count = count;
  191. return 0;
  192. err_rx_ring:
  193. kfree(tx_ring);
  194. err_tx_ring:
  195. kfree(channel_mem);
  196. err_channel:
  197. return ret;
  198. }
  199. static void xgbe_free_channels(struct xgbe_prv_data *pdata)
  200. {
  201. if (!pdata->channel)
  202. return;
  203. kfree(pdata->channel->rx_ring);
  204. kfree(pdata->channel->tx_ring);
  205. kfree(pdata->channel);
  206. pdata->channel = NULL;
  207. pdata->channel_count = 0;
  208. }
  209. static inline unsigned int xgbe_tx_avail_desc(struct xgbe_ring *ring)
  210. {
  211. return (ring->rdesc_count - (ring->cur - ring->dirty));
  212. }
  213. static inline unsigned int xgbe_rx_dirty_desc(struct xgbe_ring *ring)
  214. {
  215. return (ring->cur - ring->dirty);
  216. }
  217. static int xgbe_maybe_stop_tx_queue(struct xgbe_channel *channel,
  218. struct xgbe_ring *ring, unsigned int count)
  219. {
  220. struct xgbe_prv_data *pdata = channel->pdata;
  221. if (count > xgbe_tx_avail_desc(ring)) {
  222. netif_info(pdata, drv, pdata->netdev,
  223. "Tx queue stopped, not enough descriptors available\n");
  224. netif_stop_subqueue(pdata->netdev, channel->queue_index);
  225. ring->tx.queue_stopped = 1;
  226. /* If we haven't notified the hardware because of xmit_more
  227. * support, tell it now
  228. */
  229. if (ring->tx.xmit_more)
  230. pdata->hw_if.tx_start_xmit(channel, ring);
  231. return NETDEV_TX_BUSY;
  232. }
  233. return 0;
  234. }
  235. static int xgbe_calc_rx_buf_size(struct net_device *netdev, unsigned int mtu)
  236. {
  237. unsigned int rx_buf_size;
  238. rx_buf_size = mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  239. rx_buf_size = clamp_val(rx_buf_size, XGBE_RX_MIN_BUF_SIZE, PAGE_SIZE);
  240. rx_buf_size = (rx_buf_size + XGBE_RX_BUF_ALIGN - 1) &
  241. ~(XGBE_RX_BUF_ALIGN - 1);
  242. return rx_buf_size;
  243. }
  244. static void xgbe_enable_rx_tx_int(struct xgbe_prv_data *pdata,
  245. struct xgbe_channel *channel)
  246. {
  247. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  248. enum xgbe_int int_id;
  249. if (channel->tx_ring && channel->rx_ring)
  250. int_id = XGMAC_INT_DMA_CH_SR_TI_RI;
  251. else if (channel->tx_ring)
  252. int_id = XGMAC_INT_DMA_CH_SR_TI;
  253. else if (channel->rx_ring)
  254. int_id = XGMAC_INT_DMA_CH_SR_RI;
  255. else
  256. return;
  257. hw_if->enable_int(channel, int_id);
  258. }
  259. static void xgbe_enable_rx_tx_ints(struct xgbe_prv_data *pdata)
  260. {
  261. struct xgbe_channel *channel;
  262. unsigned int i;
  263. channel = pdata->channel;
  264. for (i = 0; i < pdata->channel_count; i++, channel++)
  265. xgbe_enable_rx_tx_int(pdata, channel);
  266. }
  267. static void xgbe_disable_rx_tx_int(struct xgbe_prv_data *pdata,
  268. struct xgbe_channel *channel)
  269. {
  270. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  271. enum xgbe_int int_id;
  272. if (channel->tx_ring && channel->rx_ring)
  273. int_id = XGMAC_INT_DMA_CH_SR_TI_RI;
  274. else if (channel->tx_ring)
  275. int_id = XGMAC_INT_DMA_CH_SR_TI;
  276. else if (channel->rx_ring)
  277. int_id = XGMAC_INT_DMA_CH_SR_RI;
  278. else
  279. return;
  280. hw_if->disable_int(channel, int_id);
  281. }
  282. static void xgbe_disable_rx_tx_ints(struct xgbe_prv_data *pdata)
  283. {
  284. struct xgbe_channel *channel;
  285. unsigned int i;
  286. channel = pdata->channel;
  287. for (i = 0; i < pdata->channel_count; i++, channel++)
  288. xgbe_disable_rx_tx_int(pdata, channel);
  289. }
  290. static bool xgbe_ecc_sec(struct xgbe_prv_data *pdata, unsigned long *period,
  291. unsigned int *count, const char *area)
  292. {
  293. if (time_before(jiffies, *period)) {
  294. (*count)++;
  295. } else {
  296. *period = jiffies + (ecc_sec_period * HZ);
  297. *count = 1;
  298. }
  299. if (*count > ecc_sec_info_threshold)
  300. dev_warn_once(pdata->dev,
  301. "%s ECC corrected errors exceed informational threshold\n",
  302. area);
  303. if (*count > ecc_sec_warn_threshold) {
  304. dev_warn_once(pdata->dev,
  305. "%s ECC corrected errors exceed warning threshold\n",
  306. area);
  307. return true;
  308. }
  309. return false;
  310. }
  311. static bool xgbe_ecc_ded(struct xgbe_prv_data *pdata, unsigned long *period,
  312. unsigned int *count, const char *area)
  313. {
  314. if (time_before(jiffies, *period)) {
  315. (*count)++;
  316. } else {
  317. *period = jiffies + (ecc_ded_period * HZ);
  318. *count = 1;
  319. }
  320. if (*count > ecc_ded_threshold) {
  321. netdev_alert(pdata->netdev,
  322. "%s ECC detected errors exceed threshold\n",
  323. area);
  324. return true;
  325. }
  326. return false;
  327. }
  328. static irqreturn_t xgbe_ecc_isr(int irq, void *data)
  329. {
  330. struct xgbe_prv_data *pdata = data;
  331. unsigned int ecc_isr;
  332. bool stop = false;
  333. /* Mask status with only the interrupts we care about */
  334. ecc_isr = XP_IOREAD(pdata, XP_ECC_ISR);
  335. ecc_isr &= XP_IOREAD(pdata, XP_ECC_IER);
  336. netif_dbg(pdata, intr, pdata->netdev, "ECC_ISR=%#010x\n", ecc_isr);
  337. if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, TX_DED)) {
  338. stop |= xgbe_ecc_ded(pdata, &pdata->tx_ded_period,
  339. &pdata->tx_ded_count, "TX fifo");
  340. }
  341. if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, RX_DED)) {
  342. stop |= xgbe_ecc_ded(pdata, &pdata->rx_ded_period,
  343. &pdata->rx_ded_count, "RX fifo");
  344. }
  345. if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, DESC_DED)) {
  346. stop |= xgbe_ecc_ded(pdata, &pdata->desc_ded_period,
  347. &pdata->desc_ded_count,
  348. "descriptor cache");
  349. }
  350. if (stop) {
  351. pdata->hw_if.disable_ecc_ded(pdata);
  352. schedule_work(&pdata->stopdev_work);
  353. goto out;
  354. }
  355. if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, TX_SEC)) {
  356. if (xgbe_ecc_sec(pdata, &pdata->tx_sec_period,
  357. &pdata->tx_sec_count, "TX fifo"))
  358. pdata->hw_if.disable_ecc_sec(pdata, XGBE_ECC_SEC_TX);
  359. }
  360. if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, RX_SEC))
  361. if (xgbe_ecc_sec(pdata, &pdata->rx_sec_period,
  362. &pdata->rx_sec_count, "RX fifo"))
  363. pdata->hw_if.disable_ecc_sec(pdata, XGBE_ECC_SEC_RX);
  364. if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, DESC_SEC))
  365. if (xgbe_ecc_sec(pdata, &pdata->desc_sec_period,
  366. &pdata->desc_sec_count, "descriptor cache"))
  367. pdata->hw_if.disable_ecc_sec(pdata, XGBE_ECC_SEC_DESC);
  368. out:
  369. /* Clear all ECC interrupts */
  370. XP_IOWRITE(pdata, XP_ECC_ISR, ecc_isr);
  371. return IRQ_HANDLED;
  372. }
  373. static irqreturn_t xgbe_isr(int irq, void *data)
  374. {
  375. struct xgbe_prv_data *pdata = data;
  376. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  377. struct xgbe_channel *channel;
  378. unsigned int dma_isr, dma_ch_isr;
  379. unsigned int mac_isr, mac_tssr, mac_mdioisr;
  380. unsigned int i;
  381. /* The DMA interrupt status register also reports MAC and MTL
  382. * interrupts. So for polling mode, we just need to check for
  383. * this register to be non-zero
  384. */
  385. dma_isr = XGMAC_IOREAD(pdata, DMA_ISR);
  386. if (!dma_isr)
  387. goto isr_done;
  388. netif_dbg(pdata, intr, pdata->netdev, "DMA_ISR=%#010x\n", dma_isr);
  389. for (i = 0; i < pdata->channel_count; i++) {
  390. if (!(dma_isr & (1 << i)))
  391. continue;
  392. channel = pdata->channel + i;
  393. dma_ch_isr = XGMAC_DMA_IOREAD(channel, DMA_CH_SR);
  394. netif_dbg(pdata, intr, pdata->netdev, "DMA_CH%u_ISR=%#010x\n",
  395. i, dma_ch_isr);
  396. /* The TI or RI interrupt bits may still be set even if using
  397. * per channel DMA interrupts. Check to be sure those are not
  398. * enabled before using the private data napi structure.
  399. */
  400. if (!pdata->per_channel_irq &&
  401. (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, TI) ||
  402. XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, RI))) {
  403. if (napi_schedule_prep(&pdata->napi)) {
  404. /* Disable Tx and Rx interrupts */
  405. xgbe_disable_rx_tx_ints(pdata);
  406. /* Turn on polling */
  407. __napi_schedule_irqoff(&pdata->napi);
  408. }
  409. } else {
  410. /* Don't clear Rx/Tx status if doing per channel DMA
  411. * interrupts, these will be cleared by the ISR for
  412. * per channel DMA interrupts.
  413. */
  414. XGMAC_SET_BITS(dma_ch_isr, DMA_CH_SR, TI, 0);
  415. XGMAC_SET_BITS(dma_ch_isr, DMA_CH_SR, RI, 0);
  416. }
  417. if (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, RBU))
  418. pdata->ext_stats.rx_buffer_unavailable++;
  419. /* Restart the device on a Fatal Bus Error */
  420. if (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, FBE))
  421. schedule_work(&pdata->restart_work);
  422. /* Clear interrupt signals */
  423. XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_ch_isr);
  424. }
  425. if (XGMAC_GET_BITS(dma_isr, DMA_ISR, MACIS)) {
  426. mac_isr = XGMAC_IOREAD(pdata, MAC_ISR);
  427. netif_dbg(pdata, intr, pdata->netdev, "MAC_ISR=%#010x\n",
  428. mac_isr);
  429. if (XGMAC_GET_BITS(mac_isr, MAC_ISR, MMCTXIS))
  430. hw_if->tx_mmc_int(pdata);
  431. if (XGMAC_GET_BITS(mac_isr, MAC_ISR, MMCRXIS))
  432. hw_if->rx_mmc_int(pdata);
  433. if (XGMAC_GET_BITS(mac_isr, MAC_ISR, TSIS)) {
  434. mac_tssr = XGMAC_IOREAD(pdata, MAC_TSSR);
  435. netif_dbg(pdata, intr, pdata->netdev,
  436. "MAC_TSSR=%#010x\n", mac_tssr);
  437. if (XGMAC_GET_BITS(mac_tssr, MAC_TSSR, TXTSC)) {
  438. /* Read Tx Timestamp to clear interrupt */
  439. pdata->tx_tstamp =
  440. hw_if->get_tx_tstamp(pdata);
  441. queue_work(pdata->dev_workqueue,
  442. &pdata->tx_tstamp_work);
  443. }
  444. }
  445. if (XGMAC_GET_BITS(mac_isr, MAC_ISR, SMI)) {
  446. mac_mdioisr = XGMAC_IOREAD(pdata, MAC_MDIOISR);
  447. netif_dbg(pdata, intr, pdata->netdev,
  448. "MAC_MDIOISR=%#010x\n", mac_mdioisr);
  449. if (XGMAC_GET_BITS(mac_mdioisr, MAC_MDIOISR,
  450. SNGLCOMPINT))
  451. complete(&pdata->mdio_complete);
  452. }
  453. }
  454. isr_done:
  455. /* If there is not a separate AN irq, handle it here */
  456. if (pdata->dev_irq == pdata->an_irq)
  457. pdata->phy_if.an_isr(irq, pdata);
  458. /* If there is not a separate ECC irq, handle it here */
  459. if (pdata->vdata->ecc_support && (pdata->dev_irq == pdata->ecc_irq))
  460. xgbe_ecc_isr(irq, pdata);
  461. /* If there is not a separate I2C irq, handle it here */
  462. if (pdata->vdata->i2c_support && (pdata->dev_irq == pdata->i2c_irq))
  463. pdata->i2c_if.i2c_isr(irq, pdata);
  464. return IRQ_HANDLED;
  465. }
  466. static irqreturn_t xgbe_dma_isr(int irq, void *data)
  467. {
  468. struct xgbe_channel *channel = data;
  469. struct xgbe_prv_data *pdata = channel->pdata;
  470. unsigned int dma_status;
  471. /* Per channel DMA interrupts are enabled, so we use the per
  472. * channel napi structure and not the private data napi structure
  473. */
  474. if (napi_schedule_prep(&channel->napi)) {
  475. /* Disable Tx and Rx interrupts */
  476. if (pdata->channel_irq_mode)
  477. xgbe_disable_rx_tx_int(pdata, channel);
  478. else
  479. disable_irq_nosync(channel->dma_irq);
  480. /* Turn on polling */
  481. __napi_schedule_irqoff(&channel->napi);
  482. }
  483. /* Clear Tx/Rx signals */
  484. dma_status = 0;
  485. XGMAC_SET_BITS(dma_status, DMA_CH_SR, TI, 1);
  486. XGMAC_SET_BITS(dma_status, DMA_CH_SR, RI, 1);
  487. XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_status);
  488. return IRQ_HANDLED;
  489. }
  490. static void xgbe_tx_timer(unsigned long data)
  491. {
  492. struct xgbe_channel *channel = (struct xgbe_channel *)data;
  493. struct xgbe_prv_data *pdata = channel->pdata;
  494. struct napi_struct *napi;
  495. DBGPR("-->xgbe_tx_timer\n");
  496. napi = (pdata->per_channel_irq) ? &channel->napi : &pdata->napi;
  497. if (napi_schedule_prep(napi)) {
  498. /* Disable Tx and Rx interrupts */
  499. if (pdata->per_channel_irq)
  500. if (pdata->channel_irq_mode)
  501. xgbe_disable_rx_tx_int(pdata, channel);
  502. else
  503. disable_irq_nosync(channel->dma_irq);
  504. else
  505. xgbe_disable_rx_tx_ints(pdata);
  506. /* Turn on polling */
  507. __napi_schedule(napi);
  508. }
  509. channel->tx_timer_active = 0;
  510. DBGPR("<--xgbe_tx_timer\n");
  511. }
  512. static void xgbe_service(struct work_struct *work)
  513. {
  514. struct xgbe_prv_data *pdata = container_of(work,
  515. struct xgbe_prv_data,
  516. service_work);
  517. pdata->phy_if.phy_status(pdata);
  518. }
  519. static void xgbe_service_timer(unsigned long data)
  520. {
  521. struct xgbe_prv_data *pdata = (struct xgbe_prv_data *)data;
  522. queue_work(pdata->dev_workqueue, &pdata->service_work);
  523. mod_timer(&pdata->service_timer, jiffies + HZ);
  524. }
  525. static void xgbe_init_timers(struct xgbe_prv_data *pdata)
  526. {
  527. struct xgbe_channel *channel;
  528. unsigned int i;
  529. setup_timer(&pdata->service_timer, xgbe_service_timer,
  530. (unsigned long)pdata);
  531. channel = pdata->channel;
  532. for (i = 0; i < pdata->channel_count; i++, channel++) {
  533. if (!channel->tx_ring)
  534. break;
  535. setup_timer(&channel->tx_timer, xgbe_tx_timer,
  536. (unsigned long)channel);
  537. }
  538. }
  539. static void xgbe_start_timers(struct xgbe_prv_data *pdata)
  540. {
  541. mod_timer(&pdata->service_timer, jiffies + HZ);
  542. }
  543. static void xgbe_stop_timers(struct xgbe_prv_data *pdata)
  544. {
  545. struct xgbe_channel *channel;
  546. unsigned int i;
  547. del_timer_sync(&pdata->service_timer);
  548. channel = pdata->channel;
  549. for (i = 0; i < pdata->channel_count; i++, channel++) {
  550. if (!channel->tx_ring)
  551. break;
  552. del_timer_sync(&channel->tx_timer);
  553. }
  554. }
  555. void xgbe_get_all_hw_features(struct xgbe_prv_data *pdata)
  556. {
  557. unsigned int mac_hfr0, mac_hfr1, mac_hfr2;
  558. struct xgbe_hw_features *hw_feat = &pdata->hw_feat;
  559. DBGPR("-->xgbe_get_all_hw_features\n");
  560. mac_hfr0 = XGMAC_IOREAD(pdata, MAC_HWF0R);
  561. mac_hfr1 = XGMAC_IOREAD(pdata, MAC_HWF1R);
  562. mac_hfr2 = XGMAC_IOREAD(pdata, MAC_HWF2R);
  563. memset(hw_feat, 0, sizeof(*hw_feat));
  564. hw_feat->version = XGMAC_IOREAD(pdata, MAC_VR);
  565. /* Hardware feature register 0 */
  566. hw_feat->gmii = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, GMIISEL);
  567. hw_feat->vlhash = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, VLHASH);
  568. hw_feat->sma = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SMASEL);
  569. hw_feat->rwk = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RWKSEL);
  570. hw_feat->mgk = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MGKSEL);
  571. hw_feat->mmc = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MMCSEL);
  572. hw_feat->aoe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, ARPOFFSEL);
  573. hw_feat->ts = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSEL);
  574. hw_feat->eee = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, EEESEL);
  575. hw_feat->tx_coe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TXCOESEL);
  576. hw_feat->rx_coe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RXCOESEL);
  577. hw_feat->addn_mac = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R,
  578. ADDMACADRSEL);
  579. hw_feat->ts_src = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSTSSEL);
  580. hw_feat->sa_vlan_ins = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SAVLANINS);
  581. /* Hardware feature register 1 */
  582. hw_feat->rx_fifo_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
  583. RXFIFOSIZE);
  584. hw_feat->tx_fifo_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
  585. TXFIFOSIZE);
  586. hw_feat->adv_ts_hi = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, ADVTHWORD);
  587. hw_feat->dma_width = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, ADDR64);
  588. hw_feat->dcb = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DCBEN);
  589. hw_feat->sph = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, SPHEN);
  590. hw_feat->tso = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, TSOEN);
  591. hw_feat->dma_debug = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DBGMEMA);
  592. hw_feat->rss = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, RSSEN);
  593. hw_feat->tc_cnt = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, NUMTC);
  594. hw_feat->hash_table_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
  595. HASHTBLSZ);
  596. hw_feat->l3l4_filter_num = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
  597. L3L4FNUM);
  598. /* Hardware feature register 2 */
  599. hw_feat->rx_q_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXQCNT);
  600. hw_feat->tx_q_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXQCNT);
  601. hw_feat->rx_ch_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXCHCNT);
  602. hw_feat->tx_ch_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXCHCNT);
  603. hw_feat->pps_out_num = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, PPSOUTNUM);
  604. hw_feat->aux_snap_num = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, AUXSNAPNUM);
  605. /* Translate the Hash Table size into actual number */
  606. switch (hw_feat->hash_table_size) {
  607. case 0:
  608. break;
  609. case 1:
  610. hw_feat->hash_table_size = 64;
  611. break;
  612. case 2:
  613. hw_feat->hash_table_size = 128;
  614. break;
  615. case 3:
  616. hw_feat->hash_table_size = 256;
  617. break;
  618. }
  619. /* Translate the address width setting into actual number */
  620. switch (hw_feat->dma_width) {
  621. case 0:
  622. hw_feat->dma_width = 32;
  623. break;
  624. case 1:
  625. hw_feat->dma_width = 40;
  626. break;
  627. case 2:
  628. hw_feat->dma_width = 48;
  629. break;
  630. default:
  631. hw_feat->dma_width = 32;
  632. }
  633. /* The Queue, Channel and TC counts are zero based so increment them
  634. * to get the actual number
  635. */
  636. hw_feat->rx_q_cnt++;
  637. hw_feat->tx_q_cnt++;
  638. hw_feat->rx_ch_cnt++;
  639. hw_feat->tx_ch_cnt++;
  640. hw_feat->tc_cnt++;
  641. /* Translate the fifo sizes into actual numbers */
  642. hw_feat->rx_fifo_size = 1 << (hw_feat->rx_fifo_size + 7);
  643. hw_feat->tx_fifo_size = 1 << (hw_feat->tx_fifo_size + 7);
  644. DBGPR("<--xgbe_get_all_hw_features\n");
  645. }
  646. static void xgbe_napi_enable(struct xgbe_prv_data *pdata, unsigned int add)
  647. {
  648. struct xgbe_channel *channel;
  649. unsigned int i;
  650. if (pdata->per_channel_irq) {
  651. channel = pdata->channel;
  652. for (i = 0; i < pdata->channel_count; i++, channel++) {
  653. if (add)
  654. netif_napi_add(pdata->netdev, &channel->napi,
  655. xgbe_one_poll, NAPI_POLL_WEIGHT);
  656. napi_enable(&channel->napi);
  657. }
  658. } else {
  659. if (add)
  660. netif_napi_add(pdata->netdev, &pdata->napi,
  661. xgbe_all_poll, NAPI_POLL_WEIGHT);
  662. napi_enable(&pdata->napi);
  663. }
  664. }
  665. static void xgbe_napi_disable(struct xgbe_prv_data *pdata, unsigned int del)
  666. {
  667. struct xgbe_channel *channel;
  668. unsigned int i;
  669. if (pdata->per_channel_irq) {
  670. channel = pdata->channel;
  671. for (i = 0; i < pdata->channel_count; i++, channel++) {
  672. napi_disable(&channel->napi);
  673. if (del)
  674. netif_napi_del(&channel->napi);
  675. }
  676. } else {
  677. napi_disable(&pdata->napi);
  678. if (del)
  679. netif_napi_del(&pdata->napi);
  680. }
  681. }
  682. static int xgbe_request_irqs(struct xgbe_prv_data *pdata)
  683. {
  684. struct xgbe_channel *channel;
  685. struct net_device *netdev = pdata->netdev;
  686. unsigned int i;
  687. int ret;
  688. ret = devm_request_irq(pdata->dev, pdata->dev_irq, xgbe_isr, 0,
  689. netdev->name, pdata);
  690. if (ret) {
  691. netdev_alert(netdev, "error requesting irq %d\n",
  692. pdata->dev_irq);
  693. return ret;
  694. }
  695. if (pdata->vdata->ecc_support && (pdata->dev_irq != pdata->ecc_irq)) {
  696. ret = devm_request_irq(pdata->dev, pdata->ecc_irq, xgbe_ecc_isr,
  697. 0, pdata->ecc_name, pdata);
  698. if (ret) {
  699. netdev_alert(netdev, "error requesting ecc irq %d\n",
  700. pdata->ecc_irq);
  701. goto err_dev_irq;
  702. }
  703. }
  704. if (!pdata->per_channel_irq)
  705. return 0;
  706. channel = pdata->channel;
  707. for (i = 0; i < pdata->channel_count; i++, channel++) {
  708. snprintf(channel->dma_irq_name,
  709. sizeof(channel->dma_irq_name) - 1,
  710. "%s-TxRx-%u", netdev_name(netdev),
  711. channel->queue_index);
  712. ret = devm_request_irq(pdata->dev, channel->dma_irq,
  713. xgbe_dma_isr, 0,
  714. channel->dma_irq_name, channel);
  715. if (ret) {
  716. netdev_alert(netdev, "error requesting irq %d\n",
  717. channel->dma_irq);
  718. goto err_dma_irq;
  719. }
  720. }
  721. return 0;
  722. err_dma_irq:
  723. /* Using an unsigned int, 'i' will go to UINT_MAX and exit */
  724. for (i--, channel--; i < pdata->channel_count; i--, channel--)
  725. devm_free_irq(pdata->dev, channel->dma_irq, channel);
  726. if (pdata->vdata->ecc_support && (pdata->dev_irq != pdata->ecc_irq))
  727. devm_free_irq(pdata->dev, pdata->ecc_irq, pdata);
  728. err_dev_irq:
  729. devm_free_irq(pdata->dev, pdata->dev_irq, pdata);
  730. return ret;
  731. }
  732. static void xgbe_free_irqs(struct xgbe_prv_data *pdata)
  733. {
  734. struct xgbe_channel *channel;
  735. unsigned int i;
  736. devm_free_irq(pdata->dev, pdata->dev_irq, pdata);
  737. if (pdata->vdata->ecc_support && (pdata->dev_irq != pdata->ecc_irq))
  738. devm_free_irq(pdata->dev, pdata->ecc_irq, pdata);
  739. if (!pdata->per_channel_irq)
  740. return;
  741. channel = pdata->channel;
  742. for (i = 0; i < pdata->channel_count; i++, channel++)
  743. devm_free_irq(pdata->dev, channel->dma_irq, channel);
  744. }
  745. void xgbe_init_tx_coalesce(struct xgbe_prv_data *pdata)
  746. {
  747. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  748. DBGPR("-->xgbe_init_tx_coalesce\n");
  749. pdata->tx_usecs = XGMAC_INIT_DMA_TX_USECS;
  750. pdata->tx_frames = XGMAC_INIT_DMA_TX_FRAMES;
  751. hw_if->config_tx_coalesce(pdata);
  752. DBGPR("<--xgbe_init_tx_coalesce\n");
  753. }
  754. void xgbe_init_rx_coalesce(struct xgbe_prv_data *pdata)
  755. {
  756. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  757. DBGPR("-->xgbe_init_rx_coalesce\n");
  758. pdata->rx_riwt = hw_if->usec_to_riwt(pdata, XGMAC_INIT_DMA_RX_USECS);
  759. pdata->rx_usecs = XGMAC_INIT_DMA_RX_USECS;
  760. pdata->rx_frames = XGMAC_INIT_DMA_RX_FRAMES;
  761. hw_if->config_rx_coalesce(pdata);
  762. DBGPR("<--xgbe_init_rx_coalesce\n");
  763. }
  764. static void xgbe_free_tx_data(struct xgbe_prv_data *pdata)
  765. {
  766. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  767. struct xgbe_channel *channel;
  768. struct xgbe_ring *ring;
  769. struct xgbe_ring_data *rdata;
  770. unsigned int i, j;
  771. DBGPR("-->xgbe_free_tx_data\n");
  772. channel = pdata->channel;
  773. for (i = 0; i < pdata->channel_count; i++, channel++) {
  774. ring = channel->tx_ring;
  775. if (!ring)
  776. break;
  777. for (j = 0; j < ring->rdesc_count; j++) {
  778. rdata = XGBE_GET_DESC_DATA(ring, j);
  779. desc_if->unmap_rdata(pdata, rdata);
  780. }
  781. }
  782. DBGPR("<--xgbe_free_tx_data\n");
  783. }
  784. static void xgbe_free_rx_data(struct xgbe_prv_data *pdata)
  785. {
  786. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  787. struct xgbe_channel *channel;
  788. struct xgbe_ring *ring;
  789. struct xgbe_ring_data *rdata;
  790. unsigned int i, j;
  791. DBGPR("-->xgbe_free_rx_data\n");
  792. channel = pdata->channel;
  793. for (i = 0; i < pdata->channel_count; i++, channel++) {
  794. ring = channel->rx_ring;
  795. if (!ring)
  796. break;
  797. for (j = 0; j < ring->rdesc_count; j++) {
  798. rdata = XGBE_GET_DESC_DATA(ring, j);
  799. desc_if->unmap_rdata(pdata, rdata);
  800. }
  801. }
  802. DBGPR("<--xgbe_free_rx_data\n");
  803. }
  804. static int xgbe_phy_reset(struct xgbe_prv_data *pdata)
  805. {
  806. pdata->phy_link = -1;
  807. pdata->phy_speed = SPEED_UNKNOWN;
  808. return pdata->phy_if.phy_reset(pdata);
  809. }
  810. int xgbe_powerdown(struct net_device *netdev, unsigned int caller)
  811. {
  812. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  813. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  814. unsigned long flags;
  815. DBGPR("-->xgbe_powerdown\n");
  816. if (!netif_running(netdev) ||
  817. (caller == XGMAC_IOCTL_CONTEXT && pdata->power_down)) {
  818. netdev_alert(netdev, "Device is already powered down\n");
  819. DBGPR("<--xgbe_powerdown\n");
  820. return -EINVAL;
  821. }
  822. spin_lock_irqsave(&pdata->lock, flags);
  823. if (caller == XGMAC_DRIVER_CONTEXT)
  824. netif_device_detach(netdev);
  825. netif_tx_stop_all_queues(netdev);
  826. xgbe_stop_timers(pdata);
  827. flush_workqueue(pdata->dev_workqueue);
  828. hw_if->powerdown_tx(pdata);
  829. hw_if->powerdown_rx(pdata);
  830. xgbe_napi_disable(pdata, 0);
  831. pdata->power_down = 1;
  832. spin_unlock_irqrestore(&pdata->lock, flags);
  833. DBGPR("<--xgbe_powerdown\n");
  834. return 0;
  835. }
  836. int xgbe_powerup(struct net_device *netdev, unsigned int caller)
  837. {
  838. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  839. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  840. unsigned long flags;
  841. DBGPR("-->xgbe_powerup\n");
  842. if (!netif_running(netdev) ||
  843. (caller == XGMAC_IOCTL_CONTEXT && !pdata->power_down)) {
  844. netdev_alert(netdev, "Device is already powered up\n");
  845. DBGPR("<--xgbe_powerup\n");
  846. return -EINVAL;
  847. }
  848. spin_lock_irqsave(&pdata->lock, flags);
  849. pdata->power_down = 0;
  850. xgbe_napi_enable(pdata, 0);
  851. hw_if->powerup_tx(pdata);
  852. hw_if->powerup_rx(pdata);
  853. if (caller == XGMAC_DRIVER_CONTEXT)
  854. netif_device_attach(netdev);
  855. netif_tx_start_all_queues(netdev);
  856. xgbe_start_timers(pdata);
  857. spin_unlock_irqrestore(&pdata->lock, flags);
  858. DBGPR("<--xgbe_powerup\n");
  859. return 0;
  860. }
  861. static int xgbe_start(struct xgbe_prv_data *pdata)
  862. {
  863. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  864. struct xgbe_phy_if *phy_if = &pdata->phy_if;
  865. struct net_device *netdev = pdata->netdev;
  866. int ret;
  867. DBGPR("-->xgbe_start\n");
  868. hw_if->init(pdata);
  869. xgbe_napi_enable(pdata, 1);
  870. ret = xgbe_request_irqs(pdata);
  871. if (ret)
  872. goto err_napi;
  873. ret = phy_if->phy_start(pdata);
  874. if (ret)
  875. goto err_irqs;
  876. hw_if->enable_tx(pdata);
  877. hw_if->enable_rx(pdata);
  878. netif_tx_start_all_queues(netdev);
  879. xgbe_start_timers(pdata);
  880. queue_work(pdata->dev_workqueue, &pdata->service_work);
  881. clear_bit(XGBE_STOPPED, &pdata->dev_state);
  882. DBGPR("<--xgbe_start\n");
  883. return 0;
  884. err_irqs:
  885. xgbe_free_irqs(pdata);
  886. err_napi:
  887. xgbe_napi_disable(pdata, 1);
  888. hw_if->exit(pdata);
  889. return ret;
  890. }
  891. static void xgbe_stop(struct xgbe_prv_data *pdata)
  892. {
  893. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  894. struct xgbe_phy_if *phy_if = &pdata->phy_if;
  895. struct xgbe_channel *channel;
  896. struct net_device *netdev = pdata->netdev;
  897. struct netdev_queue *txq;
  898. unsigned int i;
  899. DBGPR("-->xgbe_stop\n");
  900. if (test_bit(XGBE_STOPPED, &pdata->dev_state))
  901. return;
  902. netif_tx_stop_all_queues(netdev);
  903. xgbe_stop_timers(pdata);
  904. flush_workqueue(pdata->dev_workqueue);
  905. hw_if->disable_tx(pdata);
  906. hw_if->disable_rx(pdata);
  907. xgbe_free_irqs(pdata);
  908. xgbe_napi_disable(pdata, 1);
  909. phy_if->phy_stop(pdata);
  910. hw_if->exit(pdata);
  911. channel = pdata->channel;
  912. for (i = 0; i < pdata->channel_count; i++, channel++) {
  913. if (!channel->tx_ring)
  914. continue;
  915. txq = netdev_get_tx_queue(netdev, channel->queue_index);
  916. netdev_tx_reset_queue(txq);
  917. }
  918. set_bit(XGBE_STOPPED, &pdata->dev_state);
  919. DBGPR("<--xgbe_stop\n");
  920. }
  921. static void xgbe_stopdev(struct work_struct *work)
  922. {
  923. struct xgbe_prv_data *pdata = container_of(work,
  924. struct xgbe_prv_data,
  925. stopdev_work);
  926. rtnl_lock();
  927. xgbe_stop(pdata);
  928. xgbe_free_tx_data(pdata);
  929. xgbe_free_rx_data(pdata);
  930. rtnl_unlock();
  931. netdev_alert(pdata->netdev, "device stopped\n");
  932. }
  933. static void xgbe_restart_dev(struct xgbe_prv_data *pdata)
  934. {
  935. DBGPR("-->xgbe_restart_dev\n");
  936. /* If not running, "restart" will happen on open */
  937. if (!netif_running(pdata->netdev))
  938. return;
  939. xgbe_stop(pdata);
  940. xgbe_free_tx_data(pdata);
  941. xgbe_free_rx_data(pdata);
  942. xgbe_start(pdata);
  943. DBGPR("<--xgbe_restart_dev\n");
  944. }
  945. static void xgbe_restart(struct work_struct *work)
  946. {
  947. struct xgbe_prv_data *pdata = container_of(work,
  948. struct xgbe_prv_data,
  949. restart_work);
  950. rtnl_lock();
  951. xgbe_restart_dev(pdata);
  952. rtnl_unlock();
  953. }
  954. static void xgbe_tx_tstamp(struct work_struct *work)
  955. {
  956. struct xgbe_prv_data *pdata = container_of(work,
  957. struct xgbe_prv_data,
  958. tx_tstamp_work);
  959. struct skb_shared_hwtstamps hwtstamps;
  960. u64 nsec;
  961. unsigned long flags;
  962. if (pdata->tx_tstamp) {
  963. nsec = timecounter_cyc2time(&pdata->tstamp_tc,
  964. pdata->tx_tstamp);
  965. memset(&hwtstamps, 0, sizeof(hwtstamps));
  966. hwtstamps.hwtstamp = ns_to_ktime(nsec);
  967. skb_tstamp_tx(pdata->tx_tstamp_skb, &hwtstamps);
  968. }
  969. dev_kfree_skb_any(pdata->tx_tstamp_skb);
  970. spin_lock_irqsave(&pdata->tstamp_lock, flags);
  971. pdata->tx_tstamp_skb = NULL;
  972. spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
  973. }
  974. static int xgbe_get_hwtstamp_settings(struct xgbe_prv_data *pdata,
  975. struct ifreq *ifreq)
  976. {
  977. if (copy_to_user(ifreq->ifr_data, &pdata->tstamp_config,
  978. sizeof(pdata->tstamp_config)))
  979. return -EFAULT;
  980. return 0;
  981. }
  982. static int xgbe_set_hwtstamp_settings(struct xgbe_prv_data *pdata,
  983. struct ifreq *ifreq)
  984. {
  985. struct hwtstamp_config config;
  986. unsigned int mac_tscr;
  987. if (copy_from_user(&config, ifreq->ifr_data, sizeof(config)))
  988. return -EFAULT;
  989. if (config.flags)
  990. return -EINVAL;
  991. mac_tscr = 0;
  992. switch (config.tx_type) {
  993. case HWTSTAMP_TX_OFF:
  994. break;
  995. case HWTSTAMP_TX_ON:
  996. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  997. break;
  998. default:
  999. return -ERANGE;
  1000. }
  1001. switch (config.rx_filter) {
  1002. case HWTSTAMP_FILTER_NONE:
  1003. break;
  1004. case HWTSTAMP_FILTER_ALL:
  1005. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENALL, 1);
  1006. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  1007. break;
  1008. /* PTP v2, UDP, any kind of event packet */
  1009. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  1010. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
  1011. /* PTP v1, UDP, any kind of event packet */
  1012. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  1013. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
  1014. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
  1015. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
  1016. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  1017. break;
  1018. /* PTP v2, UDP, Sync packet */
  1019. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  1020. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
  1021. /* PTP v1, UDP, Sync packet */
  1022. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  1023. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
  1024. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
  1025. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
  1026. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  1027. break;
  1028. /* PTP v2, UDP, Delay_req packet */
  1029. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  1030. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
  1031. /* PTP v1, UDP, Delay_req packet */
  1032. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  1033. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
  1034. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
  1035. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
  1036. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
  1037. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  1038. break;
  1039. /* 802.AS1, Ethernet, any kind of event packet */
  1040. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  1041. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
  1042. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
  1043. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  1044. break;
  1045. /* 802.AS1, Ethernet, Sync packet */
  1046. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  1047. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
  1048. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
  1049. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  1050. break;
  1051. /* 802.AS1, Ethernet, Delay_req packet */
  1052. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  1053. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
  1054. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
  1055. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
  1056. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  1057. break;
  1058. /* PTP v2/802.AS1, any layer, any kind of event packet */
  1059. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  1060. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
  1061. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
  1062. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
  1063. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
  1064. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
  1065. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  1066. break;
  1067. /* PTP v2/802.AS1, any layer, Sync packet */
  1068. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  1069. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
  1070. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
  1071. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
  1072. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
  1073. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
  1074. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  1075. break;
  1076. /* PTP v2/802.AS1, any layer, Delay_req packet */
  1077. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  1078. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
  1079. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
  1080. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
  1081. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
  1082. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
  1083. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
  1084. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  1085. break;
  1086. default:
  1087. return -ERANGE;
  1088. }
  1089. pdata->hw_if.config_tstamp(pdata, mac_tscr);
  1090. memcpy(&pdata->tstamp_config, &config, sizeof(config));
  1091. return 0;
  1092. }
  1093. static void xgbe_prep_tx_tstamp(struct xgbe_prv_data *pdata,
  1094. struct sk_buff *skb,
  1095. struct xgbe_packet_data *packet)
  1096. {
  1097. unsigned long flags;
  1098. if (XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP)) {
  1099. spin_lock_irqsave(&pdata->tstamp_lock, flags);
  1100. if (pdata->tx_tstamp_skb) {
  1101. /* Another timestamp in progress, ignore this one */
  1102. XGMAC_SET_BITS(packet->attributes,
  1103. TX_PACKET_ATTRIBUTES, PTP, 0);
  1104. } else {
  1105. pdata->tx_tstamp_skb = skb_get(skb);
  1106. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1107. }
  1108. spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
  1109. }
  1110. if (!XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP))
  1111. skb_tx_timestamp(skb);
  1112. }
  1113. static void xgbe_prep_vlan(struct sk_buff *skb, struct xgbe_packet_data *packet)
  1114. {
  1115. if (skb_vlan_tag_present(skb))
  1116. packet->vlan_ctag = skb_vlan_tag_get(skb);
  1117. }
  1118. static int xgbe_prep_tso(struct sk_buff *skb, struct xgbe_packet_data *packet)
  1119. {
  1120. int ret;
  1121. if (!XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1122. TSO_ENABLE))
  1123. return 0;
  1124. ret = skb_cow_head(skb, 0);
  1125. if (ret)
  1126. return ret;
  1127. packet->header_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1128. packet->tcp_header_len = tcp_hdrlen(skb);
  1129. packet->tcp_payload_len = skb->len - packet->header_len;
  1130. packet->mss = skb_shinfo(skb)->gso_size;
  1131. DBGPR(" packet->header_len=%u\n", packet->header_len);
  1132. DBGPR(" packet->tcp_header_len=%u, packet->tcp_payload_len=%u\n",
  1133. packet->tcp_header_len, packet->tcp_payload_len);
  1134. DBGPR(" packet->mss=%u\n", packet->mss);
  1135. /* Update the number of packets that will ultimately be transmitted
  1136. * along with the extra bytes for each extra packet
  1137. */
  1138. packet->tx_packets = skb_shinfo(skb)->gso_segs;
  1139. packet->tx_bytes += (packet->tx_packets - 1) * packet->header_len;
  1140. return 0;
  1141. }
  1142. static int xgbe_is_tso(struct sk_buff *skb)
  1143. {
  1144. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1145. return 0;
  1146. if (!skb_is_gso(skb))
  1147. return 0;
  1148. DBGPR(" TSO packet to be processed\n");
  1149. return 1;
  1150. }
  1151. static void xgbe_packet_info(struct xgbe_prv_data *pdata,
  1152. struct xgbe_ring *ring, struct sk_buff *skb,
  1153. struct xgbe_packet_data *packet)
  1154. {
  1155. struct skb_frag_struct *frag;
  1156. unsigned int context_desc;
  1157. unsigned int len;
  1158. unsigned int i;
  1159. packet->skb = skb;
  1160. context_desc = 0;
  1161. packet->rdesc_count = 0;
  1162. packet->tx_packets = 1;
  1163. packet->tx_bytes = skb->len;
  1164. if (xgbe_is_tso(skb)) {
  1165. /* TSO requires an extra descriptor if mss is different */
  1166. if (skb_shinfo(skb)->gso_size != ring->tx.cur_mss) {
  1167. context_desc = 1;
  1168. packet->rdesc_count++;
  1169. }
  1170. /* TSO requires an extra descriptor for TSO header */
  1171. packet->rdesc_count++;
  1172. XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1173. TSO_ENABLE, 1);
  1174. XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1175. CSUM_ENABLE, 1);
  1176. } else if (skb->ip_summed == CHECKSUM_PARTIAL)
  1177. XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1178. CSUM_ENABLE, 1);
  1179. if (skb_vlan_tag_present(skb)) {
  1180. /* VLAN requires an extra descriptor if tag is different */
  1181. if (skb_vlan_tag_get(skb) != ring->tx.cur_vlan_ctag)
  1182. /* We can share with the TSO context descriptor */
  1183. if (!context_desc) {
  1184. context_desc = 1;
  1185. packet->rdesc_count++;
  1186. }
  1187. XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1188. VLAN_CTAG, 1);
  1189. }
  1190. if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
  1191. (pdata->tstamp_config.tx_type == HWTSTAMP_TX_ON))
  1192. XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1193. PTP, 1);
  1194. for (len = skb_headlen(skb); len;) {
  1195. packet->rdesc_count++;
  1196. len -= min_t(unsigned int, len, XGBE_TX_MAX_BUF_SIZE);
  1197. }
  1198. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1199. frag = &skb_shinfo(skb)->frags[i];
  1200. for (len = skb_frag_size(frag); len; ) {
  1201. packet->rdesc_count++;
  1202. len -= min_t(unsigned int, len, XGBE_TX_MAX_BUF_SIZE);
  1203. }
  1204. }
  1205. }
  1206. static int xgbe_open(struct net_device *netdev)
  1207. {
  1208. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1209. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  1210. int ret;
  1211. DBGPR("-->xgbe_open\n");
  1212. /* Reset the phy settings */
  1213. ret = xgbe_phy_reset(pdata);
  1214. if (ret)
  1215. return ret;
  1216. /* Enable the clocks */
  1217. ret = clk_prepare_enable(pdata->sysclk);
  1218. if (ret) {
  1219. netdev_alert(netdev, "dma clk_prepare_enable failed\n");
  1220. return ret;
  1221. }
  1222. ret = clk_prepare_enable(pdata->ptpclk);
  1223. if (ret) {
  1224. netdev_alert(netdev, "ptp clk_prepare_enable failed\n");
  1225. goto err_sysclk;
  1226. }
  1227. /* Calculate the Rx buffer size before allocating rings */
  1228. ret = xgbe_calc_rx_buf_size(netdev, netdev->mtu);
  1229. if (ret < 0)
  1230. goto err_ptpclk;
  1231. pdata->rx_buf_size = ret;
  1232. /* Allocate the channel and ring structures */
  1233. ret = xgbe_alloc_channels(pdata);
  1234. if (ret)
  1235. goto err_ptpclk;
  1236. /* Allocate the ring descriptors and buffers */
  1237. ret = desc_if->alloc_ring_resources(pdata);
  1238. if (ret)
  1239. goto err_channels;
  1240. INIT_WORK(&pdata->service_work, xgbe_service);
  1241. INIT_WORK(&pdata->restart_work, xgbe_restart);
  1242. INIT_WORK(&pdata->stopdev_work, xgbe_stopdev);
  1243. INIT_WORK(&pdata->tx_tstamp_work, xgbe_tx_tstamp);
  1244. xgbe_init_timers(pdata);
  1245. ret = xgbe_start(pdata);
  1246. if (ret)
  1247. goto err_rings;
  1248. clear_bit(XGBE_DOWN, &pdata->dev_state);
  1249. DBGPR("<--xgbe_open\n");
  1250. return 0;
  1251. err_rings:
  1252. desc_if->free_ring_resources(pdata);
  1253. err_channels:
  1254. xgbe_free_channels(pdata);
  1255. err_ptpclk:
  1256. clk_disable_unprepare(pdata->ptpclk);
  1257. err_sysclk:
  1258. clk_disable_unprepare(pdata->sysclk);
  1259. return ret;
  1260. }
  1261. static int xgbe_close(struct net_device *netdev)
  1262. {
  1263. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1264. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  1265. DBGPR("-->xgbe_close\n");
  1266. /* Stop the device */
  1267. xgbe_stop(pdata);
  1268. /* Free the ring descriptors and buffers */
  1269. desc_if->free_ring_resources(pdata);
  1270. /* Free the channel and ring structures */
  1271. xgbe_free_channels(pdata);
  1272. /* Disable the clocks */
  1273. clk_disable_unprepare(pdata->ptpclk);
  1274. clk_disable_unprepare(pdata->sysclk);
  1275. set_bit(XGBE_DOWN, &pdata->dev_state);
  1276. DBGPR("<--xgbe_close\n");
  1277. return 0;
  1278. }
  1279. static int xgbe_xmit(struct sk_buff *skb, struct net_device *netdev)
  1280. {
  1281. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1282. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1283. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  1284. struct xgbe_channel *channel;
  1285. struct xgbe_ring *ring;
  1286. struct xgbe_packet_data *packet;
  1287. struct netdev_queue *txq;
  1288. int ret;
  1289. DBGPR("-->xgbe_xmit: skb->len = %d\n", skb->len);
  1290. channel = pdata->channel + skb->queue_mapping;
  1291. txq = netdev_get_tx_queue(netdev, channel->queue_index);
  1292. ring = channel->tx_ring;
  1293. packet = &ring->packet_data;
  1294. ret = NETDEV_TX_OK;
  1295. if (skb->len == 0) {
  1296. netif_err(pdata, tx_err, netdev,
  1297. "empty skb received from stack\n");
  1298. dev_kfree_skb_any(skb);
  1299. goto tx_netdev_return;
  1300. }
  1301. /* Calculate preliminary packet info */
  1302. memset(packet, 0, sizeof(*packet));
  1303. xgbe_packet_info(pdata, ring, skb, packet);
  1304. /* Check that there are enough descriptors available */
  1305. ret = xgbe_maybe_stop_tx_queue(channel, ring, packet->rdesc_count);
  1306. if (ret)
  1307. goto tx_netdev_return;
  1308. ret = xgbe_prep_tso(skb, packet);
  1309. if (ret) {
  1310. netif_err(pdata, tx_err, netdev,
  1311. "error processing TSO packet\n");
  1312. dev_kfree_skb_any(skb);
  1313. goto tx_netdev_return;
  1314. }
  1315. xgbe_prep_vlan(skb, packet);
  1316. if (!desc_if->map_tx_skb(channel, skb)) {
  1317. dev_kfree_skb_any(skb);
  1318. goto tx_netdev_return;
  1319. }
  1320. xgbe_prep_tx_tstamp(pdata, skb, packet);
  1321. /* Report on the actual number of bytes (to be) sent */
  1322. netdev_tx_sent_queue(txq, packet->tx_bytes);
  1323. /* Configure required descriptor fields for transmission */
  1324. hw_if->dev_xmit(channel);
  1325. if (netif_msg_pktdata(pdata))
  1326. xgbe_print_pkt(netdev, skb, true);
  1327. /* Stop the queue in advance if there may not be enough descriptors */
  1328. xgbe_maybe_stop_tx_queue(channel, ring, XGBE_TX_MAX_DESCS);
  1329. ret = NETDEV_TX_OK;
  1330. tx_netdev_return:
  1331. return ret;
  1332. }
  1333. static void xgbe_set_rx_mode(struct net_device *netdev)
  1334. {
  1335. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1336. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1337. DBGPR("-->xgbe_set_rx_mode\n");
  1338. hw_if->config_rx_mode(pdata);
  1339. DBGPR("<--xgbe_set_rx_mode\n");
  1340. }
  1341. static int xgbe_set_mac_address(struct net_device *netdev, void *addr)
  1342. {
  1343. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1344. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1345. struct sockaddr *saddr = addr;
  1346. DBGPR("-->xgbe_set_mac_address\n");
  1347. if (!is_valid_ether_addr(saddr->sa_data))
  1348. return -EADDRNOTAVAIL;
  1349. memcpy(netdev->dev_addr, saddr->sa_data, netdev->addr_len);
  1350. hw_if->set_mac_address(pdata, netdev->dev_addr);
  1351. DBGPR("<--xgbe_set_mac_address\n");
  1352. return 0;
  1353. }
  1354. static int xgbe_ioctl(struct net_device *netdev, struct ifreq *ifreq, int cmd)
  1355. {
  1356. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1357. int ret;
  1358. switch (cmd) {
  1359. case SIOCGHWTSTAMP:
  1360. ret = xgbe_get_hwtstamp_settings(pdata, ifreq);
  1361. break;
  1362. case SIOCSHWTSTAMP:
  1363. ret = xgbe_set_hwtstamp_settings(pdata, ifreq);
  1364. break;
  1365. default:
  1366. ret = -EOPNOTSUPP;
  1367. }
  1368. return ret;
  1369. }
  1370. static int xgbe_change_mtu(struct net_device *netdev, int mtu)
  1371. {
  1372. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1373. int ret;
  1374. DBGPR("-->xgbe_change_mtu\n");
  1375. ret = xgbe_calc_rx_buf_size(netdev, mtu);
  1376. if (ret < 0)
  1377. return ret;
  1378. pdata->rx_buf_size = ret;
  1379. netdev->mtu = mtu;
  1380. xgbe_restart_dev(pdata);
  1381. DBGPR("<--xgbe_change_mtu\n");
  1382. return 0;
  1383. }
  1384. static void xgbe_tx_timeout(struct net_device *netdev)
  1385. {
  1386. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1387. netdev_warn(netdev, "tx timeout, device restarting\n");
  1388. schedule_work(&pdata->restart_work);
  1389. }
  1390. static struct rtnl_link_stats64 *xgbe_get_stats64(struct net_device *netdev,
  1391. struct rtnl_link_stats64 *s)
  1392. {
  1393. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1394. struct xgbe_mmc_stats *pstats = &pdata->mmc_stats;
  1395. DBGPR("-->%s\n", __func__);
  1396. pdata->hw_if.read_mmc_stats(pdata);
  1397. s->rx_packets = pstats->rxframecount_gb;
  1398. s->rx_bytes = pstats->rxoctetcount_gb;
  1399. s->rx_errors = pstats->rxframecount_gb -
  1400. pstats->rxbroadcastframes_g -
  1401. pstats->rxmulticastframes_g -
  1402. pstats->rxunicastframes_g;
  1403. s->multicast = pstats->rxmulticastframes_g;
  1404. s->rx_length_errors = pstats->rxlengtherror;
  1405. s->rx_crc_errors = pstats->rxcrcerror;
  1406. s->rx_fifo_errors = pstats->rxfifooverflow;
  1407. s->tx_packets = pstats->txframecount_gb;
  1408. s->tx_bytes = pstats->txoctetcount_gb;
  1409. s->tx_errors = pstats->txframecount_gb - pstats->txframecount_g;
  1410. s->tx_dropped = netdev->stats.tx_dropped;
  1411. DBGPR("<--%s\n", __func__);
  1412. return s;
  1413. }
  1414. static int xgbe_vlan_rx_add_vid(struct net_device *netdev, __be16 proto,
  1415. u16 vid)
  1416. {
  1417. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1418. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1419. DBGPR("-->%s\n", __func__);
  1420. set_bit(vid, pdata->active_vlans);
  1421. hw_if->update_vlan_hash_table(pdata);
  1422. DBGPR("<--%s\n", __func__);
  1423. return 0;
  1424. }
  1425. static int xgbe_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto,
  1426. u16 vid)
  1427. {
  1428. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1429. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1430. DBGPR("-->%s\n", __func__);
  1431. clear_bit(vid, pdata->active_vlans);
  1432. hw_if->update_vlan_hash_table(pdata);
  1433. DBGPR("<--%s\n", __func__);
  1434. return 0;
  1435. }
  1436. #ifdef CONFIG_NET_POLL_CONTROLLER
  1437. static void xgbe_poll_controller(struct net_device *netdev)
  1438. {
  1439. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1440. struct xgbe_channel *channel;
  1441. unsigned int i;
  1442. DBGPR("-->xgbe_poll_controller\n");
  1443. if (pdata->per_channel_irq) {
  1444. channel = pdata->channel;
  1445. for (i = 0; i < pdata->channel_count; i++, channel++)
  1446. xgbe_dma_isr(channel->dma_irq, channel);
  1447. } else {
  1448. disable_irq(pdata->dev_irq);
  1449. xgbe_isr(pdata->dev_irq, pdata);
  1450. enable_irq(pdata->dev_irq);
  1451. }
  1452. DBGPR("<--xgbe_poll_controller\n");
  1453. }
  1454. #endif /* End CONFIG_NET_POLL_CONTROLLER */
  1455. static int xgbe_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
  1456. struct tc_to_netdev *tc_to_netdev)
  1457. {
  1458. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1459. u8 tc;
  1460. if (tc_to_netdev->type != TC_SETUP_MQPRIO)
  1461. return -EINVAL;
  1462. tc = tc_to_netdev->tc;
  1463. if (tc > pdata->hw_feat.tc_cnt)
  1464. return -EINVAL;
  1465. pdata->num_tcs = tc;
  1466. pdata->hw_if.config_tc(pdata);
  1467. return 0;
  1468. }
  1469. static int xgbe_set_features(struct net_device *netdev,
  1470. netdev_features_t features)
  1471. {
  1472. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1473. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1474. netdev_features_t rxhash, rxcsum, rxvlan, rxvlan_filter;
  1475. int ret = 0;
  1476. rxhash = pdata->netdev_features & NETIF_F_RXHASH;
  1477. rxcsum = pdata->netdev_features & NETIF_F_RXCSUM;
  1478. rxvlan = pdata->netdev_features & NETIF_F_HW_VLAN_CTAG_RX;
  1479. rxvlan_filter = pdata->netdev_features & NETIF_F_HW_VLAN_CTAG_FILTER;
  1480. if ((features & NETIF_F_RXHASH) && !rxhash)
  1481. ret = hw_if->enable_rss(pdata);
  1482. else if (!(features & NETIF_F_RXHASH) && rxhash)
  1483. ret = hw_if->disable_rss(pdata);
  1484. if (ret)
  1485. return ret;
  1486. if ((features & NETIF_F_RXCSUM) && !rxcsum)
  1487. hw_if->enable_rx_csum(pdata);
  1488. else if (!(features & NETIF_F_RXCSUM) && rxcsum)
  1489. hw_if->disable_rx_csum(pdata);
  1490. if ((features & NETIF_F_HW_VLAN_CTAG_RX) && !rxvlan)
  1491. hw_if->enable_rx_vlan_stripping(pdata);
  1492. else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) && rxvlan)
  1493. hw_if->disable_rx_vlan_stripping(pdata);
  1494. if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) && !rxvlan_filter)
  1495. hw_if->enable_rx_vlan_filtering(pdata);
  1496. else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) && rxvlan_filter)
  1497. hw_if->disable_rx_vlan_filtering(pdata);
  1498. pdata->netdev_features = features;
  1499. DBGPR("<--xgbe_set_features\n");
  1500. return 0;
  1501. }
  1502. static const struct net_device_ops xgbe_netdev_ops = {
  1503. .ndo_open = xgbe_open,
  1504. .ndo_stop = xgbe_close,
  1505. .ndo_start_xmit = xgbe_xmit,
  1506. .ndo_set_rx_mode = xgbe_set_rx_mode,
  1507. .ndo_set_mac_address = xgbe_set_mac_address,
  1508. .ndo_validate_addr = eth_validate_addr,
  1509. .ndo_do_ioctl = xgbe_ioctl,
  1510. .ndo_change_mtu = xgbe_change_mtu,
  1511. .ndo_tx_timeout = xgbe_tx_timeout,
  1512. .ndo_get_stats64 = xgbe_get_stats64,
  1513. .ndo_vlan_rx_add_vid = xgbe_vlan_rx_add_vid,
  1514. .ndo_vlan_rx_kill_vid = xgbe_vlan_rx_kill_vid,
  1515. #ifdef CONFIG_NET_POLL_CONTROLLER
  1516. .ndo_poll_controller = xgbe_poll_controller,
  1517. #endif
  1518. .ndo_setup_tc = xgbe_setup_tc,
  1519. .ndo_set_features = xgbe_set_features,
  1520. };
  1521. const struct net_device_ops *xgbe_get_netdev_ops(void)
  1522. {
  1523. return &xgbe_netdev_ops;
  1524. }
  1525. static void xgbe_rx_refresh(struct xgbe_channel *channel)
  1526. {
  1527. struct xgbe_prv_data *pdata = channel->pdata;
  1528. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1529. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  1530. struct xgbe_ring *ring = channel->rx_ring;
  1531. struct xgbe_ring_data *rdata;
  1532. while (ring->dirty != ring->cur) {
  1533. rdata = XGBE_GET_DESC_DATA(ring, ring->dirty);
  1534. /* Reset rdata values */
  1535. desc_if->unmap_rdata(pdata, rdata);
  1536. if (desc_if->map_rx_buffer(pdata, ring, rdata))
  1537. break;
  1538. hw_if->rx_desc_reset(pdata, rdata, ring->dirty);
  1539. ring->dirty++;
  1540. }
  1541. /* Make sure everything is written before the register write */
  1542. wmb();
  1543. /* Update the Rx Tail Pointer Register with address of
  1544. * the last cleaned entry */
  1545. rdata = XGBE_GET_DESC_DATA(ring, ring->dirty - 1);
  1546. XGMAC_DMA_IOWRITE(channel, DMA_CH_RDTR_LO,
  1547. lower_32_bits(rdata->rdesc_dma));
  1548. }
  1549. static struct sk_buff *xgbe_create_skb(struct xgbe_prv_data *pdata,
  1550. struct napi_struct *napi,
  1551. struct xgbe_ring_data *rdata,
  1552. unsigned int len)
  1553. {
  1554. struct sk_buff *skb;
  1555. u8 *packet;
  1556. unsigned int copy_len;
  1557. skb = napi_alloc_skb(napi, rdata->rx.hdr.dma_len);
  1558. if (!skb)
  1559. return NULL;
  1560. /* Start with the header buffer which may contain just the header
  1561. * or the header plus data
  1562. */
  1563. dma_sync_single_range_for_cpu(pdata->dev, rdata->rx.hdr.dma_base,
  1564. rdata->rx.hdr.dma_off,
  1565. rdata->rx.hdr.dma_len, DMA_FROM_DEVICE);
  1566. packet = page_address(rdata->rx.hdr.pa.pages) +
  1567. rdata->rx.hdr.pa.pages_offset;
  1568. copy_len = (rdata->rx.hdr_len) ? rdata->rx.hdr_len : len;
  1569. copy_len = min(rdata->rx.hdr.dma_len, copy_len);
  1570. skb_copy_to_linear_data(skb, packet, copy_len);
  1571. skb_put(skb, copy_len);
  1572. len -= copy_len;
  1573. if (len) {
  1574. /* Add the remaining data as a frag */
  1575. dma_sync_single_range_for_cpu(pdata->dev,
  1576. rdata->rx.buf.dma_base,
  1577. rdata->rx.buf.dma_off,
  1578. rdata->rx.buf.dma_len,
  1579. DMA_FROM_DEVICE);
  1580. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
  1581. rdata->rx.buf.pa.pages,
  1582. rdata->rx.buf.pa.pages_offset,
  1583. len, rdata->rx.buf.dma_len);
  1584. rdata->rx.buf.pa.pages = NULL;
  1585. }
  1586. return skb;
  1587. }
  1588. static int xgbe_tx_poll(struct xgbe_channel *channel)
  1589. {
  1590. struct xgbe_prv_data *pdata = channel->pdata;
  1591. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1592. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  1593. struct xgbe_ring *ring = channel->tx_ring;
  1594. struct xgbe_ring_data *rdata;
  1595. struct xgbe_ring_desc *rdesc;
  1596. struct net_device *netdev = pdata->netdev;
  1597. struct netdev_queue *txq;
  1598. int processed = 0;
  1599. unsigned int tx_packets = 0, tx_bytes = 0;
  1600. unsigned int cur;
  1601. DBGPR("-->xgbe_tx_poll\n");
  1602. /* Nothing to do if there isn't a Tx ring for this channel */
  1603. if (!ring)
  1604. return 0;
  1605. cur = ring->cur;
  1606. /* Be sure we get ring->cur before accessing descriptor data */
  1607. smp_rmb();
  1608. txq = netdev_get_tx_queue(netdev, channel->queue_index);
  1609. while ((processed < XGBE_TX_DESC_MAX_PROC) &&
  1610. (ring->dirty != cur)) {
  1611. rdata = XGBE_GET_DESC_DATA(ring, ring->dirty);
  1612. rdesc = rdata->rdesc;
  1613. if (!hw_if->tx_complete(rdesc))
  1614. break;
  1615. /* Make sure descriptor fields are read after reading the OWN
  1616. * bit */
  1617. dma_rmb();
  1618. if (netif_msg_tx_done(pdata))
  1619. xgbe_dump_tx_desc(pdata, ring, ring->dirty, 1, 0);
  1620. if (hw_if->is_last_desc(rdesc)) {
  1621. tx_packets += rdata->tx.packets;
  1622. tx_bytes += rdata->tx.bytes;
  1623. }
  1624. /* Free the SKB and reset the descriptor for re-use */
  1625. desc_if->unmap_rdata(pdata, rdata);
  1626. hw_if->tx_desc_reset(rdata);
  1627. processed++;
  1628. ring->dirty++;
  1629. }
  1630. if (!processed)
  1631. return 0;
  1632. netdev_tx_completed_queue(txq, tx_packets, tx_bytes);
  1633. if ((ring->tx.queue_stopped == 1) &&
  1634. (xgbe_tx_avail_desc(ring) > XGBE_TX_DESC_MIN_FREE)) {
  1635. ring->tx.queue_stopped = 0;
  1636. netif_tx_wake_queue(txq);
  1637. }
  1638. DBGPR("<--xgbe_tx_poll: processed=%d\n", processed);
  1639. return processed;
  1640. }
  1641. static int xgbe_rx_poll(struct xgbe_channel *channel, int budget)
  1642. {
  1643. struct xgbe_prv_data *pdata = channel->pdata;
  1644. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1645. struct xgbe_ring *ring = channel->rx_ring;
  1646. struct xgbe_ring_data *rdata;
  1647. struct xgbe_packet_data *packet;
  1648. struct net_device *netdev = pdata->netdev;
  1649. struct napi_struct *napi;
  1650. struct sk_buff *skb;
  1651. struct skb_shared_hwtstamps *hwtstamps;
  1652. unsigned int incomplete, error, context_next, context;
  1653. unsigned int len, rdesc_len, max_len;
  1654. unsigned int received = 0;
  1655. int packet_count = 0;
  1656. DBGPR("-->xgbe_rx_poll: budget=%d\n", budget);
  1657. /* Nothing to do if there isn't a Rx ring for this channel */
  1658. if (!ring)
  1659. return 0;
  1660. incomplete = 0;
  1661. context_next = 0;
  1662. napi = (pdata->per_channel_irq) ? &channel->napi : &pdata->napi;
  1663. rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
  1664. packet = &ring->packet_data;
  1665. while (packet_count < budget) {
  1666. DBGPR(" cur = %d\n", ring->cur);
  1667. /* First time in loop see if we need to restore state */
  1668. if (!received && rdata->state_saved) {
  1669. skb = rdata->state.skb;
  1670. error = rdata->state.error;
  1671. len = rdata->state.len;
  1672. } else {
  1673. memset(packet, 0, sizeof(*packet));
  1674. skb = NULL;
  1675. error = 0;
  1676. len = 0;
  1677. }
  1678. read_again:
  1679. rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
  1680. if (xgbe_rx_dirty_desc(ring) > (XGBE_RX_DESC_CNT >> 3))
  1681. xgbe_rx_refresh(channel);
  1682. if (hw_if->dev_read(channel))
  1683. break;
  1684. received++;
  1685. ring->cur++;
  1686. incomplete = XGMAC_GET_BITS(packet->attributes,
  1687. RX_PACKET_ATTRIBUTES,
  1688. INCOMPLETE);
  1689. context_next = XGMAC_GET_BITS(packet->attributes,
  1690. RX_PACKET_ATTRIBUTES,
  1691. CONTEXT_NEXT);
  1692. context = XGMAC_GET_BITS(packet->attributes,
  1693. RX_PACKET_ATTRIBUTES,
  1694. CONTEXT);
  1695. /* Earlier error, just drain the remaining data */
  1696. if ((incomplete || context_next) && error)
  1697. goto read_again;
  1698. if (error || packet->errors) {
  1699. if (packet->errors)
  1700. netif_err(pdata, rx_err, netdev,
  1701. "error in received packet\n");
  1702. dev_kfree_skb(skb);
  1703. goto next_packet;
  1704. }
  1705. if (!context) {
  1706. /* Length is cumulative, get this descriptor's length */
  1707. rdesc_len = rdata->rx.len - len;
  1708. len += rdesc_len;
  1709. if (rdesc_len && !skb) {
  1710. skb = xgbe_create_skb(pdata, napi, rdata,
  1711. rdesc_len);
  1712. if (!skb)
  1713. error = 1;
  1714. } else if (rdesc_len) {
  1715. dma_sync_single_range_for_cpu(pdata->dev,
  1716. rdata->rx.buf.dma_base,
  1717. rdata->rx.buf.dma_off,
  1718. rdata->rx.buf.dma_len,
  1719. DMA_FROM_DEVICE);
  1720. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
  1721. rdata->rx.buf.pa.pages,
  1722. rdata->rx.buf.pa.pages_offset,
  1723. rdesc_len,
  1724. rdata->rx.buf.dma_len);
  1725. rdata->rx.buf.pa.pages = NULL;
  1726. }
  1727. }
  1728. if (incomplete || context_next)
  1729. goto read_again;
  1730. if (!skb)
  1731. goto next_packet;
  1732. /* Be sure we don't exceed the configured MTU */
  1733. max_len = netdev->mtu + ETH_HLEN;
  1734. if (!(netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
  1735. (skb->protocol == htons(ETH_P_8021Q)))
  1736. max_len += VLAN_HLEN;
  1737. if (skb->len > max_len) {
  1738. netif_err(pdata, rx_err, netdev,
  1739. "packet length exceeds configured MTU\n");
  1740. dev_kfree_skb(skb);
  1741. goto next_packet;
  1742. }
  1743. if (netif_msg_pktdata(pdata))
  1744. xgbe_print_pkt(netdev, skb, false);
  1745. skb_checksum_none_assert(skb);
  1746. if (XGMAC_GET_BITS(packet->attributes,
  1747. RX_PACKET_ATTRIBUTES, CSUM_DONE))
  1748. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1749. if (XGMAC_GET_BITS(packet->attributes,
  1750. RX_PACKET_ATTRIBUTES, VLAN_CTAG))
  1751. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  1752. packet->vlan_ctag);
  1753. if (XGMAC_GET_BITS(packet->attributes,
  1754. RX_PACKET_ATTRIBUTES, RX_TSTAMP)) {
  1755. u64 nsec;
  1756. nsec = timecounter_cyc2time(&pdata->tstamp_tc,
  1757. packet->rx_tstamp);
  1758. hwtstamps = skb_hwtstamps(skb);
  1759. hwtstamps->hwtstamp = ns_to_ktime(nsec);
  1760. }
  1761. if (XGMAC_GET_BITS(packet->attributes,
  1762. RX_PACKET_ATTRIBUTES, RSS_HASH))
  1763. skb_set_hash(skb, packet->rss_hash,
  1764. packet->rss_hash_type);
  1765. skb->dev = netdev;
  1766. skb->protocol = eth_type_trans(skb, netdev);
  1767. skb_record_rx_queue(skb, channel->queue_index);
  1768. napi_gro_receive(napi, skb);
  1769. next_packet:
  1770. packet_count++;
  1771. }
  1772. /* Check if we need to save state before leaving */
  1773. if (received && (incomplete || context_next)) {
  1774. rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
  1775. rdata->state_saved = 1;
  1776. rdata->state.skb = skb;
  1777. rdata->state.len = len;
  1778. rdata->state.error = error;
  1779. }
  1780. DBGPR("<--xgbe_rx_poll: packet_count = %d\n", packet_count);
  1781. return packet_count;
  1782. }
  1783. static int xgbe_one_poll(struct napi_struct *napi, int budget)
  1784. {
  1785. struct xgbe_channel *channel = container_of(napi, struct xgbe_channel,
  1786. napi);
  1787. struct xgbe_prv_data *pdata = channel->pdata;
  1788. int processed = 0;
  1789. DBGPR("-->xgbe_one_poll: budget=%d\n", budget);
  1790. /* Cleanup Tx ring first */
  1791. xgbe_tx_poll(channel);
  1792. /* Process Rx ring next */
  1793. processed = xgbe_rx_poll(channel, budget);
  1794. /* If we processed everything, we are done */
  1795. if (processed < budget) {
  1796. /* Turn off polling */
  1797. napi_complete_done(napi, processed);
  1798. /* Enable Tx and Rx interrupts */
  1799. if (pdata->channel_irq_mode)
  1800. xgbe_enable_rx_tx_int(pdata, channel);
  1801. else
  1802. enable_irq(channel->dma_irq);
  1803. }
  1804. DBGPR("<--xgbe_one_poll: received = %d\n", processed);
  1805. return processed;
  1806. }
  1807. static int xgbe_all_poll(struct napi_struct *napi, int budget)
  1808. {
  1809. struct xgbe_prv_data *pdata = container_of(napi, struct xgbe_prv_data,
  1810. napi);
  1811. struct xgbe_channel *channel;
  1812. int ring_budget;
  1813. int processed, last_processed;
  1814. unsigned int i;
  1815. DBGPR("-->xgbe_all_poll: budget=%d\n", budget);
  1816. processed = 0;
  1817. ring_budget = budget / pdata->rx_ring_count;
  1818. do {
  1819. last_processed = processed;
  1820. channel = pdata->channel;
  1821. for (i = 0; i < pdata->channel_count; i++, channel++) {
  1822. /* Cleanup Tx ring first */
  1823. xgbe_tx_poll(channel);
  1824. /* Process Rx ring next */
  1825. if (ring_budget > (budget - processed))
  1826. ring_budget = budget - processed;
  1827. processed += xgbe_rx_poll(channel, ring_budget);
  1828. }
  1829. } while ((processed < budget) && (processed != last_processed));
  1830. /* If we processed everything, we are done */
  1831. if (processed < budget) {
  1832. /* Turn off polling */
  1833. napi_complete_done(napi, processed);
  1834. /* Enable Tx and Rx interrupts */
  1835. xgbe_enable_rx_tx_ints(pdata);
  1836. }
  1837. DBGPR("<--xgbe_all_poll: received = %d\n", processed);
  1838. return processed;
  1839. }
  1840. void xgbe_dump_tx_desc(struct xgbe_prv_data *pdata, struct xgbe_ring *ring,
  1841. unsigned int idx, unsigned int count, unsigned int flag)
  1842. {
  1843. struct xgbe_ring_data *rdata;
  1844. struct xgbe_ring_desc *rdesc;
  1845. while (count--) {
  1846. rdata = XGBE_GET_DESC_DATA(ring, idx);
  1847. rdesc = rdata->rdesc;
  1848. netdev_dbg(pdata->netdev,
  1849. "TX_NORMAL_DESC[%d %s] = %08x:%08x:%08x:%08x\n", idx,
  1850. (flag == 1) ? "QUEUED FOR TX" : "TX BY DEVICE",
  1851. le32_to_cpu(rdesc->desc0),
  1852. le32_to_cpu(rdesc->desc1),
  1853. le32_to_cpu(rdesc->desc2),
  1854. le32_to_cpu(rdesc->desc3));
  1855. idx++;
  1856. }
  1857. }
  1858. void xgbe_dump_rx_desc(struct xgbe_prv_data *pdata, struct xgbe_ring *ring,
  1859. unsigned int idx)
  1860. {
  1861. struct xgbe_ring_data *rdata;
  1862. struct xgbe_ring_desc *rdesc;
  1863. rdata = XGBE_GET_DESC_DATA(ring, idx);
  1864. rdesc = rdata->rdesc;
  1865. netdev_dbg(pdata->netdev,
  1866. "RX_NORMAL_DESC[%d RX BY DEVICE] = %08x:%08x:%08x:%08x\n",
  1867. idx, le32_to_cpu(rdesc->desc0), le32_to_cpu(rdesc->desc1),
  1868. le32_to_cpu(rdesc->desc2), le32_to_cpu(rdesc->desc3));
  1869. }
  1870. void xgbe_print_pkt(struct net_device *netdev, struct sk_buff *skb, bool tx_rx)
  1871. {
  1872. struct ethhdr *eth = (struct ethhdr *)skb->data;
  1873. unsigned char *buf = skb->data;
  1874. unsigned char buffer[128];
  1875. unsigned int i, j;
  1876. netdev_dbg(netdev, "\n************** SKB dump ****************\n");
  1877. netdev_dbg(netdev, "%s packet of %d bytes\n",
  1878. (tx_rx ? "TX" : "RX"), skb->len);
  1879. netdev_dbg(netdev, "Dst MAC addr: %pM\n", eth->h_dest);
  1880. netdev_dbg(netdev, "Src MAC addr: %pM\n", eth->h_source);
  1881. netdev_dbg(netdev, "Protocol: %#06hx\n", ntohs(eth->h_proto));
  1882. for (i = 0, j = 0; i < skb->len;) {
  1883. j += snprintf(buffer + j, sizeof(buffer) - j, "%02hhx",
  1884. buf[i++]);
  1885. if ((i % 32) == 0) {
  1886. netdev_dbg(netdev, " %#06x: %s\n", i - 32, buffer);
  1887. j = 0;
  1888. } else if ((i % 16) == 0) {
  1889. buffer[j++] = ' ';
  1890. buffer[j++] = ' ';
  1891. } else if ((i % 4) == 0) {
  1892. buffer[j++] = ' ';
  1893. }
  1894. }
  1895. if (i % 32)
  1896. netdev_dbg(netdev, " %#06x: %s\n", i - (i % 32), buffer);
  1897. netdev_dbg(netdev, "\n************** SKB dump ****************\n");
  1898. }