blob: 6a12829151e0673026b383d92f6800199bd38074 [file] [log] [blame]
Erik Arfvidson37039872015-05-05 18:36:00 -04001/* visorbus_main.c
2 *
Benjamin Romer6f14cc12015-07-16 12:40:48 -04003 * Copyright � 2010 - 2015 UNISYS CORPORATION
Erik Arfvidson37039872015-05-05 18:36:00 -04004 * All rights reserved.
5 *
Benjamin Romer6f14cc12015-07-16 12:40:48 -04006 * 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.
Erik Arfvidson37039872015-05-05 18:36:00 -04009 *
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
Prarit Bhargava4b780002015-05-05 18:36:10 -040019#include "visorbus.h"
Prarit Bhargavac79b28f2015-05-05 18:36:15 -040020#include "visorbus_private.h"
Prarit Bhargava4b780002015-05-05 18:36:10 -040021#include "version.h"
Erik Arfvidson37039872015-05-05 18:36:00 -040022#include "periodic_work.h"
23#include "vbuschannel.h"
24#include "guestlinuxdebug.h"
Jes Sorensen0f41c722015-05-13 13:22:00 -040025#include "vmcallinterface.h"
Prarit Bhargava4b780002015-05-05 18:36:10 -040026
Prarit Bhargavac79b28f2015-05-05 18:36:15 -040027#define MYDRVNAME "visorbus"
28
Prarit Bhargava4b780002015-05-05 18:36:10 -040029/* module parameters */
Drew Fustinif6758e72015-05-28 21:04:23 -050030static int visorbus_debug;
31static int visorbus_forcematch;
32static int visorbus_forcenomatch;
Drew Fustinif6758e72015-05-28 21:04:23 -050033static int visorbus_debugref;
Prarit Bhargava4b780002015-05-05 18:36:10 -040034#define SERIALLOOPBACKCHANADDR (100 * 1024 * 1024)
35
Erik Arfvidson37039872015-05-05 18:36:00 -040036#define CURRENT_FILE_PC VISOR_BUS_PC_visorbus_main_c
37#define POLLJIFFIES_TESTWORK 100
38#define POLLJIFFIES_NORMALCHANNEL 10
39
Benjamin Romer6155a3c2015-09-04 12:01:32 -040040static int busreg_rc = -ENODEV; /* stores the result from bus registration */
41
Erik Arfvidson37039872015-05-05 18:36:00 -040042static int visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env);
43static int visorbus_match(struct device *xdev, struct device_driver *xdrv);
44static void fix_vbus_dev_info(struct visor_device *visordev);
45
Don Zickus68b04f12015-05-13 13:22:15 -040046/* BUS type attributes
47 *
48 * define & implement display of bus attributes under
49 * /sys/bus/visorbus.
50 *
51 */
52
53static ssize_t version_show(struct bus_type *bus, char *buf)
54{
55 return snprintf(buf, PAGE_SIZE, "%s\n", VERSION);
56}
57
58static BUS_ATTR_RO(version);
59
60static struct attribute *visorbus_bus_attrs[] = {
61 &bus_attr_version.attr,
62 NULL,
63};
64
65static const struct attribute_group visorbus_bus_group = {
66 .attrs = visorbus_bus_attrs,
67};
68
Drew Fustinif6758e72015-05-28 21:04:23 -050069static const struct attribute_group *visorbus_bus_groups[] = {
Don Zickus68b04f12015-05-13 13:22:15 -040070 &visorbus_bus_group,
71 NULL,
72};
73
Prarit Bhargava59fd2c82015-07-24 12:06:53 -040074/*
75 * DEVICE type attributes
76 *
77 * The modalias file will contain the guid of the device.
78 */
79static 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}
89static DEVICE_ATTR_RO(modalias);
90
91static 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 */
97static const struct attribute_group visorbus_dev_group = {
98 .attrs = visorbus_dev_attrs,
99};
100
Michał Kępieńcf7281c2015-08-11 14:07:00 +0200101static const struct attribute_group *visorbus_dev_groups[] = {
Prarit Bhargava59fd2c82015-07-24 12:06:53 -0400102 &visorbus_dev_group,
103 NULL,
104};
105
Erik Arfvidson37039872015-05-05 18:36:00 -0400106/** This describes the TYPE of bus.
107 * (Don't confuse this with an INSTANCE of the bus.)
108 */
Don Zickus1fb30162015-05-13 13:22:18 -0400109struct bus_type visorbus_type = {
Erik Arfvidson37039872015-05-05 18:36:00 -0400110 .name = "visorbus",
111 .match = visorbus_match,
112 .uevent = visorbus_uevent,
Prarit Bhargava59fd2c82015-07-24 12:06:53 -0400113 .dev_groups = visorbus_dev_groups,
Don Zickus68b04f12015-05-13 13:22:15 -0400114 .bus_groups = visorbus_bus_groups,
Erik Arfvidson37039872015-05-05 18:36:00 -0400115};
116
117static 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 */
124static struct workqueue_struct *periodic_test_workqueue;
125static struct workqueue_struct *periodic_dev_workqueue;
126static long long bus_count; /** number of bus instances */
Erik Arfvidson37039872015-05-05 18:36:00 -0400127 /** ever-increasing */
128
Don Zickusd32517e2015-06-04 09:22:41 -0400129static void chipset_bus_create(struct visor_device *bus_info);
130static void chipset_bus_destroy(struct visor_device *bus_info);
Don Zickusa298bc02015-06-04 09:22:42 -0400131static void chipset_device_create(struct visor_device *dev_info);
132static void chipset_device_destroy(struct visor_device *dev_info);
133static void chipset_device_pause(struct visor_device *dev_info);
134static void chipset_device_resume(struct visor_device *dev_info);
Erik Arfvidson37039872015-05-05 18:36:00 -0400135
136/** These functions are implemented herein, and are called by the chipset
137 * driver to notify us about specific events.
138 */
139static 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 */
151static struct visorchipset_busdev_responders chipset_responders;
152
153/* filled in with info about parent chipset driver when we register with it */
154static struct ultra_vbus_deviceinfo chipset_driverinfo;
155/* filled in with info about this driver, wrt it servicing client busses */
156static struct ultra_vbus_deviceinfo clientbus_driverinfo;
157
Don Zickus343506b2015-06-04 09:22:37 -0400158/** list of visor_device structs, linked via .list_all */
Erik Arfvidson37039872015-05-05 18:36:00 -0400159static LIST_HEAD(list_all_bus_instances);
160/** list of visor_device structs, linked via .list_all */
161static LIST_HEAD(list_all_device_instances);
162
163static int
164visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env)
165{
Prarit Bhargava59fd2c82015-07-24 12:06:53 -0400166 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))
Erik Arfvidson37039872015-05-05 18:36:00 -0400173 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 */
181static int
182visorbus_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 }
211away:
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 */
219static void
220visorbus_release_busdevice(struct device *xdev)
221{
Don Zickus343506b2015-06-04 09:22:37 -0400222 struct visor_device *dev = dev_get_drvdata(xdev);
Erik Arfvidson37039872015-05-05 18:36:00 -0400223
Don Zickus343506b2015-06-04 09:22:37 -0400224 kfree(dev);
Erik Arfvidson37039872015-05-05 18:36:00 -0400225}
226
227/** This is called when device_unregister() is called for each child
228 * device instance.
229 */
230static void
231visorbus_release_device(struct device *xdev)
232{
233 struct visor_device *dev = to_visor_device(xdev);
234
235 if (dev->periodic_work) {
236 visor_periodic_work_destroy(dev->periodic_work);
237 dev->periodic_work = NULL;
238 }
239 if (dev->visorchannel) {
240 visorchannel_destroy(dev->visorchannel);
241 dev->visorchannel = NULL;
242 }
243 kfree(dev);
244}
245
Prarit Bhargava327104572015-05-05 18:36:07 -0400246/* Implement publishing of device node attributes under:
247 *
248 * /sys/bus/visorbus<x>/dev<y>/devmajorminor
249 *
250 */
251
252#define to_devmajorminor_attr(_attr) \
253 container_of(_attr, struct devmajorminor_attribute, attr)
254#define to_visor_device_from_kobjdevmajorminor(obj) \
255 container_of(obj, struct visor_device, kobjdevmajorminor)
256
257struct devmajorminor_attribute {
258 struct attribute attr;
259 int slot;
Christophe JAILLET216c3e22015-07-13 20:52:26 +0200260 ssize_t (*show)(struct visor_device *, int slot, char *buf);
261 ssize_t (*store)(struct visor_device *, int slot, const char *buf,
262 size_t count);
Prarit Bhargava327104572015-05-05 18:36:07 -0400263};
264
265static ssize_t DEVMAJORMINOR_ATTR(struct visor_device *dev, int slot, char *buf)
266{
267 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
268
269 if (slot < 0 || slot >= maxdevnodes)
270 return 0;
271 return snprintf(buf, PAGE_SIZE, "%d:%d\n",
272 dev->devnodes[slot].major, dev->devnodes[slot].minor);
273}
274
275static ssize_t
276devmajorminor_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
277{
278 struct devmajorminor_attribute *devmajorminor_attr =
279 to_devmajorminor_attr(attr);
280 struct visor_device *dev = to_visor_device_from_kobjdevmajorminor(kobj);
281 ssize_t ret = 0;
282
283 if (devmajorminor_attr->show)
284 ret = devmajorminor_attr->show(dev,
285 devmajorminor_attr->slot, buf);
286 return ret;
287}
288
289static ssize_t
290devmajorminor_attr_store(struct kobject *kobj,
291 struct attribute *attr, const char *buf, size_t count)
292{
293 struct devmajorminor_attribute *devmajorminor_attr =
294 to_devmajorminor_attr(attr);
295 struct visor_device *dev = to_visor_device_from_kobjdevmajorminor(kobj);
296 ssize_t ret = 0;
297
298 if (devmajorminor_attr->store)
299 ret = devmajorminor_attr->store(dev,
300 devmajorminor_attr->slot,
301 buf, count);
302 return ret;
303}
304
305static int register_devmajorminor_attributes(struct visor_device *dev);
306
Drew Fustinif6758e72015-05-28 21:04:23 -0500307static int
Prarit Bhargava327104572015-05-05 18:36:07 -0400308devmajorminor_create_file(struct visor_device *dev, const char *name,
309 int major, int minor)
310{
311 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
312 struct devmajorminor_attribute *myattr = NULL;
313 int x = -1, rc = 0, slot = -1;
314
315 register_devmajorminor_attributes(dev);
316 for (slot = 0; slot < maxdevnodes; slot++)
317 if (!dev->devnodes[slot].attr)
318 break;
319 if (slot == maxdevnodes) {
320 rc = -ENOMEM;
321 goto away;
322 }
Christophe JAILLET0aa5ae12015-07-13 20:51:32 +0200323 myattr = kzalloc(sizeof(*myattr), GFP_KERNEL);
Prarit Bhargava327104572015-05-05 18:36:07 -0400324 if (!myattr) {
325 rc = -ENOMEM;
326 goto away;
327 }
Prarit Bhargava327104572015-05-05 18:36:07 -0400328 myattr->show = DEVMAJORMINOR_ATTR;
329 myattr->store = NULL;
330 myattr->slot = slot;
331 myattr->attr.name = name;
332 myattr->attr.mode = S_IRUGO;
333 dev->devnodes[slot].attr = myattr;
334 dev->devnodes[slot].major = major;
335 dev->devnodes[slot].minor = minor;
336 x = sysfs_create_file(&dev->kobjdevmajorminor, &myattr->attr);
337 if (x < 0) {
338 rc = x;
339 goto away;
340 }
341 kobject_uevent(&dev->device.kobj, KOBJ_ONLINE);
342away:
343 if (rc < 0) {
344 kfree(myattr);
345 myattr = NULL;
346 dev->devnodes[slot].attr = NULL;
347 }
348 return rc;
349}
350
Drew Fustinif6758e72015-05-28 21:04:23 -0500351static void
Prarit Bhargava327104572015-05-05 18:36:07 -0400352devmajorminor_remove_file(struct visor_device *dev, int slot)
353{
354 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
355 struct devmajorminor_attribute *myattr = NULL;
356
357 if (slot < 0 || slot >= maxdevnodes)
358 return;
359 myattr = (struct devmajorminor_attribute *)(dev->devnodes[slot].attr);
Don Zickus04dacac2015-06-04 09:22:48 -0400360 if (!myattr)
Prarit Bhargava327104572015-05-05 18:36:07 -0400361 return;
362 sysfs_remove_file(&dev->kobjdevmajorminor, &myattr->attr);
363 kobject_uevent(&dev->device.kobj, KOBJ_OFFLINE);
364 dev->devnodes[slot].attr = NULL;
365 kfree(myattr);
366}
367
Drew Fustinif6758e72015-05-28 21:04:23 -0500368static void
Prarit Bhargava327104572015-05-05 18:36:07 -0400369devmajorminor_remove_all_files(struct visor_device *dev)
370{
371 int i = 0;
372 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
373
374 for (i = 0; i < maxdevnodes; i++)
375 devmajorminor_remove_file(dev, i);
376}
377
378static const struct sysfs_ops devmajorminor_sysfs_ops = {
379 .show = devmajorminor_attr_show,
380 .store = devmajorminor_attr_store,
381};
382
383static struct kobj_type devmajorminor_kobj_type = {
384 .sysfs_ops = &devmajorminor_sysfs_ops
385};
386
387static int
388register_devmajorminor_attributes(struct visor_device *dev)
389{
390 int rc = 0, x = 0;
391
392 if (dev->kobjdevmajorminor.parent)
393 goto away; /* already registered */
394 x = kobject_init_and_add(&dev->kobjdevmajorminor,
395 &devmajorminor_kobj_type, &dev->device.kobj,
396 "devmajorminor");
397 if (x < 0) {
398 rc = x;
399 goto away;
400 }
401
402 kobject_uevent(&dev->kobjdevmajorminor, KOBJ_ADD);
403
404away:
405 return rc;
406}
407
Drew Fustinif6758e72015-05-28 21:04:23 -0500408static void
Prarit Bhargava327104572015-05-05 18:36:07 -0400409unregister_devmajorminor_attributes(struct visor_device *dev)
410{
411 if (!dev->kobjdevmajorminor.parent)
412 return; /* already unregistered */
413 devmajorminor_remove_all_files(dev);
414
415 kobject_del(&dev->kobjdevmajorminor);
416 kobject_put(&dev->kobjdevmajorminor);
417 dev->kobjdevmajorminor.parent = NULL;
418}
419
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400420/* begin implementation of specific channel attributes to appear under
421* /sys/bus/visorbus<x>/dev<y>/channel
422*/
Don Zickus79573162015-05-13 13:22:17 -0400423static ssize_t physaddr_show(struct device *dev, struct device_attribute *attr,
424 char *buf)
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400425{
Don Zickus79573162015-05-13 13:22:17 -0400426 struct visor_device *vdev = to_visor_device(dev);
427
428 if (!vdev->visorchannel)
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400429 return 0;
430 return snprintf(buf, PAGE_SIZE, "0x%Lx\n",
Don Zickus79573162015-05-13 13:22:17 -0400431 visorchannel_get_physaddr(vdev->visorchannel));
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400432}
433
Don Zickus79573162015-05-13 13:22:17 -0400434static ssize_t nbytes_show(struct device *dev, struct device_attribute *attr,
435 char *buf)
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400436{
Don Zickus79573162015-05-13 13:22:17 -0400437 struct visor_device *vdev = to_visor_device(dev);
438
439 if (!vdev->visorchannel)
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400440 return 0;
441 return snprintf(buf, PAGE_SIZE, "0x%lx\n",
Don Zickus79573162015-05-13 13:22:17 -0400442 visorchannel_get_nbytes(vdev->visorchannel));
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400443}
444
Don Zickus79573162015-05-13 13:22:17 -0400445static ssize_t clientpartition_show(struct device *dev,
446 struct device_attribute *attr, char *buf)
447{
448 struct visor_device *vdev = to_visor_device(dev);
449
450 if (!vdev->visorchannel)
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400451 return 0;
452 return snprintf(buf, PAGE_SIZE, "0x%Lx\n",
Don Zickus79573162015-05-13 13:22:17 -0400453 visorchannel_get_clientpartition(vdev->visorchannel));
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400454}
455
Don Zickus79573162015-05-13 13:22:17 -0400456static ssize_t typeguid_show(struct device *dev, struct device_attribute *attr,
457 char *buf)
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400458{
Don Zickus79573162015-05-13 13:22:17 -0400459 struct visor_device *vdev = to_visor_device(dev);
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400460 char s[99];
461
Don Zickus79573162015-05-13 13:22:17 -0400462 if (!vdev->visorchannel)
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400463 return 0;
464 return snprintf(buf, PAGE_SIZE, "%s\n",
Don Zickus79573162015-05-13 13:22:17 -0400465 visorchannel_id(vdev->visorchannel, s));
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400466}
467
Don Zickus79573162015-05-13 13:22:17 -0400468static ssize_t zoneguid_show(struct device *dev, struct device_attribute *attr,
469 char *buf)
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400470{
Don Zickus79573162015-05-13 13:22:17 -0400471 struct visor_device *vdev = to_visor_device(dev);
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400472 char s[99];
473
Don Zickus79573162015-05-13 13:22:17 -0400474 if (!vdev->visorchannel)
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400475 return 0;
476 return snprintf(buf, PAGE_SIZE, "%s\n",
Don Zickus79573162015-05-13 13:22:17 -0400477 visorchannel_zoneid(vdev->visorchannel, s));
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400478}
479
Don Zickus79573162015-05-13 13:22:17 -0400480static ssize_t typename_show(struct device *dev, struct device_attribute *attr,
481 char *buf)
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400482{
Don Zickus79573162015-05-13 13:22:17 -0400483 struct visor_device *vdev = to_visor_device(dev);
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400484 int i = 0;
Don Zickus79573162015-05-13 13:22:17 -0400485 struct bus_type *xbus = dev->bus;
486 struct device_driver *xdrv = dev->driver;
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400487 struct visor_driver *drv = NULL;
488
Don Zickus79573162015-05-13 13:22:17 -0400489 if (!vdev->visorchannel || !xbus || !xdrv)
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400490 return 0;
Don Zickus79573162015-05-13 13:22:17 -0400491 i = xbus->match(dev, xdrv);
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400492 if (!i)
493 return 0;
494 drv = to_visor_driver(xdrv);
495 return snprintf(buf, PAGE_SIZE, "%s\n", drv->channel_types[i - 1].name);
496}
497
Don Zickus79573162015-05-13 13:22:17 -0400498static DEVICE_ATTR_RO(physaddr);
499static DEVICE_ATTR_RO(nbytes);
500static DEVICE_ATTR_RO(clientpartition);
501static DEVICE_ATTR_RO(typeguid);
502static DEVICE_ATTR_RO(zoneguid);
503static DEVICE_ATTR_RO(typename);
Don Zickus79573162015-05-13 13:22:17 -0400504
505static struct attribute *channel_attrs[] = {
506 &dev_attr_physaddr.attr,
507 &dev_attr_nbytes.attr,
508 &dev_attr_clientpartition.attr,
509 &dev_attr_typeguid.attr,
510 &dev_attr_zoneguid.attr,
511 &dev_attr_typename.attr,
Tim Sellfd012d02015-07-13 14:51:23 -0400512 NULL
Don Zickus79573162015-05-13 13:22:17 -0400513};
514
515static struct attribute_group channel_attr_grp = {
516 .name = "channel",
517 .attrs = channel_attrs,
518};
519
Prarit Bhargava59fd2c82015-07-24 12:06:53 -0400520static const struct attribute_group *visorbus_channel_groups[] = {
Don Zickus79573162015-05-13 13:22:17 -0400521 &channel_attr_grp,
522 NULL
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400523};
524
525/* end implementation of specific channel attributes */
526
Erik Arfvidson37039872015-05-05 18:36:00 -0400527/* BUS instance attributes
528 *
529 * define & implement display of bus attributes under
530 * /sys/bus/visorbus/busses/visorbus<n>.
531 *
532 * This is a bit hoaky because the kernel does not yet have the infrastructure
533 * to separate bus INSTANCE attributes from bus TYPE attributes...
534 * so we roll our own. See businst.c / businst.h.
535 *
536 */
537
Don Zickusd181dd02015-05-13 13:22:13 -0400538static ssize_t partition_handle_show(struct device *dev,
539 struct device_attribute *attr,
540 char *buf) {
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400541 struct visor_device *vdev = to_visor_device(dev);
542 u64 handle = visorchannel_get_clientpartition(vdev->visorchannel);
Erik Arfvidson37039872015-05-05 18:36:00 -0400543
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400544 return snprintf(buf, PAGE_SIZE, "0x%Lx\n", handle);
Erik Arfvidson37039872015-05-05 18:36:00 -0400545}
546
Don Zickusd181dd02015-05-13 13:22:13 -0400547static ssize_t partition_guid_show(struct device *dev,
548 struct device_attribute *attr,
549 char *buf) {
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400550 struct visor_device *vdev = to_visor_device(dev);
Erik Arfvidson37039872015-05-05 18:36:00 -0400551
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400552 return snprintf(buf, PAGE_SIZE, "{%pUb}\n", &vdev->partition_uuid);
Erik Arfvidson37039872015-05-05 18:36:00 -0400553}
554
Don Zickusd181dd02015-05-13 13:22:13 -0400555static ssize_t partition_name_show(struct device *dev,
556 struct device_attribute *attr,
557 char *buf) {
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400558 struct visor_device *vdev = to_visor_device(dev);
Erik Arfvidson37039872015-05-05 18:36:00 -0400559
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400560 return snprintf(buf, PAGE_SIZE, "%s\n", vdev->name);
Erik Arfvidson37039872015-05-05 18:36:00 -0400561}
562
Don Zickusd181dd02015-05-13 13:22:13 -0400563static ssize_t channel_addr_show(struct device *dev,
564 struct device_attribute *attr,
565 char *buf) {
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400566 struct visor_device *vdev = to_visor_device(dev);
567 u64 addr = visorchannel_get_physaddr(vdev->visorchannel);
Erik Arfvidson37039872015-05-05 18:36:00 -0400568
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400569 return snprintf(buf, PAGE_SIZE, "0x%Lx\n", addr);
Erik Arfvidson37039872015-05-05 18:36:00 -0400570}
571
Don Zickusd181dd02015-05-13 13:22:13 -0400572static ssize_t channel_bytes_show(struct device *dev,
573 struct device_attribute *attr,
574 char *buf) {
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400575 struct visor_device *vdev = to_visor_device(dev);
576 u64 nbytes = visorchannel_get_nbytes(vdev->visorchannel);
Erik Arfvidson37039872015-05-05 18:36:00 -0400577
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400578 return snprintf(buf, PAGE_SIZE, "0x%Lx\n", nbytes);
Erik Arfvidson37039872015-05-05 18:36:00 -0400579}
580
Don Zickusd181dd02015-05-13 13:22:13 -0400581static ssize_t channel_id_show(struct device *dev,
582 struct device_attribute *attr,
583 char *buf) {
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400584 struct visor_device *vdev = to_visor_device(dev);
Erik Arfvidson37039872015-05-05 18:36:00 -0400585 int len = 0;
586
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400587 if (vdev->visorchannel) {
588 visorchannel_id(vdev->visorchannel, buf);
Erik Arfvidson37039872015-05-05 18:36:00 -0400589 len = strlen(buf);
590 buf[len++] = '\n';
591 }
592 return len;
593}
594
Don Zickusd181dd02015-05-13 13:22:13 -0400595static ssize_t client_bus_info_show(struct device *dev,
596 struct device_attribute *attr,
597 char *buf) {
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400598 struct visor_device *vdev = to_visor_device(dev);
599 struct visorchannel *channel = vdev->visorchannel;
600
Erik Arfvidson37039872015-05-05 18:36:00 -0400601 int i, x, remain = PAGE_SIZE;
602 unsigned long off;
603 char *p = buf;
604 u8 *partition_name;
605 struct ultra_vbus_deviceinfo dev_info;
606
607 partition_name = "";
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400608 if (channel) {
609 if (vdev->name)
610 partition_name = vdev->name;
Erik Arfvidson37039872015-05-05 18:36:00 -0400611 x = snprintf(p, remain,
Don Zickus65bd6e42015-06-04 09:22:40 -0400612 "Client device / client driver info for %s partition (vbus #%d):\n",
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400613 partition_name, vdev->chipset_dev_no);
Erik Arfvidson37039872015-05-05 18:36:00 -0400614 p += x;
615 remain -= x;
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400616 x = visorchannel_read(channel,
Erik Arfvidson37039872015-05-05 18:36:00 -0400617 offsetof(struct
618 spar_vbus_channel_protocol,
619 chp_info),
620 &dev_info, sizeof(dev_info));
621 if (x >= 0) {
622 x = vbuschannel_devinfo_to_string(&dev_info, p,
623 remain, -1);
624 p += x;
625 remain -= x;
626 }
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400627 x = visorchannel_read(channel,
Erik Arfvidson37039872015-05-05 18:36:00 -0400628 offsetof(struct
629 spar_vbus_channel_protocol,
630 bus_info),
631 &dev_info, sizeof(dev_info));
632 if (x >= 0) {
633 x = vbuschannel_devinfo_to_string(&dev_info, p,
634 remain, -1);
635 p += x;
636 remain -= x;
637 }
638 off = offsetof(struct spar_vbus_channel_protocol, dev_info);
639 i = 0;
640 while (off + sizeof(dev_info) <=
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400641 visorchannel_get_nbytes(channel)) {
642 x = visorchannel_read(channel,
Erik Arfvidson37039872015-05-05 18:36:00 -0400643 off, &dev_info, sizeof(dev_info));
644 if (x >= 0) {
645 x = vbuschannel_devinfo_to_string
646 (&dev_info, p, remain, i);
647 p += x;
648 remain -= x;
649 }
650 off += sizeof(dev_info);
651 i++;
652 }
653 }
654 return PAGE_SIZE - remain;
655}
656
Don Zickusd181dd02015-05-13 13:22:13 -0400657static DEVICE_ATTR_RO(partition_handle);
658static DEVICE_ATTR_RO(partition_guid);
659static DEVICE_ATTR_RO(partition_name);
660static DEVICE_ATTR_RO(channel_addr);
661static DEVICE_ATTR_RO(channel_bytes);
662static DEVICE_ATTR_RO(channel_id);
663static DEVICE_ATTR_RO(client_bus_info);
Erik Arfvidson37039872015-05-05 18:36:00 -0400664
Don Zickusd181dd02015-05-13 13:22:13 -0400665static struct attribute *dev_attrs[] = {
666 &dev_attr_partition_handle.attr,
667 &dev_attr_partition_guid.attr,
668 &dev_attr_partition_name.attr,
669 &dev_attr_channel_addr.attr,
670 &dev_attr_channel_bytes.attr,
671 &dev_attr_channel_id.attr,
672 &dev_attr_client_bus_info.attr,
673 NULL
674};
Erik Arfvidson37039872015-05-05 18:36:00 -0400675
Don Zickusd181dd02015-05-13 13:22:13 -0400676static struct attribute_group dev_attr_grp = {
677 .attrs = dev_attrs,
678};
Erik Arfvidson37039872015-05-05 18:36:00 -0400679
Don Zickusd181dd02015-05-13 13:22:13 -0400680static const struct attribute_group *visorbus_groups[] = {
681 &dev_attr_grp,
682 NULL
683};
Erik Arfvidson37039872015-05-05 18:36:00 -0400684
685/* DRIVER attributes
686 *
687 * define & implement display of driver attributes under
688 * /sys/bus/visorbus/drivers/<drivername>.
689 *
690 */
691
692static ssize_t
693DRIVER_ATTR_version(struct device_driver *xdrv, char *buf)
694{
695 struct visor_driver *drv = to_visor_driver(xdrv);
696
697 return snprintf(buf, PAGE_SIZE, "%s\n", drv->version);
698}
699
700static int
701register_driver_attributes(struct visor_driver *drv)
702{
Erik Arfvidson37039872015-05-05 18:36:00 -0400703 struct driver_attribute version =
704 __ATTR(version, S_IRUGO, DRIVER_ATTR_version, NULL);
705 drv->version_attr = version;
Bhaktipriya Shridhar2cda64c2016-02-25 17:26:55 +0530706 return driver_create_file(&drv->driver, &drv->version_attr);
Erik Arfvidson37039872015-05-05 18:36:00 -0400707}
708
709static void
710unregister_driver_attributes(struct visor_driver *drv)
711{
712 driver_remove_file(&drv->driver, &drv->version_attr);
713}
714
Erik Arfvidson37039872015-05-05 18:36:00 -0400715static void
716dev_periodic_work(void *xdev)
717{
Shraddha Barke0df4e3e2015-08-10 13:30:35 +0530718 struct visor_device *dev = xdev;
Erik Arfvidson37039872015-05-05 18:36:00 -0400719 struct visor_driver *drv = to_visor_driver(dev->device.driver);
720
721 down(&dev->visordriver_callback_lock);
722 if (drv->channel_interrupt)
723 drv->channel_interrupt(dev);
724 up(&dev->visordriver_callback_lock);
725 if (!visor_periodic_work_nextperiod(dev->periodic_work))
726 put_device(&dev->device);
727}
728
729static void
730dev_start_periodic_work(struct visor_device *dev)
731{
732 if (dev->being_removed)
733 return;
734 /* now up by at least 2 */
735 get_device(&dev->device);
736 if (!visor_periodic_work_start(dev->periodic_work))
737 put_device(&dev->device);
738}
739
740static void
741dev_stop_periodic_work(struct visor_device *dev)
742{
743 if (visor_periodic_work_stop(dev->periodic_work))
744 put_device(&dev->device);
745}
746
747/** This is called automatically upon adding a visor_device (device_add), or
748 * adding a visor_driver (visorbus_register_visor_driver), but only after
749 * visorbus_match has returned 1 to indicate a successful match between
750 * driver and device.
751 */
752static int
753visordriver_probe_device(struct device *xdev)
754{
David Kershner40006222016-03-12 21:27:08 -0500755 int res;
Erik Arfvidson37039872015-05-05 18:36:00 -0400756 struct visor_driver *drv;
757 struct visor_device *dev;
758
759 drv = to_visor_driver(xdev->driver);
760 dev = to_visor_device(xdev);
David Kershner40006222016-03-12 21:27:08 -0500761
762 if (!drv->probe)
763 return -ENODEV;
764
Erik Arfvidson37039872015-05-05 18:36:00 -0400765 down(&dev->visordriver_callback_lock);
Prarit Bhargava779d0752015-05-05 18:37:01 -0400766 dev->being_removed = false;
Erik Arfvidson37039872015-05-05 18:36:00 -0400767
David Kershner40006222016-03-12 21:27:08 -0500768 res = drv->probe(dev);
769 if (res >= 0) {
770 /* success: reference kept via unmatched get_device() */
771 get_device(&dev->device);
772 fix_vbus_dev_info(dev);
773 }
774
Erik Arfvidson37039872015-05-05 18:36:00 -0400775 up(&dev->visordriver_callback_lock);
David Kershner40006222016-03-12 21:27:08 -0500776 return res;
Erik Arfvidson37039872015-05-05 18:36:00 -0400777}
778
779/** This is called when device_unregister() is called for each child device
780 * instance, to notify the appropriate visorbus_driver that the device is
781 * going away, and to decrease the reference count of the device.
782 */
783static int
784visordriver_remove_device(struct device *xdev)
785{
Erik Arfvidson37039872015-05-05 18:36:00 -0400786 struct visor_device *dev;
787 struct visor_driver *drv;
788
789 dev = to_visor_device(xdev);
790 drv = to_visor_driver(xdev->driver);
791 down(&dev->visordriver_callback_lock);
Prarit Bhargava779d0752015-05-05 18:37:01 -0400792 dev->being_removed = true;
Erik Arfvidson37039872015-05-05 18:36:00 -0400793 /*
794 * ensure that the dev->being_removed flag is set before we start the
795 * actual removal
796 */
797 wmb();
798 if (drv) {
799 if (drv->remove)
800 drv->remove(dev);
801 }
802 up(&dev->visordriver_callback_lock);
803 dev_stop_periodic_work(dev);
804 devmajorminor_remove_all_files(dev);
805
806 put_device(&dev->device);
807
Abdul Hussaindf7f46e2015-06-11 10:03:49 +0000808 return 0;
Erik Arfvidson37039872015-05-05 18:36:00 -0400809}
810
811/** A particular type of visor driver calls this function to register
812 * the driver. The caller MUST fill in the following fields within the
813 * #drv structure:
814 * name, version, owner, channel_types, probe, remove
815 *
816 * Here's how the whole Linux bus / driver / device model works.
817 *
818 * At system start-up, the visorbus kernel module is loaded, which registers
819 * visorbus_type as a bus type, using bus_register().
820 *
821 * All kernel modules that support particular device types on a
822 * visorbus bus are loaded. Each of these kernel modules calls
823 * visorbus_register_visor_driver() in their init functions, passing a
824 * visor_driver struct. visorbus_register_visor_driver() in turn calls
825 * register_driver(&visor_driver.driver). This .driver member is
826 * initialized with generic methods (like probe), whose sole responsibility
827 * is to act as a broker for the real methods, which are within the
828 * visor_driver struct. (This is the way the subclass behavior is
829 * implemented, since visor_driver is essentially a subclass of the
830 * generic driver.) Whenever a driver_register() happens, core bus code in
831 * the kernel does (see device_attach() in drivers/base/dd.c):
832 *
833 * for each dev associated with the bus (the bus that driver is on) that
834 * does not yet have a driver
835 * if bus.match(dev,newdriver) == yes_matched ** .match specified
836 * ** during bus_register().
837 * newdriver.probe(dev) ** for visor drivers, this will call
838 * ** the generic driver.probe implemented in visorbus.c,
839 * ** which in turn calls the probe specified within the
840 * ** struct visor_driver (which was specified by the
841 * ** actual device driver as part of
842 * ** visorbus_register_visor_driver()).
843 *
844 * The above dance also happens when a new device appears.
845 * So the question is, how are devices created within the system?
846 * Basically, just call device_add(dev). See pci_bus_add_devices().
847 * pci_scan_device() shows an example of how to build a device struct. It
848 * returns the newly-created struct to pci_scan_single_device(), who adds it
849 * to the list of devices at PCIBUS.devices. That list of devices is what
850 * is traversed by pci_bus_add_devices().
851 *
852 */
853int visorbus_register_visor_driver(struct visor_driver *drv)
854{
855 int rc = 0;
856
Benjamin Romer6155a3c2015-09-04 12:01:32 -0400857 if (busreg_rc < 0)
858 return -ENODEV; /*can't register on a nonexistent bus*/
859
Erik Arfvidson37039872015-05-05 18:36:00 -0400860 drv->driver.name = drv->name;
861 drv->driver.bus = &visorbus_type;
862 drv->driver.probe = visordriver_probe_device;
863 drv->driver.remove = visordriver_remove_device;
864 drv->driver.owner = drv->owner;
865
866 /* driver_register does this:
867 * bus_add_driver(drv)
868 * ->if (drv.bus) ** (bus_type) **
869 * driver_attach(drv)
870 * for each dev with bus type of drv.bus
871 * if (!dev.drv) ** no driver assigned yet **
872 * if (bus.match(dev,drv)) [visorbus_match]
873 * dev.drv = drv
874 * if (!drv.probe(dev)) [visordriver_probe_device]
875 * dev.drv = NULL
876 */
877
878 rc = driver_register(&drv->driver);
879 if (rc < 0)
880 return rc;
881 rc = register_driver_attributes(drv);
David Kershner418627d2015-09-04 12:01:31 -0400882 if (rc < 0)
883 driver_unregister(&drv->driver);
Erik Arfvidson37039872015-05-05 18:36:00 -0400884 return rc;
885}
886EXPORT_SYMBOL_GPL(visorbus_register_visor_driver);
887
888/** A particular type of visor driver calls this function to unregister
889 * the driver, i.e., within its module_exit function.
890 */
891void
892visorbus_unregister_visor_driver(struct visor_driver *drv)
893{
894 unregister_driver_attributes(drv);
895 driver_unregister(&drv->driver);
896}
897EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver);
898
899int
900visorbus_read_channel(struct visor_device *dev, unsigned long offset,
901 void *dest, unsigned long nbytes)
902{
903 return visorchannel_read(dev->visorchannel, offset, dest, nbytes);
904}
905EXPORT_SYMBOL_GPL(visorbus_read_channel);
906
907int
908visorbus_write_channel(struct visor_device *dev, unsigned long offset,
909 void *src, unsigned long nbytes)
910{
911 return visorchannel_write(dev->visorchannel, offset, src, nbytes);
912}
913EXPORT_SYMBOL_GPL(visorbus_write_channel);
914
915int
916visorbus_clear_channel(struct visor_device *dev, unsigned long offset, u8 ch,
917 unsigned long nbytes)
918{
919 return visorchannel_clear(dev->visorchannel, offset, ch, nbytes);
920}
921EXPORT_SYMBOL_GPL(visorbus_clear_channel);
922
923int
924visorbus_registerdevnode(struct visor_device *dev,
925 const char *name, int major, int minor)
926{
927 return devmajorminor_create_file(dev, name, major, minor);
928}
929EXPORT_SYMBOL_GPL(visorbus_registerdevnode);
930
931/** We don't really have a real interrupt, so for now we just call the
932 * interrupt function periodically...
933 */
934void
935visorbus_enable_channel_interrupts(struct visor_device *dev)
936{
937 dev_start_periodic_work(dev);
938}
939EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts);
940
941void
942visorbus_disable_channel_interrupts(struct visor_device *dev)
943{
944 dev_stop_periodic_work(dev);
945}
946EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts);
947
948/** This is how everything starts from the device end.
949 * This function is called when a channel first appears via a ControlVM
950 * message. In response, this function allocates a visor_device to
951 * correspond to the new channel, and attempts to connect it the appropriate
952 * driver. If the appropriate driver is found, the visor_driver.probe()
953 * function for that driver will be called, and will be passed the new
954 * visor_device that we just created.
955 *
956 * It's ok if the appropriate driver is not yet loaded, because in that case
957 * the new device struct will just stick around in the bus' list of devices.
958 * When the appropriate driver calls visorbus_register_visor_driver(), the
959 * visor_driver.probe() for the new driver will be called with the new
960 * device.
961 */
962static int
Don Zickusa298bc02015-06-04 09:22:42 -0400963create_visor_device(struct visor_device *dev)
Erik Arfvidson37039872015-05-05 18:36:00 -0400964{
Benjamin Romerf99d3302016-02-23 12:44:16 -0500965 int rc;
Don Zickusa298bc02015-06-04 09:22:42 -0400966 u32 chipset_bus_no = dev->chipset_bus_no;
967 u32 chipset_dev_no = dev->chipset_dev_no;
Erik Arfvidson37039872015-05-05 18:36:00 -0400968
969 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, chipset_dev_no, chipset_bus_no,
970 POSTCODE_SEVERITY_INFO);
Erik Arfvidson37039872015-05-05 18:36:00 -0400971
Erik Arfvidson37039872015-05-05 18:36:00 -0400972 sema_init(&dev->visordriver_callback_lock, 1); /* unlocked */
973 dev->device.bus = &visorbus_type;
Prarit Bhargava59fd2c82015-07-24 12:06:53 -0400974 dev->device.groups = visorbus_channel_groups;
Erik Arfvidson37039872015-05-05 18:36:00 -0400975 device_initialize(&dev->device);
976 dev->device.release = visorbus_release_device;
977 /* keep a reference just for us (now 2) */
978 get_device(&dev->device);
Erik Arfvidson37039872015-05-05 18:36:00 -0400979 dev->periodic_work =
980 visor_periodic_work_create(POLLJIFFIES_NORMALCHANNEL,
981 periodic_dev_workqueue,
982 dev_periodic_work,
983 dev, dev_name(&dev->device));
984 if (!dev->periodic_work) {
985 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, chipset_dev_no,
986 DIAG_SEVERITY_ERR);
Benjamin Romerf99d3302016-02-23 12:44:16 -0500987 rc = -EINVAL;
Erik Arfvidson37039872015-05-05 18:36:00 -0400988 goto away;
989 }
990
991 /* bus_id must be a unique name with respect to this bus TYPE
992 * (NOT bus instance). That's why we need to include the bus
993 * number within the name.
994 */
Don Zickusb4b598f2015-05-13 13:22:25 -0400995 dev_set_name(&dev->device, "vbus%u:dev%u",
Erik Arfvidson37039872015-05-05 18:36:00 -0400996 chipset_bus_no, chipset_dev_no);
997
998 /* device_add does this:
999 * bus_add_device(dev)
1000 * ->device_attach(dev)
1001 * ->for each driver drv registered on the bus that dev is on
1002 * if (dev.drv) ** device already has a driver **
1003 * ** not sure we could ever get here... **
1004 * else
1005 * if (bus.match(dev,drv)) [visorbus_match]
1006 * dev.drv = drv
1007 * if (!drv.probe(dev)) [visordriver_probe_device]
1008 * dev.drv = NULL
1009 *
1010 * Note that device_add does NOT fail if no driver failed to
1011 * claim the device. The device will be linked onto
1012 * bus_type.klist_devices regardless (use bus_for_each_dev).
1013 */
1014 rc = device_add(&dev->device);
1015 if (rc < 0) {
1016 POSTCODE_LINUX_3(DEVICE_ADD_PC, chipset_bus_no,
1017 DIAG_SEVERITY_ERR);
1018 goto away;
1019 }
1020
Erik Arfvidson37039872015-05-05 18:36:00 -04001021 rc = register_devmajorminor_attributes(dev);
1022 if (rc < 0) {
1023 POSTCODE_LINUX_3(DEVICE_REGISTER_FAILURE_PC, chipset_dev_no,
1024 DIAG_SEVERITY_ERR);
Benjamin Romerf99d3302016-02-23 12:44:16 -05001025 goto away_unregister;
Erik Arfvidson37039872015-05-05 18:36:00 -04001026 }
1027
Don Zickusa298bc02015-06-04 09:22:42 -04001028 list_add_tail(&dev->list_all, &list_all_device_instances);
1029 return 0;
Erik Arfvidson37039872015-05-05 18:36:00 -04001030
Benjamin Romerf99d3302016-02-23 12:44:16 -05001031away_unregister:
Don Zickusa298bc02015-06-04 09:22:42 -04001032 device_unregister(&dev->device);
Benjamin Romerf99d3302016-02-23 12:44:16 -05001033
Erik Arfvidson37039872015-05-05 18:36:00 -04001034away:
Don Zickusa298bc02015-06-04 09:22:42 -04001035 put_device(&dev->device);
Erik Arfvidson37039872015-05-05 18:36:00 -04001036 return rc;
1037}
1038
1039static void
1040remove_visor_device(struct visor_device *dev)
1041{
1042 list_del(&dev->list_all);
1043 unregister_devmajorminor_attributes(dev);
Erik Arfvidson37039872015-05-05 18:36:00 -04001044 put_device(&dev->device);
1045 device_unregister(&dev->device);
1046}
1047
Erik Arfvidson37039872015-05-05 18:36:00 -04001048static int
Erik Arfvidson37039872015-05-05 18:36:00 -04001049get_vbus_header_info(struct visorchannel *chan,
1050 struct spar_vbus_headerinfo *hdr_info)
1051{
Erik Arfvidson37039872015-05-05 18:36:00 -04001052 if (!SPAR_VBUS_CHANNEL_OK_CLIENT(visorchannel_get_header(chan)))
Benjamin Romerf748f642016-02-23 10:01:50 -05001053 return -EINVAL;
1054
Erik Arfvidson37039872015-05-05 18:36:00 -04001055 if (visorchannel_read(chan, sizeof(struct channel_header), hdr_info,
1056 sizeof(*hdr_info)) < 0) {
Benjamin Romerf748f642016-02-23 10:01:50 -05001057 return -EIO;
Erik Arfvidson37039872015-05-05 18:36:00 -04001058 }
1059 if (hdr_info->struct_bytes < sizeof(struct spar_vbus_headerinfo))
Benjamin Romerf748f642016-02-23 10:01:50 -05001060 return -EINVAL;
1061
Erik Arfvidson37039872015-05-05 18:36:00 -04001062 if (hdr_info->device_info_struct_bytes <
1063 sizeof(struct ultra_vbus_deviceinfo)) {
Benjamin Romerf748f642016-02-23 10:01:50 -05001064 return -EINVAL;
Erik Arfvidson37039872015-05-05 18:36:00 -04001065 }
Benjamin Romerf748f642016-02-23 10:01:50 -05001066 return 0;
Erik Arfvidson37039872015-05-05 18:36:00 -04001067}
1068
1069/* Write the contents of <info> to the struct
Gavin O'Leary48117892015-11-09 20:12:00 -08001070 * spar_vbus_channel_protocol.chp_info.
1071 */
Erik Arfvidson37039872015-05-05 18:36:00 -04001072
1073static int
1074write_vbus_chp_info(struct visorchannel *chan,
1075 struct spar_vbus_headerinfo *hdr_info,
1076 struct ultra_vbus_deviceinfo *info)
1077{
1078 int off = sizeof(struct channel_header) + hdr_info->chp_info_offset;
1079
1080 if (hdr_info->chp_info_offset == 0)
Christophe JAILLET216c3e22015-07-13 20:52:26 +02001081 return -1;
Erik Arfvidson37039872015-05-05 18:36:00 -04001082
1083 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
Christophe JAILLET216c3e22015-07-13 20:52:26 +02001084 return -1;
Erik Arfvidson37039872015-05-05 18:36:00 -04001085 return 0;
1086}
1087
1088/* Write the contents of <info> to the struct
Gavin O'Leary48117892015-11-09 20:12:00 -08001089 * spar_vbus_channel_protocol.bus_info.
1090 */
Erik Arfvidson37039872015-05-05 18:36:00 -04001091
1092static int
1093write_vbus_bus_info(struct visorchannel *chan,
1094 struct spar_vbus_headerinfo *hdr_info,
1095 struct ultra_vbus_deviceinfo *info)
1096{
1097 int off = sizeof(struct channel_header) + hdr_info->bus_info_offset;
1098
1099 if (hdr_info->bus_info_offset == 0)
Christophe JAILLET216c3e22015-07-13 20:52:26 +02001100 return -1;
Erik Arfvidson37039872015-05-05 18:36:00 -04001101
1102 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
Christophe JAILLET216c3e22015-07-13 20:52:26 +02001103 return -1;
Erik Arfvidson37039872015-05-05 18:36:00 -04001104 return 0;
1105}
1106
1107/* Write the contents of <info> to the
1108 * struct spar_vbus_channel_protocol.dev_info[<devix>].
1109 */
1110static int
1111write_vbus_dev_info(struct visorchannel *chan,
1112 struct spar_vbus_headerinfo *hdr_info,
1113 struct ultra_vbus_deviceinfo *info, int devix)
1114{
1115 int off =
1116 (sizeof(struct channel_header) + hdr_info->dev_info_offset) +
1117 (hdr_info->device_info_struct_bytes * devix);
1118
1119 if (hdr_info->dev_info_offset == 0)
Christophe JAILLET216c3e22015-07-13 20:52:26 +02001120 return -1;
Erik Arfvidson37039872015-05-05 18:36:00 -04001121
1122 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
Christophe JAILLET216c3e22015-07-13 20:52:26 +02001123 return -1;
Erik Arfvidson37039872015-05-05 18:36:00 -04001124 return 0;
1125}
1126
1127/* For a child device just created on a client bus, fill in
1128 * information about the driver that is controlling this device into
1129 * the the appropriate slot within the vbus channel of the bus
1130 * instance.
1131 */
1132static void
1133fix_vbus_dev_info(struct visor_device *visordev)
1134{
1135 int i;
Don Zickusd32517e2015-06-04 09:22:41 -04001136 struct visor_device *bdev;
Erik Arfvidson37039872015-05-05 18:36:00 -04001137 struct visor_driver *visordrv;
1138 int bus_no = visordev->chipset_bus_no;
1139 int dev_no = visordev->chipset_dev_no;
1140 struct ultra_vbus_deviceinfo dev_info;
1141 const char *chan_type_name = NULL;
Don Zickus7726f812015-06-01 13:00:28 -04001142 struct spar_vbus_headerinfo *hdr_info;
Erik Arfvidson37039872015-05-05 18:36:00 -04001143
1144 if (!visordev->device.driver)
Christophe JAILLET216c3e22015-07-13 20:52:26 +02001145 return;
Erik Arfvidson37039872015-05-05 18:36:00 -04001146
Don Zickus7726f812015-06-01 13:00:28 -04001147 hdr_info = (struct spar_vbus_headerinfo *)visordev->vbus_hdr_info;
Don Zickusd32517e2015-06-04 09:22:41 -04001148 if (!hdr_info)
1149 return;
1150
1151 bdev = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
1152 if (!bdev)
1153 return;
Don Zickus7726f812015-06-01 13:00:28 -04001154
Erik Arfvidson37039872015-05-05 18:36:00 -04001155 visordrv = to_visor_driver(visordev->device.driver);
Erik Arfvidson37039872015-05-05 18:36:00 -04001156
1157 /* Within the list of device types (by GUID) that the driver
1158 * says it supports, find out which one of those types matches
1159 * the type of this device, so that we can include the device
1160 * type name
1161 */
1162 for (i = 0; visordrv->channel_types[i].name; i++) {
Erik Arfvidsond5b3f1d2015-05-05 18:37:04 -04001163 if (memcmp(&visordrv->channel_types[i].guid,
1164 &visordev->channel_type_guid,
1165 sizeof(visordrv->channel_types[i].guid)) == 0) {
Erik Arfvidson37039872015-05-05 18:36:00 -04001166 chan_type_name = visordrv->channel_types[i].name;
1167 break;
1168 }
1169 }
1170
1171 bus_device_info_init(&dev_info, chan_type_name,
1172 visordrv->name, visordrv->version,
1173 visordrv->vertag);
Don Zickusd32517e2015-06-04 09:22:41 -04001174 write_vbus_dev_info(bdev->visorchannel, hdr_info, &dev_info, dev_no);
Erik Arfvidson37039872015-05-05 18:36:00 -04001175
1176 /* Re-write bus+chipset info, because it is possible that this
1177 * was previously written by our evil counterpart, virtpci.
1178 */
Don Zickusd32517e2015-06-04 09:22:41 -04001179 write_vbus_chp_info(bdev->visorchannel, hdr_info, &chipset_driverinfo);
1180 write_vbus_bus_info(bdev->visorchannel, hdr_info,
1181 &clientbus_driverinfo);
Erik Arfvidson37039872015-05-05 18:36:00 -04001182}
1183
1184/** Create a device instance for the visor bus itself.
1185 */
Don Zickusd32517e2015-06-04 09:22:41 -04001186static int
1187create_bus_instance(struct visor_device *dev)
Erik Arfvidson37039872015-05-05 18:36:00 -04001188{
Don Zickusd32517e2015-06-04 09:22:41 -04001189 int id = dev->chipset_bus_no;
Don Zickus7726f812015-06-01 13:00:28 -04001190 struct spar_vbus_headerinfo *hdr_info;
Erik Arfvidson37039872015-05-05 18:36:00 -04001191
1192 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
Don Zickus7726f812015-06-01 13:00:28 -04001193
1194 hdr_info = kzalloc(sizeof(*hdr_info), GFP_KERNEL);
Benjamin Romerc2c667d2016-02-23 10:01:52 -05001195 if (!hdr_info)
1196 return -ENOMEM;
Don Zickus7726f812015-06-01 13:00:28 -04001197
Don Zickus343506b2015-06-04 09:22:37 -04001198 dev_set_name(&dev->device, "visorbus%d", id);
1199 dev->device.bus = &visorbus_type;
1200 dev->device.groups = visorbus_groups;
1201 dev->device.release = visorbus_release_busdevice;
Don Zickusd32517e2015-06-04 09:22:41 -04001202
Don Zickus343506b2015-06-04 09:22:37 -04001203 if (device_register(&dev->device) < 0) {
Don Zickus03274d32015-05-13 13:22:12 -04001204 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, id,
1205 POSTCODE_SEVERITY_ERR);
Benjamin Romerc2c667d2016-02-23 10:01:52 -05001206 kfree(hdr_info);
1207 return -ENODEV;
Don Zickus03274d32015-05-13 13:22:12 -04001208 }
Don Zickusd32517e2015-06-04 09:22:41 -04001209
Don Zickusee983d92015-06-04 09:22:39 -04001210 if (get_vbus_header_info(dev->visorchannel, hdr_info) >= 0) {
1211 dev->vbus_hdr_info = (void *)hdr_info;
1212 write_vbus_chp_info(dev->visorchannel, hdr_info,
1213 &chipset_driverinfo);
1214 write_vbus_bus_info(dev->visorchannel, hdr_info,
1215 &clientbus_driverinfo);
Don Zickusb32c4992015-06-01 13:00:26 -04001216 } else {
Don Zickusee983d92015-06-04 09:22:39 -04001217 kfree(hdr_info);
Erik Arfvidson37039872015-05-05 18:36:00 -04001218 }
Erik Arfvidson37039872015-05-05 18:36:00 -04001219 bus_count++;
Don Zickus343506b2015-06-04 09:22:37 -04001220 list_add_tail(&dev->list_all, &list_all_bus_instances);
Don Zickus343506b2015-06-04 09:22:37 -04001221 dev_set_drvdata(&dev->device, dev);
Don Zickusd32517e2015-06-04 09:22:41 -04001222 return 0;
Erik Arfvidson37039872015-05-05 18:36:00 -04001223}
1224
1225/** Remove a device instance for the visor bus itself.
1226 */
1227static void
Don Zickus343506b2015-06-04 09:22:37 -04001228remove_bus_instance(struct visor_device *dev)
Erik Arfvidson37039872015-05-05 18:36:00 -04001229{
1230 /* Note that this will result in the release method for
Don Zickus343506b2015-06-04 09:22:37 -04001231 * dev->dev being called, which will call
Erik Arfvidson37039872015-05-05 18:36:00 -04001232 * visorbus_release_busdevice(). This has something to do with
1233 * the put_device() done in device_unregister(), but I have never
1234 * successfully been able to trace thru the code to see where/how
1235 * release() gets called. But I know it does.
1236 */
Erik Arfvidson37039872015-05-05 18:36:00 -04001237 bus_count--;
Don Zickus343506b2015-06-04 09:22:37 -04001238 if (dev->visorchannel) {
1239 visorchannel_destroy(dev->visorchannel);
1240 dev->visorchannel = NULL;
Erik Arfvidson37039872015-05-05 18:36:00 -04001241 }
Don Zickus343506b2015-06-04 09:22:37 -04001242 kfree(dev->vbus_hdr_info);
1243 list_del(&dev->list_all);
1244 device_unregister(&dev->device);
Erik Arfvidson37039872015-05-05 18:36:00 -04001245}
1246
1247/** Create and register the one-and-only one instance of
1248 * the visor bus type (visorbus_type).
1249 */
1250static int
1251create_bus_type(void)
1252{
Benjamin Romer6155a3c2015-09-04 12:01:32 -04001253 busreg_rc = bus_register(&visorbus_type);
1254 return busreg_rc;
Erik Arfvidson37039872015-05-05 18:36:00 -04001255}
1256
1257/** Remove the one-and-only one instance of the visor bus type (visorbus_type).
1258 */
1259static void
1260remove_bus_type(void)
1261{
Erik Arfvidson37039872015-05-05 18:36:00 -04001262 bus_unregister(&visorbus_type);
1263}
1264
1265/** Remove all child visor bus device instances.
1266 */
1267static void
1268remove_all_visor_devices(void)
1269{
1270 struct list_head *listentry, *listtmp;
1271
1272 list_for_each_safe(listentry, listtmp, &list_all_device_instances) {
1273 struct visor_device *dev = list_entry(listentry,
1274 struct visor_device,
1275 list_all);
1276 remove_visor_device(dev);
1277 }
1278}
1279
Erik Arfvidson37039872015-05-05 18:36:00 -04001280static void
Don Zickusd32517e2015-06-04 09:22:41 -04001281chipset_bus_create(struct visor_device *dev)
Erik Arfvidson37039872015-05-05 18:36:00 -04001282{
Don Zickusd32517e2015-06-04 09:22:41 -04001283 int rc;
1284 u32 bus_no = dev->chipset_bus_no;
Erik Arfvidson37039872015-05-05 18:36:00 -04001285
1286 POSTCODE_LINUX_3(BUS_CREATE_ENTRY_PC, bus_no, POSTCODE_SEVERITY_INFO);
Don Zickusd32517e2015-06-04 09:22:41 -04001287 rc = create_bus_instance(dev);
Erik Arfvidson37039872015-05-05 18:36:00 -04001288 POSTCODE_LINUX_3(BUS_CREATE_EXIT_PC, bus_no, POSTCODE_SEVERITY_INFO);
Don Zickusd32517e2015-06-04 09:22:41 -04001289
1290 if (rc < 0)
Erik Arfvidson37039872015-05-05 18:36:00 -04001291 POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, bus_no,
1292 POSTCODE_SEVERITY_ERR);
Don Zickusd32517e2015-06-04 09:22:41 -04001293 else
1294 POSTCODE_LINUX_3(CHIPSET_INIT_SUCCESS_PC, bus_no,
1295 POSTCODE_SEVERITY_INFO);
1296
Erik Arfvidson37039872015-05-05 18:36:00 -04001297 if (chipset_responders.bus_create)
Don Zickusd32517e2015-06-04 09:22:41 -04001298 (*chipset_responders.bus_create) (dev, rc);
Erik Arfvidson37039872015-05-05 18:36:00 -04001299}
1300
1301static void
Don Zickusd32517e2015-06-04 09:22:41 -04001302chipset_bus_destroy(struct visor_device *dev)
Erik Arfvidson37039872015-05-05 18:36:00 -04001303{
Don Zickus343506b2015-06-04 09:22:37 -04001304 remove_bus_instance(dev);
Erik Arfvidson37039872015-05-05 18:36:00 -04001305 if (chipset_responders.bus_destroy)
Don Zickusd32517e2015-06-04 09:22:41 -04001306 (*chipset_responders.bus_destroy)(dev, 0);
Erik Arfvidson37039872015-05-05 18:36:00 -04001307}
1308
1309static void
Don Zickusa298bc02015-06-04 09:22:42 -04001310chipset_device_create(struct visor_device *dev_info)
Erik Arfvidson37039872015-05-05 18:36:00 -04001311{
Benjamin Romer7a9749b2016-02-23 10:01:53 -05001312 int rc;
Don Zickusa298bc02015-06-04 09:22:42 -04001313 u32 bus_no = dev_info->chipset_bus_no;
1314 u32 dev_no = dev_info->chipset_dev_no;
Erik Arfvidson37039872015-05-05 18:36:00 -04001315
1316 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, dev_no, bus_no,
1317 POSTCODE_SEVERITY_INFO);
1318
Don Zickusa298bc02015-06-04 09:22:42 -04001319 rc = create_visor_device(dev_info);
David Kershner86ea8ac2015-06-12 16:46:08 -04001320 if (chipset_responders.device_create)
1321 chipset_responders.device_create(dev_info, rc);
1322
1323 if (rc < 0)
Erik Arfvidson37039872015-05-05 18:36:00 -04001324 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
1325 POSTCODE_SEVERITY_ERR);
David Kershner86ea8ac2015-06-12 16:46:08 -04001326 else
1327 POSTCODE_LINUX_4(DEVICE_CREATE_SUCCESS_PC, dev_no, bus_no,
1328 POSTCODE_SEVERITY_INFO);
Erik Arfvidson37039872015-05-05 18:36:00 -04001329}
1330
1331static void
Don Zickusa298bc02015-06-04 09:22:42 -04001332chipset_device_destroy(struct visor_device *dev_info)
Erik Arfvidson37039872015-05-05 18:36:00 -04001333{
Don Zickusa298bc02015-06-04 09:22:42 -04001334 remove_visor_device(dev_info);
Erik Arfvidson37039872015-05-05 18:36:00 -04001335
1336 if (chipset_responders.device_destroy)
Don Zickusa298bc02015-06-04 09:22:42 -04001337 (*chipset_responders.device_destroy) (dev_info, 0);
Erik Arfvidson37039872015-05-05 18:36:00 -04001338}
1339
1340/* This is the callback function specified for a function driver, to
1341 * be called when a pending "pause device" operation has been
1342 * completed.
1343 */
1344static void
Don Zickusa298bc02015-06-04 09:22:42 -04001345pause_state_change_complete(struct visor_device *dev, int status)
Erik Arfvidson37039872015-05-05 18:36:00 -04001346{
1347 if (!dev->pausing)
Christophe JAILLET216c3e22015-07-13 20:52:26 +02001348 return;
Erik Arfvidson37039872015-05-05 18:36:00 -04001349
Prarit Bhargava779d0752015-05-05 18:37:01 -04001350 dev->pausing = false;
Erik Arfvidson37039872015-05-05 18:36:00 -04001351 if (!chipset_responders.device_pause) /* this can never happen! */
Christophe JAILLET216c3e22015-07-13 20:52:26 +02001352 return;
Erik Arfvidson37039872015-05-05 18:36:00 -04001353
1354 /* Notify the chipset driver that the pause is complete, which
Erik Arfvidson7005c162016-02-08 10:41:49 -05001355 * will presumably want to send some sort of response to the
1356 * initiator.
1357 */
Don Zickusa298bc02015-06-04 09:22:42 -04001358 (*chipset_responders.device_pause) (dev, status);
Erik Arfvidson37039872015-05-05 18:36:00 -04001359}
1360
1361/* This is the callback function specified for a function driver, to
1362 * be called when a pending "resume device" operation has been
1363 * completed.
1364 */
1365static void
Don Zickusa298bc02015-06-04 09:22:42 -04001366resume_state_change_complete(struct visor_device *dev, int status)
Erik Arfvidson37039872015-05-05 18:36:00 -04001367{
1368 if (!dev->resuming)
Christophe JAILLET216c3e22015-07-13 20:52:26 +02001369 return;
Erik Arfvidson37039872015-05-05 18:36:00 -04001370
Prarit Bhargava779d0752015-05-05 18:37:01 -04001371 dev->resuming = false;
Erik Arfvidson37039872015-05-05 18:36:00 -04001372 if (!chipset_responders.device_resume) /* this can never happen! */
Christophe JAILLET216c3e22015-07-13 20:52:26 +02001373 return;
Erik Arfvidson37039872015-05-05 18:36:00 -04001374
1375 /* Notify the chipset driver that the resume is complete,
1376 * which will presumably want to send some sort of response to
Gavin O'Leary48117892015-11-09 20:12:00 -08001377 * the initiator.
1378 */
Don Zickusa298bc02015-06-04 09:22:42 -04001379 (*chipset_responders.device_resume) (dev, status);
Erik Arfvidson37039872015-05-05 18:36:00 -04001380}
1381
1382/* Tell the subordinate function driver for a specific device to pause
1383 * or resume that device. Result is returned asynchronously via a
1384 * callback function.
1385 */
1386static void
Don Zickusa298bc02015-06-04 09:22:42 -04001387initiate_chipset_device_pause_resume(struct visor_device *dev, bool is_pause)
Erik Arfvidson37039872015-05-05 18:36:00 -04001388{
Benjamin Romer03b93f02016-02-23 10:01:54 -05001389 int rc;
Erik Arfvidson37039872015-05-05 18:36:00 -04001390 struct visor_driver *drv = NULL;
Don Zickusa298bc02015-06-04 09:22:42 -04001391 void (*notify_func)(struct visor_device *dev, int response) = NULL;
Erik Arfvidson37039872015-05-05 18:36:00 -04001392
1393 if (is_pause)
1394 notify_func = chipset_responders.device_pause;
1395 else
1396 notify_func = chipset_responders.device_resume;
1397 if (!notify_func)
Benjamin Romer03b93f02016-02-23 10:01:54 -05001398 return;
Erik Arfvidson37039872015-05-05 18:36:00 -04001399
Erik Arfvidson37039872015-05-05 18:36:00 -04001400 drv = to_visor_driver(dev->device.driver);
Benjamin Romer03b93f02016-02-23 10:01:54 -05001401 if (!drv) {
1402 (*notify_func)(dev, -ENODEV);
1403 return;
1404 }
Erik Arfvidson37039872015-05-05 18:36:00 -04001405
Benjamin Romer03b93f02016-02-23 10:01:54 -05001406 if (dev->pausing || dev->resuming) {
1407 (*notify_func)(dev, -EBUSY);
1408 return;
1409 }
Erik Arfvidson37039872015-05-05 18:36:00 -04001410
1411 /* Note that even though both drv->pause() and drv->resume
1412 * specify a callback function, it is NOT necessary for us to
1413 * increment our local module usage count. Reason is, there
1414 * is already a linkage dependency between child function
1415 * drivers and visorbus, so it is already IMPOSSIBLE to unload
1416 * visorbus while child function drivers are still running.
1417 */
1418 if (is_pause) {
Benjamin Romer03b93f02016-02-23 10:01:54 -05001419 if (!drv->pause) {
1420 (*notify_func)(dev, -EINVAL);
1421 return;
1422 }
Erik Arfvidson37039872015-05-05 18:36:00 -04001423
Prarit Bhargava779d0752015-05-05 18:37:01 -04001424 dev->pausing = true;
Benjamin Romer03b93f02016-02-23 10:01:54 -05001425 rc = drv->pause(dev, pause_state_change_complete);
Erik Arfvidson37039872015-05-05 18:36:00 -04001426 } else {
1427 /* This should be done at BUS resume time, but an
1428 * existing problem prevents us from ever getting a bus
1429 * resume... This hack would fail to work should we
1430 * ever have a bus that contains NO devices, since we
Gavin O'Leary48117892015-11-09 20:12:00 -08001431 * would never even get here in that case.
1432 */
Erik Arfvidson37039872015-05-05 18:36:00 -04001433 fix_vbus_dev_info(dev);
Benjamin Romer03b93f02016-02-23 10:01:54 -05001434 if (!drv->resume) {
1435 (*notify_func)(dev, -EINVAL);
1436 return;
1437 }
Erik Arfvidson37039872015-05-05 18:36:00 -04001438
Prarit Bhargava779d0752015-05-05 18:37:01 -04001439 dev->resuming = true;
Benjamin Romer03b93f02016-02-23 10:01:54 -05001440 rc = drv->resume(dev, resume_state_change_complete);
Erik Arfvidson37039872015-05-05 18:36:00 -04001441 }
Benjamin Romer03b93f02016-02-23 10:01:54 -05001442 if (rc < 0) {
Erik Arfvidson37039872015-05-05 18:36:00 -04001443 if (is_pause)
Prarit Bhargava779d0752015-05-05 18:37:01 -04001444 dev->pausing = false;
Erik Arfvidson37039872015-05-05 18:36:00 -04001445 else
Prarit Bhargava779d0752015-05-05 18:37:01 -04001446 dev->resuming = false;
Benjamin Romer03b93f02016-02-23 10:01:54 -05001447 (*notify_func)(dev, -EINVAL);
Erik Arfvidson37039872015-05-05 18:36:00 -04001448 }
1449}
1450
1451static void
Don Zickusa298bc02015-06-04 09:22:42 -04001452chipset_device_pause(struct visor_device *dev_info)
Erik Arfvidson37039872015-05-05 18:36:00 -04001453{
Don Zickusb4b598f2015-05-13 13:22:25 -04001454 initiate_chipset_device_pause_resume(dev_info, true);
Erik Arfvidson37039872015-05-05 18:36:00 -04001455}
1456
1457static void
Don Zickusa298bc02015-06-04 09:22:42 -04001458chipset_device_resume(struct visor_device *dev_info)
Erik Arfvidson37039872015-05-05 18:36:00 -04001459{
Don Zickusb4b598f2015-05-13 13:22:25 -04001460 initiate_chipset_device_pause_resume(dev_info, false);
Erik Arfvidson37039872015-05-05 18:36:00 -04001461}
1462
1463struct channel_size_info {
1464 uuid_le guid;
1465 unsigned long min_size;
1466 unsigned long max_size;
1467};
1468
Prarit Bhargava55c67dc2015-05-05 18:37:02 -04001469int
Erik Arfvidson37039872015-05-05 18:36:00 -04001470visorbus_init(void)
1471{
1472 int rc = 0;
1473
1474 POSTCODE_LINUX_3(DRIVER_ENTRY_PC, rc, POSTCODE_SEVERITY_INFO);
1475 bus_device_info_init(&clientbus_driverinfo,
Erik Arfvidson46168812015-05-05 18:36:14 -04001476 "clientbus", "visorbus",
Erik Arfvidson37039872015-05-05 18:36:00 -04001477 VERSION, NULL);
1478
Erik Arfvidson37039872015-05-05 18:36:00 -04001479 rc = create_bus_type();
1480 if (rc < 0) {
1481 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC, DIAG_SEVERITY_ERR);
1482 goto away;
1483 }
1484
1485 periodic_dev_workqueue = create_singlethread_workqueue("visorbus_dev");
1486 if (!periodic_dev_workqueue) {
1487 POSTCODE_LINUX_2(CREATE_WORKQUEUE_PC, DIAG_SEVERITY_ERR);
1488 rc = -ENOMEM;
1489 goto away;
1490 }
1491
1492 /* This enables us to receive notifications when devices appear for
1493 * which this service partition is to be a server for.
1494 */
David Kershner4da33362015-05-05 18:36:39 -04001495 visorchipset_register_busdev(&chipset_notifiers,
1496 &chipset_responders,
1497 &chipset_driverinfo);
Erik Arfvidson37039872015-05-05 18:36:00 -04001498
1499 rc = 0;
1500
1501away:
1502 if (rc)
Christophe JAILLET216c3e22015-07-13 20:52:26 +02001503 POSTCODE_LINUX_3(CHIPSET_INIT_FAILURE_PC, rc,
1504 POSTCODE_SEVERITY_ERR);
Erik Arfvidson37039872015-05-05 18:36:00 -04001505 return rc;
1506}
1507
Prarit Bhargavac79b28f2015-05-05 18:36:15 -04001508void
Erik Arfvidson37039872015-05-05 18:36:00 -04001509visorbus_exit(void)
1510{
1511 struct list_head *listentry, *listtmp;
1512
David Kershner4da33362015-05-05 18:36:39 -04001513 visorchipset_register_busdev(NULL, NULL, NULL);
Erik Arfvidson37039872015-05-05 18:36:00 -04001514 remove_all_visor_devices();
1515
1516 flush_workqueue(periodic_dev_workqueue); /* better not be any work! */
1517 destroy_workqueue(periodic_dev_workqueue);
1518 periodic_dev_workqueue = NULL;
1519
1520 if (periodic_test_workqueue) {
1521 cancel_delayed_work(&periodic_work);
1522 flush_workqueue(periodic_test_workqueue);
1523 destroy_workqueue(periodic_test_workqueue);
1524 periodic_test_workqueue = NULL;
1525 }
1526
1527 list_for_each_safe(listentry, listtmp, &list_all_bus_instances) {
Don Zickus343506b2015-06-04 09:22:37 -04001528 struct visor_device *dev = list_entry(listentry,
Christophe JAILLET216c3e22015-07-13 20:52:26 +02001529 struct visor_device,
1530 list_all);
Don Zickus343506b2015-06-04 09:22:37 -04001531 remove_bus_instance(dev);
Erik Arfvidson37039872015-05-05 18:36:00 -04001532 }
1533 remove_bus_type();
1534}
1535
1536module_param_named(debug, visorbus_debug, int, S_IRUGO);
1537MODULE_PARM_DESC(visorbus_debug, "1 to debug");
Erik Arfvidson37039872015-05-05 18:36:00 -04001538
1539module_param_named(forcematch, visorbus_forcematch, int, S_IRUGO);
1540MODULE_PARM_DESC(visorbus_forcematch,
1541 "1 to force a successful dev <--> drv match");
Erik Arfvidson37039872015-05-05 18:36:00 -04001542
1543module_param_named(forcenomatch, visorbus_forcenomatch, int, S_IRUGO);
1544MODULE_PARM_DESC(visorbus_forcenomatch,
1545 "1 to force an UNsuccessful dev <--> drv match");
Erik Arfvidson37039872015-05-05 18:36:00 -04001546
Erik Arfvidson37039872015-05-05 18:36:00 -04001547module_param_named(debugref, visorbus_debugref, int, S_IRUGO);
1548MODULE_PARM_DESC(visorbus_debugref, "1 to debug reference counting");