staging: unisys: Convert device creation to use visor_device
[deliverable/linux.git] / drivers / staging / unisys / visorbus / visorbus_main.c
CommitLineData
3703987c
EA
1/* visorbus_main.c
2 *
3 * Copyright � 2010 - 2013 UNISYS CORPORATION
4 * All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14 * NON INFRINGEMENT. See the GNU General Public License for more
15 * details.
16 */
17
18#include <linux/uuid.h>
19
4b78000e 20#include "visorbus.h"
c79b28f7 21#include "visorbus_private.h"
4b78000e 22#include "version.h"
3703987c
EA
23#include "periodic_work.h"
24#include "vbuschannel.h"
25#include "guestlinuxdebug.h"
0f41c727 26#include "vmcallinterface.h"
4b78000e 27
c79b28f7
PB
28#define MYDRVNAME "visorbus"
29
4b78000e 30/* module parameters */
f6758e79
DF
31static int visorbus_debug;
32static int visorbus_forcematch;
33static int visorbus_forcenomatch;
f6758e79 34static int visorbus_debugref;
4b78000e
PB
35#define SERIALLOOPBACKCHANADDR (100 * 1024 * 1024)
36
3703987c
EA
37#define CURRENT_FILE_PC VISOR_BUS_PC_visorbus_main_c
38#define POLLJIFFIES_TESTWORK 100
39#define POLLJIFFIES_NORMALCHANNEL 10
40
41static int visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env);
42static int visorbus_match(struct device *xdev, struct device_driver *xdrv);
43static void fix_vbus_dev_info(struct visor_device *visordev);
44
68b04f1f
DZ
45/* BUS type attributes
46 *
47 * define & implement display of bus attributes under
48 * /sys/bus/visorbus.
49 *
50 */
51
52static ssize_t version_show(struct bus_type *bus, char *buf)
53{
54 return snprintf(buf, PAGE_SIZE, "%s\n", VERSION);
55}
56
57static BUS_ATTR_RO(version);
58
59static struct attribute *visorbus_bus_attrs[] = {
60 &bus_attr_version.attr,
61 NULL,
62};
63
64static const struct attribute_group visorbus_bus_group = {
65 .attrs = visorbus_bus_attrs,
66};
67
f6758e79 68static const struct attribute_group *visorbus_bus_groups[] = {
68b04f1f
DZ
69 &visorbus_bus_group,
70 NULL,
71};
72
73
3703987c
EA
74/** This describes the TYPE of bus.
75 * (Don't confuse this with an INSTANCE of the bus.)
76 */
1fb3016e 77struct bus_type visorbus_type = {
3703987c
EA
78 .name = "visorbus",
79 .match = visorbus_match,
80 .uevent = visorbus_uevent,
68b04f1f 81 .bus_groups = visorbus_bus_groups,
3703987c
EA
82};
83
84static struct delayed_work periodic_work;
85
86/* YES, we need 2 workqueues.
87 * The reason is, workitems on the test queue may need to cancel
88 * workitems on the other queue. You will be in for trouble if you try to
89 * do this with workitems queued on the same workqueue.
90 */
91static struct workqueue_struct *periodic_test_workqueue;
92static struct workqueue_struct *periodic_dev_workqueue;
93static long long bus_count; /** number of bus instances */
3703987c
EA
94 /** ever-increasing */
95
d32517e3
DZ
96static void chipset_bus_create(struct visor_device *bus_info);
97static void chipset_bus_destroy(struct visor_device *bus_info);
a298bc0b
DZ
98static void chipset_device_create(struct visor_device *dev_info);
99static void chipset_device_destroy(struct visor_device *dev_info);
100static void chipset_device_pause(struct visor_device *dev_info);
101static void chipset_device_resume(struct visor_device *dev_info);
3703987c
EA
102
103/** These functions are implemented herein, and are called by the chipset
104 * driver to notify us about specific events.
105 */
106static struct visorchipset_busdev_notifiers chipset_notifiers = {
107 .bus_create = chipset_bus_create,
108 .bus_destroy = chipset_bus_destroy,
109 .device_create = chipset_device_create,
110 .device_destroy = chipset_device_destroy,
111 .device_pause = chipset_device_pause,
112 .device_resume = chipset_device_resume,
113};
114
115/** These functions are implemented in the chipset driver, and we call them
116 * herein when we want to acknowledge a specific event.
117 */
118static struct visorchipset_busdev_responders chipset_responders;
119
120/* filled in with info about parent chipset driver when we register with it */
121static struct ultra_vbus_deviceinfo chipset_driverinfo;
122/* filled in with info about this driver, wrt it servicing client busses */
123static struct ultra_vbus_deviceinfo clientbus_driverinfo;
124
343506bf 125/** list of visor_device structs, linked via .list_all */
3703987c
EA
126static LIST_HEAD(list_all_bus_instances);
127/** list of visor_device structs, linked via .list_all */
128static LIST_HEAD(list_all_device_instances);
129
130static int
131visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env)
132{
133 if (add_uevent_var(env, "VERSION=%s", VERSION))
134 return -ENOMEM;
135 return 0;
136}
137
138/* This is called automatically upon adding a visor_device (device_add), or
139 * adding a visor_driver (visorbus_register_visor_driver), and returns 1 iff the
140 * provided driver can control the specified device.
141 */
142static int
143visorbus_match(struct device *xdev, struct device_driver *xdrv)
144{
145 uuid_le channel_type;
146 int rc = 0;
147 int i;
148 struct visor_device *dev;
149 struct visor_driver *drv;
150
151 dev = to_visor_device(xdev);
152 drv = to_visor_driver(xdrv);
153 channel_type = visorchannel_get_uuid(dev->visorchannel);
154 if (visorbus_forcematch) {
155 rc = 1;
156 goto away;
157 }
158 if (visorbus_forcenomatch)
159 goto away;
160
161 if (!drv->channel_types)
162 goto away;
163 for (i = 0;
164 (uuid_le_cmp(drv->channel_types[i].guid, NULL_UUID_LE) != 0) ||
165 (drv->channel_types[i].name);
166 i++)
167 if (uuid_le_cmp(drv->channel_types[i].guid,
168 channel_type) == 0) {
169 rc = i + 1;
170 goto away;
171 }
172away:
173 return rc;
174}
175
176/** This is called when device_unregister() is called for the bus device
177 * instance, after all other tasks involved with destroying the device
178 * are complete.
179 */
180static void
181visorbus_release_busdevice(struct device *xdev)
182{
343506bf 183 struct visor_device *dev = dev_get_drvdata(xdev);
3703987c
EA
184
185 dev_set_drvdata(xdev, NULL);
343506bf 186 kfree(dev);
3703987c
EA
187}
188
189/** This is called when device_unregister() is called for each child
190 * device instance.
191 */
192static void
193visorbus_release_device(struct device *xdev)
194{
195 struct visor_device *dev = to_visor_device(xdev);
196
197 if (dev->periodic_work) {
198 visor_periodic_work_destroy(dev->periodic_work);
199 dev->periodic_work = NULL;
200 }
201 if (dev->visorchannel) {
202 visorchannel_destroy(dev->visorchannel);
203 dev->visorchannel = NULL;
204 }
205 kfree(dev);
206}
207
32710457
PB
208/* Implement publishing of device node attributes under:
209 *
210 * /sys/bus/visorbus<x>/dev<y>/devmajorminor
211 *
212 */
213
214#define to_devmajorminor_attr(_attr) \
215 container_of(_attr, struct devmajorminor_attribute, attr)
216#define to_visor_device_from_kobjdevmajorminor(obj) \
217 container_of(obj, struct visor_device, kobjdevmajorminor)
218
219struct devmajorminor_attribute {
220 struct attribute attr;
221 int slot;
222 ssize_t (*show)(struct visor_device *, int slot, char *buf);
223 ssize_t (*store)(struct visor_device *, int slot, const char *buf,
224 size_t count);
225};
226
227static ssize_t DEVMAJORMINOR_ATTR(struct visor_device *dev, int slot, char *buf)
228{
229 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
230
231 if (slot < 0 || slot >= maxdevnodes)
232 return 0;
233 return snprintf(buf, PAGE_SIZE, "%d:%d\n",
234 dev->devnodes[slot].major, dev->devnodes[slot].minor);
235}
236
237static ssize_t
238devmajorminor_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
239{
240 struct devmajorminor_attribute *devmajorminor_attr =
241 to_devmajorminor_attr(attr);
242 struct visor_device *dev = to_visor_device_from_kobjdevmajorminor(kobj);
243 ssize_t ret = 0;
244
245 if (devmajorminor_attr->show)
246 ret = devmajorminor_attr->show(dev,
247 devmajorminor_attr->slot, buf);
248 return ret;
249}
250
251static ssize_t
252devmajorminor_attr_store(struct kobject *kobj,
253 struct attribute *attr, const char *buf, size_t count)
254{
255 struct devmajorminor_attribute *devmajorminor_attr =
256 to_devmajorminor_attr(attr);
257 struct visor_device *dev = to_visor_device_from_kobjdevmajorminor(kobj);
258 ssize_t ret = 0;
259
260 if (devmajorminor_attr->store)
261 ret = devmajorminor_attr->store(dev,
262 devmajorminor_attr->slot,
263 buf, count);
264 return ret;
265}
266
267static int register_devmajorminor_attributes(struct visor_device *dev);
268
f6758e79 269static int
32710457
PB
270devmajorminor_create_file(struct visor_device *dev, const char *name,
271 int major, int minor)
272{
273 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
274 struct devmajorminor_attribute *myattr = NULL;
275 int x = -1, rc = 0, slot = -1;
276
277 register_devmajorminor_attributes(dev);
278 for (slot = 0; slot < maxdevnodes; slot++)
279 if (!dev->devnodes[slot].attr)
280 break;
281 if (slot == maxdevnodes) {
282 rc = -ENOMEM;
283 goto away;
284 }
285 myattr = kmalloc(sizeof(*myattr), GFP_KERNEL);
286 if (!myattr) {
287 rc = -ENOMEM;
288 goto away;
289 }
290 memset(myattr, 0, sizeof(struct devmajorminor_attribute));
291 myattr->show = DEVMAJORMINOR_ATTR;
292 myattr->store = NULL;
293 myattr->slot = slot;
294 myattr->attr.name = name;
295 myattr->attr.mode = S_IRUGO;
296 dev->devnodes[slot].attr = myattr;
297 dev->devnodes[slot].major = major;
298 dev->devnodes[slot].minor = minor;
299 x = sysfs_create_file(&dev->kobjdevmajorminor, &myattr->attr);
300 if (x < 0) {
301 rc = x;
302 goto away;
303 }
304 kobject_uevent(&dev->device.kobj, KOBJ_ONLINE);
305away:
306 if (rc < 0) {
307 kfree(myattr);
308 myattr = NULL;
309 dev->devnodes[slot].attr = NULL;
310 }
311 return rc;
312}
313
f6758e79 314static void
32710457
PB
315devmajorminor_remove_file(struct visor_device *dev, int slot)
316{
317 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
318 struct devmajorminor_attribute *myattr = NULL;
319
320 if (slot < 0 || slot >= maxdevnodes)
321 return;
322 myattr = (struct devmajorminor_attribute *)(dev->devnodes[slot].attr);
323 if (myattr)
324 return;
325 sysfs_remove_file(&dev->kobjdevmajorminor, &myattr->attr);
326 kobject_uevent(&dev->device.kobj, KOBJ_OFFLINE);
327 dev->devnodes[slot].attr = NULL;
328 kfree(myattr);
329}
330
f6758e79 331static void
32710457
PB
332devmajorminor_remove_all_files(struct visor_device *dev)
333{
334 int i = 0;
335 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
336
337 for (i = 0; i < maxdevnodes; i++)
338 devmajorminor_remove_file(dev, i);
339}
340
341static const struct sysfs_ops devmajorminor_sysfs_ops = {
342 .show = devmajorminor_attr_show,
343 .store = devmajorminor_attr_store,
344};
345
346static struct kobj_type devmajorminor_kobj_type = {
347 .sysfs_ops = &devmajorminor_sysfs_ops
348};
349
350static int
351register_devmajorminor_attributes(struct visor_device *dev)
352{
353 int rc = 0, x = 0;
354
355 if (dev->kobjdevmajorminor.parent)
356 goto away; /* already registered */
357 x = kobject_init_and_add(&dev->kobjdevmajorminor,
358 &devmajorminor_kobj_type, &dev->device.kobj,
359 "devmajorminor");
360 if (x < 0) {
361 rc = x;
362 goto away;
363 }
364
365 kobject_uevent(&dev->kobjdevmajorminor, KOBJ_ADD);
366
367away:
368 return rc;
369}
370
f6758e79 371static void
32710457
PB
372unregister_devmajorminor_attributes(struct visor_device *dev)
373{
374 if (!dev->kobjdevmajorminor.parent)
375 return; /* already unregistered */
376 devmajorminor_remove_all_files(dev);
377
378 kobject_del(&dev->kobjdevmajorminor);
379 kobject_put(&dev->kobjdevmajorminor);
380 dev->kobjdevmajorminor.parent = NULL;
381}
382
826b6a0f
PB
383/* begin implementation of specific channel attributes to appear under
384* /sys/bus/visorbus<x>/dev<y>/channel
385*/
79573162
DZ
386static ssize_t physaddr_show(struct device *dev, struct device_attribute *attr,
387 char *buf)
826b6a0f 388{
79573162
DZ
389 struct visor_device *vdev = to_visor_device(dev);
390
391 if (!vdev->visorchannel)
826b6a0f
PB
392 return 0;
393 return snprintf(buf, PAGE_SIZE, "0x%Lx\n",
79573162 394 visorchannel_get_physaddr(vdev->visorchannel));
826b6a0f
PB
395}
396
79573162
DZ
397static ssize_t nbytes_show(struct device *dev, struct device_attribute *attr,
398 char *buf)
826b6a0f 399{
79573162
DZ
400 struct visor_device *vdev = to_visor_device(dev);
401
402 if (!vdev->visorchannel)
826b6a0f
PB
403 return 0;
404 return snprintf(buf, PAGE_SIZE, "0x%lx\n",
79573162 405 visorchannel_get_nbytes(vdev->visorchannel));
826b6a0f
PB
406}
407
79573162
DZ
408static ssize_t clientpartition_show(struct device *dev,
409 struct device_attribute *attr, char *buf)
410{
411 struct visor_device *vdev = to_visor_device(dev);
412
413 if (!vdev->visorchannel)
826b6a0f
PB
414 return 0;
415 return snprintf(buf, PAGE_SIZE, "0x%Lx\n",
79573162 416 visorchannel_get_clientpartition(vdev->visorchannel));
826b6a0f
PB
417}
418
79573162
DZ
419static ssize_t typeguid_show(struct device *dev, struct device_attribute *attr,
420 char *buf)
826b6a0f 421{
79573162 422 struct visor_device *vdev = to_visor_device(dev);
826b6a0f
PB
423 char s[99];
424
79573162 425 if (!vdev->visorchannel)
826b6a0f
PB
426 return 0;
427 return snprintf(buf, PAGE_SIZE, "%s\n",
79573162 428 visorchannel_id(vdev->visorchannel, s));
826b6a0f
PB
429}
430
79573162
DZ
431static ssize_t zoneguid_show(struct device *dev, struct device_attribute *attr,
432 char *buf)
826b6a0f 433{
79573162 434 struct visor_device *vdev = to_visor_device(dev);
826b6a0f
PB
435 char s[99];
436
79573162 437 if (!vdev->visorchannel)
826b6a0f
PB
438 return 0;
439 return snprintf(buf, PAGE_SIZE, "%s\n",
79573162 440 visorchannel_zoneid(vdev->visorchannel, s));
826b6a0f
PB
441}
442
79573162
DZ
443static ssize_t typename_show(struct device *dev, struct device_attribute *attr,
444 char *buf)
826b6a0f 445{
79573162 446 struct visor_device *vdev = to_visor_device(dev);
826b6a0f 447 int i = 0;
79573162
DZ
448 struct bus_type *xbus = dev->bus;
449 struct device_driver *xdrv = dev->driver;
826b6a0f
PB
450 struct visor_driver *drv = NULL;
451
79573162 452 if (!vdev->visorchannel || !xbus || !xdrv)
826b6a0f 453 return 0;
79573162 454 i = xbus->match(dev, xdrv);
826b6a0f
PB
455 if (!i)
456 return 0;
457 drv = to_visor_driver(xdrv);
458 return snprintf(buf, PAGE_SIZE, "%s\n", drv->channel_types[i - 1].name);
459}
460
79573162
DZ
461static DEVICE_ATTR_RO(physaddr);
462static DEVICE_ATTR_RO(nbytes);
463static DEVICE_ATTR_RO(clientpartition);
464static DEVICE_ATTR_RO(typeguid);
465static DEVICE_ATTR_RO(zoneguid);
466static DEVICE_ATTR_RO(typename);
79573162
DZ
467
468static struct attribute *channel_attrs[] = {
469 &dev_attr_physaddr.attr,
470 &dev_attr_nbytes.attr,
471 &dev_attr_clientpartition.attr,
472 &dev_attr_typeguid.attr,
473 &dev_attr_zoneguid.attr,
474 &dev_attr_typename.attr,
826b6a0f
PB
475};
476
79573162
DZ
477static struct attribute_group channel_attr_grp = {
478 .name = "channel",
479 .attrs = channel_attrs,
826b6a0f
PB
480};
481
79573162
DZ
482static const struct attribute_group *visorbus_dev_groups[] = {
483 &channel_attr_grp,
484 NULL
826b6a0f
PB
485};
486
79573162 487/* end implementation of specific channel attributes */
826b6a0f 488
3703987c
EA
489/* BUS instance attributes
490 *
491 * define & implement display of bus attributes under
492 * /sys/bus/visorbus/busses/visorbus<n>.
493 *
494 * This is a bit hoaky because the kernel does not yet have the infrastructure
495 * to separate bus INSTANCE attributes from bus TYPE attributes...
496 * so we roll our own. See businst.c / businst.h.
497 *
498 */
499
d181dd03
DZ
500static ssize_t partition_handle_show(struct device *dev,
501 struct device_attribute *attr,
502 char *buf) {
5ecbd5d4
DZ
503 struct visor_device *vdev = to_visor_device(dev);
504 u64 handle = visorchannel_get_clientpartition(vdev->visorchannel);
3703987c 505
5ecbd5d4 506 return snprintf(buf, PAGE_SIZE, "0x%Lx\n", handle);
3703987c
EA
507}
508
d181dd03
DZ
509static ssize_t partition_guid_show(struct device *dev,
510 struct device_attribute *attr,
511 char *buf) {
5ecbd5d4 512 struct visor_device *vdev = to_visor_device(dev);
3703987c 513
5ecbd5d4 514 return snprintf(buf, PAGE_SIZE, "{%pUb}\n", &vdev->partition_uuid);
3703987c
EA
515}
516
d181dd03
DZ
517static ssize_t partition_name_show(struct device *dev,
518 struct device_attribute *attr,
519 char *buf) {
5ecbd5d4 520 struct visor_device *vdev = to_visor_device(dev);
3703987c 521
5ecbd5d4 522 return snprintf(buf, PAGE_SIZE, "%s\n", vdev->name);
3703987c
EA
523}
524
d181dd03
DZ
525static ssize_t channel_addr_show(struct device *dev,
526 struct device_attribute *attr,
527 char *buf) {
5ecbd5d4
DZ
528 struct visor_device *vdev = to_visor_device(dev);
529 u64 addr = visorchannel_get_physaddr(vdev->visorchannel);
3703987c 530
5ecbd5d4 531 return snprintf(buf, PAGE_SIZE, "0x%Lx\n", addr);
3703987c
EA
532}
533
d181dd03
DZ
534static ssize_t channel_bytes_show(struct device *dev,
535 struct device_attribute *attr,
536 char *buf) {
5ecbd5d4
DZ
537 struct visor_device *vdev = to_visor_device(dev);
538 u64 nbytes = visorchannel_get_nbytes(vdev->visorchannel);
3703987c 539
5ecbd5d4 540 return snprintf(buf, PAGE_SIZE, "0x%Lx\n", nbytes);
3703987c
EA
541}
542
d181dd03
DZ
543static ssize_t channel_id_show(struct device *dev,
544 struct device_attribute *attr,
545 char *buf) {
5ecbd5d4 546 struct visor_device *vdev = to_visor_device(dev);
3703987c
EA
547 int len = 0;
548
5ecbd5d4
DZ
549 if (vdev->visorchannel) {
550 visorchannel_id(vdev->visorchannel, buf);
3703987c
EA
551 len = strlen(buf);
552 buf[len++] = '\n';
553 }
554 return len;
555}
556
d181dd03
DZ
557static ssize_t client_bus_info_show(struct device *dev,
558 struct device_attribute *attr,
559 char *buf) {
5ecbd5d4
DZ
560 struct visor_device *vdev = to_visor_device(dev);
561 struct visorchannel *channel = vdev->visorchannel;
562
3703987c
EA
563 int i, x, remain = PAGE_SIZE;
564 unsigned long off;
565 char *p = buf;
566 u8 *partition_name;
567 struct ultra_vbus_deviceinfo dev_info;
568
569 partition_name = "";
5ecbd5d4
DZ
570 if (channel) {
571 if (vdev->name)
572 partition_name = vdev->name;
3703987c 573 x = snprintf(p, remain,
65bd6e46 574 "Client device / client driver info for %s partition (vbus #%d):\n",
5ecbd5d4 575 partition_name, vdev->chipset_dev_no);
3703987c
EA
576 p += x;
577 remain -= x;
5ecbd5d4 578 x = visorchannel_read(channel,
3703987c
EA
579 offsetof(struct
580 spar_vbus_channel_protocol,
581 chp_info),
582 &dev_info, sizeof(dev_info));
583 if (x >= 0) {
584 x = vbuschannel_devinfo_to_string(&dev_info, p,
585 remain, -1);
586 p += x;
587 remain -= x;
588 }
5ecbd5d4 589 x = visorchannel_read(channel,
3703987c
EA
590 offsetof(struct
591 spar_vbus_channel_protocol,
592 bus_info),
593 &dev_info, sizeof(dev_info));
594 if (x >= 0) {
595 x = vbuschannel_devinfo_to_string(&dev_info, p,
596 remain, -1);
597 p += x;
598 remain -= x;
599 }
600 off = offsetof(struct spar_vbus_channel_protocol, dev_info);
601 i = 0;
602 while (off + sizeof(dev_info) <=
5ecbd5d4
DZ
603 visorchannel_get_nbytes(channel)) {
604 x = visorchannel_read(channel,
3703987c
EA
605 off, &dev_info, sizeof(dev_info));
606 if (x >= 0) {
607 x = vbuschannel_devinfo_to_string
608 (&dev_info, p, remain, i);
609 p += x;
610 remain -= x;
611 }
612 off += sizeof(dev_info);
613 i++;
614 }
615 }
616 return PAGE_SIZE - remain;
617}
618
d181dd03
DZ
619static DEVICE_ATTR_RO(partition_handle);
620static DEVICE_ATTR_RO(partition_guid);
621static DEVICE_ATTR_RO(partition_name);
622static DEVICE_ATTR_RO(channel_addr);
623static DEVICE_ATTR_RO(channel_bytes);
624static DEVICE_ATTR_RO(channel_id);
625static DEVICE_ATTR_RO(client_bus_info);
626
627static struct attribute *dev_attrs[] = {
628 &dev_attr_partition_handle.attr,
629 &dev_attr_partition_guid.attr,
630 &dev_attr_partition_name.attr,
631 &dev_attr_channel_addr.attr,
632 &dev_attr_channel_bytes.attr,
633 &dev_attr_channel_id.attr,
634 &dev_attr_client_bus_info.attr,
635 NULL
636};
3703987c 637
d181dd03
DZ
638static struct attribute_group dev_attr_grp = {
639 .attrs = dev_attrs,
640};
3703987c 641
d181dd03
DZ
642static const struct attribute_group *visorbus_groups[] = {
643 &dev_attr_grp,
644 NULL
645};
3703987c
EA
646
647/* DRIVER attributes
648 *
649 * define & implement display of driver attributes under
650 * /sys/bus/visorbus/drivers/<drivername>.
651 *
652 */
653
654static ssize_t
655DRIVER_ATTR_version(struct device_driver *xdrv, char *buf)
656{
657 struct visor_driver *drv = to_visor_driver(xdrv);
658
659 return snprintf(buf, PAGE_SIZE, "%s\n", drv->version);
660}
661
662static int
663register_driver_attributes(struct visor_driver *drv)
664{
665 int rc;
666 struct driver_attribute version =
667 __ATTR(version, S_IRUGO, DRIVER_ATTR_version, NULL);
668 drv->version_attr = version;
669 rc = driver_create_file(&drv->driver, &drv->version_attr);
670 return rc;
671}
672
673static void
674unregister_driver_attributes(struct visor_driver *drv)
675{
676 driver_remove_file(&drv->driver, &drv->version_attr);
677}
678
3703987c
EA
679static void
680dev_periodic_work(void *xdev)
681{
682 struct visor_device *dev = (struct visor_device *)xdev;
683 struct visor_driver *drv = to_visor_driver(dev->device.driver);
684
685 down(&dev->visordriver_callback_lock);
686 if (drv->channel_interrupt)
687 drv->channel_interrupt(dev);
688 up(&dev->visordriver_callback_lock);
689 if (!visor_periodic_work_nextperiod(dev->periodic_work))
690 put_device(&dev->device);
691}
692
693static void
694dev_start_periodic_work(struct visor_device *dev)
695{
696 if (dev->being_removed)
697 return;
698 /* now up by at least 2 */
699 get_device(&dev->device);
700 if (!visor_periodic_work_start(dev->periodic_work))
701 put_device(&dev->device);
702}
703
704static void
705dev_stop_periodic_work(struct visor_device *dev)
706{
707 if (visor_periodic_work_stop(dev->periodic_work))
708 put_device(&dev->device);
709}
710
711/** This is called automatically upon adding a visor_device (device_add), or
712 * adding a visor_driver (visorbus_register_visor_driver), but only after
713 * visorbus_match has returned 1 to indicate a successful match between
714 * driver and device.
715 */
716static int
717visordriver_probe_device(struct device *xdev)
718{
719 int rc;
720 struct visor_driver *drv;
721 struct visor_device *dev;
722
723 drv = to_visor_driver(xdev->driver);
724 dev = to_visor_device(xdev);
725 down(&dev->visordriver_callback_lock);
779d0752 726 dev->being_removed = false;
3703987c
EA
727 /*
728 * ensure that the dev->being_removed flag is cleared before
729 * we start the probe
730 */
731 wmb();
732 get_device(&dev->device);
733 if (!drv->probe) {
734 up(&dev->visordriver_callback_lock);
735 rc = -1;
736 goto away;
737 }
738 rc = drv->probe(dev);
739 if (rc < 0)
740 goto away;
741
742 fix_vbus_dev_info(dev);
743 up(&dev->visordriver_callback_lock);
744 rc = 0;
745away:
746 if (rc != 0)
747 put_device(&dev->device);
748 /* We could get here more than once if the child driver module is
749 * unloaded and re-loaded while devices are present. That's why we
750 * need a flag to be sure that we only respond to the device_create
751 * once. We cannot respond to the device_create prior to here,
752 * because until we call drv->probe() above, the channel has not been
753 * initialized.
754 */
755 if (!dev->responded_to_device_create) {
b4b598fd 756
779d0752 757 dev->responded_to_device_create = true;
3703987c 758 if (chipset_responders.device_create)
a298bc0b 759 (*chipset_responders.device_create)(dev, rc);
3703987c
EA
760 }
761 return rc;
762}
763
764/** This is called when device_unregister() is called for each child device
765 * instance, to notify the appropriate visorbus_driver that the device is
766 * going away, and to decrease the reference count of the device.
767 */
768static int
769visordriver_remove_device(struct device *xdev)
770{
771 int rc = 0;
772 struct visor_device *dev;
773 struct visor_driver *drv;
774
775 dev = to_visor_device(xdev);
776 drv = to_visor_driver(xdev->driver);
777 down(&dev->visordriver_callback_lock);
779d0752 778 dev->being_removed = true;
3703987c
EA
779 /*
780 * ensure that the dev->being_removed flag is set before we start the
781 * actual removal
782 */
783 wmb();
784 if (drv) {
785 if (drv->remove)
786 drv->remove(dev);
787 }
788 up(&dev->visordriver_callback_lock);
789 dev_stop_periodic_work(dev);
790 devmajorminor_remove_all_files(dev);
791
792 put_device(&dev->device);
793
794 return rc;
795}
796
797/** A particular type of visor driver calls this function to register
798 * the driver. The caller MUST fill in the following fields within the
799 * #drv structure:
800 * name, version, owner, channel_types, probe, remove
801 *
802 * Here's how the whole Linux bus / driver / device model works.
803 *
804 * At system start-up, the visorbus kernel module is loaded, which registers
805 * visorbus_type as a bus type, using bus_register().
806 *
807 * All kernel modules that support particular device types on a
808 * visorbus bus are loaded. Each of these kernel modules calls
809 * visorbus_register_visor_driver() in their init functions, passing a
810 * visor_driver struct. visorbus_register_visor_driver() in turn calls
811 * register_driver(&visor_driver.driver). This .driver member is
812 * initialized with generic methods (like probe), whose sole responsibility
813 * is to act as a broker for the real methods, which are within the
814 * visor_driver struct. (This is the way the subclass behavior is
815 * implemented, since visor_driver is essentially a subclass of the
816 * generic driver.) Whenever a driver_register() happens, core bus code in
817 * the kernel does (see device_attach() in drivers/base/dd.c):
818 *
819 * for each dev associated with the bus (the bus that driver is on) that
820 * does not yet have a driver
821 * if bus.match(dev,newdriver) == yes_matched ** .match specified
822 * ** during bus_register().
823 * newdriver.probe(dev) ** for visor drivers, this will call
824 * ** the generic driver.probe implemented in visorbus.c,
825 * ** which in turn calls the probe specified within the
826 * ** struct visor_driver (which was specified by the
827 * ** actual device driver as part of
828 * ** visorbus_register_visor_driver()).
829 *
830 * The above dance also happens when a new device appears.
831 * So the question is, how are devices created within the system?
832 * Basically, just call device_add(dev). See pci_bus_add_devices().
833 * pci_scan_device() shows an example of how to build a device struct. It
834 * returns the newly-created struct to pci_scan_single_device(), who adds it
835 * to the list of devices at PCIBUS.devices. That list of devices is what
836 * is traversed by pci_bus_add_devices().
837 *
838 */
839int visorbus_register_visor_driver(struct visor_driver *drv)
840{
841 int rc = 0;
842
843 drv->driver.name = drv->name;
844 drv->driver.bus = &visorbus_type;
845 drv->driver.probe = visordriver_probe_device;
846 drv->driver.remove = visordriver_remove_device;
847 drv->driver.owner = drv->owner;
848
849 /* driver_register does this:
850 * bus_add_driver(drv)
851 * ->if (drv.bus) ** (bus_type) **
852 * driver_attach(drv)
853 * for each dev with bus type of drv.bus
854 * if (!dev.drv) ** no driver assigned yet **
855 * if (bus.match(dev,drv)) [visorbus_match]
856 * dev.drv = drv
857 * if (!drv.probe(dev)) [visordriver_probe_device]
858 * dev.drv = NULL
859 */
860
861 rc = driver_register(&drv->driver);
862 if (rc < 0)
863 return rc;
864 rc = register_driver_attributes(drv);
865 return rc;
866}
867EXPORT_SYMBOL_GPL(visorbus_register_visor_driver);
868
869/** A particular type of visor driver calls this function to unregister
870 * the driver, i.e., within its module_exit function.
871 */
872void
873visorbus_unregister_visor_driver(struct visor_driver *drv)
874{
875 unregister_driver_attributes(drv);
876 driver_unregister(&drv->driver);
877}
878EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver);
879
880int
881visorbus_read_channel(struct visor_device *dev, unsigned long offset,
882 void *dest, unsigned long nbytes)
883{
884 return visorchannel_read(dev->visorchannel, offset, dest, nbytes);
885}
886EXPORT_SYMBOL_GPL(visorbus_read_channel);
887
888int
889visorbus_write_channel(struct visor_device *dev, unsigned long offset,
890 void *src, unsigned long nbytes)
891{
892 return visorchannel_write(dev->visorchannel, offset, src, nbytes);
893}
894EXPORT_SYMBOL_GPL(visorbus_write_channel);
895
896int
897visorbus_clear_channel(struct visor_device *dev, unsigned long offset, u8 ch,
898 unsigned long nbytes)
899{
900 return visorchannel_clear(dev->visorchannel, offset, ch, nbytes);
901}
902EXPORT_SYMBOL_GPL(visorbus_clear_channel);
903
904int
905visorbus_registerdevnode(struct visor_device *dev,
906 const char *name, int major, int minor)
907{
908 return devmajorminor_create_file(dev, name, major, minor);
909}
910EXPORT_SYMBOL_GPL(visorbus_registerdevnode);
911
912/** We don't really have a real interrupt, so for now we just call the
913 * interrupt function periodically...
914 */
915void
916visorbus_enable_channel_interrupts(struct visor_device *dev)
917{
918 dev_start_periodic_work(dev);
919}
920EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts);
921
922void
923visorbus_disable_channel_interrupts(struct visor_device *dev)
924{
925 dev_stop_periodic_work(dev);
926}
927EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts);
928
929/** This is how everything starts from the device end.
930 * This function is called when a channel first appears via a ControlVM
931 * message. In response, this function allocates a visor_device to
932 * correspond to the new channel, and attempts to connect it the appropriate
933 * driver. If the appropriate driver is found, the visor_driver.probe()
934 * function for that driver will be called, and will be passed the new
935 * visor_device that we just created.
936 *
937 * It's ok if the appropriate driver is not yet loaded, because in that case
938 * the new device struct will just stick around in the bus' list of devices.
939 * When the appropriate driver calls visorbus_register_visor_driver(), the
940 * visor_driver.probe() for the new driver will be called with the new
941 * device.
942 */
943static int
a298bc0b 944create_visor_device(struct visor_device *dev)
3703987c
EA
945{
946 int rc = -1;
a298bc0b
DZ
947 u32 chipset_bus_no = dev->chipset_bus_no;
948 u32 chipset_dev_no = dev->chipset_dev_no;
3703987c
EA
949
950 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, chipset_dev_no, chipset_bus_no,
951 POSTCODE_SEVERITY_INFO);
3703987c 952
3703987c
EA
953 sema_init(&dev->visordriver_callback_lock, 1); /* unlocked */
954 dev->device.bus = &visorbus_type;
79573162 955 dev->device.groups = visorbus_dev_groups;
3703987c
EA
956 device_initialize(&dev->device);
957 dev->device.release = visorbus_release_device;
958 /* keep a reference just for us (now 2) */
959 get_device(&dev->device);
3703987c
EA
960 dev->periodic_work =
961 visor_periodic_work_create(POLLJIFFIES_NORMALCHANNEL,
962 periodic_dev_workqueue,
963 dev_periodic_work,
964 dev, dev_name(&dev->device));
965 if (!dev->periodic_work) {
966 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, chipset_dev_no,
967 DIAG_SEVERITY_ERR);
968 goto away;
969 }
970
971 /* bus_id must be a unique name with respect to this bus TYPE
972 * (NOT bus instance). That's why we need to include the bus
973 * number within the name.
974 */
b4b598fd 975 dev_set_name(&dev->device, "vbus%u:dev%u",
3703987c
EA
976 chipset_bus_no, chipset_dev_no);
977
978 /* device_add does this:
979 * bus_add_device(dev)
980 * ->device_attach(dev)
981 * ->for each driver drv registered on the bus that dev is on
982 * if (dev.drv) ** device already has a driver **
983 * ** not sure we could ever get here... **
984 * else
985 * if (bus.match(dev,drv)) [visorbus_match]
986 * dev.drv = drv
987 * if (!drv.probe(dev)) [visordriver_probe_device]
988 * dev.drv = NULL
989 *
990 * Note that device_add does NOT fail if no driver failed to
991 * claim the device. The device will be linked onto
992 * bus_type.klist_devices regardless (use bus_for_each_dev).
993 */
994 rc = device_add(&dev->device);
995 if (rc < 0) {
996 POSTCODE_LINUX_3(DEVICE_ADD_PC, chipset_bus_no,
997 DIAG_SEVERITY_ERR);
998 goto away;
999 }
1000
3703987c
EA
1001 rc = register_devmajorminor_attributes(dev);
1002 if (rc < 0) {
1003 POSTCODE_LINUX_3(DEVICE_REGISTER_FAILURE_PC, chipset_dev_no,
1004 DIAG_SEVERITY_ERR);
a298bc0b 1005 goto away_register;
3703987c
EA
1006 }
1007
a298bc0b
DZ
1008 list_add_tail(&dev->list_all, &list_all_device_instances);
1009 return 0;
3703987c 1010
a298bc0b
DZ
1011away_register:
1012 device_unregister(&dev->device);
3703987c 1013away:
a298bc0b 1014 put_device(&dev->device);
3703987c
EA
1015 return rc;
1016}
1017
1018static void
1019remove_visor_device(struct visor_device *dev)
1020{
1021 list_del(&dev->list_all);
1022 unregister_devmajorminor_attributes(dev);
3703987c
EA
1023 put_device(&dev->device);
1024 device_unregister(&dev->device);
1025}
1026
3703987c
EA
1027static int
1028get_vbus_header_info(struct visorchannel *chan,
1029 struct spar_vbus_headerinfo *hdr_info)
1030{
1031 int rc = -1;
1032
1033 if (!SPAR_VBUS_CHANNEL_OK_CLIENT(visorchannel_get_header(chan)))
1034 goto away;
1035 if (visorchannel_read(chan, sizeof(struct channel_header), hdr_info,
1036 sizeof(*hdr_info)) < 0) {
1037 goto away;
1038 }
1039 if (hdr_info->struct_bytes < sizeof(struct spar_vbus_headerinfo))
1040 goto away;
1041 if (hdr_info->device_info_struct_bytes <
1042 sizeof(struct ultra_vbus_deviceinfo)) {
1043 goto away;
1044 }
1045 rc = 0;
1046away:
1047 return rc;
1048}
1049
1050/* Write the contents of <info> to the struct
1051 * spar_vbus_channel_protocol.chp_info. */
1052
1053static int
1054write_vbus_chp_info(struct visorchannel *chan,
1055 struct spar_vbus_headerinfo *hdr_info,
1056 struct ultra_vbus_deviceinfo *info)
1057{
1058 int off = sizeof(struct channel_header) + hdr_info->chp_info_offset;
1059
1060 if (hdr_info->chp_info_offset == 0)
1061 return -1;
1062
1063 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
1064 return -1;
1065 return 0;
1066}
1067
1068/* Write the contents of <info> to the struct
1069 * spar_vbus_channel_protocol.bus_info. */
1070
1071static int
1072write_vbus_bus_info(struct visorchannel *chan,
1073 struct spar_vbus_headerinfo *hdr_info,
1074 struct ultra_vbus_deviceinfo *info)
1075{
1076 int off = sizeof(struct channel_header) + hdr_info->bus_info_offset;
1077
1078 if (hdr_info->bus_info_offset == 0)
1079 return -1;
1080
1081 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
1082 return -1;
1083 return 0;
1084}
1085
1086/* Write the contents of <info> to the
1087 * struct spar_vbus_channel_protocol.dev_info[<devix>].
1088 */
1089static int
1090write_vbus_dev_info(struct visorchannel *chan,
1091 struct spar_vbus_headerinfo *hdr_info,
1092 struct ultra_vbus_deviceinfo *info, int devix)
1093{
1094 int off =
1095 (sizeof(struct channel_header) + hdr_info->dev_info_offset) +
1096 (hdr_info->device_info_struct_bytes * devix);
1097
1098 if (hdr_info->dev_info_offset == 0)
1099 return -1;
1100
1101 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
1102 return -1;
1103 return 0;
1104}
1105
1106/* For a child device just created on a client bus, fill in
1107 * information about the driver that is controlling this device into
1108 * the the appropriate slot within the vbus channel of the bus
1109 * instance.
1110 */
1111static void
1112fix_vbus_dev_info(struct visor_device *visordev)
1113{
1114 int i;
d32517e3 1115 struct visor_device *bdev;
3703987c
EA
1116 struct visor_driver *visordrv;
1117 int bus_no = visordev->chipset_bus_no;
1118 int dev_no = visordev->chipset_dev_no;
1119 struct ultra_vbus_deviceinfo dev_info;
1120 const char *chan_type_name = NULL;
7726f813 1121 struct spar_vbus_headerinfo *hdr_info;
3703987c
EA
1122
1123 if (!visordev->device.driver)
1124 return;
1125
7726f813 1126 hdr_info = (struct spar_vbus_headerinfo *)visordev->vbus_hdr_info;
d32517e3
DZ
1127 if (!hdr_info)
1128 return;
7726f813 1129
d32517e3
DZ
1130 bdev = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
1131 if (!bdev)
1132 return;
3703987c 1133
d32517e3 1134 visordrv = to_visor_driver(visordev->device.driver);
3703987c
EA
1135
1136 /* Within the list of device types (by GUID) that the driver
1137 * says it supports, find out which one of those types matches
1138 * the type of this device, so that we can include the device
1139 * type name
1140 */
1141 for (i = 0; visordrv->channel_types[i].name; i++) {
d5b3f1dc
EA
1142 if (memcmp(&visordrv->channel_types[i].guid,
1143 &visordev->channel_type_guid,
1144 sizeof(visordrv->channel_types[i].guid)) == 0) {
3703987c
EA
1145 chan_type_name = visordrv->channel_types[i].name;
1146 break;
1147 }
1148 }
1149
1150 bus_device_info_init(&dev_info, chan_type_name,
1151 visordrv->name, visordrv->version,
1152 visordrv->vertag);
d32517e3 1153 write_vbus_dev_info(bdev->visorchannel, hdr_info, &dev_info, dev_no);
3703987c
EA
1154
1155 /* Re-write bus+chipset info, because it is possible that this
1156 * was previously written by our evil counterpart, virtpci.
1157 */
d32517e3
DZ
1158 write_vbus_chp_info(bdev->visorchannel, hdr_info, &chipset_driverinfo);
1159 write_vbus_bus_info(bdev->visorchannel, hdr_info,
1160 &clientbus_driverinfo);
3703987c
EA
1161}
1162
1163/** Create a device instance for the visor bus itself.
1164 */
d32517e3
DZ
1165static int
1166create_bus_instance(struct visor_device *dev)
3703987c 1167{
d32517e3
DZ
1168 int rc;
1169 int id = dev->chipset_bus_no;
7726f813 1170 struct spar_vbus_headerinfo *hdr_info;
3703987c
EA
1171
1172 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
7726f813
DZ
1173
1174 hdr_info = kzalloc(sizeof(*hdr_info), GFP_KERNEL);
1175 if (!hdr_info) {
d32517e3
DZ
1176 rc = -1;
1177 goto away;
7726f813
DZ
1178 }
1179
343506bf
DZ
1180 dev_set_name(&dev->device, "visorbus%d", id);
1181 dev->device.bus = &visorbus_type;
1182 dev->device.groups = visorbus_groups;
1183 dev->device.release = visorbus_release_busdevice;
d32517e3 1184
343506bf 1185 if (device_register(&dev->device) < 0) {
3703987c
EA
1186 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, id,
1187 POSTCODE_SEVERITY_ERR);
d32517e3
DZ
1188 rc = -1;
1189 goto away_mem;
3703987c 1190 }
d32517e3 1191
ee983d90
DZ
1192 if (get_vbus_header_info(dev->visorchannel, hdr_info) >= 0) {
1193 dev->vbus_hdr_info = (void *)hdr_info;
1194 write_vbus_chp_info(dev->visorchannel, hdr_info,
1195 &chipset_driverinfo);
1196 write_vbus_bus_info(dev->visorchannel, hdr_info,
1197 &clientbus_driverinfo);
b32c4997 1198 } else {
ee983d90 1199 kfree(hdr_info);
3703987c 1200 }
3703987c 1201 bus_count++;
343506bf 1202 list_add_tail(&dev->list_all, &list_all_bus_instances);
343506bf 1203 dev_set_drvdata(&dev->device, dev);
d32517e3 1204 return 0;
7726f813 1205
7726f813 1206away_mem:
d32517e3 1207 kfree(hdr_info);
3703987c
EA
1208away:
1209 return rc;
1210}
1211
1212/** Remove a device instance for the visor bus itself.
1213 */
1214static void
343506bf 1215remove_bus_instance(struct visor_device *dev)
3703987c
EA
1216{
1217 /* Note that this will result in the release method for
343506bf 1218 * dev->dev being called, which will call
3703987c
EA
1219 * visorbus_release_busdevice(). This has something to do with
1220 * the put_device() done in device_unregister(), but I have never
1221 * successfully been able to trace thru the code to see where/how
1222 * release() gets called. But I know it does.
1223 */
3703987c 1224 bus_count--;
343506bf
DZ
1225 if (dev->visorchannel) {
1226 visorchannel_destroy(dev->visorchannel);
1227 dev->visorchannel = NULL;
3703987c 1228 }
343506bf
DZ
1229 kfree(dev->vbus_hdr_info);
1230 list_del(&dev->list_all);
1231 device_unregister(&dev->device);
3703987c
EA
1232}
1233
1234/** Create and register the one-and-only one instance of
1235 * the visor bus type (visorbus_type).
1236 */
1237static int
1238create_bus_type(void)
1239{
1240 int rc = 0;
1241
3703987c 1242 rc = bus_register(&visorbus_type);
3703987c
EA
1243 return rc;
1244}
1245
1246/** Remove the one-and-only one instance of the visor bus type (visorbus_type).
1247 */
1248static void
1249remove_bus_type(void)
1250{
3703987c
EA
1251 bus_unregister(&visorbus_type);
1252}
1253
1254/** Remove all child visor bus device instances.
1255 */
1256static void
1257remove_all_visor_devices(void)
1258{
1259 struct list_head *listentry, *listtmp;
1260
1261 list_for_each_safe(listentry, listtmp, &list_all_device_instances) {
1262 struct visor_device *dev = list_entry(listentry,
1263 struct visor_device,
1264 list_all);
1265 remove_visor_device(dev);
1266 }
1267}
1268
3703987c 1269static void
d32517e3 1270chipset_bus_create(struct visor_device *dev)
3703987c 1271{
d32517e3
DZ
1272 int rc;
1273 u32 bus_no = dev->chipset_bus_no;
3703987c
EA
1274
1275 POSTCODE_LINUX_3(BUS_CREATE_ENTRY_PC, bus_no, POSTCODE_SEVERITY_INFO);
d32517e3 1276 rc = create_bus_instance(dev);
3703987c 1277 POSTCODE_LINUX_3(BUS_CREATE_EXIT_PC, bus_no, POSTCODE_SEVERITY_INFO);
d32517e3
DZ
1278
1279 if (rc < 0)
3703987c
EA
1280 POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, bus_no,
1281 POSTCODE_SEVERITY_ERR);
d32517e3
DZ
1282 else
1283 POSTCODE_LINUX_3(CHIPSET_INIT_SUCCESS_PC, bus_no,
1284 POSTCODE_SEVERITY_INFO);
1285
3703987c 1286 if (chipset_responders.bus_create)
d32517e3 1287 (*chipset_responders.bus_create) (dev, rc);
3703987c
EA
1288}
1289
1290static void
d32517e3 1291chipset_bus_destroy(struct visor_device *dev)
3703987c 1292{
343506bf 1293 remove_bus_instance(dev);
3703987c 1294 if (chipset_responders.bus_destroy)
d32517e3 1295 (*chipset_responders.bus_destroy)(dev, 0);
3703987c
EA
1296}
1297
1298static void
a298bc0b 1299chipset_device_create(struct visor_device *dev_info)
3703987c 1300{
3703987c 1301 int rc = -1;
a298bc0b
DZ
1302 u32 bus_no = dev_info->chipset_bus_no;
1303 u32 dev_no = dev_info->chipset_dev_no;
3703987c
EA
1304
1305 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, dev_no, bus_no,
1306 POSTCODE_SEVERITY_INFO);
1307
a298bc0b 1308 rc = create_visor_device(dev_info);
3703987c
EA
1309 if (rc < 0) {
1310 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
1311 POSTCODE_SEVERITY_ERR);
d32517e3
DZ
1312 if (chipset_responders.device_create)
1313 (*chipset_responders.device_create)(dev_info, rc);
3703987c 1314 }
d32517e3 1315
3703987c
EA
1316 POSTCODE_LINUX_4(DEVICE_CREATE_SUCCESS_PC, dev_no, bus_no,
1317 POSTCODE_SEVERITY_INFO);
3703987c
EA
1318}
1319
1320static void
a298bc0b 1321chipset_device_destroy(struct visor_device *dev_info)
3703987c 1322{
a298bc0b 1323 remove_visor_device(dev_info);
3703987c
EA
1324
1325 if (chipset_responders.device_destroy)
a298bc0b 1326 (*chipset_responders.device_destroy) (dev_info, 0);
3703987c
EA
1327}
1328
1329/* This is the callback function specified for a function driver, to
1330 * be called when a pending "pause device" operation has been
1331 * completed.
1332 */
1333static void
a298bc0b 1334pause_state_change_complete(struct visor_device *dev, int status)
3703987c
EA
1335{
1336 if (!dev->pausing)
1337 return;
1338
779d0752 1339 dev->pausing = false;
3703987c
EA
1340 if (!chipset_responders.device_pause) /* this can never happen! */
1341 return;
1342
1343 /* Notify the chipset driver that the pause is complete, which
1344 * will presumably want to send some sort of response to the
1345 * initiator. */
a298bc0b 1346 (*chipset_responders.device_pause) (dev, status);
3703987c
EA
1347}
1348
1349/* This is the callback function specified for a function driver, to
1350 * be called when a pending "resume device" operation has been
1351 * completed.
1352 */
1353static void
a298bc0b 1354resume_state_change_complete(struct visor_device *dev, int status)
3703987c
EA
1355{
1356 if (!dev->resuming)
1357 return;
1358
779d0752 1359 dev->resuming = false;
3703987c
EA
1360 if (!chipset_responders.device_resume) /* this can never happen! */
1361 return;
1362
1363 /* Notify the chipset driver that the resume is complete,
1364 * which will presumably want to send some sort of response to
1365 * the initiator. */
a298bc0b 1366 (*chipset_responders.device_resume) (dev, status);
3703987c
EA
1367}
1368
1369/* Tell the subordinate function driver for a specific device to pause
1370 * or resume that device. Result is returned asynchronously via a
1371 * callback function.
1372 */
1373static void
a298bc0b 1374initiate_chipset_device_pause_resume(struct visor_device *dev, bool is_pause)
3703987c 1375{
3703987c
EA
1376 int rc = -1, x;
1377 struct visor_driver *drv = NULL;
a298bc0b 1378 void (*notify_func)(struct visor_device *dev, int response) = NULL;
3703987c
EA
1379
1380 if (is_pause)
1381 notify_func = chipset_responders.device_pause;
1382 else
1383 notify_func = chipset_responders.device_resume;
1384 if (!notify_func)
1385 goto away;
1386
3703987c
EA
1387 drv = to_visor_driver(dev->device.driver);
1388 if (!drv)
1389 goto away;
1390
1391 if (dev->pausing || dev->resuming)
1392 goto away;
1393
1394 /* Note that even though both drv->pause() and drv->resume
1395 * specify a callback function, it is NOT necessary for us to
1396 * increment our local module usage count. Reason is, there
1397 * is already a linkage dependency between child function
1398 * drivers and visorbus, so it is already IMPOSSIBLE to unload
1399 * visorbus while child function drivers are still running.
1400 */
1401 if (is_pause) {
1402 if (!drv->pause)
1403 goto away;
1404
779d0752 1405 dev->pausing = true;
a298bc0b 1406 x = drv->pause(dev, pause_state_change_complete);
3703987c
EA
1407 } else {
1408 /* This should be done at BUS resume time, but an
1409 * existing problem prevents us from ever getting a bus
1410 * resume... This hack would fail to work should we
1411 * ever have a bus that contains NO devices, since we
1412 * would never even get here in that case. */
1413 fix_vbus_dev_info(dev);
1414 if (!drv->resume)
1415 goto away;
1416
779d0752 1417 dev->resuming = true;
a298bc0b 1418 x = drv->resume(dev, resume_state_change_complete);
3703987c
EA
1419 }
1420 if (x < 0) {
1421 if (is_pause)
779d0752 1422 dev->pausing = false;
3703987c 1423 else
779d0752 1424 dev->resuming = false;
3703987c
EA
1425 goto away;
1426 }
1427 rc = 0;
1428away:
1429 if (rc < 0) {
1430 if (notify_func)
a298bc0b 1431 (*notify_func)(dev, rc);
3703987c
EA
1432 }
1433}
1434
1435static void
a298bc0b 1436chipset_device_pause(struct visor_device *dev_info)
3703987c 1437{
b4b598fd 1438 initiate_chipset_device_pause_resume(dev_info, true);
3703987c
EA
1439}
1440
1441static void
a298bc0b 1442chipset_device_resume(struct visor_device *dev_info)
3703987c 1443{
b4b598fd 1444 initiate_chipset_device_pause_resume(dev_info, false);
3703987c
EA
1445}
1446
1447struct channel_size_info {
1448 uuid_le guid;
1449 unsigned long min_size;
1450 unsigned long max_size;
1451};
1452
55c67dca 1453int
3703987c
EA
1454visorbus_init(void)
1455{
1456 int rc = 0;
1457
1458 POSTCODE_LINUX_3(DRIVER_ENTRY_PC, rc, POSTCODE_SEVERITY_INFO);
1459 bus_device_info_init(&clientbus_driverinfo,
46168810 1460 "clientbus", "visorbus",
3703987c
EA
1461 VERSION, NULL);
1462
3703987c
EA
1463 rc = create_bus_type();
1464 if (rc < 0) {
1465 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC, DIAG_SEVERITY_ERR);
1466 goto away;
1467 }
1468
1469 periodic_dev_workqueue = create_singlethread_workqueue("visorbus_dev");
1470 if (!periodic_dev_workqueue) {
1471 POSTCODE_LINUX_2(CREATE_WORKQUEUE_PC, DIAG_SEVERITY_ERR);
1472 rc = -ENOMEM;
1473 goto away;
1474 }
1475
1476 /* This enables us to receive notifications when devices appear for
1477 * which this service partition is to be a server for.
1478 */
4da3336c
DK
1479 visorchipset_register_busdev(&chipset_notifiers,
1480 &chipset_responders,
1481 &chipset_driverinfo);
3703987c
EA
1482
1483 rc = 0;
1484
1485away:
1486 if (rc)
1487 POSTCODE_LINUX_3(CHIPSET_INIT_FAILURE_PC, rc,
1488 POSTCODE_SEVERITY_ERR);
1489 return rc;
1490}
1491
c79b28f7 1492void
3703987c
EA
1493visorbus_exit(void)
1494{
1495 struct list_head *listentry, *listtmp;
1496
4da3336c 1497 visorchipset_register_busdev(NULL, NULL, NULL);
3703987c
EA
1498 remove_all_visor_devices();
1499
1500 flush_workqueue(periodic_dev_workqueue); /* better not be any work! */
1501 destroy_workqueue(periodic_dev_workqueue);
1502 periodic_dev_workqueue = NULL;
1503
1504 if (periodic_test_workqueue) {
1505 cancel_delayed_work(&periodic_work);
1506 flush_workqueue(periodic_test_workqueue);
1507 destroy_workqueue(periodic_test_workqueue);
1508 periodic_test_workqueue = NULL;
1509 }
1510
1511 list_for_each_safe(listentry, listtmp, &list_all_bus_instances) {
343506bf 1512 struct visor_device *dev = list_entry(listentry,
3703987c 1513 struct
343506bf 1514 visor_device,
3703987c 1515 list_all);
343506bf 1516 remove_bus_instance(dev);
3703987c
EA
1517 }
1518 remove_bus_type();
1519}
1520
1521module_param_named(debug, visorbus_debug, int, S_IRUGO);
1522MODULE_PARM_DESC(visorbus_debug, "1 to debug");
3703987c
EA
1523
1524module_param_named(forcematch, visorbus_forcematch, int, S_IRUGO);
1525MODULE_PARM_DESC(visorbus_forcematch,
1526 "1 to force a successful dev <--> drv match");
3703987c
EA
1527
1528module_param_named(forcenomatch, visorbus_forcenomatch, int, S_IRUGO);
1529MODULE_PARM_DESC(visorbus_forcenomatch,
1530 "1 to force an UNsuccessful dev <--> drv match");
3703987c 1531
3703987c
EA
1532module_param_named(debugref, visorbus_debugref, int, S_IRUGO);
1533MODULE_PARM_DESC(visorbus_debugref, "1 to debug reference counting");
3703987c 1534
3703987c
EA
1535MODULE_AUTHOR("Unisys");
1536MODULE_LICENSE("GPL");
1537MODULE_DESCRIPTION("Supervisor bus driver for service partition: ver " VERSION);
1538MODULE_VERSION(VERSION);
This page took 0.110208 seconds and 5 git commands to generate.