18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2016 Anton Ivanov (aivanov@brocade.com) 48c2ecf20Sopenharmony_ci * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com) 58c2ecf20Sopenharmony_ci * Copyright (C) 2001 Ridgerun,Inc (glonnon@ridgerun.com) 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <stddef.h> 98c2ecf20Sopenharmony_ci#include <unistd.h> 108c2ecf20Sopenharmony_ci#include <errno.h> 118c2ecf20Sopenharmony_ci#include <sched.h> 128c2ecf20Sopenharmony_ci#include <signal.h> 138c2ecf20Sopenharmony_ci#include <string.h> 148c2ecf20Sopenharmony_ci#include <netinet/in.h> 158c2ecf20Sopenharmony_ci#include <sys/time.h> 168c2ecf20Sopenharmony_ci#include <sys/socket.h> 178c2ecf20Sopenharmony_ci#include <sys/mman.h> 188c2ecf20Sopenharmony_ci#include <sys/param.h> 198c2ecf20Sopenharmony_ci#include <endian.h> 208c2ecf20Sopenharmony_ci#include <byteswap.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#include "ubd.h" 238c2ecf20Sopenharmony_ci#include <os.h> 248c2ecf20Sopenharmony_ci#include <poll.h> 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_cistruct pollfd kernel_pollfd; 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ciint start_io_thread(unsigned long sp, int *fd_out) 298c2ecf20Sopenharmony_ci{ 308c2ecf20Sopenharmony_ci int pid, fds[2], err; 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci err = os_pipe(fds, 1, 1); 338c2ecf20Sopenharmony_ci if(err < 0){ 348c2ecf20Sopenharmony_ci printk("start_io_thread - os_pipe failed, err = %d\n", -err); 358c2ecf20Sopenharmony_ci goto out; 368c2ecf20Sopenharmony_ci } 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci kernel_fd = fds[0]; 398c2ecf20Sopenharmony_ci kernel_pollfd.fd = kernel_fd; 408c2ecf20Sopenharmony_ci kernel_pollfd.events = POLLIN; 418c2ecf20Sopenharmony_ci *fd_out = fds[1]; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci err = os_set_fd_block(*fd_out, 0); 448c2ecf20Sopenharmony_ci err = os_set_fd_block(kernel_fd, 0); 458c2ecf20Sopenharmony_ci if (err) { 468c2ecf20Sopenharmony_ci printk("start_io_thread - failed to set nonblocking I/O.\n"); 478c2ecf20Sopenharmony_ci goto out_close; 488c2ecf20Sopenharmony_ci } 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci pid = clone(io_thread, (void *) sp, CLONE_FILES | CLONE_VM, NULL); 518c2ecf20Sopenharmony_ci if(pid < 0){ 528c2ecf20Sopenharmony_ci err = -errno; 538c2ecf20Sopenharmony_ci printk("start_io_thread - clone failed : errno = %d\n", errno); 548c2ecf20Sopenharmony_ci goto out_close; 558c2ecf20Sopenharmony_ci } 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci return(pid); 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci out_close: 608c2ecf20Sopenharmony_ci os_close_file(fds[0]); 618c2ecf20Sopenharmony_ci os_close_file(fds[1]); 628c2ecf20Sopenharmony_ci kernel_fd = -1; 638c2ecf20Sopenharmony_ci *fd_out = -1; 648c2ecf20Sopenharmony_ci out: 658c2ecf20Sopenharmony_ci return err; 668c2ecf20Sopenharmony_ci} 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ciint ubd_read_poll(int timeout) 698c2ecf20Sopenharmony_ci{ 708c2ecf20Sopenharmony_ci kernel_pollfd.events = POLLIN; 718c2ecf20Sopenharmony_ci return poll(&kernel_pollfd, 1, timeout); 728c2ecf20Sopenharmony_ci} 738c2ecf20Sopenharmony_ciint ubd_write_poll(int timeout) 748c2ecf20Sopenharmony_ci{ 758c2ecf20Sopenharmony_ci kernel_pollfd.events = POLLOUT; 768c2ecf20Sopenharmony_ci return poll(&kernel_pollfd, 1, timeout); 778c2ecf20Sopenharmony_ci} 788c2ecf20Sopenharmony_ci 79