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