1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * driver for channel subsystem
4  *
5  * Copyright IBM Corp. 2002, 2010
6  *
7  * Author(s): Arnd Bergmann (arndb@de.ibm.com)
8  *	      Cornelia Huck (cornelia.huck@de.ibm.com)
9  */
10 
11 #define KMSG_COMPONENT "cio"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13 
14 #include <linux/export.h>
15 #include <linux/init.h>
16 #include <linux/device.h>
17 #include <linux/slab.h>
18 #include <linux/errno.h>
19 #include <linux/list.h>
20 #include <linux/reboot.h>
21 #include <linux/suspend.h>
22 #include <linux/proc_fs.h>
23 #include <asm/isc.h>
24 #include <asm/crw.h>
25 
26 #include "css.h"
27 #include "cio.h"
28 #include "blacklist.h"
29 #include "cio_debug.h"
30 #include "ioasm.h"
31 #include "chsc.h"
32 #include "device.h"
33 #include "idset.h"
34 #include "chp.h"
35 
36 int css_init_done = 0;
37 int max_ssid;
38 
39 #define MAX_CSS_IDX 0
40 struct channel_subsystem *channel_subsystems[MAX_CSS_IDX + 1];
41 static struct bus_type css_bus_type;
42 
43 int
for_each_subchannel(int (* fn)(struct subchannel_id,void *),void * data)44 for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
45 {
46 	struct subchannel_id schid;
47 	int ret;
48 
49 	init_subchannel_id(&schid);
50 	do {
51 		do {
52 			ret = fn(schid, data);
53 			if (ret)
54 				break;
55 		} while (schid.sch_no++ < __MAX_SUBCHANNEL);
56 		schid.sch_no = 0;
57 	} while (schid.ssid++ < max_ssid);
58 	return ret;
59 }
60 
61 struct cb_data {
62 	void *data;
63 	struct idset *set;
64 	int (*fn_known_sch)(struct subchannel *, void *);
65 	int (*fn_unknown_sch)(struct subchannel_id, void *);
66 };
67 
call_fn_known_sch(struct device * dev,void * data)68 static int call_fn_known_sch(struct device *dev, void *data)
69 {
70 	struct subchannel *sch = to_subchannel(dev);
71 	struct cb_data *cb = data;
72 	int rc = 0;
73 
74 	if (cb->set)
75 		idset_sch_del(cb->set, sch->schid);
76 	if (cb->fn_known_sch)
77 		rc = cb->fn_known_sch(sch, cb->data);
78 	return rc;
79 }
80 
call_fn_unknown_sch(struct subchannel_id schid,void * data)81 static int call_fn_unknown_sch(struct subchannel_id schid, void *data)
82 {
83 	struct cb_data *cb = data;
84 	int rc = 0;
85 
86 	if (idset_sch_contains(cb->set, schid))
87 		rc = cb->fn_unknown_sch(schid, cb->data);
88 	return rc;
89 }
90 
call_fn_all_sch(struct subchannel_id schid,void * data)91 static int call_fn_all_sch(struct subchannel_id schid, void *data)
92 {
93 	struct cb_data *cb = data;
94 	struct subchannel *sch;
95 	int rc = 0;
96 
97 	sch = get_subchannel_by_schid(schid);
98 	if (sch) {
99 		if (cb->fn_known_sch)
100 			rc = cb->fn_known_sch(sch, cb->data);
101 		put_device(&sch->dev);
102 	} else {
103 		if (cb->fn_unknown_sch)
104 			rc = cb->fn_unknown_sch(schid, cb->data);
105 	}
106 
107 	return rc;
108 }
109 
for_each_subchannel_staged(int (* fn_known)(struct subchannel *,void *),int (* fn_unknown)(struct subchannel_id,void *),void * data)110 int for_each_subchannel_staged(int (*fn_known)(struct subchannel *, void *),
111 			       int (*fn_unknown)(struct subchannel_id,
112 			       void *), void *data)
113 {
114 	struct cb_data cb;
115 	int rc;
116 
117 	cb.data = data;
118 	cb.fn_known_sch = fn_known;
119 	cb.fn_unknown_sch = fn_unknown;
120 
121 	if (fn_known && !fn_unknown) {
122 		/* Skip idset allocation in case of known-only loop. */
123 		cb.set = NULL;
124 		return bus_for_each_dev(&css_bus_type, NULL, &cb,
125 					call_fn_known_sch);
126 	}
127 
128 	cb.set = idset_sch_new();
129 	if (!cb.set)
130 		/* fall back to brute force scanning in case of oom */
131 		return for_each_subchannel(call_fn_all_sch, &cb);
132 
133 	idset_fill(cb.set);
134 
135 	/* Process registered subchannels. */
136 	rc = bus_for_each_dev(&css_bus_type, NULL, &cb, call_fn_known_sch);
137 	if (rc)
138 		goto out;
139 	/* Process unregistered subchannels. */
140 	if (fn_unknown)
141 		rc = for_each_subchannel(call_fn_unknown_sch, &cb);
142 out:
143 	idset_free(cb.set);
144 
145 	return rc;
146 }
147 
148 static void css_sch_todo(struct work_struct *work);
149 
css_sch_create_locks(struct subchannel * sch)150 static int css_sch_create_locks(struct subchannel *sch)
151 {
152 	sch->lock = kmalloc(sizeof(*sch->lock), GFP_KERNEL);
153 	if (!sch->lock)
154 		return -ENOMEM;
155 
156 	spin_lock_init(sch->lock);
157 	mutex_init(&sch->reg_mutex);
158 
159 	return 0;
160 }
161 
css_subchannel_release(struct device * dev)162 static void css_subchannel_release(struct device *dev)
163 {
164 	struct subchannel *sch = to_subchannel(dev);
165 
166 	sch->config.intparm = 0;
167 	cio_commit_config(sch);
168 	kfree(sch->lock);
169 	kfree(sch);
170 }
171 
css_validate_subchannel(struct subchannel_id schid,struct schib * schib)172 static int css_validate_subchannel(struct subchannel_id schid,
173 				   struct schib *schib)
174 {
175 	int err;
176 
177 	switch (schib->pmcw.st) {
178 	case SUBCHANNEL_TYPE_IO:
179 	case SUBCHANNEL_TYPE_MSG:
180 		if (!css_sch_is_valid(schib))
181 			err = -ENODEV;
182 		else if (is_blacklisted(schid.ssid, schib->pmcw.dev)) {
183 			CIO_MSG_EVENT(6, "Blacklisted device detected "
184 				      "at devno %04X, subchannel set %x\n",
185 				      schib->pmcw.dev, schid.ssid);
186 			err = -ENODEV;
187 		} else
188 			err = 0;
189 		break;
190 	default:
191 		err = 0;
192 	}
193 	if (err)
194 		goto out;
195 
196 	CIO_MSG_EVENT(4, "Subchannel 0.%x.%04x reports subchannel type %04X\n",
197 		      schid.ssid, schid.sch_no, schib->pmcw.st);
198 out:
199 	return err;
200 }
201 
css_alloc_subchannel(struct subchannel_id schid,struct schib * schib)202 struct subchannel *css_alloc_subchannel(struct subchannel_id schid,
203 					struct schib *schib)
204 {
205 	struct subchannel *sch;
206 	int ret;
207 
208 	ret = css_validate_subchannel(schid, schib);
209 	if (ret < 0)
210 		return ERR_PTR(ret);
211 
212 	sch = kzalloc(sizeof(*sch), GFP_KERNEL | GFP_DMA);
213 	if (!sch)
214 		return ERR_PTR(-ENOMEM);
215 
216 	sch->schid = schid;
217 	sch->schib = *schib;
218 	sch->st = schib->pmcw.st;
219 
220 	ret = css_sch_create_locks(sch);
221 	if (ret)
222 		goto err;
223 
224 	INIT_WORK(&sch->todo_work, css_sch_todo);
225 	sch->dev.release = &css_subchannel_release;
226 	device_initialize(&sch->dev);
227 	return sch;
228 
229 err:
230 	kfree(sch);
231 	return ERR_PTR(ret);
232 }
233 
css_sch_device_register(struct subchannel * sch)234 static int css_sch_device_register(struct subchannel *sch)
235 {
236 	int ret;
237 
238 	mutex_lock(&sch->reg_mutex);
239 	dev_set_name(&sch->dev, "0.%x.%04x", sch->schid.ssid,
240 		     sch->schid.sch_no);
241 	ret = device_add(&sch->dev);
242 	mutex_unlock(&sch->reg_mutex);
243 	return ret;
244 }
245 
246 /**
247  * css_sch_device_unregister - unregister a subchannel
248  * @sch: subchannel to be unregistered
249  */
css_sch_device_unregister(struct subchannel * sch)250 void css_sch_device_unregister(struct subchannel *sch)
251 {
252 	mutex_lock(&sch->reg_mutex);
253 	if (device_is_registered(&sch->dev))
254 		device_unregister(&sch->dev);
255 	mutex_unlock(&sch->reg_mutex);
256 }
257 EXPORT_SYMBOL_GPL(css_sch_device_unregister);
258 
ssd_from_pmcw(struct chsc_ssd_info * ssd,struct pmcw * pmcw)259 static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
260 {
261 	int i;
262 	int mask;
263 
264 	memset(ssd, 0, sizeof(struct chsc_ssd_info));
265 	ssd->path_mask = pmcw->pim;
266 	for (i = 0; i < 8; i++) {
267 		mask = 0x80 >> i;
268 		if (pmcw->pim & mask) {
269 			chp_id_init(&ssd->chpid[i]);
270 			ssd->chpid[i].id = pmcw->chpid[i];
271 		}
272 	}
273 }
274 
ssd_register_chpids(struct chsc_ssd_info * ssd)275 static void ssd_register_chpids(struct chsc_ssd_info *ssd)
276 {
277 	int i;
278 	int mask;
279 
280 	for (i = 0; i < 8; i++) {
281 		mask = 0x80 >> i;
282 		if (ssd->path_mask & mask)
283 			chp_new(ssd->chpid[i]);
284 	}
285 }
286 
css_update_ssd_info(struct subchannel * sch)287 void css_update_ssd_info(struct subchannel *sch)
288 {
289 	int ret;
290 
291 	ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
292 	if (ret)
293 		ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
294 
295 	ssd_register_chpids(&sch->ssd_info);
296 }
297 
type_show(struct device * dev,struct device_attribute * attr,char * buf)298 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
299 			 char *buf)
300 {
301 	struct subchannel *sch = to_subchannel(dev);
302 
303 	return sprintf(buf, "%01x\n", sch->st);
304 }
305 
306 static DEVICE_ATTR_RO(type);
307 
modalias_show(struct device * dev,struct device_attribute * attr,char * buf)308 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
309 			     char *buf)
310 {
311 	struct subchannel *sch = to_subchannel(dev);
312 
313 	return sprintf(buf, "css:t%01X\n", sch->st);
314 }
315 
316 static DEVICE_ATTR_RO(modalias);
317 
318 static struct attribute *subch_attrs[] = {
319 	&dev_attr_type.attr,
320 	&dev_attr_modalias.attr,
321 	NULL,
322 };
323 
324 static struct attribute_group subch_attr_group = {
325 	.attrs = subch_attrs,
326 };
327 
328 static const struct attribute_group *default_subch_attr_groups[] = {
329 	&subch_attr_group,
330 	NULL,
331 };
332 
chpids_show(struct device * dev,struct device_attribute * attr,char * buf)333 static ssize_t chpids_show(struct device *dev,
334 			   struct device_attribute *attr,
335 			   char *buf)
336 {
337 	struct subchannel *sch = to_subchannel(dev);
338 	struct chsc_ssd_info *ssd = &sch->ssd_info;
339 	ssize_t ret = 0;
340 	int mask;
341 	int chp;
342 
343 	for (chp = 0; chp < 8; chp++) {
344 		mask = 0x80 >> chp;
345 		if (ssd->path_mask & mask)
346 			ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
347 		else
348 			ret += sprintf(buf + ret, "00 ");
349 	}
350 	ret += sprintf(buf + ret, "\n");
351 	return ret;
352 }
353 static DEVICE_ATTR_RO(chpids);
354 
pimpampom_show(struct device * dev,struct device_attribute * attr,char * buf)355 static ssize_t pimpampom_show(struct device *dev,
356 			      struct device_attribute *attr,
357 			      char *buf)
358 {
359 	struct subchannel *sch = to_subchannel(dev);
360 	struct pmcw *pmcw = &sch->schib.pmcw;
361 
362 	return sprintf(buf, "%02x %02x %02x\n",
363 		       pmcw->pim, pmcw->pam, pmcw->pom);
364 }
365 static DEVICE_ATTR_RO(pimpampom);
366 
dev_busid_show(struct device * dev,struct device_attribute * attr,char * buf)367 static ssize_t dev_busid_show(struct device *dev,
368 			      struct device_attribute *attr,
369 			      char *buf)
370 {
371 	struct subchannel *sch = to_subchannel(dev);
372 	struct pmcw *pmcw = &sch->schib.pmcw;
373 
374 	if ((pmcw->st == SUBCHANNEL_TYPE_IO && pmcw->dnv) ||
375 	    (pmcw->st == SUBCHANNEL_TYPE_MSG && pmcw->w))
376 		return sysfs_emit(buf, "0.%x.%04x\n", sch->schid.ssid,
377 				  pmcw->dev);
378 	else
379 		return sysfs_emit(buf, "none\n");
380 }
381 static DEVICE_ATTR_RO(dev_busid);
382 
383 static struct attribute *io_subchannel_type_attrs[] = {
384 	&dev_attr_chpids.attr,
385 	&dev_attr_pimpampom.attr,
386 	&dev_attr_dev_busid.attr,
387 	NULL,
388 };
389 ATTRIBUTE_GROUPS(io_subchannel_type);
390 
391 static const struct device_type io_subchannel_type = {
392 	.groups = io_subchannel_type_groups,
393 };
394 
css_register_subchannel(struct subchannel * sch)395 int css_register_subchannel(struct subchannel *sch)
396 {
397 	int ret;
398 
399 	/* Initialize the subchannel structure */
400 	sch->dev.parent = &channel_subsystems[0]->device;
401 	sch->dev.bus = &css_bus_type;
402 	sch->dev.groups = default_subch_attr_groups;
403 
404 	if (sch->st == SUBCHANNEL_TYPE_IO)
405 		sch->dev.type = &io_subchannel_type;
406 
407 	/*
408 	 * We don't want to generate uevents for I/O subchannels that don't
409 	 * have a working ccw device behind them since they will be
410 	 * unregistered before they can be used anyway, so we delay the add
411 	 * uevent until after device recognition was successful.
412 	 * Note that we suppress the uevent for all subchannel types;
413 	 * the subchannel driver can decide itself when it wants to inform
414 	 * userspace of its existence.
415 	 */
416 	dev_set_uevent_suppress(&sch->dev, 1);
417 	css_update_ssd_info(sch);
418 	/* make it known to the system */
419 	ret = css_sch_device_register(sch);
420 	if (ret) {
421 		CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
422 			      sch->schid.ssid, sch->schid.sch_no, ret);
423 		return ret;
424 	}
425 	if (!sch->driver) {
426 		/*
427 		 * No driver matched. Generate the uevent now so that
428 		 * a fitting driver module may be loaded based on the
429 		 * modalias.
430 		 */
431 		dev_set_uevent_suppress(&sch->dev, 0);
432 		kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
433 	}
434 	return ret;
435 }
436 
css_probe_device(struct subchannel_id schid,struct schib * schib)437 static int css_probe_device(struct subchannel_id schid, struct schib *schib)
438 {
439 	struct subchannel *sch;
440 	int ret;
441 
442 	sch = css_alloc_subchannel(schid, schib);
443 	if (IS_ERR(sch))
444 		return PTR_ERR(sch);
445 
446 	ret = css_register_subchannel(sch);
447 	if (ret)
448 		put_device(&sch->dev);
449 
450 	return ret;
451 }
452 
453 static int
check_subchannel(struct device * dev,void * data)454 check_subchannel(struct device * dev, void * data)
455 {
456 	struct subchannel *sch;
457 	struct subchannel_id *schid = data;
458 
459 	sch = to_subchannel(dev);
460 	return schid_equal(&sch->schid, schid);
461 }
462 
463 struct subchannel *
get_subchannel_by_schid(struct subchannel_id schid)464 get_subchannel_by_schid(struct subchannel_id schid)
465 {
466 	struct device *dev;
467 
468 	dev = bus_find_device(&css_bus_type, NULL,
469 			      &schid, check_subchannel);
470 
471 	return dev ? to_subchannel(dev) : NULL;
472 }
473 
474 /**
475  * css_sch_is_valid() - check if a subchannel is valid
476  * @schib: subchannel information block for the subchannel
477  */
css_sch_is_valid(struct schib * schib)478 int css_sch_is_valid(struct schib *schib)
479 {
480 	if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
481 		return 0;
482 	if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
483 		return 0;
484 	return 1;
485 }
486 EXPORT_SYMBOL_GPL(css_sch_is_valid);
487 
css_evaluate_new_subchannel(struct subchannel_id schid,int slow)488 static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
489 {
490 	struct schib schib;
491 	int ccode;
492 
493 	if (!slow) {
494 		/* Will be done on the slow path. */
495 		return -EAGAIN;
496 	}
497 	/*
498 	 * The first subchannel that is not-operational (ccode==3)
499 	 * indicates that there aren't any more devices available.
500 	 * If stsch gets an exception, it means the current subchannel set
501 	 * is not valid.
502 	 */
503 	ccode = stsch(schid, &schib);
504 	if (ccode)
505 		return (ccode == 3) ? -ENXIO : ccode;
506 
507 	return css_probe_device(schid, &schib);
508 }
509 
css_evaluate_known_subchannel(struct subchannel * sch,int slow)510 static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
511 {
512 	int ret = 0;
513 
514 	if (sch->driver) {
515 		if (sch->driver->sch_event)
516 			ret = sch->driver->sch_event(sch, slow);
517 		else
518 			dev_dbg(&sch->dev,
519 				"Got subchannel machine check but "
520 				"no sch_event handler provided.\n");
521 	}
522 	if (ret != 0 && ret != -EAGAIN) {
523 		CIO_MSG_EVENT(2, "eval: sch 0.%x.%04x, rc=%d\n",
524 			      sch->schid.ssid, sch->schid.sch_no, ret);
525 	}
526 	return ret;
527 }
528 
css_evaluate_subchannel(struct subchannel_id schid,int slow)529 static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
530 {
531 	struct subchannel *sch;
532 	int ret;
533 
534 	sch = get_subchannel_by_schid(schid);
535 	if (sch) {
536 		ret = css_evaluate_known_subchannel(sch, slow);
537 		put_device(&sch->dev);
538 	} else
539 		ret = css_evaluate_new_subchannel(schid, slow);
540 	if (ret == -EAGAIN)
541 		css_schedule_eval(schid);
542 }
543 
544 /**
545  * css_sched_sch_todo - schedule a subchannel operation
546  * @sch: subchannel
547  * @todo: todo
548  *
549  * Schedule the operation identified by @todo to be performed on the slow path
550  * workqueue. Do nothing if another operation with higher priority is already
551  * scheduled. Needs to be called with subchannel lock held.
552  */
css_sched_sch_todo(struct subchannel * sch,enum sch_todo todo)553 void css_sched_sch_todo(struct subchannel *sch, enum sch_todo todo)
554 {
555 	CIO_MSG_EVENT(4, "sch_todo: sched sch=0.%x.%04x todo=%d\n",
556 		      sch->schid.ssid, sch->schid.sch_no, todo);
557 	if (sch->todo >= todo)
558 		return;
559 	/* Get workqueue ref. */
560 	if (!get_device(&sch->dev))
561 		return;
562 	sch->todo = todo;
563 	if (!queue_work(cio_work_q, &sch->todo_work)) {
564 		/* Already queued, release workqueue ref. */
565 		put_device(&sch->dev);
566 	}
567 }
568 EXPORT_SYMBOL_GPL(css_sched_sch_todo);
569 
css_sch_todo(struct work_struct * work)570 static void css_sch_todo(struct work_struct *work)
571 {
572 	struct subchannel *sch;
573 	enum sch_todo todo;
574 	int ret;
575 
576 	sch = container_of(work, struct subchannel, todo_work);
577 	/* Find out todo. */
578 	spin_lock_irq(sch->lock);
579 	todo = sch->todo;
580 	CIO_MSG_EVENT(4, "sch_todo: sch=0.%x.%04x, todo=%d\n", sch->schid.ssid,
581 		      sch->schid.sch_no, todo);
582 	sch->todo = SCH_TODO_NOTHING;
583 	spin_unlock_irq(sch->lock);
584 	/* Perform todo. */
585 	switch (todo) {
586 	case SCH_TODO_NOTHING:
587 		break;
588 	case SCH_TODO_EVAL:
589 		ret = css_evaluate_known_subchannel(sch, 1);
590 		if (ret == -EAGAIN) {
591 			spin_lock_irq(sch->lock);
592 			css_sched_sch_todo(sch, todo);
593 			spin_unlock_irq(sch->lock);
594 		}
595 		break;
596 	case SCH_TODO_UNREG:
597 		css_sch_device_unregister(sch);
598 		break;
599 	}
600 	/* Release workqueue ref. */
601 	put_device(&sch->dev);
602 }
603 
604 static struct idset *slow_subchannel_set;
605 static spinlock_t slow_subchannel_lock;
606 static wait_queue_head_t css_eval_wq;
607 static atomic_t css_eval_scheduled;
608 
slow_subchannel_init(void)609 static int __init slow_subchannel_init(void)
610 {
611 	spin_lock_init(&slow_subchannel_lock);
612 	atomic_set(&css_eval_scheduled, 0);
613 	init_waitqueue_head(&css_eval_wq);
614 	slow_subchannel_set = idset_sch_new();
615 	if (!slow_subchannel_set) {
616 		CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
617 		return -ENOMEM;
618 	}
619 	return 0;
620 }
621 
slow_eval_known_fn(struct subchannel * sch,void * data)622 static int slow_eval_known_fn(struct subchannel *sch, void *data)
623 {
624 	int eval;
625 	int rc;
626 
627 	spin_lock_irq(&slow_subchannel_lock);
628 	eval = idset_sch_contains(slow_subchannel_set, sch->schid);
629 	idset_sch_del(slow_subchannel_set, sch->schid);
630 	spin_unlock_irq(&slow_subchannel_lock);
631 	if (eval) {
632 		rc = css_evaluate_known_subchannel(sch, 1);
633 		if (rc == -EAGAIN)
634 			css_schedule_eval(sch->schid);
635 		/*
636 		 * The loop might take long time for platforms with lots of
637 		 * known devices. Allow scheduling here.
638 		 */
639 		cond_resched();
640 	}
641 	return 0;
642 }
643 
slow_eval_unknown_fn(struct subchannel_id schid,void * data)644 static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
645 {
646 	int eval;
647 	int rc = 0;
648 
649 	spin_lock_irq(&slow_subchannel_lock);
650 	eval = idset_sch_contains(slow_subchannel_set, schid);
651 	idset_sch_del(slow_subchannel_set, schid);
652 	spin_unlock_irq(&slow_subchannel_lock);
653 	if (eval) {
654 		rc = css_evaluate_new_subchannel(schid, 1);
655 		switch (rc) {
656 		case -EAGAIN:
657 			css_schedule_eval(schid);
658 			rc = 0;
659 			break;
660 		case -ENXIO:
661 		case -ENOMEM:
662 		case -EIO:
663 			/* These should abort looping */
664 			spin_lock_irq(&slow_subchannel_lock);
665 			idset_sch_del_subseq(slow_subchannel_set, schid);
666 			spin_unlock_irq(&slow_subchannel_lock);
667 			break;
668 		default:
669 			rc = 0;
670 		}
671 		/* Allow scheduling here since the containing loop might
672 		 * take a while.  */
673 		cond_resched();
674 	}
675 	return rc;
676 }
677 
css_slow_path_func(struct work_struct * unused)678 static void css_slow_path_func(struct work_struct *unused)
679 {
680 	unsigned long flags;
681 
682 	CIO_TRACE_EVENT(4, "slowpath");
683 	for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
684 				   NULL);
685 	spin_lock_irqsave(&slow_subchannel_lock, flags);
686 	if (idset_is_empty(slow_subchannel_set)) {
687 		atomic_set(&css_eval_scheduled, 0);
688 		wake_up(&css_eval_wq);
689 	}
690 	spin_unlock_irqrestore(&slow_subchannel_lock, flags);
691 }
692 
693 static DECLARE_DELAYED_WORK(slow_path_work, css_slow_path_func);
694 struct workqueue_struct *cio_work_q;
695 
css_schedule_eval(struct subchannel_id schid)696 void css_schedule_eval(struct subchannel_id schid)
697 {
698 	unsigned long flags;
699 
700 	spin_lock_irqsave(&slow_subchannel_lock, flags);
701 	idset_sch_add(slow_subchannel_set, schid);
702 	atomic_set(&css_eval_scheduled, 1);
703 	queue_delayed_work(cio_work_q, &slow_path_work, 0);
704 	spin_unlock_irqrestore(&slow_subchannel_lock, flags);
705 }
706 
css_schedule_eval_all(void)707 void css_schedule_eval_all(void)
708 {
709 	unsigned long flags;
710 
711 	spin_lock_irqsave(&slow_subchannel_lock, flags);
712 	idset_fill(slow_subchannel_set);
713 	atomic_set(&css_eval_scheduled, 1);
714 	queue_delayed_work(cio_work_q, &slow_path_work, 0);
715 	spin_unlock_irqrestore(&slow_subchannel_lock, flags);
716 }
717 
__unset_registered(struct device * dev,void * data)718 static int __unset_registered(struct device *dev, void *data)
719 {
720 	struct idset *set = data;
721 	struct subchannel *sch = to_subchannel(dev);
722 
723 	idset_sch_del(set, sch->schid);
724 	return 0;
725 }
726 
css_schedule_eval_all_unreg(unsigned long delay)727 void css_schedule_eval_all_unreg(unsigned long delay)
728 {
729 	unsigned long flags;
730 	struct idset *unreg_set;
731 
732 	/* Find unregistered subchannels. */
733 	unreg_set = idset_sch_new();
734 	if (!unreg_set) {
735 		/* Fallback. */
736 		css_schedule_eval_all();
737 		return;
738 	}
739 	idset_fill(unreg_set);
740 	bus_for_each_dev(&css_bus_type, NULL, unreg_set, __unset_registered);
741 	/* Apply to slow_subchannel_set. */
742 	spin_lock_irqsave(&slow_subchannel_lock, flags);
743 	idset_add_set(slow_subchannel_set, unreg_set);
744 	atomic_set(&css_eval_scheduled, 1);
745 	queue_delayed_work(cio_work_q, &slow_path_work, delay);
746 	spin_unlock_irqrestore(&slow_subchannel_lock, flags);
747 	idset_free(unreg_set);
748 }
749 
css_wait_for_slow_path(void)750 void css_wait_for_slow_path(void)
751 {
752 	flush_workqueue(cio_work_q);
753 }
754 
755 /* Schedule reprobing of all unregistered subchannels. */
css_schedule_reprobe(void)756 void css_schedule_reprobe(void)
757 {
758 	/* Schedule with a delay to allow merging of subsequent calls. */
759 	css_schedule_eval_all_unreg(1 * HZ);
760 }
761 EXPORT_SYMBOL_GPL(css_schedule_reprobe);
762 
763 /*
764  * Called from the machine check handler for subchannel report words.
765  */
css_process_crw(struct crw * crw0,struct crw * crw1,int overflow)766 static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
767 {
768 	struct subchannel_id mchk_schid;
769 	struct subchannel *sch;
770 
771 	if (overflow) {
772 		css_schedule_eval_all();
773 		return;
774 	}
775 	CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
776 		      "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
777 		      crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
778 		      crw0->erc, crw0->rsid);
779 	if (crw1)
780 		CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
781 			      "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
782 			      crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
783 			      crw1->anc, crw1->erc, crw1->rsid);
784 	init_subchannel_id(&mchk_schid);
785 	mchk_schid.sch_no = crw0->rsid;
786 	if (crw1)
787 		mchk_schid.ssid = (crw1->rsid >> 4) & 3;
788 
789 	if (crw0->erc == CRW_ERC_PMOD) {
790 		sch = get_subchannel_by_schid(mchk_schid);
791 		if (sch) {
792 			css_update_ssd_info(sch);
793 			put_device(&sch->dev);
794 		}
795 	}
796 	/*
797 	 * Since we are always presented with IPI in the CRW, we have to
798 	 * use stsch() to find out if the subchannel in question has come
799 	 * or gone.
800 	 */
801 	css_evaluate_subchannel(mchk_schid, 0);
802 }
803 
804 static void __init
css_generate_pgid(struct channel_subsystem * css,u32 tod_high)805 css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
806 {
807 	struct cpuid cpu_id;
808 
809 	if (css_general_characteristics.mcss) {
810 		css->global_pgid.pgid_high.ext_cssid.version = 0x80;
811 		css->global_pgid.pgid_high.ext_cssid.cssid =
812 			(css->cssid < 0) ? 0 : css->cssid;
813 	} else {
814 		css->global_pgid.pgid_high.cpu_addr = stap();
815 	}
816 	get_cpu_id(&cpu_id);
817 	css->global_pgid.cpu_id = cpu_id.ident;
818 	css->global_pgid.cpu_model = cpu_id.machine;
819 	css->global_pgid.tod_high = tod_high;
820 }
821 
channel_subsystem_release(struct device * dev)822 static void channel_subsystem_release(struct device *dev)
823 {
824 	struct channel_subsystem *css = to_css(dev);
825 
826 	mutex_destroy(&css->mutex);
827 	kfree(css);
828 }
829 
real_cssid_show(struct device * dev,struct device_attribute * a,char * buf)830 static ssize_t real_cssid_show(struct device *dev, struct device_attribute *a,
831 			       char *buf)
832 {
833 	struct channel_subsystem *css = to_css(dev);
834 
835 	if (css->cssid < 0)
836 		return -EINVAL;
837 
838 	return sprintf(buf, "%x\n", css->cssid);
839 }
840 static DEVICE_ATTR_RO(real_cssid);
841 
cm_enable_show(struct device * dev,struct device_attribute * a,char * buf)842 static ssize_t cm_enable_show(struct device *dev, struct device_attribute *a,
843 			      char *buf)
844 {
845 	struct channel_subsystem *css = to_css(dev);
846 	int ret;
847 
848 	mutex_lock(&css->mutex);
849 	ret = sprintf(buf, "%x\n", css->cm_enabled);
850 	mutex_unlock(&css->mutex);
851 	return ret;
852 }
853 
cm_enable_store(struct device * dev,struct device_attribute * a,const char * buf,size_t count)854 static ssize_t cm_enable_store(struct device *dev, struct device_attribute *a,
855 			       const char *buf, size_t count)
856 {
857 	struct channel_subsystem *css = to_css(dev);
858 	unsigned long val;
859 	int ret;
860 
861 	ret = kstrtoul(buf, 16, &val);
862 	if (ret)
863 		return ret;
864 	mutex_lock(&css->mutex);
865 	switch (val) {
866 	case 0:
867 		ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
868 		break;
869 	case 1:
870 		ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
871 		break;
872 	default:
873 		ret = -EINVAL;
874 	}
875 	mutex_unlock(&css->mutex);
876 	return ret < 0 ? ret : count;
877 }
878 static DEVICE_ATTR_RW(cm_enable);
879 
cm_enable_mode(struct kobject * kobj,struct attribute * attr,int index)880 static umode_t cm_enable_mode(struct kobject *kobj, struct attribute *attr,
881 			      int index)
882 {
883 	return css_chsc_characteristics.secm ? attr->mode : 0;
884 }
885 
886 static struct attribute *cssdev_attrs[] = {
887 	&dev_attr_real_cssid.attr,
888 	NULL,
889 };
890 
891 static struct attribute_group cssdev_attr_group = {
892 	.attrs = cssdev_attrs,
893 };
894 
895 static struct attribute *cssdev_cm_attrs[] = {
896 	&dev_attr_cm_enable.attr,
897 	NULL,
898 };
899 
900 static struct attribute_group cssdev_cm_attr_group = {
901 	.attrs = cssdev_cm_attrs,
902 	.is_visible = cm_enable_mode,
903 };
904 
905 static const struct attribute_group *cssdev_attr_groups[] = {
906 	&cssdev_attr_group,
907 	&cssdev_cm_attr_group,
908 	NULL,
909 };
910 
setup_css(int nr)911 static int __init setup_css(int nr)
912 {
913 	struct channel_subsystem *css;
914 	int ret;
915 
916 	css = kzalloc(sizeof(*css), GFP_KERNEL);
917 	if (!css)
918 		return -ENOMEM;
919 
920 	channel_subsystems[nr] = css;
921 	dev_set_name(&css->device, "css%x", nr);
922 	css->device.groups = cssdev_attr_groups;
923 	css->device.release = channel_subsystem_release;
924 
925 	mutex_init(&css->mutex);
926 	css->cssid = chsc_get_cssid(nr);
927 	css_generate_pgid(css, (u32) (get_tod_clock() >> 32));
928 
929 	ret = device_register(&css->device);
930 	if (ret) {
931 		put_device(&css->device);
932 		goto out_err;
933 	}
934 
935 	css->pseudo_subchannel = kzalloc(sizeof(*css->pseudo_subchannel),
936 					 GFP_KERNEL);
937 	if (!css->pseudo_subchannel) {
938 		device_unregister(&css->device);
939 		ret = -ENOMEM;
940 		goto out_err;
941 	}
942 
943 	css->pseudo_subchannel->dev.parent = &css->device;
944 	css->pseudo_subchannel->dev.release = css_subchannel_release;
945 	mutex_init(&css->pseudo_subchannel->reg_mutex);
946 	ret = css_sch_create_locks(css->pseudo_subchannel);
947 	if (ret) {
948 		kfree(css->pseudo_subchannel);
949 		device_unregister(&css->device);
950 		goto out_err;
951 	}
952 
953 	dev_set_name(&css->pseudo_subchannel->dev, "defunct");
954 	ret = device_register(&css->pseudo_subchannel->dev);
955 	if (ret) {
956 		put_device(&css->pseudo_subchannel->dev);
957 		device_unregister(&css->device);
958 		goto out_err;
959 	}
960 
961 	return ret;
962 out_err:
963 	channel_subsystems[nr] = NULL;
964 	return ret;
965 }
966 
css_reboot_event(struct notifier_block * this,unsigned long event,void * ptr)967 static int css_reboot_event(struct notifier_block *this,
968 			    unsigned long event,
969 			    void *ptr)
970 {
971 	struct channel_subsystem *css;
972 	int ret;
973 
974 	ret = NOTIFY_DONE;
975 	for_each_css(css) {
976 		mutex_lock(&css->mutex);
977 		if (css->cm_enabled)
978 			if (chsc_secm(css, 0))
979 				ret = NOTIFY_BAD;
980 		mutex_unlock(&css->mutex);
981 	}
982 
983 	return ret;
984 }
985 
986 static struct notifier_block css_reboot_notifier = {
987 	.notifier_call = css_reboot_event,
988 };
989 
990 /*
991  * Since the css devices are neither on a bus nor have a class
992  * nor have a special device type, we cannot stop/restart channel
993  * path measurements via the normal suspend/resume callbacks, but have
994  * to use notifiers.
995  */
css_power_event(struct notifier_block * this,unsigned long event,void * ptr)996 static int css_power_event(struct notifier_block *this, unsigned long event,
997 			   void *ptr)
998 {
999 	struct channel_subsystem *css;
1000 	int ret;
1001 
1002 	switch (event) {
1003 	case PM_HIBERNATION_PREPARE:
1004 	case PM_SUSPEND_PREPARE:
1005 		ret = NOTIFY_DONE;
1006 		for_each_css(css) {
1007 			mutex_lock(&css->mutex);
1008 			if (!css->cm_enabled) {
1009 				mutex_unlock(&css->mutex);
1010 				continue;
1011 			}
1012 			ret = __chsc_do_secm(css, 0);
1013 			ret = notifier_from_errno(ret);
1014 			mutex_unlock(&css->mutex);
1015 		}
1016 		break;
1017 	case PM_POST_HIBERNATION:
1018 	case PM_POST_SUSPEND:
1019 		ret = NOTIFY_DONE;
1020 		for_each_css(css) {
1021 			mutex_lock(&css->mutex);
1022 			if (!css->cm_enabled) {
1023 				mutex_unlock(&css->mutex);
1024 				continue;
1025 			}
1026 			ret = __chsc_do_secm(css, 1);
1027 			ret = notifier_from_errno(ret);
1028 			mutex_unlock(&css->mutex);
1029 		}
1030 		/* search for subchannels, which appeared during hibernation */
1031 		css_schedule_reprobe();
1032 		break;
1033 	default:
1034 		ret = NOTIFY_DONE;
1035 	}
1036 	return ret;
1037 
1038 }
1039 static struct notifier_block css_power_notifier = {
1040 	.notifier_call = css_power_event,
1041 };
1042 
1043 /*
1044  * Now that the driver core is running, we can setup our channel subsystem.
1045  * The struct subchannel's are created during probing.
1046  */
css_bus_init(void)1047 static int __init css_bus_init(void)
1048 {
1049 	int ret, i;
1050 
1051 	ret = chsc_init();
1052 	if (ret)
1053 		return ret;
1054 
1055 	chsc_determine_css_characteristics();
1056 	/* Try to enable MSS. */
1057 	ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
1058 	if (ret)
1059 		max_ssid = 0;
1060 	else /* Success. */
1061 		max_ssid = __MAX_SSID;
1062 
1063 	ret = slow_subchannel_init();
1064 	if (ret)
1065 		goto out;
1066 
1067 	ret = crw_register_handler(CRW_RSC_SCH, css_process_crw);
1068 	if (ret)
1069 		goto out;
1070 
1071 	if ((ret = bus_register(&css_bus_type)))
1072 		goto out;
1073 
1074 	/* Setup css structure. */
1075 	for (i = 0; i <= MAX_CSS_IDX; i++) {
1076 		ret = setup_css(i);
1077 		if (ret)
1078 			goto out_unregister;
1079 	}
1080 	ret = register_reboot_notifier(&css_reboot_notifier);
1081 	if (ret)
1082 		goto out_unregister;
1083 	ret = register_pm_notifier(&css_power_notifier);
1084 	if (ret) {
1085 		unregister_reboot_notifier(&css_reboot_notifier);
1086 		goto out_unregister;
1087 	}
1088 	css_init_done = 1;
1089 
1090 	/* Enable default isc for I/O subchannels. */
1091 	isc_register(IO_SCH_ISC);
1092 
1093 	return 0;
1094 out_unregister:
1095 	while (i-- > 0) {
1096 		struct channel_subsystem *css = channel_subsystems[i];
1097 		device_unregister(&css->pseudo_subchannel->dev);
1098 		device_unregister(&css->device);
1099 	}
1100 	bus_unregister(&css_bus_type);
1101 out:
1102 	crw_unregister_handler(CRW_RSC_SCH);
1103 	idset_free(slow_subchannel_set);
1104 	chsc_init_cleanup();
1105 	pr_alert("The CSS device driver initialization failed with "
1106 		 "errno=%d\n", ret);
1107 	return ret;
1108 }
1109 
css_bus_cleanup(void)1110 static void __init css_bus_cleanup(void)
1111 {
1112 	struct channel_subsystem *css;
1113 
1114 	for_each_css(css) {
1115 		device_unregister(&css->pseudo_subchannel->dev);
1116 		device_unregister(&css->device);
1117 	}
1118 	bus_unregister(&css_bus_type);
1119 	crw_unregister_handler(CRW_RSC_SCH);
1120 	idset_free(slow_subchannel_set);
1121 	chsc_init_cleanup();
1122 	isc_unregister(IO_SCH_ISC);
1123 }
1124 
channel_subsystem_init(void)1125 static int __init channel_subsystem_init(void)
1126 {
1127 	int ret;
1128 
1129 	ret = css_bus_init();
1130 	if (ret)
1131 		return ret;
1132 	cio_work_q = create_singlethread_workqueue("cio");
1133 	if (!cio_work_q) {
1134 		ret = -ENOMEM;
1135 		goto out_bus;
1136 	}
1137 	ret = io_subchannel_init();
1138 	if (ret)
1139 		goto out_wq;
1140 
1141 	/* Register subchannels which are already in use. */
1142 	cio_register_early_subchannels();
1143 	/* Start initial subchannel evaluation. */
1144 	css_schedule_eval_all();
1145 
1146 	return ret;
1147 out_wq:
1148 	destroy_workqueue(cio_work_q);
1149 out_bus:
1150 	css_bus_cleanup();
1151 	return ret;
1152 }
1153 subsys_initcall(channel_subsystem_init);
1154 
css_settle(struct device_driver * drv,void * unused)1155 static int css_settle(struct device_driver *drv, void *unused)
1156 {
1157 	struct css_driver *cssdrv = to_cssdriver(drv);
1158 
1159 	if (cssdrv->settle)
1160 		return cssdrv->settle();
1161 	return 0;
1162 }
1163 
css_complete_work(void)1164 int css_complete_work(void)
1165 {
1166 	int ret;
1167 
1168 	/* Wait for the evaluation of subchannels to finish. */
1169 	ret = wait_event_interruptible(css_eval_wq,
1170 				       atomic_read(&css_eval_scheduled) == 0);
1171 	if (ret)
1172 		return -EINTR;
1173 	flush_workqueue(cio_work_q);
1174 	/* Wait for the subchannel type specific initialization to finish */
1175 	return bus_for_each_drv(&css_bus_type, NULL, NULL, css_settle);
1176 }
1177 
1178 
1179 /*
1180  * Wait for the initialization of devices to finish, to make sure we are
1181  * done with our setup if the search for the root device starts.
1182  */
channel_subsystem_init_sync(void)1183 static int __init channel_subsystem_init_sync(void)
1184 {
1185 	css_complete_work();
1186 	return 0;
1187 }
1188 subsys_initcall_sync(channel_subsystem_init_sync);
1189 
channel_subsystem_reinit(void)1190 void channel_subsystem_reinit(void)
1191 {
1192 	struct channel_path *chp;
1193 	struct chp_id chpid;
1194 
1195 	chsc_enable_facility(CHSC_SDA_OC_MSS);
1196 	chp_id_for_each(&chpid) {
1197 		chp = chpid_to_chp(chpid);
1198 		if (chp)
1199 			chp_update_desc(chp);
1200 	}
1201 	cmf_reactivate();
1202 }
1203 
1204 #ifdef CONFIG_PROC_FS
cio_settle_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)1205 static ssize_t cio_settle_write(struct file *file, const char __user *buf,
1206 				size_t count, loff_t *ppos)
1207 {
1208 	int ret;
1209 
1210 	/* Handle pending CRW's. */
1211 	crw_wait_for_channel_report();
1212 	ret = css_complete_work();
1213 
1214 	return ret ? ret : count;
1215 }
1216 
1217 static const struct file_operations cio_settle_proc_fops = {
1218 	.open = nonseekable_open,
1219 	.write = cio_settle_write,
1220 	.llseek = no_llseek,
1221 };
1222 
cio_settle_init(void)1223 static int __init cio_settle_init(void)
1224 {
1225 	struct proc_dir_entry *entry;
1226 
1227 	entry = proc_create("cio_settle", S_IWUSR, NULL,
1228 			    &cio_settle_proc_fops);
1229 	if (!entry)
1230 		return -ENOMEM;
1231 	return 0;
1232 }
1233 device_initcall(cio_settle_init);
1234 #endif /*CONFIG_PROC_FS*/
1235 
sch_is_pseudo_sch(struct subchannel * sch)1236 int sch_is_pseudo_sch(struct subchannel *sch)
1237 {
1238 	if (!sch->dev.parent)
1239 		return 0;
1240 	return sch == to_css(sch->dev.parent)->pseudo_subchannel;
1241 }
1242 
css_bus_match(struct device * dev,struct device_driver * drv)1243 static int css_bus_match(struct device *dev, struct device_driver *drv)
1244 {
1245 	struct subchannel *sch = to_subchannel(dev);
1246 	struct css_driver *driver = to_cssdriver(drv);
1247 	struct css_device_id *id;
1248 
1249 	for (id = driver->subchannel_type; id->match_flags; id++) {
1250 		if (sch->st == id->type)
1251 			return 1;
1252 	}
1253 
1254 	return 0;
1255 }
1256 
css_probe(struct device * dev)1257 static int css_probe(struct device *dev)
1258 {
1259 	struct subchannel *sch;
1260 	int ret;
1261 
1262 	sch = to_subchannel(dev);
1263 	sch->driver = to_cssdriver(dev->driver);
1264 	ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
1265 	if (ret)
1266 		sch->driver = NULL;
1267 	return ret;
1268 }
1269 
css_remove(struct device * dev)1270 static int css_remove(struct device *dev)
1271 {
1272 	struct subchannel *sch;
1273 	int ret;
1274 
1275 	sch = to_subchannel(dev);
1276 	ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
1277 	sch->driver = NULL;
1278 	return ret;
1279 }
1280 
css_shutdown(struct device * dev)1281 static void css_shutdown(struct device *dev)
1282 {
1283 	struct subchannel *sch;
1284 
1285 	sch = to_subchannel(dev);
1286 	if (sch->driver && sch->driver->shutdown)
1287 		sch->driver->shutdown(sch);
1288 }
1289 
css_uevent(struct device * dev,struct kobj_uevent_env * env)1290 static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
1291 {
1292 	struct subchannel *sch = to_subchannel(dev);
1293 	int ret;
1294 
1295 	ret = add_uevent_var(env, "ST=%01X", sch->st);
1296 	if (ret)
1297 		return ret;
1298 	ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
1299 	return ret;
1300 }
1301 
css_pm_prepare(struct device * dev)1302 static int css_pm_prepare(struct device *dev)
1303 {
1304 	struct subchannel *sch = to_subchannel(dev);
1305 	struct css_driver *drv;
1306 
1307 	if (mutex_is_locked(&sch->reg_mutex))
1308 		return -EAGAIN;
1309 	if (!sch->dev.driver)
1310 		return 0;
1311 	drv = to_cssdriver(sch->dev.driver);
1312 	/* Notify drivers that they may not register children. */
1313 	return drv->prepare ? drv->prepare(sch) : 0;
1314 }
1315 
css_pm_complete(struct device * dev)1316 static void css_pm_complete(struct device *dev)
1317 {
1318 	struct subchannel *sch = to_subchannel(dev);
1319 	struct css_driver *drv;
1320 
1321 	if (!sch->dev.driver)
1322 		return;
1323 	drv = to_cssdriver(sch->dev.driver);
1324 	if (drv->complete)
1325 		drv->complete(sch);
1326 }
1327 
css_pm_freeze(struct device * dev)1328 static int css_pm_freeze(struct device *dev)
1329 {
1330 	struct subchannel *sch = to_subchannel(dev);
1331 	struct css_driver *drv;
1332 
1333 	if (!sch->dev.driver)
1334 		return 0;
1335 	drv = to_cssdriver(sch->dev.driver);
1336 	return drv->freeze ? drv->freeze(sch) : 0;
1337 }
1338 
css_pm_thaw(struct device * dev)1339 static int css_pm_thaw(struct device *dev)
1340 {
1341 	struct subchannel *sch = to_subchannel(dev);
1342 	struct css_driver *drv;
1343 
1344 	if (!sch->dev.driver)
1345 		return 0;
1346 	drv = to_cssdriver(sch->dev.driver);
1347 	return drv->thaw ? drv->thaw(sch) : 0;
1348 }
1349 
css_pm_restore(struct device * dev)1350 static int css_pm_restore(struct device *dev)
1351 {
1352 	struct subchannel *sch = to_subchannel(dev);
1353 	struct css_driver *drv;
1354 
1355 	css_update_ssd_info(sch);
1356 	if (!sch->dev.driver)
1357 		return 0;
1358 	drv = to_cssdriver(sch->dev.driver);
1359 	return drv->restore ? drv->restore(sch) : 0;
1360 }
1361 
1362 static const struct dev_pm_ops css_pm_ops = {
1363 	.prepare = css_pm_prepare,
1364 	.complete = css_pm_complete,
1365 	.freeze = css_pm_freeze,
1366 	.thaw = css_pm_thaw,
1367 	.restore = css_pm_restore,
1368 };
1369 
1370 static struct bus_type css_bus_type = {
1371 	.name     = "css",
1372 	.match    = css_bus_match,
1373 	.probe    = css_probe,
1374 	.remove   = css_remove,
1375 	.shutdown = css_shutdown,
1376 	.uevent   = css_uevent,
1377 	.pm = &css_pm_ops,
1378 };
1379 
1380 /**
1381  * css_driver_register - register a css driver
1382  * @cdrv: css driver to register
1383  *
1384  * This is mainly a wrapper around driver_register that sets name
1385  * and bus_type in the embedded struct device_driver correctly.
1386  */
css_driver_register(struct css_driver * cdrv)1387 int css_driver_register(struct css_driver *cdrv)
1388 {
1389 	cdrv->drv.bus = &css_bus_type;
1390 	return driver_register(&cdrv->drv);
1391 }
1392 EXPORT_SYMBOL_GPL(css_driver_register);
1393 
1394 /**
1395  * css_driver_unregister - unregister a css driver
1396  * @cdrv: css driver to unregister
1397  *
1398  * This is a wrapper around driver_unregister.
1399  */
css_driver_unregister(struct css_driver * cdrv)1400 void css_driver_unregister(struct css_driver *cdrv)
1401 {
1402 	driver_unregister(&cdrv->drv);
1403 }
1404 EXPORT_SYMBOL_GPL(css_driver_unregister);
1405