ntb_netdev.c 12 KB

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