gemini.c 68 KB

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