xen/blkback: rework connect_ring() to avoid inconsistent xenstore 'ring-page-order...
[linux-2.6-microblaze.git] / arch / powerpc / platforms / pseries / papr_scm.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #define pr_fmt(fmt)     "papr-scm: " fmt
4
5 #include <linux/of.h>
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/ioport.h>
9 #include <linux/slab.h>
10 #include <linux/ndctl.h>
11 #include <linux/sched.h>
12 #include <linux/libnvdimm.h>
13 #include <linux/platform_device.h>
14
15 #include <asm/plpar_wrappers.h>
16
17 #define BIND_ANY_ADDR (~0ul)
18
19 #define PAPR_SCM_DIMM_CMD_MASK \
20         ((1ul << ND_CMD_GET_CONFIG_SIZE) | \
21          (1ul << ND_CMD_GET_CONFIG_DATA) | \
22          (1ul << ND_CMD_SET_CONFIG_DATA))
23
24 struct papr_scm_priv {
25         struct platform_device *pdev;
26         struct device_node *dn;
27         uint32_t drc_index;
28         uint64_t blocks;
29         uint64_t block_size;
30         int metadata_size;
31
32         uint64_t bound_addr;
33
34         struct nvdimm_bus_descriptor bus_desc;
35         struct nvdimm_bus *bus;
36         struct nvdimm *nvdimm;
37         struct resource res;
38         struct nd_region *region;
39         struct nd_interleave_set nd_set;
40 };
41
42 static int drc_pmem_bind(struct papr_scm_priv *p)
43 {
44         unsigned long ret[PLPAR_HCALL_BUFSIZE];
45         uint64_t rc, token;
46
47         /*
48          * When the hypervisor cannot map all the requested memory in a single
49          * hcall it returns H_BUSY and we call again with the token until
50          * we get H_SUCCESS. Aborting the retry loop before getting H_SUCCESS
51          * leave the system in an undefined state, so we wait.
52          */
53         token = 0;
54
55         do {
56                 rc = plpar_hcall(H_SCM_BIND_MEM, ret, p->drc_index, 0,
57                                 p->blocks, BIND_ANY_ADDR, token);
58                 token = ret[0];
59                 cond_resched();
60         } while (rc == H_BUSY);
61
62         if (rc) {
63                 dev_err(&p->pdev->dev, "bind err: %lld\n", rc);
64                 return -ENXIO;
65         }
66
67         p->bound_addr = ret[1];
68
69         dev_dbg(&p->pdev->dev, "bound drc %x to %pR\n", p->drc_index, &p->res);
70
71         return 0;
72 }
73
74 static int drc_pmem_unbind(struct papr_scm_priv *p)
75 {
76         unsigned long ret[PLPAR_HCALL_BUFSIZE];
77         uint64_t rc, token;
78
79         token = 0;
80
81         /* NB: unbind has the same retry requirements mentioned above */
82         do {
83                 rc = plpar_hcall(H_SCM_UNBIND_MEM, ret, p->drc_index,
84                                 p->bound_addr, p->blocks, token);
85                 token = ret[0];
86                 cond_resched();
87         } while (rc == H_BUSY);
88
89         if (rc)
90                 dev_err(&p->pdev->dev, "unbind error: %lld\n", rc);
91
92         return !!rc;
93 }
94
95 static int papr_scm_meta_get(struct papr_scm_priv *p,
96                         struct nd_cmd_get_config_data_hdr *hdr)
97 {
98         unsigned long data[PLPAR_HCALL_BUFSIZE];
99         int64_t ret;
100
101         if (hdr->in_offset >= p->metadata_size || hdr->in_length != 1)
102                 return -EINVAL;
103
104         ret = plpar_hcall(H_SCM_READ_METADATA, data, p->drc_index,
105                         hdr->in_offset, 1);
106
107         if (ret == H_PARAMETER) /* bad DRC index */
108                 return -ENODEV;
109         if (ret)
110                 return -EINVAL; /* other invalid parameter */
111
112         hdr->out_buf[0] = data[0] & 0xff;
113
114         return 0;
115 }
116
117 static int papr_scm_meta_set(struct papr_scm_priv *p,
118                         struct nd_cmd_set_config_hdr *hdr)
119 {
120         int64_t ret;
121
122         if (hdr->in_offset >= p->metadata_size || hdr->in_length != 1)
123                 return -EINVAL;
124
125         ret = plpar_hcall_norets(H_SCM_WRITE_METADATA,
126                         p->drc_index, hdr->in_offset, hdr->in_buf[0], 1);
127
128         if (ret == H_PARAMETER) /* bad DRC index */
129                 return -ENODEV;
130         if (ret)
131                 return -EINVAL; /* other invalid parameter */
132
133         return 0;
134 }
135
136 int papr_scm_ndctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
137                 unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc)
138 {
139         struct nd_cmd_get_config_size *get_size_hdr;
140         struct papr_scm_priv *p;
141
142         /* Only dimm-specific calls are supported atm */
143         if (!nvdimm)
144                 return -EINVAL;
145
146         p = nvdimm_provider_data(nvdimm);
147
148         switch (cmd) {
149         case ND_CMD_GET_CONFIG_SIZE:
150                 get_size_hdr = buf;
151
152                 get_size_hdr->status = 0;
153                 get_size_hdr->max_xfer = 1;
154                 get_size_hdr->config_size = p->metadata_size;
155                 *cmd_rc = 0;
156                 break;
157
158         case ND_CMD_GET_CONFIG_DATA:
159                 *cmd_rc = papr_scm_meta_get(p, buf);
160                 break;
161
162         case ND_CMD_SET_CONFIG_DATA:
163                 *cmd_rc = papr_scm_meta_set(p, buf);
164                 break;
165
166         default:
167                 return -EINVAL;
168         }
169
170         dev_dbg(&p->pdev->dev, "returned with cmd_rc = %d\n", *cmd_rc);
171
172         return 0;
173 }
174
175 static const struct attribute_group *region_attr_groups[] = {
176         &nd_region_attribute_group,
177         &nd_device_attribute_group,
178         &nd_mapping_attribute_group,
179         &nd_numa_attribute_group,
180         NULL,
181 };
182
183 static const struct attribute_group *bus_attr_groups[] = {
184         &nvdimm_bus_attribute_group,
185         NULL,
186 };
187
188 static const struct attribute_group *papr_scm_dimm_groups[] = {
189         &nvdimm_attribute_group,
190         &nd_device_attribute_group,
191         NULL,
192 };
193
194 static int papr_scm_nvdimm_init(struct papr_scm_priv *p)
195 {
196         struct device *dev = &p->pdev->dev;
197         struct nd_mapping_desc mapping;
198         struct nd_region_desc ndr_desc;
199         unsigned long dimm_flags;
200
201         p->bus_desc.ndctl = papr_scm_ndctl;
202         p->bus_desc.module = THIS_MODULE;
203         p->bus_desc.of_node = p->pdev->dev.of_node;
204         p->bus_desc.attr_groups = bus_attr_groups;
205         p->bus_desc.provider_name = kstrdup(p->pdev->name, GFP_KERNEL);
206
207         if (!p->bus_desc.provider_name)
208                 return -ENOMEM;
209
210         p->bus = nvdimm_bus_register(NULL, &p->bus_desc);
211         if (!p->bus) {
212                 dev_err(dev, "Error creating nvdimm bus %pOF\n", p->dn);
213                 return -ENXIO;
214         }
215
216         dimm_flags = 0;
217         set_bit(NDD_ALIASING, &dimm_flags);
218
219         p->nvdimm = nvdimm_create(p->bus, p, papr_scm_dimm_groups,
220                                 dimm_flags, PAPR_SCM_DIMM_CMD_MASK, 0, NULL);
221         if (!p->nvdimm) {
222                 dev_err(dev, "Error creating DIMM object for %pOF\n", p->dn);
223                 goto err;
224         }
225
226         if (nvdimm_bus_check_dimm_count(p->bus, 1))
227                 goto err;
228
229         /* now add the region */
230
231         memset(&mapping, 0, sizeof(mapping));
232         mapping.nvdimm = p->nvdimm;
233         mapping.start = 0;
234         mapping.size = p->blocks * p->block_size; // XXX: potential overflow?
235
236         memset(&ndr_desc, 0, sizeof(ndr_desc));
237         ndr_desc.attr_groups = region_attr_groups;
238         ndr_desc.numa_node = dev_to_node(&p->pdev->dev);
239         ndr_desc.res = &p->res;
240         ndr_desc.of_node = p->dn;
241         ndr_desc.provider_data = p;
242         ndr_desc.mapping = &mapping;
243         ndr_desc.num_mappings = 1;
244         ndr_desc.nd_set = &p->nd_set;
245         set_bit(ND_REGION_PAGEMAP, &ndr_desc.flags);
246
247         p->region = nvdimm_pmem_region_create(p->bus, &ndr_desc);
248         if (!p->region) {
249                 dev_err(dev, "Error registering region %pR from %pOF\n",
250                                 ndr_desc.res, p->dn);
251                 goto err;
252         }
253
254         return 0;
255
256 err:    nvdimm_bus_unregister(p->bus);
257         kfree(p->bus_desc.provider_name);
258         return -ENXIO;
259 }
260
261 static int papr_scm_probe(struct platform_device *pdev)
262 {
263         struct device_node *dn = pdev->dev.of_node;
264         u32 drc_index, metadata_size;
265         u64 blocks, block_size;
266         struct papr_scm_priv *p;
267         const char *uuid_str;
268         u64 uuid[2];
269         int rc;
270
271         /* check we have all the required DT properties */
272         if (of_property_read_u32(dn, "ibm,my-drc-index", &drc_index)) {
273                 dev_err(&pdev->dev, "%pOF: missing drc-index!\n", dn);
274                 return -ENODEV;
275         }
276
277         if (of_property_read_u64(dn, "ibm,block-size", &block_size)) {
278                 dev_err(&pdev->dev, "%pOF: missing block-size!\n", dn);
279                 return -ENODEV;
280         }
281
282         if (of_property_read_u64(dn, "ibm,number-of-blocks", &blocks)) {
283                 dev_err(&pdev->dev, "%pOF: missing number-of-blocks!\n", dn);
284                 return -ENODEV;
285         }
286
287         if (of_property_read_string(dn, "ibm,unit-guid", &uuid_str)) {
288                 dev_err(&pdev->dev, "%pOF: missing unit-guid!\n", dn);
289                 return -ENODEV;
290         }
291
292         p = kzalloc(sizeof(*p), GFP_KERNEL);
293         if (!p)
294                 return -ENOMEM;
295
296         /* optional DT properties */
297         of_property_read_u32(dn, "ibm,metadata-size", &metadata_size);
298
299         p->dn = dn;
300         p->drc_index = drc_index;
301         p->block_size = block_size;
302         p->blocks = blocks;
303
304         /* We just need to ensure that set cookies are unique across */
305         uuid_parse(uuid_str, (uuid_t *) uuid);
306         p->nd_set.cookie1 = uuid[0];
307         p->nd_set.cookie2 = uuid[1];
308
309         /* might be zero */
310         p->metadata_size = metadata_size;
311         p->pdev = pdev;
312
313         /* request the hypervisor to bind this region to somewhere in memory */
314         rc = drc_pmem_bind(p);
315         if (rc)
316                 goto err;
317
318         /* setup the resource for the newly bound range */
319         p->res.start = p->bound_addr;
320         p->res.end   = p->bound_addr + p->blocks * p->block_size - 1;
321         p->res.name  = pdev->name;
322         p->res.flags = IORESOURCE_MEM;
323
324         rc = papr_scm_nvdimm_init(p);
325         if (rc)
326                 goto err2;
327
328         platform_set_drvdata(pdev, p);
329
330         return 0;
331
332 err2:   drc_pmem_unbind(p);
333 err:    kfree(p);
334         return rc;
335 }
336
337 static int papr_scm_remove(struct platform_device *pdev)
338 {
339         struct papr_scm_priv *p = platform_get_drvdata(pdev);
340
341         nvdimm_bus_unregister(p->bus);
342         drc_pmem_unbind(p);
343         kfree(p);
344
345         return 0;
346 }
347
348 static const struct of_device_id papr_scm_match[] = {
349         { .compatible = "ibm,pmemory" },
350         { },
351 };
352
353 static struct platform_driver papr_scm_driver = {
354         .probe = papr_scm_probe,
355         .remove = papr_scm_remove,
356         .driver = {
357                 .name = "papr_scm",
358                 .owner = THIS_MODULE,
359                 .of_match_table = papr_scm_match,
360         },
361 };
362
363 module_platform_driver(papr_scm_driver);
364 MODULE_DEVICE_TABLE(of, papr_scm_match);
365 MODULE_LICENSE("GPL");
366 MODULE_AUTHOR("IBM Corporation");