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" |
| 26 | #include "css.h" |
| 27 | #include "device.h" |
| 28 | #include "ioasm.h" |
| 29 | |
| 30 | /******************* bus type handling ***********************/ |
| 31 | |
| 32 | /* The Linux driver model distinguishes between a bus type and |
| 33 | * the bus itself. Of course we only have one channel |
| 34 | * subsystem driver and one channel system per machine, but |
| 35 | * we still use the abstraction. T.R. says it's a good idea. */ |
| 36 | static int |
| 37 | ccw_bus_match (struct device * dev, struct device_driver * drv) |
| 38 | { |
| 39 | struct ccw_device *cdev = to_ccwdev(dev); |
| 40 | struct ccw_driver *cdrv = to_ccwdrv(drv); |
| 41 | const struct ccw_device_id *ids = cdrv->ids, *found; |
| 42 | |
| 43 | if (!ids) |
| 44 | return 0; |
| 45 | |
| 46 | found = ccw_device_id_match(ids, &cdev->id); |
| 47 | if (!found) |
| 48 | return 0; |
| 49 | |
| 50 | cdev->id.driver_info = found->driver_info; |
| 51 | |
| 52 | return 1; |
| 53 | } |
| 54 | |
Peter Oberparleiter | db0c2d5 | 2006-09-20 15:59:49 +0200 | [diff] [blame] | 55 | /* Store modalias string delimited by prefix/suffix string into buffer with |
| 56 | * specified size. Return length of resulting string (excluding trailing '\0') |
| 57 | * even if string doesn't fit buffer (snprintf semantics). */ |
| 58 | static int snprint_alias(char *buf, size_t size, const char *prefix, |
| 59 | struct ccw_device_id *id, const char *suffix) |
| 60 | { |
| 61 | int len; |
| 62 | |
| 63 | len = snprintf(buf, size, "%sccw:t%04Xm%02X", prefix, id->cu_type, |
| 64 | id->cu_model); |
| 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; |
Peter Oberparleiter | db0c2d5 | 2006-09-20 15:59:49 +0200 | [diff] [blame] | 87 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Peter Oberparleiter | db0c2d5 | 2006-09-20 15:59:49 +0200 | [diff] [blame] | 89 | /* CU_TYPE= */ |
| 90 | len = snprintf(buffer, buffer_size, "CU_TYPE=%04X", id->cu_type) + 1; |
| 91 | if (len > buffer_size || i >= num_envp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | envp[i++] = buffer; |
Peter Oberparleiter | db0c2d5 | 2006-09-20 15:59:49 +0200 | [diff] [blame] | 94 | buffer += len; |
| 95 | buffer_size -= len; |
| 96 | |
| 97 | /* CU_MODEL= */ |
| 98 | len = snprintf(buffer, buffer_size, "CU_MODEL=%02X", id->cu_model) + 1; |
| 99 | if (len > buffer_size || i >= num_envp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | return -ENOMEM; |
Peter Oberparleiter | db0c2d5 | 2006-09-20 15:59:49 +0200 | [diff] [blame] | 101 | envp[i++] = buffer; |
| 102 | buffer += len; |
| 103 | buffer_size -= len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
| 105 | /* The next two can be zero, that's ok for us */ |
Peter Oberparleiter | db0c2d5 | 2006-09-20 15:59:49 +0200 | [diff] [blame] | 106 | /* DEV_TYPE= */ |
| 107 | len = snprintf(buffer, buffer_size, "DEV_TYPE=%04X", id->dev_type) + 1; |
| 108 | if (len > buffer_size || i >= num_envp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | return -ENOMEM; |
Peter Oberparleiter | db0c2d5 | 2006-09-20 15:59:49 +0200 | [diff] [blame] | 110 | envp[i++] = buffer; |
| 111 | buffer += len; |
| 112 | buffer_size -= len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Peter Oberparleiter | db0c2d5 | 2006-09-20 15:59:49 +0200 | [diff] [blame] | 114 | /* DEV_MODEL= */ |
| 115 | len = snprintf(buffer, buffer_size, "DEV_MODEL=%02X", |
| 116 | (unsigned char) id->dev_model) + 1; |
| 117 | if (len > buffer_size || i >= num_envp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | return -ENOMEM; |
Peter Oberparleiter | db0c2d5 | 2006-09-20 15:59:49 +0200 | [diff] [blame] | 119 | envp[i++] = buffer; |
| 120 | buffer += len; |
| 121 | buffer_size -= len; |
| 122 | |
| 123 | /* MODALIAS= */ |
| 124 | len = snprint_alias(buffer, buffer_size, "MODALIAS=", id, "") + 1; |
| 125 | if (len > buffer_size || i >= num_envp) |
| 126 | return -ENOMEM; |
| 127 | envp[i++] = buffer; |
| 128 | buffer += len; |
| 129 | buffer_size -= len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 131 | envp[i] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
Cornelia Huck | 8bbace7 | 2006-01-11 10:56:22 +0100 | [diff] [blame] | 136 | struct bus_type ccw_bus_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
Cornelia Huck | 8bbace7 | 2006-01-11 10:56:22 +0100 | [diff] [blame] | 138 | static int io_subchannel_probe (struct subchannel *); |
| 139 | static int io_subchannel_remove (struct subchannel *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | void io_subchannel_irq (struct device *); |
| 141 | static int io_subchannel_notify(struct device *, int); |
| 142 | static void io_subchannel_verify(struct device *); |
| 143 | static void io_subchannel_ioterm(struct device *); |
Cornelia Huck | 8bbace7 | 2006-01-11 10:56:22 +0100 | [diff] [blame] | 144 | static void io_subchannel_shutdown(struct subchannel *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
| 146 | struct css_driver io_subchannel_driver = { |
| 147 | .subchannel_type = SUBCHANNEL_TYPE_IO, |
| 148 | .drv = { |
| 149 | .name = "io_subchannel", |
| 150 | .bus = &css_bus_type, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | }, |
| 152 | .irq = io_subchannel_irq, |
| 153 | .notify = io_subchannel_notify, |
| 154 | .verify = io_subchannel_verify, |
| 155 | .termination = io_subchannel_ioterm, |
Cornelia Huck | 8bbace7 | 2006-01-11 10:56:22 +0100 | [diff] [blame] | 156 | .probe = io_subchannel_probe, |
| 157 | .remove = io_subchannel_remove, |
| 158 | .shutdown = io_subchannel_shutdown, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | struct workqueue_struct *ccw_device_work; |
| 162 | struct workqueue_struct *ccw_device_notify_work; |
Peter Oberparleiter | 40154b8 | 2006-06-29 14:57:03 +0200 | [diff] [blame] | 163 | wait_queue_head_t ccw_device_init_wq; |
| 164 | atomic_t ccw_device_init_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
| 166 | static int __init |
| 167 | init_ccw_bus_type (void) |
| 168 | { |
| 169 | int ret; |
| 170 | |
| 171 | init_waitqueue_head(&ccw_device_init_wq); |
| 172 | atomic_set(&ccw_device_init_count, 0); |
| 173 | |
| 174 | ccw_device_work = create_singlethread_workqueue("cio"); |
| 175 | if (!ccw_device_work) |
| 176 | return -ENOMEM; /* FIXME: better errno ? */ |
| 177 | ccw_device_notify_work = create_singlethread_workqueue("cio_notify"); |
| 178 | if (!ccw_device_notify_work) { |
| 179 | ret = -ENOMEM; /* FIXME: better errno ? */ |
| 180 | goto out_err; |
| 181 | } |
| 182 | slow_path_wq = create_singlethread_workqueue("kslowcrw"); |
| 183 | if (!slow_path_wq) { |
| 184 | ret = -ENOMEM; /* FIXME: better errno ? */ |
| 185 | goto out_err; |
| 186 | } |
| 187 | if ((ret = bus_register (&ccw_bus_type))) |
| 188 | goto out_err; |
| 189 | |
| 190 | if ((ret = driver_register(&io_subchannel_driver.drv))) |
| 191 | goto out_err; |
| 192 | |
| 193 | wait_event(ccw_device_init_wq, |
| 194 | atomic_read(&ccw_device_init_count) == 0); |
| 195 | flush_workqueue(ccw_device_work); |
| 196 | return 0; |
| 197 | out_err: |
| 198 | if (ccw_device_work) |
| 199 | destroy_workqueue(ccw_device_work); |
| 200 | if (ccw_device_notify_work) |
| 201 | destroy_workqueue(ccw_device_notify_work); |
| 202 | if (slow_path_wq) |
| 203 | destroy_workqueue(slow_path_wq); |
| 204 | return ret; |
| 205 | } |
| 206 | |
| 207 | static void __exit |
| 208 | cleanup_ccw_bus_type (void) |
| 209 | { |
| 210 | driver_unregister(&io_subchannel_driver.drv); |
| 211 | bus_unregister(&ccw_bus_type); |
| 212 | destroy_workqueue(ccw_device_notify_work); |
| 213 | destroy_workqueue(ccw_device_work); |
| 214 | } |
| 215 | |
| 216 | subsys_initcall(init_ccw_bus_type); |
| 217 | module_exit(cleanup_ccw_bus_type); |
| 218 | |
| 219 | /************************ device handling **************************/ |
| 220 | |
| 221 | /* |
| 222 | * A ccw_device has some interfaces in sysfs in addition to the |
| 223 | * standard ones. |
| 224 | * The following entries are designed to export the information which |
| 225 | * resided in 2.4 in /proc/subchannels. Subchannel and device number |
| 226 | * are obvious, so they don't have an entry :) |
| 227 | * TODO: Split chpids and pimpampom up? Where is "in use" in the tree? |
| 228 | */ |
| 229 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 230 | chpids_show (struct device * dev, struct device_attribute *attr, char * buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | { |
| 232 | struct subchannel *sch = to_subchannel(dev); |
| 233 | struct ssd_info *ssd = &sch->ssd_info; |
| 234 | ssize_t ret = 0; |
| 235 | int chp; |
| 236 | |
| 237 | for (chp = 0; chp < 8; chp++) |
| 238 | ret += sprintf (buf+ret, "%02x ", ssd->chpid[chp]); |
| 239 | |
| 240 | ret += sprintf (buf+ret, "\n"); |
| 241 | return min((ssize_t)PAGE_SIZE, ret); |
| 242 | } |
| 243 | |
| 244 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 245 | pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | { |
| 247 | struct subchannel *sch = to_subchannel(dev); |
| 248 | struct pmcw *pmcw = &sch->schib.pmcw; |
| 249 | |
| 250 | return sprintf (buf, "%02x %02x %02x\n", |
| 251 | pmcw->pim, pmcw->pam, pmcw->pom); |
| 252 | } |
| 253 | |
| 254 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 255 | devtype_show (struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | { |
| 257 | struct ccw_device *cdev = to_ccwdev(dev); |
| 258 | struct ccw_device_id *id = &(cdev->id); |
| 259 | |
| 260 | if (id->dev_type != 0) |
| 261 | return sprintf(buf, "%04x/%02x\n", |
| 262 | id->dev_type, id->dev_model); |
| 263 | else |
| 264 | return sprintf(buf, "n/a\n"); |
| 265 | } |
| 266 | |
| 267 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 268 | cutype_show (struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | { |
| 270 | struct ccw_device *cdev = to_ccwdev(dev); |
| 271 | struct ccw_device_id *id = &(cdev->id); |
| 272 | |
| 273 | return sprintf(buf, "%04x/%02x\n", |
| 274 | id->cu_type, id->cu_model); |
| 275 | } |
| 276 | |
| 277 | static ssize_t |
Bastian Blank | f1fc78a | 2005-10-30 15:00:12 -0800 | [diff] [blame] | 278 | modalias_show (struct device *dev, struct device_attribute *attr, char *buf) |
| 279 | { |
| 280 | struct ccw_device *cdev = to_ccwdev(dev); |
| 281 | struct ccw_device_id *id = &(cdev->id); |
Peter Oberparleiter | db0c2d5 | 2006-09-20 15:59:49 +0200 | [diff] [blame] | 282 | int len; |
Bastian Blank | f1fc78a | 2005-10-30 15:00:12 -0800 | [diff] [blame] | 283 | |
Peter Oberparleiter | db0c2d5 | 2006-09-20 15:59:49 +0200 | [diff] [blame] | 284 | len = snprint_alias(buf, PAGE_SIZE, "", id, "\n") + 1; |
| 285 | |
| 286 | return len > PAGE_SIZE ? PAGE_SIZE : len; |
Bastian Blank | f1fc78a | 2005-10-30 15:00:12 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 290 | online_show (struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | { |
| 292 | struct ccw_device *cdev = to_ccwdev(dev); |
| 293 | |
| 294 | return sprintf(buf, cdev->online ? "1\n" : "0\n"); |
| 295 | } |
| 296 | |
| 297 | static void |
| 298 | ccw_device_remove_disconnected(struct ccw_device *cdev) |
| 299 | { |
| 300 | struct subchannel *sch; |
| 301 | /* |
| 302 | * Forced offline in disconnected state means |
| 303 | * 'throw away device'. |
| 304 | */ |
| 305 | sch = to_subchannel(cdev->dev.parent); |
Cornelia Huck | 6ab4879 | 2006-07-12 16:39:50 +0200 | [diff] [blame] | 306 | css_sch_device_unregister(sch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | /* Reset intparm to zeroes. */ |
| 308 | sch->schib.pmcw.intparm = 0; |
| 309 | cio_modify(sch); |
| 310 | put_device(&sch->dev); |
| 311 | } |
| 312 | |
| 313 | int |
| 314 | ccw_device_set_offline(struct ccw_device *cdev) |
| 315 | { |
| 316 | int ret; |
| 317 | |
| 318 | if (!cdev) |
| 319 | return -ENODEV; |
| 320 | if (!cdev->online || !cdev->drv) |
| 321 | return -EINVAL; |
| 322 | |
| 323 | if (cdev->drv->set_offline) { |
| 324 | ret = cdev->drv->set_offline(cdev); |
| 325 | if (ret != 0) |
| 326 | return ret; |
| 327 | } |
| 328 | cdev->online = 0; |
| 329 | spin_lock_irq(cdev->ccwlock); |
| 330 | ret = ccw_device_offline(cdev); |
| 331 | if (ret == -ENODEV) { |
| 332 | if (cdev->private->state != DEV_STATE_NOT_OPER) { |
| 333 | cdev->private->state = DEV_STATE_OFFLINE; |
| 334 | dev_fsm_event(cdev, DEV_EVENT_NOTOPER); |
| 335 | } |
| 336 | spin_unlock_irq(cdev->ccwlock); |
| 337 | return ret; |
| 338 | } |
| 339 | spin_unlock_irq(cdev->ccwlock); |
| 340 | if (ret == 0) |
| 341 | wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev)); |
| 342 | else { |
| 343 | pr_debug("ccw_device_offline returned %d, device %s\n", |
| 344 | ret, cdev->dev.bus_id); |
| 345 | cdev->online = 1; |
| 346 | } |
| 347 | return ret; |
| 348 | } |
| 349 | |
| 350 | int |
| 351 | ccw_device_set_online(struct ccw_device *cdev) |
| 352 | { |
| 353 | int ret; |
| 354 | |
| 355 | if (!cdev) |
| 356 | return -ENODEV; |
| 357 | if (cdev->online || !cdev->drv) |
| 358 | return -EINVAL; |
| 359 | |
| 360 | spin_lock_irq(cdev->ccwlock); |
| 361 | ret = ccw_device_online(cdev); |
| 362 | spin_unlock_irq(cdev->ccwlock); |
| 363 | if (ret == 0) |
| 364 | wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev)); |
| 365 | else { |
| 366 | pr_debug("ccw_device_online returned %d, device %s\n", |
| 367 | ret, cdev->dev.bus_id); |
| 368 | return ret; |
| 369 | } |
| 370 | if (cdev->private->state != DEV_STATE_ONLINE) |
| 371 | return -ENODEV; |
| 372 | if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) { |
| 373 | cdev->online = 1; |
| 374 | return 0; |
| 375 | } |
| 376 | spin_lock_irq(cdev->ccwlock); |
| 377 | ret = ccw_device_offline(cdev); |
| 378 | spin_unlock_irq(cdev->ccwlock); |
| 379 | if (ret == 0) |
| 380 | wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev)); |
| 381 | else |
| 382 | pr_debug("ccw_device_offline returned %d, device %s\n", |
| 383 | ret, cdev->dev.bus_id); |
Cornelia Huck | 6cadb78 | 2006-02-17 13:52:49 -0800 | [diff] [blame] | 384 | return (ret == 0) ? -ENODEV : ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 388 | online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | { |
| 390 | struct ccw_device *cdev = to_ccwdev(dev); |
| 391 | int i, force, ret; |
| 392 | char *tmp; |
| 393 | |
Martin Schwidefsky | 973bd99 | 2006-01-06 00:19:07 -0800 | [diff] [blame] | 394 | if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | return -EAGAIN; |
| 396 | |
| 397 | if (cdev->drv && !try_module_get(cdev->drv->owner)) { |
| 398 | atomic_set(&cdev->private->onoff, 0); |
| 399 | return -EINVAL; |
| 400 | } |
| 401 | if (!strncmp(buf, "force\n", count)) { |
| 402 | force = 1; |
| 403 | i = 1; |
| 404 | } else { |
| 405 | force = 0; |
| 406 | i = simple_strtoul(buf, &tmp, 16); |
| 407 | } |
| 408 | if (i == 1) { |
| 409 | /* Do device recognition, if needed. */ |
| 410 | if (cdev->id.cu_type == 0) { |
| 411 | ret = ccw_device_recognition(cdev); |
| 412 | if (ret) { |
| 413 | printk(KERN_WARNING"Couldn't start recognition " |
| 414 | "for device %s (ret=%d)\n", |
| 415 | cdev->dev.bus_id, ret); |
| 416 | goto out; |
| 417 | } |
| 418 | wait_event(cdev->private->wait_q, |
| 419 | cdev->private->flags.recog_done); |
| 420 | } |
| 421 | if (cdev->drv && cdev->drv->set_online) |
| 422 | ccw_device_set_online(cdev); |
| 423 | } else if (i == 0) { |
| 424 | if (cdev->private->state == DEV_STATE_DISCONNECTED) |
| 425 | ccw_device_remove_disconnected(cdev); |
| 426 | else if (cdev->drv && cdev->drv->set_offline) |
| 427 | ccw_device_set_offline(cdev); |
| 428 | } |
| 429 | if (force && cdev->private->state == DEV_STATE_BOXED) { |
| 430 | ret = ccw_device_stlck(cdev); |
| 431 | if (ret) { |
| 432 | printk(KERN_WARNING"ccw_device_stlck for device %s " |
| 433 | "returned %d!\n", cdev->dev.bus_id, ret); |
| 434 | goto out; |
| 435 | } |
| 436 | /* Do device recognition, if needed. */ |
| 437 | if (cdev->id.cu_type == 0) { |
| 438 | cdev->private->state = DEV_STATE_NOT_OPER; |
| 439 | ret = ccw_device_recognition(cdev); |
| 440 | if (ret) { |
| 441 | printk(KERN_WARNING"Couldn't start recognition " |
| 442 | "for device %s (ret=%d)\n", |
| 443 | cdev->dev.bus_id, ret); |
| 444 | goto out; |
| 445 | } |
| 446 | wait_event(cdev->private->wait_q, |
| 447 | cdev->private->flags.recog_done); |
| 448 | } |
| 449 | if (cdev->drv && cdev->drv->set_online) |
| 450 | ccw_device_set_online(cdev); |
| 451 | } |
| 452 | out: |
| 453 | if (cdev->drv) |
| 454 | module_put(cdev->drv->owner); |
| 455 | atomic_set(&cdev->private->onoff, 0); |
| 456 | return count; |
| 457 | } |
| 458 | |
| 459 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 460 | available_show (struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | { |
| 462 | struct ccw_device *cdev = to_ccwdev(dev); |
| 463 | struct subchannel *sch; |
| 464 | |
| 465 | switch (cdev->private->state) { |
| 466 | case DEV_STATE_BOXED: |
| 467 | return sprintf(buf, "boxed\n"); |
| 468 | case DEV_STATE_DISCONNECTED: |
| 469 | case DEV_STATE_DISCONNECTED_SENSE_ID: |
| 470 | case DEV_STATE_NOT_OPER: |
| 471 | sch = to_subchannel(dev->parent); |
| 472 | if (!sch->lpm) |
| 473 | return sprintf(buf, "no path\n"); |
| 474 | else |
| 475 | return sprintf(buf, "no device\n"); |
| 476 | default: |
| 477 | /* All other states considered fine. */ |
| 478 | return sprintf(buf, "good\n"); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | static DEVICE_ATTR(chpids, 0444, chpids_show, NULL); |
| 483 | static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL); |
| 484 | static DEVICE_ATTR(devtype, 0444, devtype_show, NULL); |
| 485 | static DEVICE_ATTR(cutype, 0444, cutype_show, NULL); |
Bastian Blank | f1fc78a | 2005-10-30 15:00:12 -0800 | [diff] [blame] | 486 | static DEVICE_ATTR(modalias, 0444, modalias_show, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | static DEVICE_ATTR(online, 0644, online_show, online_store); |
| 488 | extern struct device_attribute dev_attr_cmb_enable; |
| 489 | static DEVICE_ATTR(availability, 0444, available_show, NULL); |
| 490 | |
| 491 | static struct attribute * subch_attrs[] = { |
| 492 | &dev_attr_chpids.attr, |
| 493 | &dev_attr_pimpampom.attr, |
| 494 | NULL, |
| 495 | }; |
| 496 | |
| 497 | static struct attribute_group subch_attr_group = { |
| 498 | .attrs = subch_attrs, |
| 499 | }; |
| 500 | |
| 501 | static inline int |
| 502 | subchannel_add_files (struct device *dev) |
| 503 | { |
| 504 | return sysfs_create_group(&dev->kobj, &subch_attr_group); |
| 505 | } |
| 506 | |
| 507 | static struct attribute * ccwdev_attrs[] = { |
| 508 | &dev_attr_devtype.attr, |
| 509 | &dev_attr_cutype.attr, |
Bastian Blank | f1fc78a | 2005-10-30 15:00:12 -0800 | [diff] [blame] | 510 | &dev_attr_modalias.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | &dev_attr_online.attr, |
| 512 | &dev_attr_cmb_enable.attr, |
| 513 | &dev_attr_availability.attr, |
| 514 | NULL, |
| 515 | }; |
| 516 | |
| 517 | static struct attribute_group ccwdev_attr_group = { |
| 518 | .attrs = ccwdev_attrs, |
| 519 | }; |
| 520 | |
| 521 | static inline int |
| 522 | device_add_files (struct device *dev) |
| 523 | { |
| 524 | return sysfs_create_group(&dev->kobj, &ccwdev_attr_group); |
| 525 | } |
| 526 | |
| 527 | static inline void |
| 528 | device_remove_files(struct device *dev) |
| 529 | { |
| 530 | sysfs_remove_group(&dev->kobj, &ccwdev_attr_group); |
| 531 | } |
| 532 | |
| 533 | /* this is a simple abstraction for device_register that sets the |
| 534 | * correct bus type and adds the bus specific files */ |
Cornelia Huck | 3c9da7b | 2006-10-27 12:39:33 +0200 | [diff] [blame^] | 535 | static int ccw_device_register(struct ccw_device *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | { |
| 537 | struct device *dev = &cdev->dev; |
| 538 | int ret; |
| 539 | |
| 540 | dev->bus = &ccw_bus_type; |
| 541 | |
| 542 | if ((ret = device_add(dev))) |
| 543 | return ret; |
| 544 | |
| 545 | set_bit(1, &cdev->private->registered); |
| 546 | if ((ret = device_add_files(dev))) { |
| 547 | if (test_and_clear_bit(1, &cdev->private->registered)) |
| 548 | device_del(dev); |
| 549 | } |
| 550 | return ret; |
| 551 | } |
| 552 | |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 553 | struct match_data { |
Cornelia Huck | 7896426 | 2006-10-11 15:31:38 +0200 | [diff] [blame] | 554 | struct ccw_dev_id dev_id; |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 555 | struct ccw_device * sibling; |
| 556 | }; |
| 557 | |
| 558 | static int |
| 559 | match_devno(struct device * dev, void * data) |
| 560 | { |
Cornelia Huck | 7896426 | 2006-10-11 15:31:38 +0200 | [diff] [blame] | 561 | struct match_data * d = data; |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 562 | struct ccw_device * cdev; |
| 563 | |
| 564 | cdev = to_ccwdev(dev); |
| 565 | if ((cdev->private->state == DEV_STATE_DISCONNECTED) && |
Cornelia Huck | 7896426 | 2006-10-11 15:31:38 +0200 | [diff] [blame] | 566 | ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) && |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 567 | (cdev != d->sibling)) { |
| 568 | cdev->private->state = DEV_STATE_NOT_OPER; |
| 569 | return 1; |
| 570 | } |
| 571 | return 0; |
| 572 | } |
| 573 | |
Cornelia Huck | 7896426 | 2006-10-11 15:31:38 +0200 | [diff] [blame] | 574 | static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id, |
| 575 | struct ccw_device *sibling) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | struct device *dev; |
Heiko Carstens | 292888c | 2006-08-30 14:33:35 +0200 | [diff] [blame] | 578 | struct match_data data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | |
Cornelia Huck | 7896426 | 2006-10-11 15:31:38 +0200 | [diff] [blame] | 580 | data.dev_id = *dev_id; |
Heiko Carstens | 292888c | 2006-08-30 14:33:35 +0200 | [diff] [blame] | 581 | data.sibling = sibling; |
Cornelia Huck | e5945b4 | 2005-10-11 08:28:59 -0700 | [diff] [blame] | 582 | dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 584 | return dev ? to_ccwdev(dev) : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | static void |
| 588 | ccw_device_add_changed(void *data) |
| 589 | { |
| 590 | |
| 591 | struct ccw_device *cdev; |
| 592 | |
Cornelia Huck | 12975ae | 2006-10-11 15:31:47 +0200 | [diff] [blame] | 593 | cdev = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | if (device_add(&cdev->dev)) { |
| 595 | put_device(&cdev->dev); |
| 596 | return; |
| 597 | } |
| 598 | set_bit(1, &cdev->private->registered); |
| 599 | if (device_add_files(&cdev->dev)) { |
| 600 | if (test_and_clear_bit(1, &cdev->private->registered)) |
| 601 | device_unregister(&cdev->dev); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | extern int css_get_ssd_info(struct subchannel *sch); |
| 606 | |
| 607 | void |
| 608 | ccw_device_do_unreg_rereg(void *data) |
| 609 | { |
| 610 | struct ccw_device *cdev; |
| 611 | struct subchannel *sch; |
| 612 | int need_rename; |
| 613 | |
Cornelia Huck | 12975ae | 2006-10-11 15:31:47 +0200 | [diff] [blame] | 614 | cdev = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | sch = to_subchannel(cdev->dev.parent); |
Cornelia Huck | 7896426 | 2006-10-11 15:31:38 +0200 | [diff] [blame] | 616 | if (cdev->private->dev_id.devno != sch->schib.pmcw.dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | /* |
| 618 | * The device number has changed. This is usually only when |
| 619 | * a device has been detached under VM and then re-appeared |
| 620 | * on another subchannel because of a different attachment |
| 621 | * order than before. Ideally, we should should just switch |
| 622 | * subchannels, but unfortunately, this is not possible with |
| 623 | * the current implementation. |
| 624 | * Instead, we search for the old subchannel for this device |
| 625 | * number and deregister so there are no collisions with the |
| 626 | * newly registered ccw_device. |
| 627 | * FIXME: Find another solution so the block layer doesn't |
| 628 | * get possibly sick... |
| 629 | */ |
| 630 | struct ccw_device *other_cdev; |
Cornelia Huck | 7896426 | 2006-10-11 15:31:38 +0200 | [diff] [blame] | 631 | struct ccw_dev_id dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
| 633 | need_rename = 1; |
Cornelia Huck | 7896426 | 2006-10-11 15:31:38 +0200 | [diff] [blame] | 634 | dev_id.devno = sch->schib.pmcw.dev; |
| 635 | dev_id.ssid = sch->schid.ssid; |
| 636 | other_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | if (other_cdev) { |
| 638 | struct subchannel *other_sch; |
| 639 | |
| 640 | other_sch = to_subchannel(other_cdev->dev.parent); |
| 641 | if (get_device(&other_sch->dev)) { |
Cornelia Huck | a8237fc | 2006-01-06 00:19:21 -0800 | [diff] [blame] | 642 | stsch(other_sch->schid, &other_sch->schib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | if (other_sch->schib.pmcw.dnv) { |
| 644 | other_sch->schib.pmcw.intparm = 0; |
| 645 | cio_modify(other_sch); |
| 646 | } |
Cornelia Huck | 6ab4879 | 2006-07-12 16:39:50 +0200 | [diff] [blame] | 647 | css_sch_device_unregister(other_sch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | /* Update ssd info here. */ |
| 651 | css_get_ssd_info(sch); |
Cornelia Huck | 7896426 | 2006-10-11 15:31:38 +0200 | [diff] [blame] | 652 | cdev->private->dev_id.devno = sch->schib.pmcw.dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | } else |
| 654 | need_rename = 0; |
| 655 | device_remove_files(&cdev->dev); |
| 656 | if (test_and_clear_bit(1, &cdev->private->registered)) |
| 657 | device_del(&cdev->dev); |
| 658 | if (need_rename) |
Cornelia Huck | fb6958a | 2006-01-06 00:19:25 -0800 | [diff] [blame] | 659 | snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x", |
| 660 | sch->schid.ssid, sch->schib.pmcw.dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | PREPARE_WORK(&cdev->private->kick_work, |
Cornelia Huck | 12975ae | 2006-10-11 15:31:47 +0200 | [diff] [blame] | 662 | ccw_device_add_changed, cdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | queue_work(ccw_device_work, &cdev->private->kick_work); |
| 664 | } |
| 665 | |
| 666 | static void |
| 667 | ccw_device_release(struct device *dev) |
| 668 | { |
| 669 | struct ccw_device *cdev; |
| 670 | |
| 671 | cdev = to_ccwdev(dev); |
| 672 | kfree(cdev->private); |
| 673 | kfree(cdev); |
| 674 | } |
| 675 | |
| 676 | /* |
| 677 | * Register recognized device. |
| 678 | */ |
| 679 | static void |
| 680 | io_subchannel_register(void *data) |
| 681 | { |
| 682 | struct ccw_device *cdev; |
| 683 | struct subchannel *sch; |
| 684 | int ret; |
| 685 | unsigned long flags; |
| 686 | |
Cornelia Huck | 12975ae | 2006-10-11 15:31:47 +0200 | [diff] [blame] | 687 | cdev = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | sch = to_subchannel(cdev->dev.parent); |
| 689 | |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 690 | if (klist_node_attached(&cdev->dev.knode_parent)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | bus_rescan_devices(&ccw_bus_type); |
| 692 | goto out; |
| 693 | } |
| 694 | /* make it known to the system */ |
| 695 | ret = ccw_device_register(cdev); |
| 696 | if (ret) { |
| 697 | printk (KERN_WARNING "%s: could not register %s\n", |
| 698 | __func__, cdev->dev.bus_id); |
| 699 | put_device(&cdev->dev); |
| 700 | spin_lock_irqsave(&sch->lock, flags); |
| 701 | sch->dev.driver_data = NULL; |
| 702 | spin_unlock_irqrestore(&sch->lock, flags); |
| 703 | kfree (cdev->private); |
| 704 | kfree (cdev); |
| 705 | put_device(&sch->dev); |
| 706 | if (atomic_dec_and_test(&ccw_device_init_count)) |
| 707 | wake_up(&ccw_device_init_wq); |
| 708 | return; |
| 709 | } |
| 710 | |
| 711 | ret = subchannel_add_files(cdev->dev.parent); |
| 712 | if (ret) |
| 713 | printk(KERN_WARNING "%s: could not add attributes to %s\n", |
| 714 | __func__, sch->dev.bus_id); |
| 715 | put_device(&cdev->dev); |
| 716 | out: |
| 717 | cdev->private->flags.recog_done = 1; |
| 718 | put_device(&sch->dev); |
| 719 | wake_up(&cdev->private->wait_q); |
| 720 | if (atomic_dec_and_test(&ccw_device_init_count)) |
| 721 | wake_up(&ccw_device_init_wq); |
| 722 | } |
| 723 | |
| 724 | void |
| 725 | ccw_device_call_sch_unregister(void *data) |
| 726 | { |
| 727 | struct ccw_device *cdev = data; |
| 728 | struct subchannel *sch; |
| 729 | |
| 730 | sch = to_subchannel(cdev->dev.parent); |
Cornelia Huck | 6ab4879 | 2006-07-12 16:39:50 +0200 | [diff] [blame] | 731 | css_sch_device_unregister(sch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | /* Reset intparm to zeroes. */ |
| 733 | sch->schib.pmcw.intparm = 0; |
| 734 | cio_modify(sch); |
| 735 | put_device(&cdev->dev); |
| 736 | put_device(&sch->dev); |
| 737 | } |
| 738 | |
| 739 | /* |
| 740 | * subchannel recognition done. Called from the state machine. |
| 741 | */ |
| 742 | void |
| 743 | io_subchannel_recog_done(struct ccw_device *cdev) |
| 744 | { |
| 745 | struct subchannel *sch; |
| 746 | |
| 747 | if (css_init_done == 0) { |
| 748 | cdev->private->flags.recog_done = 1; |
| 749 | return; |
| 750 | } |
| 751 | switch (cdev->private->state) { |
| 752 | case DEV_STATE_NOT_OPER: |
| 753 | cdev->private->flags.recog_done = 1; |
| 754 | /* Remove device found not operational. */ |
| 755 | if (!get_device(&cdev->dev)) |
| 756 | break; |
| 757 | sch = to_subchannel(cdev->dev.parent); |
| 758 | PREPARE_WORK(&cdev->private->kick_work, |
Cornelia Huck | 12975ae | 2006-10-11 15:31:47 +0200 | [diff] [blame] | 759 | ccw_device_call_sch_unregister, cdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | queue_work(slow_path_wq, &cdev->private->kick_work); |
| 761 | if (atomic_dec_and_test(&ccw_device_init_count)) |
| 762 | wake_up(&ccw_device_init_wq); |
| 763 | break; |
| 764 | case DEV_STATE_BOXED: |
| 765 | /* Device did not respond in time. */ |
| 766 | case DEV_STATE_OFFLINE: |
| 767 | /* |
| 768 | * We can't register the device in interrupt context so |
| 769 | * we schedule a work item. |
| 770 | */ |
| 771 | if (!get_device(&cdev->dev)) |
| 772 | break; |
| 773 | PREPARE_WORK(&cdev->private->kick_work, |
Cornelia Huck | 12975ae | 2006-10-11 15:31:47 +0200 | [diff] [blame] | 774 | io_subchannel_register, cdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | queue_work(slow_path_wq, &cdev->private->kick_work); |
| 776 | break; |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | static int |
| 781 | io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch) |
| 782 | { |
| 783 | int rc; |
| 784 | struct ccw_device_private *priv; |
| 785 | |
| 786 | sch->dev.driver_data = cdev; |
| 787 | sch->driver = &io_subchannel_driver; |
| 788 | cdev->ccwlock = &sch->lock; |
Cornelia Huck | fb6958a | 2006-01-06 00:19:25 -0800 | [diff] [blame] | 789 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | /* Init private data. */ |
| 791 | priv = cdev->private; |
Cornelia Huck | 7896426 | 2006-10-11 15:31:38 +0200 | [diff] [blame] | 792 | priv->dev_id.devno = sch->schib.pmcw.dev; |
| 793 | priv->dev_id.ssid = sch->schid.ssid; |
| 794 | priv->schid = sch->schid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | priv->state = DEV_STATE_NOT_OPER; |
| 796 | INIT_LIST_HEAD(&priv->cmb_list); |
| 797 | init_waitqueue_head(&priv->wait_q); |
| 798 | init_timer(&priv->timer); |
| 799 | |
| 800 | /* Set an initial name for the device. */ |
Cornelia Huck | fb6958a | 2006-01-06 00:19:25 -0800 | [diff] [blame] | 801 | snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x", |
| 802 | sch->schid.ssid, sch->schib.pmcw.dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | |
| 804 | /* Increase counter of devices currently in recognition. */ |
| 805 | atomic_inc(&ccw_device_init_count); |
| 806 | |
| 807 | /* Start async. device sensing. */ |
| 808 | spin_lock_irq(&sch->lock); |
| 809 | rc = ccw_device_recognition(cdev); |
| 810 | spin_unlock_irq(&sch->lock); |
| 811 | if (rc) { |
| 812 | if (atomic_dec_and_test(&ccw_device_init_count)) |
| 813 | wake_up(&ccw_device_init_wq); |
| 814 | } |
| 815 | return rc; |
| 816 | } |
| 817 | |
| 818 | static int |
Cornelia Huck | 8bbace7 | 2006-01-11 10:56:22 +0100 | [diff] [blame] | 819 | io_subchannel_probe (struct subchannel *sch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | struct ccw_device *cdev; |
| 822 | int rc; |
| 823 | unsigned long flags; |
| 824 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | if (sch->dev.driver_data) { |
| 826 | /* |
| 827 | * This subchannel already has an associated ccw_device. |
| 828 | * Register it and exit. This happens for all early |
| 829 | * device, e.g. the console. |
| 830 | */ |
| 831 | cdev = sch->dev.driver_data; |
| 832 | device_initialize(&cdev->dev); |
| 833 | ccw_device_register(cdev); |
| 834 | subchannel_add_files(&sch->dev); |
| 835 | /* |
| 836 | * Check if the device is already online. If it is |
| 837 | * the reference count needs to be corrected |
| 838 | * (see ccw_device_online and css_init_done for the |
| 839 | * ugly details). |
| 840 | */ |
| 841 | if (cdev->private->state != DEV_STATE_NOT_OPER && |
| 842 | cdev->private->state != DEV_STATE_OFFLINE && |
| 843 | cdev->private->state != DEV_STATE_BOXED) |
| 844 | get_device(&cdev->dev); |
| 845 | return 0; |
| 846 | } |
Eric Sesterhenn | 88abaab | 2006-03-24 03:15:31 -0800 | [diff] [blame] | 847 | cdev = kzalloc (sizeof(*cdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | if (!cdev) |
| 849 | return -ENOMEM; |
Eric Sesterhenn | 88abaab | 2006-03-24 03:15:31 -0800 | [diff] [blame] | 850 | cdev->private = kzalloc(sizeof(struct ccw_device_private), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | GFP_KERNEL | GFP_DMA); |
| 852 | if (!cdev->private) { |
| 853 | kfree(cdev); |
| 854 | return -ENOMEM; |
| 855 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | atomic_set(&cdev->private->onoff, 0); |
Heiko Carstens | 292888c | 2006-08-30 14:33:35 +0200 | [diff] [blame] | 857 | cdev->dev.parent = &sch->dev; |
| 858 | cdev->dev.release = ccw_device_release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | INIT_LIST_HEAD(&cdev->private->kick_work.entry); |
| 860 | /* Do first half of device_register. */ |
| 861 | device_initialize(&cdev->dev); |
| 862 | |
| 863 | if (!get_device(&sch->dev)) { |
| 864 | if (cdev->dev.release) |
| 865 | cdev->dev.release(&cdev->dev); |
| 866 | return -ENODEV; |
| 867 | } |
| 868 | |
Cornelia Huck | 8bbace7 | 2006-01-11 10:56:22 +0100 | [diff] [blame] | 869 | rc = io_subchannel_recog(cdev, sch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | if (rc) { |
| 871 | spin_lock_irqsave(&sch->lock, flags); |
| 872 | sch->dev.driver_data = NULL; |
| 873 | spin_unlock_irqrestore(&sch->lock, flags); |
| 874 | if (cdev->dev.release) |
| 875 | cdev->dev.release(&cdev->dev); |
| 876 | } |
| 877 | |
| 878 | return rc; |
| 879 | } |
| 880 | |
| 881 | static void |
| 882 | ccw_device_unregister(void *data) |
| 883 | { |
| 884 | struct ccw_device *cdev; |
| 885 | |
| 886 | cdev = (struct ccw_device *)data; |
| 887 | if (test_and_clear_bit(1, &cdev->private->registered)) |
| 888 | device_unregister(&cdev->dev); |
| 889 | put_device(&cdev->dev); |
| 890 | } |
| 891 | |
| 892 | static int |
Cornelia Huck | 8bbace7 | 2006-01-11 10:56:22 +0100 | [diff] [blame] | 893 | io_subchannel_remove (struct subchannel *sch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | { |
| 895 | struct ccw_device *cdev; |
| 896 | unsigned long flags; |
| 897 | |
Cornelia Huck | 8bbace7 | 2006-01-11 10:56:22 +0100 | [diff] [blame] | 898 | if (!sch->dev.driver_data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | return 0; |
Cornelia Huck | 8bbace7 | 2006-01-11 10:56:22 +0100 | [diff] [blame] | 900 | cdev = sch->dev.driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | /* Set ccw device to not operational and drop reference. */ |
| 902 | spin_lock_irqsave(cdev->ccwlock, flags); |
Cornelia Huck | 8bbace7 | 2006-01-11 10:56:22 +0100 | [diff] [blame] | 903 | sch->dev.driver_data = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | cdev->private->state = DEV_STATE_NOT_OPER; |
| 905 | spin_unlock_irqrestore(cdev->ccwlock, flags); |
| 906 | /* |
| 907 | * Put unregistration on workqueue to avoid livelocks on the css bus |
| 908 | * semaphore. |
| 909 | */ |
| 910 | if (get_device(&cdev->dev)) { |
| 911 | PREPARE_WORK(&cdev->private->kick_work, |
Cornelia Huck | 12975ae | 2006-10-11 15:31:47 +0200 | [diff] [blame] | 912 | ccw_device_unregister, cdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | queue_work(ccw_device_work, &cdev->private->kick_work); |
| 914 | } |
| 915 | return 0; |
| 916 | } |
| 917 | |
| 918 | static int |
| 919 | io_subchannel_notify(struct device *dev, int event) |
| 920 | { |
| 921 | struct ccw_device *cdev; |
| 922 | |
| 923 | cdev = dev->driver_data; |
| 924 | if (!cdev) |
| 925 | return 0; |
| 926 | if (!cdev->drv) |
| 927 | return 0; |
| 928 | if (!cdev->online) |
| 929 | return 0; |
| 930 | return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0; |
| 931 | } |
| 932 | |
| 933 | static void |
| 934 | io_subchannel_verify(struct device *dev) |
| 935 | { |
| 936 | struct ccw_device *cdev; |
| 937 | |
| 938 | cdev = dev->driver_data; |
| 939 | if (cdev) |
| 940 | dev_fsm_event(cdev, DEV_EVENT_VERIFY); |
| 941 | } |
| 942 | |
| 943 | static void |
| 944 | io_subchannel_ioterm(struct device *dev) |
| 945 | { |
| 946 | struct ccw_device *cdev; |
| 947 | |
| 948 | cdev = dev->driver_data; |
| 949 | if (!cdev) |
| 950 | return; |
| 951 | cdev->private->state = DEV_STATE_CLEAR_VERIFY; |
| 952 | if (cdev->handler) |
| 953 | cdev->handler(cdev, cdev->private->intparm, |
| 954 | ERR_PTR(-EIO)); |
| 955 | } |
| 956 | |
| 957 | static void |
Cornelia Huck | 8bbace7 | 2006-01-11 10:56:22 +0100 | [diff] [blame] | 958 | io_subchannel_shutdown(struct subchannel *sch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | struct ccw_device *cdev; |
| 961 | int ret; |
| 962 | |
Cornelia Huck | 8bbace7 | 2006-01-11 10:56:22 +0100 | [diff] [blame] | 963 | cdev = sch->dev.driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | |
Cornelia Huck | a8237fc | 2006-01-06 00:19:21 -0800 | [diff] [blame] | 965 | if (cio_is_console(sch->schid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | return; |
| 967 | if (!sch->schib.pmcw.ena) |
| 968 | /* Nothing to do. */ |
| 969 | return; |
| 970 | ret = cio_disable_subchannel(sch); |
| 971 | if (ret != -EBUSY) |
| 972 | /* Subchannel is disabled, we're done. */ |
| 973 | return; |
| 974 | cdev->private->state = DEV_STATE_QUIESCE; |
| 975 | if (cdev->handler) |
| 976 | cdev->handler(cdev, cdev->private->intparm, |
| 977 | ERR_PTR(-EIO)); |
| 978 | ret = ccw_device_cancel_halt_clear(cdev); |
| 979 | if (ret == -EBUSY) { |
| 980 | ccw_device_set_timeout(cdev, HZ/10); |
| 981 | wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev)); |
| 982 | } |
| 983 | cio_disable_subchannel(sch); |
| 984 | } |
| 985 | |
| 986 | #ifdef CONFIG_CCW_CONSOLE |
| 987 | static struct ccw_device console_cdev; |
| 988 | static struct ccw_device_private console_private; |
| 989 | static int console_cdev_in_use; |
| 990 | |
| 991 | static int |
| 992 | ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch) |
| 993 | { |
| 994 | int rc; |
| 995 | |
| 996 | /* Initialize the ccw_device structure. */ |
Heiko Carstens | 292888c | 2006-08-30 14:33:35 +0200 | [diff] [blame] | 997 | cdev->dev.parent= &sch->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | rc = io_subchannel_recog(cdev, sch); |
| 999 | if (rc) |
| 1000 | return rc; |
| 1001 | |
| 1002 | /* Now wait for the async. recognition to come to an end. */ |
| 1003 | spin_lock_irq(cdev->ccwlock); |
| 1004 | while (!dev_fsm_final_state(cdev)) |
| 1005 | wait_cons_dev(); |
| 1006 | rc = -EIO; |
| 1007 | if (cdev->private->state != DEV_STATE_OFFLINE) |
| 1008 | goto out_unlock; |
| 1009 | ccw_device_online(cdev); |
| 1010 | while (!dev_fsm_final_state(cdev)) |
| 1011 | wait_cons_dev(); |
| 1012 | if (cdev->private->state != DEV_STATE_ONLINE) |
| 1013 | goto out_unlock; |
| 1014 | rc = 0; |
| 1015 | out_unlock: |
| 1016 | spin_unlock_irq(cdev->ccwlock); |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
| 1020 | struct ccw_device * |
| 1021 | ccw_device_probe_console(void) |
| 1022 | { |
| 1023 | struct subchannel *sch; |
| 1024 | int ret; |
| 1025 | |
| 1026 | if (xchg(&console_cdev_in_use, 1) != 0) |
Peter Oberparleiter | 600b5d1 | 2006-02-01 03:06:35 -0800 | [diff] [blame] | 1027 | return ERR_PTR(-EBUSY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | sch = cio_probe_console(); |
| 1029 | if (IS_ERR(sch)) { |
| 1030 | console_cdev_in_use = 0; |
| 1031 | return (void *) sch; |
| 1032 | } |
| 1033 | memset(&console_cdev, 0, sizeof(struct ccw_device)); |
| 1034 | memset(&console_private, 0, sizeof(struct ccw_device_private)); |
| 1035 | console_cdev.private = &console_private; |
| 1036 | ret = ccw_device_console_enable(&console_cdev, sch); |
| 1037 | if (ret) { |
| 1038 | cio_release_console(); |
| 1039 | console_cdev_in_use = 0; |
| 1040 | return ERR_PTR(ret); |
| 1041 | } |
| 1042 | console_cdev.online = 1; |
| 1043 | return &console_cdev; |
| 1044 | } |
| 1045 | #endif |
| 1046 | |
| 1047 | /* |
| 1048 | * get ccw_device matching the busid, but only if owned by cdrv |
| 1049 | */ |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 1050 | static int |
| 1051 | __ccwdev_check_busid(struct device *dev, void *id) |
| 1052 | { |
| 1053 | char *bus_id; |
| 1054 | |
Cornelia Huck | 12975ae | 2006-10-11 15:31:47 +0200 | [diff] [blame] | 1055 | bus_id = id; |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 1056 | |
| 1057 | return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0); |
| 1058 | } |
| 1059 | |
| 1060 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | struct ccw_device * |
| 1062 | get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id) |
| 1063 | { |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 1064 | struct device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | struct device_driver *drv; |
| 1066 | |
| 1067 | drv = get_driver(&cdrv->driver); |
| 1068 | if (!drv) |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 1069 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 1071 | dev = driver_find_device(drv, NULL, (void *)bus_id, |
| 1072 | __ccwdev_check_busid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | put_driver(drv); |
| 1074 | |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 1075 | return dev ? to_ccwdev(dev) : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | /************************** device driver handling ************************/ |
| 1079 | |
| 1080 | /* This is the implementation of the ccw_driver class. The probe, remove |
| 1081 | * and release methods are initially very similar to the device_driver |
| 1082 | * implementations, with the difference that they have ccw_device |
| 1083 | * arguments. |
| 1084 | * |
| 1085 | * A ccw driver also contains the information that is needed for |
| 1086 | * device matching. |
| 1087 | */ |
| 1088 | static int |
| 1089 | ccw_device_probe (struct device *dev) |
| 1090 | { |
| 1091 | struct ccw_device *cdev = to_ccwdev(dev); |
| 1092 | struct ccw_driver *cdrv = to_ccwdrv(dev->driver); |
| 1093 | int ret; |
| 1094 | |
| 1095 | cdev->drv = cdrv; /* to let the driver call _set_online */ |
| 1096 | |
| 1097 | ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV; |
| 1098 | |
| 1099 | if (ret) { |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 1100 | cdev->drv = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | return ret; |
| 1102 | } |
| 1103 | |
| 1104 | return 0; |
| 1105 | } |
| 1106 | |
| 1107 | static int |
| 1108 | ccw_device_remove (struct device *dev) |
| 1109 | { |
| 1110 | struct ccw_device *cdev = to_ccwdev(dev); |
| 1111 | struct ccw_driver *cdrv = cdev->drv; |
| 1112 | int ret; |
| 1113 | |
| 1114 | pr_debug("removing device %s\n", cdev->dev.bus_id); |
| 1115 | if (cdrv->remove) |
| 1116 | cdrv->remove(cdev); |
| 1117 | if (cdev->online) { |
| 1118 | cdev->online = 0; |
| 1119 | spin_lock_irq(cdev->ccwlock); |
| 1120 | ret = ccw_device_offline(cdev); |
| 1121 | spin_unlock_irq(cdev->ccwlock); |
| 1122 | if (ret == 0) |
| 1123 | wait_event(cdev->private->wait_q, |
| 1124 | dev_fsm_final_state(cdev)); |
| 1125 | else |
| 1126 | //FIXME: we can't fail! |
| 1127 | pr_debug("ccw_device_offline returned %d, device %s\n", |
| 1128 | ret, cdev->dev.bus_id); |
| 1129 | } |
| 1130 | ccw_device_set_timeout(cdev, 0); |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 1131 | cdev->drv = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | return 0; |
| 1133 | } |
| 1134 | |
Cornelia Huck | 8bbace7 | 2006-01-11 10:56:22 +0100 | [diff] [blame] | 1135 | struct bus_type ccw_bus_type = { |
| 1136 | .name = "ccw", |
| 1137 | .match = ccw_bus_match, |
| 1138 | .uevent = ccw_uevent, |
| 1139 | .probe = ccw_device_probe, |
| 1140 | .remove = ccw_device_remove, |
| 1141 | }; |
| 1142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | int |
| 1144 | ccw_driver_register (struct ccw_driver *cdriver) |
| 1145 | { |
| 1146 | struct device_driver *drv = &cdriver->driver; |
| 1147 | |
| 1148 | drv->bus = &ccw_bus_type; |
| 1149 | drv->name = cdriver->name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | |
| 1151 | return driver_register(drv); |
| 1152 | } |
| 1153 | |
| 1154 | void |
| 1155 | ccw_driver_unregister (struct ccw_driver *cdriver) |
| 1156 | { |
| 1157 | driver_unregister(&cdriver->driver); |
| 1158 | } |
| 1159 | |
Cornelia Huck | a8237fc | 2006-01-06 00:19:21 -0800 | [diff] [blame] | 1160 | /* Helper func for qdio. */ |
| 1161 | struct subchannel_id |
| 1162 | ccw_device_get_subchannel_id(struct ccw_device *cdev) |
| 1163 | { |
| 1164 | struct subchannel *sch; |
| 1165 | |
| 1166 | sch = to_subchannel(cdev->dev.parent); |
| 1167 | return sch->schid; |
| 1168 | } |
| 1169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | MODULE_LICENSE("GPL"); |
| 1171 | EXPORT_SYMBOL(ccw_device_set_online); |
| 1172 | EXPORT_SYMBOL(ccw_device_set_offline); |
| 1173 | EXPORT_SYMBOL(ccw_driver_register); |
| 1174 | EXPORT_SYMBOL(ccw_driver_unregister); |
| 1175 | EXPORT_SYMBOL(get_ccwdev_by_busid); |
| 1176 | EXPORT_SYMBOL(ccw_bus_type); |
| 1177 | EXPORT_SYMBOL(ccw_device_work); |
| 1178 | EXPORT_SYMBOL(ccw_device_notify_work); |
Cornelia Huck | a8237fc | 2006-01-06 00:19:21 -0800 | [diff] [blame] | 1179 | EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id); |