netvsc_drv.c 39 KB

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