blob: 3a147dbbd7b53e39bdb25b6a9897495b0574ba5d [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 Arfvidson38d56c22016-04-13 12:07:07 -040036/* Display string that is guaranteed to be no longer the 99 characters*/
37#define LINESIZE 99
38
Erik Arfvidson37039872015-05-05 18:36:00 -040039#define CURRENT_FILE_PC VISOR_BUS_PC_visorbus_main_c
40#define POLLJIFFIES_TESTWORK 100
41#define POLLJIFFIES_NORMALCHANNEL 10
42
Benjamin Romer6155a3c2015-09-04 12:01:32 -040043static int busreg_rc = -ENODEV; /* stores the result from bus registration */
44
Erik Arfvidson37039872015-05-05 18:36:00 -040045static int visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env);
46static int visorbus_match(struct device *xdev, struct device_driver *xdrv);
47static void fix_vbus_dev_info(struct visor_device *visordev);
48
Don Zickus68b04f12015-05-13 13:22:15 -040049/* BUS type attributes
50 *
51 * define & implement display of bus attributes under
52 * /sys/bus/visorbus.
53 *
54 */
55
56static ssize_t version_show(struct bus_type *bus, char *buf)
57{
58 return snprintf(buf, PAGE_SIZE, "%s\n", VERSION);
59}
60
61static BUS_ATTR_RO(version);
62
63static struct attribute *visorbus_bus_attrs[] = {
64 &bus_attr_version.attr,
65 NULL,
66};
67
68static const struct attribute_group visorbus_bus_group = {
69 .attrs = visorbus_bus_attrs,
70};
71
Drew Fustinif6758e72015-05-28 21:04:23 -050072static const struct attribute_group *visorbus_bus_groups[] = {
Don Zickus68b04f12015-05-13 13:22:15 -040073 &visorbus_bus_group,
74 NULL,
75};
76
Prarit Bhargava59fd2c82015-07-24 12:06:53 -040077/*
78 * DEVICE type attributes
79 *
80 * The modalias file will contain the guid of the device.
81 */
82static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
83 char *buf)
84{
85 struct visor_device *vdev;
86 uuid_le guid;
87
88 vdev = to_visor_device(dev);
89 guid = visorchannel_get_uuid(vdev->visorchannel);
90 return snprintf(buf, PAGE_SIZE, "visorbus:%pUl\n", &guid);
91}
92static DEVICE_ATTR_RO(modalias);
93
94static struct attribute *visorbus_dev_attrs[] = {
95 &dev_attr_modalias.attr,
96 NULL,
97};
98
99/* sysfs example for bridge-only sysfs files using device_type's */
100static const struct attribute_group visorbus_dev_group = {
101 .attrs = visorbus_dev_attrs,
102};
103
Michał Kępieńcf7281c2015-08-11 14:07:00 +0200104static const struct attribute_group *visorbus_dev_groups[] = {
Prarit Bhargava59fd2c82015-07-24 12:06:53 -0400105 &visorbus_dev_group,
106 NULL,
107};
108
Erik Arfvidson37039872015-05-05 18:36:00 -0400109/** This describes the TYPE of bus.
110 * (Don't confuse this with an INSTANCE of the bus.)
111 */
Don Zickus1fb30162015-05-13 13:22:18 -0400112struct bus_type visorbus_type = {
Erik Arfvidson37039872015-05-05 18:36:00 -0400113 .name = "visorbus",
114 .match = visorbus_match,
115 .uevent = visorbus_uevent,
Prarit Bhargava59fd2c82015-07-24 12:06:53 -0400116 .dev_groups = visorbus_dev_groups,
Don Zickus68b04f12015-05-13 13:22:15 -0400117 .bus_groups = visorbus_bus_groups,
Erik Arfvidson37039872015-05-05 18:36:00 -0400118};
119
120static struct delayed_work periodic_work;
121
122/* YES, we need 2 workqueues.
123 * The reason is, workitems on the test queue may need to cancel
124 * workitems on the other queue. You will be in for trouble if you try to
125 * do this with workitems queued on the same workqueue.
126 */
127static struct workqueue_struct *periodic_test_workqueue;
128static struct workqueue_struct *periodic_dev_workqueue;
129static long long bus_count; /** number of bus instances */
Erik Arfvidson37039872015-05-05 18:36:00 -0400130 /** ever-increasing */
131
Don Zickusd32517e2015-06-04 09:22:41 -0400132static void chipset_bus_create(struct visor_device *bus_info);
133static void chipset_bus_destroy(struct visor_device *bus_info);
Don Zickusa298bc02015-06-04 09:22:42 -0400134static void chipset_device_create(struct visor_device *dev_info);
135static void chipset_device_destroy(struct visor_device *dev_info);
136static void chipset_device_pause(struct visor_device *dev_info);
137static void chipset_device_resume(struct visor_device *dev_info);
Erik Arfvidson37039872015-05-05 18:36:00 -0400138
139/** These functions are implemented herein, and are called by the chipset
140 * driver to notify us about specific events.
141 */
142static struct visorchipset_busdev_notifiers chipset_notifiers = {
143 .bus_create = chipset_bus_create,
144 .bus_destroy = chipset_bus_destroy,
145 .device_create = chipset_device_create,
146 .device_destroy = chipset_device_destroy,
147 .device_pause = chipset_device_pause,
148 .device_resume = chipset_device_resume,
149};
150
151/** These functions are implemented in the chipset driver, and we call them
152 * herein when we want to acknowledge a specific event.
153 */
154static struct visorchipset_busdev_responders chipset_responders;
155
156/* filled in with info about parent chipset driver when we register with it */
157static struct ultra_vbus_deviceinfo chipset_driverinfo;
158/* filled in with info about this driver, wrt it servicing client busses */
159static struct ultra_vbus_deviceinfo clientbus_driverinfo;
160
Don Zickus343506b2015-06-04 09:22:37 -0400161/** list of visor_device structs, linked via .list_all */
Erik Arfvidson37039872015-05-05 18:36:00 -0400162static LIST_HEAD(list_all_bus_instances);
163/** list of visor_device structs, linked via .list_all */
164static LIST_HEAD(list_all_device_instances);
165
166static int
167visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env)
168{
Prarit Bhargava59fd2c82015-07-24 12:06:53 -0400169 struct visor_device *dev;
170 uuid_le guid;
171
172 dev = to_visor_device(xdev);
173 guid = visorchannel_get_uuid(dev->visorchannel);
174
175 if (add_uevent_var(env, "MODALIAS=visorbus:%pUl", &guid))
Erik Arfvidson37039872015-05-05 18:36:00 -0400176 return -ENOMEM;
177 return 0;
178}
179
180/* This is called automatically upon adding a visor_device (device_add), or
181 * adding a visor_driver (visorbus_register_visor_driver), and returns 1 iff the
182 * provided driver can control the specified device.
183 */
184static int
185visorbus_match(struct device *xdev, struct device_driver *xdrv)
186{
187 uuid_le channel_type;
Erik Arfvidson37039872015-05-05 18:36:00 -0400188 int i;
189 struct visor_device *dev;
190 struct visor_driver *drv;
191
192 dev = to_visor_device(xdev);
193 drv = to_visor_driver(xdrv);
194 channel_type = visorchannel_get_uuid(dev->visorchannel);
Erik Arfvidson37039872015-05-05 18:36:00 -0400195
David Kershner8e33f482016-03-12 21:27:11 -0500196 if (visorbus_forcematch)
197 return 1;
198 if (visorbus_forcenomatch)
199 return 0;
Erik Arfvidson37039872015-05-05 18:36:00 -0400200 if (!drv->channel_types)
David Kershner8e33f482016-03-12 21:27:11 -0500201 return 0;
202
Erik Arfvidson37039872015-05-05 18:36:00 -0400203 for (i = 0;
204 (uuid_le_cmp(drv->channel_types[i].guid, NULL_UUID_LE) != 0) ||
205 (drv->channel_types[i].name);
206 i++)
207 if (uuid_le_cmp(drv->channel_types[i].guid,
David Kershner8e33f482016-03-12 21:27:11 -0500208 channel_type) == 0)
209 return i + 1;
210
211 return 0;
Erik Arfvidson37039872015-05-05 18:36:00 -0400212}
213
214/** This is called when device_unregister() is called for the bus device
215 * instance, after all other tasks involved with destroying the device
216 * are complete.
217 */
218static void
219visorbus_release_busdevice(struct device *xdev)
220{
Don Zickus343506b2015-06-04 09:22:37 -0400221 struct visor_device *dev = dev_get_drvdata(xdev);
Erik Arfvidson37039872015-05-05 18:36:00 -0400222
Don Zickus343506b2015-06-04 09:22:37 -0400223 kfree(dev);
Erik Arfvidson37039872015-05-05 18:36:00 -0400224}
225
226/** This is called when device_unregister() is called for each child
227 * device instance.
228 */
229static void
230visorbus_release_device(struct device *xdev)
231{
232 struct visor_device *dev = to_visor_device(xdev);
233
234 if (dev->periodic_work) {
235 visor_periodic_work_destroy(dev->periodic_work);
236 dev->periodic_work = NULL;
237 }
238 if (dev->visorchannel) {
239 visorchannel_destroy(dev->visorchannel);
240 dev->visorchannel = NULL;
241 }
242 kfree(dev);
243}
244
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400245/* begin implementation of specific channel attributes to appear under
246* /sys/bus/visorbus<x>/dev<y>/channel
247*/
Don Zickus79573162015-05-13 13:22:17 -0400248static ssize_t physaddr_show(struct device *dev, struct device_attribute *attr,
249 char *buf)
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400250{
Don Zickus79573162015-05-13 13:22:17 -0400251 struct visor_device *vdev = to_visor_device(dev);
252
253 if (!vdev->visorchannel)
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400254 return 0;
Erik Arfvidson8a1cca32016-05-06 13:11:18 -0400255 return snprintf(buf, PAGE_SIZE, "0x%llx\n",
Don Zickus79573162015-05-13 13:22:17 -0400256 visorchannel_get_physaddr(vdev->visorchannel));
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400257}
258
Don Zickus79573162015-05-13 13:22:17 -0400259static ssize_t nbytes_show(struct device *dev, struct device_attribute *attr,
260 char *buf)
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400261{
Don Zickus79573162015-05-13 13:22:17 -0400262 struct visor_device *vdev = to_visor_device(dev);
263
264 if (!vdev->visorchannel)
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400265 return 0;
266 return snprintf(buf, PAGE_SIZE, "0x%lx\n",
Don Zickus79573162015-05-13 13:22:17 -0400267 visorchannel_get_nbytes(vdev->visorchannel));
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400268}
269
Don Zickus79573162015-05-13 13:22:17 -0400270static ssize_t clientpartition_show(struct device *dev,
271 struct device_attribute *attr, char *buf)
272{
273 struct visor_device *vdev = to_visor_device(dev);
274
275 if (!vdev->visorchannel)
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400276 return 0;
Erik Arfvidson8a1cca32016-05-06 13:11:18 -0400277 return snprintf(buf, PAGE_SIZE, "0x%llx\n",
Don Zickus79573162015-05-13 13:22:17 -0400278 visorchannel_get_clientpartition(vdev->visorchannel));
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400279}
280
Don Zickus79573162015-05-13 13:22:17 -0400281static ssize_t typeguid_show(struct device *dev, struct device_attribute *attr,
282 char *buf)
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400283{
Don Zickus79573162015-05-13 13:22:17 -0400284 struct visor_device *vdev = to_visor_device(dev);
Erik Arfvidson38d56c22016-04-13 12:07:07 -0400285 char typeid[LINESIZE];
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400286
Don Zickus79573162015-05-13 13:22:17 -0400287 if (!vdev->visorchannel)
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400288 return 0;
289 return snprintf(buf, PAGE_SIZE, "%s\n",
Alexander Curtin48f57142016-03-23 22:15:49 -0400290 visorchannel_id(vdev->visorchannel, typeid));
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400291}
292
Don Zickus79573162015-05-13 13:22:17 -0400293static ssize_t zoneguid_show(struct device *dev, struct device_attribute *attr,
294 char *buf)
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400295{
Don Zickus79573162015-05-13 13:22:17 -0400296 struct visor_device *vdev = to_visor_device(dev);
Erik Arfvidson38d56c22016-04-13 12:07:07 -0400297 char zoneid[LINESIZE];
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400298
Don Zickus79573162015-05-13 13:22:17 -0400299 if (!vdev->visorchannel)
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400300 return 0;
301 return snprintf(buf, PAGE_SIZE, "%s\n",
Alexander Curtin62f3dc82016-03-23 22:15:50 -0400302 visorchannel_zoneid(vdev->visorchannel, zoneid));
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400303}
304
Don Zickus79573162015-05-13 13:22:17 -0400305static ssize_t typename_show(struct device *dev, struct device_attribute *attr,
306 char *buf)
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400307{
Don Zickus79573162015-05-13 13:22:17 -0400308 struct visor_device *vdev = to_visor_device(dev);
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400309 int i = 0;
Don Zickus79573162015-05-13 13:22:17 -0400310 struct bus_type *xbus = dev->bus;
311 struct device_driver *xdrv = dev->driver;
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400312 struct visor_driver *drv = NULL;
313
Don Zickus79573162015-05-13 13:22:17 -0400314 if (!vdev->visorchannel || !xbus || !xdrv)
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400315 return 0;
Don Zickus79573162015-05-13 13:22:17 -0400316 i = xbus->match(dev, xdrv);
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400317 if (!i)
318 return 0;
319 drv = to_visor_driver(xdrv);
320 return snprintf(buf, PAGE_SIZE, "%s\n", drv->channel_types[i - 1].name);
321}
322
Don Zickus79573162015-05-13 13:22:17 -0400323static DEVICE_ATTR_RO(physaddr);
324static DEVICE_ATTR_RO(nbytes);
325static DEVICE_ATTR_RO(clientpartition);
326static DEVICE_ATTR_RO(typeguid);
327static DEVICE_ATTR_RO(zoneguid);
328static DEVICE_ATTR_RO(typename);
Don Zickus79573162015-05-13 13:22:17 -0400329
330static struct attribute *channel_attrs[] = {
331 &dev_attr_physaddr.attr,
332 &dev_attr_nbytes.attr,
333 &dev_attr_clientpartition.attr,
334 &dev_attr_typeguid.attr,
335 &dev_attr_zoneguid.attr,
336 &dev_attr_typename.attr,
Tim Sellfd012d02015-07-13 14:51:23 -0400337 NULL
Don Zickus79573162015-05-13 13:22:17 -0400338};
339
340static struct attribute_group channel_attr_grp = {
341 .name = "channel",
342 .attrs = channel_attrs,
343};
344
Prarit Bhargava59fd2c82015-07-24 12:06:53 -0400345static const struct attribute_group *visorbus_channel_groups[] = {
Don Zickus79573162015-05-13 13:22:17 -0400346 &channel_attr_grp,
347 NULL
Prarit Bhargava826b6a02015-05-05 18:36:06 -0400348};
349
350/* end implementation of specific channel attributes */
351
Erik Arfvidson37039872015-05-05 18:36:00 -0400352/* BUS instance attributes
353 *
354 * define & implement display of bus attributes under
355 * /sys/bus/visorbus/busses/visorbus<n>.
356 *
357 * This is a bit hoaky because the kernel does not yet have the infrastructure
358 * to separate bus INSTANCE attributes from bus TYPE attributes...
359 * so we roll our own. See businst.c / businst.h.
360 *
361 */
362
Don Zickusd181dd02015-05-13 13:22:13 -0400363static ssize_t partition_handle_show(struct device *dev,
364 struct device_attribute *attr,
365 char *buf) {
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400366 struct visor_device *vdev = to_visor_device(dev);
367 u64 handle = visorchannel_get_clientpartition(vdev->visorchannel);
Erik Arfvidson37039872015-05-05 18:36:00 -0400368
Erik Arfvidson8a1cca32016-05-06 13:11:18 -0400369 return snprintf(buf, PAGE_SIZE, "0x%llx\n", handle);
Erik Arfvidson37039872015-05-05 18:36:00 -0400370}
371
Don Zickusd181dd02015-05-13 13:22:13 -0400372static ssize_t partition_guid_show(struct device *dev,
373 struct device_attribute *attr,
374 char *buf) {
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400375 struct visor_device *vdev = to_visor_device(dev);
Erik Arfvidson37039872015-05-05 18:36:00 -0400376
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400377 return snprintf(buf, PAGE_SIZE, "{%pUb}\n", &vdev->partition_uuid);
Erik Arfvidson37039872015-05-05 18:36:00 -0400378}
379
Don Zickusd181dd02015-05-13 13:22:13 -0400380static ssize_t partition_name_show(struct device *dev,
381 struct device_attribute *attr,
382 char *buf) {
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400383 struct visor_device *vdev = to_visor_device(dev);
Erik Arfvidson37039872015-05-05 18:36:00 -0400384
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400385 return snprintf(buf, PAGE_SIZE, "%s\n", vdev->name);
Erik Arfvidson37039872015-05-05 18:36:00 -0400386}
387
Don Zickusd181dd02015-05-13 13:22:13 -0400388static ssize_t channel_addr_show(struct device *dev,
389 struct device_attribute *attr,
390 char *buf) {
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400391 struct visor_device *vdev = to_visor_device(dev);
392 u64 addr = visorchannel_get_physaddr(vdev->visorchannel);
Erik Arfvidson37039872015-05-05 18:36:00 -0400393
Erik Arfvidson8a1cca32016-05-06 13:11:18 -0400394 return snprintf(buf, PAGE_SIZE, "0x%llx\n", addr);
Erik Arfvidson37039872015-05-05 18:36:00 -0400395}
396
Don Zickusd181dd02015-05-13 13:22:13 -0400397static ssize_t channel_bytes_show(struct device *dev,
398 struct device_attribute *attr,
399 char *buf) {
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400400 struct visor_device *vdev = to_visor_device(dev);
401 u64 nbytes = visorchannel_get_nbytes(vdev->visorchannel);
Erik Arfvidson37039872015-05-05 18:36:00 -0400402
Erik Arfvidson8a1cca32016-05-06 13:11:18 -0400403 return snprintf(buf, PAGE_SIZE, "0x%llx\n", nbytes);
Erik Arfvidson37039872015-05-05 18:36:00 -0400404}
405
Don Zickusd181dd02015-05-13 13:22:13 -0400406static ssize_t channel_id_show(struct device *dev,
407 struct device_attribute *attr,
408 char *buf) {
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400409 struct visor_device *vdev = to_visor_device(dev);
Erik Arfvidson37039872015-05-05 18:36:00 -0400410 int len = 0;
411
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400412 if (vdev->visorchannel) {
413 visorchannel_id(vdev->visorchannel, buf);
Erik Arfvidson37039872015-05-05 18:36:00 -0400414 len = strlen(buf);
415 buf[len++] = '\n';
416 }
417 return len;
418}
419
Don Zickusd181dd02015-05-13 13:22:13 -0400420static ssize_t client_bus_info_show(struct device *dev,
421 struct device_attribute *attr,
422 char *buf) {
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400423 struct visor_device *vdev = to_visor_device(dev);
424 struct visorchannel *channel = vdev->visorchannel;
425
Alexander Curtina22f57c2016-03-23 22:15:52 -0400426 int i, shift, remain = PAGE_SIZE;
Erik Arfvidson37039872015-05-05 18:36:00 -0400427 unsigned long off;
Alexander Curtin8d7da1d2016-03-23 22:15:51 -0400428 char *pos = buf;
Erik Arfvidson37039872015-05-05 18:36:00 -0400429 u8 *partition_name;
430 struct ultra_vbus_deviceinfo dev_info;
431
432 partition_name = "";
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400433 if (channel) {
434 if (vdev->name)
435 partition_name = vdev->name;
Alexander Curtina22f57c2016-03-23 22:15:52 -0400436 shift = snprintf(pos, remain,
437 "Client device / client driver info for %s eartition (vbus #%d):\n",
438 partition_name, vdev->chipset_dev_no);
439 pos += shift;
440 remain -= shift;
441 shift = visorchannel_read(channel,
442 offsetof(struct
443 spar_vbus_channel_protocol,
444 chp_info),
445 &dev_info, sizeof(dev_info));
446 if (shift >= 0) {
447 shift = vbuschannel_devinfo_to_string(&dev_info, pos,
448 remain, -1);
449 pos += shift;
450 remain -= shift;
Erik Arfvidson37039872015-05-05 18:36:00 -0400451 }
Alexander Curtina22f57c2016-03-23 22:15:52 -0400452 shift = visorchannel_read(channel,
453 offsetof(struct
454 spar_vbus_channel_protocol,
455 bus_info),
456 &dev_info, sizeof(dev_info));
457 if (shift >= 0) {
458 shift = vbuschannel_devinfo_to_string(&dev_info, pos,
459 remain, -1);
460 pos += shift;
461 remain -= shift;
Erik Arfvidson37039872015-05-05 18:36:00 -0400462 }
463 off = offsetof(struct spar_vbus_channel_protocol, dev_info);
464 i = 0;
465 while (off + sizeof(dev_info) <=
Don Zickus5ecbd5d2015-05-13 13:22:23 -0400466 visorchannel_get_nbytes(channel)) {
Alexander Curtina22f57c2016-03-23 22:15:52 -0400467 shift = visorchannel_read(channel,
468 off, &dev_info,
469 sizeof(dev_info));
470 if (shift >= 0) {
471 shift = vbuschannel_devinfo_to_string
Alexander Curtin8d7da1d2016-03-23 22:15:51 -0400472 (&dev_info, pos, remain, i);
Alexander Curtina22f57c2016-03-23 22:15:52 -0400473 pos += shift;
474 remain -= shift;
Erik Arfvidson37039872015-05-05 18:36:00 -0400475 }
476 off += sizeof(dev_info);
477 i++;
478 }
479 }
480 return PAGE_SIZE - remain;
481}
482
Don Zickusd181dd02015-05-13 13:22:13 -0400483static DEVICE_ATTR_RO(partition_handle);
484static DEVICE_ATTR_RO(partition_guid);
485static DEVICE_ATTR_RO(partition_name);
486static DEVICE_ATTR_RO(channel_addr);
487static DEVICE_ATTR_RO(channel_bytes);
488static DEVICE_ATTR_RO(channel_id);
489static DEVICE_ATTR_RO(client_bus_info);
Erik Arfvidson37039872015-05-05 18:36:00 -0400490
Don Zickusd181dd02015-05-13 13:22:13 -0400491static struct attribute *dev_attrs[] = {
492 &dev_attr_partition_handle.attr,
493 &dev_attr_partition_guid.attr,
494 &dev_attr_partition_name.attr,
495 &dev_attr_channel_addr.attr,
496 &dev_attr_channel_bytes.attr,
497 &dev_attr_channel_id.attr,
498 &dev_attr_client_bus_info.attr,
499 NULL
500};
Erik Arfvidson37039872015-05-05 18:36:00 -0400501
Don Zickusd181dd02015-05-13 13:22:13 -0400502static struct attribute_group dev_attr_grp = {
503 .attrs = dev_attrs,
504};
Erik Arfvidson37039872015-05-05 18:36:00 -0400505
Don Zickusd181dd02015-05-13 13:22:13 -0400506static const struct attribute_group *visorbus_groups[] = {
507 &dev_attr_grp,
508 NULL
509};
Erik Arfvidson37039872015-05-05 18:36:00 -0400510
511/* DRIVER attributes
512 *
513 * define & implement display of driver attributes under
514 * /sys/bus/visorbus/drivers/<drivername>.
515 *
516 */
517
518static ssize_t
519DRIVER_ATTR_version(struct device_driver *xdrv, char *buf)
520{
521 struct visor_driver *drv = to_visor_driver(xdrv);
522
523 return snprintf(buf, PAGE_SIZE, "%s\n", drv->version);
524}
525
526static int
527register_driver_attributes(struct visor_driver *drv)
528{
Erik Arfvidson37039872015-05-05 18:36:00 -0400529 struct driver_attribute version =
530 __ATTR(version, S_IRUGO, DRIVER_ATTR_version, NULL);
531 drv->version_attr = version;
Bhaktipriya Shridhar2cda64c2016-02-25 17:26:55 +0530532 return driver_create_file(&drv->driver, &drv->version_attr);
Erik Arfvidson37039872015-05-05 18:36:00 -0400533}
534
535static void
536unregister_driver_attributes(struct visor_driver *drv)
537{
538 driver_remove_file(&drv->driver, &drv->version_attr);
539}
540
Erik Arfvidson37039872015-05-05 18:36:00 -0400541static void
542dev_periodic_work(void *xdev)
543{
Shraddha Barke0df4e3e2015-08-10 13:30:35 +0530544 struct visor_device *dev = xdev;
Erik Arfvidson37039872015-05-05 18:36:00 -0400545 struct visor_driver *drv = to_visor_driver(dev->device.driver);
546
547 down(&dev->visordriver_callback_lock);
548 if (drv->channel_interrupt)
549 drv->channel_interrupt(dev);
550 up(&dev->visordriver_callback_lock);
551 if (!visor_periodic_work_nextperiod(dev->periodic_work))
552 put_device(&dev->device);
553}
554
555static void
556dev_start_periodic_work(struct visor_device *dev)
557{
558 if (dev->being_removed)
559 return;
560 /* now up by at least 2 */
561 get_device(&dev->device);
562 if (!visor_periodic_work_start(dev->periodic_work))
563 put_device(&dev->device);
564}
565
566static void
567dev_stop_periodic_work(struct visor_device *dev)
568{
569 if (visor_periodic_work_stop(dev->periodic_work))
570 put_device(&dev->device);
571}
572
573/** This is called automatically upon adding a visor_device (device_add), or
574 * adding a visor_driver (visorbus_register_visor_driver), but only after
575 * visorbus_match has returned 1 to indicate a successful match between
576 * driver and device.
577 */
578static int
579visordriver_probe_device(struct device *xdev)
580{
David Kershner40006222016-03-12 21:27:08 -0500581 int res;
Erik Arfvidson37039872015-05-05 18:36:00 -0400582 struct visor_driver *drv;
583 struct visor_device *dev;
584
585 drv = to_visor_driver(xdev->driver);
586 dev = to_visor_device(xdev);
David Kershner40006222016-03-12 21:27:08 -0500587
588 if (!drv->probe)
589 return -ENODEV;
590
Erik Arfvidson37039872015-05-05 18:36:00 -0400591 down(&dev->visordriver_callback_lock);
Prarit Bhargava779d0752015-05-05 18:37:01 -0400592 dev->being_removed = false;
Erik Arfvidson37039872015-05-05 18:36:00 -0400593
David Kershner40006222016-03-12 21:27:08 -0500594 res = drv->probe(dev);
595 if (res >= 0) {
596 /* success: reference kept via unmatched get_device() */
597 get_device(&dev->device);
598 fix_vbus_dev_info(dev);
599 }
600
Erik Arfvidson37039872015-05-05 18:36:00 -0400601 up(&dev->visordriver_callback_lock);
David Kershner40006222016-03-12 21:27:08 -0500602 return res;
Erik Arfvidson37039872015-05-05 18:36:00 -0400603}
604
605/** This is called when device_unregister() is called for each child device
606 * instance, to notify the appropriate visorbus_driver that the device is
607 * going away, and to decrease the reference count of the device.
608 */
609static int
610visordriver_remove_device(struct device *xdev)
611{
Erik Arfvidson37039872015-05-05 18:36:00 -0400612 struct visor_device *dev;
613 struct visor_driver *drv;
614
615 dev = to_visor_device(xdev);
616 drv = to_visor_driver(xdev->driver);
617 down(&dev->visordriver_callback_lock);
Prarit Bhargava779d0752015-05-05 18:37:01 -0400618 dev->being_removed = true;
David Kershner64938182016-03-30 20:38:49 -0400619 if (drv->remove)
620 drv->remove(dev);
Erik Arfvidson37039872015-05-05 18:36:00 -0400621 up(&dev->visordriver_callback_lock);
622 dev_stop_periodic_work(dev);
Erik Arfvidson37039872015-05-05 18:36:00 -0400623
624 put_device(&dev->device);
Abdul Hussaindf7f46e2015-06-11 10:03:49 +0000625 return 0;
Erik Arfvidson37039872015-05-05 18:36:00 -0400626}
627
628/** A particular type of visor driver calls this function to register
629 * the driver. The caller MUST fill in the following fields within the
630 * #drv structure:
631 * name, version, owner, channel_types, probe, remove
632 *
633 * Here's how the whole Linux bus / driver / device model works.
634 *
635 * At system start-up, the visorbus kernel module is loaded, which registers
636 * visorbus_type as a bus type, using bus_register().
637 *
638 * All kernel modules that support particular device types on a
639 * visorbus bus are loaded. Each of these kernel modules calls
640 * visorbus_register_visor_driver() in their init functions, passing a
641 * visor_driver struct. visorbus_register_visor_driver() in turn calls
642 * register_driver(&visor_driver.driver). This .driver member is
643 * initialized with generic methods (like probe), whose sole responsibility
644 * is to act as a broker for the real methods, which are within the
645 * visor_driver struct. (This is the way the subclass behavior is
646 * implemented, since visor_driver is essentially a subclass of the
647 * generic driver.) Whenever a driver_register() happens, core bus code in
648 * the kernel does (see device_attach() in drivers/base/dd.c):
649 *
650 * for each dev associated with the bus (the bus that driver is on) that
651 * does not yet have a driver
652 * if bus.match(dev,newdriver) == yes_matched ** .match specified
653 * ** during bus_register().
654 * newdriver.probe(dev) ** for visor drivers, this will call
655 * ** the generic driver.probe implemented in visorbus.c,
656 * ** which in turn calls the probe specified within the
657 * ** struct visor_driver (which was specified by the
658 * ** actual device driver as part of
659 * ** visorbus_register_visor_driver()).
660 *
661 * The above dance also happens when a new device appears.
662 * So the question is, how are devices created within the system?
663 * Basically, just call device_add(dev). See pci_bus_add_devices().
664 * pci_scan_device() shows an example of how to build a device struct. It
665 * returns the newly-created struct to pci_scan_single_device(), who adds it
666 * to the list of devices at PCIBUS.devices. That list of devices is what
667 * is traversed by pci_bus_add_devices().
668 *
669 */
670int visorbus_register_visor_driver(struct visor_driver *drv)
671{
672 int rc = 0;
673
Benjamin Romer6155a3c2015-09-04 12:01:32 -0400674 if (busreg_rc < 0)
675 return -ENODEV; /*can't register on a nonexistent bus*/
676
Erik Arfvidson37039872015-05-05 18:36:00 -0400677 drv->driver.name = drv->name;
678 drv->driver.bus = &visorbus_type;
679 drv->driver.probe = visordriver_probe_device;
680 drv->driver.remove = visordriver_remove_device;
681 drv->driver.owner = drv->owner;
682
683 /* driver_register does this:
684 * bus_add_driver(drv)
685 * ->if (drv.bus) ** (bus_type) **
686 * driver_attach(drv)
687 * for each dev with bus type of drv.bus
688 * if (!dev.drv) ** no driver assigned yet **
689 * if (bus.match(dev,drv)) [visorbus_match]
690 * dev.drv = drv
691 * if (!drv.probe(dev)) [visordriver_probe_device]
692 * dev.drv = NULL
693 */
694
695 rc = driver_register(&drv->driver);
696 if (rc < 0)
697 return rc;
698 rc = register_driver_attributes(drv);
David Kershner418627d2015-09-04 12:01:31 -0400699 if (rc < 0)
700 driver_unregister(&drv->driver);
Erik Arfvidson37039872015-05-05 18:36:00 -0400701 return rc;
702}
703EXPORT_SYMBOL_GPL(visorbus_register_visor_driver);
704
705/** A particular type of visor driver calls this function to unregister
706 * the driver, i.e., within its module_exit function.
707 */
708void
709visorbus_unregister_visor_driver(struct visor_driver *drv)
710{
711 unregister_driver_attributes(drv);
712 driver_unregister(&drv->driver);
713}
714EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver);
715
716int
717visorbus_read_channel(struct visor_device *dev, unsigned long offset,
718 void *dest, unsigned long nbytes)
719{
720 return visorchannel_read(dev->visorchannel, offset, dest, nbytes);
721}
722EXPORT_SYMBOL_GPL(visorbus_read_channel);
723
724int
725visorbus_write_channel(struct visor_device *dev, unsigned long offset,
726 void *src, unsigned long nbytes)
727{
728 return visorchannel_write(dev->visorchannel, offset, src, nbytes);
729}
730EXPORT_SYMBOL_GPL(visorbus_write_channel);
731
732int
733visorbus_clear_channel(struct visor_device *dev, unsigned long offset, u8 ch,
734 unsigned long nbytes)
735{
736 return visorchannel_clear(dev->visorchannel, offset, ch, nbytes);
737}
738EXPORT_SYMBOL_GPL(visorbus_clear_channel);
739
Erik Arfvidson37039872015-05-05 18:36:00 -0400740/** We don't really have a real interrupt, so for now we just call the
741 * interrupt function periodically...
742 */
743void
744visorbus_enable_channel_interrupts(struct visor_device *dev)
745{
746 dev_start_periodic_work(dev);
747}
748EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts);
749
750void
751visorbus_disable_channel_interrupts(struct visor_device *dev)
752{
753 dev_stop_periodic_work(dev);
754}
755EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts);
756
757/** This is how everything starts from the device end.
758 * This function is called when a channel first appears via a ControlVM
759 * message. In response, this function allocates a visor_device to
760 * correspond to the new channel, and attempts to connect it the appropriate
761 * driver. If the appropriate driver is found, the visor_driver.probe()
762 * function for that driver will be called, and will be passed the new
763 * visor_device that we just created.
764 *
765 * It's ok if the appropriate driver is not yet loaded, because in that case
766 * the new device struct will just stick around in the bus' list of devices.
767 * When the appropriate driver calls visorbus_register_visor_driver(), the
768 * visor_driver.probe() for the new driver will be called with the new
769 * device.
770 */
771static int
Don Zickusa298bc02015-06-04 09:22:42 -0400772create_visor_device(struct visor_device *dev)
Erik Arfvidson37039872015-05-05 18:36:00 -0400773{
David Kershner0b2acf32016-03-12 21:27:12 -0500774 int err;
Don Zickusa298bc02015-06-04 09:22:42 -0400775 u32 chipset_bus_no = dev->chipset_bus_no;
776 u32 chipset_dev_no = dev->chipset_dev_no;
Erik Arfvidson37039872015-05-05 18:36:00 -0400777
778 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, chipset_dev_no, chipset_bus_no,
779 POSTCODE_SEVERITY_INFO);
Erik Arfvidson37039872015-05-05 18:36:00 -0400780
Erik Arfvidson37039872015-05-05 18:36:00 -0400781 sema_init(&dev->visordriver_callback_lock, 1); /* unlocked */
782 dev->device.bus = &visorbus_type;
Prarit Bhargava59fd2c82015-07-24 12:06:53 -0400783 dev->device.groups = visorbus_channel_groups;
Erik Arfvidson37039872015-05-05 18:36:00 -0400784 device_initialize(&dev->device);
785 dev->device.release = visorbus_release_device;
786 /* keep a reference just for us (now 2) */
787 get_device(&dev->device);
Erik Arfvidson37039872015-05-05 18:36:00 -0400788 dev->periodic_work =
789 visor_periodic_work_create(POLLJIFFIES_NORMALCHANNEL,
790 periodic_dev_workqueue,
791 dev_periodic_work,
792 dev, dev_name(&dev->device));
793 if (!dev->periodic_work) {
794 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, chipset_dev_no,
795 DIAG_SEVERITY_ERR);
David Kershner0b2acf32016-03-12 21:27:12 -0500796 err = -EINVAL;
797 goto err_put;
Erik Arfvidson37039872015-05-05 18:36:00 -0400798 }
799
800 /* bus_id must be a unique name with respect to this bus TYPE
801 * (NOT bus instance). That's why we need to include the bus
802 * number within the name.
803 */
Don Zickusb4b598f2015-05-13 13:22:25 -0400804 dev_set_name(&dev->device, "vbus%u:dev%u",
Erik Arfvidson37039872015-05-05 18:36:00 -0400805 chipset_bus_no, chipset_dev_no);
806
807 /* device_add does this:
808 * bus_add_device(dev)
809 * ->device_attach(dev)
810 * ->for each driver drv registered on the bus that dev is on
811 * if (dev.drv) ** device already has a driver **
812 * ** not sure we could ever get here... **
813 * else
814 * if (bus.match(dev,drv)) [visorbus_match]
815 * dev.drv = drv
816 * if (!drv.probe(dev)) [visordriver_probe_device]
817 * dev.drv = NULL
818 *
819 * Note that device_add does NOT fail if no driver failed to
820 * claim the device. The device will be linked onto
821 * bus_type.klist_devices regardless (use bus_for_each_dev).
822 */
David Kershner0b2acf32016-03-12 21:27:12 -0500823 err = device_add(&dev->device);
824 if (err < 0) {
Erik Arfvidson37039872015-05-05 18:36:00 -0400825 POSTCODE_LINUX_3(DEVICE_ADD_PC, chipset_bus_no,
826 DIAG_SEVERITY_ERR);
David Kershner0b2acf32016-03-12 21:27:12 -0500827 goto err_put;
Erik Arfvidson37039872015-05-05 18:36:00 -0400828 }
829
Don Zickusa298bc02015-06-04 09:22:42 -0400830 list_add_tail(&dev->list_all, &list_all_device_instances);
David Kershner0b2acf32016-03-12 21:27:12 -0500831 return 0; /* success: reference kept via unmatched get_device() */
Erik Arfvidson37039872015-05-05 18:36:00 -0400832
David Kershner0b2acf32016-03-12 21:27:12 -0500833err_put:
Don Zickusa298bc02015-06-04 09:22:42 -0400834 put_device(&dev->device);
David Kershner0b2acf32016-03-12 21:27:12 -0500835 return err;
Erik Arfvidson37039872015-05-05 18:36:00 -0400836}
837
838static void
839remove_visor_device(struct visor_device *dev)
840{
841 list_del(&dev->list_all);
Erik Arfvidson37039872015-05-05 18:36:00 -0400842 put_device(&dev->device);
843 device_unregister(&dev->device);
844}
845
Erik Arfvidson37039872015-05-05 18:36:00 -0400846static int
Erik Arfvidson37039872015-05-05 18:36:00 -0400847get_vbus_header_info(struct visorchannel *chan,
848 struct spar_vbus_headerinfo *hdr_info)
849{
Erik Arfvidson37039872015-05-05 18:36:00 -0400850 if (!SPAR_VBUS_CHANNEL_OK_CLIENT(visorchannel_get_header(chan)))
Benjamin Romerf748f642016-02-23 10:01:50 -0500851 return -EINVAL;
852
Erik Arfvidson37039872015-05-05 18:36:00 -0400853 if (visorchannel_read(chan, sizeof(struct channel_header), hdr_info,
854 sizeof(*hdr_info)) < 0) {
Benjamin Romerf748f642016-02-23 10:01:50 -0500855 return -EIO;
Erik Arfvidson37039872015-05-05 18:36:00 -0400856 }
857 if (hdr_info->struct_bytes < sizeof(struct spar_vbus_headerinfo))
Benjamin Romerf748f642016-02-23 10:01:50 -0500858 return -EINVAL;
859
Erik Arfvidson37039872015-05-05 18:36:00 -0400860 if (hdr_info->device_info_struct_bytes <
861 sizeof(struct ultra_vbus_deviceinfo)) {
Benjamin Romerf748f642016-02-23 10:01:50 -0500862 return -EINVAL;
Erik Arfvidson37039872015-05-05 18:36:00 -0400863 }
Benjamin Romerf748f642016-02-23 10:01:50 -0500864 return 0;
Erik Arfvidson37039872015-05-05 18:36:00 -0400865}
866
867/* Write the contents of <info> to the struct
Gavin O'Leary48117892015-11-09 20:12:00 -0800868 * spar_vbus_channel_protocol.chp_info.
869 */
Erik Arfvidson37039872015-05-05 18:36:00 -0400870
871static int
872write_vbus_chp_info(struct visorchannel *chan,
873 struct spar_vbus_headerinfo *hdr_info,
874 struct ultra_vbus_deviceinfo *info)
875{
876 int off = sizeof(struct channel_header) + hdr_info->chp_info_offset;
877
878 if (hdr_info->chp_info_offset == 0)
Christophe JAILLET216c3e22015-07-13 20:52:26 +0200879 return -1;
Erik Arfvidson37039872015-05-05 18:36:00 -0400880
881 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
Christophe JAILLET216c3e22015-07-13 20:52:26 +0200882 return -1;
Erik Arfvidson37039872015-05-05 18:36:00 -0400883 return 0;
884}
885
886/* Write the contents of <info> to the struct
Gavin O'Leary48117892015-11-09 20:12:00 -0800887 * spar_vbus_channel_protocol.bus_info.
888 */
Erik Arfvidson37039872015-05-05 18:36:00 -0400889
890static int
891write_vbus_bus_info(struct visorchannel *chan,
892 struct spar_vbus_headerinfo *hdr_info,
893 struct ultra_vbus_deviceinfo *info)
894{
895 int off = sizeof(struct channel_header) + hdr_info->bus_info_offset;
896
897 if (hdr_info->bus_info_offset == 0)
Christophe JAILLET216c3e22015-07-13 20:52:26 +0200898 return -1;
Erik Arfvidson37039872015-05-05 18:36:00 -0400899
900 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
Christophe JAILLET216c3e22015-07-13 20:52:26 +0200901 return -1;
Erik Arfvidson37039872015-05-05 18:36:00 -0400902 return 0;
903}
904
905/* Write the contents of <info> to the
906 * struct spar_vbus_channel_protocol.dev_info[<devix>].
907 */
908static int
909write_vbus_dev_info(struct visorchannel *chan,
910 struct spar_vbus_headerinfo *hdr_info,
911 struct ultra_vbus_deviceinfo *info, int devix)
912{
913 int off =
914 (sizeof(struct channel_header) + hdr_info->dev_info_offset) +
915 (hdr_info->device_info_struct_bytes * devix);
916
917 if (hdr_info->dev_info_offset == 0)
Christophe JAILLET216c3e22015-07-13 20:52:26 +0200918 return -1;
Erik Arfvidson37039872015-05-05 18:36:00 -0400919
920 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
Christophe JAILLET216c3e22015-07-13 20:52:26 +0200921 return -1;
Erik Arfvidson37039872015-05-05 18:36:00 -0400922 return 0;
923}
924
925/* For a child device just created on a client bus, fill in
926 * information about the driver that is controlling this device into
927 * the the appropriate slot within the vbus channel of the bus
928 * instance.
929 */
930static void
931fix_vbus_dev_info(struct visor_device *visordev)
932{
933 int i;
Don Zickusd32517e2015-06-04 09:22:41 -0400934 struct visor_device *bdev;
Erik Arfvidson37039872015-05-05 18:36:00 -0400935 struct visor_driver *visordrv;
936 int bus_no = visordev->chipset_bus_no;
937 int dev_no = visordev->chipset_dev_no;
938 struct ultra_vbus_deviceinfo dev_info;
939 const char *chan_type_name = NULL;
Don Zickus7726f812015-06-01 13:00:28 -0400940 struct spar_vbus_headerinfo *hdr_info;
Erik Arfvidson37039872015-05-05 18:36:00 -0400941
942 if (!visordev->device.driver)
Christophe JAILLET216c3e22015-07-13 20:52:26 +0200943 return;
Erik Arfvidson37039872015-05-05 18:36:00 -0400944
Don Zickus7726f812015-06-01 13:00:28 -0400945 hdr_info = (struct spar_vbus_headerinfo *)visordev->vbus_hdr_info;
Don Zickusd32517e2015-06-04 09:22:41 -0400946 if (!hdr_info)
947 return;
948
949 bdev = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
950 if (!bdev)
951 return;
Don Zickus7726f812015-06-01 13:00:28 -0400952
Erik Arfvidson37039872015-05-05 18:36:00 -0400953 visordrv = to_visor_driver(visordev->device.driver);
Erik Arfvidson37039872015-05-05 18:36:00 -0400954
955 /* Within the list of device types (by GUID) that the driver
956 * says it supports, find out which one of those types matches
957 * the type of this device, so that we can include the device
958 * type name
959 */
960 for (i = 0; visordrv->channel_types[i].name; i++) {
Erik Arfvidsond5b3f1d2015-05-05 18:37:04 -0400961 if (memcmp(&visordrv->channel_types[i].guid,
962 &visordev->channel_type_guid,
963 sizeof(visordrv->channel_types[i].guid)) == 0) {
Erik Arfvidson37039872015-05-05 18:36:00 -0400964 chan_type_name = visordrv->channel_types[i].name;
965 break;
966 }
967 }
968
969 bus_device_info_init(&dev_info, chan_type_name,
970 visordrv->name, visordrv->version,
971 visordrv->vertag);
Don Zickusd32517e2015-06-04 09:22:41 -0400972 write_vbus_dev_info(bdev->visorchannel, hdr_info, &dev_info, dev_no);
Erik Arfvidson37039872015-05-05 18:36:00 -0400973
974 /* Re-write bus+chipset info, because it is possible that this
975 * was previously written by our evil counterpart, virtpci.
976 */
Don Zickusd32517e2015-06-04 09:22:41 -0400977 write_vbus_chp_info(bdev->visorchannel, hdr_info, &chipset_driverinfo);
978 write_vbus_bus_info(bdev->visorchannel, hdr_info,
979 &clientbus_driverinfo);
Erik Arfvidson37039872015-05-05 18:36:00 -0400980}
981
982/** Create a device instance for the visor bus itself.
983 */
Don Zickusd32517e2015-06-04 09:22:41 -0400984static int
985create_bus_instance(struct visor_device *dev)
Erik Arfvidson37039872015-05-05 18:36:00 -0400986{
Don Zickusd32517e2015-06-04 09:22:41 -0400987 int id = dev->chipset_bus_no;
Don Zickus7726f812015-06-01 13:00:28 -0400988 struct spar_vbus_headerinfo *hdr_info;
Erik Arfvidson37039872015-05-05 18:36:00 -0400989
990 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
Don Zickus7726f812015-06-01 13:00:28 -0400991
992 hdr_info = kzalloc(sizeof(*hdr_info), GFP_KERNEL);
Benjamin Romerc2c667d2016-02-23 10:01:52 -0500993 if (!hdr_info)
994 return -ENOMEM;
Don Zickus7726f812015-06-01 13:00:28 -0400995
Don Zickus343506b2015-06-04 09:22:37 -0400996 dev_set_name(&dev->device, "visorbus%d", id);
997 dev->device.bus = &visorbus_type;
998 dev->device.groups = visorbus_groups;
999 dev->device.release = visorbus_release_busdevice;
Don Zickusd32517e2015-06-04 09:22:41 -04001000
Don Zickus343506b2015-06-04 09:22:37 -04001001 if (device_register(&dev->device) < 0) {
Don Zickus03274d32015-05-13 13:22:12 -04001002 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, id,
1003 POSTCODE_SEVERITY_ERR);
Benjamin Romerc2c667d2016-02-23 10:01:52 -05001004 kfree(hdr_info);
1005 return -ENODEV;
Don Zickus03274d32015-05-13 13:22:12 -04001006 }
Don Zickusd32517e2015-06-04 09:22:41 -04001007
Don Zickusee983d92015-06-04 09:22:39 -04001008 if (get_vbus_header_info(dev->visorchannel, hdr_info) >= 0) {
1009 dev->vbus_hdr_info = (void *)hdr_info;
1010 write_vbus_chp_info(dev->visorchannel, hdr_info,
1011 &chipset_driverinfo);
1012 write_vbus_bus_info(dev->visorchannel, hdr_info,
1013 &clientbus_driverinfo);
Don Zickusb32c4992015-06-01 13:00:26 -04001014 } else {
Don Zickusee983d92015-06-04 09:22:39 -04001015 kfree(hdr_info);
Erik Arfvidson37039872015-05-05 18:36:00 -04001016 }
Erik Arfvidson37039872015-05-05 18:36:00 -04001017 bus_count++;
Don Zickus343506b2015-06-04 09:22:37 -04001018 list_add_tail(&dev->list_all, &list_all_bus_instances);
Don Zickus343506b2015-06-04 09:22:37 -04001019 dev_set_drvdata(&dev->device, dev);
Don Zickusd32517e2015-06-04 09:22:41 -04001020 return 0;
Erik Arfvidson37039872015-05-05 18:36:00 -04001021}
1022
1023/** Remove a device instance for the visor bus itself.
1024 */
1025static void
Don Zickus343506b2015-06-04 09:22:37 -04001026remove_bus_instance(struct visor_device *dev)
Erik Arfvidson37039872015-05-05 18:36:00 -04001027{
1028 /* Note that this will result in the release method for
Don Zickus343506b2015-06-04 09:22:37 -04001029 * dev->dev being called, which will call
Erik Arfvidson37039872015-05-05 18:36:00 -04001030 * visorbus_release_busdevice(). This has something to do with
1031 * the put_device() done in device_unregister(), but I have never
1032 * successfully been able to trace thru the code to see where/how
1033 * release() gets called. But I know it does.
1034 */
Erik Arfvidson37039872015-05-05 18:36:00 -04001035 bus_count--;
Don Zickus343506b2015-06-04 09:22:37 -04001036 if (dev->visorchannel) {
1037 visorchannel_destroy(dev->visorchannel);
1038 dev->visorchannel = NULL;
Erik Arfvidson37039872015-05-05 18:36:00 -04001039 }
Don Zickus343506b2015-06-04 09:22:37 -04001040 kfree(dev->vbus_hdr_info);
1041 list_del(&dev->list_all);
1042 device_unregister(&dev->device);
Erik Arfvidson37039872015-05-05 18:36:00 -04001043}
1044
1045/** Create and register the one-and-only one instance of
1046 * the visor bus type (visorbus_type).
1047 */
1048static int
1049create_bus_type(void)
1050{
Benjamin Romer6155a3c2015-09-04 12:01:32 -04001051 busreg_rc = bus_register(&visorbus_type);
1052 return busreg_rc;
Erik Arfvidson37039872015-05-05 18:36:00 -04001053}
1054
1055/** Remove the one-and-only one instance of the visor bus type (visorbus_type).
1056 */
1057static void
1058remove_bus_type(void)
1059{
Erik Arfvidson37039872015-05-05 18:36:00 -04001060 bus_unregister(&visorbus_type);
1061}
1062
1063/** Remove all child visor bus device instances.
1064 */
1065static void
1066remove_all_visor_devices(void)
1067{
1068 struct list_head *listentry, *listtmp;
1069
1070 list_for_each_safe(listentry, listtmp, &list_all_device_instances) {
1071 struct visor_device *dev = list_entry(listentry,
1072 struct visor_device,
1073 list_all);
1074 remove_visor_device(dev);
1075 }
1076}
1077
Erik Arfvidson37039872015-05-05 18:36:00 -04001078static void
Don Zickusd32517e2015-06-04 09:22:41 -04001079chipset_bus_create(struct visor_device *dev)
Erik Arfvidson37039872015-05-05 18:36:00 -04001080{
Don Zickusd32517e2015-06-04 09:22:41 -04001081 int rc;
1082 u32 bus_no = dev->chipset_bus_no;
Erik Arfvidson37039872015-05-05 18:36:00 -04001083
1084 POSTCODE_LINUX_3(BUS_CREATE_ENTRY_PC, bus_no, POSTCODE_SEVERITY_INFO);
Don Zickusd32517e2015-06-04 09:22:41 -04001085 rc = create_bus_instance(dev);
Erik Arfvidson37039872015-05-05 18:36:00 -04001086 POSTCODE_LINUX_3(BUS_CREATE_EXIT_PC, bus_no, POSTCODE_SEVERITY_INFO);
Don Zickusd32517e2015-06-04 09:22:41 -04001087
1088 if (rc < 0)
Erik Arfvidson37039872015-05-05 18:36:00 -04001089 POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, bus_no,
1090 POSTCODE_SEVERITY_ERR);
Don Zickusd32517e2015-06-04 09:22:41 -04001091 else
1092 POSTCODE_LINUX_3(CHIPSET_INIT_SUCCESS_PC, bus_no,
1093 POSTCODE_SEVERITY_INFO);
1094
Erik Arfvidson37039872015-05-05 18:36:00 -04001095 if (chipset_responders.bus_create)
Don Zickusd32517e2015-06-04 09:22:41 -04001096 (*chipset_responders.bus_create) (dev, rc);
Erik Arfvidson37039872015-05-05 18:36:00 -04001097}
1098
1099static void
Don Zickusd32517e2015-06-04 09:22:41 -04001100chipset_bus_destroy(struct visor_device *dev)
Erik Arfvidson37039872015-05-05 18:36:00 -04001101{
Don Zickus343506b2015-06-04 09:22:37 -04001102 remove_bus_instance(dev);
Erik Arfvidson37039872015-05-05 18:36:00 -04001103 if (chipset_responders.bus_destroy)
Don Zickusd32517e2015-06-04 09:22:41 -04001104 (*chipset_responders.bus_destroy)(dev, 0);
Erik Arfvidson37039872015-05-05 18:36:00 -04001105}
1106
1107static void
Don Zickusa298bc02015-06-04 09:22:42 -04001108chipset_device_create(struct visor_device *dev_info)
Erik Arfvidson37039872015-05-05 18:36:00 -04001109{
Benjamin Romer7a9749b2016-02-23 10:01:53 -05001110 int rc;
Don Zickusa298bc02015-06-04 09:22:42 -04001111 u32 bus_no = dev_info->chipset_bus_no;
1112 u32 dev_no = dev_info->chipset_dev_no;
Erik Arfvidson37039872015-05-05 18:36:00 -04001113
1114 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, dev_no, bus_no,
1115 POSTCODE_SEVERITY_INFO);
1116
Don Zickusa298bc02015-06-04 09:22:42 -04001117 rc = create_visor_device(dev_info);
David Kershner86ea8ac2015-06-12 16:46:08 -04001118 if (chipset_responders.device_create)
1119 chipset_responders.device_create(dev_info, rc);
1120
1121 if (rc < 0)
Erik Arfvidson37039872015-05-05 18:36:00 -04001122 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
1123 POSTCODE_SEVERITY_ERR);
David Kershner86ea8ac2015-06-12 16:46:08 -04001124 else
1125 POSTCODE_LINUX_4(DEVICE_CREATE_SUCCESS_PC, dev_no, bus_no,
1126 POSTCODE_SEVERITY_INFO);
Erik Arfvidson37039872015-05-05 18:36:00 -04001127}
1128
1129static void
Don Zickusa298bc02015-06-04 09:22:42 -04001130chipset_device_destroy(struct visor_device *dev_info)
Erik Arfvidson37039872015-05-05 18:36:00 -04001131{
Don Zickusa298bc02015-06-04 09:22:42 -04001132 remove_visor_device(dev_info);
Erik Arfvidson37039872015-05-05 18:36:00 -04001133
1134 if (chipset_responders.device_destroy)
Don Zickusa298bc02015-06-04 09:22:42 -04001135 (*chipset_responders.device_destroy) (dev_info, 0);
Erik Arfvidson37039872015-05-05 18:36:00 -04001136}
1137
1138/* This is the callback function specified for a function driver, to
1139 * be called when a pending "pause device" operation has been
1140 * completed.
1141 */
1142static void
Don Zickusa298bc02015-06-04 09:22:42 -04001143pause_state_change_complete(struct visor_device *dev, int status)
Erik Arfvidson37039872015-05-05 18:36:00 -04001144{
1145 if (!dev->pausing)
Christophe JAILLET216c3e22015-07-13 20:52:26 +02001146 return;
Erik Arfvidson37039872015-05-05 18:36:00 -04001147
Prarit Bhargava779d0752015-05-05 18:37:01 -04001148 dev->pausing = false;
Erik Arfvidson37039872015-05-05 18:36:00 -04001149 if (!chipset_responders.device_pause) /* this can never happen! */
Christophe JAILLET216c3e22015-07-13 20:52:26 +02001150 return;
Erik Arfvidson37039872015-05-05 18:36:00 -04001151
1152 /* Notify the chipset driver that the pause is complete, which
Erik Arfvidson7005c162016-02-08 10:41:49 -05001153 * will presumably want to send some sort of response to the
1154 * initiator.
1155 */
Don Zickusa298bc02015-06-04 09:22:42 -04001156 (*chipset_responders.device_pause) (dev, status);
Erik Arfvidson37039872015-05-05 18:36:00 -04001157}
1158
1159/* This is the callback function specified for a function driver, to
1160 * be called when a pending "resume device" operation has been
1161 * completed.
1162 */
1163static void
Don Zickusa298bc02015-06-04 09:22:42 -04001164resume_state_change_complete(struct visor_device *dev, int status)
Erik Arfvidson37039872015-05-05 18:36:00 -04001165{
1166 if (!dev->resuming)
Christophe JAILLET216c3e22015-07-13 20:52:26 +02001167 return;
Erik Arfvidson37039872015-05-05 18:36:00 -04001168
Prarit Bhargava779d0752015-05-05 18:37:01 -04001169 dev->resuming = false;
Erik Arfvidson37039872015-05-05 18:36:00 -04001170 if (!chipset_responders.device_resume) /* this can never happen! */
Christophe JAILLET216c3e22015-07-13 20:52:26 +02001171 return;
Erik Arfvidson37039872015-05-05 18:36:00 -04001172
1173 /* Notify the chipset driver that the resume is complete,
1174 * which will presumably want to send some sort of response to
Gavin O'Leary48117892015-11-09 20:12:00 -08001175 * the initiator.
1176 */
Don Zickusa298bc02015-06-04 09:22:42 -04001177 (*chipset_responders.device_resume) (dev, status);
Erik Arfvidson37039872015-05-05 18:36:00 -04001178}
1179
1180/* Tell the subordinate function driver for a specific device to pause
1181 * or resume that device. Result is returned asynchronously via a
1182 * callback function.
1183 */
1184static void
Don Zickusa298bc02015-06-04 09:22:42 -04001185initiate_chipset_device_pause_resume(struct visor_device *dev, bool is_pause)
Erik Arfvidson37039872015-05-05 18:36:00 -04001186{
Benjamin Romer03b93f02016-02-23 10:01:54 -05001187 int rc;
Erik Arfvidson37039872015-05-05 18:36:00 -04001188 struct visor_driver *drv = NULL;
Don Zickusa298bc02015-06-04 09:22:42 -04001189 void (*notify_func)(struct visor_device *dev, int response) = NULL;
Erik Arfvidson37039872015-05-05 18:36:00 -04001190
1191 if (is_pause)
1192 notify_func = chipset_responders.device_pause;
1193 else
1194 notify_func = chipset_responders.device_resume;
1195 if (!notify_func)
Benjamin Romer03b93f02016-02-23 10:01:54 -05001196 return;
Erik Arfvidson37039872015-05-05 18:36:00 -04001197
Erik Arfvidson37039872015-05-05 18:36:00 -04001198 drv = to_visor_driver(dev->device.driver);
Benjamin Romer03b93f02016-02-23 10:01:54 -05001199 if (!drv) {
1200 (*notify_func)(dev, -ENODEV);
1201 return;
1202 }
Erik Arfvidson37039872015-05-05 18:36:00 -04001203
Benjamin Romer03b93f02016-02-23 10:01:54 -05001204 if (dev->pausing || dev->resuming) {
1205 (*notify_func)(dev, -EBUSY);
1206 return;
1207 }
Erik Arfvidson37039872015-05-05 18:36:00 -04001208
1209 /* Note that even though both drv->pause() and drv->resume
1210 * specify a callback function, it is NOT necessary for us to
1211 * increment our local module usage count. Reason is, there
1212 * is already a linkage dependency between child function
1213 * drivers and visorbus, so it is already IMPOSSIBLE to unload
1214 * visorbus while child function drivers are still running.
1215 */
1216 if (is_pause) {
Benjamin Romer03b93f02016-02-23 10:01:54 -05001217 if (!drv->pause) {
1218 (*notify_func)(dev, -EINVAL);
1219 return;
1220 }
Erik Arfvidson37039872015-05-05 18:36:00 -04001221
Prarit Bhargava779d0752015-05-05 18:37:01 -04001222 dev->pausing = true;
Benjamin Romer03b93f02016-02-23 10:01:54 -05001223 rc = drv->pause(dev, pause_state_change_complete);
Erik Arfvidson37039872015-05-05 18:36:00 -04001224 } else {
1225 /* This should be done at BUS resume time, but an
1226 * existing problem prevents us from ever getting a bus
1227 * resume... This hack would fail to work should we
1228 * ever have a bus that contains NO devices, since we
Gavin O'Leary48117892015-11-09 20:12:00 -08001229 * would never even get here in that case.
1230 */
Erik Arfvidson37039872015-05-05 18:36:00 -04001231 fix_vbus_dev_info(dev);
Benjamin Romer03b93f02016-02-23 10:01:54 -05001232 if (!drv->resume) {
1233 (*notify_func)(dev, -EINVAL);
1234 return;
1235 }
Erik Arfvidson37039872015-05-05 18:36:00 -04001236
Prarit Bhargava779d0752015-05-05 18:37:01 -04001237 dev->resuming = true;
Benjamin Romer03b93f02016-02-23 10:01:54 -05001238 rc = drv->resume(dev, resume_state_change_complete);
Erik Arfvidson37039872015-05-05 18:36:00 -04001239 }
Benjamin Romer03b93f02016-02-23 10:01:54 -05001240 if (rc < 0) {
Erik Arfvidson37039872015-05-05 18:36:00 -04001241 if (is_pause)
Prarit Bhargava779d0752015-05-05 18:37:01 -04001242 dev->pausing = false;
Erik Arfvidson37039872015-05-05 18:36:00 -04001243 else
Prarit Bhargava779d0752015-05-05 18:37:01 -04001244 dev->resuming = false;
Benjamin Romer03b93f02016-02-23 10:01:54 -05001245 (*notify_func)(dev, -EINVAL);
Erik Arfvidson37039872015-05-05 18:36:00 -04001246 }
1247}
1248
1249static void
Don Zickusa298bc02015-06-04 09:22:42 -04001250chipset_device_pause(struct visor_device *dev_info)
Erik Arfvidson37039872015-05-05 18:36:00 -04001251{
Don Zickusb4b598f2015-05-13 13:22:25 -04001252 initiate_chipset_device_pause_resume(dev_info, true);
Erik Arfvidson37039872015-05-05 18:36:00 -04001253}
1254
1255static void
Don Zickusa298bc02015-06-04 09:22:42 -04001256chipset_device_resume(struct visor_device *dev_info)
Erik Arfvidson37039872015-05-05 18:36:00 -04001257{
Don Zickusb4b598f2015-05-13 13:22:25 -04001258 initiate_chipset_device_pause_resume(dev_info, false);
Erik Arfvidson37039872015-05-05 18:36:00 -04001259}
1260
1261struct channel_size_info {
1262 uuid_le guid;
1263 unsigned long min_size;
1264 unsigned long max_size;
1265};
1266
Prarit Bhargava55c67dc2015-05-05 18:37:02 -04001267int
Erik Arfvidson37039872015-05-05 18:36:00 -04001268visorbus_init(void)
1269{
David Kershner78af1ae2016-03-12 21:27:10 -05001270 int err;
Erik Arfvidson37039872015-05-05 18:36:00 -04001271
David Kershner78af1ae2016-03-12 21:27:10 -05001272 POSTCODE_LINUX_3(DRIVER_ENTRY_PC, 0, POSTCODE_SEVERITY_INFO);
Erik Arfvidson37039872015-05-05 18:36:00 -04001273 bus_device_info_init(&clientbus_driverinfo,
Erik Arfvidson46168812015-05-05 18:36:14 -04001274 "clientbus", "visorbus",
Erik Arfvidson37039872015-05-05 18:36:00 -04001275 VERSION, NULL);
1276
David Kershner78af1ae2016-03-12 21:27:10 -05001277 err = create_bus_type();
1278 if (err < 0) {
Erik Arfvidson37039872015-05-05 18:36:00 -04001279 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC, DIAG_SEVERITY_ERR);
David Kershner78af1ae2016-03-12 21:27:10 -05001280 goto error;
Erik Arfvidson37039872015-05-05 18:36:00 -04001281 }
1282
1283 periodic_dev_workqueue = create_singlethread_workqueue("visorbus_dev");
1284 if (!periodic_dev_workqueue) {
1285 POSTCODE_LINUX_2(CREATE_WORKQUEUE_PC, DIAG_SEVERITY_ERR);
David Kershner78af1ae2016-03-12 21:27:10 -05001286 err = -ENOMEM;
1287 goto error;
Erik Arfvidson37039872015-05-05 18:36:00 -04001288 }
1289
1290 /* This enables us to receive notifications when devices appear for
1291 * which this service partition is to be a server for.
1292 */
David Kershner4da33362015-05-05 18:36:39 -04001293 visorchipset_register_busdev(&chipset_notifiers,
1294 &chipset_responders,
1295 &chipset_driverinfo);
Erik Arfvidson37039872015-05-05 18:36:00 -04001296
David Kershner78af1ae2016-03-12 21:27:10 -05001297 return 0;
Erik Arfvidson37039872015-05-05 18:36:00 -04001298
David Kershner78af1ae2016-03-12 21:27:10 -05001299error:
1300 POSTCODE_LINUX_3(CHIPSET_INIT_FAILURE_PC, err, POSTCODE_SEVERITY_ERR);
1301 return err;
Erik Arfvidson37039872015-05-05 18:36:00 -04001302}
1303
Prarit Bhargavac79b28f2015-05-05 18:36:15 -04001304void
Erik Arfvidson37039872015-05-05 18:36:00 -04001305visorbus_exit(void)
1306{
1307 struct list_head *listentry, *listtmp;
1308
David Kershner4da33362015-05-05 18:36:39 -04001309 visorchipset_register_busdev(NULL, NULL, NULL);
Erik Arfvidson37039872015-05-05 18:36:00 -04001310 remove_all_visor_devices();
1311
1312 flush_workqueue(periodic_dev_workqueue); /* better not be any work! */
1313 destroy_workqueue(periodic_dev_workqueue);
1314 periodic_dev_workqueue = NULL;
1315
1316 if (periodic_test_workqueue) {
1317 cancel_delayed_work(&periodic_work);
1318 flush_workqueue(periodic_test_workqueue);
1319 destroy_workqueue(periodic_test_workqueue);
1320 periodic_test_workqueue = NULL;
1321 }
1322
1323 list_for_each_safe(listentry, listtmp, &list_all_bus_instances) {
Don Zickus343506b2015-06-04 09:22:37 -04001324 struct visor_device *dev = list_entry(listentry,
Christophe JAILLET216c3e22015-07-13 20:52:26 +02001325 struct visor_device,
1326 list_all);
Don Zickus343506b2015-06-04 09:22:37 -04001327 remove_bus_instance(dev);
Erik Arfvidson37039872015-05-05 18:36:00 -04001328 }
1329 remove_bus_type();
1330}
1331
1332module_param_named(debug, visorbus_debug, int, S_IRUGO);
1333MODULE_PARM_DESC(visorbus_debug, "1 to debug");
Erik Arfvidson37039872015-05-05 18:36:00 -04001334
1335module_param_named(forcematch, visorbus_forcematch, int, S_IRUGO);
1336MODULE_PARM_DESC(visorbus_forcematch,
1337 "1 to force a successful dev <--> drv match");
Erik Arfvidson37039872015-05-05 18:36:00 -04001338
1339module_param_named(forcenomatch, visorbus_forcenomatch, int, S_IRUGO);
1340MODULE_PARM_DESC(visorbus_forcenomatch,
1341 "1 to force an UNsuccessful dev <--> drv match");
Erik Arfvidson37039872015-05-05 18:36:00 -04001342
Erik Arfvidson37039872015-05-05 18:36:00 -04001343module_param_named(debugref, visorbus_debugref, int, S_IRUGO);
1344MODULE_PARM_DESC(visorbus_debugref, "1 to debug reference counting");