2 * Copyright (C) 2011 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
25 #include <ipxe/uaccess.h>
26 #include <ipxe/list.h>
27 #include <ipxe/ethernet.h>
28 #include <ipxe/bofm.h>
32 * IBM BladeCenter Open Fabric Manager (BOFM)
36 /** List of BOFM devices */
37 static LIST_HEAD ( bofmdevs );
40 * Register BOFM device
43 * @ret rc Return status code
45 int bofm_register ( struct bofm_device *bofm ) {
47 list_add ( &bofm->list, &bofmdevs );
48 DBG ( "BOFM: " PCI_FMT " registered using driver \"%s\"\n",
49 PCI_ARGS ( bofm->pci ), bofm->pci->id->name );
54 * Unregister BOFM device
58 void bofm_unregister ( struct bofm_device *bofm ) {
60 list_del ( &bofm->list );
61 DBG ( "BOFM: " PCI_FMT " unregistered\n", PCI_ARGS ( bofm->pci ) );
65 * Find BOFM device matching PCI bus:dev.fn address
67 * @v busdevfn PCI bus:dev.fn address
68 * @ret bofm BOFM device, or NULL
70 static struct bofm_device * bofm_find_busdevfn ( unsigned int busdevfn ) {
71 struct bofm_device *bofm;
73 list_for_each_entry ( bofm, &bofmdevs, list ) {
74 if ( bofm->pci->busdevfn == busdevfn )
81 * Find BOFM driver for PCI device
84 * @ret rc Return status code
86 int bofm_find_driver ( struct pci_device *pci ) {
87 struct pci_driver *driver;
88 struct pci_device_id *id;
91 for_each_table_entry ( driver, BOFM_DRIVERS ) {
92 for ( i = 0 ; i < driver->id_count ; i++ ) {
94 if ( ( id->vendor == pci->vendor ) &&
95 ( id->device == pci->device ) ) {
96 pci_set_driver ( pci, driver, id );
105 * Probe PCI device for BOFM driver
108 * @ret rc Return status code
110 static int bofm_probe ( struct pci_device *pci ) {
114 if ( ( rc = pci_probe ( pci ) ) != 0 ) {
115 DBG ( "BOFM: " PCI_FMT " could not load driver: %s\n",
116 PCI_ARGS ( pci ), strerror ( rc ) );
128 static void bofm_remove ( struct pci_device *pci ) {
130 /* Note that the IBM BIOS may re-read the expansion ROM after
131 * the BOFM initialisation call. The BOFM driver must ensure
132 * that the card is left in a state in which expansion ROM
133 * reads will succeed. (For example, if a card contains an
134 * embedded CPU that may issue reads to the same underlying
135 * flash device, and these reads are not locked against reads
136 * via the expansion ROM BAR, then the CPU must be stopped.)
138 * If this is not done, then occasional corrupted reads from
139 * the expansion ROM will be seen, and the BIOS may complain
140 * about a ROM checksum error.
143 DBG ( "BOFM: " PCI_FMT " removed\n", PCI_ARGS ( pci ) );
147 * Locate BOFM table section
149 * @v bofmtab BOFM table
150 * @v len Length of BOFM table
151 * @v magic Section magic
152 * @v bofmsec BOFM section header to fill in
153 * @ret offset Offset to section, or 0 if not found
155 static size_t bofm_locate_section ( userptr_t bofmtab, size_t len,
157 struct bofm_section_header *bofmsec ) {
158 size_t offset = sizeof ( struct bofm_global_header );
160 while ( offset < len ) {
161 copy_from_user ( bofmsec, bofmtab, offset,
162 sizeof ( *bofmsec ) );
163 if ( bofmsec->magic == magic )
165 if ( bofmsec->magic == BOFM_DONE_MAGIC )
167 offset += ( sizeof ( *bofmsec ) + bofmsec->length );
173 * Process BOFM Ethernet parameter entry
175 * @v bofm BOFM device
176 * @v en EN parameter entry
177 * @ret rc Return status code
179 static int bofm_en ( struct bofm_device *bofm, struct bofm_en *en ) {
183 /* Retrieve current MAC address */
184 if ( ( rc = bofm->op->harvest ( bofm, en->mport, mac ) ) != 0 ) {
185 DBG ( "BOFM: " PCI_FMT " mport %d could not harvest: %s\n",
186 PCI_ARGS ( bofm->pci ), en->mport, strerror ( rc ) );
190 /* Harvest MAC address if necessary */
191 if ( en->options & BOFM_EN_RQ_HVST_MASK ) {
192 DBG ( "BOFM: " PCI_FMT " mport %d harvested MAC %s\n",
193 PCI_ARGS ( bofm->pci ), en->mport, eth_ntoa ( mac ) );
194 memcpy ( en->mac_a, mac, sizeof ( en->mac_a ) );
195 en->options |= ( BOFM_EN_EN_A | BOFM_EN_HVST );
198 /* Mark as changed if necessary */
199 if ( ( en->options & BOFM_EN_EN_A ) &&
200 ( memcmp ( en->mac_a, mac, sizeof ( en->mac_a ) ) != 0 ) ) {
201 DBG ( "BOFM: " PCI_FMT " mport %d MAC %s",
202 PCI_ARGS ( bofm->pci ), en->mport, eth_ntoa ( mac ) );
203 DBG ( " changed to %s\n", eth_ntoa ( en->mac_a ) );
204 en->options |= BOFM_EN_CHG_CHANGED;
207 /* Apply MAC address if necessary */
208 if ( ( en->options & BOFM_EN_EN_A ) &&
209 ( en->options & BOFM_EN_USAGE_ENTRY ) &&
210 ( ! ( en->options & BOFM_EN_USAGE_HARVEST ) ) ) {
211 DBG ( "BOFM: " PCI_FMT " mport %d applied MAC %s\n",
212 PCI_ARGS ( bofm->pci ), en->mport,
213 eth_ntoa ( en->mac_a ) );
214 memcpy ( mac, en->mac_a, sizeof ( mac ) );
217 /* Store MAC address */
218 if ( ( rc = bofm->op->update ( bofm, en->mport, mac ) ) != 0 ) {
219 DBG ( "BOFM: " PCI_FMT " mport %d could not update: %s\n",
220 PCI_ARGS ( bofm->pci ), en->mport, strerror ( rc ) );
230 * @v bofmtab BOFM table
232 * @ret bofmrc BOFM return status
234 int bofm ( userptr_t bofmtab, struct pci_device *pci ) {
235 struct bofm_global_header bofmhdr;
236 struct bofm_section_header bofmsec;
238 struct bofm_device *bofm;
239 size_t en_region_offset;
245 /* Read BOFM structure */
246 copy_from_user ( &bofmhdr, bofmtab, 0, sizeof ( bofmhdr ) );
247 if ( bofmhdr.magic != BOFM_IOAA_MAGIC ) {
248 DBG ( "BOFM: invalid table signature " BOFM_MAGIC_FMT "\n",
249 BOFM_MAGIC_ARGS ( bofmhdr.magic ) );
250 bofmrc = BOFM_ERR_INVALID_ACTION;
251 goto err_bad_signature;
253 DBG ( "BOFM: " BOFM_MAGIC_FMT " (profile \"%s\")\n",
254 BOFM_MAGIC_ARGS ( bofmhdr.action ), bofmhdr.profile );
256 /* Determine whether or not we should skip normal POST
259 switch ( bofmhdr.action ) {
260 case BOFM_ACTION_UPDT:
261 case BOFM_ACTION_DFLT:
262 case BOFM_ACTION_HVST:
263 skip = BOFM_SKIP_INIT;
265 case BOFM_ACTION_PARM:
266 case BOFM_ACTION_NONE:
270 DBG ( "BOFM: invalid action " BOFM_MAGIC_FMT "\n",
271 BOFM_MAGIC_ARGS ( bofmhdr.action ) );
272 bofmrc = BOFM_ERR_INVALID_ACTION;
276 /* Find BOFM driver */
277 if ( ( rc = bofm_find_driver ( pci ) ) != 0 ) {
278 DBG ( "BOFM: " PCI_FMT " has no driver\n", PCI_ARGS ( pci ) );
279 bofmrc = BOFM_ERR_DEVICE_ERROR;
280 goto err_find_driver;
283 /* Probe driver for PCI device */
284 if ( ( rc = bofm_probe ( pci ) ) != 0 ) {
285 bofmrc = BOFM_ERR_DEVICE_ERROR;
289 /* Locate EN section, if present */
290 en_region_offset = bofm_locate_section ( bofmtab, bofmhdr.length,
291 BOFM_EN_MAGIC, &bofmsec );
292 if ( ! en_region_offset ) {
293 DBG ( "BOFM: No EN section found\n" );
294 bofmrc = ( BOFM_SUCCESS | skip );
295 goto err_no_en_section;
298 /* Iterate through EN entries */
299 for ( en_offset = ( en_region_offset + sizeof ( bofmsec ) ) ;
300 en_offset < ( en_region_offset + sizeof ( bofmsec ) +
301 bofmsec.length ) ; en_offset += sizeof ( en ) ) {
302 copy_from_user ( &en, bofmtab, en_offset, sizeof ( en ) );
303 DBG2 ( "BOFM: EN entry found:\n" );
304 DBG2_HDA ( en_offset, &en, sizeof ( en ) );
305 if ( ( en.options & BOFM_EN_MAP_MASK ) != BOFM_EN_MAP_PFA ) {
306 DBG ( "BOFM: slot %d port %d has no PCI mapping\n",
307 en.slot, ( en.port + 1 ) );
310 DBG ( "BOFM: slot %d port %d%s is " PCI_FMT " mport %d\n",
311 en.slot, ( en.port + 1 ),
312 ( ( en.slot || en.port ) ? "" : "(?)" ),
313 PCI_BUS ( en.busdevfn ), PCI_SLOT ( en.busdevfn ),
314 PCI_FUNC ( en.busdevfn ), en.mport );
315 bofm = bofm_find_busdevfn ( en.busdevfn );
317 DBG ( "BOFM: " PCI_FMT " mport %d ignored\n",
318 PCI_BUS ( en.busdevfn ), PCI_SLOT ( en.busdevfn ),
319 PCI_FUNC ( en.busdevfn ), en.mport );
322 if ( ( rc = bofm_en ( bofm, &en ) ) == 0 ) {
323 en.options |= BOFM_EN_CSM_SUCCESS;
325 en.options |= BOFM_EN_CSM_FAILED;
327 DBG2 ( "BOFM: EN entry after processing:\n" );
328 DBG2_HDA ( en_offset, &en, sizeof ( en ) );
329 copy_to_user ( bofmtab, en_offset, &en, sizeof ( en ) );
332 bofmrc = ( BOFM_SUCCESS | skip );