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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * av1394 FCP module
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate #include <sys/stat.h>
317c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
327c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
337c478bd9Sstevel@tonic-gate #include <sys/1394/targets/av1394/av1394_impl.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /* configuration routines */
367c478bd9Sstevel@tonic-gate static int	av1394_fcp_ctl_register(av1394_inst_t *);
377c478bd9Sstevel@tonic-gate static int	av1394_fcp_tgt_register(av1394_inst_t *);
387c478bd9Sstevel@tonic-gate static int	av1394_fcp_ctl_alloc_cmd(av1394_inst_t *);
397c478bd9Sstevel@tonic-gate static void	av1394_fcp_ctl_free_cmd(av1394_inst_t *);
407c478bd9Sstevel@tonic-gate static int	av1394_fcp_tgt_alloc_cmd(av1394_inst_t *);
417c478bd9Sstevel@tonic-gate static void	av1394_fcp_tgt_free_cmd(av1394_inst_t *);
427c478bd9Sstevel@tonic-gate static void	av1394_fcp_cleanup(av1394_inst_t *, int);
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /* FCP write and completion handling */
457c478bd9Sstevel@tonic-gate static int	av1394_fcp_cmd_write_sync(av1394_inst_t *, av1394_fcp_cmd_t *);
467c478bd9Sstevel@tonic-gate static void	av1394_fcp_cmd_completion_cb(struct cmd1394_cmd *);
477c478bd9Sstevel@tonic-gate static int	av1394_fcp_cmd_write_request_cb(cmd1394_cmd_t *);
487c478bd9Sstevel@tonic-gate static int	av1394_fcp_resp_write_request_cb(cmd1394_cmd_t *);
497c478bd9Sstevel@tonic-gate static void	av1394_fcp_common_write_request_cb(cmd1394_cmd_t *, int);
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /* misc routines */
527c478bd9Sstevel@tonic-gate static int	av1394_fcp_copyin_block(iec61883_arq_t *, mblk_t *,
537c478bd9Sstevel@tonic-gate 		struct uio *);
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  *
577c478bd9Sstevel@tonic-gate  * --- configuration entry points
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate int
av1394_fcp_attach(av1394_inst_t * avp)617c478bd9Sstevel@tonic-gate av1394_fcp_attach(av1394_inst_t *avp)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	av1394_fcp_t	*fcp = &avp->av_a.a_fcp;
647c478bd9Sstevel@tonic-gate 	int		ret;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	/* register FCP controller */
677c478bd9Sstevel@tonic-gate 	if ((ret = av1394_fcp_ctl_register(avp)) != DDI_SUCCESS) {
687c478bd9Sstevel@tonic-gate 		return (ret);
697c478bd9Sstevel@tonic-gate 	}
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	/* allocate FCP controller command */
727c478bd9Sstevel@tonic-gate 	if ((ret = av1394_fcp_ctl_alloc_cmd(avp)) != DDI_SUCCESS) {
737c478bd9Sstevel@tonic-gate 		av1394_fcp_cleanup(avp, 1);
747c478bd9Sstevel@tonic-gate 		return (ret);
757c478bd9Sstevel@tonic-gate 	}
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	/* register FCP target */
787c478bd9Sstevel@tonic-gate 	if ((ret = av1394_fcp_tgt_register(avp)) != DDI_SUCCESS) {
797c478bd9Sstevel@tonic-gate 		av1394_fcp_cleanup(avp, 2);
807c478bd9Sstevel@tonic-gate 		return (ret);
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	/* allocate FCP target command */
847c478bd9Sstevel@tonic-gate 	if ((ret = av1394_fcp_tgt_alloc_cmd(avp)) != DDI_SUCCESS) {
857c478bd9Sstevel@tonic-gate 		av1394_fcp_cleanup(avp, 3);
867c478bd9Sstevel@tonic-gate 		return (ret);
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	cv_init(&fcp->fcp_cmd.fc_xmit_cv, NULL, CV_DRIVER, NULL);
907c478bd9Sstevel@tonic-gate 	cv_init(&fcp->fcp_cmd.fc_busy_cv, NULL, CV_DRIVER, NULL);
917c478bd9Sstevel@tonic-gate 	cv_init(&fcp->fcp_resp.fc_xmit_cv, NULL, CV_DRIVER, NULL);
927c478bd9Sstevel@tonic-gate 	cv_init(&fcp->fcp_resp.fc_busy_cv, NULL, CV_DRIVER, NULL);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	return (ret);
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate void
av1394_fcp_detach(av1394_inst_t * avp)987c478bd9Sstevel@tonic-gate av1394_fcp_detach(av1394_inst_t *avp)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	av1394_fcp_cleanup(avp, AV1394_CLEANUP_LEVEL_MAX);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate int
av1394_fcp_write(av1394_inst_t * avp,iec61883_arq_t * arq,struct uio * uiop)1047c478bd9Sstevel@tonic-gate av1394_fcp_write(av1394_inst_t *avp, iec61883_arq_t *arq, struct uio *uiop)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	av1394_async_t	*ap = &avp->av_a;
1077c478bd9Sstevel@tonic-gate 	av1394_fcp_t	*fcp = &ap->a_fcp;
1087c478bd9Sstevel@tonic-gate 	int		len = arq->arq_len;
1097c478bd9Sstevel@tonic-gate 	av1394_fcp_cmd_t *fc;
1107c478bd9Sstevel@tonic-gate 	cmd1394_cmd_t	*cmd;
1117c478bd9Sstevel@tonic-gate 	mblk_t		*mp = NULL;
1127c478bd9Sstevel@tonic-gate 	int		ret;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	ASSERT((arq->arq_type == IEC61883_ARQ_FCP_CMD) ||
1157c478bd9Sstevel@tonic-gate 		(arq->arq_type == IEC61883_ARQ_FCP_RESP));
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	/* check arguments */
1187c478bd9Sstevel@tonic-gate 	if ((len == 0) || (len > AV1394_FCP_ARQ_LEN_MAX) ||
1197c478bd9Sstevel@tonic-gate 	    (len % IEEE1394_QUADLET != 0)) {
1207c478bd9Sstevel@tonic-gate 		return (EINVAL);
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	/* block write requires an mblk */
1247c478bd9Sstevel@tonic-gate 	if (len > IEEE1394_QUADLET) {
1257c478bd9Sstevel@tonic-gate 		if ((mp = allocb(len, BPRI_HI)) == NULL) {
1267c478bd9Sstevel@tonic-gate 			return (ENOMEM);
1277c478bd9Sstevel@tonic-gate 		}
1287c478bd9Sstevel@tonic-gate 		if ((ret = av1394_fcp_copyin_block(arq, mp, uiop)) != 0) {
1297c478bd9Sstevel@tonic-gate 			freemsg(mp);
1307c478bd9Sstevel@tonic-gate 			return (ret);
1317c478bd9Sstevel@tonic-gate 		}
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	/* either FCP command or response */
1357c478bd9Sstevel@tonic-gate 	fc = (arq->arq_type == IEC61883_ARQ_FCP_CMD) ?
1367c478bd9Sstevel@tonic-gate 					&fcp->fcp_cmd : &fcp->fcp_resp;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	/* one command at a time */
1397c478bd9Sstevel@tonic-gate 	mutex_enter(&ap->a_mutex);
1407c478bd9Sstevel@tonic-gate 	while (fc->fc_busy) {
1417c478bd9Sstevel@tonic-gate 		if (cv_wait_sig(&fc->fc_busy_cv, &ap->a_mutex) == 0) {
1427c478bd9Sstevel@tonic-gate 			mutex_exit(&ap->a_mutex);
1437c478bd9Sstevel@tonic-gate 			freemsg(mp);
1447c478bd9Sstevel@tonic-gate 			return (EINTR);
1457c478bd9Sstevel@tonic-gate 		}
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 	fc->fc_busy = B_TRUE;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	/* prepare command */
1507c478bd9Sstevel@tonic-gate 	cmd = fc->fc_cmd;
1517c478bd9Sstevel@tonic-gate 	if (len == IEEE1394_QUADLET) {
1527c478bd9Sstevel@tonic-gate 		cmd->cmd_type = CMD1394_ASYNCH_WR_QUAD;
1537c478bd9Sstevel@tonic-gate 		cmd->cmd_u.q.quadlet_data = arq->arq_data.quadlet;
1547c478bd9Sstevel@tonic-gate 	} else {
1557c478bd9Sstevel@tonic-gate 		cmd->cmd_type = CMD1394_ASYNCH_WR_BLOCK;
1567c478bd9Sstevel@tonic-gate 		cmd->cmd_u.b.data_block = mp;
1577c478bd9Sstevel@tonic-gate 		cmd->cmd_u.b.blk_length = len;
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	/* do the write and wait for completion */
1617c478bd9Sstevel@tonic-gate 	ret = av1394_fcp_cmd_write_sync(avp, fc);
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	/* not busy anymore */
1647c478bd9Sstevel@tonic-gate 	fc->fc_busy = B_FALSE;
1657c478bd9Sstevel@tonic-gate 	cv_broadcast(&fc->fc_busy_cv);
1667c478bd9Sstevel@tonic-gate 	mutex_exit(&ap->a_mutex);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	freemsg(mp);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	return (ret);
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate /*
1747c478bd9Sstevel@tonic-gate  *
1757c478bd9Sstevel@tonic-gate  * --- configuration routines
1767c478bd9Sstevel@tonic-gate  *
1777c478bd9Sstevel@tonic-gate  */
1787c478bd9Sstevel@tonic-gate static int
av1394_fcp_ctl_register(av1394_inst_t * avp)1797c478bd9Sstevel@tonic-gate av1394_fcp_ctl_register(av1394_inst_t *avp)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	t1394_fcp_evts_t evts;
1827c478bd9Sstevel@tonic-gate 	int		ret;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	evts.fcp_write_request = av1394_fcp_resp_write_request_cb;
1857c478bd9Sstevel@tonic-gate 	evts.fcp_arg = avp;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	ret = t1394_fcp_register_controller(avp->av_t1394_hdl, &evts, 0);
1887c478bd9Sstevel@tonic-gate 	return (ret);
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate static int
av1394_fcp_tgt_register(av1394_inst_t * avp)1927c478bd9Sstevel@tonic-gate av1394_fcp_tgt_register(av1394_inst_t *avp)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 	t1394_fcp_evts_t evts;
1957c478bd9Sstevel@tonic-gate 	int		ret;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	evts.fcp_write_request = av1394_fcp_cmd_write_request_cb;
1987c478bd9Sstevel@tonic-gate 	evts.fcp_arg = avp;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	ret = t1394_fcp_register_target(avp->av_t1394_hdl, &evts, 0);
2017c478bd9Sstevel@tonic-gate 	return (ret);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate static int
av1394_fcp_ctl_alloc_cmd(av1394_inst_t * avp)2057c478bd9Sstevel@tonic-gate av1394_fcp_ctl_alloc_cmd(av1394_inst_t *avp)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	av1394_fcp_cmd_t *fc = &avp->av_a.a_fcp.fcp_cmd;
2087c478bd9Sstevel@tonic-gate 	int		ret;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	ret = t1394_alloc_cmd(avp->av_t1394_hdl, T1394_ALLOC_CMD_FCP_COMMAND,
2117c478bd9Sstevel@tonic-gate 				&fc->fc_cmd);
2127c478bd9Sstevel@tonic-gate 	return (ret);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate static void
av1394_fcp_ctl_free_cmd(av1394_inst_t * avp)2167c478bd9Sstevel@tonic-gate av1394_fcp_ctl_free_cmd(av1394_inst_t *avp)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate 	av1394_fcp_cmd_t *fc = &avp->av_a.a_fcp.fcp_cmd;
2197c478bd9Sstevel@tonic-gate 
220*2570281cSToomas Soome 	(void) t1394_free_cmd(avp->av_t1394_hdl, 0, &fc->fc_cmd);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate static int
av1394_fcp_tgt_alloc_cmd(av1394_inst_t * avp)2247c478bd9Sstevel@tonic-gate av1394_fcp_tgt_alloc_cmd(av1394_inst_t *avp)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	av1394_fcp_cmd_t *fc = &avp->av_a.a_fcp.fcp_resp;
2277c478bd9Sstevel@tonic-gate 	int		ret;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	ret = t1394_alloc_cmd(avp->av_t1394_hdl, T1394_ALLOC_CMD_FCP_RESPONSE,
2307c478bd9Sstevel@tonic-gate 				&fc->fc_cmd);
2317c478bd9Sstevel@tonic-gate 	return (ret);
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate static void
av1394_fcp_tgt_free_cmd(av1394_inst_t * avp)2357c478bd9Sstevel@tonic-gate av1394_fcp_tgt_free_cmd(av1394_inst_t *avp)
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate 	av1394_fcp_cmd_t *fc = &avp->av_a.a_fcp.fcp_resp;
2387c478bd9Sstevel@tonic-gate 
239*2570281cSToomas Soome 	(void) t1394_free_cmd(avp->av_t1394_hdl, 0, &fc->fc_cmd);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate static void
av1394_fcp_cleanup(av1394_inst_t * avp,int level)2437c478bd9Sstevel@tonic-gate av1394_fcp_cleanup(av1394_inst_t *avp, int level)
2447c478bd9Sstevel@tonic-gate {
2457c478bd9Sstevel@tonic-gate 	av1394_fcp_t	*fcp = &avp->av_a.a_fcp;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	ASSERT((level > 0) && (level <= AV1394_CLEANUP_LEVEL_MAX));
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	switch (level) {
2507c478bd9Sstevel@tonic-gate 	default:
2517c478bd9Sstevel@tonic-gate 		cv_destroy(&fcp->fcp_cmd.fc_xmit_cv);
2527c478bd9Sstevel@tonic-gate 		cv_destroy(&fcp->fcp_cmd.fc_busy_cv);
2537c478bd9Sstevel@tonic-gate 		cv_destroy(&fcp->fcp_resp.fc_xmit_cv);
2547c478bd9Sstevel@tonic-gate 		cv_destroy(&fcp->fcp_resp.fc_busy_cv);
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 		av1394_fcp_tgt_free_cmd(avp);
2577c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
2587c478bd9Sstevel@tonic-gate 	case 3:
2597c478bd9Sstevel@tonic-gate 		(void) t1394_fcp_unregister_target(avp->av_t1394_hdl);
2607c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
2617c478bd9Sstevel@tonic-gate 	case 2:
2627c478bd9Sstevel@tonic-gate 		av1394_fcp_ctl_free_cmd(avp);
2637c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
2647c478bd9Sstevel@tonic-gate 	case 1:
2657c478bd9Sstevel@tonic-gate 		(void) t1394_fcp_unregister_controller(avp->av_t1394_hdl);
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate /*
2707c478bd9Sstevel@tonic-gate  *
2717c478bd9Sstevel@tonic-gate  * --- FCP write and completion handling
2727c478bd9Sstevel@tonic-gate  *
2737c478bd9Sstevel@tonic-gate  */
2747c478bd9Sstevel@tonic-gate static int
av1394_fcp_cmd_write_sync(av1394_inst_t * avp,av1394_fcp_cmd_t * fc)2757c478bd9Sstevel@tonic-gate av1394_fcp_cmd_write_sync(av1394_inst_t *avp, av1394_fcp_cmd_t *fc)
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate 	av1394_async_t	*ap = &avp->av_a;
2787c478bd9Sstevel@tonic-gate 	cmd1394_cmd_t	*cmd = fc->fc_cmd;
2797c478bd9Sstevel@tonic-gate 	int		ret = 0;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	cmd->completion_callback = av1394_fcp_cmd_completion_cb;
2827c478bd9Sstevel@tonic-gate 	cmd->cmd_callback_arg = avp;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	/* go */
2857c478bd9Sstevel@tonic-gate 	ASSERT(!fc->fc_xmit);
2867c478bd9Sstevel@tonic-gate 	fc->fc_xmit = B_TRUE;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	mutex_exit(&ap->a_mutex);
2897c478bd9Sstevel@tonic-gate 	ret = t1394_write(avp->av_t1394_hdl, cmd);
2907c478bd9Sstevel@tonic-gate 	mutex_enter(&ap->a_mutex);
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	/* immediate error? */
2937c478bd9Sstevel@tonic-gate 	if (ret != DDI_SUCCESS) {
2947c478bd9Sstevel@tonic-gate 		fc->fc_xmit = B_FALSE;
2957c478bd9Sstevel@tonic-gate 		return (EIO);
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	/* wait for completion */
2997c478bd9Sstevel@tonic-gate 	while (fc->fc_xmit) {
3007c478bd9Sstevel@tonic-gate 		if (cv_wait_sig(&fc->fc_xmit_cv, &ap->a_mutex) == 0) {
3017c478bd9Sstevel@tonic-gate 			return (EINTR);
3027c478bd9Sstevel@tonic-gate 		}
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	if (cmd->cmd_result != CMD1394_CMDSUCCESS) {
3067c478bd9Sstevel@tonic-gate 		if (cmd->cmd_result == CMD1394_EDEVICE_BUSY) {
3077c478bd9Sstevel@tonic-gate 			return (EBUSY);
3087c478bd9Sstevel@tonic-gate 		} else {
3097c478bd9Sstevel@tonic-gate 			return (EIO);
3107c478bd9Sstevel@tonic-gate 		}
3117c478bd9Sstevel@tonic-gate 	} else {
3127c478bd9Sstevel@tonic-gate 		return (0);
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate static void
av1394_fcp_cmd_completion_cb(struct cmd1394_cmd * cmd)3177c478bd9Sstevel@tonic-gate av1394_fcp_cmd_completion_cb(struct cmd1394_cmd *cmd)
3187c478bd9Sstevel@tonic-gate {
3197c478bd9Sstevel@tonic-gate 	av1394_inst_t	*avp = cmd->cmd_callback_arg;
3207c478bd9Sstevel@tonic-gate 	av1394_async_t	*ap = &avp->av_a;
3217c478bd9Sstevel@tonic-gate 	av1394_fcp_t	*fcp = &ap->a_fcp;
3227c478bd9Sstevel@tonic-gate 	av1394_fcp_cmd_t *fc;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	mutex_enter(&ap->a_mutex);
3257c478bd9Sstevel@tonic-gate 	/* is this FCP command or response */
3267c478bd9Sstevel@tonic-gate 	if (cmd == fcp->fcp_cmd.fc_cmd) {
3277c478bd9Sstevel@tonic-gate 		fc = &fcp->fcp_cmd;
3287c478bd9Sstevel@tonic-gate 	} else {
3297c478bd9Sstevel@tonic-gate 		ASSERT(cmd == fcp->fcp_resp.fc_cmd);
3307c478bd9Sstevel@tonic-gate 		fc = &fcp->fcp_resp;
3317c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	/* wake the waiter */
3347c478bd9Sstevel@tonic-gate 	fc->fc_xmit = B_FALSE;
3357c478bd9Sstevel@tonic-gate 	cv_signal(&fc->fc_xmit_cv);
3367c478bd9Sstevel@tonic-gate 	mutex_exit(&ap->a_mutex);
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate /*
3407c478bd9Sstevel@tonic-gate  * av1394_fcp_cmd_write_request_cb()
3417c478bd9Sstevel@tonic-gate  *    Incoming response request from an FCP target
3427c478bd9Sstevel@tonic-gate  */
3437c478bd9Sstevel@tonic-gate static int
av1394_fcp_resp_write_request_cb(cmd1394_cmd_t * req)3447c478bd9Sstevel@tonic-gate av1394_fcp_resp_write_request_cb(cmd1394_cmd_t *req)
3457c478bd9Sstevel@tonic-gate {
3467c478bd9Sstevel@tonic-gate 	av1394_inst_t	*avp = req->cmd_callback_arg;
3477c478bd9Sstevel@tonic-gate 	av1394_async_t	*ap = &avp->av_a;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	mutex_enter(&ap->a_mutex);
3507c478bd9Sstevel@tonic-gate 	if ((ap->a_nopen == 0) ||
3517c478bd9Sstevel@tonic-gate 	    (req->bus_generation != ap->a_bus_generation) ||
3527c478bd9Sstevel@tonic-gate 	    (req->nodeID != ap->a_targetinfo.target_nodeID)) {
3537c478bd9Sstevel@tonic-gate 		mutex_exit(&ap->a_mutex);
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 		return (T1394_REQ_UNCLAIMED);
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 	mutex_exit(&ap->a_mutex);
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	av1394_fcp_common_write_request_cb(req, AV1394_M_FCP_RESP);
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	return (T1394_REQ_CLAIMED);
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate /*
3657c478bd9Sstevel@tonic-gate  * av1394_fcp_cmd_write_request_cb()
3667c478bd9Sstevel@tonic-gate  *    Incoming command request from an FCP controller
3677c478bd9Sstevel@tonic-gate  */
3687c478bd9Sstevel@tonic-gate static int
av1394_fcp_cmd_write_request_cb(cmd1394_cmd_t * req)3697c478bd9Sstevel@tonic-gate av1394_fcp_cmd_write_request_cb(cmd1394_cmd_t *req)
3707c478bd9Sstevel@tonic-gate {
3717c478bd9Sstevel@tonic-gate 	av1394_inst_t	*avp = req->cmd_callback_arg;
3727c478bd9Sstevel@tonic-gate 	av1394_async_t	*ap = &avp->av_a;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	mutex_enter(&ap->a_mutex);
3757c478bd9Sstevel@tonic-gate 	if (ap->a_nopen == 0) {
3767c478bd9Sstevel@tonic-gate 		mutex_exit(&ap->a_mutex);
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 		return (T1394_REQ_UNCLAIMED);
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 	mutex_exit(&ap->a_mutex);
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	av1394_fcp_common_write_request_cb(req, AV1394_M_FCP_CMD);
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	return (T1394_REQ_CLAIMED);
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate static void
av1394_fcp_common_write_request_cb(cmd1394_cmd_t * req,int mtype)3887c478bd9Sstevel@tonic-gate av1394_fcp_common_write_request_cb(cmd1394_cmd_t *req, int mtype)
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	av1394_inst_t	*avp = req->cmd_callback_arg;
3917c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
3927c478bd9Sstevel@tonic-gate 	uint32_t	quadlet_data;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	ASSERT((req->cmd_type == CMD1394_ASYNCH_WR_QUAD) ||
3957c478bd9Sstevel@tonic-gate 		(req->cmd_type == CMD1394_ASYNCH_WR_BLOCK));
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	/* get the data */
3987c478bd9Sstevel@tonic-gate 	if (req->cmd_type == CMD1394_ASYNCH_WR_QUAD) {
3997c478bd9Sstevel@tonic-gate 		quadlet_data = req->cmd_u.q.quadlet_data;
4007c478bd9Sstevel@tonic-gate 	} else {
4017c478bd9Sstevel@tonic-gate 		mp = req->cmd_u.b.data_block;
4027c478bd9Sstevel@tonic-gate 		req->cmd_u.b.data_block = NULL;
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	/* complete request */
4067c478bd9Sstevel@tonic-gate 	req->cmd_result = IEEE1394_RESP_COMPLETE;
4077c478bd9Sstevel@tonic-gate 
408*2570281cSToomas Soome 	(void) t1394_recv_request_done(avp->av_t1394_hdl, req, 0);
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	/* allocate mblk and copy quadlet into it */
4117c478bd9Sstevel@tonic-gate 	if (req->cmd_type == CMD1394_ASYNCH_WR_QUAD) {
4127c478bd9Sstevel@tonic-gate 		mp = allocb(IEEE1394_QUADLET, BPRI_HI);
4137c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
4147c478bd9Sstevel@tonic-gate 			return;
4157c478bd9Sstevel@tonic-gate 		}
4167c478bd9Sstevel@tonic-gate 		*(uint32_t *)mp->b_rptr = quadlet_data;
4177c478bd9Sstevel@tonic-gate 		mp->b_wptr += IEEE1394_QUADLET;
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	/* queue up the data */
4217c478bd9Sstevel@tonic-gate 	DB_TYPE(mp) = mtype;
4227c478bd9Sstevel@tonic-gate 	av1394_async_putq_rq(avp, mp);
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate /*
4267c478bd9Sstevel@tonic-gate  *
4277c478bd9Sstevel@tonic-gate  * --- misc routines
4287c478bd9Sstevel@tonic-gate  *
4297c478bd9Sstevel@tonic-gate  */
4307c478bd9Sstevel@tonic-gate static int
av1394_fcp_copyin_block(iec61883_arq_t * arq,mblk_t * mp,struct uio * uiop)4317c478bd9Sstevel@tonic-gate av1394_fcp_copyin_block(iec61883_arq_t *arq, mblk_t *mp, struct uio *uiop)
4327c478bd9Sstevel@tonic-gate {
4337c478bd9Sstevel@tonic-gate 	int	len = arq->arq_len;
4347c478bd9Sstevel@tonic-gate 	int	copylen;
4357c478bd9Sstevel@tonic-gate 	int	ret = 0;
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	ASSERT((len > 0) && (len % IEEE1394_QUADLET == 0));
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	/* first copy ARQ-embedded data */
4407c478bd9Sstevel@tonic-gate 	copylen = min(len, sizeof (arq->arq_data));
4417c478bd9Sstevel@tonic-gate 	bcopy(&arq->arq_data.buf[0], mp->b_wptr, copylen);
4427c478bd9Sstevel@tonic-gate 	mp->b_wptr += copylen;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	/* now copyin the rest of the data, if any */
4457c478bd9Sstevel@tonic-gate 	copylen = len - copylen;
4467c478bd9Sstevel@tonic-gate 	if (copylen > 0) {
4477c478bd9Sstevel@tonic-gate 		ret = uiomove(mp->b_wptr, copylen, UIO_WRITE, uiop);
4487c478bd9Sstevel@tonic-gate 		if (ret != 0) {
4497c478bd9Sstevel@tonic-gate 			return (ret);
4507c478bd9Sstevel@tonic-gate 		}
4517c478bd9Sstevel@tonic-gate 		mp->b_wptr += copylen;
4527c478bd9Sstevel@tonic-gate 	}
4537c478bd9Sstevel@tonic-gate 	return (ret);
4547c478bd9Sstevel@tonic-gate }
455