netvsc.c 35 KB

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