1 // SPDX-License-Identifier: GPL-2.0-only
3 #define pr_fmt(fmt) "papr-vpd: " fmt
5 #include <linux/anon_inodes.h>
6 #include <linux/build_bug.h>
7 #include <linux/file.h>
9 #include <linux/init.h>
10 #include <linux/lockdep.h>
11 #include <linux/kernel.h>
12 #include <linux/miscdevice.h>
13 #include <linux/signal.h>
14 #include <linux/slab.h>
15 #include <linux/string.h>
16 #include <linux/string_helpers.h>
17 #include <linux/uaccess.h>
18 #include <asm/machdep.h>
19 #include <asm/papr-vpd.h>
20 #include <asm/rtas-work-area.h>
22 #include <uapi/asm/papr-vpd.h>
25 * Function-specific return values for ibm,get-vpd, derived from PAPR+
26 * v2.13 7.3.20 "ibm,get-vpd RTAS Call".
28 #define RTAS_IBM_GET_VPD_COMPLETE 0 /* All VPD has been retrieved. */
29 #define RTAS_IBM_GET_VPD_MORE_DATA 1 /* More VPD is available. */
30 #define RTAS_IBM_GET_VPD_START_OVER -4 /* VPD changed, restart call sequence. */
33 * struct rtas_ibm_get_vpd_params - Parameters (in and out) for ibm,get-vpd.
34 * @loc_code: In: Caller-provided location code buffer. Must be RTAS-addressable.
35 * @work_area: In: Caller-provided work area buffer for results.
36 * @sequence: In: Sequence number. Out: Next sequence number.
37 * @written: Out: Bytes written by ibm,get-vpd to @work_area.
38 * @status: Out: RTAS call status.
40 struct rtas_ibm_get_vpd_params {
41 const struct papr_location_code *loc_code;
42 struct rtas_work_area *work_area;
49 * rtas_ibm_get_vpd() - Call ibm,get-vpd to fill a work area buffer.
50 * @params: See &struct rtas_ibm_get_vpd_params.
52 * Calls ibm,get-vpd until it errors or successfully deposits data
53 * into the supplied work area. Handles RTAS retry statuses. Maps RTAS
54 * error statuses to reasonable errno values.
56 * The caller is expected to invoke rtas_ibm_get_vpd() multiple times
57 * to retrieve all the VPD for the provided location code. Only one
58 * sequence should be in progress at any time; starting a new sequence
59 * will disrupt any sequence already in progress. Serialization of VPD
60 * retrieval sequences is the responsibility of the caller.
62 * The caller should inspect @params.status to determine whether more
63 * calls are needed to complete the sequence.
66 * Return: -ve on error, 0 otherwise.
68 static int rtas_ibm_get_vpd(struct rtas_ibm_get_vpd_params *params)
70 const struct papr_location_code *loc_code = params->loc_code;
71 struct rtas_work_area *work_area = params->work_area;
76 lockdep_assert_held(&rtas_ibm_get_vpd_lock);
79 fwrc = rtas_call(rtas_function_token(RTAS_FN_IBM_GET_VPD), 4, 3,
82 rtas_work_area_phys(work_area),
83 rtas_work_area_size(work_area),
85 } while (rtas_busy_delay(fwrc));
88 case RTAS_HARDWARE_ERROR:
91 case RTAS_INVALID_PARAMETER:
94 case RTAS_IBM_GET_VPD_START_OVER:
97 case RTAS_IBM_GET_VPD_MORE_DATA:
98 params->sequence = rets[0];
100 case RTAS_IBM_GET_VPD_COMPLETE:
101 params->written = rets[1];
103 * Kernel or firmware bug, do not continue.
105 if (WARN(params->written > rtas_work_area_size(work_area),
106 "possible write beyond end of work area"))
113 pr_err_ratelimited("unexpected ibm,get-vpd status %d\n", fwrc);
117 params->status = fwrc;
122 * Internal VPD "blob" APIs for accumulating ibm,get-vpd results into
123 * an immutable buffer to be attached to a file descriptor.
130 static bool vpd_blob_has_data(const struct vpd_blob *blob)
132 return blob->data && blob->len;
135 static void vpd_blob_free(const struct vpd_blob *blob)
144 * vpd_blob_extend() - Append data to a &struct vpd_blob.
145 * @blob: The blob to extend.
146 * @data: The new data to append to @blob.
147 * @len: The length of @data.
149 * Context: May sleep.
150 * Return: -ENOMEM on allocation failure, 0 otherwise.
152 static int vpd_blob_extend(struct vpd_blob *blob, const char *data, size_t len)
154 const size_t new_len = blob->len + len;
155 const size_t old_len = blob->len;
156 const char *old_ptr = blob->data;
160 kvrealloc(old_ptr, old_len, new_len, GFP_KERNEL_ACCOUNT) :
161 kvmalloc(len, GFP_KERNEL_ACCOUNT);
166 memcpy(&new_ptr[old_len], data, len);
167 blob->data = new_ptr;
173 * vpd_blob_generate() - Construct a new &struct vpd_blob.
174 * @generator: Function that supplies the blob data.
175 * @arg: Context pointer supplied by caller, passed to @generator.
177 * The @generator callback is invoked until it returns NULL. @arg is
178 * passed to @generator in its first argument on each call. When
179 * @generator returns data, it should store the data length in its
182 * Context: May sleep.
183 * Return: A completely populated &struct vpd_blob, or NULL on error.
185 static const struct vpd_blob *
186 vpd_blob_generate(const char * (*generator)(void *, size_t *), void *arg)
188 struct vpd_blob *blob;
193 blob = kzalloc(sizeof(*blob), GFP_KERNEL_ACCOUNT);
197 while (err == 0 && (buf = generator(arg, &len)))
198 err = vpd_blob_extend(blob, buf, len);
200 if (err != 0 || !vpd_blob_has_data(blob))
210 * Internal VPD sequence APIs. A VPD sequence is a series of calls to
211 * ibm,get-vpd for a given location code. The sequence ends when an
212 * error is encountered or all VPD for the location code has been
217 * struct vpd_sequence - State for managing a VPD sequence.
218 * @error: Shall be zero as long as the sequence has not encountered an error,
219 * -ve errno otherwise. Use vpd_sequence_set_err() to update this.
220 * @params: Parameter block to pass to rtas_ibm_get_vpd().
222 struct vpd_sequence {
224 struct rtas_ibm_get_vpd_params params;
228 * vpd_sequence_begin() - Begin a VPD retrieval sequence.
229 * @seq: Uninitialized sequence state.
230 * @loc_code: Location code that defines the scope of the VPD to return.
232 * Initializes @seq with the resources necessary to carry out a VPD
233 * sequence. Callers must pass @seq to vpd_sequence_end() regardless
234 * of whether the sequence succeeds.
236 * Context: May sleep.
238 static void vpd_sequence_begin(struct vpd_sequence *seq,
239 const struct papr_location_code *loc_code)
242 * Use a static data structure for the location code passed to
243 * RTAS to ensure it's in the RMA and avoid a separate work
244 * area allocation. Guarded by the function lock.
246 static struct papr_location_code static_loc_code;
249 * We could allocate the work area before acquiring the
250 * function lock, but that would allow concurrent requests to
251 * exhaust the limited work area pool for no benefit. So
252 * allocate the work area under the lock.
254 mutex_lock(&rtas_ibm_get_vpd_lock);
255 static_loc_code = *loc_code;
256 *seq = (struct vpd_sequence) {
258 .work_area = rtas_work_area_alloc(SZ_4K),
259 .loc_code = &static_loc_code,
266 * vpd_sequence_end() - Finalize a VPD retrieval sequence.
267 * @seq: Sequence state.
269 * Releases resources obtained by vpd_sequence_begin().
271 static void vpd_sequence_end(struct vpd_sequence *seq)
273 rtas_work_area_free(seq->params.work_area);
274 mutex_unlock(&rtas_ibm_get_vpd_lock);
278 * vpd_sequence_should_stop() - Determine whether a VPD retrieval sequence
280 * @seq: VPD sequence state.
282 * Examines the sequence error state and outputs of the last call to
283 * ibm,get-vpd to determine whether the sequence in progress should
286 * Return: True if the sequence has encountered an error or if all VPD for
287 * this sequence has been retrieved. False otherwise.
289 static bool vpd_sequence_should_stop(const struct vpd_sequence *seq)
296 switch (seq->params.status) {
298 if (seq->params.written == 0)
299 done = false; /* Initial state. */
301 done = true; /* All data consumed. */
304 done = false; /* More data available. */
307 done = true; /* Error encountered. */
314 static int vpd_sequence_set_err(struct vpd_sequence *seq, int err)
316 /* Preserve the first error recorded. */
324 * Generator function to be passed to vpd_blob_generate().
326 static const char *vpd_sequence_fill_work_area(void *arg, size_t *len)
328 struct vpd_sequence *seq = arg;
329 struct rtas_ibm_get_vpd_params *p = &seq->params;
331 if (vpd_sequence_should_stop(seq))
333 if (vpd_sequence_set_err(seq, rtas_ibm_get_vpd(p)))
336 return rtas_work_area_raw_buf(p->work_area);
340 * Higher-level VPD retrieval code below. These functions use the
341 * vpd_blob_* and vpd_sequence_* APIs defined above to create fd-based
342 * VPD handles for consumption by user space.
346 * papr_vpd_run_sequence() - Run a single VPD retrieval sequence.
347 * @loc_code: Location code that defines the scope of VPD to return.
349 * Context: May sleep. Holds a mutex and an RTAS work area for its
350 * duration. Typically performs multiple sleepable slab
353 * Return: A populated &struct vpd_blob on success. Encoded error
356 static const struct vpd_blob *papr_vpd_run_sequence(const struct papr_location_code *loc_code)
358 const struct vpd_blob *blob;
359 struct vpd_sequence seq;
361 vpd_sequence_begin(&seq, loc_code);
362 blob = vpd_blob_generate(vpd_sequence_fill_work_area, &seq);
364 vpd_sequence_set_err(&seq, -ENOMEM);
365 vpd_sequence_end(&seq);
369 return ERR_PTR(seq.error);
376 * papr_vpd_retrieve() - Return the VPD for a location code.
377 * @loc_code: Location code that defines the scope of VPD to return.
379 * Run VPD sequences against @loc_code until a blob is successfully
380 * instantiated, or a hard error is encountered, or a fatal signal is
383 * Context: May sleep.
384 * Return: A fully populated VPD blob when successful. Encoded error
387 static const struct vpd_blob *papr_vpd_retrieve(const struct papr_location_code *loc_code)
389 const struct vpd_blob *blob;
392 * EAGAIN means the sequence errored with a -4 (VPD changed)
393 * status from ibm,get-vpd, and we should attempt a new
394 * sequence. PAPR+ v2.13 R1–7.3.20–5 indicates that this
395 * should be a transient condition, not something that happens
396 * continuously. But we'll stop trying on a fatal signal.
399 blob = papr_vpd_run_sequence(loc_code);
400 if (!IS_ERR(blob)) /* Success. */
402 if (PTR_ERR(blob) != -EAGAIN) /* Hard error. */
404 pr_info_ratelimited("VPD changed during retrieval, retrying\n");
406 } while (!fatal_signal_pending(current));
411 static ssize_t papr_vpd_handle_read(struct file *file, char __user *buf, size_t size, loff_t *off)
413 const struct vpd_blob *blob = file->private_data;
415 /* bug: we should not instantiate a handle without any data attached. */
416 if (!vpd_blob_has_data(blob)) {
417 pr_err_once("handle without data\n");
421 return simple_read_from_buffer(buf, size, off, blob->data, blob->len);
424 static int papr_vpd_handle_release(struct inode *inode, struct file *file)
426 const struct vpd_blob *blob = file->private_data;
433 static loff_t papr_vpd_handle_seek(struct file *file, loff_t off, int whence)
435 const struct vpd_blob *blob = file->private_data;
437 return fixed_size_llseek(file, off, whence, blob->len);
441 static const struct file_operations papr_vpd_handle_ops = {
442 .read = papr_vpd_handle_read,
443 .llseek = papr_vpd_handle_seek,
444 .release = papr_vpd_handle_release,
448 * papr_vpd_create_handle() - Create a fd-based handle for reading VPD.
449 * @ulc: Location code in user memory; defines the scope of the VPD to
452 * Handler for PAPR_VPD_IOC_CREATE_HANDLE ioctl command. Validates
453 * @ulc and instantiates an immutable VPD "blob" for it. The blob is
454 * attached to a file descriptor for reading by user space. The memory
455 * backing the blob is freed when the file is released.
457 * The entire requested VPD is retrieved by this call and all
458 * necessary RTAS interactions are performed before returning the fd
459 * to user space. This keeps the read handler simple and ensures that
460 * the kernel can prevent interleaving of ibm,get-vpd call sequences.
462 * Return: The installed fd number if successful, -ve errno otherwise.
464 static long papr_vpd_create_handle(struct papr_location_code __user *ulc)
466 struct papr_location_code klc;
467 const struct vpd_blob *blob;
472 if (copy_from_user(&klc, ulc, sizeof(klc)))
475 if (!string_is_terminated(klc.str, ARRAY_SIZE(klc.str)))
478 blob = papr_vpd_retrieve(&klc);
480 return PTR_ERR(blob);
482 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
488 file = anon_inode_getfile("[papr-vpd]", &papr_vpd_handle_ops,
489 (void *)blob, O_RDONLY);
495 file->f_mode |= FMODE_LSEEK | FMODE_PREAD;
496 fd_install(fd, file);
506 * Top-level ioctl handler for /dev/papr-vpd.
508 static long papr_vpd_dev_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
510 void __user *argp = (__force void __user *)arg;
514 case PAPR_VPD_IOC_CREATE_HANDLE:
515 ret = papr_vpd_create_handle(argp);
524 static const struct file_operations papr_vpd_ops = {
525 .unlocked_ioctl = papr_vpd_dev_ioctl,
528 static struct miscdevice papr_vpd_dev = {
529 .minor = MISC_DYNAMIC_MINOR,
531 .fops = &papr_vpd_ops,
534 static __init int papr_vpd_init(void)
536 if (!rtas_function_implemented(RTAS_FN_IBM_GET_VPD))
539 return misc_register(&papr_vpd_dev);
541 machine_device_initcall(pseries, papr_vpd_init);