ntb_netdev.c 9.7 KB

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