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