netvsc.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  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 <linux/rtnetlink.h>
  32. #include <linux/prefetch.h>
  33. #include <asm/sync_bitops.h>
  34. #include "hyperv_net.h"
  35. /*
  36. * Switch the data path from the synthetic interface to the VF
  37. * interface.
  38. */
  39. void netvsc_switch_datapath(struct net_device *ndev, bool vf)
  40. {
  41. struct net_device_context *net_device_ctx = netdev_priv(ndev);
  42. struct hv_device *dev = net_device_ctx->device_ctx;
  43. struct netvsc_device *nv_dev = rtnl_dereference(net_device_ctx->nvdev);
  44. struct nvsp_message *init_pkt = &nv_dev->channel_init_pkt;
  45. memset(init_pkt, 0, sizeof(struct nvsp_message));
  46. init_pkt->hdr.msg_type = NVSP_MSG4_TYPE_SWITCH_DATA_PATH;
  47. if (vf)
  48. init_pkt->msg.v4_msg.active_dp.active_datapath =
  49. NVSP_DATAPATH_VF;
  50. else
  51. init_pkt->msg.v4_msg.active_dp.active_datapath =
  52. NVSP_DATAPATH_SYNTHETIC;
  53. vmbus_sendpacket(dev->channel, init_pkt,
  54. sizeof(struct nvsp_message),
  55. (unsigned long)init_pkt,
  56. VM_PKT_DATA_INBAND, 0);
  57. }
  58. static struct netvsc_device *alloc_net_device(void)
  59. {
  60. struct netvsc_device *net_device;
  61. net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
  62. if (!net_device)
  63. return NULL;
  64. init_waitqueue_head(&net_device->wait_drain);
  65. net_device->destroy = false;
  66. atomic_set(&net_device->open_cnt, 0);
  67. net_device->max_pkt = RNDIS_MAX_PKT_DEFAULT;
  68. net_device->pkt_align = RNDIS_PKT_ALIGN_DEFAULT;
  69. init_completion(&net_device->channel_init_wait);
  70. init_waitqueue_head(&net_device->subchan_open);
  71. INIT_WORK(&net_device->subchan_work, rndis_set_subchannel);
  72. return net_device;
  73. }
  74. static void free_netvsc_device(struct rcu_head *head)
  75. {
  76. struct netvsc_device *nvdev
  77. = container_of(head, struct netvsc_device, rcu);
  78. int i;
  79. for (i = 0; i < VRSS_CHANNEL_MAX; i++)
  80. vfree(nvdev->chan_table[i].mrc.slots);
  81. kfree(nvdev);
  82. }
  83. static void free_netvsc_device_rcu(struct netvsc_device *nvdev)
  84. {
  85. call_rcu(&nvdev->rcu, free_netvsc_device);
  86. }
  87. static void netvsc_destroy_buf(struct hv_device *device)
  88. {
  89. struct nvsp_message *revoke_packet;
  90. struct net_device *ndev = hv_get_drvdata(device);
  91. struct net_device_context *ndc = netdev_priv(ndev);
  92. struct netvsc_device *net_device = rtnl_dereference(ndc->nvdev);
  93. int ret;
  94. /*
  95. * If we got a section count, it means we received a
  96. * SendReceiveBufferComplete msg (ie sent
  97. * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
  98. * to send a revoke msg here
  99. */
  100. if (net_device->recv_section_cnt) {
  101. /* Send the revoke receive buffer */
  102. revoke_packet = &net_device->revoke_packet;
  103. memset(revoke_packet, 0, sizeof(struct nvsp_message));
  104. revoke_packet->hdr.msg_type =
  105. NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
  106. revoke_packet->msg.v1_msg.
  107. revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
  108. ret = vmbus_sendpacket(device->channel,
  109. revoke_packet,
  110. sizeof(struct nvsp_message),
  111. (unsigned long)revoke_packet,
  112. VM_PKT_DATA_INBAND, 0);
  113. /* If the failure is because the channel is rescinded;
  114. * ignore the failure since we cannot send on a rescinded
  115. * channel. This would allow us to properly cleanup
  116. * even when the channel is rescinded.
  117. */
  118. if (device->channel->rescind)
  119. ret = 0;
  120. /*
  121. * If we failed here, we might as well return and
  122. * have a leak rather than continue and a bugchk
  123. */
  124. if (ret != 0) {
  125. netdev_err(ndev, "unable to send "
  126. "revoke receive buffer to netvsp\n");
  127. return;
  128. }
  129. net_device->recv_section_cnt = 0;
  130. }
  131. /* Teardown the gpadl on the vsp end */
  132. if (net_device->recv_buf_gpadl_handle) {
  133. ret = vmbus_teardown_gpadl(device->channel,
  134. net_device->recv_buf_gpadl_handle);
  135. /* If we failed here, we might as well return and have a leak
  136. * rather than continue and a bugchk
  137. */
  138. if (ret != 0) {
  139. netdev_err(ndev,
  140. "unable to teardown receive buffer's gpadl\n");
  141. return;
  142. }
  143. net_device->recv_buf_gpadl_handle = 0;
  144. }
  145. if (net_device->recv_buf) {
  146. /* Free up the receive buffer */
  147. vfree(net_device->recv_buf);
  148. net_device->recv_buf = NULL;
  149. }
  150. /* Deal with the send buffer we may have setup.
  151. * If we got a send section size, it means we received a
  152. * NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE msg (ie sent
  153. * NVSP_MSG1_TYPE_SEND_SEND_BUF msg) therefore, we need
  154. * to send a revoke msg here
  155. */
  156. if (net_device->send_section_cnt) {
  157. /* Send the revoke receive buffer */
  158. revoke_packet = &net_device->revoke_packet;
  159. memset(revoke_packet, 0, sizeof(struct nvsp_message));
  160. revoke_packet->hdr.msg_type =
  161. NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
  162. revoke_packet->msg.v1_msg.revoke_send_buf.id =
  163. NETVSC_SEND_BUFFER_ID;
  164. ret = vmbus_sendpacket(device->channel,
  165. revoke_packet,
  166. sizeof(struct nvsp_message),
  167. (unsigned long)revoke_packet,
  168. VM_PKT_DATA_INBAND, 0);
  169. /* If the failure is because the channel is rescinded;
  170. * ignore the failure since we cannot send on a rescinded
  171. * channel. This would allow us to properly cleanup
  172. * even when the channel is rescinded.
  173. */
  174. if (device->channel->rescind)
  175. ret = 0;
  176. /* If we failed here, we might as well return and
  177. * have a leak rather than continue and a bugchk
  178. */
  179. if (ret != 0) {
  180. netdev_err(ndev, "unable to send "
  181. "revoke send buffer to netvsp\n");
  182. return;
  183. }
  184. net_device->send_section_cnt = 0;
  185. }
  186. /* Teardown the gpadl on the vsp end */
  187. if (net_device->send_buf_gpadl_handle) {
  188. ret = vmbus_teardown_gpadl(device->channel,
  189. net_device->send_buf_gpadl_handle);
  190. /* If we failed here, we might as well return and have a leak
  191. * rather than continue and a bugchk
  192. */
  193. if (ret != 0) {
  194. netdev_err(ndev,
  195. "unable to teardown send buffer's gpadl\n");
  196. return;
  197. }
  198. net_device->send_buf_gpadl_handle = 0;
  199. }
  200. if (net_device->send_buf) {
  201. /* Free up the send buffer */
  202. vfree(net_device->send_buf);
  203. net_device->send_buf = NULL;
  204. }
  205. kfree(net_device->send_section_map);
  206. }
  207. int netvsc_alloc_recv_comp_ring(struct netvsc_device *net_device, u32 q_idx)
  208. {
  209. struct netvsc_channel *nvchan = &net_device->chan_table[q_idx];
  210. int node = cpu_to_node(nvchan->channel->target_cpu);
  211. size_t size;
  212. size = net_device->recv_completion_cnt * sizeof(struct recv_comp_data);
  213. nvchan->mrc.slots = vzalloc_node(size, node);
  214. if (!nvchan->mrc.slots)
  215. nvchan->mrc.slots = vzalloc(size);
  216. return nvchan->mrc.slots ? 0 : -ENOMEM;
  217. }
  218. static int netvsc_init_buf(struct hv_device *device,
  219. struct netvsc_device *net_device,
  220. const struct netvsc_device_info *device_info)
  221. {
  222. struct nvsp_1_message_send_receive_buffer_complete *resp;
  223. struct net_device *ndev = hv_get_drvdata(device);
  224. struct nvsp_message *init_packet;
  225. unsigned int buf_size;
  226. size_t map_words;
  227. int ret = 0;
  228. /* Get receive buffer area. */
  229. buf_size = device_info->recv_sections * device_info->recv_section_size;
  230. buf_size = roundup(buf_size, PAGE_SIZE);
  231. net_device->recv_buf = vzalloc(buf_size);
  232. if (!net_device->recv_buf) {
  233. netdev_err(ndev,
  234. "unable to allocate receive buffer of size %u\n",
  235. buf_size);
  236. ret = -ENOMEM;
  237. goto cleanup;
  238. }
  239. /*
  240. * Establish the gpadl handle for this buffer on this
  241. * channel. Note: This call uses the vmbus connection rather
  242. * than the channel to establish the gpadl handle.
  243. */
  244. ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
  245. buf_size,
  246. &net_device->recv_buf_gpadl_handle);
  247. if (ret != 0) {
  248. netdev_err(ndev,
  249. "unable to establish receive buffer's gpadl\n");
  250. goto cleanup;
  251. }
  252. /* Notify the NetVsp of the gpadl handle */
  253. init_packet = &net_device->channel_init_pkt;
  254. memset(init_packet, 0, sizeof(struct nvsp_message));
  255. init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
  256. init_packet->msg.v1_msg.send_recv_buf.
  257. gpadl_handle = net_device->recv_buf_gpadl_handle;
  258. init_packet->msg.v1_msg.
  259. send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
  260. /* Send the gpadl notification request */
  261. ret = vmbus_sendpacket(device->channel, init_packet,
  262. sizeof(struct nvsp_message),
  263. (unsigned long)init_packet,
  264. VM_PKT_DATA_INBAND,
  265. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  266. if (ret != 0) {
  267. netdev_err(ndev,
  268. "unable to send receive buffer's gpadl to netvsp\n");
  269. goto cleanup;
  270. }
  271. wait_for_completion(&net_device->channel_init_wait);
  272. /* Check the response */
  273. resp = &init_packet->msg.v1_msg.send_recv_buf_complete;
  274. if (resp->status != NVSP_STAT_SUCCESS) {
  275. netdev_err(ndev,
  276. "Unable to complete receive buffer initialization with NetVsp - status %d\n",
  277. resp->status);
  278. ret = -EINVAL;
  279. goto cleanup;
  280. }
  281. /* Parse the response */
  282. netdev_dbg(ndev, "Receive sections: %u sub_allocs: size %u count: %u\n",
  283. resp->num_sections, resp->sections[0].sub_alloc_size,
  284. resp->sections[0].num_sub_allocs);
  285. /* There should only be one section for the entire receive buffer */
  286. if (resp->num_sections != 1 || resp->sections[0].offset != 0) {
  287. ret = -EINVAL;
  288. goto cleanup;
  289. }
  290. net_device->recv_section_size = resp->sections[0].sub_alloc_size;
  291. net_device->recv_section_cnt = resp->sections[0].num_sub_allocs;
  292. /* Setup receive completion ring */
  293. net_device->recv_completion_cnt
  294. = round_up(net_device->recv_section_cnt + 1,
  295. PAGE_SIZE / sizeof(u64));
  296. ret = netvsc_alloc_recv_comp_ring(net_device, 0);
  297. if (ret)
  298. goto cleanup;
  299. /* Now setup the send buffer. */
  300. buf_size = device_info->send_sections * device_info->send_section_size;
  301. buf_size = round_up(buf_size, PAGE_SIZE);
  302. net_device->send_buf = vzalloc(buf_size);
  303. if (!net_device->send_buf) {
  304. netdev_err(ndev, "unable to allocate send buffer of size %u\n",
  305. buf_size);
  306. ret = -ENOMEM;
  307. goto cleanup;
  308. }
  309. /* Establish the gpadl handle for this buffer on this
  310. * channel. Note: This call uses the vmbus connection rather
  311. * than the channel to establish the gpadl handle.
  312. */
  313. ret = vmbus_establish_gpadl(device->channel, net_device->send_buf,
  314. buf_size,
  315. &net_device->send_buf_gpadl_handle);
  316. if (ret != 0) {
  317. netdev_err(ndev,
  318. "unable to establish send buffer's gpadl\n");
  319. goto cleanup;
  320. }
  321. /* Notify the NetVsp of the gpadl handle */
  322. init_packet = &net_device->channel_init_pkt;
  323. memset(init_packet, 0, sizeof(struct nvsp_message));
  324. init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF;
  325. init_packet->msg.v1_msg.send_send_buf.gpadl_handle =
  326. net_device->send_buf_gpadl_handle;
  327. init_packet->msg.v1_msg.send_send_buf.id = NETVSC_SEND_BUFFER_ID;
  328. /* Send the gpadl notification request */
  329. ret = vmbus_sendpacket(device->channel, init_packet,
  330. sizeof(struct nvsp_message),
  331. (unsigned long)init_packet,
  332. VM_PKT_DATA_INBAND,
  333. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  334. if (ret != 0) {
  335. netdev_err(ndev,
  336. "unable to send send buffer's gpadl to netvsp\n");
  337. goto cleanup;
  338. }
  339. wait_for_completion(&net_device->channel_init_wait);
  340. /* Check the response */
  341. if (init_packet->msg.v1_msg.
  342. send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
  343. netdev_err(ndev, "Unable to complete send buffer "
  344. "initialization with NetVsp - status %d\n",
  345. init_packet->msg.v1_msg.
  346. send_send_buf_complete.status);
  347. ret = -EINVAL;
  348. goto cleanup;
  349. }
  350. /* Parse the response */
  351. net_device->send_section_size = init_packet->msg.
  352. v1_msg.send_send_buf_complete.section_size;
  353. /* Section count is simply the size divided by the section size. */
  354. net_device->send_section_cnt = buf_size / net_device->send_section_size;
  355. netdev_dbg(ndev, "Send section size: %d, Section count:%d\n",
  356. net_device->send_section_size, net_device->send_section_cnt);
  357. /* Setup state for managing the send buffer. */
  358. map_words = DIV_ROUND_UP(net_device->send_section_cnt, BITS_PER_LONG);
  359. net_device->send_section_map = kcalloc(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. /* Teaming bit is needed to receive link speed updates */
  404. init_packet->msg.v2_msg.send_ndis_config.capability.teaming = 1;
  405. }
  406. ret = vmbus_sendpacket(device->channel, init_packet,
  407. sizeof(struct nvsp_message),
  408. (unsigned long)init_packet,
  409. VM_PKT_DATA_INBAND, 0);
  410. return ret;
  411. }
  412. static int netvsc_connect_vsp(struct hv_device *device,
  413. struct netvsc_device *net_device,
  414. const struct netvsc_device_info *device_info)
  415. {
  416. static const u32 ver_list[] = {
  417. NVSP_PROTOCOL_VERSION_1, NVSP_PROTOCOL_VERSION_2,
  418. NVSP_PROTOCOL_VERSION_4, NVSP_PROTOCOL_VERSION_5
  419. };
  420. struct nvsp_message *init_packet;
  421. int ndis_version, i, ret;
  422. init_packet = &net_device->channel_init_pkt;
  423. /* Negotiate the latest NVSP protocol supported */
  424. for (i = ARRAY_SIZE(ver_list) - 1; i >= 0; i--)
  425. if (negotiate_nvsp_ver(device, net_device, init_packet,
  426. ver_list[i]) == 0) {
  427. net_device->nvsp_version = ver_list[i];
  428. break;
  429. }
  430. if (i < 0) {
  431. ret = -EPROTO;
  432. goto cleanup;
  433. }
  434. pr_debug("Negotiated NVSP version:%x\n", net_device->nvsp_version);
  435. /* Send the ndis version */
  436. memset(init_packet, 0, sizeof(struct nvsp_message));
  437. if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_4)
  438. ndis_version = 0x00060001;
  439. else
  440. ndis_version = 0x0006001e;
  441. init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
  442. init_packet->msg.v1_msg.
  443. send_ndis_ver.ndis_major_ver =
  444. (ndis_version & 0xFFFF0000) >> 16;
  445. init_packet->msg.v1_msg.
  446. send_ndis_ver.ndis_minor_ver =
  447. ndis_version & 0xFFFF;
  448. /* Send the init request */
  449. ret = vmbus_sendpacket(device->channel, init_packet,
  450. sizeof(struct nvsp_message),
  451. (unsigned long)init_packet,
  452. VM_PKT_DATA_INBAND, 0);
  453. if (ret != 0)
  454. goto cleanup;
  455. ret = netvsc_init_buf(device, net_device, device_info);
  456. cleanup:
  457. return ret;
  458. }
  459. static void netvsc_disconnect_vsp(struct hv_device *device)
  460. {
  461. netvsc_destroy_buf(device);
  462. }
  463. /*
  464. * netvsc_device_remove - Callback when the root bus device is removed
  465. */
  466. void netvsc_device_remove(struct hv_device *device)
  467. {
  468. struct net_device *ndev = hv_get_drvdata(device);
  469. struct net_device_context *net_device_ctx = netdev_priv(ndev);
  470. struct netvsc_device *net_device
  471. = rtnl_dereference(net_device_ctx->nvdev);
  472. int i;
  473. cancel_work_sync(&net_device->subchan_work);
  474. netvsc_disconnect_vsp(device);
  475. RCU_INIT_POINTER(net_device_ctx->nvdev, NULL);
  476. /*
  477. * At this point, no one should be accessing net_device
  478. * except in here
  479. */
  480. netdev_dbg(ndev, "net device safe to remove\n");
  481. /* Now, we can close the channel safely */
  482. vmbus_close(device->channel);
  483. /* And dissassociate NAPI context from device */
  484. for (i = 0; i < net_device->num_chn; i++)
  485. netif_napi_del(&net_device->chan_table[i].napi);
  486. /* Release all resources */
  487. free_netvsc_device_rcu(net_device);
  488. }
  489. #define RING_AVAIL_PERCENT_HIWATER 20
  490. #define RING_AVAIL_PERCENT_LOWATER 10
  491. /*
  492. * Get the percentage of available bytes to write in the ring.
  493. * The return value is in range from 0 to 100.
  494. */
  495. static inline u32 hv_ringbuf_avail_percent(
  496. struct hv_ring_buffer_info *ring_info)
  497. {
  498. u32 avail_read, avail_write;
  499. hv_get_ringbuffer_availbytes(ring_info, &avail_read, &avail_write);
  500. return avail_write * 100 / ring_info->ring_datasize;
  501. }
  502. static inline void netvsc_free_send_slot(struct netvsc_device *net_device,
  503. u32 index)
  504. {
  505. sync_change_bit(index, net_device->send_section_map);
  506. }
  507. static void netvsc_send_tx_complete(struct netvsc_device *net_device,
  508. struct vmbus_channel *incoming_channel,
  509. struct hv_device *device,
  510. const struct vmpacket_descriptor *desc,
  511. int budget)
  512. {
  513. struct sk_buff *skb = (struct sk_buff *)(unsigned long)desc->trans_id;
  514. struct net_device *ndev = hv_get_drvdata(device);
  515. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  516. struct vmbus_channel *channel = device->channel;
  517. u16 q_idx = 0;
  518. int queue_sends;
  519. /* Notify the layer above us */
  520. if (likely(skb)) {
  521. const struct hv_netvsc_packet *packet
  522. = (struct hv_netvsc_packet *)skb->cb;
  523. u32 send_index = packet->send_buf_index;
  524. struct netvsc_stats *tx_stats;
  525. if (send_index != NETVSC_INVALID_INDEX)
  526. netvsc_free_send_slot(net_device, send_index);
  527. q_idx = packet->q_idx;
  528. channel = incoming_channel;
  529. tx_stats = &net_device->chan_table[q_idx].tx_stats;
  530. u64_stats_update_begin(&tx_stats->syncp);
  531. tx_stats->packets += packet->total_packets;
  532. tx_stats->bytes += packet->total_bytes;
  533. u64_stats_update_end(&tx_stats->syncp);
  534. napi_consume_skb(skb, budget);
  535. }
  536. queue_sends =
  537. atomic_dec_return(&net_device->chan_table[q_idx].queue_sends);
  538. if (net_device->destroy && queue_sends == 0)
  539. wake_up(&net_device->wait_drain);
  540. if (netif_tx_queue_stopped(netdev_get_tx_queue(ndev, q_idx)) &&
  541. (hv_ringbuf_avail_percent(&channel->outbound) > RING_AVAIL_PERCENT_HIWATER ||
  542. queue_sends < 1)) {
  543. netif_tx_wake_queue(netdev_get_tx_queue(ndev, q_idx));
  544. ndev_ctx->eth_stats.wake_queue++;
  545. }
  546. }
  547. static void netvsc_send_completion(struct netvsc_device *net_device,
  548. struct vmbus_channel *incoming_channel,
  549. struct hv_device *device,
  550. const struct vmpacket_descriptor *desc,
  551. int budget)
  552. {
  553. struct nvsp_message *nvsp_packet = hv_pkt_data(desc);
  554. struct net_device *ndev = hv_get_drvdata(device);
  555. switch (nvsp_packet->hdr.msg_type) {
  556. case NVSP_MSG_TYPE_INIT_COMPLETE:
  557. case NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE:
  558. case NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE:
  559. case NVSP_MSG5_TYPE_SUBCHANNEL:
  560. /* Copy the response back */
  561. memcpy(&net_device->channel_init_pkt, nvsp_packet,
  562. sizeof(struct nvsp_message));
  563. complete(&net_device->channel_init_wait);
  564. break;
  565. case NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE:
  566. netvsc_send_tx_complete(net_device, incoming_channel,
  567. device, desc, budget);
  568. break;
  569. default:
  570. netdev_err(ndev,
  571. "Unknown send completion type %d received!!\n",
  572. nvsp_packet->hdr.msg_type);
  573. }
  574. }
  575. static u32 netvsc_get_next_send_section(struct netvsc_device *net_device)
  576. {
  577. unsigned long *map_addr = net_device->send_section_map;
  578. unsigned int i;
  579. for_each_clear_bit(i, map_addr, net_device->send_section_cnt) {
  580. if (sync_test_and_set_bit(i, map_addr) == 0)
  581. return i;
  582. }
  583. return NETVSC_INVALID_INDEX;
  584. }
  585. static u32 netvsc_copy_to_send_buf(struct netvsc_device *net_device,
  586. unsigned int section_index,
  587. u32 pend_size,
  588. struct hv_netvsc_packet *packet,
  589. struct rndis_message *rndis_msg,
  590. struct hv_page_buffer *pb,
  591. struct sk_buff *skb)
  592. {
  593. char *start = net_device->send_buf;
  594. char *dest = start + (section_index * net_device->send_section_size)
  595. + pend_size;
  596. int i;
  597. u32 msg_size = 0;
  598. u32 padding = 0;
  599. u32 remain = packet->total_data_buflen % net_device->pkt_align;
  600. u32 page_count = packet->cp_partial ? packet->rmsg_pgcnt :
  601. packet->page_buf_cnt;
  602. /* Add padding */
  603. if (skb->xmit_more && remain && !packet->cp_partial) {
  604. padding = net_device->pkt_align - remain;
  605. rndis_msg->msg_len += padding;
  606. packet->total_data_buflen += padding;
  607. }
  608. for (i = 0; i < page_count; i++) {
  609. char *src = phys_to_virt(pb[i].pfn << PAGE_SHIFT);
  610. u32 offset = pb[i].offset;
  611. u32 len = pb[i].len;
  612. memcpy(dest, (src + offset), len);
  613. msg_size += len;
  614. dest += len;
  615. }
  616. if (padding) {
  617. memset(dest, 0, padding);
  618. msg_size += padding;
  619. }
  620. return msg_size;
  621. }
  622. static inline int netvsc_send_pkt(
  623. struct hv_device *device,
  624. struct hv_netvsc_packet *packet,
  625. struct netvsc_device *net_device,
  626. struct hv_page_buffer *pb,
  627. struct sk_buff *skb)
  628. {
  629. struct nvsp_message nvmsg;
  630. struct nvsp_1_message_send_rndis_packet * const rpkt =
  631. &nvmsg.msg.v1_msg.send_rndis_pkt;
  632. struct netvsc_channel * const nvchan =
  633. &net_device->chan_table[packet->q_idx];
  634. struct vmbus_channel *out_channel = nvchan->channel;
  635. struct net_device *ndev = hv_get_drvdata(device);
  636. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  637. struct netdev_queue *txq = netdev_get_tx_queue(ndev, packet->q_idx);
  638. u64 req_id;
  639. int ret;
  640. u32 ring_avail = hv_ringbuf_avail_percent(&out_channel->outbound);
  641. nvmsg.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
  642. if (skb)
  643. rpkt->channel_type = 0; /* 0 is RMC_DATA */
  644. else
  645. rpkt->channel_type = 1; /* 1 is RMC_CONTROL */
  646. rpkt->send_buf_section_index = packet->send_buf_index;
  647. if (packet->send_buf_index == NETVSC_INVALID_INDEX)
  648. rpkt->send_buf_section_size = 0;
  649. else
  650. rpkt->send_buf_section_size = packet->total_data_buflen;
  651. req_id = (ulong)skb;
  652. if (out_channel->rescind)
  653. return -ENODEV;
  654. if (packet->page_buf_cnt) {
  655. if (packet->cp_partial)
  656. pb += packet->rmsg_pgcnt;
  657. ret = vmbus_sendpacket_pagebuffer(out_channel,
  658. pb, packet->page_buf_cnt,
  659. &nvmsg, sizeof(nvmsg),
  660. req_id);
  661. } else {
  662. ret = vmbus_sendpacket(out_channel,
  663. &nvmsg, sizeof(nvmsg),
  664. req_id, VM_PKT_DATA_INBAND,
  665. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  666. }
  667. if (ret == 0) {
  668. atomic_inc_return(&nvchan->queue_sends);
  669. if (ring_avail < RING_AVAIL_PERCENT_LOWATER) {
  670. netif_tx_stop_queue(txq);
  671. ndev_ctx->eth_stats.stop_queue++;
  672. }
  673. } else if (ret == -EAGAIN) {
  674. netif_tx_stop_queue(txq);
  675. ndev_ctx->eth_stats.stop_queue++;
  676. if (atomic_read(&nvchan->queue_sends) < 1) {
  677. netif_tx_wake_queue(txq);
  678. ndev_ctx->eth_stats.wake_queue++;
  679. ret = -ENOSPC;
  680. }
  681. } else {
  682. netdev_err(ndev,
  683. "Unable to send packet pages %u len %u, ret %d\n",
  684. packet->page_buf_cnt, packet->total_data_buflen,
  685. ret);
  686. }
  687. return ret;
  688. }
  689. /* Move packet out of multi send data (msd), and clear msd */
  690. static inline void move_pkt_msd(struct hv_netvsc_packet **msd_send,
  691. struct sk_buff **msd_skb,
  692. struct multi_send_data *msdp)
  693. {
  694. *msd_skb = msdp->skb;
  695. *msd_send = msdp->pkt;
  696. msdp->skb = NULL;
  697. msdp->pkt = NULL;
  698. msdp->count = 0;
  699. }
  700. /* RCU already held by caller */
  701. int netvsc_send(struct net_device_context *ndev_ctx,
  702. struct hv_netvsc_packet *packet,
  703. struct rndis_message *rndis_msg,
  704. struct hv_page_buffer *pb,
  705. struct sk_buff *skb)
  706. {
  707. struct netvsc_device *net_device
  708. = rcu_dereference_bh(ndev_ctx->nvdev);
  709. struct hv_device *device = ndev_ctx->device_ctx;
  710. int ret = 0;
  711. struct netvsc_channel *nvchan;
  712. u32 pktlen = packet->total_data_buflen, msd_len = 0;
  713. unsigned int section_index = NETVSC_INVALID_INDEX;
  714. struct multi_send_data *msdp;
  715. struct hv_netvsc_packet *msd_send = NULL, *cur_send = NULL;
  716. struct sk_buff *msd_skb = NULL;
  717. bool try_batch;
  718. bool xmit_more = (skb != NULL) ? skb->xmit_more : false;
  719. /* If device is rescinded, return error and packet will get dropped. */
  720. if (unlikely(!net_device || net_device->destroy))
  721. return -ENODEV;
  722. /* We may race with netvsc_connect_vsp()/netvsc_init_buf() and get
  723. * here before the negotiation with the host is finished and
  724. * send_section_map may not be allocated yet.
  725. */
  726. if (unlikely(!net_device->send_section_map))
  727. return -EAGAIN;
  728. nvchan = &net_device->chan_table[packet->q_idx];
  729. packet->send_buf_index = NETVSC_INVALID_INDEX;
  730. packet->cp_partial = false;
  731. /* Send control message directly without accessing msd (Multi-Send
  732. * Data) field which may be changed during data packet processing.
  733. */
  734. if (!skb) {
  735. cur_send = packet;
  736. goto send_now;
  737. }
  738. /* batch packets in send buffer if possible */
  739. msdp = &nvchan->msd;
  740. if (msdp->pkt)
  741. msd_len = msdp->pkt->total_data_buflen;
  742. try_batch = msd_len > 0 && msdp->count < net_device->max_pkt;
  743. if (try_batch && msd_len + pktlen + net_device->pkt_align <
  744. net_device->send_section_size) {
  745. section_index = msdp->pkt->send_buf_index;
  746. } else if (try_batch && msd_len + packet->rmsg_size <
  747. net_device->send_section_size) {
  748. section_index = msdp->pkt->send_buf_index;
  749. packet->cp_partial = true;
  750. } else if (pktlen + net_device->pkt_align <
  751. net_device->send_section_size) {
  752. section_index = netvsc_get_next_send_section(net_device);
  753. if (unlikely(section_index == NETVSC_INVALID_INDEX)) {
  754. ++ndev_ctx->eth_stats.tx_send_full;
  755. } else {
  756. move_pkt_msd(&msd_send, &msd_skb, msdp);
  757. msd_len = 0;
  758. }
  759. }
  760. if (section_index != NETVSC_INVALID_INDEX) {
  761. netvsc_copy_to_send_buf(net_device,
  762. section_index, msd_len,
  763. packet, rndis_msg, pb, skb);
  764. packet->send_buf_index = section_index;
  765. if (packet->cp_partial) {
  766. packet->page_buf_cnt -= packet->rmsg_pgcnt;
  767. packet->total_data_buflen = msd_len + packet->rmsg_size;
  768. } else {
  769. packet->page_buf_cnt = 0;
  770. packet->total_data_buflen += msd_len;
  771. }
  772. if (msdp->pkt) {
  773. packet->total_packets += msdp->pkt->total_packets;
  774. packet->total_bytes += msdp->pkt->total_bytes;
  775. }
  776. if (msdp->skb)
  777. dev_consume_skb_any(msdp->skb);
  778. if (xmit_more && !packet->cp_partial) {
  779. msdp->skb = skb;
  780. msdp->pkt = packet;
  781. msdp->count++;
  782. } else {
  783. cur_send = packet;
  784. msdp->skb = NULL;
  785. msdp->pkt = NULL;
  786. msdp->count = 0;
  787. }
  788. } else {
  789. move_pkt_msd(&msd_send, &msd_skb, msdp);
  790. cur_send = packet;
  791. }
  792. if (msd_send) {
  793. int m_ret = netvsc_send_pkt(device, msd_send, net_device,
  794. NULL, msd_skb);
  795. if (m_ret != 0) {
  796. netvsc_free_send_slot(net_device,
  797. msd_send->send_buf_index);
  798. dev_kfree_skb_any(msd_skb);
  799. }
  800. }
  801. send_now:
  802. if (cur_send)
  803. ret = netvsc_send_pkt(device, cur_send, net_device, pb, skb);
  804. if (ret != 0 && section_index != NETVSC_INVALID_INDEX)
  805. netvsc_free_send_slot(net_device, section_index);
  806. return ret;
  807. }
  808. /* Send pending recv completions */
  809. static int send_recv_completions(struct net_device *ndev,
  810. struct netvsc_device *nvdev,
  811. struct netvsc_channel *nvchan)
  812. {
  813. struct multi_recv_comp *mrc = &nvchan->mrc;
  814. struct recv_comp_msg {
  815. struct nvsp_message_header hdr;
  816. u32 status;
  817. } __packed;
  818. struct recv_comp_msg msg = {
  819. .hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE,
  820. };
  821. int ret;
  822. while (mrc->first != mrc->next) {
  823. const struct recv_comp_data *rcd
  824. = mrc->slots + mrc->first;
  825. msg.status = rcd->status;
  826. ret = vmbus_sendpacket(nvchan->channel, &msg, sizeof(msg),
  827. rcd->tid, VM_PKT_COMP, 0);
  828. if (unlikely(ret)) {
  829. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  830. ++ndev_ctx->eth_stats.rx_comp_busy;
  831. return ret;
  832. }
  833. if (++mrc->first == nvdev->recv_completion_cnt)
  834. mrc->first = 0;
  835. }
  836. /* receive completion ring has been emptied */
  837. if (unlikely(nvdev->destroy))
  838. wake_up(&nvdev->wait_drain);
  839. return 0;
  840. }
  841. /* Count how many receive completions are outstanding */
  842. static void recv_comp_slot_avail(const struct netvsc_device *nvdev,
  843. const struct multi_recv_comp *mrc,
  844. u32 *filled, u32 *avail)
  845. {
  846. u32 count = nvdev->recv_completion_cnt;
  847. if (mrc->next >= mrc->first)
  848. *filled = mrc->next - mrc->first;
  849. else
  850. *filled = (count - mrc->first) + mrc->next;
  851. *avail = count - *filled - 1;
  852. }
  853. /* Add receive complete to ring to send to host. */
  854. static void enq_receive_complete(struct net_device *ndev,
  855. struct netvsc_device *nvdev, u16 q_idx,
  856. u64 tid, u32 status)
  857. {
  858. struct netvsc_channel *nvchan = &nvdev->chan_table[q_idx];
  859. struct multi_recv_comp *mrc = &nvchan->mrc;
  860. struct recv_comp_data *rcd;
  861. u32 filled, avail;
  862. recv_comp_slot_avail(nvdev, mrc, &filled, &avail);
  863. if (unlikely(filled > NAPI_POLL_WEIGHT)) {
  864. send_recv_completions(ndev, nvdev, nvchan);
  865. recv_comp_slot_avail(nvdev, mrc, &filled, &avail);
  866. }
  867. if (unlikely(!avail)) {
  868. netdev_err(ndev, "Recv_comp full buf q:%hd, tid:%llx\n",
  869. q_idx, tid);
  870. return;
  871. }
  872. rcd = mrc->slots + mrc->next;
  873. rcd->tid = tid;
  874. rcd->status = status;
  875. if (++mrc->next == nvdev->recv_completion_cnt)
  876. mrc->next = 0;
  877. }
  878. static int netvsc_receive(struct net_device *ndev,
  879. struct netvsc_device *net_device,
  880. struct net_device_context *net_device_ctx,
  881. struct hv_device *device,
  882. struct vmbus_channel *channel,
  883. const struct vmpacket_descriptor *desc,
  884. struct nvsp_message *nvsp)
  885. {
  886. const struct vmtransfer_page_packet_header *vmxferpage_packet
  887. = container_of(desc, const struct vmtransfer_page_packet_header, d);
  888. u16 q_idx = channel->offermsg.offer.sub_channel_index;
  889. char *recv_buf = net_device->recv_buf;
  890. u32 status = NVSP_STAT_SUCCESS;
  891. int i;
  892. int count = 0;
  893. /* Make sure this is a valid nvsp packet */
  894. if (unlikely(nvsp->hdr.msg_type != NVSP_MSG1_TYPE_SEND_RNDIS_PKT)) {
  895. netif_err(net_device_ctx, rx_err, ndev,
  896. "Unknown nvsp packet type received %u\n",
  897. nvsp->hdr.msg_type);
  898. return 0;
  899. }
  900. if (unlikely(vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID)) {
  901. netif_err(net_device_ctx, rx_err, ndev,
  902. "Invalid xfer page set id - expecting %x got %x\n",
  903. NETVSC_RECEIVE_BUFFER_ID,
  904. vmxferpage_packet->xfer_pageset_id);
  905. return 0;
  906. }
  907. count = vmxferpage_packet->range_cnt;
  908. /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
  909. for (i = 0; i < count; i++) {
  910. void *data = recv_buf
  911. + vmxferpage_packet->ranges[i].byte_offset;
  912. u32 buflen = vmxferpage_packet->ranges[i].byte_count;
  913. /* Pass it to the upper layer */
  914. status = rndis_filter_receive(ndev, net_device, device,
  915. channel, data, buflen);
  916. }
  917. enq_receive_complete(ndev, net_device, q_idx,
  918. vmxferpage_packet->d.trans_id, status);
  919. return count;
  920. }
  921. static void netvsc_send_table(struct hv_device *hdev,
  922. struct nvsp_message *nvmsg)
  923. {
  924. struct net_device *ndev = hv_get_drvdata(hdev);
  925. struct net_device_context *net_device_ctx = netdev_priv(ndev);
  926. int i;
  927. u32 count, *tab;
  928. count = nvmsg->msg.v5_msg.send_table.count;
  929. if (count != VRSS_SEND_TAB_SIZE) {
  930. netdev_err(ndev, "Received wrong send-table size:%u\n", count);
  931. return;
  932. }
  933. tab = (u32 *)((unsigned long)&nvmsg->msg.v5_msg.send_table +
  934. nvmsg->msg.v5_msg.send_table.offset);
  935. for (i = 0; i < count; i++)
  936. net_device_ctx->tx_table[i] = tab[i];
  937. }
  938. static void netvsc_send_vf(struct net_device_context *net_device_ctx,
  939. struct nvsp_message *nvmsg)
  940. {
  941. net_device_ctx->vf_alloc = nvmsg->msg.v4_msg.vf_assoc.allocated;
  942. net_device_ctx->vf_serial = nvmsg->msg.v4_msg.vf_assoc.serial;
  943. }
  944. static inline void netvsc_receive_inband(struct hv_device *hdev,
  945. struct net_device_context *net_device_ctx,
  946. struct nvsp_message *nvmsg)
  947. {
  948. switch (nvmsg->hdr.msg_type) {
  949. case NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE:
  950. netvsc_send_table(hdev, nvmsg);
  951. break;
  952. case NVSP_MSG4_TYPE_SEND_VF_ASSOCIATION:
  953. netvsc_send_vf(net_device_ctx, nvmsg);
  954. break;
  955. }
  956. }
  957. static int netvsc_process_raw_pkt(struct hv_device *device,
  958. struct vmbus_channel *channel,
  959. struct netvsc_device *net_device,
  960. struct net_device *ndev,
  961. const struct vmpacket_descriptor *desc,
  962. int budget)
  963. {
  964. struct net_device_context *net_device_ctx = netdev_priv(ndev);
  965. struct nvsp_message *nvmsg = hv_pkt_data(desc);
  966. switch (desc->type) {
  967. case VM_PKT_COMP:
  968. netvsc_send_completion(net_device, channel, device,
  969. desc, budget);
  970. break;
  971. case VM_PKT_DATA_USING_XFER_PAGES:
  972. return netvsc_receive(ndev, net_device, net_device_ctx,
  973. device, channel, desc, nvmsg);
  974. break;
  975. case VM_PKT_DATA_INBAND:
  976. netvsc_receive_inband(device, net_device_ctx, nvmsg);
  977. break;
  978. default:
  979. netdev_err(ndev, "unhandled packet type %d, tid %llx\n",
  980. desc->type, desc->trans_id);
  981. break;
  982. }
  983. return 0;
  984. }
  985. static struct hv_device *netvsc_channel_to_device(struct vmbus_channel *channel)
  986. {
  987. struct vmbus_channel *primary = channel->primary_channel;
  988. return primary ? primary->device_obj : channel->device_obj;
  989. }
  990. /* Network processing softirq
  991. * Process data in incoming ring buffer from host
  992. * Stops when ring is empty or budget is met or exceeded.
  993. */
  994. int netvsc_poll(struct napi_struct *napi, int budget)
  995. {
  996. struct netvsc_channel *nvchan
  997. = container_of(napi, struct netvsc_channel, napi);
  998. struct netvsc_device *net_device = nvchan->net_device;
  999. struct vmbus_channel *channel = nvchan->channel;
  1000. struct hv_device *device = netvsc_channel_to_device(channel);
  1001. struct net_device *ndev = hv_get_drvdata(device);
  1002. int work_done = 0;
  1003. /* If starting a new interval */
  1004. if (!nvchan->desc)
  1005. nvchan->desc = hv_pkt_iter_first(channel);
  1006. while (nvchan->desc && work_done < budget) {
  1007. work_done += netvsc_process_raw_pkt(device, channel, net_device,
  1008. ndev, nvchan->desc, budget);
  1009. nvchan->desc = hv_pkt_iter_next(channel, nvchan->desc);
  1010. }
  1011. /* If send of pending receive completions suceeded
  1012. * and did not exhaust NAPI budget this time
  1013. * and not doing busy poll
  1014. * then re-enable host interrupts
  1015. * and reschedule if ring is not empty.
  1016. */
  1017. if (send_recv_completions(ndev, net_device, nvchan) == 0 &&
  1018. work_done < budget &&
  1019. napi_complete_done(napi, work_done) &&
  1020. hv_end_read(&channel->inbound)) {
  1021. hv_begin_read(&channel->inbound);
  1022. napi_reschedule(napi);
  1023. }
  1024. /* Driver may overshoot since multiple packets per descriptor */
  1025. return min(work_done, budget);
  1026. }
  1027. /* Call back when data is available in host ring buffer.
  1028. * Processing is deferred until network softirq (NAPI)
  1029. */
  1030. void netvsc_channel_cb(void *context)
  1031. {
  1032. struct netvsc_channel *nvchan = context;
  1033. struct vmbus_channel *channel = nvchan->channel;
  1034. struct hv_ring_buffer_info *rbi = &channel->inbound;
  1035. /* preload first vmpacket descriptor */
  1036. prefetch(hv_get_ring_buffer(rbi) + rbi->priv_read_index);
  1037. if (napi_schedule_prep(&nvchan->napi)) {
  1038. /* disable interupts from host */
  1039. hv_begin_read(rbi);
  1040. __napi_schedule(&nvchan->napi);
  1041. }
  1042. }
  1043. /*
  1044. * netvsc_device_add - Callback when the device belonging to this
  1045. * driver is added
  1046. */
  1047. struct netvsc_device *netvsc_device_add(struct hv_device *device,
  1048. const struct netvsc_device_info *device_info)
  1049. {
  1050. int i, ret = 0;
  1051. int ring_size = device_info->ring_size;
  1052. struct netvsc_device *net_device;
  1053. struct net_device *ndev = hv_get_drvdata(device);
  1054. struct net_device_context *net_device_ctx = netdev_priv(ndev);
  1055. net_device = alloc_net_device();
  1056. if (!net_device)
  1057. return ERR_PTR(-ENOMEM);
  1058. for (i = 0; i < VRSS_SEND_TAB_SIZE; i++)
  1059. net_device_ctx->tx_table[i] = 0;
  1060. net_device->ring_size = ring_size;
  1061. /* Because the device uses NAPI, all the interrupt batching and
  1062. * control is done via Net softirq, not the channel handling
  1063. */
  1064. set_channel_read_mode(device->channel, HV_CALL_ISR);
  1065. /* If we're reopening the device we may have multiple queues, fill the
  1066. * chn_table with the default channel to use it before subchannels are
  1067. * opened.
  1068. * Initialize the channel state before we open;
  1069. * we can be interrupted as soon as we open the channel.
  1070. */
  1071. for (i = 0; i < VRSS_CHANNEL_MAX; i++) {
  1072. struct netvsc_channel *nvchan = &net_device->chan_table[i];
  1073. nvchan->channel = device->channel;
  1074. nvchan->net_device = net_device;
  1075. u64_stats_init(&nvchan->tx_stats.syncp);
  1076. u64_stats_init(&nvchan->rx_stats.syncp);
  1077. }
  1078. /* Enable NAPI handler before init callbacks */
  1079. netif_napi_add(ndev, &net_device->chan_table[0].napi,
  1080. netvsc_poll, NAPI_POLL_WEIGHT);
  1081. /* Open the channel */
  1082. ret = vmbus_open(device->channel, ring_size * PAGE_SIZE,
  1083. ring_size * PAGE_SIZE, NULL, 0,
  1084. netvsc_channel_cb,
  1085. net_device->chan_table);
  1086. if (ret != 0) {
  1087. netif_napi_del(&net_device->chan_table[0].napi);
  1088. netdev_err(ndev, "unable to open channel: %d\n", ret);
  1089. goto cleanup;
  1090. }
  1091. /* Channel is opened */
  1092. netdev_dbg(ndev, "hv_netvsc channel opened successfully\n");
  1093. napi_enable(&net_device->chan_table[0].napi);
  1094. /* Writing nvdev pointer unlocks netvsc_send(), make sure chn_table is
  1095. * populated.
  1096. */
  1097. rcu_assign_pointer(net_device_ctx->nvdev, net_device);
  1098. /* Connect with the NetVsp */
  1099. ret = netvsc_connect_vsp(device, net_device, device_info);
  1100. if (ret != 0) {
  1101. netdev_err(ndev,
  1102. "unable to connect to NetVSP - %d\n", ret);
  1103. goto close;
  1104. }
  1105. return net_device;
  1106. close:
  1107. RCU_INIT_POINTER(net_device_ctx->nvdev, NULL);
  1108. napi_disable(&net_device->chan_table[0].napi);
  1109. /* Now, we can close the channel safely */
  1110. vmbus_close(device->channel);
  1111. cleanup:
  1112. free_netvsc_device(&net_device->rcu);
  1113. return ERR_PTR(ret);
  1114. }