]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/MnpDxe/MnpMain.c
sync comments, fix function header, rename variable name to follow coding style.
[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 = gBS->RaiseTPL (TPL_CALLBACK);
66
67 if (MnpConfigData != NULL) {
68 //
69 // Copy the instance configuration data.
70 //
71 CopyMem (MnpConfigData, &Instance->ConfigData, sizeof (*MnpConfigData));
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 (*SnpModeData));
80 }
81
82 if (!Instance->Configured) {
83 Status = EFI_NOT_STARTED;
84 } else {
85 Status = EFI_SUCCESS;
86 }
87
88 gBS->RestoreTPL (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 = gBS->RaiseTPL (TPL_CALLBACK);
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 gBS->RestoreTPL (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 = gBS->RaiseTPL (TPL_CALLBACK);
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 gBS->RestoreTPL (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 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 = gBS->RaiseTPL (TPL_CALLBACK);
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 == CompareMem (
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 (IsListEmpty (&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 gBS->RestoreTPL (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 = gBS->RaiseTPL (TPL_CALLBACK);
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 gBS->RestoreTPL (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 = gBS->RaiseTPL (TPL_CALLBACK);
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 // Dispatch the DPC queued by the NotifyFunction of Token->Event.
537 //
538 NetLibDispatchDpc ();
539 }
540
541 ON_EXIT:
542 gBS->RestoreTPL (OldTpl);
543
544 return Status;
545 }
546
547
548 /**
549 Abort a pending transmit or receive request.
550
551 @param This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL
552 instance.
553 @param Token Pointer to a token that has been issued by
554 EFI_MANAGED_NETWORK_PROTOCOL.Transmit() or
555 EFI_MANAGED_NETWORK_PROTOCOL.Receive(). If NULL,
556 all pending tokens are aborted.
557
558 @retval EFI_SUCCESS The asynchronous I/O request was aborted and
559 Token->Event was signaled.
560 @retval EFI_NOT_STARTED This MNP child driver instance has not been
561 configured.
562 @retval EFI_INVALID_PARAMETER This is NULL.
563 @retval EFI_NOT_FOUND The asynchronous I/O request was not found in the
564 transmit or receive queue. It has either completed
565 or was not issued by Transmit() and Receive().
566
567 **/
568 EFI_STATUS
569 EFIAPI
570 MnpCancel (
571 IN EFI_MANAGED_NETWORK_PROTOCOL *This,
572 IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token OPTIONAL
573 )
574 {
575 EFI_STATUS Status;
576 MNP_INSTANCE_DATA *Instance;
577 EFI_TPL OldTpl;
578
579 if (This == NULL) {
580
581 return EFI_INVALID_PARAMETER;
582 }
583
584 Instance = MNP_INSTANCE_DATA_FROM_THIS (This);
585
586 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
587
588 if (!Instance->Configured) {
589
590 Status = EFI_NOT_STARTED;
591 goto ON_EXIT;
592 }
593
594 //
595 // Iterate the RxTokenMap to cancel the specified Token.
596 //
597 Status = NetMapIterate (&Instance->RxTokenMap, MnpCancelTokens, (VOID *) Token);
598
599 if (Token != NULL) {
600
601 Status = (Status == EFI_ABORTED) ? EFI_SUCCESS : EFI_NOT_FOUND;
602 }
603
604 //
605 // Dispatch the DPC queued by the NotifyFunction of the cancled token's events.
606 //
607 NetLibDispatchDpc ();
608
609 ON_EXIT:
610 gBS->RestoreTPL (OldTpl);
611
612 return Status;
613 }
614
615
616 /**
617 Poll the network interface to do transmit/receive work.
618
619 @param This Pointer to the EFI_MANAGED_NETWORK_PROTOCOL
620 instance.
621
622 @retval EFI_SUCCESS Incoming or outgoing data was processed.
623 @retval EFI_NOT_STARTED This MNP child driver instance has not been
624 configured.
625 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
626 The MNP child driver instance has been reset to
627 startup defaults.
628 @retval EFI_NOT_READY No incoming or outgoing data was processed.
629 @retval EFI_TIMEOUT Data was dropped out of the transmit and/or
630 receive queue.
631
632 **/
633 EFI_STATUS
634 EFIAPI
635 MnpPoll (
636 IN EFI_MANAGED_NETWORK_PROTOCOL *This
637 )
638 {
639 EFI_STATUS Status;
640 MNP_INSTANCE_DATA *Instance;
641 EFI_TPL OldTpl;
642
643 if (This == NULL) {
644 return EFI_INVALID_PARAMETER;
645 }
646
647 Instance = MNP_INSTANCE_DATA_FROM_THIS (This);
648
649 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
650
651 if (!Instance->Configured) {
652 Status = EFI_NOT_STARTED;
653 goto ON_EXIT;
654 }
655
656 //
657 // Try to receive packets.
658 //
659 Status = MnpReceivePacket (Instance->MnpServiceData);
660
661 NetLibDispatchDpc ();
662
663 ON_EXIT:
664 gBS->RestoreTPL (OldTpl);
665
666 return Status;
667 }
668