netvsc.c 40 KB

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