1 // SPDX-License-Identifier: GPL-2.0-only
2
3 #define pr_fmt(fmt) "papr-hvpipe: " fmt
4
5 #include <linux/module.h>
6 #include <linux/kernel.h>
7 #include <linux/types.h>
8 #include <linux/delay.h>
9 #include <linux/anon_inodes.h>
10 #include <linux/miscdevice.h>
11 #include <linux/file.h>
12 #include <linux/fs.h>
13 #include <linux/poll.h>
14 #include <linux/of.h>
15 #include <asm/machdep.h>
16 #include <asm/rtas.h>
17 #include <asm/rtas-work-area.h>
18 #include <asm/papr-sysparm.h>
19 #include <uapi/asm/papr-hvpipe.h>
20 #include "pseries.h"
21 #include "papr-hvpipe.h"
22
23 static DEFINE_SPINLOCK(hvpipe_src_list_lock);
24 static LIST_HEAD(hvpipe_src_list);
25
26 static unsigned char hvpipe_ras_buf[RTAS_ERROR_LOG_MAX];
27 static struct workqueue_struct *papr_hvpipe_wq;
28 static struct work_struct *papr_hvpipe_work;
29 static int hvpipe_check_exception_token;
30 static bool hvpipe_feature;
31
32 /*
33 * New PowerPC FW provides support for partitions and various
34 * sources (Ex: remote hardware management console (HMC)) to
35 * exchange information through an inband hypervisor channel
36 * called HVPIPE. Only HMCs are supported right now and
37 * partitions can communicate with multiple HMCs and each
38 * source represented by source ID.
39 *
40 * FW introduces send HVPIPE and recv HVPIPE RTAS calls for
41 * partitions to send and receive payloads respectively.
42 *
43 * These RTAS functions have the following certain requirements
44 * / limitations:
45 * - One hvpipe per partition for all sources.
46 * - Assume the return status of send HVPIPE as delivered to source
47 * - Assume the return status of recv HVPIPE as ACK to source
48 * - Generates HVPIPE event message when the payload is ready
49 * for the partition. The hypervisor will not deliver another
50 * event until the partition read the previous payload which
51 * means the pipe is blocked for any sources.
52 *
53 * Linux implementation:
54 * Follow the similar interfaces that the OS has for other RTAS calls.
55 * ex: /dev/papr-indices, /dev/papr-vpd, etc.
56 * - /dev/papr-hvpipe is available for the user space.
57 * - devfd = open("/dev/papr-hvpipe", ..)
58 * - fd = ioctl(fd,HVPIPE_IOC_CREATE_HANDLE,&srcID)-for each source
59 * - write(fd, buf, size) --> Issue send HVPIPE RTAS call and
60 * returns size for success or the corresponding error for RTAS
61 * return code for failure.
62 * - poll(fd,..) -> wakeup FD if the payload is available to read.
63 * HVPIPE event message handler wakeup FD based on source ID in
64 * the event message
65 * - read(fd, buf, size) --> Issue recv HVPIPE RTAS call and
66 * returns size for success or the corresponding error for RTAS
67 * return code for failure.
68 */
69
70 /*
71 * ibm,receive-hvpipe-msg RTAS call.
72 * @area: Caller-provided work area buffer for results.
73 * @srcID: Source ID returned by the RTAS call.
74 * @bytesw: Bytes written by RTAS call to @area.
75 */
rtas_ibm_receive_hvpipe_msg(struct rtas_work_area * area,u32 * srcID,u32 * bytesw)76 static int rtas_ibm_receive_hvpipe_msg(struct rtas_work_area *area,
77 u32 *srcID, u32 *bytesw)
78 {
79 const s32 token = rtas_function_token(RTAS_FN_IBM_RECEIVE_HVPIPE_MSG);
80 u32 rets[2];
81 s32 fwrc;
82 int ret;
83
84 if (token == RTAS_UNKNOWN_SERVICE)
85 return -ENOENT;
86
87 do {
88 fwrc = rtas_call(token, 2, 3, rets,
89 rtas_work_area_phys(area),
90 rtas_work_area_size(area));
91
92 } while (rtas_busy_delay(fwrc));
93
94 switch (fwrc) {
95 case RTAS_SUCCESS:
96 *srcID = rets[0];
97 *bytesw = rets[1];
98 ret = 0;
99 break;
100 case RTAS_HARDWARE_ERROR:
101 ret = -EIO;
102 break;
103 case RTAS_INVALID_PARAMETER:
104 ret = -EINVAL;
105 break;
106 case RTAS_FUNC_NOT_SUPPORTED:
107 ret = -EOPNOTSUPP;
108 break;
109 default:
110 ret = -EIO;
111 pr_err_ratelimited("unexpected ibm,receive-hvpipe-msg status %d\n", fwrc);
112 break;
113 }
114
115 return ret;
116 }
117
118 /*
119 * ibm,send-hvpipe-msg RTAS call
120 * @area: Caller-provided work area buffer to send.
121 * @srcID: Target source for the send pipe message.
122 */
rtas_ibm_send_hvpipe_msg(struct rtas_work_area * area,u32 srcID)123 static int rtas_ibm_send_hvpipe_msg(struct rtas_work_area *area, u32 srcID)
124 {
125 const s32 token = rtas_function_token(RTAS_FN_IBM_SEND_HVPIPE_MSG);
126 s32 fwrc;
127 int ret;
128
129 if (token == RTAS_UNKNOWN_SERVICE)
130 return -ENOENT;
131
132 do {
133 fwrc = rtas_call(token, 2, 1, NULL, srcID,
134 rtas_work_area_phys(area));
135
136 } while (rtas_busy_delay(fwrc));
137
138 switch (fwrc) {
139 case RTAS_SUCCESS:
140 ret = 0;
141 break;
142 case RTAS_HARDWARE_ERROR:
143 ret = -EIO;
144 break;
145 case RTAS_INVALID_PARAMETER:
146 ret = -EINVAL;
147 break;
148 case RTAS_HVPIPE_CLOSED:
149 ret = -EPIPE;
150 break;
151 case RTAS_FUNC_NOT_SUPPORTED:
152 ret = -EOPNOTSUPP;
153 break;
154 default:
155 ret = -EIO;
156 pr_err_ratelimited("unexpected ibm,receive-hvpipe-msg status %d\n", fwrc);
157 break;
158 }
159
160 return ret;
161 }
162
hvpipe_find_source(u32 srcID)163 static struct hvpipe_source_info *hvpipe_find_source(u32 srcID)
164 {
165 struct hvpipe_source_info *src_info;
166
167 list_for_each_entry(src_info, &hvpipe_src_list, list)
168 if (src_info->srcID == srcID)
169 return src_info;
170
171 return NULL;
172 }
173
174 /*
175 * This work function collects receive buffer with recv HVPIPE
176 * RTAS call. Called from read()
177 * @buf: User specified buffer to copy the payload that returned
178 * from recv HVPIPE RTAS.
179 * @size: Size of buffer user passed.
180 */
hvpipe_rtas_recv_msg(char __user * buf,int size)181 static int hvpipe_rtas_recv_msg(char __user *buf, int size)
182 {
183 struct rtas_work_area *work_area;
184 u32 srcID, bytes_written;
185 int ret;
186
187 work_area = rtas_work_area_alloc(SZ_4K);
188 if (!work_area) {
189 pr_err("Could not allocate RTAS buffer for recv pipe\n");
190 return -ENOMEM;
191 }
192
193 /*
194 * Recv HVPIPE RTAS is successful.
195 * When releasing FD or no one is waiting on the
196 * specific source, issue recv HVPIPE RTAS call
197 * so that pipe is not blocked - this func is called
198 * with NULL buf.
199 */
200 ret = rtas_ibm_receive_hvpipe_msg(work_area, &srcID, &bytes_written);
201 if (ret) {
202 pr_err("ibm,receive-hvpipe-msg failed with %d\n", ret);
203 goto out;
204 }
205
206 if (!buf)
207 goto out;
208
209 if (size < bytes_written) {
210 pr_err("Received the payload size = %d, but the buffer size = %d\n",
211 bytes_written, size);
212 bytes_written = size;
213 }
214
215 if (copy_to_user(buf, rtas_work_area_raw_buf(work_area), bytes_written))
216 ret = -EFAULT;
217 else
218 ret = bytes_written;
219
220 out:
221 rtas_work_area_free(work_area);
222 return ret;
223 }
224
225 /*
226 * papr_hvpipe_handle_write - Issue send HVPIPE RTAS and return
227 * the size (payload + HVPIPE_HDR_LEN) for RTAS success.
228 * Otherwise returns the status of RTAS to the user space
229 */
papr_hvpipe_handle_write(struct file * file,const char __user * buf,size_t size,loff_t * off)230 static ssize_t papr_hvpipe_handle_write(struct file *file,
231 const char __user *buf, size_t size, loff_t *off)
232 {
233 struct hvpipe_source_info *src_info = file->private_data;
234 struct rtas_work_area *work_area, *work_buf;
235 unsigned long ret, len;
236 __be64 *area_be;
237
238 /*
239 * Return -ENXIO during migration
240 */
241 if (!hvpipe_feature)
242 return -ENXIO;
243
244 if (!src_info)
245 return -EIO;
246
247 /*
248 * Send HVPIPE RTAS is used to send payload to the specific
249 * source with the input parameters source ID and the payload
250 * as buffer list. Each entry in the buffer list contains
251 * address/length pair of the buffer.
252 *
253 * The buffer list format is as follows:
254 *
255 * Header (length of address/length pairs and the header length)
256 * Address of 4K buffer 1
257 * Length of 4K buffer 1 used
258 * ...
259 * Address of 4K buffer n
260 * Length of 4K buffer n used
261 *
262 * See PAPR 7.3.32.2 ibm,send-hvpipe-msg
263 *
264 * Even though can support max 1MB payload, the hypervisor
265 * supports only 4048 bytes payload at present and also
266 * just one address/length entry.
267 *
268 * writev() interface can be added in future when the
269 * hypervisor supports multiple buffer list entries.
270 */
271 /* HVPIPE_MAX_WRITE_BUFFER_SIZE = 4048 bytes */
272 if ((size > (HVPIPE_HDR_LEN + HVPIPE_MAX_WRITE_BUFFER_SIZE)) ||
273 (size <= HVPIPE_HDR_LEN))
274 return -EINVAL;
275
276 /*
277 * The length of (address + length) pair + the length of header
278 */
279 len = (2 * sizeof(u64)) + sizeof(u64);
280 size -= HVPIPE_HDR_LEN;
281 buf += HVPIPE_HDR_LEN;
282 mutex_lock(&rtas_ibm_send_hvpipe_msg_lock);
283 work_area = rtas_work_area_alloc(SZ_4K);
284 if (!work_area) {
285 ret = -ENOMEM;
286 goto out;
287 }
288 area_be = (__be64 *)rtas_work_area_raw_buf(work_area);
289 /* header */
290 area_be[0] = cpu_to_be64(len);
291
292 work_buf = rtas_work_area_alloc(SZ_4K);
293 if (!work_buf) {
294 ret = -ENOMEM;
295 goto out_work;
296 }
297 /* First buffer address */
298 area_be[1] = cpu_to_be64(rtas_work_area_phys(work_buf));
299 /* First buffer address length */
300 area_be[2] = cpu_to_be64(size);
301
302 if (!copy_from_user(rtas_work_area_raw_buf(work_buf), buf, size)) {
303 ret = rtas_ibm_send_hvpipe_msg(work_area, src_info->srcID);
304 if (!ret)
305 ret = size + HVPIPE_HDR_LEN;
306 } else
307 ret = -EPERM;
308
309 rtas_work_area_free(work_buf);
310 out_work:
311 rtas_work_area_free(work_area);
312 out:
313 mutex_unlock(&rtas_ibm_send_hvpipe_msg_lock);
314 return ret;
315 }
316
317 /*
318 * papr_hvpipe_handle_read - If the payload for the specific
319 * source is pending in the hypervisor, issue recv HVPIPE RTAS
320 * and return the payload to the user space.
321 *
322 * When the payload is available for the partition, the
323 * hypervisor notifies HVPIPE event with the source ID
324 * and the event handler wakeup FD(s) that are waiting.
325 */
papr_hvpipe_handle_read(struct file * file,char __user * buf,size_t size,loff_t * off)326 static ssize_t papr_hvpipe_handle_read(struct file *file,
327 char __user *buf, size_t size, loff_t *off)
328 {
329
330 struct hvpipe_source_info *src_info = file->private_data;
331 struct papr_hvpipe_hdr hdr = {};
332 ssize_t ret = 0;
333
334 /*
335 * Return -ENXIO during migration
336 */
337 if (!hvpipe_feature)
338 return -ENXIO;
339
340 if (!src_info)
341 return -EIO;
342
343 /*
344 * Max payload is 4048 (HVPIPE_MAX_WRITE_BUFFER_SIZE)
345 */
346 if ((size > (HVPIPE_HDR_LEN + HVPIPE_MAX_WRITE_BUFFER_SIZE)) ||
347 (size < HVPIPE_HDR_LEN))
348 return -EINVAL;
349
350 /*
351 * Payload is not available to receive or source pipe
352 * is not closed.
353 */
354 if (!src_info->hvpipe_status)
355 return 0;
356
357 hdr.version = 0;
358 hdr.flags = 0;
359
360 /*
361 * In case if the hvpipe has payload and also the
362 * hypervisor closed the pipe to the source, retrieve
363 * the payload and return to the user space first and
364 * then notify the userspace about the hvpipe close in
365 * next read().
366 */
367 if (src_info->hvpipe_status & HVPIPE_MSG_AVAILABLE)
368 hdr.flags = HVPIPE_MSG_AVAILABLE;
369 else if (src_info->hvpipe_status & HVPIPE_LOST_CONNECTION)
370 hdr.flags = HVPIPE_LOST_CONNECTION;
371 else
372 /*
373 * Should not be here without one of the above
374 * flags set
375 */
376 return -EIO;
377
378 ret = copy_to_user(buf, &hdr, HVPIPE_HDR_LEN);
379 if (ret)
380 return -EFAULT;
381
382 /*
383 * Message event has payload, so get the payload with
384 * recv HVPIPE RTAS.
385 */
386 if (hdr.flags & HVPIPE_MSG_AVAILABLE) {
387 ret = hvpipe_rtas_recv_msg(buf + HVPIPE_HDR_LEN,
388 size - HVPIPE_HDR_LEN);
389 /*
390 * Always clear MSG_AVAILABLE once the RTAS call has drained
391 * the message, regardless of whether copy_to_user succeeded.
392 */
393 if (ret >= 0 || ret == -EFAULT)
394 src_info->hvpipe_status &= ~HVPIPE_MSG_AVAILABLE;
395 } else if (hdr.flags & HVPIPE_LOST_CONNECTION) {
396 /*
397 * Hypervisor is closing the pipe for the specific
398 * source. So notify user space.
399 */
400 src_info->hvpipe_status &= ~HVPIPE_LOST_CONNECTION;
401 }
402
403 if (ret >= 0)
404 ret += HVPIPE_HDR_LEN;
405
406 return ret;
407 }
408
409 /*
410 * The user space waits for the payload to receive.
411 * The hypervisor sends HVPIPE event message to the partition
412 * when the payload is available. The event handler wakeup FD
413 * depends on the source ID in the message event.
414 */
papr_hvpipe_handle_poll(struct file * filp,struct poll_table_struct * wait)415 static __poll_t papr_hvpipe_handle_poll(struct file *filp,
416 struct poll_table_struct *wait)
417 {
418 struct hvpipe_source_info *src_info = filp->private_data;
419
420 /*
421 * HVPIPE is disabled during SUSPEND and enabled after migration.
422 * So return POLLRDHUP during migration
423 */
424 if (!hvpipe_feature)
425 return POLLRDHUP;
426
427 if (!src_info)
428 return POLLNVAL;
429
430 /*
431 * If hvpipe already has pending payload, return so that
432 * the user space can issue read().
433 */
434 if (src_info->hvpipe_status)
435 return POLLIN | POLLRDNORM;
436
437 /*
438 * Wait for the message event
439 * hvpipe_event_interrupt() wakes up this wait_queue
440 */
441 poll_wait(filp, &src_info->recv_wqh, wait);
442 if (src_info->hvpipe_status)
443 return POLLIN | POLLRDNORM;
444
445 return 0;
446 }
447
papr_hvpipe_handle_release(struct inode * inode,struct file * file)448 static int papr_hvpipe_handle_release(struct inode *inode,
449 struct file *file)
450 {
451 struct hvpipe_source_info *src_info;
452 unsigned long flags;
453
454 /*
455 * Hold the lock, remove source from src_list, reset the
456 * hvpipe status and release the lock to prevent any race
457 * with message event IRQ.
458 */
459 spin_lock_irqsave(&hvpipe_src_list_lock, flags);
460 src_info = file->private_data;
461 list_del(&src_info->list);
462 file->private_data = NULL;
463 spin_unlock_irqrestore(&hvpipe_src_list_lock, flags);
464 /*
465 * If the pipe for this specific source has any pending
466 * payload, issue recv HVPIPE RTAS so that pipe will not
467 * be blocked.
468 */
469 if (src_info->hvpipe_status & HVPIPE_MSG_AVAILABLE) {
470 src_info->hvpipe_status = 0;
471 hvpipe_rtas_recv_msg(NULL, 0);
472 }
473
474 kfree(src_info);
475 return 0;
476 }
477
478 static const struct file_operations papr_hvpipe_handle_ops = {
479 .read = papr_hvpipe_handle_read,
480 .write = papr_hvpipe_handle_write,
481 .release = papr_hvpipe_handle_release,
482 .poll = papr_hvpipe_handle_poll,
483 };
484
papr_hvpipe_dev_create_handle(u32 srcID)485 static int papr_hvpipe_dev_create_handle(u32 srcID)
486 {
487 struct hvpipe_source_info *src_info;
488 int fd;
489 unsigned long flags;
490
491 src_info = kzalloc_obj(*src_info, GFP_KERNEL_ACCOUNT);
492 if (!src_info)
493 return -ENOMEM;
494
495 src_info->srcID = srcID;
496 init_waitqueue_head(&src_info->recv_wqh);
497
498 /*
499 * Do not allow more than one process communicates with
500 * each source.
501 */
502 spin_lock_irqsave(&hvpipe_src_list_lock, flags);
503 if (hvpipe_find_source(srcID)) {
504 spin_unlock_irqrestore(&hvpipe_src_list_lock, flags);
505 pr_err("pid(%s:%d) could not get the source(%d)\n",
506 current->comm, task_pid_nr(current), srcID);
507 kfree(src_info);
508 return -EALREADY;
509 }
510 list_add(&src_info->list, &hvpipe_src_list);
511 spin_unlock_irqrestore(&hvpipe_src_list_lock, flags);
512
513 fd = FD_ADD(O_RDONLY | O_CLOEXEC,
514 anon_inode_getfile("[papr-hvpipe]", &papr_hvpipe_handle_ops,
515 (void *)src_info, O_RDWR));
516 if (fd < 0) {
517 spin_lock_irqsave(&hvpipe_src_list_lock, flags);
518 list_del(&src_info->list);
519 spin_unlock_irqrestore(&hvpipe_src_list_lock, flags);
520 /*
521 * if we fail to add FD, that means no userspace program is
522 * polling. In that case if there is a msg pending because the
523 * interrupt was fired after the src_info was added to the
524 * global list, then let's consume it here, to unblock the
525 * hvpipe
526 */
527 if (src_info->hvpipe_status & HVPIPE_MSG_AVAILABLE)
528 hvpipe_rtas_recv_msg(NULL, 0);
529 kfree(src_info);
530 return fd;
531 }
532
533 return fd;
534 }
535
536 /*
537 * Top-level ioctl handler for /dev/papr_hvpipe
538 *
539 * Use separate FD for each source (exa :HMC). So ioctl is called
540 * with source ID which returns FD.
541 */
papr_hvpipe_dev_ioctl(struct file * filp,unsigned int ioctl,unsigned long arg)542 static long papr_hvpipe_dev_ioctl(struct file *filp, unsigned int ioctl,
543 unsigned long arg)
544 {
545 u32 __user *argp = (void __user *)arg;
546 u32 srcID;
547 long ret;
548
549 /*
550 * Return -ENXIO during migration
551 */
552 if (!hvpipe_feature)
553 return -ENXIO;
554
555 if (get_user(srcID, argp))
556 return -EFAULT;
557
558 /*
559 * Support only HMC source right now
560 */
561 if (!(srcID & HVPIPE_HMC_ID_MASK))
562 return -EINVAL;
563
564 switch (ioctl) {
565 case PAPR_HVPIPE_IOC_CREATE_HANDLE:
566 ret = papr_hvpipe_dev_create_handle(srcID);
567 break;
568 default:
569 ret = -ENOIOCTLCMD;
570 break;
571 }
572
573 return ret;
574 }
575
576 /*
577 * papr_hvpipe_work_fn - called to issue recv HVPIPE RTAS for
578 * sources that are not monitored by user space so that pipe
579 * will not be blocked.
580 */
papr_hvpipe_work_fn(struct work_struct * work)581 static void papr_hvpipe_work_fn(struct work_struct *work)
582 {
583 hvpipe_rtas_recv_msg(NULL, 0);
584 }
585
586 /*
587 * HVPIPE event message IRQ handler.
588 * The hypervisor sends event IRQ if the partition has payload
589 * and generates another event only after payload is read with
590 * recv HVPIPE RTAS.
591 */
hvpipe_event_interrupt(int irq,void * dev_id)592 static irqreturn_t hvpipe_event_interrupt(int irq, void *dev_id)
593 {
594 struct hvpipe_event_buf *hvpipe_event;
595 struct pseries_errorlog *pseries_log;
596 struct hvpipe_source_info *src_info;
597 struct rtas_error_log *elog;
598 int rc;
599
600 rc = rtas_call(hvpipe_check_exception_token, 6, 1, NULL,
601 RTAS_VECTOR_EXTERNAL_INTERRUPT, virq_to_hw(irq),
602 RTAS_HVPIPE_MSG_EVENTS, 1, __pa(&hvpipe_ras_buf),
603 rtas_get_error_log_max());
604
605 if (rc != 0) {
606 pr_err_ratelimited("unexpected hvpipe-event-notification failed %d\n", rc);
607 return IRQ_HANDLED;
608 }
609
610 elog = (struct rtas_error_log *)hvpipe_ras_buf;
611 if (unlikely(rtas_error_type(elog) != RTAS_TYPE_HVPIPE)) {
612 pr_warn_ratelimited("Unexpected event type %d\n",
613 rtas_error_type(elog));
614 return IRQ_HANDLED;
615 }
616
617 pseries_log = get_pseries_errorlog(elog,
618 PSERIES_ELOG_SECT_ID_HVPIPE_EVENT);
619 hvpipe_event = (struct hvpipe_event_buf *)pseries_log->data;
620
621 /*
622 * The hypervisor notifies partition when the payload is
623 * available to read with recv HVPIPE RTAS and it will not
624 * notify another event for any source until the previous
625 * payload is read. Means the pipe is blocked in the
626 * hypervisor until the payload is read.
627 *
628 * If the source is ready to accept payload and wakeup the
629 * corresponding FD. Hold lock and update hvpipe_status
630 * and this lock is needed in case the user space process
631 * is in release FD instead of poll() so that release()
632 * reads the payload to unblock pipe before closing FD.
633 *
634 * otherwise (means no other user process waiting for the
635 * payload, issue recv HVPIPE RTAS (papr_hvpipe_work_fn())
636 * to unblock pipe.
637 */
638 spin_lock(&hvpipe_src_list_lock);
639 src_info = hvpipe_find_source(be32_to_cpu(hvpipe_event->srcID));
640 if (src_info) {
641 u32 flags = 0;
642
643 if (hvpipe_event->event_type & HVPIPE_LOST_CONNECTION)
644 flags = HVPIPE_LOST_CONNECTION;
645 else if (hvpipe_event->event_type & HVPIPE_MSG_AVAILABLE)
646 flags = HVPIPE_MSG_AVAILABLE;
647
648 src_info->hvpipe_status |= flags;
649 wake_up(&src_info->recv_wqh);
650 spin_unlock(&hvpipe_src_list_lock);
651 } else {
652 spin_unlock(&hvpipe_src_list_lock);
653 /*
654 * user space is not waiting on this source. So
655 * execute receive pipe RTAS so that pipe will not
656 * be blocked.
657 */
658 if (hvpipe_event->event_type & HVPIPE_MSG_AVAILABLE)
659 queue_work(papr_hvpipe_wq, papr_hvpipe_work);
660 }
661
662 return IRQ_HANDLED;
663 }
664
665 /*
666 * Enable hvpipe by system parameter set with parameter
667 * token = 64 and with 1 byte buffer data:
668 * 0 = hvpipe not in use/disable
669 * 1 = hvpipe in use/enable
670 */
set_hvpipe_sys_param(u8 val)671 static int set_hvpipe_sys_param(u8 val)
672 {
673 struct papr_sysparm_buf *buf;
674 int ret;
675
676 buf = papr_sysparm_buf_alloc();
677 if (!buf)
678 return -ENOMEM;
679
680 buf->len = cpu_to_be16(1);
681 buf->val[0] = val;
682 ret = papr_sysparm_set(PAPR_SYSPARM_HVPIPE_ENABLE, buf);
683 if (ret)
684 pr_err("Can not enable hvpipe %d\n", ret);
685
686 papr_sysparm_buf_free(buf);
687
688 return ret;
689 }
690
enable_hvpipe_IRQ(void)691 static int __init enable_hvpipe_IRQ(void)
692 {
693 struct device_node *np;
694
695 hvpipe_check_exception_token = rtas_function_token(RTAS_FN_CHECK_EXCEPTION);
696 if (hvpipe_check_exception_token == RTAS_UNKNOWN_SERVICE)
697 return -ENODEV;
698
699 /* hvpipe events */
700 np = of_find_node_by_path("/event-sources/ibm,hvpipe-msg-events");
701 if (!np) {
702 pr_err("No device node found, could not enable hvpipe event IRQ\n");
703 return -ENODEV;
704 }
705
706 request_event_sources_irqs(np, hvpipe_event_interrupt, "HPIPE_EVENT");
707 of_node_put(np);
708
709 return 0;
710 }
711
hvpipe_migration_handler(int action)712 void hvpipe_migration_handler(int action)
713 {
714 pr_info("hvpipe migration event %d\n", action);
715
716 /*
717 * HVPIPE is not used (Failed to create /dev/papr-hvpipe).
718 * So nothing to do for migration.
719 */
720 if (!papr_hvpipe_work)
721 return;
722
723 switch (action) {
724 case HVPIPE_SUSPEND:
725 if (hvpipe_feature) {
726 /*
727 * Disable hvpipe_feature to the user space.
728 * It will be enabled with RESUME event.
729 */
730 hvpipe_feature = false;
731 /*
732 * set system parameter hvpipe 'disable'
733 */
734 set_hvpipe_sys_param(0);
735 }
736 break;
737 case HVPIPE_RESUME:
738 /*
739 * set system parameter hvpipe 'enable'
740 */
741 if (!set_hvpipe_sys_param(1))
742 hvpipe_feature = true;
743 else
744 pr_err("hvpipe is not enabled after migration\n");
745
746 break;
747 }
748 }
749
750 static const struct file_operations papr_hvpipe_ops = {
751 .unlocked_ioctl = papr_hvpipe_dev_ioctl,
752 };
753
754 static struct miscdevice papr_hvpipe_dev = {
755 .minor = MISC_DYNAMIC_MINOR,
756 .name = "papr-hvpipe",
757 .fops = &papr_hvpipe_ops,
758 };
759
papr_hvpipe_init(void)760 static int __init papr_hvpipe_init(void)
761 {
762 int ret;
763
764 if (!of_find_property(rtas.dev, "ibm,hypervisor-pipe-capable",
765 NULL))
766 return -ENODEV;
767
768 if (!rtas_function_implemented(RTAS_FN_IBM_SEND_HVPIPE_MSG) ||
769 !rtas_function_implemented(RTAS_FN_IBM_RECEIVE_HVPIPE_MSG))
770 return -ENODEV;
771
772 papr_hvpipe_work = kzalloc_obj(struct work_struct, GFP_ATOMIC);
773 if (!papr_hvpipe_work)
774 return -ENOMEM;
775
776 INIT_WORK(papr_hvpipe_work, papr_hvpipe_work_fn);
777
778 papr_hvpipe_wq = alloc_ordered_workqueue("papr hvpipe workqueue", 0);
779 if (!papr_hvpipe_wq) {
780 ret = -ENOMEM;
781 goto out;
782 }
783
784 ret = enable_hvpipe_IRQ();
785 if (ret)
786 goto out_wq;
787
788 ret = misc_register(&papr_hvpipe_dev);
789 if (ret)
790 goto out_wq;
791
792 ret = set_hvpipe_sys_param(1);
793 if (ret)
794 goto out_misc;
795
796 pr_info("hvpipe feature is enabled\n");
797 hvpipe_feature = true;
798 return 0;
799
800 out_misc:
801 misc_deregister(&papr_hvpipe_dev);
802 out_wq:
803 destroy_workqueue(papr_hvpipe_wq);
804 out:
805 kfree(papr_hvpipe_work);
806 papr_hvpipe_work = NULL;
807 pr_err("hvpipe feature is not enabled %d\n", ret);
808 return ret;
809 }
810 machine_device_initcall(pseries, papr_hvpipe_init);
811