altera_tse_main.c 42 KB

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