altera_tse_main.c 42 KB

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