Gavin Shan | 55037d1 | 2012-09-07 22:44:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * The file intends to implement PE based on the information from |
| 3 | * platforms. Basically, there have 3 types of PEs: PHB/Bus/Device. |
| 4 | * All the PEs should be organized as hierarchy tree. The first level |
| 5 | * of the tree will be associated to existing PHBs since the particular |
| 6 | * PE is only meaningful in one PHB domain. |
| 7 | * |
| 8 | * Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2012. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 | */ |
| 24 | |
| 25 | #include <linux/export.h> |
| 26 | #include <linux/gfp.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/pci.h> |
| 30 | #include <linux/string.h> |
| 31 | |
| 32 | #include <asm/pci-bridge.h> |
| 33 | #include <asm/ppc-pci.h> |
| 34 | |
| 35 | static LIST_HEAD(eeh_phb_pe); |
| 36 | |
| 37 | /** |
| 38 | * eeh_pe_alloc - Allocate PE |
| 39 | * @phb: PCI controller |
| 40 | * @type: PE type |
| 41 | * |
| 42 | * Allocate PE instance dynamically. |
| 43 | */ |
| 44 | static struct eeh_pe *eeh_pe_alloc(struct pci_controller *phb, int type) |
| 45 | { |
| 46 | struct eeh_pe *pe; |
| 47 | |
| 48 | /* Allocate PHB PE */ |
| 49 | pe = kzalloc(sizeof(struct eeh_pe), GFP_KERNEL); |
| 50 | if (!pe) return NULL; |
| 51 | |
| 52 | /* Initialize PHB PE */ |
| 53 | pe->type = type; |
| 54 | pe->phb = phb; |
| 55 | INIT_LIST_HEAD(&pe->child_list); |
| 56 | INIT_LIST_HEAD(&pe->child); |
| 57 | INIT_LIST_HEAD(&pe->edevs); |
| 58 | |
| 59 | return pe; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * eeh_phb_pe_create - Create PHB PE |
| 64 | * @phb: PCI controller |
| 65 | * |
| 66 | * The function should be called while the PHB is detected during |
| 67 | * system boot or PCI hotplug in order to create PHB PE. |
| 68 | */ |
| 69 | int __devinit eeh_phb_pe_create(struct pci_controller *phb) |
| 70 | { |
| 71 | struct eeh_pe *pe; |
| 72 | |
| 73 | /* Allocate PHB PE */ |
| 74 | pe = eeh_pe_alloc(phb, EEH_PE_PHB); |
| 75 | if (!pe) { |
| 76 | pr_err("%s: out of memory!\n", __func__); |
| 77 | return -ENOMEM; |
| 78 | } |
| 79 | |
| 80 | /* Put it into the list */ |
| 81 | eeh_lock(); |
| 82 | list_add_tail(&pe->child, &eeh_phb_pe); |
| 83 | eeh_unlock(); |
| 84 | |
| 85 | pr_debug("EEH: Add PE for PHB#%d\n", phb->global_number); |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * eeh_phb_pe_get - Retrieve PHB PE based on the given PHB |
| 92 | * @phb: PCI controller |
| 93 | * |
| 94 | * The overall PEs form hierarchy tree. The first layer of the |
| 95 | * hierarchy tree is composed of PHB PEs. The function is used |
| 96 | * to retrieve the corresponding PHB PE according to the given PHB. |
| 97 | */ |
| 98 | static struct eeh_pe *eeh_phb_pe_get(struct pci_controller *phb) |
| 99 | { |
| 100 | struct eeh_pe *pe; |
| 101 | |
Gavin Shan | 55037d1 | 2012-09-07 22:44:07 +0000 | [diff] [blame] | 102 | list_for_each_entry(pe, &eeh_phb_pe, child) { |
| 103 | /* |
| 104 | * Actually, we needn't check the type since |
| 105 | * the PE for PHB has been determined when that |
| 106 | * was created. |
| 107 | */ |
Gavin Shan | 5efc3ad | 2012-09-11 19:16:16 +0000 | [diff] [blame] | 108 | if ((pe->type & EEH_PE_PHB) && |
Gavin Shan | 55037d1 | 2012-09-07 22:44:07 +0000 | [diff] [blame] | 109 | pe->phb == phb) { |
| 110 | eeh_unlock(); |
| 111 | return pe; |
| 112 | } |
| 113 | } |
| 114 | |
Gavin Shan | 55037d1 | 2012-09-07 22:44:07 +0000 | [diff] [blame] | 115 | return NULL; |
| 116 | } |
Gavin Shan | 22f4ab1 | 2012-09-07 22:44:08 +0000 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * eeh_pe_next - Retrieve the next PE in the tree |
| 120 | * @pe: current PE |
| 121 | * @root: root PE |
| 122 | * |
| 123 | * The function is used to retrieve the next PE in the |
| 124 | * hierarchy PE tree. |
| 125 | */ |
| 126 | static struct eeh_pe *eeh_pe_next(struct eeh_pe *pe, |
| 127 | struct eeh_pe *root) |
| 128 | { |
| 129 | struct list_head *next = pe->child_list.next; |
| 130 | |
| 131 | if (next == &pe->child_list) { |
| 132 | while (1) { |
| 133 | if (pe == root) |
| 134 | return NULL; |
| 135 | next = pe->child.next; |
| 136 | if (next != &pe->parent->child_list) |
| 137 | break; |
| 138 | pe = pe->parent; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return list_entry(next, struct eeh_pe, child); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * eeh_pe_traverse - Traverse PEs in the specified PHB |
| 147 | * @root: root PE |
| 148 | * @fn: callback |
| 149 | * @flag: extra parameter to callback |
| 150 | * |
| 151 | * The function is used to traverse the specified PE and its |
| 152 | * child PEs. The traversing is to be terminated once the |
| 153 | * callback returns something other than NULL, or no more PEs |
| 154 | * to be traversed. |
| 155 | */ |
| 156 | static void *eeh_pe_traverse(struct eeh_pe *root, |
| 157 | eeh_traverse_func fn, void *flag) |
| 158 | { |
| 159 | struct eeh_pe *pe; |
| 160 | void *ret; |
| 161 | |
| 162 | for (pe = root; pe; pe = eeh_pe_next(pe, root)) { |
| 163 | ret = fn(pe, flag); |
| 164 | if (ret) return ret; |
| 165 | } |
| 166 | |
| 167 | return NULL; |
| 168 | } |
| 169 | |
| 170 | /** |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 171 | * eeh_pe_dev_traverse - Traverse the devices from the PE |
| 172 | * @root: EEH PE |
| 173 | * @fn: function callback |
| 174 | * @flag: extra parameter to callback |
| 175 | * |
| 176 | * The function is used to traverse the devices of the specified |
| 177 | * PE and its child PEs. |
| 178 | */ |
| 179 | void *eeh_pe_dev_traverse(struct eeh_pe *root, |
| 180 | eeh_traverse_func fn, void *flag) |
| 181 | { |
| 182 | struct eeh_pe *pe; |
| 183 | struct eeh_dev *edev; |
| 184 | void *ret; |
| 185 | |
| 186 | if (!root) { |
| 187 | pr_warning("%s: Invalid PE %p\n", __func__, root); |
| 188 | return NULL; |
| 189 | } |
| 190 | |
Gavin Shan | ea81245 | 2012-09-11 19:16:18 +0000 | [diff] [blame^] | 191 | eeh_lock(); |
| 192 | |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 193 | /* Traverse root PE */ |
| 194 | for (pe = root; pe; pe = eeh_pe_next(pe, root)) { |
| 195 | eeh_pe_for_each_dev(pe, edev) { |
| 196 | ret = fn(edev, flag); |
Gavin Shan | ea81245 | 2012-09-11 19:16:18 +0000 | [diff] [blame^] | 197 | if (ret) { |
| 198 | eeh_unlock(); |
| 199 | return ret; |
| 200 | } |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
Gavin Shan | ea81245 | 2012-09-11 19:16:18 +0000 | [diff] [blame^] | 204 | eeh_unlock(); |
| 205 | |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 206 | return NULL; |
| 207 | } |
| 208 | |
| 209 | /** |
Gavin Shan | 22f4ab1 | 2012-09-07 22:44:08 +0000 | [diff] [blame] | 210 | * __eeh_pe_get - Check the PE address |
| 211 | * @data: EEH PE |
| 212 | * @flag: EEH device |
| 213 | * |
| 214 | * For one particular PE, it can be identified by PE address |
| 215 | * or tranditional BDF address. BDF address is composed of |
| 216 | * Bus/Device/Function number. The extra data referred by flag |
| 217 | * indicates which type of address should be used. |
| 218 | */ |
| 219 | static void *__eeh_pe_get(void *data, void *flag) |
| 220 | { |
| 221 | struct eeh_pe *pe = (struct eeh_pe *)data; |
| 222 | struct eeh_dev *edev = (struct eeh_dev *)flag; |
| 223 | |
| 224 | /* Unexpected PHB PE */ |
Gavin Shan | 5efc3ad | 2012-09-11 19:16:16 +0000 | [diff] [blame] | 225 | if (pe->type & EEH_PE_PHB) |
Gavin Shan | 22f4ab1 | 2012-09-07 22:44:08 +0000 | [diff] [blame] | 226 | return NULL; |
| 227 | |
| 228 | /* We prefer PE address */ |
| 229 | if (edev->pe_config_addr && |
| 230 | (edev->pe_config_addr == pe->addr)) |
| 231 | return pe; |
| 232 | |
| 233 | /* Try BDF address */ |
| 234 | if (edev->pe_config_addr && |
| 235 | (edev->config_addr == pe->config_addr)) |
| 236 | return pe; |
| 237 | |
| 238 | return NULL; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * eeh_pe_get - Search PE based on the given address |
| 243 | * @edev: EEH device |
| 244 | * |
| 245 | * Search the corresponding PE based on the specified address which |
| 246 | * is included in the eeh device. The function is used to check if |
| 247 | * the associated PE has been created against the PE address. It's |
| 248 | * notable that the PE address has 2 format: traditional PE address |
| 249 | * which is composed of PCI bus/device/function number, or unified |
| 250 | * PE address. |
| 251 | */ |
| 252 | static struct eeh_pe *eeh_pe_get(struct eeh_dev *edev) |
| 253 | { |
| 254 | struct eeh_pe *root = eeh_phb_pe_get(edev->phb); |
| 255 | struct eeh_pe *pe; |
| 256 | |
Gavin Shan | 22f4ab1 | 2012-09-07 22:44:08 +0000 | [diff] [blame] | 257 | pe = eeh_pe_traverse(root, __eeh_pe_get, edev); |
Gavin Shan | 22f4ab1 | 2012-09-07 22:44:08 +0000 | [diff] [blame] | 258 | |
| 259 | return pe; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * eeh_pe_get_parent - Retrieve the parent PE |
| 264 | * @edev: EEH device |
| 265 | * |
| 266 | * The whole PEs existing in the system are organized as hierarchy |
| 267 | * tree. The function is used to retrieve the parent PE according |
| 268 | * to the parent EEH device. |
| 269 | */ |
| 270 | static struct eeh_pe *eeh_pe_get_parent(struct eeh_dev *edev) |
| 271 | { |
| 272 | struct device_node *dn; |
| 273 | struct eeh_dev *parent; |
| 274 | |
| 275 | /* |
| 276 | * It might have the case for the indirect parent |
| 277 | * EEH device already having associated PE, but |
| 278 | * the direct parent EEH device doesn't have yet. |
| 279 | */ |
| 280 | dn = edev->dn->parent; |
| 281 | while (dn) { |
| 282 | /* We're poking out of PCI territory */ |
| 283 | if (!PCI_DN(dn)) return NULL; |
| 284 | |
| 285 | parent = of_node_to_eeh_dev(dn); |
| 286 | /* We're poking out of PCI territory */ |
| 287 | if (!parent) return NULL; |
| 288 | |
| 289 | if (parent->pe) |
| 290 | return parent->pe; |
| 291 | |
| 292 | dn = dn->parent; |
| 293 | } |
| 294 | |
| 295 | return NULL; |
| 296 | } |
Gavin Shan | 9b84348 | 2012-09-07 22:44:09 +0000 | [diff] [blame] | 297 | |
| 298 | /** |
| 299 | * eeh_add_to_parent_pe - Add EEH device to parent PE |
| 300 | * @edev: EEH device |
| 301 | * |
| 302 | * Add EEH device to the parent PE. If the parent PE already |
| 303 | * exists, the PE type will be changed to EEH_PE_BUS. Otherwise, |
| 304 | * we have to create new PE to hold the EEH device and the new |
| 305 | * PE will be linked to its parent PE as well. |
| 306 | */ |
| 307 | int eeh_add_to_parent_pe(struct eeh_dev *edev) |
| 308 | { |
| 309 | struct eeh_pe *pe, *parent; |
| 310 | |
Gavin Shan | ea81245 | 2012-09-11 19:16:18 +0000 | [diff] [blame^] | 311 | eeh_lock(); |
| 312 | |
Gavin Shan | 9b84348 | 2012-09-07 22:44:09 +0000 | [diff] [blame] | 313 | /* |
| 314 | * Search the PE has been existing or not according |
| 315 | * to the PE address. If that has been existing, the |
| 316 | * PE should be composed of PCI bus and its subordinate |
| 317 | * components. |
| 318 | */ |
| 319 | pe = eeh_pe_get(edev); |
Gavin Shan | 5efc3ad | 2012-09-11 19:16:16 +0000 | [diff] [blame] | 320 | if (pe && !(pe->type & EEH_PE_INVALID)) { |
Gavin Shan | 9b84348 | 2012-09-07 22:44:09 +0000 | [diff] [blame] | 321 | if (!edev->pe_config_addr) { |
Gavin Shan | ea81245 | 2012-09-11 19:16:18 +0000 | [diff] [blame^] | 322 | eeh_unlock(); |
Gavin Shan | 9b84348 | 2012-09-07 22:44:09 +0000 | [diff] [blame] | 323 | pr_err("%s: PE with addr 0x%x already exists\n", |
| 324 | __func__, edev->config_addr); |
| 325 | return -EEXIST; |
| 326 | } |
| 327 | |
| 328 | /* Mark the PE as type of PCI bus */ |
| 329 | pe->type = EEH_PE_BUS; |
| 330 | edev->pe = pe; |
| 331 | |
| 332 | /* Put the edev to PE */ |
| 333 | list_add_tail(&edev->list, &pe->edevs); |
Gavin Shan | ea81245 | 2012-09-11 19:16:18 +0000 | [diff] [blame^] | 334 | eeh_unlock(); |
Gavin Shan | 9b84348 | 2012-09-07 22:44:09 +0000 | [diff] [blame] | 335 | pr_debug("EEH: Add %s to Bus PE#%x\n", |
| 336 | edev->dn->full_name, pe->addr); |
| 337 | |
| 338 | return 0; |
Gavin Shan | 5efc3ad | 2012-09-11 19:16:16 +0000 | [diff] [blame] | 339 | } else if (pe && (pe->type & EEH_PE_INVALID)) { |
| 340 | list_add_tail(&edev->list, &pe->edevs); |
| 341 | edev->pe = pe; |
| 342 | /* |
| 343 | * We're running to here because of PCI hotplug caused by |
| 344 | * EEH recovery. We need clear EEH_PE_INVALID until the top. |
| 345 | */ |
| 346 | parent = pe; |
| 347 | while (parent) { |
| 348 | if (!(parent->type & EEH_PE_INVALID)) |
| 349 | break; |
| 350 | parent->type &= ~EEH_PE_INVALID; |
| 351 | parent = parent->parent; |
| 352 | } |
Gavin Shan | ea81245 | 2012-09-11 19:16:18 +0000 | [diff] [blame^] | 353 | eeh_unlock(); |
Gavin Shan | 5efc3ad | 2012-09-11 19:16:16 +0000 | [diff] [blame] | 354 | pr_debug("EEH: Add %s to Device PE#%x, Parent PE#%x\n", |
| 355 | edev->dn->full_name, pe->addr, pe->parent->addr); |
| 356 | |
| 357 | return 0; |
Gavin Shan | 9b84348 | 2012-09-07 22:44:09 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /* Create a new EEH PE */ |
| 361 | pe = eeh_pe_alloc(edev->phb, EEH_PE_DEVICE); |
| 362 | if (!pe) { |
Gavin Shan | ea81245 | 2012-09-11 19:16:18 +0000 | [diff] [blame^] | 363 | eeh_unlock(); |
Gavin Shan | 9b84348 | 2012-09-07 22:44:09 +0000 | [diff] [blame] | 364 | pr_err("%s: out of memory!\n", __func__); |
| 365 | return -ENOMEM; |
| 366 | } |
| 367 | pe->addr = edev->pe_config_addr; |
| 368 | pe->config_addr = edev->config_addr; |
| 369 | |
| 370 | /* |
| 371 | * Put the new EEH PE into hierarchy tree. If the parent |
| 372 | * can't be found, the newly created PE will be attached |
| 373 | * to PHB directly. Otherwise, we have to associate the |
| 374 | * PE with its parent. |
| 375 | */ |
| 376 | parent = eeh_pe_get_parent(edev); |
| 377 | if (!parent) { |
| 378 | parent = eeh_phb_pe_get(edev->phb); |
| 379 | if (!parent) { |
Gavin Shan | ea81245 | 2012-09-11 19:16:18 +0000 | [diff] [blame^] | 380 | eeh_unlock(); |
Gavin Shan | 9b84348 | 2012-09-07 22:44:09 +0000 | [diff] [blame] | 381 | pr_err("%s: No PHB PE is found (PHB Domain=%d)\n", |
| 382 | __func__, edev->phb->global_number); |
| 383 | edev->pe = NULL; |
| 384 | kfree(pe); |
| 385 | return -EEXIST; |
| 386 | } |
| 387 | } |
| 388 | pe->parent = parent; |
| 389 | |
| 390 | /* |
| 391 | * Put the newly created PE into the child list and |
| 392 | * link the EEH device accordingly. |
| 393 | */ |
| 394 | list_add_tail(&pe->child, &parent->child_list); |
| 395 | list_add_tail(&edev->list, &pe->edevs); |
| 396 | edev->pe = pe; |
Gavin Shan | ea81245 | 2012-09-11 19:16:18 +0000 | [diff] [blame^] | 397 | eeh_unlock(); |
Gavin Shan | 9b84348 | 2012-09-07 22:44:09 +0000 | [diff] [blame] | 398 | pr_debug("EEH: Add %s to Device PE#%x, Parent PE#%x\n", |
| 399 | edev->dn->full_name, pe->addr, pe->parent->addr); |
| 400 | |
| 401 | return 0; |
| 402 | } |
Gavin Shan | 82e8882 | 2012-09-07 22:44:10 +0000 | [diff] [blame] | 403 | |
| 404 | /** |
| 405 | * eeh_rmv_from_parent_pe - Remove one EEH device from the associated PE |
| 406 | * @edev: EEH device |
Gavin Shan | 20ee6a9 | 2012-09-11 19:16:17 +0000 | [diff] [blame] | 407 | * @purge_pe: remove PE or not |
Gavin Shan | 82e8882 | 2012-09-07 22:44:10 +0000 | [diff] [blame] | 408 | * |
| 409 | * The PE hierarchy tree might be changed when doing PCI hotplug. |
| 410 | * Also, the PCI devices or buses could be removed from the system |
| 411 | * during EEH recovery. So we have to call the function remove the |
| 412 | * corresponding PE accordingly if necessary. |
| 413 | */ |
Gavin Shan | 20ee6a9 | 2012-09-11 19:16:17 +0000 | [diff] [blame] | 414 | int eeh_rmv_from_parent_pe(struct eeh_dev *edev, int purge_pe) |
Gavin Shan | 82e8882 | 2012-09-07 22:44:10 +0000 | [diff] [blame] | 415 | { |
Gavin Shan | 5efc3ad | 2012-09-11 19:16:16 +0000 | [diff] [blame] | 416 | struct eeh_pe *pe, *parent, *child; |
| 417 | int cnt; |
Gavin Shan | 82e8882 | 2012-09-07 22:44:10 +0000 | [diff] [blame] | 418 | |
| 419 | if (!edev->pe) { |
| 420 | pr_warning("%s: No PE found for EEH device %s\n", |
| 421 | __func__, edev->dn->full_name); |
| 422 | return -EEXIST; |
| 423 | } |
| 424 | |
Gavin Shan | ea81245 | 2012-09-11 19:16:18 +0000 | [diff] [blame^] | 425 | eeh_lock(); |
| 426 | |
Gavin Shan | 82e8882 | 2012-09-07 22:44:10 +0000 | [diff] [blame] | 427 | /* Remove the EEH device */ |
| 428 | pe = edev->pe; |
| 429 | edev->pe = NULL; |
| 430 | list_del(&edev->list); |
| 431 | |
| 432 | /* |
| 433 | * Check if the parent PE includes any EEH devices. |
| 434 | * If not, we should delete that. Also, we should |
| 435 | * delete the parent PE if it doesn't have associated |
| 436 | * child PEs and EEH devices. |
| 437 | */ |
| 438 | while (1) { |
| 439 | parent = pe->parent; |
Gavin Shan | 5efc3ad | 2012-09-11 19:16:16 +0000 | [diff] [blame] | 440 | if (pe->type & EEH_PE_PHB) |
Gavin Shan | 82e8882 | 2012-09-07 22:44:10 +0000 | [diff] [blame] | 441 | break; |
| 442 | |
Gavin Shan | 20ee6a9 | 2012-09-11 19:16:17 +0000 | [diff] [blame] | 443 | if (purge_pe) { |
| 444 | if (list_empty(&pe->edevs) && |
| 445 | list_empty(&pe->child_list)) { |
| 446 | list_del(&pe->child); |
| 447 | kfree(pe); |
| 448 | } else { |
Gavin Shan | 5efc3ad | 2012-09-11 19:16:16 +0000 | [diff] [blame] | 449 | break; |
Gavin Shan | 20ee6a9 | 2012-09-11 19:16:17 +0000 | [diff] [blame] | 450 | } |
| 451 | } else { |
| 452 | if (list_empty(&pe->edevs)) { |
| 453 | cnt = 0; |
| 454 | list_for_each_entry(child, &pe->child_list, child) { |
| 455 | if (!(pe->type & EEH_PE_INVALID)) { |
| 456 | cnt++; |
| 457 | break; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | if (!cnt) |
| 462 | pe->type |= EEH_PE_INVALID; |
| 463 | else |
| 464 | break; |
| 465 | } |
Gavin Shan | 82e8882 | 2012-09-07 22:44:10 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | pe = parent; |
| 469 | } |
| 470 | |
Gavin Shan | ea81245 | 2012-09-11 19:16:18 +0000 | [diff] [blame^] | 471 | eeh_unlock(); |
| 472 | |
Gavin Shan | 82e8882 | 2012-09-07 22:44:10 +0000 | [diff] [blame] | 473 | return 0; |
| 474 | } |
Gavin Shan | 5b66352 | 2012-09-07 22:44:12 +0000 | [diff] [blame] | 475 | |
| 476 | /** |
| 477 | * __eeh_pe_state_mark - Mark the state for the PE |
| 478 | * @data: EEH PE |
| 479 | * @flag: state |
| 480 | * |
| 481 | * The function is used to mark the indicated state for the given |
| 482 | * PE. Also, the associated PCI devices will be put into IO frozen |
| 483 | * state as well. |
| 484 | */ |
| 485 | static void *__eeh_pe_state_mark(void *data, void *flag) |
| 486 | { |
| 487 | struct eeh_pe *pe = (struct eeh_pe *)data; |
| 488 | int state = *((int *)flag); |
| 489 | struct eeh_dev *tmp; |
| 490 | struct pci_dev *pdev; |
| 491 | |
| 492 | /* |
| 493 | * Mark the PE with the indicated state. Also, |
| 494 | * the associated PCI device will be put into |
| 495 | * I/O frozen state to avoid I/O accesses from |
| 496 | * the PCI device driver. |
| 497 | */ |
| 498 | pe->state |= state; |
| 499 | eeh_pe_for_each_dev(pe, tmp) { |
| 500 | pdev = eeh_dev_to_pci_dev(tmp); |
| 501 | if (pdev) |
| 502 | pdev->error_state = pci_channel_io_frozen; |
| 503 | } |
| 504 | |
| 505 | return NULL; |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * eeh_pe_state_mark - Mark specified state for PE and its associated device |
| 510 | * @pe: EEH PE |
| 511 | * |
| 512 | * EEH error affects the current PE and its child PEs. The function |
| 513 | * is used to mark appropriate state for the affected PEs and the |
| 514 | * associated devices. |
| 515 | */ |
| 516 | void eeh_pe_state_mark(struct eeh_pe *pe, int state) |
| 517 | { |
Gavin Shan | ea81245 | 2012-09-11 19:16:18 +0000 | [diff] [blame^] | 518 | eeh_lock(); |
Gavin Shan | 5b66352 | 2012-09-07 22:44:12 +0000 | [diff] [blame] | 519 | eeh_pe_traverse(pe, __eeh_pe_state_mark, &state); |
Gavin Shan | ea81245 | 2012-09-11 19:16:18 +0000 | [diff] [blame^] | 520 | eeh_unlock(); |
Gavin Shan | 5b66352 | 2012-09-07 22:44:12 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /** |
| 524 | * __eeh_pe_state_clear - Clear state for the PE |
| 525 | * @data: EEH PE |
| 526 | * @flag: state |
| 527 | * |
| 528 | * The function is used to clear the indicated state from the |
| 529 | * given PE. Besides, we also clear the check count of the PE |
| 530 | * as well. |
| 531 | */ |
| 532 | static void *__eeh_pe_state_clear(void *data, void *flag) |
| 533 | { |
| 534 | struct eeh_pe *pe = (struct eeh_pe *)data; |
| 535 | int state = *((int *)flag); |
| 536 | |
| 537 | pe->state &= ~state; |
| 538 | pe->check_count = 0; |
| 539 | |
| 540 | return NULL; |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * eeh_pe_state_clear - Clear state for the PE and its children |
| 545 | * @pe: PE |
| 546 | * @state: state to be cleared |
| 547 | * |
| 548 | * When the PE and its children has been recovered from error, |
| 549 | * we need clear the error state for that. The function is used |
| 550 | * for the purpose. |
| 551 | */ |
| 552 | void eeh_pe_state_clear(struct eeh_pe *pe, int state) |
| 553 | { |
Gavin Shan | ea81245 | 2012-09-11 19:16:18 +0000 | [diff] [blame^] | 554 | eeh_lock(); |
Gavin Shan | 5b66352 | 2012-09-07 22:44:12 +0000 | [diff] [blame] | 555 | eeh_pe_traverse(pe, __eeh_pe_state_clear, &state); |
Gavin Shan | ea81245 | 2012-09-11 19:16:18 +0000 | [diff] [blame^] | 556 | eeh_unlock(); |
Gavin Shan | 5b66352 | 2012-09-07 22:44:12 +0000 | [diff] [blame] | 557 | } |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 558 | |
| 559 | /** |
| 560 | * eeh_restore_one_device_bars - Restore the Base Address Registers for one device |
| 561 | * @data: EEH device |
| 562 | * @flag: Unused |
| 563 | * |
| 564 | * Loads the PCI configuration space base address registers, |
| 565 | * the expansion ROM base address, the latency timer, and etc. |
| 566 | * from the saved values in the device node. |
| 567 | */ |
| 568 | static void *eeh_restore_one_device_bars(void *data, void *flag) |
| 569 | { |
| 570 | int i; |
| 571 | u32 cmd; |
| 572 | struct eeh_dev *edev = (struct eeh_dev *)data; |
| 573 | struct device_node *dn = eeh_dev_to_of_node(edev); |
| 574 | |
| 575 | for (i = 4; i < 10; i++) |
| 576 | eeh_ops->write_config(dn, i*4, 4, edev->config_space[i]); |
| 577 | /* 12 == Expansion ROM Address */ |
| 578 | eeh_ops->write_config(dn, 12*4, 4, edev->config_space[12]); |
| 579 | |
| 580 | #define BYTE_SWAP(OFF) (8*((OFF)/4)+3-(OFF)) |
| 581 | #define SAVED_BYTE(OFF) (((u8 *)(edev->config_space))[BYTE_SWAP(OFF)]) |
| 582 | |
| 583 | eeh_ops->write_config(dn, PCI_CACHE_LINE_SIZE, 1, |
| 584 | SAVED_BYTE(PCI_CACHE_LINE_SIZE)); |
| 585 | eeh_ops->write_config(dn, PCI_LATENCY_TIMER, 1, |
| 586 | SAVED_BYTE(PCI_LATENCY_TIMER)); |
| 587 | |
| 588 | /* max latency, min grant, interrupt pin and line */ |
| 589 | eeh_ops->write_config(dn, 15*4, 4, edev->config_space[15]); |
| 590 | |
| 591 | /* |
| 592 | * Restore PERR & SERR bits, some devices require it, |
| 593 | * don't touch the other command bits |
| 594 | */ |
| 595 | eeh_ops->read_config(dn, PCI_COMMAND, 4, &cmd); |
| 596 | if (edev->config_space[1] & PCI_COMMAND_PARITY) |
| 597 | cmd |= PCI_COMMAND_PARITY; |
| 598 | else |
| 599 | cmd &= ~PCI_COMMAND_PARITY; |
| 600 | if (edev->config_space[1] & PCI_COMMAND_SERR) |
| 601 | cmd |= PCI_COMMAND_SERR; |
| 602 | else |
| 603 | cmd &= ~PCI_COMMAND_SERR; |
| 604 | eeh_ops->write_config(dn, PCI_COMMAND, 4, cmd); |
| 605 | |
| 606 | return NULL; |
| 607 | } |
| 608 | |
| 609 | /** |
| 610 | * eeh_pe_restore_bars - Restore the PCI config space info |
| 611 | * @pe: EEH PE |
| 612 | * |
| 613 | * This routine performs a recursive walk to the children |
| 614 | * of this device as well. |
| 615 | */ |
| 616 | void eeh_pe_restore_bars(struct eeh_pe *pe) |
| 617 | { |
Gavin Shan | ea81245 | 2012-09-11 19:16:18 +0000 | [diff] [blame^] | 618 | /* |
| 619 | * We needn't take the EEH lock since eeh_pe_dev_traverse() |
| 620 | * will take that. |
| 621 | */ |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 622 | eeh_pe_dev_traverse(pe, eeh_restore_one_device_bars, NULL); |
| 623 | } |
Gavin Shan | 9b3c76f | 2012-09-07 22:44:19 +0000 | [diff] [blame] | 624 | |
| 625 | /** |
| 626 | * eeh_pe_bus_get - Retrieve PCI bus according to the given PE |
| 627 | * @pe: EEH PE |
| 628 | * |
| 629 | * Retrieve the PCI bus according to the given PE. Basically, |
| 630 | * there're 3 types of PEs: PHB/Bus/Device. For PHB PE, the |
| 631 | * primary PCI bus will be retrieved. The parent bus will be |
| 632 | * returned for BUS PE. However, we don't have associated PCI |
| 633 | * bus for DEVICE PE. |
| 634 | */ |
| 635 | struct pci_bus *eeh_pe_bus_get(struct eeh_pe *pe) |
| 636 | { |
| 637 | struct pci_bus *bus = NULL; |
| 638 | struct eeh_dev *edev; |
| 639 | struct pci_dev *pdev; |
| 640 | |
Gavin Shan | ea81245 | 2012-09-11 19:16:18 +0000 | [diff] [blame^] | 641 | eeh_lock(); |
| 642 | |
Gavin Shan | 5efc3ad | 2012-09-11 19:16:16 +0000 | [diff] [blame] | 643 | if (pe->type & EEH_PE_PHB) { |
Gavin Shan | 9b3c76f | 2012-09-07 22:44:19 +0000 | [diff] [blame] | 644 | bus = pe->phb->bus; |
Gavin Shan | 5efc3ad | 2012-09-11 19:16:16 +0000 | [diff] [blame] | 645 | } else if (pe->type & EEH_PE_BUS) { |
Gavin Shan | 9b3c76f | 2012-09-07 22:44:19 +0000 | [diff] [blame] | 646 | edev = list_first_entry(&pe->edevs, struct eeh_dev, list); |
| 647 | pdev = eeh_dev_to_pci_dev(edev); |
| 648 | if (pdev) |
| 649 | bus = pdev->bus; |
| 650 | } |
| 651 | |
Gavin Shan | ea81245 | 2012-09-11 19:16:18 +0000 | [diff] [blame^] | 652 | eeh_unlock(); |
| 653 | |
Gavin Shan | 9b3c76f | 2012-09-07 22:44:19 +0000 | [diff] [blame] | 654 | return bus; |
| 655 | } |