netvsc.c 34 KB

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