iPXE
undi.c
Go to the documentation of this file.
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
20FILE_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 */
48static 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 */
66static int undipci_probe ( struct pci_device *pci ) {
67 struct undi_device *undi;
68 struct undi_rom *undirom;
69 int rc;
70
71 /* Allocate UNDI device structure */
72 undi = zalloc ( sizeof ( *undi ) );
73 if ( ! undi )
74 return -ENOMEM;
75 pci_set_drvdata ( pci, undi );
76
77 /* Find/create our pixie */
78 if ( preloaded_undi.pci_busdevfn == pci->busdevfn ) {
79 /* Claim preloaded UNDI device */
80 DBGC ( undi, "UNDI %p using preloaded UNDI device\n", undi );
81 memcpy ( undi, &preloaded_undi, sizeof ( *undi ) );
82 memset ( &preloaded_undi, 0, sizeof ( preloaded_undi ) );
83 } else {
84 /* Find UNDI ROM for PCI device */
85 if ( ! ( undirom = undipci_find_rom ( pci ) ) ) {
86 rc = -ENODEV;
87 goto err_find_rom;
88 }
89
90 /* Call UNDI ROM loader to create pixie */
91 if ( ( rc = undi_load_pci ( undi, undirom,
92 pci->busdevfn ) ) != 0 ) {
93 goto err_load_pci;
94 }
95 }
96
97 /* Create network device */
98 if ( ( rc = undinet_probe ( undi, &pci->dev ) ) != 0 )
99 goto err_undinet_probe;
100
101 return 0;
102
103 err_undinet_probe:
104 undi_unload ( undi );
105 err_find_rom:
106 err_load_pci:
107 free ( undi );
108 pci_set_drvdata ( pci, NULL );
109 return rc;
110}
111
112/**
113 * Remove PCI device
114 *
115 * @v pci PCI device
116 */
117static void undipci_remove ( struct pci_device *pci ) {
118 struct undi_device *undi = pci_get_drvdata ( pci );
119
120 undinet_remove ( undi );
121 undi_unload ( undi );
122 free ( undi );
123 pci_set_drvdata ( pci, NULL );
124}
125
126static struct pci_device_id undipci_nics[] = {
127 PCI_ROM ( 0xffff, 0xffff, "undipci", "UNDI (PCI)", 0 ),
128};
129
130struct pci_driver undipci_driver __pci_driver_fallback = {
131 .ids = undipci_nics,
132 .id_count = ( sizeof ( undipci_nics ) / sizeof ( undipci_nics[0] ) ),
134 .probe = undipci_probe,
135 .remove = undipci_remove,
136};
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
#define PCI_CLASS_NETWORK
Definition Pci22.h:157
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
#define DBGC(...)
Definition compiler.h:505
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define ENOMEM
Not enough space.
Definition errno.h:535
#define ENODEV
No such device.
Definition errno.h:510
String functions.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
void * memset(void *dest, int character, size_t len) __nonnull
void * zalloc(size_t size)
Allocate cleared memory.
Definition malloc.c:662
unsigned long pci_bar_start(struct pci_device *pci, unsigned int reg)
Find the start of a PCI BAR.
Definition pci.c:97
PCI bus.
#define PCI_ANY_ID
Match-anything ID.
Definition pci.h:187
#define __pci_driver_fallback
Declare a fallback PCI driver.
Definition pci.h:281
static void pci_set_drvdata(struct pci_device *pci, void *priv)
Set PCI driver-private data.
Definition pci.h:366
#define PCI_ROM(_vendor, _device, _name, _description, _data)
Definition pci.h:308
#define PCI_ROM_ADDRESS
PCI expansion ROM base address.
Definition pci.h:82
static void * pci_get_drvdata(struct pci_device *pci)
Get PCI driver-private data.
Definition pci.h:376
#define PCI_CLASS_ID(base, sub, progif)
Construct PCI class ID.
Definition pci.h:203
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
A PCI device ID list entry.
Definition pci.h:175
A PCI device.
Definition pci.h:211
uint32_t busdevfn
Segment, bus, device, and function (bus:dev.fn) number.
Definition pci.h:238
struct device dev
Generic device.
Definition pci.h:213
uint16_t vendor
Vendor ID.
Definition pci.h:228
uint16_t device
Device ID.
Definition pci.h:230
A PCI driver.
Definition pci.h:252
An UNDI device.
Definition undi.h:22
An UNDI ROM.
Definition undirom.h:29
static void undipci_remove(struct pci_device *pci)
Remove PCI device.
Definition undi.c:117
static int undipci_probe(struct pci_device *pci)
Probe PCI device.
Definition undi.c:66
static struct pci_device_id undipci_nics[]
Definition undi.c:126
static struct undi_rom * undipci_find_rom(struct pci_device *pci)
Find UNDI ROM for PCI device.
Definition undi.c:48
UNDI driver.
int undi_unload(struct undi_device *undi)
Unload a pixie.
Definition undiload.c:161
UNDI load/unload.
static int undi_load_pci(struct undi_device *undi, struct undi_rom *undirom, unsigned int pci_busdevfn)
Call UNDI loader to create a pixie.
Definition undiload.h:26
int undinet_probe(struct undi_device *undi, struct device *dev)
Probe UNDI device.
Definition undinet.c:931
void undinet_remove(struct undi_device *undi)
Remove UNDI device.
Definition undinet.c:1089
UNDI network device driver.
Preloaded UNDI stack.
#define preloaded_undi
Definition undipreload.h:16
struct undi_rom * undirom_find_pci(unsigned int vendor_id, unsigned int device_id, unsigned int rombase)
Find UNDI ROM for PCI device.
Definition undirom.c:211
UNDI expansion ROMs.