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
547cd5876SAlan Perry  * Common Development and Distribution License (the "License").
647cd5876SAlan Perry  * 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 /*
2247cd5876SAlan Perry  * Copyright 2009 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  * av1394 isochronous transmit module
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate #include <sys/1394/targets/av1394/av1394_impl.h>
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate static int	av1394_it_start_common(av1394_ic_t *);
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /* configuration routines */
347c478bd9Sstevel@tonic-gate static void	av1394_it_cleanup(av1394_ic_t *, int);
357c478bd9Sstevel@tonic-gate static int	av1394_it_bld_ixl(av1394_ic_t *);
367c478bd9Sstevel@tonic-gate static void	av1394_it_destroy_ixl(av1394_ic_t *);
377c478bd9Sstevel@tonic-gate static int	av1394_it_ixl_bld_data(av1394_ic_t *);
387c478bd9Sstevel@tonic-gate static void	av1394_it_ixl_destroy_data(av1394_ic_t *);
397c478bd9Sstevel@tonic-gate static av1394_it_ixl_buf_t *av1394_it_ixl_bld_buf(av1394_ic_t *, int, int,
407c478bd9Sstevel@tonic-gate 		off_t, int, int);
417c478bd9Sstevel@tonic-gate static void	av1394_it_ixl_complete_buf(av1394_it_ixl_buf_t *,
427c478bd9Sstevel@tonic-gate 		av1394_it_ixl_empty_cip_t *);
437c478bd9Sstevel@tonic-gate static void	av1394_it_ixl_complete_buf2(av1394_it_ixl_buf_t *,
447c478bd9Sstevel@tonic-gate 		av1394_it_ixl_buf_t *);
457c478bd9Sstevel@tonic-gate static av1394_it_ixl_empty_cip_t *av1394_it_ixl_bld_empty_cip(av1394_ic_t *,
467c478bd9Sstevel@tonic-gate 		int);
477c478bd9Sstevel@tonic-gate static void	av1394_it_ixl_complete_empty_cip(av1394_it_ixl_empty_cip_t *,
487c478bd9Sstevel@tonic-gate 		av1394_it_ixl_buf_t *);
497c478bd9Sstevel@tonic-gate static void	av1394_it_ixl_bld_begin(av1394_ic_t *);
507c478bd9Sstevel@tonic-gate static void	av1394_it_ixl_begin_update_pkts(av1394_ic_t *,
517c478bd9Sstevel@tonic-gate 		av1394_it_ixl_buf_t *);
527c478bd9Sstevel@tonic-gate static int	av1394_it_alloc_isoch_dma(av1394_ic_t *);
537c478bd9Sstevel@tonic-gate static void	av1394_it_free_isoch_dma(av1394_ic_t *);
547c478bd9Sstevel@tonic-gate static void	av1394_it_dma_sync_frames(av1394_ic_t *, int, int);
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /* callbacks */
577c478bd9Sstevel@tonic-gate static void	av1394_it_ixl_begin_cb(opaque_t, struct ixl1394_callback *);
587c478bd9Sstevel@tonic-gate static void	av1394_it_ixl_buf_cb(opaque_t, struct ixl1394_callback *);
597c478bd9Sstevel@tonic-gate static void	av1394_it_ixl_eof_cb(av1394_it_ixl_buf_t *bp);
607c478bd9Sstevel@tonic-gate static int	av1394_it_underrun_resume(av1394_ic_t *);
617c478bd9Sstevel@tonic-gate static void	av1394_it_dma_stopped_cb(t1394_isoch_dma_handle_t,
627c478bd9Sstevel@tonic-gate 		opaque_t, id1394_isoch_dma_stopped_t);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /* data transfer routines */
657c478bd9Sstevel@tonic-gate static int	av1394_it_add_frames(av1394_ic_t *, int, int);
667c478bd9Sstevel@tonic-gate static int	av1394_it_wait_frames(av1394_ic_t *, int *, int *, int *);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate static void	av1394_it_update_frame_syt(av1394_ic_t *, int, int, uint16_t);
697c478bd9Sstevel@tonic-gate static uint16_t	av1394_it_ts_cyc2syt(uint16_t);
707c478bd9Sstevel@tonic-gate static uint16_t	av1394_it_ts_syt_inc(uint16_t, uint16_t);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static void	av1394_it_kcopyin(av1394_ic_t *, void *, size_t);
737c478bd9Sstevel@tonic-gate static int	av1394_it_copyin(av1394_ic_t *, struct uio *, int *, int);
747c478bd9Sstevel@tonic-gate static boolean_t av1394_it_is_dv_frame_start(caddr_t);
757c478bd9Sstevel@tonic-gate static void	av1394_it_reset_frame_syt(av1394_ic_t *, int);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /* tunables */
787c478bd9Sstevel@tonic-gate int av1394_it_hiwat_sub = 2;
797c478bd9Sstevel@tonic-gate int av1394_it_lowat = 3;
807c478bd9Sstevel@tonic-gate int av1394_it_start_thre = 3;	/* xmit start threshold */
817c478bd9Sstevel@tonic-gate int av1394_it_syt_off = 3;	/* SYT offset in cycles */
827c478bd9Sstevel@tonic-gate int av1394_it_dump_ixl = 0;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate int
av1394_it_init(av1394_ic_t * icp,int * error)857c478bd9Sstevel@tonic-gate av1394_it_init(av1394_ic_t *icp, int *error)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate 	av1394_it_t	*itp = &icp->ic_it;
887c478bd9Sstevel@tonic-gate 	av1394_isoch_pool_t *pool = &itp->it_data_pool;
897c478bd9Sstevel@tonic-gate 	int		nframes;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	nframes = av1394_ic_alloc_pool(pool, icp->ic_framesz, icp->ic_nframes,
9247cd5876SAlan Perry 	    AV1394_IT_NFRAMES_MIN);
937c478bd9Sstevel@tonic-gate 	if (nframes == 0) {
947c478bd9Sstevel@tonic-gate 		*error = IEC61883_ERR_NOMEM;
957c478bd9Sstevel@tonic-gate 		return (EINVAL);
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 	mutex_enter(&icp->ic_mutex);
987c478bd9Sstevel@tonic-gate 	icp->ic_nframes = nframes;
997c478bd9Sstevel@tonic-gate 	itp->it_hiwat = nframes - av1394_it_hiwat_sub;
1007c478bd9Sstevel@tonic-gate 	itp->it_lowat = av1394_it_lowat;
1017c478bd9Sstevel@tonic-gate 	itp->it_start_thre = av1394_it_start_thre;
1027c478bd9Sstevel@tonic-gate 	itp->it_nempty = icp->ic_nframes;
1037c478bd9Sstevel@tonic-gate 	itp->it_last_full = icp->ic_nframes - 1;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	if (av1394_ic_dma_setup(icp, pool) != DDI_SUCCESS) {
1067c478bd9Sstevel@tonic-gate 		mutex_exit(&icp->ic_mutex);
1077c478bd9Sstevel@tonic-gate 		*error = IEC61883_ERR_NOMEM;
1087c478bd9Sstevel@tonic-gate 		av1394_it_cleanup(icp, 1);
1097c478bd9Sstevel@tonic-gate 		return (EINVAL);
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	if (av1394_it_bld_ixl(icp) != DDI_SUCCESS) {
1137c478bd9Sstevel@tonic-gate 		mutex_exit(&icp->ic_mutex);
1147c478bd9Sstevel@tonic-gate 		*error = IEC61883_ERR_NOMEM;
1157c478bd9Sstevel@tonic-gate 		av1394_it_cleanup(icp, 2);
1167c478bd9Sstevel@tonic-gate 		return (EINVAL);
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 	mutex_exit(&icp->ic_mutex);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	if (av1394_it_alloc_isoch_dma(icp) != DDI_SUCCESS) {
1217c478bd9Sstevel@tonic-gate 		*error = IEC61883_ERR_NOMEM;
1227c478bd9Sstevel@tonic-gate 		av1394_it_cleanup(icp, 3);
1237c478bd9Sstevel@tonic-gate 		return (EINVAL);
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	return (0);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate void
av1394_it_fini(av1394_ic_t * icp)1307c478bd9Sstevel@tonic-gate av1394_it_fini(av1394_ic_t *icp)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	av1394_it_cleanup(icp, AV1394_CLEANUP_LEVEL_MAX);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate int
av1394_it_start(av1394_ic_t * icp)1367c478bd9Sstevel@tonic-gate av1394_it_start(av1394_ic_t *icp)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	av1394_it_t	*itp = &icp->ic_it;
1397c478bd9Sstevel@tonic-gate 	int		ret = 0;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	mutex_enter(&icp->ic_mutex);
1427c478bd9Sstevel@tonic-gate 	ASSERT(icp->ic_state == AV1394_IC_IDLE);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	/* should be enough full frames to be able to start */
1457c478bd9Sstevel@tonic-gate 	if (itp->it_nfull >= itp->it_start_thre) {
1467c478bd9Sstevel@tonic-gate 		ret = av1394_it_start_common(icp);
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 	mutex_exit(&icp->ic_mutex);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	return (ret);
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate static int
av1394_it_start_common(av1394_ic_t * icp)1547c478bd9Sstevel@tonic-gate av1394_it_start_common(av1394_ic_t *icp)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate 	av1394_inst_t	*avp = icp->ic_avp;
1577c478bd9Sstevel@tonic-gate 	id1394_isoch_dma_ctrlinfo_t idma_ctrlinfo = { 0 };
1587c478bd9Sstevel@tonic-gate 	int		result;
1597c478bd9Sstevel@tonic-gate 	int		err;
1607c478bd9Sstevel@tonic-gate 	int		ret = 0;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	ASSERT(icp->ic_state == AV1394_IC_IDLE);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	err = t1394_start_isoch_dma(avp->av_t1394_hdl, icp->ic_isoch_hdl,
16547cd5876SAlan Perry 	    &idma_ctrlinfo, 0, &result);
1667c478bd9Sstevel@tonic-gate 	if (err == DDI_SUCCESS) {
1677c478bd9Sstevel@tonic-gate 		icp->ic_state = AV1394_IC_DMA;
1687c478bd9Sstevel@tonic-gate 	} else {
1697c478bd9Sstevel@tonic-gate 		ret = EIO;
1707c478bd9Sstevel@tonic-gate 	}
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	return (ret);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate int
av1394_it_stop(av1394_ic_t * icp)1777c478bd9Sstevel@tonic-gate av1394_it_stop(av1394_ic_t *icp)
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate 	av1394_inst_t	*avp = icp->ic_avp;
1807c478bd9Sstevel@tonic-gate 	av1394_it_t	*itp = &icp->ic_it;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	mutex_enter(&icp->ic_mutex);
1837c478bd9Sstevel@tonic-gate 	if (icp->ic_state != AV1394_IC_IDLE) {
1847c478bd9Sstevel@tonic-gate 		mutex_exit(&icp->ic_mutex);
1857c478bd9Sstevel@tonic-gate 		t1394_stop_isoch_dma(avp->av_t1394_hdl, icp->ic_isoch_hdl, 0);
1867c478bd9Sstevel@tonic-gate 		mutex_enter(&icp->ic_mutex);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		icp->ic_state = AV1394_IC_IDLE;
1897c478bd9Sstevel@tonic-gate 		itp->it_nempty = icp->ic_nframes;
1907c478bd9Sstevel@tonic-gate 		itp->it_first_empty = 0;
1917c478bd9Sstevel@tonic-gate 		itp->it_last_full = icp->ic_nframes - 1;
1927c478bd9Sstevel@tonic-gate 		itp->it_nfull = 0;
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 	mutex_exit(&icp->ic_mutex);
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	return (0);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate int
av1394_it_xmit(av1394_ic_t * icp,iec61883_xmit_t * xmit)2007c478bd9Sstevel@tonic-gate av1394_it_xmit(av1394_ic_t *icp, iec61883_xmit_t *xmit)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	av1394_it_t	*itp = &icp->ic_it;
2037c478bd9Sstevel@tonic-gate 	int		ret = 0;
2047c478bd9Sstevel@tonic-gate 	int		idx, cnt;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	idx = xmit->tx_xfer.xf_full_idx;
2077c478bd9Sstevel@tonic-gate 	cnt = xmit->tx_xfer.xf_full_cnt;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	mutex_enter(&icp->ic_mutex);
2107c478bd9Sstevel@tonic-gate 	/* check arguments */
2117c478bd9Sstevel@tonic-gate 	if ((idx < 0) || (cnt < 0) || (cnt > itp->it_nempty)) {
2127c478bd9Sstevel@tonic-gate 		mutex_exit(&icp->ic_mutex);
2137c478bd9Sstevel@tonic-gate 		return (EINVAL);
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	/* add full frames to the pool */
2177c478bd9Sstevel@tonic-gate 	if (cnt > 0) {
2187c478bd9Sstevel@tonic-gate 		if ((ret = av1394_it_add_frames(icp, idx, cnt)) != 0) {
2197c478bd9Sstevel@tonic-gate 			mutex_exit(&icp->ic_mutex);
2207c478bd9Sstevel@tonic-gate 			return (ret);
2217c478bd9Sstevel@tonic-gate 		}
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	if ((icp->ic_state == AV1394_IC_IDLE) &&
2257c478bd9Sstevel@tonic-gate 	    (itp->it_nfull >= itp->it_start_thre)) {
2267c478bd9Sstevel@tonic-gate 		if ((ret = av1394_it_start_common(icp)) != 0) {
2277c478bd9Sstevel@tonic-gate 			mutex_exit(&icp->ic_mutex);
2287c478bd9Sstevel@tonic-gate 			return (ret);
2297c478bd9Sstevel@tonic-gate 		}
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	/* wait for new empty frames */
2337c478bd9Sstevel@tonic-gate 	ret = av1394_it_wait_frames(icp, &xmit->tx_xfer.xf_empty_idx,
23447cd5876SAlan Perry 	    &xmit->tx_xfer.xf_empty_cnt, &xmit->tx_miss_cnt);
2357c478bd9Sstevel@tonic-gate 	mutex_exit(&icp->ic_mutex);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	return (ret);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate int
av1394_it_write(av1394_ic_t * icp,struct uio * uiop)2417c478bd9Sstevel@tonic-gate av1394_it_write(av1394_ic_t *icp, struct uio *uiop)
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 	av1394_inst_t	*avp = icp->ic_avp;
2447c478bd9Sstevel@tonic-gate 	av1394_it_t	*itp = &icp->ic_it;
2457c478bd9Sstevel@tonic-gate 	av1394_isoch_autoxmit_t *axp = &avp->av_i.i_autoxmit;
2467c478bd9Sstevel@tonic-gate 	int		dv;
2477c478bd9Sstevel@tonic-gate 	int		ret = 0;
2487c478bd9Sstevel@tonic-gate 	int		full_cnt;
2497c478bd9Sstevel@tonic-gate 	int		miss_cnt;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	mutex_enter(&icp->ic_mutex);
2527c478bd9Sstevel@tonic-gate 	dv = (axp->ax_fmt & AV1394_ISOCH_AUTOXMIT_DV);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	while (uiop->uio_resid > 0) {
2557c478bd9Sstevel@tonic-gate 		/* must have at least one empty frame */
2567c478bd9Sstevel@tonic-gate 		if (itp->it_write_cnt == 0) {
2577c478bd9Sstevel@tonic-gate 			ret = av1394_it_wait_frames(icp, &itp->it_write_idx,
25847cd5876SAlan Perry 			    &itp->it_write_cnt, &miss_cnt);
2597c478bd9Sstevel@tonic-gate 			if (ret != 0) {
2607c478bd9Sstevel@tonic-gate 				break;
2617c478bd9Sstevel@tonic-gate 			}
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 		/* copyin as much data as we can */
2657c478bd9Sstevel@tonic-gate 		if (axp->ax_copy_ciph) {
2667c478bd9Sstevel@tonic-gate 			ASSERT(itp->it_write_off == 0);
2677c478bd9Sstevel@tonic-gate 			av1394_it_kcopyin(icp, axp->ax_ciph, AV1394_CIPSZ);
2687c478bd9Sstevel@tonic-gate 			axp->ax_copy_ciph = B_FALSE;
2697c478bd9Sstevel@tonic-gate 		}
2707c478bd9Sstevel@tonic-gate 		if ((ret = av1394_it_copyin(icp, uiop, &full_cnt, dv)) != 0) {
2717c478bd9Sstevel@tonic-gate 			break;
2727c478bd9Sstevel@tonic-gate 		}
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 		/* add full frames to the pool */
2757c478bd9Sstevel@tonic-gate 		if (full_cnt > 0) {
2767c478bd9Sstevel@tonic-gate 			ret = av1394_it_add_frames(icp,
27747cd5876SAlan Perry 			    itp->it_write_idx, full_cnt);
2787c478bd9Sstevel@tonic-gate 			if (ret != 0) {
2797c478bd9Sstevel@tonic-gate 				break;
2807c478bd9Sstevel@tonic-gate 			}
2817c478bd9Sstevel@tonic-gate 			itp->it_write_idx += full_cnt;
2827c478bd9Sstevel@tonic-gate 			itp->it_write_idx %= icp->ic_nframes;
2837c478bd9Sstevel@tonic-gate 		}
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 		/* start xfer if not already */
2867c478bd9Sstevel@tonic-gate 		if ((icp->ic_state == AV1394_IC_IDLE) &&
2877c478bd9Sstevel@tonic-gate 		    (itp->it_nfull >= itp->it_start_thre)) {
2887c478bd9Sstevel@tonic-gate 			if ((ret = av1394_it_start_common(icp)) != 0) {
2897c478bd9Sstevel@tonic-gate 				mutex_exit(&icp->ic_mutex);
2907c478bd9Sstevel@tonic-gate 				return (ret);
2917c478bd9Sstevel@tonic-gate 			}
2927c478bd9Sstevel@tonic-gate 		}
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 	mutex_exit(&icp->ic_mutex);
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	return (ret);
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate /*
3007c478bd9Sstevel@tonic-gate  *
3017c478bd9Sstevel@tonic-gate  * --- configuration routines
3027c478bd9Sstevel@tonic-gate  *
3037c478bd9Sstevel@tonic-gate  */
3047c478bd9Sstevel@tonic-gate static void
av1394_it_cleanup(av1394_ic_t * icp,int level)3057c478bd9Sstevel@tonic-gate av1394_it_cleanup(av1394_ic_t *icp, int level)
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate 	av1394_isoch_pool_t *pool = &icp->ic_it.it_data_pool;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	ASSERT((level > 0) && (level <= AV1394_CLEANUP_LEVEL_MAX));
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	switch (level) {
3127c478bd9Sstevel@tonic-gate 	default:
3137c478bd9Sstevel@tonic-gate 		av1394_it_free_isoch_dma(icp);
3147c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
3157c478bd9Sstevel@tonic-gate 	case 3:
3167c478bd9Sstevel@tonic-gate 		av1394_it_destroy_ixl(icp);
3177c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
3187c478bd9Sstevel@tonic-gate 	case 2:
3197c478bd9Sstevel@tonic-gate 		av1394_ic_dma_cleanup(icp, pool);
3207c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
3217c478bd9Sstevel@tonic-gate 	case 1:
3227c478bd9Sstevel@tonic-gate 		av1394_ic_free_pool(pool);
3237c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate /*
3287c478bd9Sstevel@tonic-gate  * av1394_it_bld_ixl()
3297c478bd9Sstevel@tonic-gate  *    Build an IXL chain out of several blocks.
3307c478bd9Sstevel@tonic-gate  */
3317c478bd9Sstevel@tonic-gate static int
av1394_it_bld_ixl(av1394_ic_t * icp)3327c478bd9Sstevel@tonic-gate av1394_it_bld_ixl(av1394_ic_t *icp)
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate 	av1394_it_t	*itp = &icp->ic_it;
3357c478bd9Sstevel@tonic-gate 	int		ret;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	/* data block */
3387c478bd9Sstevel@tonic-gate 	if ((ret = av1394_it_ixl_bld_data(icp)) != DDI_SUCCESS) {
3397c478bd9Sstevel@tonic-gate 		return (ret);
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	/* begin block */
3437c478bd9Sstevel@tonic-gate 	if (icp->ic_param.cp_ts_mode != IEC61883_TS_NONE) {
3447c478bd9Sstevel@tonic-gate 		av1394_it_ixl_bld_begin(icp);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 		itp->it_ixlp = (ixl1394_command_t *)&itp->it_ixl_begin;
3477c478bd9Sstevel@tonic-gate 	} else {
3487c478bd9Sstevel@tonic-gate 		itp->it_ixlp = (ixl1394_command_t *)
34947cd5876SAlan Perry 		    &((av1394_it_ixl_buf_t *)itp->it_ixl_data)->tb_label;
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	if (av1394_it_dump_ixl) {
3537c478bd9Sstevel@tonic-gate 		av1394_ic_ixl_dump(itp->it_ixlp);
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	return (ret);
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate static void
av1394_it_destroy_ixl(av1394_ic_t * icp)3607c478bd9Sstevel@tonic-gate av1394_it_destroy_ixl(av1394_ic_t *icp)
3617c478bd9Sstevel@tonic-gate {
3627c478bd9Sstevel@tonic-gate 	av1394_it_t	*itp = &icp->ic_it;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	av1394_it_ixl_destroy_data(icp);
3657c478bd9Sstevel@tonic-gate 	itp->it_ixlp = NULL;
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate /*
3697c478bd9Sstevel@tonic-gate  * build data transmit part of the IXL chain
3707c478bd9Sstevel@tonic-gate  */
3717c478bd9Sstevel@tonic-gate static int
av1394_it_ixl_bld_data(av1394_ic_t * icp)3727c478bd9Sstevel@tonic-gate av1394_it_ixl_bld_data(av1394_ic_t *icp)
3737c478bd9Sstevel@tonic-gate {
3747c478bd9Sstevel@tonic-gate 	av1394_it_t		*itp = &icp->ic_it;
3757c478bd9Sstevel@tonic-gate 	av1394_isoch_pool_t	*pool = &itp->it_data_pool;
3767c478bd9Sstevel@tonic-gate 	int			total = 0;	/* # of pkts in the chain */
3777c478bd9Sstevel@tonic-gate 	int			nfull = 0;	/* # of full CIPs in a series */
3787c478bd9Sstevel@tonic-gate 	int			framenum = -1;	/* frame number */
3797c478bd9Sstevel@tonic-gate 	int			bufsz_max;	/* max buffer size in pkts */
3807c478bd9Sstevel@tonic-gate 	int			segnum = 0;	/* current segment number */
3817c478bd9Sstevel@tonic-gate 	int			segsz;		/* segment size in pkts */
3827c478bd9Sstevel@tonic-gate 	off_t			segoff = 0;	/* segment offset in pkts */
3837c478bd9Sstevel@tonic-gate 	av1394_it_ixl_empty_cip_t *ep = NULL;	/* last empty CIP */
3847c478bd9Sstevel@tonic-gate 	av1394_it_ixl_buf_t	*bp = NULL;	/* last data buffer */
3857c478bd9Sstevel@tonic-gate 	av1394_it_ixl_buf_t	*prevbp = NULL;
3867c478bd9Sstevel@tonic-gate 	int			a, n, d;	/* N/D algorithm variables */
3877c478bd9Sstevel@tonic-gate 	int			type, ptype;	/* current and prev CIP type */
3887c478bd9Sstevel@tonic-gate 	int			tb_flags;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	itp->it_frame_info = kmem_zalloc(icp->ic_nframes *
39147cd5876SAlan Perry 	    sizeof (av1394_it_frame_info_t), KM_SLEEP);
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	bufsz_max = AV1394_IXL_BUFSZ_MAX / icp->ic_pktsz;
3947c478bd9Sstevel@tonic-gate 	n = icp->ic_param.cp_n;
3957c478bd9Sstevel@tonic-gate 	d = icp->ic_param.cp_d;
3967c478bd9Sstevel@tonic-gate 	/*
3977c478bd9Sstevel@tonic-gate 	 * following assert guarantees no more than one empty CIP in a row,
3987c478bd9Sstevel@tonic-gate 	 * i.e. empty CIPs account for <=50% of all packets.
3997c478bd9Sstevel@tonic-gate 	 * this should be ensured by ioctl argument validation.
4007c478bd9Sstevel@tonic-gate 	 */
4017c478bd9Sstevel@tonic-gate 	ASSERT((n == 0) || (d / n > 1));
4027c478bd9Sstevel@tonic-gate 	/*
4037c478bd9Sstevel@tonic-gate 	 * build the chain. it is hard to precalculate amount of memory
4047c478bd9Sstevel@tonic-gate 	 * needed for the entire chain, so we simply allocate as we go.
4057c478bd9Sstevel@tonic-gate 	 */
4067c478bd9Sstevel@tonic-gate 	ptype = AV1394_CIP_EMPTY;
4077c478bd9Sstevel@tonic-gate 	segsz = pool->ip_seg[0].is_size / icp->ic_pktsz;
4087c478bd9Sstevel@tonic-gate 	a = n;
4097c478bd9Sstevel@tonic-gate 	while (total < icp->ic_nframes * icp->ic_npkts) {
4107c478bd9Sstevel@tonic-gate 		/* insert empty CIPs using N/D algorithm */
4117c478bd9Sstevel@tonic-gate 		a += n;
4127c478bd9Sstevel@tonic-gate 		if (a > d) {
4137c478bd9Sstevel@tonic-gate 			a -= d;
4147c478bd9Sstevel@tonic-gate 			type = AV1394_CIP_EMPTY;
4157c478bd9Sstevel@tonic-gate 		} else {
4167c478bd9Sstevel@tonic-gate 			type = AV1394_CIP_FULL;
4177c478bd9Sstevel@tonic-gate 			nfull++;
4187c478bd9Sstevel@tonic-gate 		}
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 		/*
4217c478bd9Sstevel@tonic-gate 		 * merge series of full packets into single SEND_BUF commands.
4227c478bd9Sstevel@tonic-gate 		 * a series can be terminated by:
4237c478bd9Sstevel@tonic-gate 		 *  - an empty CIP;
4247c478bd9Sstevel@tonic-gate 		 *  - series buffer size reached maximum;
4257c478bd9Sstevel@tonic-gate 		 *  - end of isoch segment;
4267c478bd9Sstevel@tonic-gate 		 *  - end of frame (which is always at the end of segment);
4277c478bd9Sstevel@tonic-gate 		 */
4287c478bd9Sstevel@tonic-gate 		if (((type == AV1394_CIP_EMPTY) || (segoff + nfull == segsz) ||
4297c478bd9Sstevel@tonic-gate 		    (nfull == bufsz_max)) && (nfull > 0)) {
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 			/* build buffer block */
4327c478bd9Sstevel@tonic-gate 			prevbp = bp;
4337c478bd9Sstevel@tonic-gate 			tb_flags = 0;
4347c478bd9Sstevel@tonic-gate 			if (type == AV1394_CIP_EMPTY) {
4357c478bd9Sstevel@tonic-gate 				tb_flags |= AV1394_IT_IXL_BUF_NEXT_EMPTY;
4367c478bd9Sstevel@tonic-gate 			}
4377c478bd9Sstevel@tonic-gate 			if (total % icp->ic_npkts == 0) {
4387c478bd9Sstevel@tonic-gate 				tb_flags |= AV1394_IT_IXL_BUF_SOF;
4397c478bd9Sstevel@tonic-gate 				framenum++;
4407c478bd9Sstevel@tonic-gate 			}
4417c478bd9Sstevel@tonic-gate 			if ((total + nfull) % icp->ic_npkts == 0) {
4427c478bd9Sstevel@tonic-gate 				tb_flags |= AV1394_IT_IXL_BUF_EOF;
4437c478bd9Sstevel@tonic-gate 			}
4447c478bd9Sstevel@tonic-gate 			bp = av1394_it_ixl_bld_buf(icp, nfull, segnum, segoff,
44547cd5876SAlan Perry 			    tb_flags, framenum);
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 			if (itp->it_ixl_data == NULL) {
4487c478bd9Sstevel@tonic-gate 				itp->it_ixl_data = &bp->tb_common;
4497c478bd9Sstevel@tonic-gate 			}
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 			/* complete previous empty CIP or a buffer */
4527c478bd9Sstevel@tonic-gate 			if (ep) {
4537c478bd9Sstevel@tonic-gate 				av1394_it_ixl_complete_empty_cip(ep, bp);
4547c478bd9Sstevel@tonic-gate 				ep = NULL;
4557c478bd9Sstevel@tonic-gate 			} else if (prevbp) {
4567c478bd9Sstevel@tonic-gate 				av1394_it_ixl_complete_buf2(prevbp, bp);
4577c478bd9Sstevel@tonic-gate 			}
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 			/* if current segment is used up, pick next one */
4607c478bd9Sstevel@tonic-gate 			segoff += nfull;
4617c478bd9Sstevel@tonic-gate 			if (segoff == segsz) {
4627c478bd9Sstevel@tonic-gate 				if (++segnum < pool->ip_nsegs) {
4637c478bd9Sstevel@tonic-gate 					segsz = pool->ip_seg[segnum].is_size /
46447cd5876SAlan Perry 					    icp->ic_pktsz;
4657c478bd9Sstevel@tonic-gate 				}
4667c478bd9Sstevel@tonic-gate 				segoff = 0;
4677c478bd9Sstevel@tonic-gate 			}
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 			total += nfull;
4707c478bd9Sstevel@tonic-gate 			nfull = 0;
4717c478bd9Sstevel@tonic-gate 		}
4727c478bd9Sstevel@tonic-gate 		/* insert an empty packet if needed */
4737c478bd9Sstevel@tonic-gate 		if (type == AV1394_CIP_EMPTY) {
4747c478bd9Sstevel@tonic-gate 			ep = av1394_it_ixl_bld_empty_cip(icp, framenum);
4757c478bd9Sstevel@tonic-gate 			av1394_it_ixl_complete_buf(bp, ep);
4767c478bd9Sstevel@tonic-gate 		}
4777c478bd9Sstevel@tonic-gate 		ptype = type;
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 	ASSERT(nfull == 0);
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	/* last packet must be an empty CIP, except when n == 0 */
4827c478bd9Sstevel@tonic-gate 	if (n != 0) {
4837c478bd9Sstevel@tonic-gate 		if (ptype != AV1394_CIP_EMPTY) {
4847c478bd9Sstevel@tonic-gate 			ep = av1394_it_ixl_bld_empty_cip(icp, framenum);
4857c478bd9Sstevel@tonic-gate 			av1394_it_ixl_complete_buf(bp, ep);
4867c478bd9Sstevel@tonic-gate 		}
4877c478bd9Sstevel@tonic-gate 		av1394_it_ixl_complete_empty_cip(ep,
48847cd5876SAlan Perry 		    (av1394_it_ixl_buf_t *)itp->it_ixl_data);
4897c478bd9Sstevel@tonic-gate 		ep->te_jump.next_ixlp = NULL;
4907c478bd9Sstevel@tonic-gate 		ep->te_common.tc_next = NULL;
4917c478bd9Sstevel@tonic-gate 	} else {
4927c478bd9Sstevel@tonic-gate 		bp->tb_jump.label = (ixl1394_command_t *)
49347cd5876SAlan Perry 		    &(((av1394_it_ixl_buf_t *)itp->it_ixl_data)->tb_label);
4947c478bd9Sstevel@tonic-gate 	}
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate static void
av1394_it_ixl_destroy_data(av1394_ic_t * icp)5007c478bd9Sstevel@tonic-gate av1394_it_ixl_destroy_data(av1394_ic_t *icp)
5017c478bd9Sstevel@tonic-gate {
5027c478bd9Sstevel@tonic-gate 	av1394_it_t		*itp = &icp->ic_it;
5037c478bd9Sstevel@tonic-gate 	av1394_it_ixl_common_t	*cmd, *cmd_next;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	for (cmd = itp->it_ixl_data; cmd != NULL; cmd = cmd_next) {
5067c478bd9Sstevel@tonic-gate 		cmd_next = cmd->tc_next;
5077c478bd9Sstevel@tonic-gate 		kmem_free(cmd, cmd->tc_size);
5087c478bd9Sstevel@tonic-gate 	}
5097c478bd9Sstevel@tonic-gate 	itp->it_ixl_data = NULL;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	kmem_free(itp->it_frame_info,
51247cd5876SAlan Perry 	    icp->ic_nframes * sizeof (av1394_it_frame_info_t));
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate static av1394_it_ixl_buf_t *
av1394_it_ixl_bld_buf(av1394_ic_t * icp,int cnt,int segnum,off_t off,int flags,int framenum)5167c478bd9Sstevel@tonic-gate av1394_it_ixl_bld_buf(av1394_ic_t *icp, int cnt, int segnum, off_t off,
5177c478bd9Sstevel@tonic-gate 		int flags, int framenum)
5187c478bd9Sstevel@tonic-gate {
5197c478bd9Sstevel@tonic-gate 	av1394_it_t		*itp = &icp->ic_it;
5207c478bd9Sstevel@tonic-gate 	av1394_isoch_seg_t	*isp = &itp->it_data_pool.ip_seg[segnum];
5217c478bd9Sstevel@tonic-gate 	av1394_it_ixl_buf_t	*bp;
5227c478bd9Sstevel@tonic-gate 	int			pktsz = icp->ic_pktsz;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	bp = kmem_zalloc(sizeof (av1394_it_ixl_buf_t), KM_SLEEP);
5257c478bd9Sstevel@tonic-gate 	bp->tb_common.tc_size = sizeof (av1394_it_ixl_buf_t);
5267c478bd9Sstevel@tonic-gate 	/* tc_next later */
5277c478bd9Sstevel@tonic-gate 	bp->tb_flags = flags;
5287c478bd9Sstevel@tonic-gate 	bp->tb_framenum = framenum;
5297c478bd9Sstevel@tonic-gate 	bp->tb_icp = icp;
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	bp->tb_label.ixl_opcode = IXL1394_OP_LABEL;
5327c478bd9Sstevel@tonic-gate 	bp->tb_label.next_ixlp = (ixl1394_command_t *)&bp->tb_buf;
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	bp->tb_buf.ixl_opcode = IXL1394_OP_SEND_BUF;
5357c478bd9Sstevel@tonic-gate 	bp->tb_buf.pkt_size = pktsz;
5367c478bd9Sstevel@tonic-gate 	bp->tb_buf.size = cnt * pktsz;
5377c478bd9Sstevel@tonic-gate 	bp->tb_buf.ixl_buf._dmac_ll =
53847cd5876SAlan Perry 	    isp->is_dma_cookie[0].dmac_laddress + off * pktsz;
5397c478bd9Sstevel@tonic-gate 	bp->tb_buf.mem_bufp = isp->is_kaddr + off * pktsz;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	if (flags & AV1394_IT_IXL_BUF_EOF) {
5427c478bd9Sstevel@tonic-gate 		bp->tb_buf.next_ixlp = (ixl1394_command_t *)&bp->tb_store_ts;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 		bp->tb_store_ts.ixl_opcode = IXL1394_OP_STORE_TIMESTAMP;
5457c478bd9Sstevel@tonic-gate 		bp->tb_store_ts.next_ixlp = (ixl1394_command_t *)&bp->tb_cb;
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 		bp->tb_cb.ixl_opcode = IXL1394_OP_CALLBACK;
5487c478bd9Sstevel@tonic-gate 		bp->tb_cb.callback = av1394_it_ixl_buf_cb;
5497c478bd9Sstevel@tonic-gate 		bp->tb_cb.callback_arg = bp;
5507c478bd9Sstevel@tonic-gate 		bp->tb_cb.next_ixlp = (ixl1394_command_t *)&bp->tb_jump;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 		bp->tb_jump.ixl_opcode = IXL1394_OP_JUMP_U;
5537c478bd9Sstevel@tonic-gate 	} else {
5547c478bd9Sstevel@tonic-gate 		bp->tb_buf.next_ixlp = (ixl1394_command_t *)&bp->tb_jump;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 		bp->tb_jump.ixl_opcode = IXL1394_OP_JUMP;
5577c478bd9Sstevel@tonic-gate 	}
5587c478bd9Sstevel@tonic-gate 	/*
5597c478bd9Sstevel@tonic-gate 	 * jump label and next_ixlp later.
5607c478bd9Sstevel@tonic-gate 	 * unset fields will be set in av1394_it_ixl_complete_buf()
5617c478bd9Sstevel@tonic-gate 	 *
5627c478bd9Sstevel@tonic-gate 	 * save additional frame info
5637c478bd9Sstevel@tonic-gate 	 */
5647c478bd9Sstevel@tonic-gate 	if (flags & AV1394_IT_IXL_BUF_SOF) {
5657c478bd9Sstevel@tonic-gate 		itp->it_frame_info[framenum].fi_first_buf = bp;
5667c478bd9Sstevel@tonic-gate 		itp->it_frame_info[framenum].fi_ts_off = bp->tb_buf.mem_bufp +
56747cd5876SAlan Perry 		    AV1394_TS_MODE_GET_OFF(icp->ic_param.cp_ts_mode);
5687c478bd9Sstevel@tonic-gate 	} else if (flags & AV1394_IT_IXL_BUF_EOF) {
5697c478bd9Sstevel@tonic-gate 		itp->it_frame_info[framenum].fi_last_buf = bp;
5707c478bd9Sstevel@tonic-gate 	}
5717c478bd9Sstevel@tonic-gate 	itp->it_frame_info[framenum].fi_ncycs += cnt;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	return (bp);
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate static void
av1394_it_ixl_complete_buf(av1394_it_ixl_buf_t * bp,av1394_it_ixl_empty_cip_t * ep)5777c478bd9Sstevel@tonic-gate av1394_it_ixl_complete_buf(av1394_it_ixl_buf_t *bp,
5787c478bd9Sstevel@tonic-gate 	av1394_it_ixl_empty_cip_t *ep)
5797c478bd9Sstevel@tonic-gate {
5807c478bd9Sstevel@tonic-gate 	bp->tb_common.tc_next = &ep->te_common;
5817c478bd9Sstevel@tonic-gate 	bp->tb_jump.label = bp->tb_jump.next_ixlp =
58247cd5876SAlan Perry 	    (ixl1394_command_t *)&ep->te_label;
5837c478bd9Sstevel@tonic-gate }
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate static void
av1394_it_ixl_complete_buf2(av1394_it_ixl_buf_t * bp,av1394_it_ixl_buf_t * nextbp)5867c478bd9Sstevel@tonic-gate av1394_it_ixl_complete_buf2(av1394_it_ixl_buf_t *bp,
5877c478bd9Sstevel@tonic-gate 	av1394_it_ixl_buf_t *nextbp)
5887c478bd9Sstevel@tonic-gate {
5897c478bd9Sstevel@tonic-gate 	bp->tb_common.tc_next = &nextbp->tb_common;
5907c478bd9Sstevel@tonic-gate 	bp->tb_jump.label = bp->tb_jump.next_ixlp =
59147cd5876SAlan Perry 	    (ixl1394_command_t *)&nextbp->tb_label;
5927c478bd9Sstevel@tonic-gate }
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate static av1394_it_ixl_empty_cip_t *
av1394_it_ixl_bld_empty_cip(av1394_ic_t * icp,int framenum)5957c478bd9Sstevel@tonic-gate av1394_it_ixl_bld_empty_cip(av1394_ic_t *icp, int framenum)
5967c478bd9Sstevel@tonic-gate {
5977c478bd9Sstevel@tonic-gate 	av1394_it_t	*itp = &icp->ic_it;
5987c478bd9Sstevel@tonic-gate 	av1394_it_ixl_empty_cip_t *ep;
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	ep = kmem_zalloc(sizeof (av1394_it_ixl_empty_cip_t), KM_SLEEP);
6017c478bd9Sstevel@tonic-gate 	ep->te_common.tc_size = sizeof (av1394_it_ixl_empty_cip_t);
6027c478bd9Sstevel@tonic-gate 	/* tc_next later */
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	ep->te_label.ixl_opcode = IXL1394_OP_LABEL;
6057c478bd9Sstevel@tonic-gate 	ep->te_label.next_ixlp = (ixl1394_command_t *)&ep->te_pkt;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	ep->te_pkt.ixl_opcode = IXL1394_OP_SEND_PKT_ST;
6087c478bd9Sstevel@tonic-gate 	ep->te_pkt.size = AV1394_CIPSZ;
6097c478bd9Sstevel@tonic-gate 	/* ixl_buf and mem_bufp later */
6107c478bd9Sstevel@tonic-gate 	ep->te_pkt.next_ixlp = (ixl1394_command_t *)&ep->te_jump;
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	ep->te_jump.ixl_opcode = IXL1394_OP_JUMP;
6137c478bd9Sstevel@tonic-gate 	/*
6147c478bd9Sstevel@tonic-gate 	 * label and next_ixlp later.
6157c478bd9Sstevel@tonic-gate 	 * unset fields will be set in av1394_it_ixl_complete_empty_cip()
6167c478bd9Sstevel@tonic-gate 	 */
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	itp->it_frame_info[framenum].fi_ncycs++;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	return (ep);
6217c478bd9Sstevel@tonic-gate }
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate /*
6247c478bd9Sstevel@tonic-gate  * empty CIP packet contains CIP header of the next packet,
6257c478bd9Sstevel@tonic-gate  * so we just point to the same address as the next packet's header
6267c478bd9Sstevel@tonic-gate  */
6277c478bd9Sstevel@tonic-gate static void
av1394_it_ixl_complete_empty_cip(av1394_it_ixl_empty_cip_t * ep,av1394_it_ixl_buf_t * bp)6287c478bd9Sstevel@tonic-gate av1394_it_ixl_complete_empty_cip(av1394_it_ixl_empty_cip_t *ep,
6297c478bd9Sstevel@tonic-gate 	av1394_it_ixl_buf_t *bp)
6307c478bd9Sstevel@tonic-gate {
6317c478bd9Sstevel@tonic-gate 	ep->te_common.tc_next = &bp->tb_common;
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	ep->te_pkt.ixl_buf._dmac_ll = bp->tb_buf.ixl_buf._dmac_ll;
6347c478bd9Sstevel@tonic-gate 	ep->te_pkt.mem_bufp = bp->tb_buf.mem_bufp;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	ep->te_jump.label = ep->te_jump.next_ixlp =
63747cd5876SAlan Perry 	    (ixl1394_command_t *)&bp->tb_label;
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate static void
av1394_it_ixl_bld_begin(av1394_ic_t * icp)6417c478bd9Sstevel@tonic-gate av1394_it_ixl_bld_begin(av1394_ic_t *icp)
6427c478bd9Sstevel@tonic-gate {
6437c478bd9Sstevel@tonic-gate 	av1394_it_t		*itp = &icp->ic_it;
6447c478bd9Sstevel@tonic-gate 	av1394_it_ixl_buf_t	*bp = (av1394_it_ixl_buf_t *)itp->it_ixl_data;
6457c478bd9Sstevel@tonic-gate 	av1394_it_ixl_begin_t	*bep = &itp->it_ixl_begin;
6467c478bd9Sstevel@tonic-gate 	int			i;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	bep->be_label.ixl_opcode = IXL1394_OP_LABEL;
6497c478bd9Sstevel@tonic-gate 	bep->be_label.next_ixlp = (ixl1394_command_t *)&bep->be_empty_pre;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	bep->be_empty_pre.ixl_opcode = IXL1394_OP_SEND_PKT_ST;
6527c478bd9Sstevel@tonic-gate 	bep->be_empty_pre.size = AV1394_CIPSZ;
6537c478bd9Sstevel@tonic-gate 	bep->be_empty_pre.ixl_buf._dmac_ll = bp->tb_buf.ixl_buf._dmac_ll;
6547c478bd9Sstevel@tonic-gate 	bep->be_empty_pre.mem_bufp = bp->tb_buf.mem_bufp;
6557c478bd9Sstevel@tonic-gate 	bep->be_empty_pre.next_ixlp = (ixl1394_command_t *)&bep->be_store_ts;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	bep->be_store_ts.ixl_opcode = IXL1394_OP_STORE_TIMESTAMP;
6587c478bd9Sstevel@tonic-gate 	bep->be_store_ts.next_ixlp = (ixl1394_command_t *)&bep->be_cb;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	bep->be_cb.ixl_opcode = IXL1394_OP_CALLBACK;
6617c478bd9Sstevel@tonic-gate 	bep->be_cb.callback = av1394_it_ixl_begin_cb;
6627c478bd9Sstevel@tonic-gate 	bep->be_cb.callback_arg = &bep->be_store_ts.timestamp;
6637c478bd9Sstevel@tonic-gate 	bep->be_cb.next_ixlp = (ixl1394_command_t *)&bep->be_empty_post[0];
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	for (i = 0; i < AV1394_IT_IXL_BEGIN_NPOST; i++) {
6667c478bd9Sstevel@tonic-gate 		bep->be_empty_post[i].ixl_opcode = IXL1394_OP_SEND_PKT_ST;
6677c478bd9Sstevel@tonic-gate 		bep->be_empty_post[i].size = AV1394_CIPSZ;
6687c478bd9Sstevel@tonic-gate 		bep->be_empty_post[i].ixl_buf._dmac_ll =
66947cd5876SAlan Perry 		    bp->tb_buf.ixl_buf._dmac_ll;
6707c478bd9Sstevel@tonic-gate 		bep->be_empty_post[i].mem_bufp = bp->tb_buf.mem_bufp;
6717c478bd9Sstevel@tonic-gate 		bep->be_empty_post[i].next_ixlp =
67247cd5876SAlan Perry 		    (ixl1394_command_t *)&bep->be_empty_post[i + 1];
6737c478bd9Sstevel@tonic-gate 	}
6747c478bd9Sstevel@tonic-gate 	bep->be_empty_post[AV1394_IT_IXL_BEGIN_NPOST - 1].next_ixlp =
67547cd5876SAlan Perry 	    (ixl1394_command_t *)&bep->be_jump;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	bep->be_jump.ixl_opcode = IXL1394_OP_JUMP_U;
6787c478bd9Sstevel@tonic-gate 	bep->be_jump.label = (ixl1394_command_t *)&bp->tb_label;
6797c478bd9Sstevel@tonic-gate 	bep->be_jump.next_ixlp = (ixl1394_command_t *)&bp->tb_label;
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate static void
av1394_it_ixl_begin_update_pkts(av1394_ic_t * icp,av1394_it_ixl_buf_t * bp)6837c478bd9Sstevel@tonic-gate av1394_it_ixl_begin_update_pkts(av1394_ic_t *icp, av1394_it_ixl_buf_t *bp)
6847c478bd9Sstevel@tonic-gate {
6857c478bd9Sstevel@tonic-gate 	av1394_it_t		*itp = &icp->ic_it;
6867c478bd9Sstevel@tonic-gate 	av1394_it_ixl_begin_t	*bep = &itp->it_ixl_begin;
6877c478bd9Sstevel@tonic-gate 	int			i;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	for (i = 0; i < AV1394_IT_IXL_BEGIN_NPOST; i++) {
6907c478bd9Sstevel@tonic-gate 		bep->be_empty_post[i].ixl_buf._dmac_ll =
69147cd5876SAlan Perry 		    bp->tb_buf.ixl_buf._dmac_ll;
6927c478bd9Sstevel@tonic-gate 		bep->be_empty_post[i].mem_bufp = bp->tb_buf.mem_bufp;
6937c478bd9Sstevel@tonic-gate 	}
6947c478bd9Sstevel@tonic-gate }
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate static int
av1394_it_alloc_isoch_dma(av1394_ic_t * icp)6977c478bd9Sstevel@tonic-gate av1394_it_alloc_isoch_dma(av1394_ic_t *icp)
6987c478bd9Sstevel@tonic-gate {
6997c478bd9Sstevel@tonic-gate 	av1394_inst_t		*avp = icp->ic_avp;
7007c478bd9Sstevel@tonic-gate 	av1394_it_t		*itp = &icp->ic_it;
7017c478bd9Sstevel@tonic-gate 	id1394_isoch_dmainfo_t	di;
7027c478bd9Sstevel@tonic-gate 	int			result;
7037c478bd9Sstevel@tonic-gate 	int			ret;
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	di.ixlp = itp->it_ixlp;
7067c478bd9Sstevel@tonic-gate 	di.channel_num = icp->ic_num;
7077c478bd9Sstevel@tonic-gate 	di.idma_options = ID1394_TALK;
7087c478bd9Sstevel@tonic-gate 	di.it_speed = icp->ic_param.cp_bus_speed;
7097c478bd9Sstevel@tonic-gate 	/*
7107c478bd9Sstevel@tonic-gate 	 * XXX this should really be IXL1394_SKIP_TO_NEXT,
7117c478bd9Sstevel@tonic-gate 	 * but it can't be used yet due to the Framework bug
7127c478bd9Sstevel@tonic-gate 	 */
7137c478bd9Sstevel@tonic-gate 	di.it_default_skip = IXL1394_SKIP_TO_SELF;
7147c478bd9Sstevel@tonic-gate 	di.default_tag = 1;
7157c478bd9Sstevel@tonic-gate 	di.default_sync = 0;
7167c478bd9Sstevel@tonic-gate 	di.global_callback_arg = icp;
7177c478bd9Sstevel@tonic-gate 	di.isoch_dma_stopped = av1394_it_dma_stopped_cb;
7187c478bd9Sstevel@tonic-gate 	di.idma_evt_arg = icp;
7197c478bd9Sstevel@tonic-gate 
720*2570281cSToomas Soome 	ret = t1394_alloc_isoch_dma(avp->av_t1394_hdl, &di, 0,
721*2570281cSToomas Soome 	    &icp->ic_isoch_hdl, &result);
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	return (ret);
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate static void
av1394_it_free_isoch_dma(av1394_ic_t * icp)7277c478bd9Sstevel@tonic-gate av1394_it_free_isoch_dma(av1394_ic_t *icp)
7287c478bd9Sstevel@tonic-gate {
7297c478bd9Sstevel@tonic-gate 	av1394_inst_t		*avp = icp->ic_avp;
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	t1394_free_isoch_dma(avp->av_t1394_hdl, 0, &icp->ic_isoch_hdl);
7327c478bd9Sstevel@tonic-gate }
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate static void
av1394_it_dma_sync_frames(av1394_ic_t * icp,int idx,int cnt)7357c478bd9Sstevel@tonic-gate av1394_it_dma_sync_frames(av1394_ic_t *icp, int idx, int cnt)
7367c478bd9Sstevel@tonic-gate {
7377c478bd9Sstevel@tonic-gate 	av1394_ic_dma_sync_frames(icp, idx, cnt,
73847cd5876SAlan Perry 	    &icp->ic_it.it_data_pool, DDI_DMA_SYNC_FORDEV);
7397c478bd9Sstevel@tonic-gate }
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate /*
7427c478bd9Sstevel@tonic-gate  *
7437c478bd9Sstevel@tonic-gate  * --- callbacks
7447c478bd9Sstevel@tonic-gate  *
7457c478bd9Sstevel@tonic-gate  */
7467c478bd9Sstevel@tonic-gate static void
av1394_it_ixl_begin_cb(opaque_t arg,struct ixl1394_callback * cb)7477c478bd9Sstevel@tonic-gate av1394_it_ixl_begin_cb(opaque_t arg, struct ixl1394_callback *cb)
7487c478bd9Sstevel@tonic-gate {
7497c478bd9Sstevel@tonic-gate 	av1394_ic_t	*icp = arg;
7507c478bd9Sstevel@tonic-gate 	av1394_it_t	*itp = &icp->ic_it;
7517c478bd9Sstevel@tonic-gate 	uint16_t	*cycp = cb->callback_arg; /* cycle timestamp pointer */
7527c478bd9Sstevel@tonic-gate 	uint16_t	syt;
7537c478bd9Sstevel@tonic-gate 	int		first;
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	mutex_enter(&icp->ic_mutex);
7567c478bd9Sstevel@tonic-gate 	/* save initial timestamp value */
7577c478bd9Sstevel@tonic-gate 	itp->it_ts_init.ts_syt = av1394_it_ts_cyc2syt(*cycp);
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	/*
7607c478bd9Sstevel@tonic-gate 	 * update frame timestamps if needed
7617c478bd9Sstevel@tonic-gate 	 */
7627c478bd9Sstevel@tonic-gate 	if ((itp->it_nfull <= 0) ||
7637c478bd9Sstevel@tonic-gate 	    (AV1394_TS_MODE_GET_SIZE(icp->ic_param.cp_ts_mode) == 0)) {
7647c478bd9Sstevel@tonic-gate 		mutex_exit(&icp->ic_mutex);
7657c478bd9Sstevel@tonic-gate 		return;
7667c478bd9Sstevel@tonic-gate 	}
7677c478bd9Sstevel@tonic-gate 	ASSERT(itp->it_nfull <= icp->ic_nframes);
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	syt = av1394_it_ts_syt_inc(itp->it_ts_init.ts_syt,
77047cd5876SAlan Perry 	    AV1394_IT_IXL_BEGIN_NPOST + av1394_it_syt_off);
7717c478bd9Sstevel@tonic-gate 	first = (itp->it_last_full + icp->ic_nframes - itp->it_nfull + 1) %
77247cd5876SAlan Perry 	    icp->ic_nframes;
7737c478bd9Sstevel@tonic-gate 	av1394_it_update_frame_syt(icp, first, itp->it_nfull, syt);
7747c478bd9Sstevel@tonic-gate 	mutex_exit(&icp->ic_mutex);
7757c478bd9Sstevel@tonic-gate }
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7787c478bd9Sstevel@tonic-gate static void
av1394_it_ixl_buf_cb(opaque_t arg,struct ixl1394_callback * cb)7797c478bd9Sstevel@tonic-gate av1394_it_ixl_buf_cb(opaque_t arg, struct ixl1394_callback *cb)
7807c478bd9Sstevel@tonic-gate {
7817c478bd9Sstevel@tonic-gate 	av1394_it_ixl_buf_t	*bp = cb->callback_arg;
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	if (bp->tb_flags & AV1394_IT_IXL_BUF_EOF) {
7847c478bd9Sstevel@tonic-gate 		av1394_it_ixl_eof_cb(bp);
7857c478bd9Sstevel@tonic-gate 	}
7867c478bd9Sstevel@tonic-gate }
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate static void
av1394_it_ixl_eof_cb(av1394_it_ixl_buf_t * bp)7897c478bd9Sstevel@tonic-gate av1394_it_ixl_eof_cb(av1394_it_ixl_buf_t *bp)
7907c478bd9Sstevel@tonic-gate {
7917c478bd9Sstevel@tonic-gate 	av1394_ic_t	*icp = bp->tb_icp;
7927c478bd9Sstevel@tonic-gate 	av1394_isoch_t	*ip = &icp->ic_avp->av_i;
7937c478bd9Sstevel@tonic-gate 	av1394_it_t	*itp = &icp->ic_it;
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	mutex_enter(&ip->i_mutex);
7967c478bd9Sstevel@tonic-gate 	mutex_enter(&icp->ic_mutex);
7977c478bd9Sstevel@tonic-gate 	if (itp->it_nempty < icp->ic_nframes) {
7987c478bd9Sstevel@tonic-gate 		itp->it_nempty++;
7997c478bd9Sstevel@tonic-gate 		itp->it_nfull--;
8007c478bd9Sstevel@tonic-gate 		cv_signal(&icp->ic_xfer_cv);
8017c478bd9Sstevel@tonic-gate 	}
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	if ((itp->it_nempty >= itp->it_hiwat) &&
8047c478bd9Sstevel@tonic-gate 	    (icp->ic_state == AV1394_IC_DMA)) {
8057c478bd9Sstevel@tonic-gate 		av1394_ic_trigger_softintr(icp, icp->ic_num,
80647cd5876SAlan Perry 		    AV1394_PREQ_IT_UNDERRUN);
8077c478bd9Sstevel@tonic-gate 	}
8087c478bd9Sstevel@tonic-gate 	mutex_exit(&icp->ic_mutex);
8097c478bd9Sstevel@tonic-gate 	mutex_exit(&ip->i_mutex);
8107c478bd9Sstevel@tonic-gate }
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate void
av1394_it_underrun(av1394_ic_t * icp)8137c478bd9Sstevel@tonic-gate av1394_it_underrun(av1394_ic_t *icp)
8147c478bd9Sstevel@tonic-gate {
8157c478bd9Sstevel@tonic-gate 	av1394_it_t		*itp = &icp->ic_it;
8167c478bd9Sstevel@tonic-gate 	av1394_inst_t		*avp = icp->ic_avp;
8177c478bd9Sstevel@tonic-gate 	int			idx;
8187c478bd9Sstevel@tonic-gate 	ixl1394_jump_t		*old_jmp;
8197c478bd9Sstevel@tonic-gate 	ixl1394_jump_t		new_jmp;
8207c478bd9Sstevel@tonic-gate 	id1394_isoch_dma_updateinfo_t update_info;
8217c478bd9Sstevel@tonic-gate 	int			err;
8227c478bd9Sstevel@tonic-gate 	int			result;
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	/*
8257c478bd9Sstevel@tonic-gate 	 * update the last full frame's jump to NULL
8267c478bd9Sstevel@tonic-gate 	 */
8277c478bd9Sstevel@tonic-gate 	idx = (itp->it_first_empty + icp->ic_nframes - 1) % icp->ic_nframes;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	old_jmp = &itp->it_frame_info[idx].fi_last_buf->tb_jump;
8307c478bd9Sstevel@tonic-gate 	itp->it_saved_label = old_jmp->label;
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	new_jmp.ixl_opcode = IXL1394_OP_JUMP_U;
8337c478bd9Sstevel@tonic-gate 	new_jmp.label = NULL;
8347c478bd9Sstevel@tonic-gate 	new_jmp.next_ixlp = NULL;
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	update_info.orig_ixlp = (ixl1394_command_t *)old_jmp;
8377c478bd9Sstevel@tonic-gate 	update_info.temp_ixlp = (ixl1394_command_t *)&new_jmp;
8387c478bd9Sstevel@tonic-gate 	update_info.ixl_count = 1;
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	mutex_exit(&icp->ic_mutex);
8417c478bd9Sstevel@tonic-gate 	err = t1394_update_isoch_dma(avp->av_t1394_hdl,
8427c478bd9Sstevel@tonic-gate 	    icp->ic_isoch_hdl, &update_info, 0, &result);
8437c478bd9Sstevel@tonic-gate 	mutex_enter(&icp->ic_mutex);
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	if (err == DDI_SUCCESS) {
8467c478bd9Sstevel@tonic-gate 		itp->it_underrun_idx = idx;
8477c478bd9Sstevel@tonic-gate 		icp->ic_state = AV1394_IC_SUSPENDED;
8487c478bd9Sstevel@tonic-gate 		cv_signal(&icp->ic_xfer_cv);
8497c478bd9Sstevel@tonic-gate 	}
8507c478bd9Sstevel@tonic-gate }
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate /*
8537c478bd9Sstevel@tonic-gate  * resume from the underrun condition
8547c478bd9Sstevel@tonic-gate  */
8557c478bd9Sstevel@tonic-gate static int
av1394_it_underrun_resume(av1394_ic_t * icp)8567c478bd9Sstevel@tonic-gate av1394_it_underrun_resume(av1394_ic_t *icp)
8577c478bd9Sstevel@tonic-gate {
8587c478bd9Sstevel@tonic-gate 	av1394_it_t		*itp = &icp->ic_it;
8597c478bd9Sstevel@tonic-gate 	av1394_inst_t		*avp = icp->ic_avp;
8607c478bd9Sstevel@tonic-gate 	av1394_it_ixl_buf_t	*bp;
8617c478bd9Sstevel@tonic-gate 	int			idx;
8627c478bd9Sstevel@tonic-gate 	ixl1394_jump_t		*old_jmp;
8637c478bd9Sstevel@tonic-gate 	ixl1394_jump_t		new_jmp;
8647c478bd9Sstevel@tonic-gate 	id1394_isoch_dma_updateinfo_t update_info;
8657c478bd9Sstevel@tonic-gate 	int			err;
8667c478bd9Sstevel@tonic-gate 	int			result;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	/*
8697c478bd9Sstevel@tonic-gate 	 * resuming the transfer it a lot like starting the transfer:
8707c478bd9Sstevel@tonic-gate 	 * first the IXL begin block needs to be executed, then the rest
8717c478bd9Sstevel@tonic-gate 	 * of the IXL chain. The following dynamic updates are needed:
8727c478bd9Sstevel@tonic-gate 	 *
8737c478bd9Sstevel@tonic-gate 	 *  1. update the begin block to jump to the first empty frame;
8747c478bd9Sstevel@tonic-gate 	 *  2. restore the original jump label which we previously
8757c478bd9Sstevel@tonic-gate 	 *    changed to jump to the underrun block;
8767c478bd9Sstevel@tonic-gate 	 *
8777c478bd9Sstevel@tonic-gate 	 * update #1
8787c478bd9Sstevel@tonic-gate 	 *   start by updating the begin block with a new buffer address
8797c478bd9Sstevel@tonic-gate 	 */
8807c478bd9Sstevel@tonic-gate 	idx = (itp->it_underrun_idx + 1) % icp->ic_nframes;
8817c478bd9Sstevel@tonic-gate 	bp = itp->it_frame_info[idx].fi_first_buf;
8827c478bd9Sstevel@tonic-gate 	av1394_it_ixl_begin_update_pkts(icp, bp);
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	old_jmp = &itp->it_ixl_begin.be_jump;
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	new_jmp.ixl_opcode = IXL1394_OP_JUMP_U;
8877c478bd9Sstevel@tonic-gate 	new_jmp.label = (ixl1394_command_t *)&bp->tb_label;
8887c478bd9Sstevel@tonic-gate 	new_jmp.next_ixlp = NULL;
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	update_info.orig_ixlp = (ixl1394_command_t *)old_jmp;
8917c478bd9Sstevel@tonic-gate 	update_info.temp_ixlp = (ixl1394_command_t *)&new_jmp;
8927c478bd9Sstevel@tonic-gate 	update_info.ixl_count = 1;
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	mutex_exit(&icp->ic_mutex);
8957c478bd9Sstevel@tonic-gate 	err = t1394_update_isoch_dma(avp->av_t1394_hdl,
8967c478bd9Sstevel@tonic-gate 	    icp->ic_isoch_hdl, &update_info, 0, &result);
8977c478bd9Sstevel@tonic-gate 	mutex_enter(&icp->ic_mutex);
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	if (err != DDI_SUCCESS) {
9007c478bd9Sstevel@tonic-gate 		return (EIO);
9017c478bd9Sstevel@tonic-gate 	}
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	/*
9047c478bd9Sstevel@tonic-gate 	 * update #2
9057c478bd9Sstevel@tonic-gate 	 */
9067c478bd9Sstevel@tonic-gate 	bp = itp->it_frame_info[itp->it_underrun_idx].fi_last_buf;
9077c478bd9Sstevel@tonic-gate 	old_jmp = &bp->tb_jump;
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	new_jmp.ixl_opcode = IXL1394_OP_JUMP_U;
9107c478bd9Sstevel@tonic-gate 	new_jmp.label = itp->it_saved_label;
9117c478bd9Sstevel@tonic-gate 	new_jmp.next_ixlp = NULL;
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	update_info.orig_ixlp = (ixl1394_command_t *)old_jmp;
9147c478bd9Sstevel@tonic-gate 	update_info.temp_ixlp = (ixl1394_command_t *)&new_jmp;
9157c478bd9Sstevel@tonic-gate 	update_info.ixl_count = 1;
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	mutex_exit(&icp->ic_mutex);
9187c478bd9Sstevel@tonic-gate 	err = t1394_update_isoch_dma(avp->av_t1394_hdl,
9197c478bd9Sstevel@tonic-gate 	    icp->ic_isoch_hdl, &update_info, 0, &result);
9207c478bd9Sstevel@tonic-gate 	mutex_enter(&icp->ic_mutex);
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	if (err != DDI_SUCCESS) {
9237c478bd9Sstevel@tonic-gate 		return (EIO);
9247c478bd9Sstevel@tonic-gate 	}
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	icp->ic_state = AV1394_IC_DMA;
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	return (0);
9297c478bd9Sstevel@tonic-gate }
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9327c478bd9Sstevel@tonic-gate static void
av1394_it_dma_stopped_cb(t1394_isoch_dma_handle_t t1394_idma_hdl,opaque_t idma_evt_arg,id1394_isoch_dma_stopped_t status)9337c478bd9Sstevel@tonic-gate av1394_it_dma_stopped_cb(t1394_isoch_dma_handle_t t1394_idma_hdl,
9347c478bd9Sstevel@tonic-gate 	opaque_t idma_evt_arg, id1394_isoch_dma_stopped_t status)
9357c478bd9Sstevel@tonic-gate {
9367c478bd9Sstevel@tonic-gate 	av1394_ic_t	*icp = idma_evt_arg;
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	mutex_enter(&icp->ic_mutex);
9397c478bd9Sstevel@tonic-gate 	icp->ic_state = AV1394_IC_IDLE;
9407c478bd9Sstevel@tonic-gate 	mutex_exit(&icp->ic_mutex);
9417c478bd9Sstevel@tonic-gate }
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate /*
9457c478bd9Sstevel@tonic-gate  *
9467c478bd9Sstevel@tonic-gate  * --- data transfer routines
9477c478bd9Sstevel@tonic-gate  *
9487c478bd9Sstevel@tonic-gate  * av1394_it_add_frames()
9497c478bd9Sstevel@tonic-gate  *    Add full frames to the pool.
9507c478bd9Sstevel@tonic-gate  */
9517c478bd9Sstevel@tonic-gate static int
av1394_it_add_frames(av1394_ic_t * icp,int idx,int cnt)9527c478bd9Sstevel@tonic-gate av1394_it_add_frames(av1394_ic_t *icp, int idx, int cnt)
9537c478bd9Sstevel@tonic-gate {
9547c478bd9Sstevel@tonic-gate 	av1394_it_t	*itp = &icp->ic_it;
9557c478bd9Sstevel@tonic-gate 	av1394_it_frame_info_t *fip;
9567c478bd9Sstevel@tonic-gate 	int		prev_full = itp->it_last_full;
9577c478bd9Sstevel@tonic-gate 	uint16_t	syt;
9587c478bd9Sstevel@tonic-gate 	int		ret = 0;
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	/* can only add to tail */
9617c478bd9Sstevel@tonic-gate 	if (idx != ((itp->it_last_full + 1) % icp->ic_nframes)) {
9627c478bd9Sstevel@tonic-gate 		return (EINVAL);
9637c478bd9Sstevel@tonic-gate 	}
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	/* turn empty frames into full ones */
9667c478bd9Sstevel@tonic-gate 	itp->it_nempty -= cnt;
9677c478bd9Sstevel@tonic-gate 	itp->it_first_empty = (itp->it_first_empty + cnt) % icp->ic_nframes;
9687c478bd9Sstevel@tonic-gate 	itp->it_nfull += cnt;
9697c478bd9Sstevel@tonic-gate 	itp->it_last_full = (itp->it_last_full + cnt) % icp->ic_nframes;
9707c478bd9Sstevel@tonic-gate 	ASSERT((itp->it_nempty >= 0) && (itp->it_nfull <= icp->ic_nframes));
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	/*
9737c478bd9Sstevel@tonic-gate 	 * update frame timestamps if needed
9747c478bd9Sstevel@tonic-gate 	 */
9757c478bd9Sstevel@tonic-gate 	if (AV1394_TS_MODE_GET_SIZE(icp->ic_param.cp_ts_mode) > 0) {
9767c478bd9Sstevel@tonic-gate 		ASSERT(prev_full >= 0);
9777c478bd9Sstevel@tonic-gate 		fip = &itp->it_frame_info[prev_full];
9787c478bd9Sstevel@tonic-gate 		syt = *(uint16_t *)fip->fi_ts_off;
9797c478bd9Sstevel@tonic-gate 		syt = av1394_it_ts_syt_inc(syt, fip->fi_ncycs);
9807c478bd9Sstevel@tonic-gate 		av1394_it_update_frame_syt(icp, idx, cnt, syt);
9817c478bd9Sstevel@tonic-gate 	}
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 	av1394_it_dma_sync_frames(icp, idx, cnt);
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	/* if suspended due to overrun, check if we can resume */
9867c478bd9Sstevel@tonic-gate 	if ((icp->ic_state == AV1394_IC_SUSPENDED) &&
9877c478bd9Sstevel@tonic-gate 	    (itp->it_nempty >= itp->it_lowat)) {
9887c478bd9Sstevel@tonic-gate 		ret = av1394_it_underrun_resume(icp);
9897c478bd9Sstevel@tonic-gate 	}
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	return (ret);
9927c478bd9Sstevel@tonic-gate }
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate /*
9957c478bd9Sstevel@tonic-gate  * wait for empty frames
9967c478bd9Sstevel@tonic-gate  */
9977c478bd9Sstevel@tonic-gate static int
av1394_it_wait_frames(av1394_ic_t * icp,int * idx,int * cnt,int * nlost)9987c478bd9Sstevel@tonic-gate av1394_it_wait_frames(av1394_ic_t *icp, int *idx, int *cnt, int *nlost)
9997c478bd9Sstevel@tonic-gate {
10007c478bd9Sstevel@tonic-gate 	av1394_it_t	*itp = &icp->ic_it;
10017c478bd9Sstevel@tonic-gate 	int		ret = 0;
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 	while ((itp->it_nempty == 0) && (icp->ic_state == AV1394_IC_DMA)) {
10047c478bd9Sstevel@tonic-gate 		if (cv_wait_sig(&icp->ic_xfer_cv, &icp->ic_mutex) <= 0) {
10057c478bd9Sstevel@tonic-gate 			ret = EINTR;
10067c478bd9Sstevel@tonic-gate 			break;
10077c478bd9Sstevel@tonic-gate 		}
10087c478bd9Sstevel@tonic-gate 	}
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	if (itp->it_nempty > 0) {
10117c478bd9Sstevel@tonic-gate 		*idx = itp->it_first_empty;
10127c478bd9Sstevel@tonic-gate 		*cnt = itp->it_nempty;
10137c478bd9Sstevel@tonic-gate 		*nlost = 0;
10147c478bd9Sstevel@tonic-gate 		ret = 0;
10157c478bd9Sstevel@tonic-gate 	}
10167c478bd9Sstevel@tonic-gate 	return (ret);
10177c478bd9Sstevel@tonic-gate }
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate /*
10207c478bd9Sstevel@tonic-gate  * update frame timestamps for a range of frames
10217c478bd9Sstevel@tonic-gate  */
10227c478bd9Sstevel@tonic-gate static void
av1394_it_update_frame_syt(av1394_ic_t * icp,int first,int cnt,uint16_t syt)10237c478bd9Sstevel@tonic-gate av1394_it_update_frame_syt(av1394_ic_t *icp, int first, int cnt, uint16_t syt)
10247c478bd9Sstevel@tonic-gate {
10257c478bd9Sstevel@tonic-gate 	av1394_it_t	*itp = &icp->ic_it;
10267c478bd9Sstevel@tonic-gate 	int		i;
10277c478bd9Sstevel@tonic-gate 	int		j = first;	/* frame number */
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 	for (i = cnt; i > 0; i--) {
10307c478bd9Sstevel@tonic-gate 		*(uint16_t *)itp->it_frame_info[j].fi_ts_off = syt;
10317c478bd9Sstevel@tonic-gate 		syt = av1394_it_ts_syt_inc(syt, itp->it_frame_info[j].fi_ncycs);
10327c478bd9Sstevel@tonic-gate 		j = (j + 1) % icp->ic_nframes;
10337c478bd9Sstevel@tonic-gate 	}
10347c478bd9Sstevel@tonic-gate }
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate /*
10377c478bd9Sstevel@tonic-gate  * convert cycle timestamp into SYT timestamp:
10387c478bd9Sstevel@tonic-gate  *
10397c478bd9Sstevel@tonic-gate  * Cycle timer:          cycleSeconds         cycleCount     cycleOffset
10407c478bd9Sstevel@tonic-gate  *                   31_30_29_28_27_26_25  24___15_14_13_12  11________0
10417c478bd9Sstevel@tonic-gate  * Cycle timestamp:              |------------------------|
10427c478bd9Sstevel@tonic-gate  * SYT timestamp:                               |----------------------|
10437c478bd9Sstevel@tonic-gate  */
10447c478bd9Sstevel@tonic-gate static uint16_t
av1394_it_ts_cyc2syt(uint16_t cyc)10457c478bd9Sstevel@tonic-gate av1394_it_ts_cyc2syt(uint16_t cyc)
10467c478bd9Sstevel@tonic-gate {
10477c478bd9Sstevel@tonic-gate 	return (((cyc & 0xF) << 12) + 0x800);
10487c478bd9Sstevel@tonic-gate }
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate /*
10517c478bd9Sstevel@tonic-gate  * increment SYT by a number of cycles
10527c478bd9Sstevel@tonic-gate  */
10537c478bd9Sstevel@tonic-gate static uint16_t
av1394_it_ts_syt_inc(uint16_t syt,uint16_t ncycs)10547c478bd9Sstevel@tonic-gate av1394_it_ts_syt_inc(uint16_t syt, uint16_t ncycs)
10557c478bd9Sstevel@tonic-gate {
10567c478bd9Sstevel@tonic-gate 	return (syt + (ncycs << 12));
10577c478bd9Sstevel@tonic-gate }
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate /*
10607c478bd9Sstevel@tonic-gate  * copyin from the kernel buffer
10617c478bd9Sstevel@tonic-gate  */
10627c478bd9Sstevel@tonic-gate static void
av1394_it_kcopyin(av1394_ic_t * icp,void * buf,size_t len)10637c478bd9Sstevel@tonic-gate av1394_it_kcopyin(av1394_ic_t *icp, void *buf, size_t len)
10647c478bd9Sstevel@tonic-gate {
10657c478bd9Sstevel@tonic-gate 	av1394_it_t	*itp = &icp->ic_it;
10667c478bd9Sstevel@tonic-gate 	av1394_isoch_seg_t *seg = itp->it_data_pool.ip_seg;
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	ASSERT(itp->it_write_off + len < icp->ic_framesz);
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	bcopy(buf, seg[itp->it_write_idx].is_kaddr + itp->it_write_off, len);
10717c478bd9Sstevel@tonic-gate 	itp->it_write_off += len;
10727c478bd9Sstevel@tonic-gate }
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate /*
10757c478bd9Sstevel@tonic-gate  * copyin from the user buffer
10767c478bd9Sstevel@tonic-gate  */
10777c478bd9Sstevel@tonic-gate static int
av1394_it_copyin(av1394_ic_t * icp,struct uio * uiop,int * full_cnt,int dv)10787c478bd9Sstevel@tonic-gate av1394_it_copyin(av1394_ic_t *icp, struct uio *uiop, int *full_cnt, int dv)
10797c478bd9Sstevel@tonic-gate {
10807c478bd9Sstevel@tonic-gate 	av1394_it_t	*itp = &icp->ic_it;
10817c478bd9Sstevel@tonic-gate 	av1394_isoch_seg_t *seg = itp->it_data_pool.ip_seg;
10827c478bd9Sstevel@tonic-gate 	int		idx = itp->it_write_idx;
10837c478bd9Sstevel@tonic-gate 	int		framesz = icp->ic_framesz;
10847c478bd9Sstevel@tonic-gate 	size_t		len, frame_resid, start_resid;
10857c478bd9Sstevel@tonic-gate 	caddr_t		kaddr, kaddr_end;
10867c478bd9Sstevel@tonic-gate 	int		ret = 0;
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	*full_cnt = 0;
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	while ((uiop->uio_resid > 0) && (itp->it_write_cnt > 0)) {
10917c478bd9Sstevel@tonic-gate 		kaddr = seg[idx].is_kaddr + itp->it_write_off;
10927c478bd9Sstevel@tonic-gate 		frame_resid = framesz - itp->it_write_off;
10937c478bd9Sstevel@tonic-gate 		len = min(uiop->uio_resid, frame_resid);
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 		mutex_exit(&icp->ic_mutex);
10967c478bd9Sstevel@tonic-gate 		ret = uiomove(kaddr, len, UIO_WRITE, uiop);
10977c478bd9Sstevel@tonic-gate 		mutex_enter(&icp->ic_mutex);
10987c478bd9Sstevel@tonic-gate 		if (ret != 0) {
10997c478bd9Sstevel@tonic-gate 			break;
11007c478bd9Sstevel@tonic-gate 		}
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 		itp->it_write_off += len;
11037c478bd9Sstevel@tonic-gate 		if ((itp->it_write_off == framesz) && dv) {
11047c478bd9Sstevel@tonic-gate 			/*
11057c478bd9Sstevel@tonic-gate 			 * for DV formats, make sure we got a frame start.
11067c478bd9Sstevel@tonic-gate 			 * this is to ensure correct timestamping
11077c478bd9Sstevel@tonic-gate 			 */
11087c478bd9Sstevel@tonic-gate 			kaddr = seg[idx].is_kaddr;
11097c478bd9Sstevel@tonic-gate 			kaddr_end = kaddr + framesz;
11107c478bd9Sstevel@tonic-gate 			while (!av1394_it_is_dv_frame_start(kaddr)) {
11117c478bd9Sstevel@tonic-gate 				kaddr += icp->ic_pktsz;
11127c478bd9Sstevel@tonic-gate 				if (kaddr == kaddr_end) {
11137c478bd9Sstevel@tonic-gate 					break;
11147c478bd9Sstevel@tonic-gate 				}
11157c478bd9Sstevel@tonic-gate 			}
11167c478bd9Sstevel@tonic-gate 			start_resid = kaddr_end - kaddr;
11177c478bd9Sstevel@tonic-gate 			if (start_resid != framesz) {
11187c478bd9Sstevel@tonic-gate 				bcopy(kaddr, seg[idx].is_kaddr, start_resid);
11197c478bd9Sstevel@tonic-gate 				itp->it_write_off = start_resid;
11207c478bd9Sstevel@tonic-gate 			}
11217c478bd9Sstevel@tonic-gate 		}
11227c478bd9Sstevel@tonic-gate 		if (itp->it_write_off == framesz) {
11237c478bd9Sstevel@tonic-gate 			/* for DV formats, reset frame's SYT fields */
11247c478bd9Sstevel@tonic-gate 			if (dv) {
11257c478bd9Sstevel@tonic-gate 				av1394_it_reset_frame_syt(icp, idx);
11267c478bd9Sstevel@tonic-gate 			}
11277c478bd9Sstevel@tonic-gate 			itp->it_write_off = 0;
11287c478bd9Sstevel@tonic-gate 			itp->it_write_cnt--;
11297c478bd9Sstevel@tonic-gate 			idx = (idx + 1) % icp->ic_nframes;
11307c478bd9Sstevel@tonic-gate 			(*full_cnt)++;
11317c478bd9Sstevel@tonic-gate 		}
11327c478bd9Sstevel@tonic-gate 	}
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 	return (ret);
11357c478bd9Sstevel@tonic-gate }
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate /*
11387c478bd9Sstevel@tonic-gate  * check if a packet starts a new DV frame
11397c478bd9Sstevel@tonic-gate  */
11407c478bd9Sstevel@tonic-gate static boolean_t
av1394_it_is_dv_frame_start(caddr_t kaddr)11417c478bd9Sstevel@tonic-gate av1394_it_is_dv_frame_start(caddr_t kaddr)
11427c478bd9Sstevel@tonic-gate {
11437c478bd9Sstevel@tonic-gate 	uint8_t *p = (uint8_t *)kaddr + 8;
11447c478bd9Sstevel@tonic-gate 	/*
11457c478bd9Sstevel@tonic-gate 	 * in the DIF block ID data, which immediately follows CIP header,
11467c478bd9Sstevel@tonic-gate 	 * SCT, Dseq and DBN fields should be zero (Ref: IEC 61834-2, Fig. 66)
11477c478bd9Sstevel@tonic-gate 	 */
11487c478bd9Sstevel@tonic-gate 	return (((p[0] & 0xE0) == 0) && ((p[1] & 0xF0) == 0) && (p[2] == 0));
11497c478bd9Sstevel@tonic-gate }
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate /*
11527c478bd9Sstevel@tonic-gate  * reset all frame's SYT fields
11537c478bd9Sstevel@tonic-gate  */
11547c478bd9Sstevel@tonic-gate static void
av1394_it_reset_frame_syt(av1394_ic_t * icp,int idx)11557c478bd9Sstevel@tonic-gate av1394_it_reset_frame_syt(av1394_ic_t *icp, int idx)
11567c478bd9Sstevel@tonic-gate {
11577c478bd9Sstevel@tonic-gate 	caddr_t		kaddr = icp->ic_it.it_data_pool.ip_seg[idx].is_kaddr;
11587c478bd9Sstevel@tonic-gate 	caddr_t		kaddr_end = kaddr + icp->ic_framesz;
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	kaddr += 6;
11617c478bd9Sstevel@tonic-gate 	while (kaddr < kaddr_end) {
11627c478bd9Sstevel@tonic-gate 		*(uint16_t *)kaddr = 0xFFFF;
11637c478bd9Sstevel@tonic-gate 		kaddr += icp->ic_pktsz;
11647c478bd9Sstevel@tonic-gate 	}
11657c478bd9Sstevel@tonic-gate }
1166