netvsc.c 35 KB

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