2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
22 #include <ipxe/dhcp.h>
23 #include <ipxe/profile.h>
24 #include <pxeparent.h>
26 #include <pxe_types.h>
31 * Call interface to parent PXE stack
35 /* Disambiguate the various error causes */
36 #define EINFO_EPXECALL \
37 __einfo_uniqify ( EINFO_EPLATFORM, 0x01, \
38 "External PXE API error" )
39 #define EPXECALL( status ) EPLATFORM ( EINFO_EPXECALL, status )
41 /** A parent PXE API call profiler */
42 struct pxeparent_profiler {
43 /** Total time spent performing REAL_CALL() */
44 struct profiler total;
45 /** Time spent transitioning to real mode */
47 /** Time spent in external code */
49 /** Time spent transitioning back to protected mode */
53 /** PXENV_UNDI_TRANSMIT profiler */
54 static struct pxeparent_profiler pxeparent_tx_profiler __profiler = {
55 { .name = "pxeparent.tx" },
56 { .name = "pxeparent.tx_p2r" },
57 { .name = "pxeparent.tx_ext" },
58 { .name = "pxeparent.tx_r2p" },
61 /** PXENV_UNDI_ISR profiler
63 * Note that this profiler will not see calls to
64 * PXENV_UNDI_ISR_IN_START, which are handled by the UNDI ISR and do
65 * not go via pxeparent_call().
67 static struct pxeparent_profiler pxeparent_isr_profiler __profiler = {
68 { .name = "pxeparent.isr" },
69 { .name = "pxeparent.isr_p2r" },
70 { .name = "pxeparent.isr_ext" },
71 { .name = "pxeparent.isr_r2p" },
74 /** PXE unknown API call profiler
76 * This profiler can be used to measure the overhead of a dummy PXE
79 static struct pxeparent_profiler pxeparent_unknown_profiler __profiler = {
80 { .name = "pxeparent.unknown" },
81 { .name = "pxeparent.unknown_p2r" },
82 { .name = "pxeparent.unknown_ext" },
83 { .name = "pxeparent.unknown_r2p" },
86 /** Miscellaneous PXE API call profiler */
87 static struct pxeparent_profiler pxeparent_misc_profiler __profiler = {
88 { .name = "pxeparent.misc" },
89 { .name = "pxeparent.misc_p2r" },
90 { .name = "pxeparent.misc_ext" },
91 { .name = "pxeparent.misc_r2p" },
97 * @v function API call number
98 * @ret name API call name
100 static inline __attribute__ (( always_inline )) const char *
101 pxeparent_function_name ( unsigned int function ) {
102 switch ( function ) {
103 case PXENV_START_UNDI:
104 return "PXENV_START_UNDI";
105 case PXENV_STOP_UNDI:
106 return "PXENV_STOP_UNDI";
107 case PXENV_UNDI_STARTUP:
108 return "PXENV_UNDI_STARTUP";
109 case PXENV_UNDI_CLEANUP:
110 return "PXENV_UNDI_CLEANUP";
111 case PXENV_UNDI_INITIALIZE:
112 return "PXENV_UNDI_INITIALIZE";
113 case PXENV_UNDI_RESET_ADAPTER:
114 return "PXENV_UNDI_RESET_ADAPTER";
115 case PXENV_UNDI_SHUTDOWN:
116 return "PXENV_UNDI_SHUTDOWN";
117 case PXENV_UNDI_OPEN:
118 return "PXENV_UNDI_OPEN";
119 case PXENV_UNDI_CLOSE:
120 return "PXENV_UNDI_CLOSE";
121 case PXENV_UNDI_TRANSMIT:
122 return "PXENV_UNDI_TRANSMIT";
123 case PXENV_UNDI_SET_MCAST_ADDRESS:
124 return "PXENV_UNDI_SET_MCAST_ADDRESS";
125 case PXENV_UNDI_SET_STATION_ADDRESS:
126 return "PXENV_UNDI_SET_STATION_ADDRESS";
127 case PXENV_UNDI_SET_PACKET_FILTER:
128 return "PXENV_UNDI_SET_PACKET_FILTER";
129 case PXENV_UNDI_GET_INFORMATION:
130 return "PXENV_UNDI_GET_INFORMATION";
131 case PXENV_UNDI_GET_STATISTICS:
132 return "PXENV_UNDI_GET_STATISTICS";
133 case PXENV_UNDI_CLEAR_STATISTICS:
134 return "PXENV_UNDI_CLEAR_STATISTICS";
135 case PXENV_UNDI_INITIATE_DIAGS:
136 return "PXENV_UNDI_INITIATE_DIAGS";
137 case PXENV_UNDI_FORCE_INTERRUPT:
138 return "PXENV_UNDI_FORCE_INTERRUPT";
139 case PXENV_UNDI_GET_MCAST_ADDRESS:
140 return "PXENV_UNDI_GET_MCAST_ADDRESS";
141 case PXENV_UNDI_GET_NIC_TYPE:
142 return "PXENV_UNDI_GET_NIC_TYPE";
143 case PXENV_UNDI_GET_IFACE_INFO:
144 return "PXENV_UNDI_GET_IFACE_INFO";
146 * Duplicate case value; this is a bug in the PXE specification.
148 * case PXENV_UNDI_GET_STATE:
149 * return "PXENV_UNDI_GET_STATE";
152 return "PXENV_UNDI_ISR";
153 case PXENV_GET_CACHED_INFO:
154 return "PXENV_GET_CACHED_INFO";
156 return "UNKNOWN API CALL";
161 * Determine applicable profiler pair (for debugging)
163 * @v function API call number
164 * @ret profiler Profiler
166 static struct pxeparent_profiler * pxeparent_profiler ( unsigned int function ){
168 /* Determine applicable profiler */
169 switch ( function ) {
170 case PXENV_UNDI_TRANSMIT:
171 return &pxeparent_tx_profiler;
173 return &pxeparent_isr_profiler;
175 return &pxeparent_unknown_profiler;
177 return &pxeparent_misc_profiler;
182 * PXE parent parameter block
184 * Used as the parameter block for all parent PXE API calls. Resides
187 static union u_PXENV_ANY __bss16 ( pxeparent_params );
188 #define pxeparent_params __use_data16 ( pxeparent_params )
190 /** PXE parent entry point
192 * Used as the indirection vector for all parent PXE API calls. Resides in
195 SEGOFF16_t __bss16 ( pxeparent_entry_point );
196 #define pxeparent_entry_point __use_data16 ( pxeparent_entry_point )
199 * Issue parent PXE API call
201 * @v entry Parent PXE stack entry point
202 * @v function API call number
203 * @v params PXE parameter block
204 * @v params_len Length of PXE parameter block
205 * @ret rc Return status code
207 int pxeparent_call ( SEGOFF16_t entry, unsigned int function,
208 void *params, size_t params_len ) {
209 struct pxeparent_profiler *profiler = pxeparent_profiler ( function );
211 unsigned long started;
212 unsigned long stopped;
216 /* Copy parameter block and entry point */
217 assert ( params_len <= sizeof ( pxeparent_params ) );
218 memcpy ( &pxeparent_params, params, params_len );
219 memcpy ( &pxeparent_entry_point, &entry, sizeof ( entry ) );
221 /* Call real-mode entry point. This calling convention will
222 * work with both the !PXE and the PXENV+ entry points.
224 profile_start ( &profiler->total );
225 __asm__ __volatile__ ( REAL_CODE ( "pushl %%ebp\n\t" /* gcc bug */
231 "lcall *pxeparent_entry_point\n\t"
232 "movw %%ax, %%bx\n\t"
236 "popl %%ebp\n\t" /* gcc bug */ )
237 : "=a" ( stopped ), "=d" ( started ),
238 "=b" ( exit ), "=D" ( discard_D )
240 "D" ( __from_data16 ( &pxeparent_params ) )
242 profile_stop ( &profiler->total );
243 profile_start_at ( &profiler->p2r, profile_started ( &profiler->total));
244 profile_stop_at ( &profiler->p2r, started );
245 profile_start_at ( &profiler->ext, started );
246 profile_stop_at ( &profiler->ext, stopped );
247 profile_start_at ( &profiler->r2p, stopped );
248 profile_stop_at ( &profiler->r2p, profile_stopped ( &profiler->total ));
250 /* Determine return status code based on PXENV_EXIT and
253 rc = ( ( exit == PXENV_EXIT_SUCCESS ) ?
254 0 : -EPXECALL ( pxeparent_params.Status ) );
256 /* If anything goes wrong, print as much debug information as
257 * it's possible to give.
260 SEGOFF16_t rm_params = {
262 .offset = __from_data16 ( &pxeparent_params ),
265 DBG ( "PXEPARENT %s failed: %s\n",
266 pxeparent_function_name ( function ), strerror ( rc ) );
267 DBG ( "PXEPARENT parameters at %04x:%04x length "
268 "%#02zx, entry point at %04x:%04x\n",
269 rm_params.segment, rm_params.offset, params_len,
270 pxeparent_entry_point.segment,
271 pxeparent_entry_point.offset );
272 DBG ( "PXEPARENT parameters provided:\n" );
273 DBG_HDA ( rm_params, params, params_len );
274 DBG ( "PXEPARENT parameters returned:\n" );
275 DBG_HDA ( rm_params, &pxeparent_params, params_len );
278 /* Copy parameter block back */
279 memcpy ( params, &pxeparent_params, params_len );