hip04_eth.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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 int hip04_mac_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  339. {
  340. struct hip04_priv *priv = netdev_priv(ndev);
  341. struct net_device_stats *stats = &ndev->stats;
  342. unsigned int tx_head = priv->tx_head, count;
  343. struct tx_desc *desc = &priv->tx_desc[tx_head];
  344. dma_addr_t phys;
  345. smp_rmb();
  346. count = tx_count(tx_head, ACCESS_ONCE(priv->tx_tail));
  347. if (count == (TX_DESC_NUM - 1)) {
  348. netif_stop_queue(ndev);
  349. return NETDEV_TX_BUSY;
  350. }
  351. phys = dma_map_single(&ndev->dev, skb->data, skb->len, DMA_TO_DEVICE);
  352. if (dma_mapping_error(&ndev->dev, phys)) {
  353. dev_kfree_skb(skb);
  354. return NETDEV_TX_OK;
  355. }
  356. priv->tx_skb[tx_head] = skb;
  357. priv->tx_phys[tx_head] = phys;
  358. desc->send_addr = cpu_to_be32(phys);
  359. desc->send_size = cpu_to_be32(skb->len);
  360. desc->cfg = cpu_to_be32(TX_CLEAR_WB | TX_FINISH_CACHE_INV);
  361. phys = priv->tx_desc_dma + tx_head * sizeof(struct tx_desc);
  362. desc->wb_addr = cpu_to_be32(phys);
  363. skb_tx_timestamp(skb);
  364. hip04_set_xmit_desc(priv, phys);
  365. priv->tx_head = TX_NEXT(tx_head);
  366. count++;
  367. netdev_sent_queue(ndev, skb->len);
  368. stats->tx_bytes += skb->len;
  369. stats->tx_packets++;
  370. /* Ensure tx_head update visible to tx reclaim */
  371. smp_wmb();
  372. /* queue is getting full, better start cleaning up now */
  373. if (count >= priv->tx_coalesce_frames) {
  374. if (napi_schedule_prep(&priv->napi)) {
  375. /* disable rx interrupt and timer */
  376. priv->reg_inten &= ~(RCV_INT);
  377. writel_relaxed(DEF_INT_MASK & ~RCV_INT,
  378. priv->base + PPE_INTEN);
  379. hrtimer_cancel(&priv->tx_coalesce_timer);
  380. __napi_schedule(&priv->napi);
  381. }
  382. } else if (!hrtimer_is_queued(&priv->tx_coalesce_timer)) {
  383. /* cleanup not pending yet, start a new timer */
  384. hrtimer_start_expires(&priv->tx_coalesce_timer,
  385. HRTIMER_MODE_REL);
  386. }
  387. return NETDEV_TX_OK;
  388. }
  389. static int hip04_rx_poll(struct napi_struct *napi, int budget)
  390. {
  391. struct hip04_priv *priv = container_of(napi, struct hip04_priv, napi);
  392. struct net_device *ndev = priv->ndev;
  393. struct net_device_stats *stats = &ndev->stats;
  394. unsigned int cnt = hip04_recv_cnt(priv);
  395. struct rx_desc *desc;
  396. struct sk_buff *skb;
  397. unsigned char *buf;
  398. bool last = false;
  399. dma_addr_t phys;
  400. int rx = 0;
  401. int tx_remaining;
  402. u16 len;
  403. u32 err;
  404. while (cnt && !last) {
  405. buf = priv->rx_buf[priv->rx_head];
  406. skb = build_skb(buf, priv->rx_buf_size);
  407. if (unlikely(!skb))
  408. net_dbg_ratelimited("build_skb failed\n");
  409. dma_unmap_single(&ndev->dev, priv->rx_phys[priv->rx_head],
  410. RX_BUF_SIZE, DMA_FROM_DEVICE);
  411. priv->rx_phys[priv->rx_head] = 0;
  412. desc = (struct rx_desc *)skb->data;
  413. len = be16_to_cpu(desc->pkt_len);
  414. err = be32_to_cpu(desc->pkt_err);
  415. if (0 == len) {
  416. dev_kfree_skb_any(skb);
  417. last = true;
  418. } else if ((err & RX_PKT_ERR) || (len >= GMAC_MAX_PKT_LEN)) {
  419. dev_kfree_skb_any(skb);
  420. stats->rx_dropped++;
  421. stats->rx_errors++;
  422. } else {
  423. skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
  424. skb_put(skb, len);
  425. skb->protocol = eth_type_trans(skb, ndev);
  426. napi_gro_receive(&priv->napi, skb);
  427. stats->rx_packets++;
  428. stats->rx_bytes += len;
  429. rx++;
  430. }
  431. buf = netdev_alloc_frag(priv->rx_buf_size);
  432. if (!buf)
  433. goto done;
  434. phys = dma_map_single(&ndev->dev, buf,
  435. RX_BUF_SIZE, DMA_FROM_DEVICE);
  436. if (dma_mapping_error(&ndev->dev, phys))
  437. goto done;
  438. priv->rx_buf[priv->rx_head] = buf;
  439. priv->rx_phys[priv->rx_head] = phys;
  440. hip04_set_recv_desc(priv, phys);
  441. priv->rx_head = RX_NEXT(priv->rx_head);
  442. if (rx >= budget)
  443. goto done;
  444. if (--cnt == 0)
  445. cnt = hip04_recv_cnt(priv);
  446. }
  447. if (!(priv->reg_inten & RCV_INT)) {
  448. /* enable rx interrupt */
  449. priv->reg_inten |= RCV_INT;
  450. writel_relaxed(priv->reg_inten, priv->base + PPE_INTEN);
  451. }
  452. napi_complete(napi);
  453. done:
  454. /* clean up tx descriptors and start a new timer if necessary */
  455. tx_remaining = hip04_tx_reclaim(ndev, false);
  456. if (rx < budget && tx_remaining)
  457. hrtimer_start_expires(&priv->tx_coalesce_timer, HRTIMER_MODE_REL);
  458. return rx;
  459. }
  460. static irqreturn_t hip04_mac_interrupt(int irq, void *dev_id)
  461. {
  462. struct net_device *ndev = (struct net_device *)dev_id;
  463. struct hip04_priv *priv = netdev_priv(ndev);
  464. struct net_device_stats *stats = &ndev->stats;
  465. u32 ists = readl_relaxed(priv->base + PPE_INTSTS);
  466. if (!ists)
  467. return IRQ_NONE;
  468. writel_relaxed(DEF_INT_MASK, priv->base + PPE_RINT);
  469. if (unlikely(ists & DEF_INT_ERR)) {
  470. if (ists & (RCV_NOBUF | RCV_DROP)) {
  471. stats->rx_errors++;
  472. stats->rx_dropped++;
  473. netdev_err(ndev, "rx drop\n");
  474. }
  475. if (ists & TX_DROP) {
  476. stats->tx_dropped++;
  477. netdev_err(ndev, "tx drop\n");
  478. }
  479. }
  480. if (ists & RCV_INT && napi_schedule_prep(&priv->napi)) {
  481. /* disable rx interrupt */
  482. priv->reg_inten &= ~(RCV_INT);
  483. writel_relaxed(DEF_INT_MASK & ~RCV_INT, priv->base + PPE_INTEN);
  484. hrtimer_cancel(&priv->tx_coalesce_timer);
  485. __napi_schedule(&priv->napi);
  486. }
  487. return IRQ_HANDLED;
  488. }
  489. enum hrtimer_restart tx_done(struct hrtimer *hrtimer)
  490. {
  491. struct hip04_priv *priv;
  492. priv = container_of(hrtimer, struct hip04_priv, tx_coalesce_timer);
  493. if (napi_schedule_prep(&priv->napi)) {
  494. /* disable rx interrupt */
  495. priv->reg_inten &= ~(RCV_INT);
  496. writel_relaxed(DEF_INT_MASK & ~RCV_INT, priv->base + PPE_INTEN);
  497. __napi_schedule(&priv->napi);
  498. }
  499. return HRTIMER_NORESTART;
  500. }
  501. static void hip04_adjust_link(struct net_device *ndev)
  502. {
  503. struct hip04_priv *priv = netdev_priv(ndev);
  504. struct phy_device *phy = priv->phy;
  505. if ((priv->speed != phy->speed) || (priv->duplex != phy->duplex)) {
  506. hip04_config_port(ndev, phy->speed, phy->duplex);
  507. phy_print_status(phy);
  508. }
  509. }
  510. static int hip04_mac_open(struct net_device *ndev)
  511. {
  512. struct hip04_priv *priv = netdev_priv(ndev);
  513. int i;
  514. priv->rx_head = 0;
  515. priv->tx_head = 0;
  516. priv->tx_tail = 0;
  517. hip04_reset_ppe(priv);
  518. for (i = 0; i < RX_DESC_NUM; i++) {
  519. dma_addr_t phys;
  520. phys = dma_map_single(&ndev->dev, priv->rx_buf[i],
  521. RX_BUF_SIZE, DMA_FROM_DEVICE);
  522. if (dma_mapping_error(&ndev->dev, phys))
  523. return -EIO;
  524. priv->rx_phys[i] = phys;
  525. hip04_set_recv_desc(priv, phys);
  526. }
  527. if (priv->phy)
  528. phy_start(priv->phy);
  529. netdev_reset_queue(ndev);
  530. netif_start_queue(ndev);
  531. hip04_mac_enable(ndev);
  532. napi_enable(&priv->napi);
  533. return 0;
  534. }
  535. static int hip04_mac_stop(struct net_device *ndev)
  536. {
  537. struct hip04_priv *priv = netdev_priv(ndev);
  538. int i;
  539. napi_disable(&priv->napi);
  540. netif_stop_queue(ndev);
  541. hip04_mac_disable(ndev);
  542. hip04_tx_reclaim(ndev, true);
  543. hip04_reset_ppe(priv);
  544. if (priv->phy)
  545. phy_stop(priv->phy);
  546. for (i = 0; i < RX_DESC_NUM; i++) {
  547. if (priv->rx_phys[i]) {
  548. dma_unmap_single(&ndev->dev, priv->rx_phys[i],
  549. RX_BUF_SIZE, DMA_FROM_DEVICE);
  550. priv->rx_phys[i] = 0;
  551. }
  552. }
  553. return 0;
  554. }
  555. static void hip04_timeout(struct net_device *ndev)
  556. {
  557. struct hip04_priv *priv = netdev_priv(ndev);
  558. schedule_work(&priv->tx_timeout_task);
  559. }
  560. static void hip04_tx_timeout_task(struct work_struct *work)
  561. {
  562. struct hip04_priv *priv;
  563. priv = container_of(work, struct hip04_priv, tx_timeout_task);
  564. hip04_mac_stop(priv->ndev);
  565. hip04_mac_open(priv->ndev);
  566. }
  567. static struct net_device_stats *hip04_get_stats(struct net_device *ndev)
  568. {
  569. return &ndev->stats;
  570. }
  571. static int hip04_get_coalesce(struct net_device *netdev,
  572. struct ethtool_coalesce *ec)
  573. {
  574. struct hip04_priv *priv = netdev_priv(netdev);
  575. ec->tx_coalesce_usecs = priv->tx_coalesce_usecs;
  576. ec->tx_max_coalesced_frames = priv->tx_coalesce_frames;
  577. return 0;
  578. }
  579. static int hip04_set_coalesce(struct net_device *netdev,
  580. struct ethtool_coalesce *ec)
  581. {
  582. struct hip04_priv *priv = netdev_priv(netdev);
  583. /* Check not supported parameters */
  584. if ((ec->rx_max_coalesced_frames) || (ec->rx_coalesce_usecs_irq) ||
  585. (ec->rx_max_coalesced_frames_irq) || (ec->tx_coalesce_usecs_irq) ||
  586. (ec->use_adaptive_rx_coalesce) || (ec->use_adaptive_tx_coalesce) ||
  587. (ec->pkt_rate_low) || (ec->rx_coalesce_usecs_low) ||
  588. (ec->rx_max_coalesced_frames_low) || (ec->tx_coalesce_usecs_high) ||
  589. (ec->tx_max_coalesced_frames_low) || (ec->pkt_rate_high) ||
  590. (ec->tx_coalesce_usecs_low) || (ec->rx_coalesce_usecs_high) ||
  591. (ec->rx_max_coalesced_frames_high) || (ec->rx_coalesce_usecs) ||
  592. (ec->tx_max_coalesced_frames_irq) ||
  593. (ec->stats_block_coalesce_usecs) ||
  594. (ec->tx_max_coalesced_frames_high) || (ec->rate_sample_interval))
  595. return -EOPNOTSUPP;
  596. if ((ec->tx_coalesce_usecs > HIP04_MAX_TX_COALESCE_USECS ||
  597. ec->tx_coalesce_usecs < HIP04_MIN_TX_COALESCE_USECS) ||
  598. (ec->tx_max_coalesced_frames > HIP04_MAX_TX_COALESCE_FRAMES ||
  599. ec->tx_max_coalesced_frames < HIP04_MIN_TX_COALESCE_FRAMES))
  600. return -EINVAL;
  601. priv->tx_coalesce_usecs = ec->tx_coalesce_usecs;
  602. priv->tx_coalesce_frames = ec->tx_max_coalesced_frames;
  603. return 0;
  604. }
  605. static void hip04_get_drvinfo(struct net_device *netdev,
  606. struct ethtool_drvinfo *drvinfo)
  607. {
  608. strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
  609. strlcpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version));
  610. }
  611. static struct ethtool_ops hip04_ethtool_ops = {
  612. .get_coalesce = hip04_get_coalesce,
  613. .set_coalesce = hip04_set_coalesce,
  614. .get_drvinfo = hip04_get_drvinfo,
  615. };
  616. static struct net_device_ops hip04_netdev_ops = {
  617. .ndo_open = hip04_mac_open,
  618. .ndo_stop = hip04_mac_stop,
  619. .ndo_get_stats = hip04_get_stats,
  620. .ndo_start_xmit = hip04_mac_start_xmit,
  621. .ndo_set_mac_address = hip04_set_mac_address,
  622. .ndo_tx_timeout = hip04_timeout,
  623. .ndo_validate_addr = eth_validate_addr,
  624. .ndo_change_mtu = eth_change_mtu,
  625. };
  626. static int hip04_alloc_ring(struct net_device *ndev, struct device *d)
  627. {
  628. struct hip04_priv *priv = netdev_priv(ndev);
  629. int i;
  630. priv->tx_desc = dma_alloc_coherent(d,
  631. TX_DESC_NUM * sizeof(struct tx_desc),
  632. &priv->tx_desc_dma, GFP_KERNEL);
  633. if (!priv->tx_desc)
  634. return -ENOMEM;
  635. priv->rx_buf_size = RX_BUF_SIZE +
  636. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  637. for (i = 0; i < RX_DESC_NUM; i++) {
  638. priv->rx_buf[i] = netdev_alloc_frag(priv->rx_buf_size);
  639. if (!priv->rx_buf[i])
  640. return -ENOMEM;
  641. }
  642. return 0;
  643. }
  644. static void hip04_free_ring(struct net_device *ndev, struct device *d)
  645. {
  646. struct hip04_priv *priv = netdev_priv(ndev);
  647. int i;
  648. for (i = 0; i < RX_DESC_NUM; i++)
  649. if (priv->rx_buf[i])
  650. put_page(virt_to_head_page(priv->rx_buf[i]));
  651. for (i = 0; i < TX_DESC_NUM; i++)
  652. if (priv->tx_skb[i])
  653. dev_kfree_skb_any(priv->tx_skb[i]);
  654. dma_free_coherent(d, TX_DESC_NUM * sizeof(struct tx_desc),
  655. priv->tx_desc, priv->tx_desc_dma);
  656. }
  657. static int hip04_mac_probe(struct platform_device *pdev)
  658. {
  659. struct device *d = &pdev->dev;
  660. struct device_node *node = d->of_node;
  661. struct of_phandle_args arg;
  662. struct net_device *ndev;
  663. struct hip04_priv *priv;
  664. struct resource *res;
  665. unsigned int irq;
  666. ktime_t txtime;
  667. int ret;
  668. ndev = alloc_etherdev(sizeof(struct hip04_priv));
  669. if (!ndev)
  670. return -ENOMEM;
  671. priv = netdev_priv(ndev);
  672. priv->ndev = ndev;
  673. platform_set_drvdata(pdev, ndev);
  674. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  675. priv->base = devm_ioremap_resource(d, res);
  676. if (IS_ERR(priv->base)) {
  677. ret = PTR_ERR(priv->base);
  678. goto init_fail;
  679. }
  680. ret = of_parse_phandle_with_fixed_args(node, "port-handle", 2, 0, &arg);
  681. if (ret < 0) {
  682. dev_warn(d, "no port-handle\n");
  683. goto init_fail;
  684. }
  685. priv->port = arg.args[0];
  686. priv->chan = arg.args[1] * RX_DESC_NUM;
  687. hrtimer_init(&priv->tx_coalesce_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  688. /* BQL will try to keep the TX queue as short as possible, but it can't
  689. * be faster than tx_coalesce_usecs, so we need a fast timeout here,
  690. * but also long enough to gather up enough frames to ensure we don't
  691. * get more interrupts than necessary.
  692. * 200us is enough for 16 frames of 1500 bytes at gigabit ethernet rate
  693. */
  694. priv->tx_coalesce_frames = TX_DESC_NUM * 3 / 4;
  695. priv->tx_coalesce_usecs = 200;
  696. /* allow timer to fire after half the time at the earliest */
  697. txtime = ktime_set(0, priv->tx_coalesce_usecs * NSEC_PER_USEC / 2);
  698. hrtimer_set_expires_range(&priv->tx_coalesce_timer, txtime, txtime);
  699. priv->tx_coalesce_timer.function = tx_done;
  700. priv->map = syscon_node_to_regmap(arg.np);
  701. if (IS_ERR(priv->map)) {
  702. dev_warn(d, "no syscon hisilicon,hip04-ppe\n");
  703. ret = PTR_ERR(priv->map);
  704. goto init_fail;
  705. }
  706. priv->phy_mode = of_get_phy_mode(node);
  707. if (priv->phy_mode < 0) {
  708. dev_warn(d, "not find phy-mode\n");
  709. ret = -EINVAL;
  710. goto init_fail;
  711. }
  712. irq = platform_get_irq(pdev, 0);
  713. if (irq <= 0) {
  714. ret = -EINVAL;
  715. goto init_fail;
  716. }
  717. ret = devm_request_irq(d, irq, hip04_mac_interrupt,
  718. 0, pdev->name, ndev);
  719. if (ret) {
  720. netdev_err(ndev, "devm_request_irq failed\n");
  721. goto init_fail;
  722. }
  723. priv->phy_node = of_parse_phandle(node, "phy-handle", 0);
  724. if (priv->phy_node) {
  725. priv->phy = of_phy_connect(ndev, priv->phy_node,
  726. &hip04_adjust_link,
  727. 0, priv->phy_mode);
  728. if (!priv->phy) {
  729. ret = -EPROBE_DEFER;
  730. goto init_fail;
  731. }
  732. }
  733. INIT_WORK(&priv->tx_timeout_task, hip04_tx_timeout_task);
  734. ether_setup(ndev);
  735. ndev->netdev_ops = &hip04_netdev_ops;
  736. ndev->ethtool_ops = &hip04_ethtool_ops;
  737. ndev->watchdog_timeo = TX_TIMEOUT;
  738. ndev->priv_flags |= IFF_UNICAST_FLT;
  739. ndev->irq = irq;
  740. netif_napi_add(ndev, &priv->napi, hip04_rx_poll, NAPI_POLL_WEIGHT);
  741. SET_NETDEV_DEV(ndev, &pdev->dev);
  742. hip04_reset_ppe(priv);
  743. if (priv->phy_mode == PHY_INTERFACE_MODE_MII)
  744. hip04_config_port(ndev, SPEED_100, DUPLEX_FULL);
  745. hip04_config_fifo(priv);
  746. random_ether_addr(ndev->dev_addr);
  747. hip04_update_mac_address(ndev);
  748. ret = hip04_alloc_ring(ndev, d);
  749. if (ret) {
  750. netdev_err(ndev, "alloc ring fail\n");
  751. goto alloc_fail;
  752. }
  753. ret = register_netdev(ndev);
  754. if (ret) {
  755. free_netdev(ndev);
  756. goto alloc_fail;
  757. }
  758. return 0;
  759. alloc_fail:
  760. hip04_free_ring(ndev, d);
  761. init_fail:
  762. of_node_put(priv->phy_node);
  763. free_netdev(ndev);
  764. return ret;
  765. }
  766. static int hip04_remove(struct platform_device *pdev)
  767. {
  768. struct net_device *ndev = platform_get_drvdata(pdev);
  769. struct hip04_priv *priv = netdev_priv(ndev);
  770. struct device *d = &pdev->dev;
  771. if (priv->phy)
  772. phy_disconnect(priv->phy);
  773. hip04_free_ring(ndev, d);
  774. unregister_netdev(ndev);
  775. free_irq(ndev->irq, ndev);
  776. of_node_put(priv->phy_node);
  777. cancel_work_sync(&priv->tx_timeout_task);
  778. free_netdev(ndev);
  779. return 0;
  780. }
  781. static const struct of_device_id hip04_mac_match[] = {
  782. { .compatible = "hisilicon,hip04-mac" },
  783. { }
  784. };
  785. MODULE_DEVICE_TABLE(of, hip04_mac_match);
  786. static struct platform_driver hip04_mac_driver = {
  787. .probe = hip04_mac_probe,
  788. .remove = hip04_remove,
  789. .driver = {
  790. .name = DRV_NAME,
  791. .owner = THIS_MODULE,
  792. .of_match_table = hip04_mac_match,
  793. },
  794. };
  795. module_platform_driver(hip04_mac_driver);
  796. MODULE_DESCRIPTION("HISILICON P04 Ethernet driver");
  797. MODULE_LICENSE("GPL");