hip04_eth.c 25 KB

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