altera_tse_main.c 42 KB

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