ec_bhf.c 17 KB

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