netvsc_drv.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553
  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. vf_netdev = rcu_dereference(net_device_ctx->vf_netdev);
  540. if (vf_netdev && (vf_netdev->flags & IFF_UP))
  541. net = vf_netdev;
  542. /* Allocate a skb - TODO direct I/O to pages? */
  543. skb = netvsc_alloc_recv_skb(net, packet, csum_info, *data, vlan_tci);
  544. if (unlikely(!skb)) {
  545. ++net->stats.rx_dropped;
  546. return NVSP_STAT_FAIL;
  547. }
  548. if (net != vf_netdev)
  549. skb_record_rx_queue(skb,
  550. channel->offermsg.offer.sub_channel_index);
  551. /*
  552. * Even if injecting the packet, record the statistics
  553. * on the synthetic device because modifying the VF device
  554. * statistics will not work correctly.
  555. */
  556. rx_stats = this_cpu_ptr(net_device_ctx->rx_stats);
  557. u64_stats_update_begin(&rx_stats->syncp);
  558. rx_stats->packets++;
  559. rx_stats->bytes += packet->total_data_buflen;
  560. if (skb->pkt_type == PACKET_BROADCAST)
  561. ++rx_stats->broadcast;
  562. else if (skb->pkt_type == PACKET_MULTICAST)
  563. ++rx_stats->multicast;
  564. u64_stats_update_end(&rx_stats->syncp);
  565. /*
  566. * Pass the skb back up. Network stack will deallocate the skb when it
  567. * is done.
  568. * TODO - use NAPI?
  569. */
  570. netif_rx(skb);
  571. return 0;
  572. }
  573. static void netvsc_get_drvinfo(struct net_device *net,
  574. struct ethtool_drvinfo *info)
  575. {
  576. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  577. strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
  578. }
  579. static void netvsc_get_channels(struct net_device *net,
  580. struct ethtool_channels *channel)
  581. {
  582. struct net_device_context *net_device_ctx = netdev_priv(net);
  583. struct netvsc_device *nvdev = net_device_ctx->nvdev;
  584. if (nvdev) {
  585. channel->max_combined = nvdev->max_chn;
  586. channel->combined_count = nvdev->num_chn;
  587. }
  588. }
  589. static int netvsc_set_channels(struct net_device *net,
  590. struct ethtool_channels *channels)
  591. {
  592. struct net_device_context *net_device_ctx = netdev_priv(net);
  593. struct hv_device *dev = net_device_ctx->device_ctx;
  594. struct netvsc_device *nvdev = net_device_ctx->nvdev;
  595. struct netvsc_device_info device_info;
  596. u32 num_chn;
  597. u32 max_chn;
  598. int ret = 0;
  599. bool recovering = false;
  600. if (net_device_ctx->start_remove || !nvdev || nvdev->destroy)
  601. return -ENODEV;
  602. num_chn = nvdev->num_chn;
  603. max_chn = min_t(u32, nvdev->max_chn, num_online_cpus());
  604. if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5) {
  605. pr_info("vRSS unsupported before NVSP Version 5\n");
  606. return -EINVAL;
  607. }
  608. /* We do not support rx, tx, or other */
  609. if (!channels ||
  610. channels->rx_count ||
  611. channels->tx_count ||
  612. channels->other_count ||
  613. (channels->combined_count < 1))
  614. return -EINVAL;
  615. if (channels->combined_count > max_chn) {
  616. pr_info("combined channels too high, using %d\n", max_chn);
  617. channels->combined_count = max_chn;
  618. }
  619. ret = netvsc_close(net);
  620. if (ret)
  621. goto out;
  622. do_set:
  623. net_device_ctx->start_remove = true;
  624. rndis_filter_device_remove(dev);
  625. nvdev->num_chn = channels->combined_count;
  626. memset(&device_info, 0, sizeof(device_info));
  627. device_info.num_chn = nvdev->num_chn; /* passed to RNDIS */
  628. device_info.ring_size = ring_size;
  629. device_info.max_num_vrss_chns = max_num_vrss_chns;
  630. ret = rndis_filter_device_add(dev, &device_info);
  631. if (ret) {
  632. if (recovering) {
  633. netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
  634. return ret;
  635. }
  636. goto recover;
  637. }
  638. nvdev = net_device_ctx->nvdev;
  639. ret = netif_set_real_num_tx_queues(net, nvdev->num_chn);
  640. if (ret) {
  641. if (recovering) {
  642. netdev_err(net, "could not set tx queue count (ret %d)\n", ret);
  643. return ret;
  644. }
  645. goto recover;
  646. }
  647. ret = netif_set_real_num_rx_queues(net, nvdev->num_chn);
  648. if (ret) {
  649. if (recovering) {
  650. netdev_err(net, "could not set rx queue count (ret %d)\n", ret);
  651. return ret;
  652. }
  653. goto recover;
  654. }
  655. out:
  656. netvsc_open(net);
  657. net_device_ctx->start_remove = false;
  658. /* We may have missed link change notifications */
  659. schedule_delayed_work(&net_device_ctx->dwork, 0);
  660. return ret;
  661. recover:
  662. /* If the above failed, we attempt to recover through the same
  663. * process but with the original number of channels.
  664. */
  665. netdev_err(net, "could not set channels, recovering\n");
  666. recovering = true;
  667. channels->combined_count = num_chn;
  668. goto do_set;
  669. }
  670. static bool netvsc_validate_ethtool_ss_cmd(const struct ethtool_cmd *cmd)
  671. {
  672. struct ethtool_cmd diff1 = *cmd;
  673. struct ethtool_cmd diff2 = {};
  674. ethtool_cmd_speed_set(&diff1, 0);
  675. diff1.duplex = 0;
  676. /* advertising and cmd are usually set */
  677. diff1.advertising = 0;
  678. diff1.cmd = 0;
  679. /* We set port to PORT_OTHER */
  680. diff2.port = PORT_OTHER;
  681. return !memcmp(&diff1, &diff2, sizeof(diff1));
  682. }
  683. static void netvsc_init_settings(struct net_device *dev)
  684. {
  685. struct net_device_context *ndc = netdev_priv(dev);
  686. ndc->speed = SPEED_UNKNOWN;
  687. ndc->duplex = DUPLEX_UNKNOWN;
  688. }
  689. static int netvsc_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  690. {
  691. struct net_device_context *ndc = netdev_priv(dev);
  692. ethtool_cmd_speed_set(cmd, ndc->speed);
  693. cmd->duplex = ndc->duplex;
  694. cmd->port = PORT_OTHER;
  695. return 0;
  696. }
  697. static int netvsc_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  698. {
  699. struct net_device_context *ndc = netdev_priv(dev);
  700. u32 speed;
  701. speed = ethtool_cmd_speed(cmd);
  702. if (!ethtool_validate_speed(speed) ||
  703. !ethtool_validate_duplex(cmd->duplex) ||
  704. !netvsc_validate_ethtool_ss_cmd(cmd))
  705. return -EINVAL;
  706. ndc->speed = speed;
  707. ndc->duplex = cmd->duplex;
  708. return 0;
  709. }
  710. static int netvsc_change_mtu(struct net_device *ndev, int mtu)
  711. {
  712. struct net_device_context *ndevctx = netdev_priv(ndev);
  713. struct netvsc_device *nvdev = ndevctx->nvdev;
  714. struct hv_device *hdev = ndevctx->device_ctx;
  715. struct netvsc_device_info device_info;
  716. u32 num_chn;
  717. int ret = 0;
  718. if (ndevctx->start_remove || !nvdev || nvdev->destroy)
  719. return -ENODEV;
  720. ret = netvsc_close(ndev);
  721. if (ret)
  722. goto out;
  723. num_chn = nvdev->num_chn;
  724. ndevctx->start_remove = true;
  725. rndis_filter_device_remove(hdev);
  726. ndev->mtu = mtu;
  727. memset(&device_info, 0, sizeof(device_info));
  728. device_info.ring_size = ring_size;
  729. device_info.num_chn = num_chn;
  730. device_info.max_num_vrss_chns = max_num_vrss_chns;
  731. rndis_filter_device_add(hdev, &device_info);
  732. out:
  733. netvsc_open(ndev);
  734. ndevctx->start_remove = false;
  735. /* We may have missed link change notifications */
  736. schedule_delayed_work(&ndevctx->dwork, 0);
  737. return ret;
  738. }
  739. static struct rtnl_link_stats64 *netvsc_get_stats64(struct net_device *net,
  740. struct rtnl_link_stats64 *t)
  741. {
  742. struct net_device_context *ndev_ctx = netdev_priv(net);
  743. int cpu;
  744. for_each_possible_cpu(cpu) {
  745. struct netvsc_stats *tx_stats = per_cpu_ptr(ndev_ctx->tx_stats,
  746. cpu);
  747. struct netvsc_stats *rx_stats = per_cpu_ptr(ndev_ctx->rx_stats,
  748. cpu);
  749. u64 tx_packets, tx_bytes, rx_packets, rx_bytes, rx_multicast;
  750. unsigned int start;
  751. do {
  752. start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
  753. tx_packets = tx_stats->packets;
  754. tx_bytes = tx_stats->bytes;
  755. } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
  756. do {
  757. start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
  758. rx_packets = rx_stats->packets;
  759. rx_bytes = rx_stats->bytes;
  760. rx_multicast = rx_stats->multicast + rx_stats->broadcast;
  761. } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
  762. t->tx_bytes += tx_bytes;
  763. t->tx_packets += tx_packets;
  764. t->rx_bytes += rx_bytes;
  765. t->rx_packets += rx_packets;
  766. t->multicast += rx_multicast;
  767. }
  768. t->tx_dropped = net->stats.tx_dropped;
  769. t->tx_errors = net->stats.tx_dropped;
  770. t->rx_dropped = net->stats.rx_dropped;
  771. t->rx_errors = net->stats.rx_errors;
  772. return t;
  773. }
  774. static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
  775. {
  776. struct sockaddr *addr = p;
  777. char save_adr[ETH_ALEN];
  778. unsigned char save_aatype;
  779. int err;
  780. memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
  781. save_aatype = ndev->addr_assign_type;
  782. err = eth_mac_addr(ndev, p);
  783. if (err != 0)
  784. return err;
  785. err = rndis_filter_set_device_mac(ndev, addr->sa_data);
  786. if (err != 0) {
  787. /* roll back to saved MAC */
  788. memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
  789. ndev->addr_assign_type = save_aatype;
  790. }
  791. return err;
  792. }
  793. static const struct {
  794. char name[ETH_GSTRING_LEN];
  795. u16 offset;
  796. } netvsc_stats[] = {
  797. { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
  798. { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
  799. { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
  800. { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
  801. { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
  802. };
  803. static int netvsc_get_sset_count(struct net_device *dev, int string_set)
  804. {
  805. switch (string_set) {
  806. case ETH_SS_STATS:
  807. return ARRAY_SIZE(netvsc_stats);
  808. default:
  809. return -EINVAL;
  810. }
  811. }
  812. static void netvsc_get_ethtool_stats(struct net_device *dev,
  813. struct ethtool_stats *stats, u64 *data)
  814. {
  815. struct net_device_context *ndc = netdev_priv(dev);
  816. const void *nds = &ndc->eth_stats;
  817. int i;
  818. for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++)
  819. data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
  820. }
  821. static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  822. {
  823. int i;
  824. switch (stringset) {
  825. case ETH_SS_STATS:
  826. for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++)
  827. memcpy(data + i * ETH_GSTRING_LEN,
  828. netvsc_stats[i].name, ETH_GSTRING_LEN);
  829. break;
  830. }
  831. }
  832. #ifdef CONFIG_NET_POLL_CONTROLLER
  833. static void netvsc_poll_controller(struct net_device *net)
  834. {
  835. /* As netvsc_start_xmit() works synchronous we don't have to
  836. * trigger anything here.
  837. */
  838. }
  839. #endif
  840. static const struct ethtool_ops ethtool_ops = {
  841. .get_drvinfo = netvsc_get_drvinfo,
  842. .get_link = ethtool_op_get_link,
  843. .get_ethtool_stats = netvsc_get_ethtool_stats,
  844. .get_sset_count = netvsc_get_sset_count,
  845. .get_strings = netvsc_get_strings,
  846. .get_channels = netvsc_get_channels,
  847. .set_channels = netvsc_set_channels,
  848. .get_ts_info = ethtool_op_get_ts_info,
  849. .get_settings = netvsc_get_settings,
  850. .set_settings = netvsc_set_settings,
  851. };
  852. static const struct net_device_ops device_ops = {
  853. .ndo_open = netvsc_open,
  854. .ndo_stop = netvsc_close,
  855. .ndo_start_xmit = netvsc_start_xmit,
  856. .ndo_set_rx_mode = netvsc_set_multicast_list,
  857. .ndo_change_mtu = netvsc_change_mtu,
  858. .ndo_validate_addr = eth_validate_addr,
  859. .ndo_set_mac_address = netvsc_set_mac_addr,
  860. .ndo_select_queue = netvsc_select_queue,
  861. .ndo_get_stats64 = netvsc_get_stats64,
  862. #ifdef CONFIG_NET_POLL_CONTROLLER
  863. .ndo_poll_controller = netvsc_poll_controller,
  864. #endif
  865. };
  866. /*
  867. * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
  868. * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
  869. * present send GARP packet to network peers with netif_notify_peers().
  870. */
  871. static void netvsc_link_change(struct work_struct *w)
  872. {
  873. struct net_device_context *ndev_ctx =
  874. container_of(w, struct net_device_context, dwork.work);
  875. struct hv_device *device_obj = ndev_ctx->device_ctx;
  876. struct net_device *net = hv_get_drvdata(device_obj);
  877. struct netvsc_device *net_device;
  878. struct rndis_device *rdev;
  879. struct netvsc_reconfig *event = NULL;
  880. bool notify = false, reschedule = false;
  881. unsigned long flags, next_reconfig, delay;
  882. rtnl_lock();
  883. if (ndev_ctx->start_remove)
  884. goto out_unlock;
  885. net_device = ndev_ctx->nvdev;
  886. rdev = net_device->extension;
  887. next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
  888. if (time_is_after_jiffies(next_reconfig)) {
  889. /* link_watch only sends one notification with current state
  890. * per second, avoid doing reconfig more frequently. Handle
  891. * wrap around.
  892. */
  893. delay = next_reconfig - jiffies;
  894. delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
  895. schedule_delayed_work(&ndev_ctx->dwork, delay);
  896. goto out_unlock;
  897. }
  898. ndev_ctx->last_reconfig = jiffies;
  899. spin_lock_irqsave(&ndev_ctx->lock, flags);
  900. if (!list_empty(&ndev_ctx->reconfig_events)) {
  901. event = list_first_entry(&ndev_ctx->reconfig_events,
  902. struct netvsc_reconfig, list);
  903. list_del(&event->list);
  904. reschedule = !list_empty(&ndev_ctx->reconfig_events);
  905. }
  906. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  907. if (!event)
  908. goto out_unlock;
  909. switch (event->event) {
  910. /* Only the following events are possible due to the check in
  911. * netvsc_linkstatus_callback()
  912. */
  913. case RNDIS_STATUS_MEDIA_CONNECT:
  914. if (rdev->link_state) {
  915. rdev->link_state = false;
  916. netif_carrier_on(net);
  917. netif_tx_wake_all_queues(net);
  918. } else {
  919. notify = true;
  920. }
  921. kfree(event);
  922. break;
  923. case RNDIS_STATUS_MEDIA_DISCONNECT:
  924. if (!rdev->link_state) {
  925. rdev->link_state = true;
  926. netif_carrier_off(net);
  927. netif_tx_stop_all_queues(net);
  928. }
  929. kfree(event);
  930. break;
  931. case RNDIS_STATUS_NETWORK_CHANGE:
  932. /* Only makes sense if carrier is present */
  933. if (!rdev->link_state) {
  934. rdev->link_state = true;
  935. netif_carrier_off(net);
  936. netif_tx_stop_all_queues(net);
  937. event->event = RNDIS_STATUS_MEDIA_CONNECT;
  938. spin_lock_irqsave(&ndev_ctx->lock, flags);
  939. list_add(&event->list, &ndev_ctx->reconfig_events);
  940. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  941. reschedule = true;
  942. }
  943. break;
  944. }
  945. rtnl_unlock();
  946. if (notify)
  947. netdev_notify_peers(net);
  948. /* link_watch only sends one notification with current state per
  949. * second, handle next reconfig event in 2 seconds.
  950. */
  951. if (reschedule)
  952. schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
  953. return;
  954. out_unlock:
  955. rtnl_unlock();
  956. }
  957. static void netvsc_free_netdev(struct net_device *netdev)
  958. {
  959. struct net_device_context *net_device_ctx = netdev_priv(netdev);
  960. free_percpu(net_device_ctx->tx_stats);
  961. free_percpu(net_device_ctx->rx_stats);
  962. free_netdev(netdev);
  963. }
  964. static struct net_device *get_netvsc_bymac(const u8 *mac)
  965. {
  966. struct net_device *dev;
  967. ASSERT_RTNL();
  968. for_each_netdev(&init_net, dev) {
  969. if (dev->netdev_ops != &device_ops)
  970. continue; /* not a netvsc device */
  971. if (ether_addr_equal(mac, dev->perm_addr))
  972. return dev;
  973. }
  974. return NULL;
  975. }
  976. static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
  977. {
  978. struct net_device *dev;
  979. ASSERT_RTNL();
  980. for_each_netdev(&init_net, dev) {
  981. struct net_device_context *net_device_ctx;
  982. if (dev->netdev_ops != &device_ops)
  983. continue; /* not a netvsc device */
  984. net_device_ctx = netdev_priv(dev);
  985. if (net_device_ctx->nvdev == NULL)
  986. continue; /* device is removed */
  987. if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev)
  988. return dev; /* a match */
  989. }
  990. return NULL;
  991. }
  992. static int netvsc_register_vf(struct net_device *vf_netdev)
  993. {
  994. struct net_device *ndev;
  995. struct net_device_context *net_device_ctx;
  996. struct netvsc_device *netvsc_dev;
  997. if (vf_netdev->addr_len != ETH_ALEN)
  998. return NOTIFY_DONE;
  999. /*
  1000. * We will use the MAC address to locate the synthetic interface to
  1001. * associate with the VF interface. If we don't find a matching
  1002. * synthetic interface, move on.
  1003. */
  1004. ndev = get_netvsc_bymac(vf_netdev->perm_addr);
  1005. if (!ndev)
  1006. return NOTIFY_DONE;
  1007. net_device_ctx = netdev_priv(ndev);
  1008. netvsc_dev = net_device_ctx->nvdev;
  1009. if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
  1010. return NOTIFY_DONE;
  1011. netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
  1012. /*
  1013. * Take a reference on the module.
  1014. */
  1015. try_module_get(THIS_MODULE);
  1016. dev_hold(vf_netdev);
  1017. rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
  1018. return NOTIFY_OK;
  1019. }
  1020. static int netvsc_vf_up(struct net_device *vf_netdev)
  1021. {
  1022. struct net_device *ndev;
  1023. struct netvsc_device *netvsc_dev;
  1024. struct net_device_context *net_device_ctx;
  1025. ndev = get_netvsc_byref(vf_netdev);
  1026. if (!ndev)
  1027. return NOTIFY_DONE;
  1028. net_device_ctx = netdev_priv(ndev);
  1029. netvsc_dev = net_device_ctx->nvdev;
  1030. netdev_info(ndev, "VF up: %s\n", vf_netdev->name);
  1031. /*
  1032. * Open the device before switching data path.
  1033. */
  1034. rndis_filter_open(netvsc_dev);
  1035. /*
  1036. * notify the host to switch the data path.
  1037. */
  1038. netvsc_switch_datapath(ndev, true);
  1039. netdev_info(ndev, "Data path switched to VF: %s\n", vf_netdev->name);
  1040. netif_carrier_off(ndev);
  1041. /* Now notify peers through VF device. */
  1042. call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, vf_netdev);
  1043. return NOTIFY_OK;
  1044. }
  1045. static int netvsc_vf_down(struct net_device *vf_netdev)
  1046. {
  1047. struct net_device *ndev;
  1048. struct netvsc_device *netvsc_dev;
  1049. struct net_device_context *net_device_ctx;
  1050. ndev = get_netvsc_byref(vf_netdev);
  1051. if (!ndev)
  1052. return NOTIFY_DONE;
  1053. net_device_ctx = netdev_priv(ndev);
  1054. netvsc_dev = net_device_ctx->nvdev;
  1055. netdev_info(ndev, "VF down: %s\n", vf_netdev->name);
  1056. netvsc_switch_datapath(ndev, false);
  1057. netdev_info(ndev, "Data path switched from VF: %s\n", vf_netdev->name);
  1058. rndis_filter_close(netvsc_dev);
  1059. netif_carrier_on(ndev);
  1060. /* Now notify peers through netvsc device. */
  1061. call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, ndev);
  1062. return NOTIFY_OK;
  1063. }
  1064. static int netvsc_unregister_vf(struct net_device *vf_netdev)
  1065. {
  1066. struct net_device *ndev;
  1067. struct netvsc_device *netvsc_dev;
  1068. struct net_device_context *net_device_ctx;
  1069. ndev = get_netvsc_byref(vf_netdev);
  1070. if (!ndev)
  1071. return NOTIFY_DONE;
  1072. net_device_ctx = netdev_priv(ndev);
  1073. netvsc_dev = net_device_ctx->nvdev;
  1074. netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
  1075. RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
  1076. dev_put(vf_netdev);
  1077. module_put(THIS_MODULE);
  1078. return NOTIFY_OK;
  1079. }
  1080. static int netvsc_probe(struct hv_device *dev,
  1081. const struct hv_vmbus_device_id *dev_id)
  1082. {
  1083. struct net_device *net = NULL;
  1084. struct net_device_context *net_device_ctx;
  1085. struct netvsc_device_info device_info;
  1086. struct netvsc_device *nvdev;
  1087. int ret;
  1088. net = alloc_etherdev_mq(sizeof(struct net_device_context),
  1089. num_online_cpus());
  1090. if (!net)
  1091. return -ENOMEM;
  1092. netif_carrier_off(net);
  1093. netvsc_init_settings(net);
  1094. net_device_ctx = netdev_priv(net);
  1095. net_device_ctx->device_ctx = dev;
  1096. net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
  1097. if (netif_msg_probe(net_device_ctx))
  1098. netdev_dbg(net, "netvsc msg_enable: %d\n",
  1099. net_device_ctx->msg_enable);
  1100. net_device_ctx->tx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
  1101. if (!net_device_ctx->tx_stats) {
  1102. free_netdev(net);
  1103. return -ENOMEM;
  1104. }
  1105. net_device_ctx->rx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
  1106. if (!net_device_ctx->rx_stats) {
  1107. free_percpu(net_device_ctx->tx_stats);
  1108. free_netdev(net);
  1109. return -ENOMEM;
  1110. }
  1111. hv_set_drvdata(dev, net);
  1112. net_device_ctx->start_remove = false;
  1113. INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
  1114. INIT_WORK(&net_device_ctx->work, do_set_multicast);
  1115. spin_lock_init(&net_device_ctx->lock);
  1116. INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
  1117. net->netdev_ops = &device_ops;
  1118. net->hw_features = NETVSC_HW_FEATURES;
  1119. net->features = NETVSC_HW_FEATURES | NETIF_F_HW_VLAN_CTAG_TX;
  1120. net->ethtool_ops = &ethtool_ops;
  1121. SET_NETDEV_DEV(net, &dev->device);
  1122. /* We always need headroom for rndis header */
  1123. net->needed_headroom = RNDIS_AND_PPI_SIZE;
  1124. /* Notify the netvsc driver of the new device */
  1125. memset(&device_info, 0, sizeof(device_info));
  1126. device_info.ring_size = ring_size;
  1127. device_info.max_num_vrss_chns = max_num_vrss_chns;
  1128. ret = rndis_filter_device_add(dev, &device_info);
  1129. if (ret != 0) {
  1130. netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
  1131. netvsc_free_netdev(net);
  1132. hv_set_drvdata(dev, NULL);
  1133. return ret;
  1134. }
  1135. memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
  1136. nvdev = net_device_ctx->nvdev;
  1137. netif_set_real_num_tx_queues(net, nvdev->num_chn);
  1138. netif_set_real_num_rx_queues(net, nvdev->num_chn);
  1139. netif_set_gso_max_size(net, NETVSC_GSO_MAX_SIZE);
  1140. /* MTU range: 68 - 1500 or 65521 */
  1141. net->min_mtu = NETVSC_MTU_MIN;
  1142. if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
  1143. net->max_mtu = NETVSC_MTU - ETH_HLEN;
  1144. else
  1145. net->max_mtu = ETH_DATA_LEN;
  1146. ret = register_netdev(net);
  1147. if (ret != 0) {
  1148. pr_err("Unable to register netdev.\n");
  1149. rndis_filter_device_remove(dev);
  1150. netvsc_free_netdev(net);
  1151. }
  1152. return ret;
  1153. }
  1154. static int netvsc_remove(struct hv_device *dev)
  1155. {
  1156. struct net_device *net;
  1157. struct net_device_context *ndev_ctx;
  1158. struct netvsc_device *net_device;
  1159. net = hv_get_drvdata(dev);
  1160. if (net == NULL) {
  1161. dev_err(&dev->device, "No net device to remove\n");
  1162. return 0;
  1163. }
  1164. ndev_ctx = netdev_priv(net);
  1165. net_device = ndev_ctx->nvdev;
  1166. /* Avoid racing with netvsc_change_mtu()/netvsc_set_channels()
  1167. * removing the device.
  1168. */
  1169. rtnl_lock();
  1170. ndev_ctx->start_remove = true;
  1171. rtnl_unlock();
  1172. cancel_delayed_work_sync(&ndev_ctx->dwork);
  1173. cancel_work_sync(&ndev_ctx->work);
  1174. /* Stop outbound asap */
  1175. netif_tx_disable(net);
  1176. unregister_netdev(net);
  1177. /*
  1178. * Call to the vsc driver to let it know that the device is being
  1179. * removed
  1180. */
  1181. rndis_filter_device_remove(dev);
  1182. hv_set_drvdata(dev, NULL);
  1183. netvsc_free_netdev(net);
  1184. return 0;
  1185. }
  1186. static const struct hv_vmbus_device_id id_table[] = {
  1187. /* Network guid */
  1188. { HV_NIC_GUID, },
  1189. { },
  1190. };
  1191. MODULE_DEVICE_TABLE(vmbus, id_table);
  1192. /* The one and only one */
  1193. static struct hv_driver netvsc_drv = {
  1194. .name = KBUILD_MODNAME,
  1195. .id_table = id_table,
  1196. .probe = netvsc_probe,
  1197. .remove = netvsc_remove,
  1198. };
  1199. /*
  1200. * On Hyper-V, every VF interface is matched with a corresponding
  1201. * synthetic interface. The synthetic interface is presented first
  1202. * to the guest. When the corresponding VF instance is registered,
  1203. * we will take care of switching the data path.
  1204. */
  1205. static int netvsc_netdev_event(struct notifier_block *this,
  1206. unsigned long event, void *ptr)
  1207. {
  1208. struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
  1209. /* Skip our own events */
  1210. if (event_dev->netdev_ops == &device_ops)
  1211. return NOTIFY_DONE;
  1212. /* Avoid non-Ethernet type devices */
  1213. if (event_dev->type != ARPHRD_ETHER)
  1214. return NOTIFY_DONE;
  1215. /* Avoid Vlan dev with same MAC registering as VF */
  1216. if (event_dev->priv_flags & IFF_802_1Q_VLAN)
  1217. return NOTIFY_DONE;
  1218. /* Avoid Bonding master dev with same MAC registering as VF */
  1219. if ((event_dev->priv_flags & IFF_BONDING) &&
  1220. (event_dev->flags & IFF_MASTER))
  1221. return NOTIFY_DONE;
  1222. switch (event) {
  1223. case NETDEV_REGISTER:
  1224. return netvsc_register_vf(event_dev);
  1225. case NETDEV_UNREGISTER:
  1226. return netvsc_unregister_vf(event_dev);
  1227. case NETDEV_UP:
  1228. return netvsc_vf_up(event_dev);
  1229. case NETDEV_DOWN:
  1230. return netvsc_vf_down(event_dev);
  1231. default:
  1232. return NOTIFY_DONE;
  1233. }
  1234. }
  1235. static struct notifier_block netvsc_netdev_notifier = {
  1236. .notifier_call = netvsc_netdev_event,
  1237. };
  1238. static void __exit netvsc_drv_exit(void)
  1239. {
  1240. unregister_netdevice_notifier(&netvsc_netdev_notifier);
  1241. vmbus_driver_unregister(&netvsc_drv);
  1242. }
  1243. static int __init netvsc_drv_init(void)
  1244. {
  1245. int ret;
  1246. if (ring_size < RING_SIZE_MIN) {
  1247. ring_size = RING_SIZE_MIN;
  1248. pr_info("Increased ring_size to %d (min allowed)\n",
  1249. ring_size);
  1250. }
  1251. ret = vmbus_driver_register(&netvsc_drv);
  1252. if (ret)
  1253. return ret;
  1254. register_netdevice_notifier(&netvsc_netdev_notifier);
  1255. return 0;
  1256. }
  1257. MODULE_LICENSE("GPL");
  1258. MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
  1259. module_init(netvsc_drv_init);
  1260. module_exit(netvsc_drv_exit);