netvsc.c 38 KB

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