Skip to content

Commit a06876a

Browse files
bzolnierbjorn-helgaas
authored andcommitted
ide: pci: free PCI BARs on initialization failure
Recent pci_assign_irq() changes uncovered a problem with missing freeing of PCI BARs on PCI IDE host initialization failure: ide0: disabled, no IRQ ide0: failed to initialize IDE interface ide0: disabling port cmd64x 0000:00:02.0: IDE controller (0x1095:0x0646 rev 0x07) CMD64x_IDE 0000:00:02.0: BAR 0: can't reserve [io 0x8050-0x8057] cmd64x 0000:00:02.0: can't reserve resources CMD64x_IDE: probe of 0000:00:02.0 failed with error -16 Fix the problem by adding missing freeing of PCI BARs to ide_setup_pci_controller() and ide_pci_init_two(). Fixes: 30fdfb9 ("PCI: Add a call to pci_assign_irq() in pci_device_probe()") Fixes: 0e4c2ee ("alpha/PCI: Replace pci_fixup_irqs() call with host bridge IRQ mapping hooks") Link: http://lkml.kernel.org/r/[email protected] Reported-by: Guenter Roeck <[email protected]> Tested-by: Guenter Roeck <[email protected]> Signed-off-by: Bartlomiej Zolnierkiewicz <[email protected]> [bhelgaas: add Fixes:] Signed-off-by: Bjorn Helgaas <[email protected]> Cc: Richard Henderson <[email protected]> Cc: Ivan Kokshaysky <[email protected]> Cc: Matt Turner <[email protected]>
1 parent d410a64 commit a06876a

File tree

1 file changed

+40
-23
lines changed

1 file changed

+40
-23
lines changed

drivers/ide/setup-pci.c

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ EXPORT_SYMBOL_GPL(ide_setup_pci_noise);
179179
/**
180180
* ide_pci_enable - do PCI enables
181181
* @dev: PCI device
182+
* @bars: PCI BARs mask
182183
* @d: IDE port info
183184
*
184185
* Enable the IDE PCI device. We attempt to enable the device in full
@@ -189,9 +190,10 @@ EXPORT_SYMBOL_GPL(ide_setup_pci_noise);
189190
* Returns zero on success or an error code
190191
*/
191192

192-
static int ide_pci_enable(struct pci_dev *dev, const struct ide_port_info *d)
193+
static int ide_pci_enable(struct pci_dev *dev, int bars,
194+
const struct ide_port_info *d)
193195
{
194-
int ret, bars;
196+
int ret;
195197

196198
if (pci_enable_device(dev)) {
197199
ret = pci_enable_device_io(dev);
@@ -216,18 +218,6 @@ static int ide_pci_enable(struct pci_dev *dev, const struct ide_port_info *d)
216218
goto out;
217219
}
218220

219-
if (d->host_flags & IDE_HFLAG_SINGLE)
220-
bars = (1 << 2) - 1;
221-
else
222-
bars = (1 << 4) - 1;
223-
224-
if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0) {
225-
if (d->host_flags & IDE_HFLAG_CS5520)
226-
bars |= (1 << 2);
227-
else
228-
bars |= (1 << 4);
229-
}
230-
231221
ret = pci_request_selected_regions(dev, bars, d->name);
232222
if (ret < 0)
233223
printk(KERN_ERR "%s %s: can't reserve resources\n",
@@ -403,6 +393,7 @@ int ide_hwif_setup_dma(ide_hwif_t *hwif, const struct ide_port_info *d)
403393
/**
404394
* ide_setup_pci_controller - set up IDE PCI
405395
* @dev: PCI device
396+
* @bars: PCI BARs mask
406397
* @d: IDE port info
407398
* @noisy: verbose flag
408399
*
@@ -411,7 +402,7 @@ int ide_hwif_setup_dma(ide_hwif_t *hwif, const struct ide_port_info *d)
411402
* and enables it if need be
412403
*/
413404

414-
static int ide_setup_pci_controller(struct pci_dev *dev,
405+
static int ide_setup_pci_controller(struct pci_dev *dev, int bars,
415406
const struct ide_port_info *d, int noisy)
416407
{
417408
int ret;
@@ -420,24 +411,28 @@ static int ide_setup_pci_controller(struct pci_dev *dev,
420411
if (noisy)
421412
ide_setup_pci_noise(dev, d);
422413

423-
ret = ide_pci_enable(dev, d);
414+
ret = ide_pci_enable(dev, bars, d);
424415
if (ret < 0)
425416
goto out;
426417

427418
ret = pci_read_config_word(dev, PCI_COMMAND, &pcicmd);
428419
if (ret < 0) {
429420
printk(KERN_ERR "%s %s: error accessing PCI regs\n",
430421
d->name, pci_name(dev));
431-
goto out;
422+
goto out_free_bars;
432423
}
433424
if (!(pcicmd & PCI_COMMAND_IO)) { /* is device disabled? */
434425
ret = ide_pci_configure(dev, d);
435426
if (ret < 0)
436-
goto out;
427+
goto out_free_bars;
437428
printk(KERN_INFO "%s %s: device enabled (Linux)\n",
438429
d->name, pci_name(dev));
439430
}
440431

432+
goto out;
433+
434+
out_free_bars:
435+
pci_release_selected_regions(dev, bars);
441436
out:
442437
return ret;
443438
}
@@ -540,21 +535,36 @@ int ide_pci_init_two(struct pci_dev *dev1, struct pci_dev *dev2,
540535
{
541536
struct pci_dev *pdev[] = { dev1, dev2 };
542537
struct ide_host *host;
543-
int ret, i, n_ports = dev2 ? 4 : 2;
538+
int ret, i, n_ports = dev2 ? 4 : 2, bars;
544539
struct ide_hw hw[4], *hws[] = { NULL, NULL, NULL, NULL };
545540

541+
if (d->host_flags & IDE_HFLAG_SINGLE)
542+
bars = (1 << 2) - 1;
543+
else
544+
bars = (1 << 4) - 1;
545+
546+
if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0) {
547+
if (d->host_flags & IDE_HFLAG_CS5520)
548+
bars |= (1 << 2);
549+
else
550+
bars |= (1 << 4);
551+
}
552+
546553
for (i = 0; i < n_ports / 2; i++) {
547-
ret = ide_setup_pci_controller(pdev[i], d, !i);
548-
if (ret < 0)
554+
ret = ide_setup_pci_controller(pdev[i], bars, d, !i);
555+
if (ret < 0) {
556+
if (i == 1)
557+
pci_release_selected_regions(pdev[0], bars);
549558
goto out;
559+
}
550560

551561
ide_pci_setup_ports(pdev[i], d, &hw[i*2], &hws[i*2]);
552562
}
553563

554564
host = ide_host_alloc(d, hws, n_ports);
555565
if (host == NULL) {
556566
ret = -ENOMEM;
557-
goto out;
567+
goto out_free_bars;
558568
}
559569

560570
host->dev[0] = &dev1->dev;
@@ -576,7 +586,7 @@ int ide_pci_init_two(struct pci_dev *dev1, struct pci_dev *dev2,
576586
* do_ide_setup_pci_device() on the first device!
577587
*/
578588
if (ret < 0)
579-
goto out;
589+
goto out_free_bars;
580590

581591
/* fixup IRQ */
582592
if (ide_pci_is_in_compatibility_mode(pdev[i])) {
@@ -589,6 +599,13 @@ int ide_pci_init_two(struct pci_dev *dev1, struct pci_dev *dev2,
589599
ret = ide_host_register(host, d, hws);
590600
if (ret)
591601
ide_host_free(host);
602+
else
603+
goto out;
604+
605+
out_free_bars:
606+
i = n_ports / 2;
607+
while (i--)
608+
pci_release_selected_regions(pdev[i], bars);
592609
out:
593610
return ret;
594611
}

0 commit comments

Comments
 (0)