netvsc.c 35 KB

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