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