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