xgbe-drv.c 58 KB

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