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