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