ntb_netdev.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. /* Time in usecs for tx resource reaper */
  62. static unsigned int tx_time = 1;
  63. /* Number of descriptors to free before resuming tx */
  64. static unsigned int tx_start = 10;
  65. /* Number of descriptors still available before stop upper layer tx */
  66. static unsigned int tx_stop = 5;
  67. struct ntb_netdev {
  68. struct pci_dev *pdev;
  69. struct net_device *ndev;
  70. struct ntb_transport_qp *qp;
  71. struct timer_list tx_timer;
  72. };
  73. #define NTB_TX_TIMEOUT_MS 1000
  74. #define NTB_RXQ_SIZE 100
  75. static void ntb_netdev_event_handler(void *data, int link_is_up)
  76. {
  77. struct net_device *ndev = data;
  78. struct ntb_netdev *dev = netdev_priv(ndev);
  79. netdev_dbg(ndev, "Event %x, Link %x\n", link_is_up,
  80. ntb_transport_link_query(dev->qp));
  81. if (link_is_up) {
  82. if (ntb_transport_link_query(dev->qp))
  83. netif_carrier_on(ndev);
  84. } else {
  85. netif_carrier_off(ndev);
  86. }
  87. }
  88. static void ntb_netdev_rx_handler(struct ntb_transport_qp *qp, void *qp_data,
  89. void *data, int len)
  90. {
  91. struct net_device *ndev = qp_data;
  92. struct sk_buff *skb;
  93. int rc;
  94. skb = data;
  95. if (!skb)
  96. return;
  97. netdev_dbg(ndev, "%s: %d byte payload received\n", __func__, len);
  98. if (len < 0) {
  99. ndev->stats.rx_errors++;
  100. ndev->stats.rx_length_errors++;
  101. goto enqueue_again;
  102. }
  103. skb_put(skb, len);
  104. skb->protocol = eth_type_trans(skb, ndev);
  105. skb->ip_summed = CHECKSUM_NONE;
  106. if (netif_rx(skb) == NET_RX_DROP) {
  107. ndev->stats.rx_errors++;
  108. ndev->stats.rx_dropped++;
  109. } else {
  110. ndev->stats.rx_packets++;
  111. ndev->stats.rx_bytes += len;
  112. }
  113. skb = netdev_alloc_skb(ndev, ndev->mtu + ETH_HLEN);
  114. if (!skb) {
  115. ndev->stats.rx_errors++;
  116. ndev->stats.rx_frame_errors++;
  117. return;
  118. }
  119. enqueue_again:
  120. rc = ntb_transport_rx_enqueue(qp, skb, skb->data, ndev->mtu + ETH_HLEN);
  121. if (rc) {
  122. dev_kfree_skb(skb);
  123. ndev->stats.rx_errors++;
  124. ndev->stats.rx_fifo_errors++;
  125. }
  126. }
  127. static int __ntb_netdev_maybe_stop_tx(struct net_device *netdev,
  128. struct ntb_transport_qp *qp, int size)
  129. {
  130. struct ntb_netdev *dev = netdev_priv(netdev);
  131. netif_stop_queue(netdev);
  132. /* Make sure to see the latest value of ntb_transport_tx_free_entry()
  133. * since the queue was last started.
  134. */
  135. smp_mb();
  136. if (likely(ntb_transport_tx_free_entry(qp) < size)) {
  137. mod_timer(&dev->tx_timer, jiffies + usecs_to_jiffies(tx_time));
  138. return -EBUSY;
  139. }
  140. netif_start_queue(netdev);
  141. return 0;
  142. }
  143. static int ntb_netdev_maybe_stop_tx(struct net_device *ndev,
  144. struct ntb_transport_qp *qp, int size)
  145. {
  146. if (netif_queue_stopped(ndev) ||
  147. (ntb_transport_tx_free_entry(qp) >= size))
  148. return 0;
  149. return __ntb_netdev_maybe_stop_tx(ndev, qp, size);
  150. }
  151. static void ntb_netdev_tx_handler(struct ntb_transport_qp *qp, void *qp_data,
  152. void *data, int len)
  153. {
  154. struct net_device *ndev = qp_data;
  155. struct sk_buff *skb;
  156. struct ntb_netdev *dev = netdev_priv(ndev);
  157. skb = data;
  158. if (!skb || !ndev)
  159. return;
  160. if (len > 0) {
  161. ndev->stats.tx_packets++;
  162. ndev->stats.tx_bytes += skb->len;
  163. } else {
  164. ndev->stats.tx_errors++;
  165. ndev->stats.tx_aborted_errors++;
  166. }
  167. dev_kfree_skb(skb);
  168. if (ntb_transport_tx_free_entry(dev->qp) >= tx_start) {
  169. /* Make sure anybody stopping the queue after this sees the new
  170. * value of ntb_transport_tx_free_entry()
  171. */
  172. smp_mb();
  173. if (netif_queue_stopped(ndev))
  174. netif_wake_queue(ndev);
  175. }
  176. }
  177. static netdev_tx_t ntb_netdev_start_xmit(struct sk_buff *skb,
  178. struct net_device *ndev)
  179. {
  180. struct ntb_netdev *dev = netdev_priv(ndev);
  181. int rc;
  182. ntb_netdev_maybe_stop_tx(ndev, dev->qp, tx_stop);
  183. rc = ntb_transport_tx_enqueue(dev->qp, skb, skb->data, skb->len);
  184. if (rc)
  185. goto err;
  186. /* check for next submit */
  187. ntb_netdev_maybe_stop_tx(ndev, dev->qp, tx_stop);
  188. return NETDEV_TX_OK;
  189. err:
  190. ndev->stats.tx_dropped++;
  191. ndev->stats.tx_errors++;
  192. return NETDEV_TX_BUSY;
  193. }
  194. static void ntb_netdev_tx_timer(struct timer_list *t)
  195. {
  196. struct ntb_netdev *dev = from_timer(dev, t, tx_timer);
  197. struct net_device *ndev = dev->ndev;
  198. if (ntb_transport_tx_free_entry(dev->qp) < tx_stop) {
  199. mod_timer(&dev->tx_timer, jiffies + usecs_to_jiffies(tx_time));
  200. } else {
  201. /* Make sure anybody stopping the queue after this sees the new
  202. * value of ntb_transport_tx_free_entry()
  203. */
  204. smp_mb();
  205. if (netif_queue_stopped(ndev))
  206. netif_wake_queue(ndev);
  207. }
  208. }
  209. static int ntb_netdev_open(struct net_device *ndev)
  210. {
  211. struct ntb_netdev *dev = netdev_priv(ndev);
  212. struct sk_buff *skb;
  213. int rc, i, len;
  214. /* Add some empty rx bufs */
  215. for (i = 0; i < NTB_RXQ_SIZE; i++) {
  216. skb = netdev_alloc_skb(ndev, ndev->mtu + ETH_HLEN);
  217. if (!skb) {
  218. rc = -ENOMEM;
  219. goto err;
  220. }
  221. rc = ntb_transport_rx_enqueue(dev->qp, skb, skb->data,
  222. ndev->mtu + ETH_HLEN);
  223. if (rc) {
  224. dev_kfree_skb(skb);
  225. goto err;
  226. }
  227. }
  228. timer_setup(&dev->tx_timer, ntb_netdev_tx_timer, 0);
  229. netif_carrier_off(ndev);
  230. ntb_transport_link_up(dev->qp);
  231. netif_start_queue(ndev);
  232. return 0;
  233. err:
  234. while ((skb = ntb_transport_rx_remove(dev->qp, &len)))
  235. dev_kfree_skb(skb);
  236. return rc;
  237. }
  238. static int ntb_netdev_close(struct net_device *ndev)
  239. {
  240. struct ntb_netdev *dev = netdev_priv(ndev);
  241. struct sk_buff *skb;
  242. int len;
  243. ntb_transport_link_down(dev->qp);
  244. while ((skb = ntb_transport_rx_remove(dev->qp, &len)))
  245. dev_kfree_skb(skb);
  246. del_timer_sync(&dev->tx_timer);
  247. return 0;
  248. }
  249. static int ntb_netdev_change_mtu(struct net_device *ndev, int new_mtu)
  250. {
  251. struct ntb_netdev *dev = netdev_priv(ndev);
  252. struct sk_buff *skb;
  253. int len, rc;
  254. if (new_mtu > ntb_transport_max_size(dev->qp) - ETH_HLEN)
  255. return -EINVAL;
  256. if (!netif_running(ndev)) {
  257. ndev->mtu = new_mtu;
  258. return 0;
  259. }
  260. /* Bring down the link and dispose of posted rx entries */
  261. ntb_transport_link_down(dev->qp);
  262. if (ndev->mtu < new_mtu) {
  263. int i;
  264. for (i = 0; (skb = ntb_transport_rx_remove(dev->qp, &len)); i++)
  265. dev_kfree_skb(skb);
  266. for (; i; i--) {
  267. skb = netdev_alloc_skb(ndev, new_mtu + ETH_HLEN);
  268. if (!skb) {
  269. rc = -ENOMEM;
  270. goto err;
  271. }
  272. rc = ntb_transport_rx_enqueue(dev->qp, skb, skb->data,
  273. new_mtu + ETH_HLEN);
  274. if (rc) {
  275. dev_kfree_skb(skb);
  276. goto err;
  277. }
  278. }
  279. }
  280. ndev->mtu = new_mtu;
  281. ntb_transport_link_up(dev->qp);
  282. return 0;
  283. err:
  284. ntb_transport_link_down(dev->qp);
  285. while ((skb = ntb_transport_rx_remove(dev->qp, &len)))
  286. dev_kfree_skb(skb);
  287. netdev_err(ndev, "Error changing MTU, device inoperable\n");
  288. return rc;
  289. }
  290. static const struct net_device_ops ntb_netdev_ops = {
  291. .ndo_open = ntb_netdev_open,
  292. .ndo_stop = ntb_netdev_close,
  293. .ndo_start_xmit = ntb_netdev_start_xmit,
  294. .ndo_change_mtu = ntb_netdev_change_mtu,
  295. .ndo_set_mac_address = eth_mac_addr,
  296. };
  297. static void ntb_get_drvinfo(struct net_device *ndev,
  298. struct ethtool_drvinfo *info)
  299. {
  300. struct ntb_netdev *dev = netdev_priv(ndev);
  301. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  302. strlcpy(info->version, NTB_NETDEV_VER, sizeof(info->version));
  303. strlcpy(info->bus_info, pci_name(dev->pdev), sizeof(info->bus_info));
  304. }
  305. static int ntb_get_link_ksettings(struct net_device *dev,
  306. struct ethtool_link_ksettings *cmd)
  307. {
  308. ethtool_link_ksettings_zero_link_mode(cmd, supported);
  309. ethtool_link_ksettings_add_link_mode(cmd, supported, Backplane);
  310. ethtool_link_ksettings_zero_link_mode(cmd, advertising);
  311. ethtool_link_ksettings_add_link_mode(cmd, advertising, Backplane);
  312. cmd->base.speed = SPEED_UNKNOWN;
  313. cmd->base.duplex = DUPLEX_FULL;
  314. cmd->base.port = PORT_OTHER;
  315. cmd->base.phy_address = 0;
  316. cmd->base.autoneg = AUTONEG_ENABLE;
  317. return 0;
  318. }
  319. static const struct ethtool_ops ntb_ethtool_ops = {
  320. .get_drvinfo = ntb_get_drvinfo,
  321. .get_link = ethtool_op_get_link,
  322. .get_link_ksettings = ntb_get_link_ksettings,
  323. };
  324. static const struct ntb_queue_handlers ntb_netdev_handlers = {
  325. .tx_handler = ntb_netdev_tx_handler,
  326. .rx_handler = ntb_netdev_rx_handler,
  327. .event_handler = ntb_netdev_event_handler,
  328. };
  329. static int ntb_netdev_probe(struct device *client_dev)
  330. {
  331. struct ntb_dev *ntb;
  332. struct net_device *ndev;
  333. struct pci_dev *pdev;
  334. struct ntb_netdev *dev;
  335. int rc;
  336. ntb = dev_ntb(client_dev->parent);
  337. pdev = ntb->pdev;
  338. if (!pdev)
  339. return -ENODEV;
  340. ndev = alloc_etherdev(sizeof(*dev));
  341. if (!ndev)
  342. return -ENOMEM;
  343. SET_NETDEV_DEV(ndev, client_dev);
  344. dev = netdev_priv(ndev);
  345. dev->ndev = ndev;
  346. dev->pdev = pdev;
  347. ndev->features = NETIF_F_HIGHDMA;
  348. ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  349. ndev->hw_features = ndev->features;
  350. ndev->watchdog_timeo = msecs_to_jiffies(NTB_TX_TIMEOUT_MS);
  351. eth_random_addr(ndev->perm_addr);
  352. memcpy(ndev->dev_addr, ndev->perm_addr, ndev->addr_len);
  353. ndev->netdev_ops = &ntb_netdev_ops;
  354. ndev->ethtool_ops = &ntb_ethtool_ops;
  355. ndev->min_mtu = 0;
  356. ndev->max_mtu = ETH_MAX_MTU;
  357. dev->qp = ntb_transport_create_queue(ndev, client_dev,
  358. &ntb_netdev_handlers);
  359. if (!dev->qp) {
  360. rc = -EIO;
  361. goto err;
  362. }
  363. ndev->mtu = ntb_transport_max_size(dev->qp) - ETH_HLEN;
  364. rc = register_netdev(ndev);
  365. if (rc)
  366. goto err1;
  367. dev_set_drvdata(client_dev, ndev);
  368. dev_info(&pdev->dev, "%s created\n", ndev->name);
  369. return 0;
  370. err1:
  371. ntb_transport_free_queue(dev->qp);
  372. err:
  373. free_netdev(ndev);
  374. return rc;
  375. }
  376. static void ntb_netdev_remove(struct device *client_dev)
  377. {
  378. struct net_device *ndev = dev_get_drvdata(client_dev);
  379. struct ntb_netdev *dev = netdev_priv(ndev);
  380. unregister_netdev(ndev);
  381. ntb_transport_free_queue(dev->qp);
  382. free_netdev(ndev);
  383. }
  384. static struct ntb_transport_client ntb_netdev_client = {
  385. .driver.name = KBUILD_MODNAME,
  386. .driver.owner = THIS_MODULE,
  387. .probe = ntb_netdev_probe,
  388. .remove = ntb_netdev_remove,
  389. };
  390. static int __init ntb_netdev_init_module(void)
  391. {
  392. int rc;
  393. rc = ntb_transport_register_client_dev(KBUILD_MODNAME);
  394. if (rc)
  395. return rc;
  396. return ntb_transport_register_client(&ntb_netdev_client);
  397. }
  398. module_init(ntb_netdev_init_module);
  399. static void __exit ntb_netdev_exit_module(void)
  400. {
  401. ntb_transport_unregister_client(&ntb_netdev_client);
  402. ntb_transport_unregister_client_dev(KBUILD_MODNAME);
  403. }
  404. module_exit(ntb_netdev_exit_module);