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