netvsc.c 36 KB

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