Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright IBM Corp. 2012 |
| 3 | * |
| 4 | * Author(s): |
| 5 | * Jan Glauber <jang@linux.vnet.ibm.com> |
| 6 | * |
| 7 | * The System z PCI code is a rewrite from a prototype by |
| 8 | * the following people (Kudoz!): |
Jan Glauber | bedef75 | 2012-12-06 14:06:28 +0100 | [diff] [blame] | 9 | * Alexander Schmidt |
| 10 | * Christoph Raisch |
| 11 | * Hannes Hering |
| 12 | * Hoang-Nam Nguyen |
| 13 | * Jan-Bernd Themann |
| 14 | * Stefan Roscher |
| 15 | * Thomas Klein |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #define COMPONENT "zPCI" |
| 19 | #define pr_fmt(fmt) COMPONENT ": " fmt |
| 20 | |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/err.h> |
| 24 | #include <linux/export.h> |
| 25 | #include <linux/delay.h> |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 26 | #include <linux/irq.h> |
| 27 | #include <linux/kernel_stat.h> |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 28 | #include <linux/seq_file.h> |
| 29 | #include <linux/pci.h> |
| 30 | #include <linux/msi.h> |
| 31 | |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 32 | #include <asm/isc.h> |
| 33 | #include <asm/airq.h> |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 34 | #include <asm/facility.h> |
| 35 | #include <asm/pci_insn.h> |
Jan Glauber | a755a45 | 2012-11-29 12:55:21 +0100 | [diff] [blame] | 36 | #include <asm/pci_clp.h> |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 37 | #include <asm/pci_dma.h> |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 38 | |
| 39 | #define DEBUG /* enable pr_debug */ |
| 40 | |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 41 | #define SIC_IRQ_MODE_ALL 0 |
| 42 | #define SIC_IRQ_MODE_SINGLE 1 |
| 43 | |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 44 | #define ZPCI_NR_DMA_SPACES 1 |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 45 | #define ZPCI_MSI_VEC_BITS 6 |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 46 | #define ZPCI_NR_DEVICES CONFIG_PCI_NR_FUNCTIONS |
| 47 | |
| 48 | /* list of all detected zpci devices */ |
| 49 | LIST_HEAD(zpci_list); |
Jan Glauber | 7441b06 | 2012-11-29 14:35:47 +0100 | [diff] [blame] | 50 | EXPORT_SYMBOL_GPL(zpci_list); |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 51 | DEFINE_MUTEX(zpci_list_lock); |
Jan Glauber | 7441b06 | 2012-11-29 14:35:47 +0100 | [diff] [blame] | 52 | EXPORT_SYMBOL_GPL(zpci_list_lock); |
| 53 | |
Sebastian Ott | 5392335 | 2013-01-31 19:55:17 +0100 | [diff] [blame] | 54 | static struct pci_hp_callback_ops *hotplug_ops; |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 55 | |
| 56 | static DECLARE_BITMAP(zpci_domain, ZPCI_NR_DEVICES); |
| 57 | static DEFINE_SPINLOCK(zpci_domain_lock); |
| 58 | |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 59 | struct callback { |
| 60 | irq_handler_t handler; |
| 61 | void *data; |
| 62 | }; |
| 63 | |
| 64 | struct zdev_irq_map { |
| 65 | unsigned long aibv; /* AI bit vector */ |
| 66 | int msi_vecs; /* consecutive MSI-vectors used */ |
| 67 | int __unused; |
| 68 | struct callback cb[ZPCI_NR_MSI_VECS]; /* callback handler array */ |
| 69 | spinlock_t lock; /* protect callbacks against de-reg */ |
| 70 | }; |
| 71 | |
| 72 | struct intr_bucket { |
| 73 | /* amap of adapters, one bit per dev, corresponds to one irq nr */ |
| 74 | unsigned long *alloc; |
| 75 | /* AI summary bit, global page for all devices */ |
| 76 | unsigned long *aisb; |
| 77 | /* pointer to aibv and callback data in zdev */ |
| 78 | struct zdev_irq_map *imap[ZPCI_NR_DEVICES]; |
| 79 | /* protects the whole bucket struct */ |
| 80 | spinlock_t lock; |
| 81 | }; |
| 82 | |
| 83 | static struct intr_bucket *bucket; |
| 84 | |
| 85 | /* Adapter local summary indicator */ |
| 86 | static u8 *zpci_irq_si; |
| 87 | |
| 88 | static atomic_t irq_retries = ATOMIC_INIT(0); |
| 89 | |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 90 | /* I/O Map */ |
| 91 | static DEFINE_SPINLOCK(zpci_iomap_lock); |
| 92 | static DECLARE_BITMAP(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES); |
| 93 | struct zpci_iomap_entry *zpci_iomap_start; |
| 94 | EXPORT_SYMBOL_GPL(zpci_iomap_start); |
| 95 | |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 96 | /* highest irq summary bit */ |
| 97 | static int __read_mostly aisb_max; |
| 98 | |
| 99 | static struct kmem_cache *zdev_irq_cache; |
Jan Glauber | d0b0885 | 2012-12-11 14:53:35 +0100 | [diff] [blame] | 100 | static struct kmem_cache *zdev_fmb_cache; |
| 101 | |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 102 | static inline int irq_to_msi_nr(unsigned int irq) |
| 103 | { |
| 104 | return irq & ZPCI_MSI_MASK; |
| 105 | } |
| 106 | |
| 107 | static inline int irq_to_dev_nr(unsigned int irq) |
| 108 | { |
| 109 | return irq >> ZPCI_MSI_VEC_BITS; |
| 110 | } |
| 111 | |
| 112 | static inline struct zdev_irq_map *get_imap(unsigned int irq) |
| 113 | { |
| 114 | return bucket->imap[irq_to_dev_nr(irq)]; |
| 115 | } |
| 116 | |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 117 | struct zpci_dev *get_zdev(struct pci_dev *pdev) |
| 118 | { |
| 119 | return (struct zpci_dev *) pdev->sysdata; |
| 120 | } |
| 121 | |
| 122 | struct zpci_dev *get_zdev_by_fid(u32 fid) |
| 123 | { |
| 124 | struct zpci_dev *tmp, *zdev = NULL; |
| 125 | |
| 126 | mutex_lock(&zpci_list_lock); |
| 127 | list_for_each_entry(tmp, &zpci_list, entry) { |
| 128 | if (tmp->fid == fid) { |
| 129 | zdev = tmp; |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | mutex_unlock(&zpci_list_lock); |
| 134 | return zdev; |
| 135 | } |
| 136 | |
| 137 | bool zpci_fid_present(u32 fid) |
| 138 | { |
| 139 | return (get_zdev_by_fid(fid) != NULL) ? true : false; |
| 140 | } |
| 141 | |
| 142 | static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus) |
| 143 | { |
| 144 | return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL; |
| 145 | } |
| 146 | |
| 147 | int pci_domain_nr(struct pci_bus *bus) |
| 148 | { |
| 149 | return ((struct zpci_dev *) bus->sysdata)->domain; |
| 150 | } |
| 151 | EXPORT_SYMBOL_GPL(pci_domain_nr); |
| 152 | |
| 153 | int pci_proc_domain(struct pci_bus *bus) |
| 154 | { |
| 155 | return pci_domain_nr(bus); |
| 156 | } |
| 157 | EXPORT_SYMBOL_GPL(pci_proc_domain); |
| 158 | |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 159 | /* Modify PCI: Register adapter interruptions */ |
| 160 | static int zpci_register_airq(struct zpci_dev *zdev, unsigned int aisb, |
| 161 | u64 aibv) |
| 162 | { |
| 163 | u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_REG_INT); |
| 164 | struct zpci_fib *fib; |
| 165 | int rc; |
| 166 | |
| 167 | fib = (void *) get_zeroed_page(GFP_KERNEL); |
| 168 | if (!fib) |
| 169 | return -ENOMEM; |
| 170 | |
| 171 | fib->isc = PCI_ISC; |
| 172 | fib->noi = zdev->irq_map->msi_vecs; |
| 173 | fib->sum = 1; /* enable summary notifications */ |
| 174 | fib->aibv = aibv; |
| 175 | fib->aibvo = 0; /* every function has its own page */ |
| 176 | fib->aisb = (u64) bucket->aisb + aisb / 8; |
| 177 | fib->aisbo = aisb & ZPCI_MSI_MASK; |
| 178 | |
Sebastian Ott | b2a9e87 | 2013-04-16 14:15:42 +0200 | [diff] [blame] | 179 | rc = s390pci_mod_fc(req, fib); |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 180 | pr_debug("%s mpcifc returned noi: %d\n", __func__, fib->noi); |
| 181 | |
| 182 | free_page((unsigned long) fib); |
| 183 | return rc; |
| 184 | } |
| 185 | |
| 186 | struct mod_pci_args { |
| 187 | u64 base; |
| 188 | u64 limit; |
| 189 | u64 iota; |
Jan Glauber | d0b0885 | 2012-12-11 14:53:35 +0100 | [diff] [blame] | 190 | u64 fmb_addr; |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | static int mod_pci(struct zpci_dev *zdev, int fn, u8 dmaas, struct mod_pci_args *args) |
| 194 | { |
| 195 | u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, fn); |
| 196 | struct zpci_fib *fib; |
| 197 | int rc; |
| 198 | |
| 199 | /* The FIB must be available even if it's not used */ |
| 200 | fib = (void *) get_zeroed_page(GFP_KERNEL); |
| 201 | if (!fib) |
| 202 | return -ENOMEM; |
| 203 | |
| 204 | fib->pba = args->base; |
| 205 | fib->pal = args->limit; |
| 206 | fib->iota = args->iota; |
Jan Glauber | d0b0885 | 2012-12-11 14:53:35 +0100 | [diff] [blame] | 207 | fib->fmb_addr = args->fmb_addr; |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 208 | |
Sebastian Ott | b2a9e87 | 2013-04-16 14:15:42 +0200 | [diff] [blame] | 209 | rc = s390pci_mod_fc(req, fib); |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 210 | free_page((unsigned long) fib); |
| 211 | return rc; |
| 212 | } |
| 213 | |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 214 | /* Modify PCI: Register I/O address translation parameters */ |
| 215 | int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas, |
| 216 | u64 base, u64 limit, u64 iota) |
| 217 | { |
Jan Glauber | d0b0885 | 2012-12-11 14:53:35 +0100 | [diff] [blame] | 218 | struct mod_pci_args args = { base, limit, iota, 0 }; |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 219 | |
| 220 | WARN_ON_ONCE(iota & 0x3fff); |
| 221 | args.iota |= ZPCI_IOTA_RTTO_FLAG; |
| 222 | return mod_pci(zdev, ZPCI_MOD_FC_REG_IOAT, dmaas, &args); |
| 223 | } |
| 224 | |
| 225 | /* Modify PCI: Unregister I/O address translation parameters */ |
| 226 | int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas) |
| 227 | { |
Jan Glauber | d0b0885 | 2012-12-11 14:53:35 +0100 | [diff] [blame] | 228 | struct mod_pci_args args = { 0, 0, 0, 0 }; |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 229 | |
| 230 | return mod_pci(zdev, ZPCI_MOD_FC_DEREG_IOAT, dmaas, &args); |
| 231 | } |
| 232 | |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 233 | /* Modify PCI: Unregister adapter interruptions */ |
| 234 | static int zpci_unregister_airq(struct zpci_dev *zdev) |
| 235 | { |
Jan Glauber | d0b0885 | 2012-12-11 14:53:35 +0100 | [diff] [blame] | 236 | struct mod_pci_args args = { 0, 0, 0, 0 }; |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 237 | |
| 238 | return mod_pci(zdev, ZPCI_MOD_FC_DEREG_INT, 0, &args); |
| 239 | } |
| 240 | |
Jan Glauber | d0b0885 | 2012-12-11 14:53:35 +0100 | [diff] [blame] | 241 | /* Modify PCI: Set PCI function measurement parameters */ |
| 242 | int zpci_fmb_enable_device(struct zpci_dev *zdev) |
| 243 | { |
| 244 | struct mod_pci_args args = { 0, 0, 0, 0 }; |
| 245 | |
| 246 | if (zdev->fmb) |
| 247 | return -EINVAL; |
| 248 | |
Wei Yongjun | 08b4212 | 2013-02-25 22:09:25 +0800 | [diff] [blame] | 249 | zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL); |
Jan Glauber | d0b0885 | 2012-12-11 14:53:35 +0100 | [diff] [blame] | 250 | if (!zdev->fmb) |
| 251 | return -ENOMEM; |
Jan Glauber | d0b0885 | 2012-12-11 14:53:35 +0100 | [diff] [blame] | 252 | WARN_ON((u64) zdev->fmb & 0xf); |
| 253 | |
| 254 | args.fmb_addr = virt_to_phys(zdev->fmb); |
| 255 | return mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args); |
| 256 | } |
| 257 | |
| 258 | /* Modify PCI: Disable PCI function measurement */ |
| 259 | int zpci_fmb_disable_device(struct zpci_dev *zdev) |
| 260 | { |
| 261 | struct mod_pci_args args = { 0, 0, 0, 0 }; |
| 262 | int rc; |
| 263 | |
| 264 | if (!zdev->fmb) |
| 265 | return -EINVAL; |
| 266 | |
| 267 | /* Function measurement is disabled if fmb address is zero */ |
| 268 | rc = mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args); |
| 269 | |
| 270 | kmem_cache_free(zdev_fmb_cache, zdev->fmb); |
| 271 | zdev->fmb = NULL; |
| 272 | return rc; |
| 273 | } |
| 274 | |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 275 | #define ZPCI_PCIAS_CFGSPC 15 |
| 276 | |
| 277 | static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len) |
| 278 | { |
| 279 | u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); |
| 280 | u64 data; |
| 281 | int rc; |
| 282 | |
Sebastian Ott | b2a9e87 | 2013-04-16 14:15:42 +0200 | [diff] [blame] | 283 | rc = s390pci_load(&data, req, offset); |
Sebastian Ott | b170bad | 2013-04-16 14:17:15 +0200 | [diff] [blame] | 284 | if (!rc) { |
| 285 | data = data << ((8 - len) * 8); |
| 286 | data = le64_to_cpu(data); |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 287 | *val = (u32) data; |
Sebastian Ott | b170bad | 2013-04-16 14:17:15 +0200 | [diff] [blame] | 288 | } else |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 289 | *val = 0xffffffff; |
| 290 | return rc; |
| 291 | } |
| 292 | |
| 293 | static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len) |
| 294 | { |
| 295 | u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); |
| 296 | u64 data = val; |
| 297 | int rc; |
| 298 | |
| 299 | data = cpu_to_le64(data); |
| 300 | data = data >> ((8 - len) * 8); |
Sebastian Ott | b2a9e87 | 2013-04-16 14:15:42 +0200 | [diff] [blame] | 301 | rc = s390pci_store(data, req, offset); |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 302 | return rc; |
| 303 | } |
| 304 | |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 305 | void synchronize_irq(unsigned int irq) |
| 306 | { |
| 307 | /* |
| 308 | * Not needed, the handler is protected by a lock and IRQs that occur |
| 309 | * after the handler is deleted are just NOPs. |
| 310 | */ |
| 311 | } |
| 312 | EXPORT_SYMBOL_GPL(synchronize_irq); |
| 313 | |
| 314 | void enable_irq(unsigned int irq) |
| 315 | { |
| 316 | struct msi_desc *msi = irq_get_msi_desc(irq); |
| 317 | |
| 318 | zpci_msi_set_mask_bits(msi, 1, 0); |
| 319 | } |
| 320 | EXPORT_SYMBOL_GPL(enable_irq); |
| 321 | |
| 322 | void disable_irq(unsigned int irq) |
| 323 | { |
| 324 | struct msi_desc *msi = irq_get_msi_desc(irq); |
| 325 | |
| 326 | zpci_msi_set_mask_bits(msi, 1, 1); |
| 327 | } |
| 328 | EXPORT_SYMBOL_GPL(disable_irq); |
| 329 | |
| 330 | void disable_irq_nosync(unsigned int irq) |
| 331 | { |
| 332 | disable_irq(irq); |
| 333 | } |
| 334 | EXPORT_SYMBOL_GPL(disable_irq_nosync); |
| 335 | |
| 336 | unsigned long probe_irq_on(void) |
| 337 | { |
| 338 | return 0; |
| 339 | } |
| 340 | EXPORT_SYMBOL_GPL(probe_irq_on); |
| 341 | |
| 342 | int probe_irq_off(unsigned long val) |
| 343 | { |
| 344 | return 0; |
| 345 | } |
| 346 | EXPORT_SYMBOL_GPL(probe_irq_off); |
| 347 | |
| 348 | unsigned int probe_irq_mask(unsigned long val) |
| 349 | { |
| 350 | return val; |
| 351 | } |
| 352 | EXPORT_SYMBOL_GPL(probe_irq_mask); |
| 353 | |
Greg Kroah-Hartman | b881bc4 | 2012-12-21 14:06:37 -0800 | [diff] [blame] | 354 | void pcibios_fixup_bus(struct pci_bus *bus) |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 355 | { |
| 356 | } |
| 357 | |
| 358 | resource_size_t pcibios_align_resource(void *data, const struct resource *res, |
| 359 | resource_size_t size, |
| 360 | resource_size_t align) |
| 361 | { |
| 362 | return 0; |
| 363 | } |
| 364 | |
Jan Glauber | 87bc359 | 2012-12-06 14:30:28 +0100 | [diff] [blame] | 365 | /* combine single writes by using store-block insn */ |
| 366 | void __iowrite64_copy(void __iomem *to, const void *from, size_t count) |
| 367 | { |
| 368 | zpci_memcpy_toio(to, from, count); |
| 369 | } |
| 370 | |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 371 | /* Create a virtual mapping cookie for a PCI BAR */ |
| 372 | void __iomem *pci_iomap(struct pci_dev *pdev, int bar, unsigned long max) |
| 373 | { |
| 374 | struct zpci_dev *zdev = get_zdev(pdev); |
| 375 | u64 addr; |
| 376 | int idx; |
| 377 | |
| 378 | if ((bar & 7) != bar) |
| 379 | return NULL; |
| 380 | |
| 381 | idx = zdev->bars[bar].map_idx; |
| 382 | spin_lock(&zpci_iomap_lock); |
| 383 | zpci_iomap_start[idx].fh = zdev->fh; |
| 384 | zpci_iomap_start[idx].bar = bar; |
| 385 | spin_unlock(&zpci_iomap_lock); |
| 386 | |
| 387 | addr = ZPCI_IOMAP_ADDR_BASE | ((u64) idx << 48); |
| 388 | return (void __iomem *) addr; |
| 389 | } |
| 390 | EXPORT_SYMBOL_GPL(pci_iomap); |
| 391 | |
| 392 | void pci_iounmap(struct pci_dev *pdev, void __iomem *addr) |
| 393 | { |
| 394 | unsigned int idx; |
| 395 | |
| 396 | idx = (((__force u64) addr) & ~ZPCI_IOMAP_ADDR_BASE) >> 48; |
| 397 | spin_lock(&zpci_iomap_lock); |
| 398 | zpci_iomap_start[idx].fh = 0; |
| 399 | zpci_iomap_start[idx].bar = 0; |
| 400 | spin_unlock(&zpci_iomap_lock); |
| 401 | } |
| 402 | EXPORT_SYMBOL_GPL(pci_iounmap); |
| 403 | |
| 404 | static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, |
| 405 | int size, u32 *val) |
| 406 | { |
| 407 | struct zpci_dev *zdev = get_zdev_by_bus(bus); |
Sebastian Ott | 2c3700b | 2013-04-16 14:18:41 +0200 | [diff] [blame] | 408 | int ret; |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 409 | |
| 410 | if (!zdev || devfn != ZPCI_DEVFN) |
Sebastian Ott | 2c3700b | 2013-04-16 14:18:41 +0200 | [diff] [blame] | 411 | ret = -ENODEV; |
| 412 | else |
| 413 | ret = zpci_cfg_load(zdev, where, val, size); |
| 414 | |
| 415 | return ret; |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | static int pci_write(struct pci_bus *bus, unsigned int devfn, int where, |
| 419 | int size, u32 val) |
| 420 | { |
| 421 | struct zpci_dev *zdev = get_zdev_by_bus(bus); |
Sebastian Ott | 2c3700b | 2013-04-16 14:18:41 +0200 | [diff] [blame] | 422 | int ret; |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 423 | |
| 424 | if (!zdev || devfn != ZPCI_DEVFN) |
Sebastian Ott | 2c3700b | 2013-04-16 14:18:41 +0200 | [diff] [blame] | 425 | ret = -ENODEV; |
| 426 | else |
| 427 | ret = zpci_cfg_store(zdev, where, val, size); |
| 428 | |
| 429 | return ret; |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | static struct pci_ops pci_root_ops = { |
| 433 | .read = pci_read, |
| 434 | .write = pci_write, |
| 435 | }; |
| 436 | |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 437 | /* store the last handled bit to implement fair scheduling of devices */ |
| 438 | static DEFINE_PER_CPU(unsigned long, next_sbit); |
| 439 | |
| 440 | static void zpci_irq_handler(void *dont, void *need) |
| 441 | { |
| 442 | unsigned long sbit, mbit, last = 0, start = __get_cpu_var(next_sbit); |
| 443 | int rescan = 0, max = aisb_max; |
| 444 | struct zdev_irq_map *imap; |
| 445 | |
Heiko Carstens | 420f42e | 2013-01-02 15:18:18 +0100 | [diff] [blame] | 446 | inc_irq_stat(IRQIO_PCI); |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 447 | sbit = start; |
| 448 | |
| 449 | scan: |
| 450 | /* find summary_bit */ |
| 451 | for_each_set_bit_left_cont(sbit, bucket->aisb, max) { |
| 452 | clear_bit(63 - (sbit & 63), bucket->aisb + (sbit >> 6)); |
| 453 | last = sbit; |
| 454 | |
| 455 | /* find vector bit */ |
| 456 | imap = bucket->imap[sbit]; |
| 457 | for_each_set_bit_left(mbit, &imap->aibv, imap->msi_vecs) { |
Heiko Carstens | 420f42e | 2013-01-02 15:18:18 +0100 | [diff] [blame] | 458 | inc_irq_stat(IRQIO_MSI); |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 459 | clear_bit(63 - mbit, &imap->aibv); |
| 460 | |
| 461 | spin_lock(&imap->lock); |
| 462 | if (imap->cb[mbit].handler) |
| 463 | imap->cb[mbit].handler(mbit, |
| 464 | imap->cb[mbit].data); |
| 465 | spin_unlock(&imap->lock); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | if (rescan) |
| 470 | goto out; |
| 471 | |
| 472 | /* scan the skipped bits */ |
| 473 | if (start > 0) { |
| 474 | sbit = 0; |
| 475 | max = start; |
| 476 | start = 0; |
| 477 | goto scan; |
| 478 | } |
| 479 | |
| 480 | /* enable interrupts again */ |
Sebastian Ott | b2a9e87 | 2013-04-16 14:15:42 +0200 | [diff] [blame] | 481 | set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 482 | |
| 483 | /* check again to not lose initiative */ |
| 484 | rmb(); |
| 485 | max = aisb_max; |
| 486 | sbit = find_first_bit_left(bucket->aisb, max); |
| 487 | if (sbit != max) { |
| 488 | atomic_inc(&irq_retries); |
| 489 | rescan++; |
| 490 | goto scan; |
| 491 | } |
| 492 | out: |
| 493 | /* store next device bit to scan */ |
| 494 | __get_cpu_var(next_sbit) = (++last >= aisb_max) ? 0 : last; |
| 495 | } |
| 496 | |
| 497 | /* msi_vecs - number of requested interrupts, 0 place function to error state */ |
| 498 | static int zpci_setup_msi(struct pci_dev *pdev, int msi_vecs) |
| 499 | { |
| 500 | struct zpci_dev *zdev = get_zdev(pdev); |
| 501 | unsigned int aisb, msi_nr; |
| 502 | struct msi_desc *msi; |
| 503 | int rc; |
| 504 | |
| 505 | /* store the number of used MSI vectors */ |
| 506 | zdev->irq_map->msi_vecs = min(msi_vecs, ZPCI_NR_MSI_VECS); |
| 507 | |
| 508 | spin_lock(&bucket->lock); |
| 509 | aisb = find_first_zero_bit(bucket->alloc, PAGE_SIZE); |
| 510 | /* alloc map exhausted? */ |
| 511 | if (aisb == PAGE_SIZE) { |
| 512 | spin_unlock(&bucket->lock); |
| 513 | return -EIO; |
| 514 | } |
| 515 | set_bit(aisb, bucket->alloc); |
| 516 | spin_unlock(&bucket->lock); |
| 517 | |
| 518 | zdev->aisb = aisb; |
| 519 | if (aisb + 1 > aisb_max) |
| 520 | aisb_max = aisb + 1; |
| 521 | |
| 522 | /* wire up IRQ shortcut pointer */ |
| 523 | bucket->imap[zdev->aisb] = zdev->irq_map; |
| 524 | pr_debug("%s: imap[%u] linked to %p\n", __func__, zdev->aisb, zdev->irq_map); |
| 525 | |
| 526 | /* TODO: irq number 0 wont be found if we return less than requested MSIs. |
| 527 | * ignore it for now and fix in common code. |
| 528 | */ |
| 529 | msi_nr = aisb << ZPCI_MSI_VEC_BITS; |
| 530 | |
| 531 | list_for_each_entry(msi, &pdev->msi_list, list) { |
| 532 | rc = zpci_setup_msi_irq(zdev, msi, msi_nr, |
| 533 | aisb << ZPCI_MSI_VEC_BITS); |
| 534 | if (rc) |
| 535 | return rc; |
| 536 | msi_nr++; |
| 537 | } |
| 538 | |
| 539 | rc = zpci_register_airq(zdev, aisb, (u64) &zdev->irq_map->aibv); |
| 540 | if (rc) { |
| 541 | clear_bit(aisb, bucket->alloc); |
| 542 | dev_err(&pdev->dev, "register MSI failed with: %d\n", rc); |
| 543 | return rc; |
| 544 | } |
| 545 | return (zdev->irq_map->msi_vecs == msi_vecs) ? |
| 546 | 0 : zdev->irq_map->msi_vecs; |
| 547 | } |
| 548 | |
| 549 | static void zpci_teardown_msi(struct pci_dev *pdev) |
| 550 | { |
| 551 | struct zpci_dev *zdev = get_zdev(pdev); |
| 552 | struct msi_desc *msi; |
| 553 | int aisb, rc; |
| 554 | |
| 555 | rc = zpci_unregister_airq(zdev); |
| 556 | if (rc) { |
| 557 | dev_err(&pdev->dev, "deregister MSI failed with: %d\n", rc); |
| 558 | return; |
| 559 | } |
| 560 | |
| 561 | msi = list_first_entry(&pdev->msi_list, struct msi_desc, list); |
| 562 | aisb = irq_to_dev_nr(msi->irq); |
| 563 | |
| 564 | list_for_each_entry(msi, &pdev->msi_list, list) |
| 565 | zpci_teardown_msi_irq(zdev, msi); |
| 566 | |
| 567 | clear_bit(aisb, bucket->alloc); |
| 568 | if (aisb + 1 == aisb_max) |
| 569 | aisb_max--; |
| 570 | } |
| 571 | |
| 572 | int arch_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type) |
| 573 | { |
| 574 | pr_debug("%s: requesting %d MSI-X interrupts...", __func__, nvec); |
| 575 | if (type != PCI_CAP_ID_MSIX && type != PCI_CAP_ID_MSI) |
| 576 | return -EINVAL; |
| 577 | return zpci_setup_msi(pdev, nvec); |
| 578 | } |
| 579 | |
| 580 | void arch_teardown_msi_irqs(struct pci_dev *pdev) |
| 581 | { |
| 582 | pr_info("%s: on pdev: %p\n", __func__, pdev); |
| 583 | zpci_teardown_msi(pdev); |
| 584 | } |
| 585 | |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 586 | static void zpci_map_resources(struct zpci_dev *zdev) |
| 587 | { |
| 588 | struct pci_dev *pdev = zdev->pdev; |
| 589 | resource_size_t len; |
| 590 | int i; |
| 591 | |
| 592 | for (i = 0; i < PCI_BAR_COUNT; i++) { |
| 593 | len = pci_resource_len(pdev, i); |
| 594 | if (!len) |
| 595 | continue; |
| 596 | pdev->resource[i].start = (resource_size_t) pci_iomap(pdev, i, 0); |
| 597 | pdev->resource[i].end = pdev->resource[i].start + len - 1; |
| 598 | pr_debug("BAR%i: -> start: %Lx end: %Lx\n", |
| 599 | i, pdev->resource[i].start, pdev->resource[i].end); |
| 600 | } |
| 601 | }; |
| 602 | |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 603 | struct zpci_dev *zpci_alloc_device(void) |
| 604 | { |
| 605 | struct zpci_dev *zdev; |
| 606 | |
| 607 | /* Alloc memory for our private pci device data */ |
| 608 | zdev = kzalloc(sizeof(*zdev), GFP_KERNEL); |
| 609 | if (!zdev) |
| 610 | return ERR_PTR(-ENOMEM); |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 611 | |
| 612 | /* Alloc aibv & callback space */ |
Wei Yongjun | 4118fee | 2012-12-03 16:11:34 +0100 | [diff] [blame] | 613 | zdev->irq_map = kmem_cache_zalloc(zdev_irq_cache, GFP_KERNEL); |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 614 | if (!zdev->irq_map) |
| 615 | goto error; |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 616 | WARN_ON((u64) zdev->irq_map & 0xff); |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 617 | return zdev; |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 618 | |
| 619 | error: |
| 620 | kfree(zdev); |
| 621 | return ERR_PTR(-ENOMEM); |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | void zpci_free_device(struct zpci_dev *zdev) |
| 625 | { |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 626 | kmem_cache_free(zdev_irq_cache, zdev->irq_map); |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 627 | kfree(zdev); |
| 628 | } |
| 629 | |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 630 | static void zpci_scan_devices(void) |
| 631 | { |
| 632 | struct zpci_dev *zdev; |
| 633 | |
| 634 | mutex_lock(&zpci_list_lock); |
| 635 | list_for_each_entry(zdev, &zpci_list, entry) |
| 636 | if (zdev->state == ZPCI_FN_STATE_CONFIGURED) |
| 637 | zpci_scan_device(zdev); |
| 638 | mutex_unlock(&zpci_list_lock); |
| 639 | } |
| 640 | |
| 641 | /* |
| 642 | * Too late for any s390 specific setup, since interrupts must be set up |
| 643 | * already which requires DMA setup too and the pci scan will access the |
| 644 | * config space, which only works if the function handle is enabled. |
| 645 | */ |
| 646 | int pcibios_enable_device(struct pci_dev *pdev, int mask) |
| 647 | { |
| 648 | struct resource *res; |
| 649 | u16 cmd; |
| 650 | int i; |
| 651 | |
| 652 | pci_read_config_word(pdev, PCI_COMMAND, &cmd); |
| 653 | |
| 654 | for (i = 0; i < PCI_BAR_COUNT; i++) { |
| 655 | res = &pdev->resource[i]; |
| 656 | |
| 657 | if (res->flags & IORESOURCE_IO) |
| 658 | return -EINVAL; |
| 659 | |
| 660 | if (res->flags & IORESOURCE_MEM) |
| 661 | cmd |= PCI_COMMAND_MEMORY; |
| 662 | } |
| 663 | pci_write_config_word(pdev, PCI_COMMAND, cmd); |
| 664 | return 0; |
| 665 | } |
| 666 | |
Jan Glauber | 1e8da95 | 2012-11-29 14:36:55 +0100 | [diff] [blame] | 667 | int pcibios_add_platform_entries(struct pci_dev *pdev) |
| 668 | { |
| 669 | return zpci_sysfs_add_device(&pdev->dev); |
| 670 | } |
| 671 | |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 672 | int zpci_request_irq(unsigned int irq, irq_handler_t handler, void *data) |
| 673 | { |
| 674 | int msi_nr = irq_to_msi_nr(irq); |
| 675 | struct zdev_irq_map *imap; |
| 676 | struct msi_desc *msi; |
| 677 | |
| 678 | msi = irq_get_msi_desc(irq); |
| 679 | if (!msi) |
| 680 | return -EIO; |
| 681 | |
| 682 | imap = get_imap(irq); |
| 683 | spin_lock_init(&imap->lock); |
| 684 | |
| 685 | pr_debug("%s: register handler for IRQ:MSI %d:%d\n", __func__, irq >> 6, msi_nr); |
| 686 | imap->cb[msi_nr].handler = handler; |
| 687 | imap->cb[msi_nr].data = data; |
| 688 | |
| 689 | /* |
| 690 | * The generic MSI code returns with the interrupt disabled on the |
| 691 | * card, using the MSI mask bits. Firmware doesn't appear to unmask |
| 692 | * at that level, so we do it here by hand. |
| 693 | */ |
| 694 | zpci_msi_set_mask_bits(msi, 1, 0); |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | void zpci_free_irq(unsigned int irq) |
| 699 | { |
| 700 | struct zdev_irq_map *imap = get_imap(irq); |
| 701 | int msi_nr = irq_to_msi_nr(irq); |
| 702 | unsigned long flags; |
| 703 | |
| 704 | pr_debug("%s: for irq: %d\n", __func__, irq); |
| 705 | |
| 706 | spin_lock_irqsave(&imap->lock, flags); |
| 707 | imap->cb[msi_nr].handler = NULL; |
| 708 | imap->cb[msi_nr].data = NULL; |
| 709 | spin_unlock_irqrestore(&imap->lock, flags); |
| 710 | } |
| 711 | |
| 712 | int request_irq(unsigned int irq, irq_handler_t handler, |
| 713 | unsigned long irqflags, const char *devname, void *dev_id) |
| 714 | { |
| 715 | pr_debug("%s: irq: %d handler: %p flags: %lx dev: %s\n", |
| 716 | __func__, irq, handler, irqflags, devname); |
| 717 | |
| 718 | return zpci_request_irq(irq, handler, dev_id); |
| 719 | } |
| 720 | EXPORT_SYMBOL_GPL(request_irq); |
| 721 | |
| 722 | void free_irq(unsigned int irq, void *dev_id) |
| 723 | { |
| 724 | zpci_free_irq(irq); |
| 725 | } |
| 726 | EXPORT_SYMBOL_GPL(free_irq); |
| 727 | |
| 728 | static int __init zpci_irq_init(void) |
| 729 | { |
| 730 | int cpu, rc; |
| 731 | |
| 732 | bucket = kzalloc(sizeof(*bucket), GFP_KERNEL); |
| 733 | if (!bucket) |
| 734 | return -ENOMEM; |
| 735 | |
| 736 | bucket->aisb = (unsigned long *) get_zeroed_page(GFP_KERNEL); |
| 737 | if (!bucket->aisb) { |
| 738 | rc = -ENOMEM; |
| 739 | goto out_aisb; |
| 740 | } |
| 741 | |
| 742 | bucket->alloc = (unsigned long *) get_zeroed_page(GFP_KERNEL); |
| 743 | if (!bucket->alloc) { |
| 744 | rc = -ENOMEM; |
| 745 | goto out_alloc; |
| 746 | } |
| 747 | |
| 748 | isc_register(PCI_ISC); |
| 749 | zpci_irq_si = s390_register_adapter_interrupt(&zpci_irq_handler, NULL, PCI_ISC); |
| 750 | if (IS_ERR(zpci_irq_si)) { |
| 751 | rc = PTR_ERR(zpci_irq_si); |
| 752 | zpci_irq_si = NULL; |
| 753 | goto out_ai; |
| 754 | } |
| 755 | |
| 756 | for_each_online_cpu(cpu) |
| 757 | per_cpu(next_sbit, cpu) = 0; |
| 758 | |
| 759 | spin_lock_init(&bucket->lock); |
| 760 | /* set summary to 1 to be called every time for the ISC */ |
| 761 | *zpci_irq_si = 1; |
Sebastian Ott | b2a9e87 | 2013-04-16 14:15:42 +0200 | [diff] [blame] | 762 | set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 763 | return 0; |
| 764 | |
| 765 | out_ai: |
| 766 | isc_unregister(PCI_ISC); |
| 767 | free_page((unsigned long) bucket->alloc); |
| 768 | out_alloc: |
| 769 | free_page((unsigned long) bucket->aisb); |
| 770 | out_aisb: |
| 771 | kfree(bucket); |
| 772 | return rc; |
| 773 | } |
| 774 | |
| 775 | static void zpci_irq_exit(void) |
| 776 | { |
| 777 | free_page((unsigned long) bucket->alloc); |
| 778 | free_page((unsigned long) bucket->aisb); |
| 779 | s390_unregister_adapter_interrupt(zpci_irq_si, PCI_ISC); |
| 780 | isc_unregister(PCI_ISC); |
| 781 | kfree(bucket); |
| 782 | } |
| 783 | |
Jan Glauber | d0b0885 | 2012-12-11 14:53:35 +0100 | [diff] [blame] | 784 | void zpci_debug_info(struct zpci_dev *zdev, struct seq_file *m) |
| 785 | { |
| 786 | if (!zdev) |
| 787 | return; |
| 788 | |
| 789 | seq_printf(m, "global irq retries: %u\n", atomic_read(&irq_retries)); |
| 790 | seq_printf(m, "aibv[0]:%016lx aibv[1]:%016lx aisb:%016lx\n", |
| 791 | get_imap(0)->aibv, get_imap(1)->aibv, *bucket->aisb); |
| 792 | } |
| 793 | |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 794 | static struct resource *zpci_alloc_bus_resource(unsigned long start, unsigned long size, |
| 795 | unsigned long flags, int domain) |
| 796 | { |
| 797 | struct resource *r; |
| 798 | char *name; |
| 799 | int rc; |
| 800 | |
| 801 | r = kzalloc(sizeof(*r), GFP_KERNEL); |
| 802 | if (!r) |
| 803 | return ERR_PTR(-ENOMEM); |
| 804 | r->start = start; |
| 805 | r->end = r->start + size - 1; |
| 806 | r->flags = flags; |
| 807 | r->parent = &iomem_resource; |
| 808 | name = kmalloc(18, GFP_KERNEL); |
| 809 | if (!name) { |
| 810 | kfree(r); |
| 811 | return ERR_PTR(-ENOMEM); |
| 812 | } |
| 813 | sprintf(name, "PCI Bus: %04x:%02x", domain, ZPCI_BUS_NR); |
| 814 | r->name = name; |
| 815 | |
| 816 | rc = request_resource(&iomem_resource, r); |
| 817 | if (rc) |
| 818 | pr_debug("request resource %pR failed\n", r); |
| 819 | return r; |
| 820 | } |
| 821 | |
| 822 | static int zpci_alloc_iomap(struct zpci_dev *zdev) |
| 823 | { |
| 824 | int entry; |
| 825 | |
| 826 | spin_lock(&zpci_iomap_lock); |
| 827 | entry = find_first_zero_bit(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES); |
| 828 | if (entry == ZPCI_IOMAP_MAX_ENTRIES) { |
| 829 | spin_unlock(&zpci_iomap_lock); |
| 830 | return -ENOSPC; |
| 831 | } |
| 832 | set_bit(entry, zpci_iomap); |
| 833 | spin_unlock(&zpci_iomap_lock); |
| 834 | return entry; |
| 835 | } |
| 836 | |
| 837 | static void zpci_free_iomap(struct zpci_dev *zdev, int entry) |
| 838 | { |
| 839 | spin_lock(&zpci_iomap_lock); |
| 840 | memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry)); |
| 841 | clear_bit(entry, zpci_iomap); |
| 842 | spin_unlock(&zpci_iomap_lock); |
| 843 | } |
| 844 | |
Sebastian Ott | af0a8a8 | 2013-04-16 14:13:21 +0200 | [diff] [blame] | 845 | int pcibios_add_device(struct pci_dev *pdev) |
| 846 | { |
| 847 | struct zpci_dev *zdev = get_zdev(pdev); |
| 848 | |
| 849 | zpci_debug_init_device(zdev); |
| 850 | zpci_fmb_enable_device(zdev); |
| 851 | zpci_map_resources(zdev); |
| 852 | |
| 853 | return 0; |
| 854 | } |
| 855 | |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 856 | static int zpci_create_device_bus(struct zpci_dev *zdev) |
| 857 | { |
| 858 | struct resource *res; |
| 859 | LIST_HEAD(resources); |
| 860 | int i; |
| 861 | |
| 862 | /* allocate mapping entry for each used bar */ |
| 863 | for (i = 0; i < PCI_BAR_COUNT; i++) { |
| 864 | unsigned long addr, size, flags; |
| 865 | int entry; |
| 866 | |
| 867 | if (!zdev->bars[i].size) |
| 868 | continue; |
| 869 | entry = zpci_alloc_iomap(zdev); |
| 870 | if (entry < 0) |
| 871 | return entry; |
| 872 | zdev->bars[i].map_idx = entry; |
| 873 | |
| 874 | /* only MMIO is supported */ |
| 875 | flags = IORESOURCE_MEM; |
| 876 | if (zdev->bars[i].val & 8) |
| 877 | flags |= IORESOURCE_PREFETCH; |
| 878 | if (zdev->bars[i].val & 4) |
| 879 | flags |= IORESOURCE_MEM_64; |
| 880 | |
| 881 | addr = ZPCI_IOMAP_ADDR_BASE + ((u64) entry << 48); |
| 882 | |
| 883 | size = 1UL << zdev->bars[i].size; |
| 884 | |
| 885 | res = zpci_alloc_bus_resource(addr, size, flags, zdev->domain); |
| 886 | if (IS_ERR(res)) { |
| 887 | zpci_free_iomap(zdev, entry); |
| 888 | return PTR_ERR(res); |
| 889 | } |
| 890 | pci_add_resource(&resources, res); |
| 891 | } |
| 892 | |
| 893 | zdev->bus = pci_create_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops, |
| 894 | zdev, &resources); |
| 895 | if (!zdev->bus) |
| 896 | return -EIO; |
| 897 | |
| 898 | zdev->bus->max_bus_speed = zdev->max_bus_speed; |
| 899 | return 0; |
| 900 | } |
| 901 | |
| 902 | static int zpci_alloc_domain(struct zpci_dev *zdev) |
| 903 | { |
| 904 | spin_lock(&zpci_domain_lock); |
| 905 | zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES); |
| 906 | if (zdev->domain == ZPCI_NR_DEVICES) { |
| 907 | spin_unlock(&zpci_domain_lock); |
| 908 | return -ENOSPC; |
| 909 | } |
| 910 | set_bit(zdev->domain, zpci_domain); |
| 911 | spin_unlock(&zpci_domain_lock); |
| 912 | return 0; |
| 913 | } |
| 914 | |
| 915 | static void zpci_free_domain(struct zpci_dev *zdev) |
| 916 | { |
| 917 | spin_lock(&zpci_domain_lock); |
| 918 | clear_bit(zdev->domain, zpci_domain); |
| 919 | spin_unlock(&zpci_domain_lock); |
| 920 | } |
| 921 | |
Jan Glauber | a755a45 | 2012-11-29 12:55:21 +0100 | [diff] [blame] | 922 | int zpci_enable_device(struct zpci_dev *zdev) |
| 923 | { |
| 924 | int rc; |
| 925 | |
| 926 | rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); |
| 927 | if (rc) |
| 928 | goto out; |
| 929 | pr_info("Enabled fh: 0x%x fid: 0x%x\n", zdev->fh, zdev->fid); |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 930 | |
| 931 | rc = zpci_dma_init_device(zdev); |
| 932 | if (rc) |
| 933 | goto out_dma; |
Jan Glauber | a755a45 | 2012-11-29 12:55:21 +0100 | [diff] [blame] | 934 | return 0; |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 935 | |
| 936 | out_dma: |
| 937 | clp_disable_fh(zdev); |
Jan Glauber | a755a45 | 2012-11-29 12:55:21 +0100 | [diff] [blame] | 938 | out: |
| 939 | return rc; |
| 940 | } |
| 941 | EXPORT_SYMBOL_GPL(zpci_enable_device); |
| 942 | |
Sebastian Ott | cb65a66 | 2013-04-16 14:12:17 +0200 | [diff] [blame] | 943 | int zpci_disable_device(struct zpci_dev *zdev) |
| 944 | { |
| 945 | zpci_dma_exit_device(zdev); |
| 946 | return clp_disable_fh(zdev); |
| 947 | } |
| 948 | EXPORT_SYMBOL_GPL(zpci_disable_device); |
| 949 | |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 950 | int zpci_create_device(struct zpci_dev *zdev) |
| 951 | { |
| 952 | int rc; |
| 953 | |
| 954 | rc = zpci_alloc_domain(zdev); |
| 955 | if (rc) |
| 956 | goto out; |
| 957 | |
| 958 | rc = zpci_create_device_bus(zdev); |
| 959 | if (rc) |
| 960 | goto out_bus; |
| 961 | |
| 962 | mutex_lock(&zpci_list_lock); |
| 963 | list_add_tail(&zdev->entry, &zpci_list); |
Sebastian Ott | 5392335 | 2013-01-31 19:55:17 +0100 | [diff] [blame] | 964 | if (hotplug_ops) |
| 965 | hotplug_ops->create_slot(zdev); |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 966 | mutex_unlock(&zpci_list_lock); |
| 967 | |
| 968 | if (zdev->state == ZPCI_FN_STATE_STANDBY) |
| 969 | return 0; |
| 970 | |
Jan Glauber | a755a45 | 2012-11-29 12:55:21 +0100 | [diff] [blame] | 971 | rc = zpci_enable_device(zdev); |
| 972 | if (rc) |
| 973 | goto out_start; |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 974 | return 0; |
| 975 | |
Jan Glauber | a755a45 | 2012-11-29 12:55:21 +0100 | [diff] [blame] | 976 | out_start: |
| 977 | mutex_lock(&zpci_list_lock); |
| 978 | list_del(&zdev->entry); |
Sebastian Ott | 5392335 | 2013-01-31 19:55:17 +0100 | [diff] [blame] | 979 | if (hotplug_ops) |
| 980 | hotplug_ops->remove_slot(zdev); |
Jan Glauber | a755a45 | 2012-11-29 12:55:21 +0100 | [diff] [blame] | 981 | mutex_unlock(&zpci_list_lock); |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 982 | out_bus: |
| 983 | zpci_free_domain(zdev); |
| 984 | out: |
| 985 | return rc; |
| 986 | } |
| 987 | |
| 988 | void zpci_stop_device(struct zpci_dev *zdev) |
| 989 | { |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 990 | zpci_dma_exit_device(zdev); |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 991 | /* |
| 992 | * Note: SCLP disables fh via set-pci-fn so don't |
| 993 | * do that here. |
| 994 | */ |
| 995 | } |
| 996 | EXPORT_SYMBOL_GPL(zpci_stop_device); |
| 997 | |
| 998 | int zpci_scan_device(struct zpci_dev *zdev) |
| 999 | { |
| 1000 | zdev->pdev = pci_scan_single_device(zdev->bus, ZPCI_DEVFN); |
| 1001 | if (!zdev->pdev) { |
| 1002 | pr_err("pci_scan_single_device failed for fid: 0x%x\n", |
| 1003 | zdev->fid); |
| 1004 | goto out; |
| 1005 | } |
| 1006 | |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 1007 | pci_bus_add_devices(zdev->bus); |
| 1008 | |
| 1009 | /* now that pdev was added to the bus mark it as used */ |
| 1010 | zdev->state = ZPCI_FN_STATE_ONLINE; |
| 1011 | return 0; |
| 1012 | |
| 1013 | out: |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 1014 | zpci_dma_exit_device(zdev); |
Jan Glauber | a755a45 | 2012-11-29 12:55:21 +0100 | [diff] [blame] | 1015 | clp_disable_fh(zdev); |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 1016 | return -EIO; |
| 1017 | } |
| 1018 | EXPORT_SYMBOL_GPL(zpci_scan_device); |
| 1019 | |
| 1020 | static inline int barsize(u8 size) |
| 1021 | { |
| 1022 | return (size) ? (1 << size) >> 10 : 0; |
| 1023 | } |
| 1024 | |
| 1025 | static int zpci_mem_init(void) |
| 1026 | { |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 1027 | zdev_irq_cache = kmem_cache_create("PCI_IRQ_cache", sizeof(struct zdev_irq_map), |
| 1028 | L1_CACHE_BYTES, SLAB_HWCACHE_ALIGN, NULL); |
| 1029 | if (!zdev_irq_cache) |
| 1030 | goto error_zdev; |
| 1031 | |
Jan Glauber | d0b0885 | 2012-12-11 14:53:35 +0100 | [diff] [blame] | 1032 | zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb), |
| 1033 | 16, 0, NULL); |
| 1034 | if (!zdev_fmb_cache) |
| 1035 | goto error_fmb; |
| 1036 | |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 1037 | /* TODO: use realloc */ |
| 1038 | zpci_iomap_start = kzalloc(ZPCI_IOMAP_MAX_ENTRIES * sizeof(*zpci_iomap_start), |
| 1039 | GFP_KERNEL); |
| 1040 | if (!zpci_iomap_start) |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 1041 | goto error_iomap; |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 1042 | return 0; |
| 1043 | |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 1044 | error_iomap: |
Jan Glauber | d0b0885 | 2012-12-11 14:53:35 +0100 | [diff] [blame] | 1045 | kmem_cache_destroy(zdev_fmb_cache); |
| 1046 | error_fmb: |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 1047 | kmem_cache_destroy(zdev_irq_cache); |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 1048 | error_zdev: |
| 1049 | return -ENOMEM; |
| 1050 | } |
| 1051 | |
| 1052 | static void zpci_mem_exit(void) |
| 1053 | { |
| 1054 | kfree(zpci_iomap_start); |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 1055 | kmem_cache_destroy(zdev_irq_cache); |
Jan Glauber | d0b0885 | 2012-12-11 14:53:35 +0100 | [diff] [blame] | 1056 | kmem_cache_destroy(zdev_fmb_cache); |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 1057 | } |
| 1058 | |
Sebastian Ott | 5392335 | 2013-01-31 19:55:17 +0100 | [diff] [blame] | 1059 | void zpci_register_hp_ops(struct pci_hp_callback_ops *ops) |
| 1060 | { |
| 1061 | mutex_lock(&zpci_list_lock); |
| 1062 | hotplug_ops = ops; |
| 1063 | mutex_unlock(&zpci_list_lock); |
| 1064 | } |
| 1065 | EXPORT_SYMBOL_GPL(zpci_register_hp_ops); |
| 1066 | |
| 1067 | void zpci_deregister_hp_ops(void) |
| 1068 | { |
| 1069 | mutex_lock(&zpci_list_lock); |
| 1070 | hotplug_ops = NULL; |
| 1071 | mutex_unlock(&zpci_list_lock); |
| 1072 | } |
| 1073 | EXPORT_SYMBOL_GPL(zpci_deregister_hp_ops); |
| 1074 | |
Sebastian Ott | 89b0dc95 | 2013-04-16 14:19:22 +0200 | [diff] [blame] | 1075 | unsigned int s390_pci_probe; |
Heiko Carstens | 1e5635d | 2013-01-30 15:52:16 +0100 | [diff] [blame] | 1076 | EXPORT_SYMBOL_GPL(s390_pci_probe); |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 1077 | |
| 1078 | char * __init pcibios_setup(char *str) |
| 1079 | { |
Sebastian Ott | 89b0dc95 | 2013-04-16 14:19:22 +0200 | [diff] [blame] | 1080 | if (!strcmp(str, "on")) { |
| 1081 | s390_pci_probe = 1; |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 1082 | return NULL; |
| 1083 | } |
| 1084 | return str; |
| 1085 | } |
| 1086 | |
| 1087 | static int __init pci_base_init(void) |
| 1088 | { |
| 1089 | int rc; |
| 1090 | |
Heiko Carstens | 1e5635d | 2013-01-30 15:52:16 +0100 | [diff] [blame] | 1091 | if (!s390_pci_probe) |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 1092 | return 0; |
| 1093 | |
| 1094 | if (!test_facility(2) || !test_facility(69) |
| 1095 | || !test_facility(71) || !test_facility(72)) |
| 1096 | return 0; |
| 1097 | |
| 1098 | pr_info("Probing PCI hardware: PCI:%d SID:%d AEN:%d\n", |
| 1099 | test_facility(69), test_facility(70), |
| 1100 | test_facility(71)); |
| 1101 | |
Jan Glauber | d0b0885 | 2012-12-11 14:53:35 +0100 | [diff] [blame] | 1102 | rc = zpci_debug_init(); |
| 1103 | if (rc) |
| 1104 | return rc; |
| 1105 | |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 1106 | rc = zpci_mem_init(); |
| 1107 | if (rc) |
| 1108 | goto out_mem; |
| 1109 | |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 1110 | rc = zpci_msihash_init(); |
| 1111 | if (rc) |
| 1112 | goto out_hash; |
| 1113 | |
| 1114 | rc = zpci_irq_init(); |
| 1115 | if (rc) |
| 1116 | goto out_irq; |
| 1117 | |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 1118 | rc = zpci_dma_init(); |
| 1119 | if (rc) |
| 1120 | goto out_dma; |
| 1121 | |
Jan Glauber | a755a45 | 2012-11-29 12:55:21 +0100 | [diff] [blame] | 1122 | rc = clp_find_pci_devices(); |
| 1123 | if (rc) |
| 1124 | goto out_find; |
| 1125 | |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 1126 | zpci_scan_devices(); |
| 1127 | return 0; |
| 1128 | |
Jan Glauber | a755a45 | 2012-11-29 12:55:21 +0100 | [diff] [blame] | 1129 | out_find: |
Jan Glauber | 828b35f | 2012-11-29 14:33:30 +0100 | [diff] [blame] | 1130 | zpci_dma_exit(); |
| 1131 | out_dma: |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 1132 | zpci_irq_exit(); |
| 1133 | out_irq: |
| 1134 | zpci_msihash_exit(); |
| 1135 | out_hash: |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 1136 | zpci_mem_exit(); |
| 1137 | out_mem: |
Jan Glauber | d0b0885 | 2012-12-11 14:53:35 +0100 | [diff] [blame] | 1138 | zpci_debug_exit(); |
Jan Glauber | cd24834 | 2012-11-29 12:50:30 +0100 | [diff] [blame] | 1139 | return rc; |
| 1140 | } |
| 1141 | subsys_initcall(pci_base_init); |