altera_tse_main.c 40 KB

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