2 * Copyright © 2009 Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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.
31 FILE_LICENCE ( BSD2
);
34 #include <grub/misc.h>
36 /* Helper for grub_gpxe_register_pci_nic. */
38 grub_gpxe_pci_nic_init (grub_pci_device_t dev
, grub_pci_id_t pciid
, void *data
)
40 struct pci_driver
*nic
= data
;
43 for (i
= 0; i
< nic
->id_count
; i
++)
46 if (nic
->ids
[i
].devid
== pciid
)
48 struct pci_device
*pci
;
49 struct pci_device_id
*id
;
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
));
60 grub_errno
= GRUB_ERR_NONE
;
63 id
= grub_malloc (sizeof (*id
));
68 grub_errno
= GRUB_ERR_NONE
;
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
;
88 reg
= GRUB_PCI_REG_ADDRESSES
;
89 while (reg
< GRUB_PCI_REG_CIS_POINTER
)
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
);
102 if ((space
& GRUB_PCI_ADDR_SPACE_MASK
)
103 == GRUB_PCI_ADDR_SPACE_IO
)
105 pci
->ioaddr
= space
& GRUB_PCI_ADDR_IO_MASK
;
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. */
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
);
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. */
136 grub_gpxe_unregister_pci_nic (struct pci_driver
*nic
__attribute__ ((unused
)))