ntb_netdev.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright(c) 2012 Intel Corporation. All rights reserved.
  8. * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * BSD LICENSE
  15. *
  16. * Copyright(c) 2012 Intel Corporation. All rights reserved.
  17. * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions
  21. * are met:
  22. *
  23. * * Redistributions of source code must retain the above copyright
  24. * notice, this list of conditions and the following disclaimer.
  25. * * Redistributions in binary form must reproduce the above copy
  26. * notice, this list of conditions and the following disclaimer in
  27. * the documentation and/or other materials provided with the
  28. * distribution.
  29. * * Neither the name of Intel Corporation nor the names of its
  30. * contributors may be used to endorse or promote products derived
  31. * from this software without specific prior written permission.
  32. *
  33. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  34. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  35. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  36. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  37. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  39. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  40. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  41. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  43. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. *
  45. * PCIe NTB Network Linux driver
  46. *
  47. * Contact Information:
  48. * Jon Mason <jon.mason@intel.com>
  49. */
  50. #include <linux/etherdevice.h>
  51. #include <linux/ethtool.h>
  52. #include <linux/module.h>
  53. #include <linux/pci.h>
  54. #include <linux/ntb.h>
  55. #include <linux/ntb_transport.h>
  56. #define NTB_NETDEV_VER "0.7"
  57. MODULE_DESCRIPTION(KBUILD_MODNAME);
  58. MODULE_VERSION(NTB_NETDEV_VER);
  59. MODULE_LICENSE("Dual BSD/GPL");
  60. MODULE_AUTHOR("Intel Corporation");
  61. struct ntb_netdev {
  62. struct list_head list;
  63. struct pci_dev *pdev;
  64. struct net_device *ndev;
  65. struct ntb_transport_qp *qp;
  66. };
  67. #define NTB_TX_TIMEOUT_MS 1000
  68. #define NTB_RXQ_SIZE 100
  69. static LIST_HEAD(dev_list);
  70. static void ntb_netdev_event_handler(void *data, int link_is_up)
  71. {
  72. struct net_device *ndev = data;
  73. struct ntb_netdev *dev = netdev_priv(ndev);
  74. netdev_dbg(ndev, "Event %x, Link %x\n", link_is_up,
  75. ntb_transport_link_query(dev->qp));
  76. if (link_is_up) {
  77. if (ntb_transport_link_query(dev->qp))
  78. netif_carrier_on(ndev);
  79. } else {
  80. netif_carrier_off(ndev);
  81. }
  82. }
  83. static void ntb_netdev_rx_handler(struct ntb_transport_qp *qp, void *qp_data,
  84. void *data, int len)
  85. {
  86. struct net_device *ndev = qp_data;
  87. struct sk_buff *skb;
  88. int rc;
  89. skb = data;
  90. if (!skb)
  91. return;
  92. netdev_dbg(ndev, "%s: %d byte payload received\n", __func__, len);
  93. skb_put(skb, len);
  94. skb->protocol = eth_type_trans(skb, ndev);
  95. skb->ip_summed = CHECKSUM_NONE;
  96. if (netif_rx(skb) == NET_RX_DROP) {
  97. ndev->stats.rx_errors++;
  98. ndev->stats.rx_dropped++;
  99. } else {
  100. ndev->stats.rx_packets++;
  101. ndev->stats.rx_bytes += len;
  102. }
  103. skb = netdev_alloc_skb(ndev, ndev->mtu + ETH_HLEN);
  104. if (!skb) {
  105. ndev->stats.rx_errors++;
  106. ndev->stats.rx_frame_errors++;
  107. return;
  108. }
  109. rc = ntb_transport_rx_enqueue(qp, skb, skb->data, ndev->mtu + ETH_HLEN);
  110. if (rc) {
  111. dev_kfree_skb(skb);
  112. ndev->stats.rx_errors++;
  113. ndev->stats.rx_fifo_errors++;
  114. }
  115. }
  116. static void ntb_netdev_tx_handler(struct ntb_transport_qp *qp, void *qp_data,
  117. void *data, int len)
  118. {
  119. struct net_device *ndev = qp_data;
  120. struct sk_buff *skb;
  121. skb = data;
  122. if (!skb || !ndev)
  123. return;
  124. if (len > 0) {
  125. ndev->stats.tx_packets++;
  126. ndev->stats.tx_bytes += skb->len;
  127. } else {
  128. ndev->stats.tx_errors++;
  129. ndev->stats.tx_aborted_errors++;
  130. }
  131. dev_kfree_skb(skb);
  132. }
  133. static netdev_tx_t ntb_netdev_start_xmit(struct sk_buff *skb,
  134. struct net_device *ndev)
  135. {
  136. struct ntb_netdev *dev = netdev_priv(ndev);
  137. int rc;
  138. rc = ntb_transport_tx_enqueue(dev->qp, skb, skb->data, skb->len);
  139. if (rc)
  140. goto err;
  141. return NETDEV_TX_OK;
  142. err:
  143. ndev->stats.tx_dropped++;
  144. ndev->stats.tx_errors++;
  145. return NETDEV_TX_BUSY;
  146. }
  147. static int ntb_netdev_open(struct net_device *ndev)
  148. {
  149. struct ntb_netdev *dev = netdev_priv(ndev);
  150. struct sk_buff *skb;
  151. int rc, i, len;
  152. /* Add some empty rx bufs */
  153. for (i = 0; i < NTB_RXQ_SIZE; i++) {
  154. skb = netdev_alloc_skb(ndev, ndev->mtu + ETH_HLEN);
  155. if (!skb) {
  156. rc = -ENOMEM;
  157. goto err;
  158. }
  159. rc = ntb_transport_rx_enqueue(dev->qp, skb, skb->data,
  160. ndev->mtu + ETH_HLEN);
  161. if (rc == -EINVAL) {
  162. dev_kfree_skb(skb);
  163. goto err;
  164. }
  165. }
  166. netif_carrier_off(ndev);
  167. ntb_transport_link_up(dev->qp);
  168. return 0;
  169. err:
  170. while ((skb = ntb_transport_rx_remove(dev->qp, &len)))
  171. dev_kfree_skb(skb);
  172. return rc;
  173. }
  174. static int ntb_netdev_close(struct net_device *ndev)
  175. {
  176. struct ntb_netdev *dev = netdev_priv(ndev);
  177. struct sk_buff *skb;
  178. int len;
  179. ntb_transport_link_down(dev->qp);
  180. while ((skb = ntb_transport_rx_remove(dev->qp, &len)))
  181. dev_kfree_skb(skb);
  182. return 0;
  183. }
  184. static int ntb_netdev_change_mtu(struct net_device *ndev, int new_mtu)
  185. {
  186. struct ntb_netdev *dev = netdev_priv(ndev);
  187. struct sk_buff *skb;
  188. int len, rc;
  189. if (new_mtu > ntb_transport_max_size(dev->qp) - ETH_HLEN)
  190. return -EINVAL;
  191. if (!netif_running(ndev)) {
  192. ndev->mtu = new_mtu;
  193. return 0;
  194. }
  195. /* Bring down the link and dispose of posted rx entries */
  196. ntb_transport_link_down(dev->qp);
  197. if (ndev->mtu < new_mtu) {
  198. int i;
  199. for (i = 0; (skb = ntb_transport_rx_remove(dev->qp, &len)); i++)
  200. dev_kfree_skb(skb);
  201. for (; i; i--) {
  202. skb = netdev_alloc_skb(ndev, new_mtu + ETH_HLEN);
  203. if (!skb) {
  204. rc = -ENOMEM;
  205. goto err;
  206. }
  207. rc = ntb_transport_rx_enqueue(dev->qp, skb, skb->data,
  208. new_mtu + ETH_HLEN);
  209. if (rc) {
  210. dev_kfree_skb(skb);
  211. goto err;
  212. }
  213. }
  214. }
  215. ndev->mtu = new_mtu;
  216. ntb_transport_link_up(dev->qp);
  217. return 0;
  218. err:
  219. ntb_transport_link_down(dev->qp);
  220. while ((skb = ntb_transport_rx_remove(dev->qp, &len)))
  221. dev_kfree_skb(skb);
  222. netdev_err(ndev, "Error changing MTU, device inoperable\n");
  223. return rc;
  224. }
  225. static const struct net_device_ops ntb_netdev_ops = {
  226. .ndo_open = ntb_netdev_open,
  227. .ndo_stop = ntb_netdev_close,
  228. .ndo_start_xmit = ntb_netdev_start_xmit,
  229. .ndo_change_mtu = ntb_netdev_change_mtu,
  230. .ndo_set_mac_address = eth_mac_addr,
  231. };
  232. static void ntb_get_drvinfo(struct net_device *ndev,
  233. struct ethtool_drvinfo *info)
  234. {
  235. struct ntb_netdev *dev = netdev_priv(ndev);
  236. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  237. strlcpy(info->version, NTB_NETDEV_VER, sizeof(info->version));
  238. strlcpy(info->bus_info, pci_name(dev->pdev), sizeof(info->bus_info));
  239. }
  240. static int ntb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  241. {
  242. cmd->supported = SUPPORTED_Backplane;
  243. cmd->advertising = ADVERTISED_Backplane;
  244. ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
  245. cmd->duplex = DUPLEX_FULL;
  246. cmd->port = PORT_OTHER;
  247. cmd->phy_address = 0;
  248. cmd->transceiver = XCVR_DUMMY1;
  249. cmd->autoneg = AUTONEG_ENABLE;
  250. cmd->maxtxpkt = 0;
  251. cmd->maxrxpkt = 0;
  252. return 0;
  253. }
  254. static const struct ethtool_ops ntb_ethtool_ops = {
  255. .get_drvinfo = ntb_get_drvinfo,
  256. .get_link = ethtool_op_get_link,
  257. .get_settings = ntb_get_settings,
  258. };
  259. static const struct ntb_queue_handlers ntb_netdev_handlers = {
  260. .tx_handler = ntb_netdev_tx_handler,
  261. .rx_handler = ntb_netdev_rx_handler,
  262. .event_handler = ntb_netdev_event_handler,
  263. };
  264. static int ntb_netdev_probe(struct device *client_dev)
  265. {
  266. struct ntb_dev *ntb;
  267. struct net_device *ndev;
  268. struct pci_dev *pdev;
  269. struct ntb_netdev *dev;
  270. int rc;
  271. ntb = dev_ntb(client_dev->parent);
  272. pdev = ntb->pdev;
  273. if (!pdev)
  274. return -ENODEV;
  275. ndev = alloc_etherdev(sizeof(*dev));
  276. if (!ndev)
  277. return -ENOMEM;
  278. dev = netdev_priv(ndev);
  279. dev->ndev = ndev;
  280. dev->pdev = pdev;
  281. ndev->features = NETIF_F_HIGHDMA;
  282. ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  283. ndev->hw_features = ndev->features;
  284. ndev->watchdog_timeo = msecs_to_jiffies(NTB_TX_TIMEOUT_MS);
  285. random_ether_addr(ndev->perm_addr);
  286. memcpy(ndev->dev_addr, ndev->perm_addr, ndev->addr_len);
  287. ndev->netdev_ops = &ntb_netdev_ops;
  288. ndev->ethtool_ops = &ntb_ethtool_ops;
  289. dev->qp = ntb_transport_create_queue(ndev, client_dev,
  290. &ntb_netdev_handlers);
  291. if (!dev->qp) {
  292. rc = -EIO;
  293. goto err;
  294. }
  295. ndev->mtu = ntb_transport_max_size(dev->qp) - ETH_HLEN;
  296. rc = register_netdev(ndev);
  297. if (rc)
  298. goto err1;
  299. list_add(&dev->list, &dev_list);
  300. dev_info(&pdev->dev, "%s created\n", ndev->name);
  301. return 0;
  302. err1:
  303. ntb_transport_free_queue(dev->qp);
  304. err:
  305. free_netdev(ndev);
  306. return rc;
  307. }
  308. static void ntb_netdev_remove(struct device *client_dev)
  309. {
  310. struct ntb_dev *ntb;
  311. struct net_device *ndev;
  312. struct pci_dev *pdev;
  313. struct ntb_netdev *dev;
  314. bool found = false;
  315. ntb = dev_ntb(client_dev->parent);
  316. pdev = ntb->pdev;
  317. list_for_each_entry(dev, &dev_list, list) {
  318. if (dev->pdev == pdev) {
  319. found = true;
  320. break;
  321. }
  322. }
  323. if (!found)
  324. return;
  325. list_del(&dev->list);
  326. ndev = dev->ndev;
  327. unregister_netdev(ndev);
  328. ntb_transport_free_queue(dev->qp);
  329. free_netdev(ndev);
  330. }
  331. static struct ntb_transport_client ntb_netdev_client = {
  332. .driver.name = KBUILD_MODNAME,
  333. .driver.owner = THIS_MODULE,
  334. .probe = ntb_netdev_probe,
  335. .remove = ntb_netdev_remove,
  336. };
  337. static int __init ntb_netdev_init_module(void)
  338. {
  339. int rc;
  340. rc = ntb_transport_register_client_dev(KBUILD_MODNAME);
  341. if (rc)
  342. return rc;
  343. return ntb_transport_register_client(&ntb_netdev_client);
  344. }
  345. module_init(ntb_netdev_init_module);
  346. static void __exit ntb_netdev_exit_module(void)
  347. {
  348. ntb_transport_unregister_client(&ntb_netdev_client);
  349. ntb_transport_unregister_client_dev(KBUILD_MODNAME);
  350. }
  351. module_exit(ntb_netdev_exit_module);