ntb_netdev.c 10.0 KB

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