xref: /titanic_51/usr/src/lib/libpcp/common/libpcp.c (revision 1ae0874509b6811fdde1dfd46f0d93fd09867a3f)
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
5*1ae08745Sheppo  * Common Development and Distribution License (the "License").
6*1ae08745Sheppo  * 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 /*
22*1ae08745Sheppo  * Copyright 2006 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <unistd.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <assert.h>
397c478bd9Sstevel@tonic-gate #include <fcntl.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <signal.h>
427c478bd9Sstevel@tonic-gate #include <setjmp.h>
437c478bd9Sstevel@tonic-gate #include <inttypes.h>
447c478bd9Sstevel@tonic-gate #include <umem.h>
457c478bd9Sstevel@tonic-gate #include <strings.h>
4656b04ea6Smvgr #include <time.h>
477c478bd9Sstevel@tonic-gate #include <sys/types.h>
487c478bd9Sstevel@tonic-gate #include <sys/stat.h>
497c478bd9Sstevel@tonic-gate #include <sys/glvc.h>
50*1ae08745Sheppo #include <sys/vldc.h>
51*1ae08745Sheppo #include <sys/ldc.h>
527c478bd9Sstevel@tonic-gate #include <netinet/in.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include "libpcp.h"
557c478bd9Sstevel@tonic-gate #include "pcp_common.h"
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 
85*1ae08745Sheppo static int vldc_read(int fd, uint8_t *bufp, int size);
86*1ae08745Sheppo static int vldc_write(int fd, uint8_t *bufp, int size);
87*1ae08745Sheppo static int pcp_update_read_area(int byte_cnt);
88*1ae08745Sheppo static int pcp_vldc_frame_error_handle(void);
89*1ae08745Sheppo 
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 
165*1ae08745Sheppo /*
166*1ae08745Sheppo  * Variables to support vldc based streaming transport
167*1ae08745Sheppo  */
168*1ae08745Sheppo typedef enum {
169*1ae08745Sheppo 	GLVC_NON_STREAM,
170*1ae08745Sheppo 	VLDC_STREAMING
171*1ae08745Sheppo } xport_t;
172*1ae08745Sheppo 
173*1ae08745Sheppo static int xport_type = GLVC_NON_STREAM;
174*1ae08745Sheppo #define	CHANNEL_DEV	"channel-devices"
175*1ae08745Sheppo 
176*1ae08745Sheppo #define	VLDC_MTU_SIZE	(2048)
177*1ae08745Sheppo 
1787c478bd9Sstevel@tonic-gate static void
1797c478bd9Sstevel@tonic-gate glvc_timeout_handler(void)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	if (jumpok == 0)
1827c478bd9Sstevel@tonic-gate 		return;
1837c478bd9Sstevel@tonic-gate 	siglongjmp(jmpbuf, 1);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate /*
1877c478bd9Sstevel@tonic-gate  * Initialize the virtual channel. It basically opens the virtual channel
1887c478bd9Sstevel@tonic-gate  * provided by the host application.
1897c478bd9Sstevel@tonic-gate  *
1907c478bd9Sstevel@tonic-gate  */
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate int
1935db09ef5Smvgr pcp_init(char *channel_name)
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate 	sigset_t oldset;
1965db09ef5Smvgr 	int channel_fd;
1977c478bd9Sstevel@tonic-gate 
1985db09ef5Smvgr 	if (channel_name == NULL)
1997c478bd9Sstevel@tonic-gate 		return (PCPL_INVALID_ARGS);
200*1ae08745Sheppo 
2017c478bd9Sstevel@tonic-gate 	/*
2025db09ef5Smvgr 	 * Open virtual channel name.
2037c478bd9Sstevel@tonic-gate 	 */
2045db09ef5Smvgr 	if ((channel_fd = open(channel_name, O_RDWR|O_EXCL)) < 0) {
2057c478bd9Sstevel@tonic-gate 		return (PCPL_GLVC_ERROR);
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/*
209*1ae08745Sheppo 	 * Check if the channel-name points to a vldc node
210*1ae08745Sheppo 	 * or a glvc node
211*1ae08745Sheppo 	 */
212*1ae08745Sheppo 	if (strstr(channel_name, CHANNEL_DEV) != NULL) {
213*1ae08745Sheppo 		vldc_opt_op_t op;
214*1ae08745Sheppo 
215*1ae08745Sheppo 		xport_type  = VLDC_STREAMING;
216*1ae08745Sheppo 		mtu_size = VLDC_MTU_SIZE;
217*1ae08745Sheppo 
218*1ae08745Sheppo 		op.op_sel = VLDC_OP_SET;
219*1ae08745Sheppo 		op.opt_sel = VLDC_OPT_MODE;
220*1ae08745Sheppo 		op.opt_val = LDC_MODE_STREAM;
221*1ae08745Sheppo 		if (ioctl(channel_fd, VLDC_IOCTL_OPT_OP, &op) != 0) {
222*1ae08745Sheppo 			(void) close(channel_fd);
223*1ae08745Sheppo 			return (PCPL_GLVC_ERROR);
224*1ae08745Sheppo 		}
225*1ae08745Sheppo 	} else {
226*1ae08745Sheppo 		xport_type  = GLVC_NON_STREAM;
227*1ae08745Sheppo 		/*
2287c478bd9Sstevel@tonic-gate 		 * Get the Channel MTU size
2297c478bd9Sstevel@tonic-gate 		 */
2307c478bd9Sstevel@tonic-gate 
231*1ae08745Sheppo 		if (pcp_get_prop(channel_fd, GLVC_XPORT_OPT_MTU_SZ,
232*1ae08745Sheppo 				&mtu_size) != 0) {
2335db09ef5Smvgr 			(void) close(channel_fd);
2347c478bd9Sstevel@tonic-gate 			return (PCPL_GLVC_ERROR);
2357c478bd9Sstevel@tonic-gate 		}
236*1ae08745Sheppo 	}
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	/*
2397c478bd9Sstevel@tonic-gate 	 * Get current signal mask. If SIGALRM is blocked
2407c478bd9Sstevel@tonic-gate 	 * unblock it.
2417c478bd9Sstevel@tonic-gate 	 */
2427c478bd9Sstevel@tonic-gate 	(void) sigprocmask(0, NULL, &oldset);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&blkset);
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	if (sigismember(&oldset, SIGALRM)) {
2477c478bd9Sstevel@tonic-gate 		(void) sigaddset(&blkset, SIGALRM);
2487c478bd9Sstevel@tonic-gate 		(void) sigprocmask(SIG_UNBLOCK, &blkset, NULL);
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate 	/*
2517c478bd9Sstevel@tonic-gate 	 * signal handler initialization to handle glvc call timeouts.
2527c478bd9Sstevel@tonic-gate 	 */
2537c478bd9Sstevel@tonic-gate 	glvc_act.sa_handler = glvc_timeout_handler;
2547c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&glvc_act.sa_mask);
2557c478bd9Sstevel@tonic-gate 	glvc_act.sa_flags = SA_NODEFER;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	if (sigaction(SIGALRM, &glvc_act, &old_act) < 0) {
2585db09ef5Smvgr 		(void) close(channel_fd);
2597c478bd9Sstevel@tonic-gate 		return (PCPL_ERROR);
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 
2625db09ef5Smvgr 	return (channel_fd);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate /*
2667c478bd9Sstevel@tonic-gate  * Function: Close platform channel.
2675db09ef5Smvgr  * Arguments:
2685db09ef5Smvgr  *	int channel_fd - channel file descriptor.
2697c478bd9Sstevel@tonic-gate  * Returns:
2707c478bd9Sstevel@tonic-gate  *	always returns PCPL_OK for now.
2717c478bd9Sstevel@tonic-gate  */
2727c478bd9Sstevel@tonic-gate int
2735db09ef5Smvgr pcp_close(int channel_fd)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 
2765db09ef5Smvgr 	if (channel_fd >= 0) {
277*1ae08745Sheppo 		if (xport_type  == GLVC_NON_STREAM)
2785db09ef5Smvgr 			(void) pcp_cleanup(channel_fd);
2795db09ef5Smvgr 		(void) close(channel_fd);
2805db09ef5Smvgr 	} else {
2815db09ef5Smvgr 		return (-1);
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	/*
2857c478bd9Sstevel@tonic-gate 	 * free global buffers
2867c478bd9Sstevel@tonic-gate 	 */
2877c478bd9Sstevel@tonic-gate 	if (read_area != NULL) {
2887c478bd9Sstevel@tonic-gate 		umem_free(read_area, READ_AREA_SIZE);
2897c478bd9Sstevel@tonic-gate 		read_area = NULL;
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 	if (peek_area != NULL) {
2927c478bd9Sstevel@tonic-gate 		umem_free(peek_area, PEEK_AREA_SIZE);
2937c478bd9Sstevel@tonic-gate 		peek_area = NULL;
2947c478bd9Sstevel@tonic-gate 	}
2957c478bd9Sstevel@tonic-gate 	if (peek_read_area != NULL) {
2967c478bd9Sstevel@tonic-gate 		umem_free(peek_read_area, PEEK_READ_AREA_SIZE);
2977c478bd9Sstevel@tonic-gate 		peek_read_area = NULL;
2987c478bd9Sstevel@tonic-gate 	}
2997c478bd9Sstevel@tonic-gate 	if (req_msg_hdr != NULL) {
3007c478bd9Sstevel@tonic-gate 		umem_free(req_msg_hdr, req_msg_hdr_sz);
3017c478bd9Sstevel@tonic-gate 		req_msg_hdr = NULL;
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate 	if (resp_msg_hdr != NULL) {
3047c478bd9Sstevel@tonic-gate 		umem_free(resp_msg_hdr, resp_msg_hdr_sz);
3057c478bd9Sstevel@tonic-gate 		resp_msg_hdr = NULL;
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	/*
3097c478bd9Sstevel@tonic-gate 	 * Restore SIGALRM signal mask incase if we unblocked
3107c478bd9Sstevel@tonic-gate 	 * it during pcp_init.
3117c478bd9Sstevel@tonic-gate 	 */
3127c478bd9Sstevel@tonic-gate 	if (sigismember(&blkset, SIGALRM)) {
3137c478bd9Sstevel@tonic-gate 		(void) sigprocmask(SIG_BLOCK, &blkset, NULL);
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	/* Restore SIGALRM signal handler */
3177c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGALRM, &old_act, NULL);
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	return (PCPL_OK);
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate /*
3237c478bd9Sstevel@tonic-gate  * Function: Send and Receive messages on platform channel.
3247c478bd9Sstevel@tonic-gate  * Arguments:
3255db09ef5Smvgr  *	int channel_fd      - channel file descriptor.
3267c478bd9Sstevel@tonic-gate  *	pcp_msg_t *req_msg  - Request Message to send to other end of channel.
3277c478bd9Sstevel@tonic-gate  *	pcp_msg_t *resp_msg - Response Message to be received.
3287c478bd9Sstevel@tonic-gate  *	uint32_t timeout    - timeout field when waiting for data from channel.
3297c478bd9Sstevel@tonic-gate  * Returns:
3307c478bd9Sstevel@tonic-gate  *	0  - success (PCPL_OK).
3317c478bd9Sstevel@tonic-gate  *	(-ve) - failure:
3327c478bd9Sstevel@tonic-gate  *			PCPL_INVALID_ARGS - invalid args.
3337c478bd9Sstevel@tonic-gate  *			PCPL_GLVC_TIMEOUT - glvc call timeout.
3347c478bd9Sstevel@tonic-gate  *			PCPL_XPORT_ERROR - transport error in request message
3357c478bd9Sstevel@tonic-gate  *						noticed by receiver.
3367c478bd9Sstevel@tonic-gate  *			PCPL_MALLOC_FAIL - malloc failure.
3377c478bd9Sstevel@tonic-gate  *			PCPL_CKSUM_ERROR - checksum error.
3387c478bd9Sstevel@tonic-gate  */
3397c478bd9Sstevel@tonic-gate int
3405db09ef5Smvgr pcp_send_recv(int channel_fd, pcp_msg_t *req_msg, pcp_msg_t *resp_msg,
3415db09ef5Smvgr 		uint32_t timeout)
3427c478bd9Sstevel@tonic-gate {
3437c478bd9Sstevel@tonic-gate 	void *datap;
3447c478bd9Sstevel@tonic-gate 	void *resp_msg_data = NULL;
3457c478bd9Sstevel@tonic-gate 	uint32_t status;
3467c478bd9Sstevel@tonic-gate 	uint16_t cksum = 0;
3477c478bd9Sstevel@tonic-gate 	int ret;
3487c478bd9Sstevel@tonic-gate 	int resp_hdr_ok;
3497c478bd9Sstevel@tonic-gate #ifdef PCP_CKSUM_ENABLE
3507c478bd9Sstevel@tonic-gate 	uint16_t bkup_resp_hdr_cksum;
3517c478bd9Sstevel@tonic-gate #endif
3525db09ef5Smvgr 	if (channel_fd < 0) {
3537c478bd9Sstevel@tonic-gate 		return (PCPL_ERROR);
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 
3565db09ef5Smvgr 	/* copy channel_fd to local fd (chnl_fd) for other functions use */
3575db09ef5Smvgr 	chnl_fd = channel_fd;
3585db09ef5Smvgr 
3597c478bd9Sstevel@tonic-gate 	if (req_msg == NULL) {
3607c478bd9Sstevel@tonic-gate 		return (PCPL_INVALID_ARGS);
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	if (timeout > 0)
3647c478bd9Sstevel@tonic-gate 		glvc_timeout = timeout;
3657c478bd9Sstevel@tonic-gate 	else
3667c478bd9Sstevel@tonic-gate 		glvc_timeout = 0;
3677c478bd9Sstevel@tonic-gate 
3681b255844Smvgr 	if ((req_msg->msg_len != 0) && ((datap = req_msg->msg_data) == NULL))
3697c478bd9Sstevel@tonic-gate 		return (PCPL_INVALID_ARGS);
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	if (req_msg_hdr == NULL) {
3727c478bd9Sstevel@tonic-gate 		req_msg_hdr_sz = sizeof (pcp_req_msg_hdr_t);
3737c478bd9Sstevel@tonic-gate 		req_msg_hdr = (pcp_req_msg_hdr_t *)umem_zalloc(req_msg_hdr_sz,
3747c478bd9Sstevel@tonic-gate 								UMEM_DEFAULT);
3757c478bd9Sstevel@tonic-gate 		if (req_msg_hdr == NULL)
3767c478bd9Sstevel@tonic-gate 			return (PCPL_MALLOC_FAIL);
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate 
3795db09ef5Smvgr 	if (req_msg->msg_len != 0) {
3807c478bd9Sstevel@tonic-gate 		/* calculate request msg_cksum */
3817c478bd9Sstevel@tonic-gate 		cksum = checksum((uint16_t *)datap, req_msg->msg_len);
3825db09ef5Smvgr 	}
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	/*
3857c478bd9Sstevel@tonic-gate 	 * Fill in the message header for the request packet
3867c478bd9Sstevel@tonic-gate 	 */
3877c478bd9Sstevel@tonic-gate 	req_msg_hdr->magic_num = PCP_MAGIC_NUM;
3887c478bd9Sstevel@tonic-gate 	req_msg_hdr->proto_ver = PCP_PROT_VER_1;
3897c478bd9Sstevel@tonic-gate 	req_msg_hdr->msg_type = req_msg->msg_type;
3907c478bd9Sstevel@tonic-gate 	req_msg_hdr->sub_type = req_msg->sub_type;
3917c478bd9Sstevel@tonic-gate 	req_msg_hdr->rsvd_pad = 0;
3927c478bd9Sstevel@tonic-gate 	req_msg_hdr->xid = pcp_get_xid();
3937c478bd9Sstevel@tonic-gate 	req_msg_hdr->msg_len  = req_msg->msg_len;
3947c478bd9Sstevel@tonic-gate 	req_msg_hdr->timeout = timeout;
3957c478bd9Sstevel@tonic-gate 	req_msg_hdr->msg_cksum = cksum;
3967c478bd9Sstevel@tonic-gate 	req_msg_hdr->hdr_cksum = 0;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	/* fill request header checksum */
3997c478bd9Sstevel@tonic-gate 	req_msg_hdr->hdr_cksum = checksum((uint16_t *)req_msg_hdr,
4007c478bd9Sstevel@tonic-gate 					req_msg_hdr_sz);
4017c478bd9Sstevel@tonic-gate 	/*
4027c478bd9Sstevel@tonic-gate 	 * set sig jmp location
4037c478bd9Sstevel@tonic-gate 	 */
4047c478bd9Sstevel@tonic-gate 	if (sigsetjmp(jmpbuf, 1)) {
4057c478bd9Sstevel@tonic-gate 		return (PCPL_GLVC_TIMEOUT);
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 	jumpok = 1; /* monitor sigalrm from now on */
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	/*
4107c478bd9Sstevel@tonic-gate 	 * send request message header
4117c478bd9Sstevel@tonic-gate 	 */
4127c478bd9Sstevel@tonic-gate 	if ((ret = pcp_send_req_msg_hdr(req_msg_hdr))) {
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 		return (ret);
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	/*
4187c478bd9Sstevel@tonic-gate 	 * send request message
4197c478bd9Sstevel@tonic-gate 	 */
4205db09ef5Smvgr 	if (req_msg->msg_len != 0) {
4217c478bd9Sstevel@tonic-gate 		if ((ret = pcp_io_op(datap, req_msg->msg_len,
4227c478bd9Sstevel@tonic-gate 					PCPL_IO_OP_WRITE))) {
4237c478bd9Sstevel@tonic-gate 			return (ret);
4247c478bd9Sstevel@tonic-gate 		}
4255db09ef5Smvgr 	}
4265db09ef5Smvgr 
4277c478bd9Sstevel@tonic-gate 	if (timeout == (uint32_t)PCP_TO_NO_RESPONSE)
4287c478bd9Sstevel@tonic-gate 		return (PCPL_OK);
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	if (resp_msg_hdr == NULL) {
4317c478bd9Sstevel@tonic-gate 		resp_msg_hdr_sz = sizeof (pcp_resp_msg_hdr_t);
4327c478bd9Sstevel@tonic-gate 		resp_msg_hdr = (pcp_resp_msg_hdr_t *)umem_alloc(resp_msg_hdr_sz,
4337c478bd9Sstevel@tonic-gate 								UMEM_DEFAULT);
4347c478bd9Sstevel@tonic-gate 		if (resp_msg_hdr == NULL)
4357c478bd9Sstevel@tonic-gate 			return (PCPL_MALLOC_FAIL);
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	resp_hdr_ok = 0;
4397c478bd9Sstevel@tonic-gate 	while (!resp_hdr_ok) {
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 		/*
4427c478bd9Sstevel@tonic-gate 		 * Receive response message header
4437c478bd9Sstevel@tonic-gate 		 * Note: frame error handling is done in
4447c478bd9Sstevel@tonic-gate 		 * 'pcp_recv_resp_msg_hdr()'.
4457c478bd9Sstevel@tonic-gate 		 */
4467c478bd9Sstevel@tonic-gate 		if ((ret = pcp_recv_resp_msg_hdr(resp_msg_hdr))) {
4477c478bd9Sstevel@tonic-gate 			return (ret);
4487c478bd9Sstevel@tonic-gate 		}
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 		/*
4517c478bd9Sstevel@tonic-gate 		 * Check header checksum if it matches with the received hdr
4527c478bd9Sstevel@tonic-gate 		 * checksum.
4537c478bd9Sstevel@tonic-gate 		 */
4547c478bd9Sstevel@tonic-gate #ifdef PCP_CKSUM_ENABLE
4557c478bd9Sstevel@tonic-gate 		bkup_resp_hdr_cksum = resp_msg_hdr->hdr_cksum;
4567c478bd9Sstevel@tonic-gate 		resp_msg_hdr->hdr_cksum = 0;
4577c478bd9Sstevel@tonic-gate 		cksum = checksum((uint16_t *)resp_msg_hdr, resp_msg_hdr_sz);
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 		if (cksum != bkup_resp_hdr_cksum) {
4607c478bd9Sstevel@tonic-gate 			return (PCPL_CKSUM_ERROR);
4617c478bd9Sstevel@tonic-gate 		}
4627c478bd9Sstevel@tonic-gate #endif
4637c478bd9Sstevel@tonic-gate 		/*
4647c478bd9Sstevel@tonic-gate 		 * Check for matching request and response messages
4657c478bd9Sstevel@tonic-gate 		 */
4667c478bd9Sstevel@tonic-gate 		if (resp_msg_hdr->xid != req_msg_hdr->xid) {
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 			continue; /* continue reading response header */
4697c478bd9Sstevel@tonic-gate 		}
4707c478bd9Sstevel@tonic-gate 		resp_hdr_ok = 1;
4717c478bd9Sstevel@tonic-gate 	}
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	/*
4747c478bd9Sstevel@tonic-gate 	 * check status field for any channel protocol errrors
4757c478bd9Sstevel@tonic-gate 	 * This field signifies something happend during request
4767c478bd9Sstevel@tonic-gate 	 * message trasmission. This field is set by the receiver.
4777c478bd9Sstevel@tonic-gate 	 */
4787c478bd9Sstevel@tonic-gate 	status = resp_msg_hdr->status;
4797c478bd9Sstevel@tonic-gate 	if (status != PCP_OK) {
4807c478bd9Sstevel@tonic-gate 		return (PCPL_XPORT_ERROR);
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 
4835db09ef5Smvgr 	if (resp_msg_hdr->msg_len != 0) {
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 		/* libpcp users should free this memory */
4865db09ef5Smvgr 		if ((resp_msg_data = (uint8_t *)malloc(resp_msg_hdr->msg_len))
4875db09ef5Smvgr 			== NULL)
4887c478bd9Sstevel@tonic-gate 			return (PCPL_MALLOC_FAIL);
4897c478bd9Sstevel@tonic-gate 		bzero(resp_msg_data, resp_msg_hdr->msg_len);
4907c478bd9Sstevel@tonic-gate 		/*
4917c478bd9Sstevel@tonic-gate 		 * Receive response message.
4927c478bd9Sstevel@tonic-gate 		 */
4937c478bd9Sstevel@tonic-gate 		if ((ret = pcp_io_op(resp_msg_data, resp_msg_hdr->msg_len,
4947c478bd9Sstevel@tonic-gate 						PCPL_IO_OP_READ))) {
4957c478bd9Sstevel@tonic-gate 			free(resp_msg_data);
4967c478bd9Sstevel@tonic-gate 			return (ret);
4977c478bd9Sstevel@tonic-gate 		}
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate #ifdef PCP_CKSUM_ENABLE
5007c478bd9Sstevel@tonic-gate 		/* verify response message data checksum */
5017c478bd9Sstevel@tonic-gate 		cksum = checksum((uint16_t *)resp_msg_data,
5027c478bd9Sstevel@tonic-gate 					resp_msg_hdr->msg_len);
5037c478bd9Sstevel@tonic-gate 		if (cksum != resp_msg_hdr->msg_cksum) {
5047c478bd9Sstevel@tonic-gate 			free(resp_msg_data);
5057c478bd9Sstevel@tonic-gate 			return (PCPL_CKSUM_ERROR);
5067c478bd9Sstevel@tonic-gate 		}
5077c478bd9Sstevel@tonic-gate #endif
5085db09ef5Smvgr 	}
5097c478bd9Sstevel@tonic-gate 	/* Everything is okay put the received data into user */
5107c478bd9Sstevel@tonic-gate 	/* application's resp_msg struct */
5117c478bd9Sstevel@tonic-gate 	resp_msg->msg_len = resp_msg_hdr->msg_len;
5127c478bd9Sstevel@tonic-gate 	resp_msg->msg_type = resp_msg_hdr->msg_type;
5137c478bd9Sstevel@tonic-gate 	resp_msg->sub_type = resp_msg_hdr->sub_type;
5147c478bd9Sstevel@tonic-gate 	resp_msg->msg_data = (uint8_t *)resp_msg_data;
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	return (PCPL_OK);
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate /*
5217c478bd9Sstevel@tonic-gate  * Function: Get channel property values.
5227c478bd9Sstevel@tonic-gate  * Arguments:
5235db09ef5Smvgr  *	int channel_fd - channel file descriptor.
5247c478bd9Sstevel@tonic-gate  *	int prop - property id.
5257c478bd9Sstevel@tonic-gate  *	unsigned int *val - property value tobe copied.
5267c478bd9Sstevel@tonic-gate  * Returns:
5277c478bd9Sstevel@tonic-gate  *	0 - success
5287c478bd9Sstevel@tonic-gate  *	(-ve) - failure:
5297c478bd9Sstevel@tonic-gate  *		PCPL_ERR_GLVC - glvc ioctl failure.
5307c478bd9Sstevel@tonic-gate  */
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate static int
5335db09ef5Smvgr pcp_get_prop(int channel_fd, int prop, unsigned int *val)
5347c478bd9Sstevel@tonic-gate {
5357c478bd9Sstevel@tonic-gate 	glvc_xport_opt_op_t	channel_op;
5367c478bd9Sstevel@tonic-gate 	int			ret;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	channel_op.op_sel = GLVC_XPORT_OPT_GET;
5397c478bd9Sstevel@tonic-gate 	channel_op.opt_sel = prop;
5407c478bd9Sstevel@tonic-gate 	channel_op.opt_val = 0;
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	(void) alarm(glvc_timeout);
5437c478bd9Sstevel@tonic-gate 
5445db09ef5Smvgr 	if ((ret = ioctl(channel_fd, GLVC_XPORT_IOCTL_OPT_OP,
5455db09ef5Smvgr 		&channel_op)) < 0) {
5465db09ef5Smvgr 
5477c478bd9Sstevel@tonic-gate 		(void) alarm(0);
5487c478bd9Sstevel@tonic-gate 		return (ret);
5497c478bd9Sstevel@tonic-gate 	}
5507c478bd9Sstevel@tonic-gate 	(void) alarm(0);
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	*val = channel_op.opt_val;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	return (0);
5557c478bd9Sstevel@tonic-gate }
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate /*
5587c478bd9Sstevel@tonic-gate  * Function: wrapper for handling glvc calls (read/write/peek).
5597c478bd9Sstevel@tonic-gate  */
5607c478bd9Sstevel@tonic-gate static int
5617c478bd9Sstevel@tonic-gate pcp_io_op(void *buf, int byte_cnt, int io_op)
5627c478bd9Sstevel@tonic-gate {
5637c478bd9Sstevel@tonic-gate 	int	rv;
5647c478bd9Sstevel@tonic-gate 	int	n;
5657c478bd9Sstevel@tonic-gate 	uint8_t	*datap;
5667c478bd9Sstevel@tonic-gate 	int	(*func_ptr)(uint8_t *, int);
5677c478bd9Sstevel@tonic-gate 	int	io_sz;
5687c478bd9Sstevel@tonic-gate 	int	try_cnt;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	if ((buf == NULL) || (byte_cnt < 0)) {
5727c478bd9Sstevel@tonic-gate 		return (PCPL_INVALID_ARGS);
5737c478bd9Sstevel@tonic-gate 	}
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	switch (io_op) {
5767c478bd9Sstevel@tonic-gate 		case PCPL_IO_OP_READ:
5777c478bd9Sstevel@tonic-gate 			func_ptr = pcp_read;
5787c478bd9Sstevel@tonic-gate 			break;
5797c478bd9Sstevel@tonic-gate 		case PCPL_IO_OP_WRITE:
5807c478bd9Sstevel@tonic-gate 			func_ptr = pcp_write;
5817c478bd9Sstevel@tonic-gate 			break;
5827c478bd9Sstevel@tonic-gate 		case PCPL_IO_OP_PEEK:
5837c478bd9Sstevel@tonic-gate 			func_ptr = pcp_peek;
5847c478bd9Sstevel@tonic-gate 			break;
5857c478bd9Sstevel@tonic-gate 		default:
5867c478bd9Sstevel@tonic-gate 			return (PCPL_INVALID_ARGS);
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	/*
5907c478bd9Sstevel@tonic-gate 	 * loop until all I/O done, try limit exceded, or real failure
5917c478bd9Sstevel@tonic-gate 	 */
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	rv = 0;
5947c478bd9Sstevel@tonic-gate 	datap = buf;
5957c478bd9Sstevel@tonic-gate 	while (rv < byte_cnt) {
5967c478bd9Sstevel@tonic-gate 		io_sz = MIN((byte_cnt - rv), mtu_size);
5977c478bd9Sstevel@tonic-gate 		try_cnt = 0;
5987c478bd9Sstevel@tonic-gate 		while ((n = (*func_ptr)(datap, io_sz)) < 0) {
5997c478bd9Sstevel@tonic-gate 			try_cnt++;
6007c478bd9Sstevel@tonic-gate 			if (try_cnt > PCPL_MAX_TRY_CNT) {
6017c478bd9Sstevel@tonic-gate 				rv = n;
6027c478bd9Sstevel@tonic-gate 				goto done;
6037c478bd9Sstevel@tonic-gate 			}
6047c478bd9Sstevel@tonic-gate 			(void) sleep(PCPL_GLVC_SLEEP);
6057c478bd9Sstevel@tonic-gate 		} /* while trying the io operation */
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 		if (n < 0) {
6087c478bd9Sstevel@tonic-gate 			rv = n;
6097c478bd9Sstevel@tonic-gate 			goto done;
6107c478bd9Sstevel@tonic-gate 		}
6117c478bd9Sstevel@tonic-gate 		rv += n;
6127c478bd9Sstevel@tonic-gate 		datap += n;
6137c478bd9Sstevel@tonic-gate 	} /* while still have more data */
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate done:
6167c478bd9Sstevel@tonic-gate 	if (rv == byte_cnt)
6177c478bd9Sstevel@tonic-gate 		return (0);
6187c478bd9Sstevel@tonic-gate 	else
6197c478bd9Sstevel@tonic-gate 		return (PCPL_GLVC_ERROR);
6207c478bd9Sstevel@tonic-gate }
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate /*
6237c478bd9Sstevel@tonic-gate  * For peeking 'bytes_cnt' bytes in channel (glvc) buffers.
6247c478bd9Sstevel@tonic-gate  * If data is available, the data is copied into 'buf'.
6257c478bd9Sstevel@tonic-gate  */
6267c478bd9Sstevel@tonic-gate static int
6277c478bd9Sstevel@tonic-gate pcp_peek(uint8_t *buf, int bytes_cnt)
6287c478bd9Sstevel@tonic-gate {
6297c478bd9Sstevel@tonic-gate 	int			ret;
6307c478bd9Sstevel@tonic-gate 	glvc_xport_msg_peek_t	peek_ctrl;
6317c478bd9Sstevel@tonic-gate 	int			n, m;
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	if (bytes_cnt < 0 || bytes_cnt > mtu_size) {
6347c478bd9Sstevel@tonic-gate 		return (PCPL_INVALID_ARGS);
6357c478bd9Sstevel@tonic-gate 	}
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	/*
6387c478bd9Sstevel@tonic-gate 	 * initialization of buffers used for peeking data in channel buffers.
6397c478bd9Sstevel@tonic-gate 	 */
6407c478bd9Sstevel@tonic-gate 	if (peek_area == NULL) {
6417c478bd9Sstevel@tonic-gate 		peek_area = (uint8_t *)umem_zalloc(PEEK_AREA_SIZE,
6427c478bd9Sstevel@tonic-gate 							UMEM_DEFAULT);
6437c478bd9Sstevel@tonic-gate 		if (peek_area == NULL) {
6447c478bd9Sstevel@tonic-gate 			return (PCPL_MALLOC_FAIL);
6457c478bd9Sstevel@tonic-gate 		}
6467c478bd9Sstevel@tonic-gate 	}
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	/*
6497c478bd9Sstevel@tonic-gate 	 * peek max MTU size bytes
6507c478bd9Sstevel@tonic-gate 	 */
6517c478bd9Sstevel@tonic-gate 	peek_ctrl.buf = (caddr_t)peek_area;
6527c478bd9Sstevel@tonic-gate 	peek_ctrl.buflen = mtu_size;
6537c478bd9Sstevel@tonic-gate 	peek_ctrl.flags = 0;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	(void) alarm(glvc_timeout);
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	if ((ret = ioctl(chnl_fd, GLVC_XPORT_IOCTL_DATA_PEEK, &peek_ctrl))
6587c478bd9Sstevel@tonic-gate 		< 0) {
6597c478bd9Sstevel@tonic-gate 		(void) alarm(0);
6607c478bd9Sstevel@tonic-gate 		return (ret);
6617c478bd9Sstevel@tonic-gate 	}
6627c478bd9Sstevel@tonic-gate 	(void) alarm(0);
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	n = peek_ctrl.buflen;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	if (n < 0)
6677c478bd9Sstevel@tonic-gate 		return (PCPL_GLVC_ERROR);
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	/*
6707c478bd9Sstevel@tonic-gate 	 * satisfy request as best as we can
6717c478bd9Sstevel@tonic-gate 	 */
6727c478bd9Sstevel@tonic-gate 	m = MIN(bytes_cnt, n);
6737c478bd9Sstevel@tonic-gate 	(void) memcpy(buf, peek_area, m);
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	return (m);
6767c478bd9Sstevel@tonic-gate }
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate /*
6797c478bd9Sstevel@tonic-gate  * Function: write 'byte_cnt' bytes from 'buf' to channel.
6807c478bd9Sstevel@tonic-gate  */
6817c478bd9Sstevel@tonic-gate static int
6827c478bd9Sstevel@tonic-gate pcp_write(uint8_t *buf, int byte_cnt)
6837c478bd9Sstevel@tonic-gate {
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	int	ret;
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	/* check for valid arguments */
6887c478bd9Sstevel@tonic-gate 	if (buf == NULL || byte_cnt < 0 || byte_cnt > mtu_size) {
6897c478bd9Sstevel@tonic-gate 		return (PCPL_INVALID_ARGS);
6907c478bd9Sstevel@tonic-gate 	}
6917c478bd9Sstevel@tonic-gate 
692*1ae08745Sheppo 	if (xport_type == GLVC_NON_STREAM) {
6937c478bd9Sstevel@tonic-gate 		(void) alarm(glvc_timeout);
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 		if ((ret = write(chnl_fd, buf, byte_cnt)) < 0) {
6967c478bd9Sstevel@tonic-gate 			(void) alarm(0);
6977c478bd9Sstevel@tonic-gate 			return (ret);
6987c478bd9Sstevel@tonic-gate 		}
6997c478bd9Sstevel@tonic-gate 		(void) alarm(0);
700*1ae08745Sheppo 	} else {
701*1ae08745Sheppo 		if ((ret = vldc_write(chnl_fd, buf, byte_cnt)) <= 0) {
702*1ae08745Sheppo 			return (ret);
703*1ae08745Sheppo 		}
704*1ae08745Sheppo 	}
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	return (ret);
7077c478bd9Sstevel@tonic-gate }
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate /*
7107c478bd9Sstevel@tonic-gate  * In current implementaion of glvc driver, streams reads are not supported.
7117c478bd9Sstevel@tonic-gate  * pcp_read mimics stream reads by first reading all the bytes present in the
7127c478bd9Sstevel@tonic-gate  * channel buffer into a local buffer and from then on read requests
7137c478bd9Sstevel@tonic-gate  * are serviced from local buffer. When read requests are not serviceble
7147c478bd9Sstevel@tonic-gate  * from local buffer, it repeates by first reading data from channel buffers.
7157c478bd9Sstevel@tonic-gate  *
7167c478bd9Sstevel@tonic-gate  * This call may need to be enhanced when glvc supports buffered (stream)
7177c478bd9Sstevel@tonic-gate  * reads - TBD
7187c478bd9Sstevel@tonic-gate  */
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate static int
7217c478bd9Sstevel@tonic-gate pcp_read(uint8_t *buf, int byte_cnt)
7227c478bd9Sstevel@tonic-gate {
7237c478bd9Sstevel@tonic-gate 	int			ret;
7247c478bd9Sstevel@tonic-gate 	int			n, m, i;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	if (byte_cnt < 0 || byte_cnt > mtu_size) {
7277c478bd9Sstevel@tonic-gate 		return (PCPL_INVALID_ARGS);
7287c478bd9Sstevel@tonic-gate 	}
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	/*
7317c478bd9Sstevel@tonic-gate 	 * initialization of local read buffer
7327c478bd9Sstevel@tonic-gate 	 * from which the stream read requests are serviced.
7337c478bd9Sstevel@tonic-gate 	 */
7347c478bd9Sstevel@tonic-gate 	if (read_area == NULL) {
7357c478bd9Sstevel@tonic-gate 		read_area = (uint8_t *)umem_zalloc(READ_AREA_SIZE,
7367c478bd9Sstevel@tonic-gate 							UMEM_DEFAULT);
7377c478bd9Sstevel@tonic-gate 		if (read_area == NULL) {
7387c478bd9Sstevel@tonic-gate 			return (PCPL_MALLOC_FAIL);
7397c478bd9Sstevel@tonic-gate 		}
7407c478bd9Sstevel@tonic-gate 		read_head = read_area;
7417c478bd9Sstevel@tonic-gate 		read_tail = read_area;
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	/*
7457c478bd9Sstevel@tonic-gate 	 * if we already read this data then copy from local buffer it self
7467c478bd9Sstevel@tonic-gate 	 * without calling new read.
7477c478bd9Sstevel@tonic-gate 	 */
7487c478bd9Sstevel@tonic-gate 	if (byte_cnt <= (read_tail - read_head)) {
7497c478bd9Sstevel@tonic-gate 		(void) memcpy(buf, read_head, byte_cnt);
7507c478bd9Sstevel@tonic-gate 		read_head += byte_cnt;
7517c478bd9Sstevel@tonic-gate 		return (byte_cnt);
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	/*
7557c478bd9Sstevel@tonic-gate 	 * if the request is not satisfied from the buffered data, then move the
7567c478bd9Sstevel@tonic-gate 	 * remaining data to front of the buffer and read new data.
7577c478bd9Sstevel@tonic-gate 	 */
7587c478bd9Sstevel@tonic-gate 	for (i = 0; i < (read_tail - read_head); ++i) {
7597c478bd9Sstevel@tonic-gate 		read_area[i] = read_head[i];
7607c478bd9Sstevel@tonic-gate 	}
7617c478bd9Sstevel@tonic-gate 	read_head = read_area;
7627c478bd9Sstevel@tonic-gate 	read_tail = read_head + i;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	/*
7657c478bd9Sstevel@tonic-gate 	 * do a peek to see how much data is available and read complete data.
7667c478bd9Sstevel@tonic-gate 	 */
7677c478bd9Sstevel@tonic-gate 
768*1ae08745Sheppo 	if (xport_type == GLVC_NON_STREAM) {
7697c478bd9Sstevel@tonic-gate 		if ((m = pcp_peek(read_tail, mtu_size)) < 0) {
7707c478bd9Sstevel@tonic-gate 			return (m);
7717c478bd9Sstevel@tonic-gate 		}
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 		(void) alarm(glvc_timeout);
7747c478bd9Sstevel@tonic-gate 		if ((ret = read(chnl_fd, read_tail, m)) < 0) {
7757c478bd9Sstevel@tonic-gate 			(void) alarm(0);
7767c478bd9Sstevel@tonic-gate 			return (ret);
7777c478bd9Sstevel@tonic-gate 		}
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 		(void) alarm(0);
780*1ae08745Sheppo 	} else {
781*1ae08745Sheppo 		/*
782*1ae08745Sheppo 		 * Read the extra number of bytes
783*1ae08745Sheppo 		 */
784*1ae08745Sheppo 		m = byte_cnt - (read_tail - read_head);
785*1ae08745Sheppo 		if ((ret = vldc_read(chnl_fd,
786*1ae08745Sheppo 				read_tail, m)) <= 0) {
787*1ae08745Sheppo 			return (ret);
788*1ae08745Sheppo 		}
789*1ae08745Sheppo 	}
7907c478bd9Sstevel@tonic-gate 	read_tail += ret;
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	/*
7937c478bd9Sstevel@tonic-gate 	 * copy the requested bytes.
7947c478bd9Sstevel@tonic-gate 	 */
7957c478bd9Sstevel@tonic-gate 	n = MIN(byte_cnt, (read_tail - read_head));
7967c478bd9Sstevel@tonic-gate 	(void) memcpy(buf, read_head, n);
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	read_head += n;
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	return (n);
8017c478bd9Sstevel@tonic-gate }
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate /*
804*1ae08745Sheppo  * Issue read from the driver until byet_cnt number
805*1ae08745Sheppo  * of bytes are present in read buffer. Do not
806*1ae08745Sheppo  * move the read head.
807*1ae08745Sheppo  */
808*1ae08745Sheppo static int
809*1ae08745Sheppo pcp_update_read_area(int byte_cnt)
810*1ae08745Sheppo {
811*1ae08745Sheppo 	int			ret;
812*1ae08745Sheppo 	int			n, i;
813*1ae08745Sheppo 
814*1ae08745Sheppo 	if (byte_cnt < 0 || byte_cnt > mtu_size) {
815*1ae08745Sheppo 		return (PCPL_INVALID_ARGS);
816*1ae08745Sheppo 	}
817*1ae08745Sheppo 
818*1ae08745Sheppo 	/*
819*1ae08745Sheppo 	 * initialization of local read buffer
820*1ae08745Sheppo 	 * from which the stream read requests are serviced.
821*1ae08745Sheppo 	 */
822*1ae08745Sheppo 	if (read_area == NULL) {
823*1ae08745Sheppo 		read_area = (uint8_t *)umem_zalloc(READ_AREA_SIZE,
824*1ae08745Sheppo 							UMEM_DEFAULT);
825*1ae08745Sheppo 		if (read_area == NULL) {
826*1ae08745Sheppo 			return (PCPL_MALLOC_FAIL);
827*1ae08745Sheppo 		}
828*1ae08745Sheppo 		read_head = read_area;
829*1ae08745Sheppo 		read_tail = read_area;
830*1ae08745Sheppo 	}
831*1ae08745Sheppo 
832*1ae08745Sheppo 	/*
833*1ae08745Sheppo 	 * if we already have sufficient data in the buffer,
834*1ae08745Sheppo 	 * just return
835*1ae08745Sheppo 	 */
836*1ae08745Sheppo 	if (byte_cnt <= (read_tail - read_head)) {
837*1ae08745Sheppo 		return (byte_cnt);
838*1ae08745Sheppo 	}
839*1ae08745Sheppo 
840*1ae08745Sheppo 	/*
841*1ae08745Sheppo 	 * if the request is not satisfied from the buffered data, then move the
842*1ae08745Sheppo 	 * remaining data to front of the buffer and read new data.
843*1ae08745Sheppo 	 */
844*1ae08745Sheppo 	for (i = 0; i < (read_tail - read_head); ++i) {
845*1ae08745Sheppo 		read_area[i] = read_head[i];
846*1ae08745Sheppo 	}
847*1ae08745Sheppo 	read_head = read_area;
848*1ae08745Sheppo 	read_tail = read_head + i;
849*1ae08745Sheppo 
850*1ae08745Sheppo 	n = byte_cnt - (read_tail - read_head);
851*1ae08745Sheppo 
852*1ae08745Sheppo 	if ((ret = vldc_read(chnl_fd,
853*1ae08745Sheppo 			read_tail, n)) <= 0) {
854*1ae08745Sheppo 		return (ret);
855*1ae08745Sheppo 	}
856*1ae08745Sheppo 	read_tail += ret;
857*1ae08745Sheppo 
858*1ae08745Sheppo 	/*
859*1ae08745Sheppo 	 * Return the number of bytes we could read
860*1ae08745Sheppo 	 */
861*1ae08745Sheppo 	n = MIN(byte_cnt, (read_tail - read_head));
862*1ae08745Sheppo 
863*1ae08745Sheppo 	return (n);
864*1ae08745Sheppo }
865*1ae08745Sheppo 
866*1ae08745Sheppo /*
8677c478bd9Sstevel@tonic-gate  * This function is slight different from pcp_peek. The peek requests are first
8687c478bd9Sstevel@tonic-gate  * serviced from local read buffer, if data is available. If the peek request
8697c478bd9Sstevel@tonic-gate  * is not serviceble from local read buffer, then the data is peeked from
8707c478bd9Sstevel@tonic-gate  * channel buffer. This function is mainly used for proper protocol framing
8717c478bd9Sstevel@tonic-gate  * error handling.
8727c478bd9Sstevel@tonic-gate  */
8737c478bd9Sstevel@tonic-gate static int
8747c478bd9Sstevel@tonic-gate pcp_peek_read(uint8_t *buf, int byte_cnt)
8757c478bd9Sstevel@tonic-gate {
8767c478bd9Sstevel@tonic-gate 	int	n, m, i;
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	if (byte_cnt < 0 || byte_cnt > mtu_size) {
8797c478bd9Sstevel@tonic-gate 		return (PCPL_INVALID_ARGS);
8807c478bd9Sstevel@tonic-gate 	}
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	/*
8837c478bd9Sstevel@tonic-gate 	 * initialization of peek_read buffer.
8847c478bd9Sstevel@tonic-gate 	 */
8857c478bd9Sstevel@tonic-gate 	if (peek_read_area == NULL) {
8867c478bd9Sstevel@tonic-gate 		peek_read_area = (uint8_t *)umem_zalloc(PEEK_READ_AREA_SIZE,
8877c478bd9Sstevel@tonic-gate 						UMEM_DEFAULT);
8887c478bd9Sstevel@tonic-gate 		if (peek_read_area == NULL) {
8897c478bd9Sstevel@tonic-gate 			return (PCPL_MALLOC_FAIL);
8907c478bd9Sstevel@tonic-gate 		}
8917c478bd9Sstevel@tonic-gate 		peek_read_head = peek_read_area;
8927c478bd9Sstevel@tonic-gate 		peek_read_tail = peek_read_area;
8937c478bd9Sstevel@tonic-gate 	}
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 	/*
8967c478bd9Sstevel@tonic-gate 	 * if we already have the data in local read buffer then copy
8977c478bd9Sstevel@tonic-gate 	 * from local buffer it self w/out calling new peek
8987c478bd9Sstevel@tonic-gate 	 */
8997c478bd9Sstevel@tonic-gate 	if (byte_cnt <= (read_tail - read_head)) {
9007c478bd9Sstevel@tonic-gate 		(void) memcpy(buf, read_head, byte_cnt);
9017c478bd9Sstevel@tonic-gate 		return (byte_cnt);
9027c478bd9Sstevel@tonic-gate 	}
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	/*
9057c478bd9Sstevel@tonic-gate 	 * if the request is not satisfied from local read buffer, then first
9067c478bd9Sstevel@tonic-gate 	 * copy the remaining data in local read buffer to peek_read_area and
9077c478bd9Sstevel@tonic-gate 	 * then issue new peek.
9087c478bd9Sstevel@tonic-gate 	 */
9097c478bd9Sstevel@tonic-gate 	for (i = 0; i < (read_tail - read_head); ++i) {
9107c478bd9Sstevel@tonic-gate 		peek_read_area[i] = read_head[i];
9117c478bd9Sstevel@tonic-gate 	}
9127c478bd9Sstevel@tonic-gate 	peek_read_head = peek_read_area;
9137c478bd9Sstevel@tonic-gate 	peek_read_tail = peek_read_head + i;
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	/*
9167c478bd9Sstevel@tonic-gate 	 * do a peek to see how much data is available and read complete data.
9177c478bd9Sstevel@tonic-gate 	 */
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	if ((m = pcp_peek(peek_read_tail, mtu_size)) < 0) {
9207c478bd9Sstevel@tonic-gate 		return (m);
9217c478bd9Sstevel@tonic-gate 	}
9227c478bd9Sstevel@tonic-gate 	peek_read_tail += m;
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	/*
9257c478bd9Sstevel@tonic-gate 	 * copy the requested bytes
9267c478bd9Sstevel@tonic-gate 	 */
9277c478bd9Sstevel@tonic-gate 	n = MIN(byte_cnt, (peek_read_tail - peek_read_head));
9287c478bd9Sstevel@tonic-gate 	(void) memcpy(buf, peek_read_head, n);
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	return (n);
9317c478bd9Sstevel@tonic-gate }
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate /*
9347c478bd9Sstevel@tonic-gate  * Send Request Message Header.
9357c478bd9Sstevel@tonic-gate  */
9367c478bd9Sstevel@tonic-gate static int
9377c478bd9Sstevel@tonic-gate pcp_send_req_msg_hdr(pcp_req_msg_hdr_t *req_hdr)
9387c478bd9Sstevel@tonic-gate {
9397c478bd9Sstevel@tonic-gate 	pcp_req_msg_hdr_t	*hdrp;
9407c478bd9Sstevel@tonic-gate 	int			hdr_sz;
9417c478bd9Sstevel@tonic-gate 	int			ret;
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	hdr_sz = sizeof (pcp_req_msg_hdr_t);
9447c478bd9Sstevel@tonic-gate 	if ((hdrp = (pcp_req_msg_hdr_t *)umem_zalloc(hdr_sz,
9457c478bd9Sstevel@tonic-gate 						UMEM_DEFAULT)) == NULL) {
9467c478bd9Sstevel@tonic-gate 		return (PCPL_MALLOC_FAIL);
9477c478bd9Sstevel@tonic-gate 	}
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	hdrp->magic_num = htonl(req_hdr->magic_num);
9507c478bd9Sstevel@tonic-gate 	hdrp->proto_ver = req_hdr->proto_ver;
9517c478bd9Sstevel@tonic-gate 	hdrp->msg_type = req_hdr->msg_type;
9527c478bd9Sstevel@tonic-gate 	hdrp->sub_type = req_hdr->sub_type;
9537c478bd9Sstevel@tonic-gate 	hdrp->rsvd_pad = htons(req_hdr->rsvd_pad);
9547c478bd9Sstevel@tonic-gate 	hdrp->xid = htonl(req_hdr->xid);
9557c478bd9Sstevel@tonic-gate 	hdrp->timeout = htonl(req_hdr->timeout);
9567c478bd9Sstevel@tonic-gate 	hdrp->msg_len = htonl(req_hdr->msg_len);
9577c478bd9Sstevel@tonic-gate 	hdrp->msg_cksum = htons(req_hdr->msg_cksum);
9587c478bd9Sstevel@tonic-gate 	hdrp->hdr_cksum = htons(req_hdr->hdr_cksum);
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	if ((ret = pcp_io_op((char *)hdrp, hdr_sz, PCPL_IO_OP_WRITE)) != 0) {
9617c478bd9Sstevel@tonic-gate 		umem_free(hdrp, hdr_sz);
9627c478bd9Sstevel@tonic-gate 		return (ret);
9637c478bd9Sstevel@tonic-gate 	}
9647c478bd9Sstevel@tonic-gate 	umem_free(hdrp, hdr_sz);
9657c478bd9Sstevel@tonic-gate 	return (PCP_OK);
9667c478bd9Sstevel@tonic-gate }
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate /*
9697c478bd9Sstevel@tonic-gate  * Receive Response message header.
9707c478bd9Sstevel@tonic-gate  */
9717c478bd9Sstevel@tonic-gate static int
9727c478bd9Sstevel@tonic-gate pcp_recv_resp_msg_hdr(pcp_resp_msg_hdr_t *resp_hdr)
9737c478bd9Sstevel@tonic-gate {
9747c478bd9Sstevel@tonic-gate 	uint32_t	magic_num;
9757c478bd9Sstevel@tonic-gate 	uint8_t		proto_ver;
9767c478bd9Sstevel@tonic-gate 	uint8_t		msg_type;
9777c478bd9Sstevel@tonic-gate 	uint8_t		sub_type;
9787c478bd9Sstevel@tonic-gate 	uint8_t		rsvd_pad;
9797c478bd9Sstevel@tonic-gate 	uint32_t	xid;
9807c478bd9Sstevel@tonic-gate 	uint32_t	timeout;
9817c478bd9Sstevel@tonic-gate 	uint32_t	msg_len;
9827c478bd9Sstevel@tonic-gate 	uint32_t	status;
9837c478bd9Sstevel@tonic-gate 	uint16_t	msg_cksum;
9847c478bd9Sstevel@tonic-gate 	uint16_t	hdr_cksum;
9857c478bd9Sstevel@tonic-gate 	int		ret;
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 	if (resp_hdr == NULL) {
9887c478bd9Sstevel@tonic-gate 		return (PCPL_INVALID_ARGS);
9897c478bd9Sstevel@tonic-gate 	}
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	/*
9927c478bd9Sstevel@tonic-gate 	 * handle protocol framing errors.
9937c478bd9Sstevel@tonic-gate 	 * pcp_frame_error_handle() returns when proper frame arrived
9947c478bd9Sstevel@tonic-gate 	 * (magic seq) or if an error happens while reading data from
9957c478bd9Sstevel@tonic-gate 	 * channel.
9967c478bd9Sstevel@tonic-gate 	 */
997*1ae08745Sheppo 	if (xport_type  == GLVC_NON_STREAM)
998*1ae08745Sheppo 		ret = pcp_frame_error_handle();
999*1ae08745Sheppo 	else
1000*1ae08745Sheppo 		ret = pcp_vldc_frame_error_handle();
1001*1ae08745Sheppo 
1002*1ae08745Sheppo 	if (ret != 0)
10037c478bd9Sstevel@tonic-gate 		return (PCPL_FRAME_ERROR);
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	/* read magic number first */
10067c478bd9Sstevel@tonic-gate 	if ((ret = pcp_io_op(&magic_num, sizeof (magic_num),
10077c478bd9Sstevel@tonic-gate 					PCPL_IO_OP_READ)) != 0) {
10087c478bd9Sstevel@tonic-gate 		return (ret);
10097c478bd9Sstevel@tonic-gate 	}
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 	magic_num = ntohl(magic_num);
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	if (magic_num != PCP_MAGIC_NUM) {
10147c478bd9Sstevel@tonic-gate 		return (PCPL_FRAME_ERROR);
10157c478bd9Sstevel@tonic-gate 	}
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	/* read version field */
10187c478bd9Sstevel@tonic-gate 	if ((ret = pcp_io_op(&proto_ver, sizeof (proto_ver),
10197c478bd9Sstevel@tonic-gate 					PCPL_IO_OP_READ)) != 0) {
10207c478bd9Sstevel@tonic-gate 		return (ret);
10217c478bd9Sstevel@tonic-gate 	}
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	/* check protocol version */
10247c478bd9Sstevel@tonic-gate 	if (proto_ver != PCP_PROT_VER_1) {
10257c478bd9Sstevel@tonic-gate 		return (PCPL_PROT_ERROR);
10267c478bd9Sstevel@tonic-gate 	}
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	/* Read message type */
10297c478bd9Sstevel@tonic-gate 	if ((ret = pcp_io_op(&msg_type, sizeof (msg_type),
10307c478bd9Sstevel@tonic-gate 					PCPL_IO_OP_READ)) != 0) {
10317c478bd9Sstevel@tonic-gate 		return (ret);
10327c478bd9Sstevel@tonic-gate 	}
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	/* Read message sub type */
10357c478bd9Sstevel@tonic-gate 	if ((ret = pcp_io_op(&sub_type, sizeof (sub_type),
10367c478bd9Sstevel@tonic-gate 					PCPL_IO_OP_READ)) != 0) {
10377c478bd9Sstevel@tonic-gate 		return (ret);
10387c478bd9Sstevel@tonic-gate 	}
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	/* Read rcvd_pad bits */
10417c478bd9Sstevel@tonic-gate 	if ((ret = pcp_io_op(&rsvd_pad, sizeof (rsvd_pad),
10427c478bd9Sstevel@tonic-gate 					PCPL_IO_OP_READ)) != 0) {
10437c478bd9Sstevel@tonic-gate 		return (ret);
10447c478bd9Sstevel@tonic-gate 	}
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	/* receive transaction id */
10477c478bd9Sstevel@tonic-gate 	if ((ret = pcp_io_op(&xid, sizeof (xid),
10487c478bd9Sstevel@tonic-gate 			PCPL_IO_OP_READ)) != 0) {
10497c478bd9Sstevel@tonic-gate 		return (ret);
10507c478bd9Sstevel@tonic-gate 	}
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	xid = ntohl(xid);
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	/* receive timeout value */
10557c478bd9Sstevel@tonic-gate 	if ((ret = pcp_io_op(&timeout, sizeof (timeout),
10567c478bd9Sstevel@tonic-gate 				PCPL_IO_OP_READ)) != 0) {
10577c478bd9Sstevel@tonic-gate 		return (ret);
10587c478bd9Sstevel@tonic-gate 	}
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	timeout = ntohl(timeout);
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 	/* receive message length */
10637c478bd9Sstevel@tonic-gate 	if ((ret = pcp_io_op(&msg_len, sizeof (msg_len),
10647c478bd9Sstevel@tonic-gate 				PCPL_IO_OP_READ)) != 0) {
10657c478bd9Sstevel@tonic-gate 		return (ret);
10667c478bd9Sstevel@tonic-gate 	}
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	msg_len = ntohl(msg_len);
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	/* receive status field */
10717c478bd9Sstevel@tonic-gate 	if ((ret = pcp_io_op(&status, sizeof (status),
10727c478bd9Sstevel@tonic-gate 				PCPL_IO_OP_READ)) != 0) {
10737c478bd9Sstevel@tonic-gate 		return (ret);
10747c478bd9Sstevel@tonic-gate 	}
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	status = ntohl(status);
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	/* receive message checksum */
10797c478bd9Sstevel@tonic-gate 	if ((ret = pcp_io_op(&msg_cksum, sizeof (msg_cksum),
10807c478bd9Sstevel@tonic-gate 					PCPL_IO_OP_READ)) != 0) {
10817c478bd9Sstevel@tonic-gate 		return (ret);
10827c478bd9Sstevel@tonic-gate 	}
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	msg_cksum = ntohs(msg_cksum);
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	/* receive header checksum */
10877c478bd9Sstevel@tonic-gate 	if ((ret = pcp_io_op(&hdr_cksum, sizeof (hdr_cksum),
10887c478bd9Sstevel@tonic-gate 					PCPL_IO_OP_READ)) != 0) {
10897c478bd9Sstevel@tonic-gate 		return (ret);
10907c478bd9Sstevel@tonic-gate 	}
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	hdr_cksum = ntohs(hdr_cksum);
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 	/* copy to resp_hdr */
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	resp_hdr->magic_num = magic_num;
10977c478bd9Sstevel@tonic-gate 	resp_hdr->proto_ver = proto_ver;
10987c478bd9Sstevel@tonic-gate 	resp_hdr->msg_type = msg_type;
10997c478bd9Sstevel@tonic-gate 	resp_hdr->sub_type = sub_type;
11007c478bd9Sstevel@tonic-gate 	resp_hdr->rsvd_pad = rsvd_pad;
11017c478bd9Sstevel@tonic-gate 	resp_hdr->xid = xid;
11027c478bd9Sstevel@tonic-gate 	resp_hdr->timeout = timeout;
11037c478bd9Sstevel@tonic-gate 	resp_hdr->msg_len = msg_len;
11047c478bd9Sstevel@tonic-gate 	resp_hdr->status = status;
11057c478bd9Sstevel@tonic-gate 	resp_hdr->msg_cksum = msg_cksum;
11067c478bd9Sstevel@tonic-gate 	resp_hdr->hdr_cksum = hdr_cksum;
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	return (PCP_OK);
11097c478bd9Sstevel@tonic-gate }
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate /*
11127c478bd9Sstevel@tonic-gate  * Get next xid for including in request message.
11137c478bd9Sstevel@tonic-gate  * Every request and response message are matched
11147c478bd9Sstevel@tonic-gate  * for same xid.
11157c478bd9Sstevel@tonic-gate  */
111656b04ea6Smvgr 
111756b04ea6Smvgr static uint32_t
111856b04ea6Smvgr pcp_get_xid(void)
11197c478bd9Sstevel@tonic-gate {
11207c478bd9Sstevel@tonic-gate 	uint32_t ret;
112156b04ea6Smvgr 	struct timeval tv;
112256b04ea6Smvgr 	static boolean_t xid_initialized = B_FALSE;
11237c478bd9Sstevel@tonic-gate 
112456b04ea6Smvgr 	if (xid_initialized == B_FALSE) {
112556b04ea6Smvgr 		xid_initialized = B_TRUE;
112656b04ea6Smvgr 		/*
112756b04ea6Smvgr 		 * starting xid is initialized to a different value everytime
112856b04ea6Smvgr 		 * user application is restarted so that user apps will not
112956b04ea6Smvgr 		 * receive previous session's packets.
113056b04ea6Smvgr 		 *
113156b04ea6Smvgr 		 * Note: The algorithm for generating initial xid is partially
113256b04ea6Smvgr 		 *	 taken from Solaris rpc code.
113356b04ea6Smvgr 		 */
113456b04ea6Smvgr 		(void) gettimeofday(&tv, NULL);
113556b04ea6Smvgr 		msg_xid = (uint32_t)((tv.tv_sec << 20) |
113656b04ea6Smvgr 				(tv.tv_usec >> 10));
113756b04ea6Smvgr 	}
113856b04ea6Smvgr 
113956b04ea6Smvgr 	ret = msg_xid++;
114056b04ea6Smvgr 
114156b04ea6Smvgr 	/* zero xid is not allowed */
114256b04ea6Smvgr 	if (ret == 0)
114356b04ea6Smvgr 		ret = msg_xid++;
114456b04ea6Smvgr 
11457c478bd9Sstevel@tonic-gate 	return (ret);
11467c478bd9Sstevel@tonic-gate }
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate /*
11497c478bd9Sstevel@tonic-gate  * This function handles channel framing errors. It waits until proper
11507c478bd9Sstevel@tonic-gate  * frame with starting sequence as magic numder (0xAFBCAFA0)
11517c478bd9Sstevel@tonic-gate  * is arrived. It removes unexpected data (before the magic number sequence)
11527c478bd9Sstevel@tonic-gate  * on the channel. It returns when proper magic number sequence is seen
11537c478bd9Sstevel@tonic-gate  * or when any failure happens while reading/peeking the channel.
11547c478bd9Sstevel@tonic-gate  */
11557c478bd9Sstevel@tonic-gate static int
11567c478bd9Sstevel@tonic-gate pcp_frame_error_handle(void)
11577c478bd9Sstevel@tonic-gate {
11587c478bd9Sstevel@tonic-gate 	uint8_t		magic_num_buf[4];
11597c478bd9Sstevel@tonic-gate 	int		ispresent = 0;
11607c478bd9Sstevel@tonic-gate 	uint32_t	net_magic_num; /* magic byte in network byte order */
11617c478bd9Sstevel@tonic-gate 	uint32_t	host_magic_num = PCP_MAGIC_NUM;
11627c478bd9Sstevel@tonic-gate 	uint8_t		buf[2];
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	net_magic_num =  htonl(host_magic_num);
11657c478bd9Sstevel@tonic-gate 	(void) memcpy(magic_num_buf, (uint8_t *)&net_magic_num, 4);
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	while (!ispresent) {
11687c478bd9Sstevel@tonic-gate 		/*
11697c478bd9Sstevel@tonic-gate 		 * Check if next four bytes matches pcp magic number.
11707c478bd9Sstevel@tonic-gate 		 * if mathing not found, discard 1 byte and continue checking.
11717c478bd9Sstevel@tonic-gate 		 */
11727c478bd9Sstevel@tonic-gate 		if (!check_magic_byte_presence(4, &magic_num_buf[0],
11737c478bd9Sstevel@tonic-gate 							&ispresent)) {
11747c478bd9Sstevel@tonic-gate 			if (!ispresent) {
11757c478bd9Sstevel@tonic-gate 				/* remove 1 byte */
11767c478bd9Sstevel@tonic-gate 				(void) pcp_io_op(buf, 1, PCPL_IO_OP_READ);
11777c478bd9Sstevel@tonic-gate 			}
11787c478bd9Sstevel@tonic-gate 		} else {
11797c478bd9Sstevel@tonic-gate 			return (-1);
11807c478bd9Sstevel@tonic-gate 		}
11817c478bd9Sstevel@tonic-gate 	}
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 	return (0);
11847c478bd9Sstevel@tonic-gate }
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate /*
1187*1ae08745Sheppo  * This function handles channel framing errors. It waits until proper
1188*1ae08745Sheppo  * frame with starting sequence as magic numder (0xAFBCAFA0)
1189*1ae08745Sheppo  * is arrived. It removes unexpected data (before the magic number sequence)
1190*1ae08745Sheppo  * on the channel. It returns when proper magic number sequence is seen
1191*1ae08745Sheppo  * or when any failure happens while reading/peeking the channel.
1192*1ae08745Sheppo  */
1193*1ae08745Sheppo static int
1194*1ae08745Sheppo pcp_vldc_frame_error_handle(void)
1195*1ae08745Sheppo {
1196*1ae08745Sheppo 	uint8_t		magic_num_buf[4];
1197*1ae08745Sheppo 	uint32_t	net_magic_num; /* magic byte in network byte order */
1198*1ae08745Sheppo 	uint32_t	host_magic_num = PCP_MAGIC_NUM;
1199*1ae08745Sheppo 	int		found_magic = 0;
1200*1ae08745Sheppo 
1201*1ae08745Sheppo 	net_magic_num =  htonl(host_magic_num);
1202*1ae08745Sheppo 	(void) memcpy(magic_num_buf, (uint8_t *)&net_magic_num, 4);
1203*1ae08745Sheppo 
1204*1ae08745Sheppo 	/*
1205*1ae08745Sheppo 	 * For vldc, we need to read whatever data is available and
1206*1ae08745Sheppo 	 * advance the read pointer one byte at a time until we get
1207*1ae08745Sheppo 	 * the magic word. When this function is invoked, we do not
1208*1ae08745Sheppo 	 * have any byte in the read buffer.
1209*1ae08745Sheppo 	 */
1210*1ae08745Sheppo 
1211*1ae08745Sheppo 	/*
1212*1ae08745Sheppo 	 * Keep reading until we find the matching magic number
1213*1ae08745Sheppo 	 */
1214*1ae08745Sheppo 	while (!found_magic) {
1215*1ae08745Sheppo 		while ((read_tail - read_head) < sizeof (host_magic_num)) {
1216*1ae08745Sheppo 			if (pcp_update_read_area(sizeof (host_magic_num)) < 0)
1217*1ae08745Sheppo 				return (-1);
1218*1ae08745Sheppo 		}
1219*1ae08745Sheppo 
1220*1ae08745Sheppo 		/*
1221*1ae08745Sheppo 		 * We should have at least 4 bytes in read buffer. Check
1222*1ae08745Sheppo 		 * if the magic number can be matched
1223*1ae08745Sheppo 		 */
1224*1ae08745Sheppo 		if (memcmp(read_head, magic_num_buf,
1225*1ae08745Sheppo 				sizeof (host_magic_num))) {
1226*1ae08745Sheppo 			read_head += 1;
1227*1ae08745Sheppo 		} else {
1228*1ae08745Sheppo 			found_magic = 1;
1229*1ae08745Sheppo 		}
1230*1ae08745Sheppo 	}
1231*1ae08745Sheppo 
1232*1ae08745Sheppo 	return (0);
1233*1ae08745Sheppo }
1234*1ae08745Sheppo 
1235*1ae08745Sheppo /*
12367c478bd9Sstevel@tonic-gate  * checks whether certain byte sequence is present in the data stream.
12377c478bd9Sstevel@tonic-gate  */
12387c478bd9Sstevel@tonic-gate static int
12397c478bd9Sstevel@tonic-gate check_magic_byte_presence(int byte_cnt, uint8_t *byte_seq, int *ispresent)
12407c478bd9Sstevel@tonic-gate {
12417c478bd9Sstevel@tonic-gate 	int		ret, i;
12427c478bd9Sstevel@tonic-gate 	uint8_t		buf[4];
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 	if ((ret = pcp_peek_read(buf, byte_cnt)) < 0) {
12457c478bd9Sstevel@tonic-gate 		return (ret);
12467c478bd9Sstevel@tonic-gate 	}
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 	/* 'byte_cnt' bytes not present */
12497c478bd9Sstevel@tonic-gate 	if (ret != byte_cnt) {
12507c478bd9Sstevel@tonic-gate 		*ispresent = 0;
12517c478bd9Sstevel@tonic-gate 		return (0);
12527c478bd9Sstevel@tonic-gate 	}
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 	for (i = 0; i < byte_cnt; ++i) {
12557c478bd9Sstevel@tonic-gate 		if (buf[i] != byte_seq[i]) {
12567c478bd9Sstevel@tonic-gate 			*ispresent = 0;
12577c478bd9Sstevel@tonic-gate 			return (0);
12587c478bd9Sstevel@tonic-gate 		}
12597c478bd9Sstevel@tonic-gate 	}
12607c478bd9Sstevel@tonic-gate 	*ispresent = 1;
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 	return (0);
12637c478bd9Sstevel@tonic-gate }
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate /*
12667c478bd9Sstevel@tonic-gate  * 16-bit simple internet checksum
12677c478bd9Sstevel@tonic-gate  */
12687c478bd9Sstevel@tonic-gate static uint16_t
12697c478bd9Sstevel@tonic-gate checksum(uint16_t *addr, int32_t count)
12707c478bd9Sstevel@tonic-gate {
12717c478bd9Sstevel@tonic-gate 	/*
12727c478bd9Sstevel@tonic-gate 	 * Compute Internet Checksum for "count" bytes
12737c478bd9Sstevel@tonic-gate 	 * beginning at location "addr".
12747c478bd9Sstevel@tonic-gate 	 */
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate 	register uint32_t	sum = 0;
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 	while (count > 1)  {
12797c478bd9Sstevel@tonic-gate 		/*  This is the inner loop */
12807c478bd9Sstevel@tonic-gate 		sum += *(unsigned short *)addr++;
12817c478bd9Sstevel@tonic-gate 		count -= 2;
12827c478bd9Sstevel@tonic-gate 	}
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 	/*  Add left-over byte, if any */
12857c478bd9Sstevel@tonic-gate 	if (count > 0)
12867c478bd9Sstevel@tonic-gate 		sum += * (unsigned char *)addr;
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate 	/* Fold 32-bit sum to 16 bits */
12897c478bd9Sstevel@tonic-gate 	while (sum >> 16)
12907c478bd9Sstevel@tonic-gate 		sum = (sum & 0xffff) + (sum >> 16);
12917c478bd9Sstevel@tonic-gate 
12927c478bd9Sstevel@tonic-gate 	sum = (~sum) & 0xffff;
12937c478bd9Sstevel@tonic-gate 	if (sum == 0)
12947c478bd9Sstevel@tonic-gate 		sum = 0xffff;
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 	return (sum);
12977c478bd9Sstevel@tonic-gate }
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate /*
13007c478bd9Sstevel@tonic-gate  * cleanup the channel if any data is hanging in
13017c478bd9Sstevel@tonic-gate  * channel buffers.
13027c478bd9Sstevel@tonic-gate  */
13037c478bd9Sstevel@tonic-gate static int
13045db09ef5Smvgr pcp_cleanup(int channel_fd)
13057c478bd9Sstevel@tonic-gate {
13067c478bd9Sstevel@tonic-gate 	int			ret;
13077c478bd9Sstevel@tonic-gate 	glvc_xport_msg_peek_t	peek_ctrl;
13087c478bd9Sstevel@tonic-gate 	int			n, done;
13097c478bd9Sstevel@tonic-gate 	uint8_t			*buf = NULL;
13107c478bd9Sstevel@tonic-gate 	int			retry = 0;
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	buf = (uint8_t *)umem_zalloc((mtu_size), UMEM_DEFAULT);
13147c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
13157c478bd9Sstevel@tonic-gate 		return (PCPL_MALLOC_FAIL);
13167c478bd9Sstevel@tonic-gate 	}
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	peek_ctrl.buf = (caddr_t)buf;
13197c478bd9Sstevel@tonic-gate 	peek_ctrl.buflen = mtu_size;
13207c478bd9Sstevel@tonic-gate 	peek_ctrl.flags = 0;
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 	/*
13237c478bd9Sstevel@tonic-gate 	 * set sig jmp location
13247c478bd9Sstevel@tonic-gate 	 */
13257c478bd9Sstevel@tonic-gate 	if (sigsetjmp(jmpbuf, 1)) {
13267c478bd9Sstevel@tonic-gate 		umem_free(buf, mtu_size);
13277c478bd9Sstevel@tonic-gate 		return (PCPL_GLVC_TIMEOUT);
13287c478bd9Sstevel@tonic-gate 	}
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 	done = 0;
13317c478bd9Sstevel@tonic-gate 	while (!done) {
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 		(void) alarm(PCP_CLEANUP_TIMEOUT);
13345db09ef5Smvgr 		if ((ret = ioctl(channel_fd, GLVC_XPORT_IOCTL_DATA_PEEK,
13357c478bd9Sstevel@tonic-gate 							&peek_ctrl)) < 0) {
13367c478bd9Sstevel@tonic-gate 			(void) alarm(0);
13377c478bd9Sstevel@tonic-gate 			done = 1;
13387c478bd9Sstevel@tonic-gate 			continue;
13397c478bd9Sstevel@tonic-gate 		}
13407c478bd9Sstevel@tonic-gate 		(void) alarm(0);
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 		n = peek_ctrl.buflen;
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 		if (n <= 0 && retry > 2) {
13457c478bd9Sstevel@tonic-gate 			done = 1;
13467c478bd9Sstevel@tonic-gate 			continue;
13477c478bd9Sstevel@tonic-gate 		} else if (n <= 0) {
13487c478bd9Sstevel@tonic-gate 			++retry;
13497c478bd9Sstevel@tonic-gate 			continue;
13507c478bd9Sstevel@tonic-gate 		}
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 		/* remove data from channel */
13537c478bd9Sstevel@tonic-gate 		(void) alarm(PCP_CLEANUP_TIMEOUT);
13545db09ef5Smvgr 		if ((ret = read(channel_fd, buf, n)) < 0) {
13557c478bd9Sstevel@tonic-gate 			(void) alarm(0);
13567c478bd9Sstevel@tonic-gate 			done = 1;
13577c478bd9Sstevel@tonic-gate 			continue;
13587c478bd9Sstevel@tonic-gate 		}
13597c478bd9Sstevel@tonic-gate 		(void) alarm(0);
13607c478bd9Sstevel@tonic-gate 	}
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 	umem_free(buf, mtu_size);
13637c478bd9Sstevel@tonic-gate 	return (ret);
13647c478bd9Sstevel@tonic-gate }
1365*1ae08745Sheppo 
1366*1ae08745Sheppo static int
1367*1ae08745Sheppo vldc_write(int fd, uint8_t *bufp, int size)
1368*1ae08745Sheppo {
1369*1ae08745Sheppo 	int res;
1370*1ae08745Sheppo 	int left = size;
1371*1ae08745Sheppo 	pollfd_t pollfd;
1372*1ae08745Sheppo 
1373*1ae08745Sheppo 	pollfd.events = POLLOUT;
1374*1ae08745Sheppo 	pollfd.revents = 0;
1375*1ae08745Sheppo 	pollfd.fd = fd;
1376*1ae08745Sheppo 
1377*1ae08745Sheppo 	/*
1378*1ae08745Sheppo 	 * Poll for the vldc channel to be ready
1379*1ae08745Sheppo 	 */
1380*1ae08745Sheppo 	if (poll(&pollfd, 1, glvc_timeout * MILLISEC) <= 0) {
1381*1ae08745Sheppo 		return (-1);
1382*1ae08745Sheppo 	}
1383*1ae08745Sheppo 
1384*1ae08745Sheppo 	do {
1385*1ae08745Sheppo 		if ((res = write(fd, bufp, left)) <= 0) {
1386*1ae08745Sheppo 			if (errno != EWOULDBLOCK) {
1387*1ae08745Sheppo 				return (res);
1388*1ae08745Sheppo 			}
1389*1ae08745Sheppo 		} else {
1390*1ae08745Sheppo 			bufp += res;
1391*1ae08745Sheppo 			left -= res;
1392*1ae08745Sheppo 		}
1393*1ae08745Sheppo 	} while (left > 0);
1394*1ae08745Sheppo 
1395*1ae08745Sheppo 	/*
1396*1ae08745Sheppo 	 * Return number of bytes actually written
1397*1ae08745Sheppo 	 */
1398*1ae08745Sheppo 	return (size - left);
1399*1ae08745Sheppo }
1400*1ae08745Sheppo 
1401*1ae08745Sheppo /*
1402*1ae08745Sheppo  * Keep reading until we get the specified number of bytes
1403*1ae08745Sheppo  */
1404*1ae08745Sheppo static int
1405*1ae08745Sheppo vldc_read(int fd, uint8_t *bufp, int size)
1406*1ae08745Sheppo {
1407*1ae08745Sheppo 	int res;
1408*1ae08745Sheppo 	int left = size;
1409*1ae08745Sheppo 
1410*1ae08745Sheppo 	struct pollfd fds[1];
1411*1ae08745Sheppo 
1412*1ae08745Sheppo 	fds[0].events = POLLIN | POLLPRI;
1413*1ae08745Sheppo 	fds[0].revents = 0;
1414*1ae08745Sheppo 	fds[0].fd = fd;
1415*1ae08745Sheppo 
1416*1ae08745Sheppo 	if (poll(fds, 1, glvc_timeout * MILLISEC) <= 0) {
1417*1ae08745Sheppo 		return (-1);
1418*1ae08745Sheppo 	}
1419*1ae08745Sheppo 
1420*1ae08745Sheppo 	while (left > 0) {
1421*1ae08745Sheppo 		res = read(fd, bufp, left);
1422*1ae08745Sheppo 			/* return on error or short read */
1423*1ae08745Sheppo 		if ((res == 0) || ((res < 0) &&
1424*1ae08745Sheppo 			(errno == EAGAIN))) {
1425*1ae08745Sheppo 				/* poll until the read is unblocked */
1426*1ae08745Sheppo 				if ((poll(fds, 1, glvc_timeout * MILLISEC)) < 0)
1427*1ae08745Sheppo 					return (-1);
1428*1ae08745Sheppo 
1429*1ae08745Sheppo 				continue;
1430*1ae08745Sheppo 		} else
1431*1ae08745Sheppo 		if (res < 0) {
1432*1ae08745Sheppo 			/* unrecoverable error */
1433*1ae08745Sheppo 
1434*1ae08745Sheppo 			return (-1);
1435*1ae08745Sheppo 		} else {
1436*1ae08745Sheppo 			bufp += res;
1437*1ae08745Sheppo 			left -= res;
1438*1ae08745Sheppo 		}
1439*1ae08745Sheppo 	}
1440*1ae08745Sheppo 
1441*1ae08745Sheppo 	return (size - left);
1442*1ae08745Sheppo }
1443