netvsc.c 37 KB

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