hip04_eth.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. /* Copyright (c) 2014 Linaro Ltd.
  2. * Copyright (c) 2014 Hisilicon Limited.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/etherdevice.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/ktime.h>
  14. #include <linux/of_address.h>
  15. #include <linux/phy.h>
  16. #include <linux/of_mdio.h>
  17. #include <linux/of_net.h>
  18. #include <linux/mfd/syscon.h>
  19. #include <linux/regmap.h>
  20. #define PPE_CFG_RX_ADDR 0x100
  21. #define PPE_CFG_POOL_GRP 0x300
  22. #define PPE_CFG_RX_BUF_SIZE 0x400
  23. #define PPE_CFG_RX_FIFO_SIZE 0x500
  24. #define PPE_CURR_BUF_CNT 0xa200
  25. #define GE_DUPLEX_TYPE 0x08
  26. #define GE_MAX_FRM_SIZE_REG 0x3c
  27. #define GE_PORT_MODE 0x40
  28. #define GE_PORT_EN 0x44
  29. #define GE_SHORT_RUNTS_THR_REG 0x50
  30. #define GE_TX_LOCAL_PAGE_REG 0x5c
  31. #define GE_TRANSMIT_CONTROL_REG 0x60
  32. #define GE_CF_CRC_STRIP_REG 0x1b0
  33. #define GE_MODE_CHANGE_REG 0x1b4
  34. #define GE_RECV_CONTROL_REG 0x1e0
  35. #define GE_STATION_MAC_ADDRESS 0x210
  36. #define PPE_CFG_CPU_ADD_ADDR 0x580
  37. #define PPE_CFG_MAX_FRAME_LEN_REG 0x408
  38. #define PPE_CFG_BUS_CTRL_REG 0x424
  39. #define PPE_CFG_RX_CTRL_REG 0x428
  40. #define PPE_CFG_RX_PKT_MODE_REG 0x438
  41. #define PPE_CFG_QOS_VMID_GEN 0x500
  42. #define PPE_CFG_RX_PKT_INT 0x538
  43. #define PPE_INTEN 0x600
  44. #define PPE_INTSTS 0x608
  45. #define PPE_RINT 0x604
  46. #define PPE_CFG_STS_MODE 0x700
  47. #define PPE_HIS_RX_PKT_CNT 0x804
  48. /* REG_INTERRUPT */
  49. #define RCV_INT BIT(10)
  50. #define RCV_NOBUF BIT(8)
  51. #define RCV_DROP BIT(7)
  52. #define TX_DROP BIT(6)
  53. #define DEF_INT_ERR (RCV_NOBUF | RCV_DROP | TX_DROP)
  54. #define DEF_INT_MASK (RCV_INT | DEF_INT_ERR)
  55. /* TX descriptor config */
  56. #define TX_FREE_MEM BIT(0)
  57. #define TX_READ_ALLOC_L3 BIT(1)
  58. #define TX_FINISH_CACHE_INV BIT(2)
  59. #define TX_CLEAR_WB BIT(4)
  60. #define TX_L3_CHECKSUM BIT(5)
  61. #define TX_LOOP_BACK BIT(11)
  62. /* RX error */
  63. #define RX_PKT_DROP BIT(0)
  64. #define RX_L2_ERR BIT(1)
  65. #define RX_PKT_ERR (RX_PKT_DROP | RX_L2_ERR)
  66. #define SGMII_SPEED_1000 0x08
  67. #define SGMII_SPEED_100 0x07
  68. #define SGMII_SPEED_10 0x06
  69. #define MII_SPEED_100 0x01
  70. #define MII_SPEED_10 0x00
  71. #define GE_DUPLEX_FULL BIT(0)
  72. #define GE_DUPLEX_HALF 0x00
  73. #define GE_MODE_CHANGE_EN BIT(0)
  74. #define GE_TX_AUTO_NEG BIT(5)
  75. #define GE_TX_ADD_CRC BIT(6)
  76. #define GE_TX_SHORT_PAD_THROUGH BIT(7)
  77. #define GE_RX_STRIP_CRC BIT(0)
  78. #define GE_RX_STRIP_PAD BIT(3)
  79. #define GE_RX_PAD_EN BIT(4)
  80. #define GE_AUTO_NEG_CTL BIT(0)
  81. #define GE_RX_INT_THRESHOLD BIT(6)
  82. #define GE_RX_TIMEOUT 0x04
  83. #define GE_RX_PORT_EN BIT(1)
  84. #define GE_TX_PORT_EN BIT(2)
  85. #define PPE_CFG_STS_RX_PKT_CNT_RC BIT(12)
  86. #define PPE_CFG_RX_PKT_ALIGN BIT(18)
  87. #define PPE_CFG_QOS_VMID_MODE BIT(14)
  88. #define PPE_CFG_QOS_VMID_GRP_SHIFT 8
  89. #define PPE_CFG_RX_FIFO_FSFU BIT(11)
  90. #define PPE_CFG_RX_DEPTH_SHIFT 16
  91. #define PPE_CFG_RX_START_SHIFT 0
  92. #define PPE_CFG_RX_CTRL_ALIGN_SHIFT 11
  93. #define PPE_CFG_BUS_LOCAL_REL BIT(14)
  94. #define PPE_CFG_BUS_BIG_ENDIEN BIT(0)
  95. #define RX_DESC_NUM 128
  96. #define TX_DESC_NUM 256
  97. #define TX_NEXT(N) (((N) + 1) & (TX_DESC_NUM-1))
  98. #define RX_NEXT(N) (((N) + 1) & (RX_DESC_NUM-1))
  99. #define GMAC_PPE_RX_PKT_MAX_LEN 379
  100. #define GMAC_MAX_PKT_LEN 1516
  101. #define GMAC_MIN_PKT_LEN 31
  102. #define RX_BUF_SIZE 1600
  103. #define RESET_TIMEOUT 1000
  104. #define TX_TIMEOUT (6 * HZ)
  105. #define DRV_NAME "hip04-ether"
  106. #define DRV_VERSION "v1.0"
  107. #define HIP04_MAX_TX_COALESCE_USECS 200
  108. #define HIP04_MIN_TX_COALESCE_USECS 100
  109. #define HIP04_MAX_TX_COALESCE_FRAMES 200
  110. #define HIP04_MIN_TX_COALESCE_FRAMES 100
  111. struct tx_desc {
  112. u32 send_addr;
  113. u32 send_size;
  114. u32 next_addr;
  115. u32 cfg;
  116. u32 wb_addr;
  117. } __aligned(64);
  118. struct rx_desc {
  119. u16 reserved_16;
  120. u16 pkt_len;
  121. u32 reserve1[3];
  122. u32 pkt_err;
  123. u32 reserve2[4];
  124. };
  125. struct hip04_priv {
  126. void __iomem *base;
  127. int phy_mode;
  128. int chan;
  129. unsigned int port;
  130. unsigned int speed;
  131. unsigned int duplex;
  132. unsigned int reg_inten;
  133. struct napi_struct napi;
  134. struct net_device *ndev;
  135. struct tx_desc *tx_desc;
  136. dma_addr_t tx_desc_dma;
  137. struct sk_buff *tx_skb[TX_DESC_NUM];
  138. dma_addr_t tx_phys[TX_DESC_NUM];
  139. unsigned int tx_head;
  140. int tx_coalesce_frames;
  141. int tx_coalesce_usecs;
  142. struct hrtimer tx_coalesce_timer;
  143. unsigned char *rx_buf[RX_DESC_NUM];
  144. dma_addr_t rx_phys[RX_DESC_NUM];
  145. unsigned int rx_head;
  146. unsigned int rx_buf_size;
  147. struct device_node *phy_node;
  148. struct phy_device *phy;
  149. struct regmap *map;
  150. struct work_struct tx_timeout_task;
  151. /* written only by tx cleanup */
  152. unsigned int tx_tail ____cacheline_aligned_in_smp;
  153. };
  154. static inline unsigned int tx_count(unsigned int head, unsigned int tail)
  155. {
  156. return (head - tail) % (TX_DESC_NUM - 1);
  157. }
  158. static void hip04_config_port(struct net_device *ndev, u32 speed, u32 duplex)
  159. {
  160. struct hip04_priv *priv = netdev_priv(ndev);
  161. u32 val;
  162. priv->speed = speed;
  163. priv->duplex = duplex;
  164. switch (priv->phy_mode) {
  165. case PHY_INTERFACE_MODE_SGMII:
  166. if (speed == SPEED_1000)
  167. val = SGMII_SPEED_1000;
  168. else if (speed == SPEED_100)
  169. val = SGMII_SPEED_100;
  170. else
  171. val = SGMII_SPEED_10;
  172. break;
  173. case PHY_INTERFACE_MODE_MII:
  174. if (speed == SPEED_100)
  175. val = MII_SPEED_100;
  176. else
  177. val = MII_SPEED_10;
  178. break;
  179. default:
  180. netdev_warn(ndev, "not supported mode\n");
  181. val = MII_SPEED_10;
  182. break;
  183. }
  184. writel_relaxed(val, priv->base + GE_PORT_MODE);
  185. val = duplex ? GE_DUPLEX_FULL : GE_DUPLEX_HALF;
  186. writel_relaxed(val, priv->base + GE_DUPLEX_TYPE);
  187. val = GE_MODE_CHANGE_EN;
  188. writel_relaxed(val, priv->base + GE_MODE_CHANGE_REG);
  189. }
  190. static void hip04_reset_ppe(struct hip04_priv *priv)
  191. {
  192. u32 val, tmp, timeout = 0;
  193. do {
  194. regmap_read(priv->map, priv->port * 4 + PPE_CURR_BUF_CNT, &val);
  195. regmap_read(priv->map, priv->port * 4 + PPE_CFG_RX_ADDR, &tmp);
  196. if (timeout++ > RESET_TIMEOUT)
  197. break;
  198. } while (val & 0xfff);
  199. }
  200. static void hip04_config_fifo(struct hip04_priv *priv)
  201. {
  202. u32 val;
  203. val = readl_relaxed(priv->base + PPE_CFG_STS_MODE);
  204. val |= PPE_CFG_STS_RX_PKT_CNT_RC;
  205. writel_relaxed(val, priv->base + PPE_CFG_STS_MODE);
  206. val = BIT(priv->port);
  207. regmap_write(priv->map, priv->port * 4 + PPE_CFG_POOL_GRP, val);
  208. val = priv->port << PPE_CFG_QOS_VMID_GRP_SHIFT;
  209. val |= PPE_CFG_QOS_VMID_MODE;
  210. writel_relaxed(val, priv->base + PPE_CFG_QOS_VMID_GEN);
  211. val = RX_BUF_SIZE;
  212. regmap_write(priv->map, priv->port * 4 + PPE_CFG_RX_BUF_SIZE, val);
  213. val = RX_DESC_NUM << PPE_CFG_RX_DEPTH_SHIFT;
  214. val |= PPE_CFG_RX_FIFO_FSFU;
  215. val |= priv->chan << PPE_CFG_RX_START_SHIFT;
  216. regmap_write(priv->map, priv->port * 4 + PPE_CFG_RX_FIFO_SIZE, val);
  217. val = NET_IP_ALIGN << PPE_CFG_RX_CTRL_ALIGN_SHIFT;
  218. writel_relaxed(val, priv->base + PPE_CFG_RX_CTRL_REG);
  219. val = PPE_CFG_RX_PKT_ALIGN;
  220. writel_relaxed(val, priv->base + PPE_CFG_RX_PKT_MODE_REG);
  221. val = PPE_CFG_BUS_LOCAL_REL | PPE_CFG_BUS_BIG_ENDIEN;
  222. writel_relaxed(val, priv->base + PPE_CFG_BUS_CTRL_REG);
  223. val = GMAC_PPE_RX_PKT_MAX_LEN;
  224. writel_relaxed(val, priv->base + PPE_CFG_MAX_FRAME_LEN_REG);
  225. val = GMAC_MAX_PKT_LEN;
  226. writel_relaxed(val, priv->base + GE_MAX_FRM_SIZE_REG);
  227. val = GMAC_MIN_PKT_LEN;
  228. writel_relaxed(val, priv->base + GE_SHORT_RUNTS_THR_REG);
  229. val = readl_relaxed(priv->base + GE_TRANSMIT_CONTROL_REG);
  230. val |= GE_TX_AUTO_NEG | GE_TX_ADD_CRC | GE_TX_SHORT_PAD_THROUGH;
  231. writel_relaxed(val, priv->base + GE_TRANSMIT_CONTROL_REG);
  232. val = GE_RX_STRIP_CRC;
  233. writel_relaxed(val, priv->base + GE_CF_CRC_STRIP_REG);
  234. val = readl_relaxed(priv->base + GE_RECV_CONTROL_REG);
  235. val |= GE_RX_STRIP_PAD | GE_RX_PAD_EN;
  236. writel_relaxed(val, priv->base + GE_RECV_CONTROL_REG);
  237. val = GE_AUTO_NEG_CTL;
  238. writel_relaxed(val, priv->base + GE_TX_LOCAL_PAGE_REG);
  239. }
  240. static void hip04_mac_enable(struct net_device *ndev)
  241. {
  242. struct hip04_priv *priv = netdev_priv(ndev);
  243. u32 val;
  244. /* enable tx & rx */
  245. val = readl_relaxed(priv->base + GE_PORT_EN);
  246. val |= GE_RX_PORT_EN | GE_TX_PORT_EN;
  247. writel_relaxed(val, priv->base + GE_PORT_EN);
  248. /* clear rx int */
  249. val = RCV_INT;
  250. writel_relaxed(val, priv->base + PPE_RINT);
  251. /* config recv int */
  252. val = GE_RX_INT_THRESHOLD | GE_RX_TIMEOUT;
  253. writel_relaxed(val, priv->base + PPE_CFG_RX_PKT_INT);
  254. /* enable interrupt */
  255. priv->reg_inten = DEF_INT_MASK;
  256. writel_relaxed(priv->reg_inten, priv->base + PPE_INTEN);
  257. }
  258. static void hip04_mac_disable(struct net_device *ndev)
  259. {
  260. struct hip04_priv *priv = netdev_priv(ndev);
  261. u32 val;
  262. /* disable int */
  263. priv->reg_inten &= ~(DEF_INT_MASK);
  264. writel_relaxed(priv->reg_inten, priv->base + PPE_INTEN);
  265. /* disable tx & rx */
  266. val = readl_relaxed(priv->base + GE_PORT_EN);
  267. val &= ~(GE_RX_PORT_EN | GE_TX_PORT_EN);
  268. writel_relaxed(val, priv->base + GE_PORT_EN);
  269. }
  270. static void hip04_set_xmit_desc(struct hip04_priv *priv, dma_addr_t phys)
  271. {
  272. writel(phys, priv->base + PPE_CFG_CPU_ADD_ADDR);
  273. }
  274. static void hip04_set_recv_desc(struct hip04_priv *priv, dma_addr_t phys)
  275. {
  276. regmap_write(priv->map, priv->port * 4 + PPE_CFG_RX_ADDR, phys);
  277. }
  278. static u32 hip04_recv_cnt(struct hip04_priv *priv)
  279. {
  280. return readl(priv->base + PPE_HIS_RX_PKT_CNT);
  281. }
  282. static void hip04_update_mac_address(struct net_device *ndev)
  283. {
  284. struct hip04_priv *priv = netdev_priv(ndev);
  285. writel_relaxed(((ndev->dev_addr[0] << 8) | (ndev->dev_addr[1])),
  286. priv->base + GE_STATION_MAC_ADDRESS);
  287. writel_relaxed(((ndev->dev_addr[2] << 24) | (ndev->dev_addr[3] << 16) |
  288. (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5])),
  289. priv->base + GE_STATION_MAC_ADDRESS + 4);
  290. }
  291. static int hip04_set_mac_address(struct net_device *ndev, void *addr)
  292. {
  293. eth_mac_addr(ndev, addr);
  294. hip04_update_mac_address(ndev);
  295. return 0;
  296. }
  297. static int hip04_tx_reclaim(struct net_device *ndev, bool force)
  298. {
  299. struct hip04_priv *priv = netdev_priv(ndev);
  300. unsigned tx_tail = priv->tx_tail;
  301. struct tx_desc *desc;
  302. unsigned int bytes_compl = 0, pkts_compl = 0;
  303. unsigned int count;
  304. smp_rmb();
  305. count = tx_count(ACCESS_ONCE(priv->tx_head), tx_tail);
  306. if (count == 0)
  307. goto out;
  308. while (count) {
  309. desc = &priv->tx_desc[tx_tail];
  310. if (desc->send_addr != 0) {
  311. if (force)
  312. desc->send_addr = 0;
  313. else
  314. break;
  315. }
  316. if (priv->tx_phys[tx_tail]) {
  317. dma_unmap_single(&ndev->dev, priv->tx_phys[tx_tail],
  318. priv->tx_skb[tx_tail]->len,
  319. DMA_TO_DEVICE);
  320. priv->tx_phys[tx_tail] = 0;
  321. }
  322. pkts_compl++;
  323. bytes_compl += priv->tx_skb[tx_tail]->len;
  324. dev_kfree_skb(priv->tx_skb[tx_tail]);
  325. priv->tx_skb[tx_tail] = NULL;
  326. tx_tail = TX_NEXT(tx_tail);
  327. count--;
  328. }
  329. priv->tx_tail = tx_tail;
  330. smp_wmb(); /* Ensure tx_tail visible to xmit */
  331. out:
  332. if (pkts_compl || bytes_compl)
  333. netdev_completed_queue(ndev, pkts_compl, bytes_compl);
  334. if (unlikely(netif_queue_stopped(ndev)) && (count < (TX_DESC_NUM - 1)))
  335. netif_wake_queue(ndev);
  336. return count;
  337. }
  338. static void hip04_start_tx_timer(struct hip04_priv *priv)
  339. {
  340. unsigned long ns = priv->tx_coalesce_usecs * NSEC_PER_USEC / 2;
  341. /* allow timer to fire after half the time at the earliest */
  342. hrtimer_start_range_ns(&priv->tx_coalesce_timer, ns_to_ktime(ns),
  343. ns, HRTIMER_MODE_REL);
  344. }
  345. static int hip04_mac_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  346. {
  347. struct hip04_priv *priv = netdev_priv(ndev);
  348. struct net_device_stats *stats = &ndev->stats;
  349. unsigned int tx_head = priv->tx_head, count;
  350. struct tx_desc *desc = &priv->tx_desc[tx_head];
  351. dma_addr_t phys;
  352. smp_rmb();
  353. count = tx_count(tx_head, ACCESS_ONCE(priv->tx_tail));
  354. if (count == (TX_DESC_NUM - 1)) {
  355. netif_stop_queue(ndev);
  356. return NETDEV_TX_BUSY;
  357. }
  358. phys = dma_map_single(&ndev->dev, skb->data, skb->len, DMA_TO_DEVICE);
  359. if (dma_mapping_error(&ndev->dev, phys)) {
  360. dev_kfree_skb(skb);
  361. return NETDEV_TX_OK;
  362. }
  363. priv->tx_skb[tx_head] = skb;
  364. priv->tx_phys[tx_head] = phys;
  365. desc->send_addr = cpu_to_be32(phys);
  366. desc->send_size = cpu_to_be32(skb->len);
  367. desc->cfg = cpu_to_be32(TX_CLEAR_WB | TX_FINISH_CACHE_INV);
  368. phys = priv->tx_desc_dma + tx_head * sizeof(struct tx_desc);
  369. desc->wb_addr = cpu_to_be32(phys);
  370. skb_tx_timestamp(skb);
  371. hip04_set_xmit_desc(priv, phys);
  372. priv->tx_head = TX_NEXT(tx_head);
  373. count++;
  374. netdev_sent_queue(ndev, skb->len);
  375. stats->tx_bytes += skb->len;
  376. stats->tx_packets++;
  377. /* Ensure tx_head update visible to tx reclaim */
  378. smp_wmb();
  379. /* queue is getting full, better start cleaning up now */
  380. if (count >= priv->tx_coalesce_frames) {
  381. if (napi_schedule_prep(&priv->napi)) {
  382. /* disable rx interrupt and timer */
  383. priv->reg_inten &= ~(RCV_INT);
  384. writel_relaxed(DEF_INT_MASK & ~RCV_INT,
  385. priv->base + PPE_INTEN);
  386. hrtimer_cancel(&priv->tx_coalesce_timer);
  387. __napi_schedule(&priv->napi);
  388. }
  389. } else if (!hrtimer_is_queued(&priv->tx_coalesce_timer)) {
  390. /* cleanup not pending yet, start a new timer */
  391. hip04_start_tx_timer(priv);
  392. }
  393. return NETDEV_TX_OK;
  394. }
  395. static int hip04_rx_poll(struct napi_struct *napi, int budget)
  396. {
  397. struct hip04_priv *priv = container_of(napi, struct hip04_priv, napi);
  398. struct net_device *ndev = priv->ndev;
  399. struct net_device_stats *stats = &ndev->stats;
  400. unsigned int cnt = hip04_recv_cnt(priv);
  401. struct rx_desc *desc;
  402. struct sk_buff *skb;
  403. unsigned char *buf;
  404. bool last = false;
  405. dma_addr_t phys;
  406. int rx = 0;
  407. int tx_remaining;
  408. u16 len;
  409. u32 err;
  410. while (cnt && !last) {
  411. buf = priv->rx_buf[priv->rx_head];
  412. skb = build_skb(buf, priv->rx_buf_size);
  413. if (unlikely(!skb)) {
  414. net_dbg_ratelimited("build_skb failed\n");
  415. goto refill;
  416. }
  417. dma_unmap_single(&ndev->dev, priv->rx_phys[priv->rx_head],
  418. RX_BUF_SIZE, DMA_FROM_DEVICE);
  419. priv->rx_phys[priv->rx_head] = 0;
  420. desc = (struct rx_desc *)skb->data;
  421. len = be16_to_cpu(desc->pkt_len);
  422. err = be32_to_cpu(desc->pkt_err);
  423. if (0 == len) {
  424. dev_kfree_skb_any(skb);
  425. last = true;
  426. } else if ((err & RX_PKT_ERR) || (len >= GMAC_MAX_PKT_LEN)) {
  427. dev_kfree_skb_any(skb);
  428. stats->rx_dropped++;
  429. stats->rx_errors++;
  430. } else {
  431. skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
  432. skb_put(skb, len);
  433. skb->protocol = eth_type_trans(skb, ndev);
  434. napi_gro_receive(&priv->napi, skb);
  435. stats->rx_packets++;
  436. stats->rx_bytes += len;
  437. rx++;
  438. }
  439. refill:
  440. buf = netdev_alloc_frag(priv->rx_buf_size);
  441. if (!buf)
  442. goto done;
  443. phys = dma_map_single(&ndev->dev, buf,
  444. RX_BUF_SIZE, DMA_FROM_DEVICE);
  445. if (dma_mapping_error(&ndev->dev, phys))
  446. goto done;
  447. priv->rx_buf[priv->rx_head] = buf;
  448. priv->rx_phys[priv->rx_head] = phys;
  449. hip04_set_recv_desc(priv, phys);
  450. priv->rx_head = RX_NEXT(priv->rx_head);
  451. if (rx >= budget)
  452. goto done;
  453. if (--cnt == 0)
  454. cnt = hip04_recv_cnt(priv);
  455. }
  456. if (!(priv->reg_inten & RCV_INT)) {
  457. /* enable rx interrupt */
  458. priv->reg_inten |= RCV_INT;
  459. writel_relaxed(priv->reg_inten, priv->base + PPE_INTEN);
  460. }
  461. napi_complete(napi);
  462. done:
  463. /* clean up tx descriptors and start a new timer if necessary */
  464. tx_remaining = hip04_tx_reclaim(ndev, false);
  465. if (rx < budget && tx_remaining)
  466. hip04_start_tx_timer(priv);
  467. return rx;
  468. }
  469. static irqreturn_t hip04_mac_interrupt(int irq, void *dev_id)
  470. {
  471. struct net_device *ndev = (struct net_device *)dev_id;
  472. struct hip04_priv *priv = netdev_priv(ndev);
  473. struct net_device_stats *stats = &ndev->stats;
  474. u32 ists = readl_relaxed(priv->base + PPE_INTSTS);
  475. if (!ists)
  476. return IRQ_NONE;
  477. writel_relaxed(DEF_INT_MASK, priv->base + PPE_RINT);
  478. if (unlikely(ists & DEF_INT_ERR)) {
  479. if (ists & (RCV_NOBUF | RCV_DROP)) {
  480. stats->rx_errors++;
  481. stats->rx_dropped++;
  482. netdev_err(ndev, "rx drop\n");
  483. }
  484. if (ists & TX_DROP) {
  485. stats->tx_dropped++;
  486. netdev_err(ndev, "tx drop\n");
  487. }
  488. }
  489. if (ists & RCV_INT && napi_schedule_prep(&priv->napi)) {
  490. /* disable rx interrupt */
  491. priv->reg_inten &= ~(RCV_INT);
  492. writel_relaxed(DEF_INT_MASK & ~RCV_INT, priv->base + PPE_INTEN);
  493. hrtimer_cancel(&priv->tx_coalesce_timer);
  494. __napi_schedule(&priv->napi);
  495. }
  496. return IRQ_HANDLED;
  497. }
  498. static enum hrtimer_restart tx_done(struct hrtimer *hrtimer)
  499. {
  500. struct hip04_priv *priv;
  501. priv = container_of(hrtimer, struct hip04_priv, tx_coalesce_timer);
  502. if (napi_schedule_prep(&priv->napi)) {
  503. /* disable rx interrupt */
  504. priv->reg_inten &= ~(RCV_INT);
  505. writel_relaxed(DEF_INT_MASK & ~RCV_INT, priv->base + PPE_INTEN);
  506. __napi_schedule(&priv->napi);
  507. }
  508. return HRTIMER_NORESTART;
  509. }
  510. static void hip04_adjust_link(struct net_device *ndev)
  511. {
  512. struct hip04_priv *priv = netdev_priv(ndev);
  513. struct phy_device *phy = priv->phy;
  514. if ((priv->speed != phy->speed) || (priv->duplex != phy->duplex)) {
  515. hip04_config_port(ndev, phy->speed, phy->duplex);
  516. phy_print_status(phy);
  517. }
  518. }
  519. static int hip04_mac_open(struct net_device *ndev)
  520. {
  521. struct hip04_priv *priv = netdev_priv(ndev);
  522. int i;
  523. priv->rx_head = 0;
  524. priv->tx_head = 0;
  525. priv->tx_tail = 0;
  526. hip04_reset_ppe(priv);
  527. for (i = 0; i < RX_DESC_NUM; i++) {
  528. dma_addr_t phys;
  529. phys = dma_map_single(&ndev->dev, priv->rx_buf[i],
  530. RX_BUF_SIZE, DMA_FROM_DEVICE);
  531. if (dma_mapping_error(&ndev->dev, phys))
  532. return -EIO;
  533. priv->rx_phys[i] = phys;
  534. hip04_set_recv_desc(priv, phys);
  535. }
  536. if (priv->phy)
  537. phy_start(priv->phy);
  538. netdev_reset_queue(ndev);
  539. netif_start_queue(ndev);
  540. hip04_mac_enable(ndev);
  541. napi_enable(&priv->napi);
  542. return 0;
  543. }
  544. static int hip04_mac_stop(struct net_device *ndev)
  545. {
  546. struct hip04_priv *priv = netdev_priv(ndev);
  547. int i;
  548. napi_disable(&priv->napi);
  549. netif_stop_queue(ndev);
  550. hip04_mac_disable(ndev);
  551. hip04_tx_reclaim(ndev, true);
  552. hip04_reset_ppe(priv);
  553. if (priv->phy)
  554. phy_stop(priv->phy);
  555. for (i = 0; i < RX_DESC_NUM; i++) {
  556. if (priv->rx_phys[i]) {
  557. dma_unmap_single(&ndev->dev, priv->rx_phys[i],
  558. RX_BUF_SIZE, DMA_FROM_DEVICE);
  559. priv->rx_phys[i] = 0;
  560. }
  561. }
  562. return 0;
  563. }
  564. static void hip04_timeout(struct net_device *ndev)
  565. {
  566. struct hip04_priv *priv = netdev_priv(ndev);
  567. schedule_work(&priv->tx_timeout_task);
  568. }
  569. static void hip04_tx_timeout_task(struct work_struct *work)
  570. {
  571. struct hip04_priv *priv;
  572. priv = container_of(work, struct hip04_priv, tx_timeout_task);
  573. hip04_mac_stop(priv->ndev);
  574. hip04_mac_open(priv->ndev);
  575. }
  576. static struct net_device_stats *hip04_get_stats(struct net_device *ndev)
  577. {
  578. return &ndev->stats;
  579. }
  580. static int hip04_get_coalesce(struct net_device *netdev,
  581. struct ethtool_coalesce *ec)
  582. {
  583. struct hip04_priv *priv = netdev_priv(netdev);
  584. ec->tx_coalesce_usecs = priv->tx_coalesce_usecs;
  585. ec->tx_max_coalesced_frames = priv->tx_coalesce_frames;
  586. return 0;
  587. }
  588. static int hip04_set_coalesce(struct net_device *netdev,
  589. struct ethtool_coalesce *ec)
  590. {
  591. struct hip04_priv *priv = netdev_priv(netdev);
  592. /* Check not supported parameters */
  593. if ((ec->rx_max_coalesced_frames) || (ec->rx_coalesce_usecs_irq) ||
  594. (ec->rx_max_coalesced_frames_irq) || (ec->tx_coalesce_usecs_irq) ||
  595. (ec->use_adaptive_rx_coalesce) || (ec->use_adaptive_tx_coalesce) ||
  596. (ec->pkt_rate_low) || (ec->rx_coalesce_usecs_low) ||
  597. (ec->rx_max_coalesced_frames_low) || (ec->tx_coalesce_usecs_high) ||
  598. (ec->tx_max_coalesced_frames_low) || (ec->pkt_rate_high) ||
  599. (ec->tx_coalesce_usecs_low) || (ec->rx_coalesce_usecs_high) ||
  600. (ec->rx_max_coalesced_frames_high) || (ec->rx_coalesce_usecs) ||
  601. (ec->tx_max_coalesced_frames_irq) ||
  602. (ec->stats_block_coalesce_usecs) ||
  603. (ec->tx_max_coalesced_frames_high) || (ec->rate_sample_interval))
  604. return -EOPNOTSUPP;
  605. if ((ec->tx_coalesce_usecs > HIP04_MAX_TX_COALESCE_USECS ||
  606. ec->tx_coalesce_usecs < HIP04_MIN_TX_COALESCE_USECS) ||
  607. (ec->tx_max_coalesced_frames > HIP04_MAX_TX_COALESCE_FRAMES ||
  608. ec->tx_max_coalesced_frames < HIP04_MIN_TX_COALESCE_FRAMES))
  609. return -EINVAL;
  610. priv->tx_coalesce_usecs = ec->tx_coalesce_usecs;
  611. priv->tx_coalesce_frames = ec->tx_max_coalesced_frames;
  612. return 0;
  613. }
  614. static void hip04_get_drvinfo(struct net_device *netdev,
  615. struct ethtool_drvinfo *drvinfo)
  616. {
  617. strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
  618. strlcpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version));
  619. }
  620. static const struct ethtool_ops hip04_ethtool_ops = {
  621. .get_coalesce = hip04_get_coalesce,
  622. .set_coalesce = hip04_set_coalesce,
  623. .get_drvinfo = hip04_get_drvinfo,
  624. };
  625. static const struct net_device_ops hip04_netdev_ops = {
  626. .ndo_open = hip04_mac_open,
  627. .ndo_stop = hip04_mac_stop,
  628. .ndo_get_stats = hip04_get_stats,
  629. .ndo_start_xmit = hip04_mac_start_xmit,
  630. .ndo_set_mac_address = hip04_set_mac_address,
  631. .ndo_tx_timeout = hip04_timeout,
  632. .ndo_validate_addr = eth_validate_addr,
  633. .ndo_change_mtu = eth_change_mtu,
  634. };
  635. static int hip04_alloc_ring(struct net_device *ndev, struct device *d)
  636. {
  637. struct hip04_priv *priv = netdev_priv(ndev);
  638. int i;
  639. priv->tx_desc = dma_alloc_coherent(d,
  640. TX_DESC_NUM * sizeof(struct tx_desc),
  641. &priv->tx_desc_dma, GFP_KERNEL);
  642. if (!priv->tx_desc)
  643. return -ENOMEM;
  644. priv->rx_buf_size = RX_BUF_SIZE +
  645. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  646. for (i = 0; i < RX_DESC_NUM; i++) {
  647. priv->rx_buf[i] = netdev_alloc_frag(priv->rx_buf_size);
  648. if (!priv->rx_buf[i])
  649. return -ENOMEM;
  650. }
  651. return 0;
  652. }
  653. static void hip04_free_ring(struct net_device *ndev, struct device *d)
  654. {
  655. struct hip04_priv *priv = netdev_priv(ndev);
  656. int i;
  657. for (i = 0; i < RX_DESC_NUM; i++)
  658. if (priv->rx_buf[i])
  659. skb_free_frag(priv->rx_buf[i]);
  660. for (i = 0; i < TX_DESC_NUM; i++)
  661. if (priv->tx_skb[i])
  662. dev_kfree_skb_any(priv->tx_skb[i]);
  663. dma_free_coherent(d, TX_DESC_NUM * sizeof(struct tx_desc),
  664. priv->tx_desc, priv->tx_desc_dma);
  665. }
  666. static int hip04_mac_probe(struct platform_device *pdev)
  667. {
  668. struct device *d = &pdev->dev;
  669. struct device_node *node = d->of_node;
  670. struct of_phandle_args arg;
  671. struct net_device *ndev;
  672. struct hip04_priv *priv;
  673. struct resource *res;
  674. int irq;
  675. int ret;
  676. ndev = alloc_etherdev(sizeof(struct hip04_priv));
  677. if (!ndev)
  678. return -ENOMEM;
  679. priv = netdev_priv(ndev);
  680. priv->ndev = ndev;
  681. platform_set_drvdata(pdev, ndev);
  682. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  683. priv->base = devm_ioremap_resource(d, res);
  684. if (IS_ERR(priv->base)) {
  685. ret = PTR_ERR(priv->base);
  686. goto init_fail;
  687. }
  688. ret = of_parse_phandle_with_fixed_args(node, "port-handle", 2, 0, &arg);
  689. if (ret < 0) {
  690. dev_warn(d, "no port-handle\n");
  691. goto init_fail;
  692. }
  693. priv->port = arg.args[0];
  694. priv->chan = arg.args[1] * RX_DESC_NUM;
  695. hrtimer_init(&priv->tx_coalesce_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  696. /* BQL will try to keep the TX queue as short as possible, but it can't
  697. * be faster than tx_coalesce_usecs, so we need a fast timeout here,
  698. * but also long enough to gather up enough frames to ensure we don't
  699. * get more interrupts than necessary.
  700. * 200us is enough for 16 frames of 1500 bytes at gigabit ethernet rate
  701. */
  702. priv->tx_coalesce_frames = TX_DESC_NUM * 3 / 4;
  703. priv->tx_coalesce_usecs = 200;
  704. priv->tx_coalesce_timer.function = tx_done;
  705. priv->map = syscon_node_to_regmap(arg.np);
  706. if (IS_ERR(priv->map)) {
  707. dev_warn(d, "no syscon hisilicon,hip04-ppe\n");
  708. ret = PTR_ERR(priv->map);
  709. goto init_fail;
  710. }
  711. priv->phy_mode = of_get_phy_mode(node);
  712. if (priv->phy_mode < 0) {
  713. dev_warn(d, "not find phy-mode\n");
  714. ret = -EINVAL;
  715. goto init_fail;
  716. }
  717. irq = platform_get_irq(pdev, 0);
  718. if (irq <= 0) {
  719. ret = -EINVAL;
  720. goto init_fail;
  721. }
  722. ret = devm_request_irq(d, irq, hip04_mac_interrupt,
  723. 0, pdev->name, ndev);
  724. if (ret) {
  725. netdev_err(ndev, "devm_request_irq failed\n");
  726. goto init_fail;
  727. }
  728. priv->phy_node = of_parse_phandle(node, "phy-handle", 0);
  729. if (priv->phy_node) {
  730. priv->phy = of_phy_connect(ndev, priv->phy_node,
  731. &hip04_adjust_link,
  732. 0, priv->phy_mode);
  733. if (!priv->phy) {
  734. ret = -EPROBE_DEFER;
  735. goto init_fail;
  736. }
  737. }
  738. INIT_WORK(&priv->tx_timeout_task, hip04_tx_timeout_task);
  739. ether_setup(ndev);
  740. ndev->netdev_ops = &hip04_netdev_ops;
  741. ndev->ethtool_ops = &hip04_ethtool_ops;
  742. ndev->watchdog_timeo = TX_TIMEOUT;
  743. ndev->priv_flags |= IFF_UNICAST_FLT;
  744. ndev->irq = irq;
  745. netif_napi_add(ndev, &priv->napi, hip04_rx_poll, NAPI_POLL_WEIGHT);
  746. SET_NETDEV_DEV(ndev, &pdev->dev);
  747. hip04_reset_ppe(priv);
  748. if (priv->phy_mode == PHY_INTERFACE_MODE_MII)
  749. hip04_config_port(ndev, SPEED_100, DUPLEX_FULL);
  750. hip04_config_fifo(priv);
  751. random_ether_addr(ndev->dev_addr);
  752. hip04_update_mac_address(ndev);
  753. ret = hip04_alloc_ring(ndev, d);
  754. if (ret) {
  755. netdev_err(ndev, "alloc ring fail\n");
  756. goto alloc_fail;
  757. }
  758. ret = register_netdev(ndev);
  759. if (ret) {
  760. free_netdev(ndev);
  761. goto alloc_fail;
  762. }
  763. return 0;
  764. alloc_fail:
  765. hip04_free_ring(ndev, d);
  766. init_fail:
  767. of_node_put(priv->phy_node);
  768. free_netdev(ndev);
  769. return ret;
  770. }
  771. static int hip04_remove(struct platform_device *pdev)
  772. {
  773. struct net_device *ndev = platform_get_drvdata(pdev);
  774. struct hip04_priv *priv = netdev_priv(ndev);
  775. struct device *d = &pdev->dev;
  776. if (priv->phy)
  777. phy_disconnect(priv->phy);
  778. hip04_free_ring(ndev, d);
  779. unregister_netdev(ndev);
  780. free_irq(ndev->irq, ndev);
  781. of_node_put(priv->phy_node);
  782. cancel_work_sync(&priv->tx_timeout_task);
  783. free_netdev(ndev);
  784. return 0;
  785. }
  786. static const struct of_device_id hip04_mac_match[] = {
  787. { .compatible = "hisilicon,hip04-mac" },
  788. { }
  789. };
  790. MODULE_DEVICE_TABLE(of, hip04_mac_match);
  791. static struct platform_driver hip04_mac_driver = {
  792. .probe = hip04_mac_probe,
  793. .remove = hip04_remove,
  794. .driver = {
  795. .name = DRV_NAME,
  796. .of_match_table = hip04_mac_match,
  797. },
  798. };
  799. module_platform_driver(hip04_mac_driver);
  800. MODULE_DESCRIPTION("HISILICON P04 Ethernet driver");
  801. MODULE_LICENSE("GPL");