• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  bus driver for ccw devices
3  *
4  *    Copyright IBM Corp. 2002, 2008
5  *    Author(s): Arnd Bergmann (arndb@de.ibm.com)
6  *		 Cornelia Huck (cornelia.huck@de.ibm.com)
7  *		 Martin Schwidefsky (schwidefsky@de.ibm.com)
8  */
9 
10 #define KMSG_COMPONENT "cio"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 
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 #include <linux/delay.h>
23 #include <linux/timer.h>
24 #include <linux/kernel_stat.h>
25 
26 #include <asm/ccwdev.h>
27 #include <asm/cio.h>
28 #include <asm/param.h>		/* HZ */
29 #include <asm/cmb.h>
30 #include <asm/isc.h>
31 
32 #include "chp.h"
33 #include "cio.h"
34 #include "cio_debug.h"
35 #include "css.h"
36 #include "device.h"
37 #include "ioasm.h"
38 #include "io_sch.h"
39 #include "blacklist.h"
40 #include "chsc.h"
41 
42 static struct timer_list recovery_timer;
43 static DEFINE_SPINLOCK(recovery_lock);
44 static int recovery_phase;
45 static const unsigned long recovery_delay[] = { 3, 30, 300 };
46 
47 static atomic_t ccw_device_init_count = ATOMIC_INIT(0);
48 static DECLARE_WAIT_QUEUE_HEAD(ccw_device_init_wq);
49 static struct bus_type ccw_bus_type;
50 
51 /******************* bus type handling ***********************/
52 
53 /* The Linux driver model distinguishes between a bus type and
54  * the bus itself. Of course we only have one channel
55  * subsystem driver and one channel system per machine, but
56  * we still use the abstraction. T.R. says it's a good idea. */
57 static int
ccw_bus_match(struct device * dev,struct device_driver * drv)58 ccw_bus_match (struct device * dev, struct device_driver * drv)
59 {
60 	struct ccw_device *cdev = to_ccwdev(dev);
61 	struct ccw_driver *cdrv = to_ccwdrv(drv);
62 	const struct ccw_device_id *ids = cdrv->ids, *found;
63 
64 	if (!ids)
65 		return 0;
66 
67 	found = ccw_device_id_match(ids, &cdev->id);
68 	if (!found)
69 		return 0;
70 
71 	cdev->id.driver_info = found->driver_info;
72 
73 	return 1;
74 }
75 
76 /* Store modalias string delimited by prefix/suffix string into buffer with
77  * specified size. Return length of resulting string (excluding trailing '\0')
78  * even if string doesn't fit buffer (snprintf semantics). */
snprint_alias(char * buf,size_t size,struct ccw_device_id * id,const char * suffix)79 static int snprint_alias(char *buf, size_t size,
80 			 struct ccw_device_id *id, const char *suffix)
81 {
82 	int len;
83 
84 	len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
85 	if (len > size)
86 		return len;
87 	buf += len;
88 	size -= len;
89 
90 	if (id->dev_type != 0)
91 		len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
92 				id->dev_model, suffix);
93 	else
94 		len += snprintf(buf, size, "dtdm%s", suffix);
95 
96 	return len;
97 }
98 
99 /* Set up environment variables for ccw device uevent. Return 0 on success,
100  * non-zero otherwise. */
ccw_uevent(struct device * dev,struct kobj_uevent_env * env)101 static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
102 {
103 	struct ccw_device *cdev = to_ccwdev(dev);
104 	struct ccw_device_id *id = &(cdev->id);
105 	int ret;
106 	char modalias_buf[30];
107 
108 	/* CU_TYPE= */
109 	ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
110 	if (ret)
111 		return ret;
112 
113 	/* CU_MODEL= */
114 	ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
115 	if (ret)
116 		return ret;
117 
118 	/* The next two can be zero, that's ok for us */
119 	/* DEV_TYPE= */
120 	ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
121 	if (ret)
122 		return ret;
123 
124 	/* DEV_MODEL= */
125 	ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
126 	if (ret)
127 		return ret;
128 
129 	/* MODALIAS=  */
130 	snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
131 	ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
132 	return ret;
133 }
134 
135 static void io_subchannel_irq(struct subchannel *);
136 static int io_subchannel_probe(struct subchannel *);
137 static int io_subchannel_remove(struct subchannel *);
138 static void io_subchannel_shutdown(struct subchannel *);
139 static int io_subchannel_sch_event(struct subchannel *, int);
140 static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
141 				   int);
142 static void recovery_func(unsigned long data);
143 
144 static struct css_device_id io_subchannel_ids[] = {
145 	{ .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
146 	{ /* end of list */ },
147 };
148 MODULE_DEVICE_TABLE(css, io_subchannel_ids);
149 
io_subchannel_prepare(struct subchannel * sch)150 static int io_subchannel_prepare(struct subchannel *sch)
151 {
152 	struct ccw_device *cdev;
153 	/*
154 	 * Don't allow suspend while a ccw device registration
155 	 * is still outstanding.
156 	 */
157 	cdev = sch_get_cdev(sch);
158 	if (cdev && !device_is_registered(&cdev->dev))
159 		return -EAGAIN;
160 	return 0;
161 }
162 
io_subchannel_settle(void)163 static int io_subchannel_settle(void)
164 {
165 	int ret;
166 
167 	ret = wait_event_interruptible(ccw_device_init_wq,
168 				atomic_read(&ccw_device_init_count) == 0);
169 	if (ret)
170 		return -EINTR;
171 	flush_workqueue(cio_work_q);
172 	return 0;
173 }
174 
175 static struct css_driver io_subchannel_driver = {
176 	.drv = {
177 		.owner = THIS_MODULE,
178 		.name = "io_subchannel",
179 	},
180 	.subchannel_type = io_subchannel_ids,
181 	.irq = io_subchannel_irq,
182 	.sch_event = io_subchannel_sch_event,
183 	.chp_event = io_subchannel_chp_event,
184 	.probe = io_subchannel_probe,
185 	.remove = io_subchannel_remove,
186 	.shutdown = io_subchannel_shutdown,
187 	.prepare = io_subchannel_prepare,
188 	.settle = io_subchannel_settle,
189 };
190 
io_subchannel_init(void)191 int __init io_subchannel_init(void)
192 {
193 	int ret;
194 
195 	setup_timer(&recovery_timer, recovery_func, 0);
196 	ret = bus_register(&ccw_bus_type);
197 	if (ret)
198 		return ret;
199 	ret = css_driver_register(&io_subchannel_driver);
200 	if (ret)
201 		bus_unregister(&ccw_bus_type);
202 
203 	return ret;
204 }
205 
206 
207 /************************ device handling **************************/
208 
209 /*
210  * A ccw_device has some interfaces in sysfs in addition to the
211  * standard ones.
212  * The following entries are designed to export the information which
213  * resided in 2.4 in /proc/subchannels. Subchannel and device number
214  * are obvious, so they don't have an entry :)
215  * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
216  */
217 static ssize_t
chpids_show(struct device * dev,struct device_attribute * attr,char * buf)218 chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
219 {
220 	struct subchannel *sch = to_subchannel(dev);
221 	struct chsc_ssd_info *ssd = &sch->ssd_info;
222 	ssize_t ret = 0;
223 	int chp;
224 	int mask;
225 
226 	for (chp = 0; chp < 8; chp++) {
227 		mask = 0x80 >> chp;
228 		if (ssd->path_mask & mask)
229 			ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
230 		else
231 			ret += sprintf(buf + ret, "00 ");
232 	}
233 	ret += sprintf (buf+ret, "\n");
234 	return min((ssize_t)PAGE_SIZE, ret);
235 }
236 
237 static ssize_t
pimpampom_show(struct device * dev,struct device_attribute * attr,char * buf)238 pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
239 {
240 	struct subchannel *sch = to_subchannel(dev);
241 	struct pmcw *pmcw = &sch->schib.pmcw;
242 
243 	return sprintf (buf, "%02x %02x %02x\n",
244 			pmcw->pim, pmcw->pam, pmcw->pom);
245 }
246 
247 static ssize_t
devtype_show(struct device * dev,struct device_attribute * attr,char * buf)248 devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
249 {
250 	struct ccw_device *cdev = to_ccwdev(dev);
251 	struct ccw_device_id *id = &(cdev->id);
252 
253 	if (id->dev_type != 0)
254 		return sprintf(buf, "%04x/%02x\n",
255 				id->dev_type, id->dev_model);
256 	else
257 		return sprintf(buf, "n/a\n");
258 }
259 
260 static ssize_t
cutype_show(struct device * dev,struct device_attribute * attr,char * buf)261 cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
262 {
263 	struct ccw_device *cdev = to_ccwdev(dev);
264 	struct ccw_device_id *id = &(cdev->id);
265 
266 	return sprintf(buf, "%04x/%02x\n",
267 		       id->cu_type, id->cu_model);
268 }
269 
270 static ssize_t
modalias_show(struct device * dev,struct device_attribute * attr,char * buf)271 modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
272 {
273 	struct ccw_device *cdev = to_ccwdev(dev);
274 	struct ccw_device_id *id = &(cdev->id);
275 	int len;
276 
277 	len = snprint_alias(buf, PAGE_SIZE, id, "\n");
278 
279 	return len > PAGE_SIZE ? PAGE_SIZE : len;
280 }
281 
282 static ssize_t
online_show(struct device * dev,struct device_attribute * attr,char * buf)283 online_show (struct device *dev, struct device_attribute *attr, char *buf)
284 {
285 	struct ccw_device *cdev = to_ccwdev(dev);
286 
287 	return sprintf(buf, cdev->online ? "1\n" : "0\n");
288 }
289 
ccw_device_is_orphan(struct ccw_device * cdev)290 int ccw_device_is_orphan(struct ccw_device *cdev)
291 {
292 	return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
293 }
294 
ccw_device_unregister(struct ccw_device * cdev)295 static void ccw_device_unregister(struct ccw_device *cdev)
296 {
297 	if (device_is_registered(&cdev->dev)) {
298 		/* Undo device_add(). */
299 		device_del(&cdev->dev);
300 	}
301 	if (cdev->private->flags.initialized) {
302 		cdev->private->flags.initialized = 0;
303 		/* Release reference from device_initialize(). */
304 		put_device(&cdev->dev);
305 	}
306 }
307 
308 static void io_subchannel_quiesce(struct subchannel *);
309 
310 /**
311  * ccw_device_set_offline() - disable a ccw device for I/O
312  * @cdev: target ccw device
313  *
314  * This function calls the driver's set_offline() function for @cdev, if
315  * given, and then disables @cdev.
316  * Returns:
317  *   %0 on success and a negative error value on failure.
318  * Context:
319  *  enabled, ccw device lock not held
320  */
ccw_device_set_offline(struct ccw_device * cdev)321 int ccw_device_set_offline(struct ccw_device *cdev)
322 {
323 	struct subchannel *sch;
324 	int ret, state;
325 
326 	if (!cdev)
327 		return -ENODEV;
328 	if (!cdev->online || !cdev->drv)
329 		return -EINVAL;
330 
331 	if (cdev->drv->set_offline) {
332 		ret = cdev->drv->set_offline(cdev);
333 		if (ret != 0)
334 			return ret;
335 	}
336 	spin_lock_irq(cdev->ccwlock);
337 	sch = to_subchannel(cdev->dev.parent);
338 	cdev->online = 0;
339 	/* Wait until a final state or DISCONNECTED is reached */
340 	while (!dev_fsm_final_state(cdev) &&
341 	       cdev->private->state != DEV_STATE_DISCONNECTED) {
342 		spin_unlock_irq(cdev->ccwlock);
343 		wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
344 			   cdev->private->state == DEV_STATE_DISCONNECTED));
345 		spin_lock_irq(cdev->ccwlock);
346 	}
347 	do {
348 		ret = ccw_device_offline(cdev);
349 		if (!ret)
350 			break;
351 		CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device "
352 			      "0.%x.%04x\n", ret, cdev->private->dev_id.ssid,
353 			      cdev->private->dev_id.devno);
354 		if (ret != -EBUSY)
355 			goto error;
356 		state = cdev->private->state;
357 		spin_unlock_irq(cdev->ccwlock);
358 		io_subchannel_quiesce(sch);
359 		spin_lock_irq(cdev->ccwlock);
360 		cdev->private->state = state;
361 	} while (ret == -EBUSY);
362 	spin_unlock_irq(cdev->ccwlock);
363 	wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
364 		   cdev->private->state == DEV_STATE_DISCONNECTED));
365 	/* Inform the user if set offline failed. */
366 	if (cdev->private->state == DEV_STATE_BOXED) {
367 		pr_warning("%s: The device entered boxed state while "
368 			   "being set offline\n", dev_name(&cdev->dev));
369 	} else if (cdev->private->state == DEV_STATE_NOT_OPER) {
370 		pr_warning("%s: The device stopped operating while "
371 			   "being set offline\n", dev_name(&cdev->dev));
372 	}
373 	/* Give up reference from ccw_device_set_online(). */
374 	put_device(&cdev->dev);
375 	return 0;
376 
377 error:
378 	cdev->private->state = DEV_STATE_OFFLINE;
379 	dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
380 	spin_unlock_irq(cdev->ccwlock);
381 	/* Give up reference from ccw_device_set_online(). */
382 	put_device(&cdev->dev);
383 	return -ENODEV;
384 }
385 
386 /**
387  * ccw_device_set_online() - enable a ccw device for I/O
388  * @cdev: target ccw device
389  *
390  * This function first enables @cdev and then calls the driver's set_online()
391  * function for @cdev, if given. If set_online() returns an error, @cdev is
392  * disabled again.
393  * Returns:
394  *   %0 on success and a negative error value on failure.
395  * Context:
396  *  enabled, ccw device lock not held
397  */
ccw_device_set_online(struct ccw_device * cdev)398 int ccw_device_set_online(struct ccw_device *cdev)
399 {
400 	int ret;
401 	int ret2;
402 
403 	if (!cdev)
404 		return -ENODEV;
405 	if (cdev->online || !cdev->drv)
406 		return -EINVAL;
407 	/* Hold on to an extra reference while device is online. */
408 	if (!get_device(&cdev->dev))
409 		return -ENODEV;
410 
411 	spin_lock_irq(cdev->ccwlock);
412 	ret = ccw_device_online(cdev);
413 	spin_unlock_irq(cdev->ccwlock);
414 	if (ret == 0)
415 		wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
416 	else {
417 		CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
418 			      "device 0.%x.%04x\n",
419 			      ret, cdev->private->dev_id.ssid,
420 			      cdev->private->dev_id.devno);
421 		/* Give up online reference since onlining failed. */
422 		put_device(&cdev->dev);
423 		return ret;
424 	}
425 	spin_lock_irq(cdev->ccwlock);
426 	/* Check if online processing was successful */
427 	if ((cdev->private->state != DEV_STATE_ONLINE) &&
428 	    (cdev->private->state != DEV_STATE_W4SENSE)) {
429 		spin_unlock_irq(cdev->ccwlock);
430 		/* Inform the user that set online failed. */
431 		if (cdev->private->state == DEV_STATE_BOXED) {
432 			pr_warning("%s: Setting the device online failed "
433 				   "because it is boxed\n",
434 				   dev_name(&cdev->dev));
435 		} else if (cdev->private->state == DEV_STATE_NOT_OPER) {
436 			pr_warning("%s: Setting the device online failed "
437 				   "because it is not operational\n",
438 				   dev_name(&cdev->dev));
439 		}
440 		/* Give up online reference since onlining failed. */
441 		put_device(&cdev->dev);
442 		return -ENODEV;
443 	}
444 	spin_unlock_irq(cdev->ccwlock);
445 	if (cdev->drv->set_online)
446 		ret = cdev->drv->set_online(cdev);
447 	if (ret)
448 		goto rollback;
449 
450 	spin_lock_irq(cdev->ccwlock);
451 	cdev->online = 1;
452 	spin_unlock_irq(cdev->ccwlock);
453 	return 0;
454 
455 rollback:
456 	spin_lock_irq(cdev->ccwlock);
457 	/* Wait until a final state or DISCONNECTED is reached */
458 	while (!dev_fsm_final_state(cdev) &&
459 	       cdev->private->state != DEV_STATE_DISCONNECTED) {
460 		spin_unlock_irq(cdev->ccwlock);
461 		wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
462 			   cdev->private->state == DEV_STATE_DISCONNECTED));
463 		spin_lock_irq(cdev->ccwlock);
464 	}
465 	ret2 = ccw_device_offline(cdev);
466 	if (ret2)
467 		goto error;
468 	spin_unlock_irq(cdev->ccwlock);
469 	wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
470 		   cdev->private->state == DEV_STATE_DISCONNECTED));
471 	/* Give up online reference since onlining failed. */
472 	put_device(&cdev->dev);
473 	return ret;
474 
475 error:
476 	CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
477 		      "device 0.%x.%04x\n",
478 		      ret2, cdev->private->dev_id.ssid,
479 		      cdev->private->dev_id.devno);
480 	cdev->private->state = DEV_STATE_OFFLINE;
481 	spin_unlock_irq(cdev->ccwlock);
482 	/* Give up online reference since onlining failed. */
483 	put_device(&cdev->dev);
484 	return ret;
485 }
486 
online_store_handle_offline(struct ccw_device * cdev)487 static int online_store_handle_offline(struct ccw_device *cdev)
488 {
489 	if (cdev->private->state == DEV_STATE_DISCONNECTED) {
490 		spin_lock_irq(cdev->ccwlock);
491 		ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
492 		spin_unlock_irq(cdev->ccwlock);
493 		return 0;
494 	}
495 	if (cdev->drv && cdev->drv->set_offline)
496 		return ccw_device_set_offline(cdev);
497 	return -EINVAL;
498 }
499 
online_store_recog_and_online(struct ccw_device * cdev)500 static int online_store_recog_and_online(struct ccw_device *cdev)
501 {
502 	/* Do device recognition, if needed. */
503 	if (cdev->private->state == DEV_STATE_BOXED) {
504 		spin_lock_irq(cdev->ccwlock);
505 		ccw_device_recognition(cdev);
506 		spin_unlock_irq(cdev->ccwlock);
507 		wait_event(cdev->private->wait_q,
508 			   cdev->private->flags.recog_done);
509 		if (cdev->private->state != DEV_STATE_OFFLINE)
510 			/* recognition failed */
511 			return -EAGAIN;
512 	}
513 	if (cdev->drv && cdev->drv->set_online)
514 		return ccw_device_set_online(cdev);
515 	return -EINVAL;
516 }
517 
online_store_handle_online(struct ccw_device * cdev,int force)518 static int online_store_handle_online(struct ccw_device *cdev, int force)
519 {
520 	int ret;
521 
522 	ret = online_store_recog_and_online(cdev);
523 	if (ret && !force)
524 		return ret;
525 	if (force && cdev->private->state == DEV_STATE_BOXED) {
526 		ret = ccw_device_stlck(cdev);
527 		if (ret)
528 			return ret;
529 		if (cdev->id.cu_type == 0)
530 			cdev->private->state = DEV_STATE_NOT_OPER;
531 		ret = online_store_recog_and_online(cdev);
532 		if (ret)
533 			return ret;
534 	}
535 	return 0;
536 }
537 
online_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)538 static ssize_t online_store (struct device *dev, struct device_attribute *attr,
539 			     const char *buf, size_t count)
540 {
541 	struct ccw_device *cdev = to_ccwdev(dev);
542 	int force, ret;
543 	unsigned long i;
544 
545 	/* Prevent conflict between multiple on-/offline processing requests. */
546 	if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
547 		return -EAGAIN;
548 	/* Prevent conflict between internal I/Os and on-/offline processing. */
549 	if (!dev_fsm_final_state(cdev) &&
550 	    cdev->private->state != DEV_STATE_DISCONNECTED) {
551 		ret = -EAGAIN;
552 		goto out;
553 	}
554 	/* Prevent conflict between pending work and on-/offline processing.*/
555 	if (work_pending(&cdev->private->todo_work)) {
556 		ret = -EAGAIN;
557 		goto out;
558 	}
559 	if (!strncmp(buf, "force\n", count)) {
560 		force = 1;
561 		i = 1;
562 		ret = 0;
563 	} else {
564 		force = 0;
565 		ret = kstrtoul(buf, 16, &i);
566 	}
567 	if (ret)
568 		goto out;
569 
570 	device_lock(dev);
571 	switch (i) {
572 	case 0:
573 		ret = online_store_handle_offline(cdev);
574 		break;
575 	case 1:
576 		ret = online_store_handle_online(cdev, force);
577 		break;
578 	default:
579 		ret = -EINVAL;
580 	}
581 	device_unlock(dev);
582 
583 out:
584 	atomic_set(&cdev->private->onoff, 0);
585 	return (ret < 0) ? ret : count;
586 }
587 
588 static ssize_t
available_show(struct device * dev,struct device_attribute * attr,char * buf)589 available_show (struct device *dev, struct device_attribute *attr, char *buf)
590 {
591 	struct ccw_device *cdev = to_ccwdev(dev);
592 	struct subchannel *sch;
593 
594 	if (ccw_device_is_orphan(cdev))
595 		return sprintf(buf, "no device\n");
596 	switch (cdev->private->state) {
597 	case DEV_STATE_BOXED:
598 		return sprintf(buf, "boxed\n");
599 	case DEV_STATE_DISCONNECTED:
600 	case DEV_STATE_DISCONNECTED_SENSE_ID:
601 	case DEV_STATE_NOT_OPER:
602 		sch = to_subchannel(dev->parent);
603 		if (!sch->lpm)
604 			return sprintf(buf, "no path\n");
605 		else
606 			return sprintf(buf, "no device\n");
607 	default:
608 		/* All other states considered fine. */
609 		return sprintf(buf, "good\n");
610 	}
611 }
612 
613 static ssize_t
initiate_logging(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)614 initiate_logging(struct device *dev, struct device_attribute *attr,
615 		 const char *buf, size_t count)
616 {
617 	struct subchannel *sch = to_subchannel(dev);
618 	int rc;
619 
620 	rc = chsc_siosl(sch->schid);
621 	if (rc < 0) {
622 		pr_warning("Logging for subchannel 0.%x.%04x failed with "
623 			   "errno=%d\n",
624 			   sch->schid.ssid, sch->schid.sch_no, rc);
625 		return rc;
626 	}
627 	pr_notice("Logging for subchannel 0.%x.%04x was triggered\n",
628 		  sch->schid.ssid, sch->schid.sch_no);
629 	return count;
630 }
631 
vpm_show(struct device * dev,struct device_attribute * attr,char * buf)632 static ssize_t vpm_show(struct device *dev, struct device_attribute *attr,
633 			char *buf)
634 {
635 	struct subchannel *sch = to_subchannel(dev);
636 
637 	return sprintf(buf, "%02x\n", sch->vpm);
638 }
639 
640 static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
641 static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
642 static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
643 static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
644 static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
645 static DEVICE_ATTR(online, 0644, online_show, online_store);
646 static DEVICE_ATTR(availability, 0444, available_show, NULL);
647 static DEVICE_ATTR(logging, 0200, NULL, initiate_logging);
648 static DEVICE_ATTR(vpm, 0444, vpm_show, NULL);
649 
650 static struct attribute *io_subchannel_attrs[] = {
651 	&dev_attr_chpids.attr,
652 	&dev_attr_pimpampom.attr,
653 	&dev_attr_logging.attr,
654 	&dev_attr_vpm.attr,
655 	NULL,
656 };
657 
658 static struct attribute_group io_subchannel_attr_group = {
659 	.attrs = io_subchannel_attrs,
660 };
661 
662 static struct attribute * ccwdev_attrs[] = {
663 	&dev_attr_devtype.attr,
664 	&dev_attr_cutype.attr,
665 	&dev_attr_modalias.attr,
666 	&dev_attr_online.attr,
667 	&dev_attr_cmb_enable.attr,
668 	&dev_attr_availability.attr,
669 	NULL,
670 };
671 
672 static struct attribute_group ccwdev_attr_group = {
673 	.attrs = ccwdev_attrs,
674 };
675 
676 static const struct attribute_group *ccwdev_attr_groups[] = {
677 	&ccwdev_attr_group,
678 	NULL,
679 };
680 
ccw_device_add(struct ccw_device * cdev)681 static int ccw_device_add(struct ccw_device *cdev)
682 {
683 	struct device *dev = &cdev->dev;
684 
685 	dev->bus = &ccw_bus_type;
686 	return device_add(dev);
687 }
688 
match_dev_id(struct device * dev,void * data)689 static int match_dev_id(struct device *dev, void *data)
690 {
691 	struct ccw_device *cdev = to_ccwdev(dev);
692 	struct ccw_dev_id *dev_id = data;
693 
694 	return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
695 }
696 
697 /**
698  * get_ccwdev_by_dev_id() - obtain device from a ccw device id
699  * @dev_id: id of the device to be searched
700  *
701  * This function searches all devices attached to the ccw bus for a device
702  * matching @dev_id.
703  * Returns:
704  *  If a device is found its reference count is increased and returned;
705  *  else %NULL is returned.
706  */
get_ccwdev_by_dev_id(struct ccw_dev_id * dev_id)707 struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
708 {
709 	struct device *dev;
710 
711 	dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
712 
713 	return dev ? to_ccwdev(dev) : NULL;
714 }
715 EXPORT_SYMBOL_GPL(get_ccwdev_by_dev_id);
716 
ccw_device_do_unbind_bind(struct ccw_device * cdev)717 static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
718 {
719 	int ret;
720 
721 	if (device_is_registered(&cdev->dev)) {
722 		device_release_driver(&cdev->dev);
723 		ret = device_attach(&cdev->dev);
724 		WARN_ON(ret == -ENODEV);
725 	}
726 }
727 
728 static void
ccw_device_release(struct device * dev)729 ccw_device_release(struct device *dev)
730 {
731 	struct ccw_device *cdev;
732 
733 	cdev = to_ccwdev(dev);
734 	/* Release reference of parent subchannel. */
735 	put_device(cdev->dev.parent);
736 	kfree(cdev->private);
737 	kfree(cdev);
738 }
739 
io_subchannel_allocate_dev(struct subchannel * sch)740 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
741 {
742 	struct ccw_device *cdev;
743 
744 	cdev  = kzalloc(sizeof(*cdev), GFP_KERNEL);
745 	if (cdev) {
746 		cdev->private = kzalloc(sizeof(struct ccw_device_private),
747 					GFP_KERNEL | GFP_DMA);
748 		if (cdev->private)
749 			return cdev;
750 	}
751 	kfree(cdev);
752 	return ERR_PTR(-ENOMEM);
753 }
754 
755 static void ccw_device_todo(struct work_struct *work);
756 
io_subchannel_initialize_dev(struct subchannel * sch,struct ccw_device * cdev)757 static int io_subchannel_initialize_dev(struct subchannel *sch,
758 					struct ccw_device *cdev)
759 {
760 	struct ccw_device_private *priv = cdev->private;
761 	int ret;
762 
763 	priv->cdev = cdev;
764 	priv->int_class = IRQIO_CIO;
765 	priv->state = DEV_STATE_NOT_OPER;
766 	priv->dev_id.devno = sch->schib.pmcw.dev;
767 	priv->dev_id.ssid = sch->schid.ssid;
768 	priv->schid = sch->schid;
769 
770 	INIT_WORK(&priv->todo_work, ccw_device_todo);
771 	INIT_LIST_HEAD(&priv->cmb_list);
772 	init_waitqueue_head(&priv->wait_q);
773 	init_timer(&priv->timer);
774 
775 	atomic_set(&priv->onoff, 0);
776 	cdev->ccwlock = sch->lock;
777 	cdev->dev.parent = &sch->dev;
778 	cdev->dev.release = ccw_device_release;
779 	cdev->dev.groups = ccwdev_attr_groups;
780 	/* Do first half of device_register. */
781 	device_initialize(&cdev->dev);
782 	ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
783 			   cdev->private->dev_id.devno);
784 	if (ret)
785 		goto out_put;
786 	if (!get_device(&sch->dev)) {
787 		ret = -ENODEV;
788 		goto out_put;
789 	}
790 	priv->flags.initialized = 1;
791 	spin_lock_irq(sch->lock);
792 	sch_set_cdev(sch, cdev);
793 	spin_unlock_irq(sch->lock);
794 	return 0;
795 
796 out_put:
797 	/* Release reference from device_initialize(). */
798 	put_device(&cdev->dev);
799 	return ret;
800 }
801 
io_subchannel_create_ccwdev(struct subchannel * sch)802 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
803 {
804 	struct ccw_device *cdev;
805 	int ret;
806 
807 	cdev = io_subchannel_allocate_dev(sch);
808 	if (!IS_ERR(cdev)) {
809 		ret = io_subchannel_initialize_dev(sch, cdev);
810 		if (ret)
811 			cdev = ERR_PTR(ret);
812 	}
813 	return cdev;
814 }
815 
816 static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
817 
sch_create_and_recog_new_device(struct subchannel * sch)818 static void sch_create_and_recog_new_device(struct subchannel *sch)
819 {
820 	struct ccw_device *cdev;
821 
822 	/* Need to allocate a new ccw device. */
823 	cdev = io_subchannel_create_ccwdev(sch);
824 	if (IS_ERR(cdev)) {
825 		/* OK, we did everything we could... */
826 		css_sch_device_unregister(sch);
827 		return;
828 	}
829 	/* Start recognition for the new ccw device. */
830 	io_subchannel_recog(cdev, sch);
831 }
832 
833 /*
834  * Register recognized device.
835  */
io_subchannel_register(struct ccw_device * cdev)836 static void io_subchannel_register(struct ccw_device *cdev)
837 {
838 	struct subchannel *sch;
839 	int ret, adjust_init_count = 1;
840 	unsigned long flags;
841 
842 	sch = to_subchannel(cdev->dev.parent);
843 	/*
844 	 * Check if subchannel is still registered. It may have become
845 	 * unregistered if a machine check hit us after finishing
846 	 * device recognition but before the register work could be
847 	 * queued.
848 	 */
849 	if (!device_is_registered(&sch->dev))
850 		goto out_err;
851 	css_update_ssd_info(sch);
852 	/*
853 	 * io_subchannel_register() will also be called after device
854 	 * recognition has been done for a boxed device (which will already
855 	 * be registered). We need to reprobe since we may now have sense id
856 	 * information.
857 	 */
858 	if (device_is_registered(&cdev->dev)) {
859 		if (!cdev->drv) {
860 			ret = device_reprobe(&cdev->dev);
861 			if (ret)
862 				/* We can't do much here. */
863 				CIO_MSG_EVENT(0, "device_reprobe() returned"
864 					      " %d for 0.%x.%04x\n", ret,
865 					      cdev->private->dev_id.ssid,
866 					      cdev->private->dev_id.devno);
867 		}
868 		adjust_init_count = 0;
869 		goto out;
870 	}
871 	/*
872 	 * Now we know this subchannel will stay, we can throw
873 	 * our delayed uevent.
874 	 */
875 	if (dev_get_uevent_suppress(&sch->dev)) {
876 		dev_set_uevent_suppress(&sch->dev, 0);
877 		kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
878 	}
879 	/* make it known to the system */
880 	ret = ccw_device_add(cdev);
881 	if (ret) {
882 		CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
883 			      cdev->private->dev_id.ssid,
884 			      cdev->private->dev_id.devno, ret);
885 		spin_lock_irqsave(sch->lock, flags);
886 		sch_set_cdev(sch, NULL);
887 		spin_unlock_irqrestore(sch->lock, flags);
888 		/* Release initial device reference. */
889 		put_device(&cdev->dev);
890 		goto out_err;
891 	}
892 out:
893 	cdev->private->flags.recog_done = 1;
894 	wake_up(&cdev->private->wait_q);
895 out_err:
896 	if (adjust_init_count && atomic_dec_and_test(&ccw_device_init_count))
897 		wake_up(&ccw_device_init_wq);
898 }
899 
ccw_device_call_sch_unregister(struct ccw_device * cdev)900 static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
901 {
902 	struct subchannel *sch;
903 
904 	/* Get subchannel reference for local processing. */
905 	if (!get_device(cdev->dev.parent))
906 		return;
907 	sch = to_subchannel(cdev->dev.parent);
908 	css_sch_device_unregister(sch);
909 	/* Release subchannel reference for local processing. */
910 	put_device(&sch->dev);
911 }
912 
913 /*
914  * subchannel recognition done. Called from the state machine.
915  */
916 void
io_subchannel_recog_done(struct ccw_device * cdev)917 io_subchannel_recog_done(struct ccw_device *cdev)
918 {
919 	if (css_init_done == 0) {
920 		cdev->private->flags.recog_done = 1;
921 		return;
922 	}
923 	switch (cdev->private->state) {
924 	case DEV_STATE_BOXED:
925 		/* Device did not respond in time. */
926 	case DEV_STATE_NOT_OPER:
927 		cdev->private->flags.recog_done = 1;
928 		/* Remove device found not operational. */
929 		ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
930 		if (atomic_dec_and_test(&ccw_device_init_count))
931 			wake_up(&ccw_device_init_wq);
932 		break;
933 	case DEV_STATE_OFFLINE:
934 		/*
935 		 * We can't register the device in interrupt context so
936 		 * we schedule a work item.
937 		 */
938 		ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
939 		break;
940 	}
941 }
942 
io_subchannel_recog(struct ccw_device * cdev,struct subchannel * sch)943 static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
944 {
945 	/* Increase counter of devices currently in recognition. */
946 	atomic_inc(&ccw_device_init_count);
947 
948 	/* Start async. device sensing. */
949 	spin_lock_irq(sch->lock);
950 	ccw_device_recognition(cdev);
951 	spin_unlock_irq(sch->lock);
952 }
953 
ccw_device_move_to_sch(struct ccw_device * cdev,struct subchannel * sch)954 static int ccw_device_move_to_sch(struct ccw_device *cdev,
955 				  struct subchannel *sch)
956 {
957 	struct subchannel *old_sch;
958 	int rc, old_enabled = 0;
959 
960 	old_sch = to_subchannel(cdev->dev.parent);
961 	/* Obtain child reference for new parent. */
962 	if (!get_device(&sch->dev))
963 		return -ENODEV;
964 
965 	if (!sch_is_pseudo_sch(old_sch)) {
966 		spin_lock_irq(old_sch->lock);
967 		old_enabled = old_sch->schib.pmcw.ena;
968 		rc = 0;
969 		if (old_enabled)
970 			rc = cio_disable_subchannel(old_sch);
971 		spin_unlock_irq(old_sch->lock);
972 		if (rc == -EBUSY) {
973 			/* Release child reference for new parent. */
974 			put_device(&sch->dev);
975 			return rc;
976 		}
977 	}
978 
979 	mutex_lock(&sch->reg_mutex);
980 	rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
981 	mutex_unlock(&sch->reg_mutex);
982 	if (rc) {
983 		CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
984 			      cdev->private->dev_id.ssid,
985 			      cdev->private->dev_id.devno, sch->schid.ssid,
986 			      sch->schib.pmcw.dev, rc);
987 		if (old_enabled) {
988 			/* Try to reenable the old subchannel. */
989 			spin_lock_irq(old_sch->lock);
990 			cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
991 			spin_unlock_irq(old_sch->lock);
992 		}
993 		/* Release child reference for new parent. */
994 		put_device(&sch->dev);
995 		return rc;
996 	}
997 	/* Clean up old subchannel. */
998 	if (!sch_is_pseudo_sch(old_sch)) {
999 		spin_lock_irq(old_sch->lock);
1000 		sch_set_cdev(old_sch, NULL);
1001 		spin_unlock_irq(old_sch->lock);
1002 		css_schedule_eval(old_sch->schid);
1003 	}
1004 	/* Release child reference for old parent. */
1005 	put_device(&old_sch->dev);
1006 	/* Initialize new subchannel. */
1007 	spin_lock_irq(sch->lock);
1008 	cdev->private->schid = sch->schid;
1009 	cdev->ccwlock = sch->lock;
1010 	if (!sch_is_pseudo_sch(sch))
1011 		sch_set_cdev(sch, cdev);
1012 	spin_unlock_irq(sch->lock);
1013 	if (!sch_is_pseudo_sch(sch))
1014 		css_update_ssd_info(sch);
1015 	return 0;
1016 }
1017 
ccw_device_move_to_orph(struct ccw_device * cdev)1018 static int ccw_device_move_to_orph(struct ccw_device *cdev)
1019 {
1020 	struct subchannel *sch = to_subchannel(cdev->dev.parent);
1021 	struct channel_subsystem *css = to_css(sch->dev.parent);
1022 
1023 	return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
1024 }
1025 
io_subchannel_irq(struct subchannel * sch)1026 static void io_subchannel_irq(struct subchannel *sch)
1027 {
1028 	struct ccw_device *cdev;
1029 
1030 	cdev = sch_get_cdev(sch);
1031 
1032 	CIO_TRACE_EVENT(6, "IRQ");
1033 	CIO_TRACE_EVENT(6, dev_name(&sch->dev));
1034 	if (cdev)
1035 		dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1036 	else
1037 		inc_irq_stat(IRQIO_CIO);
1038 }
1039 
io_subchannel_init_config(struct subchannel * sch)1040 void io_subchannel_init_config(struct subchannel *sch)
1041 {
1042 	memset(&sch->config, 0, sizeof(sch->config));
1043 	sch->config.csense = 1;
1044 }
1045 
io_subchannel_init_fields(struct subchannel * sch)1046 static void io_subchannel_init_fields(struct subchannel *sch)
1047 {
1048 	if (cio_is_console(sch->schid))
1049 		sch->opm = 0xff;
1050 	else
1051 		sch->opm = chp_get_sch_opm(sch);
1052 	sch->lpm = sch->schib.pmcw.pam & sch->opm;
1053 	sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
1054 
1055 	CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1056 		      " - PIM = %02X, PAM = %02X, POM = %02X\n",
1057 		      sch->schib.pmcw.dev, sch->schid.ssid,
1058 		      sch->schid.sch_no, sch->schib.pmcw.pim,
1059 		      sch->schib.pmcw.pam, sch->schib.pmcw.pom);
1060 
1061 	io_subchannel_init_config(sch);
1062 }
1063 
1064 /*
1065  * Note: We always return 0 so that we bind to the device even on error.
1066  * This is needed so that our remove function is called on unregister.
1067  */
io_subchannel_probe(struct subchannel * sch)1068 static int io_subchannel_probe(struct subchannel *sch)
1069 {
1070 	struct io_subchannel_private *io_priv;
1071 	struct ccw_device *cdev;
1072 	int rc;
1073 
1074 	if (cio_is_console(sch->schid)) {
1075 		rc = sysfs_create_group(&sch->dev.kobj,
1076 					&io_subchannel_attr_group);
1077 		if (rc)
1078 			CIO_MSG_EVENT(0, "Failed to create io subchannel "
1079 				      "attributes for subchannel "
1080 				      "0.%x.%04x (rc=%d)\n",
1081 				      sch->schid.ssid, sch->schid.sch_no, rc);
1082 		/*
1083 		 * The console subchannel already has an associated ccw_device.
1084 		 * Throw the delayed uevent for the subchannel, register
1085 		 * the ccw_device and exit.
1086 		 */
1087 		if (dev_get_uevent_suppress(&sch->dev)) {
1088 			/* should always be the case for the console */
1089 			dev_set_uevent_suppress(&sch->dev, 0);
1090 			kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1091 		}
1092 		cdev = sch_get_cdev(sch);
1093 		rc = ccw_device_add(cdev);
1094 		if (rc) {
1095 			/* Release online reference. */
1096 			put_device(&cdev->dev);
1097 			goto out_schedule;
1098 		}
1099 		if (atomic_dec_and_test(&ccw_device_init_count))
1100 			wake_up(&ccw_device_init_wq);
1101 		return 0;
1102 	}
1103 	io_subchannel_init_fields(sch);
1104 	rc = cio_commit_config(sch);
1105 	if (rc)
1106 		goto out_schedule;
1107 	rc = sysfs_create_group(&sch->dev.kobj,
1108 				&io_subchannel_attr_group);
1109 	if (rc)
1110 		goto out_schedule;
1111 	/* Allocate I/O subchannel private data. */
1112 	io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1113 	if (!io_priv)
1114 		goto out_schedule;
1115 
1116 	set_io_private(sch, io_priv);
1117 	css_schedule_eval(sch->schid);
1118 	return 0;
1119 
1120 out_schedule:
1121 	spin_lock_irq(sch->lock);
1122 	css_sched_sch_todo(sch, SCH_TODO_UNREG);
1123 	spin_unlock_irq(sch->lock);
1124 	return 0;
1125 }
1126 
1127 static int
io_subchannel_remove(struct subchannel * sch)1128 io_subchannel_remove (struct subchannel *sch)
1129 {
1130 	struct io_subchannel_private *io_priv = to_io_private(sch);
1131 	struct ccw_device *cdev;
1132 
1133 	cdev = sch_get_cdev(sch);
1134 	if (!cdev)
1135 		goto out_free;
1136 	io_subchannel_quiesce(sch);
1137 	/* Set ccw device to not operational and drop reference. */
1138 	spin_lock_irq(cdev->ccwlock);
1139 	sch_set_cdev(sch, NULL);
1140 	set_io_private(sch, NULL);
1141 	cdev->private->state = DEV_STATE_NOT_OPER;
1142 	spin_unlock_irq(cdev->ccwlock);
1143 	ccw_device_unregister(cdev);
1144 out_free:
1145 	kfree(io_priv);
1146 	sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1147 	return 0;
1148 }
1149 
io_subchannel_verify(struct subchannel * sch)1150 static void io_subchannel_verify(struct subchannel *sch)
1151 {
1152 	struct ccw_device *cdev;
1153 
1154 	cdev = sch_get_cdev(sch);
1155 	if (cdev)
1156 		dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1157 }
1158 
io_subchannel_terminate_path(struct subchannel * sch,u8 mask)1159 static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
1160 {
1161 	struct ccw_device *cdev;
1162 
1163 	cdev = sch_get_cdev(sch);
1164 	if (!cdev)
1165 		return;
1166 	if (cio_update_schib(sch))
1167 		goto err;
1168 	/* Check for I/O on path. */
1169 	if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
1170 		goto out;
1171 	if (cdev->private->state == DEV_STATE_ONLINE) {
1172 		ccw_device_kill_io(cdev);
1173 		goto out;
1174 	}
1175 	if (cio_clear(sch))
1176 		goto err;
1177 out:
1178 	/* Trigger path verification. */
1179 	dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1180 	return;
1181 
1182 err:
1183 	dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1184 }
1185 
io_subchannel_chp_event(struct subchannel * sch,struct chp_link * link,int event)1186 static int io_subchannel_chp_event(struct subchannel *sch,
1187 				   struct chp_link *link, int event)
1188 {
1189 	struct ccw_device *cdev = sch_get_cdev(sch);
1190 	int mask;
1191 
1192 	mask = chp_ssd_get_mask(&sch->ssd_info, link);
1193 	if (!mask)
1194 		return 0;
1195 	switch (event) {
1196 	case CHP_VARY_OFF:
1197 		sch->opm &= ~mask;
1198 		sch->lpm &= ~mask;
1199 		if (cdev)
1200 			cdev->private->path_gone_mask |= mask;
1201 		io_subchannel_terminate_path(sch, mask);
1202 		break;
1203 	case CHP_VARY_ON:
1204 		sch->opm |= mask;
1205 		sch->lpm |= mask;
1206 		if (cdev)
1207 			cdev->private->path_new_mask |= mask;
1208 		io_subchannel_verify(sch);
1209 		break;
1210 	case CHP_OFFLINE:
1211 		if (cio_update_schib(sch))
1212 			return -ENODEV;
1213 		if (cdev)
1214 			cdev->private->path_gone_mask |= mask;
1215 		io_subchannel_terminate_path(sch, mask);
1216 		break;
1217 	case CHP_ONLINE:
1218 		if (cio_update_schib(sch))
1219 			return -ENODEV;
1220 		sch->lpm |= mask & sch->opm;
1221 		if (cdev)
1222 			cdev->private->path_new_mask |= mask;
1223 		io_subchannel_verify(sch);
1224 		break;
1225 	}
1226 	return 0;
1227 }
1228 
io_subchannel_quiesce(struct subchannel * sch)1229 static void io_subchannel_quiesce(struct subchannel *sch)
1230 {
1231 	struct ccw_device *cdev;
1232 	int ret;
1233 
1234 	spin_lock_irq(sch->lock);
1235 	cdev = sch_get_cdev(sch);
1236 	if (cio_is_console(sch->schid))
1237 		goto out_unlock;
1238 	if (!sch->schib.pmcw.ena)
1239 		goto out_unlock;
1240 	ret = cio_disable_subchannel(sch);
1241 	if (ret != -EBUSY)
1242 		goto out_unlock;
1243 	if (cdev->handler)
1244 		cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
1245 	while (ret == -EBUSY) {
1246 		cdev->private->state = DEV_STATE_QUIESCE;
1247 		cdev->private->iretry = 255;
1248 		ret = ccw_device_cancel_halt_clear(cdev);
1249 		if (ret == -EBUSY) {
1250 			ccw_device_set_timeout(cdev, HZ/10);
1251 			spin_unlock_irq(sch->lock);
1252 			wait_event(cdev->private->wait_q,
1253 				   cdev->private->state != DEV_STATE_QUIESCE);
1254 			spin_lock_irq(sch->lock);
1255 		}
1256 		ret = cio_disable_subchannel(sch);
1257 	}
1258 out_unlock:
1259 	spin_unlock_irq(sch->lock);
1260 }
1261 
io_subchannel_shutdown(struct subchannel * sch)1262 static void io_subchannel_shutdown(struct subchannel *sch)
1263 {
1264 	io_subchannel_quiesce(sch);
1265 }
1266 
device_is_disconnected(struct ccw_device * cdev)1267 static int device_is_disconnected(struct ccw_device *cdev)
1268 {
1269 	if (!cdev)
1270 		return 0;
1271 	return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1272 		cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1273 }
1274 
recovery_check(struct device * dev,void * data)1275 static int recovery_check(struct device *dev, void *data)
1276 {
1277 	struct ccw_device *cdev = to_ccwdev(dev);
1278 	int *redo = data;
1279 
1280 	spin_lock_irq(cdev->ccwlock);
1281 	switch (cdev->private->state) {
1282 	case DEV_STATE_DISCONNECTED:
1283 		CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1284 			      cdev->private->dev_id.ssid,
1285 			      cdev->private->dev_id.devno);
1286 		dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1287 		*redo = 1;
1288 		break;
1289 	case DEV_STATE_DISCONNECTED_SENSE_ID:
1290 		*redo = 1;
1291 		break;
1292 	}
1293 	spin_unlock_irq(cdev->ccwlock);
1294 
1295 	return 0;
1296 }
1297 
recovery_work_func(struct work_struct * unused)1298 static void recovery_work_func(struct work_struct *unused)
1299 {
1300 	int redo = 0;
1301 
1302 	bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1303 	if (redo) {
1304 		spin_lock_irq(&recovery_lock);
1305 		if (!timer_pending(&recovery_timer)) {
1306 			if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1307 				recovery_phase++;
1308 			mod_timer(&recovery_timer, jiffies +
1309 				  recovery_delay[recovery_phase] * HZ);
1310 		}
1311 		spin_unlock_irq(&recovery_lock);
1312 	} else
1313 		CIO_MSG_EVENT(4, "recovery: end\n");
1314 }
1315 
1316 static DECLARE_WORK(recovery_work, recovery_work_func);
1317 
recovery_func(unsigned long data)1318 static void recovery_func(unsigned long data)
1319 {
1320 	/*
1321 	 * We can't do our recovery in softirq context and it's not
1322 	 * performance critical, so we schedule it.
1323 	 */
1324 	schedule_work(&recovery_work);
1325 }
1326 
ccw_device_schedule_recovery(void)1327 static void ccw_device_schedule_recovery(void)
1328 {
1329 	unsigned long flags;
1330 
1331 	CIO_MSG_EVENT(4, "recovery: schedule\n");
1332 	spin_lock_irqsave(&recovery_lock, flags);
1333 	if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1334 		recovery_phase = 0;
1335 		mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1336 	}
1337 	spin_unlock_irqrestore(&recovery_lock, flags);
1338 }
1339 
purge_fn(struct device * dev,void * data)1340 static int purge_fn(struct device *dev, void *data)
1341 {
1342 	struct ccw_device *cdev = to_ccwdev(dev);
1343 	struct ccw_dev_id *id = &cdev->private->dev_id;
1344 
1345 	spin_lock_irq(cdev->ccwlock);
1346 	if (is_blacklisted(id->ssid, id->devno) &&
1347 	    (cdev->private->state == DEV_STATE_OFFLINE) &&
1348 	    (atomic_cmpxchg(&cdev->private->onoff, 0, 1) == 0)) {
1349 		CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1350 			      id->devno);
1351 		ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1352 		atomic_set(&cdev->private->onoff, 0);
1353 	}
1354 	spin_unlock_irq(cdev->ccwlock);
1355 	/* Abort loop in case of pending signal. */
1356 	if (signal_pending(current))
1357 		return -EINTR;
1358 
1359 	return 0;
1360 }
1361 
1362 /**
1363  * ccw_purge_blacklisted - purge unused, blacklisted devices
1364  *
1365  * Unregister all ccw devices that are offline and on the blacklist.
1366  */
ccw_purge_blacklisted(void)1367 int ccw_purge_blacklisted(void)
1368 {
1369 	CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1370 	bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1371 	return 0;
1372 }
1373 
ccw_device_set_disconnected(struct ccw_device * cdev)1374 void ccw_device_set_disconnected(struct ccw_device *cdev)
1375 {
1376 	if (!cdev)
1377 		return;
1378 	ccw_device_set_timeout(cdev, 0);
1379 	cdev->private->flags.fake_irb = 0;
1380 	cdev->private->state = DEV_STATE_DISCONNECTED;
1381 	if (cdev->online)
1382 		ccw_device_schedule_recovery();
1383 }
1384 
ccw_device_set_notoper(struct ccw_device * cdev)1385 void ccw_device_set_notoper(struct ccw_device *cdev)
1386 {
1387 	struct subchannel *sch = to_subchannel(cdev->dev.parent);
1388 
1389 	CIO_TRACE_EVENT(2, "notoper");
1390 	CIO_TRACE_EVENT(2, dev_name(&sch->dev));
1391 	ccw_device_set_timeout(cdev, 0);
1392 	cio_disable_subchannel(sch);
1393 	cdev->private->state = DEV_STATE_NOT_OPER;
1394 }
1395 
1396 enum io_sch_action {
1397 	IO_SCH_UNREG,
1398 	IO_SCH_ORPH_UNREG,
1399 	IO_SCH_ATTACH,
1400 	IO_SCH_UNREG_ATTACH,
1401 	IO_SCH_ORPH_ATTACH,
1402 	IO_SCH_REPROBE,
1403 	IO_SCH_VERIFY,
1404 	IO_SCH_DISC,
1405 	IO_SCH_NOP,
1406 };
1407 
sch_get_action(struct subchannel * sch)1408 static enum io_sch_action sch_get_action(struct subchannel *sch)
1409 {
1410 	struct ccw_device *cdev;
1411 
1412 	cdev = sch_get_cdev(sch);
1413 	if (cio_update_schib(sch)) {
1414 		/* Not operational. */
1415 		if (!cdev)
1416 			return IO_SCH_UNREG;
1417 		if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
1418 			return IO_SCH_UNREG;
1419 		return IO_SCH_ORPH_UNREG;
1420 	}
1421 	/* Operational. */
1422 	if (!cdev)
1423 		return IO_SCH_ATTACH;
1424 	if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1425 		if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
1426 			return IO_SCH_UNREG_ATTACH;
1427 		return IO_SCH_ORPH_ATTACH;
1428 	}
1429 	if ((sch->schib.pmcw.pam & sch->opm) == 0) {
1430 		if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
1431 			return IO_SCH_UNREG;
1432 		return IO_SCH_DISC;
1433 	}
1434 	if (device_is_disconnected(cdev))
1435 		return IO_SCH_REPROBE;
1436 	if (cdev->online && !cdev->private->flags.resuming)
1437 		return IO_SCH_VERIFY;
1438 	if (cdev->private->state == DEV_STATE_NOT_OPER)
1439 		return IO_SCH_UNREG_ATTACH;
1440 	return IO_SCH_NOP;
1441 }
1442 
1443 /**
1444  * io_subchannel_sch_event - process subchannel event
1445  * @sch: subchannel
1446  * @process: non-zero if function is called in process context
1447  *
1448  * An unspecified event occurred for this subchannel. Adjust data according
1449  * to the current operational state of the subchannel and device. Return
1450  * zero when the event has been handled sufficiently or -EAGAIN when this
1451  * function should be called again in process context.
1452  */
io_subchannel_sch_event(struct subchannel * sch,int process)1453 static int io_subchannel_sch_event(struct subchannel *sch, int process)
1454 {
1455 	unsigned long flags;
1456 	struct ccw_device *cdev;
1457 	struct ccw_dev_id dev_id;
1458 	enum io_sch_action action;
1459 	int rc = -EAGAIN;
1460 
1461 	spin_lock_irqsave(sch->lock, flags);
1462 	if (!device_is_registered(&sch->dev))
1463 		goto out_unlock;
1464 	if (work_pending(&sch->todo_work))
1465 		goto out_unlock;
1466 	cdev = sch_get_cdev(sch);
1467 	if (cdev && work_pending(&cdev->private->todo_work))
1468 		goto out_unlock;
1469 	action = sch_get_action(sch);
1470 	CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1471 		      sch->schid.ssid, sch->schid.sch_no, process,
1472 		      action);
1473 	/* Perform immediate actions while holding the lock. */
1474 	switch (action) {
1475 	case IO_SCH_REPROBE:
1476 		/* Trigger device recognition. */
1477 		ccw_device_trigger_reprobe(cdev);
1478 		rc = 0;
1479 		goto out_unlock;
1480 	case IO_SCH_VERIFY:
1481 		/* Trigger path verification. */
1482 		io_subchannel_verify(sch);
1483 		rc = 0;
1484 		goto out_unlock;
1485 	case IO_SCH_DISC:
1486 		ccw_device_set_disconnected(cdev);
1487 		rc = 0;
1488 		goto out_unlock;
1489 	case IO_SCH_ORPH_UNREG:
1490 	case IO_SCH_ORPH_ATTACH:
1491 		ccw_device_set_disconnected(cdev);
1492 		break;
1493 	case IO_SCH_UNREG_ATTACH:
1494 	case IO_SCH_UNREG:
1495 		if (!cdev)
1496 			break;
1497 		if (cdev->private->state == DEV_STATE_SENSE_ID) {
1498 			/*
1499 			 * Note: delayed work triggered by this event
1500 			 * and repeated calls to sch_event are synchronized
1501 			 * by the above check for work_pending(cdev).
1502 			 */
1503 			dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1504 		} else
1505 			ccw_device_set_notoper(cdev);
1506 		break;
1507 	case IO_SCH_NOP:
1508 		rc = 0;
1509 		goto out_unlock;
1510 	default:
1511 		break;
1512 	}
1513 	spin_unlock_irqrestore(sch->lock, flags);
1514 	/* All other actions require process context. */
1515 	if (!process)
1516 		goto out;
1517 	/* Handle attached ccw device. */
1518 	switch (action) {
1519 	case IO_SCH_ORPH_UNREG:
1520 	case IO_SCH_ORPH_ATTACH:
1521 		/* Move ccw device to orphanage. */
1522 		rc = ccw_device_move_to_orph(cdev);
1523 		if (rc)
1524 			goto out;
1525 		break;
1526 	case IO_SCH_UNREG_ATTACH:
1527 		spin_lock_irqsave(sch->lock, flags);
1528 		if (cdev->private->flags.resuming) {
1529 			/* Device will be handled later. */
1530 			rc = 0;
1531 			goto out_unlock;
1532 		}
1533 		sch_set_cdev(sch, NULL);
1534 		spin_unlock_irqrestore(sch->lock, flags);
1535 		/* Unregister ccw device. */
1536 		ccw_device_unregister(cdev);
1537 		break;
1538 	default:
1539 		break;
1540 	}
1541 	/* Handle subchannel. */
1542 	switch (action) {
1543 	case IO_SCH_ORPH_UNREG:
1544 	case IO_SCH_UNREG:
1545 		if (!cdev || !cdev->private->flags.resuming)
1546 			css_sch_device_unregister(sch);
1547 		break;
1548 	case IO_SCH_ORPH_ATTACH:
1549 	case IO_SCH_UNREG_ATTACH:
1550 	case IO_SCH_ATTACH:
1551 		dev_id.ssid = sch->schid.ssid;
1552 		dev_id.devno = sch->schib.pmcw.dev;
1553 		cdev = get_ccwdev_by_dev_id(&dev_id);
1554 		if (!cdev) {
1555 			sch_create_and_recog_new_device(sch);
1556 			break;
1557 		}
1558 		rc = ccw_device_move_to_sch(cdev, sch);
1559 		if (rc) {
1560 			/* Release reference from get_ccwdev_by_dev_id() */
1561 			put_device(&cdev->dev);
1562 			goto out;
1563 		}
1564 		spin_lock_irqsave(sch->lock, flags);
1565 		ccw_device_trigger_reprobe(cdev);
1566 		spin_unlock_irqrestore(sch->lock, flags);
1567 		/* Release reference from get_ccwdev_by_dev_id() */
1568 		put_device(&cdev->dev);
1569 		break;
1570 	default:
1571 		break;
1572 	}
1573 	return 0;
1574 
1575 out_unlock:
1576 	spin_unlock_irqrestore(sch->lock, flags);
1577 out:
1578 	return rc;
1579 }
1580 
ccw_device_set_int_class(struct ccw_device * cdev)1581 static void ccw_device_set_int_class(struct ccw_device *cdev)
1582 {
1583 	struct ccw_driver *cdrv = cdev->drv;
1584 
1585 	/* Note: we interpret class 0 in this context as an uninitialized
1586 	 * field since it translates to a non-I/O interrupt class. */
1587 	if (cdrv->int_class != 0)
1588 		cdev->private->int_class = cdrv->int_class;
1589 	else
1590 		cdev->private->int_class = IRQIO_CIO;
1591 }
1592 
1593 #ifdef CONFIG_CCW_CONSOLE
ccw_device_enable_console(struct ccw_device * cdev)1594 int __init ccw_device_enable_console(struct ccw_device *cdev)
1595 {
1596 	struct subchannel *sch = to_subchannel(cdev->dev.parent);
1597 	int rc;
1598 
1599 	if (!cdev->drv || !cdev->handler)
1600 		return -EINVAL;
1601 
1602 	io_subchannel_init_fields(sch);
1603 	rc = cio_commit_config(sch);
1604 	if (rc)
1605 		return rc;
1606 	sch->driver = &io_subchannel_driver;
1607 	io_subchannel_recog(cdev, sch);
1608 	/* Now wait for the async. recognition to come to an end. */
1609 	spin_lock_irq(cdev->ccwlock);
1610 	while (!dev_fsm_final_state(cdev))
1611 		ccw_device_wait_idle(cdev);
1612 
1613 	/* Hold on to an extra reference while device is online. */
1614 	get_device(&cdev->dev);
1615 	rc = ccw_device_online(cdev);
1616 	if (rc)
1617 		goto out_unlock;
1618 
1619 	while (!dev_fsm_final_state(cdev))
1620 		ccw_device_wait_idle(cdev);
1621 
1622 	if (cdev->private->state == DEV_STATE_ONLINE)
1623 		cdev->online = 1;
1624 	else
1625 		rc = -EIO;
1626 out_unlock:
1627 	spin_unlock_irq(cdev->ccwlock);
1628 	if (rc) /* Give up online reference since onlining failed. */
1629 		put_device(&cdev->dev);
1630 	return rc;
1631 }
1632 
ccw_device_create_console(struct ccw_driver * drv)1633 struct ccw_device * __init ccw_device_create_console(struct ccw_driver *drv)
1634 {
1635 	struct io_subchannel_private *io_priv;
1636 	struct ccw_device *cdev;
1637 	struct subchannel *sch;
1638 
1639 	sch = cio_probe_console();
1640 	if (IS_ERR(sch))
1641 		return ERR_CAST(sch);
1642 
1643 	io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1644 	if (!io_priv) {
1645 		put_device(&sch->dev);
1646 		return ERR_PTR(-ENOMEM);
1647 	}
1648 	set_io_private(sch, io_priv);
1649 	cdev = io_subchannel_create_ccwdev(sch);
1650 	if (IS_ERR(cdev)) {
1651 		put_device(&sch->dev);
1652 		kfree(io_priv);
1653 		return cdev;
1654 	}
1655 	cdev->drv = drv;
1656 	ccw_device_set_int_class(cdev);
1657 	return cdev;
1658 }
1659 
ccw_device_destroy_console(struct ccw_device * cdev)1660 void __init ccw_device_destroy_console(struct ccw_device *cdev)
1661 {
1662 	struct subchannel *sch = to_subchannel(cdev->dev.parent);
1663 	struct io_subchannel_private *io_priv = to_io_private(sch);
1664 
1665 	set_io_private(sch, NULL);
1666 	put_device(&sch->dev);
1667 	put_device(&cdev->dev);
1668 	kfree(io_priv);
1669 }
1670 
1671 /**
1672  * ccw_device_wait_idle() - busy wait for device to become idle
1673  * @cdev: ccw device
1674  *
1675  * Poll until activity control is zero, that is, no function or data
1676  * transfer is pending/active.
1677  * Called with device lock being held.
1678  */
ccw_device_wait_idle(struct ccw_device * cdev)1679 void ccw_device_wait_idle(struct ccw_device *cdev)
1680 {
1681 	struct subchannel *sch = to_subchannel(cdev->dev.parent);
1682 
1683 	while (1) {
1684 		cio_tsch(sch);
1685 		if (sch->schib.scsw.cmd.actl == 0)
1686 			break;
1687 		udelay_simple(100);
1688 	}
1689 }
1690 
1691 static int ccw_device_pm_restore(struct device *dev);
1692 
ccw_device_force_console(struct ccw_device * cdev)1693 int ccw_device_force_console(struct ccw_device *cdev)
1694 {
1695 	return ccw_device_pm_restore(&cdev->dev);
1696 }
1697 EXPORT_SYMBOL_GPL(ccw_device_force_console);
1698 #endif
1699 
1700 /*
1701  * get ccw_device matching the busid, but only if owned by cdrv
1702  */
1703 static int
__ccwdev_check_busid(struct device * dev,void * id)1704 __ccwdev_check_busid(struct device *dev, void *id)
1705 {
1706 	char *bus_id;
1707 
1708 	bus_id = id;
1709 
1710 	return (strcmp(bus_id, dev_name(dev)) == 0);
1711 }
1712 
1713 
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  */
get_ccwdev_by_busid(struct ccw_driver * cdrv,const char * bus_id)1725 struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1726 				       const char *bus_id)
1727 {
1728 	struct device *dev;
1729 
1730 	dev = driver_find_device(&cdrv->driver, NULL, (void *)bus_id,
1731 				 __ccwdev_check_busid);
1732 
1733 	return dev ? to_ccwdev(dev) : NULL;
1734 }
1735 
1736 /************************** device driver handling ************************/
1737 
1738 /* This is the implementation of the ccw_driver class. The probe, remove
1739  * and release methods are initially very similar to the device_driver
1740  * implementations, with the difference that they have ccw_device
1741  * arguments.
1742  *
1743  * A ccw driver also contains the information that is needed for
1744  * device matching.
1745  */
1746 static int
ccw_device_probe(struct device * dev)1747 ccw_device_probe (struct device *dev)
1748 {
1749 	struct ccw_device *cdev = to_ccwdev(dev);
1750 	struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1751 	int ret;
1752 
1753 	cdev->drv = cdrv; /* to let the driver call _set_online */
1754 	ccw_device_set_int_class(cdev);
1755 	ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1756 	if (ret) {
1757 		cdev->drv = NULL;
1758 		cdev->private->int_class = IRQIO_CIO;
1759 		return ret;
1760 	}
1761 
1762 	return 0;
1763 }
1764 
ccw_device_remove(struct device * dev)1765 static int ccw_device_remove(struct device *dev)
1766 {
1767 	struct ccw_device *cdev = to_ccwdev(dev);
1768 	struct ccw_driver *cdrv = cdev->drv;
1769 	int ret;
1770 
1771 	if (cdrv->remove)
1772 		cdrv->remove(cdev);
1773 
1774 	spin_lock_irq(cdev->ccwlock);
1775 	if (cdev->online) {
1776 		cdev->online = 0;
1777 		ret = ccw_device_offline(cdev);
1778 		spin_unlock_irq(cdev->ccwlock);
1779 		if (ret == 0)
1780 			wait_event(cdev->private->wait_q,
1781 				   dev_fsm_final_state(cdev));
1782 		else
1783 			CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1784 				      "device 0.%x.%04x\n",
1785 				      ret, cdev->private->dev_id.ssid,
1786 				      cdev->private->dev_id.devno);
1787 		/* Give up reference obtained in ccw_device_set_online(). */
1788 		put_device(&cdev->dev);
1789 		spin_lock_irq(cdev->ccwlock);
1790 	}
1791 	ccw_device_set_timeout(cdev, 0);
1792 	cdev->drv = NULL;
1793 	cdev->private->int_class = IRQIO_CIO;
1794 	spin_unlock_irq(cdev->ccwlock);
1795 	__disable_cmf(cdev);
1796 
1797 	return 0;
1798 }
1799 
ccw_device_shutdown(struct device * dev)1800 static void ccw_device_shutdown(struct device *dev)
1801 {
1802 	struct ccw_device *cdev;
1803 
1804 	cdev = to_ccwdev(dev);
1805 	if (cdev->drv && cdev->drv->shutdown)
1806 		cdev->drv->shutdown(cdev);
1807 	__disable_cmf(cdev);
1808 }
1809 
ccw_device_pm_prepare(struct device * dev)1810 static int ccw_device_pm_prepare(struct device *dev)
1811 {
1812 	struct ccw_device *cdev = to_ccwdev(dev);
1813 
1814 	if (work_pending(&cdev->private->todo_work))
1815 		return -EAGAIN;
1816 	/* Fail while device is being set online/offline. */
1817 	if (atomic_read(&cdev->private->onoff))
1818 		return -EAGAIN;
1819 
1820 	if (cdev->online && cdev->drv && cdev->drv->prepare)
1821 		return cdev->drv->prepare(cdev);
1822 
1823 	return 0;
1824 }
1825 
ccw_device_pm_complete(struct device * dev)1826 static void ccw_device_pm_complete(struct device *dev)
1827 {
1828 	struct ccw_device *cdev = to_ccwdev(dev);
1829 
1830 	if (cdev->online && cdev->drv && cdev->drv->complete)
1831 		cdev->drv->complete(cdev);
1832 }
1833 
ccw_device_pm_freeze(struct device * dev)1834 static int ccw_device_pm_freeze(struct device *dev)
1835 {
1836 	struct ccw_device *cdev = to_ccwdev(dev);
1837 	struct subchannel *sch = to_subchannel(cdev->dev.parent);
1838 	int ret, cm_enabled;
1839 
1840 	/* Fail suspend while device is in transistional state. */
1841 	if (!dev_fsm_final_state(cdev))
1842 		return -EAGAIN;
1843 	if (!cdev->online)
1844 		return 0;
1845 	if (cdev->drv && cdev->drv->freeze) {
1846 		ret = cdev->drv->freeze(cdev);
1847 		if (ret)
1848 			return ret;
1849 	}
1850 
1851 	spin_lock_irq(sch->lock);
1852 	cm_enabled = cdev->private->cmb != NULL;
1853 	spin_unlock_irq(sch->lock);
1854 	if (cm_enabled) {
1855 		/* Don't have the css write on memory. */
1856 		ret = ccw_set_cmf(cdev, 0);
1857 		if (ret)
1858 			return ret;
1859 	}
1860 	/* From here on, disallow device driver I/O. */
1861 	spin_lock_irq(sch->lock);
1862 	ret = cio_disable_subchannel(sch);
1863 	spin_unlock_irq(sch->lock);
1864 
1865 	return ret;
1866 }
1867 
ccw_device_pm_thaw(struct device * dev)1868 static int ccw_device_pm_thaw(struct device *dev)
1869 {
1870 	struct ccw_device *cdev = to_ccwdev(dev);
1871 	struct subchannel *sch = to_subchannel(cdev->dev.parent);
1872 	int ret, cm_enabled;
1873 
1874 	if (!cdev->online)
1875 		return 0;
1876 
1877 	spin_lock_irq(sch->lock);
1878 	/* Allow device driver I/O again. */
1879 	ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1880 	cm_enabled = cdev->private->cmb != NULL;
1881 	spin_unlock_irq(sch->lock);
1882 	if (ret)
1883 		return ret;
1884 
1885 	if (cm_enabled) {
1886 		ret = ccw_set_cmf(cdev, 1);
1887 		if (ret)
1888 			return ret;
1889 	}
1890 
1891 	if (cdev->drv && cdev->drv->thaw)
1892 		ret = cdev->drv->thaw(cdev);
1893 
1894 	return ret;
1895 }
1896 
__ccw_device_pm_restore(struct ccw_device * cdev)1897 static void __ccw_device_pm_restore(struct ccw_device *cdev)
1898 {
1899 	struct subchannel *sch = to_subchannel(cdev->dev.parent);
1900 
1901 	spin_lock_irq(sch->lock);
1902 	if (cio_is_console(sch->schid)) {
1903 		cio_enable_subchannel(sch, (u32)(addr_t)sch);
1904 		goto out_unlock;
1905 	}
1906 	/*
1907 	 * While we were sleeping, devices may have gone or become
1908 	 * available again. Kick re-detection.
1909 	 */
1910 	cdev->private->flags.resuming = 1;
1911 	cdev->private->path_new_mask = LPM_ANYPATH;
1912 	css_sched_sch_todo(sch, SCH_TODO_EVAL);
1913 	spin_unlock_irq(sch->lock);
1914 	css_wait_for_slow_path();
1915 
1916 	/* cdev may have been moved to a different subchannel. */
1917 	sch = to_subchannel(cdev->dev.parent);
1918 	spin_lock_irq(sch->lock);
1919 	if (cdev->private->state != DEV_STATE_ONLINE &&
1920 	    cdev->private->state != DEV_STATE_OFFLINE)
1921 		goto out_unlock;
1922 
1923 	ccw_device_recognition(cdev);
1924 	spin_unlock_irq(sch->lock);
1925 	wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
1926 		   cdev->private->state == DEV_STATE_DISCONNECTED);
1927 	spin_lock_irq(sch->lock);
1928 
1929 out_unlock:
1930 	cdev->private->flags.resuming = 0;
1931 	spin_unlock_irq(sch->lock);
1932 }
1933 
resume_handle_boxed(struct ccw_device * cdev)1934 static int resume_handle_boxed(struct ccw_device *cdev)
1935 {
1936 	cdev->private->state = DEV_STATE_BOXED;
1937 	if (ccw_device_notify(cdev, CIO_BOXED) == NOTIFY_OK)
1938 		return 0;
1939 	ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1940 	return -ENODEV;
1941 }
1942 
resume_handle_disc(struct ccw_device * cdev)1943 static int resume_handle_disc(struct ccw_device *cdev)
1944 {
1945 	cdev->private->state = DEV_STATE_DISCONNECTED;
1946 	if (ccw_device_notify(cdev, CIO_GONE) == NOTIFY_OK)
1947 		return 0;
1948 	ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1949 	return -ENODEV;
1950 }
1951 
ccw_device_pm_restore(struct device * dev)1952 static int ccw_device_pm_restore(struct device *dev)
1953 {
1954 	struct ccw_device *cdev = to_ccwdev(dev);
1955 	struct subchannel *sch;
1956 	int ret = 0;
1957 
1958 	__ccw_device_pm_restore(cdev);
1959 	sch = to_subchannel(cdev->dev.parent);
1960 	spin_lock_irq(sch->lock);
1961 	if (cio_is_console(sch->schid))
1962 		goto out_restore;
1963 
1964 	/* check recognition results */
1965 	switch (cdev->private->state) {
1966 	case DEV_STATE_OFFLINE:
1967 	case DEV_STATE_ONLINE:
1968 		cdev->private->flags.donotify = 0;
1969 		break;
1970 	case DEV_STATE_BOXED:
1971 		ret = resume_handle_boxed(cdev);
1972 		if (ret)
1973 			goto out_unlock;
1974 		goto out_restore;
1975 	default:
1976 		ret = resume_handle_disc(cdev);
1977 		if (ret)
1978 			goto out_unlock;
1979 		goto out_restore;
1980 	}
1981 	/* check if the device type has changed */
1982 	if (!ccw_device_test_sense_data(cdev)) {
1983 		ccw_device_update_sense_data(cdev);
1984 		ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
1985 		ret = -ENODEV;
1986 		goto out_unlock;
1987 	}
1988 	if (!cdev->online)
1989 		goto out_unlock;
1990 
1991 	if (ccw_device_online(cdev)) {
1992 		ret = resume_handle_disc(cdev);
1993 		if (ret)
1994 			goto out_unlock;
1995 		goto out_restore;
1996 	}
1997 	spin_unlock_irq(sch->lock);
1998 	wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1999 	spin_lock_irq(sch->lock);
2000 
2001 	if (ccw_device_notify(cdev, CIO_OPER) == NOTIFY_BAD) {
2002 		ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
2003 		ret = -ENODEV;
2004 		goto out_unlock;
2005 	}
2006 
2007 	/* reenable cmf, if needed */
2008 	if (cdev->private->cmb) {
2009 		spin_unlock_irq(sch->lock);
2010 		ret = ccw_set_cmf(cdev, 1);
2011 		spin_lock_irq(sch->lock);
2012 		if (ret) {
2013 			CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
2014 				      "(rc=%d)\n", cdev->private->dev_id.ssid,
2015 				      cdev->private->dev_id.devno, ret);
2016 			ret = 0;
2017 		}
2018 	}
2019 
2020 out_restore:
2021 	spin_unlock_irq(sch->lock);
2022 	if (cdev->online && cdev->drv && cdev->drv->restore)
2023 		ret = cdev->drv->restore(cdev);
2024 	return ret;
2025 
2026 out_unlock:
2027 	spin_unlock_irq(sch->lock);
2028 	return ret;
2029 }
2030 
2031 static const struct dev_pm_ops ccw_pm_ops = {
2032 	.prepare = ccw_device_pm_prepare,
2033 	.complete = ccw_device_pm_complete,
2034 	.freeze = ccw_device_pm_freeze,
2035 	.thaw = ccw_device_pm_thaw,
2036 	.restore = ccw_device_pm_restore,
2037 };
2038 
2039 static struct bus_type ccw_bus_type = {
2040 	.name   = "ccw",
2041 	.match  = ccw_bus_match,
2042 	.uevent = ccw_uevent,
2043 	.probe  = ccw_device_probe,
2044 	.remove = ccw_device_remove,
2045 	.shutdown = ccw_device_shutdown,
2046 	.pm = &ccw_pm_ops,
2047 };
2048 
2049 /**
2050  * ccw_driver_register() - register a ccw driver
2051  * @cdriver: driver to be registered
2052  *
2053  * This function is mainly a wrapper around driver_register().
2054  * Returns:
2055  *   %0 on success and a negative error value on failure.
2056  */
ccw_driver_register(struct ccw_driver * cdriver)2057 int ccw_driver_register(struct ccw_driver *cdriver)
2058 {
2059 	struct device_driver *drv = &cdriver->driver;
2060 
2061 	drv->bus = &ccw_bus_type;
2062 
2063 	return driver_register(drv);
2064 }
2065 
2066 /**
2067  * ccw_driver_unregister() - deregister a ccw driver
2068  * @cdriver: driver to be deregistered
2069  *
2070  * This function is mainly a wrapper around driver_unregister().
2071  */
ccw_driver_unregister(struct ccw_driver * cdriver)2072 void ccw_driver_unregister(struct ccw_driver *cdriver)
2073 {
2074 	driver_unregister(&cdriver->driver);
2075 }
2076 
ccw_device_todo(struct work_struct * work)2077 static void ccw_device_todo(struct work_struct *work)
2078 {
2079 	struct ccw_device_private *priv;
2080 	struct ccw_device *cdev;
2081 	struct subchannel *sch;
2082 	enum cdev_todo todo;
2083 
2084 	priv = container_of(work, struct ccw_device_private, todo_work);
2085 	cdev = priv->cdev;
2086 	sch = to_subchannel(cdev->dev.parent);
2087 	/* Find out todo. */
2088 	spin_lock_irq(cdev->ccwlock);
2089 	todo = priv->todo;
2090 	priv->todo = CDEV_TODO_NOTHING;
2091 	CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
2092 		      priv->dev_id.ssid, priv->dev_id.devno, todo);
2093 	spin_unlock_irq(cdev->ccwlock);
2094 	/* Perform todo. */
2095 	switch (todo) {
2096 	case CDEV_TODO_ENABLE_CMF:
2097 		cmf_reenable(cdev);
2098 		break;
2099 	case CDEV_TODO_REBIND:
2100 		ccw_device_do_unbind_bind(cdev);
2101 		break;
2102 	case CDEV_TODO_REGISTER:
2103 		io_subchannel_register(cdev);
2104 		break;
2105 	case CDEV_TODO_UNREG_EVAL:
2106 		if (!sch_is_pseudo_sch(sch))
2107 			css_schedule_eval(sch->schid);
2108 		/* fall-through */
2109 	case CDEV_TODO_UNREG:
2110 		if (sch_is_pseudo_sch(sch))
2111 			ccw_device_unregister(cdev);
2112 		else
2113 			ccw_device_call_sch_unregister(cdev);
2114 		break;
2115 	default:
2116 		break;
2117 	}
2118 	/* Release workqueue ref. */
2119 	put_device(&cdev->dev);
2120 }
2121 
2122 /**
2123  * ccw_device_sched_todo - schedule ccw device operation
2124  * @cdev: ccw device
2125  * @todo: todo
2126  *
2127  * Schedule the operation identified by @todo to be performed on the slow path
2128  * workqueue. Do nothing if another operation with higher priority is already
2129  * scheduled. Needs to be called with ccwdev lock held.
2130  */
ccw_device_sched_todo(struct ccw_device * cdev,enum cdev_todo todo)2131 void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
2132 {
2133 	CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
2134 		      cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
2135 		      todo);
2136 	if (cdev->private->todo >= todo)
2137 		return;
2138 	cdev->private->todo = todo;
2139 	/* Get workqueue ref. */
2140 	if (!get_device(&cdev->dev))
2141 		return;
2142 	if (!queue_work(cio_work_q, &cdev->private->todo_work)) {
2143 		/* Already queued, release workqueue ref. */
2144 		put_device(&cdev->dev);
2145 	}
2146 }
2147 
2148 /**
2149  * ccw_device_siosl() - initiate logging
2150  * @cdev: ccw device
2151  *
2152  * This function is used to invoke model-dependent logging within the channel
2153  * subsystem.
2154  */
ccw_device_siosl(struct ccw_device * cdev)2155 int ccw_device_siosl(struct ccw_device *cdev)
2156 {
2157 	struct subchannel *sch = to_subchannel(cdev->dev.parent);
2158 
2159 	return chsc_siosl(sch->schid);
2160 }
2161 EXPORT_SYMBOL_GPL(ccw_device_siosl);
2162 
2163 MODULE_LICENSE("GPL");
2164 EXPORT_SYMBOL(ccw_device_set_online);
2165 EXPORT_SYMBOL(ccw_device_set_offline);
2166 EXPORT_SYMBOL(ccw_driver_register);
2167 EXPORT_SYMBOL(ccw_driver_unregister);
2168 EXPORT_SYMBOL(get_ccwdev_by_busid);
2169