Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / i386 / interface / pxeparent / pxeparent.c
1 /*
2  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <ipxe/dhcp.h>
23 #include <ipxe/profile.h>
24 #include <pxeparent.h>
25 #include <pxe_api.h>
26 #include <pxe_types.h>
27 #include <pxe.h>
28
29 /** @file
30  *
31  * Call interface to parent PXE stack
32  *
33  */
34
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 )
40
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 */
46         struct profiler p2r;
47         /** Time spent in external code */
48         struct profiler ext;
49         /** Time spent transitioning back to protected mode */
50         struct profiler r2p;
51 };
52
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" },
59 };
60
61 /** PXENV_UNDI_ISR profiler
62  *
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().
66  */
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" },
72 };
73
74 /** PXE unknown API call profiler
75  *
76  * This profiler can be used to measure the overhead of a dummy PXE
77  * API call.
78  */
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" },
84 };
85
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" },
92 };
93
94 /**
95  * Name PXE API call
96  *
97  * @v function          API call number
98  * @ret name            API call name
99  */
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";
145         /*
146          * Duplicate case value; this is a bug in the PXE specification.
147          *
148          *      case PXENV_UNDI_GET_STATE:
149          *              return "PXENV_UNDI_GET_STATE";
150          */
151         case PXENV_UNDI_ISR:
152                 return "PXENV_UNDI_ISR";
153         case PXENV_GET_CACHED_INFO:
154                 return "PXENV_GET_CACHED_INFO";
155         default:
156                 return "UNKNOWN API CALL";
157         }
158 }
159
160 /**
161  * Determine applicable profiler pair (for debugging)
162  *
163  * @v function          API call number
164  * @ret profiler        Profiler
165  */
166 static struct pxeparent_profiler * pxeparent_profiler ( unsigned int function ){
167
168         /* Determine applicable profiler */
169         switch ( function ) {
170         case PXENV_UNDI_TRANSMIT:
171                 return &pxeparent_tx_profiler;
172         case PXENV_UNDI_ISR:
173                 return &pxeparent_isr_profiler;
174         case PXENV_UNKNOWN:
175                 return &pxeparent_unknown_profiler;
176         default:
177                 return &pxeparent_misc_profiler;
178         }
179 }
180
181 /**
182  * PXE parent parameter block
183  *
184  * Used as the parameter block for all parent PXE API calls.  Resides
185  * in base memory.
186  */
187 static union u_PXENV_ANY __bss16 ( pxeparent_params );
188 #define pxeparent_params __use_data16 ( pxeparent_params )
189
190 /** PXE parent entry point
191  *
192  * Used as the indirection vector for all parent PXE API calls.  Resides in
193  * base memory.
194  */
195 SEGOFF16_t __bss16 ( pxeparent_entry_point );
196 #define pxeparent_entry_point __use_data16 ( pxeparent_entry_point )
197
198 /**
199  * Issue parent PXE API call
200  *
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
206  */
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 );
210         PXENV_EXIT_t exit;
211         unsigned long started;
212         unsigned long stopped;
213         int discard_D;
214         int rc;
215
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 ) );
220
221         /* Call real-mode entry point.  This calling convention will
222          * work with both the !PXE and the PXENV+ entry points.
223          */
224         profile_start ( &profiler->total );
225         __asm__ __volatile__ ( REAL_CODE ( "pushl %%ebp\n\t" /* gcc bug */
226                                            "rdtsc\n\t"
227                                            "pushl %%eax\n\t"
228                                            "pushw %%es\n\t"
229                                            "pushw %%di\n\t"
230                                            "pushw %%bx\n\t"
231                                            "lcall *pxeparent_entry_point\n\t"
232                                            "movw %%ax, %%bx\n\t"
233                                            "rdtsc\n\t"
234                                            "addw $6, %%sp\n\t"
235                                            "popl %%edx\n\t"
236                                            "popl %%ebp\n\t" /* gcc bug */ )
237                                : "=a" ( stopped ), "=d" ( started ),
238                                  "=b" ( exit ), "=D" ( discard_D )
239                                : "b" ( function ),
240                                  "D" ( __from_data16 ( &pxeparent_params ) )
241                                : "ecx", "esi" );
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 ));
249
250         /* Determine return status code based on PXENV_EXIT and
251          * PXENV_STATUS
252          */
253         rc = ( ( exit == PXENV_EXIT_SUCCESS ) ?
254                0 : -EPXECALL ( pxeparent_params.Status ) );
255
256         /* If anything goes wrong, print as much debug information as
257          * it's possible to give.
258          */
259         if ( rc != 0 ) {
260                 SEGOFF16_t rm_params = {
261                         .segment = rm_ds,
262                         .offset = __from_data16 ( &pxeparent_params ),
263                 };
264
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 );
276         }
277
278         /* Copy parameter block back */
279         memcpy ( params, &pxeparent_params, params_len );
280
281         return rc;
282 }
283