xgbe-drv.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205
  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-%u", 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. ret = hw_if->init(pdata);
  714. if (ret)
  715. return ret;
  716. ret = phy_if->phy_start(pdata);
  717. if (ret)
  718. goto err_phy;
  719. xgbe_napi_enable(pdata, 1);
  720. ret = xgbe_request_irqs(pdata);
  721. if (ret)
  722. goto err_napi;
  723. hw_if->enable_tx(pdata);
  724. hw_if->enable_rx(pdata);
  725. netif_tx_start_all_queues(netdev);
  726. xgbe_start_timers(pdata);
  727. queue_work(pdata->dev_workqueue, &pdata->service_work);
  728. DBGPR("<--xgbe_start\n");
  729. return 0;
  730. err_napi:
  731. xgbe_napi_disable(pdata, 1);
  732. phy_if->phy_stop(pdata);
  733. err_phy:
  734. hw_if->exit(pdata);
  735. return ret;
  736. }
  737. static void xgbe_stop(struct xgbe_prv_data *pdata)
  738. {
  739. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  740. struct xgbe_phy_if *phy_if = &pdata->phy_if;
  741. struct xgbe_channel *channel;
  742. struct net_device *netdev = pdata->netdev;
  743. struct netdev_queue *txq;
  744. unsigned int i;
  745. DBGPR("-->xgbe_stop\n");
  746. netif_tx_stop_all_queues(netdev);
  747. xgbe_stop_timers(pdata);
  748. flush_workqueue(pdata->dev_workqueue);
  749. hw_if->disable_tx(pdata);
  750. hw_if->disable_rx(pdata);
  751. xgbe_free_irqs(pdata);
  752. xgbe_napi_disable(pdata, 1);
  753. phy_if->phy_stop(pdata);
  754. hw_if->exit(pdata);
  755. channel = pdata->channel;
  756. for (i = 0; i < pdata->channel_count; i++, channel++) {
  757. if (!channel->tx_ring)
  758. continue;
  759. txq = netdev_get_tx_queue(netdev, channel->queue_index);
  760. netdev_tx_reset_queue(txq);
  761. }
  762. DBGPR("<--xgbe_stop\n");
  763. }
  764. static void xgbe_restart_dev(struct xgbe_prv_data *pdata)
  765. {
  766. DBGPR("-->xgbe_restart_dev\n");
  767. /* If not running, "restart" will happen on open */
  768. if (!netif_running(pdata->netdev))
  769. return;
  770. xgbe_stop(pdata);
  771. xgbe_free_tx_data(pdata);
  772. xgbe_free_rx_data(pdata);
  773. xgbe_start(pdata);
  774. DBGPR("<--xgbe_restart_dev\n");
  775. }
  776. static void xgbe_restart(struct work_struct *work)
  777. {
  778. struct xgbe_prv_data *pdata = container_of(work,
  779. struct xgbe_prv_data,
  780. restart_work);
  781. rtnl_lock();
  782. xgbe_restart_dev(pdata);
  783. rtnl_unlock();
  784. }
  785. static void xgbe_tx_tstamp(struct work_struct *work)
  786. {
  787. struct xgbe_prv_data *pdata = container_of(work,
  788. struct xgbe_prv_data,
  789. tx_tstamp_work);
  790. struct skb_shared_hwtstamps hwtstamps;
  791. u64 nsec;
  792. unsigned long flags;
  793. if (pdata->tx_tstamp) {
  794. nsec = timecounter_cyc2time(&pdata->tstamp_tc,
  795. pdata->tx_tstamp);
  796. memset(&hwtstamps, 0, sizeof(hwtstamps));
  797. hwtstamps.hwtstamp = ns_to_ktime(nsec);
  798. skb_tstamp_tx(pdata->tx_tstamp_skb, &hwtstamps);
  799. }
  800. dev_kfree_skb_any(pdata->tx_tstamp_skb);
  801. spin_lock_irqsave(&pdata->tstamp_lock, flags);
  802. pdata->tx_tstamp_skb = NULL;
  803. spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
  804. }
  805. static int xgbe_get_hwtstamp_settings(struct xgbe_prv_data *pdata,
  806. struct ifreq *ifreq)
  807. {
  808. if (copy_to_user(ifreq->ifr_data, &pdata->tstamp_config,
  809. sizeof(pdata->tstamp_config)))
  810. return -EFAULT;
  811. return 0;
  812. }
  813. static int xgbe_set_hwtstamp_settings(struct xgbe_prv_data *pdata,
  814. struct ifreq *ifreq)
  815. {
  816. struct hwtstamp_config config;
  817. unsigned int mac_tscr;
  818. if (copy_from_user(&config, ifreq->ifr_data, sizeof(config)))
  819. return -EFAULT;
  820. if (config.flags)
  821. return -EINVAL;
  822. mac_tscr = 0;
  823. switch (config.tx_type) {
  824. case HWTSTAMP_TX_OFF:
  825. break;
  826. case HWTSTAMP_TX_ON:
  827. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  828. break;
  829. default:
  830. return -ERANGE;
  831. }
  832. switch (config.rx_filter) {
  833. case HWTSTAMP_FILTER_NONE:
  834. break;
  835. case HWTSTAMP_FILTER_ALL:
  836. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENALL, 1);
  837. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  838. break;
  839. /* PTP v2, UDP, any kind of event packet */
  840. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  841. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
  842. /* PTP v1, UDP, any kind of event packet */
  843. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  844. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
  845. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
  846. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
  847. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  848. break;
  849. /* PTP v2, UDP, Sync packet */
  850. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  851. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
  852. /* PTP v1, UDP, Sync packet */
  853. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  854. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
  855. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
  856. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
  857. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  858. break;
  859. /* PTP v2, UDP, Delay_req packet */
  860. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  861. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
  862. /* PTP v1, UDP, Delay_req packet */
  863. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  864. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
  865. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
  866. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
  867. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
  868. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  869. break;
  870. /* 802.AS1, Ethernet, any kind of event packet */
  871. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  872. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
  873. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
  874. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  875. break;
  876. /* 802.AS1, Ethernet, Sync packet */
  877. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  878. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
  879. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
  880. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  881. break;
  882. /* 802.AS1, Ethernet, Delay_req packet */
  883. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  884. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
  885. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
  886. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
  887. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  888. break;
  889. /* PTP v2/802.AS1, any layer, any kind of event packet */
  890. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  891. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
  892. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
  893. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
  894. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
  895. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
  896. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  897. break;
  898. /* PTP v2/802.AS1, any layer, Sync packet */
  899. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  900. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
  901. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
  902. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
  903. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
  904. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
  905. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  906. break;
  907. /* PTP v2/802.AS1, any layer, Delay_req packet */
  908. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  909. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
  910. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
  911. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
  912. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
  913. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
  914. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
  915. XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
  916. break;
  917. default:
  918. return -ERANGE;
  919. }
  920. pdata->hw_if.config_tstamp(pdata, mac_tscr);
  921. memcpy(&pdata->tstamp_config, &config, sizeof(config));
  922. return 0;
  923. }
  924. static void xgbe_prep_tx_tstamp(struct xgbe_prv_data *pdata,
  925. struct sk_buff *skb,
  926. struct xgbe_packet_data *packet)
  927. {
  928. unsigned long flags;
  929. if (XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP)) {
  930. spin_lock_irqsave(&pdata->tstamp_lock, flags);
  931. if (pdata->tx_tstamp_skb) {
  932. /* Another timestamp in progress, ignore this one */
  933. XGMAC_SET_BITS(packet->attributes,
  934. TX_PACKET_ATTRIBUTES, PTP, 0);
  935. } else {
  936. pdata->tx_tstamp_skb = skb_get(skb);
  937. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  938. }
  939. spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
  940. }
  941. if (!XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP))
  942. skb_tx_timestamp(skb);
  943. }
  944. static void xgbe_prep_vlan(struct sk_buff *skb, struct xgbe_packet_data *packet)
  945. {
  946. if (skb_vlan_tag_present(skb))
  947. packet->vlan_ctag = skb_vlan_tag_get(skb);
  948. }
  949. static int xgbe_prep_tso(struct sk_buff *skb, struct xgbe_packet_data *packet)
  950. {
  951. int ret;
  952. if (!XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  953. TSO_ENABLE))
  954. return 0;
  955. ret = skb_cow_head(skb, 0);
  956. if (ret)
  957. return ret;
  958. packet->header_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  959. packet->tcp_header_len = tcp_hdrlen(skb);
  960. packet->tcp_payload_len = skb->len - packet->header_len;
  961. packet->mss = skb_shinfo(skb)->gso_size;
  962. DBGPR(" packet->header_len=%u\n", packet->header_len);
  963. DBGPR(" packet->tcp_header_len=%u, packet->tcp_payload_len=%u\n",
  964. packet->tcp_header_len, packet->tcp_payload_len);
  965. DBGPR(" packet->mss=%u\n", packet->mss);
  966. /* Update the number of packets that will ultimately be transmitted
  967. * along with the extra bytes for each extra packet
  968. */
  969. packet->tx_packets = skb_shinfo(skb)->gso_segs;
  970. packet->tx_bytes += (packet->tx_packets - 1) * packet->header_len;
  971. return 0;
  972. }
  973. static int xgbe_is_tso(struct sk_buff *skb)
  974. {
  975. if (skb->ip_summed != CHECKSUM_PARTIAL)
  976. return 0;
  977. if (!skb_is_gso(skb))
  978. return 0;
  979. DBGPR(" TSO packet to be processed\n");
  980. return 1;
  981. }
  982. static void xgbe_packet_info(struct xgbe_prv_data *pdata,
  983. struct xgbe_ring *ring, struct sk_buff *skb,
  984. struct xgbe_packet_data *packet)
  985. {
  986. struct skb_frag_struct *frag;
  987. unsigned int context_desc;
  988. unsigned int len;
  989. unsigned int i;
  990. packet->skb = skb;
  991. context_desc = 0;
  992. packet->rdesc_count = 0;
  993. packet->tx_packets = 1;
  994. packet->tx_bytes = skb->len;
  995. if (xgbe_is_tso(skb)) {
  996. /* TSO requires an extra descriptor if mss is different */
  997. if (skb_shinfo(skb)->gso_size != ring->tx.cur_mss) {
  998. context_desc = 1;
  999. packet->rdesc_count++;
  1000. }
  1001. /* TSO requires an extra descriptor for TSO header */
  1002. packet->rdesc_count++;
  1003. XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1004. TSO_ENABLE, 1);
  1005. XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1006. CSUM_ENABLE, 1);
  1007. } else if (skb->ip_summed == CHECKSUM_PARTIAL)
  1008. XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1009. CSUM_ENABLE, 1);
  1010. if (skb_vlan_tag_present(skb)) {
  1011. /* VLAN requires an extra descriptor if tag is different */
  1012. if (skb_vlan_tag_get(skb) != ring->tx.cur_vlan_ctag)
  1013. /* We can share with the TSO context descriptor */
  1014. if (!context_desc) {
  1015. context_desc = 1;
  1016. packet->rdesc_count++;
  1017. }
  1018. XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1019. VLAN_CTAG, 1);
  1020. }
  1021. if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
  1022. (pdata->tstamp_config.tx_type == HWTSTAMP_TX_ON))
  1023. XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
  1024. PTP, 1);
  1025. for (len = skb_headlen(skb); len;) {
  1026. packet->rdesc_count++;
  1027. len -= min_t(unsigned int, len, XGBE_TX_MAX_BUF_SIZE);
  1028. }
  1029. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1030. frag = &skb_shinfo(skb)->frags[i];
  1031. for (len = skb_frag_size(frag); len; ) {
  1032. packet->rdesc_count++;
  1033. len -= min_t(unsigned int, len, XGBE_TX_MAX_BUF_SIZE);
  1034. }
  1035. }
  1036. }
  1037. static int xgbe_open(struct net_device *netdev)
  1038. {
  1039. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1040. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  1041. int ret;
  1042. DBGPR("-->xgbe_open\n");
  1043. /* Initialize the phy */
  1044. ret = xgbe_phy_init(pdata);
  1045. if (ret)
  1046. return ret;
  1047. /* Enable the clocks */
  1048. ret = clk_prepare_enable(pdata->sysclk);
  1049. if (ret) {
  1050. netdev_alert(netdev, "dma clk_prepare_enable failed\n");
  1051. return ret;
  1052. }
  1053. ret = clk_prepare_enable(pdata->ptpclk);
  1054. if (ret) {
  1055. netdev_alert(netdev, "ptp clk_prepare_enable failed\n");
  1056. goto err_sysclk;
  1057. }
  1058. /* Calculate the Rx buffer size before allocating rings */
  1059. ret = xgbe_calc_rx_buf_size(netdev, netdev->mtu);
  1060. if (ret < 0)
  1061. goto err_ptpclk;
  1062. pdata->rx_buf_size = ret;
  1063. /* Allocate the channel and ring structures */
  1064. ret = xgbe_alloc_channels(pdata);
  1065. if (ret)
  1066. goto err_ptpclk;
  1067. /* Allocate the ring descriptors and buffers */
  1068. ret = desc_if->alloc_ring_resources(pdata);
  1069. if (ret)
  1070. goto err_channels;
  1071. INIT_WORK(&pdata->service_work, xgbe_service);
  1072. INIT_WORK(&pdata->restart_work, xgbe_restart);
  1073. INIT_WORK(&pdata->tx_tstamp_work, xgbe_tx_tstamp);
  1074. xgbe_init_timers(pdata);
  1075. ret = xgbe_start(pdata);
  1076. if (ret)
  1077. goto err_rings;
  1078. clear_bit(XGBE_DOWN, &pdata->dev_state);
  1079. DBGPR("<--xgbe_open\n");
  1080. return 0;
  1081. err_rings:
  1082. desc_if->free_ring_resources(pdata);
  1083. err_channels:
  1084. xgbe_free_channels(pdata);
  1085. err_ptpclk:
  1086. clk_disable_unprepare(pdata->ptpclk);
  1087. err_sysclk:
  1088. clk_disable_unprepare(pdata->sysclk);
  1089. return ret;
  1090. }
  1091. static int xgbe_close(struct net_device *netdev)
  1092. {
  1093. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1094. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  1095. DBGPR("-->xgbe_close\n");
  1096. /* Stop the device */
  1097. xgbe_stop(pdata);
  1098. /* Free the ring descriptors and buffers */
  1099. desc_if->free_ring_resources(pdata);
  1100. /* Free the channel and ring structures */
  1101. xgbe_free_channels(pdata);
  1102. /* Disable the clocks */
  1103. clk_disable_unprepare(pdata->ptpclk);
  1104. clk_disable_unprepare(pdata->sysclk);
  1105. set_bit(XGBE_DOWN, &pdata->dev_state);
  1106. DBGPR("<--xgbe_close\n");
  1107. return 0;
  1108. }
  1109. static int xgbe_xmit(struct sk_buff *skb, struct net_device *netdev)
  1110. {
  1111. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1112. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1113. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  1114. struct xgbe_channel *channel;
  1115. struct xgbe_ring *ring;
  1116. struct xgbe_packet_data *packet;
  1117. struct netdev_queue *txq;
  1118. int ret;
  1119. DBGPR("-->xgbe_xmit: skb->len = %d\n", skb->len);
  1120. channel = pdata->channel + skb->queue_mapping;
  1121. txq = netdev_get_tx_queue(netdev, channel->queue_index);
  1122. ring = channel->tx_ring;
  1123. packet = &ring->packet_data;
  1124. ret = NETDEV_TX_OK;
  1125. if (skb->len == 0) {
  1126. netif_err(pdata, tx_err, netdev,
  1127. "empty skb received from stack\n");
  1128. dev_kfree_skb_any(skb);
  1129. goto tx_netdev_return;
  1130. }
  1131. /* Calculate preliminary packet info */
  1132. memset(packet, 0, sizeof(*packet));
  1133. xgbe_packet_info(pdata, ring, skb, packet);
  1134. /* Check that there are enough descriptors available */
  1135. ret = xgbe_maybe_stop_tx_queue(channel, ring, packet->rdesc_count);
  1136. if (ret)
  1137. goto tx_netdev_return;
  1138. ret = xgbe_prep_tso(skb, packet);
  1139. if (ret) {
  1140. netif_err(pdata, tx_err, netdev,
  1141. "error processing TSO packet\n");
  1142. dev_kfree_skb_any(skb);
  1143. goto tx_netdev_return;
  1144. }
  1145. xgbe_prep_vlan(skb, packet);
  1146. if (!desc_if->map_tx_skb(channel, skb)) {
  1147. dev_kfree_skb_any(skb);
  1148. goto tx_netdev_return;
  1149. }
  1150. xgbe_prep_tx_tstamp(pdata, skb, packet);
  1151. /* Report on the actual number of bytes (to be) sent */
  1152. netdev_tx_sent_queue(txq, packet->tx_bytes);
  1153. /* Configure required descriptor fields for transmission */
  1154. hw_if->dev_xmit(channel);
  1155. if (netif_msg_pktdata(pdata))
  1156. xgbe_print_pkt(netdev, skb, true);
  1157. /* Stop the queue in advance if there may not be enough descriptors */
  1158. xgbe_maybe_stop_tx_queue(channel, ring, XGBE_TX_MAX_DESCS);
  1159. ret = NETDEV_TX_OK;
  1160. tx_netdev_return:
  1161. return ret;
  1162. }
  1163. static void xgbe_set_rx_mode(struct net_device *netdev)
  1164. {
  1165. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1166. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1167. DBGPR("-->xgbe_set_rx_mode\n");
  1168. hw_if->config_rx_mode(pdata);
  1169. DBGPR("<--xgbe_set_rx_mode\n");
  1170. }
  1171. static int xgbe_set_mac_address(struct net_device *netdev, void *addr)
  1172. {
  1173. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1174. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1175. struct sockaddr *saddr = addr;
  1176. DBGPR("-->xgbe_set_mac_address\n");
  1177. if (!is_valid_ether_addr(saddr->sa_data))
  1178. return -EADDRNOTAVAIL;
  1179. memcpy(netdev->dev_addr, saddr->sa_data, netdev->addr_len);
  1180. hw_if->set_mac_address(pdata, netdev->dev_addr);
  1181. DBGPR("<--xgbe_set_mac_address\n");
  1182. return 0;
  1183. }
  1184. static int xgbe_ioctl(struct net_device *netdev, struct ifreq *ifreq, int cmd)
  1185. {
  1186. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1187. int ret;
  1188. switch (cmd) {
  1189. case SIOCGHWTSTAMP:
  1190. ret = xgbe_get_hwtstamp_settings(pdata, ifreq);
  1191. break;
  1192. case SIOCSHWTSTAMP:
  1193. ret = xgbe_set_hwtstamp_settings(pdata, ifreq);
  1194. break;
  1195. default:
  1196. ret = -EOPNOTSUPP;
  1197. }
  1198. return ret;
  1199. }
  1200. static int xgbe_change_mtu(struct net_device *netdev, int mtu)
  1201. {
  1202. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1203. int ret;
  1204. DBGPR("-->xgbe_change_mtu\n");
  1205. ret = xgbe_calc_rx_buf_size(netdev, mtu);
  1206. if (ret < 0)
  1207. return ret;
  1208. pdata->rx_buf_size = ret;
  1209. netdev->mtu = mtu;
  1210. xgbe_restart_dev(pdata);
  1211. DBGPR("<--xgbe_change_mtu\n");
  1212. return 0;
  1213. }
  1214. static void xgbe_tx_timeout(struct net_device *netdev)
  1215. {
  1216. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1217. netdev_warn(netdev, "tx timeout, device restarting\n");
  1218. schedule_work(&pdata->restart_work);
  1219. }
  1220. static struct rtnl_link_stats64 *xgbe_get_stats64(struct net_device *netdev,
  1221. struct rtnl_link_stats64 *s)
  1222. {
  1223. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1224. struct xgbe_mmc_stats *pstats = &pdata->mmc_stats;
  1225. DBGPR("-->%s\n", __func__);
  1226. pdata->hw_if.read_mmc_stats(pdata);
  1227. s->rx_packets = pstats->rxframecount_gb;
  1228. s->rx_bytes = pstats->rxoctetcount_gb;
  1229. s->rx_errors = pstats->rxframecount_gb -
  1230. pstats->rxbroadcastframes_g -
  1231. pstats->rxmulticastframes_g -
  1232. pstats->rxunicastframes_g;
  1233. s->multicast = pstats->rxmulticastframes_g;
  1234. s->rx_length_errors = pstats->rxlengtherror;
  1235. s->rx_crc_errors = pstats->rxcrcerror;
  1236. s->rx_fifo_errors = pstats->rxfifooverflow;
  1237. s->tx_packets = pstats->txframecount_gb;
  1238. s->tx_bytes = pstats->txoctetcount_gb;
  1239. s->tx_errors = pstats->txframecount_gb - pstats->txframecount_g;
  1240. s->tx_dropped = netdev->stats.tx_dropped;
  1241. DBGPR("<--%s\n", __func__);
  1242. return s;
  1243. }
  1244. static int xgbe_vlan_rx_add_vid(struct net_device *netdev, __be16 proto,
  1245. u16 vid)
  1246. {
  1247. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1248. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1249. DBGPR("-->%s\n", __func__);
  1250. set_bit(vid, pdata->active_vlans);
  1251. hw_if->update_vlan_hash_table(pdata);
  1252. DBGPR("<--%s\n", __func__);
  1253. return 0;
  1254. }
  1255. static int xgbe_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto,
  1256. u16 vid)
  1257. {
  1258. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1259. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1260. DBGPR("-->%s\n", __func__);
  1261. clear_bit(vid, pdata->active_vlans);
  1262. hw_if->update_vlan_hash_table(pdata);
  1263. DBGPR("<--%s\n", __func__);
  1264. return 0;
  1265. }
  1266. #ifdef CONFIG_NET_POLL_CONTROLLER
  1267. static void xgbe_poll_controller(struct net_device *netdev)
  1268. {
  1269. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1270. struct xgbe_channel *channel;
  1271. unsigned int i;
  1272. DBGPR("-->xgbe_poll_controller\n");
  1273. if (pdata->per_channel_irq) {
  1274. channel = pdata->channel;
  1275. for (i = 0; i < pdata->channel_count; i++, channel++)
  1276. xgbe_dma_isr(channel->dma_irq, channel);
  1277. } else {
  1278. disable_irq(pdata->dev_irq);
  1279. xgbe_isr(pdata->dev_irq, pdata);
  1280. enable_irq(pdata->dev_irq);
  1281. }
  1282. DBGPR("<--xgbe_poll_controller\n");
  1283. }
  1284. #endif /* End CONFIG_NET_POLL_CONTROLLER */
  1285. static int xgbe_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
  1286. struct tc_to_netdev *tc_to_netdev)
  1287. {
  1288. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1289. u8 tc;
  1290. if (tc_to_netdev->type != TC_SETUP_MQPRIO)
  1291. return -EINVAL;
  1292. tc = tc_to_netdev->tc;
  1293. if (tc > pdata->hw_feat.tc_cnt)
  1294. return -EINVAL;
  1295. pdata->num_tcs = tc;
  1296. pdata->hw_if.config_tc(pdata);
  1297. return 0;
  1298. }
  1299. static int xgbe_set_features(struct net_device *netdev,
  1300. netdev_features_t features)
  1301. {
  1302. struct xgbe_prv_data *pdata = netdev_priv(netdev);
  1303. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1304. netdev_features_t rxhash, rxcsum, rxvlan, rxvlan_filter;
  1305. int ret = 0;
  1306. rxhash = pdata->netdev_features & NETIF_F_RXHASH;
  1307. rxcsum = pdata->netdev_features & NETIF_F_RXCSUM;
  1308. rxvlan = pdata->netdev_features & NETIF_F_HW_VLAN_CTAG_RX;
  1309. rxvlan_filter = pdata->netdev_features & NETIF_F_HW_VLAN_CTAG_FILTER;
  1310. if ((features & NETIF_F_RXHASH) && !rxhash)
  1311. ret = hw_if->enable_rss(pdata);
  1312. else if (!(features & NETIF_F_RXHASH) && rxhash)
  1313. ret = hw_if->disable_rss(pdata);
  1314. if (ret)
  1315. return ret;
  1316. if ((features & NETIF_F_RXCSUM) && !rxcsum)
  1317. hw_if->enable_rx_csum(pdata);
  1318. else if (!(features & NETIF_F_RXCSUM) && rxcsum)
  1319. hw_if->disable_rx_csum(pdata);
  1320. if ((features & NETIF_F_HW_VLAN_CTAG_RX) && !rxvlan)
  1321. hw_if->enable_rx_vlan_stripping(pdata);
  1322. else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) && rxvlan)
  1323. hw_if->disable_rx_vlan_stripping(pdata);
  1324. if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) && !rxvlan_filter)
  1325. hw_if->enable_rx_vlan_filtering(pdata);
  1326. else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) && rxvlan_filter)
  1327. hw_if->disable_rx_vlan_filtering(pdata);
  1328. pdata->netdev_features = features;
  1329. DBGPR("<--xgbe_set_features\n");
  1330. return 0;
  1331. }
  1332. static const struct net_device_ops xgbe_netdev_ops = {
  1333. .ndo_open = xgbe_open,
  1334. .ndo_stop = xgbe_close,
  1335. .ndo_start_xmit = xgbe_xmit,
  1336. .ndo_set_rx_mode = xgbe_set_rx_mode,
  1337. .ndo_set_mac_address = xgbe_set_mac_address,
  1338. .ndo_validate_addr = eth_validate_addr,
  1339. .ndo_do_ioctl = xgbe_ioctl,
  1340. .ndo_change_mtu = xgbe_change_mtu,
  1341. .ndo_tx_timeout = xgbe_tx_timeout,
  1342. .ndo_get_stats64 = xgbe_get_stats64,
  1343. .ndo_vlan_rx_add_vid = xgbe_vlan_rx_add_vid,
  1344. .ndo_vlan_rx_kill_vid = xgbe_vlan_rx_kill_vid,
  1345. #ifdef CONFIG_NET_POLL_CONTROLLER
  1346. .ndo_poll_controller = xgbe_poll_controller,
  1347. #endif
  1348. .ndo_setup_tc = xgbe_setup_tc,
  1349. .ndo_set_features = xgbe_set_features,
  1350. };
  1351. const struct net_device_ops *xgbe_get_netdev_ops(void)
  1352. {
  1353. return &xgbe_netdev_ops;
  1354. }
  1355. static void xgbe_rx_refresh(struct xgbe_channel *channel)
  1356. {
  1357. struct xgbe_prv_data *pdata = channel->pdata;
  1358. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1359. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  1360. struct xgbe_ring *ring = channel->rx_ring;
  1361. struct xgbe_ring_data *rdata;
  1362. while (ring->dirty != ring->cur) {
  1363. rdata = XGBE_GET_DESC_DATA(ring, ring->dirty);
  1364. /* Reset rdata values */
  1365. desc_if->unmap_rdata(pdata, rdata);
  1366. if (desc_if->map_rx_buffer(pdata, ring, rdata))
  1367. break;
  1368. hw_if->rx_desc_reset(pdata, rdata, ring->dirty);
  1369. ring->dirty++;
  1370. }
  1371. /* Make sure everything is written before the register write */
  1372. wmb();
  1373. /* Update the Rx Tail Pointer Register with address of
  1374. * the last cleaned entry */
  1375. rdata = XGBE_GET_DESC_DATA(ring, ring->dirty - 1);
  1376. XGMAC_DMA_IOWRITE(channel, DMA_CH_RDTR_LO,
  1377. lower_32_bits(rdata->rdesc_dma));
  1378. }
  1379. static struct sk_buff *xgbe_create_skb(struct xgbe_prv_data *pdata,
  1380. struct napi_struct *napi,
  1381. struct xgbe_ring_data *rdata,
  1382. unsigned int len)
  1383. {
  1384. struct sk_buff *skb;
  1385. u8 *packet;
  1386. skb = napi_alloc_skb(napi, rdata->rx.hdr.dma_len);
  1387. if (!skb)
  1388. return NULL;
  1389. /* Pull in the header buffer which may contain just the header
  1390. * or the header plus data
  1391. */
  1392. dma_sync_single_range_for_cpu(pdata->dev, rdata->rx.hdr.dma_base,
  1393. rdata->rx.hdr.dma_off,
  1394. rdata->rx.hdr.dma_len, DMA_FROM_DEVICE);
  1395. packet = page_address(rdata->rx.hdr.pa.pages) +
  1396. rdata->rx.hdr.pa.pages_offset;
  1397. skb_copy_to_linear_data(skb, packet, len);
  1398. skb_put(skb, len);
  1399. return skb;
  1400. }
  1401. static unsigned int xgbe_rx_buf1_len(struct xgbe_ring_data *rdata,
  1402. struct xgbe_packet_data *packet)
  1403. {
  1404. /* Always zero if not the first descriptor */
  1405. if (!XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, FIRST))
  1406. return 0;
  1407. /* First descriptor with split header, return header length */
  1408. if (rdata->rx.hdr_len)
  1409. return rdata->rx.hdr_len;
  1410. /* First descriptor but not the last descriptor and no split header,
  1411. * so the full buffer was used
  1412. */
  1413. if (!XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, LAST))
  1414. return rdata->rx.hdr.dma_len;
  1415. /* First descriptor and last descriptor and no split header, so
  1416. * calculate how much of the buffer was used
  1417. */
  1418. return min_t(unsigned int, rdata->rx.hdr.dma_len, rdata->rx.len);
  1419. }
  1420. static unsigned int xgbe_rx_buf2_len(struct xgbe_ring_data *rdata,
  1421. struct xgbe_packet_data *packet,
  1422. unsigned int len)
  1423. {
  1424. /* Always the full buffer if not the last descriptor */
  1425. if (!XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, LAST))
  1426. return rdata->rx.buf.dma_len;
  1427. /* Last descriptor so calculate how much of the buffer was used
  1428. * for the last bit of data
  1429. */
  1430. return rdata->rx.len - len;
  1431. }
  1432. static int xgbe_tx_poll(struct xgbe_channel *channel)
  1433. {
  1434. struct xgbe_prv_data *pdata = channel->pdata;
  1435. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1436. struct xgbe_desc_if *desc_if = &pdata->desc_if;
  1437. struct xgbe_ring *ring = channel->tx_ring;
  1438. struct xgbe_ring_data *rdata;
  1439. struct xgbe_ring_desc *rdesc;
  1440. struct net_device *netdev = pdata->netdev;
  1441. struct netdev_queue *txq;
  1442. int processed = 0;
  1443. unsigned int tx_packets = 0, tx_bytes = 0;
  1444. unsigned int cur;
  1445. DBGPR("-->xgbe_tx_poll\n");
  1446. /* Nothing to do if there isn't a Tx ring for this channel */
  1447. if (!ring)
  1448. return 0;
  1449. cur = ring->cur;
  1450. /* Be sure we get ring->cur before accessing descriptor data */
  1451. smp_rmb();
  1452. txq = netdev_get_tx_queue(netdev, channel->queue_index);
  1453. while ((processed < XGBE_TX_DESC_MAX_PROC) &&
  1454. (ring->dirty != cur)) {
  1455. rdata = XGBE_GET_DESC_DATA(ring, ring->dirty);
  1456. rdesc = rdata->rdesc;
  1457. if (!hw_if->tx_complete(rdesc))
  1458. break;
  1459. /* Make sure descriptor fields are read after reading the OWN
  1460. * bit */
  1461. dma_rmb();
  1462. if (netif_msg_tx_done(pdata))
  1463. xgbe_dump_tx_desc(pdata, ring, ring->dirty, 1, 0);
  1464. if (hw_if->is_last_desc(rdesc)) {
  1465. tx_packets += rdata->tx.packets;
  1466. tx_bytes += rdata->tx.bytes;
  1467. }
  1468. /* Free the SKB and reset the descriptor for re-use */
  1469. desc_if->unmap_rdata(pdata, rdata);
  1470. hw_if->tx_desc_reset(rdata);
  1471. processed++;
  1472. ring->dirty++;
  1473. }
  1474. if (!processed)
  1475. return 0;
  1476. netdev_tx_completed_queue(txq, tx_packets, tx_bytes);
  1477. if ((ring->tx.queue_stopped == 1) &&
  1478. (xgbe_tx_avail_desc(ring) > XGBE_TX_DESC_MIN_FREE)) {
  1479. ring->tx.queue_stopped = 0;
  1480. netif_tx_wake_queue(txq);
  1481. }
  1482. DBGPR("<--xgbe_tx_poll: processed=%d\n", processed);
  1483. return processed;
  1484. }
  1485. static int xgbe_rx_poll(struct xgbe_channel *channel, int budget)
  1486. {
  1487. struct xgbe_prv_data *pdata = channel->pdata;
  1488. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  1489. struct xgbe_ring *ring = channel->rx_ring;
  1490. struct xgbe_ring_data *rdata;
  1491. struct xgbe_packet_data *packet;
  1492. struct net_device *netdev = pdata->netdev;
  1493. struct napi_struct *napi;
  1494. struct sk_buff *skb;
  1495. struct skb_shared_hwtstamps *hwtstamps;
  1496. unsigned int last, error, context_next, context;
  1497. unsigned int len, buf1_len, buf2_len, max_len;
  1498. unsigned int received = 0;
  1499. int packet_count = 0;
  1500. DBGPR("-->xgbe_rx_poll: budget=%d\n", budget);
  1501. /* Nothing to do if there isn't a Rx ring for this channel */
  1502. if (!ring)
  1503. return 0;
  1504. last = 0;
  1505. context_next = 0;
  1506. napi = (pdata->per_channel_irq) ? &channel->napi : &pdata->napi;
  1507. rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
  1508. packet = &ring->packet_data;
  1509. while (packet_count < budget) {
  1510. DBGPR(" cur = %d\n", ring->cur);
  1511. /* First time in loop see if we need to restore state */
  1512. if (!received && rdata->state_saved) {
  1513. skb = rdata->state.skb;
  1514. error = rdata->state.error;
  1515. len = rdata->state.len;
  1516. } else {
  1517. memset(packet, 0, sizeof(*packet));
  1518. skb = NULL;
  1519. error = 0;
  1520. len = 0;
  1521. }
  1522. read_again:
  1523. rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
  1524. if (xgbe_rx_dirty_desc(ring) > (XGBE_RX_DESC_CNT >> 3))
  1525. xgbe_rx_refresh(channel);
  1526. if (hw_if->dev_read(channel))
  1527. break;
  1528. received++;
  1529. ring->cur++;
  1530. last = XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
  1531. LAST);
  1532. context_next = XGMAC_GET_BITS(packet->attributes,
  1533. RX_PACKET_ATTRIBUTES,
  1534. CONTEXT_NEXT);
  1535. context = XGMAC_GET_BITS(packet->attributes,
  1536. RX_PACKET_ATTRIBUTES,
  1537. CONTEXT);
  1538. /* Earlier error, just drain the remaining data */
  1539. if ((!last || context_next) && error)
  1540. goto read_again;
  1541. if (error || packet->errors) {
  1542. if (packet->errors)
  1543. netif_err(pdata, rx_err, netdev,
  1544. "error in received packet\n");
  1545. dev_kfree_skb(skb);
  1546. goto next_packet;
  1547. }
  1548. if (!context) {
  1549. /* Get the data length in the descriptor buffers */
  1550. buf1_len = xgbe_rx_buf1_len(rdata, packet);
  1551. len += buf1_len;
  1552. buf2_len = xgbe_rx_buf2_len(rdata, packet, len);
  1553. len += buf2_len;
  1554. if (!skb) {
  1555. skb = xgbe_create_skb(pdata, napi, rdata,
  1556. buf1_len);
  1557. if (!skb) {
  1558. error = 1;
  1559. goto skip_data;
  1560. }
  1561. }
  1562. if (buf2_len) {
  1563. dma_sync_single_range_for_cpu(pdata->dev,
  1564. rdata->rx.buf.dma_base,
  1565. rdata->rx.buf.dma_off,
  1566. rdata->rx.buf.dma_len,
  1567. DMA_FROM_DEVICE);
  1568. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
  1569. rdata->rx.buf.pa.pages,
  1570. rdata->rx.buf.pa.pages_offset,
  1571. buf2_len,
  1572. rdata->rx.buf.dma_len);
  1573. rdata->rx.buf.pa.pages = NULL;
  1574. }
  1575. }
  1576. skip_data:
  1577. if (!last || context_next)
  1578. goto read_again;
  1579. if (!skb)
  1580. goto next_packet;
  1581. /* Be sure we don't exceed the configured MTU */
  1582. max_len = netdev->mtu + ETH_HLEN;
  1583. if (!(netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
  1584. (skb->protocol == htons(ETH_P_8021Q)))
  1585. max_len += VLAN_HLEN;
  1586. if (skb->len > max_len) {
  1587. netif_err(pdata, rx_err, netdev,
  1588. "packet length exceeds configured MTU\n");
  1589. dev_kfree_skb(skb);
  1590. goto next_packet;
  1591. }
  1592. if (netif_msg_pktdata(pdata))
  1593. xgbe_print_pkt(netdev, skb, false);
  1594. skb_checksum_none_assert(skb);
  1595. if (XGMAC_GET_BITS(packet->attributes,
  1596. RX_PACKET_ATTRIBUTES, CSUM_DONE))
  1597. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1598. if (XGMAC_GET_BITS(packet->attributes,
  1599. RX_PACKET_ATTRIBUTES, VLAN_CTAG))
  1600. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  1601. packet->vlan_ctag);
  1602. if (XGMAC_GET_BITS(packet->attributes,
  1603. RX_PACKET_ATTRIBUTES, RX_TSTAMP)) {
  1604. u64 nsec;
  1605. nsec = timecounter_cyc2time(&pdata->tstamp_tc,
  1606. packet->rx_tstamp);
  1607. hwtstamps = skb_hwtstamps(skb);
  1608. hwtstamps->hwtstamp = ns_to_ktime(nsec);
  1609. }
  1610. if (XGMAC_GET_BITS(packet->attributes,
  1611. RX_PACKET_ATTRIBUTES, RSS_HASH))
  1612. skb_set_hash(skb, packet->rss_hash,
  1613. packet->rss_hash_type);
  1614. skb->dev = netdev;
  1615. skb->protocol = eth_type_trans(skb, netdev);
  1616. skb_record_rx_queue(skb, channel->queue_index);
  1617. napi_gro_receive(napi, skb);
  1618. next_packet:
  1619. packet_count++;
  1620. }
  1621. /* Check if we need to save state before leaving */
  1622. if (received && (!last || context_next)) {
  1623. rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
  1624. rdata->state_saved = 1;
  1625. rdata->state.skb = skb;
  1626. rdata->state.len = len;
  1627. rdata->state.error = error;
  1628. }
  1629. DBGPR("<--xgbe_rx_poll: packet_count = %d\n", packet_count);
  1630. return packet_count;
  1631. }
  1632. static int xgbe_one_poll(struct napi_struct *napi, int budget)
  1633. {
  1634. struct xgbe_channel *channel = container_of(napi, struct xgbe_channel,
  1635. napi);
  1636. int processed = 0;
  1637. DBGPR("-->xgbe_one_poll: budget=%d\n", budget);
  1638. /* Cleanup Tx ring first */
  1639. xgbe_tx_poll(channel);
  1640. /* Process Rx ring next */
  1641. processed = xgbe_rx_poll(channel, budget);
  1642. /* If we processed everything, we are done */
  1643. if (processed < budget) {
  1644. /* Turn off polling */
  1645. napi_complete_done(napi, processed);
  1646. /* Enable Tx and Rx interrupts */
  1647. enable_irq(channel->dma_irq);
  1648. }
  1649. DBGPR("<--xgbe_one_poll: received = %d\n", processed);
  1650. return processed;
  1651. }
  1652. static int xgbe_all_poll(struct napi_struct *napi, int budget)
  1653. {
  1654. struct xgbe_prv_data *pdata = container_of(napi, struct xgbe_prv_data,
  1655. napi);
  1656. struct xgbe_channel *channel;
  1657. int ring_budget;
  1658. int processed, last_processed;
  1659. unsigned int i;
  1660. DBGPR("-->xgbe_all_poll: budget=%d\n", budget);
  1661. processed = 0;
  1662. ring_budget = budget / pdata->rx_ring_count;
  1663. do {
  1664. last_processed = processed;
  1665. channel = pdata->channel;
  1666. for (i = 0; i < pdata->channel_count; i++, channel++) {
  1667. /* Cleanup Tx ring first */
  1668. xgbe_tx_poll(channel);
  1669. /* Process Rx ring next */
  1670. if (ring_budget > (budget - processed))
  1671. ring_budget = budget - processed;
  1672. processed += xgbe_rx_poll(channel, ring_budget);
  1673. }
  1674. } while ((processed < budget) && (processed != last_processed));
  1675. /* If we processed everything, we are done */
  1676. if (processed < budget) {
  1677. /* Turn off polling */
  1678. napi_complete_done(napi, processed);
  1679. /* Enable Tx and Rx interrupts */
  1680. xgbe_enable_rx_tx_ints(pdata);
  1681. }
  1682. DBGPR("<--xgbe_all_poll: received = %d\n", processed);
  1683. return processed;
  1684. }
  1685. void xgbe_dump_tx_desc(struct xgbe_prv_data *pdata, struct xgbe_ring *ring,
  1686. unsigned int idx, unsigned int count, unsigned int flag)
  1687. {
  1688. struct xgbe_ring_data *rdata;
  1689. struct xgbe_ring_desc *rdesc;
  1690. while (count--) {
  1691. rdata = XGBE_GET_DESC_DATA(ring, idx);
  1692. rdesc = rdata->rdesc;
  1693. netdev_dbg(pdata->netdev,
  1694. "TX_NORMAL_DESC[%d %s] = %08x:%08x:%08x:%08x\n", idx,
  1695. (flag == 1) ? "QUEUED FOR TX" : "TX BY DEVICE",
  1696. le32_to_cpu(rdesc->desc0),
  1697. le32_to_cpu(rdesc->desc1),
  1698. le32_to_cpu(rdesc->desc2),
  1699. le32_to_cpu(rdesc->desc3));
  1700. idx++;
  1701. }
  1702. }
  1703. void xgbe_dump_rx_desc(struct xgbe_prv_data *pdata, struct xgbe_ring *ring,
  1704. unsigned int idx)
  1705. {
  1706. struct xgbe_ring_data *rdata;
  1707. struct xgbe_ring_desc *rdesc;
  1708. rdata = XGBE_GET_DESC_DATA(ring, idx);
  1709. rdesc = rdata->rdesc;
  1710. netdev_dbg(pdata->netdev,
  1711. "RX_NORMAL_DESC[%d RX BY DEVICE] = %08x:%08x:%08x:%08x\n",
  1712. idx, le32_to_cpu(rdesc->desc0), le32_to_cpu(rdesc->desc1),
  1713. le32_to_cpu(rdesc->desc2), le32_to_cpu(rdesc->desc3));
  1714. }
  1715. void xgbe_print_pkt(struct net_device *netdev, struct sk_buff *skb, bool tx_rx)
  1716. {
  1717. struct ethhdr *eth = (struct ethhdr *)skb->data;
  1718. unsigned char *buf = skb->data;
  1719. unsigned char buffer[128];
  1720. unsigned int i, j;
  1721. netdev_dbg(netdev, "\n************** SKB dump ****************\n");
  1722. netdev_dbg(netdev, "%s packet of %d bytes\n",
  1723. (tx_rx ? "TX" : "RX"), skb->len);
  1724. netdev_dbg(netdev, "Dst MAC addr: %pM\n", eth->h_dest);
  1725. netdev_dbg(netdev, "Src MAC addr: %pM\n", eth->h_source);
  1726. netdev_dbg(netdev, "Protocol: %#06hx\n", ntohs(eth->h_proto));
  1727. for (i = 0, j = 0; i < skb->len;) {
  1728. j += snprintf(buffer + j, sizeof(buffer) - j, "%02hhx",
  1729. buf[i++]);
  1730. if ((i % 32) == 0) {
  1731. netdev_dbg(netdev, " %#06x: %s\n", i - 32, buffer);
  1732. j = 0;
  1733. } else if ((i % 16) == 0) {
  1734. buffer[j++] = ' ';
  1735. buffer[j++] = ' ';
  1736. } else if ((i % 4) == 0) {
  1737. buffer[j++] = ' ';
  1738. }
  1739. }
  1740. if (i % 32)
  1741. netdev_dbg(netdev, " %#06x: %s\n", i - (i % 32), buffer);
  1742. netdev_dbg(netdev, "\n************** SKB dump ****************\n");
  1743. }