ll_temac_main.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for Xilinx TEMAC Ethernet device
  4. *
  5. * Copyright (c) 2008 Nissin Systems Co., Ltd., Yoshio Kashiwagi
  6. * Copyright (c) 2005-2008 DLA Systems, David H. Lynch Jr. <dhlii@dlasys.net>
  7. * Copyright (c) 2008-2009 Secret Lab Technologies Ltd.
  8. *
  9. * This is a driver for the Xilinx ll_temac ipcore which is often used
  10. * in the Virtex and Spartan series of chips.
  11. *
  12. * Notes:
  13. * - The ll_temac hardware uses indirect access for many of the TEMAC
  14. * registers, include the MDIO bus. However, indirect access to MDIO
  15. * registers take considerably more clock cycles than to TEMAC registers.
  16. * MDIO accesses are long, so threads doing them should probably sleep
  17. * rather than busywait. However, since only one indirect access can be
  18. * in progress at any given time, that means that *all* indirect accesses
  19. * could end up sleeping (to wait for an MDIO access to complete).
  20. * Fortunately none of the indirect accesses are on the 'hot' path for tx
  21. * or rx, so this should be okay.
  22. *
  23. * TODO:
  24. * - Factor out locallink DMA code into separate driver
  25. * - Fix support for hardware checksumming.
  26. * - Testing. Lots and lots of testing.
  27. *
  28. */
  29. #include <linux/delay.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/mii.h>
  32. #include <linux/module.h>
  33. #include <linux/mutex.h>
  34. #include <linux/netdevice.h>
  35. #include <linux/if_ether.h>
  36. #include <linux/of.h>
  37. #include <linux/of_irq.h>
  38. #include <linux/of_mdio.h>
  39. #include <linux/of_net.h>
  40. #include <linux/platform_device.h>
  41. #include <linux/skbuff.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/tcp.h> /* needed for sizeof(tcphdr) */
  44. #include <linux/udp.h> /* needed for sizeof(udphdr) */
  45. #include <linux/phy.h>
  46. #include <linux/in.h>
  47. #include <linux/io.h>
  48. #include <linux/ip.h>
  49. #include <linux/slab.h>
  50. #include <linux/interrupt.h>
  51. #include <linux/workqueue.h>
  52. #include <linux/dma-mapping.h>
  53. #include <linux/processor.h>
  54. #include <linux/platform_data/xilinx-ll-temac.h>
  55. #include "ll_temac.h"
  56. /* Descriptors defines for Tx and Rx DMA */
  57. #define TX_BD_NUM_DEFAULT 64
  58. #define RX_BD_NUM_DEFAULT 1024
  59. #define TX_BD_NUM_MAX 4096
  60. #define RX_BD_NUM_MAX 4096
  61. /* ---------------------------------------------------------------------
  62. * Low level register access functions
  63. */
  64. static u32 _temac_ior_be(struct temac_local *lp, int offset)
  65. {
  66. return ioread32be(lp->regs + offset);
  67. }
  68. static void _temac_iow_be(struct temac_local *lp, int offset, u32 value)
  69. {
  70. return iowrite32be(value, lp->regs + offset);
  71. }
  72. static u32 _temac_ior_le(struct temac_local *lp, int offset)
  73. {
  74. return ioread32(lp->regs + offset);
  75. }
  76. static void _temac_iow_le(struct temac_local *lp, int offset, u32 value)
  77. {
  78. return iowrite32(value, lp->regs + offset);
  79. }
  80. static bool hard_acs_rdy(struct temac_local *lp)
  81. {
  82. return temac_ior(lp, XTE_RDY0_OFFSET) & XTE_RDY0_HARD_ACS_RDY_MASK;
  83. }
  84. static bool hard_acs_rdy_or_timeout(struct temac_local *lp, ktime_t timeout)
  85. {
  86. ktime_t cur = ktime_get();
  87. return hard_acs_rdy(lp) || ktime_after(cur, timeout);
  88. }
  89. /* Poll for maximum 20 ms. This is similar to the 2 jiffies @ 100 Hz
  90. * that was used before, and should cover MDIO bus speed down to 3200
  91. * Hz.
  92. */
  93. #define HARD_ACS_RDY_POLL_NS (20 * NSEC_PER_MSEC)
  94. /*
  95. * temac_indirect_busywait - Wait for current indirect register access
  96. * to complete.
  97. */
  98. int temac_indirect_busywait(struct temac_local *lp)
  99. {
  100. ktime_t timeout = ktime_add_ns(ktime_get(), HARD_ACS_RDY_POLL_NS);
  101. spin_until_cond(hard_acs_rdy_or_timeout(lp, timeout));
  102. if (WARN_ON(!hard_acs_rdy(lp)))
  103. return -ETIMEDOUT;
  104. return 0;
  105. }
  106. /*
  107. * temac_indirect_in32 - Indirect register read access. This function
  108. * must be called without lp->indirect_lock being held.
  109. */
  110. u32 temac_indirect_in32(struct temac_local *lp, int reg)
  111. {
  112. unsigned long flags;
  113. int val;
  114. spin_lock_irqsave(lp->indirect_lock, flags);
  115. val = temac_indirect_in32_locked(lp, reg);
  116. spin_unlock_irqrestore(lp->indirect_lock, flags);
  117. return val;
  118. }
  119. /*
  120. * temac_indirect_in32_locked - Indirect register read access. This
  121. * function must be called with lp->indirect_lock being held. Use
  122. * this together with spin_lock_irqsave/spin_lock_irqrestore to avoid
  123. * repeated lock/unlock and to ensure uninterrupted access to indirect
  124. * registers.
  125. */
  126. u32 temac_indirect_in32_locked(struct temac_local *lp, int reg)
  127. {
  128. /* This initial wait should normally not spin, as we always
  129. * try to wait for indirect access to complete before
  130. * releasing the indirect_lock.
  131. */
  132. if (WARN_ON(temac_indirect_busywait(lp)))
  133. return -ETIMEDOUT;
  134. /* Initiate read from indirect register */
  135. temac_iow(lp, XTE_CTL0_OFFSET, reg);
  136. /* Wait for indirect register access to complete. We really
  137. * should not see timeouts, and could even end up causing
  138. * problem for following indirect access, so let's make a bit
  139. * of WARN noise.
  140. */
  141. if (WARN_ON(temac_indirect_busywait(lp)))
  142. return -ETIMEDOUT;
  143. /* Value is ready now */
  144. return temac_ior(lp, XTE_LSW0_OFFSET);
  145. }
  146. /*
  147. * temac_indirect_out32 - Indirect register write access. This function
  148. * must be called without lp->indirect_lock being held.
  149. */
  150. void temac_indirect_out32(struct temac_local *lp, int reg, u32 value)
  151. {
  152. unsigned long flags;
  153. spin_lock_irqsave(lp->indirect_lock, flags);
  154. temac_indirect_out32_locked(lp, reg, value);
  155. spin_unlock_irqrestore(lp->indirect_lock, flags);
  156. }
  157. /*
  158. * temac_indirect_out32_locked - Indirect register write access. This
  159. * function must be called with lp->indirect_lock being held. Use
  160. * this together with spin_lock_irqsave/spin_lock_irqrestore to avoid
  161. * repeated lock/unlock and to ensure uninterrupted access to indirect
  162. * registers.
  163. */
  164. void temac_indirect_out32_locked(struct temac_local *lp, int reg, u32 value)
  165. {
  166. /* As in temac_indirect_in32_locked(), we should normally not
  167. * spin here. And if it happens, we actually end up silently
  168. * ignoring the write request. Ouch.
  169. */
  170. if (WARN_ON(temac_indirect_busywait(lp)))
  171. return;
  172. /* Initiate write to indirect register */
  173. temac_iow(lp, XTE_LSW0_OFFSET, value);
  174. temac_iow(lp, XTE_CTL0_OFFSET, CNTLREG_WRITE_ENABLE_MASK | reg);
  175. /* As in temac_indirect_in32_locked(), we should not see timeouts
  176. * here. And if it happens, we continue before the write has
  177. * completed. Not good.
  178. */
  179. WARN_ON(temac_indirect_busywait(lp));
  180. }
  181. /*
  182. * temac_dma_in32_* - Memory mapped DMA read, these function expects a
  183. * register input that is based on DCR word addresses which are then
  184. * converted to memory mapped byte addresses. To be assigned to
  185. * lp->dma_in32.
  186. */
  187. static u32 temac_dma_in32_be(struct temac_local *lp, int reg)
  188. {
  189. return ioread32be(lp->sdma_regs + (reg << 2));
  190. }
  191. static u32 temac_dma_in32_le(struct temac_local *lp, int reg)
  192. {
  193. return ioread32(lp->sdma_regs + (reg << 2));
  194. }
  195. /*
  196. * temac_dma_out32_* - Memory mapped DMA read, these function expects
  197. * a register input that is based on DCR word addresses which are then
  198. * converted to memory mapped byte addresses. To be assigned to
  199. * lp->dma_out32.
  200. */
  201. static void temac_dma_out32_be(struct temac_local *lp, int reg, u32 value)
  202. {
  203. iowrite32be(value, lp->sdma_regs + (reg << 2));
  204. }
  205. static void temac_dma_out32_le(struct temac_local *lp, int reg, u32 value)
  206. {
  207. iowrite32(value, lp->sdma_regs + (reg << 2));
  208. }
  209. /* DMA register access functions can be DCR based or memory mapped.
  210. * The PowerPC 440 is DCR based, the PowerPC 405 and MicroBlaze are both
  211. * memory mapped.
  212. */
  213. #ifdef CONFIG_PPC_DCR
  214. /*
  215. * temac_dma_dcr_in32 - DCR based DMA read
  216. */
  217. static u32 temac_dma_dcr_in(struct temac_local *lp, int reg)
  218. {
  219. return dcr_read(lp->sdma_dcrs, reg);
  220. }
  221. /*
  222. * temac_dma_dcr_out32 - DCR based DMA write
  223. */
  224. static void temac_dma_dcr_out(struct temac_local *lp, int reg, u32 value)
  225. {
  226. dcr_write(lp->sdma_dcrs, reg, value);
  227. }
  228. /*
  229. * temac_dcr_setup - If the DMA is DCR based, then setup the address and
  230. * I/O functions
  231. */
  232. static int temac_dcr_setup(struct temac_local *lp, struct platform_device *op,
  233. struct device_node *np)
  234. {
  235. unsigned int dcrs;
  236. /* setup the dcr address mapping if it's in the device tree */
  237. dcrs = dcr_resource_start(np, 0);
  238. if (dcrs != 0) {
  239. lp->sdma_dcrs = dcr_map(np, dcrs, dcr_resource_len(np, 0));
  240. lp->dma_in = temac_dma_dcr_in;
  241. lp->dma_out = temac_dma_dcr_out;
  242. dev_dbg(&op->dev, "DCR base: %x\n", dcrs);
  243. return 0;
  244. }
  245. /* no DCR in the device tree, indicate a failure */
  246. return -1;
  247. }
  248. #else
  249. /*
  250. * temac_dcr_setup - This is a stub for when DCR is not supported,
  251. * such as with MicroBlaze and x86
  252. */
  253. static int temac_dcr_setup(struct temac_local *lp, struct platform_device *op,
  254. struct device_node *np)
  255. {
  256. return -1;
  257. }
  258. #endif
  259. /*
  260. * temac_dma_bd_release - Release buffer descriptor rings
  261. */
  262. static void temac_dma_bd_release(struct net_device *ndev)
  263. {
  264. struct temac_local *lp = netdev_priv(ndev);
  265. int i;
  266. /* Reset Local Link (DMA) */
  267. lp->dma_out(lp, DMA_CONTROL_REG, DMA_CONTROL_RST);
  268. for (i = 0; i < lp->rx_bd_num; i++) {
  269. if (!lp->rx_skb[i])
  270. break;
  271. dma_unmap_single(ndev->dev.parent, lp->rx_bd_v[i].phys,
  272. XTE_MAX_JUMBO_FRAME_SIZE, DMA_FROM_DEVICE);
  273. dev_kfree_skb(lp->rx_skb[i]);
  274. }
  275. if (lp->rx_bd_v)
  276. dma_free_coherent(ndev->dev.parent,
  277. sizeof(*lp->rx_bd_v) * lp->rx_bd_num,
  278. lp->rx_bd_v, lp->rx_bd_p);
  279. if (lp->tx_bd_v)
  280. dma_free_coherent(ndev->dev.parent,
  281. sizeof(*lp->tx_bd_v) * lp->tx_bd_num,
  282. lp->tx_bd_v, lp->tx_bd_p);
  283. }
  284. /*
  285. * temac_dma_bd_init - Setup buffer descriptor rings
  286. */
  287. static int temac_dma_bd_init(struct net_device *ndev)
  288. {
  289. struct temac_local *lp = netdev_priv(ndev);
  290. struct sk_buff *skb;
  291. dma_addr_t skb_dma_addr;
  292. int i;
  293. lp->rx_skb = devm_kcalloc(&ndev->dev, lp->rx_bd_num,
  294. sizeof(*lp->rx_skb), GFP_KERNEL);
  295. if (!lp->rx_skb)
  296. goto out;
  297. /* allocate the tx and rx ring buffer descriptors. */
  298. /* returns a virtual address and a physical address. */
  299. lp->tx_bd_v = dma_alloc_coherent(ndev->dev.parent,
  300. sizeof(*lp->tx_bd_v) * lp->tx_bd_num,
  301. &lp->tx_bd_p, GFP_KERNEL);
  302. if (!lp->tx_bd_v)
  303. goto out;
  304. lp->rx_bd_v = dma_alloc_coherent(ndev->dev.parent,
  305. sizeof(*lp->rx_bd_v) * lp->rx_bd_num,
  306. &lp->rx_bd_p, GFP_KERNEL);
  307. if (!lp->rx_bd_v)
  308. goto out;
  309. for (i = 0; i < lp->tx_bd_num; i++) {
  310. lp->tx_bd_v[i].next = cpu_to_be32(lp->tx_bd_p
  311. + sizeof(*lp->tx_bd_v) * ((i + 1) % lp->tx_bd_num));
  312. }
  313. for (i = 0; i < lp->rx_bd_num; i++) {
  314. lp->rx_bd_v[i].next = cpu_to_be32(lp->rx_bd_p
  315. + sizeof(*lp->rx_bd_v) * ((i + 1) % lp->rx_bd_num));
  316. skb = __netdev_alloc_skb_ip_align(ndev,
  317. XTE_MAX_JUMBO_FRAME_SIZE,
  318. GFP_KERNEL);
  319. if (!skb)
  320. goto out;
  321. lp->rx_skb[i] = skb;
  322. /* returns physical address of skb->data */
  323. skb_dma_addr = dma_map_single(ndev->dev.parent, skb->data,
  324. XTE_MAX_JUMBO_FRAME_SIZE,
  325. DMA_FROM_DEVICE);
  326. if (dma_mapping_error(ndev->dev.parent, skb_dma_addr))
  327. goto out;
  328. lp->rx_bd_v[i].phys = cpu_to_be32(skb_dma_addr);
  329. lp->rx_bd_v[i].len = cpu_to_be32(XTE_MAX_JUMBO_FRAME_SIZE);
  330. lp->rx_bd_v[i].app0 = cpu_to_be32(STS_CTRL_APP0_IRQONEND);
  331. }
  332. /* Configure DMA channel (irq setup) */
  333. lp->dma_out(lp, TX_CHNL_CTRL,
  334. lp->coalesce_delay_tx << 24 | lp->coalesce_count_tx << 16 |
  335. 0x00000400 | // Use 1 Bit Wide Counters. Currently Not Used!
  336. CHNL_CTRL_IRQ_EN | CHNL_CTRL_IRQ_ERR_EN |
  337. CHNL_CTRL_IRQ_DLY_EN | CHNL_CTRL_IRQ_COAL_EN);
  338. lp->dma_out(lp, RX_CHNL_CTRL,
  339. lp->coalesce_delay_rx << 24 | lp->coalesce_count_rx << 16 |
  340. CHNL_CTRL_IRQ_IOE |
  341. CHNL_CTRL_IRQ_EN | CHNL_CTRL_IRQ_ERR_EN |
  342. CHNL_CTRL_IRQ_DLY_EN | CHNL_CTRL_IRQ_COAL_EN);
  343. /* Init descriptor indexes */
  344. lp->tx_bd_ci = 0;
  345. lp->tx_bd_tail = 0;
  346. lp->rx_bd_ci = 0;
  347. lp->rx_bd_tail = lp->rx_bd_num - 1;
  348. /* Enable RX DMA transfers */
  349. wmb();
  350. lp->dma_out(lp, RX_CURDESC_PTR, lp->rx_bd_p);
  351. lp->dma_out(lp, RX_TAILDESC_PTR,
  352. lp->rx_bd_p + (sizeof(*lp->rx_bd_v) * lp->rx_bd_tail));
  353. /* Prepare for TX DMA transfer */
  354. lp->dma_out(lp, TX_CURDESC_PTR, lp->tx_bd_p);
  355. return 0;
  356. out:
  357. temac_dma_bd_release(ndev);
  358. return -ENOMEM;
  359. }
  360. /* ---------------------------------------------------------------------
  361. * net_device_ops
  362. */
  363. static void temac_do_set_mac_address(struct net_device *ndev)
  364. {
  365. struct temac_local *lp = netdev_priv(ndev);
  366. unsigned long flags;
  367. /* set up unicast MAC address filter set its mac address */
  368. spin_lock_irqsave(lp->indirect_lock, flags);
  369. temac_indirect_out32_locked(lp, XTE_UAW0_OFFSET,
  370. (ndev->dev_addr[0]) |
  371. (ndev->dev_addr[1] << 8) |
  372. (ndev->dev_addr[2] << 16) |
  373. (ndev->dev_addr[3] << 24));
  374. /* There are reserved bits in EUAW1
  375. * so don't affect them Set MAC bits [47:32] in EUAW1
  376. */
  377. temac_indirect_out32_locked(lp, XTE_UAW1_OFFSET,
  378. (ndev->dev_addr[4] & 0x000000ff) |
  379. (ndev->dev_addr[5] << 8));
  380. spin_unlock_irqrestore(lp->indirect_lock, flags);
  381. }
  382. static int temac_init_mac_address(struct net_device *ndev, const void *address)
  383. {
  384. eth_hw_addr_set(ndev, address);
  385. if (!is_valid_ether_addr(ndev->dev_addr))
  386. eth_hw_addr_random(ndev);
  387. temac_do_set_mac_address(ndev);
  388. return 0;
  389. }
  390. static int temac_set_mac_address(struct net_device *ndev, void *p)
  391. {
  392. struct sockaddr *addr = p;
  393. if (!is_valid_ether_addr(addr->sa_data))
  394. return -EADDRNOTAVAIL;
  395. eth_hw_addr_set(ndev, addr->sa_data);
  396. temac_do_set_mac_address(ndev);
  397. return 0;
  398. }
  399. static void temac_set_multicast_list(struct net_device *ndev)
  400. {
  401. struct temac_local *lp = netdev_priv(ndev);
  402. u32 multi_addr_msw, multi_addr_lsw;
  403. int i = 0;
  404. unsigned long flags;
  405. bool promisc_mode_disabled = false;
  406. if (ndev->flags & (IFF_PROMISC | IFF_ALLMULTI) ||
  407. (netdev_mc_count(ndev) > MULTICAST_CAM_TABLE_NUM)) {
  408. temac_indirect_out32(lp, XTE_AFM_OFFSET, XTE_AFM_EPPRM_MASK);
  409. dev_info(&ndev->dev, "Promiscuous mode enabled.\n");
  410. return;
  411. }
  412. spin_lock_irqsave(lp->indirect_lock, flags);
  413. if (!netdev_mc_empty(ndev)) {
  414. struct netdev_hw_addr *ha;
  415. netdev_for_each_mc_addr(ha, ndev) {
  416. if (WARN_ON(i >= MULTICAST_CAM_TABLE_NUM))
  417. break;
  418. multi_addr_msw = ((ha->addr[3] << 24) |
  419. (ha->addr[2] << 16) |
  420. (ha->addr[1] << 8) |
  421. (ha->addr[0]));
  422. temac_indirect_out32_locked(lp, XTE_MAW0_OFFSET,
  423. multi_addr_msw);
  424. multi_addr_lsw = ((ha->addr[5] << 8) |
  425. (ha->addr[4]) | (i << 16));
  426. temac_indirect_out32_locked(lp, XTE_MAW1_OFFSET,
  427. multi_addr_lsw);
  428. i++;
  429. }
  430. }
  431. /* Clear all or remaining/unused address table entries */
  432. while (i < MULTICAST_CAM_TABLE_NUM) {
  433. temac_indirect_out32_locked(lp, XTE_MAW0_OFFSET, 0);
  434. temac_indirect_out32_locked(lp, XTE_MAW1_OFFSET, i << 16);
  435. i++;
  436. }
  437. /* Enable address filter block if currently disabled */
  438. if (temac_indirect_in32_locked(lp, XTE_AFM_OFFSET)
  439. & XTE_AFM_EPPRM_MASK) {
  440. temac_indirect_out32_locked(lp, XTE_AFM_OFFSET, 0);
  441. promisc_mode_disabled = true;
  442. }
  443. spin_unlock_irqrestore(lp->indirect_lock, flags);
  444. if (promisc_mode_disabled)
  445. dev_info(&ndev->dev, "Promiscuous mode disabled.\n");
  446. }
  447. static struct temac_option {
  448. int flg;
  449. u32 opt;
  450. u32 reg;
  451. u32 m_or;
  452. u32 m_and;
  453. } temac_options[] = {
  454. /* Turn on jumbo packet support for both Rx and Tx */
  455. {
  456. .opt = XTE_OPTION_JUMBO,
  457. .reg = XTE_TXC_OFFSET,
  458. .m_or = XTE_TXC_TXJMBO_MASK,
  459. },
  460. {
  461. .opt = XTE_OPTION_JUMBO,
  462. .reg = XTE_RXC1_OFFSET,
  463. .m_or = XTE_RXC1_RXJMBO_MASK,
  464. },
  465. /* Turn on VLAN packet support for both Rx and Tx */
  466. {
  467. .opt = XTE_OPTION_VLAN,
  468. .reg = XTE_TXC_OFFSET,
  469. .m_or = XTE_TXC_TXVLAN_MASK,
  470. },
  471. {
  472. .opt = XTE_OPTION_VLAN,
  473. .reg = XTE_RXC1_OFFSET,
  474. .m_or = XTE_RXC1_RXVLAN_MASK,
  475. },
  476. /* Turn on FCS stripping on receive packets */
  477. {
  478. .opt = XTE_OPTION_FCS_STRIP,
  479. .reg = XTE_RXC1_OFFSET,
  480. .m_or = XTE_RXC1_RXFCS_MASK,
  481. },
  482. /* Turn on FCS insertion on transmit packets */
  483. {
  484. .opt = XTE_OPTION_FCS_INSERT,
  485. .reg = XTE_TXC_OFFSET,
  486. .m_or = XTE_TXC_TXFCS_MASK,
  487. },
  488. /* Turn on length/type field checking on receive packets */
  489. {
  490. .opt = XTE_OPTION_LENTYPE_ERR,
  491. .reg = XTE_RXC1_OFFSET,
  492. .m_or = XTE_RXC1_RXLT_MASK,
  493. },
  494. /* Turn on flow control */
  495. {
  496. .opt = XTE_OPTION_FLOW_CONTROL,
  497. .reg = XTE_FCC_OFFSET,
  498. .m_or = XTE_FCC_RXFLO_MASK,
  499. },
  500. /* Turn on flow control */
  501. {
  502. .opt = XTE_OPTION_FLOW_CONTROL,
  503. .reg = XTE_FCC_OFFSET,
  504. .m_or = XTE_FCC_TXFLO_MASK,
  505. },
  506. /* Turn on promiscuous frame filtering (all frames are received ) */
  507. {
  508. .opt = XTE_OPTION_PROMISC,
  509. .reg = XTE_AFM_OFFSET,
  510. .m_or = XTE_AFM_EPPRM_MASK,
  511. },
  512. /* Enable transmitter if not already enabled */
  513. {
  514. .opt = XTE_OPTION_TXEN,
  515. .reg = XTE_TXC_OFFSET,
  516. .m_or = XTE_TXC_TXEN_MASK,
  517. },
  518. /* Enable receiver? */
  519. {
  520. .opt = XTE_OPTION_RXEN,
  521. .reg = XTE_RXC1_OFFSET,
  522. .m_or = XTE_RXC1_RXEN_MASK,
  523. },
  524. {}
  525. };
  526. /*
  527. * temac_setoptions
  528. */
  529. static u32 temac_setoptions(struct net_device *ndev, u32 options)
  530. {
  531. struct temac_local *lp = netdev_priv(ndev);
  532. struct temac_option *tp = &temac_options[0];
  533. int reg;
  534. unsigned long flags;
  535. spin_lock_irqsave(lp->indirect_lock, flags);
  536. while (tp->opt) {
  537. reg = temac_indirect_in32_locked(lp, tp->reg) & ~tp->m_or;
  538. if (options & tp->opt) {
  539. reg |= tp->m_or;
  540. temac_indirect_out32_locked(lp, tp->reg, reg);
  541. }
  542. tp++;
  543. }
  544. spin_unlock_irqrestore(lp->indirect_lock, flags);
  545. lp->options |= options;
  546. return 0;
  547. }
  548. /* Initialize temac */
  549. static void temac_device_reset(struct net_device *ndev)
  550. {
  551. struct temac_local *lp = netdev_priv(ndev);
  552. u32 timeout;
  553. u32 val;
  554. unsigned long flags;
  555. /* Perform a software reset */
  556. /* 0x300 host enable bit ? */
  557. /* reset PHY through control register ?:1 */
  558. dev_dbg(&ndev->dev, "%s()\n", __func__);
  559. /* Reset the receiver and wait for it to finish reset */
  560. temac_indirect_out32(lp, XTE_RXC1_OFFSET, XTE_RXC1_RXRST_MASK);
  561. timeout = 1000;
  562. while (temac_indirect_in32(lp, XTE_RXC1_OFFSET) & XTE_RXC1_RXRST_MASK) {
  563. udelay(1);
  564. if (--timeout == 0) {
  565. dev_err(&ndev->dev,
  566. "%s RX reset timeout!!\n", __func__);
  567. break;
  568. }
  569. }
  570. /* Reset the transmitter and wait for it to finish reset */
  571. temac_indirect_out32(lp, XTE_TXC_OFFSET, XTE_TXC_TXRST_MASK);
  572. timeout = 1000;
  573. while (temac_indirect_in32(lp, XTE_TXC_OFFSET) & XTE_TXC_TXRST_MASK) {
  574. udelay(1);
  575. if (--timeout == 0) {
  576. dev_err(&ndev->dev,
  577. "%s TX reset timeout!!\n", __func__);
  578. break;
  579. }
  580. }
  581. /* Disable the receiver */
  582. spin_lock_irqsave(lp->indirect_lock, flags);
  583. val = temac_indirect_in32_locked(lp, XTE_RXC1_OFFSET);
  584. temac_indirect_out32_locked(lp, XTE_RXC1_OFFSET,
  585. val & ~XTE_RXC1_RXEN_MASK);
  586. spin_unlock_irqrestore(lp->indirect_lock, flags);
  587. /* Reset Local Link (DMA) */
  588. lp->dma_out(lp, DMA_CONTROL_REG, DMA_CONTROL_RST);
  589. timeout = 1000;
  590. while (lp->dma_in(lp, DMA_CONTROL_REG) & DMA_CONTROL_RST) {
  591. udelay(1);
  592. if (--timeout == 0) {
  593. dev_err(&ndev->dev,
  594. "%s DMA reset timeout!!\n", __func__);
  595. break;
  596. }
  597. }
  598. lp->dma_out(lp, DMA_CONTROL_REG, DMA_TAIL_ENABLE);
  599. if (temac_dma_bd_init(ndev)) {
  600. dev_err(&ndev->dev,
  601. "%s descriptor allocation failed\n", __func__);
  602. }
  603. spin_lock_irqsave(lp->indirect_lock, flags);
  604. temac_indirect_out32_locked(lp, XTE_RXC0_OFFSET, 0);
  605. temac_indirect_out32_locked(lp, XTE_RXC1_OFFSET, 0);
  606. temac_indirect_out32_locked(lp, XTE_TXC_OFFSET, 0);
  607. temac_indirect_out32_locked(lp, XTE_FCC_OFFSET, XTE_FCC_RXFLO_MASK);
  608. spin_unlock_irqrestore(lp->indirect_lock, flags);
  609. /* Sync default options with HW
  610. * but leave receiver and transmitter disabled.
  611. */
  612. temac_setoptions(ndev,
  613. lp->options & ~(XTE_OPTION_TXEN | XTE_OPTION_RXEN));
  614. temac_do_set_mac_address(ndev);
  615. /* Set address filter table */
  616. temac_set_multicast_list(ndev);
  617. if (temac_setoptions(ndev, lp->options))
  618. dev_err(&ndev->dev, "Error setting TEMAC options\n");
  619. /* Init Driver variable */
  620. netif_trans_update(ndev); /* prevent tx timeout */
  621. }
  622. static void temac_adjust_link(struct net_device *ndev)
  623. {
  624. struct temac_local *lp = netdev_priv(ndev);
  625. struct phy_device *phy = ndev->phydev;
  626. u32 mii_speed;
  627. int link_state;
  628. unsigned long flags;
  629. /* hash together the state values to decide if something has changed */
  630. link_state = phy->speed | (phy->duplex << 1) | phy->link;
  631. if (lp->last_link != link_state) {
  632. spin_lock_irqsave(lp->indirect_lock, flags);
  633. mii_speed = temac_indirect_in32_locked(lp, XTE_EMCFG_OFFSET);
  634. mii_speed &= ~XTE_EMCFG_LINKSPD_MASK;
  635. switch (phy->speed) {
  636. case SPEED_1000:
  637. mii_speed |= XTE_EMCFG_LINKSPD_1000;
  638. break;
  639. case SPEED_100:
  640. mii_speed |= XTE_EMCFG_LINKSPD_100;
  641. break;
  642. case SPEED_10:
  643. mii_speed |= XTE_EMCFG_LINKSPD_10;
  644. break;
  645. }
  646. /* Write new speed setting out to TEMAC */
  647. temac_indirect_out32_locked(lp, XTE_EMCFG_OFFSET, mii_speed);
  648. spin_unlock_irqrestore(lp->indirect_lock, flags);
  649. lp->last_link = link_state;
  650. phy_print_status(phy);
  651. }
  652. }
  653. #ifdef CONFIG_64BIT
  654. static void ptr_to_txbd(void *p, struct cdmac_bd *bd)
  655. {
  656. bd->app3 = (u32)(((u64)p) >> 32);
  657. bd->app4 = (u32)((u64)p & 0xFFFFFFFF);
  658. }
  659. static void *ptr_from_txbd(struct cdmac_bd *bd)
  660. {
  661. return (void *)(((u64)(bd->app3) << 32) | bd->app4);
  662. }
  663. #else
  664. static void ptr_to_txbd(void *p, struct cdmac_bd *bd)
  665. {
  666. bd->app4 = (u32)p;
  667. }
  668. static void *ptr_from_txbd(struct cdmac_bd *bd)
  669. {
  670. return (void *)(bd->app4);
  671. }
  672. #endif
  673. static void temac_start_xmit_done(struct net_device *ndev)
  674. {
  675. struct temac_local *lp = netdev_priv(ndev);
  676. struct cdmac_bd *cur_p;
  677. unsigned int stat = 0;
  678. struct sk_buff *skb;
  679. cur_p = &lp->tx_bd_v[lp->tx_bd_ci];
  680. stat = be32_to_cpu(cur_p->app0);
  681. while (stat & STS_CTRL_APP0_CMPLT) {
  682. /* Make sure that the other fields are read after bd is
  683. * released by dma
  684. */
  685. rmb();
  686. dma_unmap_single(ndev->dev.parent, be32_to_cpu(cur_p->phys),
  687. be32_to_cpu(cur_p->len), DMA_TO_DEVICE);
  688. skb = (struct sk_buff *)ptr_from_txbd(cur_p);
  689. if (skb)
  690. dev_consume_skb_irq(skb);
  691. cur_p->app1 = 0;
  692. cur_p->app2 = 0;
  693. cur_p->app3 = 0;
  694. cur_p->app4 = 0;
  695. ndev->stats.tx_packets++;
  696. ndev->stats.tx_bytes += be32_to_cpu(cur_p->len);
  697. /* app0 must be visible last, as it is used to flag
  698. * availability of the bd
  699. */
  700. smp_mb();
  701. cur_p->app0 = 0;
  702. lp->tx_bd_ci++;
  703. if (lp->tx_bd_ci >= lp->tx_bd_num)
  704. lp->tx_bd_ci = 0;
  705. cur_p = &lp->tx_bd_v[lp->tx_bd_ci];
  706. stat = be32_to_cpu(cur_p->app0);
  707. }
  708. /* Matches barrier in temac_start_xmit */
  709. smp_mb();
  710. netif_wake_queue(ndev);
  711. }
  712. static inline int temac_check_tx_bd_space(struct temac_local *lp, int num_frag)
  713. {
  714. struct cdmac_bd *cur_p;
  715. int tail;
  716. tail = lp->tx_bd_tail;
  717. cur_p = &lp->tx_bd_v[tail];
  718. do {
  719. if (cur_p->app0)
  720. return NETDEV_TX_BUSY;
  721. /* Make sure to read next bd app0 after this one */
  722. rmb();
  723. tail++;
  724. if (tail >= lp->tx_bd_num)
  725. tail = 0;
  726. cur_p = &lp->tx_bd_v[tail];
  727. num_frag--;
  728. } while (num_frag >= 0);
  729. return 0;
  730. }
  731. static netdev_tx_t
  732. temac_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  733. {
  734. struct temac_local *lp = netdev_priv(ndev);
  735. struct cdmac_bd *cur_p;
  736. dma_addr_t tail_p, skb_dma_addr;
  737. int ii;
  738. unsigned long num_frag;
  739. skb_frag_t *frag;
  740. num_frag = skb_shinfo(skb)->nr_frags;
  741. frag = &skb_shinfo(skb)->frags[0];
  742. cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
  743. if (temac_check_tx_bd_space(lp, num_frag + 1)) {
  744. if (netif_queue_stopped(ndev))
  745. return NETDEV_TX_BUSY;
  746. netif_stop_queue(ndev);
  747. /* Matches barrier in temac_start_xmit_done */
  748. smp_mb();
  749. /* Space might have just been freed - check again */
  750. if (temac_check_tx_bd_space(lp, num_frag + 1))
  751. return NETDEV_TX_BUSY;
  752. netif_wake_queue(ndev);
  753. }
  754. cur_p->app0 = 0;
  755. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  756. unsigned int csum_start_off = skb_checksum_start_offset(skb);
  757. unsigned int csum_index_off = csum_start_off + skb->csum_offset;
  758. cur_p->app0 |= cpu_to_be32(0x000001); /* TX Checksum Enabled */
  759. cur_p->app1 = cpu_to_be32((csum_start_off << 16)
  760. | csum_index_off);
  761. cur_p->app2 = 0; /* initial checksum seed */
  762. }
  763. cur_p->app0 |= cpu_to_be32(STS_CTRL_APP0_SOP);
  764. skb_dma_addr = dma_map_single(ndev->dev.parent, skb->data,
  765. skb_headlen(skb), DMA_TO_DEVICE);
  766. cur_p->len = cpu_to_be32(skb_headlen(skb));
  767. if (WARN_ON_ONCE(dma_mapping_error(ndev->dev.parent, skb_dma_addr))) {
  768. dev_kfree_skb_any(skb);
  769. ndev->stats.tx_dropped++;
  770. return NETDEV_TX_OK;
  771. }
  772. cur_p->phys = cpu_to_be32(skb_dma_addr);
  773. for (ii = 0; ii < num_frag; ii++) {
  774. if (++lp->tx_bd_tail >= lp->tx_bd_num)
  775. lp->tx_bd_tail = 0;
  776. cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
  777. skb_dma_addr = dma_map_single(ndev->dev.parent,
  778. skb_frag_address(frag),
  779. skb_frag_size(frag),
  780. DMA_TO_DEVICE);
  781. if (dma_mapping_error(ndev->dev.parent, skb_dma_addr)) {
  782. if (--lp->tx_bd_tail < 0)
  783. lp->tx_bd_tail = lp->tx_bd_num - 1;
  784. cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
  785. while (--ii >= 0) {
  786. --frag;
  787. dma_unmap_single(ndev->dev.parent,
  788. be32_to_cpu(cur_p->phys),
  789. skb_frag_size(frag),
  790. DMA_TO_DEVICE);
  791. if (--lp->tx_bd_tail < 0)
  792. lp->tx_bd_tail = lp->tx_bd_num - 1;
  793. cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
  794. }
  795. dma_unmap_single(ndev->dev.parent,
  796. be32_to_cpu(cur_p->phys),
  797. skb_headlen(skb), DMA_TO_DEVICE);
  798. dev_kfree_skb_any(skb);
  799. ndev->stats.tx_dropped++;
  800. return NETDEV_TX_OK;
  801. }
  802. cur_p->phys = cpu_to_be32(skb_dma_addr);
  803. cur_p->len = cpu_to_be32(skb_frag_size(frag));
  804. cur_p->app0 = 0;
  805. frag++;
  806. }
  807. cur_p->app0 |= cpu_to_be32(STS_CTRL_APP0_EOP);
  808. /* Mark last fragment with skb address, so it can be consumed
  809. * in temac_start_xmit_done()
  810. */
  811. ptr_to_txbd((void *)skb, cur_p);
  812. tail_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail;
  813. lp->tx_bd_tail++;
  814. if (lp->tx_bd_tail >= lp->tx_bd_num)
  815. lp->tx_bd_tail = 0;
  816. skb_tx_timestamp(skb);
  817. /* Kick off the transfer */
  818. wmb();
  819. lp->dma_out(lp, TX_TAILDESC_PTR, tail_p); /* DMA start */
  820. if (temac_check_tx_bd_space(lp, MAX_SKB_FRAGS + 1))
  821. netif_stop_queue(ndev);
  822. return NETDEV_TX_OK;
  823. }
  824. static int ll_temac_recv_buffers_available(struct temac_local *lp)
  825. {
  826. int available;
  827. if (!lp->rx_skb[lp->rx_bd_ci])
  828. return 0;
  829. available = 1 + lp->rx_bd_tail - lp->rx_bd_ci;
  830. if (available <= 0)
  831. available += lp->rx_bd_num;
  832. return available;
  833. }
  834. static void ll_temac_recv(struct net_device *ndev)
  835. {
  836. struct temac_local *lp = netdev_priv(ndev);
  837. unsigned long flags;
  838. int rx_bd;
  839. bool update_tail = false;
  840. spin_lock_irqsave(&lp->rx_lock, flags);
  841. /* Process all received buffers, passing them on network
  842. * stack. After this, the buffer descriptors will be in an
  843. * un-allocated stage, where no skb is allocated for it, and
  844. * they are therefore not available for TEMAC/DMA.
  845. */
  846. do {
  847. struct cdmac_bd *bd = &lp->rx_bd_v[lp->rx_bd_ci];
  848. struct sk_buff *skb = lp->rx_skb[lp->rx_bd_ci];
  849. unsigned int bdstat = be32_to_cpu(bd->app0);
  850. int length;
  851. /* While this should not normally happen, we can end
  852. * here when GFP_ATOMIC allocations fail, and we
  853. * therefore have un-allocated buffers.
  854. */
  855. if (!skb)
  856. break;
  857. /* Loop over all completed buffer descriptors */
  858. if (!(bdstat & STS_CTRL_APP0_CMPLT))
  859. break;
  860. dma_unmap_single(ndev->dev.parent, be32_to_cpu(bd->phys),
  861. XTE_MAX_JUMBO_FRAME_SIZE, DMA_FROM_DEVICE);
  862. /* The buffer is not valid for DMA anymore */
  863. bd->phys = 0;
  864. bd->len = 0;
  865. length = be32_to_cpu(bd->app4) & 0x3FFF;
  866. skb_put(skb, length);
  867. skb->protocol = eth_type_trans(skb, ndev);
  868. skb_checksum_none_assert(skb);
  869. /* if we're doing rx csum offload, set it up */
  870. if (((lp->temac_features & TEMAC_FEATURE_RX_CSUM) != 0) &&
  871. (skb->protocol == htons(ETH_P_IP)) &&
  872. (skb->len > 64)) {
  873. /* Convert from device endianness (be32) to cpu
  874. * endianness, and if necessary swap the bytes
  875. * (back) for proper IP checksum byte order
  876. * (be16).
  877. */
  878. skb->csum = htons(be32_to_cpu(bd->app3) & 0xFFFF);
  879. skb->ip_summed = CHECKSUM_COMPLETE;
  880. }
  881. if (!skb_defer_rx_timestamp(skb))
  882. netif_rx(skb);
  883. /* The skb buffer is now owned by network stack above */
  884. lp->rx_skb[lp->rx_bd_ci] = NULL;
  885. ndev->stats.rx_packets++;
  886. ndev->stats.rx_bytes += length;
  887. rx_bd = lp->rx_bd_ci;
  888. if (++lp->rx_bd_ci >= lp->rx_bd_num)
  889. lp->rx_bd_ci = 0;
  890. } while (rx_bd != lp->rx_bd_tail);
  891. /* DMA operations will halt when the last buffer descriptor is
  892. * processed (ie. the one pointed to by RX_TAILDESC_PTR).
  893. * When that happens, no more interrupt events will be
  894. * generated. No IRQ_COAL or IRQ_DLY, and not even an
  895. * IRQ_ERR. To avoid stalling, we schedule a delayed work
  896. * when there is a potential risk of that happening. The work
  897. * will call this function, and thus re-schedule itself until
  898. * enough buffers are available again.
  899. */
  900. if (ll_temac_recv_buffers_available(lp) < lp->coalesce_count_rx)
  901. schedule_delayed_work(&lp->restart_work, HZ / 1000);
  902. /* Allocate new buffers for those buffer descriptors that were
  903. * passed to network stack. Note that GFP_ATOMIC allocations
  904. * can fail (e.g. when a larger burst of GFP_ATOMIC
  905. * allocations occurs), so while we try to allocate all
  906. * buffers in the same interrupt where they were processed, we
  907. * continue with what we could get in case of allocation
  908. * failure. Allocation of remaining buffers will be retried
  909. * in following calls.
  910. */
  911. while (1) {
  912. struct sk_buff *skb;
  913. struct cdmac_bd *bd;
  914. dma_addr_t skb_dma_addr;
  915. rx_bd = lp->rx_bd_tail + 1;
  916. if (rx_bd >= lp->rx_bd_num)
  917. rx_bd = 0;
  918. bd = &lp->rx_bd_v[rx_bd];
  919. if (bd->phys)
  920. break; /* All skb's allocated */
  921. skb = netdev_alloc_skb_ip_align(ndev, XTE_MAX_JUMBO_FRAME_SIZE);
  922. if (!skb) {
  923. dev_warn(&ndev->dev, "skb alloc failed\n");
  924. break;
  925. }
  926. skb_dma_addr = dma_map_single(ndev->dev.parent, skb->data,
  927. XTE_MAX_JUMBO_FRAME_SIZE,
  928. DMA_FROM_DEVICE);
  929. if (WARN_ON_ONCE(dma_mapping_error(ndev->dev.parent,
  930. skb_dma_addr))) {
  931. dev_kfree_skb_any(skb);
  932. break;
  933. }
  934. bd->phys = cpu_to_be32(skb_dma_addr);
  935. bd->len = cpu_to_be32(XTE_MAX_JUMBO_FRAME_SIZE);
  936. bd->app0 = cpu_to_be32(STS_CTRL_APP0_IRQONEND);
  937. lp->rx_skb[rx_bd] = skb;
  938. lp->rx_bd_tail = rx_bd;
  939. update_tail = true;
  940. }
  941. /* Move tail pointer when buffers have been allocated */
  942. if (update_tail) {
  943. lp->dma_out(lp, RX_TAILDESC_PTR,
  944. lp->rx_bd_p + sizeof(*lp->rx_bd_v) * lp->rx_bd_tail);
  945. }
  946. spin_unlock_irqrestore(&lp->rx_lock, flags);
  947. }
  948. /* Function scheduled to ensure a restart in case of DMA halt
  949. * condition caused by running out of buffer descriptors.
  950. */
  951. static void ll_temac_restart_work_func(struct work_struct *work)
  952. {
  953. struct temac_local *lp = container_of(work, struct temac_local,
  954. restart_work.work);
  955. struct net_device *ndev = lp->ndev;
  956. ll_temac_recv(ndev);
  957. }
  958. static irqreturn_t ll_temac_tx_irq(int irq, void *_ndev)
  959. {
  960. struct net_device *ndev = _ndev;
  961. struct temac_local *lp = netdev_priv(ndev);
  962. unsigned int status;
  963. status = lp->dma_in(lp, TX_IRQ_REG);
  964. lp->dma_out(lp, TX_IRQ_REG, status);
  965. if (status & (IRQ_COAL | IRQ_DLY))
  966. temac_start_xmit_done(lp->ndev);
  967. if (status & (IRQ_ERR | IRQ_DMAERR))
  968. dev_err_ratelimited(&ndev->dev,
  969. "TX error 0x%x TX_CHNL_STS=0x%08x\n",
  970. status, lp->dma_in(lp, TX_CHNL_STS));
  971. return IRQ_HANDLED;
  972. }
  973. static irqreturn_t ll_temac_rx_irq(int irq, void *_ndev)
  974. {
  975. struct net_device *ndev = _ndev;
  976. struct temac_local *lp = netdev_priv(ndev);
  977. unsigned int status;
  978. /* Read and clear the status registers */
  979. status = lp->dma_in(lp, RX_IRQ_REG);
  980. lp->dma_out(lp, RX_IRQ_REG, status);
  981. if (status & (IRQ_COAL | IRQ_DLY))
  982. ll_temac_recv(lp->ndev);
  983. if (status & (IRQ_ERR | IRQ_DMAERR))
  984. dev_err_ratelimited(&ndev->dev,
  985. "RX error 0x%x RX_CHNL_STS=0x%08x\n",
  986. status, lp->dma_in(lp, RX_CHNL_STS));
  987. return IRQ_HANDLED;
  988. }
  989. static int temac_open(struct net_device *ndev)
  990. {
  991. struct temac_local *lp = netdev_priv(ndev);
  992. struct phy_device *phydev = NULL;
  993. int rc;
  994. dev_dbg(&ndev->dev, "temac_open()\n");
  995. if (lp->phy_node) {
  996. phydev = of_phy_connect(lp->ndev, lp->phy_node,
  997. temac_adjust_link, 0, 0);
  998. if (!phydev) {
  999. dev_err(lp->dev, "of_phy_connect() failed\n");
  1000. return -ENODEV;
  1001. }
  1002. phy_start(phydev);
  1003. } else if (strlen(lp->phy_name) > 0) {
  1004. phydev = phy_connect(lp->ndev, lp->phy_name, temac_adjust_link,
  1005. lp->phy_interface);
  1006. if (IS_ERR(phydev)) {
  1007. dev_err(lp->dev, "phy_connect() failed\n");
  1008. return PTR_ERR(phydev);
  1009. }
  1010. phy_start(phydev);
  1011. }
  1012. temac_device_reset(ndev);
  1013. rc = request_irq(lp->tx_irq, ll_temac_tx_irq, 0, ndev->name, ndev);
  1014. if (rc)
  1015. goto err_tx_irq;
  1016. rc = request_irq(lp->rx_irq, ll_temac_rx_irq, 0, ndev->name, ndev);
  1017. if (rc)
  1018. goto err_rx_irq;
  1019. return 0;
  1020. err_rx_irq:
  1021. free_irq(lp->tx_irq, ndev);
  1022. err_tx_irq:
  1023. if (phydev)
  1024. phy_disconnect(phydev);
  1025. dev_err(lp->dev, "request_irq() failed\n");
  1026. return rc;
  1027. }
  1028. static int temac_stop(struct net_device *ndev)
  1029. {
  1030. struct temac_local *lp = netdev_priv(ndev);
  1031. struct phy_device *phydev = ndev->phydev;
  1032. dev_dbg(&ndev->dev, "temac_close()\n");
  1033. cancel_delayed_work_sync(&lp->restart_work);
  1034. free_irq(lp->tx_irq, ndev);
  1035. free_irq(lp->rx_irq, ndev);
  1036. if (phydev)
  1037. phy_disconnect(phydev);
  1038. temac_dma_bd_release(ndev);
  1039. return 0;
  1040. }
  1041. #ifdef CONFIG_NET_POLL_CONTROLLER
  1042. static void
  1043. temac_poll_controller(struct net_device *ndev)
  1044. {
  1045. struct temac_local *lp = netdev_priv(ndev);
  1046. disable_irq(lp->tx_irq);
  1047. disable_irq(lp->rx_irq);
  1048. ll_temac_rx_irq(lp->tx_irq, ndev);
  1049. ll_temac_tx_irq(lp->rx_irq, ndev);
  1050. enable_irq(lp->tx_irq);
  1051. enable_irq(lp->rx_irq);
  1052. }
  1053. #endif
  1054. static const struct net_device_ops temac_netdev_ops = {
  1055. .ndo_open = temac_open,
  1056. .ndo_stop = temac_stop,
  1057. .ndo_start_xmit = temac_start_xmit,
  1058. .ndo_set_rx_mode = temac_set_multicast_list,
  1059. .ndo_set_mac_address = temac_set_mac_address,
  1060. .ndo_validate_addr = eth_validate_addr,
  1061. .ndo_eth_ioctl = phy_do_ioctl_running,
  1062. #ifdef CONFIG_NET_POLL_CONTROLLER
  1063. .ndo_poll_controller = temac_poll_controller,
  1064. #endif
  1065. };
  1066. /* ---------------------------------------------------------------------
  1067. * SYSFS device attributes
  1068. */
  1069. static ssize_t temac_show_llink_regs(struct device *dev,
  1070. struct device_attribute *attr, char *buf)
  1071. {
  1072. struct net_device *ndev = dev_get_drvdata(dev);
  1073. struct temac_local *lp = netdev_priv(ndev);
  1074. int i, len = 0;
  1075. for (i = 0; i < 0x11; i++)
  1076. len += sprintf(buf + len, "%.8x%s", lp->dma_in(lp, i),
  1077. (i % 8) == 7 ? "\n" : " ");
  1078. len += sprintf(buf + len, "\n");
  1079. return len;
  1080. }
  1081. static DEVICE_ATTR(llink_regs, 0440, temac_show_llink_regs, NULL);
  1082. static struct attribute *temac_device_attrs[] = {
  1083. &dev_attr_llink_regs.attr,
  1084. NULL,
  1085. };
  1086. static const struct attribute_group temac_attr_group = {
  1087. .attrs = temac_device_attrs,
  1088. };
  1089. /* ---------------------------------------------------------------------
  1090. * ethtool support
  1091. */
  1092. static void
  1093. ll_temac_ethtools_get_ringparam(struct net_device *ndev,
  1094. struct ethtool_ringparam *ering,
  1095. struct kernel_ethtool_ringparam *kernel_ering,
  1096. struct netlink_ext_ack *extack)
  1097. {
  1098. struct temac_local *lp = netdev_priv(ndev);
  1099. ering->rx_max_pending = RX_BD_NUM_MAX;
  1100. ering->rx_mini_max_pending = 0;
  1101. ering->rx_jumbo_max_pending = 0;
  1102. ering->tx_max_pending = TX_BD_NUM_MAX;
  1103. ering->rx_pending = lp->rx_bd_num;
  1104. ering->rx_mini_pending = 0;
  1105. ering->rx_jumbo_pending = 0;
  1106. ering->tx_pending = lp->tx_bd_num;
  1107. }
  1108. static int
  1109. ll_temac_ethtools_set_ringparam(struct net_device *ndev,
  1110. struct ethtool_ringparam *ering,
  1111. struct kernel_ethtool_ringparam *kernel_ering,
  1112. struct netlink_ext_ack *extack)
  1113. {
  1114. struct temac_local *lp = netdev_priv(ndev);
  1115. if (ering->rx_pending > RX_BD_NUM_MAX ||
  1116. ering->rx_mini_pending ||
  1117. ering->rx_jumbo_pending ||
  1118. ering->tx_pending > TX_BD_NUM_MAX)
  1119. return -EINVAL;
  1120. if (netif_running(ndev))
  1121. return -EBUSY;
  1122. lp->rx_bd_num = ering->rx_pending;
  1123. lp->tx_bd_num = ering->tx_pending;
  1124. return 0;
  1125. }
  1126. static int
  1127. ll_temac_ethtools_get_coalesce(struct net_device *ndev,
  1128. struct ethtool_coalesce *ec,
  1129. struct kernel_ethtool_coalesce *kernel_coal,
  1130. struct netlink_ext_ack *extack)
  1131. {
  1132. struct temac_local *lp = netdev_priv(ndev);
  1133. ec->rx_max_coalesced_frames = lp->coalesce_count_rx;
  1134. ec->tx_max_coalesced_frames = lp->coalesce_count_tx;
  1135. ec->rx_coalesce_usecs = (lp->coalesce_delay_rx * 512) / 100;
  1136. ec->tx_coalesce_usecs = (lp->coalesce_delay_tx * 512) / 100;
  1137. return 0;
  1138. }
  1139. static int
  1140. ll_temac_ethtools_set_coalesce(struct net_device *ndev,
  1141. struct ethtool_coalesce *ec,
  1142. struct kernel_ethtool_coalesce *kernel_coal,
  1143. struct netlink_ext_ack *extack)
  1144. {
  1145. struct temac_local *lp = netdev_priv(ndev);
  1146. if (netif_running(ndev)) {
  1147. netdev_err(ndev,
  1148. "Please stop netif before applying configuration\n");
  1149. return -EFAULT;
  1150. }
  1151. if (ec->rx_max_coalesced_frames)
  1152. lp->coalesce_count_rx = ec->rx_max_coalesced_frames;
  1153. if (ec->tx_max_coalesced_frames)
  1154. lp->coalesce_count_tx = ec->tx_max_coalesced_frames;
  1155. /* With typical LocalLink clock speed of 200 MHz and
  1156. * C_PRESCALAR=1023, each delay count corresponds to 5.12 us.
  1157. */
  1158. if (ec->rx_coalesce_usecs)
  1159. lp->coalesce_delay_rx =
  1160. min(255U, (ec->rx_coalesce_usecs * 100) / 512);
  1161. if (ec->tx_coalesce_usecs)
  1162. lp->coalesce_delay_tx =
  1163. min(255U, (ec->tx_coalesce_usecs * 100) / 512);
  1164. return 0;
  1165. }
  1166. static const struct ethtool_ops temac_ethtool_ops = {
  1167. .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
  1168. ETHTOOL_COALESCE_MAX_FRAMES,
  1169. .nway_reset = phy_ethtool_nway_reset,
  1170. .get_link = ethtool_op_get_link,
  1171. .get_ts_info = ethtool_op_get_ts_info,
  1172. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  1173. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  1174. .get_ringparam = ll_temac_ethtools_get_ringparam,
  1175. .set_ringparam = ll_temac_ethtools_set_ringparam,
  1176. .get_coalesce = ll_temac_ethtools_get_coalesce,
  1177. .set_coalesce = ll_temac_ethtools_set_coalesce,
  1178. };
  1179. static int temac_probe(struct platform_device *pdev)
  1180. {
  1181. struct ll_temac_platform_data *pdata = dev_get_platdata(&pdev->dev);
  1182. struct device_node *temac_np = dev_of_node(&pdev->dev), *dma_np;
  1183. struct temac_local *lp;
  1184. struct net_device *ndev;
  1185. u8 addr[ETH_ALEN];
  1186. __be32 *p;
  1187. bool little_endian;
  1188. int rc = 0;
  1189. /* Init network device structure */
  1190. ndev = devm_alloc_etherdev(&pdev->dev, sizeof(*lp));
  1191. if (!ndev)
  1192. return -ENOMEM;
  1193. platform_set_drvdata(pdev, ndev);
  1194. SET_NETDEV_DEV(ndev, &pdev->dev);
  1195. ndev->features = NETIF_F_SG;
  1196. ndev->netdev_ops = &temac_netdev_ops;
  1197. ndev->ethtool_ops = &temac_ethtool_ops;
  1198. #if 0
  1199. ndev->features |= NETIF_F_IP_CSUM; /* Can checksum TCP/UDP over IPv4. */
  1200. ndev->features |= NETIF_F_HW_CSUM; /* Can checksum all the packets. */
  1201. ndev->features |= NETIF_F_IPV6_CSUM; /* Can checksum IPV6 TCP/UDP */
  1202. ndev->features |= NETIF_F_HIGHDMA; /* Can DMA to high memory. */
  1203. ndev->features |= NETIF_F_HW_VLAN_CTAG_TX; /* Transmit VLAN hw accel */
  1204. ndev->features |= NETIF_F_HW_VLAN_CTAG_RX; /* Receive VLAN hw acceleration */
  1205. ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; /* Receive VLAN filtering */
  1206. ndev->features |= NETIF_F_VLAN_CHALLENGED; /* cannot handle VLAN pkts */
  1207. ndev->features |= NETIF_F_GSO; /* Enable software GSO. */
  1208. ndev->features |= NETIF_F_MULTI_QUEUE; /* Has multiple TX/RX queues */
  1209. ndev->features |= NETIF_F_LRO; /* large receive offload */
  1210. #endif
  1211. /* setup temac private info structure */
  1212. lp = netdev_priv(ndev);
  1213. lp->ndev = ndev;
  1214. lp->dev = &pdev->dev;
  1215. lp->options = XTE_OPTION_DEFAULTS;
  1216. lp->rx_bd_num = RX_BD_NUM_DEFAULT;
  1217. lp->tx_bd_num = TX_BD_NUM_DEFAULT;
  1218. spin_lock_init(&lp->rx_lock);
  1219. INIT_DELAYED_WORK(&lp->restart_work, ll_temac_restart_work_func);
  1220. /* Setup mutex for synchronization of indirect register access */
  1221. if (pdata) {
  1222. if (!pdata->indirect_lock) {
  1223. dev_err(&pdev->dev,
  1224. "indirect_lock missing in platform_data\n");
  1225. return -EINVAL;
  1226. }
  1227. lp->indirect_lock = pdata->indirect_lock;
  1228. } else {
  1229. lp->indirect_lock = devm_kmalloc(&pdev->dev,
  1230. sizeof(*lp->indirect_lock),
  1231. GFP_KERNEL);
  1232. if (!lp->indirect_lock)
  1233. return -ENOMEM;
  1234. spin_lock_init(lp->indirect_lock);
  1235. }
  1236. /* map device registers */
  1237. lp->regs = devm_platform_ioremap_resource(pdev, 0);
  1238. if (IS_ERR(lp->regs)) {
  1239. dev_err(&pdev->dev, "could not map TEMAC registers\n");
  1240. return -ENOMEM;
  1241. }
  1242. /* Select register access functions with the specified
  1243. * endianness mode. Default for OF devices is big-endian.
  1244. */
  1245. little_endian = false;
  1246. if (temac_np)
  1247. little_endian = of_property_read_bool(temac_np, "little-endian");
  1248. else if (pdata)
  1249. little_endian = pdata->reg_little_endian;
  1250. if (little_endian) {
  1251. lp->temac_ior = _temac_ior_le;
  1252. lp->temac_iow = _temac_iow_le;
  1253. } else {
  1254. lp->temac_ior = _temac_ior_be;
  1255. lp->temac_iow = _temac_iow_be;
  1256. }
  1257. /* Setup checksum offload, but default to off if not specified */
  1258. lp->temac_features = 0;
  1259. if (temac_np) {
  1260. p = (__be32 *)of_get_property(temac_np, "xlnx,txcsum", NULL);
  1261. if (p && be32_to_cpu(*p))
  1262. lp->temac_features |= TEMAC_FEATURE_TX_CSUM;
  1263. p = (__be32 *)of_get_property(temac_np, "xlnx,rxcsum", NULL);
  1264. if (p && be32_to_cpu(*p))
  1265. lp->temac_features |= TEMAC_FEATURE_RX_CSUM;
  1266. } else if (pdata) {
  1267. if (pdata->txcsum)
  1268. lp->temac_features |= TEMAC_FEATURE_TX_CSUM;
  1269. if (pdata->rxcsum)
  1270. lp->temac_features |= TEMAC_FEATURE_RX_CSUM;
  1271. }
  1272. if (lp->temac_features & TEMAC_FEATURE_TX_CSUM)
  1273. /* Can checksum TCP/UDP over IPv4. */
  1274. ndev->features |= NETIF_F_IP_CSUM;
  1275. /* Defaults for IRQ delay/coalescing setup. These are
  1276. * configuration values, so does not belong in device-tree.
  1277. */
  1278. lp->coalesce_delay_tx = 0x10;
  1279. lp->coalesce_count_tx = 0x22;
  1280. lp->coalesce_delay_rx = 0xff;
  1281. lp->coalesce_count_rx = 0x07;
  1282. /* Setup LocalLink DMA */
  1283. if (temac_np) {
  1284. /* Find the DMA node, map the DMA registers, and
  1285. * decode the DMA IRQs.
  1286. */
  1287. dma_np = of_parse_phandle(temac_np, "llink-connected", 0);
  1288. if (!dma_np) {
  1289. dev_err(&pdev->dev, "could not find DMA node\n");
  1290. return -ENODEV;
  1291. }
  1292. /* Setup the DMA register accesses, could be DCR or
  1293. * memory mapped.
  1294. */
  1295. if (temac_dcr_setup(lp, pdev, dma_np)) {
  1296. /* no DCR in the device tree, try non-DCR */
  1297. lp->sdma_regs = devm_of_iomap(&pdev->dev, dma_np, 0,
  1298. NULL);
  1299. if (IS_ERR(lp->sdma_regs)) {
  1300. dev_err(&pdev->dev,
  1301. "unable to map DMA registers\n");
  1302. of_node_put(dma_np);
  1303. return PTR_ERR(lp->sdma_regs);
  1304. }
  1305. if (of_property_read_bool(dma_np, "little-endian")) {
  1306. lp->dma_in = temac_dma_in32_le;
  1307. lp->dma_out = temac_dma_out32_le;
  1308. } else {
  1309. lp->dma_in = temac_dma_in32_be;
  1310. lp->dma_out = temac_dma_out32_be;
  1311. }
  1312. dev_dbg(&pdev->dev, "MEM base: %p\n", lp->sdma_regs);
  1313. }
  1314. /* Get DMA RX and TX interrupts */
  1315. lp->rx_irq = irq_of_parse_and_map(dma_np, 0);
  1316. lp->tx_irq = irq_of_parse_and_map(dma_np, 1);
  1317. /* Finished with the DMA node; drop the reference */
  1318. of_node_put(dma_np);
  1319. } else if (pdata) {
  1320. /* 2nd memory resource specifies DMA registers */
  1321. lp->sdma_regs = devm_platform_ioremap_resource(pdev, 1);
  1322. if (IS_ERR(lp->sdma_regs)) {
  1323. dev_err(&pdev->dev,
  1324. "could not map DMA registers\n");
  1325. return PTR_ERR(lp->sdma_regs);
  1326. }
  1327. if (pdata->dma_little_endian) {
  1328. lp->dma_in = temac_dma_in32_le;
  1329. lp->dma_out = temac_dma_out32_le;
  1330. } else {
  1331. lp->dma_in = temac_dma_in32_be;
  1332. lp->dma_out = temac_dma_out32_be;
  1333. }
  1334. /* Get DMA RX and TX interrupts */
  1335. lp->rx_irq = platform_get_irq(pdev, 0);
  1336. lp->tx_irq = platform_get_irq(pdev, 1);
  1337. /* IRQ delay/coalescing setup */
  1338. if (pdata->tx_irq_timeout || pdata->tx_irq_count) {
  1339. lp->coalesce_delay_tx = pdata->tx_irq_timeout;
  1340. lp->coalesce_count_tx = pdata->tx_irq_count;
  1341. }
  1342. if (pdata->rx_irq_timeout || pdata->rx_irq_count) {
  1343. lp->coalesce_delay_rx = pdata->rx_irq_timeout;
  1344. lp->coalesce_count_rx = pdata->rx_irq_count;
  1345. }
  1346. }
  1347. /* Error handle returned DMA RX and TX interrupts */
  1348. if (lp->rx_irq <= 0) {
  1349. rc = lp->rx_irq ?: -EINVAL;
  1350. return dev_err_probe(&pdev->dev, rc,
  1351. "could not get DMA RX irq\n");
  1352. }
  1353. if (lp->tx_irq <= 0) {
  1354. rc = lp->tx_irq ?: -EINVAL;
  1355. return dev_err_probe(&pdev->dev, rc,
  1356. "could not get DMA TX irq\n");
  1357. }
  1358. if (temac_np) {
  1359. /* Retrieve the MAC address */
  1360. rc = of_get_mac_address(temac_np, addr);
  1361. if (rc) {
  1362. dev_err(&pdev->dev, "could not find MAC address\n");
  1363. return -ENODEV;
  1364. }
  1365. temac_init_mac_address(ndev, addr);
  1366. } else if (pdata) {
  1367. temac_init_mac_address(ndev, pdata->mac_addr);
  1368. }
  1369. rc = temac_mdio_setup(lp, pdev);
  1370. if (rc)
  1371. dev_warn(&pdev->dev, "error registering MDIO bus\n");
  1372. if (temac_np) {
  1373. lp->phy_node = of_parse_phandle(temac_np, "phy-handle", 0);
  1374. if (lp->phy_node)
  1375. dev_dbg(lp->dev, "using PHY node %pOF\n", lp->phy_node);
  1376. } else if (pdata) {
  1377. snprintf(lp->phy_name, sizeof(lp->phy_name),
  1378. PHY_ID_FMT, lp->mii_bus->id, pdata->phy_addr);
  1379. lp->phy_interface = pdata->phy_interface;
  1380. }
  1381. /* Add the device attributes */
  1382. rc = sysfs_create_group(&lp->dev->kobj, &temac_attr_group);
  1383. if (rc) {
  1384. dev_err(lp->dev, "Error creating sysfs files\n");
  1385. goto err_sysfs_create;
  1386. }
  1387. rc = register_netdev(lp->ndev);
  1388. if (rc) {
  1389. dev_err(lp->dev, "register_netdev() error (%i)\n", rc);
  1390. goto err_register_ndev;
  1391. }
  1392. return 0;
  1393. err_register_ndev:
  1394. sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
  1395. err_sysfs_create:
  1396. if (lp->phy_node)
  1397. of_node_put(lp->phy_node);
  1398. temac_mdio_teardown(lp);
  1399. return rc;
  1400. }
  1401. static void temac_remove(struct platform_device *pdev)
  1402. {
  1403. struct net_device *ndev = platform_get_drvdata(pdev);
  1404. struct temac_local *lp = netdev_priv(ndev);
  1405. unregister_netdev(ndev);
  1406. sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
  1407. if (lp->phy_node)
  1408. of_node_put(lp->phy_node);
  1409. temac_mdio_teardown(lp);
  1410. }
  1411. static const struct of_device_id temac_of_match[] = {
  1412. { .compatible = "xlnx,xps-ll-temac-1.01.b", },
  1413. { .compatible = "xlnx,xps-ll-temac-2.00.a", },
  1414. { .compatible = "xlnx,xps-ll-temac-2.02.a", },
  1415. { .compatible = "xlnx,xps-ll-temac-2.03.a", },
  1416. {},
  1417. };
  1418. MODULE_DEVICE_TABLE(of, temac_of_match);
  1419. static struct platform_driver temac_driver = {
  1420. .probe = temac_probe,
  1421. .remove = temac_remove,
  1422. .driver = {
  1423. .name = "xilinx_temac",
  1424. .of_match_table = temac_of_match,
  1425. },
  1426. };
  1427. module_platform_driver(temac_driver);
  1428. MODULE_DESCRIPTION("Xilinx LL_TEMAC Ethernet driver");
  1429. MODULE_AUTHOR("Yoshio Kashiwagi");
  1430. MODULE_LICENSE("GPL");