Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / i386 / drivers / net / undi.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 <stdio.h>
25 #include <string.h>
26 #include <ipxe/pci.h>
27 #include <undi.h>
28 #include <undirom.h>
29 #include <undiload.h>
30 #include <undinet.h>
31 #include <undipreload.h>
32
33 /** @file
34  *
35  * UNDI PCI driver
36  *
37  */
38
39 /**
40  * Find UNDI ROM for PCI device
41  *
42  * @v pci               PCI device
43  * @ret undirom         UNDI ROM, or NULL
44  *
45  * Try to find a driver for this device.  Try an exact match on the
46  * ROM address first, then fall back to a vendor/device ID match only
47  */
48 static struct undi_rom * undipci_find_rom ( struct pci_device *pci ) {
49         struct undi_rom *undirom;
50         unsigned long rombase;
51         
52         rombase = pci_bar_start ( pci, PCI_ROM_ADDRESS );
53         undirom = undirom_find_pci ( pci->vendor, pci->device, rombase );
54         if ( ! undirom )
55                 undirom = undirom_find_pci ( pci->vendor, pci->device, 0 );
56         return undirom;
57 }
58
59 /**
60  * Probe PCI device
61  *
62  * @v pci               PCI device
63  * @v id                PCI ID
64  * @ret rc              Return status code
65  */
66 static int undipci_probe ( struct pci_device *pci ) {
67         struct undi_device *undi;
68         struct undi_rom *undirom;
69         int rc;
70
71         /* Ignore non-network devices */
72         if ( PCI_BASE_CLASS ( pci->class ) != PCI_BASE_CLASS_NETWORK )
73                 return -ENOTTY;
74
75         /* Allocate UNDI device structure */
76         undi = zalloc ( sizeof ( *undi ) );
77         if ( ! undi )
78                 return -ENOMEM;
79         pci_set_drvdata ( pci, undi );
80
81         /* Find/create our pixie */
82         if ( preloaded_undi.pci_busdevfn == pci->busdevfn ) {
83                 /* Claim preloaded UNDI device */
84                 DBGC ( undi, "UNDI %p using preloaded UNDI device\n", undi );
85                 memcpy ( undi, &preloaded_undi, sizeof ( *undi ) );
86                 memset ( &preloaded_undi, 0, sizeof ( preloaded_undi ) );
87         } else {
88                 /* Find UNDI ROM for PCI device */
89                 if ( ! ( undirom = undipci_find_rom ( pci ) ) ) {
90                         rc = -ENODEV;
91                         goto err_find_rom;
92                 }
93
94                 /* Call UNDI ROM loader to create pixie */
95                 if ( ( rc = undi_load_pci ( undi, undirom,
96                                             pci->busdevfn ) ) != 0 ) {
97                         goto err_load_pci;
98                 }
99         }
100
101         /* Add to device hierarchy */
102         snprintf ( undi->dev.name, sizeof ( undi->dev.name ),
103                    "UNDI-%s", pci->dev.name );
104         memcpy ( &undi->dev.desc, &pci->dev.desc, sizeof ( undi->dev.desc ) );
105         undi->dev.parent = &pci->dev;
106         INIT_LIST_HEAD ( &undi->dev.children );
107         list_add ( &undi->dev.siblings, &pci->dev.children );
108
109         /* Create network device */
110         if ( ( rc = undinet_probe ( undi ) ) != 0 )
111                 goto err_undinet_probe;
112         
113         return 0;
114
115  err_undinet_probe:
116         undi_unload ( undi );
117         list_del ( &undi->dev.siblings );
118  err_find_rom:
119  err_load_pci:
120         free ( undi );
121         pci_set_drvdata ( pci, NULL );
122         return rc;
123 }
124
125 /**
126  * Remove PCI device
127  *
128  * @v pci       PCI device
129  */
130 static void undipci_remove ( struct pci_device *pci ) {
131         struct undi_device *undi = pci_get_drvdata ( pci );
132
133         undinet_remove ( undi );
134         undi_unload ( undi );
135         list_del ( &undi->dev.siblings );
136         free ( undi );
137         pci_set_drvdata ( pci, NULL );
138 }
139
140 static struct pci_device_id undipci_nics[] = {
141 PCI_ROM ( 0xffff, 0xffff, "undipci", "UNDI (PCI)", 0 ),
142 };
143
144 struct pci_driver undipci_driver __pci_driver_fallback = {
145         .ids = undipci_nics,
146         .id_count = ( sizeof ( undipci_nics ) / sizeof ( undipci_nics[0] ) ),
147         .probe = undipci_probe,
148         .remove = undipci_remove,
149 };