altera_tse_main.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636
  1. /* Altera Triple-Speed Ethernet MAC driver
  2. * Copyright (C) 2008-2014 Altera Corporation. All rights reserved
  3. *
  4. * Contributors:
  5. * Dalon Westergreen
  6. * Thomas Chou
  7. * Ian Abbott
  8. * Yuriy Kozlov
  9. * Tobias Klauser
  10. * Andriy Smolskyy
  11. * Roman Bulgakov
  12. * Dmytro Mytarchuk
  13. * Matthew Gerlach
  14. *
  15. * Original driver contributed by SLS.
  16. * Major updates contributed by GlobalLogic
  17. *
  18. * This program is free software; you can redistribute it and/or modify it
  19. * under the terms and conditions of the GNU General Public License,
  20. * version 2, as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope it will be useful, but WITHOUT
  23. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  24. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  25. * more details.
  26. *
  27. * You should have received a copy of the GNU General Public License along with
  28. * this program. If not, see <http://www.gnu.org/licenses/>.
  29. */
  30. #include <linux/atomic.h>
  31. #include <linux/delay.h>
  32. #include <linux/etherdevice.h>
  33. #include <linux/if_vlan.h>
  34. #include <linux/init.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/io.h>
  37. #include <linux/kernel.h>
  38. #include <linux/module.h>
  39. #include <linux/netdevice.h>
  40. #include <linux/of_device.h>
  41. #include <linux/of_mdio.h>
  42. #include <linux/of_net.h>
  43. #include <linux/of_platform.h>
  44. #include <linux/phy.h>
  45. #include <linux/platform_device.h>
  46. #include <linux/skbuff.h>
  47. #include <asm/cacheflush.h>
  48. #include "altera_utils.h"
  49. #include "altera_tse.h"
  50. #include "altera_sgdma.h"
  51. #include "altera_msgdma.h"
  52. static atomic_t instance_count = ATOMIC_INIT(~0);
  53. /* Module parameters */
  54. static int debug = -1;
  55. module_param(debug, int, S_IRUGO | S_IWUSR);
  56. MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)");
  57. static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
  58. NETIF_MSG_LINK | NETIF_MSG_IFUP |
  59. NETIF_MSG_IFDOWN);
  60. #define RX_DESCRIPTORS 64
  61. static int dma_rx_num = RX_DESCRIPTORS;
  62. module_param(dma_rx_num, int, S_IRUGO | S_IWUSR);
  63. MODULE_PARM_DESC(dma_rx_num, "Number of descriptors in the RX list");
  64. #define TX_DESCRIPTORS 64
  65. static int dma_tx_num = TX_DESCRIPTORS;
  66. module_param(dma_tx_num, int, S_IRUGO | S_IWUSR);
  67. MODULE_PARM_DESC(dma_tx_num, "Number of descriptors in the TX list");
  68. #define POLL_PHY (-1)
  69. /* Make sure DMA buffer size is larger than the max frame size
  70. * plus some alignment offset and a VLAN header. If the max frame size is
  71. * 1518, a VLAN header would be additional 4 bytes and additional
  72. * headroom for alignment is 2 bytes, 2048 is just fine.
  73. */
  74. #define ALTERA_RXDMABUFFER_SIZE 2048
  75. /* Allow network stack to resume queueing packets after we've
  76. * finished transmitting at least 1/4 of the packets in the queue.
  77. */
  78. #define TSE_TX_THRESH(x) (x->tx_ring_size / 4)
  79. #define TXQUEUESTOP_THRESHHOLD 2
  80. static const struct of_device_id altera_tse_ids[];
  81. static inline u32 tse_tx_avail(struct altera_tse_private *priv)
  82. {
  83. return priv->tx_cons + priv->tx_ring_size - priv->tx_prod - 1;
  84. }
  85. /* MDIO specific functions
  86. */
  87. static int altera_tse_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  88. {
  89. struct net_device *ndev = bus->priv;
  90. struct altera_tse_private *priv = netdev_priv(ndev);
  91. /* set MDIO address */
  92. csrwr32((mii_id & 0x1f), priv->mac_dev,
  93. tse_csroffs(mdio_phy1_addr));
  94. /* get the data */
  95. return csrrd32(priv->mac_dev,
  96. tse_csroffs(mdio_phy1) + regnum * 4) & 0xffff;
  97. }
  98. static int altera_tse_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
  99. u16 value)
  100. {
  101. struct net_device *ndev = bus->priv;
  102. struct altera_tse_private *priv = netdev_priv(ndev);
  103. /* set MDIO address */
  104. csrwr32((mii_id & 0x1f), priv->mac_dev,
  105. tse_csroffs(mdio_phy1_addr));
  106. /* write the data */
  107. csrwr32(value, priv->mac_dev, tse_csroffs(mdio_phy1) + regnum * 4);
  108. return 0;
  109. }
  110. static int altera_tse_mdio_create(struct net_device *dev, unsigned int id)
  111. {
  112. struct altera_tse_private *priv = netdev_priv(dev);
  113. int ret;
  114. int i;
  115. struct device_node *mdio_node = NULL;
  116. struct mii_bus *mdio = NULL;
  117. struct device_node *child_node = NULL;
  118. for_each_child_of_node(priv->device->of_node, child_node) {
  119. if (of_device_is_compatible(child_node, "altr,tse-mdio")) {
  120. mdio_node = child_node;
  121. break;
  122. }
  123. }
  124. if (mdio_node) {
  125. netdev_dbg(dev, "FOUND MDIO subnode\n");
  126. } else {
  127. netdev_dbg(dev, "NO MDIO subnode\n");
  128. return 0;
  129. }
  130. mdio = mdiobus_alloc();
  131. if (mdio == NULL) {
  132. netdev_err(dev, "Error allocating MDIO bus\n");
  133. return -ENOMEM;
  134. }
  135. mdio->name = ALTERA_TSE_RESOURCE_NAME;
  136. mdio->read = &altera_tse_mdio_read;
  137. mdio->write = &altera_tse_mdio_write;
  138. snprintf(mdio->id, MII_BUS_ID_SIZE, "%s-%u", mdio->name, id);
  139. mdio->irq = kcalloc(PHY_MAX_ADDR, sizeof(int), GFP_KERNEL);
  140. if (mdio->irq == NULL) {
  141. ret = -ENOMEM;
  142. goto out_free_mdio;
  143. }
  144. for (i = 0; i < PHY_MAX_ADDR; i++)
  145. mdio->irq[i] = PHY_POLL;
  146. mdio->priv = dev;
  147. mdio->parent = priv->device;
  148. ret = of_mdiobus_register(mdio, mdio_node);
  149. if (ret != 0) {
  150. netdev_err(dev, "Cannot register MDIO bus %s\n",
  151. mdio->id);
  152. goto out_free_mdio_irq;
  153. }
  154. if (netif_msg_drv(priv))
  155. netdev_info(dev, "MDIO bus %s: created\n", mdio->id);
  156. priv->mdio = mdio;
  157. return 0;
  158. out_free_mdio_irq:
  159. kfree(mdio->irq);
  160. out_free_mdio:
  161. mdiobus_free(mdio);
  162. mdio = NULL;
  163. return ret;
  164. }
  165. static void altera_tse_mdio_destroy(struct net_device *dev)
  166. {
  167. struct altera_tse_private *priv = netdev_priv(dev);
  168. if (priv->mdio == NULL)
  169. return;
  170. if (netif_msg_drv(priv))
  171. netdev_info(dev, "MDIO bus %s: removed\n",
  172. priv->mdio->id);
  173. mdiobus_unregister(priv->mdio);
  174. kfree(priv->mdio->irq);
  175. mdiobus_free(priv->mdio);
  176. priv->mdio = NULL;
  177. }
  178. static int tse_init_rx_buffer(struct altera_tse_private *priv,
  179. struct tse_buffer *rxbuffer, int len)
  180. {
  181. rxbuffer->skb = netdev_alloc_skb_ip_align(priv->dev, len);
  182. if (!rxbuffer->skb)
  183. return -ENOMEM;
  184. rxbuffer->dma_addr = dma_map_single(priv->device, rxbuffer->skb->data,
  185. len,
  186. DMA_FROM_DEVICE);
  187. if (dma_mapping_error(priv->device, rxbuffer->dma_addr)) {
  188. netdev_err(priv->dev, "%s: DMA mapping error\n", __func__);
  189. dev_kfree_skb_any(rxbuffer->skb);
  190. return -EINVAL;
  191. }
  192. rxbuffer->dma_addr &= (dma_addr_t)~3;
  193. rxbuffer->len = len;
  194. return 0;
  195. }
  196. static void tse_free_rx_buffer(struct altera_tse_private *priv,
  197. struct tse_buffer *rxbuffer)
  198. {
  199. struct sk_buff *skb = rxbuffer->skb;
  200. dma_addr_t dma_addr = rxbuffer->dma_addr;
  201. if (skb != NULL) {
  202. if (dma_addr)
  203. dma_unmap_single(priv->device, dma_addr,
  204. rxbuffer->len,
  205. DMA_FROM_DEVICE);
  206. dev_kfree_skb_any(skb);
  207. rxbuffer->skb = NULL;
  208. rxbuffer->dma_addr = 0;
  209. }
  210. }
  211. /* Unmap and free Tx buffer resources
  212. */
  213. static void tse_free_tx_buffer(struct altera_tse_private *priv,
  214. struct tse_buffer *buffer)
  215. {
  216. if (buffer->dma_addr) {
  217. if (buffer->mapped_as_page)
  218. dma_unmap_page(priv->device, buffer->dma_addr,
  219. buffer->len, DMA_TO_DEVICE);
  220. else
  221. dma_unmap_single(priv->device, buffer->dma_addr,
  222. buffer->len, DMA_TO_DEVICE);
  223. buffer->dma_addr = 0;
  224. }
  225. if (buffer->skb) {
  226. dev_kfree_skb_any(buffer->skb);
  227. buffer->skb = NULL;
  228. }
  229. }
  230. static int alloc_init_skbufs(struct altera_tse_private *priv)
  231. {
  232. unsigned int rx_descs = priv->rx_ring_size;
  233. unsigned int tx_descs = priv->tx_ring_size;
  234. int ret = -ENOMEM;
  235. int i;
  236. /* Create Rx ring buffer */
  237. priv->rx_ring = kcalloc(rx_descs, sizeof(struct tse_buffer),
  238. GFP_KERNEL);
  239. if (!priv->rx_ring)
  240. goto err_rx_ring;
  241. /* Create Tx ring buffer */
  242. priv->tx_ring = kcalloc(tx_descs, sizeof(struct tse_buffer),
  243. GFP_KERNEL);
  244. if (!priv->tx_ring)
  245. goto err_tx_ring;
  246. priv->tx_cons = 0;
  247. priv->tx_prod = 0;
  248. /* Init Rx ring */
  249. for (i = 0; i < rx_descs; i++) {
  250. ret = tse_init_rx_buffer(priv, &priv->rx_ring[i],
  251. priv->rx_dma_buf_sz);
  252. if (ret)
  253. goto err_init_rx_buffers;
  254. }
  255. priv->rx_cons = 0;
  256. priv->rx_prod = 0;
  257. return 0;
  258. err_init_rx_buffers:
  259. while (--i >= 0)
  260. tse_free_rx_buffer(priv, &priv->rx_ring[i]);
  261. kfree(priv->tx_ring);
  262. err_tx_ring:
  263. kfree(priv->rx_ring);
  264. err_rx_ring:
  265. return ret;
  266. }
  267. static void free_skbufs(struct net_device *dev)
  268. {
  269. struct altera_tse_private *priv = netdev_priv(dev);
  270. unsigned int rx_descs = priv->rx_ring_size;
  271. unsigned int tx_descs = priv->tx_ring_size;
  272. int i;
  273. /* Release the DMA TX/RX socket buffers */
  274. for (i = 0; i < rx_descs; i++)
  275. tse_free_rx_buffer(priv, &priv->rx_ring[i]);
  276. for (i = 0; i < tx_descs; i++)
  277. tse_free_tx_buffer(priv, &priv->tx_ring[i]);
  278. kfree(priv->tx_ring);
  279. }
  280. /* Reallocate the skb for the reception process
  281. */
  282. static inline void tse_rx_refill(struct altera_tse_private *priv)
  283. {
  284. unsigned int rxsize = priv->rx_ring_size;
  285. unsigned int entry;
  286. int ret;
  287. for (; priv->rx_cons - priv->rx_prod > 0;
  288. priv->rx_prod++) {
  289. entry = priv->rx_prod % rxsize;
  290. if (likely(priv->rx_ring[entry].skb == NULL)) {
  291. ret = tse_init_rx_buffer(priv, &priv->rx_ring[entry],
  292. priv->rx_dma_buf_sz);
  293. if (unlikely(ret != 0))
  294. break;
  295. priv->dmaops->add_rx_desc(priv, &priv->rx_ring[entry]);
  296. }
  297. }
  298. }
  299. /* Pull out the VLAN tag and fix up the packet
  300. */
  301. static inline void tse_rx_vlan(struct net_device *dev, struct sk_buff *skb)
  302. {
  303. struct ethhdr *eth_hdr;
  304. u16 vid;
  305. if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
  306. !__vlan_get_tag(skb, &vid)) {
  307. eth_hdr = (struct ethhdr *)skb->data;
  308. memmove(skb->data + VLAN_HLEN, eth_hdr, ETH_ALEN * 2);
  309. skb_pull(skb, VLAN_HLEN);
  310. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
  311. }
  312. }
  313. /* Receive a packet: retrieve and pass over to upper levels
  314. */
  315. static int tse_rx(struct altera_tse_private *priv, int limit)
  316. {
  317. unsigned int count = 0;
  318. unsigned int next_entry;
  319. struct sk_buff *skb;
  320. unsigned int entry = priv->rx_cons % priv->rx_ring_size;
  321. u32 rxstatus;
  322. u16 pktlength;
  323. u16 pktstatus;
  324. /* Check for count < limit first as get_rx_status is changing
  325. * the response-fifo so we must process the next packet
  326. * after calling get_rx_status if a response is pending.
  327. * (reading the last byte of the response pops the value from the fifo.)
  328. */
  329. while ((count < limit) &&
  330. ((rxstatus = priv->dmaops->get_rx_status(priv)) != 0)) {
  331. pktstatus = rxstatus >> 16;
  332. pktlength = rxstatus & 0xffff;
  333. if ((pktstatus & 0xFF) || (pktlength == 0))
  334. netdev_err(priv->dev,
  335. "RCV pktstatus %08X pktlength %08X\n",
  336. pktstatus, pktlength);
  337. /* DMA trasfer from TSE starts with 2 aditional bytes for
  338. * IP payload alignment. Status returned by get_rx_status()
  339. * contains DMA transfer length. Packet is 2 bytes shorter.
  340. */
  341. pktlength -= 2;
  342. count++;
  343. next_entry = (++priv->rx_cons) % priv->rx_ring_size;
  344. skb = priv->rx_ring[entry].skb;
  345. if (unlikely(!skb)) {
  346. netdev_err(priv->dev,
  347. "%s: Inconsistent Rx descriptor chain\n",
  348. __func__);
  349. priv->dev->stats.rx_dropped++;
  350. break;
  351. }
  352. priv->rx_ring[entry].skb = NULL;
  353. skb_put(skb, pktlength);
  354. /* make cache consistent with receive packet buffer */
  355. dma_sync_single_for_cpu(priv->device,
  356. priv->rx_ring[entry].dma_addr,
  357. priv->rx_ring[entry].len,
  358. DMA_FROM_DEVICE);
  359. dma_unmap_single(priv->device, priv->rx_ring[entry].dma_addr,
  360. priv->rx_ring[entry].len, DMA_FROM_DEVICE);
  361. if (netif_msg_pktdata(priv)) {
  362. netdev_info(priv->dev, "frame received %d bytes\n",
  363. pktlength);
  364. print_hex_dump(KERN_ERR, "data: ", DUMP_PREFIX_OFFSET,
  365. 16, 1, skb->data, pktlength, true);
  366. }
  367. tse_rx_vlan(priv->dev, skb);
  368. skb->protocol = eth_type_trans(skb, priv->dev);
  369. skb_checksum_none_assert(skb);
  370. napi_gro_receive(&priv->napi, skb);
  371. priv->dev->stats.rx_packets++;
  372. priv->dev->stats.rx_bytes += pktlength;
  373. entry = next_entry;
  374. tse_rx_refill(priv);
  375. }
  376. return count;
  377. }
  378. /* Reclaim resources after transmission completes
  379. */
  380. static int tse_tx_complete(struct altera_tse_private *priv)
  381. {
  382. unsigned int txsize = priv->tx_ring_size;
  383. u32 ready;
  384. unsigned int entry;
  385. struct tse_buffer *tx_buff;
  386. int txcomplete = 0;
  387. spin_lock(&priv->tx_lock);
  388. ready = priv->dmaops->tx_completions(priv);
  389. /* Free sent buffers */
  390. while (ready && (priv->tx_cons != priv->tx_prod)) {
  391. entry = priv->tx_cons % txsize;
  392. tx_buff = &priv->tx_ring[entry];
  393. if (netif_msg_tx_done(priv))
  394. netdev_dbg(priv->dev, "%s: curr %d, dirty %d\n",
  395. __func__, priv->tx_prod, priv->tx_cons);
  396. if (likely(tx_buff->skb))
  397. priv->dev->stats.tx_packets++;
  398. tse_free_tx_buffer(priv, tx_buff);
  399. priv->tx_cons++;
  400. txcomplete++;
  401. ready--;
  402. }
  403. if (unlikely(netif_queue_stopped(priv->dev) &&
  404. tse_tx_avail(priv) > TSE_TX_THRESH(priv))) {
  405. netif_tx_lock(priv->dev);
  406. if (netif_queue_stopped(priv->dev) &&
  407. tse_tx_avail(priv) > TSE_TX_THRESH(priv)) {
  408. if (netif_msg_tx_done(priv))
  409. netdev_dbg(priv->dev, "%s: restart transmit\n",
  410. __func__);
  411. netif_wake_queue(priv->dev);
  412. }
  413. netif_tx_unlock(priv->dev);
  414. }
  415. spin_unlock(&priv->tx_lock);
  416. return txcomplete;
  417. }
  418. /* NAPI polling function
  419. */
  420. static int tse_poll(struct napi_struct *napi, int budget)
  421. {
  422. struct altera_tse_private *priv =
  423. container_of(napi, struct altera_tse_private, napi);
  424. int rxcomplete = 0;
  425. unsigned long int flags;
  426. tse_tx_complete(priv);
  427. rxcomplete = tse_rx(priv, budget);
  428. if (rxcomplete < budget) {
  429. napi_complete(napi);
  430. netdev_dbg(priv->dev,
  431. "NAPI Complete, did %d packets with budget %d\n",
  432. rxcomplete, budget);
  433. spin_lock_irqsave(&priv->rxdma_irq_lock, flags);
  434. priv->dmaops->enable_rxirq(priv);
  435. priv->dmaops->enable_txirq(priv);
  436. spin_unlock_irqrestore(&priv->rxdma_irq_lock, flags);
  437. }
  438. return rxcomplete;
  439. }
  440. /* DMA TX & RX FIFO interrupt routing
  441. */
  442. static irqreturn_t altera_isr(int irq, void *dev_id)
  443. {
  444. struct net_device *dev = dev_id;
  445. struct altera_tse_private *priv;
  446. if (unlikely(!dev)) {
  447. pr_err("%s: invalid dev pointer\n", __func__);
  448. return IRQ_NONE;
  449. }
  450. priv = netdev_priv(dev);
  451. spin_lock(&priv->rxdma_irq_lock);
  452. /* reset IRQs */
  453. priv->dmaops->clear_rxirq(priv);
  454. priv->dmaops->clear_txirq(priv);
  455. spin_unlock(&priv->rxdma_irq_lock);
  456. if (likely(napi_schedule_prep(&priv->napi))) {
  457. spin_lock(&priv->rxdma_irq_lock);
  458. priv->dmaops->disable_rxirq(priv);
  459. priv->dmaops->disable_txirq(priv);
  460. spin_unlock(&priv->rxdma_irq_lock);
  461. __napi_schedule(&priv->napi);
  462. }
  463. return IRQ_HANDLED;
  464. }
  465. /* Transmit a packet (called by the kernel). Dispatches
  466. * either the SGDMA method for transmitting or the
  467. * MSGDMA method, assumes no scatter/gather support,
  468. * implying an assumption that there's only one
  469. * physically contiguous fragment starting at
  470. * skb->data, for length of skb_headlen(skb).
  471. */
  472. static int tse_start_xmit(struct sk_buff *skb, struct net_device *dev)
  473. {
  474. struct altera_tse_private *priv = netdev_priv(dev);
  475. unsigned int txsize = priv->tx_ring_size;
  476. unsigned int entry;
  477. struct tse_buffer *buffer = NULL;
  478. int nfrags = skb_shinfo(skb)->nr_frags;
  479. unsigned int nopaged_len = skb_headlen(skb);
  480. enum netdev_tx ret = NETDEV_TX_OK;
  481. dma_addr_t dma_addr;
  482. spin_lock_bh(&priv->tx_lock);
  483. if (unlikely(tse_tx_avail(priv) < nfrags + 1)) {
  484. if (!netif_queue_stopped(dev)) {
  485. netif_stop_queue(dev);
  486. /* This is a hard error, log it. */
  487. netdev_err(priv->dev,
  488. "%s: Tx list full when queue awake\n",
  489. __func__);
  490. }
  491. ret = NETDEV_TX_BUSY;
  492. goto out;
  493. }
  494. /* Map the first skb fragment */
  495. entry = priv->tx_prod % txsize;
  496. buffer = &priv->tx_ring[entry];
  497. dma_addr = dma_map_single(priv->device, skb->data, nopaged_len,
  498. DMA_TO_DEVICE);
  499. if (dma_mapping_error(priv->device, dma_addr)) {
  500. netdev_err(priv->dev, "%s: DMA mapping error\n", __func__);
  501. ret = NETDEV_TX_OK;
  502. goto out;
  503. }
  504. buffer->skb = skb;
  505. buffer->dma_addr = dma_addr;
  506. buffer->len = nopaged_len;
  507. /* Push data out of the cache hierarchy into main memory */
  508. dma_sync_single_for_device(priv->device, buffer->dma_addr,
  509. buffer->len, DMA_TO_DEVICE);
  510. priv->dmaops->tx_buffer(priv, buffer);
  511. skb_tx_timestamp(skb);
  512. priv->tx_prod++;
  513. dev->stats.tx_bytes += skb->len;
  514. if (unlikely(tse_tx_avail(priv) <= TXQUEUESTOP_THRESHHOLD)) {
  515. if (netif_msg_hw(priv))
  516. netdev_dbg(priv->dev, "%s: stop transmitted packets\n",
  517. __func__);
  518. netif_stop_queue(dev);
  519. }
  520. out:
  521. spin_unlock_bh(&priv->tx_lock);
  522. return ret;
  523. }
  524. /* Called every time the controller might need to be made
  525. * aware of new link state. The PHY code conveys this
  526. * information through variables in the phydev structure, and this
  527. * function converts those variables into the appropriate
  528. * register values, and can bring down the device if needed.
  529. */
  530. static void altera_tse_adjust_link(struct net_device *dev)
  531. {
  532. struct altera_tse_private *priv = netdev_priv(dev);
  533. struct phy_device *phydev = priv->phydev;
  534. int new_state = 0;
  535. /* only change config if there is a link */
  536. spin_lock(&priv->mac_cfg_lock);
  537. if (phydev->link) {
  538. /* Read old config */
  539. u32 cfg_reg = ioread32(&priv->mac_dev->command_config);
  540. /* Check duplex */
  541. if (phydev->duplex != priv->oldduplex) {
  542. new_state = 1;
  543. if (!(phydev->duplex))
  544. cfg_reg |= MAC_CMDCFG_HD_ENA;
  545. else
  546. cfg_reg &= ~MAC_CMDCFG_HD_ENA;
  547. netdev_dbg(priv->dev, "%s: Link duplex = 0x%x\n",
  548. dev->name, phydev->duplex);
  549. priv->oldduplex = phydev->duplex;
  550. }
  551. /* Check speed */
  552. if (phydev->speed != priv->oldspeed) {
  553. new_state = 1;
  554. switch (phydev->speed) {
  555. case 1000:
  556. cfg_reg |= MAC_CMDCFG_ETH_SPEED;
  557. cfg_reg &= ~MAC_CMDCFG_ENA_10;
  558. break;
  559. case 100:
  560. cfg_reg &= ~MAC_CMDCFG_ETH_SPEED;
  561. cfg_reg &= ~MAC_CMDCFG_ENA_10;
  562. break;
  563. case 10:
  564. cfg_reg &= ~MAC_CMDCFG_ETH_SPEED;
  565. cfg_reg |= MAC_CMDCFG_ENA_10;
  566. break;
  567. default:
  568. if (netif_msg_link(priv))
  569. netdev_warn(dev, "Speed (%d) is not 10/100/1000!\n",
  570. phydev->speed);
  571. break;
  572. }
  573. priv->oldspeed = phydev->speed;
  574. }
  575. iowrite32(cfg_reg, &priv->mac_dev->command_config);
  576. if (!priv->oldlink) {
  577. new_state = 1;
  578. priv->oldlink = 1;
  579. }
  580. } else if (priv->oldlink) {
  581. new_state = 1;
  582. priv->oldlink = 0;
  583. priv->oldspeed = 0;
  584. priv->oldduplex = -1;
  585. }
  586. if (new_state && netif_msg_link(priv))
  587. phy_print_status(phydev);
  588. spin_unlock(&priv->mac_cfg_lock);
  589. }
  590. static struct phy_device *connect_local_phy(struct net_device *dev)
  591. {
  592. struct altera_tse_private *priv = netdev_priv(dev);
  593. struct phy_device *phydev = NULL;
  594. char phy_id_fmt[MII_BUS_ID_SIZE + 3];
  595. if (priv->phy_addr != POLL_PHY) {
  596. snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT,
  597. priv->mdio->id, priv->phy_addr);
  598. netdev_dbg(dev, "trying to attach to %s\n", phy_id_fmt);
  599. phydev = phy_connect(dev, phy_id_fmt, &altera_tse_adjust_link,
  600. priv->phy_iface);
  601. if (IS_ERR(phydev)) {
  602. netdev_err(dev, "Could not attach to PHY\n");
  603. phydev = NULL;
  604. }
  605. } else {
  606. int ret;
  607. phydev = phy_find_first(priv->mdio);
  608. if (phydev == NULL) {
  609. netdev_err(dev, "No PHY found\n");
  610. return phydev;
  611. }
  612. ret = phy_connect_direct(dev, phydev, &altera_tse_adjust_link,
  613. priv->phy_iface);
  614. if (ret != 0) {
  615. netdev_err(dev, "Could not attach to PHY\n");
  616. phydev = NULL;
  617. }
  618. }
  619. return phydev;
  620. }
  621. static int altera_tse_phy_get_addr_mdio_create(struct net_device *dev)
  622. {
  623. struct altera_tse_private *priv = netdev_priv(dev);
  624. struct device_node *np = priv->device->of_node;
  625. int ret = 0;
  626. priv->phy_iface = of_get_phy_mode(np);
  627. /* Avoid get phy addr and create mdio if no phy is present */
  628. if (!priv->phy_iface)
  629. return 0;
  630. /* try to get PHY address from device tree, use PHY autodetection if
  631. * no valid address is given
  632. */
  633. if (of_property_read_u32(priv->device->of_node, "phy-addr",
  634. &priv->phy_addr)) {
  635. priv->phy_addr = POLL_PHY;
  636. }
  637. if (!((priv->phy_addr == POLL_PHY) ||
  638. ((priv->phy_addr >= 0) && (priv->phy_addr < PHY_MAX_ADDR)))) {
  639. netdev_err(dev, "invalid phy-addr specified %d\n",
  640. priv->phy_addr);
  641. return -ENODEV;
  642. }
  643. /* Create/attach to MDIO bus */
  644. ret = altera_tse_mdio_create(dev,
  645. atomic_add_return(1, &instance_count));
  646. if (ret)
  647. return -ENODEV;
  648. return 0;
  649. }
  650. /* Initialize driver's PHY state, and attach to the PHY
  651. */
  652. static int init_phy(struct net_device *dev)
  653. {
  654. struct altera_tse_private *priv = netdev_priv(dev);
  655. struct phy_device *phydev;
  656. struct device_node *phynode;
  657. bool fixed_link = false;
  658. int rc = 0;
  659. /* Avoid init phy in case of no phy present */
  660. if (!priv->phy_iface)
  661. return 0;
  662. priv->oldlink = 0;
  663. priv->oldspeed = 0;
  664. priv->oldduplex = -1;
  665. phynode = of_parse_phandle(priv->device->of_node, "phy-handle", 0);
  666. if (!phynode) {
  667. /* check if a fixed-link is defined in device-tree */
  668. if (of_phy_is_fixed_link(priv->device->of_node)) {
  669. rc = of_phy_register_fixed_link(priv->device->of_node);
  670. if (rc < 0) {
  671. netdev_err(dev, "cannot register fixed PHY\n");
  672. return rc;
  673. }
  674. /* In the case of a fixed PHY, the DT node associated
  675. * to the PHY is the Ethernet MAC DT node.
  676. */
  677. phynode = of_node_get(priv->device->of_node);
  678. fixed_link = true;
  679. netdev_dbg(dev, "fixed-link detected\n");
  680. phydev = of_phy_connect(dev, phynode,
  681. &altera_tse_adjust_link,
  682. 0, priv->phy_iface);
  683. } else {
  684. netdev_dbg(dev, "no phy-handle found\n");
  685. if (!priv->mdio) {
  686. netdev_err(dev, "No phy-handle nor local mdio specified\n");
  687. return -ENODEV;
  688. }
  689. phydev = connect_local_phy(dev);
  690. }
  691. } else {
  692. netdev_dbg(dev, "phy-handle found\n");
  693. phydev = of_phy_connect(dev, phynode,
  694. &altera_tse_adjust_link, 0, priv->phy_iface);
  695. }
  696. if (!phydev) {
  697. netdev_err(dev, "Could not find the PHY\n");
  698. return -ENODEV;
  699. }
  700. /* Stop Advertising 1000BASE Capability if interface is not GMII
  701. * Note: Checkpatch throws CHECKs for the camel case defines below,
  702. * it's ok to ignore.
  703. */
  704. if ((priv->phy_iface == PHY_INTERFACE_MODE_MII) ||
  705. (priv->phy_iface == PHY_INTERFACE_MODE_RMII))
  706. phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
  707. SUPPORTED_1000baseT_Full);
  708. /* Broken HW is sometimes missing the pull-up resistor on the
  709. * MDIO line, which results in reads to non-existent devices returning
  710. * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
  711. * device as well. If a fixed-link is used the phy_id is always 0.
  712. * Note: phydev->phy_id is the result of reading the UID PHY registers.
  713. */
  714. if ((phydev->phy_id == 0) && !fixed_link) {
  715. netdev_err(dev, "Bad PHY UID 0x%08x\n", phydev->phy_id);
  716. phy_disconnect(phydev);
  717. return -ENODEV;
  718. }
  719. netdev_dbg(dev, "attached to PHY %d UID 0x%08x Link = %d\n",
  720. phydev->addr, phydev->phy_id, phydev->link);
  721. priv->phydev = phydev;
  722. return 0;
  723. }
  724. static void tse_update_mac_addr(struct altera_tse_private *priv, u8 *addr)
  725. {
  726. u32 msb;
  727. u32 lsb;
  728. msb = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
  729. lsb = ((addr[5] << 8) | addr[4]) & 0xffff;
  730. /* Set primary MAC address */
  731. csrwr32(msb, priv->mac_dev, tse_csroffs(mac_addr_0));
  732. csrwr32(lsb, priv->mac_dev, tse_csroffs(mac_addr_1));
  733. }
  734. /* MAC software reset.
  735. * When reset is triggered, the MAC function completes the current
  736. * transmission or reception, and subsequently disables the transmit and
  737. * receive logic, flushes the receive FIFO buffer, and resets the statistics
  738. * counters.
  739. */
  740. static int reset_mac(struct altera_tse_private *priv)
  741. {
  742. int counter;
  743. u32 dat;
  744. dat = csrrd32(priv->mac_dev, tse_csroffs(command_config));
  745. dat &= ~(MAC_CMDCFG_TX_ENA | MAC_CMDCFG_RX_ENA);
  746. dat |= MAC_CMDCFG_SW_RESET | MAC_CMDCFG_CNT_RESET;
  747. csrwr32(dat, priv->mac_dev, tse_csroffs(command_config));
  748. counter = 0;
  749. while (counter++ < ALTERA_TSE_SW_RESET_WATCHDOG_CNTR) {
  750. if (tse_bit_is_clear(priv->mac_dev, tse_csroffs(command_config),
  751. MAC_CMDCFG_SW_RESET))
  752. break;
  753. udelay(1);
  754. }
  755. if (counter >= ALTERA_TSE_SW_RESET_WATCHDOG_CNTR) {
  756. dat = csrrd32(priv->mac_dev, tse_csroffs(command_config));
  757. dat &= ~MAC_CMDCFG_SW_RESET;
  758. csrwr32(dat, priv->mac_dev, tse_csroffs(command_config));
  759. return -1;
  760. }
  761. return 0;
  762. }
  763. /* Initialize MAC core registers
  764. */
  765. static int init_mac(struct altera_tse_private *priv)
  766. {
  767. unsigned int cmd = 0;
  768. u32 frm_length;
  769. /* Setup Rx FIFO */
  770. csrwr32(priv->rx_fifo_depth - ALTERA_TSE_RX_SECTION_EMPTY,
  771. priv->mac_dev, tse_csroffs(rx_section_empty));
  772. csrwr32(ALTERA_TSE_RX_SECTION_FULL, priv->mac_dev,
  773. tse_csroffs(rx_section_full));
  774. csrwr32(ALTERA_TSE_RX_ALMOST_EMPTY, priv->mac_dev,
  775. tse_csroffs(rx_almost_empty));
  776. csrwr32(ALTERA_TSE_RX_ALMOST_FULL, priv->mac_dev,
  777. tse_csroffs(rx_almost_full));
  778. /* Setup Tx FIFO */
  779. csrwr32(priv->tx_fifo_depth - ALTERA_TSE_TX_SECTION_EMPTY,
  780. priv->mac_dev, tse_csroffs(tx_section_empty));
  781. csrwr32(ALTERA_TSE_TX_SECTION_FULL, priv->mac_dev,
  782. tse_csroffs(tx_section_full));
  783. csrwr32(ALTERA_TSE_TX_ALMOST_EMPTY, priv->mac_dev,
  784. tse_csroffs(tx_almost_empty));
  785. csrwr32(ALTERA_TSE_TX_ALMOST_FULL, priv->mac_dev,
  786. tse_csroffs(tx_almost_full));
  787. /* MAC Address Configuration */
  788. tse_update_mac_addr(priv, priv->dev->dev_addr);
  789. /* MAC Function Configuration */
  790. frm_length = ETH_HLEN + priv->dev->mtu + ETH_FCS_LEN;
  791. csrwr32(frm_length, priv->mac_dev, tse_csroffs(frm_length));
  792. csrwr32(ALTERA_TSE_TX_IPG_LENGTH, priv->mac_dev,
  793. tse_csroffs(tx_ipg_length));
  794. /* Disable RX/TX shift 16 for alignment of all received frames on 16-bit
  795. * start address
  796. */
  797. tse_set_bit(priv->mac_dev, tse_csroffs(rx_cmd_stat),
  798. ALTERA_TSE_RX_CMD_STAT_RX_SHIFT16);
  799. tse_clear_bit(priv->mac_dev, tse_csroffs(tx_cmd_stat),
  800. ALTERA_TSE_TX_CMD_STAT_TX_SHIFT16 |
  801. ALTERA_TSE_TX_CMD_STAT_OMIT_CRC);
  802. /* Set the MAC options */
  803. cmd = csrrd32(priv->mac_dev, tse_csroffs(command_config));
  804. cmd &= ~MAC_CMDCFG_PAD_EN; /* No padding Removal on Receive */
  805. cmd &= ~MAC_CMDCFG_CRC_FWD; /* CRC Removal */
  806. cmd |= MAC_CMDCFG_RX_ERR_DISC; /* Automatically discard frames
  807. * with CRC errors
  808. */
  809. cmd |= MAC_CMDCFG_CNTL_FRM_ENA;
  810. cmd &= ~MAC_CMDCFG_TX_ENA;
  811. cmd &= ~MAC_CMDCFG_RX_ENA;
  812. /* Default speed and duplex setting, full/100 */
  813. cmd &= ~MAC_CMDCFG_HD_ENA;
  814. cmd &= ~MAC_CMDCFG_ETH_SPEED;
  815. cmd &= ~MAC_CMDCFG_ENA_10;
  816. csrwr32(cmd, priv->mac_dev, tse_csroffs(command_config));
  817. csrwr32(ALTERA_TSE_PAUSE_QUANTA, priv->mac_dev,
  818. tse_csroffs(pause_quanta));
  819. if (netif_msg_hw(priv))
  820. dev_dbg(priv->device,
  821. "MAC post-initialization: CMD_CONFIG = 0x%08x\n", cmd);
  822. return 0;
  823. }
  824. /* Start/stop MAC transmission logic
  825. */
  826. static void tse_set_mac(struct altera_tse_private *priv, bool enable)
  827. {
  828. u32 value = csrrd32(priv->mac_dev, tse_csroffs(command_config));
  829. if (enable)
  830. value |= MAC_CMDCFG_TX_ENA | MAC_CMDCFG_RX_ENA;
  831. else
  832. value &= ~(MAC_CMDCFG_TX_ENA | MAC_CMDCFG_RX_ENA);
  833. csrwr32(value, priv->mac_dev, tse_csroffs(command_config));
  834. }
  835. /* Change the MTU
  836. */
  837. static int tse_change_mtu(struct net_device *dev, int new_mtu)
  838. {
  839. struct altera_tse_private *priv = netdev_priv(dev);
  840. unsigned int max_mtu = priv->max_mtu;
  841. unsigned int min_mtu = ETH_ZLEN + ETH_FCS_LEN;
  842. if (netif_running(dev)) {
  843. netdev_err(dev, "must be stopped to change its MTU\n");
  844. return -EBUSY;
  845. }
  846. if ((new_mtu < min_mtu) || (new_mtu > max_mtu)) {
  847. netdev_err(dev, "invalid MTU, max MTU is: %u\n", max_mtu);
  848. return -EINVAL;
  849. }
  850. dev->mtu = new_mtu;
  851. netdev_update_features(dev);
  852. return 0;
  853. }
  854. static void altera_tse_set_mcfilter(struct net_device *dev)
  855. {
  856. struct altera_tse_private *priv = netdev_priv(dev);
  857. int i;
  858. struct netdev_hw_addr *ha;
  859. /* clear the hash filter */
  860. for (i = 0; i < 64; i++)
  861. csrwr32(0, priv->mac_dev, tse_csroffs(hash_table) + i * 4);
  862. netdev_for_each_mc_addr(ha, dev) {
  863. unsigned int hash = 0;
  864. int mac_octet;
  865. for (mac_octet = 5; mac_octet >= 0; mac_octet--) {
  866. unsigned char xor_bit = 0;
  867. unsigned char octet = ha->addr[mac_octet];
  868. unsigned int bitshift;
  869. for (bitshift = 0; bitshift < 8; bitshift++)
  870. xor_bit ^= ((octet >> bitshift) & 0x01);
  871. hash = (hash << 1) | xor_bit;
  872. }
  873. csrwr32(1, priv->mac_dev, tse_csroffs(hash_table) + hash * 4);
  874. }
  875. }
  876. static void altera_tse_set_mcfilterall(struct net_device *dev)
  877. {
  878. struct altera_tse_private *priv = netdev_priv(dev);
  879. int i;
  880. /* set the hash filter */
  881. for (i = 0; i < 64; i++)
  882. csrwr32(1, priv->mac_dev, tse_csroffs(hash_table) + i * 4);
  883. }
  884. /* Set or clear the multicast filter for this adaptor
  885. */
  886. static void tse_set_rx_mode_hashfilter(struct net_device *dev)
  887. {
  888. struct altera_tse_private *priv = netdev_priv(dev);
  889. spin_lock(&priv->mac_cfg_lock);
  890. if (dev->flags & IFF_PROMISC)
  891. tse_set_bit(priv->mac_dev, tse_csroffs(command_config),
  892. MAC_CMDCFG_PROMIS_EN);
  893. if (dev->flags & IFF_ALLMULTI)
  894. altera_tse_set_mcfilterall(dev);
  895. else
  896. altera_tse_set_mcfilter(dev);
  897. spin_unlock(&priv->mac_cfg_lock);
  898. }
  899. /* Set or clear the multicast filter for this adaptor
  900. */
  901. static void tse_set_rx_mode(struct net_device *dev)
  902. {
  903. struct altera_tse_private *priv = netdev_priv(dev);
  904. spin_lock(&priv->mac_cfg_lock);
  905. if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
  906. !netdev_mc_empty(dev) || !netdev_uc_empty(dev))
  907. tse_set_bit(priv->mac_dev, tse_csroffs(command_config),
  908. MAC_CMDCFG_PROMIS_EN);
  909. else
  910. tse_clear_bit(priv->mac_dev, tse_csroffs(command_config),
  911. MAC_CMDCFG_PROMIS_EN);
  912. spin_unlock(&priv->mac_cfg_lock);
  913. }
  914. /* Open and initialize the interface
  915. */
  916. static int tse_open(struct net_device *dev)
  917. {
  918. struct altera_tse_private *priv = netdev_priv(dev);
  919. int ret = 0;
  920. int i;
  921. unsigned long int flags;
  922. /* Reset and configure TSE MAC and probe associated PHY */
  923. ret = priv->dmaops->init_dma(priv);
  924. if (ret != 0) {
  925. netdev_err(dev, "Cannot initialize DMA\n");
  926. goto phy_error;
  927. }
  928. if (netif_msg_ifup(priv))
  929. netdev_warn(dev, "device MAC address %pM\n",
  930. dev->dev_addr);
  931. if ((priv->revision < 0xd00) || (priv->revision > 0xe00))
  932. netdev_warn(dev, "TSE revision %x\n", priv->revision);
  933. spin_lock(&priv->mac_cfg_lock);
  934. ret = reset_mac(priv);
  935. /* Note that reset_mac will fail if the clocks are gated by the PHY
  936. * due to the PHY being put into isolation or power down mode.
  937. * This is not an error if reset fails due to no clock.
  938. */
  939. if (ret)
  940. netdev_dbg(dev, "Cannot reset MAC core (error: %d)\n", ret);
  941. ret = init_mac(priv);
  942. spin_unlock(&priv->mac_cfg_lock);
  943. if (ret) {
  944. netdev_err(dev, "Cannot init MAC core (error: %d)\n", ret);
  945. goto alloc_skbuf_error;
  946. }
  947. priv->dmaops->reset_dma(priv);
  948. /* Create and initialize the TX/RX descriptors chains. */
  949. priv->rx_ring_size = dma_rx_num;
  950. priv->tx_ring_size = dma_tx_num;
  951. ret = alloc_init_skbufs(priv);
  952. if (ret) {
  953. netdev_err(dev, "DMA descriptors initialization failed\n");
  954. goto alloc_skbuf_error;
  955. }
  956. /* Register RX interrupt */
  957. ret = request_irq(priv->rx_irq, altera_isr, IRQF_SHARED,
  958. dev->name, dev);
  959. if (ret) {
  960. netdev_err(dev, "Unable to register RX interrupt %d\n",
  961. priv->rx_irq);
  962. goto init_error;
  963. }
  964. /* Register TX interrupt */
  965. ret = request_irq(priv->tx_irq, altera_isr, IRQF_SHARED,
  966. dev->name, dev);
  967. if (ret) {
  968. netdev_err(dev, "Unable to register TX interrupt %d\n",
  969. priv->tx_irq);
  970. goto tx_request_irq_error;
  971. }
  972. /* Enable DMA interrupts */
  973. spin_lock_irqsave(&priv->rxdma_irq_lock, flags);
  974. priv->dmaops->enable_rxirq(priv);
  975. priv->dmaops->enable_txirq(priv);
  976. /* Setup RX descriptor chain */
  977. for (i = 0; i < priv->rx_ring_size; i++)
  978. priv->dmaops->add_rx_desc(priv, &priv->rx_ring[i]);
  979. spin_unlock_irqrestore(&priv->rxdma_irq_lock, flags);
  980. if (priv->phydev)
  981. phy_start(priv->phydev);
  982. napi_enable(&priv->napi);
  983. netif_start_queue(dev);
  984. priv->dmaops->start_rxdma(priv);
  985. /* Start MAC Rx/Tx */
  986. spin_lock(&priv->mac_cfg_lock);
  987. tse_set_mac(priv, true);
  988. spin_unlock(&priv->mac_cfg_lock);
  989. return 0;
  990. tx_request_irq_error:
  991. free_irq(priv->rx_irq, dev);
  992. init_error:
  993. free_skbufs(dev);
  994. alloc_skbuf_error:
  995. phy_error:
  996. return ret;
  997. }
  998. /* Stop TSE MAC interface and put the device in an inactive state
  999. */
  1000. static int tse_shutdown(struct net_device *dev)
  1001. {
  1002. struct altera_tse_private *priv = netdev_priv(dev);
  1003. int ret;
  1004. unsigned long int flags;
  1005. /* Stop the PHY */
  1006. if (priv->phydev)
  1007. phy_stop(priv->phydev);
  1008. netif_stop_queue(dev);
  1009. napi_disable(&priv->napi);
  1010. /* Disable DMA interrupts */
  1011. spin_lock_irqsave(&priv->rxdma_irq_lock, flags);
  1012. priv->dmaops->disable_rxirq(priv);
  1013. priv->dmaops->disable_txirq(priv);
  1014. spin_unlock_irqrestore(&priv->rxdma_irq_lock, flags);
  1015. /* Free the IRQ lines */
  1016. free_irq(priv->rx_irq, dev);
  1017. free_irq(priv->tx_irq, dev);
  1018. /* disable and reset the MAC, empties fifo */
  1019. spin_lock(&priv->mac_cfg_lock);
  1020. spin_lock(&priv->tx_lock);
  1021. ret = reset_mac(priv);
  1022. /* Note that reset_mac will fail if the clocks are gated by the PHY
  1023. * due to the PHY being put into isolation or power down mode.
  1024. * This is not an error if reset fails due to no clock.
  1025. */
  1026. if (ret)
  1027. netdev_dbg(dev, "Cannot reset MAC core (error: %d)\n", ret);
  1028. priv->dmaops->reset_dma(priv);
  1029. free_skbufs(dev);
  1030. spin_unlock(&priv->tx_lock);
  1031. spin_unlock(&priv->mac_cfg_lock);
  1032. priv->dmaops->uninit_dma(priv);
  1033. return 0;
  1034. }
  1035. static struct net_device_ops altera_tse_netdev_ops = {
  1036. .ndo_open = tse_open,
  1037. .ndo_stop = tse_shutdown,
  1038. .ndo_start_xmit = tse_start_xmit,
  1039. .ndo_set_mac_address = eth_mac_addr,
  1040. .ndo_set_rx_mode = tse_set_rx_mode,
  1041. .ndo_change_mtu = tse_change_mtu,
  1042. .ndo_validate_addr = eth_validate_addr,
  1043. };
  1044. static int request_and_map(struct platform_device *pdev, const char *name,
  1045. struct resource **res, void __iomem **ptr)
  1046. {
  1047. struct resource *region;
  1048. struct device *device = &pdev->dev;
  1049. *res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
  1050. if (*res == NULL) {
  1051. dev_err(device, "resource %s not defined\n", name);
  1052. return -ENODEV;
  1053. }
  1054. region = devm_request_mem_region(device, (*res)->start,
  1055. resource_size(*res), dev_name(device));
  1056. if (region == NULL) {
  1057. dev_err(device, "unable to request %s\n", name);
  1058. return -EBUSY;
  1059. }
  1060. *ptr = devm_ioremap_nocache(device, region->start,
  1061. resource_size(region));
  1062. if (*ptr == NULL) {
  1063. dev_err(device, "ioremap_nocache of %s failed!", name);
  1064. return -ENOMEM;
  1065. }
  1066. return 0;
  1067. }
  1068. /* Probe Altera TSE MAC device
  1069. */
  1070. static int altera_tse_probe(struct platform_device *pdev)
  1071. {
  1072. struct net_device *ndev;
  1073. int ret = -ENODEV;
  1074. struct resource *control_port;
  1075. struct resource *dma_res;
  1076. struct altera_tse_private *priv;
  1077. const unsigned char *macaddr;
  1078. void __iomem *descmap;
  1079. const struct of_device_id *of_id = NULL;
  1080. ndev = alloc_etherdev(sizeof(struct altera_tse_private));
  1081. if (!ndev) {
  1082. dev_err(&pdev->dev, "Could not allocate network device\n");
  1083. return -ENODEV;
  1084. }
  1085. SET_NETDEV_DEV(ndev, &pdev->dev);
  1086. priv = netdev_priv(ndev);
  1087. priv->device = &pdev->dev;
  1088. priv->dev = ndev;
  1089. priv->msg_enable = netif_msg_init(debug, default_msg_level);
  1090. of_id = of_match_device(altera_tse_ids, &pdev->dev);
  1091. if (of_id)
  1092. priv->dmaops = (struct altera_dmaops *)of_id->data;
  1093. if (priv->dmaops &&
  1094. priv->dmaops->altera_dtype == ALTERA_DTYPE_SGDMA) {
  1095. /* Get the mapped address to the SGDMA descriptor memory */
  1096. ret = request_and_map(pdev, "s1", &dma_res, &descmap);
  1097. if (ret)
  1098. goto err_free_netdev;
  1099. /* Start of that memory is for transmit descriptors */
  1100. priv->tx_dma_desc = descmap;
  1101. /* First half is for tx descriptors, other half for tx */
  1102. priv->txdescmem = resource_size(dma_res)/2;
  1103. priv->txdescmem_busaddr = (dma_addr_t)dma_res->start;
  1104. priv->rx_dma_desc = (void __iomem *)((uintptr_t)(descmap +
  1105. priv->txdescmem));
  1106. priv->rxdescmem = resource_size(dma_res)/2;
  1107. priv->rxdescmem_busaddr = dma_res->start;
  1108. priv->rxdescmem_busaddr += priv->txdescmem;
  1109. if (upper_32_bits(priv->rxdescmem_busaddr)) {
  1110. dev_dbg(priv->device,
  1111. "SGDMA bus addresses greater than 32-bits\n");
  1112. goto err_free_netdev;
  1113. }
  1114. if (upper_32_bits(priv->txdescmem_busaddr)) {
  1115. dev_dbg(priv->device,
  1116. "SGDMA bus addresses greater than 32-bits\n");
  1117. goto err_free_netdev;
  1118. }
  1119. } else if (priv->dmaops &&
  1120. priv->dmaops->altera_dtype == ALTERA_DTYPE_MSGDMA) {
  1121. ret = request_and_map(pdev, "rx_resp", &dma_res,
  1122. &priv->rx_dma_resp);
  1123. if (ret)
  1124. goto err_free_netdev;
  1125. ret = request_and_map(pdev, "tx_desc", &dma_res,
  1126. &priv->tx_dma_desc);
  1127. if (ret)
  1128. goto err_free_netdev;
  1129. priv->txdescmem = resource_size(dma_res);
  1130. priv->txdescmem_busaddr = dma_res->start;
  1131. ret = request_and_map(pdev, "rx_desc", &dma_res,
  1132. &priv->rx_dma_desc);
  1133. if (ret)
  1134. goto err_free_netdev;
  1135. priv->rxdescmem = resource_size(dma_res);
  1136. priv->rxdescmem_busaddr = dma_res->start;
  1137. } else {
  1138. goto err_free_netdev;
  1139. }
  1140. if (!dma_set_mask(priv->device, DMA_BIT_MASK(priv->dmaops->dmamask)))
  1141. dma_set_coherent_mask(priv->device,
  1142. DMA_BIT_MASK(priv->dmaops->dmamask));
  1143. else if (!dma_set_mask(priv->device, DMA_BIT_MASK(32)))
  1144. dma_set_coherent_mask(priv->device, DMA_BIT_MASK(32));
  1145. else
  1146. goto err_free_netdev;
  1147. /* MAC address space */
  1148. ret = request_and_map(pdev, "control_port", &control_port,
  1149. (void __iomem **)&priv->mac_dev);
  1150. if (ret)
  1151. goto err_free_netdev;
  1152. /* xSGDMA Rx Dispatcher address space */
  1153. ret = request_and_map(pdev, "rx_csr", &dma_res,
  1154. &priv->rx_dma_csr);
  1155. if (ret)
  1156. goto err_free_netdev;
  1157. /* xSGDMA Tx Dispatcher address space */
  1158. ret = request_and_map(pdev, "tx_csr", &dma_res,
  1159. &priv->tx_dma_csr);
  1160. if (ret)
  1161. goto err_free_netdev;
  1162. /* Rx IRQ */
  1163. priv->rx_irq = platform_get_irq_byname(pdev, "rx_irq");
  1164. if (priv->rx_irq == -ENXIO) {
  1165. dev_err(&pdev->dev, "cannot obtain Rx IRQ\n");
  1166. ret = -ENXIO;
  1167. goto err_free_netdev;
  1168. }
  1169. /* Tx IRQ */
  1170. priv->tx_irq = platform_get_irq_byname(pdev, "tx_irq");
  1171. if (priv->tx_irq == -ENXIO) {
  1172. dev_err(&pdev->dev, "cannot obtain Tx IRQ\n");
  1173. ret = -ENXIO;
  1174. goto err_free_netdev;
  1175. }
  1176. /* get FIFO depths from device tree */
  1177. if (of_property_read_u32(pdev->dev.of_node, "rx-fifo-depth",
  1178. &priv->rx_fifo_depth)) {
  1179. dev_err(&pdev->dev, "cannot obtain rx-fifo-depth\n");
  1180. ret = -ENXIO;
  1181. goto err_free_netdev;
  1182. }
  1183. if (of_property_read_u32(pdev->dev.of_node, "tx-fifo-depth",
  1184. &priv->tx_fifo_depth)) {
  1185. dev_err(&pdev->dev, "cannot obtain tx-fifo-depth\n");
  1186. ret = -ENXIO;
  1187. goto err_free_netdev;
  1188. }
  1189. /* get hash filter settings for this instance */
  1190. priv->hash_filter =
  1191. of_property_read_bool(pdev->dev.of_node,
  1192. "altr,has-hash-multicast-filter");
  1193. /* Set hash filter to not set for now until the
  1194. * multicast filter receive issue is debugged
  1195. */
  1196. priv->hash_filter = 0;
  1197. /* get supplemental address settings for this instance */
  1198. priv->added_unicast =
  1199. of_property_read_bool(pdev->dev.of_node,
  1200. "altr,has-supplementary-unicast");
  1201. /* Max MTU is 1500, ETH_DATA_LEN */
  1202. priv->max_mtu = ETH_DATA_LEN;
  1203. /* Get the max mtu from the device tree. Note that the
  1204. * "max-frame-size" parameter is actually max mtu. Definition
  1205. * in the ePAPR v1.1 spec and usage differ, so go with usage.
  1206. */
  1207. of_property_read_u32(pdev->dev.of_node, "max-frame-size",
  1208. &priv->max_mtu);
  1209. /* The DMA buffer size already accounts for an alignment bias
  1210. * to avoid unaligned access exceptions for the NIOS processor,
  1211. */
  1212. priv->rx_dma_buf_sz = ALTERA_RXDMABUFFER_SIZE;
  1213. /* get default MAC address from device tree */
  1214. macaddr = of_get_mac_address(pdev->dev.of_node);
  1215. if (macaddr)
  1216. ether_addr_copy(ndev->dev_addr, macaddr);
  1217. else
  1218. eth_hw_addr_random(ndev);
  1219. /* get phy addr and create mdio */
  1220. ret = altera_tse_phy_get_addr_mdio_create(ndev);
  1221. if (ret)
  1222. goto err_free_netdev;
  1223. /* initialize netdev */
  1224. ndev->mem_start = control_port->start;
  1225. ndev->mem_end = control_port->end;
  1226. ndev->netdev_ops = &altera_tse_netdev_ops;
  1227. altera_tse_set_ethtool_ops(ndev);
  1228. altera_tse_netdev_ops.ndo_set_rx_mode = tse_set_rx_mode;
  1229. if (priv->hash_filter)
  1230. altera_tse_netdev_ops.ndo_set_rx_mode =
  1231. tse_set_rx_mode_hashfilter;
  1232. /* Scatter/gather IO is not supported,
  1233. * so it is turned off
  1234. */
  1235. ndev->hw_features &= ~NETIF_F_SG;
  1236. ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
  1237. /* VLAN offloading of tagging, stripping and filtering is not
  1238. * supported by hardware, but driver will accommodate the
  1239. * extra 4-byte VLAN tag for processing by upper layers
  1240. */
  1241. ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
  1242. /* setup NAPI interface */
  1243. netif_napi_add(ndev, &priv->napi, tse_poll, NAPI_POLL_WEIGHT);
  1244. spin_lock_init(&priv->mac_cfg_lock);
  1245. spin_lock_init(&priv->tx_lock);
  1246. spin_lock_init(&priv->rxdma_irq_lock);
  1247. netif_carrier_off(ndev);
  1248. ret = register_netdev(ndev);
  1249. if (ret) {
  1250. dev_err(&pdev->dev, "failed to register TSE net device\n");
  1251. goto err_register_netdev;
  1252. }
  1253. platform_set_drvdata(pdev, ndev);
  1254. priv->revision = ioread32(&priv->mac_dev->megacore_revision);
  1255. if (netif_msg_probe(priv))
  1256. dev_info(&pdev->dev, "Altera TSE MAC version %d.%d at 0x%08lx irq %d/%d\n",
  1257. (priv->revision >> 8) & 0xff,
  1258. priv->revision & 0xff,
  1259. (unsigned long) control_port->start, priv->rx_irq,
  1260. priv->tx_irq);
  1261. ret = init_phy(ndev);
  1262. if (ret != 0) {
  1263. netdev_err(ndev, "Cannot attach to PHY (error: %d)\n", ret);
  1264. goto err_init_phy;
  1265. }
  1266. return 0;
  1267. err_init_phy:
  1268. unregister_netdev(ndev);
  1269. err_register_netdev:
  1270. netif_napi_del(&priv->napi);
  1271. altera_tse_mdio_destroy(ndev);
  1272. err_free_netdev:
  1273. free_netdev(ndev);
  1274. return ret;
  1275. }
  1276. /* Remove Altera TSE MAC device
  1277. */
  1278. static int altera_tse_remove(struct platform_device *pdev)
  1279. {
  1280. struct net_device *ndev = platform_get_drvdata(pdev);
  1281. struct altera_tse_private *priv = netdev_priv(ndev);
  1282. if (priv->phydev)
  1283. phy_disconnect(priv->phydev);
  1284. platform_set_drvdata(pdev, NULL);
  1285. altera_tse_mdio_destroy(ndev);
  1286. unregister_netdev(ndev);
  1287. free_netdev(ndev);
  1288. return 0;
  1289. }
  1290. static const struct altera_dmaops altera_dtype_sgdma = {
  1291. .altera_dtype = ALTERA_DTYPE_SGDMA,
  1292. .dmamask = 32,
  1293. .reset_dma = sgdma_reset,
  1294. .enable_txirq = sgdma_enable_txirq,
  1295. .enable_rxirq = sgdma_enable_rxirq,
  1296. .disable_txirq = sgdma_disable_txirq,
  1297. .disable_rxirq = sgdma_disable_rxirq,
  1298. .clear_txirq = sgdma_clear_txirq,
  1299. .clear_rxirq = sgdma_clear_rxirq,
  1300. .tx_buffer = sgdma_tx_buffer,
  1301. .tx_completions = sgdma_tx_completions,
  1302. .add_rx_desc = sgdma_add_rx_desc,
  1303. .get_rx_status = sgdma_rx_status,
  1304. .init_dma = sgdma_initialize,
  1305. .uninit_dma = sgdma_uninitialize,
  1306. .start_rxdma = sgdma_start_rxdma,
  1307. };
  1308. static const struct altera_dmaops altera_dtype_msgdma = {
  1309. .altera_dtype = ALTERA_DTYPE_MSGDMA,
  1310. .dmamask = 64,
  1311. .reset_dma = msgdma_reset,
  1312. .enable_txirq = msgdma_enable_txirq,
  1313. .enable_rxirq = msgdma_enable_rxirq,
  1314. .disable_txirq = msgdma_disable_txirq,
  1315. .disable_rxirq = msgdma_disable_rxirq,
  1316. .clear_txirq = msgdma_clear_txirq,
  1317. .clear_rxirq = msgdma_clear_rxirq,
  1318. .tx_buffer = msgdma_tx_buffer,
  1319. .tx_completions = msgdma_tx_completions,
  1320. .add_rx_desc = msgdma_add_rx_desc,
  1321. .get_rx_status = msgdma_rx_status,
  1322. .init_dma = msgdma_initialize,
  1323. .uninit_dma = msgdma_uninitialize,
  1324. .start_rxdma = msgdma_start_rxdma,
  1325. };
  1326. static const struct of_device_id altera_tse_ids[] = {
  1327. { .compatible = "altr,tse-msgdma-1.0", .data = &altera_dtype_msgdma, },
  1328. { .compatible = "altr,tse-1.0", .data = &altera_dtype_sgdma, },
  1329. { .compatible = "ALTR,tse-1.0", .data = &altera_dtype_sgdma, },
  1330. {},
  1331. };
  1332. MODULE_DEVICE_TABLE(of, altera_tse_ids);
  1333. static struct platform_driver altera_tse_driver = {
  1334. .probe = altera_tse_probe,
  1335. .remove = altera_tse_remove,
  1336. .suspend = NULL,
  1337. .resume = NULL,
  1338. .driver = {
  1339. .name = ALTERA_TSE_RESOURCE_NAME,
  1340. .of_match_table = altera_tse_ids,
  1341. },
  1342. };
  1343. module_platform_driver(altera_tse_driver);
  1344. MODULE_AUTHOR("Altera Corporation");
  1345. MODULE_DESCRIPTION("Altera Triple Speed Ethernet MAC driver");
  1346. MODULE_LICENSE("GPL v2");