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