summaryrefslogtreecommitdiff
path: root/MdeModulePkg/Universal/Network/SnpDxe/Initialize.c
blob: 69154fc0c71e831396bd8b55b9754f90c168d52f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/** @file
Copyright (c) 2004 - 2007, Intel Corporation
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution.  The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

Module name:
  initialize.c

Abstract:

Revision history:
  2000-Feb-09 M(f)J   Genesis.

**/


#include "Snp.h"

VOID
EFIAPI
SnpWaitForPacketNotify (
  IN EFI_EVENT  Event,
  IN VOID       *SnpPtr
  );


/**
  this routine calls undi to initialize the interface.

  @param  snp                   pointer to snp driver structure
  @param  CableDetectFlag       Do/don't detect the cable (depending on what undi
                                supports)


**/
EFI_STATUS
pxe_init (
  SNP_DRIVER *snp,
  UINT16     CableDetectFlag
  )
{
  PXE_CPB_INITIALIZE  *cpb;
  VOID                *addr;
  EFI_STATUS          Status;

  cpb = snp->cpb;
  if (snp->tx_rx_bufsize != 0) {
    Status = snp->IoFncs->AllocateBuffer (
                            snp->IoFncs,
                            AllocateAnyPages,
                            EfiBootServicesData,
                            SNP_MEM_PAGES (snp->tx_rx_bufsize),
                            &addr,
                            0
                            );

    if (Status != EFI_SUCCESS) {
      DEBUG (
        (EFI_D_ERROR,
        "\nsnp->pxe_init()  AllocateBuffer  %xh (%r)\n",
        Status,
        Status)
        );

      return Status;
    }

    ASSERT (addr);

    snp->tx_rx_buffer = addr;
  }

  cpb->MemoryAddr   = (UINT64)(UINTN) snp->tx_rx_buffer;

  cpb->MemoryLength = snp->tx_rx_bufsize;

  //
  // let UNDI decide/detect these values
  //
  cpb->LinkSpeed      = 0;
  cpb->TxBufCnt       = 0;
  cpb->TxBufSize      = 0;
  cpb->RxBufCnt       = 0;
  cpb->RxBufSize      = 0;

  cpb->DuplexMode         = PXE_DUPLEX_DEFAULT;

  cpb->LoopBackMode       = LOOPBACK_NORMAL;

  snp->cdb.OpCode     = PXE_OPCODE_INITIALIZE;
  snp->cdb.OpFlags    = CableDetectFlag;

  snp->cdb.CPBsize    = sizeof (PXE_CPB_INITIALIZE);
  snp->cdb.DBsize     = sizeof (PXE_DB_INITIALIZE);

  snp->cdb.CPBaddr    = (UINT64)(UINTN) snp->cpb;
  snp->cdb.DBaddr     = (UINT64)(UINTN) snp->db;

  snp->cdb.StatCode   = PXE_STATCODE_INITIALIZE;
  snp->cdb.StatFlags  = PXE_STATFLAGS_INITIALIZE;
  snp->cdb.IFnum      = snp->if_num;
  snp->cdb.Control    = PXE_CONTROL_LAST_CDB_IN_LIST;

  DEBUG ((EFI_D_NET, "\nsnp->undi.initialize()  "));

  (*snp->issue_undi32_command) ((UINT64)(UINTN) &snp->cdb);

  if (snp->cdb.StatCode == PXE_STATCODE_SUCCESS) {
    snp->mode.State = EfiSimpleNetworkInitialized;

    Status          = EFI_SUCCESS;
  } else {
    DEBUG (
      (EFI_D_WARN,
      "\nsnp->undi.initialize()  %xh:%xh\n",
      snp->cdb.StatFlags,
      snp->cdb.StatCode)
      );

    if (snp->tx_rx_buffer != NULL) {
      snp->IoFncs->FreeBuffer (
                    snp->IoFncs,
                    SNP_MEM_PAGES (snp->tx_rx_bufsize),
                    (VOID *) snp->tx_rx_buffer
                    );
    }

    snp->tx_rx_buffer = NULL;

    Status            = EFI_DEVICE_ERROR;
  }

  return Status;
}


/**
  This is the SNP interface routine for initializing the interface
  This routine basically retrieves snp structure, checks the SNP state and
  calls the pxe_initialize routine to actually do the undi initialization

  @param  this                  context pointer
  @param  extra_rx_buffer_size  optional parameter, indicates extra space for
                                rx_buffers
  @param  extra_tx_buffer_size  optional parameter, indicates extra space for
                                tx_buffers


**/
EFI_STATUS
EFIAPI
snp_undi32_initialize (
  IN EFI_SIMPLE_NETWORK_PROTOCOL *this,
  IN UINTN                       extra_rx_buffer_size OPTIONAL,
  IN UINTN                       extra_tx_buffer_size OPTIONAL
  )
{
  EFI_STATUS  EfiStatus;
  SNP_DRIVER  *snp;
  EFI_TPL     OldTpl;

  //
  //
  //
  if (this == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (this);

  OldTpl = gBS->RaiseTPL (TPL_CALLBACK);

  if (snp == NULL) {
    EfiStatus = EFI_INVALID_PARAMETER;
    goto ON_EXIT;
  }

  switch (snp->mode.State) {
  case EfiSimpleNetworkStarted:
    break;

  case EfiSimpleNetworkStopped:
    EfiStatus = EFI_NOT_STARTED;
    goto ON_EXIT;

  default:
    EfiStatus = EFI_DEVICE_ERROR;
    goto ON_EXIT;
  }

  EfiStatus = gBS->CreateEvent (
                    EVT_NOTIFY_WAIT,
                    TPL_NOTIFY,
                    &SnpWaitForPacketNotify,
                    snp,
                    &snp->snp.WaitForPacket
                    );

  if (EFI_ERROR (EfiStatus)) {
    snp->snp.WaitForPacket = NULL;
    EfiStatus = EFI_DEVICE_ERROR;
    goto ON_EXIT;
  }
  //
  //
  //
  snp->mode.MCastFilterCount      = 0;
  snp->mode.ReceiveFilterSetting  = 0;
  ZeroMem (snp->mode.MCastFilter, sizeof snp->mode.MCastFilter);
  CopyMem (
    &snp->mode.CurrentAddress,
    &snp->mode.PermanentAddress,
    sizeof (EFI_MAC_ADDRESS)
    );

  //
  // Compute tx/rx buffer sizes based on UNDI init info and parameters.
  //
  snp->tx_rx_bufsize = (UINT32) (snp->init_info.MemoryRequired + extra_rx_buffer_size + extra_tx_buffer_size);

  if (snp->mode.MediaPresentSupported) {
    if (pxe_init (snp, PXE_OPFLAGS_INITIALIZE_DETECT_CABLE) == EFI_SUCCESS) {
      snp->mode.MediaPresent = TRUE;
      goto ON_EXIT;
    }
  }

  snp->mode.MediaPresent  = FALSE;

  EfiStatus               = pxe_init (snp, PXE_OPFLAGS_INITIALIZE_DO_NOT_DETECT_CABLE);

  if (EFI_ERROR (EfiStatus)) {
    gBS->CloseEvent (snp->snp.WaitForPacket);
  }

ON_EXIT:
  gBS->RestoreTPL (OldTpl);

  return EfiStatus;
}