gemini.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Ethernet device driver for Cortina Systems Gemini SoC
  3. * Also known as the StorLink SL3512 and SL3516 (SL351x) or Lepus
  4. * Net Engine and Gigabit Ethernet MAC (GMAC)
  5. * This hardware contains a TCP Offload Engine (TOE) but currently the
  6. * driver does not make use of it.
  7. *
  8. * Authors:
  9. * Linus Walleij <linus.walleij@linaro.org>
  10. * Tobias Waldvogel <tobias.waldvogel@gmail.com> (OpenWRT)
  11. * Michał Mirosław <mirq-linux@rere.qmqm.pl>
  12. * Paulius Zaleckas <paulius.zaleckas@gmail.com>
  13. * Giuseppe De Robertis <Giuseppe.DeRobertis@ba.infn.it>
  14. * Gary Chen & Ch Hsu Storlink Semiconductor
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/slab.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/cache.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/reset.h>
  26. #include <linux/clk.h>
  27. #include <linux/of.h>
  28. #include <linux/of_mdio.h>
  29. #include <linux/of_net.h>
  30. #include <linux/of_platform.h>
  31. #include <linux/etherdevice.h>
  32. #include <linux/if_vlan.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/phy.h>
  35. #include <linux/crc32.h>
  36. #include <linux/ethtool.h>
  37. #include <linux/tcp.h>
  38. #include <linux/u64_stats_sync.h>
  39. #include <linux/in.h>
  40. #include <linux/ip.h>
  41. #include <linux/ipv6.h>
  42. #include "gemini.h"
  43. #define DRV_NAME "gmac-gemini"
  44. #define DRV_VERSION "1.0"
  45. #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
  46. static int debug = -1;
  47. module_param(debug, int, 0);
  48. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  49. #define HSIZE_8 0x00
  50. #define HSIZE_16 0x01
  51. #define HSIZE_32 0x02
  52. #define HBURST_SINGLE 0x00
  53. #define HBURST_INCR 0x01
  54. #define HBURST_INCR4 0x02
  55. #define HBURST_INCR8 0x03
  56. #define HPROT_DATA_CACHE BIT(0)
  57. #define HPROT_PRIVILIGED BIT(1)
  58. #define HPROT_BUFFERABLE BIT(2)
  59. #define HPROT_CACHABLE BIT(3)
  60. #define DEFAULT_RX_COALESCE_NSECS 0
  61. #define DEFAULT_GMAC_RXQ_ORDER 9
  62. #define DEFAULT_GMAC_TXQ_ORDER 8
  63. #define DEFAULT_RX_BUF_ORDER 11
  64. #define DEFAULT_NAPI_WEIGHT 64
  65. #define TX_MAX_FRAGS 16
  66. #define TX_QUEUE_NUM 1 /* max: 6 */
  67. #define RX_MAX_ALLOC_ORDER 2
  68. #define GMAC0_IRQ0_2 (GMAC0_TXDERR_INT_BIT | GMAC0_TXPERR_INT_BIT | \
  69. GMAC0_RXDERR_INT_BIT | GMAC0_RXPERR_INT_BIT)
  70. #define GMAC0_IRQ0_TXQ0_INTS (GMAC0_SWTQ00_EOF_INT_BIT | \
  71. GMAC0_SWTQ00_FIN_INT_BIT)
  72. #define GMAC0_IRQ4_8 (GMAC0_MIB_INT_BIT | GMAC0_RX_OVERRUN_INT_BIT)
  73. #define GMAC_OFFLOAD_FEATURES (NETIF_F_SG | NETIF_F_IP_CSUM | \
  74. NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM | \
  75. NETIF_F_TSO | NETIF_F_TSO_ECN | NETIF_F_TSO6)
  76. /**
  77. * struct gmac_queue_page - page buffer per-page info
  78. */
  79. struct gmac_queue_page {
  80. struct page *page;
  81. dma_addr_t mapping;
  82. };
  83. struct gmac_txq {
  84. struct gmac_txdesc *ring;
  85. struct sk_buff **skb;
  86. unsigned int cptr;
  87. unsigned int noirq_packets;
  88. };
  89. struct gemini_ethernet;
  90. struct gemini_ethernet_port {
  91. u8 id; /* 0 or 1 */
  92. struct gemini_ethernet *geth;
  93. struct net_device *netdev;
  94. struct device *dev;
  95. void __iomem *dma_base;
  96. void __iomem *gmac_base;
  97. struct clk *pclk;
  98. struct reset_control *reset;
  99. int irq;
  100. __le32 mac_addr[3];
  101. void __iomem *rxq_rwptr;
  102. struct gmac_rxdesc *rxq_ring;
  103. unsigned int rxq_order;
  104. struct napi_struct napi;
  105. struct hrtimer rx_coalesce_timer;
  106. unsigned int rx_coalesce_nsecs;
  107. unsigned int freeq_refill;
  108. struct gmac_txq txq[TX_QUEUE_NUM];
  109. unsigned int txq_order;
  110. unsigned int irq_every_tx_packets;
  111. dma_addr_t rxq_dma_base;
  112. dma_addr_t txq_dma_base;
  113. unsigned int msg_enable;
  114. spinlock_t config_lock; /* Locks config register */
  115. struct u64_stats_sync tx_stats_syncp;
  116. struct u64_stats_sync rx_stats_syncp;
  117. struct u64_stats_sync ir_stats_syncp;
  118. struct rtnl_link_stats64 stats;
  119. u64 hw_stats[RX_STATS_NUM];
  120. u64 rx_stats[RX_STATUS_NUM];
  121. u64 rx_csum_stats[RX_CHKSUM_NUM];
  122. u64 rx_napi_exits;
  123. u64 tx_frag_stats[TX_MAX_FRAGS];
  124. u64 tx_frags_linearized;
  125. u64 tx_hw_csummed;
  126. };
  127. struct gemini_ethernet {
  128. struct device *dev;
  129. void __iomem *base;
  130. struct gemini_ethernet_port *port0;
  131. struct gemini_ethernet_port *port1;
  132. bool initialized;
  133. spinlock_t irq_lock; /* Locks IRQ-related registers */
  134. unsigned int freeq_order;
  135. unsigned int freeq_frag_order;
  136. struct gmac_rxdesc *freeq_ring;
  137. dma_addr_t freeq_dma_base;
  138. struct gmac_queue_page *freeq_pages;
  139. unsigned int num_freeq_pages;
  140. spinlock_t freeq_lock; /* Locks queue from reentrance */
  141. };
  142. #define GMAC_STATS_NUM ( \
  143. RX_STATS_NUM + RX_STATUS_NUM + RX_CHKSUM_NUM + 1 + \
  144. TX_MAX_FRAGS + 2)
  145. static const char gmac_stats_strings[GMAC_STATS_NUM][ETH_GSTRING_LEN] = {
  146. "GMAC_IN_DISCARDS",
  147. "GMAC_IN_ERRORS",
  148. "GMAC_IN_MCAST",
  149. "GMAC_IN_BCAST",
  150. "GMAC_IN_MAC1",
  151. "GMAC_IN_MAC2",
  152. "RX_STATUS_GOOD_FRAME",
  153. "RX_STATUS_TOO_LONG_GOOD_CRC",
  154. "RX_STATUS_RUNT_FRAME",
  155. "RX_STATUS_SFD_NOT_FOUND",
  156. "RX_STATUS_CRC_ERROR",
  157. "RX_STATUS_TOO_LONG_BAD_CRC",
  158. "RX_STATUS_ALIGNMENT_ERROR",
  159. "RX_STATUS_TOO_LONG_BAD_ALIGN",
  160. "RX_STATUS_RX_ERR",
  161. "RX_STATUS_DA_FILTERED",
  162. "RX_STATUS_BUFFER_FULL",
  163. "RX_STATUS_11",
  164. "RX_STATUS_12",
  165. "RX_STATUS_13",
  166. "RX_STATUS_14",
  167. "RX_STATUS_15",
  168. "RX_CHKSUM_IP_UDP_TCP_OK",
  169. "RX_CHKSUM_IP_OK_ONLY",
  170. "RX_CHKSUM_NONE",
  171. "RX_CHKSUM_3",
  172. "RX_CHKSUM_IP_ERR_UNKNOWN",
  173. "RX_CHKSUM_IP_ERR",
  174. "RX_CHKSUM_TCP_UDP_ERR",
  175. "RX_CHKSUM_7",
  176. "RX_NAPI_EXITS",
  177. "TX_FRAGS[1]",
  178. "TX_FRAGS[2]",
  179. "TX_FRAGS[3]",
  180. "TX_FRAGS[4]",
  181. "TX_FRAGS[5]",
  182. "TX_FRAGS[6]",
  183. "TX_FRAGS[7]",
  184. "TX_FRAGS[8]",
  185. "TX_FRAGS[9]",
  186. "TX_FRAGS[10]",
  187. "TX_FRAGS[11]",
  188. "TX_FRAGS[12]",
  189. "TX_FRAGS[13]",
  190. "TX_FRAGS[14]",
  191. "TX_FRAGS[15]",
  192. "TX_FRAGS[16+]",
  193. "TX_FRAGS_LINEARIZED",
  194. "TX_HW_CSUMMED",
  195. };
  196. static void gmac_dump_dma_state(struct net_device *netdev);
  197. static void gmac_update_config0_reg(struct net_device *netdev,
  198. u32 val, u32 vmask)
  199. {
  200. struct gemini_ethernet_port *port = netdev_priv(netdev);
  201. unsigned long flags;
  202. u32 reg;
  203. spin_lock_irqsave(&port->config_lock, flags);
  204. reg = readl(port->gmac_base + GMAC_CONFIG0);
  205. reg = (reg & ~vmask) | val;
  206. writel(reg, port->gmac_base + GMAC_CONFIG0);
  207. spin_unlock_irqrestore(&port->config_lock, flags);
  208. }
  209. static void gmac_enable_tx_rx(struct net_device *netdev)
  210. {
  211. struct gemini_ethernet_port *port = netdev_priv(netdev);
  212. unsigned long flags;
  213. u32 reg;
  214. spin_lock_irqsave(&port->config_lock, flags);
  215. reg = readl(port->gmac_base + GMAC_CONFIG0);
  216. reg &= ~CONFIG0_TX_RX_DISABLE;
  217. writel(reg, port->gmac_base + GMAC_CONFIG0);
  218. spin_unlock_irqrestore(&port->config_lock, flags);
  219. }
  220. static void gmac_disable_tx_rx(struct net_device *netdev)
  221. {
  222. struct gemini_ethernet_port *port = netdev_priv(netdev);
  223. unsigned long flags;
  224. u32 val;
  225. spin_lock_irqsave(&port->config_lock, flags);
  226. val = readl(port->gmac_base + GMAC_CONFIG0);
  227. val |= CONFIG0_TX_RX_DISABLE;
  228. writel(val, port->gmac_base + GMAC_CONFIG0);
  229. spin_unlock_irqrestore(&port->config_lock, flags);
  230. mdelay(10); /* let GMAC consume packet */
  231. }
  232. static void gmac_set_flow_control(struct net_device *netdev, bool tx, bool rx)
  233. {
  234. struct gemini_ethernet_port *port = netdev_priv(netdev);
  235. unsigned long flags;
  236. u32 val;
  237. spin_lock_irqsave(&port->config_lock, flags);
  238. val = readl(port->gmac_base + GMAC_CONFIG0);
  239. val &= ~CONFIG0_FLOW_CTL;
  240. if (tx)
  241. val |= CONFIG0_FLOW_TX;
  242. if (rx)
  243. val |= CONFIG0_FLOW_RX;
  244. writel(val, port->gmac_base + GMAC_CONFIG0);
  245. spin_unlock_irqrestore(&port->config_lock, flags);
  246. }
  247. static void gmac_speed_set(struct net_device *netdev)
  248. {
  249. struct gemini_ethernet_port *port = netdev_priv(netdev);
  250. struct phy_device *phydev = netdev->phydev;
  251. union gmac_status status, old_status;
  252. int pause_tx = 0;
  253. int pause_rx = 0;
  254. status.bits32 = readl(port->gmac_base + GMAC_STATUS);
  255. old_status.bits32 = status.bits32;
  256. status.bits.link = phydev->link;
  257. status.bits.duplex = phydev->duplex;
  258. switch (phydev->speed) {
  259. case 1000:
  260. status.bits.speed = GMAC_SPEED_1000;
  261. if (phydev->interface == PHY_INTERFACE_MODE_RGMII)
  262. status.bits.mii_rmii = GMAC_PHY_RGMII_1000;
  263. netdev_dbg(netdev, "connect %s to RGMII @ 1Gbit\n",
  264. phydev_name(phydev));
  265. break;
  266. case 100:
  267. status.bits.speed = GMAC_SPEED_100;
  268. if (phydev->interface == PHY_INTERFACE_MODE_RGMII)
  269. status.bits.mii_rmii = GMAC_PHY_RGMII_100_10;
  270. netdev_dbg(netdev, "connect %s to RGMII @ 100 Mbit\n",
  271. phydev_name(phydev));
  272. break;
  273. case 10:
  274. status.bits.speed = GMAC_SPEED_10;
  275. if (phydev->interface == PHY_INTERFACE_MODE_RGMII)
  276. status.bits.mii_rmii = GMAC_PHY_RGMII_100_10;
  277. netdev_dbg(netdev, "connect %s to RGMII @ 10 Mbit\n",
  278. phydev_name(phydev));
  279. break;
  280. default:
  281. netdev_warn(netdev, "Unsupported PHY speed (%d) on %s\n",
  282. phydev->speed, phydev_name(phydev));
  283. }
  284. if (phydev->duplex == DUPLEX_FULL) {
  285. u16 lcladv = phy_read(phydev, MII_ADVERTISE);
  286. u16 rmtadv = phy_read(phydev, MII_LPA);
  287. u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
  288. if (cap & FLOW_CTRL_RX)
  289. pause_rx = 1;
  290. if (cap & FLOW_CTRL_TX)
  291. pause_tx = 1;
  292. }
  293. gmac_set_flow_control(netdev, pause_tx, pause_rx);
  294. if (old_status.bits32 == status.bits32)
  295. return;
  296. if (netif_msg_link(port)) {
  297. phy_print_status(phydev);
  298. netdev_info(netdev, "link flow control: %s\n",
  299. phydev->pause
  300. ? (phydev->asym_pause ? "tx" : "both")
  301. : (phydev->asym_pause ? "rx" : "none")
  302. );
  303. }
  304. gmac_disable_tx_rx(netdev);
  305. writel(status.bits32, port->gmac_base + GMAC_STATUS);
  306. gmac_enable_tx_rx(netdev);
  307. }
  308. static int gmac_setup_phy(struct net_device *netdev)
  309. {
  310. struct gemini_ethernet_port *port = netdev_priv(netdev);
  311. union gmac_status status = { .bits32 = 0 };
  312. struct device *dev = port->dev;
  313. struct phy_device *phy;
  314. phy = of_phy_get_and_connect(netdev,
  315. dev->of_node,
  316. gmac_speed_set);
  317. if (!phy)
  318. return -ENODEV;
  319. netdev->phydev = phy;
  320. phy_set_max_speed(phy, SPEED_1000);
  321. phy_support_asym_pause(phy);
  322. /* set PHY interface type */
  323. switch (phy->interface) {
  324. case PHY_INTERFACE_MODE_MII:
  325. netdev_dbg(netdev,
  326. "MII: set GMAC0 to GMII mode, GMAC1 disabled\n");
  327. status.bits.mii_rmii = GMAC_PHY_MII;
  328. break;
  329. case PHY_INTERFACE_MODE_GMII:
  330. netdev_dbg(netdev,
  331. "GMII: set GMAC0 to GMII mode, GMAC1 disabled\n");
  332. status.bits.mii_rmii = GMAC_PHY_GMII;
  333. break;
  334. case PHY_INTERFACE_MODE_RGMII:
  335. netdev_dbg(netdev,
  336. "RGMII: set GMAC0 and GMAC1 to MII/RGMII mode\n");
  337. status.bits.mii_rmii = GMAC_PHY_RGMII_100_10;
  338. break;
  339. default:
  340. netdev_err(netdev, "Unsupported MII interface\n");
  341. phy_disconnect(phy);
  342. netdev->phydev = NULL;
  343. return -EINVAL;
  344. }
  345. writel(status.bits32, port->gmac_base + GMAC_STATUS);
  346. if (netif_msg_link(port))
  347. phy_attached_info(phy);
  348. return 0;
  349. }
  350. /* The maximum frame length is not logically enumerated in the
  351. * hardware, so we do a table lookup to find the applicable max
  352. * frame length.
  353. */
  354. struct gmac_max_framelen {
  355. unsigned int max_l3_len;
  356. u8 val;
  357. };
  358. static const struct gmac_max_framelen gmac_maxlens[] = {
  359. {
  360. .max_l3_len = 1518,
  361. .val = CONFIG0_MAXLEN_1518,
  362. },
  363. {
  364. .max_l3_len = 1522,
  365. .val = CONFIG0_MAXLEN_1522,
  366. },
  367. {
  368. .max_l3_len = 1536,
  369. .val = CONFIG0_MAXLEN_1536,
  370. },
  371. {
  372. .max_l3_len = 1542,
  373. .val = CONFIG0_MAXLEN_1542,
  374. },
  375. {
  376. .max_l3_len = 9212,
  377. .val = CONFIG0_MAXLEN_9k,
  378. },
  379. {
  380. .max_l3_len = 10236,
  381. .val = CONFIG0_MAXLEN_10k,
  382. },
  383. };
  384. static int gmac_pick_rx_max_len(unsigned int max_l3_len)
  385. {
  386. const struct gmac_max_framelen *maxlen;
  387. int maxtot;
  388. int i;
  389. maxtot = max_l3_len + ETH_HLEN + VLAN_HLEN;
  390. for (i = 0; i < ARRAY_SIZE(gmac_maxlens); i++) {
  391. maxlen = &gmac_maxlens[i];
  392. if (maxtot <= maxlen->max_l3_len)
  393. return maxlen->val;
  394. }
  395. return -1;
  396. }
  397. static int gmac_init(struct net_device *netdev)
  398. {
  399. struct gemini_ethernet_port *port = netdev_priv(netdev);
  400. union gmac_config0 config0 = { .bits = {
  401. .dis_tx = 1,
  402. .dis_rx = 1,
  403. .ipv4_rx_chksum = 1,
  404. .ipv6_rx_chksum = 1,
  405. .rx_err_detect = 1,
  406. .rgmm_edge = 1,
  407. .port0_chk_hwq = 1,
  408. .port1_chk_hwq = 1,
  409. .port0_chk_toeq = 1,
  410. .port1_chk_toeq = 1,
  411. .port0_chk_classq = 1,
  412. .port1_chk_classq = 1,
  413. } };
  414. union gmac_ahb_weight ahb_weight = { .bits = {
  415. .rx_weight = 1,
  416. .tx_weight = 1,
  417. .hash_weight = 1,
  418. .pre_req = 0x1f,
  419. .tq_dv_threshold = 0,
  420. } };
  421. union gmac_tx_wcr0 hw_weigh = { .bits = {
  422. .hw_tq3 = 1,
  423. .hw_tq2 = 1,
  424. .hw_tq1 = 1,
  425. .hw_tq0 = 1,
  426. } };
  427. union gmac_tx_wcr1 sw_weigh = { .bits = {
  428. .sw_tq5 = 1,
  429. .sw_tq4 = 1,
  430. .sw_tq3 = 1,
  431. .sw_tq2 = 1,
  432. .sw_tq1 = 1,
  433. .sw_tq0 = 1,
  434. } };
  435. union gmac_config1 config1 = { .bits = {
  436. .set_threshold = 16,
  437. .rel_threshold = 24,
  438. } };
  439. union gmac_config2 config2 = { .bits = {
  440. .set_threshold = 16,
  441. .rel_threshold = 32,
  442. } };
  443. union gmac_config3 config3 = { .bits = {
  444. .set_threshold = 0,
  445. .rel_threshold = 0,
  446. } };
  447. union gmac_config0 tmp;
  448. u32 val;
  449. config0.bits.max_len = gmac_pick_rx_max_len(netdev->mtu);
  450. tmp.bits32 = readl(port->gmac_base + GMAC_CONFIG0);
  451. config0.bits.reserved = tmp.bits.reserved;
  452. writel(config0.bits32, port->gmac_base + GMAC_CONFIG0);
  453. writel(config1.bits32, port->gmac_base + GMAC_CONFIG1);
  454. writel(config2.bits32, port->gmac_base + GMAC_CONFIG2);
  455. writel(config3.bits32, port->gmac_base + GMAC_CONFIG3);
  456. val = readl(port->dma_base + GMAC_AHB_WEIGHT_REG);
  457. writel(ahb_weight.bits32, port->dma_base + GMAC_AHB_WEIGHT_REG);
  458. writel(hw_weigh.bits32,
  459. port->dma_base + GMAC_TX_WEIGHTING_CTRL_0_REG);
  460. writel(sw_weigh.bits32,
  461. port->dma_base + GMAC_TX_WEIGHTING_CTRL_1_REG);
  462. port->rxq_order = DEFAULT_GMAC_RXQ_ORDER;
  463. port->txq_order = DEFAULT_GMAC_TXQ_ORDER;
  464. port->rx_coalesce_nsecs = DEFAULT_RX_COALESCE_NSECS;
  465. /* Mark every quarter of the queue a packet for interrupt
  466. * in order to be able to wake up the queue if it was stopped
  467. */
  468. port->irq_every_tx_packets = 1 << (port->txq_order - 2);
  469. return 0;
  470. }
  471. static void gmac_uninit(struct net_device *netdev)
  472. {
  473. if (netdev->phydev)
  474. phy_disconnect(netdev->phydev);
  475. }
  476. static int gmac_setup_txqs(struct net_device *netdev)
  477. {
  478. struct gemini_ethernet_port *port = netdev_priv(netdev);
  479. unsigned int n_txq = netdev->num_tx_queues;
  480. struct gemini_ethernet *geth = port->geth;
  481. size_t entries = 1 << port->txq_order;
  482. struct gmac_txq *txq = port->txq;
  483. struct gmac_txdesc *desc_ring;
  484. size_t len = n_txq * entries;
  485. struct sk_buff **skb_tab;
  486. void __iomem *rwptr_reg;
  487. unsigned int r;
  488. int i;
  489. rwptr_reg = port->dma_base + GMAC_SW_TX_QUEUE0_PTR_REG;
  490. skb_tab = kcalloc(len, sizeof(*skb_tab), GFP_KERNEL);
  491. if (!skb_tab)
  492. return -ENOMEM;
  493. desc_ring = dma_alloc_coherent(geth->dev, len * sizeof(*desc_ring),
  494. &port->txq_dma_base, GFP_KERNEL);
  495. if (!desc_ring) {
  496. kfree(skb_tab);
  497. return -ENOMEM;
  498. }
  499. if (port->txq_dma_base & ~DMA_Q_BASE_MASK) {
  500. dev_warn(geth->dev, "TX queue base is not aligned\n");
  501. dma_free_coherent(geth->dev, len * sizeof(*desc_ring),
  502. desc_ring, port->txq_dma_base);
  503. kfree(skb_tab);
  504. return -ENOMEM;
  505. }
  506. writel(port->txq_dma_base | port->txq_order,
  507. port->dma_base + GMAC_SW_TX_QUEUE_BASE_REG);
  508. for (i = 0; i < n_txq; i++) {
  509. txq->ring = desc_ring;
  510. txq->skb = skb_tab;
  511. txq->noirq_packets = 0;
  512. r = readw(rwptr_reg);
  513. rwptr_reg += 2;
  514. writew(r, rwptr_reg);
  515. rwptr_reg += 2;
  516. txq->cptr = r;
  517. txq++;
  518. desc_ring += entries;
  519. skb_tab += entries;
  520. }
  521. return 0;
  522. }
  523. static void gmac_clean_txq(struct net_device *netdev, struct gmac_txq *txq,
  524. unsigned int r)
  525. {
  526. struct gemini_ethernet_port *port = netdev_priv(netdev);
  527. unsigned int m = (1 << port->txq_order) - 1;
  528. struct gemini_ethernet *geth = port->geth;
  529. unsigned int c = txq->cptr;
  530. union gmac_txdesc_0 word0;
  531. union gmac_txdesc_1 word1;
  532. unsigned int hwchksum = 0;
  533. unsigned long bytes = 0;
  534. struct gmac_txdesc *txd;
  535. unsigned short nfrags;
  536. unsigned int errs = 0;
  537. unsigned int pkts = 0;
  538. unsigned int word3;
  539. dma_addr_t mapping;
  540. if (c == r)
  541. return;
  542. while (c != r) {
  543. txd = txq->ring + c;
  544. word0 = txd->word0;
  545. word1 = txd->word1;
  546. mapping = txd->word2.buf_adr;
  547. word3 = txd->word3.bits32;
  548. dma_unmap_single(geth->dev, mapping,
  549. word0.bits.buffer_size, DMA_TO_DEVICE);
  550. if (word3 & EOF_BIT)
  551. dev_kfree_skb(txq->skb[c]);
  552. c++;
  553. c &= m;
  554. if (!(word3 & SOF_BIT))
  555. continue;
  556. if (!word0.bits.status_tx_ok) {
  557. errs++;
  558. continue;
  559. }
  560. pkts++;
  561. bytes += txd->word1.bits.byte_count;
  562. if (word1.bits32 & TSS_CHECKUM_ENABLE)
  563. hwchksum++;
  564. nfrags = word0.bits.desc_count - 1;
  565. if (nfrags) {
  566. if (nfrags >= TX_MAX_FRAGS)
  567. nfrags = TX_MAX_FRAGS - 1;
  568. u64_stats_update_begin(&port->tx_stats_syncp);
  569. port->tx_frag_stats[nfrags]++;
  570. u64_stats_update_end(&port->tx_stats_syncp);
  571. }
  572. }
  573. u64_stats_update_begin(&port->ir_stats_syncp);
  574. port->stats.tx_errors += errs;
  575. port->stats.tx_packets += pkts;
  576. port->stats.tx_bytes += bytes;
  577. port->tx_hw_csummed += hwchksum;
  578. u64_stats_update_end(&port->ir_stats_syncp);
  579. txq->cptr = c;
  580. }
  581. static void gmac_cleanup_txqs(struct net_device *netdev)
  582. {
  583. struct gemini_ethernet_port *port = netdev_priv(netdev);
  584. unsigned int n_txq = netdev->num_tx_queues;
  585. struct gemini_ethernet *geth = port->geth;
  586. void __iomem *rwptr_reg;
  587. unsigned int r, i;
  588. rwptr_reg = port->dma_base + GMAC_SW_TX_QUEUE0_PTR_REG;
  589. for (i = 0; i < n_txq; i++) {
  590. r = readw(rwptr_reg);
  591. rwptr_reg += 2;
  592. writew(r, rwptr_reg);
  593. rwptr_reg += 2;
  594. gmac_clean_txq(netdev, port->txq + i, r);
  595. }
  596. writel(0, port->dma_base + GMAC_SW_TX_QUEUE_BASE_REG);
  597. kfree(port->txq->skb);
  598. dma_free_coherent(geth->dev,
  599. n_txq * sizeof(*port->txq->ring) << port->txq_order,
  600. port->txq->ring, port->txq_dma_base);
  601. }
  602. static int gmac_setup_rxq(struct net_device *netdev)
  603. {
  604. struct gemini_ethernet_port *port = netdev_priv(netdev);
  605. struct gemini_ethernet *geth = port->geth;
  606. struct nontoe_qhdr __iomem *qhdr;
  607. qhdr = geth->base + TOE_DEFAULT_Q_HDR_BASE(netdev->dev_id);
  608. port->rxq_rwptr = &qhdr->word1;
  609. /* Remap a slew of memory to use for the RX queue */
  610. port->rxq_ring = dma_alloc_coherent(geth->dev,
  611. sizeof(*port->rxq_ring) << port->rxq_order,
  612. &port->rxq_dma_base, GFP_KERNEL);
  613. if (!port->rxq_ring)
  614. return -ENOMEM;
  615. if (port->rxq_dma_base & ~NONTOE_QHDR0_BASE_MASK) {
  616. dev_warn(geth->dev, "RX queue base is not aligned\n");
  617. return -ENOMEM;
  618. }
  619. writel(port->rxq_dma_base | port->rxq_order, &qhdr->word0);
  620. writel(0, port->rxq_rwptr);
  621. return 0;
  622. }
  623. static struct gmac_queue_page *
  624. gmac_get_queue_page(struct gemini_ethernet *geth,
  625. struct gemini_ethernet_port *port,
  626. dma_addr_t addr)
  627. {
  628. struct gmac_queue_page *gpage;
  629. dma_addr_t mapping;
  630. int i;
  631. /* Only look for even pages */
  632. mapping = addr & PAGE_MASK;
  633. if (!geth->freeq_pages) {
  634. dev_err(geth->dev, "try to get page with no page list\n");
  635. return NULL;
  636. }
  637. /* Look up a ring buffer page from virtual mapping */
  638. for (i = 0; i < geth->num_freeq_pages; i++) {
  639. gpage = &geth->freeq_pages[i];
  640. if (gpage->mapping == mapping)
  641. return gpage;
  642. }
  643. return NULL;
  644. }
  645. static void gmac_cleanup_rxq(struct net_device *netdev)
  646. {
  647. struct gemini_ethernet_port *port = netdev_priv(netdev);
  648. struct gemini_ethernet *geth = port->geth;
  649. struct gmac_rxdesc *rxd = port->rxq_ring;
  650. static struct gmac_queue_page *gpage;
  651. struct nontoe_qhdr __iomem *qhdr;
  652. void __iomem *dma_reg;
  653. void __iomem *ptr_reg;
  654. dma_addr_t mapping;
  655. union dma_rwptr rw;
  656. unsigned int r, w;
  657. qhdr = geth->base +
  658. TOE_DEFAULT_Q_HDR_BASE(netdev->dev_id);
  659. dma_reg = &qhdr->word0;
  660. ptr_reg = &qhdr->word1;
  661. rw.bits32 = readl(ptr_reg);
  662. r = rw.bits.rptr;
  663. w = rw.bits.wptr;
  664. writew(r, ptr_reg + 2);
  665. writel(0, dma_reg);
  666. /* Loop from read pointer to write pointer of the RX queue
  667. * and free up all pages by the queue.
  668. */
  669. while (r != w) {
  670. mapping = rxd[r].word2.buf_adr;
  671. r++;
  672. r &= ((1 << port->rxq_order) - 1);
  673. if (!mapping)
  674. continue;
  675. /* Freeq pointers are one page off */
  676. gpage = gmac_get_queue_page(geth, port, mapping + PAGE_SIZE);
  677. if (!gpage) {
  678. dev_err(geth->dev, "could not find page\n");
  679. continue;
  680. }
  681. /* Release the RX queue reference to the page */
  682. put_page(gpage->page);
  683. }
  684. dma_free_coherent(geth->dev, sizeof(*port->rxq_ring) << port->rxq_order,
  685. port->rxq_ring, port->rxq_dma_base);
  686. }
  687. static struct page *geth_freeq_alloc_map_page(struct gemini_ethernet *geth,
  688. int pn)
  689. {
  690. struct gmac_rxdesc *freeq_entry;
  691. struct gmac_queue_page *gpage;
  692. unsigned int fpp_order;
  693. unsigned int frag_len;
  694. dma_addr_t mapping;
  695. struct page *page;
  696. int i;
  697. /* First allocate and DMA map a single page */
  698. page = alloc_page(GFP_ATOMIC);
  699. if (!page)
  700. return NULL;
  701. mapping = dma_map_single(geth->dev, page_address(page),
  702. PAGE_SIZE, DMA_FROM_DEVICE);
  703. if (dma_mapping_error(geth->dev, mapping)) {
  704. put_page(page);
  705. return NULL;
  706. }
  707. /* The assign the page mapping (physical address) to the buffer address
  708. * in the hardware queue. PAGE_SHIFT on ARM is 12 (1 page is 4096 bytes,
  709. * 4k), and the default RX frag order is 11 (fragments are up 20 2048
  710. * bytes, 2k) so fpp_order (fragments per page order) is default 1. Thus
  711. * each page normally needs two entries in the queue.
  712. */
  713. frag_len = 1 << geth->freeq_frag_order; /* Usually 2048 */
  714. fpp_order = PAGE_SHIFT - geth->freeq_frag_order;
  715. freeq_entry = geth->freeq_ring + (pn << fpp_order);
  716. dev_dbg(geth->dev, "allocate page %d fragment length %d fragments per page %d, freeq entry %p\n",
  717. pn, frag_len, (1 << fpp_order), freeq_entry);
  718. for (i = (1 << fpp_order); i > 0; i--) {
  719. freeq_entry->word2.buf_adr = mapping;
  720. freeq_entry++;
  721. mapping += frag_len;
  722. }
  723. /* If the freeq entry already has a page mapped, then unmap it. */
  724. gpage = &geth->freeq_pages[pn];
  725. if (gpage->page) {
  726. mapping = geth->freeq_ring[pn << fpp_order].word2.buf_adr;
  727. dma_unmap_single(geth->dev, mapping, frag_len, DMA_FROM_DEVICE);
  728. /* This should be the last reference to the page so it gets
  729. * released
  730. */
  731. put_page(gpage->page);
  732. }
  733. /* Then put our new mapping into the page table */
  734. dev_dbg(geth->dev, "page %d, DMA addr: %08x, page %p\n",
  735. pn, (unsigned int)mapping, page);
  736. gpage->mapping = mapping;
  737. gpage->page = page;
  738. return page;
  739. }
  740. /**
  741. * geth_fill_freeq() - Fill the freeq with empty fragments to use
  742. * @geth: the ethernet adapter
  743. * @refill: whether to reset the queue by filling in all freeq entries or
  744. * just refill it, usually the interrupt to refill the queue happens when
  745. * the queue is half empty.
  746. */
  747. static unsigned int geth_fill_freeq(struct gemini_ethernet *geth, bool refill)
  748. {
  749. unsigned int fpp_order = PAGE_SHIFT - geth->freeq_frag_order;
  750. unsigned int count = 0;
  751. unsigned int pn, epn;
  752. unsigned long flags;
  753. union dma_rwptr rw;
  754. unsigned int m_pn;
  755. /* Mask for page */
  756. m_pn = (1 << (geth->freeq_order - fpp_order)) - 1;
  757. spin_lock_irqsave(&geth->freeq_lock, flags);
  758. rw.bits32 = readl(geth->base + GLOBAL_SWFQ_RWPTR_REG);
  759. pn = (refill ? rw.bits.wptr : rw.bits.rptr) >> fpp_order;
  760. epn = (rw.bits.rptr >> fpp_order) - 1;
  761. epn &= m_pn;
  762. /* Loop over the freeq ring buffer entries */
  763. while (pn != epn) {
  764. struct gmac_queue_page *gpage;
  765. struct page *page;
  766. gpage = &geth->freeq_pages[pn];
  767. page = gpage->page;
  768. dev_dbg(geth->dev, "fill entry %d page ref count %d add %d refs\n",
  769. pn, page_ref_count(page), 1 << fpp_order);
  770. if (page_ref_count(page) > 1) {
  771. unsigned int fl = (pn - epn) & m_pn;
  772. if (fl > 64 >> fpp_order)
  773. break;
  774. page = geth_freeq_alloc_map_page(geth, pn);
  775. if (!page)
  776. break;
  777. }
  778. /* Add one reference per fragment in the page */
  779. page_ref_add(page, 1 << fpp_order);
  780. count += 1 << fpp_order;
  781. pn++;
  782. pn &= m_pn;
  783. }
  784. writew(pn << fpp_order, geth->base + GLOBAL_SWFQ_RWPTR_REG + 2);
  785. spin_unlock_irqrestore(&geth->freeq_lock, flags);
  786. return count;
  787. }
  788. static int geth_setup_freeq(struct gemini_ethernet *geth)
  789. {
  790. unsigned int fpp_order = PAGE_SHIFT - geth->freeq_frag_order;
  791. unsigned int frag_len = 1 << geth->freeq_frag_order;
  792. unsigned int len = 1 << geth->freeq_order;
  793. unsigned int pages = len >> fpp_order;
  794. union queue_threshold qt;
  795. union dma_skb_size skbsz;
  796. unsigned int filled;
  797. unsigned int pn;
  798. geth->freeq_ring = dma_alloc_coherent(geth->dev,
  799. sizeof(*geth->freeq_ring) << geth->freeq_order,
  800. &geth->freeq_dma_base, GFP_KERNEL);
  801. if (!geth->freeq_ring)
  802. return -ENOMEM;
  803. if (geth->freeq_dma_base & ~DMA_Q_BASE_MASK) {
  804. dev_warn(geth->dev, "queue ring base is not aligned\n");
  805. goto err_freeq;
  806. }
  807. /* Allocate a mapping to page look-up index */
  808. geth->freeq_pages = kcalloc(pages, sizeof(*geth->freeq_pages),
  809. GFP_KERNEL);
  810. if (!geth->freeq_pages)
  811. goto err_freeq;
  812. geth->num_freeq_pages = pages;
  813. dev_info(geth->dev, "allocate %d pages for queue\n", pages);
  814. for (pn = 0; pn < pages; pn++)
  815. if (!geth_freeq_alloc_map_page(geth, pn))
  816. goto err_freeq_alloc;
  817. filled = geth_fill_freeq(geth, false);
  818. if (!filled)
  819. goto err_freeq_alloc;
  820. qt.bits32 = readl(geth->base + GLOBAL_QUEUE_THRESHOLD_REG);
  821. qt.bits.swfq_empty = 32;
  822. writel(qt.bits32, geth->base + GLOBAL_QUEUE_THRESHOLD_REG);
  823. skbsz.bits.sw_skb_size = 1 << geth->freeq_frag_order;
  824. writel(skbsz.bits32, geth->base + GLOBAL_DMA_SKB_SIZE_REG);
  825. writel(geth->freeq_dma_base | geth->freeq_order,
  826. geth->base + GLOBAL_SW_FREEQ_BASE_SIZE_REG);
  827. return 0;
  828. err_freeq_alloc:
  829. while (pn > 0) {
  830. struct gmac_queue_page *gpage;
  831. dma_addr_t mapping;
  832. --pn;
  833. mapping = geth->freeq_ring[pn << fpp_order].word2.buf_adr;
  834. dma_unmap_single(geth->dev, mapping, frag_len, DMA_FROM_DEVICE);
  835. gpage = &geth->freeq_pages[pn];
  836. put_page(gpage->page);
  837. }
  838. kfree(geth->freeq_pages);
  839. err_freeq:
  840. dma_free_coherent(geth->dev,
  841. sizeof(*geth->freeq_ring) << geth->freeq_order,
  842. geth->freeq_ring, geth->freeq_dma_base);
  843. geth->freeq_ring = NULL;
  844. return -ENOMEM;
  845. }
  846. /**
  847. * geth_cleanup_freeq() - cleanup the DMA mappings and free the queue
  848. * @geth: the Gemini global ethernet state
  849. */
  850. static void geth_cleanup_freeq(struct gemini_ethernet *geth)
  851. {
  852. unsigned int fpp_order = PAGE_SHIFT - geth->freeq_frag_order;
  853. unsigned int frag_len = 1 << geth->freeq_frag_order;
  854. unsigned int len = 1 << geth->freeq_order;
  855. unsigned int pages = len >> fpp_order;
  856. unsigned int pn;
  857. writew(readw(geth->base + GLOBAL_SWFQ_RWPTR_REG),
  858. geth->base + GLOBAL_SWFQ_RWPTR_REG + 2);
  859. writel(0, geth->base + GLOBAL_SW_FREEQ_BASE_SIZE_REG);
  860. for (pn = 0; pn < pages; pn++) {
  861. struct gmac_queue_page *gpage;
  862. dma_addr_t mapping;
  863. mapping = geth->freeq_ring[pn << fpp_order].word2.buf_adr;
  864. dma_unmap_single(geth->dev, mapping, frag_len, DMA_FROM_DEVICE);
  865. gpage = &geth->freeq_pages[pn];
  866. while (page_ref_count(gpage->page) > 0)
  867. put_page(gpage->page);
  868. }
  869. kfree(geth->freeq_pages);
  870. dma_free_coherent(geth->dev,
  871. sizeof(*geth->freeq_ring) << geth->freeq_order,
  872. geth->freeq_ring, geth->freeq_dma_base);
  873. }
  874. /**
  875. * geth_resize_freeq() - resize the software queue depth
  876. * @port: the port requesting the change
  877. *
  878. * This gets called at least once during probe() so the device queue gets
  879. * "resized" from the hardware defaults. Since both ports/net devices share
  880. * the same hardware queue, some synchronization between the ports is
  881. * needed.
  882. */
  883. static int geth_resize_freeq(struct gemini_ethernet_port *port)
  884. {
  885. struct gemini_ethernet *geth = port->geth;
  886. struct net_device *netdev = port->netdev;
  887. struct gemini_ethernet_port *other_port;
  888. struct net_device *other_netdev;
  889. unsigned int new_size = 0;
  890. unsigned int new_order;
  891. unsigned long flags;
  892. u32 en;
  893. int ret;
  894. if (netdev->dev_id == 0)
  895. other_netdev = geth->port1->netdev;
  896. else
  897. other_netdev = geth->port0->netdev;
  898. if (other_netdev && netif_running(other_netdev))
  899. return -EBUSY;
  900. new_size = 1 << (port->rxq_order + 1);
  901. netdev_dbg(netdev, "port %d size: %d order %d\n",
  902. netdev->dev_id,
  903. new_size,
  904. port->rxq_order);
  905. if (other_netdev) {
  906. other_port = netdev_priv(other_netdev);
  907. new_size += 1 << (other_port->rxq_order + 1);
  908. netdev_dbg(other_netdev, "port %d size: %d order %d\n",
  909. other_netdev->dev_id,
  910. (1 << (other_port->rxq_order + 1)),
  911. other_port->rxq_order);
  912. }
  913. new_order = min(15, ilog2(new_size - 1) + 1);
  914. dev_dbg(geth->dev, "set shared queue to size %d order %d\n",
  915. new_size, new_order);
  916. if (geth->freeq_order == new_order)
  917. return 0;
  918. spin_lock_irqsave(&geth->irq_lock, flags);
  919. /* Disable the software queue IRQs */
  920. en = readl(geth->base + GLOBAL_INTERRUPT_ENABLE_4_REG);
  921. en &= ~SWFQ_EMPTY_INT_BIT;
  922. writel(en, geth->base + GLOBAL_INTERRUPT_ENABLE_4_REG);
  923. spin_unlock_irqrestore(&geth->irq_lock, flags);
  924. /* Drop the old queue */
  925. if (geth->freeq_ring)
  926. geth_cleanup_freeq(geth);
  927. /* Allocate a new queue with the desired order */
  928. geth->freeq_order = new_order;
  929. ret = geth_setup_freeq(geth);
  930. /* Restart the interrupts - NOTE if this is the first resize
  931. * after probe(), this is where the interrupts get turned on
  932. * in the first place.
  933. */
  934. spin_lock_irqsave(&geth->irq_lock, flags);
  935. en |= SWFQ_EMPTY_INT_BIT;
  936. writel(en, geth->base + GLOBAL_INTERRUPT_ENABLE_4_REG);
  937. spin_unlock_irqrestore(&geth->irq_lock, flags);
  938. return ret;
  939. }
  940. static void gmac_tx_irq_enable(struct net_device *netdev,
  941. unsigned int txq, int en)
  942. {
  943. struct gemini_ethernet_port *port = netdev_priv(netdev);
  944. struct gemini_ethernet *geth = port->geth;
  945. u32 val, mask;
  946. netdev_dbg(netdev, "%s device %d\n", __func__, netdev->dev_id);
  947. mask = GMAC0_IRQ0_TXQ0_INTS << (6 * netdev->dev_id + txq);
  948. if (en)
  949. writel(mask, geth->base + GLOBAL_INTERRUPT_STATUS_0_REG);
  950. val = readl(geth->base + GLOBAL_INTERRUPT_ENABLE_0_REG);
  951. val = en ? val | mask : val & ~mask;
  952. writel(val, geth->base + GLOBAL_INTERRUPT_ENABLE_0_REG);
  953. }
  954. static void gmac_tx_irq(struct net_device *netdev, unsigned int txq_num)
  955. {
  956. struct netdev_queue *ntxq = netdev_get_tx_queue(netdev, txq_num);
  957. gmac_tx_irq_enable(netdev, txq_num, 0);
  958. netif_tx_wake_queue(ntxq);
  959. }
  960. static int gmac_map_tx_bufs(struct net_device *netdev, struct sk_buff *skb,
  961. struct gmac_txq *txq, unsigned short *desc)
  962. {
  963. struct gemini_ethernet_port *port = netdev_priv(netdev);
  964. struct skb_shared_info *skb_si = skb_shinfo(skb);
  965. unsigned short m = (1 << port->txq_order) - 1;
  966. short frag, last_frag = skb_si->nr_frags - 1;
  967. struct gemini_ethernet *geth = port->geth;
  968. unsigned int word1, word3, buflen;
  969. unsigned short w = *desc;
  970. struct gmac_txdesc *txd;
  971. skb_frag_t *skb_frag;
  972. dma_addr_t mapping;
  973. unsigned short mtu;
  974. void *buffer;
  975. mtu = ETH_HLEN;
  976. mtu += netdev->mtu;
  977. if (skb->protocol == htons(ETH_P_8021Q))
  978. mtu += VLAN_HLEN;
  979. word1 = skb->len;
  980. word3 = SOF_BIT;
  981. if (word1 > mtu) {
  982. word1 |= TSS_MTU_ENABLE_BIT;
  983. word3 |= mtu;
  984. }
  985. if (skb->ip_summed != CHECKSUM_NONE) {
  986. int tcp = 0;
  987. if (skb->protocol == htons(ETH_P_IP)) {
  988. word1 |= TSS_IP_CHKSUM_BIT;
  989. tcp = ip_hdr(skb)->protocol == IPPROTO_TCP;
  990. } else { /* IPv6 */
  991. word1 |= TSS_IPV6_ENABLE_BIT;
  992. tcp = ipv6_hdr(skb)->nexthdr == IPPROTO_TCP;
  993. }
  994. word1 |= tcp ? TSS_TCP_CHKSUM_BIT : TSS_UDP_CHKSUM_BIT;
  995. }
  996. frag = -1;
  997. while (frag <= last_frag) {
  998. if (frag == -1) {
  999. buffer = skb->data;
  1000. buflen = skb_headlen(skb);
  1001. } else {
  1002. skb_frag = skb_si->frags + frag;
  1003. buffer = skb_frag_address(skb_frag);
  1004. buflen = skb_frag_size(skb_frag);
  1005. }
  1006. if (frag == last_frag) {
  1007. word3 |= EOF_BIT;
  1008. txq->skb[w] = skb;
  1009. }
  1010. mapping = dma_map_single(geth->dev, buffer, buflen,
  1011. DMA_TO_DEVICE);
  1012. if (dma_mapping_error(geth->dev, mapping))
  1013. goto map_error;
  1014. txd = txq->ring + w;
  1015. txd->word0.bits32 = buflen;
  1016. txd->word1.bits32 = word1;
  1017. txd->word2.buf_adr = mapping;
  1018. txd->word3.bits32 = word3;
  1019. word3 &= MTU_SIZE_BIT_MASK;
  1020. w++;
  1021. w &= m;
  1022. frag++;
  1023. }
  1024. *desc = w;
  1025. return 0;
  1026. map_error:
  1027. while (w != *desc) {
  1028. w--;
  1029. w &= m;
  1030. dma_unmap_page(geth->dev, txq->ring[w].word2.buf_adr,
  1031. txq->ring[w].word0.bits.buffer_size,
  1032. DMA_TO_DEVICE);
  1033. }
  1034. return -ENOMEM;
  1035. }
  1036. static int gmac_start_xmit(struct sk_buff *skb, struct net_device *netdev)
  1037. {
  1038. struct gemini_ethernet_port *port = netdev_priv(netdev);
  1039. unsigned short m = (1 << port->txq_order) - 1;
  1040. struct netdev_queue *ntxq;
  1041. unsigned short r, w, d;
  1042. void __iomem *ptr_reg;
  1043. struct gmac_txq *txq;
  1044. int txq_num, nfrags;
  1045. union dma_rwptr rw;
  1046. if (skb->len >= 0x10000)
  1047. goto out_drop_free;
  1048. txq_num = skb_get_queue_mapping(skb);
  1049. ptr_reg = port->dma_base + GMAC_SW_TX_QUEUE_PTR_REG(txq_num);
  1050. txq = &port->txq[txq_num];
  1051. ntxq = netdev_get_tx_queue(netdev, txq_num);
  1052. nfrags = skb_shinfo(skb)->nr_frags;
  1053. rw.bits32 = readl(ptr_reg);
  1054. r = rw.bits.rptr;
  1055. w = rw.bits.wptr;
  1056. d = txq->cptr - w - 1;
  1057. d &= m;
  1058. if (d < nfrags + 2) {
  1059. gmac_clean_txq(netdev, txq, r);
  1060. d = txq->cptr - w - 1;
  1061. d &= m;
  1062. if (d < nfrags + 2) {
  1063. netif_tx_stop_queue(ntxq);
  1064. d = txq->cptr + nfrags + 16;
  1065. d &= m;
  1066. txq->ring[d].word3.bits.eofie = 1;
  1067. gmac_tx_irq_enable(netdev, txq_num, 1);
  1068. u64_stats_update_begin(&port->tx_stats_syncp);
  1069. netdev->stats.tx_fifo_errors++;
  1070. u64_stats_update_end(&port->tx_stats_syncp);
  1071. return NETDEV_TX_BUSY;
  1072. }
  1073. }
  1074. if (gmac_map_tx_bufs(netdev, skb, txq, &w)) {
  1075. if (skb_linearize(skb))
  1076. goto out_drop;
  1077. u64_stats_update_begin(&port->tx_stats_syncp);
  1078. port->tx_frags_linearized++;
  1079. u64_stats_update_end(&port->tx_stats_syncp);
  1080. if (gmac_map_tx_bufs(netdev, skb, txq, &w))
  1081. goto out_drop_free;
  1082. }
  1083. writew(w, ptr_reg + 2);
  1084. gmac_clean_txq(netdev, txq, r);
  1085. return NETDEV_TX_OK;
  1086. out_drop_free:
  1087. dev_kfree_skb(skb);
  1088. out_drop:
  1089. u64_stats_update_begin(&port->tx_stats_syncp);
  1090. port->stats.tx_dropped++;
  1091. u64_stats_update_end(&port->tx_stats_syncp);
  1092. return NETDEV_TX_OK;
  1093. }
  1094. static void gmac_tx_timeout(struct net_device *netdev)
  1095. {
  1096. netdev_err(netdev, "Tx timeout\n");
  1097. gmac_dump_dma_state(netdev);
  1098. }
  1099. static void gmac_enable_irq(struct net_device *netdev, int enable)
  1100. {
  1101. struct gemini_ethernet_port *port = netdev_priv(netdev);
  1102. struct gemini_ethernet *geth = port->geth;
  1103. unsigned long flags;
  1104. u32 val, mask;
  1105. netdev_dbg(netdev, "%s device %d %s\n", __func__,
  1106. netdev->dev_id, enable ? "enable" : "disable");
  1107. spin_lock_irqsave(&geth->irq_lock, flags);
  1108. mask = GMAC0_IRQ0_2 << (netdev->dev_id * 2);
  1109. val = readl(geth->base + GLOBAL_INTERRUPT_ENABLE_0_REG);
  1110. val = enable ? (val | mask) : (val & ~mask);
  1111. writel(val, geth->base + GLOBAL_INTERRUPT_ENABLE_0_REG);
  1112. mask = DEFAULT_Q0_INT_BIT << netdev->dev_id;
  1113. val = readl(geth->base + GLOBAL_INTERRUPT_ENABLE_1_REG);
  1114. val = enable ? (val | mask) : (val & ~mask);
  1115. writel(val, geth->base + GLOBAL_INTERRUPT_ENABLE_1_REG);
  1116. mask = GMAC0_IRQ4_8 << (netdev->dev_id * 8);
  1117. val = readl(geth->base + GLOBAL_INTERRUPT_ENABLE_4_REG);
  1118. val = enable ? (val | mask) : (val & ~mask);
  1119. writel(val, geth->base + GLOBAL_INTERRUPT_ENABLE_4_REG);
  1120. spin_unlock_irqrestore(&geth->irq_lock, flags);
  1121. }
  1122. static void gmac_enable_rx_irq(struct net_device *netdev, int enable)
  1123. {
  1124. struct gemini_ethernet_port *port = netdev_priv(netdev);
  1125. struct gemini_ethernet *geth = port->geth;
  1126. unsigned long flags;
  1127. u32 val, mask;
  1128. netdev_dbg(netdev, "%s device %d %s\n", __func__, netdev->dev_id,
  1129. enable ? "enable" : "disable");
  1130. spin_lock_irqsave(&geth->irq_lock, flags);
  1131. mask = DEFAULT_Q0_INT_BIT << netdev->dev_id;
  1132. val = readl(geth->base + GLOBAL_INTERRUPT_ENABLE_1_REG);
  1133. val = enable ? (val | mask) : (val & ~mask);
  1134. writel(val, geth->base + GLOBAL_INTERRUPT_ENABLE_1_REG);
  1135. spin_unlock_irqrestore(&geth->irq_lock, flags);
  1136. }
  1137. static struct sk_buff *gmac_skb_if_good_frame(struct gemini_ethernet_port *port,
  1138. union gmac_rxdesc_0 word0,
  1139. unsigned int frame_len)
  1140. {
  1141. unsigned int rx_csum = word0.bits.chksum_status;
  1142. unsigned int rx_status = word0.bits.status;
  1143. struct sk_buff *skb = NULL;
  1144. port->rx_stats[rx_status]++;
  1145. port->rx_csum_stats[rx_csum]++;
  1146. if (word0.bits.derr || word0.bits.perr ||
  1147. rx_status || frame_len < ETH_ZLEN ||
  1148. rx_csum >= RX_CHKSUM_IP_ERR_UNKNOWN) {
  1149. port->stats.rx_errors++;
  1150. if (frame_len < ETH_ZLEN || RX_ERROR_LENGTH(rx_status))
  1151. port->stats.rx_length_errors++;
  1152. if (RX_ERROR_OVER(rx_status))
  1153. port->stats.rx_over_errors++;
  1154. if (RX_ERROR_CRC(rx_status))
  1155. port->stats.rx_crc_errors++;
  1156. if (RX_ERROR_FRAME(rx_status))
  1157. port->stats.rx_frame_errors++;
  1158. return NULL;
  1159. }
  1160. skb = napi_get_frags(&port->napi);
  1161. if (!skb)
  1162. goto update_exit;
  1163. if (rx_csum == RX_CHKSUM_IP_UDP_TCP_OK)
  1164. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1165. update_exit:
  1166. port->stats.rx_bytes += frame_len;
  1167. port->stats.rx_packets++;
  1168. return skb;
  1169. }
  1170. static unsigned int gmac_rx(struct net_device *netdev, unsigned int budget)
  1171. {
  1172. struct gemini_ethernet_port *port = netdev_priv(netdev);
  1173. unsigned short m = (1 << port->rxq_order) - 1;
  1174. struct gemini_ethernet *geth = port->geth;
  1175. void __iomem *ptr_reg = port->rxq_rwptr;
  1176. unsigned int frame_len, frag_len;
  1177. struct gmac_rxdesc *rx = NULL;
  1178. struct gmac_queue_page *gpage;
  1179. static struct sk_buff *skb;
  1180. union gmac_rxdesc_0 word0;
  1181. union gmac_rxdesc_1 word1;
  1182. union gmac_rxdesc_3 word3;
  1183. struct page *page = NULL;
  1184. unsigned int page_offs;
  1185. unsigned short r, w;
  1186. union dma_rwptr rw;
  1187. dma_addr_t mapping;
  1188. int frag_nr = 0;
  1189. rw.bits32 = readl(ptr_reg);
  1190. /* Reset interrupt as all packages until here are taken into account */
  1191. writel(DEFAULT_Q0_INT_BIT << netdev->dev_id,
  1192. geth->base + GLOBAL_INTERRUPT_STATUS_1_REG);
  1193. r = rw.bits.rptr;
  1194. w = rw.bits.wptr;
  1195. while (budget && w != r) {
  1196. rx = port->rxq_ring + r;
  1197. word0 = rx->word0;
  1198. word1 = rx->word1;
  1199. mapping = rx->word2.buf_adr;
  1200. word3 = rx->word3;
  1201. r++;
  1202. r &= m;
  1203. frag_len = word0.bits.buffer_size;
  1204. frame_len = word1.bits.byte_count;
  1205. page_offs = mapping & ~PAGE_MASK;
  1206. if (!mapping) {
  1207. netdev_err(netdev,
  1208. "rxq[%u]: HW BUG: zero DMA desc\n", r);
  1209. goto err_drop;
  1210. }
  1211. /* Freeq pointers are one page off */
  1212. gpage = gmac_get_queue_page(geth, port, mapping + PAGE_SIZE);
  1213. if (!gpage) {
  1214. dev_err(geth->dev, "could not find mapping\n");
  1215. continue;
  1216. }
  1217. page = gpage->page;
  1218. if (word3.bits32 & SOF_BIT) {
  1219. if (skb) {
  1220. napi_free_frags(&port->napi);
  1221. port->stats.rx_dropped++;
  1222. }
  1223. skb = gmac_skb_if_good_frame(port, word0, frame_len);
  1224. if (!skb)
  1225. goto err_drop;
  1226. page_offs += NET_IP_ALIGN;
  1227. frag_len -= NET_IP_ALIGN;
  1228. frag_nr = 0;
  1229. } else if (!skb) {
  1230. put_page(page);
  1231. continue;
  1232. }
  1233. if (word3.bits32 & EOF_BIT)
  1234. frag_len = frame_len - skb->len;
  1235. /* append page frag to skb */
  1236. if (frag_nr == MAX_SKB_FRAGS)
  1237. goto err_drop;
  1238. if (frag_len == 0)
  1239. netdev_err(netdev, "Received fragment with len = 0\n");
  1240. skb_fill_page_desc(skb, frag_nr, page, page_offs, frag_len);
  1241. skb->len += frag_len;
  1242. skb->data_len += frag_len;
  1243. skb->truesize += frag_len;
  1244. frag_nr++;
  1245. if (word3.bits32 & EOF_BIT) {
  1246. napi_gro_frags(&port->napi);
  1247. skb = NULL;
  1248. --budget;
  1249. }
  1250. continue;
  1251. err_drop:
  1252. if (skb) {
  1253. napi_free_frags(&port->napi);
  1254. skb = NULL;
  1255. }
  1256. if (mapping)
  1257. put_page(page);
  1258. port->stats.rx_dropped++;
  1259. }
  1260. writew(r, ptr_reg);
  1261. return budget;
  1262. }
  1263. static int gmac_napi_poll(struct napi_struct *napi, int budget)
  1264. {
  1265. struct gemini_ethernet_port *port = netdev_priv(napi->dev);
  1266. struct gemini_ethernet *geth = port->geth;
  1267. unsigned int freeq_threshold;
  1268. unsigned int received;
  1269. freeq_threshold = 1 << (geth->freeq_order - 1);
  1270. u64_stats_update_begin(&port->rx_stats_syncp);
  1271. received = gmac_rx(napi->dev, budget);
  1272. if (received < budget) {
  1273. napi_gro_flush(napi, false);
  1274. napi_complete_done(napi, received);
  1275. gmac_enable_rx_irq(napi->dev, 1);
  1276. ++port->rx_napi_exits;
  1277. }
  1278. port->freeq_refill += (budget - received);
  1279. if (port->freeq_refill > freeq_threshold) {
  1280. port->freeq_refill -= freeq_threshold;
  1281. geth_fill_freeq(geth, true);
  1282. }
  1283. u64_stats_update_end(&port->rx_stats_syncp);
  1284. return received;
  1285. }
  1286. static void gmac_dump_dma_state(struct net_device *netdev)
  1287. {
  1288. struct gemini_ethernet_port *port = netdev_priv(netdev);
  1289. struct gemini_ethernet *geth = port->geth;
  1290. void __iomem *ptr_reg;
  1291. u32 reg[5];
  1292. /* Interrupt status */
  1293. reg[0] = readl(geth->base + GLOBAL_INTERRUPT_STATUS_0_REG);
  1294. reg[1] = readl(geth->base + GLOBAL_INTERRUPT_STATUS_1_REG);
  1295. reg[2] = readl(geth->base + GLOBAL_INTERRUPT_STATUS_2_REG);
  1296. reg[3] = readl(geth->base + GLOBAL_INTERRUPT_STATUS_3_REG);
  1297. reg[4] = readl(geth->base + GLOBAL_INTERRUPT_STATUS_4_REG);
  1298. netdev_err(netdev, "IRQ status: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
  1299. reg[0], reg[1], reg[2], reg[3], reg[4]);
  1300. /* Interrupt enable */
  1301. reg[0] = readl(geth->base + GLOBAL_INTERRUPT_ENABLE_0_REG);
  1302. reg[1] = readl(geth->base + GLOBAL_INTERRUPT_ENABLE_1_REG);
  1303. reg[2] = readl(geth->base + GLOBAL_INTERRUPT_ENABLE_2_REG);
  1304. reg[3] = readl(geth->base + GLOBAL_INTERRUPT_ENABLE_3_REG);
  1305. reg[4] = readl(geth->base + GLOBAL_INTERRUPT_ENABLE_4_REG);
  1306. netdev_err(netdev, "IRQ enable: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
  1307. reg[0], reg[1], reg[2], reg[3], reg[4]);
  1308. /* RX DMA status */
  1309. reg[0] = readl(port->dma_base + GMAC_DMA_RX_FIRST_DESC_REG);
  1310. reg[1] = readl(port->dma_base + GMAC_DMA_RX_CURR_DESC_REG);
  1311. reg[2] = GET_RPTR(port->rxq_rwptr);
  1312. reg[3] = GET_WPTR(port->rxq_rwptr);
  1313. netdev_err(netdev, "RX DMA regs: 0x%08x 0x%08x, ptr: %u %u\n",
  1314. reg[0], reg[1], reg[2], reg[3]);
  1315. reg[0] = readl(port->dma_base + GMAC_DMA_RX_DESC_WORD0_REG);
  1316. reg[1] = readl(port->dma_base + GMAC_DMA_RX_DESC_WORD1_REG);
  1317. reg[2] = readl(port->dma_base + GMAC_DMA_RX_DESC_WORD2_REG);
  1318. reg[3] = readl(port->dma_base + GMAC_DMA_RX_DESC_WORD3_REG);
  1319. netdev_err(netdev, "RX DMA descriptor: 0x%08x 0x%08x 0x%08x 0x%08x\n",
  1320. reg[0], reg[1], reg[2], reg[3]);
  1321. /* TX DMA status */
  1322. ptr_reg = port->dma_base + GMAC_SW_TX_QUEUE0_PTR_REG;
  1323. reg[0] = readl(port->dma_base + GMAC_DMA_TX_FIRST_DESC_REG);
  1324. reg[1] = readl(port->dma_base + GMAC_DMA_TX_CURR_DESC_REG);
  1325. reg[2] = GET_RPTR(ptr_reg);
  1326. reg[3] = GET_WPTR(ptr_reg);
  1327. netdev_err(netdev, "TX DMA regs: 0x%08x 0x%08x, ptr: %u %u\n",
  1328. reg[0], reg[1], reg[2], reg[3]);
  1329. reg[0] = readl(port->dma_base + GMAC_DMA_TX_DESC_WORD0_REG);
  1330. reg[1] = readl(port->dma_base + GMAC_DMA_TX_DESC_WORD1_REG);
  1331. reg[2] = readl(port->dma_base + GMAC_DMA_TX_DESC_WORD2_REG);
  1332. reg[3] = readl(port->dma_base + GMAC_DMA_TX_DESC_WORD3_REG);
  1333. netdev_err(netdev, "TX DMA descriptor: 0x%08x 0x%08x 0x%08x 0x%08x\n",
  1334. reg[0], reg[1], reg[2], reg[3]);
  1335. /* FREE queues status */
  1336. ptr_reg = geth->base + GLOBAL_SWFQ_RWPTR_REG;
  1337. reg[0] = GET_RPTR(ptr_reg);
  1338. reg[1] = GET_WPTR(ptr_reg);
  1339. ptr_reg = geth->base + GLOBAL_HWFQ_RWPTR_REG;
  1340. reg[2] = GET_RPTR(ptr_reg);
  1341. reg[3] = GET_WPTR(ptr_reg);
  1342. netdev_err(netdev, "FQ SW ptr: %u %u, HW ptr: %u %u\n",
  1343. reg[0], reg[1], reg[2], reg[3]);
  1344. }
  1345. static void gmac_update_hw_stats(struct net_device *netdev)
  1346. {
  1347. struct gemini_ethernet_port *port = netdev_priv(netdev);
  1348. unsigned int rx_discards, rx_mcast, rx_bcast;
  1349. struct gemini_ethernet *geth = port->geth;
  1350. unsigned long flags;
  1351. spin_lock_irqsave(&geth->irq_lock, flags);
  1352. u64_stats_update_begin(&port->ir_stats_syncp);
  1353. rx_discards = readl(port->gmac_base + GMAC_IN_DISCARDS);
  1354. port->hw_stats[0] += rx_discards;
  1355. port->hw_stats[1] += readl(port->gmac_base + GMAC_IN_ERRORS);
  1356. rx_mcast = readl(port->gmac_base + GMAC_IN_MCAST);
  1357. port->hw_stats[2] += rx_mcast;
  1358. rx_bcast = readl(port->gmac_base + GMAC_IN_BCAST);
  1359. port->hw_stats[3] += rx_bcast;
  1360. port->hw_stats[4] += readl(port->gmac_base + GMAC_IN_MAC1);
  1361. port->hw_stats[5] += readl(port->gmac_base + GMAC_IN_MAC2);
  1362. port->stats.rx_missed_errors += rx_discards;
  1363. port->stats.multicast += rx_mcast;
  1364. port->stats.multicast += rx_bcast;
  1365. writel(GMAC0_MIB_INT_BIT << (netdev->dev_id * 8),
  1366. geth->base + GLOBAL_INTERRUPT_STATUS_4_REG);
  1367. u64_stats_update_end(&port->ir_stats_syncp);
  1368. spin_unlock_irqrestore(&geth->irq_lock, flags);
  1369. }
  1370. /**
  1371. * gmac_get_intr_flags() - get interrupt status flags for a port from
  1372. * @netdev: the net device for the port to get flags from
  1373. * @i: the interrupt status register 0..4
  1374. */
  1375. static u32 gmac_get_intr_flags(struct net_device *netdev, int i)
  1376. {
  1377. struct gemini_ethernet_port *port = netdev_priv(netdev);
  1378. struct gemini_ethernet *geth = port->geth;
  1379. void __iomem *irqif_reg, *irqen_reg;
  1380. unsigned int offs, val;
  1381. /* Calculate the offset using the stride of the status registers */
  1382. offs = i * (GLOBAL_INTERRUPT_STATUS_1_REG -
  1383. GLOBAL_INTERRUPT_STATUS_0_REG);
  1384. irqif_reg = geth->base + GLOBAL_INTERRUPT_STATUS_0_REG + offs;
  1385. irqen_reg = geth->base + GLOBAL_INTERRUPT_ENABLE_0_REG + offs;
  1386. val = readl(irqif_reg) & readl(irqen_reg);
  1387. return val;
  1388. }
  1389. static enum hrtimer_restart gmac_coalesce_delay_expired(struct hrtimer *timer)
  1390. {
  1391. struct gemini_ethernet_port *port =
  1392. container_of(timer, struct gemini_ethernet_port,
  1393. rx_coalesce_timer);
  1394. napi_schedule(&port->napi);
  1395. return HRTIMER_NORESTART;
  1396. }
  1397. static irqreturn_t gmac_irq(int irq, void *data)
  1398. {
  1399. struct gemini_ethernet_port *port;
  1400. struct net_device *netdev = data;
  1401. struct gemini_ethernet *geth;
  1402. u32 val, orr = 0;
  1403. port = netdev_priv(netdev);
  1404. geth = port->geth;
  1405. val = gmac_get_intr_flags(netdev, 0);
  1406. orr |= val;
  1407. if (val & (GMAC0_IRQ0_2 << (netdev->dev_id * 2))) {
  1408. /* Oh, crap */
  1409. netdev_err(netdev, "hw failure/sw bug\n");
  1410. gmac_dump_dma_state(netdev);
  1411. /* don't know how to recover, just reduce losses */
  1412. gmac_enable_irq(netdev, 0);
  1413. return IRQ_HANDLED;
  1414. }
  1415. if (val & (GMAC0_IRQ0_TXQ0_INTS << (netdev->dev_id * 6)))
  1416. gmac_tx_irq(netdev, 0);
  1417. val = gmac_get_intr_flags(netdev, 1);
  1418. orr |= val;
  1419. if (val & (DEFAULT_Q0_INT_BIT << netdev->dev_id)) {
  1420. gmac_enable_rx_irq(netdev, 0);
  1421. if (!port->rx_coalesce_nsecs) {
  1422. napi_schedule(&port->napi);
  1423. } else {
  1424. ktime_t ktime;
  1425. ktime = ktime_set(0, port->rx_coalesce_nsecs);
  1426. hrtimer_start(&port->rx_coalesce_timer, ktime,
  1427. HRTIMER_MODE_REL);
  1428. }
  1429. }
  1430. val = gmac_get_intr_flags(netdev, 4);
  1431. orr |= val;
  1432. if (val & (GMAC0_MIB_INT_BIT << (netdev->dev_id * 8)))
  1433. gmac_update_hw_stats(netdev);
  1434. if (val & (GMAC0_RX_OVERRUN_INT_BIT << (netdev->dev_id * 8))) {
  1435. writel(GMAC0_RXDERR_INT_BIT << (netdev->dev_id * 8),
  1436. geth->base + GLOBAL_INTERRUPT_STATUS_4_REG);
  1437. spin_lock(&geth->irq_lock);
  1438. u64_stats_update_begin(&port->ir_stats_syncp);
  1439. ++port->stats.rx_fifo_errors;
  1440. u64_stats_update_end(&port->ir_stats_syncp);
  1441. spin_unlock(&geth->irq_lock);
  1442. }
  1443. return orr ? IRQ_HANDLED : IRQ_NONE;
  1444. }
  1445. static void gmac_start_dma(struct gemini_ethernet_port *port)
  1446. {
  1447. void __iomem *dma_ctrl_reg = port->dma_base + GMAC_DMA_CTRL_REG;
  1448. union gmac_dma_ctrl dma_ctrl;
  1449. dma_ctrl.bits32 = readl(dma_ctrl_reg);
  1450. dma_ctrl.bits.rd_enable = 1;
  1451. dma_ctrl.bits.td_enable = 1;
  1452. dma_ctrl.bits.loopback = 0;
  1453. dma_ctrl.bits.drop_small_ack = 0;
  1454. dma_ctrl.bits.rd_insert_bytes = NET_IP_ALIGN;
  1455. dma_ctrl.bits.rd_prot = HPROT_DATA_CACHE | HPROT_PRIVILIGED;
  1456. dma_ctrl.bits.rd_burst_size = HBURST_INCR8;
  1457. dma_ctrl.bits.rd_bus = HSIZE_8;
  1458. dma_ctrl.bits.td_prot = HPROT_DATA_CACHE;
  1459. dma_ctrl.bits.td_burst_size = HBURST_INCR8;
  1460. dma_ctrl.bits.td_bus = HSIZE_8;
  1461. writel(dma_ctrl.bits32, dma_ctrl_reg);
  1462. }
  1463. static void gmac_stop_dma(struct gemini_ethernet_port *port)
  1464. {
  1465. void __iomem *dma_ctrl_reg = port->dma_base + GMAC_DMA_CTRL_REG;
  1466. union gmac_dma_ctrl dma_ctrl;
  1467. dma_ctrl.bits32 = readl(dma_ctrl_reg);
  1468. dma_ctrl.bits.rd_enable = 0;
  1469. dma_ctrl.bits.td_enable = 0;
  1470. writel(dma_ctrl.bits32, dma_ctrl_reg);
  1471. }
  1472. static int gmac_open(struct net_device *netdev)
  1473. {
  1474. struct gemini_ethernet_port *port = netdev_priv(netdev);
  1475. int err;
  1476. if (!netdev->phydev) {
  1477. err = gmac_setup_phy(netdev);
  1478. if (err) {
  1479. netif_err(port, ifup, netdev,
  1480. "PHY init failed: %d\n", err);
  1481. return err;
  1482. }
  1483. }
  1484. err = request_irq(netdev->irq, gmac_irq,
  1485. IRQF_SHARED, netdev->name, netdev);
  1486. if (err) {
  1487. netdev_err(netdev, "no IRQ\n");
  1488. return err;
  1489. }
  1490. netif_carrier_off(netdev);
  1491. phy_start(netdev->phydev);
  1492. err = geth_resize_freeq(port);
  1493. /* It's fine if it's just busy, the other port has set up
  1494. * the freeq in that case.
  1495. */
  1496. if (err && (err != -EBUSY)) {
  1497. netdev_err(netdev, "could not resize freeq\n");
  1498. goto err_stop_phy;
  1499. }
  1500. err = gmac_setup_rxq(netdev);
  1501. if (err) {
  1502. netdev_err(netdev, "could not setup RXQ\n");
  1503. goto err_stop_phy;
  1504. }
  1505. err = gmac_setup_txqs(netdev);
  1506. if (err) {
  1507. netdev_err(netdev, "could not setup TXQs\n");
  1508. gmac_cleanup_rxq(netdev);
  1509. goto err_stop_phy;
  1510. }
  1511. napi_enable(&port->napi);
  1512. gmac_start_dma(port);
  1513. gmac_enable_irq(netdev, 1);
  1514. gmac_enable_tx_rx(netdev);
  1515. netif_tx_start_all_queues(netdev);
  1516. hrtimer_init(&port->rx_coalesce_timer, CLOCK_MONOTONIC,
  1517. HRTIMER_MODE_REL);
  1518. port->rx_coalesce_timer.function = &gmac_coalesce_delay_expired;
  1519. netdev_dbg(netdev, "opened\n");
  1520. return 0;
  1521. err_stop_phy:
  1522. phy_stop(netdev->phydev);
  1523. free_irq(netdev->irq, netdev);
  1524. return err;
  1525. }
  1526. static int gmac_stop(struct net_device *netdev)
  1527. {
  1528. struct gemini_ethernet_port *port = netdev_priv(netdev);
  1529. hrtimer_cancel(&port->rx_coalesce_timer);
  1530. netif_tx_stop_all_queues(netdev);
  1531. gmac_disable_tx_rx(netdev);
  1532. gmac_stop_dma(port);
  1533. napi_disable(&port->napi);
  1534. gmac_enable_irq(netdev, 0);
  1535. gmac_cleanup_rxq(netdev);
  1536. gmac_cleanup_txqs(netdev);
  1537. phy_stop(netdev->phydev);
  1538. free_irq(netdev->irq, netdev);
  1539. gmac_update_hw_stats(netdev);
  1540. return 0;
  1541. }
  1542. static void gmac_set_rx_mode(struct net_device *netdev)
  1543. {
  1544. struct gemini_ethernet_port *port = netdev_priv(netdev);
  1545. union gmac_rx_fltr filter = { .bits = {
  1546. .broadcast = 1,
  1547. .multicast = 1,
  1548. .unicast = 1,
  1549. } };
  1550. struct netdev_hw_addr *ha;
  1551. unsigned int bit_nr;
  1552. u32 mc_filter[2];
  1553. mc_filter[1] = 0;
  1554. mc_filter[0] = 0;
  1555. if (netdev->flags & IFF_PROMISC) {
  1556. filter.bits.error = 1;
  1557. filter.bits.promiscuous = 1;
  1558. mc_filter[1] = ~0;
  1559. mc_filter[0] = ~0;
  1560. } else if (netdev->flags & IFF_ALLMULTI) {
  1561. mc_filter[1] = ~0;
  1562. mc_filter[0] = ~0;
  1563. } else {
  1564. netdev_for_each_mc_addr(ha, netdev) {
  1565. bit_nr = ~crc32_le(~0, ha->addr, ETH_ALEN) & 0x3f;
  1566. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 0x1f);
  1567. }
  1568. }
  1569. writel(mc_filter[0], port->gmac_base + GMAC_MCAST_FIL0);
  1570. writel(mc_filter[1], port->gmac_base + GMAC_MCAST_FIL1);
  1571. writel(filter.bits32, port->gmac_base + GMAC_RX_FLTR);
  1572. }
  1573. static void gmac_write_mac_address(struct net_device *netdev)
  1574. {
  1575. struct gemini_ethernet_port *port = netdev_priv(netdev);
  1576. __le32 addr[3];
  1577. memset(addr, 0, sizeof(addr));
  1578. memcpy(addr, netdev->dev_addr, ETH_ALEN);
  1579. writel(le32_to_cpu(addr[0]), port->gmac_base + GMAC_STA_ADD0);
  1580. writel(le32_to_cpu(addr[1]), port->gmac_base + GMAC_STA_ADD1);
  1581. writel(le32_to_cpu(addr[2]), port->gmac_base + GMAC_STA_ADD2);
  1582. }
  1583. static int gmac_set_mac_address(struct net_device *netdev, void *addr)
  1584. {
  1585. struct sockaddr *sa = addr;
  1586. memcpy(netdev->dev_addr, sa->sa_data, ETH_ALEN);
  1587. gmac_write_mac_address(netdev);
  1588. return 0;
  1589. }
  1590. static void gmac_clear_hw_stats(struct net_device *netdev)
  1591. {
  1592. struct gemini_ethernet_port *port = netdev_priv(netdev);
  1593. readl(port->gmac_base + GMAC_IN_DISCARDS);
  1594. readl(port->gmac_base + GMAC_IN_ERRORS);
  1595. readl(port->gmac_base + GMAC_IN_MCAST);
  1596. readl(port->gmac_base + GMAC_IN_BCAST);
  1597. readl(port->gmac_base + GMAC_IN_MAC1);
  1598. readl(port->gmac_base + GMAC_IN_MAC2);
  1599. }
  1600. static void gmac_get_stats64(struct net_device *netdev,
  1601. struct rtnl_link_stats64 *stats)
  1602. {
  1603. struct gemini_ethernet_port *port = netdev_priv(netdev);
  1604. unsigned int start;
  1605. gmac_update_hw_stats(netdev);
  1606. /* Racing with RX NAPI */
  1607. do {
  1608. start = u64_stats_fetch_begin(&port->rx_stats_syncp);
  1609. stats->rx_packets = port->stats.rx_packets;
  1610. stats->rx_bytes = port->stats.rx_bytes;
  1611. stats->rx_errors = port->stats.rx_errors;
  1612. stats->rx_dropped = port->stats.rx_dropped;
  1613. stats->rx_length_errors = port->stats.rx_length_errors;
  1614. stats->rx_over_errors = port->stats.rx_over_errors;
  1615. stats->rx_crc_errors = port->stats.rx_crc_errors;
  1616. stats->rx_frame_errors = port->stats.rx_frame_errors;
  1617. } while (u64_stats_fetch_retry(&port->rx_stats_syncp, start));
  1618. /* Racing with MIB and TX completion interrupts */
  1619. do {
  1620. start = u64_stats_fetch_begin(&port->ir_stats_syncp);
  1621. stats->tx_errors = port->stats.tx_errors;
  1622. stats->tx_packets = port->stats.tx_packets;
  1623. stats->tx_bytes = port->stats.tx_bytes;
  1624. stats->multicast = port->stats.multicast;
  1625. stats->rx_missed_errors = port->stats.rx_missed_errors;
  1626. stats->rx_fifo_errors = port->stats.rx_fifo_errors;
  1627. } while (u64_stats_fetch_retry(&port->ir_stats_syncp, start));
  1628. /* Racing with hard_start_xmit */
  1629. do {
  1630. start = u64_stats_fetch_begin(&port->tx_stats_syncp);
  1631. stats->tx_dropped = port->stats.tx_dropped;
  1632. } while (u64_stats_fetch_retry(&port->tx_stats_syncp, start));
  1633. stats->rx_dropped += stats->rx_missed_errors;
  1634. }
  1635. static int gmac_change_mtu(struct net_device *netdev, int new_mtu)
  1636. {
  1637. int max_len = gmac_pick_rx_max_len(new_mtu);
  1638. if (max_len < 0)
  1639. return -EINVAL;
  1640. gmac_disable_tx_rx(netdev);
  1641. netdev->mtu = new_mtu;
  1642. gmac_update_config0_reg(netdev, max_len << CONFIG0_MAXLEN_SHIFT,
  1643. CONFIG0_MAXLEN_MASK);
  1644. netdev_update_features(netdev);
  1645. gmac_enable_tx_rx(netdev);
  1646. return 0;
  1647. }
  1648. static netdev_features_t gmac_fix_features(struct net_device *netdev,
  1649. netdev_features_t features)
  1650. {
  1651. if (netdev->mtu + ETH_HLEN + VLAN_HLEN > MTU_SIZE_BIT_MASK)
  1652. features &= ~GMAC_OFFLOAD_FEATURES;
  1653. return features;
  1654. }
  1655. static int gmac_set_features(struct net_device *netdev,
  1656. netdev_features_t features)
  1657. {
  1658. struct gemini_ethernet_port *port = netdev_priv(netdev);
  1659. int enable = features & NETIF_F_RXCSUM;
  1660. unsigned long flags;
  1661. u32 reg;
  1662. spin_lock_irqsave(&port->config_lock, flags);
  1663. reg = readl(port->gmac_base + GMAC_CONFIG0);
  1664. reg = enable ? reg | CONFIG0_RX_CHKSUM : reg & ~CONFIG0_RX_CHKSUM;
  1665. writel(reg, port->gmac_base + GMAC_CONFIG0);
  1666. spin_unlock_irqrestore(&port->config_lock, flags);
  1667. return 0;
  1668. }
  1669. static int gmac_get_sset_count(struct net_device *netdev, int sset)
  1670. {
  1671. return sset == ETH_SS_STATS ? GMAC_STATS_NUM : 0;
  1672. }
  1673. static void gmac_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
  1674. {
  1675. if (stringset != ETH_SS_STATS)
  1676. return;
  1677. memcpy(data, gmac_stats_strings, sizeof(gmac_stats_strings));
  1678. }
  1679. static void gmac_get_ethtool_stats(struct net_device *netdev,
  1680. struct ethtool_stats *estats, u64 *values)
  1681. {
  1682. struct gemini_ethernet_port *port = netdev_priv(netdev);
  1683. unsigned int start;
  1684. u64 *p;
  1685. int i;
  1686. gmac_update_hw_stats(netdev);
  1687. /* Racing with MIB interrupt */
  1688. do {
  1689. p = values;
  1690. start = u64_stats_fetch_begin(&port->ir_stats_syncp);
  1691. for (i = 0; i < RX_STATS_NUM; i++)
  1692. *p++ = port->hw_stats[i];
  1693. } while (u64_stats_fetch_retry(&port->ir_stats_syncp, start));
  1694. values = p;
  1695. /* Racing with RX NAPI */
  1696. do {
  1697. p = values;
  1698. start = u64_stats_fetch_begin(&port->rx_stats_syncp);
  1699. for (i = 0; i < RX_STATUS_NUM; i++)
  1700. *p++ = port->rx_stats[i];
  1701. for (i = 0; i < RX_CHKSUM_NUM; i++)
  1702. *p++ = port->rx_csum_stats[i];
  1703. *p++ = port->rx_napi_exits;
  1704. } while (u64_stats_fetch_retry(&port->rx_stats_syncp, start));
  1705. values = p;
  1706. /* Racing with TX start_xmit */
  1707. do {
  1708. p = values;
  1709. start = u64_stats_fetch_begin(&port->tx_stats_syncp);
  1710. for (i = 0; i < TX_MAX_FRAGS; i++) {
  1711. *values++ = port->tx_frag_stats[i];
  1712. port->tx_frag_stats[i] = 0;
  1713. }
  1714. *values++ = port->tx_frags_linearized;
  1715. *values++ = port->tx_hw_csummed;
  1716. } while (u64_stats_fetch_retry(&port->tx_stats_syncp, start));
  1717. }
  1718. static int gmac_get_ksettings(struct net_device *netdev,
  1719. struct ethtool_link_ksettings *cmd)
  1720. {
  1721. if (!netdev->phydev)
  1722. return -ENXIO;
  1723. phy_ethtool_ksettings_get(netdev->phydev, cmd);
  1724. return 0;
  1725. }
  1726. static int gmac_set_ksettings(struct net_device *netdev,
  1727. const struct ethtool_link_ksettings *cmd)
  1728. {
  1729. if (!netdev->phydev)
  1730. return -ENXIO;
  1731. return phy_ethtool_ksettings_set(netdev->phydev, cmd);
  1732. }
  1733. static int gmac_nway_reset(struct net_device *netdev)
  1734. {
  1735. if (!netdev->phydev)
  1736. return -ENXIO;
  1737. return phy_start_aneg(netdev->phydev);
  1738. }
  1739. static void gmac_get_pauseparam(struct net_device *netdev,
  1740. struct ethtool_pauseparam *pparam)
  1741. {
  1742. struct gemini_ethernet_port *port = netdev_priv(netdev);
  1743. union gmac_config0 config0;
  1744. config0.bits32 = readl(port->gmac_base + GMAC_CONFIG0);
  1745. pparam->rx_pause = config0.bits.rx_fc_en;
  1746. pparam->tx_pause = config0.bits.tx_fc_en;
  1747. pparam->autoneg = true;
  1748. }
  1749. static void gmac_get_ringparam(struct net_device *netdev,
  1750. struct ethtool_ringparam *rp)
  1751. {
  1752. struct gemini_ethernet_port *port = netdev_priv(netdev);
  1753. union gmac_config0 config0;
  1754. config0.bits32 = readl(port->gmac_base + GMAC_CONFIG0);
  1755. rp->rx_max_pending = 1 << 15;
  1756. rp->rx_mini_max_pending = 0;
  1757. rp->rx_jumbo_max_pending = 0;
  1758. rp->tx_max_pending = 1 << 15;
  1759. rp->rx_pending = 1 << port->rxq_order;
  1760. rp->rx_mini_pending = 0;
  1761. rp->rx_jumbo_pending = 0;
  1762. rp->tx_pending = 1 << port->txq_order;
  1763. }
  1764. static int gmac_set_ringparam(struct net_device *netdev,
  1765. struct ethtool_ringparam *rp)
  1766. {
  1767. struct gemini_ethernet_port *port = netdev_priv(netdev);
  1768. int err = 0;
  1769. if (netif_running(netdev))
  1770. return -EBUSY;
  1771. if (rp->rx_pending) {
  1772. port->rxq_order = min(15, ilog2(rp->rx_pending - 1) + 1);
  1773. err = geth_resize_freeq(port);
  1774. }
  1775. if (rp->tx_pending) {
  1776. port->txq_order = min(15, ilog2(rp->tx_pending - 1) + 1);
  1777. port->irq_every_tx_packets = 1 << (port->txq_order - 2);
  1778. }
  1779. return err;
  1780. }
  1781. static int gmac_get_coalesce(struct net_device *netdev,
  1782. struct ethtool_coalesce *ecmd)
  1783. {
  1784. struct gemini_ethernet_port *port = netdev_priv(netdev);
  1785. ecmd->rx_max_coalesced_frames = 1;
  1786. ecmd->tx_max_coalesced_frames = port->irq_every_tx_packets;
  1787. ecmd->rx_coalesce_usecs = port->rx_coalesce_nsecs / 1000;
  1788. return 0;
  1789. }
  1790. static int gmac_set_coalesce(struct net_device *netdev,
  1791. struct ethtool_coalesce *ecmd)
  1792. {
  1793. struct gemini_ethernet_port *port = netdev_priv(netdev);
  1794. if (ecmd->tx_max_coalesced_frames < 1)
  1795. return -EINVAL;
  1796. if (ecmd->tx_max_coalesced_frames >= 1 << port->txq_order)
  1797. return -EINVAL;
  1798. port->irq_every_tx_packets = ecmd->tx_max_coalesced_frames;
  1799. port->rx_coalesce_nsecs = ecmd->rx_coalesce_usecs * 1000;
  1800. return 0;
  1801. }
  1802. static u32 gmac_get_msglevel(struct net_device *netdev)
  1803. {
  1804. struct gemini_ethernet_port *port = netdev_priv(netdev);
  1805. return port->msg_enable;
  1806. }
  1807. static void gmac_set_msglevel(struct net_device *netdev, u32 level)
  1808. {
  1809. struct gemini_ethernet_port *port = netdev_priv(netdev);
  1810. port->msg_enable = level;
  1811. }
  1812. static void gmac_get_drvinfo(struct net_device *netdev,
  1813. struct ethtool_drvinfo *info)
  1814. {
  1815. strcpy(info->driver, DRV_NAME);
  1816. strcpy(info->version, DRV_VERSION);
  1817. strcpy(info->bus_info, netdev->dev_id ? "1" : "0");
  1818. }
  1819. static const struct net_device_ops gmac_351x_ops = {
  1820. .ndo_init = gmac_init,
  1821. .ndo_uninit = gmac_uninit,
  1822. .ndo_open = gmac_open,
  1823. .ndo_stop = gmac_stop,
  1824. .ndo_start_xmit = gmac_start_xmit,
  1825. .ndo_tx_timeout = gmac_tx_timeout,
  1826. .ndo_set_rx_mode = gmac_set_rx_mode,
  1827. .ndo_set_mac_address = gmac_set_mac_address,
  1828. .ndo_get_stats64 = gmac_get_stats64,
  1829. .ndo_change_mtu = gmac_change_mtu,
  1830. .ndo_fix_features = gmac_fix_features,
  1831. .ndo_set_features = gmac_set_features,
  1832. };
  1833. static const struct ethtool_ops gmac_351x_ethtool_ops = {
  1834. .get_sset_count = gmac_get_sset_count,
  1835. .get_strings = gmac_get_strings,
  1836. .get_ethtool_stats = gmac_get_ethtool_stats,
  1837. .get_link = ethtool_op_get_link,
  1838. .get_link_ksettings = gmac_get_ksettings,
  1839. .set_link_ksettings = gmac_set_ksettings,
  1840. .nway_reset = gmac_nway_reset,
  1841. .get_pauseparam = gmac_get_pauseparam,
  1842. .get_ringparam = gmac_get_ringparam,
  1843. .set_ringparam = gmac_set_ringparam,
  1844. .get_coalesce = gmac_get_coalesce,
  1845. .set_coalesce = gmac_set_coalesce,
  1846. .get_msglevel = gmac_get_msglevel,
  1847. .set_msglevel = gmac_set_msglevel,
  1848. .get_drvinfo = gmac_get_drvinfo,
  1849. };
  1850. static irqreturn_t gemini_port_irq_thread(int irq, void *data)
  1851. {
  1852. unsigned long irqmask = SWFQ_EMPTY_INT_BIT;
  1853. struct gemini_ethernet_port *port = data;
  1854. struct gemini_ethernet *geth;
  1855. unsigned long flags;
  1856. geth = port->geth;
  1857. /* The queue is half empty so refill it */
  1858. geth_fill_freeq(geth, true);
  1859. spin_lock_irqsave(&geth->irq_lock, flags);
  1860. /* ACK queue interrupt */
  1861. writel(irqmask, geth->base + GLOBAL_INTERRUPT_STATUS_4_REG);
  1862. /* Enable queue interrupt again */
  1863. irqmask |= readl(geth->base + GLOBAL_INTERRUPT_ENABLE_4_REG);
  1864. writel(irqmask, geth->base + GLOBAL_INTERRUPT_ENABLE_4_REG);
  1865. spin_unlock_irqrestore(&geth->irq_lock, flags);
  1866. return IRQ_HANDLED;
  1867. }
  1868. static irqreturn_t gemini_port_irq(int irq, void *data)
  1869. {
  1870. struct gemini_ethernet_port *port = data;
  1871. struct gemini_ethernet *geth;
  1872. irqreturn_t ret = IRQ_NONE;
  1873. u32 val, en;
  1874. geth = port->geth;
  1875. spin_lock(&geth->irq_lock);
  1876. val = readl(geth->base + GLOBAL_INTERRUPT_STATUS_4_REG);
  1877. en = readl(geth->base + GLOBAL_INTERRUPT_ENABLE_4_REG);
  1878. if (val & en & SWFQ_EMPTY_INT_BIT) {
  1879. /* Disable the queue empty interrupt while we work on
  1880. * processing the queue. Also disable overrun interrupts
  1881. * as there is not much we can do about it here.
  1882. */
  1883. en &= ~(SWFQ_EMPTY_INT_BIT | GMAC0_RX_OVERRUN_INT_BIT
  1884. | GMAC1_RX_OVERRUN_INT_BIT);
  1885. writel(en, geth->base + GLOBAL_INTERRUPT_ENABLE_4_REG);
  1886. ret = IRQ_WAKE_THREAD;
  1887. }
  1888. spin_unlock(&geth->irq_lock);
  1889. return ret;
  1890. }
  1891. static void gemini_port_remove(struct gemini_ethernet_port *port)
  1892. {
  1893. if (port->netdev)
  1894. unregister_netdev(port->netdev);
  1895. clk_disable_unprepare(port->pclk);
  1896. geth_cleanup_freeq(port->geth);
  1897. }
  1898. static void gemini_ethernet_init(struct gemini_ethernet *geth)
  1899. {
  1900. /* Only do this once both ports are online */
  1901. if (geth->initialized)
  1902. return;
  1903. if (geth->port0 && geth->port1)
  1904. geth->initialized = true;
  1905. else
  1906. return;
  1907. writel(0, geth->base + GLOBAL_INTERRUPT_ENABLE_0_REG);
  1908. writel(0, geth->base + GLOBAL_INTERRUPT_ENABLE_1_REG);
  1909. writel(0, geth->base + GLOBAL_INTERRUPT_ENABLE_2_REG);
  1910. writel(0, geth->base + GLOBAL_INTERRUPT_ENABLE_3_REG);
  1911. writel(0, geth->base + GLOBAL_INTERRUPT_ENABLE_4_REG);
  1912. /* Interrupt config:
  1913. *
  1914. * GMAC0 intr bits ------> int0 ----> eth0
  1915. * GMAC1 intr bits ------> int1 ----> eth1
  1916. * TOE intr -------------> int1 ----> eth1
  1917. * Classification Intr --> int0 ----> eth0
  1918. * Default Q0 -----------> int0 ----> eth0
  1919. * Default Q1 -----------> int1 ----> eth1
  1920. * FreeQ intr -----------> int1 ----> eth1
  1921. */
  1922. writel(0xCCFC0FC0, geth->base + GLOBAL_INTERRUPT_SELECT_0_REG);
  1923. writel(0x00F00002, geth->base + GLOBAL_INTERRUPT_SELECT_1_REG);
  1924. writel(0xFFFFFFFF, geth->base + GLOBAL_INTERRUPT_SELECT_2_REG);
  1925. writel(0xFFFFFFFF, geth->base + GLOBAL_INTERRUPT_SELECT_3_REG);
  1926. writel(0xFF000003, geth->base + GLOBAL_INTERRUPT_SELECT_4_REG);
  1927. /* edge-triggered interrupts packed to level-triggered one... */
  1928. writel(~0, geth->base + GLOBAL_INTERRUPT_STATUS_0_REG);
  1929. writel(~0, geth->base + GLOBAL_INTERRUPT_STATUS_1_REG);
  1930. writel(~0, geth->base + GLOBAL_INTERRUPT_STATUS_2_REG);
  1931. writel(~0, geth->base + GLOBAL_INTERRUPT_STATUS_3_REG);
  1932. writel(~0, geth->base + GLOBAL_INTERRUPT_STATUS_4_REG);
  1933. /* Set up queue */
  1934. writel(0, geth->base + GLOBAL_SW_FREEQ_BASE_SIZE_REG);
  1935. writel(0, geth->base + GLOBAL_HW_FREEQ_BASE_SIZE_REG);
  1936. writel(0, geth->base + GLOBAL_SWFQ_RWPTR_REG);
  1937. writel(0, geth->base + GLOBAL_HWFQ_RWPTR_REG);
  1938. geth->freeq_frag_order = DEFAULT_RX_BUF_ORDER;
  1939. /* This makes the queue resize on probe() so that we
  1940. * set up and enable the queue IRQ. FIXME: fragile.
  1941. */
  1942. geth->freeq_order = 1;
  1943. }
  1944. static void gemini_port_save_mac_addr(struct gemini_ethernet_port *port)
  1945. {
  1946. port->mac_addr[0] =
  1947. cpu_to_le32(readl(port->gmac_base + GMAC_STA_ADD0));
  1948. port->mac_addr[1] =
  1949. cpu_to_le32(readl(port->gmac_base + GMAC_STA_ADD1));
  1950. port->mac_addr[2] =
  1951. cpu_to_le32(readl(port->gmac_base + GMAC_STA_ADD2));
  1952. }
  1953. static int gemini_ethernet_port_probe(struct platform_device *pdev)
  1954. {
  1955. char *port_names[2] = { "ethernet0", "ethernet1" };
  1956. struct gemini_ethernet_port *port;
  1957. struct device *dev = &pdev->dev;
  1958. struct gemini_ethernet *geth;
  1959. struct net_device *netdev;
  1960. struct resource *gmacres;
  1961. struct resource *dmares;
  1962. struct device *parent;
  1963. unsigned int id;
  1964. int irq;
  1965. int ret;
  1966. parent = dev->parent;
  1967. geth = dev_get_drvdata(parent);
  1968. if (!strcmp(dev_name(dev), "60008000.ethernet-port"))
  1969. id = 0;
  1970. else if (!strcmp(dev_name(dev), "6000c000.ethernet-port"))
  1971. id = 1;
  1972. else
  1973. return -ENODEV;
  1974. dev_info(dev, "probe %s ID %d\n", dev_name(dev), id);
  1975. netdev = devm_alloc_etherdev_mqs(dev, sizeof(*port), TX_QUEUE_NUM, TX_QUEUE_NUM);
  1976. if (!netdev) {
  1977. dev_err(dev, "Can't allocate ethernet device #%d\n", id);
  1978. return -ENOMEM;
  1979. }
  1980. port = netdev_priv(netdev);
  1981. SET_NETDEV_DEV(netdev, dev);
  1982. port->netdev = netdev;
  1983. port->id = id;
  1984. port->geth = geth;
  1985. port->dev = dev;
  1986. port->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
  1987. /* DMA memory */
  1988. dmares = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1989. if (!dmares) {
  1990. dev_err(dev, "no DMA resource\n");
  1991. return -ENODEV;
  1992. }
  1993. port->dma_base = devm_ioremap_resource(dev, dmares);
  1994. if (IS_ERR(port->dma_base))
  1995. return PTR_ERR(port->dma_base);
  1996. /* GMAC config memory */
  1997. gmacres = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1998. if (!gmacres) {
  1999. dev_err(dev, "no GMAC resource\n");
  2000. return -ENODEV;
  2001. }
  2002. port->gmac_base = devm_ioremap_resource(dev, gmacres);
  2003. if (IS_ERR(port->gmac_base))
  2004. return PTR_ERR(port->gmac_base);
  2005. /* Interrupt */
  2006. irq = platform_get_irq(pdev, 0);
  2007. if (irq <= 0)
  2008. return irq ? irq : -ENODEV;
  2009. port->irq = irq;
  2010. /* Clock the port */
  2011. port->pclk = devm_clk_get(dev, "PCLK");
  2012. if (IS_ERR(port->pclk)) {
  2013. dev_err(dev, "no PCLK\n");
  2014. return PTR_ERR(port->pclk);
  2015. }
  2016. ret = clk_prepare_enable(port->pclk);
  2017. if (ret)
  2018. return ret;
  2019. /* Maybe there is a nice ethernet address we should use */
  2020. gemini_port_save_mac_addr(port);
  2021. /* Reset the port */
  2022. port->reset = devm_reset_control_get_exclusive(dev, NULL);
  2023. if (IS_ERR(port->reset)) {
  2024. dev_err(dev, "no reset\n");
  2025. ret = PTR_ERR(port->reset);
  2026. goto unprepare;
  2027. }
  2028. reset_control_reset(port->reset);
  2029. usleep_range(100, 500);
  2030. /* Assign pointer in the main state container */
  2031. if (!id)
  2032. geth->port0 = port;
  2033. else
  2034. geth->port1 = port;
  2035. /* This will just be done once both ports are up and reset */
  2036. gemini_ethernet_init(geth);
  2037. platform_set_drvdata(pdev, port);
  2038. /* Set up and register the netdev */
  2039. netdev->dev_id = port->id;
  2040. netdev->irq = irq;
  2041. netdev->netdev_ops = &gmac_351x_ops;
  2042. netdev->ethtool_ops = &gmac_351x_ethtool_ops;
  2043. spin_lock_init(&port->config_lock);
  2044. gmac_clear_hw_stats(netdev);
  2045. netdev->hw_features = GMAC_OFFLOAD_FEATURES;
  2046. netdev->features |= GMAC_OFFLOAD_FEATURES | NETIF_F_GRO;
  2047. /* We can handle jumbo frames up to 10236 bytes so, let's accept
  2048. * payloads of 10236 bytes minus VLAN and ethernet header
  2049. */
  2050. netdev->min_mtu = ETH_MIN_MTU;
  2051. netdev->max_mtu = 10236 - VLAN_ETH_HLEN;
  2052. port->freeq_refill = 0;
  2053. netif_napi_add(netdev, &port->napi, gmac_napi_poll,
  2054. DEFAULT_NAPI_WEIGHT);
  2055. if (is_valid_ether_addr((void *)port->mac_addr)) {
  2056. memcpy(netdev->dev_addr, port->mac_addr, ETH_ALEN);
  2057. } else {
  2058. dev_dbg(dev, "ethernet address 0x%08x%08x%08x invalid\n",
  2059. port->mac_addr[0], port->mac_addr[1],
  2060. port->mac_addr[2]);
  2061. dev_info(dev, "using a random ethernet address\n");
  2062. eth_random_addr(netdev->dev_addr);
  2063. }
  2064. gmac_write_mac_address(netdev);
  2065. ret = devm_request_threaded_irq(port->dev,
  2066. port->irq,
  2067. gemini_port_irq,
  2068. gemini_port_irq_thread,
  2069. IRQF_SHARED,
  2070. port_names[port->id],
  2071. port);
  2072. if (ret)
  2073. goto unprepare;
  2074. ret = register_netdev(netdev);
  2075. if (ret)
  2076. goto unprepare;
  2077. netdev_info(netdev,
  2078. "irq %d, DMA @ 0x%pap, GMAC @ 0x%pap\n",
  2079. port->irq, &dmares->start,
  2080. &gmacres->start);
  2081. ret = gmac_setup_phy(netdev);
  2082. if (ret)
  2083. netdev_info(netdev,
  2084. "PHY init failed, deferring to ifup time\n");
  2085. return 0;
  2086. unprepare:
  2087. clk_disable_unprepare(port->pclk);
  2088. return ret;
  2089. }
  2090. static int gemini_ethernet_port_remove(struct platform_device *pdev)
  2091. {
  2092. struct gemini_ethernet_port *port = platform_get_drvdata(pdev);
  2093. gemini_port_remove(port);
  2094. return 0;
  2095. }
  2096. static const struct of_device_id gemini_ethernet_port_of_match[] = {
  2097. {
  2098. .compatible = "cortina,gemini-ethernet-port",
  2099. },
  2100. {},
  2101. };
  2102. MODULE_DEVICE_TABLE(of, gemini_ethernet_port_of_match);
  2103. static struct platform_driver gemini_ethernet_port_driver = {
  2104. .driver = {
  2105. .name = "gemini-ethernet-port",
  2106. .of_match_table = of_match_ptr(gemini_ethernet_port_of_match),
  2107. },
  2108. .probe = gemini_ethernet_port_probe,
  2109. .remove = gemini_ethernet_port_remove,
  2110. };
  2111. static int gemini_ethernet_probe(struct platform_device *pdev)
  2112. {
  2113. struct device *dev = &pdev->dev;
  2114. struct gemini_ethernet *geth;
  2115. unsigned int retry = 5;
  2116. struct resource *res;
  2117. u32 val;
  2118. /* Global registers */
  2119. geth = devm_kzalloc(dev, sizeof(*geth), GFP_KERNEL);
  2120. if (!geth)
  2121. return -ENOMEM;
  2122. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2123. if (!res)
  2124. return -ENODEV;
  2125. geth->base = devm_ioremap_resource(dev, res);
  2126. if (IS_ERR(geth->base))
  2127. return PTR_ERR(geth->base);
  2128. geth->dev = dev;
  2129. /* Wait for ports to stabilize */
  2130. do {
  2131. udelay(2);
  2132. val = readl(geth->base + GLOBAL_TOE_VERSION_REG);
  2133. barrier();
  2134. } while (!val && --retry);
  2135. if (!retry) {
  2136. dev_err(dev, "failed to reset ethernet\n");
  2137. return -EIO;
  2138. }
  2139. dev_info(dev, "Ethernet device ID: 0x%03x, revision 0x%01x\n",
  2140. (val >> 4) & 0xFFFU, val & 0xFU);
  2141. spin_lock_init(&geth->irq_lock);
  2142. spin_lock_init(&geth->freeq_lock);
  2143. /* The children will use this */
  2144. platform_set_drvdata(pdev, geth);
  2145. /* Spawn child devices for the two ports */
  2146. return devm_of_platform_populate(dev);
  2147. }
  2148. static int gemini_ethernet_remove(struct platform_device *pdev)
  2149. {
  2150. struct gemini_ethernet *geth = platform_get_drvdata(pdev);
  2151. geth_cleanup_freeq(geth);
  2152. geth->initialized = false;
  2153. return 0;
  2154. }
  2155. static const struct of_device_id gemini_ethernet_of_match[] = {
  2156. {
  2157. .compatible = "cortina,gemini-ethernet",
  2158. },
  2159. {},
  2160. };
  2161. MODULE_DEVICE_TABLE(of, gemini_ethernet_of_match);
  2162. static struct platform_driver gemini_ethernet_driver = {
  2163. .driver = {
  2164. .name = DRV_NAME,
  2165. .of_match_table = of_match_ptr(gemini_ethernet_of_match),
  2166. },
  2167. .probe = gemini_ethernet_probe,
  2168. .remove = gemini_ethernet_remove,
  2169. };
  2170. static int __init gemini_ethernet_module_init(void)
  2171. {
  2172. int ret;
  2173. ret = platform_driver_register(&gemini_ethernet_port_driver);
  2174. if (ret)
  2175. return ret;
  2176. ret = platform_driver_register(&gemini_ethernet_driver);
  2177. if (ret) {
  2178. platform_driver_unregister(&gemini_ethernet_port_driver);
  2179. return ret;
  2180. }
  2181. return 0;
  2182. }
  2183. module_init(gemini_ethernet_module_init);
  2184. static void __exit gemini_ethernet_module_exit(void)
  2185. {
  2186. platform_driver_unregister(&gemini_ethernet_driver);
  2187. platform_driver_unregister(&gemini_ethernet_port_driver);
  2188. }
  2189. module_exit(gemini_ethernet_module_exit);
  2190. MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
  2191. MODULE_DESCRIPTION("StorLink SL351x (Gemini) ethernet driver");
  2192. MODULE_LICENSE("GPL");
  2193. MODULE_ALIAS("platform:" DRV_NAME);