Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / xen-pciback / controller.c
1 /*
2  * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
3  *      Alex Williamson <alex.williamson@hp.com>
4  *
5  * PCI "Controller" Backend - virtualize PCI bus topology based on PCI
6  * controllers.  Devices under the same PCI controller are exposed on the
7  * same virtual domain:bus.  Within a bus, device slots are virtualized
8  * to compact the bus.
9  *
10  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25  */
26
27 #include <linux/acpi.h>
28 #include "pciback.h"
29
30 #define PCI_MAX_BUSSES  255
31 #define PCI_MAX_SLOTS   32
32
33 struct controller_dev_entry {
34         struct list_head list;
35         struct pci_dev *dev;
36         unsigned int devfn;
37 };
38
39 struct controller_list_entry {
40         struct list_head list;
41         struct pci_controller *controller;
42         unsigned int domain;
43         unsigned int bus;
44         unsigned int next_devfn;
45         struct list_head dev_list;
46 };
47
48 struct controller_dev_data {
49         struct list_head list;
50         unsigned int next_domain;
51         unsigned int next_bus;
52         spinlock_t lock;
53 };
54
55 struct walk_info {
56         struct xen_pcibk_device *pdev;
57         int resource_count;
58         int root_num;
59 };
60
61 static struct pci_dev *_xen_pcibk_get_pci_dev(struct xen_pcibk_device *pdev,
62                                               unsigned int domain,
63                                               unsigned int bus,
64                                               unsigned int devfn)
65 {
66         struct controller_dev_data *dev_data = pdev->pci_dev_data;
67         struct controller_dev_entry *dev_entry;
68         struct controller_list_entry *cntrl_entry;
69         struct pci_dev *dev = NULL;
70
71         mutex_lock(&dev_data->lock);
72
73         list_for_each_entry(cntrl_entry, &dev_data->list, list) {
74                 if (cntrl_entry->domain != domain ||
75                     cntrl_entry->bus != bus)
76                         continue;
77
78                 list_for_each_entry(dev_entry, &cntrl_entry->dev_list, list) {
79                         if (devfn == dev_entry->devfn) {
80                                 dev = dev_entry->dev;
81                                 goto found;
82                         }
83                 }
84         }
85 found:
86         mutex_unlock(&dev_data->lock);
87
88         return dev;
89 }
90
91 static int _xen_pcibk_add_pci_dev(struct xen_pcibk_device *pdev,
92                                   struct pci_dev *dev, int devid,
93                                   publish_pci_dev_cb publish_cb)
94 {
95         struct controller_dev_data *dev_data = pdev->pci_dev_data;
96         struct controller_dev_entry *dev_entry;
97         struct controller_list_entry *cntrl_entry;
98         struct pci_controller *dev_controller = PCI_CONTROLLER(dev);
99         int ret = 0, found = 0;
100
101         mutex_lock(&dev_data->lock);
102
103         /* Look to see if we already have a domain:bus for this controller */
104         list_for_each_entry(cntrl_entry, &dev_data->list, list) {
105                 if (cntrl_entry->controller == dev_controller) {
106                         found = 1;
107                         break;
108                 }
109         }
110
111         if (!found) {
112                 cntrl_entry = kmalloc(sizeof(*cntrl_entry), GFP_ATOMIC);
113                 if (!cntrl_entry) {
114                         ret =  -ENOMEM;
115                         goto out;
116                 }
117
118                 cntrl_entry->controller = dev_controller;
119                 cntrl_entry->next_devfn = PCI_DEVFN(0, 0);
120
121                 cntrl_entry->domain = dev_data->next_domain;
122                 cntrl_entry->bus = dev_data->next_bus++;
123                 if (dev_data->next_bus > PCI_MAX_BUSSES) {
124                         dev_data->next_domain++;
125                         dev_data->next_bus = 0;
126                 }
127
128                 INIT_LIST_HEAD(&cntrl_entry->dev_list);
129
130                 list_add_tail(&cntrl_entry->list, &dev_data->list);
131         }
132
133         if (PCI_SLOT(cntrl_entry->next_devfn) > PCI_MAX_SLOTS) {
134                 /*
135                  * While it seems unlikely, this can actually happen if
136                  * a controller has P2P bridges under it.
137                  */
138                 xenbus_dev_fatal(pdev->xdev, -ENOSPC, "Virtual bus %04x:%02x "
139                                  "is full, no room to export %04x:%02x:%02x.%x",
140                                  cntrl_entry->domain, cntrl_entry->bus,
141                                  pci_domain_nr(dev->bus), dev->bus->number,
142                                  PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
143                 ret = -ENOSPC;
144                 goto out;
145         }
146
147         dev_entry = kmalloc(sizeof(*dev_entry), GFP_ATOMIC);
148         if (!dev_entry) {
149                 if (list_empty(&cntrl_entry->dev_list)) {
150                         list_del(&cntrl_entry->list);
151                         kfree(cntrl_entry);
152                 }
153                 ret = -ENOMEM;
154                 goto out;
155         }
156
157         dev_entry->dev = dev;
158         dev_entry->devfn = cntrl_entry->next_devfn;
159
160         list_add_tail(&dev_entry->list, &cntrl_entry->dev_list);
161
162         cntrl_entry->next_devfn += PCI_DEVFN(1, 0);
163
164 out:
165         mutex_unlock(&dev_data->lock);
166
167         /* TODO: Publish virtual domain:bus:slot.func here. */
168
169         return ret;
170 }
171
172 static void _xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
173                                        struct pci_dev *dev)
174 {
175         struct controller_dev_data *dev_data = pdev->pci_dev_data;
176         struct controller_list_entry *cntrl_entry;
177         struct controller_dev_entry *dev_entry = NULL;
178         struct pci_dev *found_dev = NULL;
179
180         mutex_lock(&dev_data->lock);
181
182         list_for_each_entry(cntrl_entry, &dev_data->list, list) {
183                 if (cntrl_entry->controller != PCI_CONTROLLER(dev))
184                         continue;
185
186                 list_for_each_entry(dev_entry, &cntrl_entry->dev_list, list) {
187                         if (dev_entry->dev == dev) {
188                                 found_dev = dev_entry->dev;
189                                 break;
190                         }
191                 }
192         }
193
194         if (!found_dev) {
195                 mutex_unlock(&dev_data->lock);
196                 return;
197         }
198
199         list_del(&dev_entry->list);
200         kfree(dev_entry);
201
202         if (list_empty(&cntrl_entry->dev_list)) {
203                 list_del(&cntrl_entry->list);
204                 kfree(cntrl_entry);
205         }
206
207         mutex_unlock(&dev_data->lock);
208         pcistub_put_pci_dev(found_dev);
209 }
210
211 static int _xen_pcibk_init_devices(struct xen_pcibk_device *pdev)
212 {
213         struct controller_dev_data *dev_data;
214
215         dev_data = kmalloc(sizeof(*dev_data), GFP_KERNEL);
216         if (!dev_data)
217                 return -ENOMEM;
218
219         mutex_init(&dev_data->lock);
220
221         INIT_LIST_HEAD(&dev_data->list);
222
223         /* Starting domain:bus numbers */
224         dev_data->next_domain = 0;
225         dev_data->next_bus = 0;
226
227         pdev->pci_dev_data = dev_data;
228
229         return 0;
230 }
231
232 static acpi_status write_xenbus_resource(struct acpi_resource *res, void *data)
233 {
234         struct walk_info *info = data;
235         struct acpi_resource_address64 addr;
236         acpi_status status;
237         int i, len, err;
238         char str[32], tmp[3];
239         unsigned char *ptr, *buf;
240
241         status = acpi_resource_to_address64(res, &addr);
242
243         /* Do we care about this range?  Let's check. */
244         if (!ACPI_SUCCESS(status) ||
245             !(addr.resource_type == ACPI_MEMORY_RANGE ||
246               addr.resource_type == ACPI_IO_RANGE) ||
247             !addr.address_length || addr.producer_consumer != ACPI_PRODUCER)
248                 return AE_OK;
249
250         /*
251          * Furthermore, we really only care to tell the guest about
252          * address ranges that require address translation of some sort.
253          */
254         if (!(addr.resource_type == ACPI_MEMORY_RANGE &&
255               addr.info.mem.translation) &&
256             !(addr.resource_type == ACPI_IO_RANGE &&
257               addr.info.io.translation))
258                 return AE_OK;
259            
260         /* Store the resource in xenbus for the guest */
261         len = snprintf(str, sizeof(str), "root-%d-resource-%d",
262                        info->root_num, info->resource_count);
263         if (unlikely(len >= (sizeof(str) - 1)))
264                 return AE_OK;
265
266         buf = kzalloc((sizeof(*res) * 2) + 1, GFP_KERNEL);
267         if (!buf)
268                 return AE_OK;
269
270         /* Clean out resource_source */
271         res->data.address64.resource_source.index = 0xFF;
272         res->data.address64.resource_source.string_length = 0;
273         res->data.address64.resource_source.string_ptr = NULL;
274
275         ptr = (unsigned char *)res;
276
277         /* Turn the acpi_resource into an ASCII byte stream */
278         for (i = 0; i < sizeof(*res); i++) {
279                 snprintf(tmp, sizeof(tmp), "%02x", ptr[i]);
280                 strncat(buf, tmp, 2);
281         }
282
283         err = xenbus_printf(XBT_NIL, info->pdev->xdev->nodename,
284                             str, "%s", buf);
285
286         if (!err)
287                 info->resource_count++;
288
289         kfree(buf);
290
291         return AE_OK;
292 }
293
294 static int _xen_pcibk_publish_pci_roots(struct xen_pcibk_device *pdev,
295                                         publish_pci_root_cb publish_root_cb)
296 {
297         struct controller_dev_data *dev_data = pdev->pci_dev_data;
298         struct controller_list_entry *cntrl_entry;
299         int i, root_num, len, err = 0;
300         unsigned int domain, bus;
301         char str[64];
302         struct walk_info info;
303
304         mutex_lock(&dev_data->lock);
305
306         list_for_each_entry(cntrl_entry, &dev_data->list, list) {
307                 /* First publish all the domain:bus info */
308                 err = publish_root_cb(pdev, cntrl_entry->domain,
309                                       cntrl_entry->bus);
310                 if (err)
311                         goto out;
312
313                 /*
314                  * Now figure out which root-%d this belongs to
315                  * so we can associate resources with it.
316                  */
317                 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
318                                    "root_num", "%d", &root_num);
319
320                 if (err != 1)
321                         goto out;
322
323                 for (i = 0; i < root_num; i++) {
324                         len = snprintf(str, sizeof(str), "root-%d", i);
325                         if (unlikely(len >= (sizeof(str) - 1))) {
326                                 err = -ENOMEM;
327                                 goto out;
328                         }
329
330                         err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
331                                            str, "%x:%x", &domain, &bus);
332                         if (err != 2)
333                                 goto out;
334
335                         /* Is this the one we just published? */
336                         if (domain == cntrl_entry->domain &&
337                             bus == cntrl_entry->bus)
338                                 break;
339                 }
340
341                 if (i == root_num)
342                         goto out;
343
344                 info.pdev = pdev;
345                 info.resource_count = 0;
346                 info.root_num = i;
347
348                 /* Let ACPI do the heavy lifting on decoding resources */
349                 acpi_walk_resources(cntrl_entry->controller->acpi_handle,
350                                     METHOD_NAME__CRS, write_xenbus_resource,
351                                     &info);
352
353                 /* No resouces.  OK.  On to the next one */
354                 if (!info.resource_count)
355                         continue;
356
357                 /* Store the number of resources we wrote for this root-%d */
358                 len = snprintf(str, sizeof(str), "root-%d-resources", i);
359                 if (unlikely(len >= (sizeof(str) - 1))) {
360                         err = -ENOMEM;
361                         goto out;
362                 }
363
364                 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
365                                     "%d", info.resource_count);
366                 if (err)
367                         goto out;
368         }
369
370         /* Finally, write some magic to synchronize with the guest. */
371         len = snprintf(str, sizeof(str), "root-resource-magic");
372         if (unlikely(len >= (sizeof(str) - 1))) {
373                 err = -ENOMEM;
374                 goto out;
375         }
376
377         err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
378                             "%lx", (sizeof(struct acpi_resource) * 2) + 1);
379
380 out:
381         mutex_unlock(&dev_data->lock);
382
383         return err;
384 }
385
386 static void _xen_pcibk_release_devices(struct xen_pcibk_device *pdev)
387 {
388         struct controller_dev_data *dev_data = pdev->pci_dev_data;
389         struct controller_list_entry *cntrl_entry, *c;
390         struct controller_dev_entry *dev_entry, *d;
391
392         list_for_each_entry_safe(cntrl_entry, c, &dev_data->list, list) {
393                 list_for_each_entry_safe(dev_entry, d,
394                                          &cntrl_entry->dev_list, list) {
395                         list_del(&dev_entry->list);
396                         pcistub_put_pci_dev(dev_entry->dev);
397                         kfree(dev_entry);
398                 }
399                 list_del(&cntrl_entry->list);
400                 kfree(cntrl_entry);
401         }
402
403         kfree(dev_data);
404         pdev->pci_dev_data = NULL;
405 }
406
407 static int _xen_pcibk_get_pcifront_dev(struct pci_dev *pcidev,
408                                        struct xen_pcibk_device *pdev,
409                                        unsigned int *domain,
410                                        unsigned int *bus, unsigned int *devfn)
411 {
412         struct controller_dev_data *dev_data = pdev->pci_dev_data;
413         struct controller_dev_entry *dev_entry;
414         struct controller_list_entry *cntrl_entry;
415         int found = 0;
416
417         mutex_lock(&dev_data->lock);
418         list_for_each_entry(cntrl_entry, &dev_data->list, list) {
419                 list_for_each_entry(dev_entry, &cntrl_entry->dev_list, list) {
420                         if ( (dev_entry->dev->bus->number == 
421                                         pcidev->bus->number) &&
422                                 (dev_entry->dev->devfn ==
423                                         pcidev->devfn) &&
424                                 (pci_domain_nr(dev_entry->dev->bus) ==
425                                         pci_domain_nr(pcidev->bus)))
426                         {
427                                 found = 1;
428                                 *domain = cntrl_entry->domain;
429                                 *bus = cntrl_entry->bus;
430                                 *devfn = dev_entry->devfn;
431                                 goto out;
432                         }
433                 }
434         }
435 out:
436         mutex_unlock(&dev_data->lock);
437         return found;
438
439 }
440
441 const struct xen_pcibk_backend xen_pcibk_controller_backend = {
442         .name           = "controller",
443         .init           = _xen_pcibk_init_devices,
444         .free           = _xen_pcibk_release_devices,
445         .find           = _xen_pcibk_get_pcifront_dev,
446         .publish        = _xen_pcibk_publish_pci_roots,
447         .release        = _xen_pcibk_release_pci_dev,
448         .add            = _xen_pcibk_add_pci_dev,
449         .get            = _xen_pcibk_get_pci_dev,
450 };