Merge remote-tracking branches 'spi/fix/img-spfi' and 'spi/fix/msiof' into spi-linus
[cascardo/linux.git] / drivers / misc / genwqe / card_sysfs.c
1 /**
2  * IBM Accelerator Family 'GenWQE'
3  *
4  * (C) Copyright IBM Corp. 2013
5  *
6  * Author: Frank Haverkamp <haver@linux.vnet.ibm.com>
7  * Author: Joerg-Stephan Vogt <jsvogt@de.ibm.com>
8  * Author: Michael Jung <mijung@gmx.net>
9  * Author: Michael Ruettger <michael@ibmra.de>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License (version 2 only)
13  * as published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  */
20
21 /*
22  * Sysfs interfaces for the GenWQE card. There are attributes to query
23  * the version of the bitstream as well as some for the driver. For
24  * debugging, please also see the debugfs interfaces of this driver.
25  */
26
27 #include <linux/version.h>
28 #include <linux/kernel.h>
29 #include <linux/types.h>
30 #include <linux/module.h>
31 #include <linux/pci.h>
32 #include <linux/string.h>
33 #include <linux/fs.h>
34 #include <linux/sysfs.h>
35 #include <linux/ctype.h>
36 #include <linux/device.h>
37
38 #include "card_base.h"
39 #include "card_ddcb.h"
40
41 static const char * const genwqe_types[] = {
42         [GENWQE_TYPE_ALTERA_230] = "GenWQE4-230",
43         [GENWQE_TYPE_ALTERA_530] = "GenWQE4-530",
44         [GENWQE_TYPE_ALTERA_A4]  = "GenWQE5-A4",
45         [GENWQE_TYPE_ALTERA_A7]  = "GenWQE5-A7",
46 };
47
48 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
49                            char *buf)
50 {
51         struct genwqe_dev *cd = dev_get_drvdata(dev);
52         const char *cs[GENWQE_CARD_STATE_MAX] = { "unused", "used", "error" };
53
54         return sprintf(buf, "%s\n", cs[cd->card_state]);
55 }
56 static DEVICE_ATTR_RO(status);
57
58 static ssize_t appid_show(struct device *dev, struct device_attribute *attr,
59                           char *buf)
60 {
61         char app_name[5];
62         struct genwqe_dev *cd = dev_get_drvdata(dev);
63
64         genwqe_read_app_id(cd, app_name, sizeof(app_name));
65         return sprintf(buf, "%s\n", app_name);
66 }
67 static DEVICE_ATTR_RO(appid);
68
69 static ssize_t version_show(struct device *dev, struct device_attribute *attr,
70                             char *buf)
71 {
72         u64 slu_id, app_id;
73         struct genwqe_dev *cd = dev_get_drvdata(dev);
74
75         slu_id = __genwqe_readq(cd, IO_SLU_UNITCFG);
76         app_id = __genwqe_readq(cd, IO_APP_UNITCFG);
77
78         return sprintf(buf, "%016llx.%016llx\n", slu_id, app_id);
79 }
80 static DEVICE_ATTR_RO(version);
81
82 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
83                          char *buf)
84 {
85         u8 card_type;
86         struct genwqe_dev *cd = dev_get_drvdata(dev);
87
88         card_type = genwqe_card_type(cd);
89         return sprintf(buf, "%s\n", (card_type >= ARRAY_SIZE(genwqe_types)) ?
90                        "invalid" : genwqe_types[card_type]);
91 }
92 static DEVICE_ATTR_RO(type);
93
94 static ssize_t tempsens_show(struct device *dev, struct device_attribute *attr,
95                              char *buf)
96 {
97         u64 tempsens;
98         struct genwqe_dev *cd = dev_get_drvdata(dev);
99
100         tempsens = __genwqe_readq(cd, IO_SLU_TEMPERATURE_SENSOR);
101         return sprintf(buf, "%016llx\n", tempsens);
102 }
103 static DEVICE_ATTR_RO(tempsens);
104
105 static ssize_t freerunning_timer_show(struct device *dev,
106                                       struct device_attribute *attr,
107                                       char *buf)
108 {
109         u64 t;
110         struct genwqe_dev *cd = dev_get_drvdata(dev);
111
112         t = __genwqe_readq(cd, IO_SLC_FREE_RUNNING_TIMER);
113         return sprintf(buf, "%016llx\n", t);
114 }
115 static DEVICE_ATTR_RO(freerunning_timer);
116
117 static ssize_t queue_working_time_show(struct device *dev,
118                                        struct device_attribute *attr,
119                                        char *buf)
120 {
121         u64 t;
122         struct genwqe_dev *cd = dev_get_drvdata(dev);
123
124         t = __genwqe_readq(cd, IO_SLC_QUEUE_WTIME);
125         return sprintf(buf, "%016llx\n", t);
126 }
127 static DEVICE_ATTR_RO(queue_working_time);
128
129 static ssize_t base_clock_show(struct device *dev,
130                                struct device_attribute *attr,
131                                char *buf)
132 {
133         u64 base_clock;
134         struct genwqe_dev *cd = dev_get_drvdata(dev);
135
136         base_clock = genwqe_base_clock_frequency(cd);
137         return sprintf(buf, "%lld\n", base_clock);
138 }
139 static DEVICE_ATTR_RO(base_clock);
140
141 /**
142  * curr_bitstream_show() - Show the current bitstream id
143  *
144  * There is a bug in some old versions of the CPLD which selects the
145  * bitstream, which causes the IO_SLU_BITSTREAM register to report
146  * unreliable data in very rare cases. This makes this sysfs
147  * unreliable up to the point were a new CPLD version is being used.
148  *
149  * Unfortunately there is no automatic way yet to query the CPLD
150  * version, such that you need to manually ensure via programming
151  * tools that you have a recent version of the CPLD software.
152  *
153  * The proposed circumvention is to use a special recovery bitstream
154  * on the backup partition (0) to identify problems while loading the
155  * image.
156  */
157 static ssize_t curr_bitstream_show(struct device *dev,
158                                    struct device_attribute *attr, char *buf)
159 {
160         int curr_bitstream;
161         struct genwqe_dev *cd = dev_get_drvdata(dev);
162
163         curr_bitstream = __genwqe_readq(cd, IO_SLU_BITSTREAM) & 0x1;
164         return sprintf(buf, "%d\n", curr_bitstream);
165 }
166 static DEVICE_ATTR_RO(curr_bitstream);
167
168 /**
169  * next_bitstream_show() - Show the next activated bitstream
170  *
171  * IO_SLC_CFGREG_SOFTRESET: This register can only be accessed by the PF.
172  */
173 static ssize_t next_bitstream_show(struct device *dev,
174                                    struct device_attribute *attr, char *buf)
175 {
176         int next_bitstream;
177         struct genwqe_dev *cd = dev_get_drvdata(dev);
178
179         switch ((cd->softreset & 0xc) >> 2) {
180         case 0x2:
181                 next_bitstream =  0;
182                 break;
183         case 0x3:
184                 next_bitstream =  1;
185                 break;
186         default:
187                 next_bitstream = -1;
188                 break;          /* error */
189         }
190         return sprintf(buf, "%d\n", next_bitstream);
191 }
192
193 static ssize_t next_bitstream_store(struct device *dev,
194                                     struct device_attribute *attr,
195                                     const char *buf, size_t count)
196 {
197         int partition;
198         struct genwqe_dev *cd = dev_get_drvdata(dev);
199
200         if (kstrtoint(buf, 0, &partition) < 0)
201                 return -EINVAL;
202
203         switch (partition) {
204         case 0x0:
205                 cd->softreset = 0x78;
206                 break;
207         case 0x1:
208                 cd->softreset = 0x7c;
209                 break;
210         default:
211                 return -EINVAL;
212         }
213
214         __genwqe_writeq(cd, IO_SLC_CFGREG_SOFTRESET, cd->softreset);
215         return count;
216 }
217 static DEVICE_ATTR_RW(next_bitstream);
218
219 static ssize_t reload_bitstream_store(struct device *dev,
220                                 struct device_attribute *attr,
221                                 const char *buf, size_t count)
222 {
223         int reload;
224         struct genwqe_dev *cd = dev_get_drvdata(dev);
225
226         if (kstrtoint(buf, 0, &reload) < 0)
227                 return -EINVAL;
228
229         if (reload == 0x1) {
230                 if (cd->card_state == GENWQE_CARD_UNUSED ||
231                     cd->card_state == GENWQE_CARD_USED)
232                         cd->card_state = GENWQE_CARD_RELOAD_BITSTREAM;
233                 else
234                         return -EIO;
235         } else {
236                 return -EINVAL;
237         }
238
239         return count;
240 }
241 static DEVICE_ATTR_WO(reload_bitstream);
242
243 /*
244  * Create device_attribute structures / params: name, mode, show, store
245  * additional flag if valid in VF
246  */
247 static struct attribute *genwqe_attributes[] = {
248         &dev_attr_tempsens.attr,
249         &dev_attr_next_bitstream.attr,
250         &dev_attr_curr_bitstream.attr,
251         &dev_attr_base_clock.attr,
252         &dev_attr_type.attr,
253         &dev_attr_version.attr,
254         &dev_attr_appid.attr,
255         &dev_attr_status.attr,
256         &dev_attr_freerunning_timer.attr,
257         &dev_attr_queue_working_time.attr,
258         &dev_attr_reload_bitstream.attr,
259         NULL,
260 };
261
262 static struct attribute *genwqe_normal_attributes[] = {
263         &dev_attr_type.attr,
264         &dev_attr_version.attr,
265         &dev_attr_appid.attr,
266         &dev_attr_status.attr,
267         &dev_attr_freerunning_timer.attr,
268         &dev_attr_queue_working_time.attr,
269         NULL,
270 };
271
272 /**
273  * genwqe_is_visible() - Determine if sysfs attribute should be visible or not
274  *
275  * VFs have restricted mmio capabilities, so not all sysfs entries
276  * are allowed in VFs.
277  */
278 static umode_t genwqe_is_visible(struct kobject *kobj,
279                                  struct attribute *attr, int n)
280 {
281         unsigned int j;
282         struct device *dev = container_of(kobj, struct device, kobj);
283         struct genwqe_dev *cd = dev_get_drvdata(dev);
284         umode_t mode = attr->mode;
285
286         if (genwqe_is_privileged(cd))
287                 return mode;
288
289         for (j = 0; genwqe_normal_attributes[j] != NULL;  j++)
290                 if (genwqe_normal_attributes[j] == attr)
291                         return mode;
292
293         return 0;
294 }
295
296 static struct attribute_group genwqe_attribute_group = {
297         .is_visible = genwqe_is_visible,
298         .attrs      = genwqe_attributes,
299 };
300
301 const struct attribute_group *genwqe_attribute_groups[] = {
302         &genwqe_attribute_group,
303         NULL,
304 };