grub-extras/lua: add fs label to grub.enum_device
[grub-extras.git] / disabled / gpxe / wrap / nic.c
blob05a3b8905c333fd7da691eebf988048f8a7460a8
1 /*
2 * Copyright © 2009 Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
31 FILE_LICENCE ( BSD2 );
33 #include <gpxe/pci.h>
34 #include <grub/misc.h>
36 /* Helper for grub_gpxe_register_pci_nic. */
37 static int
38 grub_gpxe_pci_nic_init (grub_pci_device_t dev, grub_pci_id_t pciid, void *data)
40 struct pci_driver *nic = data;
41 unsigned i;
43 for (i = 0; i < nic->id_count; i++)
45 int err;
46 if (nic->ids[i].devid == pciid)
48 struct pci_device *pci;
49 struct pci_device_id *id;
50 int reg;
52 grub_dprintf ("gpxe", "Attaching NIC %d:%d.%d\n",
53 grub_pci_get_bus (dev),
54 grub_pci_get_device (dev),
55 grub_pci_get_function (dev));
56 pci = grub_malloc (sizeof (*pci));
57 if (!pci)
59 grub_print_error ();
60 grub_errno = GRUB_ERR_NONE;
61 return 0;
63 id = grub_malloc (sizeof (*id));
64 if (!id)
66 grub_free (pci);
67 grub_print_error ();
68 grub_errno = GRUB_ERR_NONE;
69 return 0;
71 id->devid = pciid;
72 pci->dev.desc.bus_type = BUS_TYPE_PCI;
73 pci->dev.desc.bus = grub_pci_get_bus (dev);
74 pci->dev.desc.location = (grub_pci_get_device (dev) << 3)
75 | grub_pci_get_function (dev);
76 pci->dev.desc.vendor = pciid & 0xffff;
77 pci->dev.desc.device = pciid >> 16;
78 pci->vendor = pciid & 0xffff;
79 pci->device = pciid >> 16;
80 pci->dev.name = grub_xasprintf ("PCI:%02x:%02x.%x",
81 grub_pci_get_bus (dev),
82 grub_pci_get_device (dev),
83 grub_pci_get_function (dev));
84 pci->dev.pci_dev = dev;
85 pci->priv = 0;
86 pci->drvdata = 0;
88 reg = GRUB_PCI_REG_ADDRESSES;
89 while (reg < GRUB_PCI_REG_CIS_POINTER)
91 grub_uint64_t space;
92 grub_pci_address_t addr;
94 addr = grub_pci_make_address (dev, reg);
95 space = grub_pci_read (addr);
97 reg += sizeof (grub_uint32_t);
99 if (space == 0)
100 continue;
102 if ((space & GRUB_PCI_ADDR_SPACE_MASK)
103 == GRUB_PCI_ADDR_SPACE_IO)
105 pci->ioaddr = space & GRUB_PCI_ADDR_IO_MASK;
106 break;
109 if ((space & GRUB_PCI_ADDR_MEM_TYPE_MASK)
110 == GRUB_PCI_ADDR_MEM_TYPE_64)
111 reg += sizeof (grub_uint32_t);
114 /* No IRQ support yet. */
115 pci->irq = 0;
116 grub_dprintf ("gpxe", "Probing NIC %d:%d.%d\n",
117 grub_pci_get_bus (dev),
118 grub_pci_get_device (dev),
119 grub_pci_get_function (dev));
120 err = nic->probe (pci, id);
121 grub_dprintf ("gpxe", "Nic probe finished with status %d\n", err);
124 return 0;
127 void
128 grub_gpxe_register_pci_nic (struct pci_driver *nic)
130 grub_dprintf ("gpxe", "Registering nic\n");
131 grub_pci_iterate (grub_gpxe_pci_nic_init, nic);
134 /* FIXME: free all resources associated with driver and detach devices. */
135 void
136 grub_gpxe_unregister_pci_nic (struct pci_driver *nic __attribute__ ((unused)))