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