netvsc_drv.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  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, see <http://www.gnu.org/licenses/>.
  15. *
  16. * Authors:
  17. * Haiyang Zhang <haiyangz@microsoft.com>
  18. * Hank Janssen <hjanssen@microsoft.com>
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/init.h>
  22. #include <linux/atomic.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/if_vlan.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. #define RING_SIZE_MIN 64
  41. #define LINKCHANGE_INT (2 * HZ)
  42. #define NETVSC_HW_FEATURES (NETIF_F_RXCSUM | \
  43. NETIF_F_SG | \
  44. NETIF_F_TSO | \
  45. NETIF_F_TSO6 | \
  46. NETIF_F_HW_CSUM)
  47. static int ring_size = 128;
  48. module_param(ring_size, int, S_IRUGO);
  49. MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
  50. static int max_num_vrss_chns = 8;
  51. static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
  52. NETIF_MSG_LINK | NETIF_MSG_IFUP |
  53. NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
  54. NETIF_MSG_TX_ERR;
  55. static int debug = -1;
  56. module_param(debug, int, S_IRUGO);
  57. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  58. static void do_set_multicast(struct work_struct *w)
  59. {
  60. struct net_device_context *ndevctx =
  61. container_of(w, struct net_device_context, work);
  62. struct hv_device *device_obj = ndevctx->device_ctx;
  63. struct net_device *ndev = hv_get_drvdata(device_obj);
  64. struct netvsc_device *nvdev = ndevctx->nvdev;
  65. struct rndis_device *rdev;
  66. if (!nvdev)
  67. return;
  68. rdev = nvdev->extension;
  69. if (rdev == NULL)
  70. return;
  71. if (ndev->flags & IFF_PROMISC)
  72. rndis_filter_set_packet_filter(rdev,
  73. NDIS_PACKET_TYPE_PROMISCUOUS);
  74. else
  75. rndis_filter_set_packet_filter(rdev,
  76. NDIS_PACKET_TYPE_BROADCAST |
  77. NDIS_PACKET_TYPE_ALL_MULTICAST |
  78. NDIS_PACKET_TYPE_DIRECTED);
  79. }
  80. static void netvsc_set_multicast_list(struct net_device *net)
  81. {
  82. struct net_device_context *net_device_ctx = netdev_priv(net);
  83. schedule_work(&net_device_ctx->work);
  84. }
  85. static int netvsc_open(struct net_device *net)
  86. {
  87. struct netvsc_device *nvdev = net_device_to_netvsc_device(net);
  88. struct rndis_device *rdev;
  89. int ret = 0;
  90. netif_carrier_off(net);
  91. /* Open up the device */
  92. ret = rndis_filter_open(nvdev);
  93. if (ret != 0) {
  94. netdev_err(net, "unable to open device (ret %d).\n", ret);
  95. return ret;
  96. }
  97. netif_tx_wake_all_queues(net);
  98. rdev = nvdev->extension;
  99. if (!rdev->link_state)
  100. netif_carrier_on(net);
  101. return ret;
  102. }
  103. static int netvsc_close(struct net_device *net)
  104. {
  105. struct net_device_context *net_device_ctx = netdev_priv(net);
  106. struct netvsc_device *nvdev = net_device_ctx->nvdev;
  107. int ret;
  108. u32 aread, awrite, i, msec = 10, retry = 0, retry_max = 20;
  109. struct vmbus_channel *chn;
  110. netif_tx_disable(net);
  111. /* Make sure netvsc_set_multicast_list doesn't re-enable filter! */
  112. cancel_work_sync(&net_device_ctx->work);
  113. ret = rndis_filter_close(nvdev);
  114. if (ret != 0) {
  115. netdev_err(net, "unable to close device (ret %d).\n", ret);
  116. return ret;
  117. }
  118. /* Ensure pending bytes in ring are read */
  119. while (true) {
  120. aread = 0;
  121. for (i = 0; i < nvdev->num_chn; i++) {
  122. chn = nvdev->chn_table[i];
  123. if (!chn)
  124. continue;
  125. hv_get_ringbuffer_availbytes(&chn->inbound, &aread,
  126. &awrite);
  127. if (aread)
  128. break;
  129. hv_get_ringbuffer_availbytes(&chn->outbound, &aread,
  130. &awrite);
  131. if (aread)
  132. break;
  133. }
  134. retry++;
  135. if (retry > retry_max || aread == 0)
  136. break;
  137. msleep(msec);
  138. if (msec < 1000)
  139. msec *= 2;
  140. }
  141. if (aread) {
  142. netdev_err(net, "Ring buffer not empty after closing rndis\n");
  143. ret = -ETIMEDOUT;
  144. }
  145. return ret;
  146. }
  147. static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size,
  148. int pkt_type)
  149. {
  150. struct rndis_packet *rndis_pkt;
  151. struct rndis_per_packet_info *ppi;
  152. rndis_pkt = &msg->msg.pkt;
  153. rndis_pkt->data_offset += ppi_size;
  154. ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt +
  155. rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len);
  156. ppi->size = ppi_size;
  157. ppi->type = pkt_type;
  158. ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
  159. rndis_pkt->per_pkt_info_len += ppi_size;
  160. return ppi;
  161. }
  162. static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
  163. void *accel_priv, select_queue_fallback_t fallback)
  164. {
  165. struct net_device_context *net_device_ctx = netdev_priv(ndev);
  166. struct netvsc_device *nvsc_dev = net_device_ctx->nvdev;
  167. u32 hash;
  168. u16 q_idx = 0;
  169. if (nvsc_dev == NULL || ndev->real_num_tx_queues <= 1)
  170. return 0;
  171. hash = skb_get_hash(skb);
  172. q_idx = nvsc_dev->send_table[hash % VRSS_SEND_TAB_SIZE] %
  173. ndev->real_num_tx_queues;
  174. if (!nvsc_dev->chn_table[q_idx])
  175. q_idx = 0;
  176. return q_idx;
  177. }
  178. static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
  179. struct hv_page_buffer *pb)
  180. {
  181. int j = 0;
  182. /* Deal with compund pages by ignoring unused part
  183. * of the page.
  184. */
  185. page += (offset >> PAGE_SHIFT);
  186. offset &= ~PAGE_MASK;
  187. while (len > 0) {
  188. unsigned long bytes;
  189. bytes = PAGE_SIZE - offset;
  190. if (bytes > len)
  191. bytes = len;
  192. pb[j].pfn = page_to_pfn(page);
  193. pb[j].offset = offset;
  194. pb[j].len = bytes;
  195. offset += bytes;
  196. len -= bytes;
  197. if (offset == PAGE_SIZE && len) {
  198. page++;
  199. offset = 0;
  200. j++;
  201. }
  202. }
  203. return j + 1;
  204. }
  205. static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
  206. struct hv_netvsc_packet *packet,
  207. struct hv_page_buffer **page_buf)
  208. {
  209. struct hv_page_buffer *pb = *page_buf;
  210. u32 slots_used = 0;
  211. char *data = skb->data;
  212. int frags = skb_shinfo(skb)->nr_frags;
  213. int i;
  214. /* The packet is laid out thus:
  215. * 1. hdr: RNDIS header and PPI
  216. * 2. skb linear data
  217. * 3. skb fragment data
  218. */
  219. if (hdr != NULL)
  220. slots_used += fill_pg_buf(virt_to_page(hdr),
  221. offset_in_page(hdr),
  222. len, &pb[slots_used]);
  223. packet->rmsg_size = len;
  224. packet->rmsg_pgcnt = slots_used;
  225. slots_used += fill_pg_buf(virt_to_page(data),
  226. offset_in_page(data),
  227. skb_headlen(skb), &pb[slots_used]);
  228. for (i = 0; i < frags; i++) {
  229. skb_frag_t *frag = skb_shinfo(skb)->frags + i;
  230. slots_used += fill_pg_buf(skb_frag_page(frag),
  231. frag->page_offset,
  232. skb_frag_size(frag), &pb[slots_used]);
  233. }
  234. return slots_used;
  235. }
  236. static int count_skb_frag_slots(struct sk_buff *skb)
  237. {
  238. int i, frags = skb_shinfo(skb)->nr_frags;
  239. int pages = 0;
  240. for (i = 0; i < frags; i++) {
  241. skb_frag_t *frag = skb_shinfo(skb)->frags + i;
  242. unsigned long size = skb_frag_size(frag);
  243. unsigned long offset = frag->page_offset;
  244. /* Skip unused frames from start of page */
  245. offset &= ~PAGE_MASK;
  246. pages += PFN_UP(offset + size);
  247. }
  248. return pages;
  249. }
  250. static int netvsc_get_slots(struct sk_buff *skb)
  251. {
  252. char *data = skb->data;
  253. unsigned int offset = offset_in_page(data);
  254. unsigned int len = skb_headlen(skb);
  255. int slots;
  256. int frag_slots;
  257. slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
  258. frag_slots = count_skb_frag_slots(skb);
  259. return slots + frag_slots;
  260. }
  261. static u32 get_net_transport_info(struct sk_buff *skb, u32 *trans_off)
  262. {
  263. u32 ret_val = TRANSPORT_INFO_NOT_IP;
  264. if ((eth_hdr(skb)->h_proto != htons(ETH_P_IP)) &&
  265. (eth_hdr(skb)->h_proto != htons(ETH_P_IPV6))) {
  266. goto not_ip;
  267. }
  268. *trans_off = skb_transport_offset(skb);
  269. if ((eth_hdr(skb)->h_proto == htons(ETH_P_IP))) {
  270. struct iphdr *iphdr = ip_hdr(skb);
  271. if (iphdr->protocol == IPPROTO_TCP)
  272. ret_val = TRANSPORT_INFO_IPV4_TCP;
  273. else if (iphdr->protocol == IPPROTO_UDP)
  274. ret_val = TRANSPORT_INFO_IPV4_UDP;
  275. } else {
  276. if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
  277. ret_val = TRANSPORT_INFO_IPV6_TCP;
  278. else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
  279. ret_val = TRANSPORT_INFO_IPV6_UDP;
  280. }
  281. not_ip:
  282. return ret_val;
  283. }
  284. static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
  285. {
  286. struct net_device_context *net_device_ctx = netdev_priv(net);
  287. struct hv_netvsc_packet *packet = NULL;
  288. int ret;
  289. unsigned int num_data_pgs;
  290. struct rndis_message *rndis_msg;
  291. struct rndis_packet *rndis_pkt;
  292. u32 rndis_msg_size;
  293. bool isvlan;
  294. bool linear = false;
  295. struct rndis_per_packet_info *ppi;
  296. struct ndis_tcp_ip_checksum_info *csum_info;
  297. struct ndis_tcp_lso_info *lso_info;
  298. int hdr_offset;
  299. u32 net_trans_info;
  300. u32 hash;
  301. u32 skb_length;
  302. struct hv_page_buffer page_buf[MAX_PAGE_BUFFER_COUNT];
  303. struct hv_page_buffer *pb = page_buf;
  304. struct netvsc_stats *tx_stats = this_cpu_ptr(net_device_ctx->tx_stats);
  305. /* We will atmost need two pages to describe the rndis
  306. * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
  307. * of pages in a single packet. If skb is scattered around
  308. * more pages we try linearizing it.
  309. */
  310. check_size:
  311. skb_length = skb->len;
  312. num_data_pgs = netvsc_get_slots(skb) + 2;
  313. if (num_data_pgs > MAX_PAGE_BUFFER_COUNT && linear) {
  314. net_alert_ratelimited("packet too big: %u pages (%u bytes)\n",
  315. num_data_pgs, skb->len);
  316. ret = -EFAULT;
  317. goto drop;
  318. } else if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
  319. if (skb_linearize(skb)) {
  320. net_alert_ratelimited("failed to linearize skb\n");
  321. ret = -ENOMEM;
  322. goto drop;
  323. }
  324. linear = true;
  325. goto check_size;
  326. }
  327. /*
  328. * Place the rndis header in the skb head room and
  329. * the skb->cb will be used for hv_netvsc_packet
  330. * structure.
  331. */
  332. ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
  333. if (ret) {
  334. netdev_err(net, "unable to alloc hv_netvsc_packet\n");
  335. ret = -ENOMEM;
  336. goto drop;
  337. }
  338. /* Use the skb control buffer for building up the packet */
  339. BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
  340. FIELD_SIZEOF(struct sk_buff, cb));
  341. packet = (struct hv_netvsc_packet *)skb->cb;
  342. packet->q_idx = skb_get_queue_mapping(skb);
  343. packet->total_data_buflen = skb->len;
  344. rndis_msg = (struct rndis_message *)skb->head;
  345. memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE);
  346. isvlan = skb->vlan_tci & VLAN_TAG_PRESENT;
  347. /* Add the rndis header */
  348. rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
  349. rndis_msg->msg_len = packet->total_data_buflen;
  350. rndis_pkt = &rndis_msg->msg.pkt;
  351. rndis_pkt->data_offset = sizeof(struct rndis_packet);
  352. rndis_pkt->data_len = packet->total_data_buflen;
  353. rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
  354. rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
  355. hash = skb_get_hash_raw(skb);
  356. if (hash != 0 && net->real_num_tx_queues > 1) {
  357. rndis_msg_size += NDIS_HASH_PPI_SIZE;
  358. ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
  359. NBL_HASH_VALUE);
  360. *(u32 *)((void *)ppi + ppi->ppi_offset) = hash;
  361. }
  362. if (isvlan) {
  363. struct ndis_pkt_8021q_info *vlan;
  364. rndis_msg_size += NDIS_VLAN_PPI_SIZE;
  365. ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
  366. IEEE_8021Q_INFO);
  367. vlan = (struct ndis_pkt_8021q_info *)((void *)ppi +
  368. ppi->ppi_offset);
  369. vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
  370. vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
  371. VLAN_PRIO_SHIFT;
  372. }
  373. net_trans_info = get_net_transport_info(skb, &hdr_offset);
  374. if (net_trans_info == TRANSPORT_INFO_NOT_IP)
  375. goto do_send;
  376. /*
  377. * Setup the sendside checksum offload only if this is not a
  378. * GSO packet.
  379. */
  380. if (skb_is_gso(skb))
  381. goto do_lso;
  382. if ((skb->ip_summed == CHECKSUM_NONE) ||
  383. (skb->ip_summed == CHECKSUM_UNNECESSARY))
  384. goto do_send;
  385. rndis_msg_size += NDIS_CSUM_PPI_SIZE;
  386. ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
  387. TCPIP_CHKSUM_PKTINFO);
  388. csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi +
  389. ppi->ppi_offset);
  390. if (net_trans_info & (INFO_IPV4 << 16))
  391. csum_info->transmit.is_ipv4 = 1;
  392. else
  393. csum_info->transmit.is_ipv6 = 1;
  394. if (net_trans_info & INFO_TCP) {
  395. csum_info->transmit.tcp_checksum = 1;
  396. csum_info->transmit.tcp_header_offset = hdr_offset;
  397. } else if (net_trans_info & INFO_UDP) {
  398. /* UDP checksum offload is not supported on ws2008r2.
  399. * Furthermore, on ws2012 and ws2012r2, there are some
  400. * issues with udp checksum offload from Linux guests.
  401. * (these are host issues).
  402. * For now compute the checksum here.
  403. */
  404. struct udphdr *uh;
  405. u16 udp_len;
  406. ret = skb_cow_head(skb, 0);
  407. if (ret)
  408. goto drop;
  409. uh = udp_hdr(skb);
  410. udp_len = ntohs(uh->len);
  411. uh->check = 0;
  412. uh->check = csum_tcpudp_magic(ip_hdr(skb)->saddr,
  413. ip_hdr(skb)->daddr,
  414. udp_len, IPPROTO_UDP,
  415. csum_partial(uh, udp_len, 0));
  416. if (uh->check == 0)
  417. uh->check = CSUM_MANGLED_0;
  418. csum_info->transmit.udp_checksum = 0;
  419. }
  420. goto do_send;
  421. do_lso:
  422. rndis_msg_size += NDIS_LSO_PPI_SIZE;
  423. ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
  424. TCP_LARGESEND_PKTINFO);
  425. lso_info = (struct ndis_tcp_lso_info *)((void *)ppi +
  426. ppi->ppi_offset);
  427. lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
  428. if (net_trans_info & (INFO_IPV4 << 16)) {
  429. lso_info->lso_v2_transmit.ip_version =
  430. NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
  431. ip_hdr(skb)->tot_len = 0;
  432. ip_hdr(skb)->check = 0;
  433. tcp_hdr(skb)->check =
  434. ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  435. ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  436. } else {
  437. lso_info->lso_v2_transmit.ip_version =
  438. NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
  439. ipv6_hdr(skb)->payload_len = 0;
  440. tcp_hdr(skb)->check =
  441. ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  442. &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  443. }
  444. lso_info->lso_v2_transmit.tcp_header_offset = hdr_offset;
  445. lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
  446. do_send:
  447. /* Start filling in the page buffers with the rndis hdr */
  448. rndis_msg->msg_len += rndis_msg_size;
  449. packet->total_data_buflen = rndis_msg->msg_len;
  450. packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
  451. skb, packet, &pb);
  452. /* timestamp packet in software */
  453. skb_tx_timestamp(skb);
  454. ret = netvsc_send(net_device_ctx->device_ctx, packet,
  455. rndis_msg, &pb, skb);
  456. drop:
  457. if (ret == 0) {
  458. u64_stats_update_begin(&tx_stats->syncp);
  459. tx_stats->packets++;
  460. tx_stats->bytes += skb_length;
  461. u64_stats_update_end(&tx_stats->syncp);
  462. } else {
  463. if (ret != -EAGAIN) {
  464. dev_kfree_skb_any(skb);
  465. net->stats.tx_dropped++;
  466. }
  467. }
  468. return (ret == -EAGAIN) ? NETDEV_TX_BUSY : NETDEV_TX_OK;
  469. }
  470. /*
  471. * netvsc_linkstatus_callback - Link up/down notification
  472. */
  473. void netvsc_linkstatus_callback(struct hv_device *device_obj,
  474. struct rndis_message *resp)
  475. {
  476. struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
  477. struct net_device *net;
  478. struct net_device_context *ndev_ctx;
  479. struct netvsc_reconfig *event;
  480. unsigned long flags;
  481. /* Handle link change statuses only */
  482. if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
  483. indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
  484. indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
  485. return;
  486. net = hv_get_drvdata(device_obj);
  487. if (!net || net->reg_state != NETREG_REGISTERED)
  488. return;
  489. ndev_ctx = netdev_priv(net);
  490. event = kzalloc(sizeof(*event), GFP_ATOMIC);
  491. if (!event)
  492. return;
  493. event->event = indicate->status;
  494. spin_lock_irqsave(&ndev_ctx->lock, flags);
  495. list_add_tail(&event->list, &ndev_ctx->reconfig_events);
  496. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  497. schedule_delayed_work(&ndev_ctx->dwork, 0);
  498. }
  499. static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
  500. struct hv_netvsc_packet *packet,
  501. struct ndis_tcp_ip_checksum_info *csum_info,
  502. void *data, u16 vlan_tci)
  503. {
  504. struct sk_buff *skb;
  505. skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
  506. if (!skb)
  507. return skb;
  508. /*
  509. * Copy to skb. This copy is needed here since the memory pointed by
  510. * hv_netvsc_packet cannot be deallocated
  511. */
  512. memcpy(skb_put(skb, packet->total_data_buflen), data,
  513. packet->total_data_buflen);
  514. skb->protocol = eth_type_trans(skb, net);
  515. if (csum_info) {
  516. /* We only look at the IP checksum here.
  517. * Should we be dropping the packet if checksum
  518. * failed? How do we deal with other checksums - TCP/UDP?
  519. */
  520. if (csum_info->receive.ip_checksum_succeeded)
  521. skb->ip_summed = CHECKSUM_UNNECESSARY;
  522. else
  523. skb->ip_summed = CHECKSUM_NONE;
  524. }
  525. if (vlan_tci & VLAN_TAG_PRESENT)
  526. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  527. vlan_tci);
  528. return skb;
  529. }
  530. /*
  531. * netvsc_recv_callback - Callback when we receive a packet from the
  532. * "wire" on the specified device.
  533. */
  534. int netvsc_recv_callback(struct hv_device *device_obj,
  535. struct hv_netvsc_packet *packet,
  536. void **data,
  537. struct ndis_tcp_ip_checksum_info *csum_info,
  538. struct vmbus_channel *channel,
  539. u16 vlan_tci)
  540. {
  541. struct net_device *net = hv_get_drvdata(device_obj);
  542. struct net_device_context *net_device_ctx = netdev_priv(net);
  543. struct sk_buff *skb;
  544. struct sk_buff *vf_skb;
  545. struct netvsc_stats *rx_stats;
  546. u32 bytes_recvd = packet->total_data_buflen;
  547. int ret = 0;
  548. if (!net || net->reg_state != NETREG_REGISTERED)
  549. return NVSP_STAT_FAIL;
  550. if (READ_ONCE(net_device_ctx->vf_inject)) {
  551. atomic_inc(&net_device_ctx->vf_use_cnt);
  552. if (!READ_ONCE(net_device_ctx->vf_inject)) {
  553. /*
  554. * We raced; just move on.
  555. */
  556. atomic_dec(&net_device_ctx->vf_use_cnt);
  557. goto vf_injection_done;
  558. }
  559. /*
  560. * Inject this packet into the VF inerface.
  561. * On Hyper-V, multicast and brodcast packets
  562. * are only delivered on the synthetic interface
  563. * (after subjecting these to policy filters on
  564. * the host). Deliver these via the VF interface
  565. * in the guest.
  566. */
  567. vf_skb = netvsc_alloc_recv_skb(net_device_ctx->vf_netdev,
  568. packet, csum_info, *data,
  569. vlan_tci);
  570. if (vf_skb != NULL) {
  571. ++net_device_ctx->vf_netdev->stats.rx_packets;
  572. net_device_ctx->vf_netdev->stats.rx_bytes +=
  573. bytes_recvd;
  574. netif_receive_skb(vf_skb);
  575. } else {
  576. ++net->stats.rx_dropped;
  577. ret = NVSP_STAT_FAIL;
  578. }
  579. atomic_dec(&net_device_ctx->vf_use_cnt);
  580. return ret;
  581. }
  582. vf_injection_done:
  583. rx_stats = this_cpu_ptr(net_device_ctx->rx_stats);
  584. /* Allocate a skb - TODO direct I/O to pages? */
  585. skb = netvsc_alloc_recv_skb(net, packet, csum_info, *data, vlan_tci);
  586. if (unlikely(!skb)) {
  587. ++net->stats.rx_dropped;
  588. return NVSP_STAT_FAIL;
  589. }
  590. skb_record_rx_queue(skb, channel->
  591. offermsg.offer.sub_channel_index);
  592. u64_stats_update_begin(&rx_stats->syncp);
  593. rx_stats->packets++;
  594. rx_stats->bytes += packet->total_data_buflen;
  595. u64_stats_update_end(&rx_stats->syncp);
  596. /*
  597. * Pass the skb back up. Network stack will deallocate the skb when it
  598. * is done.
  599. * TODO - use NAPI?
  600. */
  601. netif_rx(skb);
  602. return 0;
  603. }
  604. static void netvsc_get_drvinfo(struct net_device *net,
  605. struct ethtool_drvinfo *info)
  606. {
  607. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  608. strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
  609. }
  610. static void netvsc_get_channels(struct net_device *net,
  611. struct ethtool_channels *channel)
  612. {
  613. struct net_device_context *net_device_ctx = netdev_priv(net);
  614. struct netvsc_device *nvdev = net_device_ctx->nvdev;
  615. if (nvdev) {
  616. channel->max_combined = nvdev->max_chn;
  617. channel->combined_count = nvdev->num_chn;
  618. }
  619. }
  620. static int netvsc_set_channels(struct net_device *net,
  621. struct ethtool_channels *channels)
  622. {
  623. struct net_device_context *net_device_ctx = netdev_priv(net);
  624. struct hv_device *dev = net_device_ctx->device_ctx;
  625. struct netvsc_device *nvdev = net_device_ctx->nvdev;
  626. struct netvsc_device_info device_info;
  627. u32 num_chn;
  628. u32 max_chn;
  629. int ret = 0;
  630. bool recovering = false;
  631. if (net_device_ctx->start_remove || !nvdev || nvdev->destroy)
  632. return -ENODEV;
  633. num_chn = nvdev->num_chn;
  634. max_chn = min_t(u32, nvdev->max_chn, num_online_cpus());
  635. if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5) {
  636. pr_info("vRSS unsupported before NVSP Version 5\n");
  637. return -EINVAL;
  638. }
  639. /* We do not support rx, tx, or other */
  640. if (!channels ||
  641. channels->rx_count ||
  642. channels->tx_count ||
  643. channels->other_count ||
  644. (channels->combined_count < 1))
  645. return -EINVAL;
  646. if (channels->combined_count > max_chn) {
  647. pr_info("combined channels too high, using %d\n", max_chn);
  648. channels->combined_count = max_chn;
  649. }
  650. ret = netvsc_close(net);
  651. if (ret)
  652. goto out;
  653. do_set:
  654. net_device_ctx->start_remove = true;
  655. rndis_filter_device_remove(dev);
  656. nvdev->num_chn = channels->combined_count;
  657. memset(&device_info, 0, sizeof(device_info));
  658. device_info.num_chn = nvdev->num_chn; /* passed to RNDIS */
  659. device_info.ring_size = ring_size;
  660. device_info.max_num_vrss_chns = max_num_vrss_chns;
  661. ret = rndis_filter_device_add(dev, &device_info);
  662. if (ret) {
  663. if (recovering) {
  664. netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
  665. return ret;
  666. }
  667. goto recover;
  668. }
  669. nvdev = net_device_ctx->nvdev;
  670. ret = netif_set_real_num_tx_queues(net, nvdev->num_chn);
  671. if (ret) {
  672. if (recovering) {
  673. netdev_err(net, "could not set tx queue count (ret %d)\n", ret);
  674. return ret;
  675. }
  676. goto recover;
  677. }
  678. ret = netif_set_real_num_rx_queues(net, nvdev->num_chn);
  679. if (ret) {
  680. if (recovering) {
  681. netdev_err(net, "could not set rx queue count (ret %d)\n", ret);
  682. return ret;
  683. }
  684. goto recover;
  685. }
  686. out:
  687. netvsc_open(net);
  688. net_device_ctx->start_remove = false;
  689. /* We may have missed link change notifications */
  690. schedule_delayed_work(&net_device_ctx->dwork, 0);
  691. return ret;
  692. recover:
  693. /* If the above failed, we attempt to recover through the same
  694. * process but with the original number of channels.
  695. */
  696. netdev_err(net, "could not set channels, recovering\n");
  697. recovering = true;
  698. channels->combined_count = num_chn;
  699. goto do_set;
  700. }
  701. static bool netvsc_validate_ethtool_ss_cmd(const struct ethtool_cmd *cmd)
  702. {
  703. struct ethtool_cmd diff1 = *cmd;
  704. struct ethtool_cmd diff2 = {};
  705. ethtool_cmd_speed_set(&diff1, 0);
  706. diff1.duplex = 0;
  707. /* advertising and cmd are usually set */
  708. diff1.advertising = 0;
  709. diff1.cmd = 0;
  710. /* We set port to PORT_OTHER */
  711. diff2.port = PORT_OTHER;
  712. return !memcmp(&diff1, &diff2, sizeof(diff1));
  713. }
  714. static void netvsc_init_settings(struct net_device *dev)
  715. {
  716. struct net_device_context *ndc = netdev_priv(dev);
  717. ndc->speed = SPEED_UNKNOWN;
  718. ndc->duplex = DUPLEX_UNKNOWN;
  719. }
  720. static int netvsc_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  721. {
  722. struct net_device_context *ndc = netdev_priv(dev);
  723. ethtool_cmd_speed_set(cmd, ndc->speed);
  724. cmd->duplex = ndc->duplex;
  725. cmd->port = PORT_OTHER;
  726. return 0;
  727. }
  728. static int netvsc_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  729. {
  730. struct net_device_context *ndc = netdev_priv(dev);
  731. u32 speed;
  732. speed = ethtool_cmd_speed(cmd);
  733. if (!ethtool_validate_speed(speed) ||
  734. !ethtool_validate_duplex(cmd->duplex) ||
  735. !netvsc_validate_ethtool_ss_cmd(cmd))
  736. return -EINVAL;
  737. ndc->speed = speed;
  738. ndc->duplex = cmd->duplex;
  739. return 0;
  740. }
  741. static int netvsc_change_mtu(struct net_device *ndev, int mtu)
  742. {
  743. struct net_device_context *ndevctx = netdev_priv(ndev);
  744. struct netvsc_device *nvdev = ndevctx->nvdev;
  745. struct hv_device *hdev = ndevctx->device_ctx;
  746. struct netvsc_device_info device_info;
  747. int limit = ETH_DATA_LEN;
  748. u32 num_chn;
  749. int ret = 0;
  750. if (ndevctx->start_remove || !nvdev || nvdev->destroy)
  751. return -ENODEV;
  752. if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
  753. limit = NETVSC_MTU - ETH_HLEN;
  754. if (mtu < NETVSC_MTU_MIN || mtu > limit)
  755. return -EINVAL;
  756. ret = netvsc_close(ndev);
  757. if (ret)
  758. goto out;
  759. num_chn = nvdev->num_chn;
  760. ndevctx->start_remove = true;
  761. rndis_filter_device_remove(hdev);
  762. ndev->mtu = mtu;
  763. memset(&device_info, 0, sizeof(device_info));
  764. device_info.ring_size = ring_size;
  765. device_info.num_chn = num_chn;
  766. device_info.max_num_vrss_chns = max_num_vrss_chns;
  767. rndis_filter_device_add(hdev, &device_info);
  768. out:
  769. netvsc_open(ndev);
  770. ndevctx->start_remove = false;
  771. /* We may have missed link change notifications */
  772. schedule_delayed_work(&ndevctx->dwork, 0);
  773. return ret;
  774. }
  775. static struct rtnl_link_stats64 *netvsc_get_stats64(struct net_device *net,
  776. struct rtnl_link_stats64 *t)
  777. {
  778. struct net_device_context *ndev_ctx = netdev_priv(net);
  779. int cpu;
  780. for_each_possible_cpu(cpu) {
  781. struct netvsc_stats *tx_stats = per_cpu_ptr(ndev_ctx->tx_stats,
  782. cpu);
  783. struct netvsc_stats *rx_stats = per_cpu_ptr(ndev_ctx->rx_stats,
  784. cpu);
  785. u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
  786. unsigned int start;
  787. do {
  788. start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
  789. tx_packets = tx_stats->packets;
  790. tx_bytes = tx_stats->bytes;
  791. } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
  792. do {
  793. start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
  794. rx_packets = rx_stats->packets;
  795. rx_bytes = rx_stats->bytes;
  796. } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
  797. t->tx_bytes += tx_bytes;
  798. t->tx_packets += tx_packets;
  799. t->rx_bytes += rx_bytes;
  800. t->rx_packets += rx_packets;
  801. }
  802. t->tx_dropped = net->stats.tx_dropped;
  803. t->tx_errors = net->stats.tx_dropped;
  804. t->rx_dropped = net->stats.rx_dropped;
  805. t->rx_errors = net->stats.rx_errors;
  806. return t;
  807. }
  808. static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
  809. {
  810. struct sockaddr *addr = p;
  811. char save_adr[ETH_ALEN];
  812. unsigned char save_aatype;
  813. int err;
  814. memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
  815. save_aatype = ndev->addr_assign_type;
  816. err = eth_mac_addr(ndev, p);
  817. if (err != 0)
  818. return err;
  819. err = rndis_filter_set_device_mac(ndev, addr->sa_data);
  820. if (err != 0) {
  821. /* roll back to saved MAC */
  822. memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
  823. ndev->addr_assign_type = save_aatype;
  824. }
  825. return err;
  826. }
  827. #ifdef CONFIG_NET_POLL_CONTROLLER
  828. static void netvsc_poll_controller(struct net_device *net)
  829. {
  830. /* As netvsc_start_xmit() works synchronous we don't have to
  831. * trigger anything here.
  832. */
  833. }
  834. #endif
  835. static const struct ethtool_ops ethtool_ops = {
  836. .get_drvinfo = netvsc_get_drvinfo,
  837. .get_link = ethtool_op_get_link,
  838. .get_channels = netvsc_get_channels,
  839. .set_channels = netvsc_set_channels,
  840. .get_ts_info = ethtool_op_get_ts_info,
  841. .get_settings = netvsc_get_settings,
  842. .set_settings = netvsc_set_settings,
  843. };
  844. static const struct net_device_ops device_ops = {
  845. .ndo_open = netvsc_open,
  846. .ndo_stop = netvsc_close,
  847. .ndo_start_xmit = netvsc_start_xmit,
  848. .ndo_set_rx_mode = netvsc_set_multicast_list,
  849. .ndo_change_mtu = netvsc_change_mtu,
  850. .ndo_validate_addr = eth_validate_addr,
  851. .ndo_set_mac_address = netvsc_set_mac_addr,
  852. .ndo_select_queue = netvsc_select_queue,
  853. .ndo_get_stats64 = netvsc_get_stats64,
  854. #ifdef CONFIG_NET_POLL_CONTROLLER
  855. .ndo_poll_controller = netvsc_poll_controller,
  856. #endif
  857. };
  858. /*
  859. * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
  860. * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
  861. * present send GARP packet to network peers with netif_notify_peers().
  862. */
  863. static void netvsc_link_change(struct work_struct *w)
  864. {
  865. struct net_device_context *ndev_ctx =
  866. container_of(w, struct net_device_context, dwork.work);
  867. struct hv_device *device_obj = ndev_ctx->device_ctx;
  868. struct net_device *net = hv_get_drvdata(device_obj);
  869. struct netvsc_device *net_device;
  870. struct rndis_device *rdev;
  871. struct netvsc_reconfig *event = NULL;
  872. bool notify = false, reschedule = false;
  873. unsigned long flags, next_reconfig, delay;
  874. rtnl_lock();
  875. if (ndev_ctx->start_remove)
  876. goto out_unlock;
  877. net_device = ndev_ctx->nvdev;
  878. rdev = net_device->extension;
  879. next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
  880. if (time_is_after_jiffies(next_reconfig)) {
  881. /* link_watch only sends one notification with current state
  882. * per second, avoid doing reconfig more frequently. Handle
  883. * wrap around.
  884. */
  885. delay = next_reconfig - jiffies;
  886. delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
  887. schedule_delayed_work(&ndev_ctx->dwork, delay);
  888. goto out_unlock;
  889. }
  890. ndev_ctx->last_reconfig = jiffies;
  891. spin_lock_irqsave(&ndev_ctx->lock, flags);
  892. if (!list_empty(&ndev_ctx->reconfig_events)) {
  893. event = list_first_entry(&ndev_ctx->reconfig_events,
  894. struct netvsc_reconfig, list);
  895. list_del(&event->list);
  896. reschedule = !list_empty(&ndev_ctx->reconfig_events);
  897. }
  898. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  899. if (!event)
  900. goto out_unlock;
  901. switch (event->event) {
  902. /* Only the following events are possible due to the check in
  903. * netvsc_linkstatus_callback()
  904. */
  905. case RNDIS_STATUS_MEDIA_CONNECT:
  906. if (rdev->link_state) {
  907. rdev->link_state = false;
  908. netif_carrier_on(net);
  909. netif_tx_wake_all_queues(net);
  910. } else {
  911. notify = true;
  912. }
  913. kfree(event);
  914. break;
  915. case RNDIS_STATUS_MEDIA_DISCONNECT:
  916. if (!rdev->link_state) {
  917. rdev->link_state = true;
  918. netif_carrier_off(net);
  919. netif_tx_stop_all_queues(net);
  920. }
  921. kfree(event);
  922. break;
  923. case RNDIS_STATUS_NETWORK_CHANGE:
  924. /* Only makes sense if carrier is present */
  925. if (!rdev->link_state) {
  926. rdev->link_state = true;
  927. netif_carrier_off(net);
  928. netif_tx_stop_all_queues(net);
  929. event->event = RNDIS_STATUS_MEDIA_CONNECT;
  930. spin_lock_irqsave(&ndev_ctx->lock, flags);
  931. list_add(&event->list, &ndev_ctx->reconfig_events);
  932. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  933. reschedule = true;
  934. }
  935. break;
  936. }
  937. rtnl_unlock();
  938. if (notify)
  939. netdev_notify_peers(net);
  940. /* link_watch only sends one notification with current state per
  941. * second, handle next reconfig event in 2 seconds.
  942. */
  943. if (reschedule)
  944. schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
  945. return;
  946. out_unlock:
  947. rtnl_unlock();
  948. }
  949. static void netvsc_free_netdev(struct net_device *netdev)
  950. {
  951. struct net_device_context *net_device_ctx = netdev_priv(netdev);
  952. free_percpu(net_device_ctx->tx_stats);
  953. free_percpu(net_device_ctx->rx_stats);
  954. free_netdev(netdev);
  955. }
  956. static struct net_device *get_netvsc_net_device(char *mac)
  957. {
  958. struct net_device *dev, *found = NULL;
  959. int rtnl_locked;
  960. rtnl_locked = rtnl_trylock();
  961. for_each_netdev(&init_net, dev) {
  962. if (memcmp(dev->dev_addr, mac, ETH_ALEN) == 0) {
  963. if (dev->netdev_ops != &device_ops)
  964. continue;
  965. found = dev;
  966. break;
  967. }
  968. }
  969. if (rtnl_locked)
  970. rtnl_unlock();
  971. return found;
  972. }
  973. static int netvsc_register_vf(struct net_device *vf_netdev)
  974. {
  975. struct net_device *ndev;
  976. struct net_device_context *net_device_ctx;
  977. struct netvsc_device *netvsc_dev;
  978. const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops;
  979. if (eth_ops == NULL || eth_ops == &ethtool_ops)
  980. return NOTIFY_DONE;
  981. /*
  982. * We will use the MAC address to locate the synthetic interface to
  983. * associate with the VF interface. If we don't find a matching
  984. * synthetic interface, move on.
  985. */
  986. ndev = get_netvsc_net_device(vf_netdev->dev_addr);
  987. if (!ndev)
  988. return NOTIFY_DONE;
  989. net_device_ctx = netdev_priv(ndev);
  990. netvsc_dev = net_device_ctx->nvdev;
  991. if (!netvsc_dev || net_device_ctx->vf_netdev)
  992. return NOTIFY_DONE;
  993. netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
  994. /*
  995. * Take a reference on the module.
  996. */
  997. try_module_get(THIS_MODULE);
  998. net_device_ctx->vf_netdev = vf_netdev;
  999. return NOTIFY_OK;
  1000. }
  1001. static void netvsc_inject_enable(struct net_device_context *net_device_ctx)
  1002. {
  1003. net_device_ctx->vf_inject = true;
  1004. }
  1005. static void netvsc_inject_disable(struct net_device_context *net_device_ctx)
  1006. {
  1007. net_device_ctx->vf_inject = false;
  1008. /* Wait for currently active users to drain out. */
  1009. while (atomic_read(&net_device_ctx->vf_use_cnt) != 0)
  1010. udelay(50);
  1011. }
  1012. static int netvsc_vf_up(struct net_device *vf_netdev)
  1013. {
  1014. struct net_device *ndev;
  1015. struct netvsc_device *netvsc_dev;
  1016. const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops;
  1017. struct net_device_context *net_device_ctx;
  1018. if (eth_ops == &ethtool_ops)
  1019. return NOTIFY_DONE;
  1020. ndev = get_netvsc_net_device(vf_netdev->dev_addr);
  1021. if (!ndev)
  1022. return NOTIFY_DONE;
  1023. net_device_ctx = netdev_priv(ndev);
  1024. netvsc_dev = net_device_ctx->nvdev;
  1025. if (!netvsc_dev || !net_device_ctx->vf_netdev)
  1026. return NOTIFY_DONE;
  1027. netdev_info(ndev, "VF up: %s\n", vf_netdev->name);
  1028. netvsc_inject_enable(net_device_ctx);
  1029. /*
  1030. * Open the device before switching data path.
  1031. */
  1032. rndis_filter_open(netvsc_dev);
  1033. /*
  1034. * notify the host to switch the data path.
  1035. */
  1036. netvsc_switch_datapath(ndev, true);
  1037. netdev_info(ndev, "Data path switched to VF: %s\n", vf_netdev->name);
  1038. netif_carrier_off(ndev);
  1039. /* Now notify peers through VF device. */
  1040. call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, vf_netdev);
  1041. return NOTIFY_OK;
  1042. }
  1043. static int netvsc_vf_down(struct net_device *vf_netdev)
  1044. {
  1045. struct net_device *ndev;
  1046. struct netvsc_device *netvsc_dev;
  1047. struct net_device_context *net_device_ctx;
  1048. const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops;
  1049. if (eth_ops == &ethtool_ops)
  1050. return NOTIFY_DONE;
  1051. ndev = get_netvsc_net_device(vf_netdev->dev_addr);
  1052. if (!ndev)
  1053. return NOTIFY_DONE;
  1054. net_device_ctx = netdev_priv(ndev);
  1055. netvsc_dev = net_device_ctx->nvdev;
  1056. if (!netvsc_dev || !net_device_ctx->vf_netdev)
  1057. return NOTIFY_DONE;
  1058. netdev_info(ndev, "VF down: %s\n", vf_netdev->name);
  1059. netvsc_inject_disable(net_device_ctx);
  1060. netvsc_switch_datapath(ndev, false);
  1061. netdev_info(ndev, "Data path switched from VF: %s\n", vf_netdev->name);
  1062. rndis_filter_close(netvsc_dev);
  1063. netif_carrier_on(ndev);
  1064. /* Now notify peers through netvsc device. */
  1065. call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, ndev);
  1066. return NOTIFY_OK;
  1067. }
  1068. static int netvsc_unregister_vf(struct net_device *vf_netdev)
  1069. {
  1070. struct net_device *ndev;
  1071. struct netvsc_device *netvsc_dev;
  1072. const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops;
  1073. struct net_device_context *net_device_ctx;
  1074. if (eth_ops == &ethtool_ops)
  1075. return NOTIFY_DONE;
  1076. ndev = get_netvsc_net_device(vf_netdev->dev_addr);
  1077. if (!ndev)
  1078. return NOTIFY_DONE;
  1079. net_device_ctx = netdev_priv(ndev);
  1080. netvsc_dev = net_device_ctx->nvdev;
  1081. if (!netvsc_dev || !net_device_ctx->vf_netdev)
  1082. return NOTIFY_DONE;
  1083. netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
  1084. netvsc_inject_disable(net_device_ctx);
  1085. net_device_ctx->vf_netdev = NULL;
  1086. module_put(THIS_MODULE);
  1087. return NOTIFY_OK;
  1088. }
  1089. static int netvsc_probe(struct hv_device *dev,
  1090. const struct hv_vmbus_device_id *dev_id)
  1091. {
  1092. struct net_device *net = NULL;
  1093. struct net_device_context *net_device_ctx;
  1094. struct netvsc_device_info device_info;
  1095. struct netvsc_device *nvdev;
  1096. int ret;
  1097. net = alloc_etherdev_mq(sizeof(struct net_device_context),
  1098. num_online_cpus());
  1099. if (!net)
  1100. return -ENOMEM;
  1101. netif_carrier_off(net);
  1102. net_device_ctx = netdev_priv(net);
  1103. net_device_ctx->device_ctx = dev;
  1104. net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
  1105. if (netif_msg_probe(net_device_ctx))
  1106. netdev_dbg(net, "netvsc msg_enable: %d\n",
  1107. net_device_ctx->msg_enable);
  1108. net_device_ctx->tx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
  1109. if (!net_device_ctx->tx_stats) {
  1110. free_netdev(net);
  1111. return -ENOMEM;
  1112. }
  1113. net_device_ctx->rx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
  1114. if (!net_device_ctx->rx_stats) {
  1115. free_percpu(net_device_ctx->tx_stats);
  1116. free_netdev(net);
  1117. return -ENOMEM;
  1118. }
  1119. hv_set_drvdata(dev, net);
  1120. net_device_ctx->start_remove = false;
  1121. INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
  1122. INIT_WORK(&net_device_ctx->work, do_set_multicast);
  1123. spin_lock_init(&net_device_ctx->lock);
  1124. INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
  1125. atomic_set(&net_device_ctx->vf_use_cnt, 0);
  1126. net_device_ctx->vf_netdev = NULL;
  1127. net_device_ctx->vf_inject = false;
  1128. net->netdev_ops = &device_ops;
  1129. net->hw_features = NETVSC_HW_FEATURES;
  1130. net->features = NETVSC_HW_FEATURES | NETIF_F_HW_VLAN_CTAG_TX;
  1131. net->ethtool_ops = &ethtool_ops;
  1132. SET_NETDEV_DEV(net, &dev->device);
  1133. /* We always need headroom for rndis header */
  1134. net->needed_headroom = RNDIS_AND_PPI_SIZE;
  1135. /* Notify the netvsc driver of the new device */
  1136. memset(&device_info, 0, sizeof(device_info));
  1137. device_info.ring_size = ring_size;
  1138. device_info.max_num_vrss_chns = max_num_vrss_chns;
  1139. ret = rndis_filter_device_add(dev, &device_info);
  1140. if (ret != 0) {
  1141. netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
  1142. netvsc_free_netdev(net);
  1143. hv_set_drvdata(dev, NULL);
  1144. return ret;
  1145. }
  1146. memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
  1147. nvdev = net_device_ctx->nvdev;
  1148. netif_set_real_num_tx_queues(net, nvdev->num_chn);
  1149. netif_set_real_num_rx_queues(net, nvdev->num_chn);
  1150. netvsc_init_settings(net);
  1151. ret = register_netdev(net);
  1152. if (ret != 0) {
  1153. pr_err("Unable to register netdev.\n");
  1154. rndis_filter_device_remove(dev);
  1155. netvsc_free_netdev(net);
  1156. }
  1157. return ret;
  1158. }
  1159. static int netvsc_remove(struct hv_device *dev)
  1160. {
  1161. struct net_device *net;
  1162. struct net_device_context *ndev_ctx;
  1163. struct netvsc_device *net_device;
  1164. net = hv_get_drvdata(dev);
  1165. if (net == NULL) {
  1166. dev_err(&dev->device, "No net device to remove\n");
  1167. return 0;
  1168. }
  1169. ndev_ctx = netdev_priv(net);
  1170. net_device = ndev_ctx->nvdev;
  1171. /* Avoid racing with netvsc_change_mtu()/netvsc_set_channels()
  1172. * removing the device.
  1173. */
  1174. rtnl_lock();
  1175. ndev_ctx->start_remove = true;
  1176. rtnl_unlock();
  1177. cancel_delayed_work_sync(&ndev_ctx->dwork);
  1178. cancel_work_sync(&ndev_ctx->work);
  1179. /* Stop outbound asap */
  1180. netif_tx_disable(net);
  1181. unregister_netdev(net);
  1182. /*
  1183. * Call to the vsc driver to let it know that the device is being
  1184. * removed
  1185. */
  1186. rndis_filter_device_remove(dev);
  1187. hv_set_drvdata(dev, NULL);
  1188. netvsc_free_netdev(net);
  1189. return 0;
  1190. }
  1191. static const struct hv_vmbus_device_id id_table[] = {
  1192. /* Network guid */
  1193. { HV_NIC_GUID, },
  1194. { },
  1195. };
  1196. MODULE_DEVICE_TABLE(vmbus, id_table);
  1197. /* The one and only one */
  1198. static struct hv_driver netvsc_drv = {
  1199. .name = KBUILD_MODNAME,
  1200. .id_table = id_table,
  1201. .probe = netvsc_probe,
  1202. .remove = netvsc_remove,
  1203. };
  1204. /*
  1205. * On Hyper-V, every VF interface is matched with a corresponding
  1206. * synthetic interface. The synthetic interface is presented first
  1207. * to the guest. When the corresponding VF instance is registered,
  1208. * we will take care of switching the data path.
  1209. */
  1210. static int netvsc_netdev_event(struct notifier_block *this,
  1211. unsigned long event, void *ptr)
  1212. {
  1213. struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
  1214. /* Avoid Vlan dev with same MAC registering as VF */
  1215. if (event_dev->priv_flags & IFF_802_1Q_VLAN)
  1216. return NOTIFY_DONE;
  1217. /* Avoid Bonding master dev with same MAC registering as VF */
  1218. if (event_dev->priv_flags & IFF_BONDING &&
  1219. event_dev->flags & IFF_MASTER)
  1220. return NOTIFY_DONE;
  1221. switch (event) {
  1222. case NETDEV_REGISTER:
  1223. return netvsc_register_vf(event_dev);
  1224. case NETDEV_UNREGISTER:
  1225. return netvsc_unregister_vf(event_dev);
  1226. case NETDEV_UP:
  1227. return netvsc_vf_up(event_dev);
  1228. case NETDEV_DOWN:
  1229. return netvsc_vf_down(event_dev);
  1230. default:
  1231. return NOTIFY_DONE;
  1232. }
  1233. }
  1234. static struct notifier_block netvsc_netdev_notifier = {
  1235. .notifier_call = netvsc_netdev_event,
  1236. };
  1237. static void __exit netvsc_drv_exit(void)
  1238. {
  1239. unregister_netdevice_notifier(&netvsc_netdev_notifier);
  1240. vmbus_driver_unregister(&netvsc_drv);
  1241. }
  1242. static int __init netvsc_drv_init(void)
  1243. {
  1244. int ret;
  1245. if (ring_size < RING_SIZE_MIN) {
  1246. ring_size = RING_SIZE_MIN;
  1247. pr_info("Increased ring_size to %d (min allowed)\n",
  1248. ring_size);
  1249. }
  1250. ret = vmbus_driver_register(&netvsc_drv);
  1251. if (ret)
  1252. return ret;
  1253. register_netdevice_notifier(&netvsc_netdev_notifier);
  1254. return 0;
  1255. }
  1256. MODULE_LICENSE("GPL");
  1257. MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
  1258. module_init(netvsc_drv_init);
  1259. module_exit(netvsc_drv_exit);