]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/MnpDxe/MnpMain.c
Fixed EBC build issues.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / MnpDxe / MnpMain.c
1 /** @file
2
3 Copyright (c) 2005 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 MnpMain.c
15
16 Abstract:
17
18 Implementation of Managed Network Protocol public services.
19
20
21 **/
22
23 #include <Library/BaseMemoryLib.h>
24 #include <Library/BaseLib.h>
25
26 #include "MnpImpl.h"
27
28
29 /**
30 Get configuration data of this instance.
31
32 @param This Pointer to the Managed Network Protocol.
33 @param MnpConfigData Pointer to strorage for MNP operational
34 parameters.
35 @param SnpModeData Pointer to strorage for SNP operational
36 parameters.
37
38 @retval EFI_SUCCESS The operation completed successfully.
39 @retval EFI_INVALID_PARAMETER This is NULL.
40 @retval EFI_NOT_STARTED This MNP child driver instance has not been
41 configured The default values are returned in
42 MnpConfigData if it is not NULL.
43
44 **/
45 EFI_STATUS
46 EFIAPI
47 MnpGetModeData (
48 IN EFI_MANAGED_NETWORK_PROTOCOL *This,
49 OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL,
50 OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL
51 )
52 {
53 MNP_INSTANCE_DATA *Instance;
54 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
55 EFI_TPL OldTpl;
56 EFI_STATUS Status;
57
58 if (This == NULL) {
59
60 return EFI_INVALID_PARAMETER;
61 }
62
63 Instance = MNP_INSTANCE_DATA_FROM_THIS (This);
64
65 OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);
66
67 if (MnpConfigData != NULL) {
68 //
69 // Copy the instance configuration data.
70 //
71 CopyMem (MnpConfigData, &Instance->ConfigData, sizeof (EFI_MANAGED_NETWORK_CONFIG_DATA));
72 }
73
74 if (SnpModeData != NULL) {
75 //
76 // Copy the underlayer Snp mode data.
77 //
78 Snp = Instance->MnpServiceData->Snp;
79 CopyMem (SnpModeData, Snp->Mode, sizeof (EFI_SIMPLE_NETWORK_MODE));
80 }
81
82 if (!Instance->Configured) {
83 Status = EFI_NOT_STARTED;
84 } else {
85 Status = EFI_SUCCESS;
86 }
87
88 NET_RESTORE_TPL (OldTpl);
89
90 return Status;
91 }
92
93
94 /**
95 Set or clear the operational parameters for the MNP child driver.
96
97 @param This Pointer to the Managed Network Protocol.
98 @param MnpConfigData Pointer to the configuration data that will be
99 assigned to the MNP child driver instance. If
100 NULL, the MNP child driver instance is reset to
101 startup defaults and all pending transmit and
102 receive requests are flushed.
103
104 @retval EFI_SUCCESS The operation completed successfully.
105 @retval EFI_INVALID_PARAMETER One or more parameter is invalid.
106 @retval EFI_OUT_OF_RESOURCES Required system resources (usually memory) could
107 not be allocated.
108 @retval EFI_UNSUPPORTED EnableReceiveTimestamps is TRUE, this
109 implementation doesn't support it.
110 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred.
111 @retval Other The MNP child driver instance has been reset to
112 startup defaults.
113
114 **/
115 EFI_STATUS
116 EFIAPI
117 MnpConfigure (
118 IN EFI_MANAGED_NETWORK_PROTOCOL *This,
119 IN EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL
120 )
121 {
122 MNP_INSTANCE_DATA *Instance;
123 EFI_TPL OldTpl;
124 EFI_STATUS Status;
125
126 if ((This == NULL) ||
127 ((MnpConfigData != NULL) &&
128 (MnpConfigData->ProtocolTypeFilter > 0) &&
129 (MnpConfigData->ProtocolTypeFilter <= 1500))) {
130
131 return EFI_INVALID_PARAMETER;
132 }
133
134 Instance = MNP_INSTANCE_DATA_FROM_THIS (This);
135
136 OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);
137
138 if ((MnpConfigData == NULL) && (!Instance->Configured)) {
139 //
140 // If the instance is not configured and a reset is requested, just return.
141 //
142 Status = EFI_SUCCESS;
143 goto ON_EXIT;
144 }
145
146 //
147 // Configure the instance.
148 //
149 Status = MnpConfigureInstance (Instance, MnpConfigData);
150
151 ON_EXIT:
152 NET_RESTORE_TPL (OldTpl);
153
154 return Status;
155 }
156
157
158 /**
159 Translate a multicast IP address to a multicast hardware (MAC) address.
160
161 @param This Pointer to the Managed Network Protocol.
162 @param Ipv6Flag Set to TRUE if IpAddress is an IPv6 multicast
163 address. Set to FALSE if IpAddress is an IPv4
164 multicast address.
165 @param IpAddress Pointer to the multicast IP address to convert.
166 @param MacAddress Pointer to the resulting multicast MAC address.
167
168 @retval EFI_SUCCESS The operation completed successfully.
169 @retval EFI_INVALID_PARAMETER One or more parameter is invalid.
170 @retval EFI_NOT_STARTED This MNP child driver instance has not been
171 configured.
172 @retval EFI_UNSUPPORTED Ipv6Flag is TRUE, this implementation doesn't
173 supported it.
174 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred.
175 @retval Other The address could not be converted.
176
177 **/
178 EFI_STATUS
179 EFIAPI
180 MnpMcastIpToMac (
181 IN EFI_MANAGED_NETWORK_PROTOCOL *This,
182 IN BOOLEAN Ipv6Flag,
183 IN EFI_IP_ADDRESS *IpAddress,
184 OUT EFI_MAC_ADDRESS *MacAddress
185 )
186 {
187 EFI_STATUS Status;
188 MNP_INSTANCE_DATA *Instance;
189 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
190 EFI_TPL OldTpl;
191
192 if ((This == NULL) || (IpAddress == NULL) || (MacAddress == NULL)) {
193
194 return EFI_INVALID_PARAMETER;
195 }
196
197 if (Ipv6Flag) {
198 //
199 // Currently IPv6 isn't supported.
200 //
201 return EFI_UNSUPPORTED;
202 }
203
204 if (!IP4_IS_MULTICAST (EFI_NTOHL (*IpAddress))) {
205 //
206 // The IPv4 address passed in is not a multicast address.
207 //
208 return EFI_INVALID_PARAMETER;
209 }
210
211 Instance = MNP_INSTANCE_DATA_FROM_THIS (This);
212
213 OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);
214
215 if (!Instance->Configured) {
216
217 Status = EFI_NOT_STARTED;
218 goto ON_EXIT;
219 }
220
221 Snp = Instance->MnpServiceData->Snp;
222 ASSERT (Snp != NULL);
223
224 if (Snp->Mode->IfType == NET_IFTYPE_ETHERNET) {
225 //
226 // Translate the IPv4 address into a multicast MAC address if the NIC is an
227 // ethernet NIC.
228 //
229 MacAddress->Addr[0] = 0x01;
230 MacAddress->Addr[1] = 0x00;
231 MacAddress->Addr[2] = 0x5E;
232 MacAddress->Addr[3] = (UINT8) (IpAddress->v4.Addr[1] & 0x7F);
233 MacAddress->Addr[4] = IpAddress->v4.Addr[2];
234 MacAddress->Addr[5] = IpAddress->v4.Addr[3];
235
236 Status = EFI_SUCCESS;
237 } else {
238 //
239 // Invoke Snp to translate the multicast IP address.
240 //
241 Status = Snp->MCastIpToMac (
242 Snp,
243 Ipv6Flag,
244 IpAddress,
245 MacAddress
246 );
247 }
248
249 ON_EXIT:
250 NET_RESTORE_TPL (OldTpl);
251
252 return Status;
253 }
254
255
256 /**
257 Enable or disable receie filters for multicast address.
258
259 @param This Pointer to the Managed Network Protocol.
260 @param JoinFlag Set to TRUE to join this multicast group. Set to
261 FALSE to leave this multicast group.
262 @param MacAddress Pointer to the multicast MAC group (address) to
263 join or leave.
264
265 @retval EFI_SUCCESS The operation completed successfully.
266 @retval EFI_INVALID_PARAMETER One or more parameter is invalid
267 @retval EFI_NOT_STARTED This MNP child driver instance has not been
268 configured.
269 @retval EFI_ALREADY_STARTED The supplied multicast group is already joined.
270 @retval EFI_NOT_FOUND The supplied multicast group is not joined.
271 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred.
272 @retval Other The requested operation could not be completed.
273 The MNP multicast group settings are unchanged.
274
275 **/
276 EFI_STATUS
277 EFIAPI
278 MnpGroups (
279 IN EFI_MANAGED_NETWORK_PROTOCOL *This,
280 IN BOOLEAN JoinFlag,
281 IN EFI_MAC_ADDRESS *MacAddress OPTIONAL
282 )
283 {
284 MNP_INSTANCE_DATA *Instance;
285 EFI_SIMPLE_NETWORK_MODE *SnpMode;
286 MNP_GROUP_CONTROL_BLOCK *GroupCtrlBlk;
287 MNP_GROUP_ADDRESS *GroupAddress;
288 NET_LIST_ENTRY *ListEntry;
289 BOOLEAN AddressExist;
290 EFI_TPL OldTpl;
291 EFI_STATUS Status;
292
293 if (This == NULL || (JoinFlag && (MacAddress == NULL))) {
294 //
295 // This is NULL, or it's a join operation but MacAddress is NULL.
296 //
297 return EFI_INVALID_PARAMETER;
298 }
299
300 Instance = MNP_INSTANCE_DATA_FROM_THIS (This);
301 SnpMode = Instance->MnpServiceData->Snp->Mode;
302
303 OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);
304
305 if (!Instance->Configured) {
306
307 Status = EFI_NOT_STARTED;
308 goto ON_EXIT;
309 }
310
311 if ((!Instance->ConfigData.EnableMulticastReceive) ||
312 ((MacAddress != NULL) && !NET_MAC_IS_MULTICAST (MacAddress, &SnpMode->BroadcastAddress, SnpMode->HwAddressSize))) {
313 //
314 // The instance isn't configured to do mulitcast receive. OR
315 // the passed in MacAddress is not a mutlticast mac address.
316 //
317 Status = EFI_INVALID_PARAMETER;
318 goto ON_EXIT;
319 }
320
321 Status = EFI_SUCCESS;
322 AddressExist = FALSE;
323 GroupCtrlBlk = NULL;
324
325 if (MacAddress != NULL) {
326 //
327 // Search the instance's GroupCtrlBlkList to find the specific address.
328 //
329 NET_LIST_FOR_EACH (ListEntry, &Instance->GroupCtrlBlkList) {
330
331 GroupCtrlBlk = NET_LIST_USER_STRUCT (
332 ListEntry,
333 MNP_GROUP_CONTROL_BLOCK,
334 CtrlBlkEntry
335 );
336 GroupAddress = GroupCtrlBlk->GroupAddress;
337 if (0 == NetCompareMem (
338 MacAddress,
339 &GroupAddress->Address,
340 SnpMode->HwAddressSize
341 )) {
342 //
343 // There is already the same multicast mac address configured.
344 //
345 AddressExist = TRUE;
346 break;
347 }
348 }
349
350 if (JoinFlag && AddressExist) {
351 //
352 // The multicast mac address to join already exists.
353 //
354 Status = EFI_ALREADY_STARTED;
355 }
356
357 if (!JoinFlag && !AddressExist) {
358 //
359 // The multicast mac address to leave doesn't exist in this instance.
360 //
361 Status = EFI_NOT_FOUND;
362 }
363
364 if (EFI_ERROR (Status)) {
365 goto ON_EXIT;
366 }
367 } else if (NetListIsEmpty (&Instance->GroupCtrlBlkList)) {
368 //
369 // The MacAddress is NULL and there is no configured multicast mac address,
370 // just return.
371 //
372 goto ON_EXIT;
373 }
374
375 //
376 // OK, it is time to take action.
377 //
378 Status = MnpGroupOp (Instance, JoinFlag, MacAddress, GroupCtrlBlk);
379
380 ON_EXIT:
381 NET_RESTORE_TPL (OldTpl);
382
383 return Status;
384 }
385
386
387 /**
388 Place an outgoing packet into the transmit queue.
389
390 @param This Pointer to the Managed Network Protocol.
391 @param Token Pointer to a token associated with the transmit
392 data descriptor.
393
394 @retval EFI_SUCCESS The operation completed successfully.
395 @retval EFI_INVALID_PARAMETER One or more parameter is invalid
396 @retval EFI_NOT_STARTED This MNP child driver instance has not been
397 configured.
398 @retval EFI_ACCESS_DENIED The transmit completion token is already in the
399 transmit queue.
400 @retval EFI_OUT_OF_RESOURCES The transmit data could not be queued due to a
401 lack of system resources (usually memory).
402 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
403 The MNP child driver instance has been reset to
404 startup defaults.
405 @retval EFI_NOT_READY The transmit request could not be queued because
406 the transmit queue is full.
407
408 **/
409 EFI_STATUS
410 EFIAPI
411 MnpTransmit (
412 IN EFI_MANAGED_NETWORK_PROTOCOL *This,
413 IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token
414 )
415 {
416 EFI_STATUS Status;
417 MNP_INSTANCE_DATA *Instance;
418 MNP_SERVICE_DATA *MnpServiceData;
419 UINT8 *PktBuf;
420 UINT32 PktLen;
421 EFI_TPL OldTpl;
422
423 if ((This == NULL) || (Token == NULL)) {
424
425 return EFI_INVALID_PARAMETER;
426 }
427
428 Instance = MNP_INSTANCE_DATA_FROM_THIS (This);
429
430 OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);
431
432 if (!Instance->Configured) {
433
434 Status = EFI_NOT_STARTED;
435 goto ON_EXIT;
436 }
437
438 if (!MnpIsValidTxToken (Instance, Token)) {
439 //
440 // The Token is invalid.
441 //
442 Status = EFI_INVALID_PARAMETER;
443 goto ON_EXIT;
444 }
445
446 MnpServiceData = Instance->MnpServiceData;
447 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
448
449 //
450 // Build the tx packet
451 //
452 MnpBuildTxPacket (MnpServiceData, Token->Packet.TxData, &PktBuf, &PktLen);
453
454 //
455 // OK, send the packet synchronously.
456 //
457 Status = MnpSyncSendPacket (MnpServiceData, PktBuf, PktLen, Token);
458
459 ON_EXIT:
460 NET_RESTORE_TPL (OldTpl);
461
462 return Status;
463 }
464
465
466 /**
467 Place an asynchronous receiving request into the receiving queue.
468
469 @param This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL
470 instance.
471 @param Token Pointer to a token associated with the receive
472 data descriptor.
473
474 @retval EFI_SUCCESS The receive completion token was cached.
475 @retval EFI_NOT_STARTED This MNP child driver instance has not been
476 configured.
477 @retval EFI_INVALID_PARAMETER One or more parameter is invalid.
478 @retval EFI_OUT_OF_RESOURCES The transmit data could not be queued due to a
479 lack of system resources (usually memory).
480 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
481 The MNP child driver instance has been reset to
482 startup defaults.
483 @retval EFI_ACCESS_DENIED The receive completion token was already in the
484 receive queue.
485 @retval EFI_NOT_READY The receive request could not be queued because
486 the receive queue is full.
487
488 **/
489 EFI_STATUS
490 EFIAPI
491 MnpReceive (
492 IN EFI_MANAGED_NETWORK_PROTOCOL *This,
493 IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token
494 )
495 {
496 EFI_STATUS Status;
497 MNP_INSTANCE_DATA *Instance;
498 EFI_TPL OldTpl;
499
500 if ((This == NULL) || (Token == NULL) || (Token->Event == NULL)) {
501
502 return EFI_INVALID_PARAMETER;
503 }
504
505 Instance = MNP_INSTANCE_DATA_FROM_THIS (This);
506
507 OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);
508
509 if (!Instance->Configured) {
510
511 Status = EFI_NOT_STARTED;
512 goto ON_EXIT;
513 }
514
515 //
516 // Check whether this token(event) is already in the rx token queue.
517 //
518 Status = NetMapIterate (&Instance->RxTokenMap, MnpTokenExist, (VOID *) Token);
519 if (EFI_ERROR (Status)) {
520
521 goto ON_EXIT;
522 }
523
524 //
525 // Insert the Token into the RxTokenMap.
526 //
527 Status = NetMapInsertTail (&Instance->RxTokenMap, (VOID *) Token, NULL);
528
529 if (!EFI_ERROR (Status)) {
530 //
531 // Try to deliver any buffered packets.
532 //
533 Status = MnpInstanceDeliverPacket (Instance);
534 }
535
536 ON_EXIT:
537 NET_RESTORE_TPL (OldTpl);
538
539 return Status;
540 }
541
542
543 /**
544 Abort a pending transmit or receive request.
545
546 @param This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL
547 instance.
548 @param Token Pointer to a token that has been issued by
549 EFI_MANAGED_NETWORK_PROTOCOL.Transmit() or
550 EFI_MANAGED_NETWORK_PROTOCOL.Receive(). If NULL,
551 all pending tokens are aborted.
552
553 @retval EFI_SUCCESS The asynchronous I/O request was aborted and
554 Token->Event was signaled.
555 @retval EFI_NOT_STARTED This MNP child driver instance has not been
556 configured.
557 @retval EFI_INVALID_PARAMETER This is NULL.
558 @retval EFI_NOT_FOUND The asynchronous I/O request was not found in the
559 transmit or receive queue. It has either completed
560 or was not issued by Transmit() and Receive().
561
562 **/
563 EFI_STATUS
564 EFIAPI
565 MnpCancel (
566 IN EFI_MANAGED_NETWORK_PROTOCOL *This,
567 IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token OPTIONAL
568 )
569 {
570 EFI_STATUS Status;
571 MNP_INSTANCE_DATA *Instance;
572 EFI_TPL OldTpl;
573
574 if (This == NULL) {
575
576 return EFI_INVALID_PARAMETER;
577 }
578
579 Instance = MNP_INSTANCE_DATA_FROM_THIS (This);
580
581 OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);
582
583 if (!Instance->Configured) {
584
585 Status = EFI_NOT_STARTED;
586 goto ON_EXIT;
587 }
588
589 //
590 // Iterate the RxTokenMap to cancel the specified Token.
591 //
592 Status = NetMapIterate (&Instance->RxTokenMap, MnpCancelTokens, (VOID *) Token);
593
594 if (Token != NULL) {
595
596 Status = (Status == EFI_ABORTED) ? EFI_SUCCESS : EFI_NOT_FOUND;
597 }
598
599 ON_EXIT:
600 NET_RESTORE_TPL (OldTpl);
601
602 return Status;
603 }
604
605
606 /**
607 Poll the network interface to do transmit/receive work.
608
609 @param This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL
610 instance.
611
612 @retval EFI_SUCCESS Incoming or outgoing data was processed.
613 @retval EFI_NOT_STARTED This MNP child driver instance has not been
614 configured.
615 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
616 The MNP child driver instance has been reset to
617 startup defaults.
618 @retval EFI_NOT_READY No incoming or outgoing data was processed.
619 @retval EFI_TIMEOUT Data was dropped out of the transmit and/or
620 receive queue.
621
622 **/
623 EFI_STATUS
624 EFIAPI
625 MnpPoll (
626 IN EFI_MANAGED_NETWORK_PROTOCOL *This
627 )
628 {
629 EFI_STATUS Status;
630 MNP_INSTANCE_DATA *Instance;
631 EFI_TPL OldTpl;
632
633 if (This == NULL) {
634 return EFI_INVALID_PARAMETER;
635 }
636
637 Instance = MNP_INSTANCE_DATA_FROM_THIS (This);
638
639 OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);
640
641 if (!Instance->Configured) {
642 Status = EFI_NOT_STARTED;
643 goto ON_EXIT;
644 }
645
646 //
647 // Try to receive packets.
648 //
649 Status = MnpReceivePacket (Instance->MnpServiceData);
650
651 ON_EXIT:
652 NET_RESTORE_TPL (OldTpl);
653
654 return Status;
655 }