netvsc.c 39 KB

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