]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/staging/unisys/visorbus/visorbus_main.c
Merge tag 'armsoc-dt' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[mirror_ubuntu-bionic-kernel.git] / drivers / staging / unisys / visorbus / visorbus_main.c
1 /* visorbus_main.c
2 *
3 * Copyright � 2010 - 2015 UNISYS CORPORATION
4 * All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for more
14 * details.
15 */
16
17 #include <linux/debugfs.h>
18 #include <linux/uuid.h>
19
20 #include "visorbus.h"
21 #include "visorbus_private.h"
22
23 #define MYDRVNAME "visorbus"
24
25 /* Display string that is guaranteed to be no longer the 99 characters*/
26 #define LINESIZE 99
27
28 #define CURRENT_FILE_PC VISOR_BUS_PC_visorbus_main_c
29 #define POLLJIFFIES_NORMALCHANNEL 10
30
31 static bool initialized; /* stores whether bus_registration was successful */
32 static struct dentry *visorbus_debugfs_dir;
33
34 /*
35 * DEVICE type attributes
36 *
37 * The modalias file will contain the guid of the device.
38 */
39 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
40 char *buf)
41 {
42 struct visor_device *vdev;
43 uuid_le guid;
44
45 vdev = to_visor_device(dev);
46 guid = visorchannel_get_uuid(vdev->visorchannel);
47 return sprintf(buf, "visorbus:%pUl\n", &guid);
48 }
49 static DEVICE_ATTR_RO(modalias);
50
51 static struct attribute *visorbus_dev_attrs[] = {
52 &dev_attr_modalias.attr,
53 NULL,
54 };
55
56 /* sysfs example for bridge-only sysfs files using device_type's */
57 static const struct attribute_group visorbus_dev_group = {
58 .attrs = visorbus_dev_attrs,
59 };
60
61 static const struct attribute_group *visorbus_dev_groups[] = {
62 &visorbus_dev_group,
63 NULL,
64 };
65
66 /* filled in with info about parent chipset driver when we register with it */
67 static struct ultra_vbus_deviceinfo chipset_driverinfo;
68 /* filled in with info about this driver, wrt it servicing client busses */
69 static struct ultra_vbus_deviceinfo clientbus_driverinfo;
70
71 /* list of visor_device structs, linked via .list_all */
72 static LIST_HEAD(list_all_bus_instances);
73 /* list of visor_device structs, linked via .list_all */
74 static LIST_HEAD(list_all_device_instances);
75
76 static int
77 visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env)
78 {
79 struct visor_device *dev;
80 uuid_le guid;
81
82 dev = to_visor_device(xdev);
83 guid = visorchannel_get_uuid(dev->visorchannel);
84
85 return add_uevent_var(env, "MODALIAS=visorbus:%pUl", &guid);
86 }
87
88 /*
89 * visorbus_match() - called automatically upon adding a visor_device
90 * (device_add), or adding a visor_driver
91 * (visorbus_register_visor_driver)
92 * @xdev: struct device for the device being matched
93 * @xdrv: struct device_driver for driver to match device against
94 *
95 * Return: 1 iff the provided driver can control the specified device
96 */
97 static int
98 visorbus_match(struct device *xdev, struct device_driver *xdrv)
99 {
100 uuid_le channel_type;
101 int i;
102 struct visor_device *dev;
103 struct visor_driver *drv;
104
105 dev = to_visor_device(xdev);
106 drv = to_visor_driver(xdrv);
107 channel_type = visorchannel_get_uuid(dev->visorchannel);
108
109 if (!drv->channel_types)
110 return 0;
111
112 for (i = 0;
113 (uuid_le_cmp(drv->channel_types[i].guid, NULL_UUID_LE) != 0) ||
114 (drv->channel_types[i].name);
115 i++)
116 if (uuid_le_cmp(drv->channel_types[i].guid,
117 channel_type) == 0)
118 return i + 1;
119
120 return 0;
121 }
122
123 /*
124 * This describes the TYPE of bus.
125 * (Don't confuse this with an INSTANCE of the bus.)
126 */
127 struct bus_type visorbus_type = {
128 .name = "visorbus",
129 .match = visorbus_match,
130 .uevent = visorbus_uevent,
131 .dev_groups = visorbus_dev_groups,
132 };
133
134 /*
135 * visorbus_release_busdevice() - called when device_unregister() is called for
136 * the bus device instance, after all other tasks
137 * involved with destroying the dev are complete
138 * @xdev: struct device for the bus being released
139 */
140 static void
141 visorbus_release_busdevice(struct device *xdev)
142 {
143 struct visor_device *dev = dev_get_drvdata(xdev);
144
145 debugfs_remove(dev->debugfs_client_bus_info);
146 debugfs_remove_recursive(dev->debugfs_dir);
147 kfree(dev);
148 }
149
150 /*
151 * visorbus_release_device() - called when device_unregister() is called for
152 * each child device instance
153 * @xdev: struct device for the visor device being released
154 */
155 static void
156 visorbus_release_device(struct device *xdev)
157 {
158 struct visor_device *dev = to_visor_device(xdev);
159
160 if (dev->visorchannel) {
161 visorchannel_destroy(dev->visorchannel);
162 dev->visorchannel = NULL;
163 }
164 kfree(dev);
165 }
166
167 /*
168 * begin implementation of specific channel attributes to appear under
169 * /sys/bus/visorbus<x>/dev<y>/channel
170 */
171
172 static ssize_t physaddr_show(struct device *dev, struct device_attribute *attr,
173 char *buf)
174 {
175 struct visor_device *vdev = to_visor_device(dev);
176
177 return sprintf(buf, "0x%llx\n",
178 visorchannel_get_physaddr(vdev->visorchannel));
179 }
180 static DEVICE_ATTR_RO(physaddr);
181
182 static ssize_t nbytes_show(struct device *dev, struct device_attribute *attr,
183 char *buf)
184 {
185 struct visor_device *vdev = to_visor_device(dev);
186
187 return sprintf(buf, "0x%lx\n",
188 visorchannel_get_nbytes(vdev->visorchannel));
189 }
190 static DEVICE_ATTR_RO(nbytes);
191
192 static ssize_t clientpartition_show(struct device *dev,
193 struct device_attribute *attr, char *buf)
194 {
195 struct visor_device *vdev = to_visor_device(dev);
196
197 return sprintf(buf, "0x%llx\n",
198 visorchannel_get_clientpartition(vdev->visorchannel));
199 }
200 static DEVICE_ATTR_RO(clientpartition);
201
202 static ssize_t typeguid_show(struct device *dev, struct device_attribute *attr,
203 char *buf)
204 {
205 struct visor_device *vdev = to_visor_device(dev);
206 char typeid[LINESIZE];
207
208 return sprintf(buf, "%s\n",
209 visorchannel_id(vdev->visorchannel, typeid));
210 }
211 static DEVICE_ATTR_RO(typeguid);
212
213 static ssize_t zoneguid_show(struct device *dev, struct device_attribute *attr,
214 char *buf)
215 {
216 struct visor_device *vdev = to_visor_device(dev);
217 char zoneid[LINESIZE];
218
219 return sprintf(buf, "%s\n",
220 visorchannel_zoneid(vdev->visorchannel, zoneid));
221 }
222 static DEVICE_ATTR_RO(zoneguid);
223
224 static ssize_t typename_show(struct device *dev, struct device_attribute *attr,
225 char *buf)
226 {
227 int i = 0;
228 struct bus_type *xbus = dev->bus;
229 struct device_driver *xdrv = dev->driver;
230 struct visor_driver *drv = NULL;
231
232 if (!xbus || !xdrv)
233 return 0;
234 i = xbus->match(dev, xdrv);
235 if (!i)
236 return 0;
237 drv = to_visor_driver(xdrv);
238 return sprintf(buf, "%s\n", drv->channel_types[i - 1].name);
239 }
240 static DEVICE_ATTR_RO(typename);
241
242 static struct attribute *channel_attrs[] = {
243 &dev_attr_physaddr.attr,
244 &dev_attr_nbytes.attr,
245 &dev_attr_clientpartition.attr,
246 &dev_attr_typeguid.attr,
247 &dev_attr_zoneguid.attr,
248 &dev_attr_typename.attr,
249 NULL
250 };
251
252 static struct attribute_group channel_attr_grp = {
253 .name = "channel",
254 .attrs = channel_attrs,
255 };
256
257 static const struct attribute_group *visorbus_channel_groups[] = {
258 &channel_attr_grp,
259 NULL
260 };
261
262 /* end implementation of specific channel attributes */
263
264 /*
265 * BUS instance attributes
266 *
267 * define & implement display of bus attributes under
268 * /sys/bus/visorbus/devices/visorbus<n>.
269 */
270
271 static ssize_t partition_handle_show(struct device *dev,
272 struct device_attribute *attr,
273 char *buf) {
274 struct visor_device *vdev = to_visor_device(dev);
275 u64 handle = visorchannel_get_clientpartition(vdev->visorchannel);
276
277 return sprintf(buf, "0x%llx\n", handle);
278 }
279 static DEVICE_ATTR_RO(partition_handle);
280
281 static ssize_t partition_guid_show(struct device *dev,
282 struct device_attribute *attr,
283 char *buf) {
284 struct visor_device *vdev = to_visor_device(dev);
285
286 return sprintf(buf, "{%pUb}\n", &vdev->partition_uuid);
287 }
288 static DEVICE_ATTR_RO(partition_guid);
289
290 static ssize_t partition_name_show(struct device *dev,
291 struct device_attribute *attr,
292 char *buf) {
293 struct visor_device *vdev = to_visor_device(dev);
294
295 return sprintf(buf, "%s\n", vdev->name);
296 }
297 static DEVICE_ATTR_RO(partition_name);
298
299 static ssize_t channel_addr_show(struct device *dev,
300 struct device_attribute *attr,
301 char *buf) {
302 struct visor_device *vdev = to_visor_device(dev);
303 u64 addr = visorchannel_get_physaddr(vdev->visorchannel);
304
305 return sprintf(buf, "0x%llx\n", addr);
306 }
307 static DEVICE_ATTR_RO(channel_addr);
308
309 static ssize_t channel_bytes_show(struct device *dev,
310 struct device_attribute *attr,
311 char *buf) {
312 struct visor_device *vdev = to_visor_device(dev);
313 u64 nbytes = visorchannel_get_nbytes(vdev->visorchannel);
314
315 return sprintf(buf, "0x%llx\n", nbytes);
316 }
317 static DEVICE_ATTR_RO(channel_bytes);
318
319 static ssize_t channel_id_show(struct device *dev,
320 struct device_attribute *attr,
321 char *buf) {
322 struct visor_device *vdev = to_visor_device(dev);
323 int len = 0;
324
325 visorchannel_id(vdev->visorchannel, buf);
326 len = strlen(buf);
327 buf[len++] = '\n';
328
329 return len;
330 }
331 static DEVICE_ATTR_RO(channel_id);
332
333 static struct attribute *dev_attrs[] = {
334 &dev_attr_partition_handle.attr,
335 &dev_attr_partition_guid.attr,
336 &dev_attr_partition_name.attr,
337 &dev_attr_channel_addr.attr,
338 &dev_attr_channel_bytes.attr,
339 &dev_attr_channel_id.attr,
340 NULL
341 };
342
343 static struct attribute_group dev_attr_grp = {
344 .attrs = dev_attrs,
345 };
346
347 static const struct attribute_group *visorbus_groups[] = {
348 &dev_attr_grp,
349 NULL
350 };
351
352 /*
353 * BUS debugfs entries
354 *
355 * define & implement display of debugfs attributes under
356 * /sys/kernel/debug/visorbus/visorbus<n>.
357 */
358 /*
359 * vbuschannel_print_devinfo() - format a struct ultra_vbus_deviceinfo
360 * and write it to a seq_file
361 * @devinfo: the struct ultra_vbus_deviceinfo to format
362 * @seq: seq_file to write to
363 * @devix: the device index to be included in the output data, or -1 if no
364 * device index is to be included
365 *
366 * Reads @devInfo, and writes it in human-readable notation to @seq.
367 */
368 static void
369 vbuschannel_print_devinfo(struct ultra_vbus_deviceinfo *devinfo,
370 struct seq_file *seq, int devix)
371 {
372 if (!isprint(devinfo->devtype[0]))
373 return; /* uninitialized vbus device entry */
374
375 if (devix >= 0)
376 seq_printf(seq, "[%d]", devix);
377 else
378 /* vbus device entry is for bus or chipset */
379 seq_puts(seq, " ");
380
381 /*
382 * Note: because the s-Par back-end is free to scribble in this area,
383 * we never assume '\0'-termination.
384 */
385 seq_printf(seq, "%-*.*s ", (int)sizeof(devinfo->devtype),
386 (int)sizeof(devinfo->devtype), devinfo->devtype);
387 seq_printf(seq, "%-*.*s ", (int)sizeof(devinfo->drvname),
388 (int)sizeof(devinfo->drvname), devinfo->drvname);
389 seq_printf(seq, "%.*s\n", (int)sizeof(devinfo->infostrs),
390 devinfo->infostrs);
391 }
392
393 static int client_bus_info_debugfs_show(struct seq_file *seq, void *v)
394 {
395 struct visor_device *vdev = seq->private;
396 struct visorchannel *channel = vdev->visorchannel;
397
398 int i;
399 unsigned long off;
400 struct ultra_vbus_deviceinfo dev_info;
401
402 if (!channel)
403 return 0;
404
405 seq_printf(seq,
406 "Client device / client driver info for %s partition (vbus #%u):\n",
407 ((vdev->name) ? (char *)(vdev->name) : ""),
408 vdev->chipset_bus_no);
409 if (visorchannel_read(channel,
410 offsetof(struct spar_vbus_channel_protocol,
411 chp_info),
412 &dev_info, sizeof(dev_info)) >= 0)
413 vbuschannel_print_devinfo(&dev_info, seq, -1);
414 if (visorchannel_read(channel,
415 offsetof(struct spar_vbus_channel_protocol,
416 bus_info),
417 &dev_info, sizeof(dev_info)) >= 0)
418 vbuschannel_print_devinfo(&dev_info, seq, -1);
419 off = offsetof(struct spar_vbus_channel_protocol, dev_info);
420 i = 0;
421 while (off + sizeof(dev_info) <= visorchannel_get_nbytes(channel)) {
422 if (visorchannel_read(channel, off, &dev_info,
423 sizeof(dev_info)) >= 0)
424 vbuschannel_print_devinfo(&dev_info, seq, i);
425 off += sizeof(dev_info);
426 i++;
427 }
428
429 return 0;
430 }
431
432 static int client_bus_info_debugfs_open(struct inode *inode, struct file *file)
433 {
434 return single_open(file, client_bus_info_debugfs_show,
435 inode->i_private);
436 }
437
438 static const struct file_operations client_bus_info_debugfs_fops = {
439 .owner = THIS_MODULE,
440 .open = client_bus_info_debugfs_open,
441 .read = seq_read,
442 .llseek = seq_lseek,
443 .release = single_release,
444 };
445
446 static void
447 dev_periodic_work(unsigned long __opaque)
448 {
449 struct visor_device *dev = (struct visor_device *)__opaque;
450 struct visor_driver *drv = to_visor_driver(dev->device.driver);
451
452 drv->channel_interrupt(dev);
453 mod_timer(&dev->timer, jiffies + POLLJIFFIES_NORMALCHANNEL);
454 }
455
456 static int
457 dev_start_periodic_work(struct visor_device *dev)
458 {
459 if (dev->being_removed || dev->timer_active)
460 return -EINVAL;
461 /* now up by at least 2 */
462 get_device(&dev->device);
463 dev->timer.expires = jiffies + POLLJIFFIES_NORMALCHANNEL;
464 add_timer(&dev->timer);
465 dev->timer_active = true;
466 return 0;
467 }
468
469 static void
470 dev_stop_periodic_work(struct visor_device *dev)
471 {
472 if (!dev->timer_active)
473 return;
474 del_timer_sync(&dev->timer);
475 dev->timer_active = false;
476 put_device(&dev->device);
477 }
478
479 /*
480 * visordriver_remove_device() - handle visor device going away
481 * @xdev: struct device for the visor device being removed
482 *
483 * This is called when device_unregister() is called for each child device
484 * instance, to notify the appropriate visorbus function driver that the device
485 * is going away, and to decrease the reference count of the device.
486 *
487 * Return: 0 iff successful
488 */
489 static int
490 visordriver_remove_device(struct device *xdev)
491 {
492 struct visor_device *dev;
493 struct visor_driver *drv;
494
495 dev = to_visor_device(xdev);
496 drv = to_visor_driver(xdev->driver);
497 mutex_lock(&dev->visordriver_callback_lock);
498 dev->being_removed = true;
499 if (drv->remove)
500 drv->remove(dev);
501 mutex_unlock(&dev->visordriver_callback_lock);
502 dev_stop_periodic_work(dev);
503
504 put_device(&dev->device);
505 return 0;
506 }
507
508 /**
509 * visorbus_unregister_visor_driver() - unregisters the provided driver
510 * @drv: the driver to unregister
511 *
512 * A visor function driver calls this function to unregister the driver,
513 * i.e., within its module_exit function.
514 */
515 void
516 visorbus_unregister_visor_driver(struct visor_driver *drv)
517 {
518 driver_unregister(&drv->driver);
519 }
520 EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver);
521
522 /**
523 * visorbus_read_channel() - reads from the designated channel into
524 * the provided buffer
525 * @dev: the device whose channel is read from
526 * @offset: the offset into the channel at which reading starts
527 * @dest: the destination buffer that is written into from the channel
528 * @nbytes: the number of bytes to read from the channel
529 *
530 * If receiving a message, use the visorchannel_signalremove()
531 * function instead.
532 *
533 * Return: integer indicating success (zero) or failure (non-zero)
534 */
535 int
536 visorbus_read_channel(struct visor_device *dev, unsigned long offset,
537 void *dest, unsigned long nbytes)
538 {
539 return visorchannel_read(dev->visorchannel, offset, dest, nbytes);
540 }
541 EXPORT_SYMBOL_GPL(visorbus_read_channel);
542
543 /**
544 * visorbus_write_channel() - writes the provided buffer into the designated
545 * channel
546 * @dev: the device whose channel is written to
547 * @offset: the offset into the channel at which writing starts
548 * @src: the source buffer that is written into the channel
549 * @nbytes: the number of bytes to write into the channel
550 *
551 * If sending a message, use the visorchannel_signalinsert()
552 * function instead.
553 *
554 * Return: integer indicating success (zero) or failure (non-zero)
555 */
556 int
557 visorbus_write_channel(struct visor_device *dev, unsigned long offset,
558 void *src, unsigned long nbytes)
559 {
560 return visorchannel_write(dev->visorchannel, offset, src, nbytes);
561 }
562 EXPORT_SYMBOL_GPL(visorbus_write_channel);
563
564 /**
565 * visorbus_enable_channel_interrupts() - enables interrupts on the
566 * designated device
567 * @dev: the device on which to enable interrupts
568 *
569 * Currently we don't yet have a real interrupt, so for now we just call the
570 * interrupt function periodically via a timer.
571 */
572 int
573 visorbus_enable_channel_interrupts(struct visor_device *dev)
574 {
575 struct visor_driver *drv = to_visor_driver(dev->device.driver);
576
577 if (!drv->channel_interrupt) {
578 dev_err(&dev->device, "%s no interrupt function!\n", __func__);
579 return -ENOENT;
580 }
581
582 return dev_start_periodic_work(dev);
583 }
584 EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts);
585
586 /**
587 * visorbus_disable_channel_interrupts() - disables interrupts on the
588 * designated device
589 * @dev: the device on which to disable interrupts
590 */
591 void
592 visorbus_disable_channel_interrupts(struct visor_device *dev)
593 {
594 dev_stop_periodic_work(dev);
595 }
596 EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts);
597
598 /*
599 * create_visor_device() - create visor device as a result of receiving the
600 * controlvm device_create message for a new device
601 * @dev: a freshly-zeroed struct visor_device, containing only filled-in values
602 * for chipset_bus_no and chipset_dev_no, that will be initialized
603 *
604 * This is how everything starts from the device end.
605 * This function is called when a channel first appears via a ControlVM
606 * message. In response, this function allocates a visor_device to
607 * correspond to the new channel, and attempts to connect it the appropriate
608 * driver. If the appropriate driver is found, the visor_driver.probe()
609 * function for that driver will be called, and will be passed the new
610 * visor_device that we just created.
611 *
612 * It's ok if the appropriate driver is not yet loaded, because in that case
613 * the new device struct will just stick around in the bus' list of devices.
614 * When the appropriate driver calls visorbus_register_visor_driver(), the
615 * visor_driver.probe() for the new driver will be called with the new
616 * device.
617 *
618 * Return: 0 if successful, otherwise the negative value returned by
619 * device_add() indicating the reason for failure
620 */
621 static int
622 create_visor_device(struct visor_device *dev)
623 {
624 int err;
625 u32 chipset_bus_no = dev->chipset_bus_no;
626 u32 chipset_dev_no = dev->chipset_dev_no;
627
628 mutex_init(&dev->visordriver_callback_lock);
629 dev->device.bus = &visorbus_type;
630 dev->device.groups = visorbus_channel_groups;
631 device_initialize(&dev->device);
632 dev->device.release = visorbus_release_device;
633 /* keep a reference just for us (now 2) */
634 get_device(&dev->device);
635 setup_timer(&dev->timer, dev_periodic_work, (unsigned long)dev);
636
637 /*
638 * bus_id must be a unique name with respect to this bus TYPE
639 * (NOT bus instance). That's why we need to include the bus
640 * number within the name.
641 */
642 err = dev_set_name(&dev->device, "vbus%u:dev%u",
643 chipset_bus_no, chipset_dev_no);
644 if (err)
645 goto err_put;
646
647 /*
648 * device_add does this:
649 * bus_add_device(dev)
650 * ->device_attach(dev)
651 * ->for each driver drv registered on the bus that dev is on
652 * if (dev.drv) ** device already has a driver **
653 * ** not sure we could ever get here... **
654 * else
655 * if (bus.match(dev,drv)) [visorbus_match]
656 * dev.drv = drv
657 * if (!drv.probe(dev)) [visordriver_probe_device]
658 * dev.drv = NULL
659 *
660 * Note that device_add does NOT fail if no driver failed to
661 * claim the device. The device will be linked onto
662 * bus_type.klist_devices regardless (use bus_for_each_dev).
663 */
664 err = device_add(&dev->device);
665 if (err < 0)
666 goto err_put;
667
668 list_add_tail(&dev->list_all, &list_all_device_instances);
669 return 0; /* success: reference kept via unmatched get_device() */
670
671 err_put:
672 put_device(&dev->device);
673 dev_err(&dev->device, "Creating visor device failed. %d\n", err);
674 return err;
675 }
676
677 static void
678 remove_visor_device(struct visor_device *dev)
679 {
680 list_del(&dev->list_all);
681 put_device(&dev->device);
682 device_unregister(&dev->device);
683 }
684
685 static int
686 get_vbus_header_info(struct visorchannel *chan,
687 struct spar_vbus_headerinfo *hdr_info)
688 {
689 int err;
690
691 if (!spar_check_channel(visorchannel_get_header(chan),
692 spar_vbus_channel_protocol_uuid,
693 "vbus",
694 sizeof(struct spar_vbus_channel_protocol),
695 SPAR_VBUS_CHANNEL_PROTOCOL_VERSIONID,
696 SPAR_VBUS_CHANNEL_PROTOCOL_SIGNATURE))
697 return -EINVAL;
698
699 err = visorchannel_read(chan, sizeof(struct channel_header), hdr_info,
700 sizeof(*hdr_info));
701 if (err < 0)
702 return err;
703
704 if (hdr_info->struct_bytes < sizeof(struct spar_vbus_headerinfo))
705 return -EINVAL;
706
707 if (hdr_info->device_info_struct_bytes <
708 sizeof(struct ultra_vbus_deviceinfo))
709 return -EINVAL;
710
711 return 0;
712 }
713
714 /*
715 * write_vbus_chp_info() - write the contents of <info> to the struct
716 * spar_vbus_channel_protocol.chp_info
717 * @chan: indentifies the s-Par channel that will be updated
718 * @hdr_info: used to find appropriate channel offset to write data
719 * @info: contains the information to write
720 *
721 * Writes chipset info into the channel memory to be used for diagnostic
722 * purposes.
723 *
724 * Returns no value since this is debug information and not needed for
725 * device functionality.
726 */
727 static void
728 write_vbus_chp_info(struct visorchannel *chan,
729 struct spar_vbus_headerinfo *hdr_info,
730 struct ultra_vbus_deviceinfo *info)
731 {
732 int off = sizeof(struct channel_header) + hdr_info->chp_info_offset;
733
734 if (hdr_info->chp_info_offset == 0)
735 return;
736
737 visorchannel_write(chan, off, info, sizeof(*info));
738 }
739
740 /*
741 * write_vbus_bus_info() - write the contents of <info> to the struct
742 * spar_vbus_channel_protocol.bus_info
743 * @chan: indentifies the s-Par channel that will be updated
744 * @hdr_info: used to find appropriate channel offset to write data
745 * @info: contains the information to write
746 *
747 * Writes bus info into the channel memory to be used for diagnostic
748 * purposes.
749 *
750 * Returns no value since this is debug information and not needed for
751 * device functionality.
752 */
753 static void
754 write_vbus_bus_info(struct visorchannel *chan,
755 struct spar_vbus_headerinfo *hdr_info,
756 struct ultra_vbus_deviceinfo *info)
757 {
758 int off = sizeof(struct channel_header) + hdr_info->bus_info_offset;
759
760 if (hdr_info->bus_info_offset == 0)
761 return;
762
763 visorchannel_write(chan, off, info, sizeof(*info));
764 }
765
766 /*
767 * write_vbus_dev_info() - write the contents of <info> to the struct
768 * spar_vbus_channel_protocol.dev_info[<devix>]
769 * @chan: indentifies the s-Par channel that will be updated
770 * @hdr_info: used to find appropriate channel offset to write data
771 * @info: contains the information to write
772 * @devix: the relative device number (0..n-1) of the device on the bus
773 *
774 * Writes device info into the channel memory to be used for diagnostic
775 * purposes.
776 *
777 * Returns no value since this is debug information and not needed for
778 * device functionality.
779 */
780 static void
781 write_vbus_dev_info(struct visorchannel *chan,
782 struct spar_vbus_headerinfo *hdr_info,
783 struct ultra_vbus_deviceinfo *info, unsigned int devix)
784 {
785 int off =
786 (sizeof(struct channel_header) + hdr_info->dev_info_offset) +
787 (hdr_info->device_info_struct_bytes * devix);
788
789 if (hdr_info->dev_info_offset == 0)
790 return;
791
792 visorchannel_write(chan, off, info, sizeof(*info));
793 }
794
795 static void bus_device_info_init(
796 struct ultra_vbus_deviceinfo *bus_device_info_ptr,
797 const char *dev_type, const char *drv_name)
798 {
799 memset(bus_device_info_ptr, 0, sizeof(struct ultra_vbus_deviceinfo));
800 snprintf(bus_device_info_ptr->devtype,
801 sizeof(bus_device_info_ptr->devtype),
802 "%s", (dev_type) ? dev_type : "unknownType");
803 snprintf(bus_device_info_ptr->drvname,
804 sizeof(bus_device_info_ptr->drvname),
805 "%s", (drv_name) ? drv_name : "unknownDriver");
806 snprintf(bus_device_info_ptr->infostrs,
807 sizeof(bus_device_info_ptr->infostrs), "kernel ver. %s",
808 utsname()->release);
809 }
810
811 /*
812 * fix_vbus_dev_info() - for a child device just created on a client bus, fill
813 * in information about the driver that is controlling
814 * this device into the appropriate slot within the
815 * vbus channel of the bus instance
816 * @visordev: struct visor_device for the desired device
817 */
818 static void
819 fix_vbus_dev_info(struct visor_device *visordev)
820 {
821 int i;
822 struct visor_device *bdev;
823 struct visor_driver *visordrv;
824 u32 bus_no = visordev->chipset_bus_no;
825 u32 dev_no = visordev->chipset_dev_no;
826 struct ultra_vbus_deviceinfo dev_info;
827 const char *chan_type_name = NULL;
828 struct spar_vbus_headerinfo *hdr_info;
829
830 if (!visordev->device.driver)
831 return;
832
833 bdev = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
834 if (!bdev)
835 return;
836 hdr_info = (struct spar_vbus_headerinfo *)bdev->vbus_hdr_info;
837 if (!hdr_info)
838 return;
839 visordrv = to_visor_driver(visordev->device.driver);
840
841 /*
842 * Within the list of device types (by GUID) that the driver
843 * says it supports, find out which one of those types matches
844 * the type of this device, so that we can include the device
845 * type name
846 */
847 for (i = 0; visordrv->channel_types[i].name; i++) {
848 if (memcmp(&visordrv->channel_types[i].guid,
849 &visordev->channel_type_guid,
850 sizeof(visordrv->channel_types[i].guid)) == 0) {
851 chan_type_name = visordrv->channel_types[i].name;
852 break;
853 }
854 }
855
856 bus_device_info_init(&dev_info, chan_type_name, visordrv->name);
857 write_vbus_dev_info(bdev->visorchannel, hdr_info, &dev_info, dev_no);
858
859 write_vbus_chp_info(bdev->visorchannel, hdr_info, &chipset_driverinfo);
860 write_vbus_bus_info(bdev->visorchannel, hdr_info,
861 &clientbus_driverinfo);
862 }
863
864 /*
865 * visordriver_probe_device() - handle new visor device coming online
866 * @xdev: struct device for the visor device being probed
867 *
868 * This is called automatically upon adding a visor_device (device_add), or
869 * adding a visor_driver (visorbus_register_visor_driver), but only after
870 * visorbus_match() has returned 1 to indicate a successful match between
871 * driver and device.
872 *
873 * If successful, a reference to the device will be held onto via get_device().
874 *
875 * Return: 0 if successful, meaning the function driver's probe() function
876 * was successful with this device, otherwise a negative errno
877 * value indicating failure reason
878 */
879 static int
880 visordriver_probe_device(struct device *xdev)
881 {
882 int res;
883 struct visor_driver *drv;
884 struct visor_device *dev;
885
886 drv = to_visor_driver(xdev->driver);
887 dev = to_visor_device(xdev);
888
889 if (!drv->probe)
890 return -ENODEV;
891
892 mutex_lock(&dev->visordriver_callback_lock);
893 dev->being_removed = false;
894
895 res = drv->probe(dev);
896 if (res >= 0) {
897 /* success: reference kept via unmatched get_device() */
898 get_device(&dev->device);
899 fix_vbus_dev_info(dev);
900 }
901
902 mutex_unlock(&dev->visordriver_callback_lock);
903 return res;
904 }
905
906 /**
907 * visorbus_register_visor_driver() - registers the provided visor driver
908 * for handling one or more visor device
909 * types (channel_types)
910 * @drv: the driver to register
911 *
912 * A visor function driver calls this function to register
913 * the driver. The caller MUST fill in the following fields within the
914 * #drv structure:
915 * name, version, owner, channel_types, probe, remove
916 *
917 * Here's how the whole Linux bus / driver / device model works.
918 *
919 * At system start-up, the visorbus kernel module is loaded, which registers
920 * visorbus_type as a bus type, using bus_register().
921 *
922 * All kernel modules that support particular device types on a
923 * visorbus bus are loaded. Each of these kernel modules calls
924 * visorbus_register_visor_driver() in their init functions, passing a
925 * visor_driver struct. visorbus_register_visor_driver() in turn calls
926 * register_driver(&visor_driver.driver). This .driver member is
927 * initialized with generic methods (like probe), whose sole responsibility
928 * is to act as a broker for the real methods, which are within the
929 * visor_driver struct. (This is the way the subclass behavior is
930 * implemented, since visor_driver is essentially a subclass of the
931 * generic driver.) Whenever a driver_register() happens, core bus code in
932 * the kernel does (see device_attach() in drivers/base/dd.c):
933 *
934 * for each dev associated with the bus (the bus that driver is on) that
935 * does not yet have a driver
936 * if bus.match(dev,newdriver) == yes_matched ** .match specified
937 * ** during bus_register().
938 * newdriver.probe(dev) ** for visor drivers, this will call
939 * ** the generic driver.probe implemented in visorbus.c,
940 * ** which in turn calls the probe specified within the
941 * ** struct visor_driver (which was specified by the
942 * ** actual device driver as part of
943 * ** visorbus_register_visor_driver()).
944 *
945 * The above dance also happens when a new device appears.
946 * So the question is, how are devices created within the system?
947 * Basically, just call device_add(dev). See pci_bus_add_devices().
948 * pci_scan_device() shows an example of how to build a device struct. It
949 * returns the newly-created struct to pci_scan_single_device(), who adds it
950 * to the list of devices at PCIBUS.devices. That list of devices is what
951 * is traversed by pci_bus_add_devices().
952 *
953 * Return: integer indicating success (zero) or failure (non-zero)
954 */
955 int visorbus_register_visor_driver(struct visor_driver *drv)
956 {
957 if (!initialized)
958 return -ENODEV; /* can't register on a nonexistent bus */
959
960 drv->driver.name = drv->name;
961 drv->driver.bus = &visorbus_type;
962 drv->driver.probe = visordriver_probe_device;
963 drv->driver.remove = visordriver_remove_device;
964 drv->driver.owner = drv->owner;
965
966 /*
967 * driver_register does this:
968 * bus_add_driver(drv)
969 * ->if (drv.bus) ** (bus_type) **
970 * driver_attach(drv)
971 * for each dev with bus type of drv.bus
972 * if (!dev.drv) ** no driver assigned yet **
973 * if (bus.match(dev,drv)) [visorbus_match]
974 * dev.drv = drv
975 * if (!drv.probe(dev)) [visordriver_probe_device]
976 * dev.drv = NULL
977 */
978
979 return driver_register(&drv->driver);
980 }
981 EXPORT_SYMBOL_GPL(visorbus_register_visor_driver);
982
983 /*
984 * create_bus_instance() - create a device instance for the visor bus itself
985 * @dev: struct visor_device indicating the bus instance
986 *
987 * Return: 0 for success, otherwise negative errno value indicating reason for
988 * failure
989 */
990 static int
991 create_bus_instance(struct visor_device *dev)
992 {
993 int id = dev->chipset_bus_no;
994 int err;
995 struct spar_vbus_headerinfo *hdr_info;
996
997 hdr_info = kzalloc(sizeof(*hdr_info), GFP_KERNEL);
998 if (!hdr_info)
999 return -ENOMEM;
1000
1001 dev_set_name(&dev->device, "visorbus%d", id);
1002 dev->device.bus = &visorbus_type;
1003 dev->device.groups = visorbus_groups;
1004 dev->device.release = visorbus_release_busdevice;
1005
1006 dev->debugfs_dir = debugfs_create_dir(dev_name(&dev->device),
1007 visorbus_debugfs_dir);
1008 dev->debugfs_client_bus_info =
1009 debugfs_create_file("client_bus_info", 0440,
1010 dev->debugfs_dir, dev,
1011 &client_bus_info_debugfs_fops);
1012
1013 dev_set_drvdata(&dev->device, dev);
1014 err = get_vbus_header_info(dev->visorchannel, hdr_info);
1015 if (err < 0)
1016 goto err_debugfs_dir;
1017
1018 err = device_register(&dev->device);
1019 if (err < 0)
1020 goto err_debugfs_dir;
1021
1022 list_add_tail(&dev->list_all, &list_all_bus_instances);
1023
1024 dev->vbus_hdr_info = (void *)hdr_info;
1025 write_vbus_chp_info(dev->visorchannel, hdr_info,
1026 &chipset_driverinfo);
1027 write_vbus_bus_info(dev->visorchannel, hdr_info,
1028 &clientbus_driverinfo);
1029
1030 return 0;
1031
1032 err_debugfs_dir:
1033 debugfs_remove_recursive(dev->debugfs_dir);
1034 kfree(hdr_info);
1035 dev_err(&dev->device, "create_bus_instance failed: %d\n", err);
1036 return err;
1037 }
1038
1039 /*
1040 * remove_bus_instance() - remove a device instance for the visor bus itself
1041 * @dev: struct visor_device indentifying the bus to remove
1042 */
1043 static void
1044 remove_bus_instance(struct visor_device *dev)
1045 {
1046 /*
1047 * Note that this will result in the release method for
1048 * dev->dev being called, which will call
1049 * visorbus_release_busdevice(). This has something to do with
1050 * the put_device() done in device_unregister(), but I have never
1051 * successfully been able to trace thru the code to see where/how
1052 * release() gets called. But I know it does.
1053 */
1054 if (dev->visorchannel) {
1055 visorchannel_destroy(dev->visorchannel);
1056 dev->visorchannel = NULL;
1057 }
1058 kfree(dev->vbus_hdr_info);
1059 list_del(&dev->list_all);
1060 device_unregister(&dev->device);
1061 }
1062
1063 /*
1064 * remove_all_visor_devices() - remove all child visor bus device instances
1065 */
1066 static void
1067 remove_all_visor_devices(void)
1068 {
1069 struct list_head *listentry, *listtmp;
1070
1071 list_for_each_safe(listentry, listtmp, &list_all_device_instances) {
1072 struct visor_device *dev = list_entry(listentry,
1073 struct visor_device,
1074 list_all);
1075 remove_visor_device(dev);
1076 }
1077 }
1078
1079 int
1080 chipset_bus_create(struct visor_device *dev)
1081 {
1082 int err;
1083
1084 err = create_bus_instance(dev);
1085
1086 if (err < 0)
1087 return err;
1088
1089 bus_create_response(dev, err);
1090
1091 return 0;
1092 }
1093
1094 void
1095 chipset_bus_destroy(struct visor_device *dev)
1096 {
1097 remove_bus_instance(dev);
1098 bus_destroy_response(dev, 0);
1099 }
1100
1101 int
1102 chipset_device_create(struct visor_device *dev_info)
1103 {
1104 int err;
1105
1106 err = create_visor_device(dev_info);
1107 if (err < 0)
1108 return err;
1109
1110 device_create_response(dev_info, err);
1111
1112 return 0;
1113 }
1114
1115 void
1116 chipset_device_destroy(struct visor_device *dev_info)
1117 {
1118 remove_visor_device(dev_info);
1119
1120 device_destroy_response(dev_info, 0);
1121 }
1122
1123 /*
1124 * pause_state_change_complete() - the callback function to be called by a
1125 * visorbus function driver when a
1126 * pending "pause device" operation has
1127 * completed
1128 * @dev: struct visor_device identifying the paused device
1129 * @status: 0 iff the pause state change completed successfully, otherwise
1130 * a negative errno value indicating the reason for failure
1131 */
1132 static void
1133 pause_state_change_complete(struct visor_device *dev, int status)
1134 {
1135 if (!dev->pausing)
1136 return;
1137
1138 dev->pausing = false;
1139
1140 device_pause_response(dev, status);
1141 }
1142
1143 /*
1144 * resume_state_change_complete() - the callback function to be called by a
1145 * visorbus function driver when a
1146 * pending "resume device" operation has
1147 * completed
1148 * @dev: struct visor_device identifying the resumed device
1149 * @status: 0 iff the resume state change completed successfully, otherwise
1150 * a negative errno value indicating the reason for failure
1151 */
1152 static void
1153 resume_state_change_complete(struct visor_device *dev, int status)
1154 {
1155 if (!dev->resuming)
1156 return;
1157
1158 dev->resuming = false;
1159
1160 /*
1161 * Notify the chipset driver that the resume is complete,
1162 * which will presumably want to send some sort of response to
1163 * the initiator.
1164 */
1165 device_resume_response(dev, status);
1166 }
1167
1168 /*
1169 * initiate_chipset_device_pause_resume() - start a pause or resume operation
1170 * for a visor device
1171 * @dev: struct visor_device identifying the device being paused or resumed
1172 * @is_pause: true to indicate pause operation, false to indicate resume
1173 *
1174 * Tell the subordinate function driver for a specific device to pause
1175 * or resume that device. Success/failure result is returned asynchronously
1176 * via a callback function; see pause_state_change_complete() and
1177 * resume_state_change_complete().
1178 */
1179 static int
1180 initiate_chipset_device_pause_resume(struct visor_device *dev, bool is_pause)
1181 {
1182 int err;
1183 struct visor_driver *drv = NULL;
1184
1185 drv = to_visor_driver(dev->device.driver);
1186 if (!drv)
1187 return -ENODEV;
1188
1189 if (dev->pausing || dev->resuming)
1190 return -EBUSY;
1191
1192 if (is_pause) {
1193 if (!drv->pause)
1194 return -EINVAL;
1195
1196 dev->pausing = true;
1197 err = drv->pause(dev, pause_state_change_complete);
1198 } else {
1199 /* The vbus_dev_info structure in the channel was been
1200 * cleared, make sure it is valid.
1201 */
1202 fix_vbus_dev_info(dev);
1203 if (!drv->resume)
1204 return -EINVAL;
1205
1206 dev->resuming = true;
1207 err = drv->resume(dev, resume_state_change_complete);
1208 }
1209
1210 return err;
1211 }
1212
1213 /**
1214 * chipset_device_pause() - start a pause operation for a visor device
1215 * @dev_info: struct visor_device identifying the device being paused
1216 *
1217 * Tell the subordinate function driver for a specific device to pause
1218 * that device. Success/failure result is returned asynchronously
1219 * via a callback function; see pause_state_change_complete().
1220 */
1221 int
1222 chipset_device_pause(struct visor_device *dev_info)
1223 {
1224 int err;
1225
1226 err = initiate_chipset_device_pause_resume(dev_info, true);
1227
1228 if (err < 0) {
1229 dev_info->pausing = false;
1230 return err;
1231 }
1232
1233 return 0;
1234 }
1235
1236 /**
1237 * chipset_device_resume() - start a resume operation for a visor device
1238 * @dev_info: struct visor_device identifying the device being resumed
1239 *
1240 * Tell the subordinate function driver for a specific device to resume
1241 * that device. Success/failure result is returned asynchronously
1242 * via a callback function; see resume_state_change_complete().
1243 */
1244 int
1245 chipset_device_resume(struct visor_device *dev_info)
1246 {
1247 int err;
1248
1249 err = initiate_chipset_device_pause_resume(dev_info, false);
1250
1251 if (err < 0) {
1252 dev_info->resuming = false;
1253 return err;
1254 }
1255
1256 return 0;
1257 }
1258
1259 int
1260 visorbus_init(void)
1261 {
1262 int err;
1263
1264 visorbus_debugfs_dir = debugfs_create_dir("visorbus", NULL);
1265 if (!visorbus_debugfs_dir)
1266 return -ENOMEM;
1267
1268 bus_device_info_init(&clientbus_driverinfo, "clientbus", "visorbus");
1269
1270 err = bus_register(&visorbus_type);
1271 if (err < 0)
1272 return err;
1273
1274 initialized = true;
1275
1276 bus_device_info_init(&chipset_driverinfo, "chipset", "visorchipset");
1277
1278 return 0;
1279 }
1280
1281 void
1282 visorbus_exit(void)
1283 {
1284 struct list_head *listentry, *listtmp;
1285
1286 remove_all_visor_devices();
1287
1288 list_for_each_safe(listentry, listtmp, &list_all_bus_instances) {
1289 struct visor_device *dev = list_entry(listentry,
1290 struct visor_device,
1291 list_all);
1292 remove_bus_instance(dev);
1293 }
1294
1295 bus_unregister(&visorbus_type);
1296 initialized = false;
1297 debugfs_remove_recursive(visorbus_debugfs_dir);
1298 }