netvsc.c 34 KB

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