]>
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: | |
17 | * Haiyang Zhang <haiyangz@microsoft.com> | |
18 | * Hank Janssen <hjanssen@microsoft.com> | |
fceaf24a | 19 | */ |
5654e932 | 20 | #include <linux/kernel.h> |
0c3b7b2f S |
21 | #include <linux/sched.h> |
22 | #include <linux/wait.h> | |
45da89e5 | 23 | #include <linux/highmem.h> |
5a0e3ad6 | 24 | #include <linux/slab.h> |
0120ee0d | 25 | #include <linux/io.h> |
9f8bd8ba | 26 | #include <linux/if_ether.h> |
eb335bc4 | 27 | #include <linux/netdevice.h> |
1f5f3a75 | 28 | #include <linux/if_vlan.h> |
1ce09e89 | 29 | #include <linux/nls.h> |
d6472302 | 30 | #include <linux/vmalloc.h> |
27f5aa92 | 31 | #include <linux/rtnetlink.h> |
3f335ea2 | 32 | |
5ca7252a | 33 | #include "hyperv_net.h" |
fceaf24a | 34 | |
4f19c0d8 | 35 | static void rndis_set_multicast(struct work_struct *w); |
fceaf24a | 36 | |
5b54dac8 | 37 | #define RNDIS_EXT_LEN PAGE_SIZE |
e681b954 | 38 | struct rndis_request { |
c2a4efdd | 39 | struct list_head list_ent; |
98d79690 | 40 | struct completion wait_event; |
fceaf24a | 41 | |
a3a6cab5 | 42 | struct rndis_message response_msg; |
0120ee0d | 43 | /* |
a3a6cab5 HZ |
44 | * The buffer for extended info after the RNDIS response message. It's |
45 | * referenced based on the data offset in the RNDIS message. Its size | |
46 | * is enough for current needs, and should be sufficient for the near | |
47 | * future. | |
0120ee0d | 48 | */ |
a3a6cab5 | 49 | u8 response_ext[RNDIS_EXT_LEN]; |
fceaf24a | 50 | |
454f18a9 | 51 | /* Simplify allocation by having a netvsc packet inline */ |
c2a4efdd | 52 | struct hv_netvsc_packet pkt; |
0f48917b | 53 | |
c2a4efdd | 54 | struct rndis_message request_msg; |
0f48917b | 55 | /* |
a3a6cab5 HZ |
56 | * The buffer for the extended info after the RNDIS request message. |
57 | * It is referenced and sized in a similar way as response_ext. | |
0f48917b | 58 | */ |
a3a6cab5 | 59 | u8 request_ext[RNDIS_EXT_LEN]; |
e681b954 | 60 | }; |
fceaf24a | 61 | |
962f3fee | 62 | static const u8 netvsc_hash_key[NETVSC_HASH_KEYLEN] = { |
63 | 0x6d, 0x5a, 0x56, 0xda, 0x25, 0x5b, 0x0e, 0xc2, | |
64 | 0x41, 0x67, 0x25, 0x3d, 0x43, 0xa3, 0x8f, 0xb0, | |
65 | 0xd0, 0xca, 0x2b, 0xcb, 0xae, 0x7b, 0x30, 0xb4, | |
66 | 0x77, 0xcb, 0x2d, 0xa3, 0x80, 0x30, 0xf2, 0x0c, | |
67 | 0x6a, 0x42, 0xb7, 0x3b, 0xbe, 0xac, 0x01, 0xfa | |
68 | }; | |
69 | ||
9c26aa0d | 70 | static struct rndis_device *get_rndis_device(void) |
fceaf24a | 71 | { |
e681b954 | 72 | struct rndis_device *device; |
fceaf24a | 73 | |
e681b954 | 74 | device = kzalloc(sizeof(struct rndis_device), GFP_KERNEL); |
fceaf24a | 75 | if (!device) |
fceaf24a | 76 | return NULL; |
fceaf24a | 77 | |
880fb89c | 78 | spin_lock_init(&device->request_lock); |
fceaf24a | 79 | |
c2a4efdd | 80 | INIT_LIST_HEAD(&device->req_list); |
4f19c0d8 | 81 | INIT_WORK(&device->mcast_work, rndis_set_multicast); |
fceaf24a | 82 | |
c2a4efdd | 83 | device->state = RNDIS_DEV_UNINITIALIZED; |
fceaf24a HJ |
84 | |
85 | return device; | |
86 | } | |
87 | ||
3962981f | 88 | static struct netvsc_device * |
89 | net_device_to_netvsc_device(struct net_device *ndev) | |
90 | { | |
91 | struct net_device_context *net_device_ctx = netdev_priv(ndev); | |
92 | ||
93 | return rtnl_dereference(net_device_ctx->nvdev); | |
94 | } | |
95 | ||
9c26aa0d | 96 | static struct rndis_request *get_rndis_request(struct rndis_device *dev, |
c2a4efdd HZ |
97 | u32 msg_type, |
98 | u32 msg_len) | |
fceaf24a | 99 | { |
e681b954 | 100 | struct rndis_request *request; |
c2a4efdd | 101 | struct rndis_message *rndis_msg; |
9f33d054 | 102 | struct rndis_set_request *set; |
880fb89c | 103 | unsigned long flags; |
fceaf24a | 104 | |
e681b954 | 105 | request = kzalloc(sizeof(struct rndis_request), GFP_KERNEL); |
fceaf24a | 106 | if (!request) |
fceaf24a | 107 | return NULL; |
fceaf24a | 108 | |
98d79690 | 109 | init_completion(&request->wait_event); |
fceaf24a | 110 | |
c2a4efdd | 111 | rndis_msg = &request->request_msg; |
a388eb17 HZ |
112 | rndis_msg->ndis_msg_type = msg_type; |
113 | rndis_msg->msg_len = msg_len; | |
fceaf24a | 114 | |
5b54dac8 HZ |
115 | request->pkt.q_idx = 0; |
116 | ||
0120ee0d GKH |
117 | /* |
118 | * Set the request id. This field is always after the rndis header for | |
119 | * request/response packet types so we just used the SetRequest as a | |
120 | * template | |
121 | */ | |
a388eb17 HZ |
122 | set = &rndis_msg->msg.set_req; |
123 | set->req_id = atomic_inc_return(&dev->new_req_id); | |
fceaf24a | 124 | |
454f18a9 | 125 | /* Add to the request list */ |
c2a4efdd HZ |
126 | spin_lock_irqsave(&dev->request_lock, flags); |
127 | list_add_tail(&request->list_ent, &dev->req_list); | |
128 | spin_unlock_irqrestore(&dev->request_lock, flags); | |
fceaf24a HJ |
129 | |
130 | return request; | |
131 | } | |
132 | ||
9c26aa0d | 133 | static void put_rndis_request(struct rndis_device *dev, |
c2a4efdd | 134 | struct rndis_request *req) |
fceaf24a | 135 | { |
880fb89c GKH |
136 | unsigned long flags; |
137 | ||
c2a4efdd HZ |
138 | spin_lock_irqsave(&dev->request_lock, flags); |
139 | list_del(&req->list_ent); | |
140 | spin_unlock_irqrestore(&dev->request_lock, flags); | |
fceaf24a | 141 | |
c2a4efdd | 142 | kfree(req); |
fceaf24a HJ |
143 | } |
144 | ||
729a2849 | 145 | static void dump_rndis_message(struct hv_device *hv_dev, |
dc54a08c | 146 | const struct rndis_message *rndis_msg) |
fceaf24a | 147 | { |
3d541ac5 | 148 | struct net_device *netdev = hv_get_drvdata(hv_dev); |
729a2849 | 149 | |
a388eb17 | 150 | switch (rndis_msg->ndis_msg_type) { |
51491167 LW |
151 | case RNDIS_MSG_PACKET: |
152 | netdev_dbg(netdev, "RNDIS_MSG_PACKET (len %u, " | |
0120ee0d GKH |
153 | "data offset %u data len %u, # oob %u, " |
154 | "oob offset %u, oob len %u, pkt offset %u, " | |
729a2849 | 155 | "pkt len %u\n", |
a388eb17 HZ |
156 | rndis_msg->msg_len, |
157 | rndis_msg->msg.pkt.data_offset, | |
158 | rndis_msg->msg.pkt.data_len, | |
159 | rndis_msg->msg.pkt.num_oob_data_elements, | |
160 | rndis_msg->msg.pkt.oob_data_offset, | |
161 | rndis_msg->msg.pkt.oob_data_len, | |
162 | rndis_msg->msg.pkt.per_pkt_info_offset, | |
163 | rndis_msg->msg.pkt.per_pkt_info_len); | |
fceaf24a HJ |
164 | break; |
165 | ||
51491167 LW |
166 | case RNDIS_MSG_INIT_C: |
167 | netdev_dbg(netdev, "RNDIS_MSG_INIT_C " | |
0120ee0d GKH |
168 | "(len %u, id 0x%x, status 0x%x, major %d, minor %d, " |
169 | "device flags %d, max xfer size 0x%x, max pkts %u, " | |
729a2849 | 170 | "pkt aligned %u)\n", |
a388eb17 HZ |
171 | rndis_msg->msg_len, |
172 | rndis_msg->msg.init_complete.req_id, | |
173 | rndis_msg->msg.init_complete.status, | |
174 | rndis_msg->msg.init_complete.major_ver, | |
175 | rndis_msg->msg.init_complete.minor_ver, | |
176 | rndis_msg->msg.init_complete.dev_flags, | |
177 | rndis_msg->msg.init_complete.max_xfer_size, | |
178 | rndis_msg->msg.init_complete. | |
179 | max_pkt_per_msg, | |
180 | rndis_msg->msg.init_complete. | |
181 | pkt_alignment_factor); | |
fceaf24a HJ |
182 | break; |
183 | ||
51491167 LW |
184 | case RNDIS_MSG_QUERY_C: |
185 | netdev_dbg(netdev, "RNDIS_MSG_QUERY_C " | |
0120ee0d | 186 | "(len %u, id 0x%x, status 0x%x, buf len %u, " |
729a2849 | 187 | "buf offset %u)\n", |
a388eb17 HZ |
188 | rndis_msg->msg_len, |
189 | rndis_msg->msg.query_complete.req_id, | |
190 | rndis_msg->msg.query_complete.status, | |
191 | rndis_msg->msg.query_complete. | |
192 | info_buflen, | |
193 | rndis_msg->msg.query_complete. | |
194 | info_buf_offset); | |
fceaf24a HJ |
195 | break; |
196 | ||
51491167 | 197 | case RNDIS_MSG_SET_C: |
729a2849 | 198 | netdev_dbg(netdev, |
51491167 | 199 | "RNDIS_MSG_SET_C (len %u, id 0x%x, status 0x%x)\n", |
a388eb17 HZ |
200 | rndis_msg->msg_len, |
201 | rndis_msg->msg.set_complete.req_id, | |
202 | rndis_msg->msg.set_complete.status); | |
fceaf24a HJ |
203 | break; |
204 | ||
51491167 LW |
205 | case RNDIS_MSG_INDICATE: |
206 | netdev_dbg(netdev, "RNDIS_MSG_INDICATE " | |
729a2849 | 207 | "(len %u, status 0x%x, buf len %u, buf offset %u)\n", |
a388eb17 HZ |
208 | rndis_msg->msg_len, |
209 | rndis_msg->msg.indicate_status.status, | |
210 | rndis_msg->msg.indicate_status.status_buflen, | |
211 | rndis_msg->msg.indicate_status.status_buf_offset); | |
fceaf24a HJ |
212 | break; |
213 | ||
214 | default: | |
729a2849 | 215 | netdev_dbg(netdev, "0x%x (len %u)\n", |
a388eb17 HZ |
216 | rndis_msg->ndis_msg_type, |
217 | rndis_msg->msg_len); | |
fceaf24a HJ |
218 | break; |
219 | } | |
220 | } | |
221 | ||
9c26aa0d | 222 | static int rndis_filter_send_request(struct rndis_device *dev, |
c2a4efdd | 223 | struct rndis_request *req) |
fceaf24a | 224 | { |
0120ee0d | 225 | int ret; |
4193d4f4 | 226 | struct hv_netvsc_packet *packet; |
b08cc791 | 227 | struct hv_page_buffer page_buf[2]; |
a9f2e2d6 | 228 | struct hv_page_buffer *pb = page_buf; |
3d541ac5 | 229 | struct net_device_context *net_device_ctx = netdev_priv(dev->ndev); |
fceaf24a | 230 | |
454f18a9 | 231 | /* Setup the packet to send it */ |
c2a4efdd | 232 | packet = &req->pkt; |
fceaf24a | 233 | |
a388eb17 | 234 | packet->total_data_buflen = req->request_msg.msg_len; |
72a2f5bd | 235 | packet->page_buf_cnt = 1; |
fceaf24a | 236 | |
a9f2e2d6 | 237 | pb[0].pfn = virt_to_phys(&req->request_msg) >> |
0120ee0d | 238 | PAGE_SHIFT; |
a9f2e2d6 KS |
239 | pb[0].len = req->request_msg.msg_len; |
240 | pb[0].offset = | |
c2a4efdd | 241 | (unsigned long)&req->request_msg & (PAGE_SIZE - 1); |
fceaf24a | 242 | |
99e3fcfa | 243 | /* Add one page_buf when request_msg crossing page boundary */ |
a9f2e2d6 | 244 | if (pb[0].offset + pb[0].len > PAGE_SIZE) { |
99e3fcfa | 245 | packet->page_buf_cnt++; |
a9f2e2d6 KS |
246 | pb[0].len = PAGE_SIZE - |
247 | pb[0].offset; | |
248 | pb[1].pfn = virt_to_phys((void *)&req->request_msg | |
249 | + pb[0].len) >> PAGE_SHIFT; | |
250 | pb[1].offset = 0; | |
251 | pb[1].len = req->request_msg.msg_len - | |
252 | pb[0].len; | |
99e3fcfa HZ |
253 | } |
254 | ||
2a926f79 | 255 | ret = netvsc_send(net_device_ctx, packet, NULL, &pb, NULL); |
fceaf24a HJ |
256 | return ret; |
257 | } | |
258 | ||
1b07da51 HZ |
259 | static void rndis_set_link_state(struct rndis_device *rdev, |
260 | struct rndis_request *request) | |
261 | { | |
262 | u32 link_status; | |
263 | struct rndis_query_complete *query_complete; | |
264 | ||
265 | query_complete = &request->response_msg.msg.query_complete; | |
266 | ||
267 | if (query_complete->status == RNDIS_STATUS_SUCCESS && | |
268 | query_complete->info_buflen == sizeof(u32)) { | |
269 | memcpy(&link_status, (void *)((unsigned long)query_complete + | |
270 | query_complete->info_buf_offset), sizeof(u32)); | |
271 | rdev->link_state = link_status != 0; | |
272 | } | |
273 | } | |
274 | ||
9c26aa0d | 275 | static void rndis_filter_receive_response(struct rndis_device *dev, |
c2a4efdd | 276 | struct rndis_message *resp) |
fceaf24a | 277 | { |
e681b954 | 278 | struct rndis_request *request = NULL; |
0e727613 | 279 | bool found = false; |
880fb89c | 280 | unsigned long flags; |
3d541ac5 | 281 | struct net_device *ndev = dev->ndev; |
fceaf24a | 282 | |
c2a4efdd HZ |
283 | spin_lock_irqsave(&dev->request_lock, flags); |
284 | list_for_each_entry(request, &dev->req_list, list_ent) { | |
0120ee0d GKH |
285 | /* |
286 | * All request/response message contains RequestId as the 1st | |
287 | * field | |
288 | */ | |
a388eb17 HZ |
289 | if (request->request_msg.msg.init_req.req_id |
290 | == resp->msg.init_complete.req_id) { | |
0e727613 | 291 | found = true; |
fceaf24a HJ |
292 | break; |
293 | } | |
294 | } | |
c2a4efdd | 295 | spin_unlock_irqrestore(&dev->request_lock, flags); |
fceaf24a | 296 | |
0120ee0d | 297 | if (found) { |
a3a6cab5 HZ |
298 | if (resp->msg_len <= |
299 | sizeof(struct rndis_message) + RNDIS_EXT_LEN) { | |
c2a4efdd | 300 | memcpy(&request->response_msg, resp, |
a388eb17 | 301 | resp->msg_len); |
1b07da51 HZ |
302 | if (request->request_msg.ndis_msg_type == |
303 | RNDIS_MSG_QUERY && request->request_msg.msg. | |
304 | query_req.oid == RNDIS_OID_GEN_MEDIA_CONNECT_STATUS) | |
305 | rndis_set_link_state(dev, request); | |
0120ee0d | 306 | } else { |
d9871158 | 307 | netdev_err(ndev, |
eb335bc4 HJ |
308 | "rndis response buffer overflow " |
309 | "detected (size %u max %zu)\n", | |
310 | resp->msg_len, | |
86eedacc | 311 | sizeof(struct rndis_message)); |
0120ee0d | 312 | |
a388eb17 | 313 | if (resp->ndis_msg_type == |
51491167 | 314 | RNDIS_MSG_RESET_C) { |
0120ee0d | 315 | /* does not have a request id field */ |
a388eb17 | 316 | request->response_msg.msg.reset_complete. |
007e5c8e | 317 | status = RNDIS_STATUS_BUFFER_OVERFLOW; |
0120ee0d | 318 | } else { |
a388eb17 HZ |
319 | request->response_msg.msg. |
320 | init_complete.status = | |
007e5c8e | 321 | RNDIS_STATUS_BUFFER_OVERFLOW; |
fceaf24a HJ |
322 | } |
323 | } | |
324 | ||
98d79690 | 325 | complete(&request->wait_event); |
0120ee0d | 326 | } else { |
d9871158 | 327 | netdev_err(ndev, |
eb335bc4 HJ |
328 | "no rndis request found for this response " |
329 | "(id 0x%x res type 0x%x)\n", | |
330 | resp->msg.init_complete.req_id, | |
331 | resp->ndis_msg_type); | |
fceaf24a | 332 | } |
fceaf24a HJ |
333 | } |
334 | ||
1f5f3a75 HZ |
335 | /* |
336 | * Get the Per-Packet-Info with the specified type | |
337 | * return NULL if not found. | |
338 | */ | |
339 | static inline void *rndis_get_ppi(struct rndis_packet *rpkt, u32 type) | |
340 | { | |
341 | struct rndis_per_packet_info *ppi; | |
342 | int len; | |
343 | ||
344 | if (rpkt->per_pkt_info_offset == 0) | |
345 | return NULL; | |
346 | ||
347 | ppi = (struct rndis_per_packet_info *)((ulong)rpkt + | |
348 | rpkt->per_pkt_info_offset); | |
349 | len = rpkt->per_pkt_info_len; | |
350 | ||
351 | while (len > 0) { | |
352 | if (ppi->type == type) | |
353 | return (void *)((ulong)ppi + ppi->ppi_offset); | |
354 | len -= ppi->size; | |
355 | ppi = (struct rndis_per_packet_info *)((ulong)ppi + ppi->size); | |
356 | } | |
357 | ||
358 | return NULL; | |
359 | } | |
360 | ||
dc54a08c | 361 | static int rndis_filter_receive_data(struct net_device *ndev, |
362 | struct rndis_device *dev, | |
363 | struct rndis_message *msg, | |
364 | struct vmbus_channel *channel, | |
365 | void *data, u32 data_buflen) | |
fceaf24a | 366 | { |
dc54a08c | 367 | struct rndis_packet *rndis_pkt = &msg->msg.pkt; |
368 | const struct ndis_tcp_ip_checksum_info *csum_info; | |
369 | const struct ndis_pkt_8021q_info *vlan; | |
c2a4efdd | 370 | u32 data_offset; |
fceaf24a | 371 | |
454f18a9 | 372 | /* Remove the rndis header and pass it back up the stack */ |
a388eb17 | 373 | data_offset = RNDIS_HEADER_SIZE + rndis_pkt->data_offset; |
fceaf24a | 374 | |
dc54a08c | 375 | data_buflen -= data_offset; |
4b8a8bc9 WY |
376 | |
377 | /* | |
378 | * Make sure we got a valid RNDIS message, now total_data_buflen | |
379 | * should be the data packet size plus the trailer padding size | |
380 | */ | |
dc54a08c | 381 | if (unlikely(data_buflen < rndis_pkt->data_len)) { |
3d541ac5 | 382 | netdev_err(dev->ndev, "rndis message buffer " |
4b8a8bc9 WY |
383 | "overflow detected (got %u, min %u)" |
384 | "...dropping this message!\n", | |
dc54a08c | 385 | data_buflen, rndis_pkt->data_len); |
10082f98 | 386 | return NVSP_STAT_FAIL; |
4b8a8bc9 WY |
387 | } |
388 | ||
dc54a08c | 389 | vlan = rndis_get_ppi(rndis_pkt, IEEE_8021Q_INFO); |
390 | ||
4b8a8bc9 WY |
391 | /* |
392 | * Remove the rndis trailer padding from rndis packet message | |
393 | * rndis_pkt->data_len tell us the real data length, we only copy | |
394 | * the data packet to the stack, without the rndis trailer padding | |
395 | */ | |
dc54a08c | 396 | data = (void *)((unsigned long)data + data_offset); |
e3d605ed | 397 | csum_info = rndis_get_ppi(rndis_pkt, TCPIP_CHKSUM_PKTINFO); |
dc54a08c | 398 | return netvsc_recv_callback(ndev, channel, |
399 | data, rndis_pkt->data_len, | |
400 | csum_info, vlan); | |
fceaf24a HJ |
401 | } |
402 | ||
dc54a08c | 403 | int rndis_filter_receive(struct net_device *ndev, |
404 | struct netvsc_device *net_dev, | |
405 | struct hv_device *dev, | |
406 | struct vmbus_channel *channel, | |
407 | void *data, u32 buflen) | |
fceaf24a | 408 | { |
3d541ac5 | 409 | struct net_device_context *net_device_ctx = netdev_priv(ndev); |
dc54a08c | 410 | struct rndis_device *rndis_dev = net_dev->extension; |
411 | struct rndis_message *rndis_msg = data; | |
8a62d716 | 412 | |
454f18a9 | 413 | /* Make sure the rndis device state is initialized */ |
dc54a08c | 414 | if (unlikely(!rndis_dev)) { |
415 | netif_err(net_device_ctx, rx_err, ndev, | |
416 | "got rndis message but no rndis device!\n"); | |
417 | return NVSP_STAT_FAIL; | |
fceaf24a HJ |
418 | } |
419 | ||
dc54a08c | 420 | if (unlikely(rndis_dev->state == RNDIS_DEV_UNINITIALIZED)) { |
421 | netif_err(net_device_ctx, rx_err, ndev, | |
422 | "got rndis message uninitialized\n"); | |
423 | return NVSP_STAT_FAIL; | |
fceaf24a HJ |
424 | } |
425 | ||
dc54a08c | 426 | if (netif_msg_rx_status(net_device_ctx)) |
3f300ff4 | 427 | dump_rndis_message(dev, rndis_msg); |
fceaf24a | 428 | |
ef31bef6 | 429 | switch (rndis_msg->ndis_msg_type) { |
51491167 | 430 | case RNDIS_MSG_PACKET: |
dc54a08c | 431 | return rndis_filter_receive_data(ndev, rndis_dev, rndis_msg, |
432 | channel, data, buflen); | |
51491167 LW |
433 | case RNDIS_MSG_INIT_C: |
434 | case RNDIS_MSG_QUERY_C: | |
435 | case RNDIS_MSG_SET_C: | |
0120ee0d | 436 | /* completion msgs */ |
ef31bef6 | 437 | rndis_filter_receive_response(rndis_dev, rndis_msg); |
fceaf24a HJ |
438 | break; |
439 | ||
51491167 | 440 | case RNDIS_MSG_INDICATE: |
0120ee0d | 441 | /* notification msgs */ |
3a494e71 | 442 | netvsc_linkstatus_callback(dev, rndis_msg); |
fceaf24a HJ |
443 | break; |
444 | default: | |
d9871158 | 445 | netdev_err(ndev, |
eb335bc4 | 446 | "unhandled rndis message (type %u len %u)\n", |
ef31bef6 HZ |
447 | rndis_msg->ndis_msg_type, |
448 | rndis_msg->msg_len); | |
fceaf24a HJ |
449 | break; |
450 | } | |
451 | ||
dc54a08c | 452 | return 0; |
fceaf24a HJ |
453 | } |
454 | ||
9c26aa0d | 455 | static int rndis_filter_query_device(struct rndis_device *dev, u32 oid, |
c2a4efdd | 456 | void *result, u32 *result_size) |
fceaf24a | 457 | { |
e681b954 | 458 | struct rndis_request *request; |
c2a4efdd | 459 | u32 inresult_size = *result_size; |
9f33d054 | 460 | struct rndis_query_request *query; |
c2a4efdd | 461 | struct rndis_query_complete *query_complete; |
0120ee0d | 462 | int ret = 0; |
fceaf24a | 463 | |
c2a4efdd | 464 | if (!result) |
8a62d716 | 465 | return -EINVAL; |
fceaf24a | 466 | |
c2a4efdd | 467 | *result_size = 0; |
51491167 | 468 | request = get_rndis_request(dev, RNDIS_MSG_QUERY, |
0120ee0d GKH |
469 | RNDIS_MESSAGE_SIZE(struct rndis_query_request)); |
470 | if (!request) { | |
de6e0580 | 471 | ret = -ENOMEM; |
1c627870 | 472 | goto cleanup; |
fceaf24a HJ |
473 | } |
474 | ||
454f18a9 | 475 | /* Setup the rndis query */ |
a388eb17 HZ |
476 | query = &request->request_msg.msg.query_req; |
477 | query->oid = oid; | |
478 | query->info_buf_offset = sizeof(struct rndis_query_request); | |
479 | query->info_buflen = 0; | |
480 | query->dev_vc_handle = 0; | |
fceaf24a | 481 | |
23312a3b | 482 | if (oid == OID_TCP_OFFLOAD_HARDWARE_CAPABILITIES) { |
483 | struct net_device_context *ndevctx = netdev_priv(dev->ndev); | |
3962981f | 484 | struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev); |
23312a3b | 485 | struct ndis_offload *hwcaps; |
486 | u32 nvsp_version = nvdev->nvsp_version; | |
487 | u8 ndis_rev; | |
488 | size_t size; | |
489 | ||
490 | if (nvsp_version >= NVSP_PROTOCOL_VERSION_5) { | |
491 | ndis_rev = NDIS_OFFLOAD_PARAMETERS_REVISION_3; | |
492 | size = NDIS_OFFLOAD_SIZE; | |
493 | } else if (nvsp_version >= NVSP_PROTOCOL_VERSION_4) { | |
494 | ndis_rev = NDIS_OFFLOAD_PARAMETERS_REVISION_2; | |
495 | size = NDIS_OFFLOAD_SIZE_6_1; | |
496 | } else { | |
497 | ndis_rev = NDIS_OFFLOAD_PARAMETERS_REVISION_1; | |
498 | size = NDIS_OFFLOAD_SIZE_6_0; | |
499 | } | |
500 | ||
501 | request->request_msg.msg_len += size; | |
502 | query->info_buflen = size; | |
503 | hwcaps = (struct ndis_offload *) | |
504 | ((unsigned long)query + query->info_buf_offset); | |
505 | ||
506 | hwcaps->header.type = NDIS_OBJECT_TYPE_OFFLOAD; | |
507 | hwcaps->header.revision = ndis_rev; | |
508 | hwcaps->header.size = size; | |
509 | ||
510 | } else if (oid == OID_GEN_RECEIVE_SCALE_CAPABILITIES) { | |
5b54dac8 HZ |
511 | struct ndis_recv_scale_cap *cap; |
512 | ||
513 | request->request_msg.msg_len += | |
514 | sizeof(struct ndis_recv_scale_cap); | |
515 | query->info_buflen = sizeof(struct ndis_recv_scale_cap); | |
516 | cap = (struct ndis_recv_scale_cap *)((unsigned long)query + | |
517 | query->info_buf_offset); | |
518 | cap->hdr.type = NDIS_OBJECT_TYPE_RSS_CAPABILITIES; | |
519 | cap->hdr.rev = NDIS_RECEIVE_SCALE_CAPABILITIES_REVISION_2; | |
520 | cap->hdr.size = sizeof(struct ndis_recv_scale_cap); | |
521 | } | |
522 | ||
9c26aa0d | 523 | ret = rndis_filter_send_request(dev, request); |
fceaf24a | 524 | if (ret != 0) |
1c627870 | 525 | goto cleanup; |
fceaf24a | 526 | |
5362855a | 527 | wait_for_completion(&request->wait_event); |
fceaf24a | 528 | |
454f18a9 | 529 | /* Copy the response back */ |
a388eb17 | 530 | query_complete = &request->response_msg.msg.query_complete; |
fceaf24a | 531 | |
a388eb17 | 532 | if (query_complete->info_buflen > inresult_size) { |
fceaf24a | 533 | ret = -1; |
1c627870 | 534 | goto cleanup; |
fceaf24a HJ |
535 | } |
536 | ||
c2a4efdd HZ |
537 | memcpy(result, |
538 | (void *)((unsigned long)query_complete + | |
a388eb17 HZ |
539 | query_complete->info_buf_offset), |
540 | query_complete->info_buflen); | |
fceaf24a | 541 | |
a388eb17 | 542 | *result_size = query_complete->info_buflen; |
fceaf24a | 543 | |
1c627870 | 544 | cleanup: |
fceaf24a | 545 | if (request) |
9c26aa0d | 546 | put_rndis_request(dev, request); |
fceaf24a HJ |
547 | |
548 | return ret; | |
549 | } | |
550 | ||
23312a3b | 551 | /* Get the hardware offload capabilities */ |
552 | static int | |
553 | rndis_query_hwcaps(struct rndis_device *dev, struct ndis_offload *caps) | |
554 | { | |
555 | u32 caps_len = sizeof(*caps); | |
556 | int ret; | |
557 | ||
558 | memset(caps, 0, sizeof(*caps)); | |
559 | ||
560 | ret = rndis_filter_query_device(dev, | |
561 | OID_TCP_OFFLOAD_HARDWARE_CAPABILITIES, | |
562 | caps, &caps_len); | |
563 | if (ret) | |
564 | return ret; | |
565 | ||
566 | if (caps->header.type != NDIS_OBJECT_TYPE_OFFLOAD) { | |
567 | netdev_warn(dev->ndev, "invalid NDIS objtype %#x\n", | |
568 | caps->header.type); | |
569 | return -EINVAL; | |
570 | } | |
571 | ||
572 | if (caps->header.revision < NDIS_OFFLOAD_PARAMETERS_REVISION_1) { | |
573 | netdev_warn(dev->ndev, "invalid NDIS objrev %x\n", | |
574 | caps->header.revision); | |
575 | return -EINVAL; | |
576 | } | |
577 | ||
578 | if (caps->header.size > caps_len || | |
579 | caps->header.size < NDIS_OFFLOAD_SIZE_6_0) { | |
580 | netdev_warn(dev->ndev, | |
581 | "invalid NDIS objsize %u, data size %u\n", | |
582 | caps->header.size, caps_len); | |
583 | return -EINVAL; | |
584 | } | |
585 | ||
586 | return 0; | |
587 | } | |
588 | ||
9c26aa0d | 589 | static int rndis_filter_query_device_mac(struct rndis_device *dev) |
fceaf24a | 590 | { |
9f8bd8ba | 591 | u32 size = ETH_ALEN; |
fceaf24a | 592 | |
9c26aa0d | 593 | return rndis_filter_query_device(dev, |
0120ee0d | 594 | RNDIS_OID_802_3_PERMANENT_ADDRESS, |
c2a4efdd | 595 | dev->hw_mac_adr, &size); |
fceaf24a HJ |
596 | } |
597 | ||
1ce09e89 HZ |
598 | #define NWADR_STR "NetworkAddress" |
599 | #define NWADR_STRLEN 14 | |
600 | ||
e834da9a | 601 | int rndis_filter_set_device_mac(struct net_device *ndev, char *mac) |
1ce09e89 | 602 | { |
2625466d | 603 | struct netvsc_device *nvdev = net_device_to_netvsc_device(ndev); |
1ce09e89 | 604 | struct rndis_device *rdev = nvdev->extension; |
1ce09e89 HZ |
605 | struct rndis_request *request; |
606 | struct rndis_set_request *set; | |
607 | struct rndis_config_parameter_info *cpi; | |
608 | wchar_t *cfg_nwadr, *cfg_mac; | |
609 | struct rndis_set_complete *set_complete; | |
610 | char macstr[2*ETH_ALEN+1]; | |
611 | u32 extlen = sizeof(struct rndis_config_parameter_info) + | |
612 | 2*NWADR_STRLEN + 4*ETH_ALEN; | |
999028cc | 613 | int ret; |
1ce09e89 HZ |
614 | |
615 | request = get_rndis_request(rdev, RNDIS_MSG_SET, | |
616 | RNDIS_MESSAGE_SIZE(struct rndis_set_request) + extlen); | |
617 | if (!request) | |
618 | return -ENOMEM; | |
619 | ||
620 | set = &request->request_msg.msg.set_req; | |
621 | set->oid = RNDIS_OID_GEN_RNDIS_CONFIG_PARAMETER; | |
622 | set->info_buflen = extlen; | |
623 | set->info_buf_offset = sizeof(struct rndis_set_request); | |
624 | set->dev_vc_handle = 0; | |
625 | ||
626 | cpi = (struct rndis_config_parameter_info *)((ulong)set + | |
627 | set->info_buf_offset); | |
628 | cpi->parameter_name_offset = | |
629 | sizeof(struct rndis_config_parameter_info); | |
630 | /* Multiply by 2 because host needs 2 bytes (utf16) for each char */ | |
631 | cpi->parameter_name_length = 2*NWADR_STRLEN; | |
632 | cpi->parameter_type = RNDIS_CONFIG_PARAM_TYPE_STRING; | |
633 | cpi->parameter_value_offset = | |
634 | cpi->parameter_name_offset + cpi->parameter_name_length; | |
635 | /* Multiply by 4 because each MAC byte displayed as 2 utf16 chars */ | |
636 | cpi->parameter_value_length = 4*ETH_ALEN; | |
637 | ||
638 | cfg_nwadr = (wchar_t *)((ulong)cpi + cpi->parameter_name_offset); | |
639 | cfg_mac = (wchar_t *)((ulong)cpi + cpi->parameter_value_offset); | |
640 | ret = utf8s_to_utf16s(NWADR_STR, NWADR_STRLEN, UTF16_HOST_ENDIAN, | |
641 | cfg_nwadr, NWADR_STRLEN); | |
642 | if (ret < 0) | |
643 | goto cleanup; | |
644 | snprintf(macstr, 2*ETH_ALEN+1, "%pm", mac); | |
645 | ret = utf8s_to_utf16s(macstr, 2*ETH_ALEN, UTF16_HOST_ENDIAN, | |
646 | cfg_mac, 2*ETH_ALEN); | |
647 | if (ret < 0) | |
648 | goto cleanup; | |
649 | ||
650 | ret = rndis_filter_send_request(rdev, request); | |
651 | if (ret != 0) | |
652 | goto cleanup; | |
653 | ||
5362855a VK |
654 | wait_for_completion(&request->wait_event); |
655 | ||
656 | set_complete = &request->response_msg.msg.set_complete; | |
657 | if (set_complete->status != RNDIS_STATUS_SUCCESS) { | |
658 | netdev_err(ndev, "Fail to set MAC on host side:0x%x\n", | |
659 | set_complete->status); | |
660 | ret = -EINVAL; | |
1ce09e89 HZ |
661 | } |
662 | ||
663 | cleanup: | |
664 | put_rndis_request(rdev, request); | |
665 | return ret; | |
666 | } | |
667 | ||
da19fcd0 | 668 | static int |
426d9541 | 669 | rndis_filter_set_offload_params(struct net_device *ndev, |
9749fed5 | 670 | struct netvsc_device *nvdev, |
4a0e70ae KS |
671 | struct ndis_offload_params *req_offloads) |
672 | { | |
4a0e70ae | 673 | struct rndis_device *rdev = nvdev->extension; |
4a0e70ae KS |
674 | struct rndis_request *request; |
675 | struct rndis_set_request *set; | |
676 | struct ndis_offload_params *offload_params; | |
677 | struct rndis_set_complete *set_complete; | |
678 | u32 extlen = sizeof(struct ndis_offload_params); | |
999028cc | 679 | int ret; |
af9893a3 KS |
680 | u32 vsp_version = nvdev->nvsp_version; |
681 | ||
682 | if (vsp_version <= NVSP_PROTOCOL_VERSION_4) { | |
683 | extlen = VERSION_4_OFFLOAD_SIZE; | |
684 | /* On NVSP_PROTOCOL_VERSION_4 and below, we do not support | |
685 | * UDP checksum offload. | |
686 | */ | |
687 | req_offloads->udp_ip_v4_csum = 0; | |
688 | req_offloads->udp_ip_v6_csum = 0; | |
689 | } | |
4a0e70ae KS |
690 | |
691 | request = get_rndis_request(rdev, RNDIS_MSG_SET, | |
692 | RNDIS_MESSAGE_SIZE(struct rndis_set_request) + extlen); | |
693 | if (!request) | |
694 | return -ENOMEM; | |
695 | ||
696 | set = &request->request_msg.msg.set_req; | |
697 | set->oid = OID_TCP_OFFLOAD_PARAMETERS; | |
698 | set->info_buflen = extlen; | |
699 | set->info_buf_offset = sizeof(struct rndis_set_request); | |
700 | set->dev_vc_handle = 0; | |
701 | ||
702 | offload_params = (struct ndis_offload_params *)((ulong)set + | |
703 | set->info_buf_offset); | |
704 | *offload_params = *req_offloads; | |
705 | offload_params->header.type = NDIS_OBJECT_TYPE_DEFAULT; | |
706 | offload_params->header.revision = NDIS_OFFLOAD_PARAMETERS_REVISION_3; | |
707 | offload_params->header.size = extlen; | |
708 | ||
709 | ret = rndis_filter_send_request(rdev, request); | |
710 | if (ret != 0) | |
711 | goto cleanup; | |
712 | ||
5362855a VK |
713 | wait_for_completion(&request->wait_event); |
714 | set_complete = &request->response_msg.msg.set_complete; | |
715 | if (set_complete->status != RNDIS_STATUS_SUCCESS) { | |
716 | netdev_err(ndev, "Fail to set offload on host side:0x%x\n", | |
717 | set_complete->status); | |
718 | ret = -EINVAL; | |
4a0e70ae KS |
719 | } |
720 | ||
721 | cleanup: | |
722 | put_rndis_request(rdev, request); | |
723 | return ret; | |
724 | } | |
1ce09e89 | 725 | |
962f3fee | 726 | int rndis_filter_set_rss_param(struct rndis_device *rdev, |
727 | const u8 *rss_key, int num_queue) | |
5b54dac8 | 728 | { |
3d541ac5 | 729 | struct net_device *ndev = rdev->ndev; |
5b54dac8 HZ |
730 | struct rndis_request *request; |
731 | struct rndis_set_request *set; | |
732 | struct rndis_set_complete *set_complete; | |
733 | u32 extlen = sizeof(struct ndis_recv_scale_param) + | |
962f3fee | 734 | 4 * ITAB_NUM + NETVSC_HASH_KEYLEN; |
5b54dac8 HZ |
735 | struct ndis_recv_scale_param *rssp; |
736 | u32 *itab; | |
737 | u8 *keyp; | |
999028cc | 738 | int i, ret; |
5b54dac8 HZ |
739 | |
740 | request = get_rndis_request( | |
741 | rdev, RNDIS_MSG_SET, | |
742 | RNDIS_MESSAGE_SIZE(struct rndis_set_request) + extlen); | |
743 | if (!request) | |
744 | return -ENOMEM; | |
745 | ||
746 | set = &request->request_msg.msg.set_req; | |
747 | set->oid = OID_GEN_RECEIVE_SCALE_PARAMETERS; | |
748 | set->info_buflen = extlen; | |
749 | set->info_buf_offset = sizeof(struct rndis_set_request); | |
750 | set->dev_vc_handle = 0; | |
751 | ||
752 | rssp = (struct ndis_recv_scale_param *)(set + 1); | |
753 | rssp->hdr.type = NDIS_OBJECT_TYPE_RSS_PARAMETERS; | |
754 | rssp->hdr.rev = NDIS_RECEIVE_SCALE_PARAMETERS_REVISION_2; | |
755 | rssp->hdr.size = sizeof(struct ndis_recv_scale_param); | |
756 | rssp->flag = 0; | |
757 | rssp->hashinfo = NDIS_HASH_FUNC_TOEPLITZ | NDIS_HASH_IPV4 | | |
4c87454a HZ |
758 | NDIS_HASH_TCP_IPV4 | NDIS_HASH_IPV6 | |
759 | NDIS_HASH_TCP_IPV6; | |
5b54dac8 HZ |
760 | rssp->indirect_tabsize = 4*ITAB_NUM; |
761 | rssp->indirect_taboffset = sizeof(struct ndis_recv_scale_param); | |
962f3fee | 762 | rssp->hashkey_size = NETVSC_HASH_KEYLEN; |
5b54dac8 HZ |
763 | rssp->kashkey_offset = rssp->indirect_taboffset + |
764 | rssp->indirect_tabsize; | |
765 | ||
766 | /* Set indirection table entries */ | |
767 | itab = (u32 *)(rssp + 1); | |
768 | for (i = 0; i < ITAB_NUM; i++) | |
ff4a4419 | 769 | itab[i] = rdev->ind_table[i]; |
5b54dac8 HZ |
770 | |
771 | /* Set hask key values */ | |
772 | keyp = (u8 *)((unsigned long)rssp + rssp->kashkey_offset); | |
962f3fee | 773 | memcpy(keyp, rss_key, NETVSC_HASH_KEYLEN); |
5b54dac8 | 774 | |
5b54dac8 HZ |
775 | ret = rndis_filter_send_request(rdev, request); |
776 | if (ret != 0) | |
777 | goto cleanup; | |
778 | ||
5362855a VK |
779 | wait_for_completion(&request->wait_event); |
780 | set_complete = &request->response_msg.msg.set_complete; | |
962f3fee | 781 | if (set_complete->status == RNDIS_STATUS_SUCCESS) |
782 | memcpy(rdev->rss_key, rss_key, NETVSC_HASH_KEYLEN); | |
783 | else { | |
5362855a VK |
784 | netdev_err(ndev, "Fail to set RSS parameters:0x%x\n", |
785 | set_complete->status); | |
786 | ret = -EINVAL; | |
5b54dac8 HZ |
787 | } |
788 | ||
789 | cleanup: | |
790 | put_rndis_request(rdev, request); | |
791 | return ret; | |
792 | } | |
793 | ||
9c26aa0d | 794 | static int rndis_filter_query_device_link_status(struct rndis_device *dev) |
fceaf24a | 795 | { |
0120ee0d | 796 | u32 size = sizeof(u32); |
6f27457b S |
797 | u32 link_status; |
798 | int ret; | |
fceaf24a | 799 | |
6f27457b | 800 | ret = rndis_filter_query_device(dev, |
0120ee0d | 801 | RNDIS_OID_GEN_MEDIA_CONNECT_STATUS, |
6f27457b | 802 | &link_status, &size); |
6f27457b S |
803 | |
804 | return ret; | |
fceaf24a HJ |
805 | } |
806 | ||
b37879e6 HZ |
807 | static int rndis_filter_query_link_speed(struct rndis_device *dev) |
808 | { | |
809 | u32 size = sizeof(u32); | |
810 | u32 link_speed; | |
811 | struct net_device_context *ndc; | |
812 | int ret; | |
813 | ||
814 | ret = rndis_filter_query_device(dev, RNDIS_OID_GEN_LINK_SPEED, | |
815 | &link_speed, &size); | |
816 | ||
817 | if (!ret) { | |
818 | ndc = netdev_priv(dev->ndev); | |
819 | ||
820 | /* The link speed reported from host is in 100bps unit, so | |
821 | * we convert it to Mbps here. | |
822 | */ | |
823 | ndc->speed = link_speed / 10000; | |
824 | } | |
825 | ||
826 | return ret; | |
827 | } | |
828 | ||
4f19c0d8 | 829 | static int rndis_filter_set_packet_filter(struct rndis_device *dev, |
830 | u32 new_filter) | |
fceaf24a | 831 | { |
e681b954 | 832 | struct rndis_request *request; |
9f33d054 | 833 | struct rndis_set_request *set; |
999028cc | 834 | int ret; |
fceaf24a | 835 | |
51491167 | 836 | request = get_rndis_request(dev, RNDIS_MSG_SET, |
0120ee0d GKH |
837 | RNDIS_MESSAGE_SIZE(struct rndis_set_request) + |
838 | sizeof(u32)); | |
ce12b810 | 839 | if (!request) |
840 | return -ENOMEM; | |
841 | ||
fceaf24a | 842 | |
454f18a9 | 843 | /* Setup the rndis set */ |
a388eb17 HZ |
844 | set = &request->request_msg.msg.set_req; |
845 | set->oid = RNDIS_OID_GEN_CURRENT_PACKET_FILTER; | |
846 | set->info_buflen = sizeof(u32); | |
847 | set->info_buf_offset = sizeof(struct rndis_set_request); | |
fceaf24a | 848 | |
0120ee0d | 849 | memcpy((void *)(unsigned long)set + sizeof(struct rndis_set_request), |
c2a4efdd | 850 | &new_filter, sizeof(u32)); |
fceaf24a | 851 | |
9c26aa0d | 852 | ret = rndis_filter_send_request(dev, request); |
ce12b810 | 853 | if (ret == 0) |
854 | wait_for_completion(&request->wait_event); | |
fceaf24a | 855 | |
ce12b810 | 856 | put_rndis_request(dev, request); |
98d79690 | 857 | |
fceaf24a HJ |
858 | return ret; |
859 | } | |
860 | ||
4f19c0d8 | 861 | static void rndis_set_multicast(struct work_struct *w) |
862 | { | |
863 | struct rndis_device *rdev | |
864 | = container_of(w, struct rndis_device, mcast_work); | |
865 | ||
866 | if (rdev->ndev->flags & IFF_PROMISC) | |
867 | rndis_filter_set_packet_filter(rdev, | |
868 | NDIS_PACKET_TYPE_PROMISCUOUS); | |
869 | else | |
870 | rndis_filter_set_packet_filter(rdev, | |
871 | NDIS_PACKET_TYPE_BROADCAST | | |
872 | NDIS_PACKET_TYPE_ALL_MULTICAST | | |
873 | NDIS_PACKET_TYPE_DIRECTED); | |
874 | } | |
875 | ||
876 | void rndis_filter_update(struct netvsc_device *nvdev) | |
877 | { | |
878 | struct rndis_device *rdev = nvdev->extension; | |
879 | ||
880 | schedule_work(&rdev->mcast_work); | |
881 | } | |
882 | ||
9c26aa0d | 883 | static int rndis_filter_init_device(struct rndis_device *dev) |
fceaf24a | 884 | { |
e681b954 | 885 | struct rndis_request *request; |
9f33d054 | 886 | struct rndis_initialize_request *init; |
c2a4efdd | 887 | struct rndis_initialize_complete *init_complete; |
4d643114 | 888 | u32 status; |
999028cc | 889 | int ret; |
2625466d | 890 | struct netvsc_device *nvdev = net_device_to_netvsc_device(dev->ndev); |
fceaf24a | 891 | |
51491167 | 892 | request = get_rndis_request(dev, RNDIS_MSG_INIT, |
0120ee0d GKH |
893 | RNDIS_MESSAGE_SIZE(struct rndis_initialize_request)); |
894 | if (!request) { | |
bc49b926 | 895 | ret = -ENOMEM; |
1c627870 | 896 | goto cleanup; |
fceaf24a HJ |
897 | } |
898 | ||
454f18a9 | 899 | /* Setup the rndis set */ |
a388eb17 HZ |
900 | init = &request->request_msg.msg.init_req; |
901 | init->major_ver = RNDIS_MAJOR_VERSION; | |
902 | init->minor_ver = RNDIS_MINOR_VERSION; | |
fb1d074e | 903 | init->max_xfer_size = 0x4000; |
fceaf24a | 904 | |
c2a4efdd | 905 | dev->state = RNDIS_DEV_INITIALIZING; |
fceaf24a | 906 | |
9c26aa0d | 907 | ret = rndis_filter_send_request(dev, request); |
0120ee0d | 908 | if (ret != 0) { |
c2a4efdd | 909 | dev->state = RNDIS_DEV_UNINITIALIZED; |
1c627870 | 910 | goto cleanup; |
fceaf24a HJ |
911 | } |
912 | ||
5362855a | 913 | wait_for_completion(&request->wait_event); |
fceaf24a | 914 | |
a388eb17 HZ |
915 | init_complete = &request->response_msg.msg.init_complete; |
916 | status = init_complete->status; | |
0120ee0d | 917 | if (status == RNDIS_STATUS_SUCCESS) { |
c2a4efdd | 918 | dev->state = RNDIS_DEV_INITIALIZED; |
7c3877f2 HZ |
919 | nvdev->max_pkt = init_complete->max_pkt_per_msg; |
920 | nvdev->pkt_align = 1 << init_complete->pkt_alignment_factor; | |
fceaf24a | 921 | ret = 0; |
0120ee0d | 922 | } else { |
c2a4efdd | 923 | dev->state = RNDIS_DEV_UNINITIALIZED; |
bc49b926 | 924 | ret = -EINVAL; |
fceaf24a HJ |
925 | } |
926 | ||
1c627870 | 927 | cleanup: |
fceaf24a | 928 | if (request) |
9c26aa0d | 929 | put_rndis_request(dev, request); |
fceaf24a HJ |
930 | |
931 | return ret; | |
932 | } | |
933 | ||
46b4f7f5 | 934 | static bool netvsc_device_idle(const struct netvsc_device *nvdev) |
935 | { | |
936 | int i; | |
937 | ||
938 | if (atomic_read(&nvdev->num_outstanding_recvs) > 0) | |
939 | return false; | |
940 | ||
941 | for (i = 0; i < nvdev->num_chn; i++) { | |
942 | const struct netvsc_channel *nvchan = &nvdev->chan_table[i]; | |
943 | ||
944 | if (atomic_read(&nvchan->queue_sends) > 0) | |
945 | return false; | |
946 | } | |
947 | ||
948 | return true; | |
949 | } | |
950 | ||
9c26aa0d | 951 | static void rndis_filter_halt_device(struct rndis_device *dev) |
fceaf24a | 952 | { |
e681b954 | 953 | struct rndis_request *request; |
9f33d054 | 954 | struct rndis_halt_request *halt; |
3d541ac5 | 955 | struct net_device_context *net_device_ctx = netdev_priv(dev->ndev); |
3962981f | 956 | struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev); |
fceaf24a | 957 | |
454f18a9 | 958 | /* Attempt to do a rndis device halt */ |
51491167 | 959 | request = get_rndis_request(dev, RNDIS_MSG_HALT, |
0120ee0d | 960 | RNDIS_MESSAGE_SIZE(struct rndis_halt_request)); |
fceaf24a | 961 | if (!request) |
1c627870 | 962 | goto cleanup; |
fceaf24a | 963 | |
454f18a9 | 964 | /* Setup the rndis set */ |
a388eb17 HZ |
965 | halt = &request->request_msg.msg.halt_req; |
966 | halt->req_id = atomic_inc_return(&dev->new_req_id); | |
fceaf24a | 967 | |
454f18a9 | 968 | /* Ignore return since this msg is optional. */ |
9c26aa0d | 969 | rndis_filter_send_request(dev, request); |
fceaf24a | 970 | |
c2a4efdd | 971 | dev->state = RNDIS_DEV_UNINITIALIZED; |
fceaf24a | 972 | |
1c627870 | 973 | cleanup: |
ae9e63bb | 974 | nvdev->destroy = true; |
00ecfb3b | 975 | |
976 | /* Force flag to be ordered before waiting */ | |
977 | wmb(); | |
ae9e63bb HZ |
978 | |
979 | /* Wait for all send completions */ | |
46b4f7f5 | 980 | wait_event(nvdev->wait_drain, netvsc_device_idle(nvdev)); |
ae9e63bb | 981 | |
fceaf24a | 982 | if (request) |
9c26aa0d | 983 | put_rndis_request(dev, request); |
fceaf24a HJ |
984 | } |
985 | ||
9c26aa0d | 986 | static int rndis_filter_open_device(struct rndis_device *dev) |
fceaf24a | 987 | { |
0120ee0d | 988 | int ret; |
fceaf24a | 989 | |
c2a4efdd | 990 | if (dev->state != RNDIS_DEV_INITIALIZED) |
fceaf24a HJ |
991 | return 0; |
992 | ||
9c26aa0d | 993 | ret = rndis_filter_set_packet_filter(dev, |
0120ee0d | 994 | NDIS_PACKET_TYPE_BROADCAST | |
95beae90 | 995 | NDIS_PACKET_TYPE_ALL_MULTICAST | |
0120ee0d | 996 | NDIS_PACKET_TYPE_DIRECTED); |
fceaf24a | 997 | if (ret == 0) |
c2a4efdd | 998 | dev->state = RNDIS_DEV_DATAINITIALIZED; |
fceaf24a | 999 | |
fceaf24a HJ |
1000 | return ret; |
1001 | } | |
1002 | ||
9c26aa0d | 1003 | static int rndis_filter_close_device(struct rndis_device *dev) |
fceaf24a HJ |
1004 | { |
1005 | int ret; | |
1006 | ||
c2a4efdd | 1007 | if (dev->state != RNDIS_DEV_DATAINITIALIZED) |
fceaf24a HJ |
1008 | return 0; |
1009 | ||
4f19c0d8 | 1010 | /* Make sure rndis_set_multicast doesn't re-enable filter! */ |
1011 | cancel_work_sync(&dev->mcast_work); | |
1012 | ||
9c26aa0d | 1013 | ret = rndis_filter_set_packet_filter(dev, 0); |
c3582a2c HZ |
1014 | if (ret == -ENODEV) |
1015 | ret = 0; | |
1016 | ||
fceaf24a | 1017 | if (ret == 0) |
c2a4efdd | 1018 | dev->state = RNDIS_DEV_INITIALIZED; |
fceaf24a | 1019 | |
fceaf24a HJ |
1020 | return ret; |
1021 | } | |
1022 | ||
5b54dac8 HZ |
1023 | static void netvsc_sc_open(struct vmbus_channel *new_sc) |
1024 | { | |
3d541ac5 VK |
1025 | struct net_device *ndev = |
1026 | hv_get_drvdata(new_sc->primary_channel->device_obj); | |
2625466d | 1027 | struct netvsc_device *nvscdev = net_device_to_netvsc_device(ndev); |
5b54dac8 | 1028 | u16 chn_index = new_sc->offermsg.offer.sub_channel_index; |
6de38af6 | 1029 | struct netvsc_channel *nvchan; |
6de38af6 | 1030 | int ret; |
5b54dac8 | 1031 | |
5b54dac8 HZ |
1032 | if (chn_index >= nvscdev->num_chn) |
1033 | return; | |
1034 | ||
6de38af6 | 1035 | nvchan = nvscdev->chan_table + chn_index; |
1036 | nvchan->mrc.buf | |
b8b835a8 | 1037 | = vzalloc(NETVSC_RECVSLOT_MAX * sizeof(struct recv_comp_data)); |
c0b558e5 | 1038 | |
6de38af6 | 1039 | if (!nvchan->mrc.buf) |
1040 | return; | |
1041 | ||
b1dd90ce S |
1042 | /* Because the device uses NAPI, all the interrupt batching and |
1043 | * control is done via Net softirq, not the channel handling | |
1044 | */ | |
1045 | set_channel_read_mode(new_sc, HV_CALL_ISR); | |
1046 | ||
bffb1842 S |
1047 | /* Set the channel before opening.*/ |
1048 | nvchan->channel = new_sc; | |
76bb5db5 | 1049 | netif_napi_add(ndev, &nvchan->napi, |
1050 | netvsc_poll, NAPI_POLL_WEIGHT); | |
bffb1842 | 1051 | |
5b54dac8 HZ |
1052 | ret = vmbus_open(new_sc, nvscdev->ring_size * PAGE_SIZE, |
1053 | nvscdev->ring_size * PAGE_SIZE, NULL, 0, | |
6de38af6 | 1054 | netvsc_channel_cb, nvchan); |
76bb5db5 | 1055 | if (ret == 0) |
1056 | napi_enable(&nvchan->napi); | |
1057 | else | |
2be0f264 | 1058 | netif_napi_del(&nvchan->napi); |
15a863bf | 1059 | |
43c7bd1f | 1060 | if (refcount_dec_and_test(&nvscdev->sc_offered)) |
3f735131 | 1061 | complete(&nvscdev->channel_init_wait); |
5b54dac8 HZ |
1062 | } |
1063 | ||
9749fed5 | 1064 | struct netvsc_device *rndis_filter_device_add(struct hv_device *dev, |
1065 | struct netvsc_device_info *device_info) | |
fceaf24a | 1066 | { |
3d541ac5 VK |
1067 | struct net_device *net = hv_get_drvdata(dev); |
1068 | struct net_device_context *net_device_ctx = netdev_priv(net); | |
86c921af | 1069 | struct netvsc_device *net_device; |
b13cc345 | 1070 | struct rndis_device *rndis_device; |
23312a3b | 1071 | struct ndis_offload hwcaps; |
4a0e70ae | 1072 | struct ndis_offload_params offloads; |
5b54dac8 | 1073 | struct nvsp_message *init_packet; |
5b54dac8 HZ |
1074 | struct ndis_recv_scale_cap rsscap; |
1075 | u32 rsscap_size = sizeof(struct ndis_recv_scale_cap); | |
23312a3b | 1076 | unsigned int gso_max_size = GSO_MAX_SIZE; |
43c7bd1f | 1077 | u32 mtu, size, num_rss_qs; |
e01ec219 KS |
1078 | const struct cpumask *node_cpu_mask; |
1079 | u32 num_possible_rss_qs; | |
ff4a4419 | 1080 | int i, ret; |
fceaf24a | 1081 | |
b13cc345 S |
1082 | rndis_device = get_rndis_device(); |
1083 | if (!rndis_device) | |
9749fed5 | 1084 | return ERR_PTR(-ENODEV); |
fceaf24a | 1085 | |
0120ee0d GKH |
1086 | /* |
1087 | * Let the inner driver handle this first to create the netvsc channel | |
1088 | * NOTE! Once the channel is created, we may get a receive callback | |
1089 | * (RndisFilterOnReceive()) before this call is completed | |
1090 | */ | |
9749fed5 | 1091 | net_device = netvsc_device_add(dev, device_info); |
1092 | if (IS_ERR(net_device)) { | |
b13cc345 | 1093 | kfree(rndis_device); |
9749fed5 | 1094 | return net_device; |
fceaf24a HJ |
1095 | } |
1096 | ||
454f18a9 | 1097 | /* Initialize the rndis device */ |
59995370 | 1098 | net_device->max_chn = 1; |
5b54dac8 | 1099 | net_device->num_chn = 1; |
fceaf24a | 1100 | |
43c7bd1f | 1101 | refcount_set(&net_device->sc_offered, 0); |
b3e6b82a | 1102 | |
b13cc345 | 1103 | net_device->extension = rndis_device; |
3d541ac5 | 1104 | rndis_device->ndev = net; |
fceaf24a | 1105 | |
454f18a9 | 1106 | /* Send the rndis initialization message */ |
b13cc345 | 1107 | ret = rndis_filter_init_device(rndis_device); |
9749fed5 | 1108 | if (ret != 0) |
1109 | goto err_dev_remv; | |
fceaf24a | 1110 | |
4d3c9d37 HZ |
1111 | /* Get the MTU from the host */ |
1112 | size = sizeof(u32); | |
1113 | ret = rndis_filter_query_device(rndis_device, | |
1114 | RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE, | |
1115 | &mtu, &size); | |
0a1275ca VK |
1116 | if (ret == 0 && size == sizeof(u32) && mtu < net->mtu) |
1117 | net->mtu = mtu; | |
4d3c9d37 | 1118 | |
454f18a9 | 1119 | /* Get the mac address */ |
b13cc345 | 1120 | ret = rndis_filter_query_device_mac(rndis_device); |
9749fed5 | 1121 | if (ret != 0) |
1122 | goto err_dev_remv; | |
fceaf24a | 1123 | |
3c4debad | 1124 | memcpy(device_info->mac_adr, rndis_device->hw_mac_adr, ETH_ALEN); |
fceaf24a | 1125 | |
23312a3b | 1126 | /* Find HW offload capabilities */ |
1127 | ret = rndis_query_hwcaps(rndis_device, &hwcaps); | |
9749fed5 | 1128 | if (ret != 0) |
1129 | goto err_dev_remv; | |
23312a3b | 1130 | |
1131 | /* A value of zero means "no change"; now turn on what we want. */ | |
4a0e70ae | 1132 | memset(&offloads, 0, sizeof(struct ndis_offload_params)); |
23312a3b | 1133 | |
1134 | /* Linux does not care about IP checksum, always does in kernel */ | |
1135 | offloads.ip_v4_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_DISABLED; | |
1136 | ||
1137 | /* Compute tx offload settings based on hw capabilities */ | |
1138 | net->hw_features = NETIF_F_RXCSUM; | |
1139 | ||
1140 | if ((hwcaps.csum.ip4_txcsum & NDIS_TXCSUM_ALL_TCP4) == NDIS_TXCSUM_ALL_TCP4) { | |
1141 | /* Can checksum TCP */ | |
1142 | net->hw_features |= NETIF_F_IP_CSUM; | |
1143 | net_device_ctx->tx_checksum_mask |= TRANSPORT_INFO_IPV4_TCP; | |
1144 | ||
1145 | offloads.tcp_ip_v4_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED; | |
1146 | ||
1147 | if (hwcaps.lsov2.ip4_encap & NDIS_OFFLOAD_ENCAP_8023) { | |
1148 | offloads.lso_v2_ipv4 = NDIS_OFFLOAD_PARAMETERS_LSOV2_ENABLED; | |
1149 | net->hw_features |= NETIF_F_TSO; | |
1150 | ||
1151 | if (hwcaps.lsov2.ip4_maxsz < gso_max_size) | |
1152 | gso_max_size = hwcaps.lsov2.ip4_maxsz; | |
1153 | } | |
1154 | ||
1155 | if (hwcaps.csum.ip4_txcsum & NDIS_TXCSUM_CAP_UDP4) { | |
1156 | offloads.udp_ip_v4_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED; | |
1157 | net_device_ctx->tx_checksum_mask |= TRANSPORT_INFO_IPV4_UDP; | |
1158 | } | |
1159 | } | |
1160 | ||
1161 | if ((hwcaps.csum.ip6_txcsum & NDIS_TXCSUM_ALL_TCP6) == NDIS_TXCSUM_ALL_TCP6) { | |
1162 | net->hw_features |= NETIF_F_IPV6_CSUM; | |
1163 | ||
1164 | offloads.tcp_ip_v6_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED; | |
1165 | net_device_ctx->tx_checksum_mask |= TRANSPORT_INFO_IPV6_TCP; | |
1166 | ||
1167 | if ((hwcaps.lsov2.ip6_encap & NDIS_OFFLOAD_ENCAP_8023) && | |
1168 | (hwcaps.lsov2.ip6_opts & NDIS_LSOV2_CAP_IP6) == NDIS_LSOV2_CAP_IP6) { | |
1169 | offloads.lso_v2_ipv6 = NDIS_OFFLOAD_PARAMETERS_LSOV2_ENABLED; | |
1170 | net->hw_features |= NETIF_F_TSO6; | |
1171 | ||
1172 | if (hwcaps.lsov2.ip6_maxsz < gso_max_size) | |
1173 | gso_max_size = hwcaps.lsov2.ip6_maxsz; | |
1174 | } | |
1175 | ||
1176 | if (hwcaps.csum.ip6_txcsum & NDIS_TXCSUM_CAP_UDP6) { | |
1177 | offloads.udp_ip_v6_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED; | |
1178 | net_device_ctx->tx_checksum_mask |= TRANSPORT_INFO_IPV6_UDP; | |
1179 | } | |
1180 | } | |
1181 | ||
1182 | netif_set_gso_max_size(net, gso_max_size); | |
4a0e70ae | 1183 | |
9749fed5 | 1184 | ret = rndis_filter_set_offload_params(net, net_device, &offloads); |
4a0e70ae KS |
1185 | if (ret) |
1186 | goto err_dev_remv; | |
1187 | ||
b13cc345 | 1188 | rndis_filter_query_device_link_status(rndis_device); |
fceaf24a | 1189 | |
93ba2222 VK |
1190 | netdev_dbg(net, "Device MAC %pM link state %s\n", |
1191 | rndis_device->hw_mac_adr, | |
dedb459e | 1192 | rndis_device->link_state ? "down" : "up"); |
fceaf24a | 1193 | |
5b54dac8 | 1194 | if (net_device->nvsp_version < NVSP_PROTOCOL_VERSION_5) |
9749fed5 | 1195 | return net_device; |
5b54dac8 | 1196 | |
b37879e6 HZ |
1197 | rndis_filter_query_link_speed(rndis_device); |
1198 | ||
5b54dac8 HZ |
1199 | /* vRSS setup */ |
1200 | memset(&rsscap, 0, rsscap_size); | |
1201 | ret = rndis_filter_query_device(rndis_device, | |
1202 | OID_GEN_RECEIVE_SCALE_CAPABILITIES, | |
1203 | &rsscap, &rsscap_size); | |
1204 | if (ret || rsscap.num_recv_que < 2) | |
1205 | goto out; | |
1206 | ||
e01ec219 KS |
1207 | /* |
1208 | * We will limit the VRSS channels to the number CPUs in the NUMA node | |
1209 | * the primary channel is currently bound to. | |
3071ada4 | 1210 | * |
1211 | * This also guarantees that num_possible_rss_qs <= num_online_cpus | |
e01ec219 KS |
1212 | */ |
1213 | node_cpu_mask = cpumask_of_node(cpu_to_node(dev->channel->target_cpu)); | |
3071ada4 | 1214 | num_possible_rss_qs = min_t(u32, cpumask_weight(node_cpu_mask), |
1215 | rsscap.num_recv_que); | |
8ebdcc52 | 1216 | |
3071ada4 | 1217 | net_device->max_chn = min_t(u32, VRSS_CHANNEL_MAX, num_possible_rss_qs); |
e01ec219 | 1218 | |
3071ada4 | 1219 | /* We will use the given number of channels if available. */ |
1220 | net_device->num_chn = min(net_device->max_chn, device_info->num_chn); | |
ff4a4419 | 1221 | |
1222 | for (i = 0; i < ITAB_NUM; i++) | |
1223 | rndis_device->ind_table[i] = ethtool_rxfh_indir_default(i, | |
1224 | net_device->num_chn); | |
1225 | ||
3071ada4 | 1226 | num_rss_qs = net_device->num_chn - 1; |
1227 | if (num_rss_qs == 0) | |
9749fed5 | 1228 | return net_device; |
5b54dac8 | 1229 | |
43c7bd1f | 1230 | refcount_set(&net_device->sc_offered, num_rss_qs); |
5b54dac8 HZ |
1231 | vmbus_set_sc_create_callback(dev->channel, netvsc_sc_open); |
1232 | ||
1233 | init_packet = &net_device->channel_init_pkt; | |
1234 | memset(init_packet, 0, sizeof(struct nvsp_message)); | |
1235 | init_packet->hdr.msg_type = NVSP_MSG5_TYPE_SUBCHANNEL; | |
1236 | init_packet->msg.v5_msg.subchn_req.op = NVSP_SUBCHANNEL_ALLOCATE; | |
1237 | init_packet->msg.v5_msg.subchn_req.num_subchannels = | |
1238 | net_device->num_chn - 1; | |
1239 | ret = vmbus_sendpacket(dev->channel, init_packet, | |
1240 | sizeof(struct nvsp_message), | |
1241 | (unsigned long)init_packet, | |
1242 | VM_PKT_DATA_INBAND, | |
1243 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
1244 | if (ret) | |
1245 | goto out; | |
5362855a | 1246 | |
43c7bd1f | 1247 | if (init_packet->msg.v5_msg.subchn_comp.status != NVSP_STAT_SUCCESS) { |
5b54dac8 HZ |
1248 | ret = -ENODEV; |
1249 | goto out; | |
1250 | } | |
43c7bd1f | 1251 | wait_for_completion(&net_device->channel_init_wait); |
1252 | ||
5b54dac8 HZ |
1253 | net_device->num_chn = 1 + |
1254 | init_packet->msg.v5_msg.subchn_comp.num_subchannels; | |
1255 | ||
43c7bd1f | 1256 | /* ignore failues from setting rss parameters, still have channels */ |
1257 | rndis_filter_set_rss_param(rndis_device, netvsc_hash_key, | |
1258 | net_device->num_chn); | |
5b54dac8 | 1259 | out: |
59995370 AS |
1260 | if (ret) { |
1261 | net_device->max_chn = 1; | |
5b54dac8 | 1262 | net_device->num_chn = 1; |
59995370 | 1263 | } |
b3e6b82a | 1264 | |
9749fed5 | 1265 | return net_device; |
4a0e70ae KS |
1266 | |
1267 | err_dev_remv: | |
2289f0aa | 1268 | rndis_filter_device_remove(dev, net_device); |
9749fed5 | 1269 | return ERR_PTR(ret); |
fceaf24a HJ |
1270 | } |
1271 | ||
2289f0aa | 1272 | void rndis_filter_device_remove(struct hv_device *dev, |
1273 | struct netvsc_device *net_dev) | |
fceaf24a | 1274 | { |
53d21fdb | 1275 | struct rndis_device *rndis_dev = net_dev->extension; |
d66ab514 | 1276 | |
454f18a9 | 1277 | /* Halt and release the rndis device */ |
9c26aa0d | 1278 | rndis_filter_halt_device(rndis_dev); |
fceaf24a | 1279 | |
c2a4efdd | 1280 | kfree(rndis_dev); |
53d21fdb | 1281 | net_dev->extension = NULL; |
fceaf24a | 1282 | |
3fae5c8f | 1283 | netvsc_device_remove(dev); |
fceaf24a HJ |
1284 | } |
1285 | ||
2f5fa6c8 | 1286 | int rndis_filter_open(struct netvsc_device *nvdev) |
fceaf24a | 1287 | { |
2f5fa6c8 | 1288 | if (!nvdev) |
8a62d716 BP |
1289 | return -EINVAL; |
1290 | ||
2f5fa6c8 | 1291 | if (atomic_inc_return(&nvdev->open_cnt) != 1) |
84bf9cef KS |
1292 | return 0; |
1293 | ||
2f5fa6c8 | 1294 | return rndis_filter_open_device(nvdev->extension); |
fceaf24a HJ |
1295 | } |
1296 | ||
2f5fa6c8 | 1297 | int rndis_filter_close(struct netvsc_device *nvdev) |
fceaf24a | 1298 | { |
5fccab3b | 1299 | if (!nvdev) |
8a62d716 BP |
1300 | return -EINVAL; |
1301 | ||
84bf9cef KS |
1302 | if (atomic_dec_return(&nvdev->open_cnt) != 0) |
1303 | return 0; | |
1304 | ||
5fccab3b | 1305 | return rndis_filter_close_device(nvdev->extension); |
fceaf24a | 1306 | } |
ea383bf1 | 1307 | |
1308 | bool rndis_filter_opened(const struct netvsc_device *nvdev) | |
1309 | { | |
1310 | return atomic_read(&nvdev->open_cnt) > 0; | |
1311 | } |