These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / i386 / drivers / net / undiload.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  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <pxe.h>
30 #include <realmode.h>
31 #include <bios.h>
32 #include <pnpbios.h>
33 #include <basemem.h>
34 #include <ipxe/pci.h>
35 #include <undi.h>
36 #include <undirom.h>
37 #include <undiload.h>
38
39 /** @file
40  *
41  * UNDI load/unload
42  *
43  */
44
45 /* Disambiguate the various error causes */
46 #define EINFO_EUNDILOAD                                                 \
47         __einfo_uniqify ( EINFO_EPLATFORM, 0x01,                        \
48                           "UNDI loader error" )
49 #define EUNDILOAD( status ) EPLATFORM ( EINFO_EUNDILOAD, status )
50
51 /** Parameter block for calling UNDI loader */
52 static struct s_UNDI_LOADER __bss16 ( undi_loader );
53 #define undi_loader __use_data16 ( undi_loader )
54
55 /** UNDI loader entry point */
56 static SEGOFF16_t __bss16 ( undi_loader_entry );
57 #define undi_loader_entry __use_data16 ( undi_loader_entry )
58
59 /**
60  * Call UNDI loader to create a pixie
61  *
62  * @v undi              UNDI device
63  * @v undirom           UNDI ROM
64  * @ret rc              Return status code
65  */
66 int undi_load ( struct undi_device *undi, struct undi_rom *undirom ) {
67         struct s_PXE ppxe;
68         unsigned int fbms_seg;
69         uint16_t exit;
70         int rc;
71
72         /* Only one UNDI instance may be loaded at any given time */
73         if ( undi_loader_entry.segment ) {
74                 DBG ( "UNDI %p cannot load multiple instances\n", undi );
75                 return -EBUSY;
76         }
77
78         /* Set up START_UNDI parameters */
79         memset ( &undi_loader, 0, sizeof ( undi_loader ) );
80         undi_loader.AX = undi->pci_busdevfn;
81         undi_loader.BX = undi->isapnp_csn;
82         undi_loader.DX = undi->isapnp_read_port;
83         undi_loader.ES = BIOS_SEG;
84         undi_loader.DI = find_pnp_bios();
85
86         /* Allocate base memory for PXE stack */
87         undi->restore_fbms = get_fbms();
88         fbms_seg = ( undi->restore_fbms << 6 );
89         fbms_seg -= ( ( undirom->code_size + 0x0f ) >> 4 );
90         undi_loader.UNDI_CS = fbms_seg;
91         fbms_seg -= ( ( undirom->data_size + 0x0f ) >> 4 );
92         undi_loader.UNDI_DS = fbms_seg;
93
94         /* Debug info */
95         DBGC ( undi, "UNDI %p loading UNDI ROM %p to CS %04x DS %04x for ",
96                undi, undirom, undi_loader.UNDI_CS, undi_loader.UNDI_DS );
97         if ( undi->pci_busdevfn != UNDI_NO_PCI_BUSDEVFN ) {
98                 unsigned int bus = ( undi->pci_busdevfn >> 8 );
99                 unsigned int devfn = ( undi->pci_busdevfn & 0xff );
100                 DBGC ( undi, "PCI %02x:%02x.%x\n",
101                        bus, PCI_SLOT ( devfn ), PCI_FUNC ( devfn ) );
102         }
103         if ( undi->isapnp_csn != UNDI_NO_ISAPNP_CSN ) {
104                 DBGC ( undi, "ISAPnP(%04x) CSN %04x\n",
105                        undi->isapnp_read_port, undi->isapnp_csn );
106         }
107
108         /* Call loader */
109         undi_loader_entry = undirom->loader_entry;
110         __asm__ __volatile__ ( REAL_CODE ( "pushl %%ebp\n\t" /* gcc bug */
111                                            "pushw %%ds\n\t"
112                                            "pushw %%ax\n\t"
113                                            "lcall *undi_loader_entry\n\t"
114                                            "popl %%ebp\n\t" /* discard */
115                                            "popl %%ebp\n\t" /* gcc bug */ )
116                                : "=a" ( exit )
117                                : "a" ( __from_data16 ( &undi_loader ) )
118                                : "ebx", "ecx", "edx", "esi", "edi" );
119
120         if ( exit != PXENV_EXIT_SUCCESS ) {
121                 /* Clear entry point */
122                 memset ( &undi_loader_entry, 0, sizeof ( undi_loader_entry ) );
123
124                 rc = -EUNDILOAD ( undi_loader.Status );
125                 DBGC ( undi, "UNDI %p loader failed: %s\n",
126                        undi, strerror ( rc ) );
127                 return rc;
128         }
129
130         /* Populate PXE device structure */
131         undi->pxenv = undi_loader.PXENVptr;
132         undi->ppxe = undi_loader.PXEptr;
133         copy_from_real ( &ppxe, undi->ppxe.segment, undi->ppxe.offset,
134                          sizeof ( ppxe ) );
135         undi->entry = ppxe.EntryPointSP;
136         DBGC ( undi, "UNDI %p loaded PXENV+ %04x:%04x !PXE %04x:%04x "
137                "entry %04x:%04x\n", undi, undi->pxenv.segment,
138                undi->pxenv.offset, undi->ppxe.segment, undi->ppxe.offset,
139                undi->entry.segment, undi->entry.offset );
140
141         /* Update free base memory counter */
142         undi->fbms = ( fbms_seg >> 6 );
143         set_fbms ( undi->fbms );
144         DBGC ( undi, "UNDI %p using [%d,%d) kB of base memory\n",
145                undi, undi->fbms, undi->restore_fbms );
146
147         return 0;
148 }
149
150 /**
151  * Unload a pixie
152  *
153  * @v undi              UNDI device
154  * @ret rc              Return status code
155  *
156  * Erases the PXENV+ and !PXE signatures, and frees the used base
157  * memory (if possible).
158  */
159 int undi_unload ( struct undi_device *undi ) {
160         static uint32_t dead = 0xdeaddead;
161
162         DBGC ( undi, "UNDI %p unloading\n", undi );
163
164         /* Clear entry point */
165         memset ( &undi_loader_entry, 0, sizeof ( undi_loader_entry ) );
166
167         /* Erase signatures */
168         if ( undi->pxenv.segment )
169                 put_real ( dead, undi->pxenv.segment, undi->pxenv.offset );
170         if ( undi->ppxe.segment )
171                 put_real ( dead, undi->ppxe.segment, undi->ppxe.offset );
172
173         /* Free base memory, if possible */
174         if ( undi->fbms == get_fbms() ) {
175                 DBGC ( undi, "UNDI %p freeing [%d,%d) kB of base memory\n",
176                        undi, undi->fbms, undi->restore_fbms );
177                 set_fbms ( undi->restore_fbms );
178                 return 0;
179         } else {
180                 DBGC ( undi, "UNDI %p leaking [%d,%d) kB of base memory\n",
181                        undi, undi->fbms, undi->restore_fbms );
182                 return -EBUSY;
183         }
184 }