Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * drivers/s390/cio/device.c | |
3 | * bus driver for ccw devices | |
1da177e4 | 4 | * |
c820de39 | 5 | * Copyright IBM Corp. 2002,2008 |
1da177e4 | 6 | * Author(s): Arnd Bergmann (arndb@de.ibm.com) |
4ce3b30c | 7 | * Cornelia Huck (cornelia.huck@de.ibm.com) |
1da177e4 LT |
8 | * Martin Schwidefsky (schwidefsky@de.ibm.com) |
9 | */ | |
1da177e4 LT |
10 | #include <linux/module.h> |
11 | #include <linux/init.h> | |
12 | #include <linux/spinlock.h> | |
13 | #include <linux/errno.h> | |
14 | #include <linux/err.h> | |
15 | #include <linux/slab.h> | |
16 | #include <linux/list.h> | |
17 | #include <linux/device.h> | |
18 | #include <linux/workqueue.h> | |
90ab1336 | 19 | #include <linux/timer.h> |
1da177e4 LT |
20 | |
21 | #include <asm/ccwdev.h> | |
22 | #include <asm/cio.h> | |
4e57b681 | 23 | #include <asm/param.h> /* HZ */ |
1842f2b1 | 24 | #include <asm/cmb.h> |
3a3fc29a | 25 | #include <asm/isc.h> |
1da177e4 | 26 | |
0ae7a7b2 | 27 | #include "chp.h" |
1da177e4 | 28 | #include "cio.h" |
d7b5a4c9 | 29 | #include "cio_debug.h" |
1da177e4 LT |
30 | #include "css.h" |
31 | #include "device.h" | |
32 | #include "ioasm.h" | |
cd6b4f27 | 33 | #include "io_sch.h" |
ecf5d9ef | 34 | #include "blacklist.h" |
1da177e4 | 35 | |
90ab1336 | 36 | static struct timer_list recovery_timer; |
486d0a00 | 37 | static DEFINE_SPINLOCK(recovery_lock); |
90ab1336 PO |
38 | static int recovery_phase; |
39 | static const unsigned long recovery_delay[] = { 3, 30, 300 }; | |
40 | ||
1da177e4 LT |
41 | /******************* bus type handling ***********************/ |
42 | ||
43 | /* The Linux driver model distinguishes between a bus type and | |
44 | * the bus itself. Of course we only have one channel | |
45 | * subsystem driver and one channel system per machine, but | |
46 | * we still use the abstraction. T.R. says it's a good idea. */ | |
47 | static int | |
48 | ccw_bus_match (struct device * dev, struct device_driver * drv) | |
49 | { | |
50 | struct ccw_device *cdev = to_ccwdev(dev); | |
51 | struct ccw_driver *cdrv = to_ccwdrv(drv); | |
52 | const struct ccw_device_id *ids = cdrv->ids, *found; | |
53 | ||
54 | if (!ids) | |
55 | return 0; | |
56 | ||
57 | found = ccw_device_id_match(ids, &cdev->id); | |
58 | if (!found) | |
59 | return 0; | |
60 | ||
61 | cdev->id.driver_info = found->driver_info; | |
62 | ||
63 | return 1; | |
64 | } | |
65 | ||
db0c2d59 PO |
66 | /* Store modalias string delimited by prefix/suffix string into buffer with |
67 | * specified size. Return length of resulting string (excluding trailing '\0') | |
68 | * even if string doesn't fit buffer (snprintf semantics). */ | |
cfbe9bb2 | 69 | static int snprint_alias(char *buf, size_t size, |
db0c2d59 | 70 | struct ccw_device_id *id, const char *suffix) |
1da177e4 | 71 | { |
db0c2d59 | 72 | int len; |
1da177e4 | 73 | |
cfbe9bb2 | 74 | len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model); |
db0c2d59 PO |
75 | if (len > size) |
76 | return len; | |
77 | buf += len; | |
78 | size -= len; | |
1da177e4 | 79 | |
db0c2d59 PO |
80 | if (id->dev_type != 0) |
81 | len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type, | |
82 | id->dev_model, suffix); | |
83 | else | |
84 | len += snprintf(buf, size, "dtdm%s", suffix); | |
1da177e4 | 85 | |
db0c2d59 PO |
86 | return len; |
87 | } | |
88 | ||
89 | /* Set up environment variables for ccw device uevent. Return 0 on success, | |
90 | * non-zero otherwise. */ | |
7eff2e7a | 91 | static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env) |
db0c2d59 PO |
92 | { |
93 | struct ccw_device *cdev = to_ccwdev(dev); | |
94 | struct ccw_device_id *id = &(cdev->id); | |
cfbe9bb2 CH |
95 | int ret; |
96 | char modalias_buf[30]; | |
1da177e4 | 97 | |
db0c2d59 | 98 | /* CU_TYPE= */ |
7eff2e7a | 99 | ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type); |
cfbe9bb2 CH |
100 | if (ret) |
101 | return ret; | |
db0c2d59 PO |
102 | |
103 | /* CU_MODEL= */ | |
7eff2e7a | 104 | ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model); |
cfbe9bb2 CH |
105 | if (ret) |
106 | return ret; | |
1da177e4 LT |
107 | |
108 | /* The next two can be zero, that's ok for us */ | |
db0c2d59 | 109 | /* DEV_TYPE= */ |
7eff2e7a | 110 | ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type); |
cfbe9bb2 CH |
111 | if (ret) |
112 | return ret; | |
1da177e4 | 113 | |
db0c2d59 | 114 | /* DEV_MODEL= */ |
7eff2e7a | 115 | ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model); |
cfbe9bb2 CH |
116 | if (ret) |
117 | return ret; | |
db0c2d59 PO |
118 | |
119 | /* MODALIAS= */ | |
cfbe9bb2 | 120 | snprint_alias(modalias_buf, sizeof(modalias_buf), id, ""); |
7eff2e7a KS |
121 | ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf); |
122 | return ret; | |
1da177e4 LT |
123 | } |
124 | ||
8bbace7e | 125 | struct bus_type ccw_bus_type; |
1da177e4 | 126 | |
602b20f2 CH |
127 | static void io_subchannel_irq(struct subchannel *); |
128 | static int io_subchannel_probe(struct subchannel *); | |
129 | static int io_subchannel_remove(struct subchannel *); | |
8bbace7e | 130 | static void io_subchannel_shutdown(struct subchannel *); |
c820de39 | 131 | static int io_subchannel_sch_event(struct subchannel *, int); |
99611f87 CH |
132 | static int io_subchannel_chp_event(struct subchannel *, struct chp_link *, |
133 | int); | |
1da177e4 | 134 | |
f08adc00 CH |
135 | static struct css_device_id io_subchannel_ids[] = { |
136 | { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, }, | |
137 | { /* end of list */ }, | |
138 | }; | |
139 | MODULE_DEVICE_TABLE(css, io_subchannel_ids); | |
140 | ||
f7e5d67c | 141 | static struct css_driver io_subchannel_driver = { |
4beee646 | 142 | .owner = THIS_MODULE, |
f08adc00 | 143 | .subchannel_type = io_subchannel_ids, |
25b7bb58 | 144 | .name = "io_subchannel", |
1da177e4 | 145 | .irq = io_subchannel_irq, |
c820de39 CH |
146 | .sch_event = io_subchannel_sch_event, |
147 | .chp_event = io_subchannel_chp_event, | |
8bbace7e CH |
148 | .probe = io_subchannel_probe, |
149 | .remove = io_subchannel_remove, | |
150 | .shutdown = io_subchannel_shutdown, | |
1da177e4 LT |
151 | }; |
152 | ||
153 | struct workqueue_struct *ccw_device_work; | |
40154b82 PO |
154 | wait_queue_head_t ccw_device_init_wq; |
155 | atomic_t ccw_device_init_count; | |
1da177e4 | 156 | |
90ab1336 PO |
157 | static void recovery_func(unsigned long data); |
158 | ||
1da177e4 LT |
159 | static int __init |
160 | init_ccw_bus_type (void) | |
161 | { | |
162 | int ret; | |
163 | ||
164 | init_waitqueue_head(&ccw_device_init_wq); | |
165 | atomic_set(&ccw_device_init_count, 0); | |
90ab1336 | 166 | setup_timer(&recovery_timer, recovery_func, 0); |
1da177e4 LT |
167 | |
168 | ccw_device_work = create_singlethread_workqueue("cio"); | |
169 | if (!ccw_device_work) | |
170 | return -ENOMEM; /* FIXME: better errno ? */ | |
1da177e4 LT |
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 | ||
25b7bb58 CH |
179 | ret = css_driver_register(&io_subchannel_driver); |
180 | if (ret) | |
1da177e4 LT |
181 | goto out_err; |
182 | ||
183 | wait_event(ccw_device_init_wq, | |
184 | atomic_read(&ccw_device_init_count) == 0); | |
185 | flush_workqueue(ccw_device_work); | |
186 | return 0; | |
187 | out_err: | |
188 | if (ccw_device_work) | |
189 | destroy_workqueue(ccw_device_work); | |
1da177e4 LT |
190 | if (slow_path_wq) |
191 | destroy_workqueue(slow_path_wq); | |
192 | return ret; | |
193 | } | |
194 | ||
195 | static void __exit | |
196 | cleanup_ccw_bus_type (void) | |
197 | { | |
25b7bb58 | 198 | css_driver_unregister(&io_subchannel_driver); |
1da177e4 | 199 | bus_unregister(&ccw_bus_type); |
1da177e4 LT |
200 | destroy_workqueue(ccw_device_work); |
201 | } | |
202 | ||
203 | subsys_initcall(init_ccw_bus_type); | |
204 | module_exit(cleanup_ccw_bus_type); | |
205 | ||
206 | /************************ device handling **************************/ | |
207 | ||
208 | /* | |
209 | * A ccw_device has some interfaces in sysfs in addition to the | |
210 | * standard ones. | |
211 | * The following entries are designed to export the information which | |
212 | * resided in 2.4 in /proc/subchannels. Subchannel and device number | |
213 | * are obvious, so they don't have an entry :) | |
214 | * TODO: Split chpids and pimpampom up? Where is "in use" in the tree? | |
215 | */ | |
216 | static ssize_t | |
3fd3c0a5 | 217 | chpids_show (struct device * dev, struct device_attribute *attr, char * buf) |
1da177e4 LT |
218 | { |
219 | struct subchannel *sch = to_subchannel(dev); | |
7ad6a249 | 220 | struct chsc_ssd_info *ssd = &sch->ssd_info; |
1da177e4 LT |
221 | ssize_t ret = 0; |
222 | int chp; | |
7ad6a249 PO |
223 | int mask; |
224 | ||
225 | for (chp = 0; chp < 8; chp++) { | |
226 | mask = 0x80 >> chp; | |
227 | if (ssd->path_mask & mask) | |
228 | ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id); | |
229 | else | |
230 | ret += sprintf(buf + ret, "00 "); | |
231 | } | |
1da177e4 LT |
232 | ret += sprintf (buf+ret, "\n"); |
233 | return min((ssize_t)PAGE_SIZE, ret); | |
234 | } | |
235 | ||
236 | static ssize_t | |
3fd3c0a5 | 237 | pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf) |
1da177e4 LT |
238 | { |
239 | struct subchannel *sch = to_subchannel(dev); | |
240 | struct pmcw *pmcw = &sch->schib.pmcw; | |
241 | ||
242 | return sprintf (buf, "%02x %02x %02x\n", | |
243 | pmcw->pim, pmcw->pam, pmcw->pom); | |
244 | } | |
245 | ||
246 | static ssize_t | |
3fd3c0a5 | 247 | devtype_show (struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
248 | { |
249 | struct ccw_device *cdev = to_ccwdev(dev); | |
250 | struct ccw_device_id *id = &(cdev->id); | |
251 | ||
252 | if (id->dev_type != 0) | |
253 | return sprintf(buf, "%04x/%02x\n", | |
254 | id->dev_type, id->dev_model); | |
255 | else | |
256 | return sprintf(buf, "n/a\n"); | |
257 | } | |
258 | ||
259 | static ssize_t | |
3fd3c0a5 | 260 | cutype_show (struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
261 | { |
262 | struct ccw_device *cdev = to_ccwdev(dev); | |
263 | struct ccw_device_id *id = &(cdev->id); | |
264 | ||
265 | return sprintf(buf, "%04x/%02x\n", | |
266 | id->cu_type, id->cu_model); | |
267 | } | |
268 | ||
f1fc78a8 BB |
269 | static ssize_t |
270 | modalias_show (struct device *dev, struct device_attribute *attr, char *buf) | |
271 | { | |
272 | struct ccw_device *cdev = to_ccwdev(dev); | |
273 | struct ccw_device_id *id = &(cdev->id); | |
db0c2d59 | 274 | int len; |
f1fc78a8 | 275 | |
086a6c62 | 276 | len = snprint_alias(buf, PAGE_SIZE, id, "\n"); |
db0c2d59 PO |
277 | |
278 | return len > PAGE_SIZE ? PAGE_SIZE : len; | |
f1fc78a8 BB |
279 | } |
280 | ||
1da177e4 | 281 | static ssize_t |
3fd3c0a5 | 282 | online_show (struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
283 | { |
284 | struct ccw_device *cdev = to_ccwdev(dev); | |
285 | ||
286 | return sprintf(buf, cdev->online ? "1\n" : "0\n"); | |
287 | } | |
288 | ||
d7b5a4c9 CH |
289 | int ccw_device_is_orphan(struct ccw_device *cdev) |
290 | { | |
291 | return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent)); | |
292 | } | |
293 | ||
ef99516c | 294 | static void ccw_device_unregister(struct ccw_device *cdev) |
7674da77 | 295 | { |
7674da77 | 296 | if (test_and_clear_bit(1, &cdev->private->registered)) |
ef99516c | 297 | device_del(&cdev->dev); |
7674da77 CH |
298 | } |
299 | ||
46fbe4e4 | 300 | static void ccw_device_remove_orphan_cb(struct work_struct *work) |
59a8a6e2 | 301 | { |
46fbe4e4 PO |
302 | struct ccw_device_private *priv; |
303 | struct ccw_device *cdev; | |
59a8a6e2 | 304 | |
46fbe4e4 PO |
305 | priv = container_of(work, struct ccw_device_private, kick_work); |
306 | cdev = priv->cdev; | |
59a8a6e2 CH |
307 | ccw_device_unregister(cdev); |
308 | put_device(&cdev->dev); | |
46fbe4e4 PO |
309 | /* Release cdev reference for workqueue processing. */ |
310 | put_device(&cdev->dev); | |
59a8a6e2 CH |
311 | } |
312 | ||
46fbe4e4 | 313 | static void ccw_device_call_sch_unregister(struct work_struct *work); |
59a8a6e2 | 314 | |
1da177e4 LT |
315 | static void |
316 | ccw_device_remove_disconnected(struct ccw_device *cdev) | |
317 | { | |
d7b5a4c9 | 318 | unsigned long flags; |
59a8a6e2 | 319 | |
1da177e4 LT |
320 | /* |
321 | * Forced offline in disconnected state means | |
322 | * 'throw away device'. | |
323 | */ | |
46fbe4e4 PO |
324 | /* Get cdev reference for workqueue processing. */ |
325 | if (!get_device(&cdev->dev)) | |
326 | return; | |
d7b5a4c9 | 327 | if (ccw_device_is_orphan(cdev)) { |
59a8a6e2 CH |
328 | /* |
329 | * Deregister ccw device. | |
330 | * Unfortunately, we cannot do this directly from the | |
331 | * attribute method. | |
332 | */ | |
d7b5a4c9 CH |
333 | spin_lock_irqsave(cdev->ccwlock, flags); |
334 | cdev->private->state = DEV_STATE_NOT_OPER; | |
335 | spin_unlock_irqrestore(cdev->ccwlock, flags); | |
46fbe4e4 PO |
336 | PREPARE_WORK(&cdev->private->kick_work, |
337 | ccw_device_remove_orphan_cb); | |
338 | } else | |
339 | /* Deregister subchannel, which will kill the ccw device. */ | |
340 | PREPARE_WORK(&cdev->private->kick_work, | |
341 | ccw_device_call_sch_unregister); | |
342 | queue_work(slow_path_wq, &cdev->private->kick_work); | |
1da177e4 LT |
343 | } |
344 | ||
b2ffd8e9 CH |
345 | /** |
346 | * ccw_device_set_offline() - disable a ccw device for I/O | |
347 | * @cdev: target ccw device | |
348 | * | |
349 | * This function calls the driver's set_offline() function for @cdev, if | |
350 | * given, and then disables @cdev. | |
351 | * Returns: | |
352 | * %0 on success and a negative error value on failure. | |
353 | * Context: | |
354 | * enabled, ccw device lock not held | |
355 | */ | |
356 | int ccw_device_set_offline(struct ccw_device *cdev) | |
1da177e4 LT |
357 | { |
358 | int ret; | |
359 | ||
360 | if (!cdev) | |
361 | return -ENODEV; | |
362 | if (!cdev->online || !cdev->drv) | |
363 | return -EINVAL; | |
364 | ||
365 | if (cdev->drv->set_offline) { | |
366 | ret = cdev->drv->set_offline(cdev); | |
367 | if (ret != 0) | |
368 | return ret; | |
369 | } | |
370 | cdev->online = 0; | |
371 | spin_lock_irq(cdev->ccwlock); | |
372 | ret = ccw_device_offline(cdev); | |
373 | if (ret == -ENODEV) { | |
374 | if (cdev->private->state != DEV_STATE_NOT_OPER) { | |
375 | cdev->private->state = DEV_STATE_OFFLINE; | |
376 | dev_fsm_event(cdev, DEV_EVENT_NOTOPER); | |
377 | } | |
378 | spin_unlock_irq(cdev->ccwlock); | |
379 | return ret; | |
380 | } | |
381 | spin_unlock_irq(cdev->ccwlock); | |
382 | if (ret == 0) | |
383 | wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev)); | |
384 | else { | |
139b83dd | 385 | CIO_MSG_EVENT(0, "ccw_device_offline returned %d, " |
e556bbbd CH |
386 | "device 0.%x.%04x\n", |
387 | ret, cdev->private->dev_id.ssid, | |
388 | cdev->private->dev_id.devno); | |
1da177e4 LT |
389 | cdev->online = 1; |
390 | } | |
391 | return ret; | |
392 | } | |
393 | ||
b2ffd8e9 CH |
394 | /** |
395 | * ccw_device_set_online() - enable a ccw device for I/O | |
396 | * @cdev: target ccw device | |
397 | * | |
398 | * This function first enables @cdev and then calls the driver's set_online() | |
399 | * function for @cdev, if given. If set_online() returns an error, @cdev is | |
400 | * disabled again. | |
401 | * Returns: | |
402 | * %0 on success and a negative error value on failure. | |
403 | * Context: | |
404 | * enabled, ccw device lock not held | |
405 | */ | |
406 | int ccw_device_set_online(struct ccw_device *cdev) | |
1da177e4 LT |
407 | { |
408 | int ret; | |
409 | ||
410 | if (!cdev) | |
411 | return -ENODEV; | |
412 | if (cdev->online || !cdev->drv) | |
413 | return -EINVAL; | |
414 | ||
415 | spin_lock_irq(cdev->ccwlock); | |
416 | ret = ccw_device_online(cdev); | |
417 | spin_unlock_irq(cdev->ccwlock); | |
418 | if (ret == 0) | |
419 | wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev)); | |
420 | else { | |
139b83dd | 421 | CIO_MSG_EVENT(0, "ccw_device_online returned %d, " |
e556bbbd CH |
422 | "device 0.%x.%04x\n", |
423 | ret, cdev->private->dev_id.ssid, | |
424 | cdev->private->dev_id.devno); | |
1da177e4 LT |
425 | return ret; |
426 | } | |
427 | if (cdev->private->state != DEV_STATE_ONLINE) | |
428 | return -ENODEV; | |
429 | if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) { | |
430 | cdev->online = 1; | |
431 | return 0; | |
432 | } | |
433 | spin_lock_irq(cdev->ccwlock); | |
434 | ret = ccw_device_offline(cdev); | |
435 | spin_unlock_irq(cdev->ccwlock); | |
436 | if (ret == 0) | |
437 | wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev)); | |
e556bbbd | 438 | else |
139b83dd | 439 | CIO_MSG_EVENT(0, "ccw_device_offline returned %d, " |
e556bbbd CH |
440 | "device 0.%x.%04x\n", |
441 | ret, cdev->private->dev_id.ssid, | |
442 | cdev->private->dev_id.devno); | |
6cadb78b | 443 | return (ret == 0) ? -ENODEV : ret; |
1da177e4 LT |
444 | } |
445 | ||
f5ba6c86 CH |
446 | static void online_store_handle_offline(struct ccw_device *cdev) |
447 | { | |
448 | if (cdev->private->state == DEV_STATE_DISCONNECTED) | |
449 | ccw_device_remove_disconnected(cdev); | |
450 | else if (cdev->drv && cdev->drv->set_offline) | |
451 | ccw_device_set_offline(cdev); | |
452 | } | |
453 | ||
454 | static int online_store_recog_and_online(struct ccw_device *cdev) | |
455 | { | |
456 | int ret; | |
457 | ||
458 | /* Do device recognition, if needed. */ | |
459 | if (cdev->id.cu_type == 0) { | |
460 | ret = ccw_device_recognition(cdev); | |
461 | if (ret) { | |
e556bbbd CH |
462 | CIO_MSG_EVENT(0, "Couldn't start recognition " |
463 | "for device 0.%x.%04x (ret=%d)\n", | |
464 | cdev->private->dev_id.ssid, | |
465 | cdev->private->dev_id.devno, ret); | |
f5ba6c86 CH |
466 | return ret; |
467 | } | |
468 | wait_event(cdev->private->wait_q, | |
469 | cdev->private->flags.recog_done); | |
470 | } | |
471 | if (cdev->drv && cdev->drv->set_online) | |
472 | ccw_device_set_online(cdev); | |
473 | return 0; | |
474 | } | |
c78aa6cb | 475 | static int online_store_handle_online(struct ccw_device *cdev, int force) |
f5ba6c86 CH |
476 | { |
477 | int ret; | |
478 | ||
479 | ret = online_store_recog_and_online(cdev); | |
480 | if (ret) | |
c78aa6cb | 481 | return ret; |
f5ba6c86 CH |
482 | if (force && cdev->private->state == DEV_STATE_BOXED) { |
483 | ret = ccw_device_stlck(cdev); | |
c78aa6cb ME |
484 | if (ret) |
485 | return ret; | |
f5ba6c86 CH |
486 | if (cdev->id.cu_type == 0) |
487 | cdev->private->state = DEV_STATE_NOT_OPER; | |
488 | online_store_recog_and_online(cdev); | |
489 | } | |
c78aa6cb | 490 | return 0; |
f5ba6c86 CH |
491 | } |
492 | ||
493 | static ssize_t online_store (struct device *dev, struct device_attribute *attr, | |
494 | const char *buf, size_t count) | |
1da177e4 LT |
495 | { |
496 | struct ccw_device *cdev = to_ccwdev(dev); | |
2f972202 CH |
497 | int force, ret; |
498 | unsigned long i; | |
1da177e4 | 499 | |
973bd993 | 500 | if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0) |
1da177e4 LT |
501 | return -EAGAIN; |
502 | ||
503 | if (cdev->drv && !try_module_get(cdev->drv->owner)) { | |
504 | atomic_set(&cdev->private->onoff, 0); | |
505 | return -EINVAL; | |
506 | } | |
507 | if (!strncmp(buf, "force\n", count)) { | |
508 | force = 1; | |
509 | i = 1; | |
2f972202 | 510 | ret = 0; |
1da177e4 LT |
511 | } else { |
512 | force = 0; | |
2f972202 | 513 | ret = strict_strtoul(buf, 16, &i); |
1da177e4 | 514 | } |
2f972202 CH |
515 | if (ret) |
516 | goto out; | |
f5ba6c86 CH |
517 | switch (i) { |
518 | case 0: | |
519 | online_store_handle_offline(cdev); | |
2f972202 | 520 | ret = count; |
f5ba6c86 CH |
521 | break; |
522 | case 1: | |
c78aa6cb ME |
523 | ret = online_store_handle_online(cdev, force); |
524 | if (!ret) | |
525 | ret = count; | |
f5ba6c86 CH |
526 | break; |
527 | default: | |
2f972202 | 528 | ret = -EINVAL; |
1da177e4 | 529 | } |
2f972202 | 530 | out: |
1da177e4 LT |
531 | if (cdev->drv) |
532 | module_put(cdev->drv->owner); | |
533 | atomic_set(&cdev->private->onoff, 0); | |
2f972202 | 534 | return ret; |
1da177e4 LT |
535 | } |
536 | ||
537 | static ssize_t | |
3fd3c0a5 | 538 | available_show (struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
539 | { |
540 | struct ccw_device *cdev = to_ccwdev(dev); | |
541 | struct subchannel *sch; | |
542 | ||
d7b5a4c9 CH |
543 | if (ccw_device_is_orphan(cdev)) |
544 | return sprintf(buf, "no device\n"); | |
1da177e4 LT |
545 | switch (cdev->private->state) { |
546 | case DEV_STATE_BOXED: | |
547 | return sprintf(buf, "boxed\n"); | |
548 | case DEV_STATE_DISCONNECTED: | |
549 | case DEV_STATE_DISCONNECTED_SENSE_ID: | |
550 | case DEV_STATE_NOT_OPER: | |
551 | sch = to_subchannel(dev->parent); | |
552 | if (!sch->lpm) | |
553 | return sprintf(buf, "no path\n"); | |
554 | else | |
555 | return sprintf(buf, "no device\n"); | |
556 | default: | |
557 | /* All other states considered fine. */ | |
558 | return sprintf(buf, "good\n"); | |
559 | } | |
560 | } | |
561 | ||
562 | static DEVICE_ATTR(chpids, 0444, chpids_show, NULL); | |
563 | static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL); | |
564 | static DEVICE_ATTR(devtype, 0444, devtype_show, NULL); | |
565 | static DEVICE_ATTR(cutype, 0444, cutype_show, NULL); | |
f1fc78a8 | 566 | static DEVICE_ATTR(modalias, 0444, modalias_show, NULL); |
1da177e4 | 567 | static DEVICE_ATTR(online, 0644, online_show, online_store); |
1da177e4 LT |
568 | static DEVICE_ATTR(availability, 0444, available_show, NULL); |
569 | ||
7e9db9ea | 570 | static struct attribute *io_subchannel_attrs[] = { |
1da177e4 LT |
571 | &dev_attr_chpids.attr, |
572 | &dev_attr_pimpampom.attr, | |
573 | NULL, | |
574 | }; | |
575 | ||
7e9db9ea CH |
576 | static struct attribute_group io_subchannel_attr_group = { |
577 | .attrs = io_subchannel_attrs, | |
529192f3 | 578 | }; |
1da177e4 LT |
579 | |
580 | static struct attribute * ccwdev_attrs[] = { | |
581 | &dev_attr_devtype.attr, | |
582 | &dev_attr_cutype.attr, | |
f1fc78a8 | 583 | &dev_attr_modalias.attr, |
1da177e4 LT |
584 | &dev_attr_online.attr, |
585 | &dev_attr_cmb_enable.attr, | |
586 | &dev_attr_availability.attr, | |
587 | NULL, | |
588 | }; | |
589 | ||
590 | static struct attribute_group ccwdev_attr_group = { | |
591 | .attrs = ccwdev_attrs, | |
592 | }; | |
593 | ||
f7e5d67c | 594 | static struct attribute_group *ccwdev_attr_groups[] = { |
ef99516c CH |
595 | &ccwdev_attr_group, |
596 | NULL, | |
597 | }; | |
1da177e4 LT |
598 | |
599 | /* this is a simple abstraction for device_register that sets the | |
600 | * correct bus type and adds the bus specific files */ | |
3c9da7ba | 601 | static int ccw_device_register(struct ccw_device *cdev) |
1da177e4 LT |
602 | { |
603 | struct device *dev = &cdev->dev; | |
604 | int ret; | |
605 | ||
606 | dev->bus = &ccw_bus_type; | |
607 | ||
608 | if ((ret = device_add(dev))) | |
609 | return ret; | |
610 | ||
611 | set_bit(1, &cdev->private->registered); | |
1da177e4 LT |
612 | return ret; |
613 | } | |
614 | ||
b0744bd2 | 615 | struct match_data { |
78964268 | 616 | struct ccw_dev_id dev_id; |
b0744bd2 CH |
617 | struct ccw_device * sibling; |
618 | }; | |
619 | ||
620 | static int | |
621 | match_devno(struct device * dev, void * data) | |
622 | { | |
78964268 | 623 | struct match_data * d = data; |
b0744bd2 CH |
624 | struct ccw_device * cdev; |
625 | ||
626 | cdev = to_ccwdev(dev); | |
627 | if ((cdev->private->state == DEV_STATE_DISCONNECTED) && | |
d7b5a4c9 | 628 | !ccw_device_is_orphan(cdev) && |
78964268 | 629 | ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) && |
d7b5a4c9 | 630 | (cdev != d->sibling)) |
b0744bd2 | 631 | return 1; |
b0744bd2 CH |
632 | return 0; |
633 | } | |
634 | ||
78964268 CH |
635 | static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id, |
636 | struct ccw_device *sibling) | |
1da177e4 | 637 | { |
1da177e4 | 638 | struct device *dev; |
292888c8 | 639 | struct match_data data; |
1da177e4 | 640 | |
78964268 | 641 | data.dev_id = *dev_id; |
292888c8 | 642 | data.sibling = sibling; |
e5945b4f | 643 | dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno); |
1da177e4 | 644 | |
b0744bd2 | 645 | return dev ? to_ccwdev(dev) : NULL; |
1da177e4 LT |
646 | } |
647 | ||
d7b5a4c9 CH |
648 | static int match_orphan(struct device *dev, void *data) |
649 | { | |
650 | struct ccw_dev_id *dev_id; | |
651 | struct ccw_device *cdev; | |
652 | ||
653 | dev_id = data; | |
654 | cdev = to_ccwdev(dev); | |
655 | return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id); | |
656 | } | |
657 | ||
658 | static struct ccw_device * | |
659 | get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css, | |
660 | struct ccw_dev_id *dev_id) | |
661 | { | |
662 | struct device *dev; | |
663 | ||
664 | dev = device_find_child(&css->pseudo_subchannel->dev, dev_id, | |
665 | match_orphan); | |
666 | ||
667 | return dev ? to_ccwdev(dev) : NULL; | |
668 | } | |
669 | ||
1da177e4 | 670 | static void |
c1637532 | 671 | ccw_device_add_changed(struct work_struct *work) |
1da177e4 | 672 | { |
c1637532 | 673 | struct ccw_device_private *priv; |
1da177e4 LT |
674 | struct ccw_device *cdev; |
675 | ||
c1637532 MS |
676 | priv = container_of(work, struct ccw_device_private, kick_work); |
677 | cdev = priv->cdev; | |
1da177e4 LT |
678 | if (device_add(&cdev->dev)) { |
679 | put_device(&cdev->dev); | |
680 | return; | |
681 | } | |
682 | set_bit(1, &cdev->private->registered); | |
1da177e4 LT |
683 | } |
684 | ||
d7b5a4c9 | 685 | void ccw_device_do_unreg_rereg(struct work_struct *work) |
1da177e4 | 686 | { |
c1637532 | 687 | struct ccw_device_private *priv; |
1da177e4 LT |
688 | struct ccw_device *cdev; |
689 | struct subchannel *sch; | |
1da177e4 | 690 | |
c1637532 MS |
691 | priv = container_of(work, struct ccw_device_private, kick_work); |
692 | cdev = priv->cdev; | |
1da177e4 | 693 | sch = to_subchannel(cdev->dev.parent); |
d7b5a4c9 | 694 | |
ef99516c | 695 | ccw_device_unregister(cdev); |
1da177e4 | 696 | PREPARE_WORK(&cdev->private->kick_work, |
c1637532 | 697 | ccw_device_add_changed); |
1da177e4 LT |
698 | queue_work(ccw_device_work, &cdev->private->kick_work); |
699 | } | |
700 | ||
701 | static void | |
702 | ccw_device_release(struct device *dev) | |
703 | { | |
704 | struct ccw_device *cdev; | |
705 | ||
706 | cdev = to_ccwdev(dev); | |
707 | kfree(cdev->private); | |
708 | kfree(cdev); | |
709 | } | |
710 | ||
7674da77 CH |
711 | static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch) |
712 | { | |
713 | struct ccw_device *cdev; | |
714 | ||
715 | cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); | |
716 | if (cdev) { | |
717 | cdev->private = kzalloc(sizeof(struct ccw_device_private), | |
718 | GFP_KERNEL | GFP_DMA); | |
719 | if (cdev->private) | |
720 | return cdev; | |
721 | } | |
722 | kfree(cdev); | |
723 | return ERR_PTR(-ENOMEM); | |
724 | } | |
725 | ||
726 | static int io_subchannel_initialize_dev(struct subchannel *sch, | |
727 | struct ccw_device *cdev) | |
728 | { | |
729 | cdev->private->cdev = cdev; | |
730 | atomic_set(&cdev->private->onoff, 0); | |
731 | cdev->dev.parent = &sch->dev; | |
732 | cdev->dev.release = ccw_device_release; | |
33583c36 | 733 | INIT_WORK(&cdev->private->kick_work, NULL); |
ef99516c | 734 | cdev->dev.groups = ccwdev_attr_groups; |
7674da77 CH |
735 | /* Do first half of device_register. */ |
736 | device_initialize(&cdev->dev); | |
737 | if (!get_device(&sch->dev)) { | |
738 | if (cdev->dev.release) | |
739 | cdev->dev.release(&cdev->dev); | |
740 | return -ENODEV; | |
741 | } | |
742 | return 0; | |
743 | } | |
744 | ||
745 | static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch) | |
746 | { | |
747 | struct ccw_device *cdev; | |
748 | int ret; | |
749 | ||
750 | cdev = io_subchannel_allocate_dev(sch); | |
751 | if (!IS_ERR(cdev)) { | |
752 | ret = io_subchannel_initialize_dev(sch, cdev); | |
753 | if (ret) { | |
754 | kfree(cdev); | |
755 | cdev = ERR_PTR(ret); | |
756 | } | |
757 | } | |
758 | return cdev; | |
759 | } | |
760 | ||
d7b5a4c9 CH |
761 | static int io_subchannel_recog(struct ccw_device *, struct subchannel *); |
762 | ||
763 | static void sch_attach_device(struct subchannel *sch, | |
764 | struct ccw_device *cdev) | |
765 | { | |
82b7ac05 | 766 | css_update_ssd_info(sch); |
d7b5a4c9 | 767 | spin_lock_irq(sch->lock); |
db6a6423 | 768 | sch_set_cdev(sch, cdev); |
d7b5a4c9 CH |
769 | cdev->private->schid = sch->schid; |
770 | cdev->ccwlock = sch->lock; | |
c820de39 | 771 | ccw_device_trigger_reprobe(cdev); |
d7b5a4c9 CH |
772 | spin_unlock_irq(sch->lock); |
773 | } | |
774 | ||
775 | static void sch_attach_disconnected_device(struct subchannel *sch, | |
776 | struct ccw_device *cdev) | |
777 | { | |
778 | struct subchannel *other_sch; | |
779 | int ret; | |
780 | ||
781 | other_sch = to_subchannel(get_device(cdev->dev.parent)); | |
782 | ret = device_move(&cdev->dev, &sch->dev); | |
783 | if (ret) { | |
139b83dd | 784 | CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed " |
d7b5a4c9 CH |
785 | "(ret=%d)!\n", cdev->private->dev_id.ssid, |
786 | cdev->private->dev_id.devno, ret); | |
787 | put_device(&other_sch->dev); | |
788 | return; | |
789 | } | |
db6a6423 | 790 | sch_set_cdev(other_sch, NULL); |
d7b5a4c9 CH |
791 | /* No need to keep a subchannel without ccw device around. */ |
792 | css_sch_device_unregister(other_sch); | |
793 | put_device(&other_sch->dev); | |
794 | sch_attach_device(sch, cdev); | |
795 | } | |
796 | ||
797 | static void sch_attach_orphaned_device(struct subchannel *sch, | |
798 | struct ccw_device *cdev) | |
799 | { | |
800 | int ret; | |
801 | ||
802 | /* Try to move the ccw device to its new subchannel. */ | |
803 | ret = device_move(&cdev->dev, &sch->dev); | |
804 | if (ret) { | |
805 | CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage " | |
806 | "failed (ret=%d)!\n", | |
807 | cdev->private->dev_id.ssid, | |
808 | cdev->private->dev_id.devno, ret); | |
809 | return; | |
810 | } | |
811 | sch_attach_device(sch, cdev); | |
812 | } | |
813 | ||
814 | static void sch_create_and_recog_new_device(struct subchannel *sch) | |
815 | { | |
816 | struct ccw_device *cdev; | |
817 | ||
818 | /* Need to allocate a new ccw device. */ | |
819 | cdev = io_subchannel_create_ccwdev(sch); | |
820 | if (IS_ERR(cdev)) { | |
821 | /* OK, we did everything we could... */ | |
822 | css_sch_device_unregister(sch); | |
823 | return; | |
824 | } | |
825 | spin_lock_irq(sch->lock); | |
db6a6423 | 826 | sch_set_cdev(sch, cdev); |
d7b5a4c9 CH |
827 | spin_unlock_irq(sch->lock); |
828 | /* Start recognition for the new ccw device. */ | |
829 | if (io_subchannel_recog(cdev, sch)) { | |
830 | spin_lock_irq(sch->lock); | |
db6a6423 | 831 | sch_set_cdev(sch, NULL); |
d7b5a4c9 CH |
832 | spin_unlock_irq(sch->lock); |
833 | if (cdev->dev.release) | |
834 | cdev->dev.release(&cdev->dev); | |
835 | css_sch_device_unregister(sch); | |
836 | } | |
837 | } | |
838 | ||
839 | ||
840 | void ccw_device_move_to_orphanage(struct work_struct *work) | |
841 | { | |
842 | struct ccw_device_private *priv; | |
843 | struct ccw_device *cdev; | |
844 | struct ccw_device *replacing_cdev; | |
845 | struct subchannel *sch; | |
846 | int ret; | |
847 | struct channel_subsystem *css; | |
848 | struct ccw_dev_id dev_id; | |
849 | ||
850 | priv = container_of(work, struct ccw_device_private, kick_work); | |
851 | cdev = priv->cdev; | |
852 | sch = to_subchannel(cdev->dev.parent); | |
853 | css = to_css(sch->dev.parent); | |
854 | dev_id.devno = sch->schib.pmcw.dev; | |
855 | dev_id.ssid = sch->schid.ssid; | |
856 | ||
857 | /* | |
858 | * Move the orphaned ccw device to the orphanage so the replacing | |
859 | * ccw device can take its place on the subchannel. | |
860 | */ | |
861 | ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev); | |
862 | if (ret) { | |
863 | CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed " | |
864 | "(ret=%d)!\n", cdev->private->dev_id.ssid, | |
865 | cdev->private->dev_id.devno, ret); | |
866 | return; | |
867 | } | |
868 | cdev->ccwlock = css->pseudo_subchannel->lock; | |
869 | /* | |
870 | * Search for the replacing ccw device | |
871 | * - among the disconnected devices | |
872 | * - in the orphanage | |
873 | */ | |
874 | replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev); | |
875 | if (replacing_cdev) { | |
876 | sch_attach_disconnected_device(sch, replacing_cdev); | |
877 | return; | |
878 | } | |
879 | replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id); | |
880 | if (replacing_cdev) { | |
881 | sch_attach_orphaned_device(sch, replacing_cdev); | |
882 | return; | |
883 | } | |
884 | sch_create_and_recog_new_device(sch); | |
885 | } | |
886 | ||
1da177e4 LT |
887 | /* |
888 | * Register recognized device. | |
889 | */ | |
890 | static void | |
c1637532 | 891 | io_subchannel_register(struct work_struct *work) |
1da177e4 | 892 | { |
c1637532 | 893 | struct ccw_device_private *priv; |
1da177e4 LT |
894 | struct ccw_device *cdev; |
895 | struct subchannel *sch; | |
896 | int ret; | |
897 | unsigned long flags; | |
898 | ||
c1637532 MS |
899 | priv = container_of(work, struct ccw_device_private, kick_work); |
900 | cdev = priv->cdev; | |
1da177e4 | 901 | sch = to_subchannel(cdev->dev.parent); |
82b7ac05 | 902 | css_update_ssd_info(sch); |
47af5518 CH |
903 | /* |
904 | * io_subchannel_register() will also be called after device | |
905 | * recognition has been done for a boxed device (which will already | |
906 | * be registered). We need to reprobe since we may now have sense id | |
907 | * information. | |
908 | */ | |
b0744bd2 | 909 | if (klist_node_attached(&cdev->dev.knode_parent)) { |
47af5518 CH |
910 | if (!cdev->drv) { |
911 | ret = device_reprobe(&cdev->dev); | |
912 | if (ret) | |
913 | /* We can't do much here. */ | |
139b83dd | 914 | CIO_MSG_EVENT(0, "device_reprobe() returned" |
e556bbbd CH |
915 | " %d for 0.%x.%04x\n", ret, |
916 | cdev->private->dev_id.ssid, | |
917 | cdev->private->dev_id.devno); | |
47af5518 | 918 | } |
1da177e4 LT |
919 | goto out; |
920 | } | |
fa1a8c23 CH |
921 | /* |
922 | * Now we know this subchannel will stay, we can throw | |
923 | * our delayed uevent. | |
924 | */ | |
925 | sch->dev.uevent_suppress = 0; | |
926 | kobject_uevent(&sch->dev.kobj, KOBJ_ADD); | |
1da177e4 LT |
927 | /* make it known to the system */ |
928 | ret = ccw_device_register(cdev); | |
929 | if (ret) { | |
e556bbbd CH |
930 | CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n", |
931 | cdev->private->dev_id.ssid, | |
932 | cdev->private->dev_id.devno, ret); | |
1da177e4 | 933 | put_device(&cdev->dev); |
2ec22984 | 934 | spin_lock_irqsave(sch->lock, flags); |
db6a6423 | 935 | sch_set_cdev(sch, NULL); |
2ec22984 | 936 | spin_unlock_irqrestore(sch->lock, flags); |
1da177e4 LT |
937 | kfree (cdev->private); |
938 | kfree (cdev); | |
939 | put_device(&sch->dev); | |
940 | if (atomic_dec_and_test(&ccw_device_init_count)) | |
941 | wake_up(&ccw_device_init_wq); | |
942 | return; | |
943 | } | |
1da177e4 LT |
944 | put_device(&cdev->dev); |
945 | out: | |
946 | cdev->private->flags.recog_done = 1; | |
947 | put_device(&sch->dev); | |
948 | wake_up(&cdev->private->wait_q); | |
949 | if (atomic_dec_and_test(&ccw_device_init_count)) | |
950 | wake_up(&ccw_device_init_wq); | |
951 | } | |
952 | ||
3f4cf6e7 | 953 | static void ccw_device_call_sch_unregister(struct work_struct *work) |
1da177e4 | 954 | { |
c1637532 MS |
955 | struct ccw_device_private *priv; |
956 | struct ccw_device *cdev; | |
1da177e4 LT |
957 | struct subchannel *sch; |
958 | ||
c1637532 MS |
959 | priv = container_of(work, struct ccw_device_private, kick_work); |
960 | cdev = priv->cdev; | |
46fbe4e4 PO |
961 | /* Get subchannel reference for local processing. */ |
962 | if (!get_device(cdev->dev.parent)) | |
963 | return; | |
1da177e4 | 964 | sch = to_subchannel(cdev->dev.parent); |
6ab4879a | 965 | css_sch_device_unregister(sch); |
1da177e4 LT |
966 | /* Reset intparm to zeroes. */ |
967 | sch->schib.pmcw.intparm = 0; | |
968 | cio_modify(sch); | |
46fbe4e4 | 969 | /* Release cdev reference for workqueue processing.*/ |
1da177e4 | 970 | put_device(&cdev->dev); |
46fbe4e4 | 971 | /* Release subchannel reference for local processing. */ |
1da177e4 LT |
972 | put_device(&sch->dev); |
973 | } | |
974 | ||
975 | /* | |
976 | * subchannel recognition done. Called from the state machine. | |
977 | */ | |
978 | void | |
979 | io_subchannel_recog_done(struct ccw_device *cdev) | |
980 | { | |
981 | struct subchannel *sch; | |
982 | ||
983 | if (css_init_done == 0) { | |
984 | cdev->private->flags.recog_done = 1; | |
985 | return; | |
986 | } | |
987 | switch (cdev->private->state) { | |
988 | case DEV_STATE_NOT_OPER: | |
989 | cdev->private->flags.recog_done = 1; | |
990 | /* Remove device found not operational. */ | |
991 | if (!get_device(&cdev->dev)) | |
992 | break; | |
993 | sch = to_subchannel(cdev->dev.parent); | |
994 | PREPARE_WORK(&cdev->private->kick_work, | |
c1637532 | 995 | ccw_device_call_sch_unregister); |
1da177e4 | 996 | queue_work(slow_path_wq, &cdev->private->kick_work); |
46fbe4e4 PO |
997 | /* Release subchannel reference for asynchronous recognition. */ |
998 | put_device(&sch->dev); | |
1da177e4 LT |
999 | if (atomic_dec_and_test(&ccw_device_init_count)) |
1000 | wake_up(&ccw_device_init_wq); | |
1001 | break; | |
1002 | case DEV_STATE_BOXED: | |
1003 | /* Device did not respond in time. */ | |
1004 | case DEV_STATE_OFFLINE: | |
1005 | /* | |
1006 | * We can't register the device in interrupt context so | |
1007 | * we schedule a work item. | |
1008 | */ | |
1009 | if (!get_device(&cdev->dev)) | |
1010 | break; | |
1011 | PREPARE_WORK(&cdev->private->kick_work, | |
c1637532 | 1012 | io_subchannel_register); |
1da177e4 LT |
1013 | queue_work(slow_path_wq, &cdev->private->kick_work); |
1014 | break; | |
1015 | } | |
1016 | } | |
1017 | ||
1018 | static int | |
1019 | io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch) | |
1020 | { | |
1021 | int rc; | |
1022 | struct ccw_device_private *priv; | |
1023 | ||
db6a6423 | 1024 | sch_set_cdev(sch, cdev); |
2ec22984 | 1025 | cdev->ccwlock = sch->lock; |
fb6958a5 | 1026 | |
1da177e4 LT |
1027 | /* Init private data. */ |
1028 | priv = cdev->private; | |
78964268 CH |
1029 | priv->dev_id.devno = sch->schib.pmcw.dev; |
1030 | priv->dev_id.ssid = sch->schid.ssid; | |
1031 | priv->schid = sch->schid; | |
1da177e4 LT |
1032 | priv->state = DEV_STATE_NOT_OPER; |
1033 | INIT_LIST_HEAD(&priv->cmb_list); | |
1034 | init_waitqueue_head(&priv->wait_q); | |
1035 | init_timer(&priv->timer); | |
1036 | ||
1037 | /* Set an initial name for the device. */ | |
fb6958a5 CH |
1038 | snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x", |
1039 | sch->schid.ssid, sch->schib.pmcw.dev); | |
1da177e4 LT |
1040 | |
1041 | /* Increase counter of devices currently in recognition. */ | |
1042 | atomic_inc(&ccw_device_init_count); | |
1043 | ||
1044 | /* Start async. device sensing. */ | |
2ec22984 | 1045 | spin_lock_irq(sch->lock); |
1da177e4 | 1046 | rc = ccw_device_recognition(cdev); |
2ec22984 | 1047 | spin_unlock_irq(sch->lock); |
1da177e4 LT |
1048 | if (rc) { |
1049 | if (atomic_dec_and_test(&ccw_device_init_count)) | |
1050 | wake_up(&ccw_device_init_wq); | |
1051 | } | |
1052 | return rc; | |
1053 | } | |
1054 | ||
d7b5a4c9 CH |
1055 | static void ccw_device_move_to_sch(struct work_struct *work) |
1056 | { | |
1057 | struct ccw_device_private *priv; | |
1058 | int rc; | |
1059 | struct subchannel *sch; | |
1060 | struct ccw_device *cdev; | |
1061 | struct subchannel *former_parent; | |
1062 | ||
1063 | priv = container_of(work, struct ccw_device_private, kick_work); | |
1064 | sch = priv->sch; | |
1065 | cdev = priv->cdev; | |
1066 | former_parent = ccw_device_is_orphan(cdev) ? | |
1067 | NULL : to_subchannel(get_device(cdev->dev.parent)); | |
1068 | mutex_lock(&sch->reg_mutex); | |
1069 | /* Try to move the ccw device to its new subchannel. */ | |
1070 | rc = device_move(&cdev->dev, &sch->dev); | |
1071 | mutex_unlock(&sch->reg_mutex); | |
1072 | if (rc) { | |
139b83dd | 1073 | CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel " |
d7b5a4c9 CH |
1074 | "0.%x.%04x failed (ret=%d)!\n", |
1075 | cdev->private->dev_id.ssid, | |
1076 | cdev->private->dev_id.devno, sch->schid.ssid, | |
1077 | sch->schid.sch_no, rc); | |
1078 | css_sch_device_unregister(sch); | |
1079 | goto out; | |
1080 | } | |
1081 | if (former_parent) { | |
1082 | spin_lock_irq(former_parent->lock); | |
db6a6423 | 1083 | sch_set_cdev(former_parent, NULL); |
d7b5a4c9 CH |
1084 | spin_unlock_irq(former_parent->lock); |
1085 | css_sch_device_unregister(former_parent); | |
1086 | /* Reset intparm to zeroes. */ | |
1087 | former_parent->schib.pmcw.intparm = 0; | |
1088 | cio_modify(former_parent); | |
1089 | } | |
1090 | sch_attach_device(sch, cdev); | |
1091 | out: | |
1092 | if (former_parent) | |
1093 | put_device(&former_parent->dev); | |
1094 | put_device(&cdev->dev); | |
1095 | } | |
1096 | ||
602b20f2 CH |
1097 | static void io_subchannel_irq(struct subchannel *sch) |
1098 | { | |
1099 | struct ccw_device *cdev; | |
1100 | ||
db6a6423 | 1101 | cdev = sch_get_cdev(sch); |
602b20f2 CH |
1102 | |
1103 | CIO_TRACE_EVENT(3, "IRQ"); | |
2a0217d5 | 1104 | CIO_TRACE_EVENT(3, dev_name(&sch->dev)); |
602b20f2 CH |
1105 | if (cdev) |
1106 | dev_fsm_event(cdev, DEV_EVENT_INTERRUPT); | |
1107 | } | |
1108 | ||
0ae7a7b2 CH |
1109 | static void io_subchannel_init_fields(struct subchannel *sch) |
1110 | { | |
1111 | if (cio_is_console(sch->schid)) | |
1112 | sch->opm = 0xff; | |
1113 | else | |
1114 | sch->opm = chp_get_sch_opm(sch); | |
1115 | sch->lpm = sch->schib.pmcw.pam & sch->opm; | |
3a3fc29a | 1116 | sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC; |
0ae7a7b2 CH |
1117 | |
1118 | CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X" | |
1119 | " - PIM = %02X, PAM = %02X, POM = %02X\n", | |
1120 | sch->schib.pmcw.dev, sch->schid.ssid, | |
1121 | sch->schid.sch_no, sch->schib.pmcw.pim, | |
1122 | sch->schib.pmcw.pam, sch->schib.pmcw.pom); | |
1123 | /* Initially set up some fields in the pmcw. */ | |
1124 | sch->schib.pmcw.ena = 0; | |
1125 | sch->schib.pmcw.csense = 1; /* concurrent sense */ | |
1126 | if ((sch->lpm & (sch->lpm - 1)) != 0) | |
1127 | sch->schib.pmcw.mp = 1; /* multipath mode */ | |
1128 | /* clean up possible residual cmf stuff */ | |
1129 | sch->schib.pmcw.mme = 0; | |
1130 | sch->schib.pmcw.mbfc = 0; | |
1131 | sch->schib.pmcw.mbi = 0; | |
1132 | sch->schib.mba = 0; | |
1133 | } | |
1134 | ||
1135 | static int io_subchannel_probe(struct subchannel *sch) | |
1da177e4 | 1136 | { |
1da177e4 LT |
1137 | struct ccw_device *cdev; |
1138 | int rc; | |
1139 | unsigned long flags; | |
d7b5a4c9 | 1140 | struct ccw_dev_id dev_id; |
1da177e4 | 1141 | |
db6a6423 CH |
1142 | cdev = sch_get_cdev(sch); |
1143 | if (cdev) { | |
7e9db9ea CH |
1144 | rc = sysfs_create_group(&sch->dev.kobj, |
1145 | &io_subchannel_attr_group); | |
1146 | if (rc) | |
1147 | CIO_MSG_EVENT(0, "Failed to create io subchannel " | |
1148 | "attributes for subchannel " | |
1149 | "0.%x.%04x (rc=%d)\n", | |
1150 | sch->schid.ssid, sch->schid.sch_no, rc); | |
1da177e4 LT |
1151 | /* |
1152 | * This subchannel already has an associated ccw_device. | |
7e9db9ea CH |
1153 | * Throw the delayed uevent for the subchannel, register |
1154 | * the ccw_device and exit. This happens for all early | |
1155 | * devices, e.g. the console. | |
1da177e4 | 1156 | */ |
7e9db9ea CH |
1157 | sch->dev.uevent_suppress = 0; |
1158 | kobject_uevent(&sch->dev.kobj, KOBJ_ADD); | |
e1031786 | 1159 | cdev->dev.groups = ccwdev_attr_groups; |
1da177e4 LT |
1160 | device_initialize(&cdev->dev); |
1161 | ccw_device_register(cdev); | |
1da177e4 LT |
1162 | /* |
1163 | * Check if the device is already online. If it is | |
1164 | * the reference count needs to be corrected | |
1165 | * (see ccw_device_online and css_init_done for the | |
1166 | * ugly details). | |
1167 | */ | |
1168 | if (cdev->private->state != DEV_STATE_NOT_OPER && | |
1169 | cdev->private->state != DEV_STATE_OFFLINE && | |
1170 | cdev->private->state != DEV_STATE_BOXED) | |
1171 | get_device(&cdev->dev); | |
1172 | return 0; | |
1173 | } | |
0ae7a7b2 | 1174 | io_subchannel_init_fields(sch); |
d7b5a4c9 CH |
1175 | /* |
1176 | * First check if a fitting device may be found amongst the | |
1177 | * disconnected devices or in the orphanage. | |
1178 | */ | |
1179 | dev_id.devno = sch->schib.pmcw.dev; | |
1180 | dev_id.ssid = sch->schid.ssid; | |
7e9db9ea CH |
1181 | rc = sysfs_create_group(&sch->dev.kobj, |
1182 | &io_subchannel_attr_group); | |
1183 | if (rc) | |
1184 | return rc; | |
cd6b4f27 CH |
1185 | /* Allocate I/O subchannel private data. */ |
1186 | sch->private = kzalloc(sizeof(struct io_subchannel_private), | |
1187 | GFP_KERNEL | GFP_DMA); | |
7e9db9ea CH |
1188 | if (!sch->private) { |
1189 | rc = -ENOMEM; | |
1190 | goto out_err; | |
1191 | } | |
d7b5a4c9 CH |
1192 | cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL); |
1193 | if (!cdev) | |
1194 | cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent), | |
1195 | &dev_id); | |
1196 | if (cdev) { | |
1197 | /* | |
1198 | * Schedule moving the device until when we have a registered | |
1199 | * subchannel to move to and succeed the probe. We can | |
1200 | * unregister later again, when the probe is through. | |
1201 | */ | |
1202 | cdev->private->sch = sch; | |
1203 | PREPARE_WORK(&cdev->private->kick_work, | |
1204 | ccw_device_move_to_sch); | |
1205 | queue_work(slow_path_wq, &cdev->private->kick_work); | |
1206 | return 0; | |
1207 | } | |
7674da77 | 1208 | cdev = io_subchannel_create_ccwdev(sch); |
cd6b4f27 | 1209 | if (IS_ERR(cdev)) { |
7e9db9ea CH |
1210 | rc = PTR_ERR(cdev); |
1211 | goto out_err; | |
cd6b4f27 | 1212 | } |
8bbace7e | 1213 | rc = io_subchannel_recog(cdev, sch); |
1da177e4 | 1214 | if (rc) { |
2ec22984 | 1215 | spin_lock_irqsave(sch->lock, flags); |
db6a6423 | 1216 | sch_set_cdev(sch, NULL); |
2ec22984 | 1217 | spin_unlock_irqrestore(sch->lock, flags); |
1da177e4 LT |
1218 | if (cdev->dev.release) |
1219 | cdev->dev.release(&cdev->dev); | |
7e9db9ea | 1220 | goto out_err; |
1da177e4 | 1221 | } |
7e9db9ea CH |
1222 | return 0; |
1223 | out_err: | |
1224 | kfree(sch->private); | |
1225 | sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group); | |
1da177e4 LT |
1226 | return rc; |
1227 | } | |
1228 | ||
1da177e4 | 1229 | static int |
8bbace7e | 1230 | io_subchannel_remove (struct subchannel *sch) |
1da177e4 LT |
1231 | { |
1232 | struct ccw_device *cdev; | |
1233 | unsigned long flags; | |
1234 | ||
db6a6423 CH |
1235 | cdev = sch_get_cdev(sch); |
1236 | if (!cdev) | |
1da177e4 | 1237 | return 0; |
1da177e4 LT |
1238 | /* Set ccw device to not operational and drop reference. */ |
1239 | spin_lock_irqsave(cdev->ccwlock, flags); | |
db6a6423 | 1240 | sch_set_cdev(sch, NULL); |
1da177e4 LT |
1241 | cdev->private->state = DEV_STATE_NOT_OPER; |
1242 | spin_unlock_irqrestore(cdev->ccwlock, flags); | |
ef99516c CH |
1243 | ccw_device_unregister(cdev); |
1244 | put_device(&cdev->dev); | |
cd6b4f27 | 1245 | kfree(sch->private); |
7e9db9ea | 1246 | sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group); |
1da177e4 LT |
1247 | return 0; |
1248 | } | |
1249 | ||
602b20f2 | 1250 | static int io_subchannel_notify(struct subchannel *sch, int event) |
1da177e4 LT |
1251 | { |
1252 | struct ccw_device *cdev; | |
1253 | ||
db6a6423 | 1254 | cdev = sch_get_cdev(sch); |
1da177e4 LT |
1255 | if (!cdev) |
1256 | return 0; | |
c820de39 | 1257 | return ccw_device_notify(cdev, event); |
1da177e4 LT |
1258 | } |
1259 | ||
602b20f2 | 1260 | static void io_subchannel_verify(struct subchannel *sch) |
1da177e4 LT |
1261 | { |
1262 | struct ccw_device *cdev; | |
1263 | ||
db6a6423 | 1264 | cdev = sch_get_cdev(sch); |
1da177e4 LT |
1265 | if (cdev) |
1266 | dev_fsm_event(cdev, DEV_EVENT_VERIFY); | |
1267 | } | |
1268 | ||
c820de39 | 1269 | static int check_for_io_on_path(struct subchannel *sch, int mask) |
1da177e4 | 1270 | { |
c820de39 | 1271 | int cc; |
1da177e4 | 1272 | |
c820de39 CH |
1273 | cc = stsch(sch->schid, &sch->schib); |
1274 | if (cc) | |
1275 | return 0; | |
23d805b6 | 1276 | if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask) |
c820de39 CH |
1277 | return 1; |
1278 | return 0; | |
1279 | } | |
1280 | ||
1281 | static void terminate_internal_io(struct subchannel *sch, | |
1282 | struct ccw_device *cdev) | |
1283 | { | |
1284 | if (cio_clear(sch)) { | |
1285 | /* Recheck device in case clear failed. */ | |
1286 | sch->lpm = 0; | |
1287 | if (cdev->online) | |
1288 | dev_fsm_event(cdev, DEV_EVENT_VERIFY); | |
1289 | else | |
1290 | css_schedule_eval(sch->schid); | |
d23861ff | 1291 | return; |
c820de39 | 1292 | } |
1da177e4 | 1293 | cdev->private->state = DEV_STATE_CLEAR_VERIFY; |
c820de39 CH |
1294 | /* Request retry of internal operation. */ |
1295 | cdev->private->flags.intretry = 1; | |
1296 | /* Call handler. */ | |
1da177e4 LT |
1297 | if (cdev->handler) |
1298 | cdev->handler(cdev, cdev->private->intparm, | |
1299 | ERR_PTR(-EIO)); | |
1300 | } | |
1301 | ||
c820de39 CH |
1302 | static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask) |
1303 | { | |
1304 | struct ccw_device *cdev; | |
1305 | ||
1306 | cdev = sch_get_cdev(sch); | |
1307 | if (!cdev) | |
1308 | return; | |
1309 | if (check_for_io_on_path(sch, mask)) { | |
1310 | if (cdev->private->state == DEV_STATE_ONLINE) | |
1311 | ccw_device_kill_io(cdev); | |
1312 | else { | |
1313 | terminate_internal_io(sch, cdev); | |
1314 | /* Re-start path verification. */ | |
1315 | dev_fsm_event(cdev, DEV_EVENT_VERIFY); | |
1316 | } | |
1317 | } else | |
1318 | /* trigger path verification. */ | |
1319 | dev_fsm_event(cdev, DEV_EVENT_VERIFY); | |
1320 | ||
1321 | } | |
1322 | ||
99611f87 CH |
1323 | static int io_subchannel_chp_event(struct subchannel *sch, |
1324 | struct chp_link *link, int event) | |
c820de39 CH |
1325 | { |
1326 | int mask; | |
c820de39 | 1327 | |
99611f87 | 1328 | mask = chp_ssd_get_mask(&sch->ssd_info, link); |
c820de39 CH |
1329 | if (!mask) |
1330 | return 0; | |
1331 | switch (event) { | |
1332 | case CHP_VARY_OFF: | |
1333 | sch->opm &= ~mask; | |
1334 | sch->lpm &= ~mask; | |
1335 | io_subchannel_terminate_path(sch, mask); | |
1336 | break; | |
1337 | case CHP_VARY_ON: | |
1338 | sch->opm |= mask; | |
1339 | sch->lpm |= mask; | |
1340 | io_subchannel_verify(sch); | |
1341 | break; | |
1342 | case CHP_OFFLINE: | |
1343 | if (stsch(sch->schid, &sch->schib)) | |
1344 | return -ENXIO; | |
1345 | if (!css_sch_is_valid(&sch->schib)) | |
1346 | return -ENODEV; | |
1347 | io_subchannel_terminate_path(sch, mask); | |
1348 | break; | |
1349 | case CHP_ONLINE: | |
1350 | if (stsch(sch->schid, &sch->schib)) | |
1351 | return -ENXIO; | |
1352 | sch->lpm |= mask & sch->opm; | |
1353 | io_subchannel_verify(sch); | |
1354 | break; | |
1355 | } | |
1356 | return 0; | |
1357 | } | |
1358 | ||
1da177e4 | 1359 | static void |
8bbace7e | 1360 | io_subchannel_shutdown(struct subchannel *sch) |
1da177e4 | 1361 | { |
1da177e4 LT |
1362 | struct ccw_device *cdev; |
1363 | int ret; | |
1364 | ||
db6a6423 | 1365 | cdev = sch_get_cdev(sch); |
1da177e4 | 1366 | |
a8237fc4 | 1367 | if (cio_is_console(sch->schid)) |
1da177e4 LT |
1368 | return; |
1369 | if (!sch->schib.pmcw.ena) | |
1370 | /* Nothing to do. */ | |
1371 | return; | |
1372 | ret = cio_disable_subchannel(sch); | |
1373 | if (ret != -EBUSY) | |
1374 | /* Subchannel is disabled, we're done. */ | |
1375 | return; | |
1376 | cdev->private->state = DEV_STATE_QUIESCE; | |
1377 | if (cdev->handler) | |
1378 | cdev->handler(cdev, cdev->private->intparm, | |
1379 | ERR_PTR(-EIO)); | |
1380 | ret = ccw_device_cancel_halt_clear(cdev); | |
1381 | if (ret == -EBUSY) { | |
1382 | ccw_device_set_timeout(cdev, HZ/10); | |
1383 | wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev)); | |
1384 | } | |
1385 | cio_disable_subchannel(sch); | |
1386 | } | |
1387 | ||
c820de39 CH |
1388 | static int io_subchannel_get_status(struct subchannel *sch) |
1389 | { | |
1390 | struct schib schib; | |
1391 | ||
1392 | if (stsch(sch->schid, &schib) || !schib.pmcw.dnv) | |
1393 | return CIO_GONE; | |
1394 | if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev)) | |
1395 | return CIO_REVALIDATE; | |
1396 | if (!sch->lpm) | |
1397 | return CIO_NO_PATH; | |
1398 | return CIO_OPER; | |
1399 | } | |
1400 | ||
1401 | static int device_is_disconnected(struct ccw_device *cdev) | |
1402 | { | |
1403 | if (!cdev) | |
1404 | return 0; | |
1405 | return (cdev->private->state == DEV_STATE_DISCONNECTED || | |
1406 | cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID); | |
1407 | } | |
1408 | ||
1409 | static int recovery_check(struct device *dev, void *data) | |
1410 | { | |
1411 | struct ccw_device *cdev = to_ccwdev(dev); | |
1412 | int *redo = data; | |
1413 | ||
1414 | spin_lock_irq(cdev->ccwlock); | |
1415 | switch (cdev->private->state) { | |
1416 | case DEV_STATE_DISCONNECTED: | |
1417 | CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n", | |
1418 | cdev->private->dev_id.ssid, | |
1419 | cdev->private->dev_id.devno); | |
1420 | dev_fsm_event(cdev, DEV_EVENT_VERIFY); | |
1421 | *redo = 1; | |
1422 | break; | |
1423 | case DEV_STATE_DISCONNECTED_SENSE_ID: | |
1424 | *redo = 1; | |
1425 | break; | |
1426 | } | |
1427 | spin_unlock_irq(cdev->ccwlock); | |
1428 | ||
1429 | return 0; | |
1430 | } | |
1431 | ||
1432 | static void recovery_work_func(struct work_struct *unused) | |
1433 | { | |
1434 | int redo = 0; | |
1435 | ||
1436 | bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check); | |
1437 | if (redo) { | |
1438 | spin_lock_irq(&recovery_lock); | |
1439 | if (!timer_pending(&recovery_timer)) { | |
1440 | if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1) | |
1441 | recovery_phase++; | |
1442 | mod_timer(&recovery_timer, jiffies + | |
1443 | recovery_delay[recovery_phase] * HZ); | |
1444 | } | |
1445 | spin_unlock_irq(&recovery_lock); | |
1446 | } else | |
1447 | CIO_MSG_EVENT(4, "recovery: end\n"); | |
1448 | } | |
1449 | ||
1450 | static DECLARE_WORK(recovery_work, recovery_work_func); | |
1451 | ||
1452 | static void recovery_func(unsigned long data) | |
1453 | { | |
1454 | /* | |
1455 | * We can't do our recovery in softirq context and it's not | |
1456 | * performance critical, so we schedule it. | |
1457 | */ | |
1458 | schedule_work(&recovery_work); | |
1459 | } | |
1460 | ||
1461 | static void ccw_device_schedule_recovery(void) | |
1462 | { | |
1463 | unsigned long flags; | |
1464 | ||
1465 | CIO_MSG_EVENT(4, "recovery: schedule\n"); | |
1466 | spin_lock_irqsave(&recovery_lock, flags); | |
1467 | if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) { | |
1468 | recovery_phase = 0; | |
1469 | mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ); | |
1470 | } | |
1471 | spin_unlock_irqrestore(&recovery_lock, flags); | |
1472 | } | |
1473 | ||
ecf5d9ef PO |
1474 | static int purge_fn(struct device *dev, void *data) |
1475 | { | |
1476 | struct ccw_device *cdev = to_ccwdev(dev); | |
1477 | struct ccw_device_private *priv = cdev->private; | |
1478 | int unreg; | |
1479 | ||
1480 | spin_lock_irq(cdev->ccwlock); | |
1481 | unreg = is_blacklisted(priv->dev_id.ssid, priv->dev_id.devno) && | |
1482 | (priv->state == DEV_STATE_OFFLINE); | |
1483 | spin_unlock_irq(cdev->ccwlock); | |
1484 | if (!unreg) | |
1485 | goto out; | |
1486 | if (!get_device(&cdev->dev)) | |
1487 | goto out; | |
1488 | CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv->dev_id.ssid, | |
1489 | priv->dev_id.devno); | |
1490 | PREPARE_WORK(&cdev->private->kick_work, ccw_device_call_sch_unregister); | |
1491 | queue_work(slow_path_wq, &cdev->private->kick_work); | |
1492 | ||
1493 | out: | |
1494 | /* Abort loop in case of pending signal. */ | |
1495 | if (signal_pending(current)) | |
1496 | return -EINTR; | |
1497 | ||
1498 | return 0; | |
1499 | } | |
1500 | ||
1501 | /** | |
1502 | * ccw_purge_blacklisted - purge unused, blacklisted devices | |
1503 | * | |
1504 | * Unregister all ccw devices that are offline and on the blacklist. | |
1505 | */ | |
1506 | int ccw_purge_blacklisted(void) | |
1507 | { | |
1508 | CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n"); | |
1509 | bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn); | |
1510 | return 0; | |
1511 | } | |
1512 | ||
c820de39 CH |
1513 | static void device_set_disconnected(struct ccw_device *cdev) |
1514 | { | |
1515 | if (!cdev) | |
1516 | return; | |
1517 | ccw_device_set_timeout(cdev, 0); | |
1518 | cdev->private->flags.fake_irb = 0; | |
1519 | cdev->private->state = DEV_STATE_DISCONNECTED; | |
1520 | if (cdev->online) | |
1521 | ccw_device_schedule_recovery(); | |
1522 | } | |
1523 | ||
91c36919 PO |
1524 | void ccw_device_set_notoper(struct ccw_device *cdev) |
1525 | { | |
1526 | struct subchannel *sch = to_subchannel(cdev->dev.parent); | |
1527 | ||
1528 | CIO_TRACE_EVENT(2, "notoper"); | |
1529 | CIO_TRACE_EVENT(2, sch->dev.bus_id); | |
1530 | ccw_device_set_timeout(cdev, 0); | |
1531 | cio_disable_subchannel(sch); | |
1532 | cdev->private->state = DEV_STATE_NOT_OPER; | |
1533 | } | |
1534 | ||
c820de39 CH |
1535 | static int io_subchannel_sch_event(struct subchannel *sch, int slow) |
1536 | { | |
1537 | int event, ret, disc; | |
1538 | unsigned long flags; | |
91c36919 | 1539 | enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE, DISC } action; |
c820de39 CH |
1540 | struct ccw_device *cdev; |
1541 | ||
1542 | spin_lock_irqsave(sch->lock, flags); | |
1543 | cdev = sch_get_cdev(sch); | |
1544 | disc = device_is_disconnected(cdev); | |
1545 | if (disc && slow) { | |
1546 | /* Disconnected devices are evaluated directly only.*/ | |
1547 | spin_unlock_irqrestore(sch->lock, flags); | |
1548 | return 0; | |
1549 | } | |
1550 | /* No interrupt after machine check - kill pending timers. */ | |
1551 | if (cdev) | |
1552 | ccw_device_set_timeout(cdev, 0); | |
1553 | if (!disc && !slow) { | |
1554 | /* Non-disconnected devices are evaluated on the slow path. */ | |
1555 | spin_unlock_irqrestore(sch->lock, flags); | |
1556 | return -EAGAIN; | |
1557 | } | |
1558 | event = io_subchannel_get_status(sch); | |
1559 | CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n", | |
1560 | sch->schid.ssid, sch->schid.sch_no, event, | |
1561 | disc ? "disconnected" : "normal", | |
1562 | slow ? "slow" : "fast"); | |
1563 | /* Analyze subchannel status. */ | |
1564 | action = NONE; | |
1565 | switch (event) { | |
1566 | case CIO_NO_PATH: | |
1567 | if (disc) { | |
1568 | /* Check if paths have become available. */ | |
1569 | action = REPROBE; | |
1570 | break; | |
1571 | } | |
1572 | /* fall through */ | |
1573 | case CIO_GONE: | |
c820de39 | 1574 | /* Ask driver what to do with device. */ |
91c36919 PO |
1575 | if (io_subchannel_notify(sch, event)) |
1576 | action = DISC; | |
1577 | else | |
1578 | action = UNREGISTER; | |
c820de39 CH |
1579 | break; |
1580 | case CIO_REVALIDATE: | |
1581 | /* Device will be removed, so no notify necessary. */ | |
1582 | if (disc) | |
1583 | /* Reprobe because immediate unregister might block. */ | |
1584 | action = REPROBE; | |
1585 | else | |
1586 | action = UNREGISTER_PROBE; | |
1587 | break; | |
1588 | case CIO_OPER: | |
1589 | if (disc) | |
1590 | /* Get device operational again. */ | |
1591 | action = REPROBE; | |
1592 | break; | |
1593 | } | |
1594 | /* Perform action. */ | |
1595 | ret = 0; | |
1596 | switch (action) { | |
1597 | case UNREGISTER: | |
1598 | case UNREGISTER_PROBE: | |
91c36919 | 1599 | ccw_device_set_notoper(cdev); |
c820de39 CH |
1600 | /* Unregister device (will use subchannel lock). */ |
1601 | spin_unlock_irqrestore(sch->lock, flags); | |
1602 | css_sch_device_unregister(sch); | |
1603 | spin_lock_irqsave(sch->lock, flags); | |
1604 | ||
1605 | /* Reset intparm to zeroes. */ | |
1606 | sch->schib.pmcw.intparm = 0; | |
1607 | cio_modify(sch); | |
1608 | break; | |
1609 | case REPROBE: | |
1610 | ccw_device_trigger_reprobe(cdev); | |
1611 | break; | |
91c36919 PO |
1612 | case DISC: |
1613 | device_set_disconnected(cdev); | |
1614 | break; | |
c820de39 CH |
1615 | default: |
1616 | break; | |
1617 | } | |
1618 | spin_unlock_irqrestore(sch->lock, flags); | |
1619 | /* Probe if necessary. */ | |
1620 | if (action == UNREGISTER_PROBE) | |
1621 | ret = css_probe_device(sch->schid); | |
1622 | ||
1623 | return ret; | |
1624 | } | |
1625 | ||
1da177e4 LT |
1626 | #ifdef CONFIG_CCW_CONSOLE |
1627 | static struct ccw_device console_cdev; | |
1628 | static struct ccw_device_private console_private; | |
1629 | static int console_cdev_in_use; | |
1630 | ||
2ec22984 CH |
1631 | static DEFINE_SPINLOCK(ccw_console_lock); |
1632 | ||
1633 | spinlock_t * cio_get_console_lock(void) | |
1634 | { | |
1635 | return &ccw_console_lock; | |
1636 | } | |
1637 | ||
0ae7a7b2 CH |
1638 | static int ccw_device_console_enable(struct ccw_device *cdev, |
1639 | struct subchannel *sch) | |
1da177e4 LT |
1640 | { |
1641 | int rc; | |
1642 | ||
cd6b4f27 CH |
1643 | /* Attach subchannel private data. */ |
1644 | sch->private = cio_get_console_priv(); | |
1645 | memset(sch->private, 0, sizeof(struct io_subchannel_private)); | |
0ae7a7b2 CH |
1646 | io_subchannel_init_fields(sch); |
1647 | sch->driver = &io_subchannel_driver; | |
1da177e4 | 1648 | /* Initialize the ccw_device structure. */ |
292888c8 | 1649 | cdev->dev.parent= &sch->dev; |
1da177e4 LT |
1650 | rc = io_subchannel_recog(cdev, sch); |
1651 | if (rc) | |
1652 | return rc; | |
1653 | ||
1654 | /* Now wait for the async. recognition to come to an end. */ | |
1655 | spin_lock_irq(cdev->ccwlock); | |
1656 | while (!dev_fsm_final_state(cdev)) | |
1657 | wait_cons_dev(); | |
1658 | rc = -EIO; | |
1659 | if (cdev->private->state != DEV_STATE_OFFLINE) | |
1660 | goto out_unlock; | |
1661 | ccw_device_online(cdev); | |
1662 | while (!dev_fsm_final_state(cdev)) | |
1663 | wait_cons_dev(); | |
1664 | if (cdev->private->state != DEV_STATE_ONLINE) | |
1665 | goto out_unlock; | |
1666 | rc = 0; | |
1667 | out_unlock: | |
1668 | spin_unlock_irq(cdev->ccwlock); | |
1669 | return 0; | |
1670 | } | |
1671 | ||
1672 | struct ccw_device * | |
1673 | ccw_device_probe_console(void) | |
1674 | { | |
1675 | struct subchannel *sch; | |
1676 | int ret; | |
1677 | ||
1678 | if (xchg(&console_cdev_in_use, 1) != 0) | |
600b5d16 | 1679 | return ERR_PTR(-EBUSY); |
1da177e4 LT |
1680 | sch = cio_probe_console(); |
1681 | if (IS_ERR(sch)) { | |
1682 | console_cdev_in_use = 0; | |
1683 | return (void *) sch; | |
1684 | } | |
1685 | memset(&console_cdev, 0, sizeof(struct ccw_device)); | |
1686 | memset(&console_private, 0, sizeof(struct ccw_device_private)); | |
1687 | console_cdev.private = &console_private; | |
c1637532 | 1688 | console_private.cdev = &console_cdev; |
1da177e4 LT |
1689 | ret = ccw_device_console_enable(&console_cdev, sch); |
1690 | if (ret) { | |
1691 | cio_release_console(); | |
1692 | console_cdev_in_use = 0; | |
1693 | return ERR_PTR(ret); | |
1694 | } | |
1695 | console_cdev.online = 1; | |
1696 | return &console_cdev; | |
1697 | } | |
1698 | #endif | |
1699 | ||
1700 | /* | |
1701 | * get ccw_device matching the busid, but only if owned by cdrv | |
1702 | */ | |
b0744bd2 CH |
1703 | static int |
1704 | __ccwdev_check_busid(struct device *dev, void *id) | |
1705 | { | |
1706 | char *bus_id; | |
1707 | ||
12975aef | 1708 | bus_id = id; |
b0744bd2 | 1709 | |
2a0217d5 | 1710 | return (strncmp(bus_id, dev_name(dev), BUS_ID_SIZE) == 0); |
b0744bd2 CH |
1711 | } |
1712 | ||
1713 | ||
b2ffd8e9 CH |
1714 | /** |
1715 | * get_ccwdev_by_busid() - obtain device from a bus id | |
1716 | * @cdrv: driver the device is owned by | |
1717 | * @bus_id: bus id of the device to be searched | |
1718 | * | |
1719 | * This function searches all devices owned by @cdrv for a device with a bus | |
1720 | * id matching @bus_id. | |
1721 | * Returns: | |
1722 | * If a match is found, its reference count of the found device is increased | |
1723 | * and it is returned; else %NULL is returned. | |
1724 | */ | |
1725 | struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv, | |
1726 | const char *bus_id) | |
1da177e4 | 1727 | { |
b0744bd2 | 1728 | struct device *dev; |
1da177e4 LT |
1729 | struct device_driver *drv; |
1730 | ||
1731 | drv = get_driver(&cdrv->driver); | |
1732 | if (!drv) | |
b0744bd2 | 1733 | return NULL; |
1da177e4 | 1734 | |
b0744bd2 CH |
1735 | dev = driver_find_device(drv, NULL, (void *)bus_id, |
1736 | __ccwdev_check_busid); | |
1da177e4 LT |
1737 | put_driver(drv); |
1738 | ||
d2c993d8 | 1739 | return dev ? to_ccwdev(dev) : NULL; |
1da177e4 LT |
1740 | } |
1741 | ||
1742 | /************************** device driver handling ************************/ | |
1743 | ||
1744 | /* This is the implementation of the ccw_driver class. The probe, remove | |
1745 | * and release methods are initially very similar to the device_driver | |
1746 | * implementations, with the difference that they have ccw_device | |
1747 | * arguments. | |
1748 | * | |
1749 | * A ccw driver also contains the information that is needed for | |
1750 | * device matching. | |
1751 | */ | |
1752 | static int | |
1753 | ccw_device_probe (struct device *dev) | |
1754 | { | |
1755 | struct ccw_device *cdev = to_ccwdev(dev); | |
1756 | struct ccw_driver *cdrv = to_ccwdrv(dev->driver); | |
1757 | int ret; | |
1758 | ||
1759 | cdev->drv = cdrv; /* to let the driver call _set_online */ | |
1760 | ||
1761 | ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV; | |
1762 | ||
1763 | if (ret) { | |
d2c993d8 | 1764 | cdev->drv = NULL; |
1da177e4 LT |
1765 | return ret; |
1766 | } | |
1767 | ||
1768 | return 0; | |
1769 | } | |
1770 | ||
1771 | static int | |
1772 | ccw_device_remove (struct device *dev) | |
1773 | { | |
1774 | struct ccw_device *cdev = to_ccwdev(dev); | |
1775 | struct ccw_driver *cdrv = cdev->drv; | |
1776 | int ret; | |
1777 | ||
1da177e4 LT |
1778 | if (cdrv->remove) |
1779 | cdrv->remove(cdev); | |
1780 | if (cdev->online) { | |
1781 | cdev->online = 0; | |
1782 | spin_lock_irq(cdev->ccwlock); | |
1783 | ret = ccw_device_offline(cdev); | |
1784 | spin_unlock_irq(cdev->ccwlock); | |
1785 | if (ret == 0) | |
1786 | wait_event(cdev->private->wait_q, | |
1787 | dev_fsm_final_state(cdev)); | |
1788 | else | |
139b83dd | 1789 | CIO_MSG_EVENT(0, "ccw_device_offline returned %d, " |
e556bbbd CH |
1790 | "device 0.%x.%04x\n", |
1791 | ret, cdev->private->dev_id.ssid, | |
1792 | cdev->private->dev_id.devno); | |
1da177e4 LT |
1793 | } |
1794 | ccw_device_set_timeout(cdev, 0); | |
d2c993d8 | 1795 | cdev->drv = NULL; |
1da177e4 LT |
1796 | return 0; |
1797 | } | |
1798 | ||
958974fb CH |
1799 | static void ccw_device_shutdown(struct device *dev) |
1800 | { | |
1801 | struct ccw_device *cdev; | |
1802 | ||
1803 | cdev = to_ccwdev(dev); | |
1804 | if (cdev->drv && cdev->drv->shutdown) | |
1805 | cdev->drv->shutdown(cdev); | |
1842f2b1 | 1806 | disable_cmf(cdev); |
958974fb CH |
1807 | } |
1808 | ||
8bbace7e CH |
1809 | struct bus_type ccw_bus_type = { |
1810 | .name = "ccw", | |
1811 | .match = ccw_bus_match, | |
1812 | .uevent = ccw_uevent, | |
1813 | .probe = ccw_device_probe, | |
1814 | .remove = ccw_device_remove, | |
958974fb | 1815 | .shutdown = ccw_device_shutdown, |
8bbace7e CH |
1816 | }; |
1817 | ||
b2ffd8e9 CH |
1818 | /** |
1819 | * ccw_driver_register() - register a ccw driver | |
1820 | * @cdriver: driver to be registered | |
1821 | * | |
1822 | * This function is mainly a wrapper around driver_register(). | |
1823 | * Returns: | |
1824 | * %0 on success and a negative error value on failure. | |
1825 | */ | |
1826 | int ccw_driver_register(struct ccw_driver *cdriver) | |
1da177e4 LT |
1827 | { |
1828 | struct device_driver *drv = &cdriver->driver; | |
1829 | ||
1830 | drv->bus = &ccw_bus_type; | |
1831 | drv->name = cdriver->name; | |
4beee646 | 1832 | drv->owner = cdriver->owner; |
1da177e4 LT |
1833 | |
1834 | return driver_register(drv); | |
1835 | } | |
1836 | ||
b2ffd8e9 CH |
1837 | /** |
1838 | * ccw_driver_unregister() - deregister a ccw driver | |
1839 | * @cdriver: driver to be deregistered | |
1840 | * | |
1841 | * This function is mainly a wrapper around driver_unregister(). | |
1842 | */ | |
1843 | void ccw_driver_unregister(struct ccw_driver *cdriver) | |
1da177e4 LT |
1844 | { |
1845 | driver_unregister(&cdriver->driver); | |
1846 | } | |
1847 | ||
a8237fc4 CH |
1848 | /* Helper func for qdio. */ |
1849 | struct subchannel_id | |
1850 | ccw_device_get_subchannel_id(struct ccw_device *cdev) | |
1851 | { | |
1852 | struct subchannel *sch; | |
1853 | ||
1854 | sch = to_subchannel(cdev->dev.parent); | |
1855 | return sch->schid; | |
1856 | } | |
1857 | ||
1da177e4 LT |
1858 | MODULE_LICENSE("GPL"); |
1859 | EXPORT_SYMBOL(ccw_device_set_online); | |
1860 | EXPORT_SYMBOL(ccw_device_set_offline); | |
1861 | EXPORT_SYMBOL(ccw_driver_register); | |
1862 | EXPORT_SYMBOL(ccw_driver_unregister); | |
1863 | EXPORT_SYMBOL(get_ccwdev_by_busid); | |
1864 | EXPORT_SYMBOL(ccw_bus_type); | |
1865 | EXPORT_SYMBOL(ccw_device_work); | |
a8237fc4 | 1866 | EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id); |