iPXE
undi.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  */
00019 
00020 FILE_LICENCE ( GPL2_OR_LATER );
00021 
00022 #include <stdint.h>
00023 #include <stdlib.h>
00024 #include <stdio.h>
00025 #include <string.h>
00026 #include <ipxe/pci.h>
00027 #include <undi.h>
00028 #include <undirom.h>
00029 #include <undiload.h>
00030 #include <undinet.h>
00031 #include <undipreload.h>
00032 
00033 /** @file
00034  *
00035  * UNDI PCI driver
00036  *
00037  */
00038 
00039 /**
00040  * Find UNDI ROM for PCI device
00041  *
00042  * @v pci               PCI device
00043  * @ret undirom         UNDI ROM, or NULL
00044  *
00045  * Try to find a driver for this device.  Try an exact match on the
00046  * ROM address first, then fall back to a vendor/device ID match only
00047  */
00048 static struct undi_rom * undipci_find_rom ( struct pci_device *pci ) {
00049         struct undi_rom *undirom;
00050         unsigned long rombase;
00051         
00052         rombase = pci_bar_start ( pci, PCI_ROM_ADDRESS );
00053         undirom = undirom_find_pci ( pci->vendor, pci->device, rombase );
00054         if ( ! undirom )
00055                 undirom = undirom_find_pci ( pci->vendor, pci->device, 0 );
00056         return undirom;
00057 }
00058 
00059 /**
00060  * Probe PCI device
00061  *
00062  * @v pci               PCI device
00063  * @v id                PCI ID
00064  * @ret rc              Return status code
00065  */
00066 static int undipci_probe ( struct pci_device *pci ) {
00067         struct undi_device *undi;
00068         struct undi_rom *undirom;
00069         int rc;
00070 
00071         /* Allocate UNDI device structure */
00072         undi = zalloc ( sizeof ( *undi ) );
00073         if ( ! undi )
00074                 return -ENOMEM;
00075         pci_set_drvdata ( pci, undi );
00076 
00077         /* Find/create our pixie */
00078         if ( preloaded_undi.pci_busdevfn == pci->busdevfn ) {
00079                 /* Claim preloaded UNDI device */
00080                 DBGC ( undi, "UNDI %p using preloaded UNDI device\n", undi );
00081                 memcpy ( undi, &preloaded_undi, sizeof ( *undi ) );
00082                 memset ( &preloaded_undi, 0, sizeof ( preloaded_undi ) );
00083         } else {
00084                 /* Find UNDI ROM for PCI device */
00085                 if ( ! ( undirom = undipci_find_rom ( pci ) ) ) {
00086                         rc = -ENODEV;
00087                         goto err_find_rom;
00088                 }
00089 
00090                 /* Call UNDI ROM loader to create pixie */
00091                 if ( ( rc = undi_load_pci ( undi, undirom,
00092                                             pci->busdevfn ) ) != 0 ) {
00093                         goto err_load_pci;
00094                 }
00095         }
00096 
00097         /* Create network device */
00098         if ( ( rc = undinet_probe ( undi, &pci->dev ) ) != 0 )
00099                 goto err_undinet_probe;
00100         
00101         return 0;
00102 
00103  err_undinet_probe:
00104         undi_unload ( undi );
00105  err_find_rom:
00106  err_load_pci:
00107         free ( undi );
00108         pci_set_drvdata ( pci, NULL );
00109         return rc;
00110 }
00111 
00112 /**
00113  * Remove PCI device
00114  *
00115  * @v pci       PCI device
00116  */
00117 static void undipci_remove ( struct pci_device *pci ) {
00118         struct undi_device *undi = pci_get_drvdata ( pci );
00119 
00120         undinet_remove ( undi );
00121         undi_unload ( undi );
00122         free ( undi );
00123         pci_set_drvdata ( pci, NULL );
00124 }
00125 
00126 static struct pci_device_id undipci_nics[] = {
00127         PCI_ROM ( 0xffff, 0xffff, "undipci", "UNDI (PCI)", 0 ),
00128 };
00129 
00130 struct pci_driver undipci_driver __pci_driver_fallback = {
00131         .ids = undipci_nics,
00132         .id_count = ( sizeof ( undipci_nics ) / sizeof ( undipci_nics[0] ) ),
00133         .class = PCI_CLASS_ID ( PCI_CLASS_NETWORK, PCI_ANY_ID, PCI_ANY_ID ),
00134         .probe = undipci_probe,
00135         .remove = undipci_remove,
00136 };