blob: d8add34177f2cfa2725972bb3d57308d61b603df [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * CompactPCI Hot Plug Driver PCI functions
3 *
Scott Murraybcc488a2005-05-27 16:48:52 -04004 * Copyright (C) 2002,2005 by SOMA Networks, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * All rights reserved.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
16 * NON INFRINGEMENT. See the GNU General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 * Send feedback to <scottm@somanetworks.com>
24 */
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/module.h>
27#include <linux/kernel.h>
28#include <linux/pci.h>
Greg Kroah-Hartman7a54f252006-10-13 20:05:19 -070029#include <linux/pci_hotplug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/proc_fs.h>
31#include "../pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "cpci_hotplug.h"
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define MY_NAME "cpci_hotplug"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36extern int cpci_debug;
37
38#define dbg(format, arg...) \
39 do { \
Scott Murraybcc488a2005-05-27 16:48:52 -040040 if (cpci_debug) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 printk (KERN_DEBUG "%s: " format "\n", \
42 MY_NAME , ## arg); \
Scott Murraybcc488a2005-05-27 16:48:52 -040043 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME , ## arg)
45#define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME , ## arg)
46#define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg)
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49u8 cpci_get_attention_status(struct slot* slot)
50{
51 int hs_cap;
52 u16 hs_csr;
53
54 hs_cap = pci_bus_find_capability(slot->bus,
55 slot->devfn,
56 PCI_CAP_ID_CHSWP);
Scott Murraybcc488a2005-05-27 16:48:52 -040057 if (!hs_cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Scott Murraybcc488a2005-05-27 16:48:52 -040060 if (pci_bus_read_config_word(slot->bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 slot->devfn,
62 hs_cap + 2,
Scott Murraybcc488a2005-05-27 16:48:52 -040063 &hs_csr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 return 0;
Scott Murraybcc488a2005-05-27 16:48:52 -040065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return hs_csr & 0x0008 ? 1 : 0;
67}
68
69int cpci_set_attention_status(struct slot* slot, int status)
70{
71 int hs_cap;
72 u16 hs_csr;
73
74 hs_cap = pci_bus_find_capability(slot->bus,
75 slot->devfn,
76 PCI_CAP_ID_CHSWP);
Scott Murraybcc488a2005-05-27 16:48:52 -040077 if (!hs_cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return 0;
Scott Murraybcc488a2005-05-27 16:48:52 -040079 if (pci_bus_read_config_word(slot->bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 slot->devfn,
81 hs_cap + 2,
Scott Murraybcc488a2005-05-27 16:48:52 -040082 &hs_csr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return 0;
Scott Murraybcc488a2005-05-27 16:48:52 -040084 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 hs_csr |= HS_CSR_LOO;
Scott Murraybcc488a2005-05-27 16:48:52 -040086 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 hs_csr &= ~HS_CSR_LOO;
Scott Murraybcc488a2005-05-27 16:48:52 -040088 if (pci_bus_write_config_word(slot->bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 slot->devfn,
90 hs_cap + 2,
Scott Murraybcc488a2005-05-27 16:48:52 -040091 hs_csr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return 1;
94}
95
96u16 cpci_get_hs_csr(struct slot* slot)
97{
98 int hs_cap;
99 u16 hs_csr;
100
101 hs_cap = pci_bus_find_capability(slot->bus,
102 slot->devfn,
103 PCI_CAP_ID_CHSWP);
Scott Murraybcc488a2005-05-27 16:48:52 -0400104 if (!hs_cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return 0xFFFF;
Scott Murraybcc488a2005-05-27 16:48:52 -0400106 if (pci_bus_read_config_word(slot->bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 slot->devfn,
108 hs_cap + 2,
Scott Murraybcc488a2005-05-27 16:48:52 -0400109 &hs_csr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return hs_csr;
112}
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114int cpci_check_and_clear_ins(struct slot* slot)
115{
116 int hs_cap;
117 u16 hs_csr;
118 int ins = 0;
119
120 hs_cap = pci_bus_find_capability(slot->bus,
121 slot->devfn,
122 PCI_CAP_ID_CHSWP);
Scott Murraybcc488a2005-05-27 16:48:52 -0400123 if (!hs_cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return 0;
Scott Murraybcc488a2005-05-27 16:48:52 -0400125 if (pci_bus_read_config_word(slot->bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 slot->devfn,
127 hs_cap + 2,
Scott Murraybcc488a2005-05-27 16:48:52 -0400128 &hs_csr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return 0;
Scott Murraybcc488a2005-05-27 16:48:52 -0400130 if (hs_csr & HS_CSR_INS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 /* Clear INS (by setting it) */
Scott Murraybcc488a2005-05-27 16:48:52 -0400132 if (pci_bus_write_config_word(slot->bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 slot->devfn,
134 hs_cap + 2,
Scott Murraybcc488a2005-05-27 16:48:52 -0400135 hs_csr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 ins = 0;
Scott Murraybcc488a2005-05-27 16:48:52 -0400137 else
138 ins = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
140 return ins;
141}
142
143int cpci_check_ext(struct slot* slot)
144{
145 int hs_cap;
146 u16 hs_csr;
147 int ext = 0;
148
149 hs_cap = pci_bus_find_capability(slot->bus,
150 slot->devfn,
151 PCI_CAP_ID_CHSWP);
Scott Murraybcc488a2005-05-27 16:48:52 -0400152 if (!hs_cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return 0;
Scott Murraybcc488a2005-05-27 16:48:52 -0400154 if (pci_bus_read_config_word(slot->bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 slot->devfn,
156 hs_cap + 2,
Scott Murraybcc488a2005-05-27 16:48:52 -0400157 &hs_csr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return 0;
Scott Murraybcc488a2005-05-27 16:48:52 -0400159 if (hs_csr & HS_CSR_EXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 ext = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return ext;
162}
163
164int cpci_clear_ext(struct slot* slot)
165{
166 int hs_cap;
167 u16 hs_csr;
168
169 hs_cap = pci_bus_find_capability(slot->bus,
170 slot->devfn,
171 PCI_CAP_ID_CHSWP);
Scott Murraybcc488a2005-05-27 16:48:52 -0400172 if (!hs_cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return -ENODEV;
Scott Murraybcc488a2005-05-27 16:48:52 -0400174 if (pci_bus_read_config_word(slot->bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 slot->devfn,
176 hs_cap + 2,
Scott Murraybcc488a2005-05-27 16:48:52 -0400177 &hs_csr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return -ENODEV;
Scott Murraybcc488a2005-05-27 16:48:52 -0400179 if (hs_csr & HS_CSR_EXT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 /* Clear EXT (by setting it) */
Scott Murraybcc488a2005-05-27 16:48:52 -0400181 if (pci_bus_write_config_word(slot->bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 slot->devfn,
183 hs_cap + 2,
Scott Murraybcc488a2005-05-27 16:48:52 -0400184 hs_csr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187 return 0;
188}
189
190int cpci_led_on(struct slot* slot)
191{
192 int hs_cap;
193 u16 hs_csr;
194
195 hs_cap = pci_bus_find_capability(slot->bus,
196 slot->devfn,
197 PCI_CAP_ID_CHSWP);
Scott Murraybcc488a2005-05-27 16:48:52 -0400198 if (!hs_cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return -ENODEV;
Scott Murraybcc488a2005-05-27 16:48:52 -0400200 if (pci_bus_read_config_word(slot->bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 slot->devfn,
202 hs_cap + 2,
Scott Murraybcc488a2005-05-27 16:48:52 -0400203 &hs_csr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return -ENODEV;
Scott Murraybcc488a2005-05-27 16:48:52 -0400205 if ((hs_csr & HS_CSR_LOO) != HS_CSR_LOO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 hs_csr |= HS_CSR_LOO;
Scott Murraybcc488a2005-05-27 16:48:52 -0400207 if (pci_bus_write_config_word(slot->bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 slot->devfn,
209 hs_cap + 2,
210 hs_csr)) {
211 err("Could not set LOO for slot %s",
Alex Chiangd6c479e2008-10-20 17:41:17 -0600212 hotplug_slot_name(slot->hotplug_slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return -ENODEV;
214 }
215 }
216 return 0;
217}
218
219int cpci_led_off(struct slot* slot)
220{
221 int hs_cap;
222 u16 hs_csr;
223
224 hs_cap = pci_bus_find_capability(slot->bus,
225 slot->devfn,
226 PCI_CAP_ID_CHSWP);
Scott Murraybcc488a2005-05-27 16:48:52 -0400227 if (!hs_cap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return -ENODEV;
Scott Murraybcc488a2005-05-27 16:48:52 -0400229 if (pci_bus_read_config_word(slot->bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 slot->devfn,
231 hs_cap + 2,
Scott Murraybcc488a2005-05-27 16:48:52 -0400232 &hs_csr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return -ENODEV;
Scott Murraybcc488a2005-05-27 16:48:52 -0400234 if (hs_csr & HS_CSR_LOO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 hs_csr &= ~HS_CSR_LOO;
Scott Murraybcc488a2005-05-27 16:48:52 -0400236 if (pci_bus_write_config_word(slot->bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 slot->devfn,
238 hs_cap + 2,
239 hs_csr)) {
240 err("Could not clear LOO for slot %s",
Alex Chiangd6c479e2008-10-20 17:41:17 -0600241 hotplug_slot_name(slot->hotplug_slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return -ENODEV;
243 }
244 }
245 return 0;
246}
247
248
249/*
250 * Device configuration functions
251 */
252
Sam Ravnborg0ab2b572008-02-17 10:45:28 +0100253int __ref cpci_configure_slot(struct slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Yijing Wang05b12502013-01-15 11:12:20 +0800255 struct pci_dev *dev;
Scott Murraycc702c22006-08-22 19:55:57 -0400256 struct pci_bus *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800258 dbg("%s - enter", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Scott Murraybcc488a2005-05-27 16:48:52 -0400260 if (slot->dev == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 dbg("pci_dev null, finding %02x:%02x:%x",
262 slot->bus->number, PCI_SLOT(slot->devfn), PCI_FUNC(slot->devfn));
Scott Murraybcc488a2005-05-27 16:48:52 -0400263 slot->dev = pci_get_slot(slot->bus, slot->devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265
266 /* Still NULL? Well then scan for it! */
Scott Murraybcc488a2005-05-27 16:48:52 -0400267 if (slot->dev == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 int n;
269 dbg("pci_dev still null");
270
271 /*
272 * This will generate pci_dev structures for all functions, but
273 * we will only call this case when lookup fails.
274 */
275 n = pci_scan_slot(slot->bus, slot->devfn);
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800276 dbg("%s: pci_scan_slot returned %d", __func__, n);
Scott Murraybcc488a2005-05-27 16:48:52 -0400277 slot->dev = pci_get_slot(slot->bus, slot->devfn);
278 if (slot->dev == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 err("Could not find PCI device for slot %02x", slot->number);
Scott Murraycc702c22006-08-22 19:55:57 -0400280 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282 }
Scott Murraycc702c22006-08-22 19:55:57 -0400283 parent = slot->dev->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Yijing Wang05b12502013-01-15 11:12:20 +0800285 list_for_each_entry(dev, &parent->devices, bus_list)
286 if (PCI_SLOT(dev->devfn) != PCI_SLOT(slot->devfn))
Scott Murraycc702c22006-08-22 19:55:57 -0400287 continue;
288 if ((dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) ||
Yinghai Lu073ae102012-05-17 18:58:41 -0700289 (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS))
290 pci_hp_add_bridge(dev);
Yijing Wang05b12502013-01-15 11:12:20 +0800291
Scott Murray43b7d7c2005-05-09 17:31:50 -0400292
Yinghai Lu073ae102012-05-17 18:58:41 -0700293 pci_assign_unassigned_bridge_resources(parent->self);
294
Scott Murraycc702c22006-08-22 19:55:57 -0400295 pci_bus_add_devices(parent);
Scott Murray43b7d7c2005-05-09 17:31:50 -0400296
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800297 dbg("%s - exit", __func__);
Scott Murray43b7d7c2005-05-09 17:31:50 -0400298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301int cpci_unconfigure_slot(struct slot* slot)
302{
Yijing Wang05b12502013-01-15 11:12:20 +0800303 struct pci_dev *dev, *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800305 dbg("%s - enter", __func__);
Scott Murraybcc488a2005-05-27 16:48:52 -0400306 if (!slot->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 err("No device for slot %02x\n", slot->number);
308 return -ENODEV;
309 }
310
Yijing Wang05b12502013-01-15 11:12:20 +0800311 list_for_each_entry_safe(dev, temp, &slot->bus->devices, bus_list) {
312 if (PCI_SLOT(dev->devfn) != PCI_SLOT(slot->devfn))
313 continue;
314 pci_dev_get(dev);
315 pci_stop_and_remove_bus_device(dev);
316 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
Scott Murray03e49d42005-06-06 15:48:04 -0400318 pci_dev_put(slot->dev);
319 slot->dev = NULL;
320
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800321 dbg("%s - exit", __func__);
Scott Murray43b7d7c2005-05-09 17:31:50 -0400322 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}