2 * Intel MIC Platform Software Stack (MPSS)
4 * Copyright(c) 2015 Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2, as
8 * published by the Free Software Foundation.
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. See the GNU
13 * General Public License for more details.
15 * The full GNU General Public License is included in this distribution in
16 * the file called "COPYING".
18 * Intel MIC Coprocessor State Management (COSM) Driver
21 #include <linux/slab.h>
22 #include "cosm_main.h"
25 * A state-to-string lookup table, for exposing a human readable state
26 * via sysfs. Always keep in sync with enum cosm_states
28 const char * const cosm_state_string[] = {
29 [MIC_READY] = "ready",
30 [MIC_BOOTING] = "booting",
31 [MIC_ONLINE] = "online",
32 [MIC_SHUTTING_DOWN] = "shutting_down",
33 [MIC_RESETTING] = "resetting",
34 [MIC_RESET_FAILED] = "reset_failed",
38 * A shutdown-status-to-string lookup table, for exposing a human
39 * readable state via sysfs. Always keep in sync with enum cosm_shutdown_status
41 const char * const cosm_shutdown_status_string[] = {
43 [MIC_CRASHED] = "crashed",
44 [MIC_HALTED] = "halted",
45 [MIC_POWER_OFF] = "poweroff",
46 [MIC_RESTART] = "restart",
49 void cosm_set_shutdown_status(struct cosm_device *cdev, u8 shutdown_status)
51 dev_dbg(&cdev->dev, "Shutdown Status %s -> %s\n",
52 cosm_shutdown_status_string[cdev->shutdown_status],
53 cosm_shutdown_status_string[shutdown_status]);
54 cdev->shutdown_status = shutdown_status;
57 void cosm_set_state(struct cosm_device *cdev, u8 state)
59 dev_dbg(&cdev->dev, "State %s -> %s\n",
60 cosm_state_string[cdev->state],
61 cosm_state_string[state]);
63 sysfs_notify_dirent(cdev->state_sysfs);
67 family_show(struct device *dev, struct device_attribute *attr, char *buf)
69 struct cosm_device *cdev = dev_get_drvdata(dev);
74 return cdev->hw_ops->family(cdev, buf);
76 static DEVICE_ATTR_RO(family);
79 stepping_show(struct device *dev, struct device_attribute *attr, char *buf)
81 struct cosm_device *cdev = dev_get_drvdata(dev);
86 return cdev->hw_ops->stepping(cdev, buf);
88 static DEVICE_ATTR_RO(stepping);
91 state_show(struct device *dev, struct device_attribute *attr, char *buf)
93 struct cosm_device *cdev = dev_get_drvdata(dev);
95 if (!cdev || cdev->state >= MIC_LAST)
98 return scnprintf(buf, PAGE_SIZE, "%s\n",
99 cosm_state_string[cdev->state]);
103 state_store(struct device *dev, struct device_attribute *attr,
104 const char *buf, size_t count)
106 struct cosm_device *cdev = dev_get_drvdata(dev);
112 if (sysfs_streq(buf, "boot")) {
113 rc = cosm_start(cdev);
116 if (sysfs_streq(buf, "reset")) {
117 rc = cosm_reset(cdev);
121 if (sysfs_streq(buf, "shutdown")) {
122 rc = cosm_shutdown(cdev);
131 static DEVICE_ATTR_RW(state);
133 static ssize_t shutdown_status_show(struct device *dev,
134 struct device_attribute *attr, char *buf)
136 struct cosm_device *cdev = dev_get_drvdata(dev);
138 if (!cdev || cdev->shutdown_status >= MIC_STATUS_LAST)
141 return scnprintf(buf, PAGE_SIZE, "%s\n",
142 cosm_shutdown_status_string[cdev->shutdown_status]);
144 static DEVICE_ATTR_RO(shutdown_status);
147 heartbeat_enable_show(struct device *dev,
148 struct device_attribute *attr, char *buf)
150 struct cosm_device *cdev = dev_get_drvdata(dev);
155 return scnprintf(buf, PAGE_SIZE, "%d\n", cdev->sysfs_heartbeat_enable);
159 heartbeat_enable_store(struct device *dev,
160 struct device_attribute *attr,
161 const char *buf, size_t count)
163 struct cosm_device *cdev = dev_get_drvdata(dev);
170 mutex_lock(&cdev->cosm_mutex);
171 ret = kstrtoint(buf, 10, &enable);
175 cdev->sysfs_heartbeat_enable = enable;
176 /* if state is not online, cdev->heartbeat_watchdog_enable is 0 */
177 if (cdev->state == MIC_ONLINE)
178 cdev->heartbeat_watchdog_enable = enable;
181 mutex_unlock(&cdev->cosm_mutex);
184 static DEVICE_ATTR_RW(heartbeat_enable);
187 cmdline_show(struct device *dev, struct device_attribute *attr, char *buf)
189 struct cosm_device *cdev = dev_get_drvdata(dev);
195 cmdline = cdev->cmdline;
198 return scnprintf(buf, PAGE_SIZE, "%s\n", cmdline);
203 cmdline_store(struct device *dev, struct device_attribute *attr,
204 const char *buf, size_t count)
206 struct cosm_device *cdev = dev_get_drvdata(dev);
211 mutex_lock(&cdev->cosm_mutex);
212 kfree(cdev->cmdline);
214 cdev->cmdline = kmalloc(count + 1, GFP_KERNEL);
215 if (!cdev->cmdline) {
220 strncpy(cdev->cmdline, buf, count);
222 if (cdev->cmdline[count - 1] == '\n')
223 cdev->cmdline[count - 1] = '\0';
225 cdev->cmdline[count] = '\0';
227 mutex_unlock(&cdev->cosm_mutex);
230 static DEVICE_ATTR_RW(cmdline);
233 firmware_show(struct device *dev, struct device_attribute *attr, char *buf)
235 struct cosm_device *cdev = dev_get_drvdata(dev);
241 firmware = cdev->firmware;
244 return scnprintf(buf, PAGE_SIZE, "%s\n", firmware);
249 firmware_store(struct device *dev, struct device_attribute *attr,
250 const char *buf, size_t count)
252 struct cosm_device *cdev = dev_get_drvdata(dev);
257 mutex_lock(&cdev->cosm_mutex);
258 kfree(cdev->firmware);
260 cdev->firmware = kmalloc(count + 1, GFP_KERNEL);
261 if (!cdev->firmware) {
265 strncpy(cdev->firmware, buf, count);
267 if (cdev->firmware[count - 1] == '\n')
268 cdev->firmware[count - 1] = '\0';
270 cdev->firmware[count] = '\0';
272 mutex_unlock(&cdev->cosm_mutex);
275 static DEVICE_ATTR_RW(firmware);
278 ramdisk_show(struct device *dev, struct device_attribute *attr, char *buf)
280 struct cosm_device *cdev = dev_get_drvdata(dev);
286 ramdisk = cdev->ramdisk;
289 return scnprintf(buf, PAGE_SIZE, "%s\n", ramdisk);
294 ramdisk_store(struct device *dev, struct device_attribute *attr,
295 const char *buf, size_t count)
297 struct cosm_device *cdev = dev_get_drvdata(dev);
302 mutex_lock(&cdev->cosm_mutex);
303 kfree(cdev->ramdisk);
305 cdev->ramdisk = kmalloc(count + 1, GFP_KERNEL);
306 if (!cdev->ramdisk) {
311 strncpy(cdev->ramdisk, buf, count);
313 if (cdev->ramdisk[count - 1] == '\n')
314 cdev->ramdisk[count - 1] = '\0';
316 cdev->ramdisk[count] = '\0';
318 mutex_unlock(&cdev->cosm_mutex);
321 static DEVICE_ATTR_RW(ramdisk);
324 bootmode_show(struct device *dev, struct device_attribute *attr, char *buf)
326 struct cosm_device *cdev = dev_get_drvdata(dev);
332 bootmode = cdev->bootmode;
335 return scnprintf(buf, PAGE_SIZE, "%s\n", bootmode);
340 bootmode_store(struct device *dev, struct device_attribute *attr,
341 const char *buf, size_t count)
343 struct cosm_device *cdev = dev_get_drvdata(dev);
348 if (!sysfs_streq(buf, "linux") && !sysfs_streq(buf, "flash"))
351 mutex_lock(&cdev->cosm_mutex);
352 kfree(cdev->bootmode);
354 cdev->bootmode = kmalloc(count + 1, GFP_KERNEL);
355 if (!cdev->bootmode) {
360 strncpy(cdev->bootmode, buf, count);
362 if (cdev->bootmode[count - 1] == '\n')
363 cdev->bootmode[count - 1] = '\0';
365 cdev->bootmode[count] = '\0';
367 mutex_unlock(&cdev->cosm_mutex);
370 static DEVICE_ATTR_RW(bootmode);
373 log_buf_addr_show(struct device *dev, struct device_attribute *attr,
376 struct cosm_device *cdev = dev_get_drvdata(dev);
381 return scnprintf(buf, PAGE_SIZE, "%p\n", cdev->log_buf_addr);
385 log_buf_addr_store(struct device *dev, struct device_attribute *attr,
386 const char *buf, size_t count)
388 struct cosm_device *cdev = dev_get_drvdata(dev);
395 ret = kstrtoul(buf, 16, &addr);
399 cdev->log_buf_addr = (void *)addr;
404 static DEVICE_ATTR_RW(log_buf_addr);
407 log_buf_len_show(struct device *dev, struct device_attribute *attr,
410 struct cosm_device *cdev = dev_get_drvdata(dev);
415 return scnprintf(buf, PAGE_SIZE, "%p\n", cdev->log_buf_len);
419 log_buf_len_store(struct device *dev, struct device_attribute *attr,
420 const char *buf, size_t count)
422 struct cosm_device *cdev = dev_get_drvdata(dev);
429 ret = kstrtoul(buf, 16, &addr);
433 cdev->log_buf_len = (int *)addr;
438 static DEVICE_ATTR_RW(log_buf_len);
440 static struct attribute *cosm_default_attrs[] = {
441 &dev_attr_family.attr,
442 &dev_attr_stepping.attr,
443 &dev_attr_state.attr,
444 &dev_attr_shutdown_status.attr,
445 &dev_attr_heartbeat_enable.attr,
446 &dev_attr_cmdline.attr,
447 &dev_attr_firmware.attr,
448 &dev_attr_ramdisk.attr,
449 &dev_attr_bootmode.attr,
450 &dev_attr_log_buf_addr.attr,
451 &dev_attr_log_buf_len.attr,
456 ATTRIBUTE_GROUPS(cosm_default);
458 void cosm_sysfs_init(struct cosm_device *cdev)
460 cdev->attr_group = cosm_default_groups;