netvsc.c 43 KB

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