Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/s390/cio/device.c |
| 3 | * bus driver for ccw devices |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH, |
| 6 | * IBM Corporation |
| 7 | * Author(s): Arnd Bergmann (arndb@de.ibm.com) |
Cornelia Huck | 4ce3b30 | 2006-01-14 13:21:04 -0800 | [diff] [blame] | 8 | * Cornelia Huck (cornelia.huck@de.ibm.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * Martin Schwidefsky (schwidefsky@de.ibm.com) |
| 10 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/spinlock.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/list.h> |
| 18 | #include <linux/device.h> |
| 19 | #include <linux/workqueue.h> |
| 20 | |
| 21 | #include <asm/ccwdev.h> |
| 22 | #include <asm/cio.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 23 | #include <asm/param.h> /* HZ */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | #include "cio.h" |
Cornelia Huck | d7b5a4c | 2006-12-08 15:54:28 +0100 | [diff] [blame^] | 26 | #include "cio_debug.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "css.h" |
| 28 | #include "device.h" |
| 29 | #include "ioasm.h" |
| 30 | |
| 31 | /******************* bus type handling ***********************/ |
| 32 | |
| 33 | /* The Linux driver model distinguishes between a bus type and |
| 34 | * the bus itself. Of course we only have one channel |
| 35 | * subsystem driver and one channel system per machine, but |
| 36 | * we still use the abstraction. T.R. says it's a good idea. */ |
| 37 | static int |
| 38 | ccw_bus_match (struct device * dev, struct device_driver * drv) |
| 39 | { |
| 40 | struct ccw_device *cdev = to_ccwdev(dev); |
| 41 | struct ccw_driver *cdrv = to_ccwdrv(drv); |
| 42 | const struct ccw_device_id *ids = cdrv->ids, *found; |
| 43 | |
| 44 | if (!ids) |
| 45 | return 0; |
| 46 | |
| 47 | found = ccw_device_id_match(ids, &cdev->id); |
| 48 | if (!found) |
| 49 | return 0; |
| 50 | |
| 51 | cdev->id.driver_info = found->driver_info; |
| 52 | |
| 53 | return 1; |
| 54 | } |
| 55 | |
Peter Oberparleiter | db0c2d5 | 2006-09-20 15:59:49 +0200 | [diff] [blame] | 56 | /* Store modalias string delimited by prefix/suffix string into buffer with |
| 57 | * specified size. Return length of resulting string (excluding trailing '\0') |
| 58 | * even if string doesn't fit buffer (snprintf semantics). */ |
| 59 | static int snprint_alias(char *buf, size_t size, const char *prefix, |
| 60 | struct ccw_device_id *id, const char *suffix) |
| 61 | { |
| 62 | int len; |
| 63 | |
| 64 | len = snprintf(buf, size, "%sccw:t%04Xm%02X", prefix, id->cu_type, |
| 65 | id->cu_model); |
| 66 | if (len > size) |
| 67 | return len; |
| 68 | buf += len; |
| 69 | size -= len; |
| 70 | |
| 71 | if (id->dev_type != 0) |
| 72 | len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type, |
| 73 | id->dev_model, suffix); |
| 74 | else |
| 75 | len += snprintf(buf, size, "dtdm%s", suffix); |
| 76 | |
| 77 | return len; |
| 78 | } |
| 79 | |
| 80 | /* Set up environment variables for ccw device uevent. Return 0 on success, |
| 81 | * non-zero otherwise. */ |
| 82 | static int ccw_uevent(struct device *dev, char **envp, int num_envp, |
| 83 | char *buffer, int buffer_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { |
| 85 | struct ccw_device *cdev = to_ccwdev(dev); |
Peter Oberparleiter | db0c2d5 | 2006-09-20 15:59:49 +0200 | [diff] [blame] | 86 | struct ccw_device_id *id = &(cdev->id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | int i = 0; |
Peter Oberparleiter | db0c2d5 | 2006-09-20 15:59:49 +0200 | [diff] [blame] | 88 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
Peter Oberparleiter | db0c2d5 | 2006-09-20 15:59:49 +0200 | [diff] [blame] | 90 | /* CU_TYPE= */ |
| 91 | len = snprintf(buffer, buffer_size, "CU_TYPE=%04X", id->cu_type) + 1; |
| 92 | if (len > buffer_size || i >= num_envp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | envp[i++] = buffer; |
Peter Oberparleiter | db0c2d5 | 2006-09-20 15:59:49 +0200 | [diff] [blame] | 95 | buffer += len; |
| 96 | buffer_size -= len; |
| 97 | |
| 98 | /* CU_MODEL= */ |
| 99 | len = snprintf(buffer, buffer_size, "CU_MODEL=%02X", id->cu_model) + 1; |
| 100 | if (len > buffer_size || i >= num_envp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | return -ENOMEM; |
Peter Oberparleiter | db0c2d5 | 2006-09-20 15:59:49 +0200 | [diff] [blame] | 102 | envp[i++] = buffer; |
| 103 | buffer += len; |
| 104 | buffer_size -= len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
| 106 | /* The next two can be zero, that's ok for us */ |
Peter Oberparleiter | db0c2d5 | 2006-09-20 15:59:49 +0200 | [diff] [blame] | 107 | /* DEV_TYPE= */ |
| 108 | len = snprintf(buffer, buffer_size, "DEV_TYPE=%04X", id->dev_type) + 1; |
| 109 | if (len > buffer_size || i >= num_envp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | return -ENOMEM; |
Peter Oberparleiter | db0c2d5 | 2006-09-20 15:59:49 +0200 | [diff] [blame] | 111 | envp[i++] = buffer; |
| 112 | buffer += len; |
| 113 | buffer_size -= len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Peter Oberparleiter | db0c2d5 | 2006-09-20 15:59:49 +0200 | [diff] [blame] | 115 | /* DEV_MODEL= */ |
| 116 | len = snprintf(buffer, buffer_size, "DEV_MODEL=%02X", |
| 117 | (unsigned char) id->dev_model) + 1; |
| 118 | if (len > buffer_size || i >= num_envp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | return -ENOMEM; |
Peter Oberparleiter | db0c2d5 | 2006-09-20 15:59:49 +0200 | [diff] [blame] | 120 | envp[i++] = buffer; |
| 121 | buffer += len; |
| 122 | buffer_size -= len; |
| 123 | |
| 124 | /* MODALIAS= */ |
| 125 | len = snprint_alias(buffer, buffer_size, "MODALIAS=", id, "") + 1; |
| 126 | if (len > buffer_size || i >= num_envp) |
| 127 | return -ENOMEM; |
| 128 | envp[i++] = buffer; |
| 129 | buffer += len; |
| 130 | buffer_size -= len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 132 | envp[i] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
Cornelia Huck | 8bbace7 | 2006-01-11 10:56:22 +0100 | [diff] [blame] | 137 | struct bus_type ccw_bus_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Cornelia Huck | 8bbace7 | 2006-01-11 10:56:22 +0100 | [diff] [blame] | 139 | static int io_subchannel_probe (struct subchannel *); |
| 140 | static int io_subchannel_remove (struct subchannel *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | void io_subchannel_irq (struct device *); |
| 142 | static int io_subchannel_notify(struct device *, int); |
| 143 | static void io_subchannel_verify(struct device *); |
| 144 | static void io_subchannel_ioterm(struct device *); |
Cornelia Huck | 8bbace7 | 2006-01-11 10:56:22 +0100 | [diff] [blame] | 145 | static void io_subchannel_shutdown(struct subchannel *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | struct css_driver io_subchannel_driver = { |
| 148 | .subchannel_type = SUBCHANNEL_TYPE_IO, |
| 149 | .drv = { |
| 150 | .name = "io_subchannel", |
| 151 | .bus = &css_bus_type, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | }, |
| 153 | .irq = io_subchannel_irq, |
| 154 | .notify = io_subchannel_notify, |
| 155 | .verify = io_subchannel_verify, |
| 156 | .termination = io_subchannel_ioterm, |
Cornelia Huck | 8bbace7 | 2006-01-11 10:56:22 +0100 | [diff] [blame] | 157 | .probe = io_subchannel_probe, |
| 158 | .remove = io_subchannel_remove, |
| 159 | .shutdown = io_subchannel_shutdown, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | struct workqueue_struct *ccw_device_work; |
| 163 | struct workqueue_struct *ccw_device_notify_work; |
Peter Oberparleiter | 40154b8 | 2006-06-29 14:57:03 +0200 | [diff] [blame] | 164 | wait_queue_head_t ccw_device_init_wq; |
| 165 | atomic_t ccw_device_init_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
| 167 | static int __init |
| 168 | init_ccw_bus_type (void) |
| 169 | { |
| 170 | int ret; |
| 171 | |
| 172 | init_waitqueue_head(&ccw_device_init_wq); |
| 173 | atomic_set(&ccw_device_init_count, 0); |
| 174 | |
| 175 | ccw_device_work = create_singlethread_workqueue("cio"); |
| 176 | if (!ccw_device_work) |
| 177 | return -ENOMEM; /* FIXME: better errno ? */ |
| 178 | ccw_device_notify_work = create_singlethread_workqueue("cio_notify"); |
| 179 | if (!ccw_device_notify_work) { |
| 180 | ret = -ENOMEM; /* FIXME: better errno ? */ |
| 181 | goto out_err; |
| 182 | } |
| 183 | slow_path_wq = create_singlethread_workqueue("kslowcrw"); |
| 184 | if (!slow_path_wq) { |
| 185 | ret = -ENOMEM; /* FIXME: better errno ? */ |
| 186 | goto out_err; |
| 187 | } |
| 188 | if ((ret = bus_register (&ccw_bus_type))) |
| 189 | goto out_err; |
| 190 | |
| 191 | if ((ret = driver_register(&io_subchannel_driver.drv))) |
| 192 | goto out_err; |
| 193 | |
| 194 | wait_event(ccw_device_init_wq, |
| 195 | atomic_read(&ccw_device_init_count) == 0); |
| 196 | flush_workqueue(ccw_device_work); |
| 197 | return 0; |
| 198 | out_err: |
| 199 | if (ccw_device_work) |
| 200 | destroy_workqueue(ccw_device_work); |
| 201 | if (ccw_device_notify_work) |
| 202 | destroy_workqueue(ccw_device_notify_work); |
| 203 | if (slow_path_wq) |
| 204 | destroy_workqueue(slow_path_wq); |
| 205 | return ret; |
| 206 | } |
| 207 | |
| 208 | static void __exit |
| 209 | cleanup_ccw_bus_type (void) |
| 210 | { |
| 211 | driver_unregister(&io_subchannel_driver.drv); |
| 212 | bus_unregister(&ccw_bus_type); |
| 213 | destroy_workqueue(ccw_device_notify_work); |
| 214 | destroy_workqueue(ccw_device_work); |
| 215 | } |
| 216 | |
| 217 | subsys_initcall(init_ccw_bus_type); |
| 218 | module_exit(cleanup_ccw_bus_type); |
| 219 | |
| 220 | /************************ device handling **************************/ |
| 221 | |
| 222 | /* |
| 223 | * A ccw_device has some interfaces in sysfs in addition to the |
| 224 | * standard ones. |
| 225 | * The following entries are designed to export the information which |
| 226 | * resided in 2.4 in /proc/subchannels. Subchannel and device number |
| 227 | * are obvious, so they don't have an entry :) |
| 228 | * TODO: Split chpids and pimpampom up? Where is "in use" in the tree? |
| 229 | */ |
| 230 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 231 | chpids_show (struct device * dev, struct device_attribute *attr, char * buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | { |
| 233 | struct subchannel *sch = to_subchannel(dev); |
| 234 | struct ssd_info *ssd = &sch->ssd_info; |
| 235 | ssize_t ret = 0; |
| 236 | int chp; |
| 237 | |
| 238 | for (chp = 0; chp < 8; chp++) |
| 239 | ret += sprintf (buf+ret, "%02x ", ssd->chpid[chp]); |
| 240 | |
| 241 | ret += sprintf (buf+ret, "\n"); |
| 242 | return min((ssize_t)PAGE_SIZE, ret); |
| 243 | } |
| 244 | |
| 245 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 246 | pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | { |
| 248 | struct subchannel *sch = to_subchannel(dev); |
| 249 | struct pmcw *pmcw = &sch->schib.pmcw; |
| 250 | |
| 251 | return sprintf (buf, "%02x %02x %02x\n", |
| 252 | pmcw->pim, pmcw->pam, pmcw->pom); |
| 253 | } |
| 254 | |
| 255 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 256 | devtype_show (struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | { |
| 258 | struct ccw_device *cdev = to_ccwdev(dev); |
| 259 | struct ccw_device_id *id = &(cdev->id); |
| 260 | |
| 261 | if (id->dev_type != 0) |
| 262 | return sprintf(buf, "%04x/%02x\n", |
| 263 | id->dev_type, id->dev_model); |
| 264 | else |
| 265 | return sprintf(buf, "n/a\n"); |
| 266 | } |
| 267 | |
| 268 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 269 | cutype_show (struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | { |
| 271 | struct ccw_device *cdev = to_ccwdev(dev); |
| 272 | struct ccw_device_id *id = &(cdev->id); |
| 273 | |
| 274 | return sprintf(buf, "%04x/%02x\n", |
| 275 | id->cu_type, id->cu_model); |
| 276 | } |
| 277 | |
| 278 | static ssize_t |
Bastian Blank | f1fc78a | 2005-10-30 15:00:12 -0800 | [diff] [blame] | 279 | modalias_show (struct device *dev, struct device_attribute *attr, char *buf) |
| 280 | { |
| 281 | struct ccw_device *cdev = to_ccwdev(dev); |
| 282 | struct ccw_device_id *id = &(cdev->id); |
Peter Oberparleiter | db0c2d5 | 2006-09-20 15:59:49 +0200 | [diff] [blame] | 283 | int len; |
Bastian Blank | f1fc78a | 2005-10-30 15:00:12 -0800 | [diff] [blame] | 284 | |
Peter Oberparleiter | db0c2d5 | 2006-09-20 15:59:49 +0200 | [diff] [blame] | 285 | len = snprint_alias(buf, PAGE_SIZE, "", id, "\n") + 1; |
| 286 | |
| 287 | return len > PAGE_SIZE ? PAGE_SIZE : len; |
Bastian Blank | f1fc78a | 2005-10-30 15:00:12 -0800 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 291 | online_show (struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | { |
| 293 | struct ccw_device *cdev = to_ccwdev(dev); |
| 294 | |
| 295 | return sprintf(buf, cdev->online ? "1\n" : "0\n"); |
| 296 | } |
| 297 | |
Cornelia Huck | d7b5a4c | 2006-12-08 15:54:28 +0100 | [diff] [blame^] | 298 | int ccw_device_is_orphan(struct ccw_device *cdev) |
| 299 | { |
| 300 | return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent)); |
| 301 | } |
| 302 | |
Cornelia Huck | 7674da7 | 2006-12-08 15:54:21 +0100 | [diff] [blame] | 303 | static void ccw_device_unregister(struct work_struct *work) |
| 304 | { |
| 305 | struct ccw_device_private *priv; |
| 306 | struct ccw_device *cdev; |
| 307 | |
| 308 | priv = container_of(work, struct ccw_device_private, kick_work); |
| 309 | cdev = priv->cdev; |
| 310 | if (test_and_clear_bit(1, &cdev->private->registered)) |
| 311 | device_unregister(&cdev->dev); |
| 312 | put_device(&cdev->dev); |
| 313 | } |
| 314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | static void |
| 316 | ccw_device_remove_disconnected(struct ccw_device *cdev) |
| 317 | { |
| 318 | struct subchannel *sch; |
Cornelia Huck | d7b5a4c | 2006-12-08 15:54:28 +0100 | [diff] [blame^] | 319 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | /* |
| 321 | * Forced offline in disconnected state means |
| 322 | * 'throw away device'. |
| 323 | */ |
Cornelia Huck | d7b5a4c | 2006-12-08 15:54:28 +0100 | [diff] [blame^] | 324 | if (ccw_device_is_orphan(cdev)) { |
| 325 | /* Deregister ccw device. */ |
| 326 | spin_lock_irqsave(cdev->ccwlock, flags); |
| 327 | cdev->private->state = DEV_STATE_NOT_OPER; |
| 328 | spin_unlock_irqrestore(cdev->ccwlock, flags); |
| 329 | if (get_device(&cdev->dev)) { |
| 330 | PREPARE_WORK(&cdev->private->kick_work, |
| 331 | ccw_device_unregister); |
| 332 | queue_work(ccw_device_work, &cdev->private->kick_work); |
| 333 | } |
| 334 | return ; |
| 335 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | sch = to_subchannel(cdev->dev.parent); |
Cornelia Huck | 6ab4879 | 2006-07-12 16:39:50 +0200 | [diff] [blame] | 337 | css_sch_device_unregister(sch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | /* Reset intparm to zeroes. */ |
| 339 | sch->schib.pmcw.intparm = 0; |
| 340 | cio_modify(sch); |
| 341 | put_device(&sch->dev); |
| 342 | } |
| 343 | |
| 344 | int |
| 345 | ccw_device_set_offline(struct ccw_device *cdev) |
| 346 | { |
| 347 | int ret; |
| 348 | |
| 349 | if (!cdev) |
| 350 | return -ENODEV; |
| 351 | if (!cdev->online || !cdev->drv) |
| 352 | return -EINVAL; |
| 353 | |
| 354 | if (cdev->drv->set_offline) { |
| 355 | ret = cdev->drv->set_offline(cdev); |
| 356 | if (ret != 0) |
| 357 | return ret; |
| 358 | } |
| 359 | cdev->online = 0; |
| 360 | spin_lock_irq(cdev->ccwlock); |
| 361 | ret = ccw_device_offline(cdev); |
| 362 | if (ret == -ENODEV) { |
| 363 | if (cdev->private->state != DEV_STATE_NOT_OPER) { |
| 364 | cdev->private->state = DEV_STATE_OFFLINE; |
| 365 | dev_fsm_event(cdev, DEV_EVENT_NOTOPER); |
| 366 | } |
| 367 | spin_unlock_irq(cdev->ccwlock); |
| 368 | return ret; |
| 369 | } |
| 370 | spin_unlock_irq(cdev->ccwlock); |
| 371 | if (ret == 0) |
| 372 | wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev)); |
| 373 | else { |
| 374 | pr_debug("ccw_device_offline returned %d, device %s\n", |
| 375 | ret, cdev->dev.bus_id); |
| 376 | cdev->online = 1; |
| 377 | } |
| 378 | return ret; |
| 379 | } |
| 380 | |
| 381 | int |
| 382 | ccw_device_set_online(struct ccw_device *cdev) |
| 383 | { |
| 384 | int ret; |
| 385 | |
| 386 | if (!cdev) |
| 387 | return -ENODEV; |
| 388 | if (cdev->online || !cdev->drv) |
| 389 | return -EINVAL; |
| 390 | |
| 391 | spin_lock_irq(cdev->ccwlock); |
| 392 | ret = ccw_device_online(cdev); |
| 393 | spin_unlock_irq(cdev->ccwlock); |
| 394 | if (ret == 0) |
| 395 | wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev)); |
| 396 | else { |
| 397 | pr_debug("ccw_device_online returned %d, device %s\n", |
| 398 | ret, cdev->dev.bus_id); |
| 399 | return ret; |
| 400 | } |
| 401 | if (cdev->private->state != DEV_STATE_ONLINE) |
| 402 | return -ENODEV; |
| 403 | if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) { |
| 404 | cdev->online = 1; |
| 405 | return 0; |
| 406 | } |
| 407 | spin_lock_irq(cdev->ccwlock); |
| 408 | ret = ccw_device_offline(cdev); |
| 409 | spin_unlock_irq(cdev->ccwlock); |
| 410 | if (ret == 0) |
| 411 | wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev)); |
| 412 | else |
| 413 | pr_debug("ccw_device_offline returned %d, device %s\n", |
| 414 | ret, cdev->dev.bus_id); |
Cornelia Huck | 6cadb78 | 2006-02-17 13:52:49 -0800 | [diff] [blame] | 415 | return (ret == 0) ? -ENODEV : ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 419 | online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | { |
| 421 | struct ccw_device *cdev = to_ccwdev(dev); |
| 422 | int i, force, ret; |
| 423 | char *tmp; |
| 424 | |
Martin Schwidefsky | 973bd99 | 2006-01-06 00:19:07 -0800 | [diff] [blame] | 425 | if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | return -EAGAIN; |
| 427 | |
| 428 | if (cdev->drv && !try_module_get(cdev->drv->owner)) { |
| 429 | atomic_set(&cdev->private->onoff, 0); |
| 430 | return -EINVAL; |
| 431 | } |
| 432 | if (!strncmp(buf, "force\n", count)) { |
| 433 | force = 1; |
| 434 | i = 1; |
| 435 | } else { |
| 436 | force = 0; |
| 437 | i = simple_strtoul(buf, &tmp, 16); |
| 438 | } |
| 439 | if (i == 1) { |
| 440 | /* Do device recognition, if needed. */ |
| 441 | if (cdev->id.cu_type == 0) { |
| 442 | ret = ccw_device_recognition(cdev); |
| 443 | if (ret) { |
| 444 | printk(KERN_WARNING"Couldn't start recognition " |
| 445 | "for device %s (ret=%d)\n", |
| 446 | cdev->dev.bus_id, ret); |
| 447 | goto out; |
| 448 | } |
| 449 | wait_event(cdev->private->wait_q, |
| 450 | cdev->private->flags.recog_done); |
| 451 | } |
| 452 | if (cdev->drv && cdev->drv->set_online) |
| 453 | ccw_device_set_online(cdev); |
| 454 | } else if (i == 0) { |
| 455 | if (cdev->private->state == DEV_STATE_DISCONNECTED) |
| 456 | ccw_device_remove_disconnected(cdev); |
| 457 | else if (cdev->drv && cdev->drv->set_offline) |
| 458 | ccw_device_set_offline(cdev); |
| 459 | } |
| 460 | if (force && cdev->private->state == DEV_STATE_BOXED) { |
| 461 | ret = ccw_device_stlck(cdev); |
| 462 | if (ret) { |
| 463 | printk(KERN_WARNING"ccw_device_stlck for device %s " |
| 464 | "returned %d!\n", cdev->dev.bus_id, ret); |
| 465 | goto out; |
| 466 | } |
| 467 | /* Do device recognition, if needed. */ |
| 468 | if (cdev->id.cu_type == 0) { |
| 469 | cdev->private->state = DEV_STATE_NOT_OPER; |
| 470 | ret = ccw_device_recognition(cdev); |
| 471 | if (ret) { |
| 472 | printk(KERN_WARNING"Couldn't start recognition " |
| 473 | "for device %s (ret=%d)\n", |
| 474 | cdev->dev.bus_id, ret); |
| 475 | goto out; |
| 476 | } |
| 477 | wait_event(cdev->private->wait_q, |
| 478 | cdev->private->flags.recog_done); |
| 479 | } |
| 480 | if (cdev->drv && cdev->drv->set_online) |
| 481 | ccw_device_set_online(cdev); |
| 482 | } |
| 483 | out: |
| 484 | if (cdev->drv) |
| 485 | module_put(cdev->drv->owner); |
| 486 | atomic_set(&cdev->private->onoff, 0); |
| 487 | return count; |
| 488 | } |
| 489 | |
| 490 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 491 | available_show (struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | { |
| 493 | struct ccw_device *cdev = to_ccwdev(dev); |
| 494 | struct subchannel *sch; |
| 495 | |
Cornelia Huck | d7b5a4c | 2006-12-08 15:54:28 +0100 | [diff] [blame^] | 496 | if (ccw_device_is_orphan(cdev)) |
| 497 | return sprintf(buf, "no device\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | switch (cdev->private->state) { |
| 499 | case DEV_STATE_BOXED: |
| 500 | return sprintf(buf, "boxed\n"); |
| 501 | case DEV_STATE_DISCONNECTED: |
| 502 | case DEV_STATE_DISCONNECTED_SENSE_ID: |
| 503 | case DEV_STATE_NOT_OPER: |
| 504 | sch = to_subchannel(dev->parent); |
| 505 | if (!sch->lpm) |
| 506 | return sprintf(buf, "no path\n"); |
| 507 | else |
| 508 | return sprintf(buf, "no device\n"); |
| 509 | default: |
| 510 | /* All other states considered fine. */ |
| 511 | return sprintf(buf, "good\n"); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | static DEVICE_ATTR(chpids, 0444, chpids_show, NULL); |
| 516 | static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL); |
| 517 | static DEVICE_ATTR(devtype, 0444, devtype_show, NULL); |
| 518 | static DEVICE_ATTR(cutype, 0444, cutype_show, NULL); |
Bastian Blank | f1fc78a | 2005-10-30 15:00:12 -0800 | [diff] [blame] | 519 | static DEVICE_ATTR(modalias, 0444, modalias_show, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | static DEVICE_ATTR(online, 0644, online_show, online_store); |
| 521 | extern struct device_attribute dev_attr_cmb_enable; |
| 522 | static DEVICE_ATTR(availability, 0444, available_show, NULL); |
| 523 | |
| 524 | static struct attribute * subch_attrs[] = { |
| 525 | &dev_attr_chpids.attr, |
| 526 | &dev_attr_pimpampom.attr, |
| 527 | NULL, |
| 528 | }; |
| 529 | |
| 530 | static struct attribute_group subch_attr_group = { |
| 531 | .attrs = subch_attrs, |
| 532 | }; |
| 533 | |
Cornelia Huck | 7674da7 | 2006-12-08 15:54:21 +0100 | [diff] [blame] | 534 | int subchannel_add_files (struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | { |
| 536 | return sysfs_create_group(&dev->kobj, &subch_attr_group); |
| 537 | } |
| 538 | |
| 539 | static struct attribute * ccwdev_attrs[] = { |
| 540 | &dev_attr_devtype.attr, |
| 541 | &dev_attr_cutype.attr, |
Bastian Blank | f1fc78a | 2005-10-30 15:00:12 -0800 | [diff] [blame] | 542 | &dev_attr_modalias.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | &dev_attr_online.attr, |
| 544 | &dev_attr_cmb_enable.attr, |
| 545 | &dev_attr_availability.attr, |
| 546 | NULL, |
| 547 | }; |
| 548 | |
| 549 | static struct attribute_group ccwdev_attr_group = { |
| 550 | .attrs = ccwdev_attrs, |
| 551 | }; |
| 552 | |
| 553 | static inline int |
| 554 | device_add_files (struct device *dev) |
| 555 | { |
| 556 | return sysfs_create_group(&dev->kobj, &ccwdev_attr_group); |
| 557 | } |
| 558 | |
| 559 | static inline void |
| 560 | device_remove_files(struct device *dev) |
| 561 | { |
| 562 | sysfs_remove_group(&dev->kobj, &ccwdev_attr_group); |
| 563 | } |
| 564 | |
| 565 | /* this is a simple abstraction for device_register that sets the |
| 566 | * correct bus type and adds the bus specific files */ |
Cornelia Huck | 3c9da7b | 2006-10-27 12:39:33 +0200 | [diff] [blame] | 567 | static int ccw_device_register(struct ccw_device *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | { |
| 569 | struct device *dev = &cdev->dev; |
| 570 | int ret; |
| 571 | |
| 572 | dev->bus = &ccw_bus_type; |
| 573 | |
| 574 | if ((ret = device_add(dev))) |
| 575 | return ret; |
| 576 | |
| 577 | set_bit(1, &cdev->private->registered); |
| 578 | if ((ret = device_add_files(dev))) { |
| 579 | if (test_and_clear_bit(1, &cdev->private->registered)) |
| 580 | device_del(dev); |
| 581 | } |
| 582 | return ret; |
| 583 | } |
| 584 | |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 585 | struct match_data { |
Cornelia Huck | 7896426 | 2006-10-11 15:31:38 +0200 | [diff] [blame] | 586 | struct ccw_dev_id dev_id; |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 587 | struct ccw_device * sibling; |
| 588 | }; |
| 589 | |
| 590 | static int |
| 591 | match_devno(struct device * dev, void * data) |
| 592 | { |
Cornelia Huck | 7896426 | 2006-10-11 15:31:38 +0200 | [diff] [blame] | 593 | struct match_data * d = data; |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 594 | struct ccw_device * cdev; |
| 595 | |
| 596 | cdev = to_ccwdev(dev); |
| 597 | if ((cdev->private->state == DEV_STATE_DISCONNECTED) && |
Cornelia Huck | d7b5a4c | 2006-12-08 15:54:28 +0100 | [diff] [blame^] | 598 | !ccw_device_is_orphan(cdev) && |
Cornelia Huck | 7896426 | 2006-10-11 15:31:38 +0200 | [diff] [blame] | 599 | ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) && |
Cornelia Huck | d7b5a4c | 2006-12-08 15:54:28 +0100 | [diff] [blame^] | 600 | (cdev != d->sibling)) |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 601 | return 1; |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 602 | return 0; |
| 603 | } |
| 604 | |
Cornelia Huck | 7896426 | 2006-10-11 15:31:38 +0200 | [diff] [blame] | 605 | static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id, |
| 606 | struct ccw_device *sibling) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | struct device *dev; |
Heiko Carstens | 292888c | 2006-08-30 14:33:35 +0200 | [diff] [blame] | 609 | struct match_data data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
Cornelia Huck | 7896426 | 2006-10-11 15:31:38 +0200 | [diff] [blame] | 611 | data.dev_id = *dev_id; |
Heiko Carstens | 292888c | 2006-08-30 14:33:35 +0200 | [diff] [blame] | 612 | data.sibling = sibling; |
Cornelia Huck | e5945b4 | 2005-10-11 08:28:59 -0700 | [diff] [blame] | 613 | dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 615 | return dev ? to_ccwdev(dev) : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Cornelia Huck | d7b5a4c | 2006-12-08 15:54:28 +0100 | [diff] [blame^] | 618 | static int match_orphan(struct device *dev, void *data) |
| 619 | { |
| 620 | struct ccw_dev_id *dev_id; |
| 621 | struct ccw_device *cdev; |
| 622 | |
| 623 | dev_id = data; |
| 624 | cdev = to_ccwdev(dev); |
| 625 | return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id); |
| 626 | } |
| 627 | |
| 628 | static struct ccw_device * |
| 629 | get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css, |
| 630 | struct ccw_dev_id *dev_id) |
| 631 | { |
| 632 | struct device *dev; |
| 633 | |
| 634 | dev = device_find_child(&css->pseudo_subchannel->dev, dev_id, |
| 635 | match_orphan); |
| 636 | |
| 637 | return dev ? to_ccwdev(dev) : NULL; |
| 638 | } |
| 639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | static void |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 641 | ccw_device_add_changed(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | { |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 643 | struct ccw_device_private *priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | struct ccw_device *cdev; |
| 645 | |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 646 | priv = container_of(work, struct ccw_device_private, kick_work); |
| 647 | cdev = priv->cdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | if (device_add(&cdev->dev)) { |
| 649 | put_device(&cdev->dev); |
| 650 | return; |
| 651 | } |
| 652 | set_bit(1, &cdev->private->registered); |
| 653 | if (device_add_files(&cdev->dev)) { |
| 654 | if (test_and_clear_bit(1, &cdev->private->registered)) |
| 655 | device_unregister(&cdev->dev); |
| 656 | } |
| 657 | } |
| 658 | |
Cornelia Huck | d7b5a4c | 2006-12-08 15:54:28 +0100 | [diff] [blame^] | 659 | void ccw_device_do_unreg_rereg(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | { |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 661 | struct ccw_device_private *priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | struct ccw_device *cdev; |
| 663 | struct subchannel *sch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 665 | priv = container_of(work, struct ccw_device_private, kick_work); |
| 666 | cdev = priv->cdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | sch = to_subchannel(cdev->dev.parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | device_remove_files(&cdev->dev); |
| 670 | if (test_and_clear_bit(1, &cdev->private->registered)) |
| 671 | device_del(&cdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | PREPARE_WORK(&cdev->private->kick_work, |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 673 | ccw_device_add_changed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | queue_work(ccw_device_work, &cdev->private->kick_work); |
| 675 | } |
| 676 | |
| 677 | static void |
| 678 | ccw_device_release(struct device *dev) |
| 679 | { |
| 680 | struct ccw_device *cdev; |
| 681 | |
| 682 | cdev = to_ccwdev(dev); |
| 683 | kfree(cdev->private); |
| 684 | kfree(cdev); |
| 685 | } |
| 686 | |
Cornelia Huck | 7674da7 | 2006-12-08 15:54:21 +0100 | [diff] [blame] | 687 | static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch) |
| 688 | { |
| 689 | struct ccw_device *cdev; |
| 690 | |
| 691 | cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); |
| 692 | if (cdev) { |
| 693 | cdev->private = kzalloc(sizeof(struct ccw_device_private), |
| 694 | GFP_KERNEL | GFP_DMA); |
| 695 | if (cdev->private) |
| 696 | return cdev; |
| 697 | } |
| 698 | kfree(cdev); |
| 699 | return ERR_PTR(-ENOMEM); |
| 700 | } |
| 701 | |
| 702 | static int io_subchannel_initialize_dev(struct subchannel *sch, |
| 703 | struct ccw_device *cdev) |
| 704 | { |
| 705 | cdev->private->cdev = cdev; |
| 706 | atomic_set(&cdev->private->onoff, 0); |
| 707 | cdev->dev.parent = &sch->dev; |
| 708 | cdev->dev.release = ccw_device_release; |
| 709 | INIT_LIST_HEAD(&cdev->private->kick_work.entry); |
| 710 | /* Do first half of device_register. */ |
| 711 | device_initialize(&cdev->dev); |
| 712 | if (!get_device(&sch->dev)) { |
| 713 | if (cdev->dev.release) |
| 714 | cdev->dev.release(&cdev->dev); |
| 715 | return -ENODEV; |
| 716 | } |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch) |
| 721 | { |
| 722 | struct ccw_device *cdev; |
| 723 | int ret; |
| 724 | |
| 725 | cdev = io_subchannel_allocate_dev(sch); |
| 726 | if (!IS_ERR(cdev)) { |
| 727 | ret = io_subchannel_initialize_dev(sch, cdev); |
| 728 | if (ret) { |
| 729 | kfree(cdev); |
| 730 | cdev = ERR_PTR(ret); |
| 731 | } |
| 732 | } |
| 733 | return cdev; |
| 734 | } |
| 735 | |
Cornelia Huck | d7b5a4c | 2006-12-08 15:54:28 +0100 | [diff] [blame^] | 736 | static int io_subchannel_recog(struct ccw_device *, struct subchannel *); |
| 737 | |
| 738 | static void sch_attach_device(struct subchannel *sch, |
| 739 | struct ccw_device *cdev) |
| 740 | { |
| 741 | spin_lock_irq(sch->lock); |
| 742 | sch->dev.driver_data = cdev; |
| 743 | cdev->private->schid = sch->schid; |
| 744 | cdev->ccwlock = sch->lock; |
| 745 | device_trigger_reprobe(sch); |
| 746 | spin_unlock_irq(sch->lock); |
| 747 | } |
| 748 | |
| 749 | static void sch_attach_disconnected_device(struct subchannel *sch, |
| 750 | struct ccw_device *cdev) |
| 751 | { |
| 752 | struct subchannel *other_sch; |
| 753 | int ret; |
| 754 | |
| 755 | other_sch = to_subchannel(get_device(cdev->dev.parent)); |
| 756 | ret = device_move(&cdev->dev, &sch->dev); |
| 757 | if (ret) { |
| 758 | CIO_MSG_EVENT(2, "Moving disconnected device 0.%x.%04x failed " |
| 759 | "(ret=%d)!\n", cdev->private->dev_id.ssid, |
| 760 | cdev->private->dev_id.devno, ret); |
| 761 | put_device(&other_sch->dev); |
| 762 | return; |
| 763 | } |
| 764 | other_sch->dev.driver_data = NULL; |
| 765 | /* No need to keep a subchannel without ccw device around. */ |
| 766 | css_sch_device_unregister(other_sch); |
| 767 | put_device(&other_sch->dev); |
| 768 | sch_attach_device(sch, cdev); |
| 769 | } |
| 770 | |
| 771 | static void sch_attach_orphaned_device(struct subchannel *sch, |
| 772 | struct ccw_device *cdev) |
| 773 | { |
| 774 | int ret; |
| 775 | |
| 776 | /* Try to move the ccw device to its new subchannel. */ |
| 777 | ret = device_move(&cdev->dev, &sch->dev); |
| 778 | if (ret) { |
| 779 | CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage " |
| 780 | "failed (ret=%d)!\n", |
| 781 | cdev->private->dev_id.ssid, |
| 782 | cdev->private->dev_id.devno, ret); |
| 783 | return; |
| 784 | } |
| 785 | sch_attach_device(sch, cdev); |
| 786 | } |
| 787 | |
| 788 | static void sch_create_and_recog_new_device(struct subchannel *sch) |
| 789 | { |
| 790 | struct ccw_device *cdev; |
| 791 | |
| 792 | /* Need to allocate a new ccw device. */ |
| 793 | cdev = io_subchannel_create_ccwdev(sch); |
| 794 | if (IS_ERR(cdev)) { |
| 795 | /* OK, we did everything we could... */ |
| 796 | css_sch_device_unregister(sch); |
| 797 | return; |
| 798 | } |
| 799 | spin_lock_irq(sch->lock); |
| 800 | sch->dev.driver_data = cdev; |
| 801 | spin_unlock_irq(sch->lock); |
| 802 | /* Start recognition for the new ccw device. */ |
| 803 | if (io_subchannel_recog(cdev, sch)) { |
| 804 | spin_lock_irq(sch->lock); |
| 805 | sch->dev.driver_data = NULL; |
| 806 | spin_unlock_irq(sch->lock); |
| 807 | if (cdev->dev.release) |
| 808 | cdev->dev.release(&cdev->dev); |
| 809 | css_sch_device_unregister(sch); |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | |
| 814 | void ccw_device_move_to_orphanage(struct work_struct *work) |
| 815 | { |
| 816 | struct ccw_device_private *priv; |
| 817 | struct ccw_device *cdev; |
| 818 | struct ccw_device *replacing_cdev; |
| 819 | struct subchannel *sch; |
| 820 | int ret; |
| 821 | struct channel_subsystem *css; |
| 822 | struct ccw_dev_id dev_id; |
| 823 | |
| 824 | priv = container_of(work, struct ccw_device_private, kick_work); |
| 825 | cdev = priv->cdev; |
| 826 | sch = to_subchannel(cdev->dev.parent); |
| 827 | css = to_css(sch->dev.parent); |
| 828 | dev_id.devno = sch->schib.pmcw.dev; |
| 829 | dev_id.ssid = sch->schid.ssid; |
| 830 | |
| 831 | /* |
| 832 | * Move the orphaned ccw device to the orphanage so the replacing |
| 833 | * ccw device can take its place on the subchannel. |
| 834 | */ |
| 835 | ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev); |
| 836 | if (ret) { |
| 837 | CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed " |
| 838 | "(ret=%d)!\n", cdev->private->dev_id.ssid, |
| 839 | cdev->private->dev_id.devno, ret); |
| 840 | return; |
| 841 | } |
| 842 | cdev->ccwlock = css->pseudo_subchannel->lock; |
| 843 | /* |
| 844 | * Search for the replacing ccw device |
| 845 | * - among the disconnected devices |
| 846 | * - in the orphanage |
| 847 | */ |
| 848 | replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev); |
| 849 | if (replacing_cdev) { |
| 850 | sch_attach_disconnected_device(sch, replacing_cdev); |
| 851 | return; |
| 852 | } |
| 853 | replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id); |
| 854 | if (replacing_cdev) { |
| 855 | sch_attach_orphaned_device(sch, replacing_cdev); |
| 856 | return; |
| 857 | } |
| 858 | sch_create_and_recog_new_device(sch); |
| 859 | } |
| 860 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | /* |
| 862 | * Register recognized device. |
| 863 | */ |
| 864 | static void |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 865 | io_subchannel_register(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | { |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 867 | struct ccw_device_private *priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | struct ccw_device *cdev; |
| 869 | struct subchannel *sch; |
| 870 | int ret; |
| 871 | unsigned long flags; |
| 872 | |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 873 | priv = container_of(work, struct ccw_device_private, kick_work); |
| 874 | cdev = priv->cdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | sch = to_subchannel(cdev->dev.parent); |
| 876 | |
Cornelia Huck | 47af551 | 2006-12-04 15:41:07 +0100 | [diff] [blame] | 877 | /* |
| 878 | * io_subchannel_register() will also be called after device |
| 879 | * recognition has been done for a boxed device (which will already |
| 880 | * be registered). We need to reprobe since we may now have sense id |
| 881 | * information. |
| 882 | */ |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 883 | if (klist_node_attached(&cdev->dev.knode_parent)) { |
Cornelia Huck | 47af551 | 2006-12-04 15:41:07 +0100 | [diff] [blame] | 884 | if (!cdev->drv) { |
| 885 | ret = device_reprobe(&cdev->dev); |
| 886 | if (ret) |
| 887 | /* We can't do much here. */ |
| 888 | dev_info(&cdev->dev, "device_reprobe() returned" |
| 889 | " %d\n", ret); |
| 890 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | goto out; |
| 892 | } |
| 893 | /* make it known to the system */ |
| 894 | ret = ccw_device_register(cdev); |
| 895 | if (ret) { |
| 896 | printk (KERN_WARNING "%s: could not register %s\n", |
| 897 | __func__, cdev->dev.bus_id); |
| 898 | put_device(&cdev->dev); |
Cornelia Huck | 2ec2298 | 2006-12-08 15:54:26 +0100 | [diff] [blame] | 899 | spin_lock_irqsave(sch->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | sch->dev.driver_data = NULL; |
Cornelia Huck | 2ec2298 | 2006-12-08 15:54:26 +0100 | [diff] [blame] | 901 | spin_unlock_irqrestore(sch->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | kfree (cdev->private); |
| 903 | kfree (cdev); |
| 904 | put_device(&sch->dev); |
| 905 | if (atomic_dec_and_test(&ccw_device_init_count)) |
| 906 | wake_up(&ccw_device_init_wq); |
| 907 | return; |
| 908 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | put_device(&cdev->dev); |
| 910 | out: |
| 911 | cdev->private->flags.recog_done = 1; |
| 912 | put_device(&sch->dev); |
| 913 | wake_up(&cdev->private->wait_q); |
| 914 | if (atomic_dec_and_test(&ccw_device_init_count)) |
| 915 | wake_up(&ccw_device_init_wq); |
| 916 | } |
| 917 | |
| 918 | void |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 919 | ccw_device_call_sch_unregister(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | { |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 921 | struct ccw_device_private *priv; |
| 922 | struct ccw_device *cdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | struct subchannel *sch; |
| 924 | |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 925 | priv = container_of(work, struct ccw_device_private, kick_work); |
| 926 | cdev = priv->cdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | sch = to_subchannel(cdev->dev.parent); |
Cornelia Huck | 6ab4879 | 2006-07-12 16:39:50 +0200 | [diff] [blame] | 928 | css_sch_device_unregister(sch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | /* Reset intparm to zeroes. */ |
| 930 | sch->schib.pmcw.intparm = 0; |
| 931 | cio_modify(sch); |
| 932 | put_device(&cdev->dev); |
| 933 | put_device(&sch->dev); |
| 934 | } |
| 935 | |
| 936 | /* |
| 937 | * subchannel recognition done. Called from the state machine. |
| 938 | */ |
| 939 | void |
| 940 | io_subchannel_recog_done(struct ccw_device *cdev) |
| 941 | { |
| 942 | struct subchannel *sch; |
| 943 | |
| 944 | if (css_init_done == 0) { |
| 945 | cdev->private->flags.recog_done = 1; |
| 946 | return; |
| 947 | } |
| 948 | switch (cdev->private->state) { |
| 949 | case DEV_STATE_NOT_OPER: |
| 950 | cdev->private->flags.recog_done = 1; |
| 951 | /* Remove device found not operational. */ |
| 952 | if (!get_device(&cdev->dev)) |
| 953 | break; |
| 954 | sch = to_subchannel(cdev->dev.parent); |
| 955 | PREPARE_WORK(&cdev->private->kick_work, |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 956 | ccw_device_call_sch_unregister); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | queue_work(slow_path_wq, &cdev->private->kick_work); |
| 958 | if (atomic_dec_and_test(&ccw_device_init_count)) |
| 959 | wake_up(&ccw_device_init_wq); |
| 960 | break; |
| 961 | case DEV_STATE_BOXED: |
| 962 | /* Device did not respond in time. */ |
| 963 | case DEV_STATE_OFFLINE: |
| 964 | /* |
| 965 | * We can't register the device in interrupt context so |
| 966 | * we schedule a work item. |
| 967 | */ |
| 968 | if (!get_device(&cdev->dev)) |
| 969 | break; |
| 970 | PREPARE_WORK(&cdev->private->kick_work, |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 971 | io_subchannel_register); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | queue_work(slow_path_wq, &cdev->private->kick_work); |
| 973 | break; |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | static int |
| 978 | io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch) |
| 979 | { |
| 980 | int rc; |
| 981 | struct ccw_device_private *priv; |
| 982 | |
| 983 | sch->dev.driver_data = cdev; |
| 984 | sch->driver = &io_subchannel_driver; |
Cornelia Huck | 2ec2298 | 2006-12-08 15:54:26 +0100 | [diff] [blame] | 985 | cdev->ccwlock = sch->lock; |
Cornelia Huck | fb6958a | 2006-01-06 00:19:25 -0800 | [diff] [blame] | 986 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | /* Init private data. */ |
| 988 | priv = cdev->private; |
Cornelia Huck | 7896426 | 2006-10-11 15:31:38 +0200 | [diff] [blame] | 989 | priv->dev_id.devno = sch->schib.pmcw.dev; |
| 990 | priv->dev_id.ssid = sch->schid.ssid; |
| 991 | priv->schid = sch->schid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | priv->state = DEV_STATE_NOT_OPER; |
| 993 | INIT_LIST_HEAD(&priv->cmb_list); |
| 994 | init_waitqueue_head(&priv->wait_q); |
| 995 | init_timer(&priv->timer); |
| 996 | |
| 997 | /* Set an initial name for the device. */ |
Cornelia Huck | fb6958a | 2006-01-06 00:19:25 -0800 | [diff] [blame] | 998 | snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x", |
| 999 | sch->schid.ssid, sch->schib.pmcw.dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | |
| 1001 | /* Increase counter of devices currently in recognition. */ |
| 1002 | atomic_inc(&ccw_device_init_count); |
| 1003 | |
| 1004 | /* Start async. device sensing. */ |
Cornelia Huck | 2ec2298 | 2006-12-08 15:54:26 +0100 | [diff] [blame] | 1005 | spin_lock_irq(sch->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | rc = ccw_device_recognition(cdev); |
Cornelia Huck | 2ec2298 | 2006-12-08 15:54:26 +0100 | [diff] [blame] | 1007 | spin_unlock_irq(sch->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | if (rc) { |
| 1009 | if (atomic_dec_and_test(&ccw_device_init_count)) |
| 1010 | wake_up(&ccw_device_init_wq); |
| 1011 | } |
| 1012 | return rc; |
| 1013 | } |
| 1014 | |
Cornelia Huck | d7b5a4c | 2006-12-08 15:54:28 +0100 | [diff] [blame^] | 1015 | static void ccw_device_move_to_sch(struct work_struct *work) |
| 1016 | { |
| 1017 | struct ccw_device_private *priv; |
| 1018 | int rc; |
| 1019 | struct subchannel *sch; |
| 1020 | struct ccw_device *cdev; |
| 1021 | struct subchannel *former_parent; |
| 1022 | |
| 1023 | priv = container_of(work, struct ccw_device_private, kick_work); |
| 1024 | sch = priv->sch; |
| 1025 | cdev = priv->cdev; |
| 1026 | former_parent = ccw_device_is_orphan(cdev) ? |
| 1027 | NULL : to_subchannel(get_device(cdev->dev.parent)); |
| 1028 | mutex_lock(&sch->reg_mutex); |
| 1029 | /* Try to move the ccw device to its new subchannel. */ |
| 1030 | rc = device_move(&cdev->dev, &sch->dev); |
| 1031 | mutex_unlock(&sch->reg_mutex); |
| 1032 | if (rc) { |
| 1033 | CIO_MSG_EVENT(2, "Moving device 0.%x.%04x to subchannel " |
| 1034 | "0.%x.%04x failed (ret=%d)!\n", |
| 1035 | cdev->private->dev_id.ssid, |
| 1036 | cdev->private->dev_id.devno, sch->schid.ssid, |
| 1037 | sch->schid.sch_no, rc); |
| 1038 | css_sch_device_unregister(sch); |
| 1039 | goto out; |
| 1040 | } |
| 1041 | if (former_parent) { |
| 1042 | spin_lock_irq(former_parent->lock); |
| 1043 | former_parent->dev.driver_data = NULL; |
| 1044 | spin_unlock_irq(former_parent->lock); |
| 1045 | css_sch_device_unregister(former_parent); |
| 1046 | /* Reset intparm to zeroes. */ |
| 1047 | former_parent->schib.pmcw.intparm = 0; |
| 1048 | cio_modify(former_parent); |
| 1049 | } |
| 1050 | sch_attach_device(sch, cdev); |
| 1051 | out: |
| 1052 | if (former_parent) |
| 1053 | put_device(&former_parent->dev); |
| 1054 | put_device(&cdev->dev); |
| 1055 | } |
| 1056 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | static int |
Cornelia Huck | 8bbace7 | 2006-01-11 10:56:22 +0100 | [diff] [blame] | 1058 | io_subchannel_probe (struct subchannel *sch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | struct ccw_device *cdev; |
| 1061 | int rc; |
| 1062 | unsigned long flags; |
Cornelia Huck | d7b5a4c | 2006-12-08 15:54:28 +0100 | [diff] [blame^] | 1063 | struct ccw_dev_id dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | if (sch->dev.driver_data) { |
| 1066 | /* |
| 1067 | * This subchannel already has an associated ccw_device. |
| 1068 | * Register it and exit. This happens for all early |
| 1069 | * device, e.g. the console. |
| 1070 | */ |
| 1071 | cdev = sch->dev.driver_data; |
| 1072 | device_initialize(&cdev->dev); |
| 1073 | ccw_device_register(cdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | /* |
| 1075 | * Check if the device is already online. If it is |
| 1076 | * the reference count needs to be corrected |
| 1077 | * (see ccw_device_online and css_init_done for the |
| 1078 | * ugly details). |
| 1079 | */ |
| 1080 | if (cdev->private->state != DEV_STATE_NOT_OPER && |
| 1081 | cdev->private->state != DEV_STATE_OFFLINE && |
| 1082 | cdev->private->state != DEV_STATE_BOXED) |
| 1083 | get_device(&cdev->dev); |
| 1084 | return 0; |
| 1085 | } |
Cornelia Huck | d7b5a4c | 2006-12-08 15:54:28 +0100 | [diff] [blame^] | 1086 | /* |
| 1087 | * First check if a fitting device may be found amongst the |
| 1088 | * disconnected devices or in the orphanage. |
| 1089 | */ |
| 1090 | dev_id.devno = sch->schib.pmcw.dev; |
| 1091 | dev_id.ssid = sch->schid.ssid; |
| 1092 | cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL); |
| 1093 | if (!cdev) |
| 1094 | cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent), |
| 1095 | &dev_id); |
| 1096 | if (cdev) { |
| 1097 | /* |
| 1098 | * Schedule moving the device until when we have a registered |
| 1099 | * subchannel to move to and succeed the probe. We can |
| 1100 | * unregister later again, when the probe is through. |
| 1101 | */ |
| 1102 | cdev->private->sch = sch; |
| 1103 | PREPARE_WORK(&cdev->private->kick_work, |
| 1104 | ccw_device_move_to_sch); |
| 1105 | queue_work(slow_path_wq, &cdev->private->kick_work); |
| 1106 | return 0; |
| 1107 | } |
Cornelia Huck | 7674da7 | 2006-12-08 15:54:21 +0100 | [diff] [blame] | 1108 | cdev = io_subchannel_create_ccwdev(sch); |
| 1109 | if (IS_ERR(cdev)) |
| 1110 | return PTR_ERR(cdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | |
Cornelia Huck | 8bbace7 | 2006-01-11 10:56:22 +0100 | [diff] [blame] | 1112 | rc = io_subchannel_recog(cdev, sch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | if (rc) { |
Cornelia Huck | 2ec2298 | 2006-12-08 15:54:26 +0100 | [diff] [blame] | 1114 | spin_lock_irqsave(sch->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | sch->dev.driver_data = NULL; |
Cornelia Huck | 2ec2298 | 2006-12-08 15:54:26 +0100 | [diff] [blame] | 1116 | spin_unlock_irqrestore(sch->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | if (cdev->dev.release) |
| 1118 | cdev->dev.release(&cdev->dev); |
| 1119 | } |
| 1120 | |
| 1121 | return rc; |
| 1122 | } |
| 1123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | static int |
Cornelia Huck | 8bbace7 | 2006-01-11 10:56:22 +0100 | [diff] [blame] | 1125 | io_subchannel_remove (struct subchannel *sch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | { |
| 1127 | struct ccw_device *cdev; |
| 1128 | unsigned long flags; |
| 1129 | |
Cornelia Huck | 8bbace7 | 2006-01-11 10:56:22 +0100 | [diff] [blame] | 1130 | if (!sch->dev.driver_data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | return 0; |
Cornelia Huck | 8bbace7 | 2006-01-11 10:56:22 +0100 | [diff] [blame] | 1132 | cdev = sch->dev.driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | /* Set ccw device to not operational and drop reference. */ |
| 1134 | spin_lock_irqsave(cdev->ccwlock, flags); |
Cornelia Huck | 8bbace7 | 2006-01-11 10:56:22 +0100 | [diff] [blame] | 1135 | sch->dev.driver_data = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | cdev->private->state = DEV_STATE_NOT_OPER; |
| 1137 | spin_unlock_irqrestore(cdev->ccwlock, flags); |
| 1138 | /* |
| 1139 | * Put unregistration on workqueue to avoid livelocks on the css bus |
| 1140 | * semaphore. |
| 1141 | */ |
| 1142 | if (get_device(&cdev->dev)) { |
| 1143 | PREPARE_WORK(&cdev->private->kick_work, |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 1144 | ccw_device_unregister); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | queue_work(ccw_device_work, &cdev->private->kick_work); |
| 1146 | } |
| 1147 | return 0; |
| 1148 | } |
| 1149 | |
| 1150 | static int |
| 1151 | io_subchannel_notify(struct device *dev, int event) |
| 1152 | { |
| 1153 | struct ccw_device *cdev; |
| 1154 | |
| 1155 | cdev = dev->driver_data; |
| 1156 | if (!cdev) |
| 1157 | return 0; |
| 1158 | if (!cdev->drv) |
| 1159 | return 0; |
| 1160 | if (!cdev->online) |
| 1161 | return 0; |
| 1162 | return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0; |
| 1163 | } |
| 1164 | |
| 1165 | static void |
| 1166 | io_subchannel_verify(struct device *dev) |
| 1167 | { |
| 1168 | struct ccw_device *cdev; |
| 1169 | |
| 1170 | cdev = dev->driver_data; |
| 1171 | if (cdev) |
| 1172 | dev_fsm_event(cdev, DEV_EVENT_VERIFY); |
| 1173 | } |
| 1174 | |
| 1175 | static void |
| 1176 | io_subchannel_ioterm(struct device *dev) |
| 1177 | { |
| 1178 | struct ccw_device *cdev; |
| 1179 | |
| 1180 | cdev = dev->driver_data; |
| 1181 | if (!cdev) |
| 1182 | return; |
Cornelia Huck | d23861f | 2006-12-04 15:41:04 +0100 | [diff] [blame] | 1183 | /* Internal I/O will be retried by the interrupt handler. */ |
| 1184 | if (cdev->private->flags.intretry) |
| 1185 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | cdev->private->state = DEV_STATE_CLEAR_VERIFY; |
| 1187 | if (cdev->handler) |
| 1188 | cdev->handler(cdev, cdev->private->intparm, |
| 1189 | ERR_PTR(-EIO)); |
| 1190 | } |
| 1191 | |
| 1192 | static void |
Cornelia Huck | 8bbace7 | 2006-01-11 10:56:22 +0100 | [diff] [blame] | 1193 | io_subchannel_shutdown(struct subchannel *sch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | struct ccw_device *cdev; |
| 1196 | int ret; |
| 1197 | |
Cornelia Huck | 8bbace7 | 2006-01-11 10:56:22 +0100 | [diff] [blame] | 1198 | cdev = sch->dev.driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | |
Cornelia Huck | a8237fc | 2006-01-06 00:19:21 -0800 | [diff] [blame] | 1200 | if (cio_is_console(sch->schid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | return; |
| 1202 | if (!sch->schib.pmcw.ena) |
| 1203 | /* Nothing to do. */ |
| 1204 | return; |
| 1205 | ret = cio_disable_subchannel(sch); |
| 1206 | if (ret != -EBUSY) |
| 1207 | /* Subchannel is disabled, we're done. */ |
| 1208 | return; |
| 1209 | cdev->private->state = DEV_STATE_QUIESCE; |
| 1210 | if (cdev->handler) |
| 1211 | cdev->handler(cdev, cdev->private->intparm, |
| 1212 | ERR_PTR(-EIO)); |
| 1213 | ret = ccw_device_cancel_halt_clear(cdev); |
| 1214 | if (ret == -EBUSY) { |
| 1215 | ccw_device_set_timeout(cdev, HZ/10); |
| 1216 | wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev)); |
| 1217 | } |
| 1218 | cio_disable_subchannel(sch); |
| 1219 | } |
| 1220 | |
| 1221 | #ifdef CONFIG_CCW_CONSOLE |
| 1222 | static struct ccw_device console_cdev; |
| 1223 | static struct ccw_device_private console_private; |
| 1224 | static int console_cdev_in_use; |
| 1225 | |
Cornelia Huck | 2ec2298 | 2006-12-08 15:54:26 +0100 | [diff] [blame] | 1226 | static DEFINE_SPINLOCK(ccw_console_lock); |
| 1227 | |
| 1228 | spinlock_t * cio_get_console_lock(void) |
| 1229 | { |
| 1230 | return &ccw_console_lock; |
| 1231 | } |
| 1232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | static int |
| 1234 | ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch) |
| 1235 | { |
| 1236 | int rc; |
| 1237 | |
| 1238 | /* Initialize the ccw_device structure. */ |
Heiko Carstens | 292888c | 2006-08-30 14:33:35 +0200 | [diff] [blame] | 1239 | cdev->dev.parent= &sch->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | rc = io_subchannel_recog(cdev, sch); |
| 1241 | if (rc) |
| 1242 | return rc; |
| 1243 | |
| 1244 | /* Now wait for the async. recognition to come to an end. */ |
| 1245 | spin_lock_irq(cdev->ccwlock); |
| 1246 | while (!dev_fsm_final_state(cdev)) |
| 1247 | wait_cons_dev(); |
| 1248 | rc = -EIO; |
| 1249 | if (cdev->private->state != DEV_STATE_OFFLINE) |
| 1250 | goto out_unlock; |
| 1251 | ccw_device_online(cdev); |
| 1252 | while (!dev_fsm_final_state(cdev)) |
| 1253 | wait_cons_dev(); |
| 1254 | if (cdev->private->state != DEV_STATE_ONLINE) |
| 1255 | goto out_unlock; |
| 1256 | rc = 0; |
| 1257 | out_unlock: |
| 1258 | spin_unlock_irq(cdev->ccwlock); |
| 1259 | return 0; |
| 1260 | } |
| 1261 | |
| 1262 | struct ccw_device * |
| 1263 | ccw_device_probe_console(void) |
| 1264 | { |
| 1265 | struct subchannel *sch; |
| 1266 | int ret; |
| 1267 | |
| 1268 | if (xchg(&console_cdev_in_use, 1) != 0) |
Peter Oberparleiter | 600b5d1 | 2006-02-01 03:06:35 -0800 | [diff] [blame] | 1269 | return ERR_PTR(-EBUSY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | sch = cio_probe_console(); |
| 1271 | if (IS_ERR(sch)) { |
| 1272 | console_cdev_in_use = 0; |
| 1273 | return (void *) sch; |
| 1274 | } |
| 1275 | memset(&console_cdev, 0, sizeof(struct ccw_device)); |
| 1276 | memset(&console_private, 0, sizeof(struct ccw_device_private)); |
| 1277 | console_cdev.private = &console_private; |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 1278 | console_private.cdev = &console_cdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | ret = ccw_device_console_enable(&console_cdev, sch); |
| 1280 | if (ret) { |
| 1281 | cio_release_console(); |
| 1282 | console_cdev_in_use = 0; |
| 1283 | return ERR_PTR(ret); |
| 1284 | } |
| 1285 | console_cdev.online = 1; |
| 1286 | return &console_cdev; |
| 1287 | } |
| 1288 | #endif |
| 1289 | |
| 1290 | /* |
| 1291 | * get ccw_device matching the busid, but only if owned by cdrv |
| 1292 | */ |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 1293 | static int |
| 1294 | __ccwdev_check_busid(struct device *dev, void *id) |
| 1295 | { |
| 1296 | char *bus_id; |
| 1297 | |
Cornelia Huck | 12975ae | 2006-10-11 15:31:47 +0200 | [diff] [blame] | 1298 | bus_id = id; |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 1299 | |
| 1300 | return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0); |
| 1301 | } |
| 1302 | |
| 1303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | struct ccw_device * |
| 1305 | get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id) |
| 1306 | { |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 1307 | struct device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | struct device_driver *drv; |
| 1309 | |
| 1310 | drv = get_driver(&cdrv->driver); |
| 1311 | if (!drv) |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 1312 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 1314 | dev = driver_find_device(drv, NULL, (void *)bus_id, |
| 1315 | __ccwdev_check_busid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | put_driver(drv); |
| 1317 | |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 1318 | return dev ? to_ccwdev(dev) : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | /************************** device driver handling ************************/ |
| 1322 | |
| 1323 | /* This is the implementation of the ccw_driver class. The probe, remove |
| 1324 | * and release methods are initially very similar to the device_driver |
| 1325 | * implementations, with the difference that they have ccw_device |
| 1326 | * arguments. |
| 1327 | * |
| 1328 | * A ccw driver also contains the information that is needed for |
| 1329 | * device matching. |
| 1330 | */ |
| 1331 | static int |
| 1332 | ccw_device_probe (struct device *dev) |
| 1333 | { |
| 1334 | struct ccw_device *cdev = to_ccwdev(dev); |
| 1335 | struct ccw_driver *cdrv = to_ccwdrv(dev->driver); |
| 1336 | int ret; |
| 1337 | |
| 1338 | cdev->drv = cdrv; /* to let the driver call _set_online */ |
| 1339 | |
| 1340 | ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV; |
| 1341 | |
| 1342 | if (ret) { |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 1343 | cdev->drv = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | return ret; |
| 1345 | } |
| 1346 | |
| 1347 | return 0; |
| 1348 | } |
| 1349 | |
| 1350 | static int |
| 1351 | ccw_device_remove (struct device *dev) |
| 1352 | { |
| 1353 | struct ccw_device *cdev = to_ccwdev(dev); |
| 1354 | struct ccw_driver *cdrv = cdev->drv; |
| 1355 | int ret; |
| 1356 | |
| 1357 | pr_debug("removing device %s\n", cdev->dev.bus_id); |
| 1358 | if (cdrv->remove) |
| 1359 | cdrv->remove(cdev); |
| 1360 | if (cdev->online) { |
| 1361 | cdev->online = 0; |
| 1362 | spin_lock_irq(cdev->ccwlock); |
| 1363 | ret = ccw_device_offline(cdev); |
| 1364 | spin_unlock_irq(cdev->ccwlock); |
| 1365 | if (ret == 0) |
| 1366 | wait_event(cdev->private->wait_q, |
| 1367 | dev_fsm_final_state(cdev)); |
| 1368 | else |
| 1369 | //FIXME: we can't fail! |
| 1370 | pr_debug("ccw_device_offline returned %d, device %s\n", |
| 1371 | ret, cdev->dev.bus_id); |
| 1372 | } |
| 1373 | ccw_device_set_timeout(cdev, 0); |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 1374 | cdev->drv = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | return 0; |
| 1376 | } |
| 1377 | |
Cornelia Huck | 8bbace7 | 2006-01-11 10:56:22 +0100 | [diff] [blame] | 1378 | struct bus_type ccw_bus_type = { |
| 1379 | .name = "ccw", |
| 1380 | .match = ccw_bus_match, |
| 1381 | .uevent = ccw_uevent, |
| 1382 | .probe = ccw_device_probe, |
| 1383 | .remove = ccw_device_remove, |
| 1384 | }; |
| 1385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | int |
| 1387 | ccw_driver_register (struct ccw_driver *cdriver) |
| 1388 | { |
| 1389 | struct device_driver *drv = &cdriver->driver; |
| 1390 | |
| 1391 | drv->bus = &ccw_bus_type; |
| 1392 | drv->name = cdriver->name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | |
| 1394 | return driver_register(drv); |
| 1395 | } |
| 1396 | |
| 1397 | void |
| 1398 | ccw_driver_unregister (struct ccw_driver *cdriver) |
| 1399 | { |
| 1400 | driver_unregister(&cdriver->driver); |
| 1401 | } |
| 1402 | |
Cornelia Huck | a8237fc | 2006-01-06 00:19:21 -0800 | [diff] [blame] | 1403 | /* Helper func for qdio. */ |
| 1404 | struct subchannel_id |
| 1405 | ccw_device_get_subchannel_id(struct ccw_device *cdev) |
| 1406 | { |
| 1407 | struct subchannel *sch; |
| 1408 | |
| 1409 | sch = to_subchannel(cdev->dev.parent); |
| 1410 | return sch->schid; |
| 1411 | } |
| 1412 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | MODULE_LICENSE("GPL"); |
| 1414 | EXPORT_SYMBOL(ccw_device_set_online); |
| 1415 | EXPORT_SYMBOL(ccw_device_set_offline); |
| 1416 | EXPORT_SYMBOL(ccw_driver_register); |
| 1417 | EXPORT_SYMBOL(ccw_driver_unregister); |
| 1418 | EXPORT_SYMBOL(get_ccwdev_by_busid); |
| 1419 | EXPORT_SYMBOL(ccw_bus_type); |
| 1420 | EXPORT_SYMBOL(ccw_device_work); |
| 1421 | EXPORT_SYMBOL(ccw_device_notify_work); |
Cornelia Huck | a8237fc | 2006-01-06 00:19:21 -0800 | [diff] [blame] | 1422 | EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id); |