ec_bhf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/net/ethernet/ec_bhf.c
  4. *
  5. * Copyright (C) 2014 Darek Marcinkiewicz <[email protected]>
  6. */
  7. /* This is a driver for EtherCAT master module present on CCAT FPGA.
  8. * Those can be found on Bechhoff CX50xx industrial PCs.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/pci.h>
  14. #include <linux/init.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/ip.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/hrtimer.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/stat.h>
  22. #define TIMER_INTERVAL_NSEC 20000
  23. #define INFO_BLOCK_SIZE 0x10
  24. #define INFO_BLOCK_TYPE 0x0
  25. #define INFO_BLOCK_REV 0x2
  26. #define INFO_BLOCK_BLK_CNT 0x4
  27. #define INFO_BLOCK_TX_CHAN 0x4
  28. #define INFO_BLOCK_RX_CHAN 0x5
  29. #define INFO_BLOCK_OFFSET 0x8
  30. #define EC_MII_OFFSET 0x4
  31. #define EC_FIFO_OFFSET 0x8
  32. #define EC_MAC_OFFSET 0xc
  33. #define MAC_FRAME_ERR_CNT 0x0
  34. #define MAC_RX_ERR_CNT 0x1
  35. #define MAC_CRC_ERR_CNT 0x2
  36. #define MAC_LNK_LST_ERR_CNT 0x3
  37. #define MAC_TX_FRAME_CNT 0x10
  38. #define MAC_RX_FRAME_CNT 0x14
  39. #define MAC_TX_FIFO_LVL 0x20
  40. #define MAC_DROPPED_FRMS 0x28
  41. #define MAC_CONNECTED_CCAT_FLAG 0x78
  42. #define MII_MAC_ADDR 0x8
  43. #define MII_MAC_FILT_FLAG 0xe
  44. #define MII_LINK_STATUS 0xf
  45. #define FIFO_TX_REG 0x0
  46. #define FIFO_TX_RESET 0x8
  47. #define FIFO_RX_REG 0x10
  48. #define FIFO_RX_ADDR_VALID (1u << 31)
  49. #define FIFO_RX_RESET 0x18
  50. #define DMA_CHAN_OFFSET 0x1000
  51. #define DMA_CHAN_SIZE 0x8
  52. #define DMA_WINDOW_SIZE_MASK 0xfffffffc
  53. #define ETHERCAT_MASTER_ID 0x14
  54. static const struct pci_device_id ids[] = {
  55. { PCI_DEVICE(0x15ec, 0x5000), },
  56. { 0, }
  57. };
  58. MODULE_DEVICE_TABLE(pci, ids);
  59. struct rx_header {
  60. #define RXHDR_NEXT_ADDR_MASK 0xffffffu
  61. #define RXHDR_NEXT_VALID (1u << 31)
  62. __le32 next;
  63. #define RXHDR_NEXT_RECV_FLAG 0x1
  64. __le32 recv;
  65. #define RXHDR_LEN_MASK 0xfffu
  66. __le16 len;
  67. __le16 port;
  68. __le32 reserved;
  69. u8 timestamp[8];
  70. } __packed;
  71. #define PKT_PAYLOAD_SIZE 0x7e8
  72. struct rx_desc {
  73. struct rx_header header;
  74. u8 data[PKT_PAYLOAD_SIZE];
  75. } __packed;
  76. struct tx_header {
  77. __le16 len;
  78. #define TX_HDR_PORT_0 0x1
  79. #define TX_HDR_PORT_1 0x2
  80. u8 port;
  81. u8 ts_enable;
  82. #define TX_HDR_SENT 0x1
  83. __le32 sent;
  84. u8 timestamp[8];
  85. } __packed;
  86. struct tx_desc {
  87. struct tx_header header;
  88. u8 data[PKT_PAYLOAD_SIZE];
  89. } __packed;
  90. #define FIFO_SIZE 64
  91. static long polling_frequency = TIMER_INTERVAL_NSEC;
  92. struct bhf_dma {
  93. u8 *buf;
  94. size_t len;
  95. dma_addr_t buf_phys;
  96. u8 *alloc;
  97. size_t alloc_len;
  98. dma_addr_t alloc_phys;
  99. };
  100. struct ec_bhf_priv {
  101. struct net_device *net_dev;
  102. struct pci_dev *dev;
  103. void __iomem *io;
  104. void __iomem *dma_io;
  105. struct hrtimer hrtimer;
  106. int tx_dma_chan;
  107. int rx_dma_chan;
  108. void __iomem *ec_io;
  109. void __iomem *fifo_io;
  110. void __iomem *mii_io;
  111. void __iomem *mac_io;
  112. struct bhf_dma rx_buf;
  113. struct rx_desc *rx_descs;
  114. int rx_dnext;
  115. int rx_dcount;
  116. struct bhf_dma tx_buf;
  117. struct tx_desc *tx_descs;
  118. int tx_dcount;
  119. int tx_dnext;
  120. u64 stat_rx_bytes;
  121. u64 stat_tx_bytes;
  122. };
  123. #define PRIV_TO_DEV(priv) (&(priv)->dev->dev)
  124. static void ec_bhf_reset(struct ec_bhf_priv *priv)
  125. {
  126. iowrite8(0, priv->mac_io + MAC_FRAME_ERR_CNT);
  127. iowrite8(0, priv->mac_io + MAC_RX_ERR_CNT);
  128. iowrite8(0, priv->mac_io + MAC_CRC_ERR_CNT);
  129. iowrite8(0, priv->mac_io + MAC_LNK_LST_ERR_CNT);
  130. iowrite32(0, priv->mac_io + MAC_TX_FRAME_CNT);
  131. iowrite32(0, priv->mac_io + MAC_RX_FRAME_CNT);
  132. iowrite8(0, priv->mac_io + MAC_DROPPED_FRMS);
  133. iowrite8(0, priv->fifo_io + FIFO_TX_RESET);
  134. iowrite8(0, priv->fifo_io + FIFO_RX_RESET);
  135. iowrite8(0, priv->mac_io + MAC_TX_FIFO_LVL);
  136. }
  137. static void ec_bhf_send_packet(struct ec_bhf_priv *priv, struct tx_desc *desc)
  138. {
  139. u32 len = le16_to_cpu(desc->header.len) + sizeof(desc->header);
  140. u32 addr = (u8 *)desc - priv->tx_buf.buf;
  141. iowrite32((ALIGN(len, 8) << 24) | addr, priv->fifo_io + FIFO_TX_REG);
  142. }
  143. static int ec_bhf_desc_sent(struct tx_desc *desc)
  144. {
  145. return le32_to_cpu(desc->header.sent) & TX_HDR_SENT;
  146. }
  147. static void ec_bhf_process_tx(struct ec_bhf_priv *priv)
  148. {
  149. if (unlikely(netif_queue_stopped(priv->net_dev))) {
  150. /* Make sure that we perceive changes to tx_dnext. */
  151. smp_rmb();
  152. if (ec_bhf_desc_sent(&priv->tx_descs[priv->tx_dnext]))
  153. netif_wake_queue(priv->net_dev);
  154. }
  155. }
  156. static int ec_bhf_pkt_received(struct rx_desc *desc)
  157. {
  158. return le32_to_cpu(desc->header.recv) & RXHDR_NEXT_RECV_FLAG;
  159. }
  160. static void ec_bhf_add_rx_desc(struct ec_bhf_priv *priv, struct rx_desc *desc)
  161. {
  162. iowrite32(FIFO_RX_ADDR_VALID | ((u8 *)(desc) - priv->rx_buf.buf),
  163. priv->fifo_io + FIFO_RX_REG);
  164. }
  165. static void ec_bhf_process_rx(struct ec_bhf_priv *priv)
  166. {
  167. struct rx_desc *desc = &priv->rx_descs[priv->rx_dnext];
  168. while (ec_bhf_pkt_received(desc)) {
  169. int pkt_size = (le16_to_cpu(desc->header.len) &
  170. RXHDR_LEN_MASK) - sizeof(struct rx_header) - 4;
  171. u8 *data = desc->data;
  172. struct sk_buff *skb;
  173. skb = netdev_alloc_skb_ip_align(priv->net_dev, pkt_size);
  174. if (skb) {
  175. skb_put_data(skb, data, pkt_size);
  176. skb->protocol = eth_type_trans(skb, priv->net_dev);
  177. priv->stat_rx_bytes += pkt_size;
  178. netif_rx(skb);
  179. } else {
  180. dev_err_ratelimited(PRIV_TO_DEV(priv),
  181. "Couldn't allocate a skb_buff for a packet of size %u\n",
  182. pkt_size);
  183. }
  184. desc->header.recv = 0;
  185. ec_bhf_add_rx_desc(priv, desc);
  186. priv->rx_dnext = (priv->rx_dnext + 1) % priv->rx_dcount;
  187. desc = &priv->rx_descs[priv->rx_dnext];
  188. }
  189. }
  190. static enum hrtimer_restart ec_bhf_timer_fun(struct hrtimer *timer)
  191. {
  192. struct ec_bhf_priv *priv = container_of(timer, struct ec_bhf_priv,
  193. hrtimer);
  194. ec_bhf_process_rx(priv);
  195. ec_bhf_process_tx(priv);
  196. if (!netif_running(priv->net_dev))
  197. return HRTIMER_NORESTART;
  198. hrtimer_forward_now(timer, polling_frequency);
  199. return HRTIMER_RESTART;
  200. }
  201. static int ec_bhf_setup_offsets(struct ec_bhf_priv *priv)
  202. {
  203. struct device *dev = PRIV_TO_DEV(priv);
  204. unsigned block_count, i;
  205. void __iomem *ec_info;
  206. block_count = ioread8(priv->io + INFO_BLOCK_BLK_CNT);
  207. for (i = 0; i < block_count; i++) {
  208. u16 type = ioread16(priv->io + i * INFO_BLOCK_SIZE +
  209. INFO_BLOCK_TYPE);
  210. if (type == ETHERCAT_MASTER_ID)
  211. break;
  212. }
  213. if (i == block_count) {
  214. dev_err(dev, "EtherCAT master with DMA block not found\n");
  215. return -ENODEV;
  216. }
  217. ec_info = priv->io + i * INFO_BLOCK_SIZE;
  218. priv->tx_dma_chan = ioread8(ec_info + INFO_BLOCK_TX_CHAN);
  219. priv->rx_dma_chan = ioread8(ec_info + INFO_BLOCK_RX_CHAN);
  220. priv->ec_io = priv->io + ioread32(ec_info + INFO_BLOCK_OFFSET);
  221. priv->mii_io = priv->ec_io + ioread32(priv->ec_io + EC_MII_OFFSET);
  222. priv->fifo_io = priv->ec_io + ioread32(priv->ec_io + EC_FIFO_OFFSET);
  223. priv->mac_io = priv->ec_io + ioread32(priv->ec_io + EC_MAC_OFFSET);
  224. return 0;
  225. }
  226. static netdev_tx_t ec_bhf_start_xmit(struct sk_buff *skb,
  227. struct net_device *net_dev)
  228. {
  229. struct ec_bhf_priv *priv = netdev_priv(net_dev);
  230. struct tx_desc *desc;
  231. unsigned len;
  232. desc = &priv->tx_descs[priv->tx_dnext];
  233. skb_copy_and_csum_dev(skb, desc->data);
  234. len = skb->len;
  235. memset(&desc->header, 0, sizeof(desc->header));
  236. desc->header.len = cpu_to_le16(len);
  237. desc->header.port = TX_HDR_PORT_0;
  238. ec_bhf_send_packet(priv, desc);
  239. priv->tx_dnext = (priv->tx_dnext + 1) % priv->tx_dcount;
  240. if (!ec_bhf_desc_sent(&priv->tx_descs[priv->tx_dnext])) {
  241. /* Make sure that updates to tx_dnext are perceived
  242. * by timer routine.
  243. */
  244. smp_wmb();
  245. netif_stop_queue(net_dev);
  246. }
  247. priv->stat_tx_bytes += len;
  248. dev_kfree_skb(skb);
  249. return NETDEV_TX_OK;
  250. }
  251. static int ec_bhf_alloc_dma_mem(struct ec_bhf_priv *priv,
  252. struct bhf_dma *buf,
  253. int channel,
  254. int size)
  255. {
  256. int offset = channel * DMA_CHAN_SIZE + DMA_CHAN_OFFSET;
  257. struct device *dev = PRIV_TO_DEV(priv);
  258. u32 mask;
  259. iowrite32(0xffffffff, priv->dma_io + offset);
  260. mask = ioread32(priv->dma_io + offset);
  261. mask &= DMA_WINDOW_SIZE_MASK;
  262. /* We want to allocate a chunk of memory that is:
  263. * - aligned to the mask we just read
  264. * - is of size 2^mask bytes (at most)
  265. * In order to ensure that we will allocate buffer of
  266. * 2 * 2^mask bytes.
  267. */
  268. buf->len = min_t(int, ~mask + 1, size);
  269. buf->alloc_len = 2 * buf->len;
  270. buf->alloc = dma_alloc_coherent(dev, buf->alloc_len, &buf->alloc_phys,
  271. GFP_KERNEL);
  272. if (buf->alloc == NULL) {
  273. dev_err(dev, "Failed to allocate buffer\n");
  274. return -ENOMEM;
  275. }
  276. buf->buf_phys = (buf->alloc_phys + buf->len) & mask;
  277. buf->buf = buf->alloc + (buf->buf_phys - buf->alloc_phys);
  278. iowrite32(0, priv->dma_io + offset + 4);
  279. iowrite32(buf->buf_phys, priv->dma_io + offset);
  280. return 0;
  281. }
  282. static void ec_bhf_setup_tx_descs(struct ec_bhf_priv *priv)
  283. {
  284. int i = 0;
  285. priv->tx_dcount = priv->tx_buf.len / sizeof(struct tx_desc);
  286. priv->tx_descs = (struct tx_desc *)priv->tx_buf.buf;
  287. priv->tx_dnext = 0;
  288. for (i = 0; i < priv->tx_dcount; i++)
  289. priv->tx_descs[i].header.sent = cpu_to_le32(TX_HDR_SENT);
  290. }
  291. static void ec_bhf_setup_rx_descs(struct ec_bhf_priv *priv)
  292. {
  293. int i;
  294. priv->rx_dcount = priv->rx_buf.len / sizeof(struct rx_desc);
  295. priv->rx_descs = (struct rx_desc *)priv->rx_buf.buf;
  296. priv->rx_dnext = 0;
  297. for (i = 0; i < priv->rx_dcount; i++) {
  298. struct rx_desc *desc = &priv->rx_descs[i];
  299. u32 next;
  300. if (i != priv->rx_dcount - 1)
  301. next = (u8 *)(desc + 1) - priv->rx_buf.buf;
  302. else
  303. next = 0;
  304. next |= RXHDR_NEXT_VALID;
  305. desc->header.next = cpu_to_le32(next);
  306. desc->header.recv = 0;
  307. ec_bhf_add_rx_desc(priv, desc);
  308. }
  309. }
  310. static int ec_bhf_open(struct net_device *net_dev)
  311. {
  312. struct ec_bhf_priv *priv = netdev_priv(net_dev);
  313. struct device *dev = PRIV_TO_DEV(priv);
  314. int err = 0;
  315. ec_bhf_reset(priv);
  316. err = ec_bhf_alloc_dma_mem(priv, &priv->rx_buf, priv->rx_dma_chan,
  317. FIFO_SIZE * sizeof(struct rx_desc));
  318. if (err) {
  319. dev_err(dev, "Failed to allocate rx buffer\n");
  320. goto out;
  321. }
  322. ec_bhf_setup_rx_descs(priv);
  323. err = ec_bhf_alloc_dma_mem(priv, &priv->tx_buf, priv->tx_dma_chan,
  324. FIFO_SIZE * sizeof(struct tx_desc));
  325. if (err) {
  326. dev_err(dev, "Failed to allocate tx buffer\n");
  327. goto error_rx_free;
  328. }
  329. iowrite8(0, priv->mii_io + MII_MAC_FILT_FLAG);
  330. ec_bhf_setup_tx_descs(priv);
  331. netif_start_queue(net_dev);
  332. hrtimer_init(&priv->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  333. priv->hrtimer.function = ec_bhf_timer_fun;
  334. hrtimer_start(&priv->hrtimer, polling_frequency, HRTIMER_MODE_REL);
  335. return 0;
  336. error_rx_free:
  337. dma_free_coherent(dev, priv->rx_buf.alloc_len, priv->rx_buf.alloc,
  338. priv->rx_buf.alloc_len);
  339. out:
  340. return err;
  341. }
  342. static int ec_bhf_stop(struct net_device *net_dev)
  343. {
  344. struct ec_bhf_priv *priv = netdev_priv(net_dev);
  345. struct device *dev = PRIV_TO_DEV(priv);
  346. hrtimer_cancel(&priv->hrtimer);
  347. ec_bhf_reset(priv);
  348. netif_tx_disable(net_dev);
  349. dma_free_coherent(dev, priv->tx_buf.alloc_len,
  350. priv->tx_buf.alloc, priv->tx_buf.alloc_phys);
  351. dma_free_coherent(dev, priv->rx_buf.alloc_len,
  352. priv->rx_buf.alloc, priv->rx_buf.alloc_phys);
  353. return 0;
  354. }
  355. static void
  356. ec_bhf_get_stats(struct net_device *net_dev,
  357. struct rtnl_link_stats64 *stats)
  358. {
  359. struct ec_bhf_priv *priv = netdev_priv(net_dev);
  360. stats->rx_errors = ioread8(priv->mac_io + MAC_RX_ERR_CNT) +
  361. ioread8(priv->mac_io + MAC_CRC_ERR_CNT) +
  362. ioread8(priv->mac_io + MAC_FRAME_ERR_CNT);
  363. stats->rx_packets = ioread32(priv->mac_io + MAC_RX_FRAME_CNT);
  364. stats->tx_packets = ioread32(priv->mac_io + MAC_TX_FRAME_CNT);
  365. stats->rx_dropped = ioread8(priv->mac_io + MAC_DROPPED_FRMS);
  366. stats->tx_bytes = priv->stat_tx_bytes;
  367. stats->rx_bytes = priv->stat_rx_bytes;
  368. }
  369. static const struct net_device_ops ec_bhf_netdev_ops = {
  370. .ndo_start_xmit = ec_bhf_start_xmit,
  371. .ndo_open = ec_bhf_open,
  372. .ndo_stop = ec_bhf_stop,
  373. .ndo_get_stats64 = ec_bhf_get_stats,
  374. .ndo_validate_addr = eth_validate_addr,
  375. .ndo_set_mac_address = eth_mac_addr
  376. };
  377. static int ec_bhf_probe(struct pci_dev *dev, const struct pci_device_id *id)
  378. {
  379. struct net_device *net_dev;
  380. struct ec_bhf_priv *priv;
  381. void __iomem *dma_io;
  382. u8 addr[ETH_ALEN];
  383. void __iomem *io;
  384. int err = 0;
  385. err = pci_enable_device(dev);
  386. if (err)
  387. return err;
  388. pci_set_master(dev);
  389. err = dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
  390. if (err) {
  391. dev_err(&dev->dev,
  392. "Required dma mask not supported, failed to initialize device\n");
  393. goto err_disable_dev;
  394. }
  395. err = pci_request_regions(dev, "ec_bhf");
  396. if (err) {
  397. dev_err(&dev->dev, "Failed to request pci memory regions\n");
  398. goto err_disable_dev;
  399. }
  400. io = pci_iomap(dev, 0, 0);
  401. if (!io) {
  402. dev_err(&dev->dev, "Failed to map pci card memory bar 0");
  403. err = -EIO;
  404. goto err_release_regions;
  405. }
  406. dma_io = pci_iomap(dev, 2, 0);
  407. if (!dma_io) {
  408. dev_err(&dev->dev, "Failed to map pci card memory bar 2");
  409. err = -EIO;
  410. goto err_unmap;
  411. }
  412. net_dev = alloc_etherdev(sizeof(struct ec_bhf_priv));
  413. if (net_dev == NULL) {
  414. err = -ENOMEM;
  415. goto err_unmap_dma_io;
  416. }
  417. pci_set_drvdata(dev, net_dev);
  418. SET_NETDEV_DEV(net_dev, &dev->dev);
  419. net_dev->features = 0;
  420. net_dev->flags |= IFF_NOARP;
  421. net_dev->netdev_ops = &ec_bhf_netdev_ops;
  422. priv = netdev_priv(net_dev);
  423. priv->net_dev = net_dev;
  424. priv->io = io;
  425. priv->dma_io = dma_io;
  426. priv->dev = dev;
  427. err = ec_bhf_setup_offsets(priv);
  428. if (err < 0)
  429. goto err_free_net_dev;
  430. memcpy_fromio(addr, priv->mii_io + MII_MAC_ADDR, ETH_ALEN);
  431. eth_hw_addr_set(net_dev, addr);
  432. err = register_netdev(net_dev);
  433. if (err < 0)
  434. goto err_free_net_dev;
  435. return 0;
  436. err_free_net_dev:
  437. free_netdev(net_dev);
  438. err_unmap_dma_io:
  439. pci_iounmap(dev, dma_io);
  440. err_unmap:
  441. pci_iounmap(dev, io);
  442. err_release_regions:
  443. pci_release_regions(dev);
  444. err_disable_dev:
  445. pci_clear_master(dev);
  446. pci_disable_device(dev);
  447. return err;
  448. }
  449. static void ec_bhf_remove(struct pci_dev *dev)
  450. {
  451. struct net_device *net_dev = pci_get_drvdata(dev);
  452. struct ec_bhf_priv *priv = netdev_priv(net_dev);
  453. unregister_netdev(net_dev);
  454. pci_iounmap(dev, priv->dma_io);
  455. pci_iounmap(dev, priv->io);
  456. free_netdev(net_dev);
  457. pci_release_regions(dev);
  458. pci_clear_master(dev);
  459. pci_disable_device(dev);
  460. }
  461. static struct pci_driver pci_driver = {
  462. .name = "ec_bhf",
  463. .id_table = ids,
  464. .probe = ec_bhf_probe,
  465. .remove = ec_bhf_remove,
  466. };
  467. module_pci_driver(pci_driver);
  468. module_param(polling_frequency, long, 0444);
  469. MODULE_PARM_DESC(polling_frequency, "Polling timer frequency in ns");
  470. MODULE_LICENSE("GPL");
  471. MODULE_AUTHOR("Dariusz Marcinkiewicz <[email protected]>");