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