1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
// SPDX-License-Identifier: GPL-2.0-only
#define pr_fmt(fmt) "papr-vpd: " fmt
#include <linux/build_bug.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/lockdep.h>
#include <linux/kernel.h>
#include <linux/miscdevice.h>
#include <linux/signal.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/string_helpers.h>
#include <linux/uaccess.h>
#include <asm/machdep.h>
#include <asm/papr-vpd.h>
#include <asm/rtas-work-area.h>
#include <asm/rtas.h>
#include <uapi/asm/papr-vpd.h>
#include "papr-rtas-common.h"
/**
* struct rtas_ibm_get_vpd_params - Parameters (in and out) for ibm,get-vpd.
* @loc_code: In: Caller-provided location code buffer. Must be RTAS-addressable.
* @work_area: In: Caller-provided work area buffer for results.
* @sequence: In: Sequence number. Out: Next sequence number.
* @written: Out: Bytes written by ibm,get-vpd to @work_area.
* @status: Out: RTAS call status.
*/
struct rtas_ibm_get_vpd_params {
const struct papr_location_code *loc_code;
struct rtas_work_area *work_area;
u32 sequence;
u32 written;
s32 status;
};
/**
* rtas_ibm_get_vpd() - Call ibm,get-vpd to fill a work area buffer.
* @params: See &struct rtas_ibm_get_vpd_params.
*
* Calls ibm,get-vpd until it errors or successfully deposits data
* into the supplied work area. Handles RTAS retry statuses. Maps RTAS
* error statuses to reasonable errno values.
*
* The caller is expected to invoke rtas_ibm_get_vpd() multiple times
* to retrieve all the VPD for the provided location code. Only one
* sequence should be in progress at any time; starting a new sequence
* will disrupt any sequence already in progress. Serialization of VPD
* retrieval sequences is the responsibility of the caller.
*
* The caller should inspect @params.status to determine whether more
* calls are needed to complete the sequence.
*
* Context: May sleep.
* Return: -ve on error, 0 otherwise.
*/
static int rtas_ibm_get_vpd(struct rtas_ibm_get_vpd_params *params)
{
const struct papr_location_code *loc_code = params->loc_code;
struct rtas_work_area *work_area = params->work_area;
u32 rets[2];
s32 fwrc;
int ret;
lockdep_assert_held(&rtas_ibm_get_vpd_lock);
do {
fwrc = rtas_call(rtas_function_token(RTAS_FN_IBM_GET_VPD), 4, 3,
rets,
__pa(loc_code),
rtas_work_area_phys(work_area),
rtas_work_area_size(work_area),
params->sequence);
} while (rtas_busy_delay(fwrc));
switch (fwrc) {
case RTAS_HARDWARE_ERROR:
ret = -EIO;
break;
case RTAS_INVALID_PARAMETER:
ret = -EINVAL;
break;
case RTAS_SEQ_START_OVER:
ret = -EAGAIN;
pr_info_ratelimited("VPD changed during retrieval, retrying\n");
break;
case RTAS_SEQ_MORE_DATA:
params->sequence = rets[0];
fallthrough;
case RTAS_SEQ_COMPLETE:
params->written = rets[1];
/*
* Kernel or firmware bug, do not continue.
*/
if (WARN(params->written > rtas_work_area_size(work_area),
"possible write beyond end of work area"))
ret = -EFAULT;
else
ret = 0;
break;
default:
ret = -EIO;
pr_err_ratelimited("unexpected ibm,get-vpd status %d\n", fwrc);
break;
}
params->status = fwrc;
return ret;
}
/*
* Internal VPD sequence APIs. A VPD sequence is a series of calls to
* ibm,get-vpd for a given location code. The sequence ends when an
* error is encountered or all VPD for the location code has been
* returned.
*/
/**
* vpd_sequence_begin() - Begin a VPD retrieval sequence.
* @seq: vpd call parameters from sequence struct
*
* Context: May sleep.
*/
static void vpd_sequence_begin(struct papr_rtas_sequence *seq)
{
struct rtas_ibm_get_vpd_params *vpd_params;
/*
* Use a static data structure for the location code passed to
* RTAS to ensure it's in the RMA and avoid a separate work
* area allocation. Guarded by the function lock.
*/
static struct papr_location_code static_loc_code;
vpd_params = (struct rtas_ibm_get_vpd_params *)seq->params;
/*
* We could allocate the work area before acquiring the
* function lock, but that would allow concurrent requests to
* exhaust the limited work area pool for no benefit. So
* allocate the work area under the lock.
*/
mutex_lock(&rtas_ibm_get_vpd_lock);
static_loc_code = *(struct papr_location_code *)vpd_params->loc_code;
vpd_params = (struct rtas_ibm_get_vpd_params *)seq->params;
vpd_params->work_area = rtas_work_area_alloc(SZ_4K);
vpd_params->loc_code = &static_loc_code;
vpd_params->sequence = 1;
vpd_params->status = 0;
}
/**
* vpd_sequence_end() - Finalize a VPD retrieval sequence.
* @seq: Sequence state.
*
* Releases resources obtained by vpd_sequence_begin().
*/
static void vpd_sequence_end(struct papr_rtas_sequence *seq)
{
struct rtas_ibm_get_vpd_params *vpd_params;
vpd_params = (struct rtas_ibm_get_vpd_params *)seq->params;
rtas_work_area_free(vpd_params->work_area);
mutex_unlock(&rtas_ibm_get_vpd_lock);
}
/*
* Generator function to be passed to papr_rtas_blob_generate().
*/
static const char *vpd_sequence_fill_work_area(struct papr_rtas_sequence *seq,
size_t *len)
{
struct rtas_ibm_get_vpd_params *p;
bool init_state;
p = (struct rtas_ibm_get_vpd_params *)seq->params;
init_state = (p->written == 0) ? true : false;
if (papr_rtas_sequence_should_stop(seq, p->status, init_state))
return NULL;
if (papr_rtas_sequence_set_err(seq, rtas_ibm_get_vpd(p)))
return NULL;
*len = p->written;
return rtas_work_area_raw_buf(p->work_area);
}
static const struct file_operations papr_vpd_handle_ops = {
.read = papr_rtas_common_handle_read,
.llseek = papr_rtas_common_handle_seek,
.release = papr_rtas_common_handle_release,
};
/**
* papr_vpd_create_handle() - Create a fd-based handle for reading VPD.
* @ulc: Location code in user memory; defines the scope of the VPD to
* retrieve.
*
* Handler for PAPR_VPD_IOC_CREATE_HANDLE ioctl command. Validates
* @ulc and instantiates an immutable VPD "blob" for it. The blob is
* attached to a file descriptor for reading by user space. The memory
* backing the blob is freed when the file is released.
*
* The entire requested VPD is retrieved by this call and all
* necessary RTAS interactions are performed before returning the fd
* to user space. This keeps the read handler simple and ensures that
* the kernel can prevent interleaving of ibm,get-vpd call sequences.
*
* Return: The installed fd number if successful, -ve errno otherwise.
*/
static long papr_vpd_create_handle(struct papr_location_code __user *ulc)
{
struct rtas_ibm_get_vpd_params vpd_params = {};
struct papr_rtas_sequence seq = {};
struct papr_location_code klc;
int fd;
if (copy_from_user(&klc, ulc, sizeof(klc)))
return -EFAULT;
if (!string_is_terminated(klc.str, ARRAY_SIZE(klc.str)))
return -EINVAL;
seq = (struct papr_rtas_sequence) {
.begin = vpd_sequence_begin,
.end = vpd_sequence_end,
.work = vpd_sequence_fill_work_area,
};
vpd_params.loc_code = &klc;
seq.params = (void *)&vpd_params;
fd = papr_rtas_setup_file_interface(&seq, &papr_vpd_handle_ops,
"[papr-vpd]");
return fd;
}
/*
* Top-level ioctl handler for /dev/papr-vpd.
*/
static long papr_vpd_dev_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
{
void __user *argp = (__force void __user *)arg;
long ret;
switch (ioctl) {
case PAPR_VPD_IOC_CREATE_HANDLE:
ret = papr_vpd_create_handle(argp);
break;
default:
ret = -ENOIOCTLCMD;
break;
}
return ret;
}
static const struct file_operations papr_vpd_ops = {
.unlocked_ioctl = papr_vpd_dev_ioctl,
};
static struct miscdevice papr_vpd_dev = {
.minor = MISC_DYNAMIC_MINOR,
.name = "papr-vpd",
.fops = &papr_vpd_ops,
};
static __init int papr_vpd_init(void)
{
if (!rtas_function_implemented(RTAS_FN_IBM_GET_VPD))
return -ENODEV;
return misc_register(&papr_vpd_dev);
}
machine_device_initcall(pseries, papr_vpd_init);
|