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