2 * PCI Frontend Operations - Communicates with frontend
4 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
6 #include <linux/module.h>
7 #include <linux/version.h>
8 #include <linux/init.h>
10 #include <linux/spinlock.h>
11 #include <linux/time.h>
12 #include <xen/evtchn.h>
15 static int verbose_request = 0;
16 module_param(verbose_request, int, 0644);
19 static void pcifront_init_sd(struct pcifront_sd *sd,
20 unsigned int domain, unsigned int bus,
21 struct pcifront_device *pdev)
23 int err, i, j, k, len, root_num, res_count;
24 struct acpi_resource res;
25 unsigned int d, b, byte;
28 unsigned char *buf, *bufp;
31 memset(sd, 0, sizeof(*sd));
34 sd->node = -1; /* Revisit for NUMA */
35 sd->platform_data = pdev;
37 /* Look for resources for this controller in xenbus. */
38 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, "root_num",
43 for (i = 0; i < root_num; i++) {
44 len = snprintf(str, sizeof(str), "root-%d", i);
45 if (unlikely(len >= (sizeof(str) - 1)))
48 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
49 str, "%x:%x", &d, &b);
53 if (d == domain && b == bus)
60 len = snprintf(str, sizeof(str), "root-resource-magic");
62 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
66 return; /* No resources, nothing to do */
68 if (magic != (sizeof(res) * 2) + 1) {
69 printk(KERN_WARNING "pcifront: resource magic mismatch\n");
73 len = snprintf(str, sizeof(str), "root-%d-resources", i);
74 if (unlikely(len >= (sizeof(str) - 1)))
77 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
78 str, "%d", &res_count);
81 return; /* No resources, nothing to do */
83 sd->window = kzalloc(sizeof(*sd->window) * res_count, GFP_KERNEL);
87 /* magic is also the size of the byte stream in xenbus */
88 buf = kmalloc(magic, GFP_KERNEL);
95 /* Read the resources out of xenbus */
96 for (j = 0; j < res_count; j++) {
97 memset(&res, 0, sizeof(res));
98 memset(buf, 0, magic);
100 len = snprintf(str, sizeof(str), "root-%d-resource-%d", i, j);
101 if (unlikely(len >= (sizeof(str) - 1)))
104 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
107 printk(KERN_WARNING "pcifront: error reading "
108 "resource %d on bus %04x:%02x\n",
115 memset(tmp, 0, sizeof(tmp));
117 /* Copy ASCII byte stream into structure */
118 for (k = 0; k < magic - 1; k += 2) {
119 memcpy(tmp, bufp, 2);
122 sscanf(tmp, "%02x", &byte);
127 xen_add_resource(sd, domain, bus, &res);
134 static int errno_to_pcibios_err(int errno)
137 case XEN_PCI_ERR_success:
138 return PCIBIOS_SUCCESSFUL;
140 case XEN_PCI_ERR_dev_not_found:
141 return PCIBIOS_DEVICE_NOT_FOUND;
143 case XEN_PCI_ERR_invalid_offset:
144 case XEN_PCI_ERR_op_failed:
145 return PCIBIOS_BAD_REGISTER_NUMBER;
147 case XEN_PCI_ERR_not_implemented:
148 return PCIBIOS_FUNC_NOT_SUPPORTED;
150 case XEN_PCI_ERR_access_denied:
151 return PCIBIOS_SET_FAILED;
156 static int do_pci_op(struct pcifront_device *pdev, struct xen_pci_op *op)
159 struct xen_pci_op *active_op = &pdev->sh_info->op;
160 unsigned long irq_flags;
161 evtchn_port_t port = pdev->evtchn;
165 spin_lock_irqsave(&pdev->sh_info_lock, irq_flags);
167 memcpy(active_op, op, sizeof(struct xen_pci_op));
171 set_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags);
172 notify_remote_via_evtchn(port);
175 * We set a poll timeout of 3 seconds but give up on return after
176 * 2 seconds. It is better to time out too late rather than too early
177 * (in the latter case we end up continually re-executing poll() with a
178 * timeout in the past). 1s difference gives plenty of slack for error.
180 do_gettimeofday(&tv);
181 ns_timeout = timeval_to_ns(&tv) + 2 * (s64)NSEC_PER_SEC;
185 while (test_bit(_XEN_PCIF_active,
186 (unsigned long *)&pdev->sh_info->flags)) {
187 if (HYPERVISOR_poll(&port, 1, jiffies + 3*HZ))
190 do_gettimeofday(&tv);
191 ns = timeval_to_ns(&tv);
192 if (ns > ns_timeout) {
193 dev_err(&pdev->xdev->dev,
194 "pciback not responding!!!\n");
195 clear_bit(_XEN_PCIF_active,
196 (unsigned long *)&pdev->sh_info->flags);
197 err = XEN_PCI_ERR_dev_not_found;
202 memcpy(op, active_op, sizeof(struct xen_pci_op));
206 spin_unlock_irqrestore(&pdev->sh_info_lock, irq_flags);
210 /* Access to this function is spinlocked in drivers/pci/access.c */
211 static int pcifront_bus_read(struct pci_bus *bus, unsigned int devfn,
212 int where, int size, u32 * val)
215 struct xen_pci_op op = {
216 .cmd = XEN_PCI_OP_conf_read,
217 .domain = pci_domain_nr(bus),
223 struct pcifront_sd *sd = bus->sysdata;
224 struct pcifront_device *pdev = pcifront_get_pdev(sd);
227 dev_info(&pdev->xdev->dev,
228 "read dev=%04x:%02x:%02x.%01x - offset %x size %d\n",
229 pci_domain_nr(bus), bus->number, PCI_SLOT(devfn),
230 PCI_FUNC(devfn), where, size);
232 err = do_pci_op(pdev, &op);
236 dev_info(&pdev->xdev->dev, "read got back value %x\n",
240 } else if (err == -ENODEV) {
241 /* No device here, pretend that it just returned 0 */
246 return errno_to_pcibios_err(err);
249 /* Access to this function is spinlocked in drivers/pci/access.c */
250 static int pcifront_bus_write(struct pci_bus *bus, unsigned int devfn,
251 int where, int size, u32 val)
253 struct xen_pci_op op = {
254 .cmd = XEN_PCI_OP_conf_write,
255 .domain = pci_domain_nr(bus),
262 struct pcifront_sd *sd = bus->sysdata;
263 struct pcifront_device *pdev = pcifront_get_pdev(sd);
266 dev_info(&pdev->xdev->dev,
267 "write dev=%04x:%02x:%02x.%01x - "
268 "offset %x size %d val %x\n",
269 pci_domain_nr(bus), bus->number,
270 PCI_SLOT(devfn), PCI_FUNC(devfn), where, size, val);
272 return errno_to_pcibios_err(do_pci_op(pdev, &op));
275 struct pci_ops pcifront_bus_ops = {
276 .read = pcifront_bus_read,
277 .write = pcifront_bus_write,
280 /* Claim resources for the PCI frontend as-is, backend won't allow changes */
281 static void pcifront_claim_resource(struct pci_dev *dev, void *data)
283 struct pcifront_device *pdev = data;
287 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
288 r = &dev->resource[i];
290 if (!r->parent && r->start && r->flags) {
291 dev_dbg(&pdev->xdev->dev, "claiming resource %s/%d\n",
293 pci_claim_resource(dev, i);
298 int pcifront_scan_root(struct pcifront_device *pdev,
299 unsigned int domain, unsigned int bus)
302 struct pcifront_sd *sd = NULL;
303 struct pci_bus_entry *bus_entry = NULL;
306 #ifndef CONFIG_PCI_DOMAINS
308 dev_err(&pdev->xdev->dev,
309 "PCI Root in non-zero PCI Domain! domain=%d\n", domain);
310 dev_err(&pdev->xdev->dev,
311 "Please compile with CONFIG_PCI_DOMAINS\n");
317 dev_info(&pdev->xdev->dev, "Creating PCI Frontend Bus %04x:%02x\n",
320 bus_entry = kmalloc(sizeof(*bus_entry), GFP_KERNEL);
321 sd = kmalloc(sizeof(*sd), GFP_KERNEL);
322 if (!bus_entry || !sd) {
326 pcifront_init_sd(sd, domain, bus, pdev);
328 b = pci_scan_bus_parented(&pdev->xdev->dev, bus,
329 &pcifront_bus_ops, sd);
331 dev_err(&pdev->xdev->dev,
332 "Error creating PCI Frontend Bus!\n");
337 pcifront_setup_root_resources(b, sd);
340 list_add(&bus_entry->list, &pdev->root_buses);
342 /* Claim resources before going "live" with our devices */
343 pci_walk_bus(b, pcifront_claim_resource, pdev);
345 pci_bus_add_devices(b);
356 static void free_root_bus_devs(struct pci_bus *bus)
360 while (!list_empty(&bus->devices)) {
361 dev = container_of(bus->devices.next, struct pci_dev,
363 dev_dbg(&dev->dev, "removing device\n");
364 pci_remove_bus_device(dev);
368 void pcifront_free_roots(struct pcifront_device *pdev)
370 struct pci_bus_entry *bus_entry, *t;
372 dev_dbg(&pdev->xdev->dev, "cleaning up root buses\n");
374 list_for_each_entry_safe(bus_entry, t, &pdev->root_buses, list) {
375 list_del(&bus_entry->list);
377 free_root_bus_devs(bus_entry->bus);
379 kfree(bus_entry->bus->sysdata);
381 device_unregister(bus_entry->bus->bridge);
382 pci_remove_bus(bus_entry->bus);