altera_tse_main.c 42 KB

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