netvsc.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2009, Microsoft Corporation.
  4. *
  5. * Authors:
  6. * Haiyang Zhang <haiyangz@microsoft.com>
  7. * Hank Janssen <hjanssen@microsoft.com>
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/kernel.h>
  11. #include <linux/sched.h>
  12. #include <linux/wait.h>
  13. #include <linux/mm.h>
  14. #include <linux/delay.h>
  15. #include <linux/io.h>
  16. #include <linux/slab.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/if_ether.h>
  19. #include <linux/vmalloc.h>
  20. #include <linux/rtnetlink.h>
  21. #include <linux/prefetch.h>
  22. #include <linux/filter.h>
  23. #include <asm/sync_bitops.h>
  24. #include <asm/mshyperv.h>
  25. #include "hyperv_net.h"
  26. #include "netvsc_trace.h"
  27. /*
  28. * Switch the data path from the synthetic interface to the VF
  29. * interface.
  30. */
  31. int netvsc_switch_datapath(struct net_device *ndev, bool vf)
  32. {
  33. struct net_device_context *net_device_ctx = netdev_priv(ndev);
  34. struct hv_device *dev = net_device_ctx->device_ctx;
  35. struct netvsc_device *nv_dev = rtnl_dereference(net_device_ctx->nvdev);
  36. struct nvsp_message *init_pkt = &nv_dev->channel_init_pkt;
  37. int ret, retry = 0;
  38. /* Block sending traffic to VF if it's about to be gone */
  39. if (!vf)
  40. net_device_ctx->data_path_is_vf = vf;
  41. memset(init_pkt, 0, sizeof(struct nvsp_message));
  42. init_pkt->hdr.msg_type = NVSP_MSG4_TYPE_SWITCH_DATA_PATH;
  43. if (vf)
  44. init_pkt->msg.v4_msg.active_dp.active_datapath =
  45. NVSP_DATAPATH_VF;
  46. else
  47. init_pkt->msg.v4_msg.active_dp.active_datapath =
  48. NVSP_DATAPATH_SYNTHETIC;
  49. again:
  50. trace_nvsp_send(ndev, init_pkt);
  51. ret = vmbus_sendpacket(dev->channel, init_pkt,
  52. sizeof(struct nvsp_message),
  53. (unsigned long)init_pkt, VM_PKT_DATA_INBAND,
  54. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  55. /* If failed to switch to/from VF, let data_path_is_vf stay false,
  56. * so we use synthetic path to send data.
  57. */
  58. if (ret) {
  59. if (ret != -EAGAIN) {
  60. netdev_err(ndev,
  61. "Unable to send sw datapath msg, err: %d\n",
  62. ret);
  63. return ret;
  64. }
  65. if (retry++ < RETRY_MAX) {
  66. usleep_range(RETRY_US_LO, RETRY_US_HI);
  67. goto again;
  68. } else {
  69. netdev_err(
  70. ndev,
  71. "Retry failed to send sw datapath msg, err: %d\n",
  72. ret);
  73. return ret;
  74. }
  75. }
  76. wait_for_completion(&nv_dev->channel_init_wait);
  77. net_device_ctx->data_path_is_vf = vf;
  78. return 0;
  79. }
  80. /* Worker to setup sub channels on initial setup
  81. * Initial hotplug event occurs in softirq context
  82. * and can't wait for channels.
  83. */
  84. static void netvsc_subchan_work(struct work_struct *w)
  85. {
  86. struct netvsc_device *nvdev =
  87. container_of(w, struct netvsc_device, subchan_work);
  88. struct rndis_device *rdev;
  89. int i, ret;
  90. /* Avoid deadlock with device removal already under RTNL */
  91. if (!rtnl_trylock()) {
  92. schedule_work(w);
  93. return;
  94. }
  95. rdev = nvdev->extension;
  96. if (rdev) {
  97. ret = rndis_set_subchannel(rdev->ndev, nvdev, NULL);
  98. if (ret == 0) {
  99. netif_device_attach(rdev->ndev);
  100. } else {
  101. /* fallback to only primary channel */
  102. for (i = 1; i < nvdev->num_chn; i++)
  103. netif_napi_del(&nvdev->chan_table[i].napi);
  104. nvdev->max_chn = 1;
  105. nvdev->num_chn = 1;
  106. }
  107. }
  108. rtnl_unlock();
  109. }
  110. static struct netvsc_device *alloc_net_device(void)
  111. {
  112. struct netvsc_device *net_device;
  113. net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
  114. if (!net_device)
  115. return NULL;
  116. init_waitqueue_head(&net_device->wait_drain);
  117. net_device->destroy = false;
  118. net_device->tx_disable = true;
  119. net_device->max_pkt = RNDIS_MAX_PKT_DEFAULT;
  120. net_device->pkt_align = RNDIS_PKT_ALIGN_DEFAULT;
  121. init_completion(&net_device->channel_init_wait);
  122. init_waitqueue_head(&net_device->subchan_open);
  123. INIT_WORK(&net_device->subchan_work, netvsc_subchan_work);
  124. return net_device;
  125. }
  126. static void free_netvsc_device(struct rcu_head *head)
  127. {
  128. struct netvsc_device *nvdev
  129. = container_of(head, struct netvsc_device, rcu);
  130. int i;
  131. kfree(nvdev->extension);
  132. if (!nvdev->recv_buf_gpadl_handle.decrypted)
  133. vfree(nvdev->recv_buf);
  134. if (!nvdev->send_buf_gpadl_handle.decrypted)
  135. vfree(nvdev->send_buf);
  136. bitmap_free(nvdev->send_section_map);
  137. for (i = 0; i < VRSS_CHANNEL_MAX; i++) {
  138. xdp_rxq_info_unreg(&nvdev->chan_table[i].xdp_rxq);
  139. kfree(nvdev->chan_table[i].recv_buf);
  140. vfree(nvdev->chan_table[i].mrc.slots);
  141. }
  142. kfree(nvdev);
  143. }
  144. static void free_netvsc_device_rcu(struct netvsc_device *nvdev)
  145. {
  146. call_rcu(&nvdev->rcu, free_netvsc_device);
  147. }
  148. static void netvsc_revoke_recv_buf(struct hv_device *device,
  149. struct netvsc_device *net_device,
  150. struct net_device *ndev)
  151. {
  152. struct nvsp_message *revoke_packet;
  153. int ret;
  154. /*
  155. * If we got a section count, it means we received a
  156. * SendReceiveBufferComplete msg (ie sent
  157. * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
  158. * to send a revoke msg here
  159. */
  160. if (net_device->recv_section_cnt) {
  161. /* Send the revoke receive buffer */
  162. revoke_packet = &net_device->revoke_packet;
  163. memset(revoke_packet, 0, sizeof(struct nvsp_message));
  164. revoke_packet->hdr.msg_type =
  165. NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
  166. revoke_packet->msg.v1_msg.
  167. revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
  168. trace_nvsp_send(ndev, revoke_packet);
  169. ret = vmbus_sendpacket(device->channel,
  170. revoke_packet,
  171. sizeof(struct nvsp_message),
  172. VMBUS_RQST_ID_NO_RESPONSE,
  173. VM_PKT_DATA_INBAND, 0);
  174. /* If the failure is because the channel is rescinded;
  175. * ignore the failure since we cannot send on a rescinded
  176. * channel. This would allow us to properly cleanup
  177. * even when the channel is rescinded.
  178. */
  179. if (device->channel->rescind)
  180. ret = 0;
  181. /*
  182. * If we failed here, we might as well return and
  183. * have a leak rather than continue and a bugchk
  184. */
  185. if (ret != 0) {
  186. netdev_err(ndev, "unable to send "
  187. "revoke receive buffer to netvsp\n");
  188. return;
  189. }
  190. net_device->recv_section_cnt = 0;
  191. }
  192. }
  193. static void netvsc_revoke_send_buf(struct hv_device *device,
  194. struct netvsc_device *net_device,
  195. struct net_device *ndev)
  196. {
  197. struct nvsp_message *revoke_packet;
  198. int ret;
  199. /* Deal with the send buffer we may have setup.
  200. * If we got a send section size, it means we received a
  201. * NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE msg (ie sent
  202. * NVSP_MSG1_TYPE_SEND_SEND_BUF msg) therefore, we need
  203. * to send a revoke msg here
  204. */
  205. if (net_device->send_section_cnt) {
  206. /* Send the revoke receive buffer */
  207. revoke_packet = &net_device->revoke_packet;
  208. memset(revoke_packet, 0, sizeof(struct nvsp_message));
  209. revoke_packet->hdr.msg_type =
  210. NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
  211. revoke_packet->msg.v1_msg.revoke_send_buf.id =
  212. NETVSC_SEND_BUFFER_ID;
  213. trace_nvsp_send(ndev, revoke_packet);
  214. ret = vmbus_sendpacket(device->channel,
  215. revoke_packet,
  216. sizeof(struct nvsp_message),
  217. VMBUS_RQST_ID_NO_RESPONSE,
  218. VM_PKT_DATA_INBAND, 0);
  219. /* If the failure is because the channel is rescinded;
  220. * ignore the failure since we cannot send on a rescinded
  221. * channel. This would allow us to properly cleanup
  222. * even when the channel is rescinded.
  223. */
  224. if (device->channel->rescind)
  225. ret = 0;
  226. /* If we failed here, we might as well return and
  227. * have a leak rather than continue and a bugchk
  228. */
  229. if (ret != 0) {
  230. netdev_err(ndev, "unable to send "
  231. "revoke send buffer to netvsp\n");
  232. return;
  233. }
  234. net_device->send_section_cnt = 0;
  235. }
  236. }
  237. static void netvsc_teardown_recv_gpadl(struct hv_device *device,
  238. struct netvsc_device *net_device,
  239. struct net_device *ndev)
  240. {
  241. int ret;
  242. if (net_device->recv_buf_gpadl_handle.gpadl_handle) {
  243. ret = vmbus_teardown_gpadl(device->channel,
  244. &net_device->recv_buf_gpadl_handle);
  245. /* If we failed here, we might as well return and have a leak
  246. * rather than continue and a bugchk
  247. */
  248. if (ret != 0) {
  249. netdev_err(ndev,
  250. "unable to teardown receive buffer's gpadl\n");
  251. return;
  252. }
  253. }
  254. }
  255. static void netvsc_teardown_send_gpadl(struct hv_device *device,
  256. struct netvsc_device *net_device,
  257. struct net_device *ndev)
  258. {
  259. int ret;
  260. if (net_device->send_buf_gpadl_handle.gpadl_handle) {
  261. ret = vmbus_teardown_gpadl(device->channel,
  262. &net_device->send_buf_gpadl_handle);
  263. /* If we failed here, we might as well return and have a leak
  264. * rather than continue and a bugchk
  265. */
  266. if (ret != 0) {
  267. netdev_err(ndev,
  268. "unable to teardown send buffer's gpadl\n");
  269. return;
  270. }
  271. }
  272. }
  273. int netvsc_alloc_recv_comp_ring(struct netvsc_device *net_device, u32 q_idx)
  274. {
  275. struct netvsc_channel *nvchan = &net_device->chan_table[q_idx];
  276. int node = cpu_to_node(nvchan->channel->target_cpu);
  277. size_t size;
  278. size = net_device->recv_completion_cnt * sizeof(struct recv_comp_data);
  279. nvchan->mrc.slots = vzalloc_node(size, node);
  280. if (!nvchan->mrc.slots)
  281. nvchan->mrc.slots = vzalloc(size);
  282. return nvchan->mrc.slots ? 0 : -ENOMEM;
  283. }
  284. static int netvsc_init_buf(struct hv_device *device,
  285. struct netvsc_device *net_device,
  286. const struct netvsc_device_info *device_info)
  287. {
  288. struct nvsp_1_message_send_receive_buffer_complete *resp;
  289. struct net_device *ndev = hv_get_drvdata(device);
  290. struct nvsp_message *init_packet;
  291. unsigned int buf_size;
  292. int i, ret = 0;
  293. /* Get receive buffer area. */
  294. buf_size = device_info->recv_sections * device_info->recv_section_size;
  295. buf_size = roundup(buf_size, PAGE_SIZE);
  296. /* Legacy hosts only allow smaller receive buffer */
  297. if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
  298. buf_size = min_t(unsigned int, buf_size,
  299. NETVSC_RECEIVE_BUFFER_SIZE_LEGACY);
  300. net_device->recv_buf = vzalloc(buf_size);
  301. if (!net_device->recv_buf) {
  302. netdev_err(ndev,
  303. "unable to allocate receive buffer of size %u\n",
  304. buf_size);
  305. ret = -ENOMEM;
  306. goto cleanup;
  307. }
  308. net_device->recv_buf_size = buf_size;
  309. /*
  310. * Establish the gpadl handle for this buffer on this
  311. * channel. Note: This call uses the vmbus connection rather
  312. * than the channel to establish the gpadl handle.
  313. */
  314. ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
  315. buf_size,
  316. &net_device->recv_buf_gpadl_handle);
  317. if (ret != 0) {
  318. netdev_err(ndev,
  319. "unable to establish receive buffer's gpadl\n");
  320. goto cleanup;
  321. }
  322. /* Notify the NetVsp of the gpadl handle */
  323. init_packet = &net_device->channel_init_pkt;
  324. memset(init_packet, 0, sizeof(struct nvsp_message));
  325. init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
  326. init_packet->msg.v1_msg.send_recv_buf.
  327. gpadl_handle = net_device->recv_buf_gpadl_handle.gpadl_handle;
  328. init_packet->msg.v1_msg.
  329. send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
  330. trace_nvsp_send(ndev, init_packet);
  331. /* Send the gpadl notification request */
  332. ret = vmbus_sendpacket(device->channel, init_packet,
  333. sizeof(struct nvsp_message),
  334. (unsigned long)init_packet,
  335. VM_PKT_DATA_INBAND,
  336. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  337. if (ret != 0) {
  338. netdev_err(ndev,
  339. "unable to send receive buffer's gpadl to netvsp\n");
  340. goto cleanup;
  341. }
  342. wait_for_completion(&net_device->channel_init_wait);
  343. /* Check the response */
  344. resp = &init_packet->msg.v1_msg.send_recv_buf_complete;
  345. if (resp->status != NVSP_STAT_SUCCESS) {
  346. netdev_err(ndev,
  347. "Unable to complete receive buffer initialization with NetVsp - status %d\n",
  348. resp->status);
  349. ret = -EINVAL;
  350. goto cleanup;
  351. }
  352. /* Parse the response */
  353. netdev_dbg(ndev, "Receive sections: %u sub_allocs: size %u count: %u\n",
  354. resp->num_sections, resp->sections[0].sub_alloc_size,
  355. resp->sections[0].num_sub_allocs);
  356. /* There should only be one section for the entire receive buffer */
  357. if (resp->num_sections != 1 || resp->sections[0].offset != 0) {
  358. ret = -EINVAL;
  359. goto cleanup;
  360. }
  361. net_device->recv_section_size = resp->sections[0].sub_alloc_size;
  362. net_device->recv_section_cnt = resp->sections[0].num_sub_allocs;
  363. /* Ensure buffer will not overflow */
  364. if (net_device->recv_section_size < NETVSC_MTU_MIN || (u64)net_device->recv_section_size *
  365. (u64)net_device->recv_section_cnt > (u64)buf_size) {
  366. netdev_err(ndev, "invalid recv_section_size %u\n",
  367. net_device->recv_section_size);
  368. ret = -EINVAL;
  369. goto cleanup;
  370. }
  371. for (i = 0; i < VRSS_CHANNEL_MAX; i++) {
  372. struct netvsc_channel *nvchan = &net_device->chan_table[i];
  373. nvchan->recv_buf = kzalloc(net_device->recv_section_size, GFP_KERNEL);
  374. if (nvchan->recv_buf == NULL) {
  375. ret = -ENOMEM;
  376. goto cleanup;
  377. }
  378. }
  379. /* Setup receive completion ring.
  380. * Add 1 to the recv_section_cnt because at least one entry in a
  381. * ring buffer has to be empty.
  382. */
  383. net_device->recv_completion_cnt = net_device->recv_section_cnt + 1;
  384. ret = netvsc_alloc_recv_comp_ring(net_device, 0);
  385. if (ret)
  386. goto cleanup;
  387. /* Now setup the send buffer. */
  388. buf_size = device_info->send_sections * device_info->send_section_size;
  389. buf_size = round_up(buf_size, PAGE_SIZE);
  390. net_device->send_buf = vzalloc(buf_size);
  391. if (!net_device->send_buf) {
  392. netdev_err(ndev, "unable to allocate send buffer of size %u\n",
  393. buf_size);
  394. ret = -ENOMEM;
  395. goto cleanup;
  396. }
  397. net_device->send_buf_size = buf_size;
  398. /* Establish the gpadl handle for this buffer on this
  399. * channel. Note: This call uses the vmbus connection rather
  400. * than the channel to establish the gpadl handle.
  401. */
  402. ret = vmbus_establish_gpadl(device->channel, net_device->send_buf,
  403. buf_size,
  404. &net_device->send_buf_gpadl_handle);
  405. if (ret != 0) {
  406. netdev_err(ndev,
  407. "unable to establish send buffer's gpadl\n");
  408. goto cleanup;
  409. }
  410. /* Notify the NetVsp of the gpadl handle */
  411. init_packet = &net_device->channel_init_pkt;
  412. memset(init_packet, 0, sizeof(struct nvsp_message));
  413. init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF;
  414. init_packet->msg.v1_msg.send_send_buf.gpadl_handle =
  415. net_device->send_buf_gpadl_handle.gpadl_handle;
  416. init_packet->msg.v1_msg.send_send_buf.id = NETVSC_SEND_BUFFER_ID;
  417. trace_nvsp_send(ndev, init_packet);
  418. /* Send the gpadl notification request */
  419. ret = vmbus_sendpacket(device->channel, init_packet,
  420. sizeof(struct nvsp_message),
  421. (unsigned long)init_packet,
  422. VM_PKT_DATA_INBAND,
  423. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  424. if (ret != 0) {
  425. netdev_err(ndev,
  426. "unable to send send buffer's gpadl to netvsp\n");
  427. goto cleanup;
  428. }
  429. wait_for_completion(&net_device->channel_init_wait);
  430. /* Check the response */
  431. if (init_packet->msg.v1_msg.
  432. send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
  433. netdev_err(ndev, "Unable to complete send buffer "
  434. "initialization with NetVsp - status %d\n",
  435. init_packet->msg.v1_msg.
  436. send_send_buf_complete.status);
  437. ret = -EINVAL;
  438. goto cleanup;
  439. }
  440. /* Parse the response */
  441. net_device->send_section_size = init_packet->msg.
  442. v1_msg.send_send_buf_complete.section_size;
  443. if (net_device->send_section_size < NETVSC_MTU_MIN) {
  444. netdev_err(ndev, "invalid send_section_size %u\n",
  445. net_device->send_section_size);
  446. ret = -EINVAL;
  447. goto cleanup;
  448. }
  449. /* Section count is simply the size divided by the section size. */
  450. net_device->send_section_cnt = buf_size / net_device->send_section_size;
  451. netdev_dbg(ndev, "Send section size: %d, Section count:%d\n",
  452. net_device->send_section_size, net_device->send_section_cnt);
  453. /* Setup state for managing the send buffer. */
  454. net_device->send_section_map = bitmap_zalloc(net_device->send_section_cnt,
  455. GFP_KERNEL);
  456. if (!net_device->send_section_map) {
  457. ret = -ENOMEM;
  458. goto cleanup;
  459. }
  460. goto exit;
  461. cleanup:
  462. netvsc_revoke_recv_buf(device, net_device, ndev);
  463. netvsc_revoke_send_buf(device, net_device, ndev);
  464. netvsc_teardown_recv_gpadl(device, net_device, ndev);
  465. netvsc_teardown_send_gpadl(device, net_device, ndev);
  466. exit:
  467. return ret;
  468. }
  469. /* Negotiate NVSP protocol version */
  470. static int negotiate_nvsp_ver(struct hv_device *device,
  471. struct netvsc_device *net_device,
  472. struct nvsp_message *init_packet,
  473. u32 nvsp_ver)
  474. {
  475. struct net_device *ndev = hv_get_drvdata(device);
  476. int ret;
  477. memset(init_packet, 0, sizeof(struct nvsp_message));
  478. init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
  479. init_packet->msg.init_msg.init.min_protocol_ver = nvsp_ver;
  480. init_packet->msg.init_msg.init.max_protocol_ver = nvsp_ver;
  481. trace_nvsp_send(ndev, init_packet);
  482. /* Send the init request */
  483. ret = vmbus_sendpacket(device->channel, init_packet,
  484. sizeof(struct nvsp_message),
  485. (unsigned long)init_packet,
  486. VM_PKT_DATA_INBAND,
  487. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  488. if (ret != 0)
  489. return ret;
  490. wait_for_completion(&net_device->channel_init_wait);
  491. if (init_packet->msg.init_msg.init_complete.status !=
  492. NVSP_STAT_SUCCESS)
  493. return -EINVAL;
  494. if (nvsp_ver == NVSP_PROTOCOL_VERSION_1)
  495. return 0;
  496. /* NVSPv2 or later: Send NDIS config */
  497. memset(init_packet, 0, sizeof(struct nvsp_message));
  498. init_packet->hdr.msg_type = NVSP_MSG2_TYPE_SEND_NDIS_CONFIG;
  499. init_packet->msg.v2_msg.send_ndis_config.mtu = ndev->mtu + ETH_HLEN;
  500. init_packet->msg.v2_msg.send_ndis_config.capability.ieee8021q = 1;
  501. if (nvsp_ver >= NVSP_PROTOCOL_VERSION_5) {
  502. if (hv_is_isolation_supported())
  503. netdev_info(ndev, "SR-IOV not advertised by guests on the host supporting isolation\n");
  504. else
  505. init_packet->msg.v2_msg.send_ndis_config.capability.sriov = 1;
  506. /* Teaming bit is needed to receive link speed updates */
  507. init_packet->msg.v2_msg.send_ndis_config.capability.teaming = 1;
  508. }
  509. if (nvsp_ver >= NVSP_PROTOCOL_VERSION_61)
  510. init_packet->msg.v2_msg.send_ndis_config.capability.rsc = 1;
  511. trace_nvsp_send(ndev, init_packet);
  512. ret = vmbus_sendpacket(device->channel, init_packet,
  513. sizeof(struct nvsp_message),
  514. VMBUS_RQST_ID_NO_RESPONSE,
  515. VM_PKT_DATA_INBAND, 0);
  516. return ret;
  517. }
  518. static int netvsc_connect_vsp(struct hv_device *device,
  519. struct netvsc_device *net_device,
  520. const struct netvsc_device_info *device_info)
  521. {
  522. struct net_device *ndev = hv_get_drvdata(device);
  523. static const u32 ver_list[] = {
  524. NVSP_PROTOCOL_VERSION_1, NVSP_PROTOCOL_VERSION_2,
  525. NVSP_PROTOCOL_VERSION_4, NVSP_PROTOCOL_VERSION_5,
  526. NVSP_PROTOCOL_VERSION_6, NVSP_PROTOCOL_VERSION_61
  527. };
  528. struct nvsp_message *init_packet;
  529. int ndis_version, i, ret;
  530. init_packet = &net_device->channel_init_pkt;
  531. /* Negotiate the latest NVSP protocol supported */
  532. for (i = ARRAY_SIZE(ver_list) - 1; i >= 0; i--)
  533. if (negotiate_nvsp_ver(device, net_device, init_packet,
  534. ver_list[i]) == 0) {
  535. net_device->nvsp_version = ver_list[i];
  536. break;
  537. }
  538. if (i < 0) {
  539. ret = -EPROTO;
  540. goto cleanup;
  541. }
  542. if (hv_is_isolation_supported() && net_device->nvsp_version < NVSP_PROTOCOL_VERSION_61) {
  543. netdev_err(ndev, "Invalid NVSP version 0x%x (expected >= 0x%x) from the host supporting isolation\n",
  544. net_device->nvsp_version, NVSP_PROTOCOL_VERSION_61);
  545. ret = -EPROTO;
  546. goto cleanup;
  547. }
  548. pr_debug("Negotiated NVSP version:%x\n", net_device->nvsp_version);
  549. /* Send the ndis version */
  550. memset(init_packet, 0, sizeof(struct nvsp_message));
  551. if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_4)
  552. ndis_version = 0x00060001;
  553. else
  554. ndis_version = 0x0006001e;
  555. init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
  556. init_packet->msg.v1_msg.
  557. send_ndis_ver.ndis_major_ver =
  558. (ndis_version & 0xFFFF0000) >> 16;
  559. init_packet->msg.v1_msg.
  560. send_ndis_ver.ndis_minor_ver =
  561. ndis_version & 0xFFFF;
  562. trace_nvsp_send(ndev, init_packet);
  563. /* Send the init request */
  564. ret = vmbus_sendpacket(device->channel, init_packet,
  565. sizeof(struct nvsp_message),
  566. VMBUS_RQST_ID_NO_RESPONSE,
  567. VM_PKT_DATA_INBAND, 0);
  568. if (ret != 0)
  569. goto cleanup;
  570. ret = netvsc_init_buf(device, net_device, device_info);
  571. cleanup:
  572. return ret;
  573. }
  574. /*
  575. * netvsc_device_remove - Callback when the root bus device is removed
  576. */
  577. void netvsc_device_remove(struct hv_device *device)
  578. {
  579. struct net_device *ndev = hv_get_drvdata(device);
  580. struct net_device_context *net_device_ctx = netdev_priv(ndev);
  581. struct netvsc_device *net_device
  582. = rtnl_dereference(net_device_ctx->nvdev);
  583. int i;
  584. /*
  585. * Revoke receive buffer. If host is pre-Win2016 then tear down
  586. * receive buffer GPADL. Do the same for send buffer.
  587. */
  588. netvsc_revoke_recv_buf(device, net_device, ndev);
  589. if (vmbus_proto_version < VERSION_WIN10)
  590. netvsc_teardown_recv_gpadl(device, net_device, ndev);
  591. netvsc_revoke_send_buf(device, net_device, ndev);
  592. if (vmbus_proto_version < VERSION_WIN10)
  593. netvsc_teardown_send_gpadl(device, net_device, ndev);
  594. RCU_INIT_POINTER(net_device_ctx->nvdev, NULL);
  595. /* Disable NAPI and disassociate its context from the device. */
  596. for (i = 0; i < net_device->num_chn; i++) {
  597. /* See also vmbus_reset_channel_cb(). */
  598. /* only disable enabled NAPI channel */
  599. if (i < ndev->real_num_rx_queues) {
  600. netif_queue_set_napi(ndev, i, NETDEV_QUEUE_TYPE_TX,
  601. NULL);
  602. netif_queue_set_napi(ndev, i, NETDEV_QUEUE_TYPE_RX,
  603. NULL);
  604. napi_disable(&net_device->chan_table[i].napi);
  605. }
  606. netif_napi_del(&net_device->chan_table[i].napi);
  607. }
  608. /*
  609. * At this point, no one should be accessing net_device
  610. * except in here
  611. */
  612. netdev_dbg(ndev, "net device safe to remove\n");
  613. /* Now, we can close the channel safely */
  614. vmbus_close(device->channel);
  615. /*
  616. * If host is Win2016 or higher then we do the GPADL tear down
  617. * here after VMBus is closed.
  618. */
  619. if (vmbus_proto_version >= VERSION_WIN10) {
  620. netvsc_teardown_recv_gpadl(device, net_device, ndev);
  621. netvsc_teardown_send_gpadl(device, net_device, ndev);
  622. }
  623. /* Release all resources */
  624. free_netvsc_device_rcu(net_device);
  625. }
  626. #define RING_AVAIL_PERCENT_HIWATER 20
  627. #define RING_AVAIL_PERCENT_LOWATER 10
  628. static inline void netvsc_free_send_slot(struct netvsc_device *net_device,
  629. u32 index)
  630. {
  631. sync_change_bit(index, net_device->send_section_map);
  632. }
  633. static void netvsc_send_tx_complete(struct net_device *ndev,
  634. struct netvsc_device *net_device,
  635. struct vmbus_channel *channel,
  636. const struct vmpacket_descriptor *desc,
  637. int budget)
  638. {
  639. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  640. struct sk_buff *skb;
  641. u16 q_idx = 0;
  642. int queue_sends;
  643. u64 cmd_rqst;
  644. cmd_rqst = channel->request_addr_callback(channel, desc->trans_id);
  645. if (cmd_rqst == VMBUS_RQST_ERROR) {
  646. netdev_err(ndev, "Invalid transaction ID %llx\n", desc->trans_id);
  647. return;
  648. }
  649. skb = (struct sk_buff *)(unsigned long)cmd_rqst;
  650. /* Notify the layer above us */
  651. if (likely(skb)) {
  652. struct hv_netvsc_packet *packet
  653. = (struct hv_netvsc_packet *)skb->cb;
  654. u32 send_index = packet->send_buf_index;
  655. struct netvsc_stats_tx *tx_stats;
  656. if (send_index != NETVSC_INVALID_INDEX)
  657. netvsc_free_send_slot(net_device, send_index);
  658. q_idx = packet->q_idx;
  659. tx_stats = &net_device->chan_table[q_idx].tx_stats;
  660. u64_stats_update_begin(&tx_stats->syncp);
  661. tx_stats->packets += packet->total_packets;
  662. tx_stats->bytes += packet->total_bytes;
  663. u64_stats_update_end(&tx_stats->syncp);
  664. netvsc_dma_unmap(ndev_ctx->device_ctx, packet);
  665. napi_consume_skb(skb, budget);
  666. }
  667. queue_sends =
  668. atomic_dec_return(&net_device->chan_table[q_idx].queue_sends);
  669. if (unlikely(net_device->destroy)) {
  670. if (queue_sends == 0)
  671. wake_up(&net_device->wait_drain);
  672. } else {
  673. struct netdev_queue *txq = netdev_get_tx_queue(ndev, q_idx);
  674. if (netif_tx_queue_stopped(txq) && !net_device->tx_disable &&
  675. (hv_get_avail_to_write_percent(&channel->outbound) >
  676. RING_AVAIL_PERCENT_HIWATER || queue_sends < 1)) {
  677. netif_tx_wake_queue(txq);
  678. ndev_ctx->eth_stats.wake_queue++;
  679. }
  680. }
  681. }
  682. static void netvsc_send_completion(struct net_device *ndev,
  683. struct netvsc_device *net_device,
  684. struct vmbus_channel *incoming_channel,
  685. const struct vmpacket_descriptor *desc,
  686. int budget)
  687. {
  688. const struct nvsp_message *nvsp_packet;
  689. u32 msglen = hv_pkt_datalen(desc);
  690. struct nvsp_message *pkt_rqst;
  691. u64 cmd_rqst;
  692. u32 status;
  693. /* First check if this is a VMBUS completion without data payload */
  694. if (!msglen) {
  695. cmd_rqst = incoming_channel->request_addr_callback(incoming_channel,
  696. desc->trans_id);
  697. if (cmd_rqst == VMBUS_RQST_ERROR) {
  698. netdev_err(ndev, "Invalid transaction ID %llx\n", desc->trans_id);
  699. return;
  700. }
  701. pkt_rqst = (struct nvsp_message *)(uintptr_t)cmd_rqst;
  702. switch (pkt_rqst->hdr.msg_type) {
  703. case NVSP_MSG4_TYPE_SWITCH_DATA_PATH:
  704. complete(&net_device->channel_init_wait);
  705. break;
  706. default:
  707. netdev_err(ndev, "Unexpected VMBUS completion!!\n");
  708. }
  709. return;
  710. }
  711. /* Ensure packet is big enough to read header fields */
  712. if (msglen < sizeof(struct nvsp_message_header)) {
  713. netdev_err(ndev, "nvsp_message length too small: %u\n", msglen);
  714. return;
  715. }
  716. nvsp_packet = hv_pkt_data(desc);
  717. switch (nvsp_packet->hdr.msg_type) {
  718. case NVSP_MSG_TYPE_INIT_COMPLETE:
  719. if (msglen < sizeof(struct nvsp_message_header) +
  720. sizeof(struct nvsp_message_init_complete)) {
  721. netdev_err(ndev, "nvsp_msg length too small: %u\n",
  722. msglen);
  723. return;
  724. }
  725. break;
  726. case NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE:
  727. if (msglen < sizeof(struct nvsp_message_header) +
  728. sizeof(struct nvsp_1_message_send_receive_buffer_complete)) {
  729. netdev_err(ndev, "nvsp_msg1 length too small: %u\n",
  730. msglen);
  731. return;
  732. }
  733. break;
  734. case NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE:
  735. if (msglen < sizeof(struct nvsp_message_header) +
  736. sizeof(struct nvsp_1_message_send_send_buffer_complete)) {
  737. netdev_err(ndev, "nvsp_msg1 length too small: %u\n",
  738. msglen);
  739. return;
  740. }
  741. break;
  742. case NVSP_MSG5_TYPE_SUBCHANNEL:
  743. if (msglen < sizeof(struct nvsp_message_header) +
  744. sizeof(struct nvsp_5_subchannel_complete)) {
  745. netdev_err(ndev, "nvsp_msg5 length too small: %u\n",
  746. msglen);
  747. return;
  748. }
  749. break;
  750. case NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE:
  751. if (msglen < sizeof(struct nvsp_message_header) +
  752. sizeof(struct nvsp_1_message_send_rndis_packet_complete)) {
  753. if (net_ratelimit())
  754. netdev_err(ndev, "nvsp_rndis_pkt_complete length too small: %u\n",
  755. msglen);
  756. return;
  757. }
  758. /* If status indicates an error, output a message so we know
  759. * there's a problem. But process the completion anyway so the
  760. * resources are released.
  761. */
  762. status = nvsp_packet->msg.v1_msg.send_rndis_pkt_complete.status;
  763. if (status != NVSP_STAT_SUCCESS && net_ratelimit())
  764. netdev_err(ndev, "nvsp_rndis_pkt_complete error status: %x\n",
  765. status);
  766. netvsc_send_tx_complete(ndev, net_device, incoming_channel,
  767. desc, budget);
  768. return;
  769. default:
  770. netdev_err(ndev,
  771. "Unknown send completion type %d received!!\n",
  772. nvsp_packet->hdr.msg_type);
  773. return;
  774. }
  775. /* Copy the response back */
  776. memcpy(&net_device->channel_init_pkt, nvsp_packet,
  777. sizeof(struct nvsp_message));
  778. complete(&net_device->channel_init_wait);
  779. }
  780. static u32 netvsc_get_next_send_section(struct netvsc_device *net_device)
  781. {
  782. unsigned long *map_addr = net_device->send_section_map;
  783. unsigned int i;
  784. for_each_clear_bit(i, map_addr, net_device->send_section_cnt) {
  785. if (sync_test_and_set_bit(i, map_addr) == 0)
  786. return i;
  787. }
  788. return NETVSC_INVALID_INDEX;
  789. }
  790. static void netvsc_copy_to_send_buf(struct netvsc_device *net_device,
  791. unsigned int section_index,
  792. u32 pend_size,
  793. struct hv_netvsc_packet *packet,
  794. struct rndis_message *rndis_msg,
  795. struct hv_page_buffer *pb,
  796. bool xmit_more)
  797. {
  798. char *start = net_device->send_buf;
  799. char *dest = start + (section_index * net_device->send_section_size)
  800. + pend_size;
  801. int i;
  802. u32 padding = 0;
  803. u32 page_count = packet->cp_partial ? 1 : packet->page_buf_cnt;
  804. u32 remain;
  805. /* Add padding */
  806. remain = packet->total_data_buflen & (net_device->pkt_align - 1);
  807. if (xmit_more && remain) {
  808. padding = net_device->pkt_align - remain;
  809. rndis_msg->msg_len += padding;
  810. packet->total_data_buflen += padding;
  811. }
  812. for (i = 0; i < page_count; i++) {
  813. char *src = phys_to_virt(pb[i].pfn << HV_HYP_PAGE_SHIFT);
  814. u32 offset = pb[i].offset;
  815. u32 len = pb[i].len;
  816. memcpy(dest, (src + offset), len);
  817. dest += len;
  818. }
  819. if (padding)
  820. memset(dest, 0, padding);
  821. }
  822. void netvsc_dma_unmap(struct hv_device *hv_dev,
  823. struct hv_netvsc_packet *packet)
  824. {
  825. int i;
  826. if (!hv_is_isolation_supported())
  827. return;
  828. if (!packet->dma_range)
  829. return;
  830. for (i = 0; i < packet->page_buf_cnt; i++)
  831. dma_unmap_single(&hv_dev->device, packet->dma_range[i].dma,
  832. packet->dma_range[i].mapping_size,
  833. DMA_TO_DEVICE);
  834. kfree(packet->dma_range);
  835. }
  836. /* netvsc_dma_map - Map swiotlb bounce buffer with data page of
  837. * packet sent by vmbus_sendpacket_pagebuffer() in the Isolation
  838. * VM.
  839. *
  840. * In isolation VM, netvsc send buffer has been marked visible to
  841. * host and so the data copied to send buffer doesn't need to use
  842. * bounce buffer. The data pages handled by vmbus_sendpacket_pagebuffer()
  843. * may not be copied to send buffer and so these pages need to be
  844. * mapped with swiotlb bounce buffer. netvsc_dma_map() is to do
  845. * that. The pfns in the struct hv_page_buffer need to be converted
  846. * to bounce buffer's pfn. The loop here is necessary because the
  847. * entries in the page buffer array are not necessarily full
  848. * pages of data. Each entry in the array has a separate offset and
  849. * len that may be non-zero, even for entries in the middle of the
  850. * array. And the entries are not physically contiguous. So each
  851. * entry must be individually mapped rather than as a contiguous unit.
  852. * So not use dma_map_sg() here.
  853. */
  854. static int netvsc_dma_map(struct hv_device *hv_dev,
  855. struct hv_netvsc_packet *packet,
  856. struct hv_page_buffer *pb)
  857. {
  858. u32 page_count = packet->page_buf_cnt;
  859. dma_addr_t dma;
  860. int i;
  861. if (!hv_is_isolation_supported())
  862. return 0;
  863. packet->dma_range = kcalloc(page_count,
  864. sizeof(*packet->dma_range),
  865. GFP_ATOMIC);
  866. if (!packet->dma_range)
  867. return -ENOMEM;
  868. for (i = 0; i < page_count; i++) {
  869. char *src = phys_to_virt((pb[i].pfn << HV_HYP_PAGE_SHIFT)
  870. + pb[i].offset);
  871. u32 len = pb[i].len;
  872. dma = dma_map_single(&hv_dev->device, src, len,
  873. DMA_TO_DEVICE);
  874. if (dma_mapping_error(&hv_dev->device, dma)) {
  875. kfree(packet->dma_range);
  876. return -ENOMEM;
  877. }
  878. /* pb[].offset and pb[].len are not changed during dma mapping
  879. * and so not reassign.
  880. */
  881. packet->dma_range[i].dma = dma;
  882. packet->dma_range[i].mapping_size = len;
  883. pb[i].pfn = dma >> HV_HYP_PAGE_SHIFT;
  884. }
  885. return 0;
  886. }
  887. /* Build an "array" of mpb entries describing the data to be transferred
  888. * over VMBus. After the desc header fields, each "array" entry is variable
  889. * size, and each entry starts after the end of the previous entry. The
  890. * "offset" and "len" fields for each entry imply the size of the entry.
  891. *
  892. * The pfns are in HV_HYP_PAGE_SIZE, because all communication with Hyper-V
  893. * uses that granularity, even if the system page size of the guest is larger.
  894. * Each entry in the input "pb" array must describe a contiguous range of
  895. * guest physical memory so that the pfns are sequential if the range crosses
  896. * a page boundary. The offset field must be < HV_HYP_PAGE_SIZE.
  897. */
  898. static inline void netvsc_build_mpb_array(struct hv_page_buffer *pb,
  899. u32 page_buffer_count,
  900. struct vmbus_packet_mpb_array *desc,
  901. u32 *desc_size)
  902. {
  903. struct hv_mpb_array *mpb_entry = &desc->range;
  904. int i, j;
  905. for (i = 0; i < page_buffer_count; i++) {
  906. u32 offset = pb[i].offset;
  907. u32 len = pb[i].len;
  908. mpb_entry->offset = offset;
  909. mpb_entry->len = len;
  910. for (j = 0; j < HVPFN_UP(offset + len); j++)
  911. mpb_entry->pfn_array[j] = pb[i].pfn + j;
  912. mpb_entry = (struct hv_mpb_array *)&mpb_entry->pfn_array[j];
  913. }
  914. desc->rangecount = page_buffer_count;
  915. *desc_size = (char *)mpb_entry - (char *)desc;
  916. }
  917. static inline int netvsc_send_pkt(
  918. struct hv_device *device,
  919. struct hv_netvsc_packet *packet,
  920. struct netvsc_device *net_device,
  921. struct hv_page_buffer *pb,
  922. struct sk_buff *skb)
  923. {
  924. struct nvsp_message nvmsg;
  925. struct nvsp_1_message_send_rndis_packet *rpkt =
  926. &nvmsg.msg.v1_msg.send_rndis_pkt;
  927. struct netvsc_channel * const nvchan =
  928. &net_device->chan_table[packet->q_idx];
  929. struct vmbus_channel *out_channel = nvchan->channel;
  930. struct net_device *ndev = hv_get_drvdata(device);
  931. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  932. struct netdev_queue *txq = netdev_get_tx_queue(ndev, packet->q_idx);
  933. u64 req_id;
  934. int ret;
  935. u32 ring_avail = hv_get_avail_to_write_percent(&out_channel->outbound);
  936. memset(&nvmsg, 0, sizeof(struct nvsp_message));
  937. nvmsg.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
  938. if (skb)
  939. rpkt->channel_type = 0; /* 0 is RMC_DATA */
  940. else
  941. rpkt->channel_type = 1; /* 1 is RMC_CONTROL */
  942. rpkt->send_buf_section_index = packet->send_buf_index;
  943. if (packet->send_buf_index == NETVSC_INVALID_INDEX)
  944. rpkt->send_buf_section_size = 0;
  945. else
  946. rpkt->send_buf_section_size = packet->total_data_buflen;
  947. req_id = (ulong)skb;
  948. if (out_channel->rescind)
  949. return -ENODEV;
  950. trace_nvsp_send_pkt(ndev, out_channel, rpkt);
  951. packet->dma_range = NULL;
  952. if (packet->page_buf_cnt) {
  953. struct vmbus_channel_packet_page_buffer desc;
  954. u32 desc_size;
  955. if (packet->cp_partial)
  956. pb++;
  957. ret = netvsc_dma_map(ndev_ctx->device_ctx, packet, pb);
  958. if (ret) {
  959. ret = -EAGAIN;
  960. goto exit;
  961. }
  962. netvsc_build_mpb_array(pb, packet->page_buf_cnt,
  963. (struct vmbus_packet_mpb_array *)&desc,
  964. &desc_size);
  965. ret = vmbus_sendpacket_mpb_desc(out_channel,
  966. (struct vmbus_packet_mpb_array *)&desc,
  967. desc_size, &nvmsg, sizeof(nvmsg), req_id);
  968. if (ret)
  969. netvsc_dma_unmap(ndev_ctx->device_ctx, packet);
  970. } else {
  971. ret = vmbus_sendpacket(out_channel,
  972. &nvmsg, sizeof(nvmsg),
  973. req_id, VM_PKT_DATA_INBAND,
  974. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  975. }
  976. exit:
  977. if (ret == 0) {
  978. atomic_inc_return(&nvchan->queue_sends);
  979. if (ring_avail < RING_AVAIL_PERCENT_LOWATER) {
  980. netif_tx_stop_queue(txq);
  981. ndev_ctx->eth_stats.stop_queue++;
  982. }
  983. } else if (ret == -EAGAIN) {
  984. netif_tx_stop_queue(txq);
  985. ndev_ctx->eth_stats.stop_queue++;
  986. } else {
  987. netdev_err(ndev,
  988. "Unable to send packet pages %u len %u, ret %d\n",
  989. packet->page_buf_cnt, packet->total_data_buflen,
  990. ret);
  991. }
  992. if (netif_tx_queue_stopped(txq) &&
  993. atomic_read(&nvchan->queue_sends) < 1 &&
  994. !net_device->tx_disable) {
  995. netif_tx_wake_queue(txq);
  996. ndev_ctx->eth_stats.wake_queue++;
  997. if (ret == -EAGAIN)
  998. ret = -ENOSPC;
  999. }
  1000. return ret;
  1001. }
  1002. /* Move packet out of multi send data (msd), and clear msd */
  1003. static inline void move_pkt_msd(struct hv_netvsc_packet **msd_send,
  1004. struct sk_buff **msd_skb,
  1005. struct multi_send_data *msdp)
  1006. {
  1007. *msd_skb = msdp->skb;
  1008. *msd_send = msdp->pkt;
  1009. msdp->skb = NULL;
  1010. msdp->pkt = NULL;
  1011. msdp->count = 0;
  1012. }
  1013. /* RCU already held by caller */
  1014. /* Batching/bouncing logic is designed to attempt to optimize
  1015. * performance.
  1016. *
  1017. * For small, non-LSO packets we copy the packet to a send buffer
  1018. * which is pre-registered with the Hyper-V side. This enables the
  1019. * hypervisor to avoid remapping the aperture to access the packet
  1020. * descriptor and data.
  1021. *
  1022. * If we already started using a buffer and the netdev is transmitting
  1023. * a burst of packets, keep on copying into the buffer until it is
  1024. * full or we are done collecting a burst. If there is an existing
  1025. * buffer with space for the RNDIS descriptor but not the packet, copy
  1026. * the RNDIS descriptor to the buffer, keeping the packet in place.
  1027. *
  1028. * If we do batching and send more than one packet using a single
  1029. * NetVSC message, free the SKBs of the packets copied, except for the
  1030. * last packet. This is done to streamline the handling of the case
  1031. * where the last packet only had the RNDIS descriptor copied to the
  1032. * send buffer, with the data pointers included in the NetVSC message.
  1033. */
  1034. int netvsc_send(struct net_device *ndev,
  1035. struct hv_netvsc_packet *packet,
  1036. struct rndis_message *rndis_msg,
  1037. struct hv_page_buffer *pb,
  1038. struct sk_buff *skb,
  1039. bool xdp_tx)
  1040. {
  1041. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  1042. struct netvsc_device *net_device
  1043. = rcu_dereference_bh(ndev_ctx->nvdev);
  1044. struct hv_device *device = ndev_ctx->device_ctx;
  1045. int ret = 0;
  1046. struct netvsc_channel *nvchan;
  1047. u32 pktlen = packet->total_data_buflen, msd_len = 0;
  1048. unsigned int section_index = NETVSC_INVALID_INDEX;
  1049. struct multi_send_data *msdp;
  1050. struct hv_netvsc_packet *msd_send = NULL, *cur_send = NULL;
  1051. struct sk_buff *msd_skb = NULL;
  1052. bool try_batch, xmit_more;
  1053. /* If device is rescinded, return error and packet will get dropped. */
  1054. if (unlikely(!net_device || net_device->destroy))
  1055. return -ENODEV;
  1056. nvchan = &net_device->chan_table[packet->q_idx];
  1057. packet->send_buf_index = NETVSC_INVALID_INDEX;
  1058. packet->cp_partial = false;
  1059. /* Send a control message or XDP packet directly without accessing
  1060. * msd (Multi-Send Data) field which may be changed during data packet
  1061. * processing.
  1062. */
  1063. if (!skb || xdp_tx)
  1064. return netvsc_send_pkt(device, packet, net_device, pb, skb);
  1065. /* batch packets in send buffer if possible */
  1066. msdp = &nvchan->msd;
  1067. if (msdp->pkt)
  1068. msd_len = msdp->pkt->total_data_buflen;
  1069. try_batch = msd_len > 0 && msdp->count < net_device->max_pkt;
  1070. if (try_batch && msd_len + pktlen + net_device->pkt_align <
  1071. net_device->send_section_size) {
  1072. section_index = msdp->pkt->send_buf_index;
  1073. } else if (try_batch && msd_len + packet->rmsg_size <
  1074. net_device->send_section_size) {
  1075. section_index = msdp->pkt->send_buf_index;
  1076. packet->cp_partial = true;
  1077. } else if (pktlen + net_device->pkt_align <
  1078. net_device->send_section_size) {
  1079. section_index = netvsc_get_next_send_section(net_device);
  1080. if (unlikely(section_index == NETVSC_INVALID_INDEX)) {
  1081. ++ndev_ctx->eth_stats.tx_send_full;
  1082. } else {
  1083. move_pkt_msd(&msd_send, &msd_skb, msdp);
  1084. msd_len = 0;
  1085. }
  1086. }
  1087. /* Keep aggregating only if stack says more data is coming
  1088. * and not doing mixed modes send and not flow blocked
  1089. */
  1090. xmit_more = netdev_xmit_more() &&
  1091. !packet->cp_partial &&
  1092. !netif_xmit_stopped(netdev_get_tx_queue(ndev, packet->q_idx));
  1093. if (section_index != NETVSC_INVALID_INDEX) {
  1094. netvsc_copy_to_send_buf(net_device,
  1095. section_index, msd_len,
  1096. packet, rndis_msg, pb, xmit_more);
  1097. packet->send_buf_index = section_index;
  1098. if (packet->cp_partial) {
  1099. packet->page_buf_cnt--;
  1100. packet->total_data_buflen = msd_len + packet->rmsg_size;
  1101. } else {
  1102. packet->page_buf_cnt = 0;
  1103. packet->total_data_buflen += msd_len;
  1104. }
  1105. if (msdp->pkt) {
  1106. packet->total_packets += msdp->pkt->total_packets;
  1107. packet->total_bytes += msdp->pkt->total_bytes;
  1108. }
  1109. if (msdp->skb)
  1110. dev_consume_skb_any(msdp->skb);
  1111. if (xmit_more) {
  1112. msdp->skb = skb;
  1113. msdp->pkt = packet;
  1114. msdp->count++;
  1115. } else {
  1116. cur_send = packet;
  1117. msdp->skb = NULL;
  1118. msdp->pkt = NULL;
  1119. msdp->count = 0;
  1120. }
  1121. } else {
  1122. move_pkt_msd(&msd_send, &msd_skb, msdp);
  1123. cur_send = packet;
  1124. }
  1125. if (msd_send) {
  1126. int m_ret = netvsc_send_pkt(device, msd_send, net_device,
  1127. NULL, msd_skb);
  1128. if (m_ret != 0) {
  1129. netvsc_free_send_slot(net_device,
  1130. msd_send->send_buf_index);
  1131. dev_kfree_skb_any(msd_skb);
  1132. }
  1133. }
  1134. if (cur_send)
  1135. ret = netvsc_send_pkt(device, cur_send, net_device, pb, skb);
  1136. if (ret != 0 && section_index != NETVSC_INVALID_INDEX)
  1137. netvsc_free_send_slot(net_device, section_index);
  1138. return ret;
  1139. }
  1140. /* Send pending recv completions */
  1141. static int send_recv_completions(struct net_device *ndev,
  1142. struct netvsc_device *nvdev,
  1143. struct netvsc_channel *nvchan)
  1144. {
  1145. struct multi_recv_comp *mrc = &nvchan->mrc;
  1146. struct recv_comp_msg {
  1147. struct nvsp_message_header hdr;
  1148. u32 status;
  1149. } __packed;
  1150. struct recv_comp_msg msg = {
  1151. .hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE,
  1152. };
  1153. int ret;
  1154. while (mrc->first != mrc->next) {
  1155. const struct recv_comp_data *rcd
  1156. = mrc->slots + mrc->first;
  1157. msg.status = rcd->status;
  1158. ret = vmbus_sendpacket(nvchan->channel, &msg, sizeof(msg),
  1159. rcd->tid, VM_PKT_COMP, 0);
  1160. if (unlikely(ret)) {
  1161. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  1162. ++ndev_ctx->eth_stats.rx_comp_busy;
  1163. return ret;
  1164. }
  1165. if (++mrc->first == nvdev->recv_completion_cnt)
  1166. mrc->first = 0;
  1167. }
  1168. /* receive completion ring has been emptied */
  1169. if (unlikely(nvdev->destroy))
  1170. wake_up(&nvdev->wait_drain);
  1171. return 0;
  1172. }
  1173. /* Count how many receive completions are outstanding */
  1174. static void recv_comp_slot_avail(const struct netvsc_device *nvdev,
  1175. const struct multi_recv_comp *mrc,
  1176. u32 *filled, u32 *avail)
  1177. {
  1178. u32 count = nvdev->recv_completion_cnt;
  1179. if (mrc->next >= mrc->first)
  1180. *filled = mrc->next - mrc->first;
  1181. else
  1182. *filled = (count - mrc->first) + mrc->next;
  1183. *avail = count - *filled - 1;
  1184. }
  1185. /* Add receive complete to ring to send to host. */
  1186. static void enq_receive_complete(struct net_device *ndev,
  1187. struct netvsc_device *nvdev, u16 q_idx,
  1188. u64 tid, u32 status)
  1189. {
  1190. struct netvsc_channel *nvchan = &nvdev->chan_table[q_idx];
  1191. struct multi_recv_comp *mrc = &nvchan->mrc;
  1192. struct recv_comp_data *rcd;
  1193. u32 filled, avail;
  1194. recv_comp_slot_avail(nvdev, mrc, &filled, &avail);
  1195. if (unlikely(filled > NAPI_POLL_WEIGHT)) {
  1196. send_recv_completions(ndev, nvdev, nvchan);
  1197. recv_comp_slot_avail(nvdev, mrc, &filled, &avail);
  1198. }
  1199. if (unlikely(!avail)) {
  1200. netdev_err(ndev, "Recv_comp full buf q:%hd, tid:%llx\n",
  1201. q_idx, tid);
  1202. return;
  1203. }
  1204. rcd = mrc->slots + mrc->next;
  1205. rcd->tid = tid;
  1206. rcd->status = status;
  1207. if (++mrc->next == nvdev->recv_completion_cnt)
  1208. mrc->next = 0;
  1209. }
  1210. static int netvsc_receive(struct net_device *ndev,
  1211. struct netvsc_device *net_device,
  1212. struct netvsc_channel *nvchan,
  1213. const struct vmpacket_descriptor *desc)
  1214. {
  1215. struct net_device_context *net_device_ctx = netdev_priv(ndev);
  1216. struct vmbus_channel *channel = nvchan->channel;
  1217. const struct vmtransfer_page_packet_header *vmxferpage_packet
  1218. = container_of(desc, const struct vmtransfer_page_packet_header, d);
  1219. const struct nvsp_message *nvsp = hv_pkt_data(desc);
  1220. u32 msglen = hv_pkt_datalen(desc);
  1221. u16 q_idx = channel->offermsg.offer.sub_channel_index;
  1222. char *recv_buf = net_device->recv_buf;
  1223. u32 status = NVSP_STAT_SUCCESS;
  1224. int i;
  1225. int count = 0;
  1226. /* Ensure packet is big enough to read header fields */
  1227. if (msglen < sizeof(struct nvsp_message_header)) {
  1228. netif_err(net_device_ctx, rx_err, ndev,
  1229. "invalid nvsp header, length too small: %u\n",
  1230. msglen);
  1231. return 0;
  1232. }
  1233. /* Make sure this is a valid nvsp packet */
  1234. if (unlikely(nvsp->hdr.msg_type != NVSP_MSG1_TYPE_SEND_RNDIS_PKT)) {
  1235. netif_err(net_device_ctx, rx_err, ndev,
  1236. "Unknown nvsp packet type received %u\n",
  1237. nvsp->hdr.msg_type);
  1238. return 0;
  1239. }
  1240. /* Validate xfer page pkt header */
  1241. if ((desc->offset8 << 3) < sizeof(struct vmtransfer_page_packet_header)) {
  1242. netif_err(net_device_ctx, rx_err, ndev,
  1243. "Invalid xfer page pkt, offset too small: %u\n",
  1244. desc->offset8 << 3);
  1245. return 0;
  1246. }
  1247. if (unlikely(vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID)) {
  1248. netif_err(net_device_ctx, rx_err, ndev,
  1249. "Invalid xfer page set id - expecting %x got %x\n",
  1250. NETVSC_RECEIVE_BUFFER_ID,
  1251. vmxferpage_packet->xfer_pageset_id);
  1252. return 0;
  1253. }
  1254. count = vmxferpage_packet->range_cnt;
  1255. /* Check count for a valid value */
  1256. if (NETVSC_XFER_HEADER_SIZE(count) > desc->offset8 << 3) {
  1257. netif_err(net_device_ctx, rx_err, ndev,
  1258. "Range count is not valid: %d\n",
  1259. count);
  1260. return 0;
  1261. }
  1262. /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
  1263. for (i = 0; i < count; i++) {
  1264. u32 offset = vmxferpage_packet->ranges[i].byte_offset;
  1265. u32 buflen = vmxferpage_packet->ranges[i].byte_count;
  1266. void *data;
  1267. int ret;
  1268. if (unlikely(offset > net_device->recv_buf_size ||
  1269. buflen > net_device->recv_buf_size - offset)) {
  1270. nvchan->rsc.cnt = 0;
  1271. status = NVSP_STAT_FAIL;
  1272. netif_err(net_device_ctx, rx_err, ndev,
  1273. "Packet offset:%u + len:%u too big\n",
  1274. offset, buflen);
  1275. continue;
  1276. }
  1277. /* We're going to copy (sections of) the packet into nvchan->recv_buf;
  1278. * make sure that nvchan->recv_buf is large enough to hold the packet.
  1279. */
  1280. if (unlikely(buflen > net_device->recv_section_size)) {
  1281. nvchan->rsc.cnt = 0;
  1282. status = NVSP_STAT_FAIL;
  1283. netif_err(net_device_ctx, rx_err, ndev,
  1284. "Packet too big: buflen=%u recv_section_size=%u\n",
  1285. buflen, net_device->recv_section_size);
  1286. continue;
  1287. }
  1288. data = recv_buf + offset;
  1289. nvchan->rsc.is_last = (i == count - 1);
  1290. trace_rndis_recv(ndev, q_idx, data);
  1291. /* Pass it to the upper layer */
  1292. ret = rndis_filter_receive(ndev, net_device,
  1293. nvchan, data, buflen);
  1294. if (unlikely(ret != NVSP_STAT_SUCCESS)) {
  1295. /* Drop incomplete packet */
  1296. nvchan->rsc.cnt = 0;
  1297. status = NVSP_STAT_FAIL;
  1298. }
  1299. }
  1300. enq_receive_complete(ndev, net_device, q_idx,
  1301. vmxferpage_packet->d.trans_id, status);
  1302. return count;
  1303. }
  1304. static void netvsc_send_table(struct net_device *ndev,
  1305. struct netvsc_device *nvscdev,
  1306. const struct nvsp_message *nvmsg,
  1307. u32 msglen)
  1308. {
  1309. struct net_device_context *net_device_ctx = netdev_priv(ndev);
  1310. u32 count, offset, *tab;
  1311. int i;
  1312. /* Ensure packet is big enough to read send_table fields */
  1313. if (msglen < sizeof(struct nvsp_message_header) +
  1314. sizeof(struct nvsp_5_send_indirect_table)) {
  1315. netdev_err(ndev, "nvsp_v5_msg length too small: %u\n", msglen);
  1316. return;
  1317. }
  1318. count = nvmsg->msg.v5_msg.send_table.count;
  1319. offset = nvmsg->msg.v5_msg.send_table.offset;
  1320. if (count != VRSS_SEND_TAB_SIZE) {
  1321. netdev_err(ndev, "Received wrong send-table size:%u\n", count);
  1322. return;
  1323. }
  1324. /* If negotiated version <= NVSP_PROTOCOL_VERSION_6, the offset may be
  1325. * wrong due to a host bug. So fix the offset here.
  1326. */
  1327. if (nvscdev->nvsp_version <= NVSP_PROTOCOL_VERSION_6 &&
  1328. msglen >= sizeof(struct nvsp_message_header) +
  1329. sizeof(union nvsp_6_message_uber) + count * sizeof(u32))
  1330. offset = sizeof(struct nvsp_message_header) +
  1331. sizeof(union nvsp_6_message_uber);
  1332. /* Boundary check for all versions */
  1333. if (msglen < count * sizeof(u32) || offset > msglen - count * sizeof(u32)) {
  1334. netdev_err(ndev, "Received send-table offset too big:%u\n",
  1335. offset);
  1336. return;
  1337. }
  1338. tab = (void *)nvmsg + offset;
  1339. for (i = 0; i < count; i++)
  1340. net_device_ctx->tx_table[i] = tab[i];
  1341. }
  1342. static void netvsc_send_vf(struct net_device *ndev,
  1343. const struct nvsp_message *nvmsg,
  1344. u32 msglen)
  1345. {
  1346. struct net_device_context *net_device_ctx = netdev_priv(ndev);
  1347. /* Ensure packet is big enough to read its fields */
  1348. if (msglen < sizeof(struct nvsp_message_header) +
  1349. sizeof(struct nvsp_4_send_vf_association)) {
  1350. netdev_err(ndev, "nvsp_v4_msg length too small: %u\n", msglen);
  1351. return;
  1352. }
  1353. net_device_ctx->vf_alloc = nvmsg->msg.v4_msg.vf_assoc.allocated;
  1354. net_device_ctx->vf_serial = nvmsg->msg.v4_msg.vf_assoc.serial;
  1355. if (net_device_ctx->vf_alloc)
  1356. complete(&net_device_ctx->vf_add);
  1357. netdev_info(ndev, "VF slot %u %s\n",
  1358. net_device_ctx->vf_serial,
  1359. net_device_ctx->vf_alloc ? "added" : "removed");
  1360. }
  1361. static void netvsc_receive_inband(struct net_device *ndev,
  1362. struct netvsc_device *nvscdev,
  1363. const struct vmpacket_descriptor *desc)
  1364. {
  1365. const struct nvsp_message *nvmsg = hv_pkt_data(desc);
  1366. u32 msglen = hv_pkt_datalen(desc);
  1367. /* Ensure packet is big enough to read header fields */
  1368. if (msglen < sizeof(struct nvsp_message_header)) {
  1369. netdev_err(ndev, "inband nvsp_message length too small: %u\n", msglen);
  1370. return;
  1371. }
  1372. switch (nvmsg->hdr.msg_type) {
  1373. case NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE:
  1374. netvsc_send_table(ndev, nvscdev, nvmsg, msglen);
  1375. break;
  1376. case NVSP_MSG4_TYPE_SEND_VF_ASSOCIATION:
  1377. if (hv_is_isolation_supported())
  1378. netdev_err(ndev, "Ignore VF_ASSOCIATION msg from the host supporting isolation\n");
  1379. else
  1380. netvsc_send_vf(ndev, nvmsg, msglen);
  1381. break;
  1382. }
  1383. }
  1384. static int netvsc_process_raw_pkt(struct hv_device *device,
  1385. struct netvsc_channel *nvchan,
  1386. struct netvsc_device *net_device,
  1387. struct net_device *ndev,
  1388. const struct vmpacket_descriptor *desc,
  1389. int budget)
  1390. {
  1391. struct vmbus_channel *channel = nvchan->channel;
  1392. const struct nvsp_message *nvmsg = hv_pkt_data(desc);
  1393. trace_nvsp_recv(ndev, channel, nvmsg);
  1394. switch (desc->type) {
  1395. case VM_PKT_COMP:
  1396. netvsc_send_completion(ndev, net_device, channel, desc, budget);
  1397. break;
  1398. case VM_PKT_DATA_USING_XFER_PAGES:
  1399. return netvsc_receive(ndev, net_device, nvchan, desc);
  1400. case VM_PKT_DATA_INBAND:
  1401. netvsc_receive_inband(ndev, net_device, desc);
  1402. break;
  1403. default:
  1404. netdev_err(ndev, "unhandled packet type %d, tid %llx\n",
  1405. desc->type, desc->trans_id);
  1406. break;
  1407. }
  1408. return 0;
  1409. }
  1410. static struct hv_device *netvsc_channel_to_device(struct vmbus_channel *channel)
  1411. {
  1412. struct vmbus_channel *primary = channel->primary_channel;
  1413. return primary ? primary->device_obj : channel->device_obj;
  1414. }
  1415. /* Network processing softirq
  1416. * Process data in incoming ring buffer from host
  1417. * Stops when ring is empty or budget is met or exceeded.
  1418. */
  1419. int netvsc_poll(struct napi_struct *napi, int budget)
  1420. {
  1421. struct netvsc_channel *nvchan
  1422. = container_of(napi, struct netvsc_channel, napi);
  1423. struct netvsc_device *net_device = nvchan->net_device;
  1424. struct vmbus_channel *channel = nvchan->channel;
  1425. struct hv_device *device = netvsc_channel_to_device(channel);
  1426. struct net_device *ndev = hv_get_drvdata(device);
  1427. int work_done = 0;
  1428. int ret;
  1429. /* If starting a new interval */
  1430. if (!nvchan->desc)
  1431. nvchan->desc = hv_pkt_iter_first(channel);
  1432. nvchan->xdp_flush = false;
  1433. while (nvchan->desc && work_done < budget) {
  1434. work_done += netvsc_process_raw_pkt(device, nvchan, net_device,
  1435. ndev, nvchan->desc, budget);
  1436. nvchan->desc = hv_pkt_iter_next(channel, nvchan->desc);
  1437. }
  1438. if (nvchan->xdp_flush)
  1439. xdp_do_flush();
  1440. /* Send any pending receive completions */
  1441. ret = send_recv_completions(ndev, net_device, nvchan);
  1442. /* If it did not exhaust NAPI budget this time
  1443. * and not doing busy poll
  1444. * then re-enable host interrupts
  1445. * and reschedule if ring is not empty
  1446. * or sending receive completion failed.
  1447. */
  1448. if (work_done < budget &&
  1449. napi_complete_done(napi, work_done) &&
  1450. (ret || hv_end_read(&channel->inbound)) &&
  1451. napi_schedule_prep(napi)) {
  1452. hv_begin_read(&channel->inbound);
  1453. __napi_schedule(napi);
  1454. }
  1455. /* Driver may overshoot since multiple packets per descriptor */
  1456. return min(work_done, budget);
  1457. }
  1458. /* Call back when data is available in host ring buffer.
  1459. * Processing is deferred until network softirq (NAPI)
  1460. */
  1461. void netvsc_channel_cb(void *context)
  1462. {
  1463. struct netvsc_channel *nvchan = context;
  1464. struct vmbus_channel *channel = nvchan->channel;
  1465. struct hv_ring_buffer_info *rbi = &channel->inbound;
  1466. /* preload first vmpacket descriptor */
  1467. prefetch(hv_get_ring_buffer(rbi) + rbi->priv_read_index);
  1468. if (napi_schedule_prep(&nvchan->napi)) {
  1469. /* disable interrupts from host */
  1470. hv_begin_read(rbi);
  1471. __napi_schedule_irqoff(&nvchan->napi);
  1472. }
  1473. }
  1474. /*
  1475. * netvsc_device_add - Callback when the device belonging to this
  1476. * driver is added
  1477. */
  1478. struct netvsc_device *netvsc_device_add(struct hv_device *device,
  1479. const struct netvsc_device_info *device_info)
  1480. {
  1481. int i, ret = 0;
  1482. struct netvsc_device *net_device;
  1483. struct net_device *ndev = hv_get_drvdata(device);
  1484. struct net_device_context *net_device_ctx = netdev_priv(ndev);
  1485. net_device = alloc_net_device();
  1486. if (!net_device)
  1487. return ERR_PTR(-ENOMEM);
  1488. for (i = 0; i < VRSS_SEND_TAB_SIZE; i++)
  1489. net_device_ctx->tx_table[i] = 0;
  1490. /* Because the device uses NAPI, all the interrupt batching and
  1491. * control is done via Net softirq, not the channel handling
  1492. */
  1493. set_channel_read_mode(device->channel, HV_CALL_ISR);
  1494. /* If we're reopening the device we may have multiple queues, fill the
  1495. * chn_table with the default channel to use it before subchannels are
  1496. * opened.
  1497. * Initialize the channel state before we open;
  1498. * we can be interrupted as soon as we open the channel.
  1499. */
  1500. for (i = 0; i < VRSS_CHANNEL_MAX; i++) {
  1501. struct netvsc_channel *nvchan = &net_device->chan_table[i];
  1502. nvchan->channel = device->channel;
  1503. nvchan->net_device = net_device;
  1504. u64_stats_init(&nvchan->tx_stats.syncp);
  1505. u64_stats_init(&nvchan->rx_stats.syncp);
  1506. ret = xdp_rxq_info_reg(&nvchan->xdp_rxq, ndev, i, 0);
  1507. if (ret) {
  1508. netdev_err(ndev, "xdp_rxq_info_reg fail: %d\n", ret);
  1509. goto cleanup2;
  1510. }
  1511. ret = xdp_rxq_info_reg_mem_model(&nvchan->xdp_rxq,
  1512. MEM_TYPE_PAGE_SHARED, NULL);
  1513. if (ret) {
  1514. netdev_err(ndev, "xdp reg_mem_model fail: %d\n", ret);
  1515. goto cleanup2;
  1516. }
  1517. }
  1518. /* Enable NAPI handler before init callbacks */
  1519. netif_napi_add(ndev, &net_device->chan_table[0].napi, netvsc_poll);
  1520. napi_enable(&net_device->chan_table[0].napi);
  1521. netif_queue_set_napi(ndev, 0, NETDEV_QUEUE_TYPE_RX,
  1522. &net_device->chan_table[0].napi);
  1523. netif_queue_set_napi(ndev, 0, NETDEV_QUEUE_TYPE_TX,
  1524. &net_device->chan_table[0].napi);
  1525. /* Open the channel */
  1526. device->channel->next_request_id_callback = vmbus_next_request_id;
  1527. device->channel->request_addr_callback = vmbus_request_addr;
  1528. device->channel->rqstor_size = netvsc_rqstor_size(netvsc_ring_bytes);
  1529. device->channel->max_pkt_size = NETVSC_MAX_PKT_SIZE;
  1530. ret = vmbus_open(device->channel, netvsc_ring_bytes,
  1531. netvsc_ring_bytes, NULL, 0,
  1532. netvsc_channel_cb, net_device->chan_table);
  1533. if (ret != 0) {
  1534. netdev_err(ndev, "unable to open channel: %d\n", ret);
  1535. goto cleanup;
  1536. }
  1537. /* Channel is opened */
  1538. netdev_dbg(ndev, "hv_netvsc channel opened successfully\n");
  1539. /* Connect with the NetVsp */
  1540. ret = netvsc_connect_vsp(device, net_device, device_info);
  1541. if (ret != 0) {
  1542. netdev_err(ndev,
  1543. "unable to connect to NetVSP - %d\n", ret);
  1544. goto close;
  1545. }
  1546. /* Writing nvdev pointer unlocks netvsc_send(), make sure chn_table is
  1547. * populated.
  1548. */
  1549. rcu_assign_pointer(net_device_ctx->nvdev, net_device);
  1550. return net_device;
  1551. close:
  1552. RCU_INIT_POINTER(net_device_ctx->nvdev, NULL);
  1553. /* Now, we can close the channel safely */
  1554. vmbus_close(device->channel);
  1555. cleanup:
  1556. netif_queue_set_napi(ndev, 0, NETDEV_QUEUE_TYPE_TX, NULL);
  1557. netif_queue_set_napi(ndev, 0, NETDEV_QUEUE_TYPE_RX, NULL);
  1558. napi_disable(&net_device->chan_table[0].napi);
  1559. netif_napi_del(&net_device->chan_table[0].napi);
  1560. cleanup2:
  1561. free_netvsc_device(&net_device->rcu);
  1562. return ERR_PTR(ret);
  1563. }