blob: 1610b845198f79f2c71cbd6f666613cdebddd29d [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:
18 * Hank Janssen <hjanssen@microsoft.com>
Hank Janssenfceaf242009-07-13 15:34:54 -070019 */
Greg Kroah-Hartman5654e932009-07-14 15:08:20 -070020#include <linux/kernel.h>
Greg Kroah-Hartman0ffa63b2009-07-15 11:06:01 -070021#include <linux/mm.h>
Greg Kroah-Hartmanb4362c92009-07-16 11:50:41 -070022#include <linux/delay.h>
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070023#include <linux/io.h>
Greg Kroah-Hartman4983b392009-08-19 16:14:47 -070024#include "osd.h"
Greg Kroah-Hartman645954c2009-08-28 16:22:59 -070025#include "logging.h"
Hank Janssenfceaf242009-07-13 15:34:54 -070026#include "NetVsc.h"
27#include "RndisFilter.h"
28
29
Bill Pemberton454f18a2009-07-27 16:47:24 -040030/* Globals */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070031static const char *gDriverName = "netvsc";
Hank Janssenfceaf242009-07-13 15:34:54 -070032
Bill Pemberton454f18a2009-07-27 16:47:24 -040033/* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
Greg Kroah-Hartmancaf26a32009-08-19 16:17:03 -070034static const struct hv_guid gNetVscDeviceType = {
35 .data = {
36 0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
37 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E
38 }
Hank Janssenfceaf242009-07-13 15:34:54 -070039};
40
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070041static int NetVscOnDeviceAdd(struct hv_device *Device, void *AdditionalInfo);
Hank Janssenfceaf242009-07-13 15:34:54 -070042
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070043static int NetVscOnDeviceRemove(struct hv_device *Device);
Hank Janssenfceaf242009-07-13 15:34:54 -070044
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070045static void NetVscOnCleanup(struct hv_driver *Driver);
Hank Janssenfceaf242009-07-13 15:34:54 -070046
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070047static void NetVscOnChannelCallback(void *context);
Hank Janssenfceaf242009-07-13 15:34:54 -070048
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070049static int NetVscInitializeSendBufferWithNetVsp(struct hv_device *Device);
Hank Janssenfceaf242009-07-13 15:34:54 -070050
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070051static int NetVscInitializeReceiveBufferWithNetVsp(struct hv_device *Device);
Hank Janssenfceaf242009-07-13 15:34:54 -070052
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -070053static int NetVscDestroySendBuffer(struct netvsc_device *NetDevice);
Hank Janssenfceaf242009-07-13 15:34:54 -070054
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -070055static int NetVscDestroyReceiveBuffer(struct netvsc_device *NetDevice);
Hank Janssenfceaf242009-07-13 15:34:54 -070056
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070057static int NetVscConnectToVsp(struct hv_device *Device);
Hank Janssenfceaf242009-07-13 15:34:54 -070058
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070059static void NetVscOnSendCompletion(struct hv_device *Device,
60 struct vmpacket_descriptor *Packet);
Hank Janssenfceaf242009-07-13 15:34:54 -070061
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070062static int NetVscOnSend(struct hv_device *Device,
63 struct hv_netvsc_packet *Packet);
Hank Janssenfceaf242009-07-13 15:34:54 -070064
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070065static void NetVscOnReceive(struct hv_device *Device,
66 struct vmpacket_descriptor *Packet);
Hank Janssenfceaf242009-07-13 15:34:54 -070067
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070068static void NetVscOnReceiveCompletion(void *Context);
Hank Janssenfceaf242009-07-13 15:34:54 -070069
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070070static void NetVscSendReceiveCompletion(struct hv_device *Device,
71 u64 TransactionId);
Hank Janssenfceaf242009-07-13 15:34:54 -070072
Hank Janssenfceaf242009-07-13 15:34:54 -070073
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -070074static struct netvsc_device *AllocNetDevice(struct hv_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -070075{
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -070076 struct netvsc_device *netDevice;
Hank Janssenfceaf242009-07-13 15:34:54 -070077
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -070078 netDevice = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
Hank Janssenfceaf242009-07-13 15:34:54 -070079 if (!netDevice)
80 return NULL;
81
Bill Pemberton454f18a2009-07-27 16:47:24 -040082 /* Set to 2 to allow both inbound and outbound traffic */
Bill Pembertonf4888412009-07-29 17:00:12 -040083 atomic_cmpxchg(&netDevice->RefCount, 0, 2);
Hank Janssenfceaf242009-07-13 15:34:54 -070084
85 netDevice->Device = Device;
86 Device->Extension = netDevice;
87
88 return netDevice;
89}
90
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -070091static void FreeNetDevice(struct netvsc_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -070092{
Bill Pembertonf4888412009-07-29 17:00:12 -040093 ASSERT(atomic_read(&Device->RefCount) == 0);
Hank Janssenfceaf242009-07-13 15:34:54 -070094 Device->Device->Extension = NULL;
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -070095 kfree(Device);
Hank Janssenfceaf242009-07-13 15:34:54 -070096}
97
98
Bill Pemberton454f18a2009-07-27 16:47:24 -040099/* Get the net device object iff exists and its refcount > 1 */
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700100static struct netvsc_device *GetOutboundNetDevice(struct hv_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700101{
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700102 struct netvsc_device *netDevice;
Hank Janssenfceaf242009-07-13 15:34:54 -0700103
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700104 netDevice = Device->Extension;
Bill Pembertonf4888412009-07-29 17:00:12 -0400105 if (netDevice && atomic_read(&netDevice->RefCount) > 1)
106 atomic_inc(&netDevice->RefCount);
Hank Janssenfceaf242009-07-13 15:34:54 -0700107 else
Hank Janssenfceaf242009-07-13 15:34:54 -0700108 netDevice = NULL;
Hank Janssenfceaf242009-07-13 15:34:54 -0700109
110 return netDevice;
111}
112
Bill Pemberton454f18a2009-07-27 16:47:24 -0400113/* Get the net device object iff exists and its refcount > 0 */
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700114static struct netvsc_device *GetInboundNetDevice(struct hv_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700115{
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700116 struct netvsc_device *netDevice;
Hank Janssenfceaf242009-07-13 15:34:54 -0700117
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700118 netDevice = Device->Extension;
Bill Pembertonf4888412009-07-29 17:00:12 -0400119 if (netDevice && atomic_read(&netDevice->RefCount))
120 atomic_inc(&netDevice->RefCount);
Hank Janssenfceaf242009-07-13 15:34:54 -0700121 else
Hank Janssenfceaf242009-07-13 15:34:54 -0700122 netDevice = NULL;
Hank Janssenfceaf242009-07-13 15:34:54 -0700123
124 return netDevice;
125}
126
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700127static void PutNetDevice(struct hv_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700128{
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700129 struct netvsc_device *netDevice;
Hank Janssenfceaf242009-07-13 15:34:54 -0700130
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700131 netDevice = Device->Extension;
Hank Janssenfceaf242009-07-13 15:34:54 -0700132 ASSERT(netDevice);
133
Bill Pembertonf4888412009-07-29 17:00:12 -0400134 atomic_dec(&netDevice->RefCount);
Hank Janssenfceaf242009-07-13 15:34:54 -0700135}
136
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700137static struct netvsc_device *ReleaseOutboundNetDevice(struct hv_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700138{
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700139 struct netvsc_device *netDevice;
Hank Janssenfceaf242009-07-13 15:34:54 -0700140
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700141 netDevice = Device->Extension;
Hank Janssenfceaf242009-07-13 15:34:54 -0700142 if (netDevice == NULL)
143 return NULL;
144
Bill Pemberton454f18a2009-07-27 16:47:24 -0400145 /* Busy wait until the ref drop to 2, then set it to 1 */
Bill Pembertonf4888412009-07-29 17:00:12 -0400146 while (atomic_cmpxchg(&netDevice->RefCount, 2, 1) != 2)
Greg Kroah-Hartmanb4362c92009-07-16 11:50:41 -0700147 udelay(100);
Hank Janssenfceaf242009-07-13 15:34:54 -0700148
149 return netDevice;
150}
151
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700152static struct netvsc_device *ReleaseInboundNetDevice(struct hv_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700153{
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700154 struct netvsc_device *netDevice;
Hank Janssenfceaf242009-07-13 15:34:54 -0700155
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700156 netDevice = Device->Extension;
Hank Janssenfceaf242009-07-13 15:34:54 -0700157 if (netDevice == NULL)
158 return NULL;
159
Bill Pemberton454f18a2009-07-27 16:47:24 -0400160 /* Busy wait until the ref drop to 1, then set it to 0 */
Bill Pembertonf4888412009-07-29 17:00:12 -0400161 while (atomic_cmpxchg(&netDevice->RefCount, 1, 0) != 1)
Greg Kroah-Hartmanb4362c92009-07-16 11:50:41 -0700162 udelay(100);
Hank Janssenfceaf242009-07-13 15:34:54 -0700163
164 Device->Extension = NULL;
165 return netDevice;
166}
167
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700168/**
169 * NetVscInitialize - Main entry point
170 */
171int NetVscInitialize(struct hv_driver *drv)
Hank Janssenfceaf242009-07-13 15:34:54 -0700172{
Greg Kroah-Hartman7e23a6e2009-08-27 15:58:15 -0700173 struct netvsc_driver *driver = (struct netvsc_driver *)drv;
Hank Janssenfceaf242009-07-13 15:34:54 -0700174
175 DPRINT_ENTER(NETVSC);
176
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700177 DPRINT_DBG(NETVSC, "sizeof(struct hv_netvsc_packet)=%zd, "
178 "sizeof(struct nvsp_message)=%zd, "
179 "sizeof(struct vmtransfer_page_packet_header)=%zd",
180 sizeof(struct hv_netvsc_packet),
181 sizeof(struct nvsp_message),
182 sizeof(struct vmtransfer_page_packet_header));
Hank Janssenfceaf242009-07-13 15:34:54 -0700183
Bill Pemberton454f18a2009-07-27 16:47:24 -0400184 /* Make sure we are at least 2 pages since 1 page is used for control */
Hank Janssenfceaf242009-07-13 15:34:54 -0700185 ASSERT(driver->RingBufferSize >= (PAGE_SIZE << 1));
186
187 drv->name = gDriverName;
Greg Kroah-Hartmancaf26a32009-08-19 16:17:03 -0700188 memcpy(&drv->deviceType, &gNetVscDeviceType, sizeof(struct hv_guid));
Hank Janssenfceaf242009-07-13 15:34:54 -0700189
Bill Pemberton454f18a2009-07-27 16:47:24 -0400190 /* Make sure it is set by the caller */
Hank Janssenfceaf242009-07-13 15:34:54 -0700191 ASSERT(driver->OnReceiveCallback);
192 ASSERT(driver->OnLinkStatusChanged);
193
Bill Pemberton454f18a2009-07-27 16:47:24 -0400194 /* Setup the dispatch table */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700195 driver->Base.OnDeviceAdd = NetVscOnDeviceAdd;
196 driver->Base.OnDeviceRemove = NetVscOnDeviceRemove;
197 driver->Base.OnCleanup = NetVscOnCleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700198
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700199 driver->OnSend = NetVscOnSend;
Hank Janssenfceaf242009-07-13 15:34:54 -0700200
201 RndisFilterInit(driver);
202
203 DPRINT_EXIT(NETVSC);
204
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700205 return 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700206}
207
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700208static int NetVscInitializeReceiveBufferWithNetVsp(struct hv_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700209{
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700210 int ret = 0;
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700211 struct netvsc_device *netDevice;
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700212 struct nvsp_message *initPacket;
Hank Janssenfceaf242009-07-13 15:34:54 -0700213
214 DPRINT_ENTER(NETVSC);
215
216 netDevice = GetOutboundNetDevice(Device);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700217 if (!netDevice) {
218 DPRINT_ERR(NETVSC, "unable to get net device..."
219 "device being destroyed?");
Hank Janssenfceaf242009-07-13 15:34:54 -0700220 DPRINT_EXIT(NETVSC);
221 return -1;
222 }
223 ASSERT(netDevice->ReceiveBufferSize > 0);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700224 /* page-size grandularity */
225 ASSERT((netDevice->ReceiveBufferSize & (PAGE_SIZE - 1)) == 0);
Hank Janssenfceaf242009-07-13 15:34:54 -0700226
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700227 netDevice->ReceiveBuffer =
228 osd_PageAlloc(netDevice->ReceiveBufferSize >> PAGE_SHIFT);
229 if (!netDevice->ReceiveBuffer) {
230 DPRINT_ERR(NETVSC,
231 "unable to allocate receive buffer of size %d",
232 netDevice->ReceiveBufferSize);
Hank Janssenfceaf242009-07-13 15:34:54 -0700233 ret = -1;
234 goto Cleanup;
235 }
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700236 /* page-aligned buffer */
237 ASSERT(((unsigned long)netDevice->ReceiveBuffer & (PAGE_SIZE - 1)) ==
238 0);
Hank Janssenfceaf242009-07-13 15:34:54 -0700239
240 DPRINT_INFO(NETVSC, "Establishing receive buffer's GPADL...");
241
Bill Pemberton454f18a2009-07-27 16:47:24 -0400242 /*
243 * Establish the gpadl handle for this buffer on this
244 * channel. Note: This call uses the vmbus connection rather
245 * than the channel to establish the gpadl handle.
246 */
Hank Janssenfceaf242009-07-13 15:34:54 -0700247 ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700248 netDevice->ReceiveBuffer,
249 netDevice->ReceiveBufferSize,
250 &netDevice->ReceiveBufferGpadlHandle);
251 if (ret != 0) {
252 DPRINT_ERR(NETVSC,
253 "unable to establish receive buffer's gpadl");
Hank Janssenfceaf242009-07-13 15:34:54 -0700254 goto Cleanup;
255 }
256
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700257 /* osd_WaitEventWait(ext->ChannelInitEvent); */
Hank Janssenfceaf242009-07-13 15:34:54 -0700258
Bill Pemberton454f18a2009-07-27 16:47:24 -0400259 /* Notify the NetVsp of the gpadl handle */
Hank Janssenfceaf242009-07-13 15:34:54 -0700260 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendReceiveBuffer...");
261
262 initPacket = &netDevice->ChannelInitPacket;
263
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700264 memset(initPacket, 0, sizeof(struct nvsp_message));
Hank Janssenfceaf242009-07-13 15:34:54 -0700265
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700266 initPacket->Header.MessageType = NvspMessage1TypeSendReceiveBuffer;
267 initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->ReceiveBufferGpadlHandle;
268 initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
Hank Janssenfceaf242009-07-13 15:34:54 -0700269
Bill Pemberton454f18a2009-07-27 16:47:24 -0400270 /* Send the gpadl notification request */
Hank Janssenfceaf242009-07-13 15:34:54 -0700271 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700272 initPacket,
273 sizeof(struct nvsp_message),
274 (unsigned long)initPacket,
275 VmbusPacketTypeDataInBand,
276 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
277 if (ret != 0) {
278 DPRINT_ERR(NETVSC,
279 "unable to send receive buffer's gpadl to netvsp");
Hank Janssenfceaf242009-07-13 15:34:54 -0700280 goto Cleanup;
281 }
282
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700283 osd_WaitEventWait(netDevice->ChannelInitEvent);
Hank Janssenfceaf242009-07-13 15:34:54 -0700284
Bill Pemberton454f18a2009-07-27 16:47:24 -0400285 /* Check the response */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700286 if (initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Status != NvspStatusSuccess) {
287 DPRINT_ERR(NETVSC, "Unable to complete receive buffer "
288 "initialzation with NetVsp - status %d",
289 initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Status);
Hank Janssenfceaf242009-07-13 15:34:54 -0700290 ret = -1;
291 goto Cleanup;
292 }
293
Bill Pemberton454f18a2009-07-27 16:47:24 -0400294 /* Parse the response */
Hank Janssenfceaf242009-07-13 15:34:54 -0700295 ASSERT(netDevice->ReceiveSectionCount == 0);
296 ASSERT(netDevice->ReceiveSections == NULL);
297
298 netDevice->ReceiveSectionCount = initPacket->Messages.Version1Messages.SendReceiveBufferComplete.NumSections;
299
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700300 netDevice->ReceiveSections = kmalloc(netDevice->ReceiveSectionCount * sizeof(struct nvsp_1_receive_buffer_section), GFP_KERNEL);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700301 if (netDevice->ReceiveSections == NULL) {
Hank Janssenfceaf242009-07-13 15:34:54 -0700302 ret = -1;
303 goto Cleanup;
304 }
305
306 memcpy(netDevice->ReceiveSections,
307 initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Sections,
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700308 netDevice->ReceiveSectionCount * sizeof(struct nvsp_1_receive_buffer_section));
Hank Janssenfceaf242009-07-13 15:34:54 -0700309
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700310 DPRINT_INFO(NETVSC, "Receive sections info (count %d, offset %d, "
311 "endoffset %d, suballoc size %d, num suballocs %d)",
312 netDevice->ReceiveSectionCount,
313 netDevice->ReceiveSections[0].Offset,
314 netDevice->ReceiveSections[0].EndOffset,
315 netDevice->ReceiveSections[0].SubAllocationSize,
316 netDevice->ReceiveSections[0].NumSubAllocations);
Hank Janssenfceaf242009-07-13 15:34:54 -0700317
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700318 /*
319 * For 1st release, there should only be 1 section that represents the
320 * entire receive buffer
321 */
Hank Janssenfceaf242009-07-13 15:34:54 -0700322 if (netDevice->ReceiveSectionCount != 1 ||
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700323 netDevice->ReceiveSections->Offset != 0) {
Hank Janssenfceaf242009-07-13 15:34:54 -0700324 ret = -1;
325 goto Cleanup;
326 }
327
328 goto Exit;
329
330Cleanup:
331 NetVscDestroyReceiveBuffer(netDevice);
332
333Exit:
334 PutNetDevice(Device);
335 DPRINT_EXIT(NETVSC);
336 return ret;
337}
338
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700339static int NetVscInitializeSendBufferWithNetVsp(struct hv_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700340{
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700341 int ret = 0;
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700342 struct netvsc_device *netDevice;
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700343 struct nvsp_message *initPacket;
Hank Janssenfceaf242009-07-13 15:34:54 -0700344
345 DPRINT_ENTER(NETVSC);
346
347 netDevice = GetOutboundNetDevice(Device);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700348 if (!netDevice) {
349 DPRINT_ERR(NETVSC, "unable to get net device..."
350 "device being destroyed?");
Hank Janssenfceaf242009-07-13 15:34:54 -0700351 DPRINT_EXIT(NETVSC);
352 return -1;
353 }
354 ASSERT(netDevice->SendBufferSize > 0);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700355 /* page-size grandularity */
356 ASSERT((netDevice->SendBufferSize & (PAGE_SIZE - 1)) == 0);
Hank Janssenfceaf242009-07-13 15:34:54 -0700357
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700358 netDevice->SendBuffer =
359 osd_PageAlloc(netDevice->SendBufferSize >> PAGE_SHIFT);
360 if (!netDevice->SendBuffer) {
361 DPRINT_ERR(NETVSC, "unable to allocate send buffer of size %d",
362 netDevice->SendBufferSize);
Hank Janssenfceaf242009-07-13 15:34:54 -0700363 ret = -1;
364 goto Cleanup;
365 }
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700366 /* page-aligned buffer */
367 ASSERT(((unsigned long)netDevice->SendBuffer & (PAGE_SIZE - 1)) == 0);
Hank Janssenfceaf242009-07-13 15:34:54 -0700368
369 DPRINT_INFO(NETVSC, "Establishing send buffer's GPADL...");
370
Bill Pemberton454f18a2009-07-27 16:47:24 -0400371 /*
372 * Establish the gpadl handle for this buffer on this
373 * channel. Note: This call uses the vmbus connection rather
374 * than the channel to establish the gpadl handle.
375 */
Hank Janssenfceaf242009-07-13 15:34:54 -0700376 ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700377 netDevice->SendBuffer,
378 netDevice->SendBufferSize,
379 &netDevice->SendBufferGpadlHandle);
380 if (ret != 0) {
Hank Janssenfceaf242009-07-13 15:34:54 -0700381 DPRINT_ERR(NETVSC, "unable to establish send buffer's gpadl");
382 goto Cleanup;
383 }
384
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700385 /* osd_WaitEventWait(ext->ChannelInitEvent); */
Hank Janssenfceaf242009-07-13 15:34:54 -0700386
Bill Pemberton454f18a2009-07-27 16:47:24 -0400387 /* Notify the NetVsp of the gpadl handle */
Hank Janssenfceaf242009-07-13 15:34:54 -0700388 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendSendBuffer...");
389
390 initPacket = &netDevice->ChannelInitPacket;
391
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700392 memset(initPacket, 0, sizeof(struct nvsp_message));
Hank Janssenfceaf242009-07-13 15:34:54 -0700393
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700394 initPacket->Header.MessageType = NvspMessage1TypeSendSendBuffer;
395 initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->SendBufferGpadlHandle;
396 initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_SEND_BUFFER_ID;
Hank Janssenfceaf242009-07-13 15:34:54 -0700397
Bill Pemberton454f18a2009-07-27 16:47:24 -0400398 /* Send the gpadl notification request */
Hank Janssenfceaf242009-07-13 15:34:54 -0700399 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700400 initPacket, sizeof(struct nvsp_message),
401 (unsigned long)initPacket,
402 VmbusPacketTypeDataInBand,
403 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
404 if (ret != 0) {
405 DPRINT_ERR(NETVSC,
406 "unable to send receive buffer's gpadl to netvsp");
Hank Janssenfceaf242009-07-13 15:34:54 -0700407 goto Cleanup;
408 }
409
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700410 osd_WaitEventWait(netDevice->ChannelInitEvent);
Hank Janssenfceaf242009-07-13 15:34:54 -0700411
Bill Pemberton454f18a2009-07-27 16:47:24 -0400412 /* Check the response */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700413 if (initPacket->Messages.Version1Messages.SendSendBufferComplete.Status != NvspStatusSuccess) {
414 DPRINT_ERR(NETVSC, "Unable to complete send buffer "
415 "initialzation with NetVsp - status %d",
416 initPacket->Messages.Version1Messages.SendSendBufferComplete.Status);
Hank Janssenfceaf242009-07-13 15:34:54 -0700417 ret = -1;
418 goto Cleanup;
419 }
420
421 netDevice->SendSectionSize = initPacket->Messages.Version1Messages.SendSendBufferComplete.SectionSize;
422
423 goto Exit;
424
425Cleanup:
426 NetVscDestroySendBuffer(netDevice);
427
428Exit:
429 PutNetDevice(Device);
430 DPRINT_EXIT(NETVSC);
431 return ret;
432}
433
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700434static int NetVscDestroyReceiveBuffer(struct netvsc_device *NetDevice)
Hank Janssenfceaf242009-07-13 15:34:54 -0700435{
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700436 struct nvsp_message *revokePacket;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700437 int ret = 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700438
439 DPRINT_ENTER(NETVSC);
440
Bill Pemberton454f18a2009-07-27 16:47:24 -0400441 /*
442 * If we got a section count, it means we received a
443 * SendReceiveBufferComplete msg (ie sent
444 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
445 * to send a revoke msg here
446 */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700447 if (NetDevice->ReceiveSectionCount) {
448 DPRINT_INFO(NETVSC,
449 "Sending NvspMessage1TypeRevokeReceiveBuffer...");
Hank Janssenfceaf242009-07-13 15:34:54 -0700450
Bill Pemberton454f18a2009-07-27 16:47:24 -0400451 /* Send the revoke receive buffer */
Hank Janssenfceaf242009-07-13 15:34:54 -0700452 revokePacket = &NetDevice->RevokePacket;
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700453 memset(revokePacket, 0, sizeof(struct nvsp_message));
Hank Janssenfceaf242009-07-13 15:34:54 -0700454
455 revokePacket->Header.MessageType = NvspMessage1TypeRevokeReceiveBuffer;
456 revokePacket->Messages.Version1Messages.RevokeReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
457
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700458 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(
459 NetDevice->Device,
460 revokePacket,
461 sizeof(struct nvsp_message),
462 (unsigned long)revokePacket,
463 VmbusPacketTypeDataInBand, 0);
Bill Pemberton454f18a2009-07-27 16:47:24 -0400464 /*
465 * If we failed here, we might as well return and
466 * have a leak rather than continue and a bugchk
467 */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700468 if (ret != 0) {
469 DPRINT_ERR(NETVSC, "unable to send revoke receive "
470 "buffer to netvsp");
Hank Janssenfceaf242009-07-13 15:34:54 -0700471 DPRINT_EXIT(NETVSC);
472 return -1;
473 }
474 }
475
Bill Pemberton454f18a2009-07-27 16:47:24 -0400476 /* Teardown the gpadl on the vsp end */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700477 if (NetDevice->ReceiveBufferGpadlHandle) {
Hank Janssenfceaf242009-07-13 15:34:54 -0700478 DPRINT_INFO(NETVSC, "Tearing down receive buffer's GPADL...");
479
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700480 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(
481 NetDevice->Device,
482 NetDevice->ReceiveBufferGpadlHandle);
Hank Janssenfceaf242009-07-13 15:34:54 -0700483
Bill Pemberton454f18a2009-07-27 16:47:24 -0400484 /* 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 -0700485 if (ret != 0) {
486 DPRINT_ERR(NETVSC,
487 "unable to teardown receive buffer's gpadl");
Hank Janssenfceaf242009-07-13 15:34:54 -0700488 DPRINT_EXIT(NETVSC);
489 return -1;
490 }
491 NetDevice->ReceiveBufferGpadlHandle = 0;
492 }
493
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700494 if (NetDevice->ReceiveBuffer) {
Hank Janssenfceaf242009-07-13 15:34:54 -0700495 DPRINT_INFO(NETVSC, "Freeing up receive buffer...");
496
Bill Pemberton454f18a2009-07-27 16:47:24 -0400497 /* Free up the receive buffer */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700498 osd_PageFree(NetDevice->ReceiveBuffer,
499 NetDevice->ReceiveBufferSize >> PAGE_SHIFT);
Hank Janssenfceaf242009-07-13 15:34:54 -0700500 NetDevice->ReceiveBuffer = NULL;
501 }
502
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700503 if (NetDevice->ReceiveSections) {
504 NetDevice->ReceiveSectionCount = 0;
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700505 kfree(NetDevice->ReceiveSections);
Hank Janssenfceaf242009-07-13 15:34:54 -0700506 NetDevice->ReceiveSections = NULL;
Hank Janssenfceaf242009-07-13 15:34:54 -0700507 }
508
509 DPRINT_EXIT(NETVSC);
510
511 return ret;
512}
513
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700514static int NetVscDestroySendBuffer(struct netvsc_device *NetDevice)
Hank Janssenfceaf242009-07-13 15:34:54 -0700515{
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700516 struct nvsp_message *revokePacket;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700517 int ret = 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700518
519 DPRINT_ENTER(NETVSC);
520
Bill Pemberton454f18a2009-07-27 16:47:24 -0400521 /*
522 * If we got a section count, it means we received a
523 * SendReceiveBufferComplete msg (ie sent
524 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
525 * to send a revoke msg here
526 */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700527 if (NetDevice->SendSectionSize) {
528 DPRINT_INFO(NETVSC,
529 "Sending NvspMessage1TypeRevokeSendBuffer...");
Hank Janssenfceaf242009-07-13 15:34:54 -0700530
Bill Pemberton454f18a2009-07-27 16:47:24 -0400531 /* Send the revoke send buffer */
Hank Janssenfceaf242009-07-13 15:34:54 -0700532 revokePacket = &NetDevice->RevokePacket;
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700533 memset(revokePacket, 0, sizeof(struct nvsp_message));
Hank Janssenfceaf242009-07-13 15:34:54 -0700534
535 revokePacket->Header.MessageType = NvspMessage1TypeRevokeSendBuffer;
536 revokePacket->Messages.Version1Messages.RevokeSendBuffer.Id = NETVSC_SEND_BUFFER_ID;
537
538 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(NetDevice->Device,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700539 revokePacket,
540 sizeof(struct nvsp_message),
541 (unsigned long)revokePacket,
542 VmbusPacketTypeDataInBand, 0);
543 /*
544 * If we failed here, we might as well return and have a leak
545 * rather than continue and a bugchk
546 */
547 if (ret != 0) {
548 DPRINT_ERR(NETVSC, "unable to send revoke send buffer "
549 "to netvsp");
Hank Janssenfceaf242009-07-13 15:34:54 -0700550 DPRINT_EXIT(NETVSC);
551 return -1;
552 }
553 }
554
Bill Pemberton454f18a2009-07-27 16:47:24 -0400555 /* Teardown the gpadl on the vsp end */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700556 if (NetDevice->SendBufferGpadlHandle) {
Hank Janssenfceaf242009-07-13 15:34:54 -0700557 DPRINT_INFO(NETVSC, "Tearing down send buffer's GPADL...");
558
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700559 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(NetDevice->Device, NetDevice->SendBufferGpadlHandle);
Hank Janssenfceaf242009-07-13 15:34:54 -0700560
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700561 /*
562 * If we failed here, we might as well return and have a leak
563 * rather than continue and a bugchk
564 */
565 if (ret != 0) {
566 DPRINT_ERR(NETVSC, "unable to teardown send buffer's "
567 "gpadl");
Hank Janssenfceaf242009-07-13 15:34:54 -0700568 DPRINT_EXIT(NETVSC);
569 return -1;
570 }
571 NetDevice->SendBufferGpadlHandle = 0;
572 }
573
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700574 if (NetDevice->SendBuffer) {
Hank Janssenfceaf242009-07-13 15:34:54 -0700575 DPRINT_INFO(NETVSC, "Freeing up send buffer...");
576
Bill Pemberton454f18a2009-07-27 16:47:24 -0400577 /* Free up the receive buffer */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700578 osd_PageFree(NetDevice->SendBuffer,
579 NetDevice->SendBufferSize >> PAGE_SHIFT);
Hank Janssenfceaf242009-07-13 15:34:54 -0700580 NetDevice->SendBuffer = NULL;
581 }
582
583 DPRINT_EXIT(NETVSC);
584
585 return ret;
586}
587
588
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700589static int NetVscConnectToVsp(struct hv_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700590{
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700591 int ret;
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700592 struct netvsc_device *netDevice;
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700593 struct nvsp_message *initPacket;
Hank Janssenfceaf242009-07-13 15:34:54 -0700594 int ndisVersion;
595
596 DPRINT_ENTER(NETVSC);
597
598 netDevice = GetOutboundNetDevice(Device);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700599 if (!netDevice) {
600 DPRINT_ERR(NETVSC, "unable to get net device..."
601 "device being destroyed?");
Hank Janssenfceaf242009-07-13 15:34:54 -0700602 DPRINT_EXIT(NETVSC);
603 return -1;
604 }
605
606 initPacket = &netDevice->ChannelInitPacket;
607
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700608 memset(initPacket, 0, sizeof(struct nvsp_message));
Hank Janssenfceaf242009-07-13 15:34:54 -0700609 initPacket->Header.MessageType = NvspMessageTypeInit;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700610 initPacket->Messages.InitMessages.Init.MinProtocolVersion = NVSP_MIN_PROTOCOL_VERSION;
611 initPacket->Messages.InitMessages.Init.MaxProtocolVersion = NVSP_MAX_PROTOCOL_VERSION;
Hank Janssenfceaf242009-07-13 15:34:54 -0700612
613 DPRINT_INFO(NETVSC, "Sending NvspMessageTypeInit...");
614
Bill Pemberton454f18a2009-07-27 16:47:24 -0400615 /* Send the init request */
Hank Janssenfceaf242009-07-13 15:34:54 -0700616 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700617 initPacket,
618 sizeof(struct nvsp_message),
619 (unsigned long)initPacket,
620 VmbusPacketTypeDataInBand,
621 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Hank Janssenfceaf242009-07-13 15:34:54 -0700622
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700623 if (ret != 0) {
Hank Janssenfceaf242009-07-13 15:34:54 -0700624 DPRINT_ERR(NETVSC, "unable to send NvspMessageTypeInit");
625 goto Cleanup;
626 }
627
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700628 osd_WaitEventWait(netDevice->ChannelInitEvent);
Hank Janssenfceaf242009-07-13 15:34:54 -0700629
Bill Pemberton454f18a2009-07-27 16:47:24 -0400630 /* Now, check the response */
631 /* ASSERT(initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength <= MAX_MULTIPAGE_BUFFER_COUNT); */
Hank Janssenfceaf242009-07-13 15:34:54 -0700632 DPRINT_INFO(NETVSC, "NvspMessageTypeInit status(%d) max mdl chain (%d)",
633 initPacket->Messages.InitMessages.InitComplete.Status,
634 initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength);
635
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700636 if (initPacket->Messages.InitMessages.InitComplete.Status !=
637 NvspStatusSuccess) {
638 DPRINT_ERR(NETVSC,
639 "unable to initialize with netvsp (status 0x%x)",
640 initPacket->Messages.InitMessages.InitComplete.Status);
Hank Janssenfceaf242009-07-13 15:34:54 -0700641 ret = -1;
642 goto Cleanup;
643 }
644
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700645 if (initPacket->Messages.InitMessages.InitComplete.NegotiatedProtocolVersion != NVSP_PROTOCOL_VERSION_1) {
646 DPRINT_ERR(NETVSC, "unable to initialize with netvsp "
647 "(version expected 1 got %d)",
648 initPacket->Messages.InitMessages.InitComplete.NegotiatedProtocolVersion);
Hank Janssenfceaf242009-07-13 15:34:54 -0700649 ret = -1;
650 goto Cleanup;
651 }
652 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendNdisVersion...");
653
Bill Pemberton454f18a2009-07-27 16:47:24 -0400654 /* Send the ndis version */
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700655 memset(initPacket, 0, sizeof(struct nvsp_message));
Hank Janssenfceaf242009-07-13 15:34:54 -0700656
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700657 ndisVersion = 0x00050000;
Hank Janssenfceaf242009-07-13 15:34:54 -0700658
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700659 initPacket->Header.MessageType = NvspMessage1TypeSendNdisVersion;
660 initPacket->Messages.Version1Messages.SendNdisVersion.NdisMajorVersion =
661 (ndisVersion & 0xFFFF0000) >> 16;
662 initPacket->Messages.Version1Messages.SendNdisVersion.NdisMinorVersion =
663 ndisVersion & 0xFFFF;
Hank Janssenfceaf242009-07-13 15:34:54 -0700664
Bill Pemberton454f18a2009-07-27 16:47:24 -0400665 /* Send the init request */
Hank Janssenfceaf242009-07-13 15:34:54 -0700666 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700667 initPacket,
668 sizeof(struct nvsp_message),
669 (unsigned long)initPacket,
670 VmbusPacketTypeDataInBand, 0);
671 if (ret != 0) {
672 DPRINT_ERR(NETVSC,
673 "unable to send NvspMessage1TypeSendNdisVersion");
Hank Janssenfceaf242009-07-13 15:34:54 -0700674 ret = -1;
675 goto Cleanup;
676 }
Bill Pemberton454f18a2009-07-27 16:47:24 -0400677 /*
678 * BUGBUG - We have to wait for the above msg since the
679 * netvsp uses KMCL which acknowledges packet (completion
680 * packet) since our Vmbus always set the
681 * VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag
682 */
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700683 /* osd_WaitEventWait(NetVscChannel->ChannelInitEvent); */
Hank Janssenfceaf242009-07-13 15:34:54 -0700684
Bill Pemberton454f18a2009-07-27 16:47:24 -0400685 /* Post the big receive buffer to NetVSP */
Hank Janssenfceaf242009-07-13 15:34:54 -0700686 ret = NetVscInitializeReceiveBufferWithNetVsp(Device);
687 if (ret == 0)
Hank Janssenfceaf242009-07-13 15:34:54 -0700688 ret = NetVscInitializeSendBufferWithNetVsp(Device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700689
690Cleanup:
691 PutNetDevice(Device);
692 DPRINT_EXIT(NETVSC);
693 return ret;
694}
695
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700696static void NetVscDisconnectFromVsp(struct netvsc_device *NetDevice)
Hank Janssenfceaf242009-07-13 15:34:54 -0700697{
698 DPRINT_ENTER(NETVSC);
699
700 NetVscDestroyReceiveBuffer(NetDevice);
701 NetVscDestroySendBuffer(NetDevice);
702
703 DPRINT_EXIT(NETVSC);
704}
705
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700706/**
707 * NetVscOnDeviceAdd - Callback when the device belonging to this driver is added
708 */
709static int NetVscOnDeviceAdd(struct hv_device *Device, void *AdditionalInfo)
Hank Janssenfceaf242009-07-13 15:34:54 -0700710{
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700711 int ret = 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700712 int i;
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700713 struct netvsc_device *netDevice;
Bill Pembertond29274e2009-09-11 21:46:43 -0400714 struct hv_netvsc_packet *packet, *pos;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700715 struct netvsc_driver *netDriver =
716 (struct netvsc_driver *)Device->Driver;
Hank Janssenfceaf242009-07-13 15:34:54 -0700717
718 DPRINT_ENTER(NETVSC);
719
720 netDevice = AllocNetDevice(Device);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700721 if (!netDevice) {
Hank Janssenfceaf242009-07-13 15:34:54 -0700722 ret = -1;
723 goto Cleanup;
724 }
725
726 DPRINT_DBG(NETVSC, "netvsc channel object allocated - %p", netDevice);
727
Bill Pemberton454f18a2009-07-27 16:47:24 -0400728 /* Initialize the NetVSC channel extension */
Hank Janssenfceaf242009-07-13 15:34:54 -0700729 netDevice->ReceiveBufferSize = NETVSC_RECEIVE_BUFFER_SIZE;
Greg Kroah-Hartman64368732009-07-15 14:56:15 -0700730 spin_lock_init(&netDevice->receive_packet_list_lock);
Hank Janssenfceaf242009-07-13 15:34:54 -0700731
732 netDevice->SendBufferSize = NETVSC_SEND_BUFFER_SIZE;
733
Bill Pembertond29274e2009-09-11 21:46:43 -0400734 INIT_LIST_HEAD(&netDevice->ReceivePacketList);
Hank Janssenfceaf242009-07-13 15:34:54 -0700735
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700736 for (i = 0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++) {
737 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
738 (NETVSC_RECEIVE_SG_COUNT *
739 sizeof(struct hv_page_buffer)), GFP_KERNEL);
740 if (!packet) {
741 DPRINT_DBG(NETVSC, "unable to allocate netvsc pkts "
742 "for receive pool (wanted %d got %d)",
743 NETVSC_RECEIVE_PACKETLIST_COUNT, i);
Hank Janssenfceaf242009-07-13 15:34:54 -0700744 break;
745 }
Bill Pembertond29274e2009-09-11 21:46:43 -0400746 list_add_tail(&packet->ListEntry,
747 &netDevice->ReceivePacketList);
Hank Janssenfceaf242009-07-13 15:34:54 -0700748 }
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700749 netDevice->ChannelInitEvent = osd_WaitEventCreate();
Hank Janssenfceaf242009-07-13 15:34:54 -0700750
Bill Pemberton454f18a2009-07-27 16:47:24 -0400751 /* Open the channel */
Hank Janssenfceaf242009-07-13 15:34:54 -0700752 ret = Device->Driver->VmbusChannelInterface.Open(Device,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700753 netDriver->RingBufferSize,
754 netDriver->RingBufferSize,
755 NULL, 0,
756 NetVscOnChannelCallback,
757 Device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700758
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700759 if (ret != 0) {
Hank Janssenfceaf242009-07-13 15:34:54 -0700760 DPRINT_ERR(NETVSC, "unable to open channel: %d", ret);
761 ret = -1;
762 goto Cleanup;
763 }
764
Bill Pemberton454f18a2009-07-27 16:47:24 -0400765 /* Channel is opened */
Hank Janssenfceaf242009-07-13 15:34:54 -0700766 DPRINT_INFO(NETVSC, "*** NetVSC channel opened successfully! ***");
767
Bill Pemberton454f18a2009-07-27 16:47:24 -0400768 /* Connect with the NetVsp */
Hank Janssenfceaf242009-07-13 15:34:54 -0700769 ret = NetVscConnectToVsp(Device);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700770 if (ret != 0) {
Hank Janssenfceaf242009-07-13 15:34:54 -0700771 DPRINT_ERR(NETVSC, "unable to connect to NetVSP - %d", ret);
772 ret = -1;
773 goto Close;
774 }
775
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700776 DPRINT_INFO(NETVSC, "*** NetVSC channel handshake result - %d ***",
777 ret);
Hank Janssenfceaf242009-07-13 15:34:54 -0700778
779 DPRINT_EXIT(NETVSC);
780 return ret;
781
782Close:
Bill Pemberton454f18a2009-07-27 16:47:24 -0400783 /* Now, we can close the channel safely */
Hank Janssenfceaf242009-07-13 15:34:54 -0700784 Device->Driver->VmbusChannelInterface.Close(Device);
785
786Cleanup:
787
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700788 if (netDevice) {
Bill Pemberton420beac2009-07-29 17:00:10 -0400789 kfree(netDevice->ChannelInitEvent);
Hank Janssenfceaf242009-07-13 15:34:54 -0700790
Bill Pembertond29274e2009-09-11 21:46:43 -0400791 list_for_each_entry_safe(packet, pos,
792 &netDevice->ReceivePacketList,
793 ListEntry) {
794 list_del(&packet->ListEntry);
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700795 kfree(packet);
Hank Janssenfceaf242009-07-13 15:34:54 -0700796 }
797
Hank Janssenfceaf242009-07-13 15:34:54 -0700798 ReleaseOutboundNetDevice(Device);
799 ReleaseInboundNetDevice(Device);
800
801 FreeNetDevice(netDevice);
802 }
803
804 DPRINT_EXIT(NETVSC);
805 return ret;
806}
807
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700808/**
809 * NetVscOnDeviceRemove - Callback when the root bus device is removed
810 */
811static int NetVscOnDeviceRemove(struct hv_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700812{
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700813 struct netvsc_device *netDevice;
Bill Pembertond29274e2009-09-11 21:46:43 -0400814 struct hv_netvsc_packet *netvscPacket, *pos;
Hank Janssenfceaf242009-07-13 15:34:54 -0700815
816 DPRINT_ENTER(NETVSC);
817
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700818 DPRINT_INFO(NETVSC, "Disabling outbound traffic on net device (%p)...",
819 Device->Extension);
Hank Janssenfceaf242009-07-13 15:34:54 -0700820
Bill Pemberton454f18a2009-07-27 16:47:24 -0400821 /* Stop outbound traffic ie sends and receives completions */
Hank Janssenfceaf242009-07-13 15:34:54 -0700822 netDevice = ReleaseOutboundNetDevice(Device);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700823 if (!netDevice) {
Hank Janssenfceaf242009-07-13 15:34:54 -0700824 DPRINT_ERR(NETVSC, "No net device present!!");
825 return -1;
826 }
827
Bill Pemberton454f18a2009-07-27 16:47:24 -0400828 /* Wait for all send completions */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700829 while (atomic_read(&netDevice->NumOutstandingSends)) {
830 DPRINT_INFO(NETVSC, "waiting for %d requests to complete...",
831 atomic_read(&netDevice->NumOutstandingSends));
Greg Kroah-Hartmanb4362c92009-07-16 11:50:41 -0700832 udelay(100);
Hank Janssenfceaf242009-07-13 15:34:54 -0700833 }
834
835 DPRINT_INFO(NETVSC, "Disconnecting from netvsp...");
836
837 NetVscDisconnectFromVsp(netDevice);
838
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700839 DPRINT_INFO(NETVSC, "Disabling inbound traffic on net device (%p)...",
840 Device->Extension);
Hank Janssenfceaf242009-07-13 15:34:54 -0700841
Bill Pemberton454f18a2009-07-27 16:47:24 -0400842 /* Stop inbound traffic ie receives and sends completions */
Hank Janssenfceaf242009-07-13 15:34:54 -0700843 netDevice = ReleaseInboundNetDevice(Device);
844
Bill Pemberton454f18a2009-07-27 16:47:24 -0400845 /* At this point, no one should be accessing netDevice except in here */
Hank Janssenfceaf242009-07-13 15:34:54 -0700846 DPRINT_INFO(NETVSC, "net device (%p) safe to remove", netDevice);
847
Bill Pemberton454f18a2009-07-27 16:47:24 -0400848 /* Now, we can close the channel safely */
Hank Janssenfceaf242009-07-13 15:34:54 -0700849 Device->Driver->VmbusChannelInterface.Close(Device);
850
Bill Pemberton454f18a2009-07-27 16:47:24 -0400851 /* Release all resources */
Bill Pembertond29274e2009-09-11 21:46:43 -0400852 list_for_each_entry_safe(netvscPacket, pos,
853 &netDevice->ReceivePacketList, ListEntry) {
854 list_del(&netvscPacket->ListEntry);
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700855 kfree(netvscPacket);
Hank Janssenfceaf242009-07-13 15:34:54 -0700856 }
857
Bill Pemberton420beac2009-07-29 17:00:10 -0400858 kfree(netDevice->ChannelInitEvent);
Hank Janssenfceaf242009-07-13 15:34:54 -0700859 FreeNetDevice(netDevice);
860
861 DPRINT_EXIT(NETVSC);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700862 return 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700863}
864
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700865/**
866 * NetVscOnCleanup - Perform any cleanup when the driver is removed
867 */
868static void NetVscOnCleanup(struct hv_driver *drv)
Hank Janssenfceaf242009-07-13 15:34:54 -0700869{
870 DPRINT_ENTER(NETVSC);
Hank Janssenfceaf242009-07-13 15:34:54 -0700871 DPRINT_EXIT(NETVSC);
872}
873
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700874static void NetVscOnSendCompletion(struct hv_device *Device,
875 struct vmpacket_descriptor *Packet)
Hank Janssenfceaf242009-07-13 15:34:54 -0700876{
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700877 struct netvsc_device *netDevice;
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700878 struct nvsp_message *nvspPacket;
Nicolas Palix4193d4f2009-07-29 14:10:10 +0200879 struct hv_netvsc_packet *nvscPacket;
Hank Janssenfceaf242009-07-13 15:34:54 -0700880
881 DPRINT_ENTER(NETVSC);
882
883 netDevice = GetInboundNetDevice(Device);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700884 if (!netDevice) {
885 DPRINT_ERR(NETVSC, "unable to get net device..."
886 "device being destroyed?");
Hank Janssenfceaf242009-07-13 15:34:54 -0700887 DPRINT_EXIT(NETVSC);
888 return;
889 }
890
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700891 nvspPacket = (struct nvsp_message *)((unsigned long)Packet + (Packet->DataOffset8 << 3));
Hank Janssenfceaf242009-07-13 15:34:54 -0700892
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700893 DPRINT_DBG(NETVSC, "send completion packet - type %d",
894 nvspPacket->Header.MessageType);
Hank Janssenfceaf242009-07-13 15:34:54 -0700895
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700896 if ((nvspPacket->Header.MessageType == NvspMessageTypeInitComplete) ||
897 (nvspPacket->Header.MessageType ==
898 NvspMessage1TypeSendReceiveBufferComplete) ||
899 (nvspPacket->Header.MessageType ==
900 NvspMessage1TypeSendSendBufferComplete)) {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400901 /* Copy the response back */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700902 memcpy(&netDevice->ChannelInitPacket, nvspPacket,
903 sizeof(struct nvsp_message));
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700904 osd_WaitEventSet(netDevice->ChannelInitEvent);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700905 } else if (nvspPacket->Header.MessageType ==
906 NvspMessage1TypeSendRNDISPacketComplete) {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400907 /* Get the send context */
Nicolas Palix4193d4f2009-07-29 14:10:10 +0200908 nvscPacket = (struct hv_netvsc_packet *)(unsigned long)Packet->TransactionId;
Hank Janssenfceaf242009-07-13 15:34:54 -0700909 ASSERT(nvscPacket);
910
Bill Pemberton454f18a2009-07-27 16:47:24 -0400911 /* Notify the layer above us */
Hank Janssenfceaf242009-07-13 15:34:54 -0700912 nvscPacket->Completion.Send.OnSendCompletion(nvscPacket->Completion.Send.SendCompletionContext);
913
Bill Pembertonf4888412009-07-29 17:00:12 -0400914 atomic_dec(&netDevice->NumOutstandingSends);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700915 } else {
916 DPRINT_ERR(NETVSC, "Unknown send completion packet type - "
917 "%d received!!", nvspPacket->Header.MessageType);
Hank Janssenfceaf242009-07-13 15:34:54 -0700918 }
919
920 PutNetDevice(Device);
921 DPRINT_EXIT(NETVSC);
922}
923
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700924static int NetVscOnSend(struct hv_device *Device,
925 struct hv_netvsc_packet *Packet)
Hank Janssenfceaf242009-07-13 15:34:54 -0700926{
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700927 struct netvsc_device *netDevice;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700928 int ret = 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700929
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700930 struct nvsp_message sendMessage;
Hank Janssenfceaf242009-07-13 15:34:54 -0700931
932 DPRINT_ENTER(NETVSC);
933
934 netDevice = GetOutboundNetDevice(Device);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700935 if (!netDevice) {
936 DPRINT_ERR(NETVSC, "net device (%p) shutting down..."
937 "ignoring outbound packets", netDevice);
Hank Janssenfceaf242009-07-13 15:34:54 -0700938 DPRINT_EXIT(NETVSC);
939 return -2;
940 }
941
942 sendMessage.Header.MessageType = NvspMessage1TypeSendRNDISPacket;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700943 if (Packet->IsDataPacket) {
944 /* 0 is RMC_DATA; */
945 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 0;
946 } else {
947 /* 1 is RMC_CONTROL; */
948 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 1;
949 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700950
Bill Pemberton454f18a2009-07-27 16:47:24 -0400951 /* Not using send buffer section */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700952 sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionIndex = 0xFFFFFFFF;
953 sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionSize = 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700954
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700955 if (Packet->PageBufferCount) {
956 ret = Device->Driver->VmbusChannelInterface.SendPacketPageBuffer(
957 Device, Packet->PageBuffers,
958 Packet->PageBufferCount,
959 &sendMessage,
960 sizeof(struct nvsp_message),
961 (unsigned long)Packet);
962 } else {
Hank Janssenfceaf242009-07-13 15:34:54 -0700963 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700964 &sendMessage,
965 sizeof(struct nvsp_message),
966 (unsigned long)Packet,
967 VmbusPacketTypeDataInBand,
968 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Hank Janssenfceaf242009-07-13 15:34:54 -0700969
970 }
971
972 if (ret != 0)
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700973 DPRINT_ERR(NETVSC, "Unable to send packet %p ret %d",
974 Packet, ret);
Hank Janssenfceaf242009-07-13 15:34:54 -0700975
Bill Pembertonf4888412009-07-29 17:00:12 -0400976 atomic_inc(&netDevice->NumOutstandingSends);
Hank Janssenfceaf242009-07-13 15:34:54 -0700977 PutNetDevice(Device);
978
979 DPRINT_EXIT(NETVSC);
980 return ret;
981}
982
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700983static void NetVscOnReceive(struct hv_device *Device,
984 struct vmpacket_descriptor *Packet)
Hank Janssenfceaf242009-07-13 15:34:54 -0700985{
Greg Kroah-Hartmance9ea4c2009-09-02 10:35:56 -0700986 struct netvsc_device *netDevice;
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700987 struct vmtransfer_page_packet_header *vmxferpagePacket;
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700988 struct nvsp_message *nvspPacket;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700989 struct hv_netvsc_packet *netvscPacket = NULL;
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -0700990 unsigned long start;
991 unsigned long end, endVirtual;
Greg Kroah-Hartman7e23a6e2009-08-27 15:58:15 -0700992 /* struct netvsc_driver *netvscDriver; */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700993 struct xferpage_packet *xferpagePacket = NULL;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700994 int i, j;
995 int count = 0, bytesRemain = 0;
Greg Kroah-Hartman64368732009-07-15 14:56:15 -0700996 unsigned long flags;
Bill Pembertond29274e2009-09-11 21:46:43 -0400997 LIST_HEAD(listHead);
Hank Janssenfceaf242009-07-13 15:34:54 -0700998
999 DPRINT_ENTER(NETVSC);
1000
1001 netDevice = GetInboundNetDevice(Device);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001002 if (!netDevice) {
1003 DPRINT_ERR(NETVSC, "unable to get net device..."
1004 "device being destroyed?");
Hank Janssenfceaf242009-07-13 15:34:54 -07001005 DPRINT_EXIT(NETVSC);
1006 return;
1007 }
1008
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001009 /*
1010 * All inbound packets other than send completion should be xfer page
1011 * packet
1012 */
1013 if (Packet->Type != VmbusPacketTypeDataUsingTransferPages) {
1014 DPRINT_ERR(NETVSC, "Unknown packet type received - %d",
1015 Packet->Type);
Hank Janssenfceaf242009-07-13 15:34:54 -07001016 PutNetDevice(Device);
1017 return;
1018 }
1019
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001020 nvspPacket = (struct nvsp_message *)((unsigned long)Packet +
1021 (Packet->DataOffset8 << 3));
Hank Janssenfceaf242009-07-13 15:34:54 -07001022
Bill Pemberton454f18a2009-07-27 16:47:24 -04001023 /* Make sure this is a valid nvsp packet */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001024 if (nvspPacket->Header.MessageType != NvspMessage1TypeSendRNDISPacket) {
1025 DPRINT_ERR(NETVSC, "Unknown nvsp packet type received - %d",
1026 nvspPacket->Header.MessageType);
Hank Janssenfceaf242009-07-13 15:34:54 -07001027 PutNetDevice(Device);
1028 return;
1029 }
1030
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001031 DPRINT_DBG(NETVSC, "NVSP packet received - type %d",
1032 nvspPacket->Header.MessageType);
Hank Janssenfceaf242009-07-13 15:34:54 -07001033
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -07001034 vmxferpagePacket = (struct vmtransfer_page_packet_header *)Packet;
Hank Janssenfceaf242009-07-13 15:34:54 -07001035
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001036 if (vmxferpagePacket->TransferPageSetId != NETVSC_RECEIVE_BUFFER_ID) {
1037 DPRINT_ERR(NETVSC, "Invalid xfer page set id - "
1038 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID,
1039 vmxferpagePacket->TransferPageSetId);
Hank Janssenfceaf242009-07-13 15:34:54 -07001040 PutNetDevice(Device);
1041 return;
1042 }
1043
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001044 DPRINT_DBG(NETVSC, "xfer page - range count %d",
1045 vmxferpagePacket->RangeCount);
Hank Janssenfceaf242009-07-13 15:34:54 -07001046
Bill Pemberton454f18a2009-07-27 16:47:24 -04001047 /*
1048 * Grab free packets (range count + 1) to represent this xfer
1049 * page packet. +1 to represent the xfer page packet itself.
1050 * We grab it here so that we know exactly how many we can
1051 * fulfil
1052 */
Greg Kroah-Hartman64368732009-07-15 14:56:15 -07001053 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
Bill Pembertond29274e2009-09-11 21:46:43 -04001054 while (!list_empty(&netDevice->ReceivePacketList)) {
1055 list_move_tail(&netDevice->ReceivePacketList, &listHead);
Hank Janssenfceaf242009-07-13 15:34:54 -07001056 if (++count == vmxferpagePacket->RangeCount + 1)
1057 break;
1058 }
Greg Kroah-Hartman64368732009-07-15 14:56:15 -07001059 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
Hank Janssenfceaf242009-07-13 15:34:54 -07001060
Bill Pemberton454f18a2009-07-27 16:47:24 -04001061 /*
1062 * We need at least 2 netvsc pkts (1 to represent the xfer
1063 * page and at least 1 for the range) i.e. we can handled
1064 * some of the xfer page packet ranges...
1065 */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001066 if (count < 2) {
1067 DPRINT_ERR(NETVSC, "Got only %d netvsc pkt...needed %d pkts. "
1068 "Dropping this xfer page packet completely!",
1069 count, vmxferpagePacket->RangeCount + 1);
Hank Janssenfceaf242009-07-13 15:34:54 -07001070
Bill Pemberton454f18a2009-07-27 16:47:24 -04001071 /* Return it to the freelist */
Greg Kroah-Hartman64368732009-07-15 14:56:15 -07001072 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001073 for (i = count; i != 0; i--) {
Bill Pembertond29274e2009-09-11 21:46:43 -04001074 list_move_tail(&listHead,
1075 &netDevice->ReceivePacketList);
Hank Janssenfceaf242009-07-13 15:34:54 -07001076 }
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001077 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock,
1078 flags);
Hank Janssenfceaf242009-07-13 15:34:54 -07001079
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001080 NetVscSendReceiveCompletion(Device,
1081 vmxferpagePacket->d.TransactionId);
Hank Janssenfceaf242009-07-13 15:34:54 -07001082
1083 PutNetDevice(Device);
1084 return;
1085 }
1086
Bill Pemberton454f18a2009-07-27 16:47:24 -04001087 /* Remove the 1st packet to represent the xfer page packet itself */
Bill Pembertond29274e2009-09-11 21:46:43 -04001088 xferpagePacket = list_entry(&listHead, struct xferpage_packet,
1089 ListEntry);
1090 list_del(&xferpagePacket->ListEntry);
1091
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001092 /* This is how much we can satisfy */
1093 xferpagePacket->Count = count - 1;
1094 ASSERT(xferpagePacket->Count > 0 && xferpagePacket->Count <=
1095 vmxferpagePacket->RangeCount);
Hank Janssenfceaf242009-07-13 15:34:54 -07001096
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001097 if (xferpagePacket->Count != vmxferpagePacket->RangeCount) {
1098 DPRINT_INFO(NETVSC, "Needed %d netvsc pkts to satisy this xfer "
1099 "page...got %d", vmxferpagePacket->RangeCount,
1100 xferpagePacket->Count);
Hank Janssenfceaf242009-07-13 15:34:54 -07001101 }
1102
Bill Pemberton454f18a2009-07-27 16:47:24 -04001103 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001104 for (i = 0; i < (count - 1); i++) {
Bill Pembertond29274e2009-09-11 21:46:43 -04001105 netvscPacket = list_entry(&listHead, struct hv_netvsc_packet,
1106 ListEntry);
1107 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}