blob: dc1876feb06f9e4b12ebfb81c10ab4964394ef7a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IBM Hot Plug Controller Driver
Bjorn Helgaasf7625982013-11-14 11:28:18 -07003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Written By: Irene Zubarev, IBM Corporation
Bjorn Helgaasf7625982013-11-14 11:28:18 -07005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
7 * Copyright (C) 2001,2002 IBM Corp.
8 *
9 * All rights reserved.
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 (at
14 * your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
19 * NON INFRINGEMENT. See the GNU General Public License for more
20 * details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 * Send feedback to <gregkh@us.ibm.com>
27 *
28 */
29
30#include <linux/module.h>
31#include <linux/slab.h>
32#include <linux/pci.h>
33#include <linux/list.h>
34#include "ibmphp.h"
35
36
37static int configure_device(struct pci_func *);
38static int configure_bridge(struct pci_func **, u8);
39static struct res_needed *scan_behind_bridge(struct pci_func *, u8);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -080040static int add_new_bus(struct bus_node *, struct resource_node *, struct resource_node *, struct resource_node *, u8);
41static u8 find_sec_number(u8 primary_busno, u8 slotno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
44 * NOTE..... If BIOS doesn't provide default routing, we assign:
Bjorn Helgaasf7625982013-11-14 11:28:18 -070045 * 9 for SCSI, 10 for LAN adapters, and 11 for everything else.
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * If adapter is bridged, then we assign 11 to it and devices behind it.
47 * We also assign the same irq numbers for multi function devices.
48 * These are PIC mode, so shouldn't matter n.e.ways (hopefully)
49 */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -080050static void assign_alt_irq(struct pci_func *cur_func, u8 class_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 int j;
53 for (j = 0; j < 4; j++) {
54 if (cur_func->irq[j] == 0xff) {
55 switch (class_code) {
56 case PCI_BASE_CLASS_STORAGE:
57 cur_func->irq[j] = SCSI_IRQ;
58 break;
59 case PCI_BASE_CLASS_NETWORK:
60 cur_func->irq[j] = LAN_IRQ;
61 break;
62 default:
63 cur_func->irq[j] = OTHER_IRQ;
64 break;
65 }
66 }
67 }
68}
69
70/*
71 * Configures the device to be added (will allocate needed resources if it
72 * can), the device can be a bridge or a regular pci device, can also be
73 * multi-functional
Bjorn Helgaasf7625982013-11-14 11:28:18 -070074 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * Input: function to be added
Bjorn Helgaasf7625982013-11-14 11:28:18 -070076 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * TO DO: The error case with Multifunction device or multi function bridge,
Bjorn Helgaasf7625982013-11-14 11:28:18 -070078 * if there is an error, will need to go through all previous functions and
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * unconfigure....or can add some code into unconfigure_card....
80 */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -080081int ibmphp_configure_card(struct pci_func *func, u8 slotno)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 u16 vendor_id;
84 u32 class;
85 u8 class_code;
86 u8 hdr_type, device, sec_number;
87 u8 function;
88 struct pci_func *newfunc; /* for multi devices */
89 struct pci_func *cur_func, *prev_func;
90 int rc, i, j;
91 int cleanup_count;
92 u8 flag;
93 u8 valid_device = 0x00; /* to see if we are able to read from card any device info at all */
94
Bogicevic Sasaff3ce482015-12-27 13:21:11 -080095 debug("inside configure_card, func->busno = %x\n", func->busno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 device = func->device;
98 cur_func = func;
99
100 /* We only get bus and device from IRQ routing table. So at this point,
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700101 * func->busno is correct, and func->device contains only device (at the 5
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 * highest bits)
103 */
104
105 /* For every function on the card */
106 for (function = 0x00; function < 0x08; function++) {
107 unsigned int devfn = PCI_DEVFN(device, function);
108 ibmphp_pci_bus->number = cur_func->busno;
109
110 cur_func->function = function;
111
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800112 debug("inside the loop, cur_func->busno = %x, cur_func->device = %x, cur_func->function = %x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 cur_func->busno, cur_func->device, cur_func->function);
114
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800115 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800117 debug("vendor_id is %x\n", vendor_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
119 /* found correct device!!! */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800120 debug("found valid device, vendor_id = %x\n", vendor_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 ++valid_device;
123
124 /* header: x x x x x x x x
125 * | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
126 * |_=> 0 = single function device, 1 = multi-function device
127 */
128
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800129 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
130 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 class_code = class >> 24;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800133 debug("hrd_type = %x, class = %x, class_code %x\n", hdr_type, class, class_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 class >>= 8; /* to take revision out, class = class.subclass.prog i/f */
135 if (class == PCI_CLASS_NOT_DEFINED_VGA) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800136 err("The device %x is VGA compatible and as is not supported for hot plugging. "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 "Please choose another device.\n", cur_func->device);
138 return -ENODEV;
139 } else if (class == PCI_CLASS_DISPLAY_VGA) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800140 err("The device %x is not supported for hot plugging. Please choose another device.\n",
Ryan Desfosses227f0642014-04-18 20:13:50 -0400141 cur_func->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return -ENODEV;
143 }
144 switch (hdr_type) {
145 case PCI_HEADER_TYPE_NORMAL:
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800146 debug("single device case.... vendor id = %x, hdr_type = %x, class = %x\n", vendor_id, hdr_type, class);
147 assign_alt_irq(cur_func, class_code);
Quentin Lambert79e50e72014-09-07 20:03:32 +0200148 rc = configure_device(cur_func);
149 if (rc < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 /* We need to do this in case some other BARs were properly inserted */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800151 err("was not able to configure devfunc %x on bus %x.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 cur_func->device, cur_func->busno);
153 cleanup_count = 6;
154 goto error;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 cur_func->next = NULL;
157 function = 0x8;
158 break;
159 case PCI_HEADER_TYPE_MULTIDEVICE:
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800160 assign_alt_irq(cur_func, class_code);
Quentin Lambert79e50e72014-09-07 20:03:32 +0200161 rc = configure_device(cur_func);
162 if (rc < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 /* We need to do this in case some other BARs were properly inserted */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800164 err("was not able to configure devfunc %x on bus %x...bailing out\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 cur_func->device, cur_func->busno);
166 cleanup_count = 6;
167 goto error;
168 }
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100169 newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (!newfunc) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800171 err("out of system memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return -ENOMEM;
173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 newfunc->busno = cur_func->busno;
175 newfunc->device = device;
176 cur_func->next = newfunc;
177 cur_func = newfunc;
178 for (j = 0; j < 4; j++)
179 newfunc->irq[j] = cur_func->irq[j];
180 break;
181 case PCI_HEADER_TYPE_MULTIBRIDGE:
182 class >>= 8;
183 if (class != PCI_CLASS_BRIDGE_PCI) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800184 err("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging. Please insert another card.\n",
Ryan Desfosses227f0642014-04-18 20:13:50 -0400185 cur_func->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return -ENODEV;
187 }
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800188 assign_alt_irq(cur_func, class_code);
189 rc = configure_bridge(&cur_func, slotno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (rc == -ENODEV) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800191 err("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
192 err("Bus %x, devfunc %x\n", cur_func->busno, cur_func->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return rc;
194 }
195 if (rc) {
196 /* We need to do this in case some other BARs were properly inserted */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800197 err("was not able to hot-add PPB properly.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
199 cleanup_count = 2;
200 goto error;
201 }
202
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800203 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
Kristen Accardidc6712d2006-03-14 16:24:47 -0800204 flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 for (i = 0; i < 32; i++) {
206 if (func->devices[i]) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100207 newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (!newfunc) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800209 err("out of system memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return -ENOMEM;
211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 newfunc->busno = sec_number;
213 newfunc->device = (u8) i;
214 for (j = 0; j < 4; j++)
215 newfunc->irq[j] = cur_func->irq[j];
216
217 if (flag) {
218 for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next) ;
219 prev_func->next = newfunc;
220 } else
221 cur_func->next = newfunc;
222
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800223 rc = ibmphp_configure_card(newfunc, slotno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 /* This could only happen if kmalloc failed */
225 if (rc) {
226 /* We need to do this in case bridge itself got configured properly, but devices behind it failed */
227 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
228 cleanup_count = 2;
229 goto error;
230 }
Kristen Accardidc6712d2006-03-14 16:24:47 -0800231 flag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233 }
234
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100235 newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (!newfunc) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800237 err("out of system memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return -ENOMEM;
239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 newfunc->busno = cur_func->busno;
241 newfunc->device = device;
242 for (j = 0; j < 4; j++)
243 newfunc->irq[j] = cur_func->irq[j];
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800244 for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 prev_func->next = newfunc;
246 cur_func = newfunc;
247 break;
248 case PCI_HEADER_TYPE_BRIDGE:
249 class >>= 8;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800250 debug("class now is %x\n", class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (class != PCI_CLASS_BRIDGE_PCI) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800252 err("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging. Please insert another card.\n",
Ryan Desfosses227f0642014-04-18 20:13:50 -0400253 cur_func->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return -ENODEV;
255 }
256
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800257 assign_alt_irq(cur_func, class_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800259 debug("cur_func->busno b4 configure_bridge is %x\n", cur_func->busno);
260 rc = configure_bridge(&cur_func, slotno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (rc == -ENODEV) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800262 err("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
263 err("Bus %x, devfunc %x\n", cur_func->busno, cur_func->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return rc;
265 }
266 if (rc) {
267 /* We need to do this in case some other BARs were properly inserted */
268 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800269 err("was not able to hot-add PPB properly.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 cleanup_count = 2;
271 goto error;
272 }
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800273 debug("cur_func->busno = %x, device = %x, function = %x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 cur_func->busno, device, function);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800275 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
276 debug("after configuring bridge..., sec_number = %x\n", sec_number);
Kristen Accardidc6712d2006-03-14 16:24:47 -0800277 flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 for (i = 0; i < 32; i++) {
279 if (func->devices[i]) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800280 debug("inside for loop, device is %x\n", i);
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100281 newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (!newfunc) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800283 err(" out of system memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return -ENOMEM;
285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 newfunc->busno = sec_number;
287 newfunc->device = (u8) i;
288 for (j = 0; j < 4; j++)
289 newfunc->irq[j] = cur_func->irq[j];
290
291 if (flag) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800292 for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 prev_func->next = newfunc;
294 } else
295 cur_func->next = newfunc;
296
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800297 rc = ibmphp_configure_card(newfunc, slotno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 /* Again, this case should not happen... For complete paranoia, will need to call remove_bus */
300 if (rc) {
301 /* We need to do this in case some other BARs were properly inserted */
302 func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
303 cleanup_count = 2;
304 goto error;
305 }
Kristen Accardidc6712d2006-03-14 16:24:47 -0800306 flag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308 }
309
310 function = 0x8;
311 break;
312 default:
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800313 err("MAJOR PROBLEM!!!!, header type not supported? %x\n", hdr_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return -ENXIO;
315 break;
316 } /* end of switch */
317 } /* end of valid device */
318 } /* end of for */
319
320 if (!valid_device) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800321 err("Cannot find any valid devices on the card. Or unable to read from card.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return -ENODEV;
323 }
324
325 return 0;
326
327error:
328 for (i = 0; i < cleanup_count; i++) {
329 if (cur_func->io[i]) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800330 ibmphp_remove_resource(cur_func->io[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 cur_func->io[i] = NULL;
332 } else if (cur_func->pfmem[i]) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800333 ibmphp_remove_resource(cur_func->pfmem[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 cur_func->pfmem[i] = NULL;
335 } else if (cur_func->mem[i]) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800336 ibmphp_remove_resource(cur_func->mem[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 cur_func->mem[i] = NULL;
338 }
339 }
340 return rc;
341}
342
343/*
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700344 * This function configures the pci BARs of a single device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 * Input: pointer to the pci_func
346 * Output: configured PCI, 0, or error
347 */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800348static int configure_device(struct pci_func *func)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 u32 bar[6];
351 u32 address[] = {
352 PCI_BASE_ADDRESS_0,
353 PCI_BASE_ADDRESS_1,
354 PCI_BASE_ADDRESS_2,
355 PCI_BASE_ADDRESS_3,
356 PCI_BASE_ADDRESS_4,
357 PCI_BASE_ADDRESS_5,
358 0
359 };
360 u8 irq;
361 int count;
362 int len[6];
363 struct resource_node *io[6];
364 struct resource_node *mem[6];
365 struct resource_node *mem_tmp;
366 struct resource_node *pfmem[6];
367 unsigned int devfn;
368
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800369 debug("%s - inside\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 devfn = PCI_DEVFN(func->device, func->function);
372 ibmphp_pci_bus->number = func->busno;
373
374 for (count = 0; address[count]; count++) { /* for 6 BARs */
375
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700376 /* not sure if i need this. per scott, said maybe need * something like this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if devices don't adhere 100% to the spec, so don't want to write
378 to the reserved bits
379
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700380 pcibios_read_config_byte(cur_func->busno, cur_func->device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 PCI_BASE_ADDRESS_0 + 4 * count, &tmp);
382 if (tmp & 0x01) // IO
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700383 pcibios_write_config_dword(cur_func->busno, cur_func->device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFD);
385 else // Memory
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700386 pcibios_write_config_dword(cur_func->busno, cur_func->device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFF);
388 */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800389 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
390 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 if (!bar[count]) /* This BAR is not implemented */
393 continue;
394
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800395 debug("Device %x BAR %d wants %x\n", func->device, count, bar[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
398 /* This is IO */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800399 debug("inside IO SPACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 len[count] = bar[count] & 0xFFFFFFFC;
402 len[count] = ~len[count] + 1;
403
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800404 debug("len[count] in IO %x, count %d\n", len[count], count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100406 io[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 if (!io[count]) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800409 err("out of system memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return -ENOMEM;
411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 io[count]->type = IO;
413 io[count]->busno = func->busno;
414 io[count]->devfunc = PCI_DEVFN(func->device, func->function);
415 io[count]->len = len[count];
416 if (ibmphp_check_resource(io[count], 0) == 0) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800417 ibmphp_add_resource(io[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 func->io[count] = io[count];
419 } else {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800420 err("cannot allocate requested io for bus %x device %x function %x len %x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 func->busno, func->device, func->function, len[count]);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800422 kfree(io[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return -EIO;
424 }
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800425 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->io[count]->start);
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700426
427 /* _______________This is for debugging purposes only_____________________ */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800428 debug("b4 writing, the IO address is %x\n", func->io[count]->start);
429 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
430 debug("after writing.... the start address is %x\n", bar[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /* _________________________________________________________________________*/
432
433 } else {
434 /* This is Memory */
435 if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
436 /* pfmem */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800437 debug("PFMEM SPACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 len[count] = bar[count] & 0xFFFFFFF0;
440 len[count] = ~len[count] + 1;
441
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800442 debug("len[count] in PFMEM %x, count %d\n", len[count], count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100444 pfmem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (!pfmem[count]) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800446 err("out of system memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return -ENOMEM;
448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 pfmem[count]->type = PFMEM;
450 pfmem[count]->busno = func->busno;
451 pfmem[count]->devfunc = PCI_DEVFN(func->device,
452 func->function);
453 pfmem[count]->len = len[count];
Kristen Accardidc6712d2006-03-14 16:24:47 -0800454 pfmem[count]->fromMem = 0;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800455 if (ibmphp_check_resource(pfmem[count], 0) == 0) {
456 ibmphp_add_resource(pfmem[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 func->pfmem[count] = pfmem[count];
458 } else {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100459 mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (!mem_tmp) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800461 err("out of system memory\n");
462 kfree(pfmem[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return -ENOMEM;
464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 mem_tmp->type = MEM;
466 mem_tmp->busno = pfmem[count]->busno;
467 mem_tmp->devfunc = pfmem[count]->devfunc;
468 mem_tmp->len = pfmem[count]->len;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800469 debug("there's no pfmem... going into mem.\n");
470 if (ibmphp_check_resource(mem_tmp, 0) == 0) {
471 ibmphp_add_resource(mem_tmp);
Kristen Accardidc6712d2006-03-14 16:24:47 -0800472 pfmem[count]->fromMem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 pfmem[count]->rangeno = mem_tmp->rangeno;
474 pfmem[count]->start = mem_tmp->start;
475 pfmem[count]->end = mem_tmp->end;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800476 ibmphp_add_pfmem_from_mem(pfmem[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 func->pfmem[count] = pfmem[count];
478 } else {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800479 err("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 func->busno, func->device, len[count]);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800481 kfree(mem_tmp);
482 kfree(pfmem[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return -EIO;
484 }
485 }
486
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800487 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->pfmem[count]->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700489 /*_______________This is for debugging purposes only______________________________*/
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800490 debug("b4 writing, start address is %x\n", func->pfmem[count]->start);
491 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
492 debug("after writing, start address is %x\n", bar[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /*_________________________________________________________________________________*/
494
495 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) { /* takes up another dword */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800496 debug("inside the mem 64 case, count %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 count += 1;
498 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800499 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501 } else {
502 /* regular memory */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800503 debug("REGULAR MEM SPACE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 len[count] = bar[count] & 0xFFFFFFF0;
506 len[count] = ~len[count] + 1;
507
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800508 debug("len[count] in Mem %x, count %d\n", len[count], count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100510 mem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (!mem[count]) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800512 err("out of system memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return -ENOMEM;
514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 mem[count]->type = MEM;
516 mem[count]->busno = func->busno;
517 mem[count]->devfunc = PCI_DEVFN(func->device,
518 func->function);
519 mem[count]->len = len[count];
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800520 if (ibmphp_check_resource(mem[count], 0) == 0) {
521 ibmphp_add_resource(mem[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 func->mem[count] = mem[count];
523 } else {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800524 err("cannot allocate requested mem for bus %x, device %x, len %x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 func->busno, func->device, len[count]);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800526 kfree(mem[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return -EIO;
528 }
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800529 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->mem[count]->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /* _______________________This is for debugging purposes only _______________________*/
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800531 debug("b4 writing, start address is %x\n", func->mem[count]->start);
532 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
533 debug("after writing, the address is %x\n", bar[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /* __________________________________________________________________________________*/
535
536 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
537 /* takes up another dword */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800538 debug("inside mem 64 case, reg. mem, count %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 count += 1;
540 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800541 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543 }
544 } /* end of mem */
545 } /* end of for */
546
547 func->bus = 0; /* To indicate that this is not a PPB */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800548 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if ((irq > 0x00) && (irq < 0x05))
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800550 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800552 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_CACHE_LINE_SIZE, CACHE);
553 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_LATENCY_TIMER, LATENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800555 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, PCI_ROM_ADDRESS, 0x00L);
556 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 return 0;
559}
560
561/******************************************************************************
562 * This routine configures a PCI-2-PCI bridge and the functions behind it
563 * Parameters: pci_func
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700564 * Returns:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 ******************************************************************************/
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800566static int configure_bridge(struct pci_func **func_passed, u8 slotno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
568 int count;
569 int i;
570 int rc;
571 u8 sec_number;
572 u8 io_base;
573 u16 pfmem_base;
574 u32 bar[2];
575 u32 len[2];
Kristen Accardidc6712d2006-03-14 16:24:47 -0800576 u8 flag_io = 0;
577 u8 flag_mem = 0;
578 u8 flag_pfmem = 0;
579 u8 need_io_upper = 0;
580 u8 need_pfmem_upper = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 struct res_needed *amount_needed = NULL;
582 struct resource_node *io = NULL;
583 struct resource_node *bus_io[2] = {NULL, NULL};
584 struct resource_node *mem = NULL;
585 struct resource_node *bus_mem[2] = {NULL, NULL};
586 struct resource_node *mem_tmp = NULL;
587 struct resource_node *pfmem = NULL;
588 struct resource_node *bus_pfmem[2] = {NULL, NULL};
589 struct bus_node *bus;
590 u32 address[] = {
591 PCI_BASE_ADDRESS_0,
592 PCI_BASE_ADDRESS_1,
593 0
594 };
595 struct pci_func *func = *func_passed;
596 unsigned int devfn;
597 u8 irq;
598 int retval;
599
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800600 debug("%s - enter\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 devfn = PCI_DEVFN(func->function, func->device);
603 ibmphp_pci_bus->number = func->busno;
604
605 /* Configuring necessary info for the bridge so that we could see the devices
606 * behind it
607 */
608
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800609 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, func->busno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 /* _____________________For debugging purposes only __________________________
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800612 pci_bus_config_byte(ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
613 debug("primary # written into the bridge is %x\n", pri_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 ___________________________________________________________________________*/
615
616 /* in EBDA, only get allocated 1 additional bus # per slot */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800617 sec_number = find_sec_number(func->busno, slotno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (sec_number == 0xff) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800619 err("cannot allocate secondary bus number for the bridged device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return -EINVAL;
621 }
622
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800623 debug("after find_sec_number, the number we got is %x\n", sec_number);
624 debug("AFTER FIND_SEC_NUMBER, func->busno IS %x\n", func->busno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800626 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, sec_number);
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 /* __________________For debugging purposes only __________________________________
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800629 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
630 debug("sec_number after write/read is %x\n", sec_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 ________________________________________________________________________________*/
632
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800633 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, sec_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 /* __________________For debugging purposes only ____________________________________
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800636 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sec_number);
637 debug("subordinate number after write/read is %x\n", sec_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 __________________________________________________________________________________*/
639
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800640 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_CACHE_LINE_SIZE, CACHE);
641 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_LATENCY_TIMER, LATENCY);
642 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_SEC_LATENCY_TIMER, LATENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800644 debug("func->busno is %x\n", func->busno);
645 debug("sec_number after writing is %x\n", sec_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647
648 /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700649 !!!!!!!!!!!!!!!NEED TO ADD!!! FAST BACK-TO-BACK ENABLE!!!!!!!!!!!!!!!!!!!!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
651
652
653 /* First we need to allocate mem/io for the bridge itself in case it needs it */
654 for (count = 0; address[count]; count++) { /* for 2 BARs */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800655 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
656 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 if (!bar[count]) {
659 /* This BAR is not implemented */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800660 debug("so we come here then, eh?, count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 continue;
662 }
663 // tmp_bar = bar[count];
664
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800665 debug("Bar %d wants %x\n", count, bar[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
668 /* This is IO */
669 len[count] = bar[count] & 0xFFFFFFFC;
670 len[count] = ~len[count] + 1;
671
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800672 debug("len[count] in IO = %x\n", len[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100674 bus_io[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (!bus_io[count]) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800677 err("out of system memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 retval = -ENOMEM;
679 goto error;
680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 bus_io[count]->type = IO;
682 bus_io[count]->busno = func->busno;
683 bus_io[count]->devfunc = PCI_DEVFN(func->device,
684 func->function);
685 bus_io[count]->len = len[count];
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800686 if (ibmphp_check_resource(bus_io[count], 0) == 0) {
687 ibmphp_add_resource(bus_io[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 func->io[count] = bus_io[count];
689 } else {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800690 err("cannot allocate requested io for bus %x, device %x, len %x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 func->busno, func->device, len[count]);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800692 kfree(bus_io[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return -EIO;
694 }
695
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800696 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->io[count]->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 } else {
699 /* This is Memory */
700 if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
701 /* pfmem */
702 len[count] = bar[count] & 0xFFFFFFF0;
703 len[count] = ~len[count] + 1;
704
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800705 debug("len[count] in PFMEM = %x\n", len[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100707 bus_pfmem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (!bus_pfmem[count]) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800709 err("out of system memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 retval = -ENOMEM;
711 goto error;
712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 bus_pfmem[count]->type = PFMEM;
714 bus_pfmem[count]->busno = func->busno;
715 bus_pfmem[count]->devfunc = PCI_DEVFN(func->device,
716 func->function);
717 bus_pfmem[count]->len = len[count];
Kristen Accardidc6712d2006-03-14 16:24:47 -0800718 bus_pfmem[count]->fromMem = 0;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800719 if (ibmphp_check_resource(bus_pfmem[count], 0) == 0) {
720 ibmphp_add_resource(bus_pfmem[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 func->pfmem[count] = bus_pfmem[count];
722 } else {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100723 mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (!mem_tmp) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800725 err("out of system memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 retval = -ENOMEM;
727 goto error;
728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 mem_tmp->type = MEM;
730 mem_tmp->busno = bus_pfmem[count]->busno;
731 mem_tmp->devfunc = bus_pfmem[count]->devfunc;
732 mem_tmp->len = bus_pfmem[count]->len;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800733 if (ibmphp_check_resource(mem_tmp, 0) == 0) {
734 ibmphp_add_resource(mem_tmp);
Kristen Accardidc6712d2006-03-14 16:24:47 -0800735 bus_pfmem[count]->fromMem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 bus_pfmem[count]->rangeno = mem_tmp->rangeno;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800737 ibmphp_add_pfmem_from_mem(bus_pfmem[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 func->pfmem[count] = bus_pfmem[count];
739 } else {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800740 err("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 func->busno, func->device, len[count]);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800742 kfree(mem_tmp);
743 kfree(bus_pfmem[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return -EIO;
745 }
746 }
747
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800748 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->pfmem[count]->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
751 /* takes up another dword */
752 count += 1;
753 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800754 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 }
757 } else {
758 /* regular memory */
759 len[count] = bar[count] & 0xFFFFFFF0;
760 len[count] = ~len[count] + 1;
761
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800762 debug("len[count] in Memory is %x\n", len[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100764 bus_mem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (!bus_mem[count]) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800766 err("out of system memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 retval = -ENOMEM;
768 goto error;
769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 bus_mem[count]->type = MEM;
771 bus_mem[count]->busno = func->busno;
772 bus_mem[count]->devfunc = PCI_DEVFN(func->device,
773 func->function);
774 bus_mem[count]->len = len[count];
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800775 if (ibmphp_check_resource(bus_mem[count], 0) == 0) {
776 ibmphp_add_resource(bus_mem[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 func->mem[count] = bus_mem[count];
778 } else {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800779 err("cannot allocate requested mem for bus %x, device %x, len %x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 func->busno, func->device, len[count]);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800781 kfree(bus_mem[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return -EIO;
783 }
784
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800785 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->mem[count]->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
788 /* takes up another dword */
789 count += 1;
790 /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800791 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 }
794 }
795 } /* end of mem */
796 } /* end of for */
797
798 /* Now need to see how much space the devices behind the bridge needed */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800799 amount_needed = scan_behind_bridge(func, sec_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (amount_needed == NULL)
801 return -ENOMEM;
802
803 ibmphp_pci_bus->number = func->busno;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800804 debug("after coming back from scan_behind_bridge\n");
805 debug("amount_needed->not_correct = %x\n", amount_needed->not_correct);
806 debug("amount_needed->io = %x\n", amount_needed->io);
807 debug("amount_needed->mem = %x\n", amount_needed->mem);
808 debug("amount_needed->pfmem = %x\n", amount_needed->pfmem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700810 if (amount_needed->not_correct) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800811 debug("amount_needed is not correct\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 for (count = 0; address[count]; count++) {
813 /* for 2 BARs */
814 if (bus_io[count]) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800815 ibmphp_remove_resource(bus_io[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 func->io[count] = NULL;
817 } else if (bus_pfmem[count]) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800818 ibmphp_remove_resource(bus_pfmem[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 func->pfmem[count] = NULL;
820 } else if (bus_mem[count]) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800821 ibmphp_remove_resource(bus_mem[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 func->mem[count] = NULL;
823 }
824 }
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800825 kfree(amount_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return -ENODEV;
827 }
828
829 if (!amount_needed->io) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800830 debug("it doesn't want IO?\n");
Kristen Accardidc6712d2006-03-14 16:24:47 -0800831 flag_io = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 } else {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800833 debug("it wants %x IO behind the bridge\n", amount_needed->io);
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100834 io = kzalloc(sizeof(*io), GFP_KERNEL);
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (!io) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800837 err("out of system memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 retval = -ENOMEM;
839 goto error;
840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 io->type = IO;
842 io->busno = func->busno;
843 io->devfunc = PCI_DEVFN(func->device, func->function);
844 io->len = amount_needed->io;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800845 if (ibmphp_check_resource(io, 1) == 0) {
846 debug("were we able to add io\n");
847 ibmphp_add_resource(io);
Kristen Accardidc6712d2006-03-14 16:24:47 -0800848 flag_io = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850 }
851
852 if (!amount_needed->mem) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800853 debug("it doesn't want n.e.memory?\n");
Kristen Accardidc6712d2006-03-14 16:24:47 -0800854 flag_mem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 } else {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800856 debug("it wants %x memory behind the bridge\n", amount_needed->mem);
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100857 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (!mem) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800859 err("out of system memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 retval = -ENOMEM;
861 goto error;
862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 mem->type = MEM;
864 mem->busno = func->busno;
865 mem->devfunc = PCI_DEVFN(func->device, func->function);
866 mem->len = amount_needed->mem;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800867 if (ibmphp_check_resource(mem, 1) == 0) {
868 ibmphp_add_resource(mem);
Kristen Accardidc6712d2006-03-14 16:24:47 -0800869 flag_mem = 1;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800870 debug("were we able to add mem\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
872 }
873
874 if (!amount_needed->pfmem) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800875 debug("it doesn't want n.e.pfmem mem?\n");
Kristen Accardidc6712d2006-03-14 16:24:47 -0800876 flag_pfmem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 } else {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800878 debug("it wants %x pfmemory behind the bridge\n", amount_needed->pfmem);
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100879 pfmem = kzalloc(sizeof(*pfmem), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (!pfmem) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800881 err("out of system memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 retval = -ENOMEM;
883 goto error;
884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 pfmem->type = PFMEM;
886 pfmem->busno = func->busno;
887 pfmem->devfunc = PCI_DEVFN(func->device, func->function);
888 pfmem->len = amount_needed->pfmem;
Kristen Accardidc6712d2006-03-14 16:24:47 -0800889 pfmem->fromMem = 0;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800890 if (ibmphp_check_resource(pfmem, 1) == 0) {
891 ibmphp_add_resource(pfmem);
Kristen Accardidc6712d2006-03-14 16:24:47 -0800892 flag_pfmem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 } else {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100894 mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (!mem_tmp) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800896 err("out of system memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 retval = -ENOMEM;
898 goto error;
899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 mem_tmp->type = MEM;
901 mem_tmp->busno = pfmem->busno;
902 mem_tmp->devfunc = pfmem->devfunc;
903 mem_tmp->len = pfmem->len;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800904 if (ibmphp_check_resource(mem_tmp, 1) == 0) {
905 ibmphp_add_resource(mem_tmp);
Kristen Accardidc6712d2006-03-14 16:24:47 -0800906 pfmem->fromMem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 pfmem->rangeno = mem_tmp->rangeno;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800908 ibmphp_add_pfmem_from_mem(pfmem);
Kristen Accardidc6712d2006-03-14 16:24:47 -0800909 flag_pfmem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911 }
912 }
913
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800914 debug("b4 if (flag_io && flag_mem && flag_pfmem)\n");
915 debug("flag_io = %x, flag_mem = %x, flag_pfmem = %x\n", flag_io, flag_mem, flag_pfmem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 if (flag_io && flag_mem && flag_pfmem) {
918 /* If on bootup, there was a bridged card in this slot,
919 * then card was removed and ibmphp got unloaded and loaded
920 * back again, there's no way for us to remove the bus
921 * struct, so no need to kmalloc, can use existing node
922 */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800923 bus = ibmphp_find_res_bus(sec_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (!bus) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100925 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (!bus) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800927 err("out of system memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 retval = -ENOMEM;
929 goto error;
930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 bus->busno = sec_number;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800932 debug("b4 adding new bus\n");
933 rc = add_new_bus(bus, io, mem, pfmem, func->busno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 } else if (!(bus->rangeIO) && !(bus->rangeMem) && !(bus->rangePFMem))
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800935 rc = add_new_bus(bus, io, mem, pfmem, 0xFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 else {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800937 err("expected bus structure not empty?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 retval = -EIO;
939 goto error;
940 }
941 if (rc) {
942 if (rc == -ENOMEM) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800943 ibmphp_remove_bus(bus, func->busno);
944 kfree(amount_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return rc;
946 }
947 retval = rc;
948 goto error;
949 }
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800950 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, &io_base);
951 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &pfmem_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 if ((io_base & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800954 debug("io 32\n");
Kristen Accardidc6712d2006-03-14 16:24:47 -0800955 need_io_upper = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
Jordan, William P88d69a12005-11-21 22:51:57 -0800957 if ((pfmem_base & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800958 debug("pfmem 64\n");
Kristen Accardidc6712d2006-03-14 16:24:47 -0800959 need_pfmem_upper = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961
962 if (bus->noIORanges) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800963 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00 | bus->rangeIO->start >> 8);
964 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00 | bus->rangeIO->end >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 /* _______________This is for debugging purposes only ____________________
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800967 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, &temp);
968 debug("io_base = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
969 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, &temp);
970 debug("io_limit = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 ________________________________________________________________________*/
972
973 if (need_io_upper) { /* since can't support n.e.ways */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800974 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_IO_BASE_UPPER16, 0x0000);
975 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_IO_LIMIT_UPPER16, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977 } else {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800978 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00);
979 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
981
982 if (bus->noMemRanges) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800983 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0x0000 | bus->rangeMem->start >> 16);
984 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000 | bus->rangeMem->end >> 16);
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 /* ____________________This is for debugging purposes only ________________________
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800987 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, &temp);
988 debug("mem_base = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
989 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, &temp);
990 debug("mem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 __________________________________________________________________________________*/
992
993 } else {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800994 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0xffff);
995 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997 if (bus->noPFMemRanges) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800998 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0x0000 | bus->rangePFMem->start >> 16);
999 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000 | bus->rangePFMem->end >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 /* __________________________This is for debugging purposes only _______________________
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001002 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &temp);
1003 debug("pfmem_base = %x", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
1004 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &temp);
1005 debug("pfmem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 ______________________________________________________________________________________*/
1007
1008 if (need_pfmem_upper) { /* since can't support n.e.ways */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001009 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, PCI_PREF_BASE_UPPER32, 0x00000000);
1010 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, PCI_PREF_LIMIT_UPPER32, 0x00000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012 } else {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001013 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0xffff);
1014 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001017 debug("b4 writing control information\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001019 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if ((irq > 0x00) && (irq < 0x05))
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001021 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001022 /*
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001023 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, ctrl);
1024 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_PARITY);
1025 pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_SERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 */
1027
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001028 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE);
1029 pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, 0x07);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 for (i = 0; i < 32; i++) {
1031 if (amount_needed->devices[i]) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001032 debug("device where devices[i] is 1 = %x\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 func->devices[i] = 1;
1034 }
1035 }
1036 func->bus = 1; /* For unconfiguring, to indicate it's PPB */
1037 func_passed = &func;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001038 debug("func->busno b4 returning is %x\n", func->busno);
1039 debug("func->busno b4 returning in the other structure is %x\n", (*func_passed)->busno);
1040 kfree(amount_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return 0;
1042 } else {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001043 err("Configuring bridge was unsuccessful...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 mem_tmp = NULL;
1045 retval = -EIO;
1046 goto error;
1047 }
1048
1049error:
1050 kfree(amount_needed);
1051 if (pfmem)
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001052 ibmphp_remove_resource(pfmem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (io)
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001054 ibmphp_remove_resource(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (mem)
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001056 ibmphp_remove_resource(mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 for (i = 0; i < 2; i++) { /* for 2 BARs */
1058 if (bus_io[i]) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001059 ibmphp_remove_resource(bus_io[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 func->io[i] = NULL;
1061 } else if (bus_pfmem[i]) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001062 ibmphp_remove_resource(bus_pfmem[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 func->pfmem[i] = NULL;
1064 } else if (bus_mem[i]) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001065 ibmphp_remove_resource(bus_mem[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 func->mem[i] = NULL;
1067 }
1068 }
1069 return retval;
1070}
1071
1072/*****************************************************************************
1073 * This function adds up the amount of resources needed behind the PPB bridge
1074 * and passes it to the configure_bridge function
1075 * Input: bridge function
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001076 * Output: amount of resources needed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 *****************************************************************************/
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001078static struct res_needed *scan_behind_bridge(struct pci_func *func, u8 busno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
1080 int count, len[6];
1081 u16 vendor_id;
1082 u8 hdr_type;
1083 u8 device, function;
1084 unsigned int devfn;
1085 int howmany = 0; /*this is to see if there are any devices behind the bridge */
1086
1087 u32 bar[6], class;
1088 u32 address[] = {
1089 PCI_BASE_ADDRESS_0,
1090 PCI_BASE_ADDRESS_1,
1091 PCI_BASE_ADDRESS_2,
1092 PCI_BASE_ADDRESS_3,
1093 PCI_BASE_ADDRESS_4,
1094 PCI_BASE_ADDRESS_5,
1095 0
1096 };
1097 struct res_needed *amount;
1098
Eric Sesterhennf5afe802006-02-28 15:34:49 +01001099 amount = kzalloc(sizeof(*amount), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (amount == NULL)
1101 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 ibmphp_pci_bus->number = busno;
1104
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001105 debug("the bus_no behind the bridge is %x\n", busno);
1106 debug("scanning devices behind the bridge...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 for (device = 0; device < 32; device++) {
1108 amount->devices[device] = 0;
1109 for (function = 0; function < 8; function++) {
1110 devfn = PCI_DEVFN(device, function);
1111
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001112 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1115 /* found correct device!!! */
1116 howmany++;
1117
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001118 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
1119 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001121 debug("hdr_type behind the bridge is %x\n", hdr_type);
Bjorn Helgaas93de6902015-12-03 13:18:18 -06001122 if ((hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001123 err("embedded bridges not supported for hot-plugging.\n");
Kristen Accardidc6712d2006-03-14 16:24:47 -08001124 amount->not_correct = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return amount;
1126 }
1127
1128 class >>= 8; /* to take revision out, class = class.subclass.prog i/f */
1129 if (class == PCI_CLASS_NOT_DEFINED_VGA) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001130 err("The device %x is VGA compatible and as is not supported for hot plugging. Please choose another device.\n", device);
Kristen Accardidc6712d2006-03-14 16:24:47 -08001131 amount->not_correct = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return amount;
1133 } else if (class == PCI_CLASS_DISPLAY_VGA) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001134 err("The device %x is not supported for hot plugging. Please choose another device.\n", device);
Kristen Accardidc6712d2006-03-14 16:24:47 -08001135 amount->not_correct = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return amount;
1137 }
1138
1139 amount->devices[device] = 1;
1140
1141 for (count = 0; address[count]; count++) {
1142 /* for 6 BARs */
1143 /*
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001144 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, address[count], &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (tmp & 0x01) // IO
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001146 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 else // MEMORY
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001148 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001150 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1151 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001153 debug("what is bar[count]? %x, count = %d\n", bar[count], count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155 if (!bar[count]) /* This BAR is not implemented */
1156 continue;
1157
1158 //tmp_bar = bar[count];
1159
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001160 debug("count %d device %x function %x wants %x resources\n", count, device, function, bar[count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
1163 /* This is IO */
1164 len[count] = bar[count] & 0xFFFFFFFC;
1165 len[count] = ~len[count] + 1;
1166 amount->io += len[count];
1167 } else {
1168 /* This is Memory */
1169 if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1170 /* pfmem */
1171 len[count] = bar[count] & 0xFFFFFFF0;
1172 len[count] = ~len[count] + 1;
1173 amount->pfmem += len[count];
1174 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64)
1175 /* takes up another dword */
1176 count += 1;
1177
1178 } else {
1179 /* regular memory */
1180 len[count] = bar[count] & 0xFFFFFFF0;
1181 len[count] = ~len[count] + 1;
1182 amount->mem += len[count];
1183 if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1184 /* takes up another dword */
1185 count += 1;
1186 }
1187 }
1188 }
1189 } /* end for */
1190 } /* end if (valid) */
1191 } /* end for */
1192 } /* end for */
1193
1194 if (!howmany)
Kristen Accardidc6712d2006-03-14 16:24:47 -08001195 amount->not_correct = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 else
Kristen Accardidc6712d2006-03-14 16:24:47 -08001197 amount->not_correct = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if ((amount->io) && (amount->io < IOBRIDGE))
1199 amount->io = IOBRIDGE;
1200 if ((amount->mem) && (amount->mem < MEMBRIDGE))
1201 amount->mem = MEMBRIDGE;
1202 if ((amount->pfmem) && (amount->pfmem < MEMBRIDGE))
1203 amount->pfmem = MEMBRIDGE;
1204 return amount;
1205}
1206
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001207/* The following 3 unconfigure_boot_ routines deal with the case when we had the card
1208 * upon bootup in the system, since we don't allocate func to such case, we need to read
1209 * the start addresses from pci config space and then find the corresponding entries in
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 * our resource lists. The functions return either 0, -ENODEV, or -1 (general failure)
1211 * Change: we also call these functions even if we configured the card ourselves (i.e., not
1212 * the bootup case), since it should work same way
1213 */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001214static int unconfigure_boot_device(u8 busno, u8 device, u8 function)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
1216 u32 start_address;
1217 u32 address[] = {
1218 PCI_BASE_ADDRESS_0,
1219 PCI_BASE_ADDRESS_1,
1220 PCI_BASE_ADDRESS_2,
1221 PCI_BASE_ADDRESS_3,
1222 PCI_BASE_ADDRESS_4,
1223 PCI_BASE_ADDRESS_5,
1224 0
1225 };
1226 int count;
1227 struct resource_node *io;
1228 struct resource_node *mem;
1229 struct resource_node *pfmem;
1230 struct bus_node *bus;
1231 u32 end_address;
1232 u32 temp_end;
1233 u32 size;
1234 u32 tmp_address;
1235 unsigned int devfn;
1236
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001237 debug("%s - enter\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001239 bus = ibmphp_find_res_bus(busno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (!bus) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001241 debug("cannot find corresponding bus.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return -EINVAL;
1243 }
1244
1245 devfn = PCI_DEVFN(device, function);
1246 ibmphp_pci_bus->number = busno;
1247 for (count = 0; address[count]; count++) { /* for 6 BARs */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001248 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &start_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 /* We can do this here, b/c by that time the device driver of the card has been stopped */
1251
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001252 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1253 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &size);
1254 pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], start_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001256 debug("start_address is %x\n", start_address);
1257 debug("busno, device, function %x %x %x\n", busno, device, function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (!size) {
1259 /* This BAR is not implemented */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001260 debug("is this bar no implemented?, count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 continue;
1262 }
1263 tmp_address = start_address;
1264 if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1265 /* This is IO */
1266 start_address &= PCI_BASE_ADDRESS_IO_MASK;
1267 size = size & 0xFFFFFFFC;
1268 size = ~size + 1;
1269 end_address = start_address + size - 1;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001270 if (ibmphp_find_resource(bus, start_address, &io, IO) < 0) {
1271 err("cannot find corresponding IO resource to remove\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return -EIO;
1273 }
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001274 debug("io->start = %x\n", io->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 temp_end = io->end;
1276 start_address = io->end + 1;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001277 ibmphp_remove_resource(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 /* This is needed b/c of the old I/O restrictions in the BIOS */
1279 while (temp_end < end_address) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001280 if (ibmphp_find_resource(bus, start_address, &io, IO) < 0) {
1281 err("cannot find corresponding IO resource to remove\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return -EIO;
1283 }
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001284 debug("io->start = %x\n", io->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 temp_end = io->end;
1286 start_address = io->end + 1;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001287 ibmphp_remove_resource(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
1289
1290 /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1291 } else {
1292 /* This is Memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1294 /* pfmem */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001295 debug("start address of pfmem is %x\n", start_address);
Rolf Eike Beer034ecc72005-04-11 15:01:54 +02001296 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001298 if (ibmphp_find_resource(bus, start_address, &pfmem, PFMEM) < 0) {
1299 err("cannot find corresponding PFMEM resource to remove\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return -EIO;
1301 }
1302 if (pfmem) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001303 debug("pfmem->start = %x\n", pfmem->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 ibmphp_remove_resource(pfmem);
1306 }
1307 } else {
1308 /* regular memory */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001309 debug("start address of mem is %x\n", start_address);
Rolf Eike Beer034ecc72005-04-11 15:01:54 +02001310 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1311
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001312 if (ibmphp_find_resource(bus, start_address, &mem, MEM) < 0) {
1313 err("cannot find corresponding MEM resource to remove\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 return -EIO;
1315 }
1316 if (mem) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001317 debug("mem->start = %x\n", mem->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 ibmphp_remove_resource(mem);
1320 }
1321 }
1322 if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1323 /* takes up another dword */
1324 count += 1;
1325 }
1326 } /* end of mem */
1327 } /* end of for */
1328
1329 return 0;
1330}
1331
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001332static int unconfigure_boot_bridge(u8 busno, u8 device, u8 function)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 int count;
1335 int bus_no, pri_no, sub_no, sec_no = 0;
1336 u32 start_address, tmp_address;
1337 u8 sec_number, sub_number, pri_number;
1338 struct resource_node *io = NULL;
1339 struct resource_node *mem = NULL;
1340 struct resource_node *pfmem = NULL;
1341 struct bus_node *bus;
1342 u32 address[] = {
1343 PCI_BASE_ADDRESS_0,
1344 PCI_BASE_ADDRESS_1,
1345 0
1346 };
1347 unsigned int devfn;
1348
1349 devfn = PCI_DEVFN(device, function);
1350 ibmphp_pci_bus->number = busno;
1351 bus_no = (int) busno;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001352 debug("busno is %x\n", busno);
1353 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
1354 debug("%s - busno = %x, primary_number = %x\n", __func__, busno, pri_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001356 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
1357 debug("sec_number is %x\n", sec_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 sec_no = (int) sec_number;
1359 pri_no = (int) pri_number;
1360 if (pri_no != bus_no) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001361 err("primary numbers in our structures and pci config space don't match.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return -EINVAL;
1363 }
1364
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001365 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sub_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 sub_no = (int) sub_number;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001367 debug("sub_no is %d, sec_no is %d\n", sub_no, sec_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 if (sec_no != sub_number) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001369 err("there're more buses behind this bridge. Hot removal is not supported. Please choose another card\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return -ENODEV;
1371 }
1372
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001373 bus = ibmphp_find_res_bus(sec_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if (!bus) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001375 err("cannot find Bus structure for the bridged device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return -EINVAL;
1377 }
Adrian Bunkb0d974e2006-11-17 02:19:25 +01001378 debug("bus->busno is %x\n", bus->busno);
1379 debug("sec_number is %x\n", sec_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001381 ibmphp_remove_bus(bus, busno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 for (count = 0; address[count]; count++) {
1384 /* for 2 BARs */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001385 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &start_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 if (!start_address) {
1388 /* This BAR is not implemented */
1389 continue;
1390 }
1391
1392 tmp_address = start_address;
1393
1394 if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1395 /* This is IO */
1396 start_address &= PCI_BASE_ADDRESS_IO_MASK;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001397 if (ibmphp_find_resource(bus, start_address, &io, IO) < 0) {
1398 err("cannot find corresponding IO resource to remove\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return -EIO;
1400 }
1401 if (io)
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001402 debug("io->start = %x\n", io->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001404 ibmphp_remove_resource(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1407 } else {
1408 /* This is Memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1410 /* pfmem */
Rolf Eike Beer034ecc72005-04-11 15:01:54 +02001411 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001412 if (ibmphp_find_resource(bus, start_address, &pfmem, PFMEM) < 0) {
1413 err("cannot find corresponding PFMEM resource to remove\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return -EINVAL;
1415 }
1416 if (pfmem) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001417 debug("pfmem->start = %x\n", pfmem->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 ibmphp_remove_resource(pfmem);
1420 }
1421 } else {
1422 /* regular memory */
Rolf Eike Beer034ecc72005-04-11 15:01:54 +02001423 start_address &= PCI_BASE_ADDRESS_MEM_MASK;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001424 if (ibmphp_find_resource(bus, start_address, &mem, MEM) < 0) {
1425 err("cannot find corresponding MEM resource to remove\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return -EINVAL;
1427 }
1428 if (mem) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001429 debug("mem->start = %x\n", mem->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 ibmphp_remove_resource(mem);
1432 }
1433 }
1434 if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1435 /* takes up another dword */
1436 count += 1;
1437 }
1438 } /* end of mem */
1439 } /* end of for */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001440 debug("%s - exiting, returning success\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return 0;
1442}
1443
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001444static int unconfigure_boot_card(struct slot *slot_cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 u16 vendor_id;
1447 u32 class;
1448 u8 hdr_type;
1449 u8 device;
1450 u8 busno;
1451 u8 function;
1452 int rc;
1453 unsigned int devfn;
1454 u8 valid_device = 0x00; /* To see if we are ever able to find valid device and read it */
1455
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001456 debug("%s - enter\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 device = slot_cur->device;
1459 busno = slot_cur->bus;
1460
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001461 debug("b4 for loop, device is %x\n", device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 /* For every function on the card */
1463 for (function = 0x0; function < 0x08; function++) {
1464 devfn = PCI_DEVFN(device, function);
1465 ibmphp_pci_bus->number = busno;
1466
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001467 pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1470 /* found correct device!!! */
1471 ++valid_device;
1472
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001473 debug("%s - found correct device\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 /* header: x x x x x x x x
1476 * | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
1477 * |_=> 0 = single function device, 1 = multi-function device
1478 */
1479
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001480 pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
1481 pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001483 debug("hdr_type %x, class %x\n", hdr_type, class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 class >>= 8; /* to take revision out, class = class.subclass.prog i/f */
1485 if (class == PCI_CLASS_NOT_DEFINED_VGA) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001486 err("The device %x function %x is VGA compatible and is not supported for hot removing. Please choose another device.\n", device, function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 return -ENODEV;
1488 } else if (class == PCI_CLASS_DISPLAY_VGA) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001489 err("The device %x function %x is not supported for hot removing. Please choose another device.\n", device, function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return -ENODEV;
1491 }
1492
1493 switch (hdr_type) {
1494 case PCI_HEADER_TYPE_NORMAL:
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001495 rc = unconfigure_boot_device(busno, device, function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 if (rc) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001497 err("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 device, function, busno);
1499 return rc;
1500 }
1501 function = 0x8;
1502 break;
1503 case PCI_HEADER_TYPE_MULTIDEVICE:
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001504 rc = unconfigure_boot_device(busno, device, function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (rc) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001506 err("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 device, function, busno);
1508 return rc;
1509 }
1510 break;
1511 case PCI_HEADER_TYPE_BRIDGE:
1512 class >>= 8;
1513 if (class != PCI_CLASS_BRIDGE_PCI) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001514 err("This device %x function %x is not PCI-to-PCI bridge, and is not supported for hot-removing. Please try another card.\n", device, function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return -ENODEV;
1516 }
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001517 rc = unconfigure_boot_bridge(busno, device, function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 if (rc != 0) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001519 err("was not able to hot-remove PPB properly.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return rc;
1521 }
1522
1523 function = 0x8;
1524 break;
1525 case PCI_HEADER_TYPE_MULTIBRIDGE:
1526 class >>= 8;
1527 if (class != PCI_CLASS_BRIDGE_PCI) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001528 err("This device %x function %x is not PCI-to-PCI bridge, and is not supported for hot-removing. Please try another card.\n", device, function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return -ENODEV;
1530 }
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001531 rc = unconfigure_boot_bridge(busno, device, function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (rc != 0) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001533 err("was not able to hot-remove PPB properly.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return rc;
1535 }
1536 break;
1537 default:
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001538 err("MAJOR PROBLEM!!!! Cannot read device's header\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return -1;
1540 break;
1541 } /* end of switch */
1542 } /* end of valid device */
1543 } /* end of for */
1544
1545 if (!valid_device) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001546 err("Could not find device to unconfigure. Or could not read the card.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 return -1;
1548 }
1549 return 0;
1550}
1551
1552/*
1553 * free the resources of the card (multi, single, or bridged)
1554 * Parameters: slot, flag to say if this is for removing entire module or just
1555 * unconfiguring the device
1556 * TO DO: will probably need to add some code in case there was some resource,
1557 * to remove it... this is from when we have errors in the configure_card...
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001558 * !!!!!!!!!!!!!!!!!!!!!!!!!FOR BUSES!!!!!!!!!!!!
1559 * Returns: 0, -1, -ENODEV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001561int ibmphp_unconfigure_card(struct slot **slot_cur, int the_end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
1563 int i;
1564 int count;
1565 int rc;
1566 struct slot *sl = *slot_cur;
1567 struct pci_func *cur_func = NULL;
1568 struct pci_func *temp_func;
1569
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001570 debug("%s - enter\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 if (!the_end) {
1573 /* Need to unconfigure the card */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001574 rc = unconfigure_boot_card(sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 if ((rc == -ENODEV) || (rc == -EIO) || (rc == -EINVAL)) {
1576 /* In all other cases, will still need to get rid of func structure if it exists */
1577 return rc;
1578 }
1579 }
1580
1581 if (sl->func) {
1582 cur_func = sl->func;
1583 while (cur_func) {
1584 /* TO DO: WILL MOST LIKELY NEED TO GET RID OF THE BUS STRUCTURE FROM RESOURCES AS WELL */
1585 if (cur_func->bus) {
1586 /* in other words, it's a PPB */
1587 count = 2;
1588 } else {
1589 count = 6;
1590 }
1591
1592 for (i = 0; i < count; i++) {
1593 if (cur_func->io[i]) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001594 debug("io[%d] exists\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (the_end > 0)
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001596 ibmphp_remove_resource(cur_func->io[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 cur_func->io[i] = NULL;
1598 }
1599 if (cur_func->mem[i]) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001600 debug("mem[%d] exists\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (the_end > 0)
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001602 ibmphp_remove_resource(cur_func->mem[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 cur_func->mem[i] = NULL;
1604 }
1605 if (cur_func->pfmem[i]) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001606 debug("pfmem[%d] exists\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 if (the_end > 0)
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001608 ibmphp_remove_resource(cur_func->pfmem[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 cur_func->pfmem[i] = NULL;
1610 }
1611 }
1612
1613 temp_func = cur_func->next;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001614 kfree(cur_func);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 cur_func = temp_func;
1616 }
1617 }
1618
1619 sl->func = NULL;
1620 *slot_cur = sl;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001621 debug("%s - exit\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 return 0;
1623}
1624
1625/*
1626 * add a new bus resulting from hot-plugging a PPB bridge with devices
1627 *
1628 * Input: bus and the amount of resources needed (we know we can assign those,
1629 * since they've been checked already
1630 * Output: bus added to the correct spot
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001631 * 0, -1, error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001633static int add_new_bus(struct bus_node *bus, struct resource_node *io, struct resource_node *mem, struct resource_node *pfmem, u8 parent_busno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
1635 struct range_node *io_range = NULL;
1636 struct range_node *mem_range = NULL;
1637 struct range_node *pfmem_range = NULL;
1638 struct bus_node *cur_bus = NULL;
1639
1640 /* Trying to find the parent bus number */
1641 if (parent_busno != 0xFF) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001642 cur_bus = ibmphp_find_res_bus(parent_busno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (!cur_bus) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001644 err("strange, cannot find bus which is supposed to be at the system... something is terribly wrong...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 return -ENODEV;
1646 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001647
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001648 list_add(&bus->bus_list, &cur_bus->bus_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
1650 if (io) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +01001651 io_range = kzalloc(sizeof(*io_range), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (!io_range) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001653 err("out of system memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 return -ENOMEM;
1655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 io_range->start = io->start;
1657 io_range->end = io->end;
1658 io_range->rangeno = 1;
1659 bus->noIORanges = 1;
1660 bus->rangeIO = io_range;
1661 }
1662 if (mem) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +01001663 mem_range = kzalloc(sizeof(*mem_range), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 if (!mem_range) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001665 err("out of system memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 return -ENOMEM;
1667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 mem_range->start = mem->start;
1669 mem_range->end = mem->end;
1670 mem_range->rangeno = 1;
1671 bus->noMemRanges = 1;
1672 bus->rangeMem = mem_range;
1673 }
1674 if (pfmem) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +01001675 pfmem_range = kzalloc(sizeof(*pfmem_range), GFP_KERNEL);
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001676 if (!pfmem_range) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001677 err("out of system memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 return -ENOMEM;
1679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 pfmem_range->start = pfmem->start;
1681 pfmem_range->end = pfmem->end;
1682 pfmem_range->rangeno = 1;
1683 bus->noPFMemRanges = 1;
1684 bus->rangePFMem = pfmem_range;
1685 }
1686 return 0;
1687}
1688
1689/*
1690 * find the 1st available bus number for PPB to set as its secondary bus
1691 * Parameters: bus_number of the primary bus
1692 * Returns: bus_number of the secondary bus or 0xff in case of failure
1693 */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001694static u8 find_sec_number(u8 primary_busno, u8 slotno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695{
1696 int min, max;
1697 u8 busno;
1698 struct bus_info *bus;
1699 struct bus_node *bus_cur;
1700
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001701 bus = ibmphp_find_same_bus_num(primary_busno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 if (!bus) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001703 err("cannot get slot range of the bus from the BIOS\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return 0xff;
1705 }
1706 max = bus->slot_max;
1707 min = bus->slot_min;
1708 if ((slotno > max) || (slotno < min)) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001709 err("got the wrong range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 return 0xff;
1711 }
1712 busno = (u8) (slotno - (u8) min);
1713 busno += primary_busno + 0x01;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001714 bus_cur = ibmphp_find_res_bus(busno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 /* either there is no such bus number, or there are no ranges, which
1716 * can only happen if we removed the bridged device in previous load
1717 * of the driver, and now only have the skeleton bus struct
1718 */
1719 if ((!bus_cur) || (!(bus_cur->rangeIO) && !(bus_cur->rangeMem) && !(bus_cur->rangePFMem)))
1720 return busno;
1721 return 0xff;
1722}