Merge branch 'for-linus' of git://oss.sgi.com/xfs/xfs
[linux-2.6] / drivers / pci / pcie / portdrv_core.c
1 /*
2  * File:        portdrv_core.c
3  * Purpose:     PCI Express Port Bus Driver's Core Functions
4  *
5  * Copyright (C) 2004 Intel
6  * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
7  */
8
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/pm.h>
14 #include <linux/string.h>
15 #include <linux/slab.h>
16 #include <linux/pcieport_if.h>
17
18 #include "../pci.h"
19 #include "portdrv.h"
20
21 /**
22  * release_pcie_device - free PCI Express port service device structure
23  * @dev: Port service device to release
24  *
25  * Invoked automatically when device is being removed in response to
26  * device_unregister(dev).  Release all resources being claimed.
27  */
28 static void release_pcie_device(struct device *dev)
29 {
30         kfree(to_pcie_device(dev));                     
31 }
32
33 /**
34  * pcie_port_msix_add_entry - add entry to given array of MSI-X entries
35  * @entries: Array of MSI-X entries
36  * @new_entry: Index of the entry to add to the array
37  * @nr_entries: Number of entries aleady in the array
38  *
39  * Return value: Position of the added entry in the array
40  */
41 static int pcie_port_msix_add_entry(
42         struct msix_entry *entries, int new_entry, int nr_entries)
43 {
44         int j;
45
46         for (j = 0; j < nr_entries; j++)
47                 if (entries[j].entry == new_entry)
48                         return j;
49
50         entries[j].entry = new_entry;
51         return j;
52 }
53
54 /**
55  * pcie_port_enable_msix - try to set up MSI-X as interrupt mode for given port
56  * @dev: PCI Express port to handle
57  * @vectors: Array of interrupt vectors to populate
58  * @mask: Bitmask of port capabilities returned by get_port_device_capability()
59  *
60  * Return value: 0 on success, error code on failure
61  */
62 static int pcie_port_enable_msix(struct pci_dev *dev, int *vectors, int mask)
63 {
64         struct msix_entry *msix_entries;
65         int idx[PCIE_PORT_DEVICE_MAXSERVICES];
66         int nr_entries, status, pos, i, nvec;
67         u16 reg16;
68         u32 reg32;
69
70         nr_entries = pci_msix_table_size(dev);
71         if (!nr_entries)
72                 return -EINVAL;
73         if (nr_entries > PCIE_PORT_MAX_MSIX_ENTRIES)
74                 nr_entries = PCIE_PORT_MAX_MSIX_ENTRIES;
75
76         msix_entries = kzalloc(sizeof(*msix_entries) * nr_entries, GFP_KERNEL);
77         if (!msix_entries)
78                 return -ENOMEM;
79
80         /*
81          * Allocate as many entries as the port wants, so that we can check
82          * which of them will be useful.  Moreover, if nr_entries is correctly
83          * equal to the number of entries this port actually uses, we'll happily
84          * go through without any tricks.
85          */
86         for (i = 0; i < nr_entries; i++)
87                 msix_entries[i].entry = i;
88
89         status = pci_enable_msix(dev, msix_entries, nr_entries);
90         if (status)
91                 goto Exit;
92
93         for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
94                 idx[i] = -1;
95         status = -EIO;
96         nvec = 0;
97
98         if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP)) {
99                 int entry;
100
101                 /*
102                  * The code below follows the PCI Express Base Specification 2.0
103                  * stating in Section 6.1.6 that "PME and Hot-Plug Event
104                  * interrupts (when both are implemented) always share the same
105                  * MSI or MSI-X vector, as indicated by the Interrupt Message
106                  * Number field in the PCI Express Capabilities register", where
107                  * according to Section 7.8.2 of the specification "For MSI-X,
108                  * the value in this field indicates which MSI-X Table entry is
109                  * used to generate the interrupt message."
110                  */
111                 pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
112                 pci_read_config_word(dev, pos + PCIE_CAPABILITIES_REG, &reg16);
113                 entry = (reg16 >> 9) & PCIE_PORT_MSI_VECTOR_MASK;
114                 if (entry >= nr_entries)
115                         goto Error;
116
117                 i = pcie_port_msix_add_entry(msix_entries, entry, nvec);
118                 if (i == nvec)
119                         nvec++;
120
121                 idx[PCIE_PORT_SERVICE_PME_SHIFT] = i;
122                 idx[PCIE_PORT_SERVICE_HP_SHIFT] = i;
123         }
124
125         if (mask & PCIE_PORT_SERVICE_AER) {
126                 int entry;
127
128                 /*
129                  * The code below follows Section 7.10.10 of the PCI Express
130                  * Base Specification 2.0 stating that bits 31-27 of the Root
131                  * Error Status Register contain a value indicating which of the
132                  * MSI/MSI-X vectors assigned to the port is going to be used
133                  * for AER, where "For MSI-X, the value in this register
134                  * indicates which MSI-X Table entry is used to generate the
135                  * interrupt message."
136                  */
137                 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
138                 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &reg32);
139                 entry = reg32 >> 27;
140                 if (entry >= nr_entries)
141                         goto Error;
142
143                 i = pcie_port_msix_add_entry(msix_entries, entry, nvec);
144                 if (i == nvec)
145                         nvec++;
146
147                 idx[PCIE_PORT_SERVICE_AER_SHIFT] = i;
148         }
149
150         /*
151          * If nvec is equal to the allocated number of entries, we can just use
152          * what we have.  Otherwise, the port has some extra entries not for the
153          * services we know and we need to work around that.
154          */
155         if (nvec == nr_entries) {
156                 status = 0;
157         } else {
158                 /* Drop the temporary MSI-X setup */
159                 pci_disable_msix(dev);
160
161                 /* Now allocate the MSI-X vectors for real */
162                 status = pci_enable_msix(dev, msix_entries, nvec);
163                 if (status)
164                         goto Exit;
165         }
166
167         for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
168                 vectors[i] = idx[i] >= 0 ? msix_entries[idx[i]].vector : -1;
169
170  Exit:
171         kfree(msix_entries);
172         return status;
173
174  Error:
175         pci_disable_msix(dev);
176         goto Exit;
177 }
178
179 /**
180  * assign_interrupt_mode - choose interrupt mode for PCI Express port services
181  *                         (INTx, MSI-X, MSI) and set up vectors
182  * @dev: PCI Express port to handle
183  * @vectors: Array of interrupt vectors to populate
184  * @mask: Bitmask of port capabilities returned by get_port_device_capability()
185  *
186  * Return value: Interrupt mode associated with the port
187  */
188 static int assign_interrupt_mode(struct pci_dev *dev, int *vectors, int mask)
189 {
190         struct pcie_port_data *port_data = pci_get_drvdata(dev);
191         int irq, interrupt_mode = PCIE_PORT_NO_IRQ;
192         int i;
193
194         /* Check MSI quirk */
195         if (port_data->port_type == PCIE_RC_PORT && pcie_mch_quirk)
196                 goto Fallback;
197
198         /* Try to use MSI-X if supported */
199         if (!pcie_port_enable_msix(dev, vectors, mask))
200                 return PCIE_PORT_MSIX_MODE;
201
202         /* We're not going to use MSI-X, so try MSI and fall back to INTx */
203         if (!pci_enable_msi(dev))
204                 interrupt_mode = PCIE_PORT_MSI_MODE;
205
206  Fallback:
207         if (interrupt_mode == PCIE_PORT_NO_IRQ && dev->pin)
208                 interrupt_mode = PCIE_PORT_INTx_MODE;
209
210         irq = interrupt_mode != PCIE_PORT_NO_IRQ ? dev->irq : -1;
211         for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
212                 vectors[i] = irq;
213
214         vectors[PCIE_PORT_SERVICE_VC_SHIFT] = -1;
215
216         return interrupt_mode;
217 }
218
219 /**
220  * get_port_device_capability - discover capabilities of a PCI Express port
221  * @dev: PCI Express port to examine
222  *
223  * The capabilities are read from the port's PCI Express configuration registers
224  * as described in PCI Express Base Specification 1.0a sections 7.8.2, 7.8.9 and
225  * 7.9 - 7.11.
226  *
227  * Return value: Bitmask of discovered port capabilities
228  */
229 static int get_port_device_capability(struct pci_dev *dev)
230 {
231         int services = 0, pos;
232         u16 reg16;
233         u32 reg32;
234
235         pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
236         pci_read_config_word(dev, pos + PCIE_CAPABILITIES_REG, &reg16);
237         /* Hot-Plug Capable */
238         if (reg16 & PORT_TO_SLOT_MASK) {
239                 pci_read_config_dword(dev, 
240                         pos + PCIE_SLOT_CAPABILITIES_REG, &reg32);
241                 if (reg32 & SLOT_HP_CAPABLE_MASK)
242                         services |= PCIE_PORT_SERVICE_HP;
243         }
244         /* AER capable */
245         if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR))
246                 services |= PCIE_PORT_SERVICE_AER;
247         /* VC support */
248         if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_VC))
249                 services |= PCIE_PORT_SERVICE_VC;
250
251         return services;
252 }
253
254 /**
255  * pcie_device_init - initialize PCI Express port service device
256  * @dev: Port service device to initialize
257  * @parent: PCI Express port to associate the service device with
258  * @port_type: Type of the port
259  * @service_type: Type of service to associate with the service device
260  * @irq: Interrupt vector to associate with the service device
261  */
262 static void pcie_device_init(struct pci_dev *parent, struct pcie_device *dev, 
263         int service_type, int irq)
264 {
265         struct pcie_port_data *port_data = pci_get_drvdata(parent);
266         struct device *device;
267         int port_type = port_data->port_type;
268
269         dev->port = parent;
270         dev->irq = irq;
271         dev->service = service_type;
272
273         /* Initialize generic device interface */
274         device = &dev->device;
275         memset(device, 0, sizeof(struct device));
276         device->bus = &pcie_port_bus_type;
277         device->driver = NULL;
278         device->driver_data = NULL;
279         device->release = release_pcie_device;  /* callback to free pcie dev */
280         dev_set_name(device, "%s:pcie%02x",
281                  pci_name(parent), get_descriptor_id(port_type, service_type));
282         device->parent = &parent->dev;
283 }
284
285 /**
286  * alloc_pcie_device - allocate PCI Express port service device structure
287  * @parent: PCI Express port to associate the service device with
288  * @port_type: Type of the port
289  * @service_type: Type of service to associate with the service device
290  * @irq: Interrupt vector to associate with the service device
291  */
292 static struct pcie_device* alloc_pcie_device(struct pci_dev *parent,
293         int service_type, int irq)
294 {
295         struct pcie_device *device;
296
297         device = kzalloc(sizeof(struct pcie_device), GFP_KERNEL);
298         if (!device)
299                 return NULL;
300
301         pcie_device_init(parent, device, service_type, irq);
302         return device;
303 }
304
305 /**
306  * pcie_port_device_probe - check if device is a PCI Express port
307  * @dev: Device to check
308  */
309 int pcie_port_device_probe(struct pci_dev *dev)
310 {
311         int pos, type;
312         u16 reg;
313
314         if (!(pos = pci_find_capability(dev, PCI_CAP_ID_EXP)))
315                 return -ENODEV;
316
317         pci_read_config_word(dev, pos + PCIE_CAPABILITIES_REG, &reg);
318         type = (reg >> 4) & PORT_TYPE_MASK;
319         if (    type == PCIE_RC_PORT || type == PCIE_SW_UPSTREAM_PORT ||
320                 type == PCIE_SW_DOWNSTREAM_PORT )
321                 return 0;
322
323         return -ENODEV;
324 }
325
326 /**
327  * pcie_port_device_register - register PCI Express port
328  * @dev: PCI Express port to register
329  *
330  * Allocate the port extension structure and register services associated with
331  * the port.
332  */
333 int pcie_port_device_register(struct pci_dev *dev)
334 {
335         struct pcie_port_data *port_data;
336         int status, capabilities, irq_mode, i, nr_serv;
337         int vectors[PCIE_PORT_DEVICE_MAXSERVICES];
338         u16 reg16;
339
340         port_data = kzalloc(sizeof(*port_data), GFP_KERNEL);
341         if (!port_data)
342                 return -ENOMEM;
343         pci_set_drvdata(dev, port_data);
344
345         /* Get port type */
346         pci_read_config_word(dev,
347                 pci_find_capability(dev, PCI_CAP_ID_EXP) +
348                 PCIE_CAPABILITIES_REG, &reg16);
349         port_data->port_type = (reg16 >> 4) & PORT_TYPE_MASK;
350
351         capabilities = get_port_device_capability(dev);
352         /* Root ports are capable of generating PME too */
353         if (port_data->port_type == PCIE_RC_PORT)
354                 capabilities |= PCIE_PORT_SERVICE_PME;
355
356         irq_mode = assign_interrupt_mode(dev, vectors, capabilities);
357         if (irq_mode == PCIE_PORT_NO_IRQ) {
358                 /*
359                  * Don't use service devices that require interrupts if there is
360                  * no way to generate them.
361                  */
362                 if (!(capabilities & PCIE_PORT_SERVICE_VC)) {
363                         status = -ENODEV;
364                         goto Error;
365                 }
366                 capabilities = PCIE_PORT_SERVICE_VC;
367         }
368         port_data->port_irq_mode = irq_mode;
369
370         status = pci_enable_device(dev);
371         if (status)
372                 goto Error;
373         pci_set_master(dev);
374
375         /* Allocate child services if any */
376         for (i = 0, nr_serv = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) {
377                 struct pcie_device *child;
378                 int service = 1 << i;
379
380                 if (!(capabilities & service))
381                         continue;
382
383                 child = alloc_pcie_device(dev, service, vectors[i]);
384                 if (!child)
385                         continue;
386
387                 status = device_register(&child->device);
388                 if (status) {
389                         kfree(child);
390                         continue;
391                 }
392
393                 get_device(&child->device);
394                 nr_serv++;
395         }
396         if (!nr_serv) {
397                 pci_disable_device(dev);
398                 status = -ENODEV;
399                 goto Error;
400         }
401
402         return 0;
403
404  Error:
405         kfree(port_data);
406         return status;
407 }
408
409 #ifdef CONFIG_PM
410 static int suspend_iter(struct device *dev, void *data)
411 {
412         struct pcie_port_service_driver *service_driver;
413
414         if ((dev->bus == &pcie_port_bus_type) &&
415             (dev->driver)) {
416                 service_driver = to_service_driver(dev->driver);
417                 if (service_driver->suspend)
418                         service_driver->suspend(to_pcie_device(dev));
419         }
420         return 0;
421 }
422
423 /**
424  * pcie_port_device_suspend - suspend port services associated with a PCIe port
425  * @dev: PCI Express port to handle
426  */
427 int pcie_port_device_suspend(struct device *dev)
428 {
429         return device_for_each_child(dev, NULL, suspend_iter);
430 }
431
432 static int resume_iter(struct device *dev, void *data)
433 {
434         struct pcie_port_service_driver *service_driver;
435
436         if ((dev->bus == &pcie_port_bus_type) &&
437             (dev->driver)) {
438                 service_driver = to_service_driver(dev->driver);
439                 if (service_driver->resume)
440                         service_driver->resume(to_pcie_device(dev));
441         }
442         return 0;
443 }
444
445 /**
446  * pcie_port_device_suspend - resume port services associated with a PCIe port
447  * @dev: PCI Express port to handle
448  */
449 int pcie_port_device_resume(struct device *dev)
450 {
451         return device_for_each_child(dev, NULL, resume_iter);
452 }
453 #endif /* PM */
454
455 static int remove_iter(struct device *dev, void *data)
456 {
457         if (dev->bus == &pcie_port_bus_type) {
458                 put_device(dev);
459                 device_unregister(dev);
460         }
461         return 0;
462 }
463
464 /**
465  * pcie_port_device_remove - unregister PCI Express port service devices
466  * @dev: PCI Express port the service devices to unregister are associated with
467  *
468  * Remove PCI Express port service devices associated with given port and
469  * disable MSI-X or MSI for the port.
470  */
471 void pcie_port_device_remove(struct pci_dev *dev)
472 {
473         struct pcie_port_data *port_data = pci_get_drvdata(dev);
474
475         device_for_each_child(&dev->dev, NULL, remove_iter);
476         pci_disable_device(dev);
477
478         switch (port_data->port_irq_mode) {
479         case PCIE_PORT_MSIX_MODE:
480                 pci_disable_msix(dev);
481                 break;
482         case PCIE_PORT_MSI_MODE:
483                 pci_disable_msi(dev);
484                 break;
485         }
486
487         kfree(port_data);
488 }
489
490 /**
491  * pcie_port_probe_service - probe driver for given PCI Express port service
492  * @dev: PCI Express port service device to probe against
493  *
494  * If PCI Express port service driver is registered with
495  * pcie_port_service_register(), this function will be called by the driver core
496  * whenever match is found between the driver and a port service device.
497  */
498 static int pcie_port_probe_service(struct device *dev)
499 {
500         struct pcie_device *pciedev;
501         struct pcie_port_service_driver *driver;
502         int status;
503
504         if (!dev || !dev->driver)
505                 return -ENODEV;
506
507         driver = to_service_driver(dev->driver);
508         if (!driver || !driver->probe)
509                 return -ENODEV;
510
511         pciedev = to_pcie_device(dev);
512         status = driver->probe(pciedev);
513         if (!status) {
514                 dev_printk(KERN_DEBUG, dev, "service driver %s loaded\n",
515                         driver->name);
516                 get_device(dev);
517         }
518         return status;
519 }
520
521 /**
522  * pcie_port_remove_service - detach driver from given PCI Express port service
523  * @dev: PCI Express port service device to handle
524  *
525  * If PCI Express port service driver is registered with
526  * pcie_port_service_register(), this function will be called by the driver core
527  * when device_unregister() is called for the port service device associated
528  * with the driver.
529  */
530 static int pcie_port_remove_service(struct device *dev)
531 {
532         struct pcie_device *pciedev;
533         struct pcie_port_service_driver *driver;
534
535         if (!dev || !dev->driver)
536                 return 0;
537
538         pciedev = to_pcie_device(dev);
539         driver = to_service_driver(dev->driver);
540         if (driver && driver->remove) {
541                 dev_printk(KERN_DEBUG, dev, "unloading service driver %s\n",
542                         driver->name);
543                 driver->remove(pciedev);
544                 put_device(dev);
545         }
546         return 0;
547 }
548
549 /**
550  * pcie_port_shutdown_service - shut down given PCI Express port service
551  * @dev: PCI Express port service device to handle
552  *
553  * If PCI Express port service driver is registered with
554  * pcie_port_service_register(), this function will be called by the driver core
555  * when device_shutdown() is called for the port service device associated
556  * with the driver.
557  */
558 static void pcie_port_shutdown_service(struct device *dev) {}
559
560 /**
561  * pcie_port_service_register - register PCI Express port service driver
562  * @new: PCI Express port service driver to register
563  */
564 int pcie_port_service_register(struct pcie_port_service_driver *new)
565 {
566         new->driver.name = (char *)new->name;
567         new->driver.bus = &pcie_port_bus_type;
568         new->driver.probe = pcie_port_probe_service;
569         new->driver.remove = pcie_port_remove_service;
570         new->driver.shutdown = pcie_port_shutdown_service;
571
572         return driver_register(&new->driver);
573 }
574
575 /**
576  * pcie_port_service_unregister - unregister PCI Express port service driver
577  * @drv: PCI Express port service driver to unregister
578  */
579 void pcie_port_service_unregister(struct pcie_port_service_driver *drv)
580 {
581         driver_unregister(&drv->driver);
582 }
583
584 EXPORT_SYMBOL(pcie_port_service_register);
585 EXPORT_SYMBOL(pcie_port_service_unregister);