xgbe-drv.c 65 KB

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