netvsc.c 38 KB

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