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