17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51ae08745Sheppo * Common Development and Distribution License (the "License").
61ae08745Sheppo * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
2220ae46ebSha137994 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate * Platform Channel Protocol Library functions on Nigara platforms
287c478bd9Sstevel@tonic-gate * (Ontario, Erie, etc..) Solaris applications use these interfaces
297c478bd9Sstevel@tonic-gate * to communicate with entities that reside on service processor.
307c478bd9Sstevel@tonic-gate */
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <assert.h>
377c478bd9Sstevel@tonic-gate #include <fcntl.h>
387c478bd9Sstevel@tonic-gate #include <errno.h>
397c478bd9Sstevel@tonic-gate #include <signal.h>
407c478bd9Sstevel@tonic-gate #include <setjmp.h>
417c478bd9Sstevel@tonic-gate #include <inttypes.h>
427c478bd9Sstevel@tonic-gate #include <umem.h>
437c478bd9Sstevel@tonic-gate #include <strings.h>
4456b04ea6Smvgr #include <time.h>
457c478bd9Sstevel@tonic-gate #include <sys/types.h>
467c478bd9Sstevel@tonic-gate #include <sys/stat.h>
477c478bd9Sstevel@tonic-gate #include <sys/glvc.h>
481ae08745Sheppo #include <sys/vldc.h>
491ae08745Sheppo #include <sys/ldc.h>
507c478bd9Sstevel@tonic-gate #include <netinet/in.h>
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate #include "libpcp.h"
537c478bd9Sstevel@tonic-gate #include "pcp_common.h"
544bac2208Snarayan #include "pcp_utils.h"
554bac2208Snarayan
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate * Following libpcp interfaces are exposed to user applications.
597c478bd9Sstevel@tonic-gate *
605db09ef5Smvgr * int pcp_init(char *channel_name);
615db09ef5Smvgr * int pcp_send_recv(int channel_fd, pcp_msg_t *req_msg, pcp_msg_t *resp_msg,
625db09ef5Smvgr * uint32_t timeout);
635db09ef5Smvgr * int pcp_close(int channel_fd);
647c478bd9Sstevel@tonic-gate *
657c478bd9Sstevel@tonic-gate */
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate * Forward declarations.
697c478bd9Sstevel@tonic-gate */
707c478bd9Sstevel@tonic-gate static int pcp_send_req_msg_hdr(pcp_req_msg_hdr_t *req_hdr);
717c478bd9Sstevel@tonic-gate static int pcp_recv_resp_msg_hdr(pcp_resp_msg_hdr_t *resp_hdr);
727c478bd9Sstevel@tonic-gate static int pcp_io_op(void *buf, int byte_cnt, int io_op);
7356b04ea6Smvgr static uint32_t pcp_get_xid(void);
745db09ef5Smvgr static int pcp_get_prop(int channel_fd, int prop, unsigned int *val);
757c478bd9Sstevel@tonic-gate static int pcp_read(uint8_t *buf, int buf_len);
767c478bd9Sstevel@tonic-gate static int pcp_write(uint8_t *buf, int buf_len);
777c478bd9Sstevel@tonic-gate static int pcp_peek(uint8_t *buf, int buf_len);
787c478bd9Sstevel@tonic-gate static int pcp_peek_read(uint8_t *buf, int buf_len);
797c478bd9Sstevel@tonic-gate static int pcp_frame_error_handle(void);
807c478bd9Sstevel@tonic-gate static int check_magic_byte_presence(int byte_cnt, uint8_t *byte_val,
817c478bd9Sstevel@tonic-gate int *ispresent);
827c478bd9Sstevel@tonic-gate static uint16_t checksum(uint16_t *addr, int32_t count);
835db09ef5Smvgr static int pcp_cleanup(int channel_fd);
847c478bd9Sstevel@tonic-gate
851ae08745Sheppo static int vldc_read(int fd, uint8_t *bufp, int size);
861ae08745Sheppo static int vldc_write(int fd, uint8_t *bufp, int size);
871ae08745Sheppo static int pcp_update_read_area(int byte_cnt);
881ae08745Sheppo static int pcp_vldc_frame_error_handle(void);
891ae08745Sheppo
907c478bd9Sstevel@tonic-gate /*
915db09ef5Smvgr * local channel (glvc) file descriptor set by pcp_send_recv()
927c478bd9Sstevel@tonic-gate */
937c478bd9Sstevel@tonic-gate static int chnl_fd = -1;
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate * Message Transaction ID
977c478bd9Sstevel@tonic-gate */
987c478bd9Sstevel@tonic-gate static uint32_t msg_xid = 0;
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate * Channel MTU size.
1027c478bd9Sstevel@tonic-gate */
1037c478bd9Sstevel@tonic-gate static unsigned int mtu_size = PCPL_DEF_MTU_SZ;
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate * timeout field is supplied by user. timeout field is used to decide
1077c478bd9Sstevel@tonic-gate * how long to block on glvc driver calls before we return timeout error
1087c478bd9Sstevel@tonic-gate * to user applications.
1097c478bd9Sstevel@tonic-gate *
1107c478bd9Sstevel@tonic-gate * Note: In the current implementation of glvc driver, all glvc calls are
1117c478bd9Sstevel@tonic-gate * blocking.
1127c478bd9Sstevel@tonic-gate */
1137c478bd9Sstevel@tonic-gate static uint32_t glvc_timeout = 0;
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate /*
1167c478bd9Sstevel@tonic-gate * variables used by setsetjmp/siglongjmp.
1177c478bd9Sstevel@tonic-gate */
1187c478bd9Sstevel@tonic-gate static volatile sig_atomic_t jumpok = 0;
1197c478bd9Sstevel@tonic-gate static sigjmp_buf jmpbuf;
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate * To unblock SIGALRM signal incase if it's blocked in libpcp user apps.
1237c478bd9Sstevel@tonic-gate * Restore it to old state during pcp_close.
1247c478bd9Sstevel@tonic-gate */
1257c478bd9Sstevel@tonic-gate static sigset_t blkset;
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate * Buffers used for stream reading channel data. When data is read in
1297c478bd9Sstevel@tonic-gate * stream fashion, first data is copied from channel (glvc) buffers to
1307c478bd9Sstevel@tonic-gate * these local buffers from which the read requests are serviced.
1317c478bd9Sstevel@tonic-gate */
1327c478bd9Sstevel@tonic-gate #define READ_AREA_SIZE (2*mtu_size)
1337c478bd9Sstevel@tonic-gate static uint8_t *read_head = NULL;
1347c478bd9Sstevel@tonic-gate static uint8_t *read_tail = NULL;
1357c478bd9Sstevel@tonic-gate static uint8_t *read_area = NULL;
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate * Buffer used for peeking new data available in channel (glvc) buffers.
1397c478bd9Sstevel@tonic-gate */
1407c478bd9Sstevel@tonic-gate #define PEEK_AREA_SIZE (mtu_size)
1417c478bd9Sstevel@tonic-gate static uint8_t *peek_area = NULL;
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate /*
1447c478bd9Sstevel@tonic-gate * Buffers used for peeking data available either in local buffers or
1457c478bd9Sstevel@tonic-gate * new data available in channel (glvc) buffers.
1467c478bd9Sstevel@tonic-gate */
1477c478bd9Sstevel@tonic-gate #define PEEK_READ_AREA_SIZE (2*mtu_size)
1487c478bd9Sstevel@tonic-gate static uint8_t *peek_read_head = NULL;
1497c478bd9Sstevel@tonic-gate static uint8_t *peek_read_tail = NULL;
1507c478bd9Sstevel@tonic-gate static uint8_t *peek_read_area = NULL;
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate static pcp_req_msg_hdr_t *req_msg_hdr = NULL;
1537c478bd9Sstevel@tonic-gate static pcp_resp_msg_hdr_t *resp_msg_hdr = NULL;
1547c478bd9Sstevel@tonic-gate static int req_msg_hdr_sz = 0;
1557c478bd9Sstevel@tonic-gate static int resp_msg_hdr_sz = 0;
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate * signal handling variables to handle glvc blocking calls.
1597c478bd9Sstevel@tonic-gate */
1607c478bd9Sstevel@tonic-gate static struct sigaction glvc_act;
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate /* To restore old SIGALRM signal handler */
1637c478bd9Sstevel@tonic-gate static struct sigaction old_act;
1647c478bd9Sstevel@tonic-gate
1651ae08745Sheppo /*
1661ae08745Sheppo * Variables to support vldc based streaming transport
1671ae08745Sheppo */
1684bac2208Snarayan static pcp_xport_t xport_type = GLVC_NON_STREAM;
1691ae08745Sheppo #define VLDC_MTU_SIZE (2048)
1701ae08745Sheppo
1717c478bd9Sstevel@tonic-gate static void
glvc_timeout_handler(void)1727c478bd9Sstevel@tonic-gate glvc_timeout_handler(void)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate if (jumpok == 0)
1757c478bd9Sstevel@tonic-gate return;
1767c478bd9Sstevel@tonic-gate siglongjmp(jmpbuf, 1);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate /*
1807c478bd9Sstevel@tonic-gate * Initialize the virtual channel. It basically opens the virtual channel
1817c478bd9Sstevel@tonic-gate * provided by the host application.
1827c478bd9Sstevel@tonic-gate *
1837c478bd9Sstevel@tonic-gate */
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate int
pcp_init(char * channel_name)1865db09ef5Smvgr pcp_init(char *channel_name)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate sigset_t oldset;
1895db09ef5Smvgr int channel_fd;
1904bac2208Snarayan char *dev_path;
1914bac2208Snarayan vldc_opt_op_t op;
1927c478bd9Sstevel@tonic-gate
1935db09ef5Smvgr if (channel_name == NULL)
1947c478bd9Sstevel@tonic-gate return (PCPL_INVALID_ARGS);
1951ae08745Sheppo
1967c478bd9Sstevel@tonic-gate /*
1974bac2208Snarayan * Given the argument, try to locate a device in the device tree
1984bac2208Snarayan */
1994bac2208Snarayan dev_path = platsvc_name_to_path(channel_name, &xport_type);
2004bac2208Snarayan
2014bac2208Snarayan /*
2024bac2208Snarayan * Path exists ?
2034bac2208Snarayan */
2044bac2208Snarayan if (NULL == dev_path)
2054bac2208Snarayan return (PCPL_INVALID_ARGS);
2064bac2208Snarayan
2074bac2208Snarayan /*
2085db09ef5Smvgr * Open virtual channel name.
2097c478bd9Sstevel@tonic-gate */
2104bac2208Snarayan if ((channel_fd = open(dev_path, O_RDWR|O_EXCL)) < 0) {
2114bac2208Snarayan free(dev_path);
2127c478bd9Sstevel@tonic-gate return (PCPL_GLVC_ERROR);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate
2154bac2208Snarayan free(dev_path);
2161ae08745Sheppo
2174bac2208Snarayan /*
2184bac2208Snarayan * Handle transport-specific processing
2194bac2208Snarayan */
2204bac2208Snarayan switch (xport_type) {
2214bac2208Snarayan case VLDC_STREAMING:
2221ae08745Sheppo mtu_size = VLDC_MTU_SIZE;
2231ae08745Sheppo
2241ae08745Sheppo op.op_sel = VLDC_OP_SET;
2251ae08745Sheppo op.opt_sel = VLDC_OPT_MODE;
22620ae46ebSha137994 op.opt_val = LDC_MODE_RELIABLE;
2271ae08745Sheppo if (ioctl(channel_fd, VLDC_IOCTL_OPT_OP, &op) != 0) {
2281ae08745Sheppo (void) close(channel_fd);
2291ae08745Sheppo return (PCPL_GLVC_ERROR);
2301ae08745Sheppo }
2314bac2208Snarayan break;
2324bac2208Snarayan case GLVC_NON_STREAM:
2334bac2208Snarayan default:
2341ae08745Sheppo /*
2357c478bd9Sstevel@tonic-gate * Get the Channel MTU size
2367c478bd9Sstevel@tonic-gate */
2377c478bd9Sstevel@tonic-gate
2381ae08745Sheppo if (pcp_get_prop(channel_fd, GLVC_XPORT_OPT_MTU_SZ,
2391ae08745Sheppo &mtu_size) != 0) {
2405db09ef5Smvgr (void) close(channel_fd);
2417c478bd9Sstevel@tonic-gate return (PCPL_GLVC_ERROR);
2427c478bd9Sstevel@tonic-gate }
2434bac2208Snarayan break;
2441ae08745Sheppo }
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate /*
2477c478bd9Sstevel@tonic-gate * Get current signal mask. If SIGALRM is blocked
2487c478bd9Sstevel@tonic-gate * unblock it.
2497c478bd9Sstevel@tonic-gate */
2507c478bd9Sstevel@tonic-gate (void) sigprocmask(0, NULL, &oldset);
2517c478bd9Sstevel@tonic-gate
2527c478bd9Sstevel@tonic-gate (void) sigemptyset(&blkset);
2537c478bd9Sstevel@tonic-gate
2547c478bd9Sstevel@tonic-gate if (sigismember(&oldset, SIGALRM)) {
2557c478bd9Sstevel@tonic-gate (void) sigaddset(&blkset, SIGALRM);
2567c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &blkset, NULL);
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate * signal handler initialization to handle glvc call timeouts.
2607c478bd9Sstevel@tonic-gate */
2617c478bd9Sstevel@tonic-gate glvc_act.sa_handler = glvc_timeout_handler;
2627c478bd9Sstevel@tonic-gate (void) sigemptyset(&glvc_act.sa_mask);
2637c478bd9Sstevel@tonic-gate glvc_act.sa_flags = SA_NODEFER;
2647c478bd9Sstevel@tonic-gate
2657c478bd9Sstevel@tonic-gate if (sigaction(SIGALRM, &glvc_act, &old_act) < 0) {
2665db09ef5Smvgr (void) close(channel_fd);
2677c478bd9Sstevel@tonic-gate return (PCPL_ERROR);
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate
2705db09ef5Smvgr return (channel_fd);
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate
2737c478bd9Sstevel@tonic-gate /*
2747c478bd9Sstevel@tonic-gate * Function: Close platform channel.
2755db09ef5Smvgr * Arguments:
2765db09ef5Smvgr * int channel_fd - channel file descriptor.
2777c478bd9Sstevel@tonic-gate * Returns:
2787c478bd9Sstevel@tonic-gate * always returns PCPL_OK for now.
2797c478bd9Sstevel@tonic-gate */
2807c478bd9Sstevel@tonic-gate int
pcp_close(int channel_fd)2815db09ef5Smvgr pcp_close(int channel_fd)
2827c478bd9Sstevel@tonic-gate {
2837c478bd9Sstevel@tonic-gate
2845db09ef5Smvgr if (channel_fd >= 0) {
2851ae08745Sheppo if (xport_type == GLVC_NON_STREAM)
2865db09ef5Smvgr (void) pcp_cleanup(channel_fd);
2875db09ef5Smvgr (void) close(channel_fd);
2885db09ef5Smvgr } else {
2895db09ef5Smvgr return (-1);
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate
2927c478bd9Sstevel@tonic-gate /*
2937c478bd9Sstevel@tonic-gate * free global buffers
2947c478bd9Sstevel@tonic-gate */
2957c478bd9Sstevel@tonic-gate if (read_area != NULL) {
2967c478bd9Sstevel@tonic-gate umem_free(read_area, READ_AREA_SIZE);
2977c478bd9Sstevel@tonic-gate read_area = NULL;
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate if (peek_area != NULL) {
3007c478bd9Sstevel@tonic-gate umem_free(peek_area, PEEK_AREA_SIZE);
3017c478bd9Sstevel@tonic-gate peek_area = NULL;
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate if (peek_read_area != NULL) {
3047c478bd9Sstevel@tonic-gate umem_free(peek_read_area, PEEK_READ_AREA_SIZE);
3057c478bd9Sstevel@tonic-gate peek_read_area = NULL;
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate if (req_msg_hdr != NULL) {
3087c478bd9Sstevel@tonic-gate umem_free(req_msg_hdr, req_msg_hdr_sz);
3097c478bd9Sstevel@tonic-gate req_msg_hdr = NULL;
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate if (resp_msg_hdr != NULL) {
3127c478bd9Sstevel@tonic-gate umem_free(resp_msg_hdr, resp_msg_hdr_sz);
3137c478bd9Sstevel@tonic-gate resp_msg_hdr = NULL;
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate /*
3177c478bd9Sstevel@tonic-gate * Restore SIGALRM signal mask incase if we unblocked
3187c478bd9Sstevel@tonic-gate * it during pcp_init.
3197c478bd9Sstevel@tonic-gate */
3207c478bd9Sstevel@tonic-gate if (sigismember(&blkset, SIGALRM)) {
3217c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &blkset, NULL);
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate
3247c478bd9Sstevel@tonic-gate /* Restore SIGALRM signal handler */
3257c478bd9Sstevel@tonic-gate (void) sigaction(SIGALRM, &old_act, NULL);
3267c478bd9Sstevel@tonic-gate
3277c478bd9Sstevel@tonic-gate return (PCPL_OK);
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate
3307c478bd9Sstevel@tonic-gate /*
3317c478bd9Sstevel@tonic-gate * Function: Send and Receive messages on platform channel.
3327c478bd9Sstevel@tonic-gate * Arguments:
3335db09ef5Smvgr * int channel_fd - channel file descriptor.
3347c478bd9Sstevel@tonic-gate * pcp_msg_t *req_msg - Request Message to send to other end of channel.
3357c478bd9Sstevel@tonic-gate * pcp_msg_t *resp_msg - Response Message to be received.
3367c478bd9Sstevel@tonic-gate * uint32_t timeout - timeout field when waiting for data from channel.
3377c478bd9Sstevel@tonic-gate * Returns:
3387c478bd9Sstevel@tonic-gate * 0 - success (PCPL_OK).
3397c478bd9Sstevel@tonic-gate * (-ve) - failure:
3407c478bd9Sstevel@tonic-gate * PCPL_INVALID_ARGS - invalid args.
3417c478bd9Sstevel@tonic-gate * PCPL_GLVC_TIMEOUT - glvc call timeout.
3427c478bd9Sstevel@tonic-gate * PCPL_XPORT_ERROR - transport error in request message
3437c478bd9Sstevel@tonic-gate * noticed by receiver.
3447c478bd9Sstevel@tonic-gate * PCPL_MALLOC_FAIL - malloc failure.
3457c478bd9Sstevel@tonic-gate * PCPL_CKSUM_ERROR - checksum error.
3467c478bd9Sstevel@tonic-gate */
3477c478bd9Sstevel@tonic-gate int
pcp_send_recv(int channel_fd,pcp_msg_t * req_msg,pcp_msg_t * resp_msg,uint32_t timeout)3485db09ef5Smvgr pcp_send_recv(int channel_fd, pcp_msg_t *req_msg, pcp_msg_t *resp_msg,
3495db09ef5Smvgr uint32_t timeout)
3507c478bd9Sstevel@tonic-gate {
3517c478bd9Sstevel@tonic-gate void *datap;
3527c478bd9Sstevel@tonic-gate void *resp_msg_data = NULL;
3537c478bd9Sstevel@tonic-gate uint32_t status;
3547c478bd9Sstevel@tonic-gate uint16_t cksum = 0;
3557c478bd9Sstevel@tonic-gate int ret;
3567c478bd9Sstevel@tonic-gate int resp_hdr_ok;
3577c478bd9Sstevel@tonic-gate #ifdef PCP_CKSUM_ENABLE
3587c478bd9Sstevel@tonic-gate uint16_t bkup_resp_hdr_cksum;
3597c478bd9Sstevel@tonic-gate #endif
3605db09ef5Smvgr if (channel_fd < 0) {
3617c478bd9Sstevel@tonic-gate return (PCPL_ERROR);
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate
3645db09ef5Smvgr /* copy channel_fd to local fd (chnl_fd) for other functions use */
3655db09ef5Smvgr chnl_fd = channel_fd;
3665db09ef5Smvgr
3677c478bd9Sstevel@tonic-gate if (req_msg == NULL) {
3687c478bd9Sstevel@tonic-gate return (PCPL_INVALID_ARGS);
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate
3717c478bd9Sstevel@tonic-gate if (timeout > 0)
3727c478bd9Sstevel@tonic-gate glvc_timeout = timeout;
3737c478bd9Sstevel@tonic-gate else
3747c478bd9Sstevel@tonic-gate glvc_timeout = 0;
3757c478bd9Sstevel@tonic-gate
3761b255844Smvgr if ((req_msg->msg_len != 0) && ((datap = req_msg->msg_data) == NULL))
3777c478bd9Sstevel@tonic-gate return (PCPL_INVALID_ARGS);
3787c478bd9Sstevel@tonic-gate
3797c478bd9Sstevel@tonic-gate if (req_msg_hdr == NULL) {
3807c478bd9Sstevel@tonic-gate req_msg_hdr_sz = sizeof (pcp_req_msg_hdr_t);
3817c478bd9Sstevel@tonic-gate req_msg_hdr = (pcp_req_msg_hdr_t *)umem_zalloc(req_msg_hdr_sz,
3827c478bd9Sstevel@tonic-gate UMEM_DEFAULT);
3837c478bd9Sstevel@tonic-gate if (req_msg_hdr == NULL)
3847c478bd9Sstevel@tonic-gate return (PCPL_MALLOC_FAIL);
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate
3875db09ef5Smvgr if (req_msg->msg_len != 0) {
3887c478bd9Sstevel@tonic-gate /* calculate request msg_cksum */
3897c478bd9Sstevel@tonic-gate cksum = checksum((uint16_t *)datap, req_msg->msg_len);
3905db09ef5Smvgr }
3917c478bd9Sstevel@tonic-gate
3927c478bd9Sstevel@tonic-gate /*
3937c478bd9Sstevel@tonic-gate * Fill in the message header for the request packet
3947c478bd9Sstevel@tonic-gate */
3957c478bd9Sstevel@tonic-gate req_msg_hdr->magic_num = PCP_MAGIC_NUM;
3967c478bd9Sstevel@tonic-gate req_msg_hdr->proto_ver = PCP_PROT_VER_1;
3977c478bd9Sstevel@tonic-gate req_msg_hdr->msg_type = req_msg->msg_type;
3987c478bd9Sstevel@tonic-gate req_msg_hdr->sub_type = req_msg->sub_type;
3997c478bd9Sstevel@tonic-gate req_msg_hdr->rsvd_pad = 0;
4007c478bd9Sstevel@tonic-gate req_msg_hdr->xid = pcp_get_xid();
4017c478bd9Sstevel@tonic-gate req_msg_hdr->msg_len = req_msg->msg_len;
4027c478bd9Sstevel@tonic-gate req_msg_hdr->timeout = timeout;
4037c478bd9Sstevel@tonic-gate req_msg_hdr->msg_cksum = cksum;
4047c478bd9Sstevel@tonic-gate req_msg_hdr->hdr_cksum = 0;
4057c478bd9Sstevel@tonic-gate
4067c478bd9Sstevel@tonic-gate /* fill request header checksum */
4077c478bd9Sstevel@tonic-gate req_msg_hdr->hdr_cksum = checksum((uint16_t *)req_msg_hdr,
4087c478bd9Sstevel@tonic-gate req_msg_hdr_sz);
4097c478bd9Sstevel@tonic-gate /*
4107c478bd9Sstevel@tonic-gate * set sig jmp location
4117c478bd9Sstevel@tonic-gate */
4127c478bd9Sstevel@tonic-gate if (sigsetjmp(jmpbuf, 1)) {
4137c478bd9Sstevel@tonic-gate return (PCPL_GLVC_TIMEOUT);
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate jumpok = 1; /* monitor sigalrm from now on */
4167c478bd9Sstevel@tonic-gate
4177c478bd9Sstevel@tonic-gate /*
4187c478bd9Sstevel@tonic-gate * send request message header
4197c478bd9Sstevel@tonic-gate */
4207c478bd9Sstevel@tonic-gate if ((ret = pcp_send_req_msg_hdr(req_msg_hdr))) {
4217c478bd9Sstevel@tonic-gate
4227c478bd9Sstevel@tonic-gate return (ret);
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate
4257c478bd9Sstevel@tonic-gate /*
4267c478bd9Sstevel@tonic-gate * send request message
4277c478bd9Sstevel@tonic-gate */
4285db09ef5Smvgr if (req_msg->msg_len != 0) {
4297c478bd9Sstevel@tonic-gate if ((ret = pcp_io_op(datap, req_msg->msg_len,
4307c478bd9Sstevel@tonic-gate PCPL_IO_OP_WRITE))) {
4317c478bd9Sstevel@tonic-gate return (ret);
4327c478bd9Sstevel@tonic-gate }
4335db09ef5Smvgr }
4345db09ef5Smvgr
4357c478bd9Sstevel@tonic-gate if (timeout == (uint32_t)PCP_TO_NO_RESPONSE)
4367c478bd9Sstevel@tonic-gate return (PCPL_OK);
4377c478bd9Sstevel@tonic-gate
4387c478bd9Sstevel@tonic-gate if (resp_msg_hdr == NULL) {
4397c478bd9Sstevel@tonic-gate resp_msg_hdr_sz = sizeof (pcp_resp_msg_hdr_t);
4407c478bd9Sstevel@tonic-gate resp_msg_hdr = (pcp_resp_msg_hdr_t *)umem_alloc(resp_msg_hdr_sz,
4417c478bd9Sstevel@tonic-gate UMEM_DEFAULT);
4427c478bd9Sstevel@tonic-gate if (resp_msg_hdr == NULL)
4437c478bd9Sstevel@tonic-gate return (PCPL_MALLOC_FAIL);
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate
4467c478bd9Sstevel@tonic-gate resp_hdr_ok = 0;
4477c478bd9Sstevel@tonic-gate while (!resp_hdr_ok) {
4487c478bd9Sstevel@tonic-gate
4497c478bd9Sstevel@tonic-gate /*
4507c478bd9Sstevel@tonic-gate * Receive response message header
4517c478bd9Sstevel@tonic-gate * Note: frame error handling is done in
4527c478bd9Sstevel@tonic-gate * 'pcp_recv_resp_msg_hdr()'.
4537c478bd9Sstevel@tonic-gate */
4547c478bd9Sstevel@tonic-gate if ((ret = pcp_recv_resp_msg_hdr(resp_msg_hdr))) {
4557c478bd9Sstevel@tonic-gate return (ret);
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate
4587c478bd9Sstevel@tonic-gate /*
4597c478bd9Sstevel@tonic-gate * Check header checksum if it matches with the received hdr
4607c478bd9Sstevel@tonic-gate * checksum.
4617c478bd9Sstevel@tonic-gate */
4627c478bd9Sstevel@tonic-gate #ifdef PCP_CKSUM_ENABLE
4637c478bd9Sstevel@tonic-gate bkup_resp_hdr_cksum = resp_msg_hdr->hdr_cksum;
4647c478bd9Sstevel@tonic-gate resp_msg_hdr->hdr_cksum = 0;
4657c478bd9Sstevel@tonic-gate cksum = checksum((uint16_t *)resp_msg_hdr, resp_msg_hdr_sz);
4667c478bd9Sstevel@tonic-gate
4677c478bd9Sstevel@tonic-gate if (cksum != bkup_resp_hdr_cksum) {
4687c478bd9Sstevel@tonic-gate return (PCPL_CKSUM_ERROR);
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate #endif
4717c478bd9Sstevel@tonic-gate /*
4727c478bd9Sstevel@tonic-gate * Check for matching request and response messages
4737c478bd9Sstevel@tonic-gate */
4747c478bd9Sstevel@tonic-gate if (resp_msg_hdr->xid != req_msg_hdr->xid) {
4757c478bd9Sstevel@tonic-gate
4767c478bd9Sstevel@tonic-gate continue; /* continue reading response header */
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate resp_hdr_ok = 1;
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate
4817c478bd9Sstevel@tonic-gate /*
482*4cad604cSMarcel Telka * check status field for any channel protocol errors
4837c478bd9Sstevel@tonic-gate * This field signifies something happend during request
4847c478bd9Sstevel@tonic-gate * message trasmission. This field is set by the receiver.
4857c478bd9Sstevel@tonic-gate */
4867c478bd9Sstevel@tonic-gate status = resp_msg_hdr->status;
4877c478bd9Sstevel@tonic-gate if (status != PCP_OK) {
4887c478bd9Sstevel@tonic-gate return (PCPL_XPORT_ERROR);
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate
4915db09ef5Smvgr if (resp_msg_hdr->msg_len != 0) {
4927c478bd9Sstevel@tonic-gate
4937c478bd9Sstevel@tonic-gate /* libpcp users should free this memory */
4945db09ef5Smvgr if ((resp_msg_data = (uint8_t *)malloc(resp_msg_hdr->msg_len))
4955db09ef5Smvgr == NULL)
4967c478bd9Sstevel@tonic-gate return (PCPL_MALLOC_FAIL);
4977c478bd9Sstevel@tonic-gate bzero(resp_msg_data, resp_msg_hdr->msg_len);
4987c478bd9Sstevel@tonic-gate /*
4997c478bd9Sstevel@tonic-gate * Receive response message.
5007c478bd9Sstevel@tonic-gate */
5017c478bd9Sstevel@tonic-gate if ((ret = pcp_io_op(resp_msg_data, resp_msg_hdr->msg_len,
5027c478bd9Sstevel@tonic-gate PCPL_IO_OP_READ))) {
5037c478bd9Sstevel@tonic-gate free(resp_msg_data);
5047c478bd9Sstevel@tonic-gate return (ret);
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate
5077c478bd9Sstevel@tonic-gate #ifdef PCP_CKSUM_ENABLE
5087c478bd9Sstevel@tonic-gate /* verify response message data checksum */
5097c478bd9Sstevel@tonic-gate cksum = checksum((uint16_t *)resp_msg_data,
5107c478bd9Sstevel@tonic-gate resp_msg_hdr->msg_len);
5117c478bd9Sstevel@tonic-gate if (cksum != resp_msg_hdr->msg_cksum) {
5127c478bd9Sstevel@tonic-gate free(resp_msg_data);
5137c478bd9Sstevel@tonic-gate return (PCPL_CKSUM_ERROR);
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate #endif
5165db09ef5Smvgr }
5177c478bd9Sstevel@tonic-gate /* Everything is okay put the received data into user */
5187c478bd9Sstevel@tonic-gate /* application's resp_msg struct */
5197c478bd9Sstevel@tonic-gate resp_msg->msg_len = resp_msg_hdr->msg_len;
5207c478bd9Sstevel@tonic-gate resp_msg->msg_type = resp_msg_hdr->msg_type;
5217c478bd9Sstevel@tonic-gate resp_msg->sub_type = resp_msg_hdr->sub_type;
5227c478bd9Sstevel@tonic-gate resp_msg->msg_data = (uint8_t *)resp_msg_data;
5237c478bd9Sstevel@tonic-gate
5247c478bd9Sstevel@tonic-gate return (PCPL_OK);
5257c478bd9Sstevel@tonic-gate
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate
5287c478bd9Sstevel@tonic-gate /*
5297c478bd9Sstevel@tonic-gate * Function: Get channel property values.
5307c478bd9Sstevel@tonic-gate * Arguments:
5315db09ef5Smvgr * int channel_fd - channel file descriptor.
5327c478bd9Sstevel@tonic-gate * int prop - property id.
5337c478bd9Sstevel@tonic-gate * unsigned int *val - property value tobe copied.
5347c478bd9Sstevel@tonic-gate * Returns:
5357c478bd9Sstevel@tonic-gate * 0 - success
5367c478bd9Sstevel@tonic-gate * (-ve) - failure:
5377c478bd9Sstevel@tonic-gate * PCPL_ERR_GLVC - glvc ioctl failure.
5387c478bd9Sstevel@tonic-gate */
5397c478bd9Sstevel@tonic-gate
5407c478bd9Sstevel@tonic-gate static int
pcp_get_prop(int channel_fd,int prop,unsigned int * val)5415db09ef5Smvgr pcp_get_prop(int channel_fd, int prop, unsigned int *val)
5427c478bd9Sstevel@tonic-gate {
5437c478bd9Sstevel@tonic-gate glvc_xport_opt_op_t channel_op;
5447c478bd9Sstevel@tonic-gate int ret;
5457c478bd9Sstevel@tonic-gate
5467c478bd9Sstevel@tonic-gate channel_op.op_sel = GLVC_XPORT_OPT_GET;
5477c478bd9Sstevel@tonic-gate channel_op.opt_sel = prop;
5487c478bd9Sstevel@tonic-gate channel_op.opt_val = 0;
5497c478bd9Sstevel@tonic-gate
5507c478bd9Sstevel@tonic-gate (void) alarm(glvc_timeout);
5517c478bd9Sstevel@tonic-gate
5525db09ef5Smvgr if ((ret = ioctl(channel_fd, GLVC_XPORT_IOCTL_OPT_OP,
5535db09ef5Smvgr &channel_op)) < 0) {
5545db09ef5Smvgr
5557c478bd9Sstevel@tonic-gate (void) alarm(0);
5567c478bd9Sstevel@tonic-gate return (ret);
5577c478bd9Sstevel@tonic-gate }
5587c478bd9Sstevel@tonic-gate (void) alarm(0);
5597c478bd9Sstevel@tonic-gate
5607c478bd9Sstevel@tonic-gate *val = channel_op.opt_val;
5617c478bd9Sstevel@tonic-gate
5627c478bd9Sstevel@tonic-gate return (0);
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate
5657c478bd9Sstevel@tonic-gate /*
5667c478bd9Sstevel@tonic-gate * Function: wrapper for handling glvc calls (read/write/peek).
5677c478bd9Sstevel@tonic-gate */
5687c478bd9Sstevel@tonic-gate static int
pcp_io_op(void * buf,int byte_cnt,int io_op)5697c478bd9Sstevel@tonic-gate pcp_io_op(void *buf, int byte_cnt, int io_op)
5707c478bd9Sstevel@tonic-gate {
5717c478bd9Sstevel@tonic-gate int rv;
5727c478bd9Sstevel@tonic-gate int n;
5737c478bd9Sstevel@tonic-gate uint8_t *datap;
5747c478bd9Sstevel@tonic-gate int (*func_ptr)(uint8_t *, int);
5757c478bd9Sstevel@tonic-gate int io_sz;
5767c478bd9Sstevel@tonic-gate int try_cnt;
5777c478bd9Sstevel@tonic-gate
5787c478bd9Sstevel@tonic-gate
5797c478bd9Sstevel@tonic-gate if ((buf == NULL) || (byte_cnt < 0)) {
5807c478bd9Sstevel@tonic-gate return (PCPL_INVALID_ARGS);
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate
5837c478bd9Sstevel@tonic-gate switch (io_op) {
5847c478bd9Sstevel@tonic-gate case PCPL_IO_OP_READ:
5857c478bd9Sstevel@tonic-gate func_ptr = pcp_read;
5867c478bd9Sstevel@tonic-gate break;
5877c478bd9Sstevel@tonic-gate case PCPL_IO_OP_WRITE:
5887c478bd9Sstevel@tonic-gate func_ptr = pcp_write;
5897c478bd9Sstevel@tonic-gate break;
5907c478bd9Sstevel@tonic-gate case PCPL_IO_OP_PEEK:
5917c478bd9Sstevel@tonic-gate func_ptr = pcp_peek;
5927c478bd9Sstevel@tonic-gate break;
5937c478bd9Sstevel@tonic-gate default:
5947c478bd9Sstevel@tonic-gate return (PCPL_INVALID_ARGS);
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate
5977c478bd9Sstevel@tonic-gate /*
5987c478bd9Sstevel@tonic-gate * loop until all I/O done, try limit exceded, or real failure
5997c478bd9Sstevel@tonic-gate */
6007c478bd9Sstevel@tonic-gate
6017c478bd9Sstevel@tonic-gate rv = 0;
6027c478bd9Sstevel@tonic-gate datap = buf;
6037c478bd9Sstevel@tonic-gate while (rv < byte_cnt) {
6047c478bd9Sstevel@tonic-gate io_sz = MIN((byte_cnt - rv), mtu_size);
6057c478bd9Sstevel@tonic-gate try_cnt = 0;
6067c478bd9Sstevel@tonic-gate while ((n = (*func_ptr)(datap, io_sz)) < 0) {
6077c478bd9Sstevel@tonic-gate try_cnt++;
6087c478bd9Sstevel@tonic-gate if (try_cnt > PCPL_MAX_TRY_CNT) {
6097c478bd9Sstevel@tonic-gate rv = n;
6107c478bd9Sstevel@tonic-gate goto done;
6117c478bd9Sstevel@tonic-gate }
6127c478bd9Sstevel@tonic-gate (void) sleep(PCPL_GLVC_SLEEP);
6137c478bd9Sstevel@tonic-gate } /* while trying the io operation */
6147c478bd9Sstevel@tonic-gate
6157c478bd9Sstevel@tonic-gate if (n < 0) {
6167c478bd9Sstevel@tonic-gate rv = n;
6177c478bd9Sstevel@tonic-gate goto done;
6187c478bd9Sstevel@tonic-gate }
6197c478bd9Sstevel@tonic-gate rv += n;
6207c478bd9Sstevel@tonic-gate datap += n;
6217c478bd9Sstevel@tonic-gate } /* while still have more data */
6227c478bd9Sstevel@tonic-gate
6237c478bd9Sstevel@tonic-gate done:
6247c478bd9Sstevel@tonic-gate if (rv == byte_cnt)
6257c478bd9Sstevel@tonic-gate return (0);
6267c478bd9Sstevel@tonic-gate else
6277c478bd9Sstevel@tonic-gate return (PCPL_GLVC_ERROR);
6287c478bd9Sstevel@tonic-gate }
6297c478bd9Sstevel@tonic-gate
6307c478bd9Sstevel@tonic-gate /*
6317c478bd9Sstevel@tonic-gate * For peeking 'bytes_cnt' bytes in channel (glvc) buffers.
6327c478bd9Sstevel@tonic-gate * If data is available, the data is copied into 'buf'.
6337c478bd9Sstevel@tonic-gate */
6347c478bd9Sstevel@tonic-gate static int
pcp_peek(uint8_t * buf,int bytes_cnt)6357c478bd9Sstevel@tonic-gate pcp_peek(uint8_t *buf, int bytes_cnt)
6367c478bd9Sstevel@tonic-gate {
6377c478bd9Sstevel@tonic-gate int ret;
6387c478bd9Sstevel@tonic-gate glvc_xport_msg_peek_t peek_ctrl;
6397c478bd9Sstevel@tonic-gate int n, m;
6407c478bd9Sstevel@tonic-gate
6417c478bd9Sstevel@tonic-gate if (bytes_cnt < 0 || bytes_cnt > mtu_size) {
6427c478bd9Sstevel@tonic-gate return (PCPL_INVALID_ARGS);
6437c478bd9Sstevel@tonic-gate }
6447c478bd9Sstevel@tonic-gate
6457c478bd9Sstevel@tonic-gate /*
6467c478bd9Sstevel@tonic-gate * initialization of buffers used for peeking data in channel buffers.
6477c478bd9Sstevel@tonic-gate */
6487c478bd9Sstevel@tonic-gate if (peek_area == NULL) {
6497c478bd9Sstevel@tonic-gate peek_area = (uint8_t *)umem_zalloc(PEEK_AREA_SIZE,
6507c478bd9Sstevel@tonic-gate UMEM_DEFAULT);
6517c478bd9Sstevel@tonic-gate if (peek_area == NULL) {
6527c478bd9Sstevel@tonic-gate return (PCPL_MALLOC_FAIL);
6537c478bd9Sstevel@tonic-gate }
6547c478bd9Sstevel@tonic-gate }
6557c478bd9Sstevel@tonic-gate
6567c478bd9Sstevel@tonic-gate /*
6577c478bd9Sstevel@tonic-gate * peek max MTU size bytes
6587c478bd9Sstevel@tonic-gate */
6597c478bd9Sstevel@tonic-gate peek_ctrl.buf = (caddr_t)peek_area;
6607c478bd9Sstevel@tonic-gate peek_ctrl.buflen = mtu_size;
6617c478bd9Sstevel@tonic-gate peek_ctrl.flags = 0;
6627c478bd9Sstevel@tonic-gate
6637c478bd9Sstevel@tonic-gate (void) alarm(glvc_timeout);
6647c478bd9Sstevel@tonic-gate
6657c478bd9Sstevel@tonic-gate if ((ret = ioctl(chnl_fd, GLVC_XPORT_IOCTL_DATA_PEEK, &peek_ctrl))
6667c478bd9Sstevel@tonic-gate < 0) {
6677c478bd9Sstevel@tonic-gate (void) alarm(0);
6687c478bd9Sstevel@tonic-gate return (ret);
6697c478bd9Sstevel@tonic-gate }
6707c478bd9Sstevel@tonic-gate (void) alarm(0);
6717c478bd9Sstevel@tonic-gate
6727c478bd9Sstevel@tonic-gate n = peek_ctrl.buflen;
6737c478bd9Sstevel@tonic-gate
6747c478bd9Sstevel@tonic-gate if (n < 0)
6757c478bd9Sstevel@tonic-gate return (PCPL_GLVC_ERROR);
6767c478bd9Sstevel@tonic-gate
6777c478bd9Sstevel@tonic-gate /*
6787c478bd9Sstevel@tonic-gate * satisfy request as best as we can
6797c478bd9Sstevel@tonic-gate */
6807c478bd9Sstevel@tonic-gate m = MIN(bytes_cnt, n);
6817c478bd9Sstevel@tonic-gate (void) memcpy(buf, peek_area, m);
6827c478bd9Sstevel@tonic-gate
6837c478bd9Sstevel@tonic-gate return (m);
6847c478bd9Sstevel@tonic-gate }
6857c478bd9Sstevel@tonic-gate
6867c478bd9Sstevel@tonic-gate /*
6877c478bd9Sstevel@tonic-gate * Function: write 'byte_cnt' bytes from 'buf' to channel.
6887c478bd9Sstevel@tonic-gate */
6897c478bd9Sstevel@tonic-gate static int
pcp_write(uint8_t * buf,int byte_cnt)6907c478bd9Sstevel@tonic-gate pcp_write(uint8_t *buf, int byte_cnt)
6917c478bd9Sstevel@tonic-gate {
6927c478bd9Sstevel@tonic-gate
6937c478bd9Sstevel@tonic-gate int ret;
6947c478bd9Sstevel@tonic-gate
6957c478bd9Sstevel@tonic-gate /* check for valid arguments */
6967c478bd9Sstevel@tonic-gate if (buf == NULL || byte_cnt < 0 || byte_cnt > mtu_size) {
6977c478bd9Sstevel@tonic-gate return (PCPL_INVALID_ARGS);
6987c478bd9Sstevel@tonic-gate }
6997c478bd9Sstevel@tonic-gate
7001ae08745Sheppo if (xport_type == GLVC_NON_STREAM) {
7017c478bd9Sstevel@tonic-gate (void) alarm(glvc_timeout);
7027c478bd9Sstevel@tonic-gate
7037c478bd9Sstevel@tonic-gate if ((ret = write(chnl_fd, buf, byte_cnt)) < 0) {
7047c478bd9Sstevel@tonic-gate (void) alarm(0);
7057c478bd9Sstevel@tonic-gate return (ret);
7067c478bd9Sstevel@tonic-gate }
7077c478bd9Sstevel@tonic-gate (void) alarm(0);
7081ae08745Sheppo } else {
7091ae08745Sheppo if ((ret = vldc_write(chnl_fd, buf, byte_cnt)) <= 0) {
7101ae08745Sheppo return (ret);
7111ae08745Sheppo }
7121ae08745Sheppo }
7137c478bd9Sstevel@tonic-gate
7147c478bd9Sstevel@tonic-gate return (ret);
7157c478bd9Sstevel@tonic-gate }
7167c478bd9Sstevel@tonic-gate
7177c478bd9Sstevel@tonic-gate /*
7187c478bd9Sstevel@tonic-gate * In current implementaion of glvc driver, streams reads are not supported.
7197c478bd9Sstevel@tonic-gate * pcp_read mimics stream reads by first reading all the bytes present in the
7207c478bd9Sstevel@tonic-gate * channel buffer into a local buffer and from then on read requests
7217c478bd9Sstevel@tonic-gate * are serviced from local buffer. When read requests are not serviceble
7227c478bd9Sstevel@tonic-gate * from local buffer, it repeates by first reading data from channel buffers.
7237c478bd9Sstevel@tonic-gate *
7247c478bd9Sstevel@tonic-gate * This call may need to be enhanced when glvc supports buffered (stream)
7257c478bd9Sstevel@tonic-gate * reads - TBD
7267c478bd9Sstevel@tonic-gate */
7277c478bd9Sstevel@tonic-gate
7287c478bd9Sstevel@tonic-gate static int
pcp_read(uint8_t * buf,int byte_cnt)7297c478bd9Sstevel@tonic-gate pcp_read(uint8_t *buf, int byte_cnt)
7307c478bd9Sstevel@tonic-gate {
7317c478bd9Sstevel@tonic-gate int ret;
7327c478bd9Sstevel@tonic-gate int n, m, i;
7337c478bd9Sstevel@tonic-gate
7347c478bd9Sstevel@tonic-gate if (byte_cnt < 0 || byte_cnt > mtu_size) {
7357c478bd9Sstevel@tonic-gate return (PCPL_INVALID_ARGS);
7367c478bd9Sstevel@tonic-gate }
7377c478bd9Sstevel@tonic-gate
7387c478bd9Sstevel@tonic-gate /*
7397c478bd9Sstevel@tonic-gate * initialization of local read buffer
7407c478bd9Sstevel@tonic-gate * from which the stream read requests are serviced.
7417c478bd9Sstevel@tonic-gate */
7427c478bd9Sstevel@tonic-gate if (read_area == NULL) {
7437c478bd9Sstevel@tonic-gate read_area = (uint8_t *)umem_zalloc(READ_AREA_SIZE,
7447c478bd9Sstevel@tonic-gate UMEM_DEFAULT);
7457c478bd9Sstevel@tonic-gate if (read_area == NULL) {
7467c478bd9Sstevel@tonic-gate return (PCPL_MALLOC_FAIL);
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate read_head = read_area;
7497c478bd9Sstevel@tonic-gate read_tail = read_area;
7507c478bd9Sstevel@tonic-gate }
7517c478bd9Sstevel@tonic-gate
7527c478bd9Sstevel@tonic-gate /*
7537c478bd9Sstevel@tonic-gate * if we already read this data then copy from local buffer it self
7547c478bd9Sstevel@tonic-gate * without calling new read.
7557c478bd9Sstevel@tonic-gate */
7567c478bd9Sstevel@tonic-gate if (byte_cnt <= (read_tail - read_head)) {
7577c478bd9Sstevel@tonic-gate (void) memcpy(buf, read_head, byte_cnt);
7587c478bd9Sstevel@tonic-gate read_head += byte_cnt;
7597c478bd9Sstevel@tonic-gate return (byte_cnt);
7607c478bd9Sstevel@tonic-gate }
7617c478bd9Sstevel@tonic-gate
7627c478bd9Sstevel@tonic-gate /*
7637c478bd9Sstevel@tonic-gate * if the request is not satisfied from the buffered data, then move the
7647c478bd9Sstevel@tonic-gate * remaining data to front of the buffer and read new data.
7657c478bd9Sstevel@tonic-gate */
7667c478bd9Sstevel@tonic-gate for (i = 0; i < (read_tail - read_head); ++i) {
7677c478bd9Sstevel@tonic-gate read_area[i] = read_head[i];
7687c478bd9Sstevel@tonic-gate }
7697c478bd9Sstevel@tonic-gate read_head = read_area;
7707c478bd9Sstevel@tonic-gate read_tail = read_head + i;
7717c478bd9Sstevel@tonic-gate
7727c478bd9Sstevel@tonic-gate /*
7737c478bd9Sstevel@tonic-gate * do a peek to see how much data is available and read complete data.
7747c478bd9Sstevel@tonic-gate */
7757c478bd9Sstevel@tonic-gate
7761ae08745Sheppo if (xport_type == GLVC_NON_STREAM) {
7777c478bd9Sstevel@tonic-gate if ((m = pcp_peek(read_tail, mtu_size)) < 0) {
7787c478bd9Sstevel@tonic-gate return (m);
7797c478bd9Sstevel@tonic-gate }
7807c478bd9Sstevel@tonic-gate
7817c478bd9Sstevel@tonic-gate (void) alarm(glvc_timeout);
7827c478bd9Sstevel@tonic-gate if ((ret = read(chnl_fd, read_tail, m)) < 0) {
7837c478bd9Sstevel@tonic-gate (void) alarm(0);
7847c478bd9Sstevel@tonic-gate return (ret);
7857c478bd9Sstevel@tonic-gate }
7867c478bd9Sstevel@tonic-gate
7877c478bd9Sstevel@tonic-gate (void) alarm(0);
7881ae08745Sheppo } else {
7891ae08745Sheppo /*
7901ae08745Sheppo * Read the extra number of bytes
7911ae08745Sheppo */
7921ae08745Sheppo m = byte_cnt - (read_tail - read_head);
7931ae08745Sheppo if ((ret = vldc_read(chnl_fd,
7941ae08745Sheppo read_tail, m)) <= 0) {
7951ae08745Sheppo return (ret);
7961ae08745Sheppo }
7971ae08745Sheppo }
7987c478bd9Sstevel@tonic-gate read_tail += ret;
7997c478bd9Sstevel@tonic-gate
8007c478bd9Sstevel@tonic-gate /*
8017c478bd9Sstevel@tonic-gate * copy the requested bytes.
8027c478bd9Sstevel@tonic-gate */
8037c478bd9Sstevel@tonic-gate n = MIN(byte_cnt, (read_tail - read_head));
8047c478bd9Sstevel@tonic-gate (void) memcpy(buf, read_head, n);
8057c478bd9Sstevel@tonic-gate
8067c478bd9Sstevel@tonic-gate read_head += n;
8077c478bd9Sstevel@tonic-gate
8087c478bd9Sstevel@tonic-gate return (n);
8097c478bd9Sstevel@tonic-gate }
8107c478bd9Sstevel@tonic-gate
8117c478bd9Sstevel@tonic-gate /*
8121ae08745Sheppo * Issue read from the driver until byet_cnt number
8131ae08745Sheppo * of bytes are present in read buffer. Do not
8141ae08745Sheppo * move the read head.
8151ae08745Sheppo */
8161ae08745Sheppo static int
pcp_update_read_area(int byte_cnt)8171ae08745Sheppo pcp_update_read_area(int byte_cnt)
8181ae08745Sheppo {
8191ae08745Sheppo int ret;
8201ae08745Sheppo int n, i;
8211ae08745Sheppo
8221ae08745Sheppo if (byte_cnt < 0 || byte_cnt > mtu_size) {
8231ae08745Sheppo return (PCPL_INVALID_ARGS);
8241ae08745Sheppo }
8251ae08745Sheppo
8261ae08745Sheppo /*
8271ae08745Sheppo * initialization of local read buffer
8281ae08745Sheppo * from which the stream read requests are serviced.
8291ae08745Sheppo */
8301ae08745Sheppo if (read_area == NULL) {
8311ae08745Sheppo read_area = (uint8_t *)umem_zalloc(READ_AREA_SIZE,
8321ae08745Sheppo UMEM_DEFAULT);
8331ae08745Sheppo if (read_area == NULL) {
8341ae08745Sheppo return (PCPL_MALLOC_FAIL);
8351ae08745Sheppo }
8361ae08745Sheppo read_head = read_area;
8371ae08745Sheppo read_tail = read_area;
8381ae08745Sheppo }
8391ae08745Sheppo
8401ae08745Sheppo /*
8411ae08745Sheppo * if we already have sufficient data in the buffer,
8421ae08745Sheppo * just return
8431ae08745Sheppo */
8441ae08745Sheppo if (byte_cnt <= (read_tail - read_head)) {
8451ae08745Sheppo return (byte_cnt);
8461ae08745Sheppo }
8471ae08745Sheppo
8481ae08745Sheppo /*
8491ae08745Sheppo * if the request is not satisfied from the buffered data, then move the
8501ae08745Sheppo * remaining data to front of the buffer and read new data.
8511ae08745Sheppo */
8521ae08745Sheppo for (i = 0; i < (read_tail - read_head); ++i) {
8531ae08745Sheppo read_area[i] = read_head[i];
8541ae08745Sheppo }
8551ae08745Sheppo read_head = read_area;
8561ae08745Sheppo read_tail = read_head + i;
8571ae08745Sheppo
8581ae08745Sheppo n = byte_cnt - (read_tail - read_head);
8591ae08745Sheppo
8601ae08745Sheppo if ((ret = vldc_read(chnl_fd,
8611ae08745Sheppo read_tail, n)) <= 0) {
8621ae08745Sheppo return (ret);
8631ae08745Sheppo }
8641ae08745Sheppo read_tail += ret;
8651ae08745Sheppo
8661ae08745Sheppo /*
8671ae08745Sheppo * Return the number of bytes we could read
8681ae08745Sheppo */
8691ae08745Sheppo n = MIN(byte_cnt, (read_tail - read_head));
8701ae08745Sheppo
8711ae08745Sheppo return (n);
8721ae08745Sheppo }
8731ae08745Sheppo
8741ae08745Sheppo /*
8757c478bd9Sstevel@tonic-gate * This function is slight different from pcp_peek. The peek requests are first
8767c478bd9Sstevel@tonic-gate * serviced from local read buffer, if data is available. If the peek request
8777c478bd9Sstevel@tonic-gate * is not serviceble from local read buffer, then the data is peeked from
8787c478bd9Sstevel@tonic-gate * channel buffer. This function is mainly used for proper protocol framing
8797c478bd9Sstevel@tonic-gate * error handling.
8807c478bd9Sstevel@tonic-gate */
8817c478bd9Sstevel@tonic-gate static int
pcp_peek_read(uint8_t * buf,int byte_cnt)8827c478bd9Sstevel@tonic-gate pcp_peek_read(uint8_t *buf, int byte_cnt)
8837c478bd9Sstevel@tonic-gate {
8847c478bd9Sstevel@tonic-gate int n, m, i;
8857c478bd9Sstevel@tonic-gate
8867c478bd9Sstevel@tonic-gate if (byte_cnt < 0 || byte_cnt > mtu_size) {
8877c478bd9Sstevel@tonic-gate return (PCPL_INVALID_ARGS);
8887c478bd9Sstevel@tonic-gate }
8897c478bd9Sstevel@tonic-gate
8907c478bd9Sstevel@tonic-gate /*
8917c478bd9Sstevel@tonic-gate * initialization of peek_read buffer.
8927c478bd9Sstevel@tonic-gate */
8937c478bd9Sstevel@tonic-gate if (peek_read_area == NULL) {
8947c478bd9Sstevel@tonic-gate peek_read_area = (uint8_t *)umem_zalloc(PEEK_READ_AREA_SIZE,
8957c478bd9Sstevel@tonic-gate UMEM_DEFAULT);
8967c478bd9Sstevel@tonic-gate if (peek_read_area == NULL) {
8977c478bd9Sstevel@tonic-gate return (PCPL_MALLOC_FAIL);
8987c478bd9Sstevel@tonic-gate }
8997c478bd9Sstevel@tonic-gate peek_read_head = peek_read_area;
9007c478bd9Sstevel@tonic-gate peek_read_tail = peek_read_area;
9017c478bd9Sstevel@tonic-gate }
9027c478bd9Sstevel@tonic-gate
9037c478bd9Sstevel@tonic-gate /*
9047c478bd9Sstevel@tonic-gate * if we already have the data in local read buffer then copy
9057c478bd9Sstevel@tonic-gate * from local buffer it self w/out calling new peek
9067c478bd9Sstevel@tonic-gate */
9077c478bd9Sstevel@tonic-gate if (byte_cnt <= (read_tail - read_head)) {
9087c478bd9Sstevel@tonic-gate (void) memcpy(buf, read_head, byte_cnt);
9097c478bd9Sstevel@tonic-gate return (byte_cnt);
9107c478bd9Sstevel@tonic-gate }
9117c478bd9Sstevel@tonic-gate
9127c478bd9Sstevel@tonic-gate /*
9137c478bd9Sstevel@tonic-gate * if the request is not satisfied from local read buffer, then first
9147c478bd9Sstevel@tonic-gate * copy the remaining data in local read buffer to peek_read_area and
9157c478bd9Sstevel@tonic-gate * then issue new peek.
9167c478bd9Sstevel@tonic-gate */
9177c478bd9Sstevel@tonic-gate for (i = 0; i < (read_tail - read_head); ++i) {
9187c478bd9Sstevel@tonic-gate peek_read_area[i] = read_head[i];
9197c478bd9Sstevel@tonic-gate }
9207c478bd9Sstevel@tonic-gate peek_read_head = peek_read_area;
9217c478bd9Sstevel@tonic-gate peek_read_tail = peek_read_head + i;
9227c478bd9Sstevel@tonic-gate
9237c478bd9Sstevel@tonic-gate /*
9247c478bd9Sstevel@tonic-gate * do a peek to see how much data is available and read complete data.
9257c478bd9Sstevel@tonic-gate */
9267c478bd9Sstevel@tonic-gate
9277c478bd9Sstevel@tonic-gate if ((m = pcp_peek(peek_read_tail, mtu_size)) < 0) {
9287c478bd9Sstevel@tonic-gate return (m);
9297c478bd9Sstevel@tonic-gate }
9307c478bd9Sstevel@tonic-gate peek_read_tail += m;
9317c478bd9Sstevel@tonic-gate
9327c478bd9Sstevel@tonic-gate /*
9337c478bd9Sstevel@tonic-gate * copy the requested bytes
9347c478bd9Sstevel@tonic-gate */
9357c478bd9Sstevel@tonic-gate n = MIN(byte_cnt, (peek_read_tail - peek_read_head));
9367c478bd9Sstevel@tonic-gate (void) memcpy(buf, peek_read_head, n);
9377c478bd9Sstevel@tonic-gate
9387c478bd9Sstevel@tonic-gate return (n);
9397c478bd9Sstevel@tonic-gate }
9407c478bd9Sstevel@tonic-gate
9417c478bd9Sstevel@tonic-gate /*
9427c478bd9Sstevel@tonic-gate * Send Request Message Header.
9437c478bd9Sstevel@tonic-gate */
9447c478bd9Sstevel@tonic-gate static int
pcp_send_req_msg_hdr(pcp_req_msg_hdr_t * req_hdr)9457c478bd9Sstevel@tonic-gate pcp_send_req_msg_hdr(pcp_req_msg_hdr_t *req_hdr)
9467c478bd9Sstevel@tonic-gate {
9477c478bd9Sstevel@tonic-gate pcp_req_msg_hdr_t *hdrp;
9487c478bd9Sstevel@tonic-gate int hdr_sz;
9497c478bd9Sstevel@tonic-gate int ret;
9507c478bd9Sstevel@tonic-gate
9517c478bd9Sstevel@tonic-gate hdr_sz = sizeof (pcp_req_msg_hdr_t);
9527c478bd9Sstevel@tonic-gate if ((hdrp = (pcp_req_msg_hdr_t *)umem_zalloc(hdr_sz,
9537c478bd9Sstevel@tonic-gate UMEM_DEFAULT)) == NULL) {
9547c478bd9Sstevel@tonic-gate return (PCPL_MALLOC_FAIL);
9557c478bd9Sstevel@tonic-gate }
9567c478bd9Sstevel@tonic-gate
9577c478bd9Sstevel@tonic-gate hdrp->magic_num = htonl(req_hdr->magic_num);
9587c478bd9Sstevel@tonic-gate hdrp->proto_ver = req_hdr->proto_ver;
9597c478bd9Sstevel@tonic-gate hdrp->msg_type = req_hdr->msg_type;
9607c478bd9Sstevel@tonic-gate hdrp->sub_type = req_hdr->sub_type;
9617c478bd9Sstevel@tonic-gate hdrp->rsvd_pad = htons(req_hdr->rsvd_pad);
9627c478bd9Sstevel@tonic-gate hdrp->xid = htonl(req_hdr->xid);
9637c478bd9Sstevel@tonic-gate hdrp->timeout = htonl(req_hdr->timeout);
9647c478bd9Sstevel@tonic-gate hdrp->msg_len = htonl(req_hdr->msg_len);
9657c478bd9Sstevel@tonic-gate hdrp->msg_cksum = htons(req_hdr->msg_cksum);
9667c478bd9Sstevel@tonic-gate hdrp->hdr_cksum = htons(req_hdr->hdr_cksum);
9677c478bd9Sstevel@tonic-gate
9687c478bd9Sstevel@tonic-gate if ((ret = pcp_io_op((char *)hdrp, hdr_sz, PCPL_IO_OP_WRITE)) != 0) {
9697c478bd9Sstevel@tonic-gate umem_free(hdrp, hdr_sz);
9707c478bd9Sstevel@tonic-gate return (ret);
9717c478bd9Sstevel@tonic-gate }
9727c478bd9Sstevel@tonic-gate umem_free(hdrp, hdr_sz);
9737c478bd9Sstevel@tonic-gate return (PCP_OK);
9747c478bd9Sstevel@tonic-gate }
9757c478bd9Sstevel@tonic-gate
9767c478bd9Sstevel@tonic-gate /*
9777c478bd9Sstevel@tonic-gate * Receive Response message header.
9787c478bd9Sstevel@tonic-gate */
9797c478bd9Sstevel@tonic-gate static int
pcp_recv_resp_msg_hdr(pcp_resp_msg_hdr_t * resp_hdr)9807c478bd9Sstevel@tonic-gate pcp_recv_resp_msg_hdr(pcp_resp_msg_hdr_t *resp_hdr)
9817c478bd9Sstevel@tonic-gate {
9827c478bd9Sstevel@tonic-gate uint32_t magic_num;
9837c478bd9Sstevel@tonic-gate uint8_t proto_ver;
9847c478bd9Sstevel@tonic-gate uint8_t msg_type;
9857c478bd9Sstevel@tonic-gate uint8_t sub_type;
9867c478bd9Sstevel@tonic-gate uint8_t rsvd_pad;
9877c478bd9Sstevel@tonic-gate uint32_t xid;
9887c478bd9Sstevel@tonic-gate uint32_t timeout;
9897c478bd9Sstevel@tonic-gate uint32_t msg_len;
9907c478bd9Sstevel@tonic-gate uint32_t status;
9917c478bd9Sstevel@tonic-gate uint16_t msg_cksum;
9927c478bd9Sstevel@tonic-gate uint16_t hdr_cksum;
9937c478bd9Sstevel@tonic-gate int ret;
9947c478bd9Sstevel@tonic-gate
9957c478bd9Sstevel@tonic-gate if (resp_hdr == NULL) {
9967c478bd9Sstevel@tonic-gate return (PCPL_INVALID_ARGS);
9977c478bd9Sstevel@tonic-gate }
9987c478bd9Sstevel@tonic-gate
9997c478bd9Sstevel@tonic-gate /*
10007c478bd9Sstevel@tonic-gate * handle protocol framing errors.
10017c478bd9Sstevel@tonic-gate * pcp_frame_error_handle() returns when proper frame arrived
10027c478bd9Sstevel@tonic-gate * (magic seq) or if an error happens while reading data from
10037c478bd9Sstevel@tonic-gate * channel.
10047c478bd9Sstevel@tonic-gate */
10051ae08745Sheppo if (xport_type == GLVC_NON_STREAM)
10061ae08745Sheppo ret = pcp_frame_error_handle();
10071ae08745Sheppo else
10081ae08745Sheppo ret = pcp_vldc_frame_error_handle();
10091ae08745Sheppo
10101ae08745Sheppo if (ret != 0)
10117c478bd9Sstevel@tonic-gate return (PCPL_FRAME_ERROR);
10127c478bd9Sstevel@tonic-gate
10137c478bd9Sstevel@tonic-gate /* read magic number first */
10147c478bd9Sstevel@tonic-gate if ((ret = pcp_io_op(&magic_num, sizeof (magic_num),
10157c478bd9Sstevel@tonic-gate PCPL_IO_OP_READ)) != 0) {
10167c478bd9Sstevel@tonic-gate return (ret);
10177c478bd9Sstevel@tonic-gate }
10187c478bd9Sstevel@tonic-gate
10197c478bd9Sstevel@tonic-gate magic_num = ntohl(magic_num);
10207c478bd9Sstevel@tonic-gate
10217c478bd9Sstevel@tonic-gate if (magic_num != PCP_MAGIC_NUM) {
10227c478bd9Sstevel@tonic-gate return (PCPL_FRAME_ERROR);
10237c478bd9Sstevel@tonic-gate }
10247c478bd9Sstevel@tonic-gate
10257c478bd9Sstevel@tonic-gate /* read version field */
10267c478bd9Sstevel@tonic-gate if ((ret = pcp_io_op(&proto_ver, sizeof (proto_ver),
10277c478bd9Sstevel@tonic-gate PCPL_IO_OP_READ)) != 0) {
10287c478bd9Sstevel@tonic-gate return (ret);
10297c478bd9Sstevel@tonic-gate }
10307c478bd9Sstevel@tonic-gate
10317c478bd9Sstevel@tonic-gate /* check protocol version */
10327c478bd9Sstevel@tonic-gate if (proto_ver != PCP_PROT_VER_1) {
10337c478bd9Sstevel@tonic-gate return (PCPL_PROT_ERROR);
10347c478bd9Sstevel@tonic-gate }
10357c478bd9Sstevel@tonic-gate
10367c478bd9Sstevel@tonic-gate /* Read message type */
10377c478bd9Sstevel@tonic-gate if ((ret = pcp_io_op(&msg_type, sizeof (msg_type),
10387c478bd9Sstevel@tonic-gate PCPL_IO_OP_READ)) != 0) {
10397c478bd9Sstevel@tonic-gate return (ret);
10407c478bd9Sstevel@tonic-gate }
10417c478bd9Sstevel@tonic-gate
10427c478bd9Sstevel@tonic-gate /* Read message sub type */
10437c478bd9Sstevel@tonic-gate if ((ret = pcp_io_op(&sub_type, sizeof (sub_type),
10447c478bd9Sstevel@tonic-gate PCPL_IO_OP_READ)) != 0) {
10457c478bd9Sstevel@tonic-gate return (ret);
10467c478bd9Sstevel@tonic-gate }
10477c478bd9Sstevel@tonic-gate
10487c478bd9Sstevel@tonic-gate /* Read rcvd_pad bits */
10497c478bd9Sstevel@tonic-gate if ((ret = pcp_io_op(&rsvd_pad, sizeof (rsvd_pad),
10507c478bd9Sstevel@tonic-gate PCPL_IO_OP_READ)) != 0) {
10517c478bd9Sstevel@tonic-gate return (ret);
10527c478bd9Sstevel@tonic-gate }
10537c478bd9Sstevel@tonic-gate
10547c478bd9Sstevel@tonic-gate /* receive transaction id */
10557c478bd9Sstevel@tonic-gate if ((ret = pcp_io_op(&xid, sizeof (xid),
10567c478bd9Sstevel@tonic-gate PCPL_IO_OP_READ)) != 0) {
10577c478bd9Sstevel@tonic-gate return (ret);
10587c478bd9Sstevel@tonic-gate }
10597c478bd9Sstevel@tonic-gate
10607c478bd9Sstevel@tonic-gate xid = ntohl(xid);
10617c478bd9Sstevel@tonic-gate
10627c478bd9Sstevel@tonic-gate /* receive timeout value */
10637c478bd9Sstevel@tonic-gate if ((ret = pcp_io_op(&timeout, sizeof (timeout),
10647c478bd9Sstevel@tonic-gate PCPL_IO_OP_READ)) != 0) {
10657c478bd9Sstevel@tonic-gate return (ret);
10667c478bd9Sstevel@tonic-gate }
10677c478bd9Sstevel@tonic-gate
10687c478bd9Sstevel@tonic-gate timeout = ntohl(timeout);
10697c478bd9Sstevel@tonic-gate
10707c478bd9Sstevel@tonic-gate /* receive message length */
10717c478bd9Sstevel@tonic-gate if ((ret = pcp_io_op(&msg_len, sizeof (msg_len),
10727c478bd9Sstevel@tonic-gate PCPL_IO_OP_READ)) != 0) {
10737c478bd9Sstevel@tonic-gate return (ret);
10747c478bd9Sstevel@tonic-gate }
10757c478bd9Sstevel@tonic-gate
10767c478bd9Sstevel@tonic-gate msg_len = ntohl(msg_len);
10777c478bd9Sstevel@tonic-gate
10787c478bd9Sstevel@tonic-gate /* receive status field */
10797c478bd9Sstevel@tonic-gate if ((ret = pcp_io_op(&status, sizeof (status),
10807c478bd9Sstevel@tonic-gate PCPL_IO_OP_READ)) != 0) {
10817c478bd9Sstevel@tonic-gate return (ret);
10827c478bd9Sstevel@tonic-gate }
10837c478bd9Sstevel@tonic-gate
10847c478bd9Sstevel@tonic-gate status = ntohl(status);
10857c478bd9Sstevel@tonic-gate
10867c478bd9Sstevel@tonic-gate /* receive message checksum */
10877c478bd9Sstevel@tonic-gate if ((ret = pcp_io_op(&msg_cksum, sizeof (msg_cksum),
10887c478bd9Sstevel@tonic-gate PCPL_IO_OP_READ)) != 0) {
10897c478bd9Sstevel@tonic-gate return (ret);
10907c478bd9Sstevel@tonic-gate }
10917c478bd9Sstevel@tonic-gate
10927c478bd9Sstevel@tonic-gate msg_cksum = ntohs(msg_cksum);
10937c478bd9Sstevel@tonic-gate
10947c478bd9Sstevel@tonic-gate /* receive header checksum */
10957c478bd9Sstevel@tonic-gate if ((ret = pcp_io_op(&hdr_cksum, sizeof (hdr_cksum),
10967c478bd9Sstevel@tonic-gate PCPL_IO_OP_READ)) != 0) {
10977c478bd9Sstevel@tonic-gate return (ret);
10987c478bd9Sstevel@tonic-gate }
10997c478bd9Sstevel@tonic-gate
11007c478bd9Sstevel@tonic-gate hdr_cksum = ntohs(hdr_cksum);
11017c478bd9Sstevel@tonic-gate
11027c478bd9Sstevel@tonic-gate /* copy to resp_hdr */
11037c478bd9Sstevel@tonic-gate
11047c478bd9Sstevel@tonic-gate resp_hdr->magic_num = magic_num;
11057c478bd9Sstevel@tonic-gate resp_hdr->proto_ver = proto_ver;
11067c478bd9Sstevel@tonic-gate resp_hdr->msg_type = msg_type;
11077c478bd9Sstevel@tonic-gate resp_hdr->sub_type = sub_type;
11087c478bd9Sstevel@tonic-gate resp_hdr->rsvd_pad = rsvd_pad;
11097c478bd9Sstevel@tonic-gate resp_hdr->xid = xid;
11107c478bd9Sstevel@tonic-gate resp_hdr->timeout = timeout;
11117c478bd9Sstevel@tonic-gate resp_hdr->msg_len = msg_len;
11127c478bd9Sstevel@tonic-gate resp_hdr->status = status;
11137c478bd9Sstevel@tonic-gate resp_hdr->msg_cksum = msg_cksum;
11147c478bd9Sstevel@tonic-gate resp_hdr->hdr_cksum = hdr_cksum;
11157c478bd9Sstevel@tonic-gate
11167c478bd9Sstevel@tonic-gate return (PCP_OK);
11177c478bd9Sstevel@tonic-gate }
11187c478bd9Sstevel@tonic-gate
11197c478bd9Sstevel@tonic-gate /*
11207c478bd9Sstevel@tonic-gate * Get next xid for including in request message.
11217c478bd9Sstevel@tonic-gate * Every request and response message are matched
11227c478bd9Sstevel@tonic-gate * for same xid.
11237c478bd9Sstevel@tonic-gate */
112456b04ea6Smvgr
112556b04ea6Smvgr static uint32_t
pcp_get_xid(void)112656b04ea6Smvgr pcp_get_xid(void)
11277c478bd9Sstevel@tonic-gate {
11287c478bd9Sstevel@tonic-gate uint32_t ret;
112956b04ea6Smvgr struct timeval tv;
113056b04ea6Smvgr static boolean_t xid_initialized = B_FALSE;
11317c478bd9Sstevel@tonic-gate
113256b04ea6Smvgr if (xid_initialized == B_FALSE) {
113356b04ea6Smvgr xid_initialized = B_TRUE;
113456b04ea6Smvgr /*
113556b04ea6Smvgr * starting xid is initialized to a different value everytime
113656b04ea6Smvgr * user application is restarted so that user apps will not
113756b04ea6Smvgr * receive previous session's packets.
113856b04ea6Smvgr *
113956b04ea6Smvgr * Note: The algorithm for generating initial xid is partially
114056b04ea6Smvgr * taken from Solaris rpc code.
114156b04ea6Smvgr */
114256b04ea6Smvgr (void) gettimeofday(&tv, NULL);
114356b04ea6Smvgr msg_xid = (uint32_t)((tv.tv_sec << 20) |
114456b04ea6Smvgr (tv.tv_usec >> 10));
114556b04ea6Smvgr }
114656b04ea6Smvgr
114756b04ea6Smvgr ret = msg_xid++;
114856b04ea6Smvgr
114956b04ea6Smvgr /* zero xid is not allowed */
115056b04ea6Smvgr if (ret == 0)
115156b04ea6Smvgr ret = msg_xid++;
115256b04ea6Smvgr
11537c478bd9Sstevel@tonic-gate return (ret);
11547c478bd9Sstevel@tonic-gate }
11557c478bd9Sstevel@tonic-gate
11567c478bd9Sstevel@tonic-gate /*
11577c478bd9Sstevel@tonic-gate * This function handles channel framing errors. It waits until proper
11587c478bd9Sstevel@tonic-gate * frame with starting sequence as magic numder (0xAFBCAFA0)
11597c478bd9Sstevel@tonic-gate * is arrived. It removes unexpected data (before the magic number sequence)
11607c478bd9Sstevel@tonic-gate * on the channel. It returns when proper magic number sequence is seen
11617c478bd9Sstevel@tonic-gate * or when any failure happens while reading/peeking the channel.
11627c478bd9Sstevel@tonic-gate */
11637c478bd9Sstevel@tonic-gate static int
pcp_frame_error_handle(void)11647c478bd9Sstevel@tonic-gate pcp_frame_error_handle(void)
11657c478bd9Sstevel@tonic-gate {
11667c478bd9Sstevel@tonic-gate uint8_t magic_num_buf[4];
11677c478bd9Sstevel@tonic-gate int ispresent = 0;
11687c478bd9Sstevel@tonic-gate uint32_t net_magic_num; /* magic byte in network byte order */
11697c478bd9Sstevel@tonic-gate uint32_t host_magic_num = PCP_MAGIC_NUM;
11707c478bd9Sstevel@tonic-gate uint8_t buf[2];
11717c478bd9Sstevel@tonic-gate
11727c478bd9Sstevel@tonic-gate net_magic_num = htonl(host_magic_num);
11737c478bd9Sstevel@tonic-gate (void) memcpy(magic_num_buf, (uint8_t *)&net_magic_num, 4);
11747c478bd9Sstevel@tonic-gate
11757c478bd9Sstevel@tonic-gate while (!ispresent) {
11767c478bd9Sstevel@tonic-gate /*
11777c478bd9Sstevel@tonic-gate * Check if next four bytes matches pcp magic number.
11787c478bd9Sstevel@tonic-gate * if mathing not found, discard 1 byte and continue checking.
11797c478bd9Sstevel@tonic-gate */
11807c478bd9Sstevel@tonic-gate if (!check_magic_byte_presence(4, &magic_num_buf[0],
11817c478bd9Sstevel@tonic-gate &ispresent)) {
11827c478bd9Sstevel@tonic-gate if (!ispresent) {
11837c478bd9Sstevel@tonic-gate /* remove 1 byte */
11847c478bd9Sstevel@tonic-gate (void) pcp_io_op(buf, 1, PCPL_IO_OP_READ);
11857c478bd9Sstevel@tonic-gate }
11867c478bd9Sstevel@tonic-gate } else {
11877c478bd9Sstevel@tonic-gate return (-1);
11887c478bd9Sstevel@tonic-gate }
11897c478bd9Sstevel@tonic-gate }
11907c478bd9Sstevel@tonic-gate
11917c478bd9Sstevel@tonic-gate return (0);
11927c478bd9Sstevel@tonic-gate }
11937c478bd9Sstevel@tonic-gate
11947c478bd9Sstevel@tonic-gate /*
11951ae08745Sheppo * This function handles channel framing errors. It waits until proper
11961ae08745Sheppo * frame with starting sequence as magic numder (0xAFBCAFA0)
11971ae08745Sheppo * is arrived. It removes unexpected data (before the magic number sequence)
11981ae08745Sheppo * on the channel. It returns when proper magic number sequence is seen
11991ae08745Sheppo * or when any failure happens while reading/peeking the channel.
12001ae08745Sheppo */
12011ae08745Sheppo static int
pcp_vldc_frame_error_handle(void)12021ae08745Sheppo pcp_vldc_frame_error_handle(void)
12031ae08745Sheppo {
12041ae08745Sheppo uint8_t magic_num_buf[4];
12051ae08745Sheppo uint32_t net_magic_num; /* magic byte in network byte order */
12061ae08745Sheppo uint32_t host_magic_num = PCP_MAGIC_NUM;
12071ae08745Sheppo int found_magic = 0;
12081ae08745Sheppo
12091ae08745Sheppo net_magic_num = htonl(host_magic_num);
12101ae08745Sheppo (void) memcpy(magic_num_buf, (uint8_t *)&net_magic_num, 4);
12111ae08745Sheppo
12121ae08745Sheppo /*
12131ae08745Sheppo * For vldc, we need to read whatever data is available and
12141ae08745Sheppo * advance the read pointer one byte at a time until we get
12151ae08745Sheppo * the magic word. When this function is invoked, we do not
12161ae08745Sheppo * have any byte in the read buffer.
12171ae08745Sheppo */
12181ae08745Sheppo
12191ae08745Sheppo /*
12201ae08745Sheppo * Keep reading until we find the matching magic number
12211ae08745Sheppo */
12221ae08745Sheppo while (!found_magic) {
12231ae08745Sheppo while ((read_tail - read_head) < sizeof (host_magic_num)) {
12241ae08745Sheppo if (pcp_update_read_area(sizeof (host_magic_num)) < 0)
12251ae08745Sheppo return (-1);
12261ae08745Sheppo }
12271ae08745Sheppo
12281ae08745Sheppo /*
12291ae08745Sheppo * We should have at least 4 bytes in read buffer. Check
12301ae08745Sheppo * if the magic number can be matched
12311ae08745Sheppo */
12321ae08745Sheppo if (memcmp(read_head, magic_num_buf,
12331ae08745Sheppo sizeof (host_magic_num))) {
12341ae08745Sheppo read_head += 1;
12351ae08745Sheppo } else {
12361ae08745Sheppo found_magic = 1;
12371ae08745Sheppo }
12381ae08745Sheppo }
12391ae08745Sheppo
12401ae08745Sheppo return (0);
12411ae08745Sheppo }
12421ae08745Sheppo
12431ae08745Sheppo /*
12447c478bd9Sstevel@tonic-gate * checks whether certain byte sequence is present in the data stream.
12457c478bd9Sstevel@tonic-gate */
12467c478bd9Sstevel@tonic-gate static int
check_magic_byte_presence(int byte_cnt,uint8_t * byte_seq,int * ispresent)12477c478bd9Sstevel@tonic-gate check_magic_byte_presence(int byte_cnt, uint8_t *byte_seq, int *ispresent)
12487c478bd9Sstevel@tonic-gate {
12497c478bd9Sstevel@tonic-gate int ret, i;
12507c478bd9Sstevel@tonic-gate uint8_t buf[4];
12517c478bd9Sstevel@tonic-gate
12527c478bd9Sstevel@tonic-gate if ((ret = pcp_peek_read(buf, byte_cnt)) < 0) {
12537c478bd9Sstevel@tonic-gate return (ret);
12547c478bd9Sstevel@tonic-gate }
12557c478bd9Sstevel@tonic-gate
12567c478bd9Sstevel@tonic-gate /* 'byte_cnt' bytes not present */
12577c478bd9Sstevel@tonic-gate if (ret != byte_cnt) {
12587c478bd9Sstevel@tonic-gate *ispresent = 0;
12597c478bd9Sstevel@tonic-gate return (0);
12607c478bd9Sstevel@tonic-gate }
12617c478bd9Sstevel@tonic-gate
12627c478bd9Sstevel@tonic-gate for (i = 0; i < byte_cnt; ++i) {
12637c478bd9Sstevel@tonic-gate if (buf[i] != byte_seq[i]) {
12647c478bd9Sstevel@tonic-gate *ispresent = 0;
12657c478bd9Sstevel@tonic-gate return (0);
12667c478bd9Sstevel@tonic-gate }
12677c478bd9Sstevel@tonic-gate }
12687c478bd9Sstevel@tonic-gate *ispresent = 1;
12697c478bd9Sstevel@tonic-gate
12707c478bd9Sstevel@tonic-gate return (0);
12717c478bd9Sstevel@tonic-gate }
12727c478bd9Sstevel@tonic-gate
12737c478bd9Sstevel@tonic-gate /*
12747c478bd9Sstevel@tonic-gate * 16-bit simple internet checksum
12757c478bd9Sstevel@tonic-gate */
12767c478bd9Sstevel@tonic-gate static uint16_t
checksum(uint16_t * addr,int32_t count)12777c478bd9Sstevel@tonic-gate checksum(uint16_t *addr, int32_t count)
12787c478bd9Sstevel@tonic-gate {
12797c478bd9Sstevel@tonic-gate /*
12807c478bd9Sstevel@tonic-gate * Compute Internet Checksum for "count" bytes
12817c478bd9Sstevel@tonic-gate * beginning at location "addr".
12827c478bd9Sstevel@tonic-gate */
12837c478bd9Sstevel@tonic-gate
12847c478bd9Sstevel@tonic-gate register uint32_t sum = 0;
12857c478bd9Sstevel@tonic-gate
12867c478bd9Sstevel@tonic-gate while (count > 1) {
12877c478bd9Sstevel@tonic-gate /* This is the inner loop */
12887c478bd9Sstevel@tonic-gate sum += *(unsigned short *)addr++;
12897c478bd9Sstevel@tonic-gate count -= 2;
12907c478bd9Sstevel@tonic-gate }
12917c478bd9Sstevel@tonic-gate
12927c478bd9Sstevel@tonic-gate /* Add left-over byte, if any */
12937c478bd9Sstevel@tonic-gate if (count > 0)
12947c478bd9Sstevel@tonic-gate sum += * (unsigned char *)addr;
12957c478bd9Sstevel@tonic-gate
12967c478bd9Sstevel@tonic-gate /* Fold 32-bit sum to 16 bits */
12977c478bd9Sstevel@tonic-gate while (sum >> 16)
12987c478bd9Sstevel@tonic-gate sum = (sum & 0xffff) + (sum >> 16);
12997c478bd9Sstevel@tonic-gate
13007c478bd9Sstevel@tonic-gate sum = (~sum) & 0xffff;
13017c478bd9Sstevel@tonic-gate if (sum == 0)
13027c478bd9Sstevel@tonic-gate sum = 0xffff;
13037c478bd9Sstevel@tonic-gate
13047c478bd9Sstevel@tonic-gate return (sum);
13057c478bd9Sstevel@tonic-gate }
13067c478bd9Sstevel@tonic-gate
13077c478bd9Sstevel@tonic-gate /*
13087c478bd9Sstevel@tonic-gate * cleanup the channel if any data is hanging in
13097c478bd9Sstevel@tonic-gate * channel buffers.
13107c478bd9Sstevel@tonic-gate */
13117c478bd9Sstevel@tonic-gate static int
pcp_cleanup(int channel_fd)13125db09ef5Smvgr pcp_cleanup(int channel_fd)
13137c478bd9Sstevel@tonic-gate {
13147c478bd9Sstevel@tonic-gate int ret;
13157c478bd9Sstevel@tonic-gate glvc_xport_msg_peek_t peek_ctrl;
13167c478bd9Sstevel@tonic-gate int n, done;
13177c478bd9Sstevel@tonic-gate uint8_t *buf = NULL;
13187c478bd9Sstevel@tonic-gate int retry = 0;
13197c478bd9Sstevel@tonic-gate
13207c478bd9Sstevel@tonic-gate
13217c478bd9Sstevel@tonic-gate buf = (uint8_t *)umem_zalloc((mtu_size), UMEM_DEFAULT);
13227c478bd9Sstevel@tonic-gate if (buf == NULL) {
13237c478bd9Sstevel@tonic-gate return (PCPL_MALLOC_FAIL);
13247c478bd9Sstevel@tonic-gate }
13257c478bd9Sstevel@tonic-gate
13267c478bd9Sstevel@tonic-gate peek_ctrl.buf = (caddr_t)buf;
13277c478bd9Sstevel@tonic-gate peek_ctrl.buflen = mtu_size;
13287c478bd9Sstevel@tonic-gate peek_ctrl.flags = 0;
13297c478bd9Sstevel@tonic-gate
13307c478bd9Sstevel@tonic-gate /*
13317c478bd9Sstevel@tonic-gate * set sig jmp location
13327c478bd9Sstevel@tonic-gate */
13337c478bd9Sstevel@tonic-gate if (sigsetjmp(jmpbuf, 1)) {
13347c478bd9Sstevel@tonic-gate umem_free(buf, mtu_size);
13357c478bd9Sstevel@tonic-gate return (PCPL_GLVC_TIMEOUT);
13367c478bd9Sstevel@tonic-gate }
13377c478bd9Sstevel@tonic-gate
13387c478bd9Sstevel@tonic-gate done = 0;
13397c478bd9Sstevel@tonic-gate while (!done) {
13407c478bd9Sstevel@tonic-gate
13417c478bd9Sstevel@tonic-gate (void) alarm(PCP_CLEANUP_TIMEOUT);
13425db09ef5Smvgr if ((ret = ioctl(channel_fd, GLVC_XPORT_IOCTL_DATA_PEEK,
13437c478bd9Sstevel@tonic-gate &peek_ctrl)) < 0) {
13447c478bd9Sstevel@tonic-gate (void) alarm(0);
13457c478bd9Sstevel@tonic-gate done = 1;
13467c478bd9Sstevel@tonic-gate continue;
13477c478bd9Sstevel@tonic-gate }
13487c478bd9Sstevel@tonic-gate (void) alarm(0);
13497c478bd9Sstevel@tonic-gate
13507c478bd9Sstevel@tonic-gate n = peek_ctrl.buflen;
13517c478bd9Sstevel@tonic-gate
13527c478bd9Sstevel@tonic-gate if (n <= 0 && retry > 2) {
13537c478bd9Sstevel@tonic-gate done = 1;
13547c478bd9Sstevel@tonic-gate continue;
13557c478bd9Sstevel@tonic-gate } else if (n <= 0) {
13567c478bd9Sstevel@tonic-gate ++retry;
13577c478bd9Sstevel@tonic-gate continue;
13587c478bd9Sstevel@tonic-gate }
13597c478bd9Sstevel@tonic-gate
13607c478bd9Sstevel@tonic-gate /* remove data from channel */
13617c478bd9Sstevel@tonic-gate (void) alarm(PCP_CLEANUP_TIMEOUT);
13625db09ef5Smvgr if ((ret = read(channel_fd, buf, n)) < 0) {
13637c478bd9Sstevel@tonic-gate (void) alarm(0);
13647c478bd9Sstevel@tonic-gate done = 1;
13657c478bd9Sstevel@tonic-gate continue;
13667c478bd9Sstevel@tonic-gate }
13677c478bd9Sstevel@tonic-gate (void) alarm(0);
13687c478bd9Sstevel@tonic-gate }
13697c478bd9Sstevel@tonic-gate
13707c478bd9Sstevel@tonic-gate umem_free(buf, mtu_size);
13717c478bd9Sstevel@tonic-gate return (ret);
13727c478bd9Sstevel@tonic-gate }
13731ae08745Sheppo
13741ae08745Sheppo static int
vldc_write(int fd,uint8_t * bufp,int size)13751ae08745Sheppo vldc_write(int fd, uint8_t *bufp, int size)
13761ae08745Sheppo {
13771ae08745Sheppo int res;
13781ae08745Sheppo int left = size;
13791ae08745Sheppo pollfd_t pollfd;
13801ae08745Sheppo
13811ae08745Sheppo pollfd.events = POLLOUT;
13821ae08745Sheppo pollfd.revents = 0;
13831ae08745Sheppo pollfd.fd = fd;
13841ae08745Sheppo
13851ae08745Sheppo /*
13861ae08745Sheppo * Poll for the vldc channel to be ready
13871ae08745Sheppo */
13881ae08745Sheppo if (poll(&pollfd, 1, glvc_timeout * MILLISEC) <= 0) {
13891ae08745Sheppo return (-1);
13901ae08745Sheppo }
13911ae08745Sheppo
13921ae08745Sheppo do {
13931ae08745Sheppo if ((res = write(fd, bufp, left)) <= 0) {
13941ae08745Sheppo if (errno != EWOULDBLOCK) {
13951ae08745Sheppo return (res);
13961ae08745Sheppo }
13971ae08745Sheppo } else {
13981ae08745Sheppo bufp += res;
13991ae08745Sheppo left -= res;
14001ae08745Sheppo }
14011ae08745Sheppo } while (left > 0);
14021ae08745Sheppo
14031ae08745Sheppo /*
14041ae08745Sheppo * Return number of bytes actually written
14051ae08745Sheppo */
14061ae08745Sheppo return (size - left);
14071ae08745Sheppo }
14081ae08745Sheppo
14091ae08745Sheppo /*
14101ae08745Sheppo * Keep reading until we get the specified number of bytes
14111ae08745Sheppo */
14121ae08745Sheppo static int
vldc_read(int fd,uint8_t * bufp,int size)14131ae08745Sheppo vldc_read(int fd, uint8_t *bufp, int size)
14141ae08745Sheppo {
14151ae08745Sheppo int res;
14161ae08745Sheppo int left = size;
14171ae08745Sheppo
14181ae08745Sheppo struct pollfd fds[1];
14191ae08745Sheppo
14201ae08745Sheppo fds[0].events = POLLIN | POLLPRI;
14211ae08745Sheppo fds[0].revents = 0;
14221ae08745Sheppo fds[0].fd = fd;
14231ae08745Sheppo
14241ae08745Sheppo if (poll(fds, 1, glvc_timeout * MILLISEC) <= 0) {
14251ae08745Sheppo return (-1);
14261ae08745Sheppo }
14271ae08745Sheppo
14281ae08745Sheppo while (left > 0) {
14291ae08745Sheppo res = read(fd, bufp, left);
14301ae08745Sheppo /* return on error or short read */
14311ae08745Sheppo if ((res == 0) || ((res < 0) &&
14321ae08745Sheppo (errno == EAGAIN))) {
14331ae08745Sheppo /* poll until the read is unblocked */
14341ae08745Sheppo if ((poll(fds, 1, glvc_timeout * MILLISEC)) < 0)
14351ae08745Sheppo return (-1);
14361ae08745Sheppo
14371ae08745Sheppo continue;
14381ae08745Sheppo } else
14391ae08745Sheppo if (res < 0) {
14401ae08745Sheppo /* unrecoverable error */
14411ae08745Sheppo
14421ae08745Sheppo return (-1);
14431ae08745Sheppo } else {
14441ae08745Sheppo bufp += res;
14451ae08745Sheppo left -= res;
14461ae08745Sheppo }
14471ae08745Sheppo }
14481ae08745Sheppo
14491ae08745Sheppo return (size - left);
14501ae08745Sheppo }
1451