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., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 #include <stdint.h>
00022 #include <stdlib.h>
00023 #include <stdio.h>
00024 #include <string.h>
00025 #include <gpxe/pci.h>
00026 #include <undi.h>
00027 #include <undirom.h>
00028 #include <undiload.h>
00029 #include <undinet.h>
00030 #include <undipreload.h>
00031 
00032 /** @file
00033  *
00034  * UNDI PCI driver
00035  *
00036  */
00037 
00038 /**
00039  * Find UNDI ROM for PCI device
00040  *
00041  * @v pci               PCI device
00042  * @ret undirom         UNDI ROM, or NULL
00043  *
00044  * Try to find a driver for this device.  Try an exact match on the
00045  * ROM address first, then fall back to a vendor/device ID match only
00046  */
00047 static struct undi_rom * undipci_find_rom ( struct pci_device *pci ) {
00048         struct undi_rom *undirom;
00049         unsigned long rombase;
00050         
00051         rombase = pci_bar_start ( pci, PCI_ROM_ADDRESS );
00052         undirom = undirom_find_pci ( pci->vendor, pci->device, rombase );
00053         if ( ! undirom )
00054                 undirom = undirom_find_pci ( pci->vendor, pci->device, 0 );
00055         return undirom;
00056 }
00057 
00058 /**
00059  * Probe PCI device
00060  *
00061  * @v pci               PCI device
00062  * @v id                PCI ID
00063  * @ret rc              Return status code
00064  */
00065 static int undipci_probe ( struct pci_device *pci,
00066                            const struct pci_device_id *id __unused ) {
00067         struct undi_device *undi;
00068         struct undi_rom *undirom;
00069         unsigned int busdevfn = PCI_BUSDEVFN ( pci->bus, pci->devfn );
00070         int rc;
00071 
00072         /* Ignore non-network devices */
00073         if ( PCI_BASE_CLASS ( pci->class ) != PCI_BASE_CLASS_NETWORK )
00074                 return -ENOTTY;
00075 
00076         /* Allocate UNDI device structure */
00077         undi = zalloc ( sizeof ( *undi ) );
00078         if ( ! undi )
00079                 return -ENOMEM;
00080         pci_set_drvdata ( pci, undi );
00081 
00082         /* Find/create our pixie */
00083         if ( preloaded_undi.pci_busdevfn == busdevfn ) {
00084                 /* Claim preloaded UNDI device */
00085                 DBGC ( undi, "UNDI %p using preloaded UNDI device\n", undi );
00086                 memcpy ( undi, &preloaded_undi, sizeof ( *undi ) );
00087                 memset ( &preloaded_undi, 0, sizeof ( preloaded_undi ) );
00088         } else {
00089                 /* Find UNDI ROM for PCI device */
00090                 if ( ! ( undirom = undipci_find_rom ( pci ) ) ) {
00091                         rc = -ENODEV;
00092                         goto err_find_rom;
00093                 }
00094 
00095                 /* Call UNDI ROM loader to create pixie */
00096                 if ( ( rc = undi_load_pci ( undi, undirom, busdevfn ) ) != 0 )
00097                         goto err_load_pci;
00098         }
00099 
00100         /* Add to device hierarchy */
00101         snprintf ( undi->dev.name, sizeof ( undi->dev.name ),
00102                    "UNDI-%s", pci->dev.name );
00103         memcpy ( &undi->dev.desc, &pci->dev.desc, sizeof ( undi->dev.desc ) );
00104         undi->dev.parent = &pci->dev;
00105         INIT_LIST_HEAD ( &undi->dev.children );
00106         list_add ( &undi->dev.siblings, &pci->dev.children );
00107 
00108         /* Create network device */
00109         if ( ( rc = undinet_probe ( undi ) ) != 0 )
00110                 goto err_undinet_probe;
00111         
00112         return 0;
00113 
00114  err_undinet_probe:
00115         undi_unload ( undi );
00116         list_del ( &undi->dev.siblings );
00117  err_find_rom:
00118  err_load_pci:
00119         free ( undi );
00120         pci_set_drvdata ( pci, NULL );
00121         return rc;
00122 }
00123 
00124 /**
00125  * Remove PCI device
00126  *
00127  * @v pci       PCI device
00128  */
00129 static void undipci_remove ( struct pci_device *pci ) {
00130         struct undi_device *undi = pci_get_drvdata ( pci );
00131 
00132         undinet_remove ( undi );
00133         undi_unload ( undi );
00134         list_del ( &undi->dev.siblings );
00135         free ( undi );
00136         pci_set_drvdata ( pci, NULL );
00137 }
00138 
00139 static struct pci_device_id undipci_nics[] = {
00140 PCI_ROM ( 0xffff, 0xffff, "undipci", "UNDI (PCI)", 0 ),
00141 };
00142 
00143 struct pci_driver undipci_driver __pci_driver = {
00144         .ids = undipci_nics,
00145         .id_count = ( sizeof ( undipci_nics ) / sizeof ( undipci_nics[0] ) ),
00146         .probe = undipci_probe,
00147         .remove = undipci_remove,
00148 };

Generated on Tue Apr 6 20:00:49 2010 for gPXE by  doxygen 1.5.7.1