win-pvdrivers

annotate xennet/xennet_tx.c @ 1007:4cda50fe71d5

merge ndis5 and ndis6 code in xennet
author James Harper <james.harper@bendigoit.com.au>
date Sun Feb 10 23:14:05 2013 +1100 (2013-02-10)
parents 3c7778b9f877
children c21a4feb4a27
rev   line source
james@1007 1 /*
james@1007 2 PV Net Driver for Windows Xen HVM Domains
james@1007 3 Copyright (C) 2007 James Harper
james@1007 4 Copyright (C) 2007 Andrew Grover <andy.grover@oracle.com>
james@1007 5
james@1007 6 This program is free software; you can redistribute it and/or
james@1007 7 modify it under the terms of the GNU General Public License
james@1007 8 as published by the Free Software Foundation; either version 2
james@1007 9 of the License, or (at your option) any later version.
james@1007 10
james@1007 11 This program is distributed in the hope that it will be useful,
james@1007 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
james@1007 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
james@1007 14 GNU General Public License for more details.
james@1007 15
james@1007 16 You should have received a copy of the GNU General Public License
james@1007 17 along with this program; if not, write to the Free Software
james@1007 18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
james@1007 19 */
james@1007 20
james@1007 21 #include "xennet.h"
james@1007 22
james@1007 23
james@1007 24 static USHORT
james@1007 25 get_id_from_freelist(struct xennet_info *xi)
james@1007 26 {
james@1007 27 NT_ASSERT(xi->tx_id_free);
james@1007 28 xi->tx_id_free--;
james@1007 29
james@1007 30 return xi->tx_id_list[xi->tx_id_free];
james@1007 31 }
james@1007 32
james@1007 33 static VOID
james@1007 34 put_id_on_freelist(struct xennet_info *xi, USHORT id)
james@1007 35 {
james@1007 36 xi->tx_id_list[xi->tx_id_free] = id;
james@1007 37 xi->tx_id_free++;
james@1007 38 }
james@1007 39
james@1007 40 #define SWAP_USHORT(x) (USHORT)((((x & 0xFF) << 8)|((x >> 8) & 0xFF)))
james@1007 41
james@1007 42 static __forceinline struct netif_tx_request *
james@1007 43 XenNet_PutCbOnRing(struct xennet_info *xi, PVOID coalesce_buf, ULONG length, grant_ref_t gref)
james@1007 44 {
james@1007 45 struct netif_tx_request *tx;
james@1007 46 tx = RING_GET_REQUEST(&xi->tx_ring, xi->tx_ring.req_prod_pvt);
james@1007 47 xi->tx_ring.req_prod_pvt++;
james@1007 48 xi->tx_ring_free--;
james@1007 49 tx->id = get_id_from_freelist(xi);
james@1007 50 NT_ASSERT(xi->tx_shadows[tx->id].gref == INVALID_GRANT_REF);
james@1007 51 NT_ASSERT(!xi->tx_shadows[tx->id].cb);
james@1007 52 xi->tx_shadows[tx->id].cb = coalesce_buf;
james@1007 53 tx->gref = XnGrantAccess(xi->handle, (ULONG)(MmGetPhysicalAddress(coalesce_buf).QuadPart >> PAGE_SHIFT), FALSE, gref, (ULONG)'XNTX');
james@1007 54 xi->tx_shadows[tx->id].gref = tx->gref;
james@1007 55 tx->offset = 0;
james@1007 56 tx->size = (USHORT)length;
james@1007 57 NT_ASSERT(tx->offset + tx->size <= PAGE_SIZE);
james@1007 58 NT_ASSERT(tx->size);
james@1007 59 return tx;
james@1007 60 }
james@1007 61
james@1007 62 static VOID dump_packet_data(PNDIS_PACKET packet, PCHAR header) {
james@1007 63 UINT mdl_count;
james@1007 64 PMDL first_mdl;
james@1007 65 UINT total_length;
james@1007 66
james@1007 67 NdisQueryPacket(packet, NULL, (PUINT)&mdl_count, &first_mdl, (PUINT)&total_length);
james@1007 68 FUNCTION_MSG("%s mdl_count = %d, first_mdl = %p, total_length = %d\n", header, mdl_count, first_mdl, total_length);
james@1007 69 }
james@1007 70
james@1007 71 /* Called at DISPATCH_LEVEL with tx_lock held */
james@1007 72 /*
james@1007 73 * Send one NDIS_PACKET. This may involve multiple entries on TX ring.
james@1007 74 */
james@1007 75 static BOOLEAN
james@1007 76 XenNet_HWSendPacket(struct xennet_info *xi, PNDIS_PACKET packet)
james@1007 77 {
james@1007 78 struct netif_tx_request *tx0 = NULL;
james@1007 79 struct netif_tx_request *txN = NULL;
james@1007 80 struct netif_extra_info *ei = NULL;
james@1007 81 ULONG mss = 0;
james@1007 82 #if NTDDI_VERSION < NTDDI_VISTA
james@1007 83 PNDIS_TCP_IP_CHECKSUM_PACKET_INFO csum_info;
james@1007 84 #else
james@1007 85 NDIS_TCP_LARGE_SEND_OFFLOAD_NET_BUFFER_LIST_INFO lso_info;
james@1007 86 NDIS_TCP_IP_CHECKSUM_NET_BUFFER_LIST_INFO csum_info;
james@1007 87 #endif
james@1007 88 uint16_t flags = NETTXF_more_data;
james@1007 89 packet_info_t pi;
james@1007 90 BOOLEAN ndis_lso = FALSE;
james@1007 91 BOOLEAN xen_gso = FALSE;
james@1007 92 ULONG remaining;
james@1007 93 ULONG frags = 0;
james@1007 94 BOOLEAN coalesce_required = FALSE;
james@1007 95 PVOID coalesce_buf;
james@1007 96 ULONG coalesce_remaining = 0;
james@1007 97 grant_ref_t gref;
james@1007 98 ULONG tx_length = 0;
james@1007 99 UINT mdl_count;
james@1007 100
james@1007 101 gref = XnAllocateGrant(xi->handle, (ULONG)'XNTX');
james@1007 102 if (gref == INVALID_GRANT_REF)
james@1007 103 {
james@1007 104 FUNCTION_MSG("out of grefs\n");
james@1007 105 return FALSE;
james@1007 106 }
james@1007 107 coalesce_buf = NdisAllocateFromNPagedLookasideList(&xi->tx_lookaside_list);
james@1007 108 if (!coalesce_buf)
james@1007 109 {
james@1007 110 XnFreeGrant(xi->handle, gref, (ULONG)'XNTX');
james@1007 111 FUNCTION_MSG("out of memory\n");
james@1007 112 return FALSE;
james@1007 113 }
james@1007 114 XenNet_ClearPacketInfo(&pi);
james@1007 115 #if NTDDI_VERSION < NTDDI_VISTA
james@1007 116 NdisQueryPacket(packet, NULL, (PUINT)&mdl_count, &pi.first_mdl, (PUINT)&pi.total_length);
james@1007 117 pi.curr_mdl = pi.first_mdl;
james@1007 118 #else
james@1007 119 /* create a new MDL over the data portion of the first MDL in the packet... it's just easier this way */
james@1007 120 IoBuildPartialMdl(packet->CurrentMdl,
james@1007 121 &pi.first_mdl_storage,
james@1007 122 (PUCHAR)MmGetMdlVirtualAddress(nb->CurrentMdl) + nb->CurrentMdlOffset,
james@1007 123 MmGetMdlByteCount(nb->CurrentMdl) - nb->CurrentMdlOffset);
james@1007 124 pi.total_length = nb->DataLength;
james@1007 125 pi.first_mdl_storage.Next = nb->CurrentMdl->Next;
james@1007 126 pi.first_mdl = pi.curr_mdl = &pi.first_mdl_storage;
james@1007 127 #endif
james@1007 128 pi.first_mdl_offset = pi.curr_mdl_offset = 0;
james@1007 129 remaining = min(pi.total_length, PAGE_SIZE);
james@1007 130 while (remaining) { /* this much gets put in the header */
james@1007 131 ULONG length = XenNet_QueryData(&pi, remaining);
james@1007 132 remaining -= length;
james@1007 133 XenNet_EatData(&pi, length);
james@1007 134 }
james@1007 135 frags++;
james@1007 136 if (pi.total_length > PAGE_SIZE) { /* these are the frags we care about */
james@1007 137 remaining = pi.total_length - PAGE_SIZE;
james@1007 138 while (remaining) {
james@1007 139 ULONG length = XenNet_QueryData(&pi, PAGE_SIZE);
james@1007 140 if (length != 0) {
james@1007 141 frags++;
james@1007 142 if (frags > LINUX_MAX_SG_ELEMENTS)
james@1007 143 break; /* worst case there could be hundreds of fragments - leave the loop now */
james@1007 144 }
james@1007 145 remaining -= length;
james@1007 146 XenNet_EatData(&pi, length);
james@1007 147 }
james@1007 148 }
james@1007 149 if (frags > LINUX_MAX_SG_ELEMENTS) {
james@1007 150 frags = LINUX_MAX_SG_ELEMENTS;
james@1007 151 coalesce_required = TRUE;
james@1007 152 }
james@1007 153
james@1007 154 /* if we have enough space on the ring then we have enough id's so no need to check for that */
james@1007 155 if (xi->tx_ring_free < frags + 1) {
james@1007 156 XnFreeGrant(xi->handle, gref, (ULONG)'XNTX');
james@1007 157 NdisFreeToNPagedLookasideList(&xi->tx_lookaside_list, coalesce_buf);
james@1007 158 //FUNCTION_MSG("Full on send - ring full\n");
james@1007 159 return FALSE;
james@1007 160 }
james@1007 161 XenNet_ParsePacketHeader(&pi, coalesce_buf, PAGE_SIZE);
james@1007 162 remaining = pi.total_length - pi.header_length;
james@1007 163 if (pi.ip_version == 4 && pi.ip_proto == 6 && pi.ip4_length == 0) {
james@1007 164 *((PUSHORT)(pi.header + 0x10)) = GET_NET_USHORT((USHORT)pi.total_length - XN_HDR_SIZE);
james@1007 165 }
james@1007 166
james@1007 167 #if NTDDI_VERSION < NTDDI_VISTA
james@1007 168 if (NDIS_GET_PACKET_PROTOCOL_TYPE(packet) == NDIS_PROTOCOL_ID_TCP_IP) {
james@1007 169 csum_info = (PNDIS_TCP_IP_CHECKSUM_PACKET_INFO)&NDIS_PER_PACKET_INFO_FROM_PACKET(
james@1007 170 packet, TcpIpChecksumPacketInfo);
james@1007 171 if (csum_info->Transmit.NdisPacketChecksumV4) {
james@1007 172 if (csum_info->Transmit.NdisPacketTcpChecksum) {
james@1007 173 flags |= NETTXF_csum_blank | NETTXF_data_validated;
james@1007 174 } else if (csum_info->Transmit.NdisPacketUdpChecksum) {
james@1007 175 flags |= NETTXF_csum_blank | NETTXF_data_validated;
james@1007 176 }
james@1007 177 }
james@1007 178 }
james@1007 179 #else
james@1007 180 csum_info.Value = NET_BUFFER_LIST_INFO(NB_NBL(nb), TcpIpChecksumNetBufferListInfo);
james@1007 181 if (csum_info.Transmit.IsIPv4) {
james@1007 182 if (csum_info.Transmit.TcpChecksum) {
james@1007 183 flags |= NETTXF_csum_blank | NETTXF_data_validated;
james@1007 184 } else if (csum_info.Transmit.UdpChecksum) {
james@1007 185 flags |= NETTXF_csum_blank | NETTXF_data_validated;
james@1007 186 }
james@1007 187 } else if (csum_info.Transmit.IsIPv6) {
james@1007 188 KdPrint((__DRIVER_NAME " Transmit.IsIPv6 not supported\n"));
james@1007 189 }
james@1007 190 #endif
james@1007 191
james@1007 192 #if NTDDI_VERSION < NTDDI_VISTA
james@1007 193 mss = PtrToUlong(NDIS_PER_PACKET_INFO_FROM_PACKET(packet, TcpLargeSendPacketInfo));
james@1007 194 #else
james@1007 195 lso_info.Value = NET_BUFFER_LIST_INFO(NB_NBL(nb), TcpLargeSendNetBufferListInfo);
james@1007 196 switch (lso_info.Transmit.Type) {
james@1007 197 case NDIS_TCP_LARGE_SEND_OFFLOAD_V1_TYPE:
james@1007 198 mss = lso_info.LsoV1Transmit.MSS;
james@1007 199 /* should make use of TcpHeaderOffset too... maybe just assert if it's not what we expect */
james@1007 200 break;
james@1007 201 case NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE:
james@1007 202 mss = lso_info.LsoV2Transmit.MSS;
james@1007 203 /* should make use of TcpHeaderOffset too... maybe just assert if it's not what we expect */
james@1007 204 break;
james@1007 205 }
james@1007 206 #endif
james@1007 207 if (mss && pi.parse_result == PARSE_OK) {
james@1007 208 ndis_lso = TRUE;
james@1007 209 }
james@1007 210
james@1007 211 if (ndis_lso) {
james@1007 212 flags |= NETTXF_csum_blank | NETTXF_data_validated; /* these may be implied but not specified when lso is used*/
james@1007 213 if (pi.tcp_length >= mss) {
james@1007 214 flags |= NETTXF_extra_info;
james@1007 215 xen_gso = TRUE;
james@1007 216 }
james@1007 217 }
james@1007 218 /*
james@1007 219 * See io/netif.h. Must put (A) 1st request, then (B) optional extra_info, then
james@1007 220 * (C) rest of requests on the ring. Only (A) has csum flags.
james@1007 221 */
james@1007 222
james@1007 223 /* (A) */
james@1007 224 tx0 = XenNet_PutCbOnRing(xi, coalesce_buf, pi.header_length, gref);
james@1007 225 NT_ASSERT(tx0); /* this will never happen */
james@1007 226 tx0->flags = flags;
james@1007 227 tx_length += pi.header_length;
james@1007 228
james@1007 229 /* lso implies IpHeaderChecksum */
james@1007 230 #if NTDDI_VERSION < NTDDI_VISTA
james@1007 231 if (ndis_lso) {
james@1007 232 XenNet_SumIpHeader(coalesce_buf, pi.ip4_header_length);
james@1007 233 }
james@1007 234 #else
james@1007 235 if (ndis_lso || csum_info.Transmit.IpHeaderChecksum) {
james@1007 236 XenNet_SumIpHeader(coalesce_buf, pi.ip4_header_length);
james@1007 237 }
james@1007 238 #endif
james@1007 239 txN = tx0;
james@1007 240
james@1007 241 /* (B) */
james@1007 242 if (xen_gso) {
james@1007 243 NT_ASSERT(flags & NETTXF_extra_info);
james@1007 244 ei = (struct netif_extra_info *)RING_GET_REQUEST(&xi->tx_ring, xi->tx_ring.req_prod_pvt);
james@1007 245 //KdPrint((__DRIVER_NAME " pos = %d\n", xi->tx_ring.req_prod_pvt));
james@1007 246 xi->tx_ring.req_prod_pvt++;
james@1007 247 xi->tx_ring_free--;
james@1007 248 ei->type = XEN_NETIF_EXTRA_TYPE_GSO;
james@1007 249 ei->flags = 0;
james@1007 250 ei->u.gso.size = (USHORT)mss;
james@1007 251 ei->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
james@1007 252 ei->u.gso.pad = 0;
james@1007 253 ei->u.gso.features = 0;
james@1007 254 }
james@1007 255
james@1007 256 NT_ASSERT(xi->current_sg_supported || !remaining);
james@1007 257
james@1007 258 /* (C) - only if data is remaining */
james@1007 259 coalesce_buf = NULL;
james@1007 260 while (remaining > 0) {
james@1007 261 ULONG length;
james@1007 262 PFN_NUMBER pfn;
james@1007 263
james@1007 264 NT_ASSERT(pi.curr_mdl);
james@1007 265 if (coalesce_required) {
james@1007 266 PVOID va;
james@1007 267 if (!coalesce_buf) {
james@1007 268 gref = XnAllocateGrant(xi->handle, (ULONG)'XNTX');
james@1007 269 if (gref == INVALID_GRANT_REF) {
james@1007 270 KdPrint((__DRIVER_NAME " out of grefs - partial send\n"));
james@1007 271 break;
james@1007 272 }
james@1007 273 coalesce_buf = NdisAllocateFromNPagedLookasideList(&xi->tx_lookaside_list);
james@1007 274 if (!coalesce_buf) {
james@1007 275 XnFreeGrant(xi->handle, gref, (ULONG)'XNTX');
james@1007 276 KdPrint((__DRIVER_NAME " out of memory - partial send\n"));
james@1007 277 break;
james@1007 278 }
james@1007 279 coalesce_remaining = min(PAGE_SIZE, remaining);
james@1007 280 }
james@1007 281 length = XenNet_QueryData(&pi, coalesce_remaining);
james@1007 282 va = NdisBufferVirtualAddressSafe(pi.curr_mdl, LowPagePriority);
james@1007 283 if (!va) {
james@1007 284 KdPrint((__DRIVER_NAME " failed to map buffer va - partial send\n"));
james@1007 285 coalesce_remaining = 0;
james@1007 286 remaining -= min(PAGE_SIZE, remaining);
james@1007 287 NdisFreeToNPagedLookasideList(&xi->tx_lookaside_list, coalesce_buf);
james@1007 288 } else {
james@1007 289 memcpy((PUCHAR)coalesce_buf + min(PAGE_SIZE, remaining) - coalesce_remaining, (PUCHAR)va + pi.curr_mdl_offset, length);
james@1007 290 coalesce_remaining -= length;
james@1007 291 }
james@1007 292 } else {
james@1007 293 length = XenNet_QueryData(&pi, PAGE_SIZE);
james@1007 294 }
james@1007 295 if (!length || coalesce_remaining) { /* sometimes there are zero length buffers... */
james@1007 296 XenNet_EatData(&pi, length); /* do this so we actually move to the next buffer */
james@1007 297 continue;
james@1007 298 }
james@1007 299
james@1007 300 if (coalesce_buf) {
james@1007 301 if (remaining) {
james@1007 302 txN = XenNet_PutCbOnRing(xi, coalesce_buf, min(PAGE_SIZE, remaining), gref);
james@1007 303 NT_ASSERT(txN);
james@1007 304 coalesce_buf = NULL;
james@1007 305 tx_length += min(PAGE_SIZE, remaining);
james@1007 306 remaining -= min(PAGE_SIZE, remaining);
james@1007 307 }
james@1007 308 } else {
james@1007 309 ULONG offset;
james@1007 310
james@1007 311 gref = XnAllocateGrant(xi->handle, (ULONG)'XNTX');
james@1007 312 if (gref == INVALID_GRANT_REF) {
james@1007 313 KdPrint((__DRIVER_NAME " out of grefs - partial send\n"));
james@1007 314 break;
james@1007 315 }
james@1007 316 txN = RING_GET_REQUEST(&xi->tx_ring, xi->tx_ring.req_prod_pvt);
james@1007 317 xi->tx_ring.req_prod_pvt++;
james@1007 318 xi->tx_ring_free--;
james@1007 319 txN->id = get_id_from_freelist(xi);
james@1007 320 NT_ASSERT(!xi->tx_shadows[txN->id].cb);
james@1007 321 offset = MmGetMdlByteOffset(pi.curr_mdl) + pi.curr_mdl_offset;
james@1007 322 pfn = MmGetMdlPfnArray(pi.curr_mdl)[offset >> PAGE_SHIFT];
james@1007 323 txN->offset = (USHORT)offset & (PAGE_SIZE - 1);
james@1007 324 txN->gref = XnGrantAccess(xi->handle, (ULONG)pfn, FALSE, gref, (ULONG)'XNTX');
james@1007 325 NT_ASSERT(xi->tx_shadows[txN->id].gref == INVALID_GRANT_REF);
james@1007 326 xi->tx_shadows[txN->id].gref = txN->gref;
james@1007 327 //ASSERT(sg->Elements[sg_element].Length > sg_offset);
james@1007 328 txN->size = (USHORT)length;
james@1007 329 NT_ASSERT(txN->offset + txN->size <= PAGE_SIZE);
james@1007 330 NT_ASSERT(txN->size);
james@1007 331 NT_ASSERT(txN->gref != INVALID_GRANT_REF);
james@1007 332 remaining -= length;
james@1007 333 tx_length += length;
james@1007 334 }
james@1007 335 tx0->size = tx0->size + txN->size;
james@1007 336 txN->flags = NETTXF_more_data;
james@1007 337 XenNet_EatData(&pi, length);
james@1007 338 }
james@1007 339 txN->flags &= ~NETTXF_more_data;
james@1007 340 NT_ASSERT(tx0->size == pi.total_length);
james@1007 341 NT_ASSERT(!xi->tx_shadows[txN->id].packet);
james@1007 342 xi->tx_shadows[txN->id].packet = packet;
james@1007 343
james@1007 344 #if NTDDI_VERSION < NTDDI_VISTA
james@1007 345 if (ndis_lso) {
james@1007 346 NDIS_PER_PACKET_INFO_FROM_PACKET(packet, TcpLargeSendPacketInfo) = UlongToPtr(tx_length - MAX_ETH_HEADER_LENGTH - pi.ip4_header_length - pi.tcp_header_length);
james@1007 347 }
james@1007 348 #else
james@1007 349 switch (lso_info.Transmit.Type) {
james@1007 350 case NDIS_TCP_LARGE_SEND_OFFLOAD_V1_TYPE:
james@1007 351 lso_info.LsoV1TransmitComplete.TcpPayload = tx_length - MAX_ETH_HEADER_LENGTH - pi.ip4_header_length - pi.tcp_header_length;
james@1007 352 break;
james@1007 353 case NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE:
james@1007 354 break;
james@1007 355 }
james@1007 356 #endif
james@1007 357
james@1007 358 xi->tx_outstanding++;
james@1007 359 return TRUE;
james@1007 360 }
james@1007 361
james@1007 362 /* Called at DISPATCH_LEVEL with tx_lock held */
james@1007 363 static VOID
james@1007 364 XenNet_SendQueuedPackets(struct xennet_info *xi)
james@1007 365 {
james@1007 366 PLIST_ENTRY entry;
james@1007 367 #if NTDDI_VERSION < NTDDI_VISTA
james@1007 368 PNDIS_PACKET packet;
james@1007 369 #else
james@1007 370 PNET_BUFFER packet;
james@1007 371 #endif
james@1007 372 int notify;
james@1007 373
james@1007 374 if (xi->device_state != DEVICE_STATE_ACTIVE)
james@1007 375 return;
james@1007 376
james@1007 377 while (!IsListEmpty(&xi->tx_waiting_pkt_list)) {
james@1007 378 entry = RemoveHeadList(&xi->tx_waiting_pkt_list);
james@1007 379 #if NTDDI_VERSION < NTDDI_VISTA
james@1007 380 packet = CONTAINING_RECORD(entry, NDIS_PACKET, PACKET_LIST_ENTRY_FIELD);
james@1007 381 #else
james@1007 382 packet = CONTAINING_RECORD(entry, NET_BUFFER, NB_LIST_ENTRY_FIELD);
james@1007 383 #endif
james@1007 384 if (!XenNet_HWSendPacket(xi, packet)) {
james@1007 385 InsertHeadList(&xi->tx_waiting_pkt_list, entry);
james@1007 386 break;
james@1007 387 }
james@1007 388 }
james@1007 389
james@1007 390 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&xi->tx_ring, notify);
james@1007 391 if (notify) {
james@1007 392 XnNotify(xi->handle, xi->event_channel);
james@1007 393 }
james@1007 394 }
james@1007 395
james@1007 396 // Called at DISPATCH_LEVEL
james@1007 397 VOID
james@1007 398 XenNet_TxBufferGC(struct xennet_info *xi, BOOLEAN dont_set_event) {
james@1007 399 RING_IDX cons, prod;
james@1007 400 #if NTDDI_VERSION < NTDDI_VISTA
james@1007 401 PNDIS_PACKET head = NULL, tail = NULL;
james@1007 402 PNDIS_PACKET packet;
james@1007 403 #else
james@1007 404 PNET_BUFFER_LIST head = NULL;
james@1007 405 PNET_BUFFER_LIST tail = NULL;
james@1007 406 PNET_BUFFER_LIST nbl;
james@1007 407 PNET_BUFFER packet;
james@1007 408 #endif
james@1007 409 ULONG tx_packets = 0;
james@1007 410
james@1007 411 NT_ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL);
james@1007 412
james@1007 413 KeAcquireSpinLockAtDpcLevel(&xi->tx_lock);
james@1007 414
james@1007 415 if (xi->device_state != DEVICE_STATE_ACTIVE && !xi->tx_outstanding) {
james@1007 416 /* there is a chance that our Dpc had been queued just before the shutdown... */
james@1007 417 KeSetEvent(&xi->tx_idle_event, IO_NO_INCREMENT, FALSE);
james@1007 418 KeReleaseSpinLockFromDpcLevel(&xi->tx_lock);
james@1007 419 return;
james@1007 420 }
james@1007 421
james@1007 422 do {
james@1007 423 prod = xi->tx_ring.sring->rsp_prod;
james@1007 424 KeMemoryBarrier(); /* Ensure we see responses up to 'rsp_prod'. */
james@1007 425
james@1007 426 for (cons = xi->tx_ring.rsp_cons; cons != prod; cons++)
james@1007 427 {
james@1007 428 struct netif_tx_response *txrsp;
james@1007 429 tx_shadow_t *shadow;
james@1007 430
james@1007 431 txrsp = RING_GET_RESPONSE(&xi->tx_ring, cons);
james@1007 432
james@1007 433 xi->tx_ring_free++;
james@1007 434
james@1007 435 if (txrsp->status == NETIF_RSP_NULL) {
james@1007 436 continue;
james@1007 437 }
james@1007 438
james@1007 439 shadow = &xi->tx_shadows[txrsp->id];
james@1007 440 if (shadow->cb) {
james@1007 441 NdisFreeToNPagedLookasideList(&xi->tx_lookaside_list, shadow->cb);
james@1007 442 shadow->cb = NULL;
james@1007 443 }
james@1007 444
james@1007 445 if (shadow->gref != INVALID_GRANT_REF) {
james@1007 446 XnEndAccess(xi->handle, shadow->gref, FALSE, (ULONG)'XNTX');
james@1007 447 shadow->gref = INVALID_GRANT_REF;
james@1007 448 }
james@1007 449
james@1007 450 if (shadow->packet) {
james@1007 451 PMDL mdl;
james@1007 452 PUCHAR header;
james@1007 453 packet = shadow->packet;
james@1007 454 #if NTDDI_VERSION < NTDDI_VISTA
james@1007 455 mdl = NDIS_PACKET_FIRST_NDIS_BUFFER(packet);
james@1007 456 #else
james@1007 457 mdl = NET_BUFFER_CURRENT_MDL(packet);
james@1007 458 #endif
james@1007 459 #pragma warning(suppress:28193) /* already mapped so guaranteed to work */
james@1007 460 header = MmGetSystemAddressForMdlSafe(mdl, LowPagePriority);
james@1007 461 #if NTDDI_VERSION < NTDDI_VISTA
james@1007 462 #else
james@1007 463 header += NET_BUFFER_CURRENT_MDL_OFFSET(nb);
james@1007 464 #endif
james@1007 465
james@1007 466 #if NTDDI_VERSION < NTDDI_VISTA
james@1007 467 #else
james@1007 468 xi->stats.ifHCOutOctets += nb->DataLength;
james@1007 469 if (nb->DataLength < XN_HDR_SIZE || !(header[0] & 0x01)) {
james@1007 470 /* unicast or tiny packet */
james@1007 471 xi->stats.ifHCOutUcastPkts++;
james@1007 472 xi->stats.ifHCOutUcastOctets += nb->DataLength;
james@1007 473 }
james@1007 474 else if (header[0] == 0xFF && header[1] == 0xFF && header[2] == 0xFF
james@1007 475 && header[3] == 0xFF && header[4] == 0xFF && header[5] == 0xFF) {
james@1007 476 /* broadcast */
james@1007 477 xi->stats.ifHCOutBroadcastPkts++;
james@1007 478 xi->stats.ifHCOutBroadcastOctets += nb->DataLength;
james@1007 479 } else {
james@1007 480 /* multicast */
james@1007 481 xi->stats.ifHCOutMulticastPkts++;
james@1007 482 xi->stats.ifHCOutMulticastOctets += nb->DataLength;
james@1007 483 }
james@1007 484 #endif
james@1007 485
james@1007 486 #if NTDDI_VERSION < NTDDI_VISTA
james@1007 487 PACKET_NEXT_PACKET(packet) = NULL;
james@1007 488 if (!head) {
james@1007 489 head = packet;
james@1007 490 } else {
james@1007 491 PACKET_NEXT_PACKET(tail) = packet;
james@1007 492 }
james@1007 493 tail = packet;
james@1007 494 #else
james@1007 495 nbl = NB_NBL(nb);
james@1007 496 NBL_REF(nbl)--;
james@1007 497 if (!NBL_REF(nbl)) {
james@1007 498 NET_BUFFER_LIST_NEXT_NBL(nbl) = NULL;
james@1007 499 if (nbl_head) {
james@1007 500 NET_BUFFER_LIST_NEXT_NBL(nbl_tail) = nbl;
james@1007 501 nbl_tail = nbl;
james@1007 502 } else {
james@1007 503 nbl_head = nbl;
james@1007 504 nbl_tail = nbl;
james@1007 505 }
james@1007 506 }
james@1007 507 #endif
james@1007 508 shadow->packet = NULL;
james@1007 509 tx_packets++;
james@1007 510 }
james@1007 511 put_id_on_freelist(xi, txrsp->id);
james@1007 512 }
james@1007 513
james@1007 514 xi->tx_ring.rsp_cons = prod;
james@1007 515 /* resist the temptation to set the event more than +1... it breaks things */
james@1007 516 if (!dont_set_event)
james@1007 517 xi->tx_ring.sring->rsp_event = prod + 1;
james@1007 518 KeMemoryBarrier();
james@1007 519 } while (prod != xi->tx_ring.sring->rsp_prod);
james@1007 520
james@1007 521 /* if queued packets, send them now */
james@1007 522 XenNet_SendQueuedPackets(xi);
james@1007 523
james@1007 524 KeReleaseSpinLockFromDpcLevel(&xi->tx_lock);
james@1007 525
james@1007 526 /* must be done without holding any locks */
james@1007 527 #if NTDDI_VERSION < NTDDI_VISTA
james@1007 528 while (head) {
james@1007 529 packet = (PNDIS_PACKET)head;
james@1007 530 head = PACKET_NEXT_PACKET(packet);
james@1007 531 NdisMSendComplete(xi->adapter_handle, packet, NDIS_STATUS_SUCCESS);
james@1007 532 }
james@1007 533 #else
james@1007 534 if (nbl_head)
james@1007 535 NdisMSendNetBufferListsComplete(xi->adapter_handle, nbl_head, NDIS_SEND_COMPLETE_FLAGS_DISPATCH_LEVEL);
james@1007 536 #endif
james@1007 537
james@1007 538 /* must be done after we have truly given back all packets */
james@1007 539 KeAcquireSpinLockAtDpcLevel(&xi->tx_lock);
james@1007 540 xi->tx_outstanding -= tx_packets;
james@1007 541 if (xi->device_state != DEVICE_STATE_ACTIVE && !xi->tx_outstanding) {
james@1007 542 KeSetEvent(&xi->tx_idle_event, IO_NO_INCREMENT, FALSE);
james@1007 543 }
james@1007 544 KeReleaseSpinLockFromDpcLevel(&xi->tx_lock);
james@1007 545 }
james@1007 546
james@1007 547 #if NTDDI_VERSION < NTDDI_VISTA
james@1007 548 VOID
james@1007 549 XenNet_SendPackets(NDIS_HANDLE MiniportAdapterContext, PPNDIS_PACKET PacketArray, UINT NumberOfPackets) {
james@1007 550 struct xennet_info *xi = MiniportAdapterContext;
james@1007 551 PNDIS_PACKET packet;
james@1007 552 UINT i;
james@1007 553 PLIST_ENTRY entry;
james@1007 554 KIRQL old_irql;
james@1007 555
james@1007 556 if (xi->device_state != DEVICE_STATE_ACTIVE) {
james@1007 557 for (i = 0; i < NumberOfPackets; i++) {
james@1007 558 NdisMSendComplete(xi->adapter_handle, PacketArray[i], NDIS_STATUS_FAILURE);
james@1007 559 }
james@1007 560 return;
james@1007 561 }
james@1007 562
james@1007 563 KeAcquireSpinLock(&xi->tx_lock, &old_irql);
james@1007 564
james@1007 565 for (i = 0; i < NumberOfPackets; i++) {
james@1007 566 packet = PacketArray[i];
james@1007 567 ASSERT(packet);
james@1007 568 entry = &PACKET_LIST_ENTRY(packet);
james@1007 569 InsertTailList(&xi->tx_waiting_pkt_list, entry);
james@1007 570 }
james@1007 571
james@1007 572 XenNet_SendQueuedPackets(xi);
james@1007 573
james@1007 574 KeReleaseSpinLock(&xi->tx_lock, old_irql);
james@1007 575 }
james@914 576 #else
james@1007 577 // called at <= DISPATCH_LEVEL
james@1007 578 VOID
james@1007 579 XenNet_SendNetBufferLists(
james@1007 580 NDIS_HANDLE adapter_context,
james@1007 581 PNET_BUFFER_LIST nb_lists,
james@1007 582 NDIS_PORT_NUMBER port_number,
james@1007 583 ULONG send_flags) {
james@1007 584 struct xennet_info *xi = adapter_context;
james@1007 585 PLIST_ENTRY nb_entry;
james@1007 586 KIRQL old_irql;
james@1007 587 PNET_BUFFER_LIST curr_nbl;
james@1007 588 PNET_BUFFER_LIST next_nbl;
james@1007 589
james@1007 590 UNREFERENCED_PARAMETER(port_number);
james@1007 591
james@1007 592 if (xi->device_state == DEVICE_STATE_INACTIVE) {
james@1007 593 curr_nbl = nb_lists;
james@1007 594 for (curr_nbl = nb_lists; curr_nbl; curr_nbl = NET_BUFFER_LIST_NEXT_NBL(curr_nbl)) {
james@1007 595 curr_nbl->Status = NDIS_STATUS_FAILURE;
james@1007 596 }
james@1007 597 /* this actions the whole list */
james@1007 598 NdisMSendNetBufferListsComplete(xi->adapter_handle, nb_lists, (send_flags & NDIS_SEND_FLAGS_DISPATCH_LEVEL)?NDIS_SEND_COMPLETE_FLAGS_DISPATCH_LEVEL:0);
james@1007 599 return;
james@1007 600 }
james@1007 601
james@1007 602 KeAcquireSpinLock(&xi->tx_lock, &old_irql);
james@1007 603
james@1007 604 for (curr_nbl = nb_lists; curr_nbl; curr_nbl = next_nbl) {
james@1007 605 PNET_BUFFER curr_nb;
james@1007 606 NBL_REF(curr_nbl) = 0;
james@1007 607 next_nbl = NET_BUFFER_LIST_NEXT_NBL(curr_nbl);
james@1007 608 NET_BUFFER_LIST_NEXT_NBL(curr_nbl) = NULL;
james@1007 609 for (curr_nb = NET_BUFFER_LIST_FIRST_NB(curr_nbl); curr_nb; curr_nb = NET_BUFFER_NEXT_NB(curr_nb)) {
james@1007 610 NB_NBL(curr_nb) = curr_nbl;
james@1007 611 nb_entry = &NB_LIST_ENTRY(curr_nb);
james@1007 612 InsertTailList(&xi->tx_waiting_pkt_list, nb_entry);
james@1007 613 NBL_REF(curr_nbl)++;
james@1007 614 }
james@1007 615 }
james@1007 616
james@1007 617 XenNet_SendQueuedPackets(xi);
james@1007 618
james@1007 619 KeReleaseSpinLock(&xi->tx_lock, old_irql);
james@1007 620 }
james@821 621 #endif
james@1007 622
james@1007 623 #if 0
james@1007 624 VOID
james@1007 625 XenNet_CancelSend(NDIS_HANDLE adapter_context, PVOID cancel_id)
james@1007 626 {
james@1007 627 UNREFERENCED_PARAMETER(adapter_context);
james@1007 628 UNREFERENCED_PARAMETER(cancel_id);
james@1007 629 FUNCTION_ENTER();
james@1007 630
james@1007 631 FUNCTION_EXIT();
james@1007 632 }
james@1007 633 #endif
james@1007 634
james@1007 635 BOOLEAN
james@1007 636 XenNet_TxInit(xennet_info_t *xi) {
james@1007 637 USHORT i;
james@1007 638 UNREFERENCED_PARAMETER(xi);
james@1007 639
james@1007 640 KeInitializeSpinLock(&xi->tx_lock);
james@1007 641 InitializeListHead(&xi->tx_waiting_pkt_list);
james@1007 642
james@1007 643 KeInitializeEvent(&xi->tx_idle_event, SynchronizationEvent, FALSE);
james@1007 644 xi->tx_outstanding = 0;
james@1007 645 xi->tx_ring_free = NET_TX_RING_SIZE;
james@1007 646
james@1007 647 NdisInitializeNPagedLookasideList(&xi->tx_lookaside_list, NULL, NULL, 0,
james@1007 648 PAGE_SIZE, XENNET_POOL_TAG, 0);
james@1007 649
james@1007 650 xi->tx_id_free = 0;
james@1007 651 for (i = 0; i < NET_TX_RING_SIZE; i++) {
james@1007 652 xi->tx_shadows[i].gref = INVALID_GRANT_REF;
james@1007 653 xi->tx_shadows[i].cb = NULL;
james@1007 654 put_id_on_freelist(xi, i);
james@1007 655 }
james@1007 656
james@1007 657 return TRUE;
james@1007 658 }
james@1007 659
james@1007 660 /*
james@1007 661 The ring is completely closed down now. We just need to empty anything left
james@1007 662 on our freelists and harvest anything left on the rings.
james@1007 663 */
james@1007 664
james@1007 665 BOOLEAN
james@1007 666 XenNet_TxShutdown(xennet_info_t *xi) {
james@1007 667 #if NTDDI_VERSION < NTDDI_VISTA
james@1007 668 PNDIS_PACKET packet;
james@1007 669 #else
james@1007 670 PNET_BUFFER packet;
james@1007 671 PNET_BUFFER_LIST nbl;
james@1007 672 #endif
james@1007 673 PLIST_ENTRY entry;
james@1007 674 LARGE_INTEGER timeout;
james@1007 675 KIRQL old_irql;
james@1007 676
james@1007 677 FUNCTION_ENTER();
james@1007 678
james@1007 679 KeAcquireSpinLock(&xi->tx_lock, &old_irql);
james@1007 680
james@1007 681 while (xi->tx_outstanding) {
james@1007 682 KeReleaseSpinLock(&xi->tx_lock, old_irql);
james@1007 683 KdPrint((__DRIVER_NAME " Waiting for %d remaining packets to be sent\n", xi->tx_outstanding));
james@1007 684 timeout.QuadPart = -1 * 1 * 1000 * 1000 * 10; /* 1 second */
james@1007 685 KeWaitForSingleObject(&xi->tx_idle_event, Executive, KernelMode, FALSE, &timeout);
james@1007 686 KeAcquireSpinLock(&xi->tx_lock, &old_irql);
james@1007 687 }
james@1007 688 KeReleaseSpinLock(&xi->tx_lock, old_irql);
james@1007 689
james@1007 690 /* Free packets in tx queue */
james@1007 691 while (!IsListEmpty(&xi->tx_waiting_pkt_list)) {
james@1007 692 entry = RemoveHeadList(&xi->tx_waiting_pkt_list);
james@1007 693 #if NTDDI_VERSION < NTDDI_VISTA
james@1007 694 packet = CONTAINING_RECORD(entry, NDIS_PACKET, PACKET_LIST_ENTRY_FIELD);
james@1007 695 NdisMSendComplete(xi->adapter_handle, packet, NDIS_STATUS_FAILURE);
james@1007 696 entry = RemoveHeadList(&xi->tx_waiting_pkt_list);
james@1007 697 #else
james@1007 698 packet = CONTAINING_RECORD(nb_entry, NET_BUFFER, NB_LIST_ENTRY_FIELD);
james@1007 699 nbl = NB_NBL(nb);
james@1007 700 NBL_REF(nbl)--;
james@1007 701 if (!NBL_REF(nbl)) {
james@1007 702 nbl->Status = NDIS_STATUS_FAILURE;
james@1007 703 NdisMSendNetBufferListsComplete(xi->adapter_handle, nbl, NDIS_SEND_COMPLETE_FLAGS_DISPATCH_LEVEL);
james@1007 704 }
james@1007 705 #endif
james@1007 706 }
james@1007 707 NdisDeleteNPagedLookasideList(&xi->tx_lookaside_list);
james@1007 708
james@1007 709 FUNCTION_EXIT();
james@1007 710
james@1007 711 return TRUE;
james@1007 712 }