PageRenderTime 174ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/staging/hv/netvsc_drv.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2
C | 490 lines | 316 code | 90 blank | 84 comment | 34 complexity | 461a76540d795b25e1111178b734daef MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * Copyright (c) 2009, Microsoft Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  15. * Place - Suite 330, Boston, MA 02111-1307 USA.
  16. *
  17. * Authors:
  18. * Haiyang Zhang <haiyangz@microsoft.com>
  19. * Hank Janssen <hjanssen@microsoft.com>
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/highmem.h>
  25. #include <linux/device.h>
  26. #include <linux/io.h>
  27. #include <linux/delay.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/inetdevice.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/skbuff.h>
  32. #include <linux/in.h>
  33. #include <linux/slab.h>
  34. #include <linux/dmi.h>
  35. #include <linux/pci.h>
  36. #include <net/arp.h>
  37. #include <net/route.h>
  38. #include <net/sock.h>
  39. #include <net/pkt_sched.h>
  40. #include "hyperv.h"
  41. #include "hyperv_net.h"
  42. struct net_device_context {
  43. /* point back to our device context */
  44. struct hv_device *device_ctx;
  45. unsigned long avail;
  46. struct work_struct work;
  47. };
  48. #define PACKET_PAGES_LOWATER 8
  49. /* Need this many pages to handle worst case fragmented packet */
  50. #define PACKET_PAGES_HIWATER (MAX_SKB_FRAGS + 2)
  51. static int ring_size = 128;
  52. module_param(ring_size, int, S_IRUGO);
  53. MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
  54. /* no-op so the netdev core doesn't return -EINVAL when modifying the the
  55. * multicast address list in SIOCADDMULTI. hv is setup to get all multicast
  56. * when it calls RndisFilterOnOpen() */
  57. static void netvsc_set_multicast_list(struct net_device *net)
  58. {
  59. }
  60. static int netvsc_open(struct net_device *net)
  61. {
  62. struct net_device_context *net_device_ctx = netdev_priv(net);
  63. struct hv_device *device_obj = net_device_ctx->device_ctx;
  64. int ret = 0;
  65. if (netif_carrier_ok(net)) {
  66. /* Open up the device */
  67. ret = rndis_filter_open(device_obj);
  68. if (ret != 0) {
  69. netdev_err(net, "unable to open device (ret %d).\n",
  70. ret);
  71. return ret;
  72. }
  73. netif_start_queue(net);
  74. } else {
  75. netdev_err(net, "unable to open device...link is down.\n");
  76. }
  77. return ret;
  78. }
  79. static int netvsc_close(struct net_device *net)
  80. {
  81. struct net_device_context *net_device_ctx = netdev_priv(net);
  82. struct hv_device *device_obj = net_device_ctx->device_ctx;
  83. int ret;
  84. netif_stop_queue(net);
  85. ret = rndis_filter_close(device_obj);
  86. if (ret != 0)
  87. netdev_err(net, "unable to close device (ret %d).\n", ret);
  88. return ret;
  89. }
  90. static void netvsc_xmit_completion(void *context)
  91. {
  92. struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
  93. struct sk_buff *skb = (struct sk_buff *)
  94. (unsigned long)packet->completion.send.send_completion_tid;
  95. kfree(packet);
  96. if (skb) {
  97. struct net_device *net = skb->dev;
  98. struct net_device_context *net_device_ctx = netdev_priv(net);
  99. unsigned int num_pages = skb_shinfo(skb)->nr_frags + 2;
  100. dev_kfree_skb_any(skb);
  101. net_device_ctx->avail += num_pages;
  102. if (net_device_ctx->avail >= PACKET_PAGES_HIWATER)
  103. netif_wake_queue(net);
  104. }
  105. }
  106. static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
  107. {
  108. struct net_device_context *net_device_ctx = netdev_priv(net);
  109. struct hv_netvsc_packet *packet;
  110. int ret;
  111. unsigned int i, num_pages;
  112. /* Add 1 for skb->data and additional one for RNDIS */
  113. num_pages = skb_shinfo(skb)->nr_frags + 1 + 1;
  114. if (num_pages > net_device_ctx->avail)
  115. return NETDEV_TX_BUSY;
  116. /* Allocate a netvsc packet based on # of frags. */
  117. packet = kzalloc(sizeof(struct hv_netvsc_packet) +
  118. (num_pages * sizeof(struct hv_page_buffer)) +
  119. sizeof(struct rndis_filter_packet), GFP_ATOMIC);
  120. if (!packet) {
  121. /* out of memory, silently drop packet */
  122. netdev_err(net, "unable to allocate hv_netvsc_packet\n");
  123. dev_kfree_skb(skb);
  124. net->stats.tx_dropped++;
  125. return NETDEV_TX_OK;
  126. }
  127. packet->extension = (void *)(unsigned long)packet +
  128. sizeof(struct hv_netvsc_packet) +
  129. (num_pages * sizeof(struct hv_page_buffer));
  130. /* Setup the rndis header */
  131. packet->page_buf_cnt = num_pages;
  132. /* TODO: Flush all write buffers/ memory fence ??? */
  133. /* wmb(); */
  134. /* Initialize it from the skb */
  135. packet->total_data_buflen = skb->len;
  136. /* Start filling in the page buffers starting after RNDIS buffer. */
  137. packet->page_buf[1].pfn = virt_to_phys(skb->data) >> PAGE_SHIFT;
  138. packet->page_buf[1].offset
  139. = (unsigned long)skb->data & (PAGE_SIZE - 1);
  140. packet->page_buf[1].len = skb_headlen(skb);
  141. /* Additional fragments are after SKB data */
  142. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  143. skb_frag_t *f = &skb_shinfo(skb)->frags[i];
  144. packet->page_buf[i+2].pfn = page_to_pfn(f->page);
  145. packet->page_buf[i+2].offset = f->page_offset;
  146. packet->page_buf[i+2].len = f->size;
  147. }
  148. /* Set the completion routine */
  149. packet->completion.send.send_completion = netvsc_xmit_completion;
  150. packet->completion.send.send_completion_ctx = packet;
  151. packet->completion.send.send_completion_tid = (unsigned long)skb;
  152. ret = rndis_filter_send(net_device_ctx->device_ctx,
  153. packet);
  154. if (ret == 0) {
  155. net->stats.tx_bytes += skb->len;
  156. net->stats.tx_packets++;
  157. net_device_ctx->avail -= num_pages;
  158. if (net_device_ctx->avail < PACKET_PAGES_LOWATER)
  159. netif_stop_queue(net);
  160. } else {
  161. /* we are shutting down or bus overloaded, just drop packet */
  162. net->stats.tx_dropped++;
  163. netvsc_xmit_completion(packet);
  164. }
  165. return NETDEV_TX_OK;
  166. }
  167. /*
  168. * netvsc_linkstatus_callback - Link up/down notification
  169. */
  170. void netvsc_linkstatus_callback(struct hv_device *device_obj,
  171. unsigned int status)
  172. {
  173. struct net_device *net = dev_get_drvdata(&device_obj->device);
  174. struct net_device_context *ndev_ctx;
  175. if (!net) {
  176. netdev_err(net, "got link status but net device "
  177. "not initialized yet\n");
  178. return;
  179. }
  180. if (status == 1) {
  181. netif_carrier_on(net);
  182. netif_wake_queue(net);
  183. netif_notify_peers(net);
  184. ndev_ctx = netdev_priv(net);
  185. schedule_work(&ndev_ctx->work);
  186. } else {
  187. netif_carrier_off(net);
  188. netif_stop_queue(net);
  189. }
  190. }
  191. /*
  192. * netvsc_recv_callback - Callback when we receive a packet from the
  193. * "wire" on the specified device.
  194. */
  195. int netvsc_recv_callback(struct hv_device *device_obj,
  196. struct hv_netvsc_packet *packet)
  197. {
  198. struct net_device *net = dev_get_drvdata(&device_obj->device);
  199. struct sk_buff *skb;
  200. void *data;
  201. int i;
  202. unsigned long flags;
  203. if (!net) {
  204. netdev_err(net, "got receive callback but net device"
  205. " not initialized yet\n");
  206. return 0;
  207. }
  208. /* Allocate a skb - TODO direct I/O to pages? */
  209. skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
  210. if (unlikely(!skb)) {
  211. ++net->stats.rx_dropped;
  212. return 0;
  213. }
  214. /* for kmap_atomic */
  215. local_irq_save(flags);
  216. /*
  217. * Copy to skb. This copy is needed here since the memory pointed by
  218. * hv_netvsc_packet cannot be deallocated
  219. */
  220. for (i = 0; i < packet->page_buf_cnt; i++) {
  221. data = kmap_atomic(pfn_to_page(packet->page_buf[i].pfn),
  222. KM_IRQ1);
  223. data = (void *)(unsigned long)data +
  224. packet->page_buf[i].offset;
  225. memcpy(skb_put(skb, packet->page_buf[i].len), data,
  226. packet->page_buf[i].len);
  227. kunmap_atomic((void *)((unsigned long)data -
  228. packet->page_buf[i].offset), KM_IRQ1);
  229. }
  230. local_irq_restore(flags);
  231. skb->protocol = eth_type_trans(skb, net);
  232. skb->ip_summed = CHECKSUM_NONE;
  233. net->stats.rx_packets++;
  234. net->stats.rx_bytes += skb->len;
  235. /*
  236. * Pass the skb back up. Network stack will deallocate the skb when it
  237. * is done.
  238. * TODO - use NAPI?
  239. */
  240. netif_rx(skb);
  241. return 0;
  242. }
  243. static void netvsc_get_drvinfo(struct net_device *net,
  244. struct ethtool_drvinfo *info)
  245. {
  246. strcpy(info->driver, "hv_netvsc");
  247. strcpy(info->version, HV_DRV_VERSION);
  248. strcpy(info->fw_version, "N/A");
  249. }
  250. static const struct ethtool_ops ethtool_ops = {
  251. .get_drvinfo = netvsc_get_drvinfo,
  252. .get_link = ethtool_op_get_link,
  253. };
  254. static const struct net_device_ops device_ops = {
  255. .ndo_open = netvsc_open,
  256. .ndo_stop = netvsc_close,
  257. .ndo_start_xmit = netvsc_start_xmit,
  258. .ndo_set_multicast_list = netvsc_set_multicast_list,
  259. .ndo_change_mtu = eth_change_mtu,
  260. .ndo_validate_addr = eth_validate_addr,
  261. .ndo_set_mac_address = eth_mac_addr,
  262. };
  263. /*
  264. * Send GARP packet to network peers after migrations.
  265. * After Quick Migration, the network is not immediately operational in the
  266. * current context when receiving RNDIS_STATUS_MEDIA_CONNECT event. So, add
  267. * another netif_notify_peers() into a scheduled work, otherwise GARP packet
  268. * will not be sent after quick migration, and cause network disconnection.
  269. */
  270. static void netvsc_send_garp(struct work_struct *w)
  271. {
  272. struct net_device_context *ndev_ctx;
  273. struct net_device *net;
  274. msleep(20);
  275. ndev_ctx = container_of(w, struct net_device_context, work);
  276. net = dev_get_drvdata(&ndev_ctx->device_ctx->device);
  277. netif_notify_peers(net);
  278. }
  279. static int netvsc_probe(struct hv_device *dev)
  280. {
  281. struct net_device *net = NULL;
  282. struct net_device_context *net_device_ctx;
  283. struct netvsc_device_info device_info;
  284. int ret;
  285. net = alloc_etherdev(sizeof(struct net_device_context));
  286. if (!net)
  287. return -1;
  288. /* Set initial state */
  289. netif_carrier_off(net);
  290. net_device_ctx = netdev_priv(net);
  291. net_device_ctx->device_ctx = dev;
  292. net_device_ctx->avail = ring_size;
  293. dev_set_drvdata(&dev->device, net);
  294. INIT_WORK(&net_device_ctx->work, netvsc_send_garp);
  295. /* Notify the netvsc driver of the new device */
  296. device_info.ring_size = ring_size;
  297. ret = rndis_filte_device_add(dev, &device_info);
  298. if (ret != 0) {
  299. free_netdev(net);
  300. dev_set_drvdata(&dev->device, NULL);
  301. netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
  302. return ret;
  303. }
  304. /*
  305. * If carrier is still off ie we did not get a link status callback,
  306. * update it if necessary
  307. */
  308. /*
  309. * FIXME: We should use a atomic or test/set instead to avoid getting
  310. * out of sync with the device's link status
  311. */
  312. if (!netif_carrier_ok(net))
  313. if (!device_info.link_state)
  314. netif_carrier_on(net);
  315. memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
  316. net->netdev_ops = &device_ops;
  317. /* TODO: Add GSO and Checksum offload */
  318. net->hw_features = NETIF_F_SG;
  319. net->features = NETIF_F_SG;
  320. SET_ETHTOOL_OPS(net, &ethtool_ops);
  321. SET_NETDEV_DEV(net, &dev->device);
  322. ret = register_netdev(net);
  323. if (ret != 0) {
  324. /* Remove the device and release the resource */
  325. rndis_filter_device_remove(dev);
  326. free_netdev(net);
  327. }
  328. return ret;
  329. }
  330. static int netvsc_remove(struct hv_device *dev)
  331. {
  332. struct net_device *net = dev_get_drvdata(&dev->device);
  333. int ret;
  334. if (net == NULL) {
  335. dev_err(&dev->device, "No net device to remove\n");
  336. return 0;
  337. }
  338. /* Stop outbound asap */
  339. netif_stop_queue(net);
  340. /* netif_carrier_off(net); */
  341. unregister_netdev(net);
  342. /*
  343. * Call to the vsc driver to let it know that the device is being
  344. * removed
  345. */
  346. ret = rndis_filter_device_remove(dev);
  347. if (ret != 0) {
  348. /* TODO: */
  349. netdev_err(net, "unable to remove vsc device (ret %d)\n", ret);
  350. }
  351. free_netdev(net);
  352. return ret;
  353. }
  354. /* The one and only one */
  355. static struct hv_driver netvsc_drv = {
  356. .probe = netvsc_probe,
  357. .remove = netvsc_remove,
  358. };
  359. static void __exit netvsc_drv_exit(void)
  360. {
  361. vmbus_child_driver_unregister(&netvsc_drv.driver);
  362. }
  363. static const struct dmi_system_id __initconst
  364. hv_netvsc_dmi_table[] __maybe_unused = {
  365. {
  366. .ident = "Hyper-V",
  367. .matches = {
  368. DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
  369. DMI_MATCH(DMI_PRODUCT_NAME, "Virtual Machine"),
  370. DMI_MATCH(DMI_BOARD_NAME, "Virtual Machine"),
  371. },
  372. },
  373. { },
  374. };
  375. MODULE_DEVICE_TABLE(dmi, hv_netvsc_dmi_table);
  376. static int __init netvsc_drv_init(void)
  377. {
  378. struct hv_driver *drv = &netvsc_drv;
  379. int ret;
  380. pr_info("initializing....");
  381. if (!dmi_check_system(hv_netvsc_dmi_table))
  382. return -ENODEV;
  383. /* Callback to client driver to complete the initialization */
  384. netvsc_initialize(drv);
  385. drv->driver.name = drv->name;
  386. /* The driver belongs to vmbus */
  387. ret = vmbus_child_driver_register(&drv->driver);
  388. return ret;
  389. }
  390. static const struct pci_device_id __initconst
  391. hv_netvsc_pci_table[] __maybe_unused = {
  392. { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
  393. { 0 }
  394. };
  395. MODULE_DEVICE_TABLE(pci, hv_netvsc_pci_table);
  396. MODULE_LICENSE("GPL");
  397. MODULE_VERSION(HV_DRV_VERSION);
  398. MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
  399. module_init(netvsc_drv_init);
  400. module_exit(netvsc_drv_exit);