ntb_netdev.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. switch (status) {
  74. case NTB_LINK_DOWN:
  75. netif_carrier_off(ndev);
  76. break;
  77. case NTB_LINK_UP:
  78. if (!ntb_transport_link_query(dev->qp))
  79. return;
  80. netif_carrier_on(ndev);
  81. break;
  82. default:
  83. netdev_warn(ndev, "Unsupported event type %d\n", status);
  84. }
  85. }
  86. static void ntb_netdev_rx_handler(struct ntb_transport_qp *qp, void *qp_data,
  87. void *data, int len)
  88. {
  89. struct net_device *ndev = qp_data;
  90. struct sk_buff *skb;
  91. int rc;
  92. skb = data;
  93. if (!skb)
  94. return;
  95. netdev_dbg(ndev, "%s: %d byte payload received\n", __func__, len);
  96. skb_put(skb, len);
  97. skb->protocol = eth_type_trans(skb, ndev);
  98. skb->ip_summed = CHECKSUM_NONE;
  99. if (netif_rx(skb) == NET_RX_DROP) {
  100. ndev->stats.rx_errors++;
  101. ndev->stats.rx_dropped++;
  102. } else {
  103. ndev->stats.rx_packets++;
  104. ndev->stats.rx_bytes += len;
  105. }
  106. skb = netdev_alloc_skb(ndev, ndev->mtu + ETH_HLEN);
  107. if (!skb) {
  108. ndev->stats.rx_errors++;
  109. ndev->stats.rx_frame_errors++;
  110. return;
  111. }
  112. rc = ntb_transport_rx_enqueue(qp, skb, skb->data, ndev->mtu + ETH_HLEN);
  113. if (rc) {
  114. dev_kfree_skb(skb);
  115. ndev->stats.rx_errors++;
  116. ndev->stats.rx_fifo_errors++;
  117. }
  118. }
  119. static void ntb_netdev_tx_handler(struct ntb_transport_qp *qp, void *qp_data,
  120. void *data, int len)
  121. {
  122. struct net_device *ndev = qp_data;
  123. struct sk_buff *skb;
  124. skb = data;
  125. if (!skb || !ndev)
  126. return;
  127. if (len > 0) {
  128. ndev->stats.tx_packets++;
  129. ndev->stats.tx_bytes += skb->len;
  130. } else {
  131. ndev->stats.tx_errors++;
  132. ndev->stats.tx_aborted_errors++;
  133. }
  134. dev_kfree_skb(skb);
  135. }
  136. static netdev_tx_t ntb_netdev_start_xmit(struct sk_buff *skb,
  137. struct net_device *ndev)
  138. {
  139. struct ntb_netdev *dev = netdev_priv(ndev);
  140. int rc;
  141. netdev_dbg(ndev, "%s: skb len %d\n", __func__, skb->len);
  142. rc = ntb_transport_tx_enqueue(dev->qp, skb, skb->data, skb->len);
  143. if (rc)
  144. goto err;
  145. return NETDEV_TX_OK;
  146. err:
  147. ndev->stats.tx_dropped++;
  148. ndev->stats.tx_errors++;
  149. return NETDEV_TX_BUSY;
  150. }
  151. static int ntb_netdev_open(struct net_device *ndev)
  152. {
  153. struct ntb_netdev *dev = netdev_priv(ndev);
  154. struct sk_buff *skb;
  155. int rc, i, len;
  156. /* Add some empty rx bufs */
  157. for (i = 0; i < NTB_RXQ_SIZE; i++) {
  158. skb = netdev_alloc_skb(ndev, ndev->mtu + ETH_HLEN);
  159. if (!skb) {
  160. rc = -ENOMEM;
  161. goto err;
  162. }
  163. rc = ntb_transport_rx_enqueue(dev->qp, skb, skb->data,
  164. ndev->mtu + ETH_HLEN);
  165. if (rc == -EINVAL) {
  166. dev_kfree_skb(skb);
  167. goto err;
  168. }
  169. }
  170. netif_carrier_off(ndev);
  171. ntb_transport_link_up(dev->qp);
  172. return 0;
  173. err:
  174. while ((skb = ntb_transport_rx_remove(dev->qp, &len)))
  175. dev_kfree_skb(skb);
  176. return rc;
  177. }
  178. static int ntb_netdev_close(struct net_device *ndev)
  179. {
  180. struct ntb_netdev *dev = netdev_priv(ndev);
  181. struct sk_buff *skb;
  182. int len;
  183. ntb_transport_link_down(dev->qp);
  184. while ((skb = ntb_transport_rx_remove(dev->qp, &len)))
  185. dev_kfree_skb(skb);
  186. return 0;
  187. }
  188. static int ntb_netdev_change_mtu(struct net_device *ndev, int new_mtu)
  189. {
  190. struct ntb_netdev *dev = netdev_priv(ndev);
  191. struct sk_buff *skb;
  192. int len, rc;
  193. if (new_mtu > ntb_transport_max_size(dev->qp) - ETH_HLEN)
  194. return -EINVAL;
  195. if (!netif_running(ndev)) {
  196. ndev->mtu = new_mtu;
  197. return 0;
  198. }
  199. /* Bring down the link and dispose of posted rx entries */
  200. ntb_transport_link_down(dev->qp);
  201. if (ndev->mtu < new_mtu) {
  202. int i;
  203. for (i = 0; (skb = ntb_transport_rx_remove(dev->qp, &len)); i++)
  204. dev_kfree_skb(skb);
  205. for (; i; i--) {
  206. skb = netdev_alloc_skb(ndev, new_mtu + ETH_HLEN);
  207. if (!skb) {
  208. rc = -ENOMEM;
  209. goto err;
  210. }
  211. rc = ntb_transport_rx_enqueue(dev->qp, skb, skb->data,
  212. new_mtu + ETH_HLEN);
  213. if (rc) {
  214. dev_kfree_skb(skb);
  215. goto err;
  216. }
  217. }
  218. }
  219. ndev->mtu = new_mtu;
  220. ntb_transport_link_up(dev->qp);
  221. return 0;
  222. err:
  223. ntb_transport_link_down(dev->qp);
  224. while ((skb = ntb_transport_rx_remove(dev->qp, &len)))
  225. dev_kfree_skb(skb);
  226. netdev_err(ndev, "Error changing MTU, device inoperable\n");
  227. return rc;
  228. }
  229. static const struct net_device_ops ntb_netdev_ops = {
  230. .ndo_open = ntb_netdev_open,
  231. .ndo_stop = ntb_netdev_close,
  232. .ndo_start_xmit = ntb_netdev_start_xmit,
  233. .ndo_change_mtu = ntb_netdev_change_mtu,
  234. .ndo_set_mac_address = eth_mac_addr,
  235. };
  236. static void ntb_get_drvinfo(struct net_device *ndev,
  237. struct ethtool_drvinfo *info)
  238. {
  239. struct ntb_netdev *dev = netdev_priv(ndev);
  240. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  241. strlcpy(info->version, NTB_NETDEV_VER, sizeof(info->version));
  242. strlcpy(info->bus_info, pci_name(dev->pdev), sizeof(info->bus_info));
  243. }
  244. static int ntb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  245. {
  246. cmd->supported = SUPPORTED_Backplane;
  247. cmd->advertising = ADVERTISED_Backplane;
  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. ndev->ethtool_ops = &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. dev_info(&pdev->dev, "%s created\n", 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. bool found = false;
  311. list_for_each_entry(dev, &dev_list, list) {
  312. if (dev->pdev == pdev) {
  313. found = true;
  314. break;
  315. }
  316. }
  317. if (!found)
  318. return;
  319. list_del(&dev->list);
  320. ndev = dev->ndev;
  321. unregister_netdev(ndev);
  322. ntb_transport_free_queue(dev->qp);
  323. free_netdev(ndev);
  324. }
  325. static struct ntb_client ntb_netdev_client = {
  326. .driver.name = KBUILD_MODNAME,
  327. .driver.owner = THIS_MODULE,
  328. .probe = ntb_netdev_probe,
  329. .remove = ntb_netdev_remove,
  330. };
  331. static int __init ntb_netdev_init_module(void)
  332. {
  333. int rc;
  334. rc = ntb_register_client_dev(KBUILD_MODNAME);
  335. if (rc)
  336. return rc;
  337. return ntb_register_client(&ntb_netdev_client);
  338. }
  339. module_init(ntb_netdev_init_module);
  340. static void __exit ntb_netdev_exit_module(void)
  341. {
  342. ntb_unregister_client(&ntb_netdev_client);
  343. ntb_unregister_client_dev(KBUILD_MODNAME);
  344. }
  345. module_exit(ntb_netdev_exit_module);