netvsc.c 37 KB

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