| 1 | /* |
| 2 | * Copyright 2010-2017 Intel Corporation. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License, version 2, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * Disclaimer: The codes contained in these modules may be specific to |
| 14 | * the Intel Software Development Platform codenamed Knights Ferry, |
| 15 | * and the Intel product codenamed Knights Corner, and are not backward |
| 16 | * compatible with other Intel products. Additionally, Intel will NOT |
| 17 | * support the codes or instruction set in future products. |
| 18 | * |
| 19 | * Intel offers no warranty of any kind regarding the code. This code is |
| 20 | * licensed on an "AS IS" basis and Intel is not obligated to provide |
| 21 | * any support, assistance, installation, training, or other services |
| 22 | * of any kind. Intel is also not obligated to provide any updates, |
| 23 | * enhancements or extensions. Intel specifically disclaims any warranty |
| 24 | * of merchantability, non-infringement, fitness for any particular |
| 25 | * purpose, and any other warranty. |
| 26 | * |
| 27 | * Further, Intel disclaims all liability of any kind, including but |
| 28 | * not limited to liability for infringement of any proprietary rights, |
| 29 | * relating to the use of the code, even if Intel is notified of the |
| 30 | * possibility of such liability. Except as expressly stated in an Intel |
| 31 | * license agreement provided with this code and agreed upon with Intel, |
| 32 | * no license, express or implied, by estoppel or otherwise, to any |
| 33 | * intellectual property rights is granted herein. |
| 34 | */ |
| 35 | |
| 36 | #include <linux/module.h> |
| 37 | #include <linux/init.h> |
| 38 | #include <scif.h> |
| 39 | #include <linux/errno.h> |
| 40 | #include <linux/hardirq.h> |
| 41 | #include <linux/types.h> |
| 42 | #include <linux/capability.h> |
| 43 | #include <linux/slab.h> |
| 44 | #include <linux/string.h> |
| 45 | #include <linux/gfp.h> |
| 46 | #include <linux/vmalloc.h> |
| 47 | #include <asm/io.h> |
| 48 | #include <linux/kernel.h> |
| 49 | #include <linux/mm_types.h> |
| 50 | #include <linux/jiffies.h> |
| 51 | #include <linux/timer.h> |
| 52 | #include <linux/irqflags.h> |
| 53 | #include <linux/time.h> |
| 54 | #include <linux/spinlock.h> |
| 55 | #include <linux/mutex.h> |
| 56 | #include <linux/semaphore.h> |
| 57 | #include <linux/kthread.h> |
| 58 | #include <linux/sched.h> |
| 59 | #include <linux/delay.h> |
| 60 | #include <linux/wait.h> |
| 61 | #include <asm/bug.h> |
| 62 | #include <linux/pci.h> |
| 63 | #include <linux/device.h> |
| 64 | #include <linux/fs.h> |
| 65 | #include <linux/list.h> |
| 66 | #include <linux/workqueue.h> |
| 67 | #include <linux/interrupt.h> |
| 68 | #include <asm/atomic.h> |
| 69 | #include <linux/netdevice.h> |
| 70 | #include <linux/debugfs.h> |
| 71 | |
| 72 | #define ACPT_BOOTED 1 |
| 73 | #define ACPT_BOOT_ACK 2 |
| 74 | #define ACPT_NACK_VERSION 3 |
| 75 | #define ACPT_REQUEST_TIME 4 |
| 76 | #define ACPT_TIME_DATA 5 |
| 77 | |
| 78 | #define ACPT_VERSION 1 |
| 79 | |
| 80 | static dev_t dev; |
| 81 | static struct class *class; |
| 82 | static struct device *mbdev; |
| 83 | |
| 84 | static int host_notified; |
| 85 | static struct timespec tod; |
| 86 | static int timeset = 0; |
| 87 | |
| 88 | static ssize_t |
| 89 | show_timesync(struct device *dev, struct device_attribute *attr, char *buf) |
| 90 | { |
| 91 | return snprintf(buf, PAGE_SIZE, "Time: %s\n", timeset? "set" : "not set"); |
| 92 | } |
| 93 | |
| 94 | static ssize_t |
| 95 | set_synctime(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
| 96 | { |
| 97 | struct scif_portID port = {0, MIC_NOTIFY}; |
| 98 | static scif_epd_t epd; |
| 99 | int proto = ACPT_REQUEST_TIME; |
| 100 | int version = ACPT_VERSION; |
| 101 | int err; |
| 102 | |
| 103 | epd = scif_open(); |
| 104 | |
| 105 | if ((err = scif_connect(epd, &port))) { |
| 106 | printk("MPSSBOOT error, synctime connect failed: %d\n", err); |
| 107 | goto close_synctime; |
| 108 | } |
| 109 | |
| 110 | if ((err = scif_send(epd, &version, sizeof(version), 0)) != sizeof(version)) { |
| 111 | printk("MPSSBOOT send version failed: %d\n", err); |
| 112 | goto close_synctime; |
| 113 | } |
| 114 | |
| 115 | if ((err = scif_send(epd, &proto, sizeof(proto), 0)) != sizeof(proto)) { |
| 116 | printk("MPSSBOOT send boot finished failed: %d\n", err); |
| 117 | goto close_synctime; |
| 118 | } |
| 119 | |
| 120 | if ((err = scif_recv(epd, &proto, sizeof(proto), SCIF_RECV_BLOCK)) != sizeof(proto)) { |
| 121 | printk("MPSSBOOT protocol recv ack failed: %d\n", err); |
| 122 | goto close_synctime; |
| 123 | } |
| 124 | |
| 125 | if (proto != ACPT_TIME_DATA) { |
| 126 | printk("MPSSBOOT failed to receive time data packet %d\n", proto); |
| 127 | goto close_synctime; |
| 128 | } |
| 129 | |
| 130 | if ((err = scif_recv(epd, &tod, sizeof(tod), SCIF_RECV_BLOCK)) != sizeof(tod)) { |
| 131 | printk("MPSSBOOT time data read size failed: %d\n", err); |
| 132 | goto close_synctime; |
| 133 | } |
| 134 | |
| 135 | do_settimeofday(&tod); |
| 136 | printk("MPSSBOOT Time of day sycned with host\n"); |
| 137 | timeset = 1; |
| 138 | |
| 139 | close_synctime: |
| 140 | scif_close(epd); |
| 141 | return count; |
| 142 | } |
| 143 | static DEVICE_ATTR(synctime, S_IRUGO | S_IWUSR, show_timesync, set_synctime); |
| 144 | |
| 145 | static ssize_t |
| 146 | show_host_notified(struct device *dev, struct device_attribute *attr, char *buf) |
| 147 | { |
| 148 | return snprintf(buf, PAGE_SIZE, "%d\n", host_notified); |
| 149 | } |
| 150 | |
| 151 | static ssize_t |
| 152 | set_host_notified(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
| 153 | { |
| 154 | struct scif_portID port = {0, MIC_NOTIFY}; |
| 155 | static scif_epd_t epd; |
| 156 | int proto = ACPT_BOOTED; |
| 157 | int version = ACPT_VERSION; |
| 158 | int err; |
| 159 | |
| 160 | epd = scif_open(); |
| 161 | |
| 162 | if ((err = scif_connect(epd, &port))) { |
| 163 | printk("MPSSBOOT error, notify connect failed: %d\n", err); |
| 164 | goto close_notify; |
| 165 | } |
| 166 | |
| 167 | if ((err = scif_send(epd, &version, sizeof(version), 0)) != sizeof(version)) { |
| 168 | printk("MPSSBOOT send version failed: %d\n", err); |
| 169 | goto close_notify; |
| 170 | } |
| 171 | |
| 172 | if ((err = scif_send(epd, &proto, sizeof(proto), 0)) != sizeof(proto)) { |
| 173 | printk("MPSSBOOT send boot finished failed: %d\n", err); |
| 174 | goto close_notify; |
| 175 | } |
| 176 | |
| 177 | if ((err = scif_recv(epd, &proto, sizeof(proto), SCIF_RECV_BLOCK)) != sizeof(proto)) { |
| 178 | printk("MPSSBOOT protocol recv ack failed: %d\n", err); |
| 179 | goto close_notify; |
| 180 | } |
| 181 | |
| 182 | if (proto != ACPT_BOOT_ACK) |
| 183 | printk("MPSSBOOT failed to receive boot ACK, got %d\n", proto); |
| 184 | else |
| 185 | printk("MPSSBOOT Boot acknowledged\n"); |
| 186 | |
| 187 | close_notify: |
| 188 | scif_close(epd); |
| 189 | return count; |
| 190 | } |
| 191 | static DEVICE_ATTR(host_notified, S_IRUGO | S_IWUSR, show_host_notified, set_host_notified); |
| 192 | |
| 193 | static struct attribute *mb_attributes[] = { |
| 194 | &dev_attr_synctime.attr, |
| 195 | &dev_attr_host_notified.attr, |
| 196 | NULL |
| 197 | }; |
| 198 | |
| 199 | struct attribute_group mb_attr_group = { |
| 200 | .attrs = mb_attributes |
| 201 | }; |
| 202 | |
| 203 | /* This function closes the endpoint established on init */ |
| 204 | static void |
| 205 | mpssboot_exit(void) |
| 206 | { |
| 207 | sysfs_remove_group(&mbdev->kobj, &mb_attr_group); |
| 208 | device_destroy(class, dev); |
| 209 | class_destroy(class); |
| 210 | } |
| 211 | |
| 212 | static char * |
| 213 | mpssboot_devnode(struct device *dev, mode_t *mode) |
| 214 | { |
| 215 | return kasprintf(GFP_KERNEL, "%s", dev_name(dev)); |
| 216 | } |
| 217 | |
| 218 | /* This function initializes a SCIF connection to the host */ |
| 219 | static int |
| 220 | mpssboot_init(void) |
| 221 | { |
| 222 | //static struct device dev; |
| 223 | int result; |
| 224 | |
| 225 | alloc_chrdev_region(&dev, 0, 2, "micnotify"); |
| 226 | class = class_create(THIS_MODULE, "micnotify"); |
| 227 | class->devnode = mpssboot_devnode; |
| 228 | mbdev = device_create(class, NULL, dev, NULL, "notify"); |
| 229 | |
| 230 | result = sysfs_create_group(&mbdev->kobj, &mb_attr_group); |
| 231 | result = result; |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | module_init(mpssboot_init); |
| 236 | module_exit(mpssboot_exit); |
| 237 | MODULE_LICENSE("GPL"); |
| 238 | |