netvsc.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2009, Microsoft Corporation.
  4. *
  5. * Authors:
  6. * Haiyang Zhang <haiyangz@microsoft.com>
  7. * Hank Janssen <hjanssen@microsoft.com>
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/kernel.h>
  11. #include <linux/sched.h>
  12. #include <linux/wait.h>
  13. #include <linux/mm.h>
  14. #include <linux/delay.h>
  15. #include <linux/io.h>
  16. #include <linux/slab.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/if_ether.h>
  19. #include <linux/vmalloc.h>
  20. #include <linux/rtnetlink.h>
  21. #include <linux/prefetch.h>
  22. #include <asm/sync_bitops.h>
  23. #include "hyperv_net.h"
  24. #include "netvsc_trace.h"
  25. /*
  26. * Switch the data path from the synthetic interface to the VF
  27. * interface.
  28. */
  29. void netvsc_switch_datapath(struct net_device *ndev, bool vf)
  30. {
  31. struct net_device_context *net_device_ctx = netdev_priv(ndev);
  32. struct hv_device *dev = net_device_ctx->device_ctx;
  33. struct netvsc_device *nv_dev = rtnl_dereference(net_device_ctx->nvdev);
  34. struct nvsp_message *init_pkt = &nv_dev->channel_init_pkt;
  35. memset(init_pkt, 0, sizeof(struct nvsp_message));
  36. init_pkt->hdr.msg_type = NVSP_MSG4_TYPE_SWITCH_DATA_PATH;
  37. if (vf)
  38. init_pkt->msg.v4_msg.active_dp.active_datapath =
  39. NVSP_DATAPATH_VF;
  40. else
  41. init_pkt->msg.v4_msg.active_dp.active_datapath =
  42. NVSP_DATAPATH_SYNTHETIC;
  43. trace_nvsp_send(ndev, init_pkt);
  44. vmbus_sendpacket(dev->channel, init_pkt,
  45. sizeof(struct nvsp_message),
  46. (unsigned long)init_pkt,
  47. VM_PKT_DATA_INBAND, 0);
  48. }
  49. /* Worker to setup sub channels on initial setup
  50. * Initial hotplug event occurs in softirq context
  51. * and can't wait for channels.
  52. */
  53. static void netvsc_subchan_work(struct work_struct *w)
  54. {
  55. struct netvsc_device *nvdev =
  56. container_of(w, struct netvsc_device, subchan_work);
  57. struct rndis_device *rdev;
  58. int i, ret;
  59. /* Avoid deadlock with device removal already under RTNL */
  60. if (!rtnl_trylock()) {
  61. schedule_work(w);
  62. return;
  63. }
  64. rdev = nvdev->extension;
  65. if (rdev) {
  66. ret = rndis_set_subchannel(rdev->ndev, nvdev, NULL);
  67. if (ret == 0) {
  68. netif_device_attach(rdev->ndev);
  69. } else {
  70. /* fallback to only primary channel */
  71. for (i = 1; i < nvdev->num_chn; i++)
  72. netif_napi_del(&nvdev->chan_table[i].napi);
  73. nvdev->max_chn = 1;
  74. nvdev->num_chn = 1;
  75. }
  76. }
  77. rtnl_unlock();
  78. }
  79. static struct netvsc_device *alloc_net_device(void)
  80. {
  81. struct netvsc_device *net_device;
  82. net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
  83. if (!net_device)
  84. return NULL;
  85. init_waitqueue_head(&net_device->wait_drain);
  86. net_device->destroy = false;
  87. net_device->tx_disable = true;
  88. net_device->max_pkt = RNDIS_MAX_PKT_DEFAULT;
  89. net_device->pkt_align = RNDIS_PKT_ALIGN_DEFAULT;
  90. init_completion(&net_device->channel_init_wait);
  91. init_waitqueue_head(&net_device->subchan_open);
  92. INIT_WORK(&net_device->subchan_work, netvsc_subchan_work);
  93. return net_device;
  94. }
  95. static void free_netvsc_device(struct rcu_head *head)
  96. {
  97. struct netvsc_device *nvdev
  98. = container_of(head, struct netvsc_device, rcu);
  99. int i;
  100. kfree(nvdev->extension);
  101. vfree(nvdev->recv_buf);
  102. vfree(nvdev->send_buf);
  103. kfree(nvdev->send_section_map);
  104. for (i = 0; i < VRSS_CHANNEL_MAX; i++)
  105. vfree(nvdev->chan_table[i].mrc.slots);
  106. kfree(nvdev);
  107. }
  108. static void free_netvsc_device_rcu(struct netvsc_device *nvdev)
  109. {
  110. call_rcu(&nvdev->rcu, free_netvsc_device);
  111. }
  112. static void netvsc_revoke_recv_buf(struct hv_device *device,
  113. struct netvsc_device *net_device,
  114. struct net_device *ndev)
  115. {
  116. struct nvsp_message *revoke_packet;
  117. int ret;
  118. /*
  119. * If we got a section count, it means we received a
  120. * SendReceiveBufferComplete msg (ie sent
  121. * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
  122. * to send a revoke msg here
  123. */
  124. if (net_device->recv_section_cnt) {
  125. /* Send the revoke receive buffer */
  126. revoke_packet = &net_device->revoke_packet;
  127. memset(revoke_packet, 0, sizeof(struct nvsp_message));
  128. revoke_packet->hdr.msg_type =
  129. NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
  130. revoke_packet->msg.v1_msg.
  131. revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
  132. trace_nvsp_send(ndev, revoke_packet);
  133. ret = vmbus_sendpacket(device->channel,
  134. revoke_packet,
  135. sizeof(struct nvsp_message),
  136. (unsigned long)revoke_packet,
  137. VM_PKT_DATA_INBAND, 0);
  138. /* If the failure is because the channel is rescinded;
  139. * ignore the failure since we cannot send on a rescinded
  140. * channel. This would allow us to properly cleanup
  141. * even when the channel is rescinded.
  142. */
  143. if (device->channel->rescind)
  144. ret = 0;
  145. /*
  146. * If we failed here, we might as well return and
  147. * have a leak rather than continue and a bugchk
  148. */
  149. if (ret != 0) {
  150. netdev_err(ndev, "unable to send "
  151. "revoke receive buffer to netvsp\n");
  152. return;
  153. }
  154. net_device->recv_section_cnt = 0;
  155. }
  156. }
  157. static void netvsc_revoke_send_buf(struct hv_device *device,
  158. struct netvsc_device *net_device,
  159. struct net_device *ndev)
  160. {
  161. struct nvsp_message *revoke_packet;
  162. int ret;
  163. /* Deal with the send buffer we may have setup.
  164. * If we got a send section size, it means we received a
  165. * NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE msg (ie sent
  166. * NVSP_MSG1_TYPE_SEND_SEND_BUF msg) therefore, we need
  167. * to send a revoke msg here
  168. */
  169. if (net_device->send_section_cnt) {
  170. /* Send the revoke receive buffer */
  171. revoke_packet = &net_device->revoke_packet;
  172. memset(revoke_packet, 0, sizeof(struct nvsp_message));
  173. revoke_packet->hdr.msg_type =
  174. NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
  175. revoke_packet->msg.v1_msg.revoke_send_buf.id =
  176. NETVSC_SEND_BUFFER_ID;
  177. trace_nvsp_send(ndev, revoke_packet);
  178. ret = vmbus_sendpacket(device->channel,
  179. revoke_packet,
  180. sizeof(struct nvsp_message),
  181. (unsigned long)revoke_packet,
  182. VM_PKT_DATA_INBAND, 0);
  183. /* If the failure is because the channel is rescinded;
  184. * ignore the failure since we cannot send on a rescinded
  185. * channel. This would allow us to properly cleanup
  186. * even when the channel is rescinded.
  187. */
  188. if (device->channel->rescind)
  189. ret = 0;
  190. /* If we failed here, we might as well return and
  191. * have a leak rather than continue and a bugchk
  192. */
  193. if (ret != 0) {
  194. netdev_err(ndev, "unable to send "
  195. "revoke send buffer to netvsp\n");
  196. return;
  197. }
  198. net_device->send_section_cnt = 0;
  199. }
  200. }
  201. static void netvsc_teardown_recv_gpadl(struct hv_device *device,
  202. struct netvsc_device *net_device,
  203. struct net_device *ndev)
  204. {
  205. int ret;
  206. if (net_device->recv_buf_gpadl_handle) {
  207. ret = vmbus_teardown_gpadl(device->channel,
  208. net_device->recv_buf_gpadl_handle);
  209. /* If we failed here, we might as well return and have a leak
  210. * rather than continue and a bugchk
  211. */
  212. if (ret != 0) {
  213. netdev_err(ndev,
  214. "unable to teardown receive buffer's gpadl\n");
  215. return;
  216. }
  217. net_device->recv_buf_gpadl_handle = 0;
  218. }
  219. }
  220. static void netvsc_teardown_send_gpadl(struct hv_device *device,
  221. struct netvsc_device *net_device,
  222. struct net_device *ndev)
  223. {
  224. int ret;
  225. if (net_device->send_buf_gpadl_handle) {
  226. ret = vmbus_teardown_gpadl(device->channel,
  227. net_device->send_buf_gpadl_handle);
  228. /* If we failed here, we might as well return and have a leak
  229. * rather than continue and a bugchk
  230. */
  231. if (ret != 0) {
  232. netdev_err(ndev,
  233. "unable to teardown send buffer's gpadl\n");
  234. return;
  235. }
  236. net_device->send_buf_gpadl_handle = 0;
  237. }
  238. }
  239. int netvsc_alloc_recv_comp_ring(struct netvsc_device *net_device, u32 q_idx)
  240. {
  241. struct netvsc_channel *nvchan = &net_device->chan_table[q_idx];
  242. int node = cpu_to_node(nvchan->channel->target_cpu);
  243. size_t size;
  244. size = net_device->recv_completion_cnt * sizeof(struct recv_comp_data);
  245. nvchan->mrc.slots = vzalloc_node(size, node);
  246. if (!nvchan->mrc.slots)
  247. nvchan->mrc.slots = vzalloc(size);
  248. return nvchan->mrc.slots ? 0 : -ENOMEM;
  249. }
  250. static int netvsc_init_buf(struct hv_device *device,
  251. struct netvsc_device *net_device,
  252. const struct netvsc_device_info *device_info)
  253. {
  254. struct nvsp_1_message_send_receive_buffer_complete *resp;
  255. struct net_device *ndev = hv_get_drvdata(device);
  256. struct nvsp_message *init_packet;
  257. unsigned int buf_size;
  258. size_t map_words;
  259. int ret = 0;
  260. /* Get receive buffer area. */
  261. buf_size = device_info->recv_sections * device_info->recv_section_size;
  262. buf_size = roundup(buf_size, PAGE_SIZE);
  263. /* Legacy hosts only allow smaller receive buffer */
  264. if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
  265. buf_size = min_t(unsigned int, buf_size,
  266. NETVSC_RECEIVE_BUFFER_SIZE_LEGACY);
  267. net_device->recv_buf = vzalloc(buf_size);
  268. if (!net_device->recv_buf) {
  269. netdev_err(ndev,
  270. "unable to allocate receive buffer of size %u\n",
  271. buf_size);
  272. ret = -ENOMEM;
  273. goto cleanup;
  274. }
  275. net_device->recv_buf_size = buf_size;
  276. /*
  277. * Establish the gpadl handle for this buffer on this
  278. * channel. Note: This call uses the vmbus connection rather
  279. * than the channel to establish the gpadl handle.
  280. */
  281. ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
  282. buf_size,
  283. &net_device->recv_buf_gpadl_handle);
  284. if (ret != 0) {
  285. netdev_err(ndev,
  286. "unable to establish receive buffer's gpadl\n");
  287. goto cleanup;
  288. }
  289. /* Notify the NetVsp of the gpadl handle */
  290. init_packet = &net_device->channel_init_pkt;
  291. memset(init_packet, 0, sizeof(struct nvsp_message));
  292. init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
  293. init_packet->msg.v1_msg.send_recv_buf.
  294. gpadl_handle = net_device->recv_buf_gpadl_handle;
  295. init_packet->msg.v1_msg.
  296. send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
  297. trace_nvsp_send(ndev, init_packet);
  298. /* Send the gpadl notification request */
  299. ret = vmbus_sendpacket(device->channel, init_packet,
  300. sizeof(struct nvsp_message),
  301. (unsigned long)init_packet,
  302. VM_PKT_DATA_INBAND,
  303. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  304. if (ret != 0) {
  305. netdev_err(ndev,
  306. "unable to send receive buffer's gpadl to netvsp\n");
  307. goto cleanup;
  308. }
  309. wait_for_completion(&net_device->channel_init_wait);
  310. /* Check the response */
  311. resp = &init_packet->msg.v1_msg.send_recv_buf_complete;
  312. if (resp->status != NVSP_STAT_SUCCESS) {
  313. netdev_err(ndev,
  314. "Unable to complete receive buffer initialization with NetVsp - status %d\n",
  315. resp->status);
  316. ret = -EINVAL;
  317. goto cleanup;
  318. }
  319. /* Parse the response */
  320. netdev_dbg(ndev, "Receive sections: %u sub_allocs: size %u count: %u\n",
  321. resp->num_sections, resp->sections[0].sub_alloc_size,
  322. resp->sections[0].num_sub_allocs);
  323. /* There should only be one section for the entire receive buffer */
  324. if (resp->num_sections != 1 || resp->sections[0].offset != 0) {
  325. ret = -EINVAL;
  326. goto cleanup;
  327. }
  328. net_device->recv_section_size = resp->sections[0].sub_alloc_size;
  329. net_device->recv_section_cnt = resp->sections[0].num_sub_allocs;
  330. /* Setup receive completion ring */
  331. net_device->recv_completion_cnt
  332. = round_up(net_device->recv_section_cnt + 1,
  333. PAGE_SIZE / sizeof(u64));
  334. ret = netvsc_alloc_recv_comp_ring(net_device, 0);
  335. if (ret)
  336. goto cleanup;
  337. /* Now setup the send buffer. */
  338. buf_size = device_info->send_sections * device_info->send_section_size;
  339. buf_size = round_up(buf_size, PAGE_SIZE);
  340. net_device->send_buf = vzalloc(buf_size);
  341. if (!net_device->send_buf) {
  342. netdev_err(ndev, "unable to allocate send buffer of size %u\n",
  343. buf_size);
  344. ret = -ENOMEM;
  345. goto cleanup;
  346. }
  347. /* Establish the gpadl handle for this buffer on this
  348. * channel. Note: This call uses the vmbus connection rather
  349. * than the channel to establish the gpadl handle.
  350. */
  351. ret = vmbus_establish_gpadl(device->channel, net_device->send_buf,
  352. buf_size,
  353. &net_device->send_buf_gpadl_handle);
  354. if (ret != 0) {
  355. netdev_err(ndev,
  356. "unable to establish send buffer's gpadl\n");
  357. goto cleanup;
  358. }
  359. /* Notify the NetVsp of the gpadl handle */
  360. init_packet = &net_device->channel_init_pkt;
  361. memset(init_packet, 0, sizeof(struct nvsp_message));
  362. init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF;
  363. init_packet->msg.v1_msg.send_send_buf.gpadl_handle =
  364. net_device->send_buf_gpadl_handle;
  365. init_packet->msg.v1_msg.send_send_buf.id = NETVSC_SEND_BUFFER_ID;
  366. trace_nvsp_send(ndev, init_packet);
  367. /* Send the gpadl notification request */
  368. ret = vmbus_sendpacket(device->channel, init_packet,
  369. sizeof(struct nvsp_message),
  370. (unsigned long)init_packet,
  371. VM_PKT_DATA_INBAND,
  372. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  373. if (ret != 0) {
  374. netdev_err(ndev,
  375. "unable to send send buffer's gpadl to netvsp\n");
  376. goto cleanup;
  377. }
  378. wait_for_completion(&net_device->channel_init_wait);
  379. /* Check the response */
  380. if (init_packet->msg.v1_msg.
  381. send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
  382. netdev_err(ndev, "Unable to complete send buffer "
  383. "initialization with NetVsp - status %d\n",
  384. init_packet->msg.v1_msg.
  385. send_send_buf_complete.status);
  386. ret = -EINVAL;
  387. goto cleanup;
  388. }
  389. /* Parse the response */
  390. net_device->send_section_size = init_packet->msg.
  391. v1_msg.send_send_buf_complete.section_size;
  392. /* Section count is simply the size divided by the section size. */
  393. net_device->send_section_cnt = buf_size / net_device->send_section_size;
  394. netdev_dbg(ndev, "Send section size: %d, Section count:%d\n",
  395. net_device->send_section_size, net_device->send_section_cnt);
  396. /* Setup state for managing the send buffer. */
  397. map_words = DIV_ROUND_UP(net_device->send_section_cnt, BITS_PER_LONG);
  398. net_device->send_section_map = kcalloc(map_words, sizeof(ulong), GFP_KERNEL);
  399. if (net_device->send_section_map == NULL) {
  400. ret = -ENOMEM;
  401. goto cleanup;
  402. }
  403. goto exit;
  404. cleanup:
  405. netvsc_revoke_recv_buf(device, net_device, ndev);
  406. netvsc_revoke_send_buf(device, net_device, ndev);
  407. netvsc_teardown_recv_gpadl(device, net_device, ndev);
  408. netvsc_teardown_send_gpadl(device, net_device, ndev);
  409. exit:
  410. return ret;
  411. }
  412. /* Negotiate NVSP protocol version */
  413. static int negotiate_nvsp_ver(struct hv_device *device,
  414. struct netvsc_device *net_device,
  415. struct nvsp_message *init_packet,
  416. u32 nvsp_ver)
  417. {
  418. struct net_device *ndev = hv_get_drvdata(device);
  419. int ret;
  420. memset(init_packet, 0, sizeof(struct nvsp_message));
  421. init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
  422. init_packet->msg.init_msg.init.min_protocol_ver = nvsp_ver;
  423. init_packet->msg.init_msg.init.max_protocol_ver = nvsp_ver;
  424. trace_nvsp_send(ndev, init_packet);
  425. /* Send the init request */
  426. ret = vmbus_sendpacket(device->channel, init_packet,
  427. sizeof(struct nvsp_message),
  428. (unsigned long)init_packet,
  429. VM_PKT_DATA_INBAND,
  430. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  431. if (ret != 0)
  432. return ret;
  433. wait_for_completion(&net_device->channel_init_wait);
  434. if (init_packet->msg.init_msg.init_complete.status !=
  435. NVSP_STAT_SUCCESS)
  436. return -EINVAL;
  437. if (nvsp_ver == NVSP_PROTOCOL_VERSION_1)
  438. return 0;
  439. /* NVSPv2 or later: Send NDIS config */
  440. memset(init_packet, 0, sizeof(struct nvsp_message));
  441. init_packet->hdr.msg_type = NVSP_MSG2_TYPE_SEND_NDIS_CONFIG;
  442. init_packet->msg.v2_msg.send_ndis_config.mtu = ndev->mtu + ETH_HLEN;
  443. init_packet->msg.v2_msg.send_ndis_config.capability.ieee8021q = 1;
  444. if (nvsp_ver >= NVSP_PROTOCOL_VERSION_5) {
  445. init_packet->msg.v2_msg.send_ndis_config.capability.sriov = 1;
  446. /* Teaming bit is needed to receive link speed updates */
  447. init_packet->msg.v2_msg.send_ndis_config.capability.teaming = 1;
  448. }
  449. if (nvsp_ver >= NVSP_PROTOCOL_VERSION_61)
  450. init_packet->msg.v2_msg.send_ndis_config.capability.rsc = 1;
  451. trace_nvsp_send(ndev, init_packet);
  452. ret = vmbus_sendpacket(device->channel, init_packet,
  453. sizeof(struct nvsp_message),
  454. (unsigned long)init_packet,
  455. VM_PKT_DATA_INBAND, 0);
  456. return ret;
  457. }
  458. static int netvsc_connect_vsp(struct hv_device *device,
  459. struct netvsc_device *net_device,
  460. const struct netvsc_device_info *device_info)
  461. {
  462. struct net_device *ndev = hv_get_drvdata(device);
  463. static const u32 ver_list[] = {
  464. NVSP_PROTOCOL_VERSION_1, NVSP_PROTOCOL_VERSION_2,
  465. NVSP_PROTOCOL_VERSION_4, NVSP_PROTOCOL_VERSION_5,
  466. NVSP_PROTOCOL_VERSION_6, NVSP_PROTOCOL_VERSION_61
  467. };
  468. struct nvsp_message *init_packet;
  469. int ndis_version, i, ret;
  470. init_packet = &net_device->channel_init_pkt;
  471. /* Negotiate the latest NVSP protocol supported */
  472. for (i = ARRAY_SIZE(ver_list) - 1; i >= 0; i--)
  473. if (negotiate_nvsp_ver(device, net_device, init_packet,
  474. ver_list[i]) == 0) {
  475. net_device->nvsp_version = ver_list[i];
  476. break;
  477. }
  478. if (i < 0) {
  479. ret = -EPROTO;
  480. goto cleanup;
  481. }
  482. pr_debug("Negotiated NVSP version:%x\n", net_device->nvsp_version);
  483. /* Send the ndis version */
  484. memset(init_packet, 0, sizeof(struct nvsp_message));
  485. if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_4)
  486. ndis_version = 0x00060001;
  487. else
  488. ndis_version = 0x0006001e;
  489. init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
  490. init_packet->msg.v1_msg.
  491. send_ndis_ver.ndis_major_ver =
  492. (ndis_version & 0xFFFF0000) >> 16;
  493. init_packet->msg.v1_msg.
  494. send_ndis_ver.ndis_minor_ver =
  495. ndis_version & 0xFFFF;
  496. trace_nvsp_send(ndev, init_packet);
  497. /* Send the init request */
  498. ret = vmbus_sendpacket(device->channel, init_packet,
  499. sizeof(struct nvsp_message),
  500. (unsigned long)init_packet,
  501. VM_PKT_DATA_INBAND, 0);
  502. if (ret != 0)
  503. goto cleanup;
  504. ret = netvsc_init_buf(device, net_device, device_info);
  505. cleanup:
  506. return ret;
  507. }
  508. /*
  509. * netvsc_device_remove - Callback when the root bus device is removed
  510. */
  511. void netvsc_device_remove(struct hv_device *device)
  512. {
  513. struct net_device *ndev = hv_get_drvdata(device);
  514. struct net_device_context *net_device_ctx = netdev_priv(ndev);
  515. struct netvsc_device *net_device
  516. = rtnl_dereference(net_device_ctx->nvdev);
  517. int i;
  518. /*
  519. * Revoke receive buffer. If host is pre-Win2016 then tear down
  520. * receive buffer GPADL. Do the same for send buffer.
  521. */
  522. netvsc_revoke_recv_buf(device, net_device, ndev);
  523. if (vmbus_proto_version < VERSION_WIN10)
  524. netvsc_teardown_recv_gpadl(device, net_device, ndev);
  525. netvsc_revoke_send_buf(device, net_device, ndev);
  526. if (vmbus_proto_version < VERSION_WIN10)
  527. netvsc_teardown_send_gpadl(device, net_device, ndev);
  528. RCU_INIT_POINTER(net_device_ctx->nvdev, NULL);
  529. /* And disassociate NAPI context from device */
  530. for (i = 0; i < net_device->num_chn; i++)
  531. netif_napi_del(&net_device->chan_table[i].napi);
  532. /*
  533. * At this point, no one should be accessing net_device
  534. * except in here
  535. */
  536. netdev_dbg(ndev, "net device safe to remove\n");
  537. /* Now, we can close the channel safely */
  538. vmbus_close(device->channel);
  539. /*
  540. * If host is Win2016 or higher then we do the GPADL tear down
  541. * here after VMBus is closed.
  542. */
  543. if (vmbus_proto_version >= VERSION_WIN10) {
  544. netvsc_teardown_recv_gpadl(device, net_device, ndev);
  545. netvsc_teardown_send_gpadl(device, net_device, ndev);
  546. }
  547. /* Release all resources */
  548. free_netvsc_device_rcu(net_device);
  549. }
  550. #define RING_AVAIL_PERCENT_HIWATER 20
  551. #define RING_AVAIL_PERCENT_LOWATER 10
  552. static inline void netvsc_free_send_slot(struct netvsc_device *net_device,
  553. u32 index)
  554. {
  555. sync_change_bit(index, net_device->send_section_map);
  556. }
  557. static void netvsc_send_tx_complete(struct net_device *ndev,
  558. struct netvsc_device *net_device,
  559. struct vmbus_channel *channel,
  560. const struct vmpacket_descriptor *desc,
  561. int budget)
  562. {
  563. struct sk_buff *skb = (struct sk_buff *)(unsigned long)desc->trans_id;
  564. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  565. u16 q_idx = 0;
  566. int queue_sends;
  567. /* Notify the layer above us */
  568. if (likely(skb)) {
  569. const struct hv_netvsc_packet *packet
  570. = (struct hv_netvsc_packet *)skb->cb;
  571. u32 send_index = packet->send_buf_index;
  572. struct netvsc_stats *tx_stats;
  573. if (send_index != NETVSC_INVALID_INDEX)
  574. netvsc_free_send_slot(net_device, send_index);
  575. q_idx = packet->q_idx;
  576. tx_stats = &net_device->chan_table[q_idx].tx_stats;
  577. u64_stats_update_begin(&tx_stats->syncp);
  578. tx_stats->packets += packet->total_packets;
  579. tx_stats->bytes += packet->total_bytes;
  580. u64_stats_update_end(&tx_stats->syncp);
  581. napi_consume_skb(skb, budget);
  582. }
  583. queue_sends =
  584. atomic_dec_return(&net_device->chan_table[q_idx].queue_sends);
  585. if (unlikely(net_device->destroy)) {
  586. if (queue_sends == 0)
  587. wake_up(&net_device->wait_drain);
  588. } else {
  589. struct netdev_queue *txq = netdev_get_tx_queue(ndev, q_idx);
  590. if (netif_tx_queue_stopped(txq) && !net_device->tx_disable &&
  591. (hv_get_avail_to_write_percent(&channel->outbound) >
  592. RING_AVAIL_PERCENT_HIWATER || queue_sends < 1)) {
  593. netif_tx_wake_queue(txq);
  594. ndev_ctx->eth_stats.wake_queue++;
  595. }
  596. }
  597. }
  598. static void netvsc_send_completion(struct net_device *ndev,
  599. struct netvsc_device *net_device,
  600. struct vmbus_channel *incoming_channel,
  601. const struct vmpacket_descriptor *desc,
  602. int budget)
  603. {
  604. const struct nvsp_message *nvsp_packet = hv_pkt_data(desc);
  605. switch (nvsp_packet->hdr.msg_type) {
  606. case NVSP_MSG_TYPE_INIT_COMPLETE:
  607. case NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE:
  608. case NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE:
  609. case NVSP_MSG5_TYPE_SUBCHANNEL:
  610. /* Copy the response back */
  611. memcpy(&net_device->channel_init_pkt, nvsp_packet,
  612. sizeof(struct nvsp_message));
  613. complete(&net_device->channel_init_wait);
  614. break;
  615. case NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE:
  616. netvsc_send_tx_complete(ndev, net_device, incoming_channel,
  617. desc, budget);
  618. break;
  619. default:
  620. netdev_err(ndev,
  621. "Unknown send completion type %d received!!\n",
  622. nvsp_packet->hdr.msg_type);
  623. }
  624. }
  625. static u32 netvsc_get_next_send_section(struct netvsc_device *net_device)
  626. {
  627. unsigned long *map_addr = net_device->send_section_map;
  628. unsigned int i;
  629. for_each_clear_bit(i, map_addr, net_device->send_section_cnt) {
  630. if (sync_test_and_set_bit(i, map_addr) == 0)
  631. return i;
  632. }
  633. return NETVSC_INVALID_INDEX;
  634. }
  635. static void netvsc_copy_to_send_buf(struct netvsc_device *net_device,
  636. unsigned int section_index,
  637. u32 pend_size,
  638. struct hv_netvsc_packet *packet,
  639. struct rndis_message *rndis_msg,
  640. struct hv_page_buffer *pb,
  641. bool xmit_more)
  642. {
  643. char *start = net_device->send_buf;
  644. char *dest = start + (section_index * net_device->send_section_size)
  645. + pend_size;
  646. int i;
  647. u32 padding = 0;
  648. u32 page_count = packet->cp_partial ? packet->rmsg_pgcnt :
  649. packet->page_buf_cnt;
  650. u32 remain;
  651. /* Add padding */
  652. remain = packet->total_data_buflen & (net_device->pkt_align - 1);
  653. if (xmit_more && remain) {
  654. padding = net_device->pkt_align - remain;
  655. rndis_msg->msg_len += padding;
  656. packet->total_data_buflen += padding;
  657. }
  658. for (i = 0; i < page_count; i++) {
  659. char *src = phys_to_virt(pb[i].pfn << PAGE_SHIFT);
  660. u32 offset = pb[i].offset;
  661. u32 len = pb[i].len;
  662. memcpy(dest, (src + offset), len);
  663. dest += len;
  664. }
  665. if (padding)
  666. memset(dest, 0, padding);
  667. }
  668. static inline int netvsc_send_pkt(
  669. struct hv_device *device,
  670. struct hv_netvsc_packet *packet,
  671. struct netvsc_device *net_device,
  672. struct hv_page_buffer *pb,
  673. struct sk_buff *skb)
  674. {
  675. struct nvsp_message nvmsg;
  676. struct nvsp_1_message_send_rndis_packet *rpkt =
  677. &nvmsg.msg.v1_msg.send_rndis_pkt;
  678. struct netvsc_channel * const nvchan =
  679. &net_device->chan_table[packet->q_idx];
  680. struct vmbus_channel *out_channel = nvchan->channel;
  681. struct net_device *ndev = hv_get_drvdata(device);
  682. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  683. struct netdev_queue *txq = netdev_get_tx_queue(ndev, packet->q_idx);
  684. u64 req_id;
  685. int ret;
  686. u32 ring_avail = hv_get_avail_to_write_percent(&out_channel->outbound);
  687. nvmsg.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
  688. if (skb)
  689. rpkt->channel_type = 0; /* 0 is RMC_DATA */
  690. else
  691. rpkt->channel_type = 1; /* 1 is RMC_CONTROL */
  692. rpkt->send_buf_section_index = packet->send_buf_index;
  693. if (packet->send_buf_index == NETVSC_INVALID_INDEX)
  694. rpkt->send_buf_section_size = 0;
  695. else
  696. rpkt->send_buf_section_size = packet->total_data_buflen;
  697. req_id = (ulong)skb;
  698. if (out_channel->rescind)
  699. return -ENODEV;
  700. trace_nvsp_send_pkt(ndev, out_channel, rpkt);
  701. if (packet->page_buf_cnt) {
  702. if (packet->cp_partial)
  703. pb += packet->rmsg_pgcnt;
  704. ret = vmbus_sendpacket_pagebuffer(out_channel,
  705. pb, packet->page_buf_cnt,
  706. &nvmsg, sizeof(nvmsg),
  707. req_id);
  708. } else {
  709. ret = vmbus_sendpacket(out_channel,
  710. &nvmsg, sizeof(nvmsg),
  711. req_id, VM_PKT_DATA_INBAND,
  712. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  713. }
  714. if (ret == 0) {
  715. atomic_inc_return(&nvchan->queue_sends);
  716. if (ring_avail < RING_AVAIL_PERCENT_LOWATER) {
  717. netif_tx_stop_queue(txq);
  718. ndev_ctx->eth_stats.stop_queue++;
  719. }
  720. } else if (ret == -EAGAIN) {
  721. netif_tx_stop_queue(txq);
  722. ndev_ctx->eth_stats.stop_queue++;
  723. } else {
  724. netdev_err(ndev,
  725. "Unable to send packet pages %u len %u, ret %d\n",
  726. packet->page_buf_cnt, packet->total_data_buflen,
  727. ret);
  728. }
  729. if (netif_tx_queue_stopped(txq) &&
  730. atomic_read(&nvchan->queue_sends) < 1 &&
  731. !net_device->tx_disable) {
  732. netif_tx_wake_queue(txq);
  733. ndev_ctx->eth_stats.wake_queue++;
  734. if (ret == -EAGAIN)
  735. ret = -ENOSPC;
  736. }
  737. return ret;
  738. }
  739. /* Move packet out of multi send data (msd), and clear msd */
  740. static inline void move_pkt_msd(struct hv_netvsc_packet **msd_send,
  741. struct sk_buff **msd_skb,
  742. struct multi_send_data *msdp)
  743. {
  744. *msd_skb = msdp->skb;
  745. *msd_send = msdp->pkt;
  746. msdp->skb = NULL;
  747. msdp->pkt = NULL;
  748. msdp->count = 0;
  749. }
  750. /* RCU already held by caller */
  751. int netvsc_send(struct net_device *ndev,
  752. struct hv_netvsc_packet *packet,
  753. struct rndis_message *rndis_msg,
  754. struct hv_page_buffer *pb,
  755. struct sk_buff *skb)
  756. {
  757. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  758. struct netvsc_device *net_device
  759. = rcu_dereference_bh(ndev_ctx->nvdev);
  760. struct hv_device *device = ndev_ctx->device_ctx;
  761. int ret = 0;
  762. struct netvsc_channel *nvchan;
  763. u32 pktlen = packet->total_data_buflen, msd_len = 0;
  764. unsigned int section_index = NETVSC_INVALID_INDEX;
  765. struct multi_send_data *msdp;
  766. struct hv_netvsc_packet *msd_send = NULL, *cur_send = NULL;
  767. struct sk_buff *msd_skb = NULL;
  768. bool try_batch, xmit_more;
  769. /* If device is rescinded, return error and packet will get dropped. */
  770. if (unlikely(!net_device || net_device->destroy))
  771. return -ENODEV;
  772. nvchan = &net_device->chan_table[packet->q_idx];
  773. packet->send_buf_index = NETVSC_INVALID_INDEX;
  774. packet->cp_partial = false;
  775. /* Send control message directly without accessing msd (Multi-Send
  776. * Data) field which may be changed during data packet processing.
  777. */
  778. if (!skb)
  779. return netvsc_send_pkt(device, packet, net_device, pb, skb);
  780. /* batch packets in send buffer if possible */
  781. msdp = &nvchan->msd;
  782. if (msdp->pkt)
  783. msd_len = msdp->pkt->total_data_buflen;
  784. try_batch = msd_len > 0 && msdp->count < net_device->max_pkt;
  785. if (try_batch && msd_len + pktlen + net_device->pkt_align <
  786. net_device->send_section_size) {
  787. section_index = msdp->pkt->send_buf_index;
  788. } else if (try_batch && msd_len + packet->rmsg_size <
  789. net_device->send_section_size) {
  790. section_index = msdp->pkt->send_buf_index;
  791. packet->cp_partial = true;
  792. } else if (pktlen + net_device->pkt_align <
  793. net_device->send_section_size) {
  794. section_index = netvsc_get_next_send_section(net_device);
  795. if (unlikely(section_index == NETVSC_INVALID_INDEX)) {
  796. ++ndev_ctx->eth_stats.tx_send_full;
  797. } else {
  798. move_pkt_msd(&msd_send, &msd_skb, msdp);
  799. msd_len = 0;
  800. }
  801. }
  802. /* Keep aggregating only if stack says more data is coming
  803. * and not doing mixed modes send and not flow blocked
  804. */
  805. xmit_more = netdev_xmit_more() &&
  806. !packet->cp_partial &&
  807. !netif_xmit_stopped(netdev_get_tx_queue(ndev, packet->q_idx));
  808. if (section_index != NETVSC_INVALID_INDEX) {
  809. netvsc_copy_to_send_buf(net_device,
  810. section_index, msd_len,
  811. packet, rndis_msg, pb, xmit_more);
  812. packet->send_buf_index = section_index;
  813. if (packet->cp_partial) {
  814. packet->page_buf_cnt -= packet->rmsg_pgcnt;
  815. packet->total_data_buflen = msd_len + packet->rmsg_size;
  816. } else {
  817. packet->page_buf_cnt = 0;
  818. packet->total_data_buflen += msd_len;
  819. }
  820. if (msdp->pkt) {
  821. packet->total_packets += msdp->pkt->total_packets;
  822. packet->total_bytes += msdp->pkt->total_bytes;
  823. }
  824. if (msdp->skb)
  825. dev_consume_skb_any(msdp->skb);
  826. if (xmit_more) {
  827. msdp->skb = skb;
  828. msdp->pkt = packet;
  829. msdp->count++;
  830. } else {
  831. cur_send = packet;
  832. msdp->skb = NULL;
  833. msdp->pkt = NULL;
  834. msdp->count = 0;
  835. }
  836. } else {
  837. move_pkt_msd(&msd_send, &msd_skb, msdp);
  838. cur_send = packet;
  839. }
  840. if (msd_send) {
  841. int m_ret = netvsc_send_pkt(device, msd_send, net_device,
  842. NULL, msd_skb);
  843. if (m_ret != 0) {
  844. netvsc_free_send_slot(net_device,
  845. msd_send->send_buf_index);
  846. dev_kfree_skb_any(msd_skb);
  847. }
  848. }
  849. if (cur_send)
  850. ret = netvsc_send_pkt(device, cur_send, net_device, pb, skb);
  851. if (ret != 0 && section_index != NETVSC_INVALID_INDEX)
  852. netvsc_free_send_slot(net_device, section_index);
  853. return ret;
  854. }
  855. /* Send pending recv completions */
  856. static int send_recv_completions(struct net_device *ndev,
  857. struct netvsc_device *nvdev,
  858. struct netvsc_channel *nvchan)
  859. {
  860. struct multi_recv_comp *mrc = &nvchan->mrc;
  861. struct recv_comp_msg {
  862. struct nvsp_message_header hdr;
  863. u32 status;
  864. } __packed;
  865. struct recv_comp_msg msg = {
  866. .hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE,
  867. };
  868. int ret;
  869. while (mrc->first != mrc->next) {
  870. const struct recv_comp_data *rcd
  871. = mrc->slots + mrc->first;
  872. msg.status = rcd->status;
  873. ret = vmbus_sendpacket(nvchan->channel, &msg, sizeof(msg),
  874. rcd->tid, VM_PKT_COMP, 0);
  875. if (unlikely(ret)) {
  876. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  877. ++ndev_ctx->eth_stats.rx_comp_busy;
  878. return ret;
  879. }
  880. if (++mrc->first == nvdev->recv_completion_cnt)
  881. mrc->first = 0;
  882. }
  883. /* receive completion ring has been emptied */
  884. if (unlikely(nvdev->destroy))
  885. wake_up(&nvdev->wait_drain);
  886. return 0;
  887. }
  888. /* Count how many receive completions are outstanding */
  889. static void recv_comp_slot_avail(const struct netvsc_device *nvdev,
  890. const struct multi_recv_comp *mrc,
  891. u32 *filled, u32 *avail)
  892. {
  893. u32 count = nvdev->recv_completion_cnt;
  894. if (mrc->next >= mrc->first)
  895. *filled = mrc->next - mrc->first;
  896. else
  897. *filled = (count - mrc->first) + mrc->next;
  898. *avail = count - *filled - 1;
  899. }
  900. /* Add receive complete to ring to send to host. */
  901. static void enq_receive_complete(struct net_device *ndev,
  902. struct netvsc_device *nvdev, u16 q_idx,
  903. u64 tid, u32 status)
  904. {
  905. struct netvsc_channel *nvchan = &nvdev->chan_table[q_idx];
  906. struct multi_recv_comp *mrc = &nvchan->mrc;
  907. struct recv_comp_data *rcd;
  908. u32 filled, avail;
  909. recv_comp_slot_avail(nvdev, mrc, &filled, &avail);
  910. if (unlikely(filled > NAPI_POLL_WEIGHT)) {
  911. send_recv_completions(ndev, nvdev, nvchan);
  912. recv_comp_slot_avail(nvdev, mrc, &filled, &avail);
  913. }
  914. if (unlikely(!avail)) {
  915. netdev_err(ndev, "Recv_comp full buf q:%hd, tid:%llx\n",
  916. q_idx, tid);
  917. return;
  918. }
  919. rcd = mrc->slots + mrc->next;
  920. rcd->tid = tid;
  921. rcd->status = status;
  922. if (++mrc->next == nvdev->recv_completion_cnt)
  923. mrc->next = 0;
  924. }
  925. static int netvsc_receive(struct net_device *ndev,
  926. struct netvsc_device *net_device,
  927. struct netvsc_channel *nvchan,
  928. const struct vmpacket_descriptor *desc,
  929. const struct nvsp_message *nvsp)
  930. {
  931. struct net_device_context *net_device_ctx = netdev_priv(ndev);
  932. struct vmbus_channel *channel = nvchan->channel;
  933. const struct vmtransfer_page_packet_header *vmxferpage_packet
  934. = container_of(desc, const struct vmtransfer_page_packet_header, d);
  935. u16 q_idx = channel->offermsg.offer.sub_channel_index;
  936. char *recv_buf = net_device->recv_buf;
  937. u32 status = NVSP_STAT_SUCCESS;
  938. int i;
  939. int count = 0;
  940. /* Make sure this is a valid nvsp packet */
  941. if (unlikely(nvsp->hdr.msg_type != NVSP_MSG1_TYPE_SEND_RNDIS_PKT)) {
  942. netif_err(net_device_ctx, rx_err, ndev,
  943. "Unknown nvsp packet type received %u\n",
  944. nvsp->hdr.msg_type);
  945. return 0;
  946. }
  947. if (unlikely(vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID)) {
  948. netif_err(net_device_ctx, rx_err, ndev,
  949. "Invalid xfer page set id - expecting %x got %x\n",
  950. NETVSC_RECEIVE_BUFFER_ID,
  951. vmxferpage_packet->xfer_pageset_id);
  952. return 0;
  953. }
  954. count = vmxferpage_packet->range_cnt;
  955. /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
  956. for (i = 0; i < count; i++) {
  957. u32 offset = vmxferpage_packet->ranges[i].byte_offset;
  958. u32 buflen = vmxferpage_packet->ranges[i].byte_count;
  959. void *data;
  960. int ret;
  961. if (unlikely(offset + buflen > net_device->recv_buf_size)) {
  962. nvchan->rsc.cnt = 0;
  963. status = NVSP_STAT_FAIL;
  964. netif_err(net_device_ctx, rx_err, ndev,
  965. "Packet offset:%u + len:%u too big\n",
  966. offset, buflen);
  967. continue;
  968. }
  969. data = recv_buf + offset;
  970. nvchan->rsc.is_last = (i == count - 1);
  971. trace_rndis_recv(ndev, q_idx, data);
  972. /* Pass it to the upper layer */
  973. ret = rndis_filter_receive(ndev, net_device,
  974. nvchan, data, buflen);
  975. if (unlikely(ret != NVSP_STAT_SUCCESS))
  976. status = NVSP_STAT_FAIL;
  977. }
  978. enq_receive_complete(ndev, net_device, q_idx,
  979. vmxferpage_packet->d.trans_id, status);
  980. return count;
  981. }
  982. static void netvsc_send_table(struct net_device *ndev,
  983. struct netvsc_device *nvscdev,
  984. const struct nvsp_message *nvmsg,
  985. u32 msglen)
  986. {
  987. struct net_device_context *net_device_ctx = netdev_priv(ndev);
  988. u32 count, offset, *tab;
  989. int i;
  990. count = nvmsg->msg.v5_msg.send_table.count;
  991. offset = nvmsg->msg.v5_msg.send_table.offset;
  992. if (count != VRSS_SEND_TAB_SIZE) {
  993. netdev_err(ndev, "Received wrong send-table size:%u\n", count);
  994. return;
  995. }
  996. /* If negotiated version <= NVSP_PROTOCOL_VERSION_6, the offset may be
  997. * wrong due to a host bug. So fix the offset here.
  998. */
  999. if (nvscdev->nvsp_version <= NVSP_PROTOCOL_VERSION_6 &&
  1000. msglen >= sizeof(struct nvsp_message_header) +
  1001. sizeof(union nvsp_6_message_uber) + count * sizeof(u32))
  1002. offset = sizeof(struct nvsp_message_header) +
  1003. sizeof(union nvsp_6_message_uber);
  1004. /* Boundary check for all versions */
  1005. if (offset > msglen - count * sizeof(u32)) {
  1006. netdev_err(ndev, "Received send-table offset too big:%u\n",
  1007. offset);
  1008. return;
  1009. }
  1010. tab = (void *)nvmsg + offset;
  1011. for (i = 0; i < count; i++)
  1012. net_device_ctx->tx_table[i] = tab[i];
  1013. }
  1014. static void netvsc_send_vf(struct net_device *ndev,
  1015. const struct nvsp_message *nvmsg)
  1016. {
  1017. struct net_device_context *net_device_ctx = netdev_priv(ndev);
  1018. net_device_ctx->vf_alloc = nvmsg->msg.v4_msg.vf_assoc.allocated;
  1019. net_device_ctx->vf_serial = nvmsg->msg.v4_msg.vf_assoc.serial;
  1020. netdev_info(ndev, "VF slot %u %s\n",
  1021. net_device_ctx->vf_serial,
  1022. net_device_ctx->vf_alloc ? "added" : "removed");
  1023. }
  1024. static void netvsc_receive_inband(struct net_device *ndev,
  1025. struct netvsc_device *nvscdev,
  1026. const struct nvsp_message *nvmsg,
  1027. u32 msglen)
  1028. {
  1029. switch (nvmsg->hdr.msg_type) {
  1030. case NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE:
  1031. netvsc_send_table(ndev, nvscdev, nvmsg, msglen);
  1032. break;
  1033. case NVSP_MSG4_TYPE_SEND_VF_ASSOCIATION:
  1034. netvsc_send_vf(ndev, nvmsg);
  1035. break;
  1036. }
  1037. }
  1038. static int netvsc_process_raw_pkt(struct hv_device *device,
  1039. struct netvsc_channel *nvchan,
  1040. struct netvsc_device *net_device,
  1041. struct net_device *ndev,
  1042. const struct vmpacket_descriptor *desc,
  1043. int budget)
  1044. {
  1045. struct vmbus_channel *channel = nvchan->channel;
  1046. const struct nvsp_message *nvmsg = hv_pkt_data(desc);
  1047. u32 msglen = hv_pkt_datalen(desc);
  1048. trace_nvsp_recv(ndev, channel, nvmsg);
  1049. switch (desc->type) {
  1050. case VM_PKT_COMP:
  1051. netvsc_send_completion(ndev, net_device, channel,
  1052. desc, budget);
  1053. break;
  1054. case VM_PKT_DATA_USING_XFER_PAGES:
  1055. return netvsc_receive(ndev, net_device, nvchan,
  1056. desc, nvmsg);
  1057. break;
  1058. case VM_PKT_DATA_INBAND:
  1059. netvsc_receive_inband(ndev, net_device, nvmsg, msglen);
  1060. break;
  1061. default:
  1062. netdev_err(ndev, "unhandled packet type %d, tid %llx\n",
  1063. desc->type, desc->trans_id);
  1064. break;
  1065. }
  1066. return 0;
  1067. }
  1068. static struct hv_device *netvsc_channel_to_device(struct vmbus_channel *channel)
  1069. {
  1070. struct vmbus_channel *primary = channel->primary_channel;
  1071. return primary ? primary->device_obj : channel->device_obj;
  1072. }
  1073. /* Network processing softirq
  1074. * Process data in incoming ring buffer from host
  1075. * Stops when ring is empty or budget is met or exceeded.
  1076. */
  1077. int netvsc_poll(struct napi_struct *napi, int budget)
  1078. {
  1079. struct netvsc_channel *nvchan
  1080. = container_of(napi, struct netvsc_channel, napi);
  1081. struct netvsc_device *net_device = nvchan->net_device;
  1082. struct vmbus_channel *channel = nvchan->channel;
  1083. struct hv_device *device = netvsc_channel_to_device(channel);
  1084. struct net_device *ndev = hv_get_drvdata(device);
  1085. int work_done = 0;
  1086. int ret;
  1087. /* If starting a new interval */
  1088. if (!nvchan->desc)
  1089. nvchan->desc = hv_pkt_iter_first(channel);
  1090. while (nvchan->desc && work_done < budget) {
  1091. work_done += netvsc_process_raw_pkt(device, nvchan, net_device,
  1092. ndev, nvchan->desc, budget);
  1093. nvchan->desc = hv_pkt_iter_next(channel, nvchan->desc);
  1094. }
  1095. /* Send any pending receive completions */
  1096. ret = send_recv_completions(ndev, net_device, nvchan);
  1097. /* If it did not exhaust NAPI budget this time
  1098. * and not doing busy poll
  1099. * then re-enable host interrupts
  1100. * and reschedule if ring is not empty
  1101. * or sending receive completion failed.
  1102. */
  1103. if (work_done < budget &&
  1104. napi_complete_done(napi, work_done) &&
  1105. (ret || hv_end_read(&channel->inbound)) &&
  1106. napi_schedule_prep(napi)) {
  1107. hv_begin_read(&channel->inbound);
  1108. __napi_schedule(napi);
  1109. }
  1110. /* Driver may overshoot since multiple packets per descriptor */
  1111. return min(work_done, budget);
  1112. }
  1113. /* Call back when data is available in host ring buffer.
  1114. * Processing is deferred until network softirq (NAPI)
  1115. */
  1116. void netvsc_channel_cb(void *context)
  1117. {
  1118. struct netvsc_channel *nvchan = context;
  1119. struct vmbus_channel *channel = nvchan->channel;
  1120. struct hv_ring_buffer_info *rbi = &channel->inbound;
  1121. /* preload first vmpacket descriptor */
  1122. prefetch(hv_get_ring_buffer(rbi) + rbi->priv_read_index);
  1123. if (napi_schedule_prep(&nvchan->napi)) {
  1124. /* disable interrupts from host */
  1125. hv_begin_read(rbi);
  1126. __napi_schedule_irqoff(&nvchan->napi);
  1127. }
  1128. }
  1129. /*
  1130. * netvsc_device_add - Callback when the device belonging to this
  1131. * driver is added
  1132. */
  1133. struct netvsc_device *netvsc_device_add(struct hv_device *device,
  1134. const struct netvsc_device_info *device_info)
  1135. {
  1136. int i, ret = 0;
  1137. struct netvsc_device *net_device;
  1138. struct net_device *ndev = hv_get_drvdata(device);
  1139. struct net_device_context *net_device_ctx = netdev_priv(ndev);
  1140. net_device = alloc_net_device();
  1141. if (!net_device)
  1142. return ERR_PTR(-ENOMEM);
  1143. for (i = 0; i < VRSS_SEND_TAB_SIZE; i++)
  1144. net_device_ctx->tx_table[i] = 0;
  1145. /* Because the device uses NAPI, all the interrupt batching and
  1146. * control is done via Net softirq, not the channel handling
  1147. */
  1148. set_channel_read_mode(device->channel, HV_CALL_ISR);
  1149. /* If we're reopening the device we may have multiple queues, fill the
  1150. * chn_table with the default channel to use it before subchannels are
  1151. * opened.
  1152. * Initialize the channel state before we open;
  1153. * we can be interrupted as soon as we open the channel.
  1154. */
  1155. for (i = 0; i < VRSS_CHANNEL_MAX; i++) {
  1156. struct netvsc_channel *nvchan = &net_device->chan_table[i];
  1157. nvchan->channel = device->channel;
  1158. nvchan->net_device = net_device;
  1159. u64_stats_init(&nvchan->tx_stats.syncp);
  1160. u64_stats_init(&nvchan->rx_stats.syncp);
  1161. }
  1162. /* Enable NAPI handler before init callbacks */
  1163. netif_napi_add(ndev, &net_device->chan_table[0].napi,
  1164. netvsc_poll, NAPI_POLL_WEIGHT);
  1165. /* Open the channel */
  1166. ret = vmbus_open(device->channel, netvsc_ring_bytes,
  1167. netvsc_ring_bytes, NULL, 0,
  1168. netvsc_channel_cb, net_device->chan_table);
  1169. if (ret != 0) {
  1170. netdev_err(ndev, "unable to open channel: %d\n", ret);
  1171. goto cleanup;
  1172. }
  1173. /* Channel is opened */
  1174. netdev_dbg(ndev, "hv_netvsc channel opened successfully\n");
  1175. napi_enable(&net_device->chan_table[0].napi);
  1176. /* Connect with the NetVsp */
  1177. ret = netvsc_connect_vsp(device, net_device, device_info);
  1178. if (ret != 0) {
  1179. netdev_err(ndev,
  1180. "unable to connect to NetVSP - %d\n", ret);
  1181. goto close;
  1182. }
  1183. /* Writing nvdev pointer unlocks netvsc_send(), make sure chn_table is
  1184. * populated.
  1185. */
  1186. rcu_assign_pointer(net_device_ctx->nvdev, net_device);
  1187. return net_device;
  1188. close:
  1189. RCU_INIT_POINTER(net_device_ctx->nvdev, NULL);
  1190. napi_disable(&net_device->chan_table[0].napi);
  1191. /* Now, we can close the channel safely */
  1192. vmbus_close(device->channel);
  1193. cleanup:
  1194. netif_napi_del(&net_device->chan_table[0].napi);
  1195. free_netvsc_device(&net_device->rcu);
  1196. return ERR_PTR(ret);
  1197. }