netvsc.c 39 KB

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