ntb_netdev.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. if (len < 0) {
  94. ndev->stats.rx_errors++;
  95. ndev->stats.rx_length_errors++;
  96. goto enqueue_again;
  97. }
  98. skb_put(skb, len);
  99. skb->protocol = eth_type_trans(skb, ndev);
  100. skb->ip_summed = CHECKSUM_NONE;
  101. if (netif_rx(skb) == NET_RX_DROP) {
  102. ndev->stats.rx_errors++;
  103. ndev->stats.rx_dropped++;
  104. } else {
  105. ndev->stats.rx_packets++;
  106. ndev->stats.rx_bytes += len;
  107. }
  108. skb = netdev_alloc_skb(ndev, ndev->mtu + ETH_HLEN);
  109. if (!skb) {
  110. ndev->stats.rx_errors++;
  111. ndev->stats.rx_frame_errors++;
  112. return;
  113. }
  114. enqueue_again:
  115. rc = ntb_transport_rx_enqueue(qp, skb, skb->data, ndev->mtu + ETH_HLEN);
  116. if (rc) {
  117. dev_kfree_skb(skb);
  118. ndev->stats.rx_errors++;
  119. ndev->stats.rx_fifo_errors++;
  120. }
  121. }
  122. static void ntb_netdev_tx_handler(struct ntb_transport_qp *qp, void *qp_data,
  123. void *data, int len)
  124. {
  125. struct net_device *ndev = qp_data;
  126. struct sk_buff *skb;
  127. skb = data;
  128. if (!skb || !ndev)
  129. return;
  130. if (len > 0) {
  131. ndev->stats.tx_packets++;
  132. ndev->stats.tx_bytes += skb->len;
  133. } else {
  134. ndev->stats.tx_errors++;
  135. ndev->stats.tx_aborted_errors++;
  136. }
  137. dev_kfree_skb(skb);
  138. }
  139. static netdev_tx_t ntb_netdev_start_xmit(struct sk_buff *skb,
  140. struct net_device *ndev)
  141. {
  142. struct ntb_netdev *dev = netdev_priv(ndev);
  143. int rc;
  144. rc = ntb_transport_tx_enqueue(dev->qp, skb, skb->data, skb->len);
  145. if (rc)
  146. goto err;
  147. return NETDEV_TX_OK;
  148. err:
  149. ndev->stats.tx_dropped++;
  150. ndev->stats.tx_errors++;
  151. return NETDEV_TX_BUSY;
  152. }
  153. static int ntb_netdev_open(struct net_device *ndev)
  154. {
  155. struct ntb_netdev *dev = netdev_priv(ndev);
  156. struct sk_buff *skb;
  157. int rc, i, len;
  158. /* Add some empty rx bufs */
  159. for (i = 0; i < NTB_RXQ_SIZE; i++) {
  160. skb = netdev_alloc_skb(ndev, ndev->mtu + ETH_HLEN);
  161. if (!skb) {
  162. rc = -ENOMEM;
  163. goto err;
  164. }
  165. rc = ntb_transport_rx_enqueue(dev->qp, skb, skb->data,
  166. ndev->mtu + ETH_HLEN);
  167. if (rc) {
  168. dev_kfree_skb(skb);
  169. goto err;
  170. }
  171. }
  172. netif_carrier_off(ndev);
  173. ntb_transport_link_up(dev->qp);
  174. return 0;
  175. err:
  176. while ((skb = ntb_transport_rx_remove(dev->qp, &len)))
  177. dev_kfree_skb(skb);
  178. return rc;
  179. }
  180. static int ntb_netdev_close(struct net_device *ndev)
  181. {
  182. struct ntb_netdev *dev = netdev_priv(ndev);
  183. struct sk_buff *skb;
  184. int len;
  185. ntb_transport_link_down(dev->qp);
  186. while ((skb = ntb_transport_rx_remove(dev->qp, &len)))
  187. dev_kfree_skb(skb);
  188. return 0;
  189. }
  190. static int ntb_netdev_change_mtu(struct net_device *ndev, int new_mtu)
  191. {
  192. struct ntb_netdev *dev = netdev_priv(ndev);
  193. struct sk_buff *skb;
  194. int len, rc;
  195. if (new_mtu > ntb_transport_max_size(dev->qp) - ETH_HLEN)
  196. return -EINVAL;
  197. if (!netif_running(ndev)) {
  198. ndev->mtu = new_mtu;
  199. return 0;
  200. }
  201. /* Bring down the link and dispose of posted rx entries */
  202. ntb_transport_link_down(dev->qp);
  203. if (ndev->mtu < new_mtu) {
  204. int i;
  205. for (i = 0; (skb = ntb_transport_rx_remove(dev->qp, &len)); i++)
  206. dev_kfree_skb(skb);
  207. for (; i; i--) {
  208. skb = netdev_alloc_skb(ndev, new_mtu + ETH_HLEN);
  209. if (!skb) {
  210. rc = -ENOMEM;
  211. goto err;
  212. }
  213. rc = ntb_transport_rx_enqueue(dev->qp, skb, skb->data,
  214. new_mtu + ETH_HLEN);
  215. if (rc) {
  216. dev_kfree_skb(skb);
  217. goto err;
  218. }
  219. }
  220. }
  221. ndev->mtu = new_mtu;
  222. ntb_transport_link_up(dev->qp);
  223. return 0;
  224. err:
  225. ntb_transport_link_down(dev->qp);
  226. while ((skb = ntb_transport_rx_remove(dev->qp, &len)))
  227. dev_kfree_skb(skb);
  228. netdev_err(ndev, "Error changing MTU, device inoperable\n");
  229. return rc;
  230. }
  231. static const struct net_device_ops ntb_netdev_ops = {
  232. .ndo_open = ntb_netdev_open,
  233. .ndo_stop = ntb_netdev_close,
  234. .ndo_start_xmit = ntb_netdev_start_xmit,
  235. .ndo_change_mtu = ntb_netdev_change_mtu,
  236. .ndo_set_mac_address = eth_mac_addr,
  237. };
  238. static void ntb_get_drvinfo(struct net_device *ndev,
  239. struct ethtool_drvinfo *info)
  240. {
  241. struct ntb_netdev *dev = netdev_priv(ndev);
  242. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  243. strlcpy(info->version, NTB_NETDEV_VER, sizeof(info->version));
  244. strlcpy(info->bus_info, pci_name(dev->pdev), sizeof(info->bus_info));
  245. }
  246. static int ntb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  247. {
  248. cmd->supported = SUPPORTED_Backplane;
  249. cmd->advertising = ADVERTISED_Backplane;
  250. ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
  251. cmd->duplex = DUPLEX_FULL;
  252. cmd->port = PORT_OTHER;
  253. cmd->phy_address = 0;
  254. cmd->transceiver = XCVR_DUMMY1;
  255. cmd->autoneg = AUTONEG_ENABLE;
  256. cmd->maxtxpkt = 0;
  257. cmd->maxrxpkt = 0;
  258. return 0;
  259. }
  260. static const struct ethtool_ops ntb_ethtool_ops = {
  261. .get_drvinfo = ntb_get_drvinfo,
  262. .get_link = ethtool_op_get_link,
  263. .get_settings = ntb_get_settings,
  264. };
  265. static const struct ntb_queue_handlers ntb_netdev_handlers = {
  266. .tx_handler = ntb_netdev_tx_handler,
  267. .rx_handler = ntb_netdev_rx_handler,
  268. .event_handler = ntb_netdev_event_handler,
  269. };
  270. static int ntb_netdev_probe(struct device *client_dev)
  271. {
  272. struct ntb_dev *ntb;
  273. struct net_device *ndev;
  274. struct pci_dev *pdev;
  275. struct ntb_netdev *dev;
  276. int rc;
  277. ntb = dev_ntb(client_dev->parent);
  278. pdev = ntb->pdev;
  279. if (!pdev)
  280. return -ENODEV;
  281. ndev = alloc_etherdev(sizeof(*dev));
  282. if (!ndev)
  283. return -ENOMEM;
  284. dev = netdev_priv(ndev);
  285. dev->ndev = ndev;
  286. dev->pdev = pdev;
  287. ndev->features = NETIF_F_HIGHDMA;
  288. ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  289. ndev->hw_features = ndev->features;
  290. ndev->watchdog_timeo = msecs_to_jiffies(NTB_TX_TIMEOUT_MS);
  291. random_ether_addr(ndev->perm_addr);
  292. memcpy(ndev->dev_addr, ndev->perm_addr, ndev->addr_len);
  293. ndev->netdev_ops = &ntb_netdev_ops;
  294. ndev->ethtool_ops = &ntb_ethtool_ops;
  295. dev->qp = ntb_transport_create_queue(ndev, client_dev,
  296. &ntb_netdev_handlers);
  297. if (!dev->qp) {
  298. rc = -EIO;
  299. goto err;
  300. }
  301. ndev->mtu = ntb_transport_max_size(dev->qp) - ETH_HLEN;
  302. rc = register_netdev(ndev);
  303. if (rc)
  304. goto err1;
  305. list_add(&dev->list, &dev_list);
  306. dev_info(&pdev->dev, "%s created\n", ndev->name);
  307. return 0;
  308. err1:
  309. ntb_transport_free_queue(dev->qp);
  310. err:
  311. free_netdev(ndev);
  312. return rc;
  313. }
  314. static void ntb_netdev_remove(struct device *client_dev)
  315. {
  316. struct ntb_dev *ntb;
  317. struct net_device *ndev;
  318. struct pci_dev *pdev;
  319. struct ntb_netdev *dev;
  320. bool found = false;
  321. ntb = dev_ntb(client_dev->parent);
  322. pdev = ntb->pdev;
  323. list_for_each_entry(dev, &dev_list, list) {
  324. if (dev->pdev == pdev) {
  325. found = true;
  326. break;
  327. }
  328. }
  329. if (!found)
  330. return;
  331. list_del(&dev->list);
  332. ndev = dev->ndev;
  333. unregister_netdev(ndev);
  334. ntb_transport_free_queue(dev->qp);
  335. free_netdev(ndev);
  336. }
  337. static struct ntb_transport_client ntb_netdev_client = {
  338. .driver.name = KBUILD_MODNAME,
  339. .driver.owner = THIS_MODULE,
  340. .probe = ntb_netdev_probe,
  341. .remove = ntb_netdev_remove,
  342. };
  343. static int __init ntb_netdev_init_module(void)
  344. {
  345. int rc;
  346. rc = ntb_transport_register_client_dev(KBUILD_MODNAME);
  347. if (rc)
  348. return rc;
  349. return ntb_transport_register_client(&ntb_netdev_client);
  350. }
  351. module_init(ntb_netdev_init_module);
  352. static void __exit ntb_netdev_exit_module(void)
  353. {
  354. ntb_transport_unregister_client(&ntb_netdev_client);
  355. ntb_transport_unregister_client_dev(KBUILD_MODNAME);
  356. }
  357. module_exit(ntb_netdev_exit_module);