netvsc.c 39 KB

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