altera_tse_main.c 42 KB

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