blob: e4bf8229750469d4e20ca92a90767a6743cfb002 [file] [log] [blame]
Hank Janssenfceaf242009-07-13 15:34:54 -07001/*
Hank Janssenfceaf242009-07-13 15:34:54 -07002 * Copyright (c) 2009, Microsoft Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
16 *
17 * Authors:
Haiyang Zhangd0e94d12009-11-23 17:00:22 +000018 * Haiyang Zhang <haiyangz@microsoft.com>
Hank Janssenfceaf242009-07-13 15:34:54 -070019 * Hank Janssen <hjanssen@microsoft.com>
Hank Janssenfceaf242009-07-13 15:34:54 -070020 */
Greg Kroah-Hartman5654e932009-07-14 15:08:20 -070021#include <linux/kernel.h>
Greg Kroah-Hartman0ffa63b2009-07-15 11:06:01 -070022#include <linux/mm.h>
Greg Kroah-Hartmanb4362c92009-07-16 11:50:41 -070023#include <linux/delay.h>
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070024#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Greg Kroah-Hartman4983b392009-08-19 16:14:47 -070026#include "osd.h"
Greg Kroah-Hartman645954c2009-08-28 16:22:59 -070027#include "logging.h"
Hank Janssenfceaf242009-07-13 15:34:54 -070028#include "NetVsc.h"
29#include "RndisFilter.h"
30
31
Bill Pemberton454f18a2009-07-27 16:47:24 -040032/* Globals */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070033static const char *gDriverName = "netvsc";
Hank Janssenfceaf242009-07-13 15:34:54 -070034
Bill Pemberton454f18a2009-07-27 16:47:24 -040035/* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
Greg Kroah-Hartmancaf26a32009-08-19 16:17:03 -070036static const struct hv_guid gNetVscDeviceType = {
37 .data = {
38 0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
39 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E
40 }
Hank Janssenfceaf242009-07-13 15:34:54 -070041};
42
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070043static int NetVscOnDeviceAdd(struct hv_device *Device, void *AdditionalInfo);
Hank Janssenfceaf242009-07-13 15:34:54 -070044
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070045static int NetVscOnDeviceRemove(struct hv_device *Device);
Hank Janssenfceaf242009-07-13 15:34:54 -070046
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070047static void NetVscOnCleanup(struct hv_driver *Driver);
Hank Janssenfceaf242009-07-13 15:34:54 -070048
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070049static void NetVscOnChannelCallback(void *context);
Hank Janssenfceaf242009-07-13 15:34:54 -070050
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070051static int NetVscInitializeSendBufferWithNetVsp(struct hv_device *Device);
Hank Janssenfceaf242009-07-13 15:34:54 -070052
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070053static int NetVscInitializeReceiveBufferWithNetVsp(struct hv_device *Device);
Hank Janssenfceaf242009-07-13 15:34:54 -070054
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -070055static int NetVscDestroySendBuffer(struct netvsc_device *NetDevice);
Hank Janssenfceaf242009-07-13 15:34:54 -070056
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -070057static int NetVscDestroyReceiveBuffer(struct netvsc_device *NetDevice);
Hank Janssenfceaf242009-07-13 15:34:54 -070058
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070059static int NetVscConnectToVsp(struct hv_device *Device);
Hank Janssenfceaf242009-07-13 15:34:54 -070060
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070061static void NetVscOnSendCompletion(struct hv_device *Device,
62 struct vmpacket_descriptor *Packet);
Hank Janssenfceaf242009-07-13 15:34:54 -070063
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070064static int NetVscOnSend(struct hv_device *Device,
65 struct hv_netvsc_packet *Packet);
Hank Janssenfceaf242009-07-13 15:34:54 -070066
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070067static void NetVscOnReceive(struct hv_device *Device,
68 struct vmpacket_descriptor *Packet);
Hank Janssenfceaf242009-07-13 15:34:54 -070069
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070070static void NetVscOnReceiveCompletion(void *Context);
Hank Janssenfceaf242009-07-13 15:34:54 -070071
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070072static void NetVscSendReceiveCompletion(struct hv_device *Device,
73 u64 TransactionId);
Hank Janssenfceaf242009-07-13 15:34:54 -070074
Hank Janssenfceaf242009-07-13 15:34:54 -070075
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -070076static struct netvsc_device *AllocNetDevice(struct hv_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -070077{
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -070078 struct netvsc_device *netDevice;
Hank Janssenfceaf242009-07-13 15:34:54 -070079
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -070080 netDevice = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
Hank Janssenfceaf242009-07-13 15:34:54 -070081 if (!netDevice)
82 return NULL;
83
Bill Pemberton454f18a2009-07-27 16:47:24 -040084 /* Set to 2 to allow both inbound and outbound traffic */
Bill Pembertonf4888412009-07-29 17:00:12 -040085 atomic_cmpxchg(&netDevice->RefCount, 0, 2);
Hank Janssenfceaf242009-07-13 15:34:54 -070086
87 netDevice->Device = Device;
88 Device->Extension = netDevice;
89
90 return netDevice;
91}
92
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -070093static void FreeNetDevice(struct netvsc_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -070094{
Bill Pembertonf4888412009-07-29 17:00:12 -040095 ASSERT(atomic_read(&Device->RefCount) == 0);
Hank Janssenfceaf242009-07-13 15:34:54 -070096 Device->Device->Extension = NULL;
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -070097 kfree(Device);
Hank Janssenfceaf242009-07-13 15:34:54 -070098}
99
100
Bill Pemberton454f18a2009-07-27 16:47:24 -0400101/* Get the net device object iff exists and its refcount > 1 */
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700102static struct netvsc_device *GetOutboundNetDevice(struct hv_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700103{
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700104 struct netvsc_device *netDevice;
Hank Janssenfceaf242009-07-13 15:34:54 -0700105
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700106 netDevice = Device->Extension;
Bill Pembertonf4888412009-07-29 17:00:12 -0400107 if (netDevice && atomic_read(&netDevice->RefCount) > 1)
108 atomic_inc(&netDevice->RefCount);
Hank Janssenfceaf242009-07-13 15:34:54 -0700109 else
Hank Janssenfceaf242009-07-13 15:34:54 -0700110 netDevice = NULL;
Hank Janssenfceaf242009-07-13 15:34:54 -0700111
112 return netDevice;
113}
114
Bill Pemberton454f18a2009-07-27 16:47:24 -0400115/* Get the net device object iff exists and its refcount > 0 */
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700116static struct netvsc_device *GetInboundNetDevice(struct hv_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700117{
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700118 struct netvsc_device *netDevice;
Hank Janssenfceaf242009-07-13 15:34:54 -0700119
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700120 netDevice = Device->Extension;
Bill Pembertonf4888412009-07-29 17:00:12 -0400121 if (netDevice && atomic_read(&netDevice->RefCount))
122 atomic_inc(&netDevice->RefCount);
Hank Janssenfceaf242009-07-13 15:34:54 -0700123 else
Hank Janssenfceaf242009-07-13 15:34:54 -0700124 netDevice = NULL;
Hank Janssenfceaf242009-07-13 15:34:54 -0700125
126 return netDevice;
127}
128
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700129static void PutNetDevice(struct hv_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700130{
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700131 struct netvsc_device *netDevice;
Hank Janssenfceaf242009-07-13 15:34:54 -0700132
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700133 netDevice = Device->Extension;
Hank Janssenfceaf242009-07-13 15:34:54 -0700134 ASSERT(netDevice);
135
Bill Pembertonf4888412009-07-29 17:00:12 -0400136 atomic_dec(&netDevice->RefCount);
Hank Janssenfceaf242009-07-13 15:34:54 -0700137}
138
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700139static struct netvsc_device *ReleaseOutboundNetDevice(struct hv_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700140{
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700141 struct netvsc_device *netDevice;
Hank Janssenfceaf242009-07-13 15:34:54 -0700142
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700143 netDevice = Device->Extension;
Hank Janssenfceaf242009-07-13 15:34:54 -0700144 if (netDevice == NULL)
145 return NULL;
146
Bill Pemberton454f18a2009-07-27 16:47:24 -0400147 /* Busy wait until the ref drop to 2, then set it to 1 */
Bill Pembertonf4888412009-07-29 17:00:12 -0400148 while (atomic_cmpxchg(&netDevice->RefCount, 2, 1) != 2)
Greg Kroah-Hartmanb4362c92009-07-16 11:50:41 -0700149 udelay(100);
Hank Janssenfceaf242009-07-13 15:34:54 -0700150
151 return netDevice;
152}
153
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700154static struct netvsc_device *ReleaseInboundNetDevice(struct hv_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700155{
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700156 struct netvsc_device *netDevice;
Hank Janssenfceaf242009-07-13 15:34:54 -0700157
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700158 netDevice = Device->Extension;
Hank Janssenfceaf242009-07-13 15:34:54 -0700159 if (netDevice == NULL)
160 return NULL;
161
Bill Pemberton454f18a2009-07-27 16:47:24 -0400162 /* Busy wait until the ref drop to 1, then set it to 0 */
Bill Pembertonf4888412009-07-29 17:00:12 -0400163 while (atomic_cmpxchg(&netDevice->RefCount, 1, 0) != 1)
Greg Kroah-Hartmanb4362c92009-07-16 11:50:41 -0700164 udelay(100);
Hank Janssenfceaf242009-07-13 15:34:54 -0700165
166 Device->Extension = NULL;
167 return netDevice;
168}
169
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700170/**
171 * NetVscInitialize - Main entry point
172 */
173int NetVscInitialize(struct hv_driver *drv)
Hank Janssenfceaf242009-07-13 15:34:54 -0700174{
Greg Kroah-Hartman7e23a6e2009-08-27 15:58:15 -0700175 struct netvsc_driver *driver = (struct netvsc_driver *)drv;
Hank Janssenfceaf242009-07-13 15:34:54 -0700176
177 DPRINT_ENTER(NETVSC);
178
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700179 DPRINT_DBG(NETVSC, "sizeof(struct hv_netvsc_packet)=%zd, "
180 "sizeof(struct nvsp_message)=%zd, "
181 "sizeof(struct vmtransfer_page_packet_header)=%zd",
182 sizeof(struct hv_netvsc_packet),
183 sizeof(struct nvsp_message),
184 sizeof(struct vmtransfer_page_packet_header));
Hank Janssenfceaf242009-07-13 15:34:54 -0700185
Bill Pemberton454f18a2009-07-27 16:47:24 -0400186 /* Make sure we are at least 2 pages since 1 page is used for control */
Hank Janssenfceaf242009-07-13 15:34:54 -0700187 ASSERT(driver->RingBufferSize >= (PAGE_SIZE << 1));
188
189 drv->name = gDriverName;
Greg Kroah-Hartmancaf26a32009-08-19 16:17:03 -0700190 memcpy(&drv->deviceType, &gNetVscDeviceType, sizeof(struct hv_guid));
Hank Janssenfceaf242009-07-13 15:34:54 -0700191
Bill Pemberton454f18a2009-07-27 16:47:24 -0400192 /* Make sure it is set by the caller */
Hank Janssenfceaf242009-07-13 15:34:54 -0700193 ASSERT(driver->OnReceiveCallback);
194 ASSERT(driver->OnLinkStatusChanged);
195
Bill Pemberton454f18a2009-07-27 16:47:24 -0400196 /* Setup the dispatch table */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700197 driver->Base.OnDeviceAdd = NetVscOnDeviceAdd;
198 driver->Base.OnDeviceRemove = NetVscOnDeviceRemove;
199 driver->Base.OnCleanup = NetVscOnCleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700200
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700201 driver->OnSend = NetVscOnSend;
Hank Janssenfceaf242009-07-13 15:34:54 -0700202
203 RndisFilterInit(driver);
204
205 DPRINT_EXIT(NETVSC);
206
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700207 return 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700208}
209
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700210static int NetVscInitializeReceiveBufferWithNetVsp(struct hv_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700211{
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700212 int ret = 0;
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700213 struct netvsc_device *netDevice;
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700214 struct nvsp_message *initPacket;
Hank Janssenfceaf242009-07-13 15:34:54 -0700215
216 DPRINT_ENTER(NETVSC);
217
218 netDevice = GetOutboundNetDevice(Device);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700219 if (!netDevice) {
220 DPRINT_ERR(NETVSC, "unable to get net device..."
221 "device being destroyed?");
Hank Janssenfceaf242009-07-13 15:34:54 -0700222 DPRINT_EXIT(NETVSC);
223 return -1;
224 }
225 ASSERT(netDevice->ReceiveBufferSize > 0);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700226 /* page-size grandularity */
227 ASSERT((netDevice->ReceiveBufferSize & (PAGE_SIZE - 1)) == 0);
Hank Janssenfceaf242009-07-13 15:34:54 -0700228
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700229 netDevice->ReceiveBuffer =
230 osd_PageAlloc(netDevice->ReceiveBufferSize >> PAGE_SHIFT);
231 if (!netDevice->ReceiveBuffer) {
232 DPRINT_ERR(NETVSC,
233 "unable to allocate receive buffer of size %d",
234 netDevice->ReceiveBufferSize);
Hank Janssenfceaf242009-07-13 15:34:54 -0700235 ret = -1;
236 goto Cleanup;
237 }
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700238 /* page-aligned buffer */
239 ASSERT(((unsigned long)netDevice->ReceiveBuffer & (PAGE_SIZE - 1)) ==
240 0);
Hank Janssenfceaf242009-07-13 15:34:54 -0700241
242 DPRINT_INFO(NETVSC, "Establishing receive buffer's GPADL...");
243
Bill Pemberton454f18a2009-07-27 16:47:24 -0400244 /*
245 * Establish the gpadl handle for this buffer on this
246 * channel. Note: This call uses the vmbus connection rather
247 * than the channel to establish the gpadl handle.
248 */
Hank Janssenfceaf242009-07-13 15:34:54 -0700249 ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700250 netDevice->ReceiveBuffer,
251 netDevice->ReceiveBufferSize,
252 &netDevice->ReceiveBufferGpadlHandle);
253 if (ret != 0) {
254 DPRINT_ERR(NETVSC,
255 "unable to establish receive buffer's gpadl");
Hank Janssenfceaf242009-07-13 15:34:54 -0700256 goto Cleanup;
257 }
258
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700259 /* osd_WaitEventWait(ext->ChannelInitEvent); */
Hank Janssenfceaf242009-07-13 15:34:54 -0700260
Bill Pemberton454f18a2009-07-27 16:47:24 -0400261 /* Notify the NetVsp of the gpadl handle */
Hank Janssenfceaf242009-07-13 15:34:54 -0700262 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendReceiveBuffer...");
263
264 initPacket = &netDevice->ChannelInitPacket;
265
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700266 memset(initPacket, 0, sizeof(struct nvsp_message));
Hank Janssenfceaf242009-07-13 15:34:54 -0700267
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700268 initPacket->Header.MessageType = NvspMessage1TypeSendReceiveBuffer;
269 initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->ReceiveBufferGpadlHandle;
270 initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
Hank Janssenfceaf242009-07-13 15:34:54 -0700271
Bill Pemberton454f18a2009-07-27 16:47:24 -0400272 /* Send the gpadl notification request */
Hank Janssenfceaf242009-07-13 15:34:54 -0700273 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700274 initPacket,
275 sizeof(struct nvsp_message),
276 (unsigned long)initPacket,
277 VmbusPacketTypeDataInBand,
278 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
279 if (ret != 0) {
280 DPRINT_ERR(NETVSC,
281 "unable to send receive buffer's gpadl to netvsp");
Hank Janssenfceaf242009-07-13 15:34:54 -0700282 goto Cleanup;
283 }
284
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700285 osd_WaitEventWait(netDevice->ChannelInitEvent);
Hank Janssenfceaf242009-07-13 15:34:54 -0700286
Bill Pemberton454f18a2009-07-27 16:47:24 -0400287 /* Check the response */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700288 if (initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Status != NvspStatusSuccess) {
289 DPRINT_ERR(NETVSC, "Unable to complete receive buffer "
290 "initialzation with NetVsp - status %d",
291 initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Status);
Hank Janssenfceaf242009-07-13 15:34:54 -0700292 ret = -1;
293 goto Cleanup;
294 }
295
Bill Pemberton454f18a2009-07-27 16:47:24 -0400296 /* Parse the response */
Hank Janssenfceaf242009-07-13 15:34:54 -0700297 ASSERT(netDevice->ReceiveSectionCount == 0);
298 ASSERT(netDevice->ReceiveSections == NULL);
299
300 netDevice->ReceiveSectionCount = initPacket->Messages.Version1Messages.SendReceiveBufferComplete.NumSections;
301
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700302 netDevice->ReceiveSections = kmalloc(netDevice->ReceiveSectionCount * sizeof(struct nvsp_1_receive_buffer_section), GFP_KERNEL);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700303 if (netDevice->ReceiveSections == NULL) {
Hank Janssenfceaf242009-07-13 15:34:54 -0700304 ret = -1;
305 goto Cleanup;
306 }
307
308 memcpy(netDevice->ReceiveSections,
309 initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Sections,
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700310 netDevice->ReceiveSectionCount * sizeof(struct nvsp_1_receive_buffer_section));
Hank Janssenfceaf242009-07-13 15:34:54 -0700311
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700312 DPRINT_INFO(NETVSC, "Receive sections info (count %d, offset %d, "
313 "endoffset %d, suballoc size %d, num suballocs %d)",
314 netDevice->ReceiveSectionCount,
315 netDevice->ReceiveSections[0].Offset,
316 netDevice->ReceiveSections[0].EndOffset,
317 netDevice->ReceiveSections[0].SubAllocationSize,
318 netDevice->ReceiveSections[0].NumSubAllocations);
Hank Janssenfceaf242009-07-13 15:34:54 -0700319
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700320 /*
321 * For 1st release, there should only be 1 section that represents the
322 * entire receive buffer
323 */
Hank Janssenfceaf242009-07-13 15:34:54 -0700324 if (netDevice->ReceiveSectionCount != 1 ||
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700325 netDevice->ReceiveSections->Offset != 0) {
Hank Janssenfceaf242009-07-13 15:34:54 -0700326 ret = -1;
327 goto Cleanup;
328 }
329
330 goto Exit;
331
332Cleanup:
333 NetVscDestroyReceiveBuffer(netDevice);
334
335Exit:
336 PutNetDevice(Device);
337 DPRINT_EXIT(NETVSC);
338 return ret;
339}
340
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700341static int NetVscInitializeSendBufferWithNetVsp(struct hv_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700342{
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700343 int ret = 0;
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700344 struct netvsc_device *netDevice;
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700345 struct nvsp_message *initPacket;
Hank Janssenfceaf242009-07-13 15:34:54 -0700346
347 DPRINT_ENTER(NETVSC);
348
349 netDevice = GetOutboundNetDevice(Device);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700350 if (!netDevice) {
351 DPRINT_ERR(NETVSC, "unable to get net device..."
352 "device being destroyed?");
Hank Janssenfceaf242009-07-13 15:34:54 -0700353 DPRINT_EXIT(NETVSC);
354 return -1;
355 }
356 ASSERT(netDevice->SendBufferSize > 0);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700357 /* page-size grandularity */
358 ASSERT((netDevice->SendBufferSize & (PAGE_SIZE - 1)) == 0);
Hank Janssenfceaf242009-07-13 15:34:54 -0700359
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700360 netDevice->SendBuffer =
361 osd_PageAlloc(netDevice->SendBufferSize >> PAGE_SHIFT);
362 if (!netDevice->SendBuffer) {
363 DPRINT_ERR(NETVSC, "unable to allocate send buffer of size %d",
364 netDevice->SendBufferSize);
Hank Janssenfceaf242009-07-13 15:34:54 -0700365 ret = -1;
366 goto Cleanup;
367 }
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700368 /* page-aligned buffer */
369 ASSERT(((unsigned long)netDevice->SendBuffer & (PAGE_SIZE - 1)) == 0);
Hank Janssenfceaf242009-07-13 15:34:54 -0700370
371 DPRINT_INFO(NETVSC, "Establishing send buffer's GPADL...");
372
Bill Pemberton454f18a2009-07-27 16:47:24 -0400373 /*
374 * Establish the gpadl handle for this buffer on this
375 * channel. Note: This call uses the vmbus connection rather
376 * than the channel to establish the gpadl handle.
377 */
Hank Janssenfceaf242009-07-13 15:34:54 -0700378 ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700379 netDevice->SendBuffer,
380 netDevice->SendBufferSize,
381 &netDevice->SendBufferGpadlHandle);
382 if (ret != 0) {
Hank Janssenfceaf242009-07-13 15:34:54 -0700383 DPRINT_ERR(NETVSC, "unable to establish send buffer's gpadl");
384 goto Cleanup;
385 }
386
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700387 /* osd_WaitEventWait(ext->ChannelInitEvent); */
Hank Janssenfceaf242009-07-13 15:34:54 -0700388
Bill Pemberton454f18a2009-07-27 16:47:24 -0400389 /* Notify the NetVsp of the gpadl handle */
Hank Janssenfceaf242009-07-13 15:34:54 -0700390 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendSendBuffer...");
391
392 initPacket = &netDevice->ChannelInitPacket;
393
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700394 memset(initPacket, 0, sizeof(struct nvsp_message));
Hank Janssenfceaf242009-07-13 15:34:54 -0700395
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700396 initPacket->Header.MessageType = NvspMessage1TypeSendSendBuffer;
397 initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->SendBufferGpadlHandle;
398 initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_SEND_BUFFER_ID;
Hank Janssenfceaf242009-07-13 15:34:54 -0700399
Bill Pemberton454f18a2009-07-27 16:47:24 -0400400 /* Send the gpadl notification request */
Hank Janssenfceaf242009-07-13 15:34:54 -0700401 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700402 initPacket, sizeof(struct nvsp_message),
403 (unsigned long)initPacket,
404 VmbusPacketTypeDataInBand,
405 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
406 if (ret != 0) {
407 DPRINT_ERR(NETVSC,
408 "unable to send receive buffer's gpadl to netvsp");
Hank Janssenfceaf242009-07-13 15:34:54 -0700409 goto Cleanup;
410 }
411
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700412 osd_WaitEventWait(netDevice->ChannelInitEvent);
Hank Janssenfceaf242009-07-13 15:34:54 -0700413
Bill Pemberton454f18a2009-07-27 16:47:24 -0400414 /* Check the response */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700415 if (initPacket->Messages.Version1Messages.SendSendBufferComplete.Status != NvspStatusSuccess) {
416 DPRINT_ERR(NETVSC, "Unable to complete send buffer "
417 "initialzation with NetVsp - status %d",
418 initPacket->Messages.Version1Messages.SendSendBufferComplete.Status);
Hank Janssenfceaf242009-07-13 15:34:54 -0700419 ret = -1;
420 goto Cleanup;
421 }
422
423 netDevice->SendSectionSize = initPacket->Messages.Version1Messages.SendSendBufferComplete.SectionSize;
424
425 goto Exit;
426
427Cleanup:
428 NetVscDestroySendBuffer(netDevice);
429
430Exit:
431 PutNetDevice(Device);
432 DPRINT_EXIT(NETVSC);
433 return ret;
434}
435
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700436static int NetVscDestroyReceiveBuffer(struct netvsc_device *NetDevice)
Hank Janssenfceaf242009-07-13 15:34:54 -0700437{
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700438 struct nvsp_message *revokePacket;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700439 int ret = 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700440
441 DPRINT_ENTER(NETVSC);
442
Bill Pemberton454f18a2009-07-27 16:47:24 -0400443 /*
444 * If we got a section count, it means we received a
445 * SendReceiveBufferComplete msg (ie sent
446 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
447 * to send a revoke msg here
448 */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700449 if (NetDevice->ReceiveSectionCount) {
450 DPRINT_INFO(NETVSC,
451 "Sending NvspMessage1TypeRevokeReceiveBuffer...");
Hank Janssenfceaf242009-07-13 15:34:54 -0700452
Bill Pemberton454f18a2009-07-27 16:47:24 -0400453 /* Send the revoke receive buffer */
Hank Janssenfceaf242009-07-13 15:34:54 -0700454 revokePacket = &NetDevice->RevokePacket;
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700455 memset(revokePacket, 0, sizeof(struct nvsp_message));
Hank Janssenfceaf242009-07-13 15:34:54 -0700456
457 revokePacket->Header.MessageType = NvspMessage1TypeRevokeReceiveBuffer;
458 revokePacket->Messages.Version1Messages.RevokeReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
459
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700460 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(
461 NetDevice->Device,
462 revokePacket,
463 sizeof(struct nvsp_message),
464 (unsigned long)revokePacket,
465 VmbusPacketTypeDataInBand, 0);
Bill Pemberton454f18a2009-07-27 16:47:24 -0400466 /*
467 * If we failed here, we might as well return and
468 * have a leak rather than continue and a bugchk
469 */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700470 if (ret != 0) {
471 DPRINT_ERR(NETVSC, "unable to send revoke receive "
472 "buffer to netvsp");
Hank Janssenfceaf242009-07-13 15:34:54 -0700473 DPRINT_EXIT(NETVSC);
474 return -1;
475 }
476 }
477
Bill Pemberton454f18a2009-07-27 16:47:24 -0400478 /* Teardown the gpadl on the vsp end */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700479 if (NetDevice->ReceiveBufferGpadlHandle) {
Hank Janssenfceaf242009-07-13 15:34:54 -0700480 DPRINT_INFO(NETVSC, "Tearing down receive buffer's GPADL...");
481
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700482 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(
483 NetDevice->Device,
484 NetDevice->ReceiveBufferGpadlHandle);
Hank Janssenfceaf242009-07-13 15:34:54 -0700485
Bill Pemberton454f18a2009-07-27 16:47:24 -0400486 /* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700487 if (ret != 0) {
488 DPRINT_ERR(NETVSC,
489 "unable to teardown receive buffer's gpadl");
Hank Janssenfceaf242009-07-13 15:34:54 -0700490 DPRINT_EXIT(NETVSC);
491 return -1;
492 }
493 NetDevice->ReceiveBufferGpadlHandle = 0;
494 }
495
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700496 if (NetDevice->ReceiveBuffer) {
Hank Janssenfceaf242009-07-13 15:34:54 -0700497 DPRINT_INFO(NETVSC, "Freeing up receive buffer...");
498
Bill Pemberton454f18a2009-07-27 16:47:24 -0400499 /* Free up the receive buffer */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700500 osd_PageFree(NetDevice->ReceiveBuffer,
501 NetDevice->ReceiveBufferSize >> PAGE_SHIFT);
Hank Janssenfceaf242009-07-13 15:34:54 -0700502 NetDevice->ReceiveBuffer = NULL;
503 }
504
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700505 if (NetDevice->ReceiveSections) {
506 NetDevice->ReceiveSectionCount = 0;
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700507 kfree(NetDevice->ReceiveSections);
Hank Janssenfceaf242009-07-13 15:34:54 -0700508 NetDevice->ReceiveSections = NULL;
Hank Janssenfceaf242009-07-13 15:34:54 -0700509 }
510
511 DPRINT_EXIT(NETVSC);
512
513 return ret;
514}
515
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700516static int NetVscDestroySendBuffer(struct netvsc_device *NetDevice)
Hank Janssenfceaf242009-07-13 15:34:54 -0700517{
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700518 struct nvsp_message *revokePacket;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700519 int ret = 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700520
521 DPRINT_ENTER(NETVSC);
522
Bill Pemberton454f18a2009-07-27 16:47:24 -0400523 /*
524 * If we got a section count, it means we received a
525 * SendReceiveBufferComplete msg (ie sent
526 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
527 * to send a revoke msg here
528 */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700529 if (NetDevice->SendSectionSize) {
530 DPRINT_INFO(NETVSC,
531 "Sending NvspMessage1TypeRevokeSendBuffer...");
Hank Janssenfceaf242009-07-13 15:34:54 -0700532
Bill Pemberton454f18a2009-07-27 16:47:24 -0400533 /* Send the revoke send buffer */
Hank Janssenfceaf242009-07-13 15:34:54 -0700534 revokePacket = &NetDevice->RevokePacket;
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700535 memset(revokePacket, 0, sizeof(struct nvsp_message));
Hank Janssenfceaf242009-07-13 15:34:54 -0700536
537 revokePacket->Header.MessageType = NvspMessage1TypeRevokeSendBuffer;
538 revokePacket->Messages.Version1Messages.RevokeSendBuffer.Id = NETVSC_SEND_BUFFER_ID;
539
540 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(NetDevice->Device,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700541 revokePacket,
542 sizeof(struct nvsp_message),
543 (unsigned long)revokePacket,
544 VmbusPacketTypeDataInBand, 0);
545 /*
546 * If we failed here, we might as well return and have a leak
547 * rather than continue and a bugchk
548 */
549 if (ret != 0) {
550 DPRINT_ERR(NETVSC, "unable to send revoke send buffer "
551 "to netvsp");
Hank Janssenfceaf242009-07-13 15:34:54 -0700552 DPRINT_EXIT(NETVSC);
553 return -1;
554 }
555 }
556
Bill Pemberton454f18a2009-07-27 16:47:24 -0400557 /* Teardown the gpadl on the vsp end */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700558 if (NetDevice->SendBufferGpadlHandle) {
Hank Janssenfceaf242009-07-13 15:34:54 -0700559 DPRINT_INFO(NETVSC, "Tearing down send buffer's GPADL...");
560
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700561 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(NetDevice->Device, NetDevice->SendBufferGpadlHandle);
Hank Janssenfceaf242009-07-13 15:34:54 -0700562
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700563 /*
564 * If we failed here, we might as well return and have a leak
565 * rather than continue and a bugchk
566 */
567 if (ret != 0) {
568 DPRINT_ERR(NETVSC, "unable to teardown send buffer's "
569 "gpadl");
Hank Janssenfceaf242009-07-13 15:34:54 -0700570 DPRINT_EXIT(NETVSC);
571 return -1;
572 }
573 NetDevice->SendBufferGpadlHandle = 0;
574 }
575
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700576 if (NetDevice->SendBuffer) {
Hank Janssenfceaf242009-07-13 15:34:54 -0700577 DPRINT_INFO(NETVSC, "Freeing up send buffer...");
578
Bill Pemberton454f18a2009-07-27 16:47:24 -0400579 /* Free up the receive buffer */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700580 osd_PageFree(NetDevice->SendBuffer,
581 NetDevice->SendBufferSize >> PAGE_SHIFT);
Hank Janssenfceaf242009-07-13 15:34:54 -0700582 NetDevice->SendBuffer = NULL;
583 }
584
585 DPRINT_EXIT(NETVSC);
586
587 return ret;
588}
589
590
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700591static int NetVscConnectToVsp(struct hv_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700592{
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700593 int ret;
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700594 struct netvsc_device *netDevice;
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700595 struct nvsp_message *initPacket;
Hank Janssenfceaf242009-07-13 15:34:54 -0700596 int ndisVersion;
597
598 DPRINT_ENTER(NETVSC);
599
600 netDevice = GetOutboundNetDevice(Device);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700601 if (!netDevice) {
602 DPRINT_ERR(NETVSC, "unable to get net device..."
603 "device being destroyed?");
Hank Janssenfceaf242009-07-13 15:34:54 -0700604 DPRINT_EXIT(NETVSC);
605 return -1;
606 }
607
608 initPacket = &netDevice->ChannelInitPacket;
609
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700610 memset(initPacket, 0, sizeof(struct nvsp_message));
Hank Janssenfceaf242009-07-13 15:34:54 -0700611 initPacket->Header.MessageType = NvspMessageTypeInit;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700612 initPacket->Messages.InitMessages.Init.MinProtocolVersion = NVSP_MIN_PROTOCOL_VERSION;
613 initPacket->Messages.InitMessages.Init.MaxProtocolVersion = NVSP_MAX_PROTOCOL_VERSION;
Hank Janssenfceaf242009-07-13 15:34:54 -0700614
615 DPRINT_INFO(NETVSC, "Sending NvspMessageTypeInit...");
616
Bill Pemberton454f18a2009-07-27 16:47:24 -0400617 /* Send the init request */
Hank Janssenfceaf242009-07-13 15:34:54 -0700618 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700619 initPacket,
620 sizeof(struct nvsp_message),
621 (unsigned long)initPacket,
622 VmbusPacketTypeDataInBand,
623 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Hank Janssenfceaf242009-07-13 15:34:54 -0700624
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700625 if (ret != 0) {
Hank Janssenfceaf242009-07-13 15:34:54 -0700626 DPRINT_ERR(NETVSC, "unable to send NvspMessageTypeInit");
627 goto Cleanup;
628 }
629
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700630 osd_WaitEventWait(netDevice->ChannelInitEvent);
Hank Janssenfceaf242009-07-13 15:34:54 -0700631
Bill Pemberton454f18a2009-07-27 16:47:24 -0400632 /* Now, check the response */
633 /* ASSERT(initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength <= MAX_MULTIPAGE_BUFFER_COUNT); */
Hank Janssenfceaf242009-07-13 15:34:54 -0700634 DPRINT_INFO(NETVSC, "NvspMessageTypeInit status(%d) max mdl chain (%d)",
635 initPacket->Messages.InitMessages.InitComplete.Status,
636 initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength);
637
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700638 if (initPacket->Messages.InitMessages.InitComplete.Status !=
639 NvspStatusSuccess) {
640 DPRINT_ERR(NETVSC,
641 "unable to initialize with netvsp (status 0x%x)",
642 initPacket->Messages.InitMessages.InitComplete.Status);
Hank Janssenfceaf242009-07-13 15:34:54 -0700643 ret = -1;
644 goto Cleanup;
645 }
646
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700647 if (initPacket->Messages.InitMessages.InitComplete.NegotiatedProtocolVersion != NVSP_PROTOCOL_VERSION_1) {
648 DPRINT_ERR(NETVSC, "unable to initialize with netvsp "
649 "(version expected 1 got %d)",
650 initPacket->Messages.InitMessages.InitComplete.NegotiatedProtocolVersion);
Hank Janssenfceaf242009-07-13 15:34:54 -0700651 ret = -1;
652 goto Cleanup;
653 }
654 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendNdisVersion...");
655
Bill Pemberton454f18a2009-07-27 16:47:24 -0400656 /* Send the ndis version */
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700657 memset(initPacket, 0, sizeof(struct nvsp_message));
Hank Janssenfceaf242009-07-13 15:34:54 -0700658
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700659 ndisVersion = 0x00050000;
Hank Janssenfceaf242009-07-13 15:34:54 -0700660
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700661 initPacket->Header.MessageType = NvspMessage1TypeSendNdisVersion;
662 initPacket->Messages.Version1Messages.SendNdisVersion.NdisMajorVersion =
663 (ndisVersion & 0xFFFF0000) >> 16;
664 initPacket->Messages.Version1Messages.SendNdisVersion.NdisMinorVersion =
665 ndisVersion & 0xFFFF;
Hank Janssenfceaf242009-07-13 15:34:54 -0700666
Bill Pemberton454f18a2009-07-27 16:47:24 -0400667 /* Send the init request */
Hank Janssenfceaf242009-07-13 15:34:54 -0700668 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700669 initPacket,
670 sizeof(struct nvsp_message),
671 (unsigned long)initPacket,
672 VmbusPacketTypeDataInBand, 0);
673 if (ret != 0) {
674 DPRINT_ERR(NETVSC,
675 "unable to send NvspMessage1TypeSendNdisVersion");
Hank Janssenfceaf242009-07-13 15:34:54 -0700676 ret = -1;
677 goto Cleanup;
678 }
Bill Pemberton454f18a2009-07-27 16:47:24 -0400679 /*
680 * BUGBUG - We have to wait for the above msg since the
681 * netvsp uses KMCL which acknowledges packet (completion
682 * packet) since our Vmbus always set the
683 * VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag
684 */
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700685 /* osd_WaitEventWait(NetVscChannel->ChannelInitEvent); */
Hank Janssenfceaf242009-07-13 15:34:54 -0700686
Bill Pemberton454f18a2009-07-27 16:47:24 -0400687 /* Post the big receive buffer to NetVSP */
Hank Janssenfceaf242009-07-13 15:34:54 -0700688 ret = NetVscInitializeReceiveBufferWithNetVsp(Device);
689 if (ret == 0)
Hank Janssenfceaf242009-07-13 15:34:54 -0700690 ret = NetVscInitializeSendBufferWithNetVsp(Device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700691
692Cleanup:
693 PutNetDevice(Device);
694 DPRINT_EXIT(NETVSC);
695 return ret;
696}
697
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700698static void NetVscDisconnectFromVsp(struct netvsc_device *NetDevice)
Hank Janssenfceaf242009-07-13 15:34:54 -0700699{
700 DPRINT_ENTER(NETVSC);
701
702 NetVscDestroyReceiveBuffer(NetDevice);
703 NetVscDestroySendBuffer(NetDevice);
704
705 DPRINT_EXIT(NETVSC);
706}
707
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700708/**
709 * NetVscOnDeviceAdd - Callback when the device belonging to this driver is added
710 */
711static int NetVscOnDeviceAdd(struct hv_device *Device, void *AdditionalInfo)
Hank Janssenfceaf242009-07-13 15:34:54 -0700712{
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700713 int ret = 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700714 int i;
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700715 struct netvsc_device *netDevice;
Bill Pembertond29274e2009-09-11 21:46:43 -0400716 struct hv_netvsc_packet *packet, *pos;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700717 struct netvsc_driver *netDriver =
718 (struct netvsc_driver *)Device->Driver;
Hank Janssenfceaf242009-07-13 15:34:54 -0700719
720 DPRINT_ENTER(NETVSC);
721
722 netDevice = AllocNetDevice(Device);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700723 if (!netDevice) {
Hank Janssenfceaf242009-07-13 15:34:54 -0700724 ret = -1;
725 goto Cleanup;
726 }
727
728 DPRINT_DBG(NETVSC, "netvsc channel object allocated - %p", netDevice);
729
Bill Pemberton454f18a2009-07-27 16:47:24 -0400730 /* Initialize the NetVSC channel extension */
Hank Janssenfceaf242009-07-13 15:34:54 -0700731 netDevice->ReceiveBufferSize = NETVSC_RECEIVE_BUFFER_SIZE;
Greg Kroah-Hartman64368732009-07-15 14:56:15 -0700732 spin_lock_init(&netDevice->receive_packet_list_lock);
Hank Janssenfceaf242009-07-13 15:34:54 -0700733
734 netDevice->SendBufferSize = NETVSC_SEND_BUFFER_SIZE;
735
Bill Pembertond29274e2009-09-11 21:46:43 -0400736 INIT_LIST_HEAD(&netDevice->ReceivePacketList);
Hank Janssenfceaf242009-07-13 15:34:54 -0700737
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700738 for (i = 0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++) {
739 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
740 (NETVSC_RECEIVE_SG_COUNT *
741 sizeof(struct hv_page_buffer)), GFP_KERNEL);
742 if (!packet) {
743 DPRINT_DBG(NETVSC, "unable to allocate netvsc pkts "
744 "for receive pool (wanted %d got %d)",
745 NETVSC_RECEIVE_PACKETLIST_COUNT, i);
Hank Janssenfceaf242009-07-13 15:34:54 -0700746 break;
747 }
Bill Pembertond29274e2009-09-11 21:46:43 -0400748 list_add_tail(&packet->ListEntry,
749 &netDevice->ReceivePacketList);
Hank Janssenfceaf242009-07-13 15:34:54 -0700750 }
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700751 netDevice->ChannelInitEvent = osd_WaitEventCreate();
Hank Janssenfceaf242009-07-13 15:34:54 -0700752
Bill Pemberton454f18a2009-07-27 16:47:24 -0400753 /* Open the channel */
Hank Janssenfceaf242009-07-13 15:34:54 -0700754 ret = Device->Driver->VmbusChannelInterface.Open(Device,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700755 netDriver->RingBufferSize,
756 netDriver->RingBufferSize,
757 NULL, 0,
758 NetVscOnChannelCallback,
759 Device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700760
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700761 if (ret != 0) {
Hank Janssenfceaf242009-07-13 15:34:54 -0700762 DPRINT_ERR(NETVSC, "unable to open channel: %d", ret);
763 ret = -1;
764 goto Cleanup;
765 }
766
Bill Pemberton454f18a2009-07-27 16:47:24 -0400767 /* Channel is opened */
Hank Janssenfceaf242009-07-13 15:34:54 -0700768 DPRINT_INFO(NETVSC, "*** NetVSC channel opened successfully! ***");
769
Bill Pemberton454f18a2009-07-27 16:47:24 -0400770 /* Connect with the NetVsp */
Hank Janssenfceaf242009-07-13 15:34:54 -0700771 ret = NetVscConnectToVsp(Device);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700772 if (ret != 0) {
Hank Janssenfceaf242009-07-13 15:34:54 -0700773 DPRINT_ERR(NETVSC, "unable to connect to NetVSP - %d", ret);
774 ret = -1;
775 goto Close;
776 }
777
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700778 DPRINT_INFO(NETVSC, "*** NetVSC channel handshake result - %d ***",
779 ret);
Hank Janssenfceaf242009-07-13 15:34:54 -0700780
781 DPRINT_EXIT(NETVSC);
782 return ret;
783
784Close:
Bill Pemberton454f18a2009-07-27 16:47:24 -0400785 /* Now, we can close the channel safely */
Hank Janssenfceaf242009-07-13 15:34:54 -0700786 Device->Driver->VmbusChannelInterface.Close(Device);
787
788Cleanup:
789
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700790 if (netDevice) {
Bill Pemberton420beac2009-07-29 17:00:10 -0400791 kfree(netDevice->ChannelInitEvent);
Hank Janssenfceaf242009-07-13 15:34:54 -0700792
Bill Pembertond29274e2009-09-11 21:46:43 -0400793 list_for_each_entry_safe(packet, pos,
794 &netDevice->ReceivePacketList,
795 ListEntry) {
796 list_del(&packet->ListEntry);
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700797 kfree(packet);
Hank Janssenfceaf242009-07-13 15:34:54 -0700798 }
799
Hank Janssenfceaf242009-07-13 15:34:54 -0700800 ReleaseOutboundNetDevice(Device);
801 ReleaseInboundNetDevice(Device);
802
803 FreeNetDevice(netDevice);
804 }
805
806 DPRINT_EXIT(NETVSC);
807 return ret;
808}
809
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700810/**
811 * NetVscOnDeviceRemove - Callback when the root bus device is removed
812 */
813static int NetVscOnDeviceRemove(struct hv_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700814{
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700815 struct netvsc_device *netDevice;
Bill Pembertond29274e2009-09-11 21:46:43 -0400816 struct hv_netvsc_packet *netvscPacket, *pos;
Hank Janssenfceaf242009-07-13 15:34:54 -0700817
818 DPRINT_ENTER(NETVSC);
819
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700820 DPRINT_INFO(NETVSC, "Disabling outbound traffic on net device (%p)...",
821 Device->Extension);
Hank Janssenfceaf242009-07-13 15:34:54 -0700822
Bill Pemberton454f18a2009-07-27 16:47:24 -0400823 /* Stop outbound traffic ie sends and receives completions */
Hank Janssenfceaf242009-07-13 15:34:54 -0700824 netDevice = ReleaseOutboundNetDevice(Device);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700825 if (!netDevice) {
Hank Janssenfceaf242009-07-13 15:34:54 -0700826 DPRINT_ERR(NETVSC, "No net device present!!");
827 return -1;
828 }
829
Bill Pemberton454f18a2009-07-27 16:47:24 -0400830 /* Wait for all send completions */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700831 while (atomic_read(&netDevice->NumOutstandingSends)) {
832 DPRINT_INFO(NETVSC, "waiting for %d requests to complete...",
833 atomic_read(&netDevice->NumOutstandingSends));
Greg Kroah-Hartmanb4362c92009-07-16 11:50:41 -0700834 udelay(100);
Hank Janssenfceaf242009-07-13 15:34:54 -0700835 }
836
837 DPRINT_INFO(NETVSC, "Disconnecting from netvsp...");
838
839 NetVscDisconnectFromVsp(netDevice);
840
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700841 DPRINT_INFO(NETVSC, "Disabling inbound traffic on net device (%p)...",
842 Device->Extension);
Hank Janssenfceaf242009-07-13 15:34:54 -0700843
Bill Pemberton454f18a2009-07-27 16:47:24 -0400844 /* Stop inbound traffic ie receives and sends completions */
Hank Janssenfceaf242009-07-13 15:34:54 -0700845 netDevice = ReleaseInboundNetDevice(Device);
846
Bill Pemberton454f18a2009-07-27 16:47:24 -0400847 /* At this point, no one should be accessing netDevice except in here */
Hank Janssenfceaf242009-07-13 15:34:54 -0700848 DPRINT_INFO(NETVSC, "net device (%p) safe to remove", netDevice);
849
Bill Pemberton454f18a2009-07-27 16:47:24 -0400850 /* Now, we can close the channel safely */
Hank Janssenfceaf242009-07-13 15:34:54 -0700851 Device->Driver->VmbusChannelInterface.Close(Device);
852
Bill Pemberton454f18a2009-07-27 16:47:24 -0400853 /* Release all resources */
Bill Pembertond29274e2009-09-11 21:46:43 -0400854 list_for_each_entry_safe(netvscPacket, pos,
855 &netDevice->ReceivePacketList, ListEntry) {
856 list_del(&netvscPacket->ListEntry);
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700857 kfree(netvscPacket);
Hank Janssenfceaf242009-07-13 15:34:54 -0700858 }
859
Bill Pemberton420beac2009-07-29 17:00:10 -0400860 kfree(netDevice->ChannelInitEvent);
Hank Janssenfceaf242009-07-13 15:34:54 -0700861 FreeNetDevice(netDevice);
862
863 DPRINT_EXIT(NETVSC);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700864 return 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700865}
866
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700867/**
868 * NetVscOnCleanup - Perform any cleanup when the driver is removed
869 */
870static void NetVscOnCleanup(struct hv_driver *drv)
Hank Janssenfceaf242009-07-13 15:34:54 -0700871{
872 DPRINT_ENTER(NETVSC);
Hank Janssenfceaf242009-07-13 15:34:54 -0700873 DPRINT_EXIT(NETVSC);
874}
875
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700876static void NetVscOnSendCompletion(struct hv_device *Device,
877 struct vmpacket_descriptor *Packet)
Hank Janssenfceaf242009-07-13 15:34:54 -0700878{
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700879 struct netvsc_device *netDevice;
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700880 struct nvsp_message *nvspPacket;
Nicolas Palix4193d4f2009-07-29 14:10:10 +0200881 struct hv_netvsc_packet *nvscPacket;
Hank Janssenfceaf242009-07-13 15:34:54 -0700882
883 DPRINT_ENTER(NETVSC);
884
885 netDevice = GetInboundNetDevice(Device);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700886 if (!netDevice) {
887 DPRINT_ERR(NETVSC, "unable to get net device..."
888 "device being destroyed?");
Hank Janssenfceaf242009-07-13 15:34:54 -0700889 DPRINT_EXIT(NETVSC);
890 return;
891 }
892
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700893 nvspPacket = (struct nvsp_message *)((unsigned long)Packet + (Packet->DataOffset8 << 3));
Hank Janssenfceaf242009-07-13 15:34:54 -0700894
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700895 DPRINT_DBG(NETVSC, "send completion packet - type %d",
896 nvspPacket->Header.MessageType);
Hank Janssenfceaf242009-07-13 15:34:54 -0700897
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700898 if ((nvspPacket->Header.MessageType == NvspMessageTypeInitComplete) ||
899 (nvspPacket->Header.MessageType ==
900 NvspMessage1TypeSendReceiveBufferComplete) ||
901 (nvspPacket->Header.MessageType ==
902 NvspMessage1TypeSendSendBufferComplete)) {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400903 /* Copy the response back */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700904 memcpy(&netDevice->ChannelInitPacket, nvspPacket,
905 sizeof(struct nvsp_message));
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700906 osd_WaitEventSet(netDevice->ChannelInitEvent);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700907 } else if (nvspPacket->Header.MessageType ==
908 NvspMessage1TypeSendRNDISPacketComplete) {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400909 /* Get the send context */
Nicolas Palix4193d4f2009-07-29 14:10:10 +0200910 nvscPacket = (struct hv_netvsc_packet *)(unsigned long)Packet->TransactionId;
Hank Janssenfceaf242009-07-13 15:34:54 -0700911 ASSERT(nvscPacket);
912
Bill Pemberton454f18a2009-07-27 16:47:24 -0400913 /* Notify the layer above us */
Hank Janssenfceaf242009-07-13 15:34:54 -0700914 nvscPacket->Completion.Send.OnSendCompletion(nvscPacket->Completion.Send.SendCompletionContext);
915
Bill Pembertonf4888412009-07-29 17:00:12 -0400916 atomic_dec(&netDevice->NumOutstandingSends);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700917 } else {
918 DPRINT_ERR(NETVSC, "Unknown send completion packet type - "
919 "%d received!!", nvspPacket->Header.MessageType);
Hank Janssenfceaf242009-07-13 15:34:54 -0700920 }
921
922 PutNetDevice(Device);
923 DPRINT_EXIT(NETVSC);
924}
925
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700926static int NetVscOnSend(struct hv_device *Device,
927 struct hv_netvsc_packet *Packet)
Hank Janssenfceaf242009-07-13 15:34:54 -0700928{
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700929 struct netvsc_device *netDevice;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700930 int ret = 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700931
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700932 struct nvsp_message sendMessage;
Hank Janssenfceaf242009-07-13 15:34:54 -0700933
934 DPRINT_ENTER(NETVSC);
935
936 netDevice = GetOutboundNetDevice(Device);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700937 if (!netDevice) {
938 DPRINT_ERR(NETVSC, "net device (%p) shutting down..."
939 "ignoring outbound packets", netDevice);
Hank Janssenfceaf242009-07-13 15:34:54 -0700940 DPRINT_EXIT(NETVSC);
941 return -2;
942 }
943
944 sendMessage.Header.MessageType = NvspMessage1TypeSendRNDISPacket;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700945 if (Packet->IsDataPacket) {
946 /* 0 is RMC_DATA; */
947 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 0;
948 } else {
949 /* 1 is RMC_CONTROL; */
950 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 1;
951 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700952
Bill Pemberton454f18a2009-07-27 16:47:24 -0400953 /* Not using send buffer section */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700954 sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionIndex = 0xFFFFFFFF;
955 sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionSize = 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700956
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700957 if (Packet->PageBufferCount) {
958 ret = Device->Driver->VmbusChannelInterface.SendPacketPageBuffer(
959 Device, Packet->PageBuffers,
960 Packet->PageBufferCount,
961 &sendMessage,
962 sizeof(struct nvsp_message),
963 (unsigned long)Packet);
964 } else {
Hank Janssenfceaf242009-07-13 15:34:54 -0700965 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700966 &sendMessage,
967 sizeof(struct nvsp_message),
968 (unsigned long)Packet,
969 VmbusPacketTypeDataInBand,
970 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Hank Janssenfceaf242009-07-13 15:34:54 -0700971
972 }
973
974 if (ret != 0)
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700975 DPRINT_ERR(NETVSC, "Unable to send packet %p ret %d",
976 Packet, ret);
Hank Janssenfceaf242009-07-13 15:34:54 -0700977
Bill Pembertonf4888412009-07-29 17:00:12 -0400978 atomic_inc(&netDevice->NumOutstandingSends);
Hank Janssenfceaf242009-07-13 15:34:54 -0700979 PutNetDevice(Device);
980
981 DPRINT_EXIT(NETVSC);
982 return ret;
983}
984
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700985static void NetVscOnReceive(struct hv_device *Device,
986 struct vmpacket_descriptor *Packet)
Hank Janssenfceaf242009-07-13 15:34:54 -0700987{
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700988 struct netvsc_device *netDevice;
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700989 struct vmtransfer_page_packet_header *vmxferpagePacket;
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700990 struct nvsp_message *nvspPacket;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700991 struct hv_netvsc_packet *netvscPacket = NULL;
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -0700992 unsigned long start;
993 unsigned long end, endVirtual;
Greg Kroah-Hartman7e23a6e2009-08-27 15:58:15 -0700994 /* struct netvsc_driver *netvscDriver; */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700995 struct xferpage_packet *xferpagePacket = NULL;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700996 int i, j;
997 int count = 0, bytesRemain = 0;
Greg Kroah-Hartman64368732009-07-15 14:56:15 -0700998 unsigned long flags;
Bill Pembertond29274e2009-09-11 21:46:43 -0400999 LIST_HEAD(listHead);
Hank Janssenfceaf242009-07-13 15:34:54 -07001000
1001 DPRINT_ENTER(NETVSC);
1002
1003 netDevice = GetInboundNetDevice(Device);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001004 if (!netDevice) {
1005 DPRINT_ERR(NETVSC, "unable to get net device..."
1006 "device being destroyed?");
Hank Janssenfceaf242009-07-13 15:34:54 -07001007 DPRINT_EXIT(NETVSC);
1008 return;
1009 }
1010
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001011 /*
1012 * All inbound packets other than send completion should be xfer page
1013 * packet
1014 */
1015 if (Packet->Type != VmbusPacketTypeDataUsingTransferPages) {
1016 DPRINT_ERR(NETVSC, "Unknown packet type received - %d",
1017 Packet->Type);
Hank Janssenfceaf242009-07-13 15:34:54 -07001018 PutNetDevice(Device);
1019 return;
1020 }
1021
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001022 nvspPacket = (struct nvsp_message *)((unsigned long)Packet +
1023 (Packet->DataOffset8 << 3));
Hank Janssenfceaf242009-07-13 15:34:54 -07001024
Bill Pemberton454f18a2009-07-27 16:47:24 -04001025 /* Make sure this is a valid nvsp packet */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001026 if (nvspPacket->Header.MessageType != NvspMessage1TypeSendRNDISPacket) {
1027 DPRINT_ERR(NETVSC, "Unknown nvsp packet type received - %d",
1028 nvspPacket->Header.MessageType);
Hank Janssenfceaf242009-07-13 15:34:54 -07001029 PutNetDevice(Device);
1030 return;
1031 }
1032
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001033 DPRINT_DBG(NETVSC, "NVSP packet received - type %d",
1034 nvspPacket->Header.MessageType);
Hank Janssenfceaf242009-07-13 15:34:54 -07001035
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -07001036 vmxferpagePacket = (struct vmtransfer_page_packet_header *)Packet;
Hank Janssenfceaf242009-07-13 15:34:54 -07001037
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001038 if (vmxferpagePacket->TransferPageSetId != NETVSC_RECEIVE_BUFFER_ID) {
1039 DPRINT_ERR(NETVSC, "Invalid xfer page set id - "
1040 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID,
1041 vmxferpagePacket->TransferPageSetId);
Hank Janssenfceaf242009-07-13 15:34:54 -07001042 PutNetDevice(Device);
1043 return;
1044 }
1045
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001046 DPRINT_DBG(NETVSC, "xfer page - range count %d",
1047 vmxferpagePacket->RangeCount);
Hank Janssenfceaf242009-07-13 15:34:54 -07001048
Bill Pemberton454f18a2009-07-27 16:47:24 -04001049 /*
1050 * Grab free packets (range count + 1) to represent this xfer
1051 * page packet. +1 to represent the xfer page packet itself.
1052 * We grab it here so that we know exactly how many we can
1053 * fulfil
1054 */
Greg Kroah-Hartman64368732009-07-15 14:56:15 -07001055 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
Bill Pembertond29274e2009-09-11 21:46:43 -04001056 while (!list_empty(&netDevice->ReceivePacketList)) {
Milan Dadok92ec0892009-10-28 23:23:37 +01001057 list_move_tail(netDevice->ReceivePacketList.next, &listHead);
Hank Janssenfceaf242009-07-13 15:34:54 -07001058 if (++count == vmxferpagePacket->RangeCount + 1)
1059 break;
1060 }
Greg Kroah-Hartman64368732009-07-15 14:56:15 -07001061 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
Hank Janssenfceaf242009-07-13 15:34:54 -07001062
Bill Pemberton454f18a2009-07-27 16:47:24 -04001063 /*
1064 * We need at least 2 netvsc pkts (1 to represent the xfer
1065 * page and at least 1 for the range) i.e. we can handled
1066 * some of the xfer page packet ranges...
1067 */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001068 if (count < 2) {
1069 DPRINT_ERR(NETVSC, "Got only %d netvsc pkt...needed %d pkts. "
1070 "Dropping this xfer page packet completely!",
1071 count, vmxferpagePacket->RangeCount + 1);
Hank Janssenfceaf242009-07-13 15:34:54 -07001072
Bill Pemberton454f18a2009-07-27 16:47:24 -04001073 /* Return it to the freelist */
Greg Kroah-Hartman64368732009-07-15 14:56:15 -07001074 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001075 for (i = count; i != 0; i--) {
Milan Dadok92ec0892009-10-28 23:23:37 +01001076 list_move_tail(listHead.next,
Bill Pembertond29274e2009-09-11 21:46:43 -04001077 &netDevice->ReceivePacketList);
Hank Janssenfceaf242009-07-13 15:34:54 -07001078 }
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001079 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock,
1080 flags);
Hank Janssenfceaf242009-07-13 15:34:54 -07001081
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001082 NetVscSendReceiveCompletion(Device,
1083 vmxferpagePacket->d.TransactionId);
Hank Janssenfceaf242009-07-13 15:34:54 -07001084
1085 PutNetDevice(Device);
1086 return;
1087 }
1088
Bill Pemberton454f18a2009-07-27 16:47:24 -04001089 /* Remove the 1st packet to represent the xfer page packet itself */
Milan Dadok92ec0892009-10-28 23:23:37 +01001090 xferpagePacket = (struct xferpage_packet*)listHead.next;
Bill Pembertond29274e2009-09-11 21:46:43 -04001091 list_del(&xferpagePacket->ListEntry);
1092
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001093 /* This is how much we can satisfy */
1094 xferpagePacket->Count = count - 1;
1095 ASSERT(xferpagePacket->Count > 0 && xferpagePacket->Count <=
1096 vmxferpagePacket->RangeCount);
Hank Janssenfceaf242009-07-13 15:34:54 -07001097
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001098 if (xferpagePacket->Count != vmxferpagePacket->RangeCount) {
1099 DPRINT_INFO(NETVSC, "Needed %d netvsc pkts to satisy this xfer "
1100 "page...got %d", vmxferpagePacket->RangeCount,
1101 xferpagePacket->Count);
Hank Janssenfceaf242009-07-13 15:34:54 -07001102 }
1103
Bill Pemberton454f18a2009-07-27 16:47:24 -04001104 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001105 for (i = 0; i < (count - 1); i++) {
Milan Dadok92ec0892009-10-28 23:23:37 +01001106 netvscPacket = (struct hv_netvsc_packet*)listHead.next;
Bill Pembertond29274e2009-09-11 21:46:43 -04001107 list_del(&netvscPacket->ListEntry);
Hank Janssenfceaf242009-07-13 15:34:54 -07001108
Bill Pemberton454f18a2009-07-27 16:47:24 -04001109 /* Initialize the netvsc packet */
Hank Janssenfceaf242009-07-13 15:34:54 -07001110 netvscPacket->XferPagePacket = xferpagePacket;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001111 netvscPacket->Completion.Recv.OnReceiveCompletion =
1112 NetVscOnReceiveCompletion;
1113 netvscPacket->Completion.Recv.ReceiveCompletionContext =
1114 netvscPacket;
Hank Janssenfceaf242009-07-13 15:34:54 -07001115 netvscPacket->Device = Device;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001116 /* Save this so that we can send it back */
1117 netvscPacket->Completion.Recv.ReceiveCompletionTid =
1118 vmxferpagePacket->d.TransactionId;
Hank Janssenfceaf242009-07-13 15:34:54 -07001119
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001120 netvscPacket->TotalDataBufferLength =
1121 vmxferpagePacket->Ranges[i].ByteCount;
Hank Janssenfceaf242009-07-13 15:34:54 -07001122 netvscPacket->PageBufferCount = 1;
1123
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001124 ASSERT(vmxferpagePacket->Ranges[i].ByteOffset +
1125 vmxferpagePacket->Ranges[i].ByteCount <
1126 netDevice->ReceiveBufferSize);
Hank Janssenfceaf242009-07-13 15:34:54 -07001127
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001128 netvscPacket->PageBuffers[0].Length =
1129 vmxferpagePacket->Ranges[i].ByteCount;
Hank Janssenfceaf242009-07-13 15:34:54 -07001130
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001131 start = virt_to_phys((void *)((unsigned long)netDevice->ReceiveBuffer + vmxferpagePacket->Ranges[i].ByteOffset));
Hank Janssenfceaf242009-07-13 15:34:54 -07001132
1133 netvscPacket->PageBuffers[0].Pfn = start >> PAGE_SHIFT;
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -07001134 endVirtual = (unsigned long)netDevice->ReceiveBuffer
Hank Janssenfceaf242009-07-13 15:34:54 -07001135 + vmxferpagePacket->Ranges[i].ByteOffset
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001136 + vmxferpagePacket->Ranges[i].ByteCount - 1;
1137 end = virt_to_phys((void *)endVirtual);
Hank Janssenfceaf242009-07-13 15:34:54 -07001138
Bill Pemberton454f18a2009-07-27 16:47:24 -04001139 /* Calculate the page relative offset */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001140 netvscPacket->PageBuffers[0].Offset =
1141 vmxferpagePacket->Ranges[i].ByteOffset & (PAGE_SIZE - 1);
1142 if ((end >> PAGE_SHIFT) != (start >> PAGE_SHIFT)) {
1143 /* Handle frame across multiple pages: */
1144 netvscPacket->PageBuffers[0].Length =
1145 (netvscPacket->PageBuffers[0].Pfn << PAGE_SHIFT)
1146 + PAGE_SIZE - start;
1147 bytesRemain = netvscPacket->TotalDataBufferLength -
1148 netvscPacket->PageBuffers[0].Length;
1149 for (j = 1; j < NETVSC_PACKET_MAXPAGE; j++) {
1150 netvscPacket->PageBuffers[j].Offset = 0;
1151 if (bytesRemain <= PAGE_SIZE) {
1152 netvscPacket->PageBuffers[j].Length = bytesRemain;
1153 bytesRemain = 0;
1154 } else {
1155 netvscPacket->PageBuffers[j].Length = PAGE_SIZE;
1156 bytesRemain -= PAGE_SIZE;
1157 }
1158 netvscPacket->PageBuffers[j].Pfn =
1159 virt_to_phys((void *)(endVirtual - bytesRemain)) >> PAGE_SHIFT;
1160 netvscPacket->PageBufferCount++;
1161 if (bytesRemain == 0)
1162 break;
Hank Janssenfceaf242009-07-13 15:34:54 -07001163 }
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001164 ASSERT(bytesRemain == 0);
Hank Janssenfceaf242009-07-13 15:34:54 -07001165 }
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001166 DPRINT_DBG(NETVSC, "[%d] - (abs offset %u len %u) => "
1167 "(pfn %llx, offset %u, len %u)", i,
1168 vmxferpagePacket->Ranges[i].ByteOffset,
1169 vmxferpagePacket->Ranges[i].ByteCount,
1170 netvscPacket->PageBuffers[0].Pfn,
1171 netvscPacket->PageBuffers[0].Offset,
1172 netvscPacket->PageBuffers[0].Length);
Hank Janssenfceaf242009-07-13 15:34:54 -07001173
Bill Pemberton454f18a2009-07-27 16:47:24 -04001174 /* Pass it to the upper layer */
Greg Kroah-Hartman7e23a6e2009-08-27 15:58:15 -07001175 ((struct netvsc_driver *)Device->Driver)->OnReceiveCallback(Device, netvscPacket);
Hank Janssenfceaf242009-07-13 15:34:54 -07001176
1177 NetVscOnReceiveCompletion(netvscPacket->Completion.Recv.ReceiveCompletionContext);
1178 }
1179
Bill Pembertond29274e2009-09-11 21:46:43 -04001180 ASSERT(list_empty(&listHead));
Hank Janssenfceaf242009-07-13 15:34:54 -07001181
1182 PutNetDevice(Device);
1183 DPRINT_EXIT(NETVSC);
1184}
1185
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001186static void NetVscSendReceiveCompletion(struct hv_device *Device,
1187 u64 TransactionId)
Hank Janssenfceaf242009-07-13 15:34:54 -07001188{
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -07001189 struct nvsp_message recvcompMessage;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001190 int retries = 0;
1191 int ret;
Hank Janssenfceaf242009-07-13 15:34:54 -07001192
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001193 DPRINT_DBG(NETVSC, "Sending receive completion pkt - %llx",
1194 TransactionId);
Hank Janssenfceaf242009-07-13 15:34:54 -07001195
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001196 recvcompMessage.Header.MessageType =
1197 NvspMessage1TypeSendRNDISPacketComplete;
Hank Janssenfceaf242009-07-13 15:34:54 -07001198
Bill Pemberton454f18a2009-07-27 16:47:24 -04001199 /* FIXME: Pass in the status */
Hank Janssenfceaf242009-07-13 15:34:54 -07001200 recvcompMessage.Messages.Version1Messages.SendRNDISPacketComplete.Status = NvspStatusSuccess;
1201
1202retry_send_cmplt:
Bill Pemberton454f18a2009-07-27 16:47:24 -04001203 /* Send the completion */
Hank Janssenfceaf242009-07-13 15:34:54 -07001204 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001205 &recvcompMessage,
1206 sizeof(struct nvsp_message),
1207 TransactionId,
1208 VmbusPacketTypeCompletion, 0);
1209 if (ret == 0) {
1210 /* success */
Bill Pemberton454f18a2009-07-27 16:47:24 -04001211 /* no-op */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001212 } else if (ret == -1) {
1213 /* no more room...wait a bit and attempt to retry 3 times */
Hank Janssenfceaf242009-07-13 15:34:54 -07001214 retries++;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001215 DPRINT_ERR(NETVSC, "unable to send receive completion pkt "
1216 "(tid %llx)...retrying %d", TransactionId, retries);
Hank Janssenfceaf242009-07-13 15:34:54 -07001217
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001218 if (retries < 4) {
Greg Kroah-Hartmanb4362c92009-07-16 11:50:41 -07001219 udelay(100);
Hank Janssenfceaf242009-07-13 15:34:54 -07001220 goto retry_send_cmplt;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001221 } else {
1222 DPRINT_ERR(NETVSC, "unable to send receive completion "
1223 "pkt (tid %llx)...give up retrying",
1224 TransactionId);
Hank Janssenfceaf242009-07-13 15:34:54 -07001225 }
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001226 } else {
1227 DPRINT_ERR(NETVSC, "unable to send receive completion pkt - "
1228 "%llx", TransactionId);
Hank Janssenfceaf242009-07-13 15:34:54 -07001229 }
1230}
1231
Bill Pemberton454f18a2009-07-27 16:47:24 -04001232/* Send a receive completion packet to RNDIS device (ie NetVsp) */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001233static void NetVscOnReceiveCompletion(void *Context)
Hank Janssenfceaf242009-07-13 15:34:54 -07001234{
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001235 struct hv_netvsc_packet *packet = Context;
1236 struct hv_device *device = (struct hv_device *)packet->Device;
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -07001237 struct netvsc_device *netDevice;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001238 u64 transactionId = 0;
Greg Kroah-Hartman0e727612009-07-15 12:46:44 -07001239 bool fSendReceiveComp = false;
Greg Kroah-Hartman64368732009-07-15 14:56:15 -07001240 unsigned long flags;
Hank Janssenfceaf242009-07-13 15:34:54 -07001241
1242 DPRINT_ENTER(NETVSC);
1243
1244 ASSERT(packet->XferPagePacket);
1245
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001246 /*
1247 * Even though it seems logical to do a GetOutboundNetDevice() here to
1248 * send out receive completion, we are using GetInboundNetDevice()
1249 * since we may have disable outbound traffic already.
1250 */
Hank Janssenfceaf242009-07-13 15:34:54 -07001251 netDevice = GetInboundNetDevice(device);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001252 if (!netDevice) {
1253 DPRINT_ERR(NETVSC, "unable to get net device..."
1254 "device being destroyed?");
Hank Janssenfceaf242009-07-13 15:34:54 -07001255 DPRINT_EXIT(NETVSC);
1256 return;
1257 }
1258
Bill Pemberton454f18a2009-07-27 16:47:24 -04001259 /* Overloading use of the lock. */
Greg Kroah-Hartman64368732009-07-15 14:56:15 -07001260 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
Hank Janssenfceaf242009-07-13 15:34:54 -07001261
1262 ASSERT(packet->XferPagePacket->Count > 0);
1263 packet->XferPagePacket->Count--;
1264
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001265 /*
1266 * Last one in the line that represent 1 xfer page packet.
1267 * Return the xfer page packet itself to the freelist
1268 */
1269 if (packet->XferPagePacket->Count == 0) {
Greg Kroah-Hartman0e727612009-07-15 12:46:44 -07001270 fSendReceiveComp = true;
Hank Janssenfceaf242009-07-13 15:34:54 -07001271 transactionId = packet->Completion.Recv.ReceiveCompletionTid;
Bill Pembertond29274e2009-09-11 21:46:43 -04001272 list_add_tail(&packet->XferPagePacket->ListEntry,
1273 &netDevice->ReceivePacketList);
Hank Janssenfceaf242009-07-13 15:34:54 -07001274
Hank Janssenfceaf242009-07-13 15:34:54 -07001275 }
1276
Bill Pemberton454f18a2009-07-27 16:47:24 -04001277 /* Put the packet back */
Bill Pembertond29274e2009-09-11 21:46:43 -04001278 list_add_tail(&packet->ListEntry, &netDevice->ReceivePacketList);
Greg Kroah-Hartman64368732009-07-15 14:56:15 -07001279 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
Hank Janssenfceaf242009-07-13 15:34:54 -07001280
Bill Pemberton454f18a2009-07-27 16:47:24 -04001281 /* Send a receive completion for the xfer page packet */
Hank Janssenfceaf242009-07-13 15:34:54 -07001282 if (fSendReceiveComp)
Hank Janssenfceaf242009-07-13 15:34:54 -07001283 NetVscSendReceiveCompletion(device, transactionId);
Hank Janssenfceaf242009-07-13 15:34:54 -07001284
1285 PutNetDevice(device);
1286 DPRINT_EXIT(NETVSC);
1287}
1288
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001289void NetVscOnChannelCallback(void *Context)
Hank Janssenfceaf242009-07-13 15:34:54 -07001290{
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001291 const int netPacketSize = 2048;
1292 int ret;
1293 struct hv_device *device = Context;
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -07001294 struct netvsc_device *netDevice;
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -07001295 u32 bytesRecvd;
Greg Kroah-Hartman59471432009-07-14 15:10:26 -07001296 u64 requestId;
1297 unsigned char packet[netPacketSize];
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -07001298 struct vmpacket_descriptor *desc;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001299 unsigned char *buffer = packet;
1300 int bufferlen = netPacketSize;
Hank Janssenfceaf242009-07-13 15:34:54 -07001301
1302
1303 DPRINT_ENTER(NETVSC);
1304
1305 ASSERT(device);
1306
1307 netDevice = GetInboundNetDevice(device);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001308 if (!netDevice) {
1309 DPRINT_ERR(NETVSC, "net device (%p) shutting down..."
1310 "ignoring inbound packets", netDevice);
Hank Janssenfceaf242009-07-13 15:34:54 -07001311 DPRINT_EXIT(NETVSC);
1312 return;
1313 }
1314
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001315 do {
1316 ret = device->Driver->VmbusChannelInterface.RecvPacketRaw(
1317 device, buffer, bufferlen,
1318 &bytesRecvd, &requestId);
1319 if (ret == 0) {
1320 if (bytesRecvd > 0) {
1321 DPRINT_DBG(NETVSC, "receive %d bytes, tid %llx",
1322 bytesRecvd, requestId);
Hank Janssenfceaf242009-07-13 15:34:54 -07001323
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001324 desc = (struct vmpacket_descriptor *)buffer;
1325 switch (desc->Type) {
1326 case VmbusPacketTypeCompletion:
1327 NetVscOnSendCompletion(device, desc);
1328 break;
Hank Janssenfceaf242009-07-13 15:34:54 -07001329
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001330 case VmbusPacketTypeDataUsingTransferPages:
1331 NetVscOnReceive(device, desc);
1332 break;
Hank Janssenfceaf242009-07-13 15:34:54 -07001333
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001334 default:
1335 DPRINT_ERR(NETVSC,
1336 "unhandled packet type %d, "
1337 "tid %llx len %d\n",
1338 desc->Type, requestId,
1339 bytesRecvd);
1340 break;
Hank Janssenfceaf242009-07-13 15:34:54 -07001341 }
1342
Bill Pemberton454f18a2009-07-27 16:47:24 -04001343 /* reset */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001344 if (bufferlen > netPacketSize) {
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -07001345 kfree(buffer);
Hank Janssenfceaf242009-07-13 15:34:54 -07001346 buffer = packet;
1347 bufferlen = netPacketSize;
1348 }
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001349 } else {
Bill Pemberton454f18a2009-07-27 16:47:24 -04001350 /* reset */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001351 if (bufferlen > netPacketSize) {
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -07001352 kfree(buffer);
Hank Janssenfceaf242009-07-13 15:34:54 -07001353 buffer = packet;
1354 bufferlen = netPacketSize;
1355 }
1356
1357 break;
1358 }
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001359 } else if (ret == -2) {
1360 /* Handle large packet */
Greg Kroah-Hartman0a72f3c2009-07-15 12:48:01 -07001361 buffer = kmalloc(bytesRecvd, GFP_ATOMIC);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001362 if (buffer == NULL) {
Bill Pemberton454f18a2009-07-27 16:47:24 -04001363 /* Try again next time around */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001364 DPRINT_ERR(NETVSC,
1365 "unable to allocate buffer of size "
1366 "(%d)!!", bytesRecvd);
Hank Janssenfceaf242009-07-13 15:34:54 -07001367 break;
1368 }
1369
1370 bufferlen = bytesRecvd;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001371 } else {
Hank Janssenfceaf242009-07-13 15:34:54 -07001372 ASSERT(0);
1373 }
1374 } while (1);
1375
1376 PutNetDevice(device);
1377 DPRINT_EXIT(NETVSC);
1378 return;
1379}