Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is subject to the terms and conditions of the GNU General Public |
| 3 | * License. See the file "COPYING" in the main directory of this archive |
| 4 | * for more details. |
| 5 | * |
Brent Casavant | 107d5a7 | 2006-10-17 00:09:24 -0700 | [diff] [blame] | 6 | * Copyright (C) 2005-2006 Silicon Graphics, Inc. All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 9 | /* This file contains the master driver module for use by SGI IOC4 subdrivers. |
| 10 | * |
| 11 | * It allocates any resources shared between multiple subdevices, and |
| 12 | * provides accessor functions (where needed) and the like for those |
| 13 | * resources. It also provides a mechanism for the subdevice modules |
| 14 | * to support loading and unloading. |
| 15 | * |
| 16 | * Non-shared resources (e.g. external interrupt A_INT_OUT register page |
| 17 | * alias, serial port and UART registers) are handled by the subdevice |
| 18 | * modules themselves. |
| 19 | * |
| 20 | * This is all necessary because IOC4 is not implemented as a multi-function |
| 21 | * PCI device, but an amalgamation of disparate registers for several |
| 22 | * types of device (ATA, serial, external interrupts). The normal |
| 23 | * resource management in the kernel doesn't have quite the right interfaces |
| 24 | * to handle this situation (e.g. multiple modules can't claim the same |
| 25 | * PCI ID), thus this IOC4 master module. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | */ |
| 27 | |
| 28 | #include <linux/errno.h> |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/pci.h> |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 31 | #include <linux/ioc4.h> |
Brent Casavant | 107d5a7 | 2006-10-17 00:09:24 -0700 | [diff] [blame] | 32 | #include <linux/ktime.h> |
Jes Sorensen | 6e586f3 | 2006-01-17 12:24:39 -0500 | [diff] [blame] | 33 | #include <linux/mutex.h> |
Brent Casavant | 107d5a7 | 2006-10-17 00:09:24 -0700 | [diff] [blame] | 34 | #include <linux/time.h> |
Al Viro | 2099c99 | 2006-10-24 11:17:06 +0100 | [diff] [blame] | 35 | #include <asm/io.h> |
Brent Casavant | d4c477c | 2005-06-21 17:16:01 -0700 | [diff] [blame] | 36 | |
| 37 | /*************** |
| 38 | * Definitions * |
| 39 | ***************/ |
| 40 | |
| 41 | /* Tweakable values */ |
| 42 | |
| 43 | /* PCI bus speed detection/calibration */ |
Brent Casavant | 107d5a7 | 2006-10-17 00:09:24 -0700 | [diff] [blame] | 44 | #define IOC4_CALIBRATE_COUNT 63 /* Calibration cycle period */ |
Brent Casavant | d4c477c | 2005-06-21 17:16:01 -0700 | [diff] [blame] | 45 | #define IOC4_CALIBRATE_CYCLES 256 /* Average over this many cycles */ |
| 46 | #define IOC4_CALIBRATE_DISCARD 2 /* Discard first few cycles */ |
| 47 | #define IOC4_CALIBRATE_LOW_MHZ 25 /* Lower bound on bus speed sanity */ |
| 48 | #define IOC4_CALIBRATE_HIGH_MHZ 75 /* Upper bound on bus speed sanity */ |
| 49 | #define IOC4_CALIBRATE_DEFAULT_MHZ 66 /* Assumed if sanity check fails */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 51 | /************************ |
| 52 | * Submodule management * |
| 53 | ************************/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Jes Sorensen | 6e586f3 | 2006-01-17 12:24:39 -0500 | [diff] [blame] | 55 | static DEFINE_MUTEX(ioc4_mutex); |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 56 | |
Jes Sorensen | 6e586f3 | 2006-01-17 12:24:39 -0500 | [diff] [blame] | 57 | static LIST_HEAD(ioc4_devices); |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 58 | static LIST_HEAD(ioc4_submodules); |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 59 | |
| 60 | /* Register an IOC4 submodule */ |
| 61 | int |
| 62 | ioc4_register_submodule(struct ioc4_submodule *is) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 64 | struct ioc4_driver_data *idd; |
| 65 | |
Jes Sorensen | 6e586f3 | 2006-01-17 12:24:39 -0500 | [diff] [blame] | 66 | mutex_lock(&ioc4_mutex); |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 67 | list_add(&is->is_list, &ioc4_submodules); |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 68 | |
| 69 | /* Initialize submodule for each IOC4 */ |
| 70 | if (!is->is_probe) |
Jes Sorensen | 6e586f3 | 2006-01-17 12:24:39 -0500 | [diff] [blame] | 71 | goto out; |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 72 | |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 73 | list_for_each_entry(idd, &ioc4_devices, idd_list) { |
| 74 | if (is->is_probe(idd)) { |
| 75 | printk(KERN_WARNING |
| 76 | "%s: IOC4 submodule %s probe failed " |
| 77 | "for pci_dev %s", |
Harvey Harrison | 6e57419 | 2008-04-29 00:59:20 -0700 | [diff] [blame] | 78 | __func__, module_name(is->is_owner), |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 79 | pci_name(idd->idd_pdev)); |
| 80 | } |
| 81 | } |
Jes Sorensen | 6e586f3 | 2006-01-17 12:24:39 -0500 | [diff] [blame] | 82 | out: |
| 83 | mutex_unlock(&ioc4_mutex); |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | /* Unregister an IOC4 submodule */ |
| 88 | void |
| 89 | ioc4_unregister_submodule(struct ioc4_submodule *is) |
| 90 | { |
| 91 | struct ioc4_driver_data *idd; |
| 92 | |
Jes Sorensen | 6e586f3 | 2006-01-17 12:24:39 -0500 | [diff] [blame] | 93 | mutex_lock(&ioc4_mutex); |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 94 | list_del(&is->is_list); |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 95 | |
| 96 | /* Remove submodule for each IOC4 */ |
| 97 | if (!is->is_remove) |
Jes Sorensen | 6e586f3 | 2006-01-17 12:24:39 -0500 | [diff] [blame] | 98 | goto out; |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 99 | |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 100 | list_for_each_entry(idd, &ioc4_devices, idd_list) { |
| 101 | if (is->is_remove(idd)) { |
| 102 | printk(KERN_WARNING |
| 103 | "%s: IOC4 submodule %s remove failed " |
| 104 | "for pci_dev %s.\n", |
Harvey Harrison | 6e57419 | 2008-04-29 00:59:20 -0700 | [diff] [blame] | 105 | __func__, module_name(is->is_owner), |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 106 | pci_name(idd->idd_pdev)); |
| 107 | } |
| 108 | } |
Jes Sorensen | 6e586f3 | 2006-01-17 12:24:39 -0500 | [diff] [blame] | 109 | out: |
| 110 | mutex_unlock(&ioc4_mutex); |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /********************* |
| 114 | * Device management * |
| 115 | *********************/ |
| 116 | |
Brent Casavant | d4c477c | 2005-06-21 17:16:01 -0700 | [diff] [blame] | 117 | #define IOC4_CALIBRATE_LOW_LIMIT \ |
| 118 | (1000*IOC4_EXTINT_COUNT_DIVISOR/IOC4_CALIBRATE_LOW_MHZ) |
| 119 | #define IOC4_CALIBRATE_HIGH_LIMIT \ |
| 120 | (1000*IOC4_EXTINT_COUNT_DIVISOR/IOC4_CALIBRATE_HIGH_MHZ) |
| 121 | #define IOC4_CALIBRATE_DEFAULT \ |
| 122 | (1000*IOC4_EXTINT_COUNT_DIVISOR/IOC4_CALIBRATE_DEFAULT_MHZ) |
| 123 | |
| 124 | #define IOC4_CALIBRATE_END \ |
| 125 | (IOC4_CALIBRATE_CYCLES + IOC4_CALIBRATE_DISCARD) |
| 126 | |
| 127 | #define IOC4_INT_OUT_MODE_TOGGLE 0x7 /* Toggle INT_OUT every COUNT+1 ticks */ |
| 128 | |
| 129 | /* Determines external interrupt output clock period of the PCI bus an |
| 130 | * IOC4 is attached to. This value can be used to determine the PCI |
| 131 | * bus speed. |
| 132 | * |
| 133 | * IOC4 has a design feature that various internal timers are derived from |
| 134 | * the PCI bus clock. This causes IOC4 device drivers to need to take the |
| 135 | * bus speed into account when setting various register values (e.g. INT_OUT |
| 136 | * register COUNT field, UART divisors, etc). Since this information is |
| 137 | * needed by several subdrivers, it is determined by the main IOC4 driver, |
| 138 | * even though the following code utilizes external interrupt registers |
| 139 | * to perform the speed calculation. |
| 140 | */ |
Jean Delvare | 2ea5d35 | 2009-12-14 18:00:27 -0800 | [diff] [blame] | 141 | static void __devinit |
Brent Casavant | d4c477c | 2005-06-21 17:16:01 -0700 | [diff] [blame] | 142 | ioc4_clock_calibrate(struct ioc4_driver_data *idd) |
| 143 | { |
Brent Casavant | d4c477c | 2005-06-21 17:16:01 -0700 | [diff] [blame] | 144 | union ioc4_int_out int_out; |
| 145 | union ioc4_gpcr gpcr; |
| 146 | unsigned int state, last_state = 1; |
Brent Casavant | 107d5a7 | 2006-10-17 00:09:24 -0700 | [diff] [blame] | 147 | struct timespec start_ts, end_ts; |
| 148 | uint64_t start, end, period; |
Brent Casavant | d4c477c | 2005-06-21 17:16:01 -0700 | [diff] [blame] | 149 | unsigned int count = 0; |
| 150 | |
| 151 | /* Enable output */ |
| 152 | gpcr.raw = 0; |
| 153 | gpcr.fields.dir = IOC4_GPCR_DIR_0; |
| 154 | gpcr.fields.int_out_en = 1; |
| 155 | writel(gpcr.raw, &idd->idd_misc_regs->gpcr_s.raw); |
| 156 | |
| 157 | /* Reset to power-on state */ |
| 158 | writel(0, &idd->idd_misc_regs->int_out.raw); |
| 159 | mmiowb(); |
| 160 | |
Brent Casavant | d4c477c | 2005-06-21 17:16:01 -0700 | [diff] [blame] | 161 | /* Set up square wave */ |
| 162 | int_out.raw = 0; |
| 163 | int_out.fields.count = IOC4_CALIBRATE_COUNT; |
| 164 | int_out.fields.mode = IOC4_INT_OUT_MODE_TOGGLE; |
| 165 | int_out.fields.diag = 0; |
| 166 | writel(int_out.raw, &idd->idd_misc_regs->int_out.raw); |
| 167 | mmiowb(); |
| 168 | |
| 169 | /* Check square wave period averaged over some number of cycles */ |
| 170 | do { |
| 171 | int_out.raw = readl(&idd->idd_misc_regs->int_out.raw); |
| 172 | state = int_out.fields.int_out; |
| 173 | if (!last_state && state) { |
| 174 | count++; |
| 175 | if (count == IOC4_CALIBRATE_END) { |
Brent Casavant | 107d5a7 | 2006-10-17 00:09:24 -0700 | [diff] [blame] | 176 | ktime_get_ts(&end_ts); |
Brent Casavant | d4c477c | 2005-06-21 17:16:01 -0700 | [diff] [blame] | 177 | break; |
| 178 | } else if (count == IOC4_CALIBRATE_DISCARD) |
Brent Casavant | 107d5a7 | 2006-10-17 00:09:24 -0700 | [diff] [blame] | 179 | ktime_get_ts(&start_ts); |
Brent Casavant | d4c477c | 2005-06-21 17:16:01 -0700 | [diff] [blame] | 180 | } |
| 181 | last_state = state; |
| 182 | } while (1); |
| 183 | |
| 184 | /* Calculation rearranged to preserve intermediate precision. |
| 185 | * Logically: |
Brent Casavant | 107d5a7 | 2006-10-17 00:09:24 -0700 | [diff] [blame] | 186 | * 1. "end - start" gives us the measurement period over all |
| 187 | * the square wave cycles. |
| 188 | * 2. Divide by number of square wave cycles to get the period |
| 189 | * of a square wave cycle. |
Brent Casavant | d4c477c | 2005-06-21 17:16:01 -0700 | [diff] [blame] | 190 | * 3. Divide by 2*(int_out.fields.count+1), which is the formula |
| 191 | * by which the IOC4 generates the square wave, to get the |
Brent Casavant | 107d5a7 | 2006-10-17 00:09:24 -0700 | [diff] [blame] | 192 | * period of an IOC4 INT_OUT count. |
Brent Casavant | d4c477c | 2005-06-21 17:16:01 -0700 | [diff] [blame] | 193 | */ |
Brent Casavant | 107d5a7 | 2006-10-17 00:09:24 -0700 | [diff] [blame] | 194 | end = end_ts.tv_sec * NSEC_PER_SEC + end_ts.tv_nsec; |
| 195 | start = start_ts.tv_sec * NSEC_PER_SEC + start_ts.tv_nsec; |
| 196 | period = (end - start) / |
| 197 | (IOC4_CALIBRATE_CYCLES * 2 * (IOC4_CALIBRATE_COUNT + 1)); |
Brent Casavant | d4c477c | 2005-06-21 17:16:01 -0700 | [diff] [blame] | 198 | |
| 199 | /* Bounds check the result. */ |
| 200 | if (period > IOC4_CALIBRATE_LOW_LIMIT || |
| 201 | period < IOC4_CALIBRATE_HIGH_LIMIT) { |
Brent Casavant | f5befce | 2006-06-23 02:05:52 -0700 | [diff] [blame] | 202 | printk(KERN_INFO |
| 203 | "IOC4 %s: Clock calibration failed. Assuming" |
| 204 | "PCI clock is %d ns.\n", |
| 205 | pci_name(idd->idd_pdev), |
Brent Casavant | d4c477c | 2005-06-21 17:16:01 -0700 | [diff] [blame] | 206 | IOC4_CALIBRATE_DEFAULT / IOC4_EXTINT_COUNT_DIVISOR); |
| 207 | period = IOC4_CALIBRATE_DEFAULT; |
| 208 | } else { |
Brent Casavant | 59f1480 | 2006-10-17 00:09:25 -0700 | [diff] [blame] | 209 | u64 ns = period; |
| 210 | |
| 211 | do_div(ns, IOC4_EXTINT_COUNT_DIVISOR); |
Brent Casavant | f5befce | 2006-06-23 02:05:52 -0700 | [diff] [blame] | 212 | printk(KERN_DEBUG |
Randy Dunlap | 760fe9a | 2006-10-28 10:38:39 -0700 | [diff] [blame] | 213 | "IOC4 %s: PCI clock is %llu ns.\n", |
| 214 | pci_name(idd->idd_pdev), (unsigned long long)ns); |
Brent Casavant | d4c477c | 2005-06-21 17:16:01 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /* Remember results. We store the extint clock period rather |
| 218 | * than the PCI clock period so that greater precision is |
| 219 | * retained. Divide by IOC4_EXTINT_COUNT_DIVISOR to get |
| 220 | * PCI clock period. |
| 221 | */ |
| 222 | idd->count_period = period; |
| 223 | } |
| 224 | |
Brent Casavant | f5befce | 2006-06-23 02:05:52 -0700 | [diff] [blame] | 225 | /* There are three variants of IOC4 cards: IO9, IO10, and PCI-RT. |
| 226 | * Each brings out different combinations of IOC4 signals, thus. |
| 227 | * the IOC4 subdrivers need to know to which we're attached. |
| 228 | * |
| 229 | * We look for the presence of a SCSI (IO9) or SATA (IO10) controller |
| 230 | * on the same PCI bus at slot number 3 to differentiate IO9 from IO10. |
| 231 | * If neither is present, it's a PCI-RT. |
| 232 | */ |
Jean Delvare | 2ea5d35 | 2009-12-14 18:00:27 -0800 | [diff] [blame] | 233 | static unsigned int __devinit |
Brent Casavant | f5befce | 2006-06-23 02:05:52 -0700 | [diff] [blame] | 234 | ioc4_variant(struct ioc4_driver_data *idd) |
| 235 | { |
| 236 | struct pci_dev *pdev = NULL; |
| 237 | int found = 0; |
| 238 | |
| 239 | /* IO9: Look for a QLogic ISP 12160 at the same bus and slot 3. */ |
| 240 | do { |
| 241 | pdev = pci_get_device(PCI_VENDOR_ID_QLOGIC, |
| 242 | PCI_DEVICE_ID_QLOGIC_ISP12160, pdev); |
| 243 | if (pdev && |
| 244 | idd->idd_pdev->bus->number == pdev->bus->number && |
| 245 | 3 == PCI_SLOT(pdev->devfn)) |
| 246 | found = 1; |
Brent Casavant | f5befce | 2006-06-23 02:05:52 -0700 | [diff] [blame] | 247 | } while (pdev && !found); |
Julia Lawall | 90d8dab | 2007-11-14 16:59:26 -0800 | [diff] [blame] | 248 | if (NULL != pdev) { |
| 249 | pci_dev_put(pdev); |
Brent Casavant | f5befce | 2006-06-23 02:05:52 -0700 | [diff] [blame] | 250 | return IOC4_VARIANT_IO9; |
Julia Lawall | 90d8dab | 2007-11-14 16:59:26 -0800 | [diff] [blame] | 251 | } |
Brent Casavant | f5befce | 2006-06-23 02:05:52 -0700 | [diff] [blame] | 252 | |
| 253 | /* IO10: Look for a Vitesse VSC 7174 at the same bus and slot 3. */ |
| 254 | pdev = NULL; |
| 255 | do { |
| 256 | pdev = pci_get_device(PCI_VENDOR_ID_VITESSE, |
| 257 | PCI_DEVICE_ID_VITESSE_VSC7174, pdev); |
| 258 | if (pdev && |
| 259 | idd->idd_pdev->bus->number == pdev->bus->number && |
| 260 | 3 == PCI_SLOT(pdev->devfn)) |
| 261 | found = 1; |
Brent Casavant | f5befce | 2006-06-23 02:05:52 -0700 | [diff] [blame] | 262 | } while (pdev && !found); |
Julia Lawall | 90d8dab | 2007-11-14 16:59:26 -0800 | [diff] [blame] | 263 | if (NULL != pdev) { |
| 264 | pci_dev_put(pdev); |
Brent Casavant | f5befce | 2006-06-23 02:05:52 -0700 | [diff] [blame] | 265 | return IOC4_VARIANT_IO10; |
Julia Lawall | 90d8dab | 2007-11-14 16:59:26 -0800 | [diff] [blame] | 266 | } |
Brent Casavant | f5befce | 2006-06-23 02:05:52 -0700 | [diff] [blame] | 267 | |
| 268 | /* PCI-RT: No SCSI/SATA controller will be present */ |
| 269 | return IOC4_VARIANT_PCI_RT; |
| 270 | } |
| 271 | |
Jean Delvare | 2ea5d35 | 2009-12-14 18:00:27 -0800 | [diff] [blame] | 272 | static void __devinit |
Brent Casavant | 08adefd | 2009-01-06 14:41:06 -0800 | [diff] [blame] | 273 | ioc4_load_modules(struct work_struct *work) |
| 274 | { |
| 275 | /* arg just has to be freed */ |
| 276 | |
| 277 | request_module("sgiioc4"); |
| 278 | |
| 279 | kfree(work); |
| 280 | } |
| 281 | |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 282 | /* Adds a new instance of an IOC4 card */ |
Jean Delvare | 2ea5d35 | 2009-12-14 18:00:27 -0800 | [diff] [blame] | 283 | static int __devinit |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 284 | ioc4_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id) |
| 285 | { |
| 286 | struct ioc4_driver_data *idd; |
| 287 | struct ioc4_submodule *is; |
| 288 | uint32_t pcmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | int ret; |
| 290 | |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 291 | /* Enable IOC4 and take ownership of it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | if ((ret = pci_enable_device(pdev))) { |
| 293 | printk(KERN_WARNING |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 294 | "%s: Failed to enable IOC4 device for pci_dev %s.\n", |
Harvey Harrison | 6e57419 | 2008-04-29 00:59:20 -0700 | [diff] [blame] | 295 | __func__, pci_name(pdev)); |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 296 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
| 298 | pci_set_master(pdev); |
| 299 | |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 300 | /* Set up per-IOC4 data */ |
| 301 | idd = kmalloc(sizeof(struct ioc4_driver_data), GFP_KERNEL); |
| 302 | if (!idd) { |
| 303 | printk(KERN_WARNING |
| 304 | "%s: Failed to allocate IOC4 data for pci_dev %s.\n", |
Harvey Harrison | 6e57419 | 2008-04-29 00:59:20 -0700 | [diff] [blame] | 305 | __func__, pci_name(pdev)); |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 306 | ret = -ENODEV; |
| 307 | goto out_idd; |
| 308 | } |
| 309 | idd->idd_pdev = pdev; |
| 310 | idd->idd_pci_id = pci_id; |
| 311 | |
| 312 | /* Map IOC4 misc registers. These are shared between subdevices |
| 313 | * so the main IOC4 module manages them. |
| 314 | */ |
| 315 | idd->idd_bar0 = pci_resource_start(idd->idd_pdev, 0); |
| 316 | if (!idd->idd_bar0) { |
| 317 | printk(KERN_WARNING |
| 318 | "%s: Unable to find IOC4 misc resource " |
| 319 | "for pci_dev %s.\n", |
Harvey Harrison | 6e57419 | 2008-04-29 00:59:20 -0700 | [diff] [blame] | 320 | __func__, pci_name(idd->idd_pdev)); |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 321 | ret = -ENODEV; |
| 322 | goto out_pci; |
| 323 | } |
Brent Casavant | 52c9ae0 | 2007-02-10 01:44:46 -0800 | [diff] [blame] | 324 | if (!request_mem_region(idd->idd_bar0, sizeof(struct ioc4_misc_regs), |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 325 | "ioc4_misc")) { |
| 326 | printk(KERN_WARNING |
| 327 | "%s: Unable to request IOC4 misc region " |
| 328 | "for pci_dev %s.\n", |
Harvey Harrison | 6e57419 | 2008-04-29 00:59:20 -0700 | [diff] [blame] | 329 | __func__, pci_name(idd->idd_pdev)); |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 330 | ret = -ENODEV; |
| 331 | goto out_pci; |
| 332 | } |
| 333 | idd->idd_misc_regs = ioremap(idd->idd_bar0, |
| 334 | sizeof(struct ioc4_misc_regs)); |
| 335 | if (!idd->idd_misc_regs) { |
| 336 | printk(KERN_WARNING |
| 337 | "%s: Unable to remap IOC4 misc region " |
| 338 | "for pci_dev %s.\n", |
Harvey Harrison | 6e57419 | 2008-04-29 00:59:20 -0700 | [diff] [blame] | 339 | __func__, pci_name(idd->idd_pdev)); |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 340 | ret = -ENODEV; |
| 341 | goto out_misc_region; |
| 342 | } |
| 343 | |
| 344 | /* Failsafe portion of per-IOC4 initialization */ |
| 345 | |
Brent Casavant | f5befce | 2006-06-23 02:05:52 -0700 | [diff] [blame] | 346 | /* Detect card variant */ |
| 347 | idd->idd_variant = ioc4_variant(idd); |
| 348 | printk(KERN_INFO "IOC4 %s: %s card detected.\n", pci_name(pdev), |
| 349 | idd->idd_variant == IOC4_VARIANT_IO9 ? "IO9" : |
| 350 | idd->idd_variant == IOC4_VARIANT_PCI_RT ? "PCI-RT" : |
| 351 | idd->idd_variant == IOC4_VARIANT_IO10 ? "IO10" : "unknown"); |
| 352 | |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 353 | /* Initialize IOC4 */ |
| 354 | pci_read_config_dword(idd->idd_pdev, PCI_COMMAND, &pcmd); |
| 355 | pci_write_config_dword(idd->idd_pdev, PCI_COMMAND, |
| 356 | pcmd | PCI_COMMAND_PARITY | PCI_COMMAND_SERR); |
| 357 | |
Brent Casavant | d4c477c | 2005-06-21 17:16:01 -0700 | [diff] [blame] | 358 | /* Determine PCI clock */ |
| 359 | ioc4_clock_calibrate(idd); |
| 360 | |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 361 | /* Disable/clear all interrupts. Need to do this here lest |
| 362 | * one submodule request the shared IOC4 IRQ, but interrupt |
| 363 | * is generated by a different subdevice. |
| 364 | */ |
| 365 | /* Disable */ |
| 366 | writel(~0, &idd->idd_misc_regs->other_iec.raw); |
| 367 | writel(~0, &idd->idd_misc_regs->sio_iec); |
| 368 | /* Clear (i.e. acknowledge) */ |
| 369 | writel(~0, &idd->idd_misc_regs->other_ir.raw); |
| 370 | writel(~0, &idd->idd_misc_regs->sio_ir); |
| 371 | |
| 372 | /* Track PCI-device specific data */ |
| 373 | idd->idd_serial_data = NULL; |
| 374 | pci_set_drvdata(idd->idd_pdev, idd); |
Jes Sorensen | 6e586f3 | 2006-01-17 12:24:39 -0500 | [diff] [blame] | 375 | |
| 376 | mutex_lock(&ioc4_mutex); |
Brent Casavant | 8683dc9 | 2006-05-03 19:55:10 -0700 | [diff] [blame] | 377 | list_add_tail(&idd->idd_list, &ioc4_devices); |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 378 | |
| 379 | /* Add this IOC4 to all submodules */ |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 380 | list_for_each_entry(is, &ioc4_submodules, is_list) { |
| 381 | if (is->is_probe && is->is_probe(idd)) { |
| 382 | printk(KERN_WARNING |
| 383 | "%s: IOC4 submodule 0x%s probe failed " |
| 384 | "for pci_dev %s.\n", |
Harvey Harrison | 6e57419 | 2008-04-29 00:59:20 -0700 | [diff] [blame] | 385 | __func__, module_name(is->is_owner), |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 386 | pci_name(idd->idd_pdev)); |
| 387 | } |
| 388 | } |
Jes Sorensen | 6e586f3 | 2006-01-17 12:24:39 -0500 | [diff] [blame] | 389 | mutex_unlock(&ioc4_mutex); |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 390 | |
Brent Casavant | 08adefd | 2009-01-06 14:41:06 -0800 | [diff] [blame] | 391 | /* Request sgiioc4 IDE driver on boards that bring that functionality |
| 392 | * off of IOC4. The root filesystem may be hosted on a drive connected |
| 393 | * to IOC4, so we need to make sure the sgiioc4 driver is loaded as it |
| 394 | * won't be picked up by modprobes due to the ioc4 module owning the |
| 395 | * PCI device. |
| 396 | */ |
| 397 | if (idd->idd_variant != IOC4_VARIANT_PCI_RT) { |
| 398 | struct work_struct *work; |
| 399 | work = kzalloc(sizeof(struct work_struct), GFP_KERNEL); |
| 400 | if (!work) { |
| 401 | printk(KERN_WARNING |
| 402 | "%s: IOC4 unable to allocate memory for " |
| 403 | "load of sub-modules.\n", __func__); |
| 404 | } else { |
| 405 | /* Request the module from a work procedure as the |
| 406 | * modprobe goes out to a userland helper and that |
| 407 | * will hang if done directly from ioc4_probe(). |
| 408 | */ |
| 409 | printk(KERN_INFO "IOC4 loading sgiioc4 submodule\n"); |
| 410 | INIT_WORK(work, ioc4_load_modules); |
| 411 | schedule_work(work); |
| 412 | } |
| 413 | } |
| 414 | |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 415 | return 0; |
| 416 | |
| 417 | out_misc_region: |
Brent Casavant | 52c9ae0 | 2007-02-10 01:44:46 -0800 | [diff] [blame] | 418 | release_mem_region(idd->idd_bar0, sizeof(struct ioc4_misc_regs)); |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 419 | out_pci: |
| 420 | kfree(idd); |
| 421 | out_idd: |
| 422 | pci_disable_device(pdev); |
| 423 | out: |
| 424 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 427 | /* Removes a particular instance of an IOC4 card. */ |
Jean Delvare | 2ea5d35 | 2009-12-14 18:00:27 -0800 | [diff] [blame] | 428 | static void __devexit |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 429 | ioc4_remove(struct pci_dev *pdev) |
| 430 | { |
| 431 | struct ioc4_submodule *is; |
| 432 | struct ioc4_driver_data *idd; |
| 433 | |
| 434 | idd = pci_get_drvdata(pdev); |
| 435 | |
| 436 | /* Remove this IOC4 from all submodules */ |
Jes Sorensen | 6e586f3 | 2006-01-17 12:24:39 -0500 | [diff] [blame] | 437 | mutex_lock(&ioc4_mutex); |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 438 | list_for_each_entry(is, &ioc4_submodules, is_list) { |
| 439 | if (is->is_remove && is->is_remove(idd)) { |
| 440 | printk(KERN_WARNING |
| 441 | "%s: IOC4 submodule 0x%s remove failed " |
| 442 | "for pci_dev %s.\n", |
Harvey Harrison | 6e57419 | 2008-04-29 00:59:20 -0700 | [diff] [blame] | 443 | __func__, module_name(is->is_owner), |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 444 | pci_name(idd->idd_pdev)); |
| 445 | } |
| 446 | } |
Jes Sorensen | 6e586f3 | 2006-01-17 12:24:39 -0500 | [diff] [blame] | 447 | mutex_unlock(&ioc4_mutex); |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 448 | |
| 449 | /* Release resources */ |
| 450 | iounmap(idd->idd_misc_regs); |
| 451 | if (!idd->idd_bar0) { |
| 452 | printk(KERN_WARNING |
| 453 | "%s: Unable to get IOC4 misc mapping for pci_dev %s. " |
| 454 | "Device removal may be incomplete.\n", |
Harvey Harrison | 6e57419 | 2008-04-29 00:59:20 -0700 | [diff] [blame] | 455 | __func__, pci_name(idd->idd_pdev)); |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 456 | } |
Brent Casavant | 52c9ae0 | 2007-02-10 01:44:46 -0800 | [diff] [blame] | 457 | release_mem_region(idd->idd_bar0, sizeof(struct ioc4_misc_regs)); |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 458 | |
| 459 | /* Disable IOC4 and relinquish */ |
| 460 | pci_disable_device(pdev); |
| 461 | |
| 462 | /* Remove and free driver data */ |
Jes Sorensen | 6e586f3 | 2006-01-17 12:24:39 -0500 | [diff] [blame] | 463 | mutex_lock(&ioc4_mutex); |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 464 | list_del(&idd->idd_list); |
Jes Sorensen | 6e586f3 | 2006-01-17 12:24:39 -0500 | [diff] [blame] | 465 | mutex_unlock(&ioc4_mutex); |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 466 | kfree(idd); |
| 467 | } |
| 468 | |
| 469 | static struct pci_device_id ioc4_id_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | {PCI_VENDOR_ID_SGI, PCI_DEVICE_ID_SGI_IOC4, PCI_ANY_ID, |
| 471 | PCI_ANY_ID, 0x0b4000, 0xFFFFFF}, |
| 472 | {0} |
| 473 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
Dave Jones | 85bd843 | 2006-06-29 02:24:31 -0700 | [diff] [blame] | 475 | static struct pci_driver ioc4_driver = { |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 476 | .name = "IOC4", |
| 477 | .id_table = ioc4_id_table, |
| 478 | .probe = ioc4_probe, |
Jean Delvare | 2ea5d35 | 2009-12-14 18:00:27 -0800 | [diff] [blame] | 479 | .remove = __devexit_p(ioc4_remove), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | }; |
| 481 | |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 482 | MODULE_DEVICE_TABLE(pci, ioc4_id_table); |
| 483 | |
| 484 | /********************* |
| 485 | * Module management * |
| 486 | *********************/ |
| 487 | |
| 488 | /* Module load */ |
Jean Delvare | 2ea5d35 | 2009-12-14 18:00:27 -0800 | [diff] [blame] | 489 | static int __init |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 490 | ioc4_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | { |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 492 | return pci_register_driver(&ioc4_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 495 | /* Module unload */ |
Jean Delvare | 2ea5d35 | 2009-12-14 18:00:27 -0800 | [diff] [blame] | 496 | static void __exit |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 497 | ioc4_exit(void) |
| 498 | { |
Brent Casavant | 08adefd | 2009-01-06 14:41:06 -0800 | [diff] [blame] | 499 | /* Ensure ioc4_load_modules() has completed before exiting */ |
| 500 | flush_scheduled_work(); |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 501 | pci_unregister_driver(&ioc4_driver); |
| 502 | } |
| 503 | |
| 504 | module_init(ioc4_init); |
| 505 | module_exit(ioc4_exit); |
| 506 | |
| 507 | MODULE_AUTHOR("Brent Casavant - Silicon Graphics, Inc. <bcasavan@sgi.com>"); |
| 508 | MODULE_DESCRIPTION("PCI driver master module for SGI IOC4 Base-IO Card"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | MODULE_LICENSE("GPL"); |
Brent Casavant | 22329b5 | 2005-06-21 17:15:59 -0700 | [diff] [blame] | 510 | |
| 511 | EXPORT_SYMBOL(ioc4_register_submodule); |
| 512 | EXPORT_SYMBOL(ioc4_unregister_submodule); |