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