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