netvsc.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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 <asm/sync_bitops.h>
  31. #include "hyperv_net.h"
  32. static struct netvsc_device *alloc_net_device(struct hv_device *device)
  33. {
  34. struct netvsc_device *net_device;
  35. struct net_device *ndev = hv_get_drvdata(device);
  36. net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
  37. if (!net_device)
  38. return NULL;
  39. init_waitqueue_head(&net_device->wait_drain);
  40. net_device->start_remove = false;
  41. net_device->destroy = false;
  42. net_device->dev = device;
  43. net_device->ndev = ndev;
  44. hv_set_drvdata(device, net_device);
  45. return net_device;
  46. }
  47. static struct netvsc_device *get_outbound_net_device(struct hv_device *device)
  48. {
  49. struct netvsc_device *net_device;
  50. net_device = hv_get_drvdata(device);
  51. if (net_device && net_device->destroy)
  52. net_device = NULL;
  53. return net_device;
  54. }
  55. static struct netvsc_device *get_inbound_net_device(struct hv_device *device)
  56. {
  57. struct netvsc_device *net_device;
  58. net_device = hv_get_drvdata(device);
  59. if (!net_device)
  60. goto get_in_err;
  61. if (net_device->destroy &&
  62. atomic_read(&net_device->num_outstanding_sends) == 0)
  63. net_device = NULL;
  64. get_in_err:
  65. return net_device;
  66. }
  67. static int netvsc_destroy_buf(struct netvsc_device *net_device)
  68. {
  69. struct nvsp_message *revoke_packet;
  70. int ret = 0;
  71. struct net_device *ndev = net_device->ndev;
  72. /*
  73. * If we got a section count, it means we received a
  74. * SendReceiveBufferComplete msg (ie sent
  75. * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
  76. * to send a revoke msg here
  77. */
  78. if (net_device->recv_section_cnt) {
  79. /* Send the revoke receive buffer */
  80. revoke_packet = &net_device->revoke_packet;
  81. memset(revoke_packet, 0, sizeof(struct nvsp_message));
  82. revoke_packet->hdr.msg_type =
  83. NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
  84. revoke_packet->msg.v1_msg.
  85. revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
  86. ret = vmbus_sendpacket(net_device->dev->channel,
  87. revoke_packet,
  88. sizeof(struct nvsp_message),
  89. (unsigned long)revoke_packet,
  90. VM_PKT_DATA_INBAND, 0);
  91. /*
  92. * If we failed here, we might as well return and
  93. * have a leak rather than continue and a bugchk
  94. */
  95. if (ret != 0) {
  96. netdev_err(ndev, "unable to send "
  97. "revoke receive buffer to netvsp\n");
  98. return ret;
  99. }
  100. }
  101. /* Teardown the gpadl on the vsp end */
  102. if (net_device->recv_buf_gpadl_handle) {
  103. ret = vmbus_teardown_gpadl(net_device->dev->channel,
  104. net_device->recv_buf_gpadl_handle);
  105. /* If we failed here, we might as well return and have a leak
  106. * rather than continue and a bugchk
  107. */
  108. if (ret != 0) {
  109. netdev_err(ndev,
  110. "unable to teardown receive buffer's gpadl\n");
  111. return ret;
  112. }
  113. net_device->recv_buf_gpadl_handle = 0;
  114. }
  115. if (net_device->recv_buf) {
  116. /* Free up the receive buffer */
  117. vfree(net_device->recv_buf);
  118. net_device->recv_buf = NULL;
  119. }
  120. if (net_device->recv_section) {
  121. net_device->recv_section_cnt = 0;
  122. kfree(net_device->recv_section);
  123. net_device->recv_section = NULL;
  124. }
  125. /* Deal with the send buffer we may have setup.
  126. * If we got a send section size, it means we received a
  127. * SendsendBufferComplete msg (ie sent
  128. * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
  129. * to send a revoke msg here
  130. */
  131. if (net_device->send_section_size) {
  132. /* Send the revoke receive buffer */
  133. revoke_packet = &net_device->revoke_packet;
  134. memset(revoke_packet, 0, sizeof(struct nvsp_message));
  135. revoke_packet->hdr.msg_type =
  136. NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
  137. revoke_packet->msg.v1_msg.revoke_recv_buf.id = 0;
  138. ret = vmbus_sendpacket(net_device->dev->channel,
  139. revoke_packet,
  140. sizeof(struct nvsp_message),
  141. (unsigned long)revoke_packet,
  142. VM_PKT_DATA_INBAND, 0);
  143. /* If we failed here, we might as well return and
  144. * have a leak rather than continue and a bugchk
  145. */
  146. if (ret != 0) {
  147. netdev_err(ndev, "unable to send "
  148. "revoke send buffer to netvsp\n");
  149. return ret;
  150. }
  151. }
  152. /* Teardown the gpadl on the vsp end */
  153. if (net_device->send_buf_gpadl_handle) {
  154. ret = vmbus_teardown_gpadl(net_device->dev->channel,
  155. net_device->send_buf_gpadl_handle);
  156. /* If we failed here, we might as well return and have a leak
  157. * rather than continue and a bugchk
  158. */
  159. if (ret != 0) {
  160. netdev_err(ndev,
  161. "unable to teardown send buffer's gpadl\n");
  162. return ret;
  163. }
  164. net_device->send_buf_gpadl_handle = 0;
  165. }
  166. if (net_device->send_buf) {
  167. /* Free up the receive buffer */
  168. vfree(net_device->send_buf);
  169. net_device->send_buf = NULL;
  170. }
  171. kfree(net_device->send_section_map);
  172. return ret;
  173. }
  174. static int netvsc_init_buf(struct hv_device *device)
  175. {
  176. int ret = 0;
  177. int t;
  178. struct netvsc_device *net_device;
  179. struct nvsp_message *init_packet;
  180. struct net_device *ndev;
  181. net_device = get_outbound_net_device(device);
  182. if (!net_device)
  183. return -ENODEV;
  184. ndev = net_device->ndev;
  185. net_device->recv_buf = vzalloc(net_device->recv_buf_size);
  186. if (!net_device->recv_buf) {
  187. netdev_err(ndev, "unable to allocate receive "
  188. "buffer of size %d\n", net_device->recv_buf_size);
  189. ret = -ENOMEM;
  190. goto cleanup;
  191. }
  192. /*
  193. * Establish the gpadl handle for this buffer on this
  194. * channel. Note: This call uses the vmbus connection rather
  195. * than the channel to establish the gpadl handle.
  196. */
  197. ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
  198. net_device->recv_buf_size,
  199. &net_device->recv_buf_gpadl_handle);
  200. if (ret != 0) {
  201. netdev_err(ndev,
  202. "unable to establish receive buffer's gpadl\n");
  203. goto cleanup;
  204. }
  205. /* Notify the NetVsp of the gpadl handle */
  206. init_packet = &net_device->channel_init_pkt;
  207. memset(init_packet, 0, sizeof(struct nvsp_message));
  208. init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
  209. init_packet->msg.v1_msg.send_recv_buf.
  210. gpadl_handle = net_device->recv_buf_gpadl_handle;
  211. init_packet->msg.v1_msg.
  212. send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
  213. /* Send the gpadl notification request */
  214. ret = vmbus_sendpacket(device->channel, init_packet,
  215. sizeof(struct nvsp_message),
  216. (unsigned long)init_packet,
  217. VM_PKT_DATA_INBAND,
  218. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  219. if (ret != 0) {
  220. netdev_err(ndev,
  221. "unable to send receive buffer's gpadl to netvsp\n");
  222. goto cleanup;
  223. }
  224. t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
  225. BUG_ON(t == 0);
  226. /* Check the response */
  227. if (init_packet->msg.v1_msg.
  228. send_recv_buf_complete.status != NVSP_STAT_SUCCESS) {
  229. netdev_err(ndev, "Unable to complete receive buffer "
  230. "initialization with NetVsp - status %d\n",
  231. init_packet->msg.v1_msg.
  232. send_recv_buf_complete.status);
  233. ret = -EINVAL;
  234. goto cleanup;
  235. }
  236. /* Parse the response */
  237. net_device->recv_section_cnt = init_packet->msg.
  238. v1_msg.send_recv_buf_complete.num_sections;
  239. net_device->recv_section = kmemdup(
  240. init_packet->msg.v1_msg.send_recv_buf_complete.sections,
  241. net_device->recv_section_cnt *
  242. sizeof(struct nvsp_1_receive_buffer_section),
  243. GFP_KERNEL);
  244. if (net_device->recv_section == NULL) {
  245. ret = -EINVAL;
  246. goto cleanup;
  247. }
  248. /*
  249. * For 1st release, there should only be 1 section that represents the
  250. * entire receive buffer
  251. */
  252. if (net_device->recv_section_cnt != 1 ||
  253. net_device->recv_section->offset != 0) {
  254. ret = -EINVAL;
  255. goto cleanup;
  256. }
  257. /* Now setup the send buffer.
  258. */
  259. net_device->send_buf = vzalloc(net_device->send_buf_size);
  260. if (!net_device->send_buf) {
  261. netdev_err(ndev, "unable to allocate send "
  262. "buffer of size %d\n", net_device->send_buf_size);
  263. ret = -ENOMEM;
  264. goto cleanup;
  265. }
  266. /* Establish the gpadl handle for this buffer on this
  267. * channel. Note: This call uses the vmbus connection rather
  268. * than the channel to establish the gpadl handle.
  269. */
  270. ret = vmbus_establish_gpadl(device->channel, net_device->send_buf,
  271. net_device->send_buf_size,
  272. &net_device->send_buf_gpadl_handle);
  273. if (ret != 0) {
  274. netdev_err(ndev,
  275. "unable to establish send buffer's gpadl\n");
  276. goto cleanup;
  277. }
  278. /* Notify the NetVsp of the gpadl handle */
  279. init_packet = &net_device->channel_init_pkt;
  280. memset(init_packet, 0, sizeof(struct nvsp_message));
  281. init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF;
  282. init_packet->msg.v1_msg.send_recv_buf.gpadl_handle =
  283. net_device->send_buf_gpadl_handle;
  284. init_packet->msg.v1_msg.send_recv_buf.id = 0;
  285. /* Send the gpadl notification request */
  286. ret = vmbus_sendpacket(device->channel, init_packet,
  287. sizeof(struct nvsp_message),
  288. (unsigned long)init_packet,
  289. VM_PKT_DATA_INBAND,
  290. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  291. if (ret != 0) {
  292. netdev_err(ndev,
  293. "unable to send send buffer's gpadl to netvsp\n");
  294. goto cleanup;
  295. }
  296. t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
  297. BUG_ON(t == 0);
  298. /* Check the response */
  299. if (init_packet->msg.v1_msg.
  300. send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
  301. netdev_err(ndev, "Unable to complete send buffer "
  302. "initialization with NetVsp - status %d\n",
  303. init_packet->msg.v1_msg.
  304. send_recv_buf_complete.status);
  305. ret = -EINVAL;
  306. goto cleanup;
  307. }
  308. /* Parse the response */
  309. net_device->send_section_size = init_packet->msg.
  310. v1_msg.send_send_buf_complete.section_size;
  311. /* Section count is simply the size divided by the section size.
  312. */
  313. net_device->send_section_cnt =
  314. net_device->send_buf_size/net_device->send_section_size;
  315. dev_info(&device->device, "Send section size: %d, Section count:%d\n",
  316. net_device->send_section_size, net_device->send_section_cnt);
  317. /* Setup state for managing the send buffer. */
  318. net_device->map_words = DIV_ROUND_UP(net_device->send_section_cnt,
  319. BITS_PER_LONG);
  320. net_device->send_section_map =
  321. kzalloc(net_device->map_words * sizeof(ulong), GFP_KERNEL);
  322. if (net_device->send_section_map == NULL) {
  323. ret = -ENOMEM;
  324. goto cleanup;
  325. }
  326. goto exit;
  327. cleanup:
  328. netvsc_destroy_buf(net_device);
  329. exit:
  330. return ret;
  331. }
  332. /* Negotiate NVSP protocol version */
  333. static int negotiate_nvsp_ver(struct hv_device *device,
  334. struct netvsc_device *net_device,
  335. struct nvsp_message *init_packet,
  336. u32 nvsp_ver)
  337. {
  338. int ret, t;
  339. memset(init_packet, 0, sizeof(struct nvsp_message));
  340. init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
  341. init_packet->msg.init_msg.init.min_protocol_ver = nvsp_ver;
  342. init_packet->msg.init_msg.init.max_protocol_ver = nvsp_ver;
  343. /* Send the init request */
  344. ret = vmbus_sendpacket(device->channel, init_packet,
  345. sizeof(struct nvsp_message),
  346. (unsigned long)init_packet,
  347. VM_PKT_DATA_INBAND,
  348. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  349. if (ret != 0)
  350. return ret;
  351. t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
  352. if (t == 0)
  353. return -ETIMEDOUT;
  354. if (init_packet->msg.init_msg.init_complete.status !=
  355. NVSP_STAT_SUCCESS)
  356. return -EINVAL;
  357. if (nvsp_ver == NVSP_PROTOCOL_VERSION_1)
  358. return 0;
  359. /* NVSPv2 only: Send NDIS config */
  360. memset(init_packet, 0, sizeof(struct nvsp_message));
  361. init_packet->hdr.msg_type = NVSP_MSG2_TYPE_SEND_NDIS_CONFIG;
  362. init_packet->msg.v2_msg.send_ndis_config.mtu = net_device->ndev->mtu;
  363. init_packet->msg.v2_msg.send_ndis_config.capability.ieee8021q = 1;
  364. ret = vmbus_sendpacket(device->channel, init_packet,
  365. sizeof(struct nvsp_message),
  366. (unsigned long)init_packet,
  367. VM_PKT_DATA_INBAND, 0);
  368. return ret;
  369. }
  370. static int netvsc_connect_vsp(struct hv_device *device)
  371. {
  372. int ret;
  373. struct netvsc_device *net_device;
  374. struct nvsp_message *init_packet;
  375. int ndis_version;
  376. struct net_device *ndev;
  377. u32 ver_list[] = { NVSP_PROTOCOL_VERSION_1, NVSP_PROTOCOL_VERSION_2,
  378. NVSP_PROTOCOL_VERSION_4, NVSP_PROTOCOL_VERSION_5 };
  379. int i, num_ver = 4; /* number of different NVSP versions */
  380. net_device = get_outbound_net_device(device);
  381. if (!net_device)
  382. return -ENODEV;
  383. ndev = net_device->ndev;
  384. init_packet = &net_device->channel_init_pkt;
  385. /* Negotiate the latest NVSP protocol supported */
  386. for (i = num_ver - 1; i >= 0; i--)
  387. if (negotiate_nvsp_ver(device, net_device, init_packet,
  388. ver_list[i]) == 0) {
  389. net_device->nvsp_version = ver_list[i];
  390. break;
  391. }
  392. if (i < 0) {
  393. ret = -EPROTO;
  394. goto cleanup;
  395. }
  396. pr_debug("Negotiated NVSP version:%x\n", net_device->nvsp_version);
  397. /* Send the ndis version */
  398. memset(init_packet, 0, sizeof(struct nvsp_message));
  399. if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_4)
  400. ndis_version = 0x00060001;
  401. else
  402. ndis_version = 0x0006001e;
  403. init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
  404. init_packet->msg.v1_msg.
  405. send_ndis_ver.ndis_major_ver =
  406. (ndis_version & 0xFFFF0000) >> 16;
  407. init_packet->msg.v1_msg.
  408. send_ndis_ver.ndis_minor_ver =
  409. ndis_version & 0xFFFF;
  410. /* Send the init request */
  411. ret = vmbus_sendpacket(device->channel, init_packet,
  412. sizeof(struct nvsp_message),
  413. (unsigned long)init_packet,
  414. VM_PKT_DATA_INBAND, 0);
  415. if (ret != 0)
  416. goto cleanup;
  417. /* Post the big receive buffer to NetVSP */
  418. if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
  419. net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
  420. else
  421. net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
  422. net_device->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
  423. ret = netvsc_init_buf(device);
  424. cleanup:
  425. return ret;
  426. }
  427. static void netvsc_disconnect_vsp(struct netvsc_device *net_device)
  428. {
  429. netvsc_destroy_buf(net_device);
  430. }
  431. /*
  432. * netvsc_device_remove - Callback when the root bus device is removed
  433. */
  434. int netvsc_device_remove(struct hv_device *device)
  435. {
  436. struct netvsc_device *net_device;
  437. unsigned long flags;
  438. net_device = hv_get_drvdata(device);
  439. netvsc_disconnect_vsp(net_device);
  440. /*
  441. * Since we have already drained, we don't need to busy wait
  442. * as was done in final_release_stor_device()
  443. * Note that we cannot set the ext pointer to NULL until
  444. * we have drained - to drain the outgoing packets, we need to
  445. * allow incoming packets.
  446. */
  447. spin_lock_irqsave(&device->channel->inbound_lock, flags);
  448. hv_set_drvdata(device, NULL);
  449. spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
  450. /*
  451. * At this point, no one should be accessing net_device
  452. * except in here
  453. */
  454. dev_notice(&device->device, "net device safe to remove\n");
  455. /* Now, we can close the channel safely */
  456. vmbus_close(device->channel);
  457. /* Release all resources */
  458. if (net_device->sub_cb_buf)
  459. vfree(net_device->sub_cb_buf);
  460. kfree(net_device);
  461. return 0;
  462. }
  463. #define RING_AVAIL_PERCENT_HIWATER 20
  464. #define RING_AVAIL_PERCENT_LOWATER 10
  465. /*
  466. * Get the percentage of available bytes to write in the ring.
  467. * The return value is in range from 0 to 100.
  468. */
  469. static inline u32 hv_ringbuf_avail_percent(
  470. struct hv_ring_buffer_info *ring_info)
  471. {
  472. u32 avail_read, avail_write;
  473. hv_get_ringbuffer_availbytes(ring_info, &avail_read, &avail_write);
  474. return avail_write * 100 / ring_info->ring_datasize;
  475. }
  476. static inline void netvsc_free_send_slot(struct netvsc_device *net_device,
  477. u32 index)
  478. {
  479. sync_change_bit(index, net_device->send_section_map);
  480. }
  481. static void netvsc_send_completion(struct netvsc_device *net_device,
  482. struct hv_device *device,
  483. struct vmpacket_descriptor *packet)
  484. {
  485. struct nvsp_message *nvsp_packet;
  486. struct hv_netvsc_packet *nvsc_packet;
  487. struct net_device *ndev;
  488. u32 send_index;
  489. ndev = net_device->ndev;
  490. nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
  491. (packet->offset8 << 3));
  492. if ((nvsp_packet->hdr.msg_type == NVSP_MSG_TYPE_INIT_COMPLETE) ||
  493. (nvsp_packet->hdr.msg_type ==
  494. NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE) ||
  495. (nvsp_packet->hdr.msg_type ==
  496. NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE) ||
  497. (nvsp_packet->hdr.msg_type ==
  498. NVSP_MSG5_TYPE_SUBCHANNEL)) {
  499. /* Copy the response back */
  500. memcpy(&net_device->channel_init_pkt, nvsp_packet,
  501. sizeof(struct nvsp_message));
  502. complete(&net_device->channel_init_wait);
  503. } else if (nvsp_packet->hdr.msg_type ==
  504. NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE) {
  505. int num_outstanding_sends;
  506. u16 q_idx = 0;
  507. struct vmbus_channel *channel = device->channel;
  508. int queue_sends;
  509. /* Get the send context */
  510. nvsc_packet = (struct hv_netvsc_packet *)(unsigned long)
  511. packet->trans_id;
  512. /* Notify the layer above us */
  513. if (nvsc_packet) {
  514. send_index = nvsc_packet->send_buf_index;
  515. if (send_index != NETVSC_INVALID_INDEX)
  516. netvsc_free_send_slot(net_device, send_index);
  517. q_idx = nvsc_packet->q_idx;
  518. channel = nvsc_packet->channel;
  519. nvsc_packet->send_completion(nvsc_packet->
  520. send_completion_ctx);
  521. }
  522. num_outstanding_sends =
  523. atomic_dec_return(&net_device->num_outstanding_sends);
  524. queue_sends = atomic_dec_return(&net_device->
  525. queue_sends[q_idx]);
  526. if (net_device->destroy && num_outstanding_sends == 0)
  527. wake_up(&net_device->wait_drain);
  528. if (netif_tx_queue_stopped(netdev_get_tx_queue(ndev, q_idx)) &&
  529. !net_device->start_remove &&
  530. (hv_ringbuf_avail_percent(&channel->outbound) >
  531. RING_AVAIL_PERCENT_HIWATER || queue_sends < 1))
  532. netif_tx_wake_queue(netdev_get_tx_queue(
  533. ndev, q_idx));
  534. } else {
  535. netdev_err(ndev, "Unknown send completion packet type- "
  536. "%d received!!\n", nvsp_packet->hdr.msg_type);
  537. }
  538. }
  539. static u32 netvsc_get_next_send_section(struct netvsc_device *net_device)
  540. {
  541. unsigned long index;
  542. u32 max_words = net_device->map_words;
  543. unsigned long *map_addr = (unsigned long *)net_device->send_section_map;
  544. u32 section_cnt = net_device->send_section_cnt;
  545. int ret_val = NETVSC_INVALID_INDEX;
  546. int i;
  547. int prev_val;
  548. for (i = 0; i < max_words; i++) {
  549. if (!~(map_addr[i]))
  550. continue;
  551. index = ffz(map_addr[i]);
  552. prev_val = sync_test_and_set_bit(index, &map_addr[i]);
  553. if (prev_val)
  554. continue;
  555. if ((index + (i * BITS_PER_LONG)) >= section_cnt)
  556. break;
  557. ret_val = (index + (i * BITS_PER_LONG));
  558. break;
  559. }
  560. return ret_val;
  561. }
  562. u32 netvsc_copy_to_send_buf(struct netvsc_device *net_device,
  563. unsigned int section_index,
  564. struct hv_netvsc_packet *packet)
  565. {
  566. char *start = net_device->send_buf;
  567. char *dest = (start + (section_index * net_device->send_section_size));
  568. int i;
  569. u32 msg_size = 0;
  570. for (i = 0; i < packet->page_buf_cnt; i++) {
  571. char *src = phys_to_virt(packet->page_buf[i].pfn << PAGE_SHIFT);
  572. u32 offset = packet->page_buf[i].offset;
  573. u32 len = packet->page_buf[i].len;
  574. memcpy(dest, (src + offset), len);
  575. msg_size += len;
  576. dest += len;
  577. }
  578. return msg_size;
  579. }
  580. int netvsc_send(struct hv_device *device,
  581. struct hv_netvsc_packet *packet)
  582. {
  583. struct netvsc_device *net_device;
  584. int ret = 0;
  585. struct nvsp_message sendMessage;
  586. struct net_device *ndev;
  587. struct vmbus_channel *out_channel = NULL;
  588. u64 req_id;
  589. unsigned int section_index = NETVSC_INVALID_INDEX;
  590. u32 msg_size = 0;
  591. struct sk_buff *skb;
  592. net_device = get_outbound_net_device(device);
  593. if (!net_device)
  594. return -ENODEV;
  595. ndev = net_device->ndev;
  596. sendMessage.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
  597. if (packet->is_data_pkt) {
  598. /* 0 is RMC_DATA; */
  599. sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 0;
  600. } else {
  601. /* 1 is RMC_CONTROL; */
  602. sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 1;
  603. }
  604. /* Attempt to send via sendbuf */
  605. if (packet->total_data_buflen < net_device->send_section_size) {
  606. section_index = netvsc_get_next_send_section(net_device);
  607. if (section_index != NETVSC_INVALID_INDEX) {
  608. msg_size = netvsc_copy_to_send_buf(net_device,
  609. section_index,
  610. packet);
  611. skb = (struct sk_buff *)
  612. (unsigned long)packet->send_completion_tid;
  613. if (skb)
  614. dev_kfree_skb_any(skb);
  615. packet->page_buf_cnt = 0;
  616. }
  617. }
  618. packet->send_buf_index = section_index;
  619. sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_index =
  620. section_index;
  621. sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_size = msg_size;
  622. if (packet->send_completion)
  623. req_id = (ulong)packet;
  624. else
  625. req_id = 0;
  626. out_channel = net_device->chn_table[packet->q_idx];
  627. if (out_channel == NULL)
  628. out_channel = device->channel;
  629. packet->channel = out_channel;
  630. if (packet->page_buf_cnt) {
  631. ret = vmbus_sendpacket_pagebuffer(out_channel,
  632. packet->page_buf,
  633. packet->page_buf_cnt,
  634. &sendMessage,
  635. sizeof(struct nvsp_message),
  636. req_id);
  637. } else {
  638. ret = vmbus_sendpacket(out_channel, &sendMessage,
  639. sizeof(struct nvsp_message),
  640. req_id,
  641. VM_PKT_DATA_INBAND,
  642. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  643. }
  644. if (ret == 0) {
  645. atomic_inc(&net_device->num_outstanding_sends);
  646. atomic_inc(&net_device->queue_sends[packet->q_idx]);
  647. if (hv_ringbuf_avail_percent(&out_channel->outbound) <
  648. RING_AVAIL_PERCENT_LOWATER) {
  649. netif_tx_stop_queue(netdev_get_tx_queue(
  650. ndev, packet->q_idx));
  651. if (atomic_read(&net_device->
  652. queue_sends[packet->q_idx]) < 1)
  653. netif_tx_wake_queue(netdev_get_tx_queue(
  654. ndev, packet->q_idx));
  655. }
  656. } else if (ret == -EAGAIN) {
  657. netif_tx_stop_queue(netdev_get_tx_queue(
  658. ndev, packet->q_idx));
  659. if (atomic_read(&net_device->queue_sends[packet->q_idx]) < 1) {
  660. netif_tx_wake_queue(netdev_get_tx_queue(
  661. ndev, packet->q_idx));
  662. ret = -ENOSPC;
  663. }
  664. } else {
  665. netdev_err(ndev, "Unable to send packet %p ret %d\n",
  666. packet, ret);
  667. }
  668. return ret;
  669. }
  670. static void netvsc_send_recv_completion(struct hv_device *device,
  671. struct vmbus_channel *channel,
  672. struct netvsc_device *net_device,
  673. u64 transaction_id, u32 status)
  674. {
  675. struct nvsp_message recvcompMessage;
  676. int retries = 0;
  677. int ret;
  678. struct net_device *ndev;
  679. ndev = net_device->ndev;
  680. recvcompMessage.hdr.msg_type =
  681. NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
  682. recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status = status;
  683. retry_send_cmplt:
  684. /* Send the completion */
  685. ret = vmbus_sendpacket(channel, &recvcompMessage,
  686. sizeof(struct nvsp_message), transaction_id,
  687. VM_PKT_COMP, 0);
  688. if (ret == 0) {
  689. /* success */
  690. /* no-op */
  691. } else if (ret == -EAGAIN) {
  692. /* no more room...wait a bit and attempt to retry 3 times */
  693. retries++;
  694. netdev_err(ndev, "unable to send receive completion pkt"
  695. " (tid %llx)...retrying %d\n", transaction_id, retries);
  696. if (retries < 4) {
  697. udelay(100);
  698. goto retry_send_cmplt;
  699. } else {
  700. netdev_err(ndev, "unable to send receive "
  701. "completion pkt (tid %llx)...give up retrying\n",
  702. transaction_id);
  703. }
  704. } else {
  705. netdev_err(ndev, "unable to send receive "
  706. "completion pkt - %llx\n", transaction_id);
  707. }
  708. }
  709. static void netvsc_receive(struct netvsc_device *net_device,
  710. struct vmbus_channel *channel,
  711. struct hv_device *device,
  712. struct vmpacket_descriptor *packet)
  713. {
  714. struct vmtransfer_page_packet_header *vmxferpage_packet;
  715. struct nvsp_message *nvsp_packet;
  716. struct hv_netvsc_packet nv_pkt;
  717. struct hv_netvsc_packet *netvsc_packet = &nv_pkt;
  718. u32 status = NVSP_STAT_SUCCESS;
  719. int i;
  720. int count = 0;
  721. struct net_device *ndev;
  722. ndev = net_device->ndev;
  723. /*
  724. * All inbound packets other than send completion should be xfer page
  725. * packet
  726. */
  727. if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) {
  728. netdev_err(ndev, "Unknown packet type received - %d\n",
  729. packet->type);
  730. return;
  731. }
  732. nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
  733. (packet->offset8 << 3));
  734. /* Make sure this is a valid nvsp packet */
  735. if (nvsp_packet->hdr.msg_type !=
  736. NVSP_MSG1_TYPE_SEND_RNDIS_PKT) {
  737. netdev_err(ndev, "Unknown nvsp packet type received-"
  738. " %d\n", nvsp_packet->hdr.msg_type);
  739. return;
  740. }
  741. vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
  742. if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) {
  743. netdev_err(ndev, "Invalid xfer page set id - "
  744. "expecting %x got %x\n", NETVSC_RECEIVE_BUFFER_ID,
  745. vmxferpage_packet->xfer_pageset_id);
  746. return;
  747. }
  748. count = vmxferpage_packet->range_cnt;
  749. netvsc_packet->device = device;
  750. netvsc_packet->channel = channel;
  751. /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
  752. for (i = 0; i < count; i++) {
  753. /* Initialize the netvsc packet */
  754. netvsc_packet->status = NVSP_STAT_SUCCESS;
  755. netvsc_packet->data = (void *)((unsigned long)net_device->
  756. recv_buf + vmxferpage_packet->ranges[i].byte_offset);
  757. netvsc_packet->total_data_buflen =
  758. vmxferpage_packet->ranges[i].byte_count;
  759. /* Pass it to the upper layer */
  760. rndis_filter_receive(device, netvsc_packet);
  761. if (netvsc_packet->status != NVSP_STAT_SUCCESS)
  762. status = NVSP_STAT_FAIL;
  763. }
  764. netvsc_send_recv_completion(device, channel, net_device,
  765. vmxferpage_packet->d.trans_id, status);
  766. }
  767. static void netvsc_send_table(struct hv_device *hdev,
  768. struct vmpacket_descriptor *vmpkt)
  769. {
  770. struct netvsc_device *nvscdev;
  771. struct net_device *ndev;
  772. struct nvsp_message *nvmsg;
  773. int i;
  774. u32 count, *tab;
  775. nvscdev = get_outbound_net_device(hdev);
  776. if (!nvscdev)
  777. return;
  778. ndev = nvscdev->ndev;
  779. nvmsg = (struct nvsp_message *)((unsigned long)vmpkt +
  780. (vmpkt->offset8 << 3));
  781. if (nvmsg->hdr.msg_type != NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE)
  782. return;
  783. count = nvmsg->msg.v5_msg.send_table.count;
  784. if (count != VRSS_SEND_TAB_SIZE) {
  785. netdev_err(ndev, "Received wrong send-table size:%u\n", count);
  786. return;
  787. }
  788. tab = (u32 *)((unsigned long)&nvmsg->msg.v5_msg.send_table +
  789. nvmsg->msg.v5_msg.send_table.offset);
  790. for (i = 0; i < count; i++)
  791. nvscdev->send_table[i] = tab[i];
  792. }
  793. void netvsc_channel_cb(void *context)
  794. {
  795. int ret;
  796. struct vmbus_channel *channel = (struct vmbus_channel *)context;
  797. struct hv_device *device;
  798. struct netvsc_device *net_device;
  799. u32 bytes_recvd;
  800. u64 request_id;
  801. struct vmpacket_descriptor *desc;
  802. unsigned char *buffer;
  803. int bufferlen = NETVSC_PACKET_SIZE;
  804. struct net_device *ndev;
  805. if (channel->primary_channel != NULL)
  806. device = channel->primary_channel->device_obj;
  807. else
  808. device = channel->device_obj;
  809. net_device = get_inbound_net_device(device);
  810. if (!net_device)
  811. return;
  812. ndev = net_device->ndev;
  813. buffer = get_per_channel_state(channel);
  814. do {
  815. ret = vmbus_recvpacket_raw(channel, buffer, bufferlen,
  816. &bytes_recvd, &request_id);
  817. if (ret == 0) {
  818. if (bytes_recvd > 0) {
  819. desc = (struct vmpacket_descriptor *)buffer;
  820. switch (desc->type) {
  821. case VM_PKT_COMP:
  822. netvsc_send_completion(net_device,
  823. device, desc);
  824. break;
  825. case VM_PKT_DATA_USING_XFER_PAGES:
  826. netvsc_receive(net_device, channel,
  827. device, desc);
  828. break;
  829. case VM_PKT_DATA_INBAND:
  830. netvsc_send_table(device, desc);
  831. break;
  832. default:
  833. netdev_err(ndev,
  834. "unhandled packet type %d, "
  835. "tid %llx len %d\n",
  836. desc->type, request_id,
  837. bytes_recvd);
  838. break;
  839. }
  840. } else {
  841. /*
  842. * We are done for this pass.
  843. */
  844. break;
  845. }
  846. } else if (ret == -ENOBUFS) {
  847. if (bufferlen > NETVSC_PACKET_SIZE)
  848. kfree(buffer);
  849. /* Handle large packet */
  850. buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
  851. if (buffer == NULL) {
  852. /* Try again next time around */
  853. netdev_err(ndev,
  854. "unable to allocate buffer of size "
  855. "(%d)!!\n", bytes_recvd);
  856. break;
  857. }
  858. bufferlen = bytes_recvd;
  859. }
  860. } while (1);
  861. if (bufferlen > NETVSC_PACKET_SIZE)
  862. kfree(buffer);
  863. return;
  864. }
  865. /*
  866. * netvsc_device_add - Callback when the device belonging to this
  867. * driver is added
  868. */
  869. int netvsc_device_add(struct hv_device *device, void *additional_info)
  870. {
  871. int ret = 0;
  872. int ring_size =
  873. ((struct netvsc_device_info *)additional_info)->ring_size;
  874. struct netvsc_device *net_device;
  875. struct net_device *ndev;
  876. net_device = alloc_net_device(device);
  877. if (!net_device) {
  878. ret = -ENOMEM;
  879. goto cleanup;
  880. }
  881. net_device->ring_size = ring_size;
  882. /*
  883. * Coming into this function, struct net_device * is
  884. * registered as the driver private data.
  885. * In alloc_net_device(), we register struct netvsc_device *
  886. * as the driver private data and stash away struct net_device *
  887. * in struct netvsc_device *.
  888. */
  889. ndev = net_device->ndev;
  890. /* Initialize the NetVSC channel extension */
  891. init_completion(&net_device->channel_init_wait);
  892. set_per_channel_state(device->channel, net_device->cb_buffer);
  893. /* Open the channel */
  894. ret = vmbus_open(device->channel, ring_size * PAGE_SIZE,
  895. ring_size * PAGE_SIZE, NULL, 0,
  896. netvsc_channel_cb, device->channel);
  897. if (ret != 0) {
  898. netdev_err(ndev, "unable to open channel: %d\n", ret);
  899. goto cleanup;
  900. }
  901. /* Channel is opened */
  902. pr_info("hv_netvsc channel opened successfully\n");
  903. net_device->chn_table[0] = device->channel;
  904. /* Connect with the NetVsp */
  905. ret = netvsc_connect_vsp(device);
  906. if (ret != 0) {
  907. netdev_err(ndev,
  908. "unable to connect to NetVSP - %d\n", ret);
  909. goto close;
  910. }
  911. return ret;
  912. close:
  913. /* Now, we can close the channel safely */
  914. vmbus_close(device->channel);
  915. cleanup:
  916. kfree(net_device);
  917. return ret;
  918. }