netvsc.c 37 KB

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