netvsc_drv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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/atomic.h>
  24. #include <linux/module.h>
  25. #include <linux/highmem.h>
  26. #include <linux/device.h>
  27. #include <linux/io.h>
  28. #include <linux/delay.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/inetdevice.h>
  31. #include <linux/etherdevice.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/in.h>
  34. #include <linux/slab.h>
  35. #include <net/arp.h>
  36. #include <net/route.h>
  37. #include <net/sock.h>
  38. #include <net/pkt_sched.h>
  39. #include "hyperv_net.h"
  40. struct net_device_context {
  41. /* point back to our device context */
  42. struct hv_device *device_ctx;
  43. atomic_t avail;
  44. struct delayed_work dwork;
  45. };
  46. #define PACKET_PAGES_LOWATER 8
  47. /* Need this many pages to handle worst case fragmented packet */
  48. #define PACKET_PAGES_HIWATER (MAX_SKB_FRAGS + 2)
  49. static int ring_size = 128;
  50. module_param(ring_size, int, S_IRUGO);
  51. MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
  52. /* no-op so the netdev core doesn't return -EINVAL when modifying the the
  53. * multicast address list in SIOCADDMULTI. hv is setup to get all multicast
  54. * when it calls RndisFilterOnOpen() */
  55. static void netvsc_set_multicast_list(struct net_device *net)
  56. {
  57. }
  58. static int netvsc_open(struct net_device *net)
  59. {
  60. struct net_device_context *net_device_ctx = netdev_priv(net);
  61. struct hv_device *device_obj = net_device_ctx->device_ctx;
  62. int ret = 0;
  63. /* Open up the device */
  64. ret = rndis_filter_open(device_obj);
  65. if (ret != 0) {
  66. netdev_err(net, "unable to open device (ret %d).\n", ret);
  67. return ret;
  68. }
  69. netif_start_queue(net);
  70. return ret;
  71. }
  72. static int netvsc_close(struct net_device *net)
  73. {
  74. struct net_device_context *net_device_ctx = netdev_priv(net);
  75. struct hv_device *device_obj = net_device_ctx->device_ctx;
  76. int ret;
  77. netif_stop_queue(net);
  78. ret = rndis_filter_close(device_obj);
  79. if (ret != 0)
  80. netdev_err(net, "unable to close device (ret %d).\n", ret);
  81. return ret;
  82. }
  83. static void netvsc_xmit_completion(void *context)
  84. {
  85. struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
  86. struct sk_buff *skb = (struct sk_buff *)
  87. (unsigned long)packet->completion.send.send_completion_tid;
  88. kfree(packet);
  89. if (skb) {
  90. struct net_device *net = skb->dev;
  91. struct net_device_context *net_device_ctx = netdev_priv(net);
  92. unsigned int num_pages = skb_shinfo(skb)->nr_frags + 2;
  93. dev_kfree_skb_any(skb);
  94. atomic_add(num_pages, &net_device_ctx->avail);
  95. if (atomic_read(&net_device_ctx->avail) >=
  96. PACKET_PAGES_HIWATER)
  97. netif_wake_queue(net);
  98. }
  99. }
  100. static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
  101. {
  102. struct net_device_context *net_device_ctx = netdev_priv(net);
  103. struct hv_netvsc_packet *packet;
  104. int ret;
  105. unsigned int i, num_pages;
  106. /* Add 1 for skb->data and additional one for RNDIS */
  107. num_pages = skb_shinfo(skb)->nr_frags + 1 + 1;
  108. if (num_pages > atomic_read(&net_device_ctx->avail))
  109. return NETDEV_TX_BUSY;
  110. /* Allocate a netvsc packet based on # of frags. */
  111. packet = kzalloc(sizeof(struct hv_netvsc_packet) +
  112. (num_pages * sizeof(struct hv_page_buffer)) +
  113. sizeof(struct rndis_filter_packet), GFP_ATOMIC);
  114. if (!packet) {
  115. /* out of memory, drop packet */
  116. netdev_err(net, "unable to allocate hv_netvsc_packet\n");
  117. dev_kfree_skb(skb);
  118. net->stats.tx_dropped++;
  119. return NETDEV_TX_BUSY;
  120. }
  121. packet->extension = (void *)(unsigned long)packet +
  122. sizeof(struct hv_netvsc_packet) +
  123. (num_pages * sizeof(struct hv_page_buffer));
  124. /* Setup the rndis header */
  125. packet->page_buf_cnt = num_pages;
  126. /* Initialize it from the skb */
  127. packet->total_data_buflen = skb->len;
  128. /* Start filling in the page buffers starting after RNDIS buffer. */
  129. packet->page_buf[1].pfn = virt_to_phys(skb->data) >> PAGE_SHIFT;
  130. packet->page_buf[1].offset
  131. = (unsigned long)skb->data & (PAGE_SIZE - 1);
  132. packet->page_buf[1].len = skb_headlen(skb);
  133. /* Additional fragments are after SKB data */
  134. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  135. const skb_frag_t *f = &skb_shinfo(skb)->frags[i];
  136. packet->page_buf[i+2].pfn = page_to_pfn(skb_frag_page(f));
  137. packet->page_buf[i+2].offset = f->page_offset;
  138. packet->page_buf[i+2].len = skb_frag_size(f);
  139. }
  140. /* Set the completion routine */
  141. packet->completion.send.send_completion = netvsc_xmit_completion;
  142. packet->completion.send.send_completion_ctx = packet;
  143. packet->completion.send.send_completion_tid = (unsigned long)skb;
  144. ret = rndis_filter_send(net_device_ctx->device_ctx,
  145. packet);
  146. if (ret == 0) {
  147. net->stats.tx_bytes += skb->len;
  148. net->stats.tx_packets++;
  149. atomic_sub(num_pages, &net_device_ctx->avail);
  150. if (atomic_read(&net_device_ctx->avail) < PACKET_PAGES_LOWATER)
  151. netif_stop_queue(net);
  152. } else {
  153. /* we are shutting down or bus overloaded, just drop packet */
  154. net->stats.tx_dropped++;
  155. kfree(packet);
  156. dev_kfree_skb_any(skb);
  157. }
  158. return ret ? NETDEV_TX_BUSY : NETDEV_TX_OK;
  159. }
  160. /*
  161. * netvsc_linkstatus_callback - Link up/down notification
  162. */
  163. void netvsc_linkstatus_callback(struct hv_device *device_obj,
  164. unsigned int status)
  165. {
  166. struct net_device *net;
  167. struct net_device_context *ndev_ctx;
  168. struct netvsc_device *net_device;
  169. net_device = hv_get_drvdata(device_obj);
  170. net = net_device->ndev;
  171. if (!net) {
  172. netdev_err(net, "got link status but net device "
  173. "not initialized yet\n");
  174. return;
  175. }
  176. if (status == 1) {
  177. netif_carrier_on(net);
  178. netif_wake_queue(net);
  179. ndev_ctx = netdev_priv(net);
  180. schedule_delayed_work(&ndev_ctx->dwork, 0);
  181. schedule_delayed_work(&ndev_ctx->dwork, msecs_to_jiffies(20));
  182. } else {
  183. netif_carrier_off(net);
  184. netif_stop_queue(net);
  185. }
  186. }
  187. /*
  188. * netvsc_recv_callback - Callback when we receive a packet from the
  189. * "wire" on the specified device.
  190. */
  191. int netvsc_recv_callback(struct hv_device *device_obj,
  192. struct hv_netvsc_packet *packet)
  193. {
  194. struct net_device *net = dev_get_drvdata(&device_obj->device);
  195. struct sk_buff *skb;
  196. void *data;
  197. int i;
  198. unsigned long flags;
  199. struct netvsc_device *net_device;
  200. net_device = hv_get_drvdata(device_obj);
  201. net = net_device->ndev;
  202. if (!net) {
  203. netdev_err(net, "got receive callback but net device"
  204. " not initialized yet\n");
  205. return 0;
  206. }
  207. /* Allocate a skb - TODO direct I/O to pages? */
  208. skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
  209. if (unlikely(!skb)) {
  210. ++net->stats.rx_dropped;
  211. return 0;
  212. }
  213. /* for kmap_atomic */
  214. local_irq_save(flags);
  215. /*
  216. * Copy to skb. This copy is needed here since the memory pointed by
  217. * hv_netvsc_packet cannot be deallocated
  218. */
  219. for (i = 0; i < packet->page_buf_cnt; i++) {
  220. data = kmap_atomic(pfn_to_page(packet->page_buf[i].pfn),
  221. KM_IRQ1);
  222. data = (void *)(unsigned long)data +
  223. packet->page_buf[i].offset;
  224. memcpy(skb_put(skb, packet->page_buf[i].len), data,
  225. packet->page_buf[i].len);
  226. kunmap_atomic((void *)((unsigned long)data -
  227. packet->page_buf[i].offset), KM_IRQ1);
  228. }
  229. local_irq_restore(flags);
  230. skb->protocol = eth_type_trans(skb, net);
  231. skb->ip_summed = CHECKSUM_NONE;
  232. net->stats.rx_packets++;
  233. net->stats.rx_bytes += skb->len;
  234. /*
  235. * Pass the skb back up. Network stack will deallocate the skb when it
  236. * is done.
  237. * TODO - use NAPI?
  238. */
  239. netif_rx(skb);
  240. return 0;
  241. }
  242. static void netvsc_get_drvinfo(struct net_device *net,
  243. struct ethtool_drvinfo *info)
  244. {
  245. strcpy(info->driver, "hv_netvsc");
  246. strcpy(info->version, HV_DRV_VERSION);
  247. strcpy(info->fw_version, "N/A");
  248. }
  249. static const struct ethtool_ops ethtool_ops = {
  250. .get_drvinfo = netvsc_get_drvinfo,
  251. .get_link = ethtool_op_get_link,
  252. };
  253. static const struct net_device_ops device_ops = {
  254. .ndo_open = netvsc_open,
  255. .ndo_stop = netvsc_close,
  256. .ndo_start_xmit = netvsc_start_xmit,
  257. .ndo_set_rx_mode = netvsc_set_multicast_list,
  258. .ndo_change_mtu = eth_change_mtu,
  259. .ndo_validate_addr = eth_validate_addr,
  260. .ndo_set_mac_address = eth_mac_addr,
  261. };
  262. /*
  263. * Send GARP packet to network peers after migrations.
  264. * After Quick Migration, the network is not immediately operational in the
  265. * current context when receiving RNDIS_STATUS_MEDIA_CONNECT event. So, add
  266. * another netif_notify_peers() into a delayed work, otherwise GARP packet
  267. * will not be sent after quick migration, and cause network disconnection.
  268. */
  269. static void netvsc_send_garp(struct work_struct *w)
  270. {
  271. struct net_device_context *ndev_ctx;
  272. struct net_device *net;
  273. struct netvsc_device *net_device;
  274. ndev_ctx = container_of(w, struct net_device_context, dwork.work);
  275. net_device = hv_get_drvdata(ndev_ctx->device_ctx);
  276. net = net_device->ndev;
  277. netif_notify_peers(net);
  278. }
  279. static int netvsc_probe(struct hv_device *dev,
  280. const struct hv_vmbus_device_id *dev_id)
  281. {
  282. struct net_device *net = NULL;
  283. struct net_device_context *net_device_ctx;
  284. struct netvsc_device_info device_info;
  285. int ret;
  286. net = alloc_etherdev(sizeof(struct net_device_context));
  287. if (!net)
  288. return -ENOMEM;
  289. /* Set initial state */
  290. netif_carrier_off(net);
  291. net_device_ctx = netdev_priv(net);
  292. net_device_ctx->device_ctx = dev;
  293. atomic_set(&net_device_ctx->avail, ring_size);
  294. hv_set_drvdata(dev, net);
  295. INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_send_garp);
  296. net->netdev_ops = &device_ops;
  297. /* TODO: Add GSO and Checksum offload */
  298. net->hw_features = NETIF_F_SG;
  299. net->features = NETIF_F_SG;
  300. SET_ETHTOOL_OPS(net, &ethtool_ops);
  301. SET_NETDEV_DEV(net, &dev->device);
  302. ret = register_netdev(net);
  303. if (ret != 0) {
  304. pr_err("Unable to register netdev.\n");
  305. free_netdev(net);
  306. goto out;
  307. }
  308. /* Notify the netvsc driver of the new device */
  309. device_info.ring_size = ring_size;
  310. ret = rndis_filter_device_add(dev, &device_info);
  311. if (ret != 0) {
  312. netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
  313. unregister_netdev(net);
  314. free_netdev(net);
  315. hv_set_drvdata(dev, NULL);
  316. return ret;
  317. }
  318. memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
  319. netif_carrier_on(net);
  320. out:
  321. return ret;
  322. }
  323. static int netvsc_remove(struct hv_device *dev)
  324. {
  325. struct net_device *net;
  326. struct net_device_context *ndev_ctx;
  327. struct netvsc_device *net_device;
  328. net_device = hv_get_drvdata(dev);
  329. net = net_device->ndev;
  330. if (net == NULL) {
  331. dev_err(&dev->device, "No net device to remove\n");
  332. return 0;
  333. }
  334. ndev_ctx = netdev_priv(net);
  335. cancel_delayed_work_sync(&ndev_ctx->dwork);
  336. /* Stop outbound asap */
  337. netif_stop_queue(net);
  338. unregister_netdev(net);
  339. /*
  340. * Call to the vsc driver to let it know that the device is being
  341. * removed
  342. */
  343. rndis_filter_device_remove(dev);
  344. free_netdev(net);
  345. return 0;
  346. }
  347. static const struct hv_vmbus_device_id id_table[] = {
  348. /* Network guid */
  349. { VMBUS_DEVICE(0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
  350. 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E) },
  351. { },
  352. };
  353. MODULE_DEVICE_TABLE(vmbus, id_table);
  354. /* The one and only one */
  355. static struct hv_driver netvsc_drv = {
  356. .name = "netvsc",
  357. .id_table = id_table,
  358. .probe = netvsc_probe,
  359. .remove = netvsc_remove,
  360. };
  361. static void __exit netvsc_drv_exit(void)
  362. {
  363. vmbus_driver_unregister(&netvsc_drv);
  364. }
  365. static int __init netvsc_drv_init(void)
  366. {
  367. return vmbus_driver_register(&netvsc_drv);
  368. }
  369. MODULE_LICENSE("GPL");
  370. MODULE_VERSION(HV_DRV_VERSION);
  371. MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
  372. module_init(netvsc_drv_init);
  373. module_exit(netvsc_drv_exit);