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 | |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 25 | #include <linux/delay.h> |
Gavin Shan | 55037d1 | 2012-09-07 22:44:07 +0000 | [diff] [blame] | 26 | #include <linux/export.h> |
| 27 | #include <linux/gfp.h> |
Gavin Shan | 55037d1 | 2012-09-07 22:44:07 +0000 | [diff] [blame] | 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 | |
Gavin Shan | bb593c0 | 2014-07-17 14:41:43 +1000 | [diff] [blame] | 35 | static int eeh_pe_aux_size = 0; |
Gavin Shan | 55037d1 | 2012-09-07 22:44:07 +0000 | [diff] [blame] | 36 | static LIST_HEAD(eeh_phb_pe); |
| 37 | |
| 38 | /** |
Gavin Shan | bb593c0 | 2014-07-17 14:41:43 +1000 | [diff] [blame] | 39 | * eeh_set_pe_aux_size - Set PE auxillary data size |
| 40 | * @size: PE auxillary data size |
| 41 | * |
| 42 | * Set PE auxillary data size |
| 43 | */ |
| 44 | void eeh_set_pe_aux_size(int size) |
| 45 | { |
| 46 | if (size < 0) |
| 47 | return; |
| 48 | |
| 49 | eeh_pe_aux_size = size; |
| 50 | } |
| 51 | |
| 52 | /** |
Gavin Shan | 55037d1 | 2012-09-07 22:44:07 +0000 | [diff] [blame] | 53 | * eeh_pe_alloc - Allocate PE |
| 54 | * @phb: PCI controller |
| 55 | * @type: PE type |
| 56 | * |
| 57 | * Allocate PE instance dynamically. |
| 58 | */ |
| 59 | static struct eeh_pe *eeh_pe_alloc(struct pci_controller *phb, int type) |
| 60 | { |
| 61 | struct eeh_pe *pe; |
Gavin Shan | bb593c0 | 2014-07-17 14:41:43 +1000 | [diff] [blame] | 62 | size_t alloc_size; |
| 63 | |
| 64 | alloc_size = sizeof(struct eeh_pe); |
| 65 | if (eeh_pe_aux_size) { |
| 66 | alloc_size = ALIGN(alloc_size, cache_line_size()); |
| 67 | alloc_size += eeh_pe_aux_size; |
| 68 | } |
Gavin Shan | 55037d1 | 2012-09-07 22:44:07 +0000 | [diff] [blame] | 69 | |
| 70 | /* Allocate PHB PE */ |
Gavin Shan | bb593c0 | 2014-07-17 14:41:43 +1000 | [diff] [blame] | 71 | pe = kzalloc(alloc_size, GFP_KERNEL); |
Gavin Shan | 55037d1 | 2012-09-07 22:44:07 +0000 | [diff] [blame] | 72 | if (!pe) return NULL; |
| 73 | |
| 74 | /* Initialize PHB PE */ |
| 75 | pe->type = type; |
| 76 | pe->phb = phb; |
| 77 | INIT_LIST_HEAD(&pe->child_list); |
Gavin Shan | 55037d1 | 2012-09-07 22:44:07 +0000 | [diff] [blame] | 78 | INIT_LIST_HEAD(&pe->edevs); |
| 79 | |
Gavin Shan | bb593c0 | 2014-07-17 14:41:43 +1000 | [diff] [blame] | 80 | pe->data = (void *)pe + ALIGN(sizeof(struct eeh_pe), |
| 81 | cache_line_size()); |
Gavin Shan | 55037d1 | 2012-09-07 22:44:07 +0000 | [diff] [blame] | 82 | return pe; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * eeh_phb_pe_create - Create PHB PE |
| 87 | * @phb: PCI controller |
| 88 | * |
| 89 | * The function should be called while the PHB is detected during |
| 90 | * system boot or PCI hotplug in order to create PHB PE. |
| 91 | */ |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 92 | int eeh_phb_pe_create(struct pci_controller *phb) |
Gavin Shan | 55037d1 | 2012-09-07 22:44:07 +0000 | [diff] [blame] | 93 | { |
| 94 | struct eeh_pe *pe; |
| 95 | |
| 96 | /* Allocate PHB PE */ |
| 97 | pe = eeh_pe_alloc(phb, EEH_PE_PHB); |
| 98 | if (!pe) { |
| 99 | pr_err("%s: out of memory!\n", __func__); |
| 100 | return -ENOMEM; |
| 101 | } |
| 102 | |
| 103 | /* Put it into the list */ |
Gavin Shan | 55037d1 | 2012-09-07 22:44:07 +0000 | [diff] [blame] | 104 | list_add_tail(&pe->child, &eeh_phb_pe); |
Gavin Shan | 55037d1 | 2012-09-07 22:44:07 +0000 | [diff] [blame] | 105 | |
Russell Currey | 1f52f17 | 2016-11-16 14:02:15 +1100 | [diff] [blame] | 106 | pr_debug("EEH: Add PE for PHB#%x\n", phb->global_number); |
Gavin Shan | 55037d1 | 2012-09-07 22:44:07 +0000 | [diff] [blame] | 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * eeh_phb_pe_get - Retrieve PHB PE based on the given PHB |
| 113 | * @phb: PCI controller |
| 114 | * |
| 115 | * The overall PEs form hierarchy tree. The first layer of the |
| 116 | * hierarchy tree is composed of PHB PEs. The function is used |
| 117 | * to retrieve the corresponding PHB PE according to the given PHB. |
| 118 | */ |
Gavin Shan | 9ff6743 | 2013-06-20 13:20:53 +0800 | [diff] [blame] | 119 | struct eeh_pe *eeh_phb_pe_get(struct pci_controller *phb) |
Gavin Shan | 55037d1 | 2012-09-07 22:44:07 +0000 | [diff] [blame] | 120 | { |
| 121 | struct eeh_pe *pe; |
| 122 | |
Gavin Shan | 55037d1 | 2012-09-07 22:44:07 +0000 | [diff] [blame] | 123 | list_for_each_entry(pe, &eeh_phb_pe, child) { |
| 124 | /* |
| 125 | * Actually, we needn't check the type since |
| 126 | * the PE for PHB has been determined when that |
| 127 | * was created. |
| 128 | */ |
Aneesh Kumar K.V | 7844663 | 2012-09-20 23:29:46 +0000 | [diff] [blame] | 129 | if ((pe->type & EEH_PE_PHB) && pe->phb == phb) |
Gavin Shan | 55037d1 | 2012-09-07 22:44:07 +0000 | [diff] [blame] | 130 | return pe; |
Gavin Shan | 55037d1 | 2012-09-07 22:44:07 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Gavin Shan | 55037d1 | 2012-09-07 22:44:07 +0000 | [diff] [blame] | 133 | return NULL; |
| 134 | } |
Gavin Shan | 22f4ab1 | 2012-09-07 22:44:08 +0000 | [diff] [blame] | 135 | |
| 136 | /** |
| 137 | * eeh_pe_next - Retrieve the next PE in the tree |
| 138 | * @pe: current PE |
| 139 | * @root: root PE |
| 140 | * |
| 141 | * The function is used to retrieve the next PE in the |
| 142 | * hierarchy PE tree. |
| 143 | */ |
Sam Bobroff | 309ed3a | 2018-05-25 13:11:35 +1000 | [diff] [blame] | 144 | struct eeh_pe *eeh_pe_next(struct eeh_pe *pe, struct eeh_pe *root) |
Gavin Shan | 22f4ab1 | 2012-09-07 22:44:08 +0000 | [diff] [blame] | 145 | { |
| 146 | struct list_head *next = pe->child_list.next; |
| 147 | |
| 148 | if (next == &pe->child_list) { |
| 149 | while (1) { |
| 150 | if (pe == root) |
| 151 | return NULL; |
| 152 | next = pe->child.next; |
| 153 | if (next != &pe->parent->child_list) |
| 154 | break; |
| 155 | pe = pe->parent; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return list_entry(next, struct eeh_pe, child); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * eeh_pe_traverse - Traverse PEs in the specified PHB |
| 164 | * @root: root PE |
| 165 | * @fn: callback |
| 166 | * @flag: extra parameter to callback |
| 167 | * |
| 168 | * The function is used to traverse the specified PE and its |
| 169 | * child PEs. The traversing is to be terminated once the |
| 170 | * callback returns something other than NULL, or no more PEs |
| 171 | * to be traversed. |
| 172 | */ |
Gavin Shan | f5c5771 | 2013-07-24 10:24:58 +0800 | [diff] [blame] | 173 | void *eeh_pe_traverse(struct eeh_pe *root, |
Sam Bobroff | d6c4932 | 2018-05-25 13:11:32 +1000 | [diff] [blame] | 174 | eeh_pe_traverse_func fn, void *flag) |
Gavin Shan | 22f4ab1 | 2012-09-07 22:44:08 +0000 | [diff] [blame] | 175 | { |
| 176 | struct eeh_pe *pe; |
| 177 | void *ret; |
| 178 | |
Sam Bobroff | 309ed3a | 2018-05-25 13:11:35 +1000 | [diff] [blame] | 179 | eeh_for_each_pe(root, pe) { |
Gavin Shan | 22f4ab1 | 2012-09-07 22:44:08 +0000 | [diff] [blame] | 180 | ret = fn(pe, flag); |
| 181 | if (ret) return ret; |
| 182 | } |
| 183 | |
| 184 | return NULL; |
| 185 | } |
| 186 | |
| 187 | /** |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 188 | * eeh_pe_dev_traverse - Traverse the devices from the PE |
| 189 | * @root: EEH PE |
| 190 | * @fn: function callback |
| 191 | * @flag: extra parameter to callback |
| 192 | * |
| 193 | * The function is used to traverse the devices of the specified |
| 194 | * PE and its child PEs. |
| 195 | */ |
| 196 | void *eeh_pe_dev_traverse(struct eeh_pe *root, |
Sam Bobroff | d6c4932 | 2018-05-25 13:11:32 +1000 | [diff] [blame] | 197 | eeh_edev_traverse_func fn, void *flag) |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 198 | { |
| 199 | struct eeh_pe *pe; |
Gavin Shan | 9feed42 | 2013-07-24 10:24:56 +0800 | [diff] [blame] | 200 | struct eeh_dev *edev, *tmp; |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 201 | void *ret; |
| 202 | |
| 203 | if (!root) { |
Gavin Shan | 0dae274 | 2014-07-17 14:41:41 +1000 | [diff] [blame] | 204 | pr_warn("%s: Invalid PE %p\n", |
| 205 | __func__, root); |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 206 | return NULL; |
| 207 | } |
| 208 | |
| 209 | /* Traverse root PE */ |
Sam Bobroff | 309ed3a | 2018-05-25 13:11:35 +1000 | [diff] [blame] | 210 | eeh_for_each_pe(root, pe) { |
Gavin Shan | 9feed42 | 2013-07-24 10:24:56 +0800 | [diff] [blame] | 211 | eeh_pe_for_each_dev(pe, edev, tmp) { |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 212 | ret = fn(edev, flag); |
Gavin Shan | ef6a285 | 2013-06-25 14:35:27 +0800 | [diff] [blame] | 213 | if (ret) |
Gavin Shan | ea81245 | 2012-09-11 19:16:18 +0000 | [diff] [blame] | 214 | return ret; |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
| 218 | return NULL; |
| 219 | } |
| 220 | |
| 221 | /** |
Gavin Shan | 22f4ab1 | 2012-09-07 22:44:08 +0000 | [diff] [blame] | 222 | * __eeh_pe_get - Check the PE address |
| 223 | * @data: EEH PE |
| 224 | * @flag: EEH device |
| 225 | * |
| 226 | * For one particular PE, it can be identified by PE address |
| 227 | * or tranditional BDF address. BDF address is composed of |
| 228 | * Bus/Device/Function number. The extra data referred by flag |
| 229 | * indicates which type of address should be used. |
| 230 | */ |
Alexey Kardashevskiy | 8bae6a2 | 2017-08-29 17:34:00 +1000 | [diff] [blame] | 231 | struct eeh_pe_get_flag { |
| 232 | int pe_no; |
| 233 | int config_addr; |
| 234 | }; |
| 235 | |
Sam Bobroff | d6c4932 | 2018-05-25 13:11:32 +1000 | [diff] [blame] | 236 | static void *__eeh_pe_get(struct eeh_pe *pe, void *flag) |
Gavin Shan | 22f4ab1 | 2012-09-07 22:44:08 +0000 | [diff] [blame] | 237 | { |
Alexey Kardashevskiy | 8bae6a2 | 2017-08-29 17:34:00 +1000 | [diff] [blame] | 238 | struct eeh_pe_get_flag *tmp = (struct eeh_pe_get_flag *) flag; |
Gavin Shan | 22f4ab1 | 2012-09-07 22:44:08 +0000 | [diff] [blame] | 239 | |
| 240 | /* Unexpected PHB PE */ |
Gavin Shan | 5efc3ad | 2012-09-11 19:16:16 +0000 | [diff] [blame] | 241 | if (pe->type & EEH_PE_PHB) |
Gavin Shan | 22f4ab1 | 2012-09-07 22:44:08 +0000 | [diff] [blame] | 242 | return NULL; |
| 243 | |
Gavin Shan | 2aa5cf9 | 2014-11-25 09:27:00 +1100 | [diff] [blame] | 244 | /* |
| 245 | * We prefer PE address. For most cases, we should |
| 246 | * have non-zero PE address |
| 247 | */ |
| 248 | if (eeh_has_flag(EEH_VALID_PE_ZERO)) { |
Alexey Kardashevskiy | 8bae6a2 | 2017-08-29 17:34:00 +1000 | [diff] [blame] | 249 | if (tmp->pe_no == pe->addr) |
Gavin Shan | 2aa5cf9 | 2014-11-25 09:27:00 +1100 | [diff] [blame] | 250 | return pe; |
| 251 | } else { |
Alexey Kardashevskiy | 8bae6a2 | 2017-08-29 17:34:00 +1000 | [diff] [blame] | 252 | if (tmp->pe_no && |
| 253 | (tmp->pe_no == pe->addr)) |
Andrew Donnellan | 2d52178 | 2016-04-26 15:02:50 +1000 | [diff] [blame] | 254 | return pe; |
Gavin Shan | 2aa5cf9 | 2014-11-25 09:27:00 +1100 | [diff] [blame] | 255 | } |
Gavin Shan | 22f4ab1 | 2012-09-07 22:44:08 +0000 | [diff] [blame] | 256 | |
| 257 | /* Try BDF address */ |
Alexey Kardashevskiy | 8bae6a2 | 2017-08-29 17:34:00 +1000 | [diff] [blame] | 258 | if (tmp->config_addr && |
| 259 | (tmp->config_addr == pe->config_addr)) |
Gavin Shan | 22f4ab1 | 2012-09-07 22:44:08 +0000 | [diff] [blame] | 260 | return pe; |
| 261 | |
| 262 | return NULL; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * eeh_pe_get - Search PE based on the given address |
Alexey Kardashevskiy | 8bae6a2 | 2017-08-29 17:34:00 +1000 | [diff] [blame] | 267 | * @phb: PCI controller |
| 268 | * @pe_no: PE number |
| 269 | * @config_addr: Config address |
Gavin Shan | 22f4ab1 | 2012-09-07 22:44:08 +0000 | [diff] [blame] | 270 | * |
| 271 | * Search the corresponding PE based on the specified address which |
| 272 | * is included in the eeh device. The function is used to check if |
| 273 | * the associated PE has been created against the PE address. It's |
| 274 | * notable that the PE address has 2 format: traditional PE address |
| 275 | * which is composed of PCI bus/device/function number, or unified |
| 276 | * PE address. |
| 277 | */ |
Alexey Kardashevskiy | 8bae6a2 | 2017-08-29 17:34:00 +1000 | [diff] [blame] | 278 | struct eeh_pe *eeh_pe_get(struct pci_controller *phb, |
| 279 | int pe_no, int config_addr) |
Gavin Shan | 22f4ab1 | 2012-09-07 22:44:08 +0000 | [diff] [blame] | 280 | { |
Alexey Kardashevskiy | 8bae6a2 | 2017-08-29 17:34:00 +1000 | [diff] [blame] | 281 | struct eeh_pe *root = eeh_phb_pe_get(phb); |
| 282 | struct eeh_pe_get_flag tmp = { pe_no, config_addr }; |
Gavin Shan | 22f4ab1 | 2012-09-07 22:44:08 +0000 | [diff] [blame] | 283 | struct eeh_pe *pe; |
| 284 | |
Alexey Kardashevskiy | 8bae6a2 | 2017-08-29 17:34:00 +1000 | [diff] [blame] | 285 | pe = eeh_pe_traverse(root, __eeh_pe_get, &tmp); |
Gavin Shan | 22f4ab1 | 2012-09-07 22:44:08 +0000 | [diff] [blame] | 286 | |
| 287 | return pe; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * eeh_pe_get_parent - Retrieve the parent PE |
| 292 | * @edev: EEH device |
| 293 | * |
| 294 | * The whole PEs existing in the system are organized as hierarchy |
| 295 | * tree. The function is used to retrieve the parent PE according |
| 296 | * to the parent EEH device. |
| 297 | */ |
| 298 | static struct eeh_pe *eeh_pe_get_parent(struct eeh_dev *edev) |
| 299 | { |
Gavin Shan | 22f4ab1 | 2012-09-07 22:44:08 +0000 | [diff] [blame] | 300 | struct eeh_dev *parent; |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 301 | struct pci_dn *pdn = eeh_dev_to_pdn(edev); |
Gavin Shan | 22f4ab1 | 2012-09-07 22:44:08 +0000 | [diff] [blame] | 302 | |
| 303 | /* |
| 304 | * It might have the case for the indirect parent |
| 305 | * EEH device already having associated PE, but |
| 306 | * the direct parent EEH device doesn't have yet. |
| 307 | */ |
Wei Yang | c29fa27 | 2016-03-04 10:53:08 +1100 | [diff] [blame] | 308 | if (edev->physfn) |
| 309 | pdn = pci_get_pdn(edev->physfn); |
| 310 | else |
| 311 | pdn = pdn ? pdn->parent : NULL; |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 312 | while (pdn) { |
Gavin Shan | 22f4ab1 | 2012-09-07 22:44:08 +0000 | [diff] [blame] | 313 | /* We're poking out of PCI territory */ |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 314 | parent = pdn_to_eeh_dev(pdn); |
| 315 | if (!parent) |
| 316 | return NULL; |
Gavin Shan | 22f4ab1 | 2012-09-07 22:44:08 +0000 | [diff] [blame] | 317 | |
| 318 | if (parent->pe) |
| 319 | return parent->pe; |
| 320 | |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 321 | pdn = pdn->parent; |
Gavin Shan | 22f4ab1 | 2012-09-07 22:44:08 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | return NULL; |
| 325 | } |
Gavin Shan | 9b84348 | 2012-09-07 22:44:09 +0000 | [diff] [blame] | 326 | |
| 327 | /** |
| 328 | * eeh_add_to_parent_pe - Add EEH device to parent PE |
| 329 | * @edev: EEH device |
| 330 | * |
| 331 | * Add EEH device to the parent PE. If the parent PE already |
| 332 | * exists, the PE type will be changed to EEH_PE_BUS. Otherwise, |
| 333 | * we have to create new PE to hold the EEH device and the new |
| 334 | * PE will be linked to its parent PE as well. |
| 335 | */ |
| 336 | int eeh_add_to_parent_pe(struct eeh_dev *edev) |
| 337 | { |
| 338 | struct eeh_pe *pe, *parent; |
Alexey Kardashevskiy | 69672bd | 2017-08-29 17:34:01 +1000 | [diff] [blame] | 339 | struct pci_dn *pdn = eeh_dev_to_pdn(edev); |
Alexey Kardashevskiy | 405b33a | 2017-08-29 17:34:02 +1000 | [diff] [blame] | 340 | int config_addr = (pdn->busno << 8) | (pdn->devfn); |
Gavin Shan | 9b84348 | 2012-09-07 22:44:09 +0000 | [diff] [blame] | 341 | |
Gavin Shan | 433185d | 2015-03-27 11:22:17 +1100 | [diff] [blame] | 342 | /* Check if the PE number is valid */ |
| 343 | if (!eeh_has_flag(EEH_VALID_PE_ZERO) && !edev->pe_config_addr) { |
Russell Currey | 1f52f17 | 2016-11-16 14:02:15 +1100 | [diff] [blame] | 344 | pr_err("%s: Invalid PE#0 for edev 0x%x on PHB#%x\n", |
Alexey Kardashevskiy | 405b33a | 2017-08-29 17:34:02 +1000 | [diff] [blame] | 345 | __func__, config_addr, pdn->phb->global_number); |
Gavin Shan | 433185d | 2015-03-27 11:22:17 +1100 | [diff] [blame] | 346 | return -EINVAL; |
| 347 | } |
| 348 | |
Gavin Shan | 9b84348 | 2012-09-07 22:44:09 +0000 | [diff] [blame] | 349 | /* |
| 350 | * Search the PE has been existing or not according |
| 351 | * to the PE address. If that has been existing, the |
| 352 | * PE should be composed of PCI bus and its subordinate |
| 353 | * components. |
| 354 | */ |
Alexey Kardashevskiy | 405b33a | 2017-08-29 17:34:02 +1000 | [diff] [blame] | 355 | pe = eeh_pe_get(pdn->phb, edev->pe_config_addr, config_addr); |
Gavin Shan | 5efc3ad | 2012-09-11 19:16:16 +0000 | [diff] [blame] | 356 | if (pe && !(pe->type & EEH_PE_INVALID)) { |
Gavin Shan | 9b84348 | 2012-09-07 22:44:09 +0000 | [diff] [blame] | 357 | /* Mark the PE as type of PCI bus */ |
| 358 | pe->type = EEH_PE_BUS; |
| 359 | edev->pe = pe; |
| 360 | |
| 361 | /* Put the edev to PE */ |
Sam Bobroff | 80e65b0 | 2018-09-12 11:23:26 +1000 | [diff] [blame] | 362 | list_add_tail(&edev->entry, &pe->edevs); |
Gavin Shan | c6406d8 | 2015-03-17 16:15:08 +1100 | [diff] [blame] | 363 | pr_debug("EEH: Add %04x:%02x:%02x.%01x to Bus PE#%x\n", |
Alexey Kardashevskiy | 69672bd | 2017-08-29 17:34:01 +1000 | [diff] [blame] | 364 | pdn->phb->global_number, |
Alexey Kardashevskiy | 405b33a | 2017-08-29 17:34:02 +1000 | [diff] [blame] | 365 | pdn->busno, |
| 366 | PCI_SLOT(pdn->devfn), |
| 367 | PCI_FUNC(pdn->devfn), |
| 368 | pe->addr); |
Gavin Shan | 9b84348 | 2012-09-07 22:44:09 +0000 | [diff] [blame] | 369 | return 0; |
Gavin Shan | 5efc3ad | 2012-09-11 19:16:16 +0000 | [diff] [blame] | 370 | } else if (pe && (pe->type & EEH_PE_INVALID)) { |
Sam Bobroff | 80e65b0 | 2018-09-12 11:23:26 +1000 | [diff] [blame] | 371 | list_add_tail(&edev->entry, &pe->edevs); |
Gavin Shan | 5efc3ad | 2012-09-11 19:16:16 +0000 | [diff] [blame] | 372 | edev->pe = pe; |
| 373 | /* |
| 374 | * We're running to here because of PCI hotplug caused by |
| 375 | * EEH recovery. We need clear EEH_PE_INVALID until the top. |
| 376 | */ |
| 377 | parent = pe; |
| 378 | while (parent) { |
| 379 | if (!(parent->type & EEH_PE_INVALID)) |
| 380 | break; |
Sam Bobroff | 473af09 | 2018-09-12 11:23:22 +1000 | [diff] [blame] | 381 | parent->type &= ~EEH_PE_INVALID; |
Gavin Shan | 5efc3ad | 2012-09-11 19:16:16 +0000 | [diff] [blame] | 382 | parent = parent->parent; |
| 383 | } |
Gavin Shan | 5efc3ad | 2012-09-11 19:16:16 +0000 | [diff] [blame] | 384 | |
Gavin Shan | c6406d8 | 2015-03-17 16:15:08 +1100 | [diff] [blame] | 385 | pr_debug("EEH: Add %04x:%02x:%02x.%01x to Device " |
| 386 | "PE#%x, Parent PE#%x\n", |
Alexey Kardashevskiy | 69672bd | 2017-08-29 17:34:01 +1000 | [diff] [blame] | 387 | pdn->phb->global_number, |
Alexey Kardashevskiy | 405b33a | 2017-08-29 17:34:02 +1000 | [diff] [blame] | 388 | pdn->busno, |
| 389 | PCI_SLOT(pdn->devfn), |
| 390 | PCI_FUNC(pdn->devfn), |
| 391 | pe->addr, pe->parent->addr); |
Gavin Shan | 5efc3ad | 2012-09-11 19:16:16 +0000 | [diff] [blame] | 392 | return 0; |
Gavin Shan | 9b84348 | 2012-09-07 22:44:09 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /* Create a new EEH PE */ |
Wei Yang | c29fa27 | 2016-03-04 10:53:08 +1100 | [diff] [blame] | 396 | if (edev->physfn) |
Alexey Kardashevskiy | 69672bd | 2017-08-29 17:34:01 +1000 | [diff] [blame] | 397 | pe = eeh_pe_alloc(pdn->phb, EEH_PE_VF); |
Wei Yang | c29fa27 | 2016-03-04 10:53:08 +1100 | [diff] [blame] | 398 | else |
Alexey Kardashevskiy | 69672bd | 2017-08-29 17:34:01 +1000 | [diff] [blame] | 399 | pe = eeh_pe_alloc(pdn->phb, EEH_PE_DEVICE); |
Gavin Shan | 9b84348 | 2012-09-07 22:44:09 +0000 | [diff] [blame] | 400 | if (!pe) { |
| 401 | pr_err("%s: out of memory!\n", __func__); |
| 402 | return -ENOMEM; |
| 403 | } |
| 404 | pe->addr = edev->pe_config_addr; |
Alexey Kardashevskiy | 405b33a | 2017-08-29 17:34:02 +1000 | [diff] [blame] | 405 | pe->config_addr = config_addr; |
Gavin Shan | 9b84348 | 2012-09-07 22:44:09 +0000 | [diff] [blame] | 406 | |
| 407 | /* |
| 408 | * Put the new EEH PE into hierarchy tree. If the parent |
| 409 | * can't be found, the newly created PE will be attached |
| 410 | * to PHB directly. Otherwise, we have to associate the |
| 411 | * PE with its parent. |
| 412 | */ |
| 413 | parent = eeh_pe_get_parent(edev); |
| 414 | if (!parent) { |
Alexey Kardashevskiy | 69672bd | 2017-08-29 17:34:01 +1000 | [diff] [blame] | 415 | parent = eeh_phb_pe_get(pdn->phb); |
Gavin Shan | 9b84348 | 2012-09-07 22:44:09 +0000 | [diff] [blame] | 416 | if (!parent) { |
| 417 | pr_err("%s: No PHB PE is found (PHB Domain=%d)\n", |
Alexey Kardashevskiy | 69672bd | 2017-08-29 17:34:01 +1000 | [diff] [blame] | 418 | __func__, pdn->phb->global_number); |
Gavin Shan | 9b84348 | 2012-09-07 22:44:09 +0000 | [diff] [blame] | 419 | edev->pe = NULL; |
| 420 | kfree(pe); |
| 421 | return -EEXIST; |
| 422 | } |
| 423 | } |
| 424 | pe->parent = parent; |
| 425 | |
| 426 | /* |
| 427 | * Put the newly created PE into the child list and |
| 428 | * link the EEH device accordingly. |
| 429 | */ |
| 430 | list_add_tail(&pe->child, &parent->child_list); |
Sam Bobroff | 80e65b0 | 2018-09-12 11:23:26 +1000 | [diff] [blame] | 431 | list_add_tail(&edev->entry, &pe->edevs); |
Gavin Shan | 9b84348 | 2012-09-07 22:44:09 +0000 | [diff] [blame] | 432 | edev->pe = pe; |
Gavin Shan | c6406d8 | 2015-03-17 16:15:08 +1100 | [diff] [blame] | 433 | pr_debug("EEH: Add %04x:%02x:%02x.%01x to " |
| 434 | "Device PE#%x, Parent PE#%x\n", |
Alexey Kardashevskiy | 69672bd | 2017-08-29 17:34:01 +1000 | [diff] [blame] | 435 | pdn->phb->global_number, |
Alexey Kardashevskiy | 405b33a | 2017-08-29 17:34:02 +1000 | [diff] [blame] | 436 | pdn->busno, |
| 437 | PCI_SLOT(pdn->devfn), |
| 438 | PCI_FUNC(pdn->devfn), |
Gavin Shan | c6406d8 | 2015-03-17 16:15:08 +1100 | [diff] [blame] | 439 | pe->addr, pe->parent->addr); |
Gavin Shan | 9b84348 | 2012-09-07 22:44:09 +0000 | [diff] [blame] | 440 | |
| 441 | return 0; |
| 442 | } |
Gavin Shan | 82e8882 | 2012-09-07 22:44:10 +0000 | [diff] [blame] | 443 | |
| 444 | /** |
| 445 | * eeh_rmv_from_parent_pe - Remove one EEH device from the associated PE |
| 446 | * @edev: EEH device |
| 447 | * |
| 448 | * The PE hierarchy tree might be changed when doing PCI hotplug. |
| 449 | * Also, the PCI devices or buses could be removed from the system |
| 450 | * during EEH recovery. So we have to call the function remove the |
| 451 | * corresponding PE accordingly if necessary. |
| 452 | */ |
Gavin Shan | 807a827 | 2013-07-24 10:24:55 +0800 | [diff] [blame] | 453 | int eeh_rmv_from_parent_pe(struct eeh_dev *edev) |
Gavin Shan | 82e8882 | 2012-09-07 22:44:10 +0000 | [diff] [blame] | 454 | { |
Gavin Shan | 5efc3ad | 2012-09-11 19:16:16 +0000 | [diff] [blame] | 455 | struct eeh_pe *pe, *parent, *child; |
| 456 | int cnt; |
Alexey Kardashevskiy | 69672bd | 2017-08-29 17:34:01 +1000 | [diff] [blame] | 457 | struct pci_dn *pdn = eeh_dev_to_pdn(edev); |
Gavin Shan | 82e8882 | 2012-09-07 22:44:10 +0000 | [diff] [blame] | 458 | |
Sam Bobroff | 9a3eda2 | 2018-09-12 11:23:28 +1000 | [diff] [blame] | 459 | pe = eeh_dev_to_pe(edev); |
| 460 | if (!pe) { |
Gavin Shan | c6406d8 | 2015-03-17 16:15:08 +1100 | [diff] [blame] | 461 | pr_debug("%s: No PE found for device %04x:%02x:%02x.%01x\n", |
Alexey Kardashevskiy | 69672bd | 2017-08-29 17:34:01 +1000 | [diff] [blame] | 462 | __func__, pdn->phb->global_number, |
Alexey Kardashevskiy | 405b33a | 2017-08-29 17:34:02 +1000 | [diff] [blame] | 463 | pdn->busno, |
| 464 | PCI_SLOT(pdn->devfn), |
| 465 | PCI_FUNC(pdn->devfn)); |
Gavin Shan | 82e8882 | 2012-09-07 22:44:10 +0000 | [diff] [blame] | 466 | return -EEXIST; |
| 467 | } |
| 468 | |
| 469 | /* Remove the EEH device */ |
Gavin Shan | 82e8882 | 2012-09-07 22:44:10 +0000 | [diff] [blame] | 470 | edev->pe = NULL; |
Sam Bobroff | 80e65b0 | 2018-09-12 11:23:26 +1000 | [diff] [blame] | 471 | list_del(&edev->entry); |
Gavin Shan | 82e8882 | 2012-09-07 22:44:10 +0000 | [diff] [blame] | 472 | |
| 473 | /* |
| 474 | * Check if the parent PE includes any EEH devices. |
| 475 | * If not, we should delete that. Also, we should |
| 476 | * delete the parent PE if it doesn't have associated |
| 477 | * child PEs and EEH devices. |
| 478 | */ |
| 479 | while (1) { |
| 480 | parent = pe->parent; |
Gavin Shan | 5efc3ad | 2012-09-11 19:16:16 +0000 | [diff] [blame] | 481 | if (pe->type & EEH_PE_PHB) |
Gavin Shan | 82e8882 | 2012-09-07 22:44:10 +0000 | [diff] [blame] | 482 | break; |
| 483 | |
Gavin Shan | 807a827 | 2013-07-24 10:24:55 +0800 | [diff] [blame] | 484 | if (!(pe->state & EEH_PE_KEEP)) { |
Gavin Shan | 20ee6a9 | 2012-09-11 19:16:17 +0000 | [diff] [blame] | 485 | if (list_empty(&pe->edevs) && |
| 486 | list_empty(&pe->child_list)) { |
| 487 | list_del(&pe->child); |
| 488 | kfree(pe); |
| 489 | } else { |
Gavin Shan | 5efc3ad | 2012-09-11 19:16:16 +0000 | [diff] [blame] | 490 | break; |
Gavin Shan | 20ee6a9 | 2012-09-11 19:16:17 +0000 | [diff] [blame] | 491 | } |
| 492 | } else { |
| 493 | if (list_empty(&pe->edevs)) { |
| 494 | cnt = 0; |
| 495 | list_for_each_entry(child, &pe->child_list, child) { |
Gavin Shan | e716e01 | 2012-11-22 21:58:26 +0000 | [diff] [blame] | 496 | if (!(child->type & EEH_PE_INVALID)) { |
Gavin Shan | 20ee6a9 | 2012-09-11 19:16:17 +0000 | [diff] [blame] | 497 | cnt++; |
| 498 | break; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | if (!cnt) |
| 503 | pe->type |= EEH_PE_INVALID; |
| 504 | else |
| 505 | break; |
| 506 | } |
Gavin Shan | 82e8882 | 2012-09-07 22:44:10 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | pe = parent; |
| 510 | } |
| 511 | |
| 512 | return 0; |
| 513 | } |
Gavin Shan | 5b66352 | 2012-09-07 22:44:12 +0000 | [diff] [blame] | 514 | |
| 515 | /** |
Gavin Shan | 5a71978 | 2013-06-20 13:21:01 +0800 | [diff] [blame] | 516 | * eeh_pe_update_time_stamp - Update PE's frozen time stamp |
| 517 | * @pe: EEH PE |
| 518 | * |
| 519 | * We have time stamp for each PE to trace its time of getting |
| 520 | * frozen in last hour. The function should be called to update |
| 521 | * the time stamp on first error of the specific PE. On the other |
| 522 | * handle, we needn't account for errors happened in last hour. |
| 523 | */ |
| 524 | void eeh_pe_update_time_stamp(struct eeh_pe *pe) |
| 525 | { |
Arnd Bergmann | edfd17f | 2017-11-04 22:26:52 +0100 | [diff] [blame] | 526 | time64_t tstamp; |
Gavin Shan | 5a71978 | 2013-06-20 13:21:01 +0800 | [diff] [blame] | 527 | |
| 528 | if (!pe) return; |
| 529 | |
| 530 | if (pe->freeze_count <= 0) { |
| 531 | pe->freeze_count = 0; |
Arnd Bergmann | edfd17f | 2017-11-04 22:26:52 +0100 | [diff] [blame] | 532 | pe->tstamp = ktime_get_seconds(); |
Gavin Shan | 5a71978 | 2013-06-20 13:21:01 +0800 | [diff] [blame] | 533 | } else { |
Arnd Bergmann | edfd17f | 2017-11-04 22:26:52 +0100 | [diff] [blame] | 534 | tstamp = ktime_get_seconds(); |
| 535 | if (tstamp - pe->tstamp > 3600) { |
Gavin Shan | 5a71978 | 2013-06-20 13:21:01 +0800 | [diff] [blame] | 536 | pe->tstamp = tstamp; |
| 537 | pe->freeze_count = 0; |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | /** |
Gavin Shan | 5b66352 | 2012-09-07 22:44:12 +0000 | [diff] [blame] | 543 | * eeh_pe_state_mark - Mark specified state for PE and its associated device |
| 544 | * @pe: EEH PE |
| 545 | * |
| 546 | * EEH error affects the current PE and its child PEs. The function |
| 547 | * is used to mark appropriate state for the affected PEs and the |
| 548 | * associated devices. |
| 549 | */ |
Sam Bobroff | e762bb8 | 2018-09-12 11:23:31 +1000 | [diff] [blame^] | 550 | void eeh_pe_state_mark(struct eeh_pe *root, int state) |
Gavin Shan | 5b66352 | 2012-09-07 22:44:12 +0000 | [diff] [blame] | 551 | { |
Sam Bobroff | e762bb8 | 2018-09-12 11:23:31 +1000 | [diff] [blame^] | 552 | struct eeh_pe *pe; |
| 553 | |
| 554 | eeh_for_each_pe(root, pe) |
| 555 | if (!(pe->state & EEH_PE_REMOVED)) |
| 556 | pe->state |= state; |
Gavin Shan | 5b66352 | 2012-09-07 22:44:12 +0000 | [diff] [blame] | 557 | } |
Gavin Shan | e0056b0 | 2016-09-28 14:34:55 +1000 | [diff] [blame] | 558 | EXPORT_SYMBOL_GPL(eeh_pe_state_mark); |
Gavin Shan | 5b66352 | 2012-09-07 22:44:12 +0000 | [diff] [blame] | 559 | |
Sam Bobroff | e762bb8 | 2018-09-12 11:23:31 +1000 | [diff] [blame^] | 560 | /** |
| 561 | * eeh_pe_mark_isolated |
| 562 | * @pe: EEH PE |
| 563 | * |
| 564 | * Record that a PE has been isolated by marking the PE and it's children as |
| 565 | * EEH_PE_ISOLATED (and EEH_PE_CFG_BLOCKED, if required) and their PCI devices |
| 566 | * as pci_channel_io_frozen. |
| 567 | */ |
| 568 | void eeh_pe_mark_isolated(struct eeh_pe *root) |
| 569 | { |
| 570 | struct eeh_pe *pe; |
| 571 | struct eeh_dev *edev; |
| 572 | struct pci_dev *pdev; |
| 573 | |
| 574 | eeh_pe_state_mark(root, EEH_PE_ISOLATED); |
| 575 | eeh_for_each_pe(root, pe) { |
| 576 | list_for_each_entry(edev, &pe->edevs, entry) { |
| 577 | pdev = eeh_dev_to_pci_dev(edev); |
| 578 | if (pdev) |
| 579 | pdev->error_state = pci_channel_io_frozen; |
| 580 | } |
| 581 | /* Block PCI config access if required */ |
| 582 | if (pe->state & EEH_PE_CFG_RESTRICTED) |
| 583 | pe->state |= EEH_PE_CFG_BLOCKED; |
| 584 | } |
| 585 | } |
| 586 | EXPORT_SYMBOL_GPL(eeh_pe_mark_isolated); |
| 587 | |
Sam Bobroff | d6c4932 | 2018-05-25 13:11:32 +1000 | [diff] [blame] | 588 | static void *__eeh_pe_dev_mode_mark(struct eeh_dev *edev, void *flag) |
Gavin Shan | d2b0f6f | 2014-04-24 18:00:19 +1000 | [diff] [blame] | 589 | { |
Gavin Shan | d2b0f6f | 2014-04-24 18:00:19 +1000 | [diff] [blame] | 590 | int mode = *((int *)flag); |
| 591 | |
| 592 | edev->mode |= mode; |
| 593 | |
| 594 | return NULL; |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * eeh_pe_dev_state_mark - Mark state for all device under the PE |
| 599 | * @pe: EEH PE |
| 600 | * |
| 601 | * Mark specific state for all child devices of the PE. |
| 602 | */ |
| 603 | void eeh_pe_dev_mode_mark(struct eeh_pe *pe, int mode) |
| 604 | { |
| 605 | eeh_pe_dev_traverse(pe, __eeh_pe_dev_mode_mark, &mode); |
| 606 | } |
| 607 | |
Gavin Shan | 5b66352 | 2012-09-07 22:44:12 +0000 | [diff] [blame] | 608 | /** |
| 609 | * __eeh_pe_state_clear - Clear state for the PE |
| 610 | * @data: EEH PE |
| 611 | * @flag: state |
| 612 | * |
| 613 | * The function is used to clear the indicated state from the |
| 614 | * given PE. Besides, we also clear the check count of the PE |
| 615 | * as well. |
| 616 | */ |
Sam Bobroff | d6c4932 | 2018-05-25 13:11:32 +1000 | [diff] [blame] | 617 | static void *__eeh_pe_state_clear(struct eeh_pe *pe, void *flag) |
Gavin Shan | 5b66352 | 2012-09-07 22:44:12 +0000 | [diff] [blame] | 618 | { |
Gavin Shan | 5b66352 | 2012-09-07 22:44:12 +0000 | [diff] [blame] | 619 | int state = *((int *)flag); |
Gavin Shan | 22fca17 | 2014-09-30 12:38:59 +1000 | [diff] [blame] | 620 | struct eeh_dev *edev, *tmp; |
| 621 | struct pci_dev *pdev; |
Gavin Shan | 5b66352 | 2012-09-07 22:44:12 +0000 | [diff] [blame] | 622 | |
Gavin Shan | d2b0f6f | 2014-04-24 18:00:19 +1000 | [diff] [blame] | 623 | /* Keep the state of permanently removed PE intact */ |
Gavin Shan | 432227e | 2014-12-11 14:28:55 +1100 | [diff] [blame] | 624 | if (pe->state & EEH_PE_REMOVED) |
Gavin Shan | d2b0f6f | 2014-04-24 18:00:19 +1000 | [diff] [blame] | 625 | return NULL; |
| 626 | |
Gavin Shan | 5b66352 | 2012-09-07 22:44:12 +0000 | [diff] [blame] | 627 | pe->state &= ~state; |
Gavin Shan | d2b0f6f | 2014-04-24 18:00:19 +1000 | [diff] [blame] | 628 | |
Gavin Shan | 22fca17 | 2014-09-30 12:38:59 +1000 | [diff] [blame] | 629 | /* |
| 630 | * Special treatment on clearing isolated state. Clear |
| 631 | * check count since last isolation and put all affected |
| 632 | * devices to normal state. |
| 633 | */ |
| 634 | if (!(state & EEH_PE_ISOLATED)) |
| 635 | return NULL; |
| 636 | |
| 637 | pe->check_count = 0; |
| 638 | eeh_pe_for_each_dev(pe, edev, tmp) { |
| 639 | pdev = eeh_dev_to_pci_dev(edev); |
| 640 | if (!pdev) |
| 641 | continue; |
| 642 | |
| 643 | pdev->error_state = pci_channel_io_normal; |
| 644 | } |
Gavin Shan | 5b66352 | 2012-09-07 22:44:12 +0000 | [diff] [blame] | 645 | |
Gavin Shan | b6541db | 2014-10-01 17:07:53 +1000 | [diff] [blame] | 646 | /* Unblock PCI config access if required */ |
| 647 | if (pe->state & EEH_PE_CFG_RESTRICTED) |
| 648 | pe->state &= ~EEH_PE_CFG_BLOCKED; |
| 649 | |
Gavin Shan | 5b66352 | 2012-09-07 22:44:12 +0000 | [diff] [blame] | 650 | return NULL; |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * eeh_pe_state_clear - Clear state for the PE and its children |
| 655 | * @pe: PE |
| 656 | * @state: state to be cleared |
| 657 | * |
| 658 | * When the PE and its children has been recovered from error, |
| 659 | * we need clear the error state for that. The function is used |
| 660 | * for the purpose. |
| 661 | */ |
| 662 | void eeh_pe_state_clear(struct eeh_pe *pe, int state) |
| 663 | { |
| 664 | eeh_pe_traverse(pe, __eeh_pe_state_clear, &state); |
| 665 | } |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 666 | |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 667 | /* |
| 668 | * Some PCI bridges (e.g. PLX bridges) have primary/secondary |
| 669 | * buses assigned explicitly by firmware, and we probably have |
| 670 | * lost that after reset. So we have to delay the check until |
| 671 | * the PCI-CFG registers have been restored for the parent |
| 672 | * bridge. |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 673 | * |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 674 | * Don't use normal PCI-CFG accessors, which probably has been |
| 675 | * blocked on normal path during the stage. So we need utilize |
| 676 | * eeh operations, which is always permitted. |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 677 | */ |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 678 | static void eeh_bridge_check_link(struct eeh_dev *edev) |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 679 | { |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 680 | struct pci_dn *pdn = eeh_dev_to_pdn(edev); |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 681 | int cap; |
| 682 | uint32_t val; |
| 683 | int timeout = 0; |
| 684 | |
| 685 | /* |
| 686 | * We only check root port and downstream ports of |
| 687 | * PCIe switches |
| 688 | */ |
Gavin Shan | 4b83bd4 | 2013-07-24 10:24:59 +0800 | [diff] [blame] | 689 | if (!(edev->mode & (EEH_DEV_ROOT_PORT | EEH_DEV_DS_PORT))) |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 690 | return; |
| 691 | |
Gavin Shan | 4b83bd4 | 2013-07-24 10:24:59 +0800 | [diff] [blame] | 692 | pr_debug("%s: Check PCIe link for %04x:%02x:%02x.%01x ...\n", |
Alexey Kardashevskiy | 69672bd | 2017-08-29 17:34:01 +1000 | [diff] [blame] | 693 | __func__, pdn->phb->global_number, |
Alexey Kardashevskiy | 405b33a | 2017-08-29 17:34:02 +1000 | [diff] [blame] | 694 | pdn->busno, |
| 695 | PCI_SLOT(pdn->devfn), |
| 696 | PCI_FUNC(pdn->devfn)); |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 697 | |
| 698 | /* Check slot status */ |
Gavin Shan | 4b83bd4 | 2013-07-24 10:24:59 +0800 | [diff] [blame] | 699 | cap = edev->pcie_cap; |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 700 | eeh_ops->read_config(pdn, cap + PCI_EXP_SLTSTA, 2, &val); |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 701 | if (!(val & PCI_EXP_SLTSTA_PDS)) { |
| 702 | pr_debug(" No card in the slot (0x%04x) !\n", val); |
| 703 | return; |
| 704 | } |
| 705 | |
| 706 | /* Check power status if we have the capability */ |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 707 | eeh_ops->read_config(pdn, cap + PCI_EXP_SLTCAP, 2, &val); |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 708 | if (val & PCI_EXP_SLTCAP_PCP) { |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 709 | eeh_ops->read_config(pdn, cap + PCI_EXP_SLTCTL, 2, &val); |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 710 | if (val & PCI_EXP_SLTCTL_PCC) { |
| 711 | pr_debug(" In power-off state, power it on ...\n"); |
| 712 | val &= ~(PCI_EXP_SLTCTL_PCC | PCI_EXP_SLTCTL_PIC); |
| 713 | val |= (0x0100 & PCI_EXP_SLTCTL_PIC); |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 714 | eeh_ops->write_config(pdn, cap + PCI_EXP_SLTCTL, 2, val); |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 715 | msleep(2 * 1000); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | /* Enable link */ |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 720 | eeh_ops->read_config(pdn, cap + PCI_EXP_LNKCTL, 2, &val); |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 721 | val &= ~PCI_EXP_LNKCTL_LD; |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 722 | eeh_ops->write_config(pdn, cap + PCI_EXP_LNKCTL, 2, val); |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 723 | |
| 724 | /* Check link */ |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 725 | eeh_ops->read_config(pdn, cap + PCI_EXP_LNKCAP, 4, &val); |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 726 | if (!(val & PCI_EXP_LNKCAP_DLLLARC)) { |
| 727 | pr_debug(" No link reporting capability (0x%08x) \n", val); |
| 728 | msleep(1000); |
| 729 | return; |
| 730 | } |
| 731 | |
| 732 | /* Wait the link is up until timeout (5s) */ |
| 733 | timeout = 0; |
| 734 | while (timeout < 5000) { |
| 735 | msleep(20); |
| 736 | timeout += 20; |
| 737 | |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 738 | eeh_ops->read_config(pdn, cap + PCI_EXP_LNKSTA, 2, &val); |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 739 | if (val & PCI_EXP_LNKSTA_DLLLA) |
| 740 | break; |
| 741 | } |
| 742 | |
| 743 | if (val & PCI_EXP_LNKSTA_DLLLA) |
| 744 | pr_debug(" Link up (%s)\n", |
| 745 | (val & PCI_EXP_LNKSTA_CLS_2_5GB) ? "2.5GB" : "5GB"); |
| 746 | else |
| 747 | pr_debug(" Link not ready (0x%04x)\n", val); |
| 748 | } |
| 749 | |
| 750 | #define BYTE_SWAP(OFF) (8*((OFF)/4)+3-(OFF)) |
| 751 | #define SAVED_BYTE(OFF) (((u8 *)(edev->config_space))[BYTE_SWAP(OFF)]) |
| 752 | |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 753 | static void eeh_restore_bridge_bars(struct eeh_dev *edev) |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 754 | { |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 755 | struct pci_dn *pdn = eeh_dev_to_pdn(edev); |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 756 | int i; |
| 757 | |
| 758 | /* |
| 759 | * Device BARs: 0x10 - 0x18 |
| 760 | * Bus numbers and windows: 0x18 - 0x30 |
| 761 | */ |
| 762 | for (i = 4; i < 13; i++) |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 763 | eeh_ops->write_config(pdn, i*4, 4, edev->config_space[i]); |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 764 | /* Rom: 0x38 */ |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 765 | eeh_ops->write_config(pdn, 14*4, 4, edev->config_space[14]); |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 766 | |
| 767 | /* Cache line & Latency timer: 0xC 0xD */ |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 768 | eeh_ops->write_config(pdn, PCI_CACHE_LINE_SIZE, 1, |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 769 | SAVED_BYTE(PCI_CACHE_LINE_SIZE)); |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 770 | eeh_ops->write_config(pdn, PCI_LATENCY_TIMER, 1, |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 771 | SAVED_BYTE(PCI_LATENCY_TIMER)); |
| 772 | /* Max latency, min grant, interrupt ping and line: 0x3C */ |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 773 | eeh_ops->write_config(pdn, 15*4, 4, edev->config_space[15]); |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 774 | |
| 775 | /* PCI Command: 0x4 */ |
Michael Neuling | 13a83ea | 2018-04-11 13:37:58 +1000 | [diff] [blame] | 776 | eeh_ops->write_config(pdn, PCI_COMMAND, 4, edev->config_space[1] | |
| 777 | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER); |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 778 | |
| 779 | /* Check the PCIe link is ready */ |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 780 | eeh_bridge_check_link(edev); |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 781 | } |
| 782 | |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 783 | static void eeh_restore_device_bars(struct eeh_dev *edev) |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 784 | { |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 785 | struct pci_dn *pdn = eeh_dev_to_pdn(edev); |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 786 | int i; |
| 787 | u32 cmd; |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 788 | |
| 789 | for (i = 4; i < 10; i++) |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 790 | eeh_ops->write_config(pdn, i*4, 4, edev->config_space[i]); |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 791 | /* 12 == Expansion ROM Address */ |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 792 | eeh_ops->write_config(pdn, 12*4, 4, edev->config_space[12]); |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 793 | |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 794 | eeh_ops->write_config(pdn, PCI_CACHE_LINE_SIZE, 1, |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 795 | SAVED_BYTE(PCI_CACHE_LINE_SIZE)); |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 796 | eeh_ops->write_config(pdn, PCI_LATENCY_TIMER, 1, |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 797 | SAVED_BYTE(PCI_LATENCY_TIMER)); |
| 798 | |
| 799 | /* max latency, min grant, interrupt pin and line */ |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 800 | eeh_ops->write_config(pdn, 15*4, 4, edev->config_space[15]); |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 801 | |
| 802 | /* |
| 803 | * Restore PERR & SERR bits, some devices require it, |
| 804 | * don't touch the other command bits |
| 805 | */ |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 806 | eeh_ops->read_config(pdn, PCI_COMMAND, 4, &cmd); |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 807 | if (edev->config_space[1] & PCI_COMMAND_PARITY) |
| 808 | cmd |= PCI_COMMAND_PARITY; |
| 809 | else |
| 810 | cmd &= ~PCI_COMMAND_PARITY; |
| 811 | if (edev->config_space[1] & PCI_COMMAND_SERR) |
| 812 | cmd |= PCI_COMMAND_SERR; |
| 813 | else |
| 814 | cmd &= ~PCI_COMMAND_SERR; |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 815 | eeh_ops->write_config(pdn, PCI_COMMAND, 4, cmd); |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | /** |
| 819 | * eeh_restore_one_device_bars - Restore the Base Address Registers for one device |
| 820 | * @data: EEH device |
| 821 | * @flag: Unused |
| 822 | * |
| 823 | * Loads the PCI configuration space base address registers, |
| 824 | * the expansion ROM base address, the latency timer, and etc. |
| 825 | * from the saved values in the device node. |
| 826 | */ |
Sam Bobroff | d6c4932 | 2018-05-25 13:11:32 +1000 | [diff] [blame] | 827 | static void *eeh_restore_one_device_bars(struct eeh_dev *edev, void *flag) |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 828 | { |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 829 | struct pci_dn *pdn = eeh_dev_to_pdn(edev); |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 830 | |
Gavin Shan | f5c5771 | 2013-07-24 10:24:58 +0800 | [diff] [blame] | 831 | /* Do special restore for bridges */ |
Gavin Shan | 4b83bd4 | 2013-07-24 10:24:59 +0800 | [diff] [blame] | 832 | if (edev->mode & EEH_DEV_BRIDGE) |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 833 | eeh_restore_bridge_bars(edev); |
Gavin Shan | 652defe | 2013-06-27 13:46:43 +0800 | [diff] [blame] | 834 | else |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 835 | eeh_restore_device_bars(edev); |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 836 | |
Gavin Shan | 0bd7858 | 2015-03-17 16:15:07 +1100 | [diff] [blame] | 837 | if (eeh_ops->restore_config && pdn) |
| 838 | eeh_ops->restore_config(pdn); |
Gavin Shan | 1d35054 | 2014-01-03 17:47:12 +0800 | [diff] [blame] | 839 | |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 840 | return NULL; |
| 841 | } |
| 842 | |
| 843 | /** |
| 844 | * eeh_pe_restore_bars - Restore the PCI config space info |
| 845 | * @pe: EEH PE |
| 846 | * |
| 847 | * This routine performs a recursive walk to the children |
| 848 | * of this device as well. |
| 849 | */ |
| 850 | void eeh_pe_restore_bars(struct eeh_pe *pe) |
| 851 | { |
Gavin Shan | ea81245 | 2012-09-11 19:16:18 +0000 | [diff] [blame] | 852 | /* |
| 853 | * We needn't take the EEH lock since eeh_pe_dev_traverse() |
| 854 | * will take that. |
| 855 | */ |
Gavin Shan | 9e6d2cf | 2012-09-07 22:44:15 +0000 | [diff] [blame] | 856 | eeh_pe_dev_traverse(pe, eeh_restore_one_device_bars, NULL); |
| 857 | } |
Gavin Shan | 9b3c76f | 2012-09-07 22:44:19 +0000 | [diff] [blame] | 858 | |
| 859 | /** |
Gavin Shan | 357b2f3 | 2014-06-11 18:26:44 +1000 | [diff] [blame] | 860 | * eeh_pe_loc_get - Retrieve location code binding to the given PE |
| 861 | * @pe: EEH PE |
| 862 | * |
| 863 | * Retrieve the location code of the given PE. If the primary PE bus |
| 864 | * is root bus, we will grab location code from PHB device tree node |
| 865 | * or root port. Otherwise, the upstream bridge's device tree node |
| 866 | * of the primary PE bus will be checked for the location code. |
| 867 | */ |
| 868 | const char *eeh_pe_loc_get(struct eeh_pe *pe) |
| 869 | { |
Gavin Shan | 357b2f3 | 2014-06-11 18:26:44 +1000 | [diff] [blame] | 870 | struct pci_bus *bus = eeh_pe_bus_get(pe); |
Gavin Shan | 7e56f62 | 2015-12-02 16:25:32 +1100 | [diff] [blame] | 871 | struct device_node *dn; |
Mike Qiu | 9e5c6e5 | 2014-07-15 01:42:22 -0400 | [diff] [blame] | 872 | const char *loc = NULL; |
Gavin Shan | 357b2f3 | 2014-06-11 18:26:44 +1000 | [diff] [blame] | 873 | |
Gavin Shan | 7e56f62 | 2015-12-02 16:25:32 +1100 | [diff] [blame] | 874 | while (bus) { |
| 875 | dn = pci_bus_to_OF_node(bus); |
| 876 | if (!dn) { |
| 877 | bus = bus->parent; |
| 878 | continue; |
| 879 | } |
Gavin Shan | 357b2f3 | 2014-06-11 18:26:44 +1000 | [diff] [blame] | 880 | |
Gavin Shan | 7e56f62 | 2015-12-02 16:25:32 +1100 | [diff] [blame] | 881 | if (pci_is_root_bus(bus)) |
Mike Qiu | 9e5c6e5 | 2014-07-15 01:42:22 -0400 | [diff] [blame] | 882 | loc = of_get_property(dn, "ibm,io-base-loc-code", NULL); |
Gavin Shan | 7e56f62 | 2015-12-02 16:25:32 +1100 | [diff] [blame] | 883 | else |
| 884 | loc = of_get_property(dn, "ibm,slot-location-code", |
| 885 | NULL); |
Gavin Shan | 357b2f3 | 2014-06-11 18:26:44 +1000 | [diff] [blame] | 886 | |
Gavin Shan | 7e56f62 | 2015-12-02 16:25:32 +1100 | [diff] [blame] | 887 | if (loc) |
| 888 | return loc; |
| 889 | |
| 890 | bus = bus->parent; |
Gavin Shan | 357b2f3 | 2014-06-11 18:26:44 +1000 | [diff] [blame] | 891 | } |
| 892 | |
Gavin Shan | 7e56f62 | 2015-12-02 16:25:32 +1100 | [diff] [blame] | 893 | return "N/A"; |
Gavin Shan | 357b2f3 | 2014-06-11 18:26:44 +1000 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | /** |
Gavin Shan | 9b3c76f | 2012-09-07 22:44:19 +0000 | [diff] [blame] | 897 | * eeh_pe_bus_get - Retrieve PCI bus according to the given PE |
| 898 | * @pe: EEH PE |
| 899 | * |
| 900 | * Retrieve the PCI bus according to the given PE. Basically, |
| 901 | * there're 3 types of PEs: PHB/Bus/Device. For PHB PE, the |
| 902 | * primary PCI bus will be retrieved. The parent bus will be |
| 903 | * returned for BUS PE. However, we don't have associated PCI |
| 904 | * bus for DEVICE PE. |
| 905 | */ |
| 906 | struct pci_bus *eeh_pe_bus_get(struct eeh_pe *pe) |
| 907 | { |
Gavin Shan | 9b3c76f | 2012-09-07 22:44:19 +0000 | [diff] [blame] | 908 | struct eeh_dev *edev; |
| 909 | struct pci_dev *pdev; |
| 910 | |
Gavin Shan | 4eb0799 | 2016-02-09 15:50:23 +1100 | [diff] [blame] | 911 | if (pe->type & EEH_PE_PHB) |
| 912 | return pe->phb->bus; |
Gavin Shan | 8cdb283 | 2013-06-20 13:20:55 +0800 | [diff] [blame] | 913 | |
Gavin Shan | 4eb0799 | 2016-02-09 15:50:23 +1100 | [diff] [blame] | 914 | /* The primary bus might be cached during probe time */ |
| 915 | if (pe->state & EEH_PE_PRI_BUS) |
| 916 | return pe->bus; |
Gavin Shan | 9b3c76f | 2012-09-07 22:44:19 +0000 | [diff] [blame] | 917 | |
Gavin Shan | 4eb0799 | 2016-02-09 15:50:23 +1100 | [diff] [blame] | 918 | /* Retrieve the parent PCI bus of first (top) PCI device */ |
Sam Bobroff | 80e65b0 | 2018-09-12 11:23:26 +1000 | [diff] [blame] | 919 | edev = list_first_entry_or_null(&pe->edevs, struct eeh_dev, entry); |
Gavin Shan | 4eb0799 | 2016-02-09 15:50:23 +1100 | [diff] [blame] | 920 | pdev = eeh_dev_to_pci_dev(edev); |
| 921 | if (pdev) |
| 922 | return pdev->bus; |
| 923 | |
| 924 | return NULL; |
Gavin Shan | 9b3c76f | 2012-09-07 22:44:19 +0000 | [diff] [blame] | 925 | } |