xref: /titanic_41/usr/src/uts/common/io/1394/s1394_cmp.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * s1394_cmp.c
31*7c478bd9Sstevel@tonic-gate  *    1394 Services Layer Connection Management Procedures Support Routines
32*7c478bd9Sstevel@tonic-gate  */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include <sys/conf.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/tnf_probe.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include <sys/1394/t1394.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/1394/s1394.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/1394/h1394.h>
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate static void s1394_cmp_init(s1394_hal_t *hal);
47*7c478bd9Sstevel@tonic-gate static void s1394_cmp_fini(s1394_hal_t *hal);
48*7c478bd9Sstevel@tonic-gate static void s1394_cmp_ompr_recv_read_request(cmd1394_cmd_t *req);
49*7c478bd9Sstevel@tonic-gate static void s1394_cmp_impr_recv_read_request(cmd1394_cmd_t *req);
50*7c478bd9Sstevel@tonic-gate static void s1394_cmp_ompr_recv_lock_request(cmd1394_cmd_t *req);
51*7c478bd9Sstevel@tonic-gate static void s1394_cmp_impr_recv_lock_request(cmd1394_cmd_t *req);
52*7c478bd9Sstevel@tonic-gate static void s1394_cmp_notify_reg_change(s1394_hal_t *hal, t1394_cmp_reg_t reg,
53*7c478bd9Sstevel@tonic-gate     s1394_target_t *self);
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate /*
57*7c478bd9Sstevel@tonic-gate  * number of retries to notify registered targets in case target list
58*7c478bd9Sstevel@tonic-gate  * changes while the list rwlock is dropped for the time of callback
59*7c478bd9Sstevel@tonic-gate  */
60*7c478bd9Sstevel@tonic-gate uint_t s1394_cmp_notify_retry_cnt = 3;
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate s1394_fa_descr_t s1394_cmp_ompr_descr = {
63*7c478bd9Sstevel@tonic-gate 	IEC61883_CMP_OMPR_ADDR,
64*7c478bd9Sstevel@tonic-gate 	4,
65*7c478bd9Sstevel@tonic-gate 	T1394_ADDR_RDENBL | T1394_ADDR_LKENBL,
66*7c478bd9Sstevel@tonic-gate 	{
67*7c478bd9Sstevel@tonic-gate 		s1394_cmp_ompr_recv_read_request,
68*7c478bd9Sstevel@tonic-gate 		NULL,
69*7c478bd9Sstevel@tonic-gate 		s1394_cmp_ompr_recv_lock_request
70*7c478bd9Sstevel@tonic-gate 	},
71*7c478bd9Sstevel@tonic-gate 	0
72*7c478bd9Sstevel@tonic-gate };
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate s1394_fa_descr_t s1394_cmp_impr_descr = {
75*7c478bd9Sstevel@tonic-gate 	IEC61883_CMP_IMPR_ADDR,
76*7c478bd9Sstevel@tonic-gate 	4,
77*7c478bd9Sstevel@tonic-gate 	T1394_ADDR_RDENBL | T1394_ADDR_LKENBL,
78*7c478bd9Sstevel@tonic-gate 	{
79*7c478bd9Sstevel@tonic-gate 		s1394_cmp_impr_recv_read_request,
80*7c478bd9Sstevel@tonic-gate 		NULL,
81*7c478bd9Sstevel@tonic-gate 		s1394_cmp_impr_recv_lock_request
82*7c478bd9Sstevel@tonic-gate 	},
83*7c478bd9Sstevel@tonic-gate 	0
84*7c478bd9Sstevel@tonic-gate };
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate int
s1394_cmp_register(s1394_target_t * target,t1394_cmp_evts_t * evts)88*7c478bd9Sstevel@tonic-gate s1394_cmp_register(s1394_target_t *target, t1394_cmp_evts_t *evts)
89*7c478bd9Sstevel@tonic-gate {
90*7c478bd9Sstevel@tonic-gate 	s1394_hal_t	*hal = target->on_hal;
91*7c478bd9Sstevel@tonic-gate 	static t1394_cmp_evts_t default_evts = { NULL, NULL };
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_cmp_register_enter, S1394_TNF_SL_CMP_STACK, "");
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 	rw_enter(&hal->target_list_rwlock, RW_WRITER);
96*7c478bd9Sstevel@tonic-gate 	/*
97*7c478bd9Sstevel@tonic-gate 	 * if registering the first target, claim and initialize addresses
98*7c478bd9Sstevel@tonic-gate 	 */
99*7c478bd9Sstevel@tonic-gate 	if (s1394_fa_list_is_empty(hal, S1394_FA_TYPE_CMP)) {
100*7c478bd9Sstevel@tonic-gate 		if (s1394_fa_claim_addr(hal, S1394_FA_TYPE_CMP_OMPR,
101*7c478bd9Sstevel@tonic-gate 		    &s1394_cmp_ompr_descr) != DDI_SUCCESS) {
102*7c478bd9Sstevel@tonic-gate 			rw_exit(&hal->target_list_rwlock);
103*7c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
104*7c478bd9Sstevel@tonic-gate 		}
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 		if (s1394_fa_claim_addr(hal, S1394_FA_TYPE_CMP_IMPR,
107*7c478bd9Sstevel@tonic-gate 		    &s1394_cmp_impr_descr) != DDI_SUCCESS) {
108*7c478bd9Sstevel@tonic-gate 			s1394_fa_free_addr(hal, S1394_FA_TYPE_CMP_OMPR);
109*7c478bd9Sstevel@tonic-gate 			rw_exit(&hal->target_list_rwlock);
110*7c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
111*7c478bd9Sstevel@tonic-gate 		}
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 		s1394_cmp_init(hal);
114*7c478bd9Sstevel@tonic-gate 	}
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	/* Add on the target list (we only use one list) */
117*7c478bd9Sstevel@tonic-gate 	s1394_fa_list_add(hal, target, S1394_FA_TYPE_CMP);
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	if (evts == NULL) {
120*7c478bd9Sstevel@tonic-gate 		evts = &default_evts;
121*7c478bd9Sstevel@tonic-gate 	}
122*7c478bd9Sstevel@tonic-gate 	target->target_fa[S1394_FA_TYPE_CMP].fat_u.cmp.cm_evts = *evts;
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	rw_exit(&hal->target_list_rwlock);
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_cmp_register_exit, S1394_TNF_SL_CMP_STACK, "");
127*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
128*7c478bd9Sstevel@tonic-gate }
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate int
s1394_cmp_unregister(s1394_target_t * target)131*7c478bd9Sstevel@tonic-gate s1394_cmp_unregister(s1394_target_t *target)
132*7c478bd9Sstevel@tonic-gate {
133*7c478bd9Sstevel@tonic-gate 	s1394_hal_t	*hal = target->on_hal;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_cmp_unregister_enter, S1394_TNF_SL_CMP_STACK,
136*7c478bd9Sstevel@tonic-gate 	    "");
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	rw_enter(&hal->target_list_rwlock, RW_WRITER);
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	if (s1394_fa_list_remove(hal, target,
141*7c478bd9Sstevel@tonic-gate 	    S1394_FA_TYPE_CMP) == DDI_SUCCESS) {
142*7c478bd9Sstevel@tonic-gate 		if (s1394_fa_list_is_empty(hal, S1394_FA_TYPE_CMP)) {
143*7c478bd9Sstevel@tonic-gate 			s1394_fa_free_addr(hal, S1394_FA_TYPE_CMP_OMPR);
144*7c478bd9Sstevel@tonic-gate 			s1394_fa_free_addr(hal, S1394_FA_TYPE_CMP_IMPR);
145*7c478bd9Sstevel@tonic-gate 			s1394_cmp_fini(hal);
146*7c478bd9Sstevel@tonic-gate 		}
147*7c478bd9Sstevel@tonic-gate 	} else {
148*7c478bd9Sstevel@tonic-gate 		TNF_PROBE_0(s1394_cmp_unregister_common_error_list,
149*7c478bd9Sstevel@tonic-gate 		    S1394_TNF_SL_CMP_ERROR, "");
150*7c478bd9Sstevel@tonic-gate 	}
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	rw_exit(&hal->target_list_rwlock);
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_cmp_unregister_exit, S1394_TNF_SL_CMP_STACK,
155*7c478bd9Sstevel@tonic-gate 	    "");
156*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
157*7c478bd9Sstevel@tonic-gate }
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate int
s1394_cmp_read(s1394_target_t * target,t1394_cmp_reg_t reg,uint32_t * valp)160*7c478bd9Sstevel@tonic-gate s1394_cmp_read(s1394_target_t *target, t1394_cmp_reg_t reg, uint32_t *valp)
161*7c478bd9Sstevel@tonic-gate {
162*7c478bd9Sstevel@tonic-gate 	s1394_hal_t	*hal = target->on_hal;
163*7c478bd9Sstevel@tonic-gate 	s1394_cmp_hal_t *cmp = &hal->hal_cmp;
164*7c478bd9Sstevel@tonic-gate 	int		ret = DDI_FAILURE;
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_cmp_read_enter, S1394_TNF_SL_CMP_STACK, "");
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 	if (reg == T1394_CMP_OMPR) {
169*7c478bd9Sstevel@tonic-gate 		rw_enter(&cmp->cmp_ompr_rwlock, RW_READER);
170*7c478bd9Sstevel@tonic-gate 		*valp = cmp->cmp_ompr_val;
171*7c478bd9Sstevel@tonic-gate 		rw_exit(&cmp->cmp_ompr_rwlock);
172*7c478bd9Sstevel@tonic-gate 		ret = DDI_SUCCESS;
173*7c478bd9Sstevel@tonic-gate 	} else if (reg == T1394_CMP_IMPR) {
174*7c478bd9Sstevel@tonic-gate 		rw_enter(&cmp->cmp_impr_rwlock, RW_READER);
175*7c478bd9Sstevel@tonic-gate 		*valp = cmp->cmp_impr_val;
176*7c478bd9Sstevel@tonic-gate 		rw_exit(&cmp->cmp_impr_rwlock);
177*7c478bd9Sstevel@tonic-gate 		ret = DDI_SUCCESS;
178*7c478bd9Sstevel@tonic-gate 	}
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_cmp_read_exit, S1394_TNF_SL_CMP_STACK, "");
181*7c478bd9Sstevel@tonic-gate 	return (ret);
182*7c478bd9Sstevel@tonic-gate }
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate int
s1394_cmp_cas(s1394_target_t * target,t1394_cmp_reg_t reg,uint32_t arg_val,uint32_t new_val,uint32_t * old_valp)185*7c478bd9Sstevel@tonic-gate s1394_cmp_cas(s1394_target_t *target, t1394_cmp_reg_t reg, uint32_t arg_val,
186*7c478bd9Sstevel@tonic-gate 		uint32_t new_val, uint32_t *old_valp)
187*7c478bd9Sstevel@tonic-gate {
188*7c478bd9Sstevel@tonic-gate 	s1394_hal_t	*hal = target->on_hal;
189*7c478bd9Sstevel@tonic-gate 	s1394_cmp_hal_t *cmp = &hal->hal_cmp;
190*7c478bd9Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_cmp_cas_enter, S1394_TNF_SL_CMP_STACK, "");
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	if (reg == T1394_CMP_OMPR) {
195*7c478bd9Sstevel@tonic-gate 		rw_enter(&cmp->cmp_ompr_rwlock, RW_WRITER);
196*7c478bd9Sstevel@tonic-gate 		*old_valp = cmp->cmp_ompr_val;
197*7c478bd9Sstevel@tonic-gate 		if (cmp->cmp_ompr_val == arg_val) {
198*7c478bd9Sstevel@tonic-gate 			cmp->cmp_ompr_val = new_val;
199*7c478bd9Sstevel@tonic-gate 		}
200*7c478bd9Sstevel@tonic-gate 		rw_exit(&cmp->cmp_ompr_rwlock);
201*7c478bd9Sstevel@tonic-gate 	} else if (reg == T1394_CMP_IMPR) {
202*7c478bd9Sstevel@tonic-gate 		rw_enter(&cmp->cmp_impr_rwlock, RW_WRITER);
203*7c478bd9Sstevel@tonic-gate 		*old_valp = cmp->cmp_impr_val;
204*7c478bd9Sstevel@tonic-gate 		if (cmp->cmp_impr_val == arg_val) {
205*7c478bd9Sstevel@tonic-gate 			cmp->cmp_impr_val = new_val;
206*7c478bd9Sstevel@tonic-gate 		}
207*7c478bd9Sstevel@tonic-gate 		rw_exit(&cmp->cmp_impr_rwlock);
208*7c478bd9Sstevel@tonic-gate 	} else {
209*7c478bd9Sstevel@tonic-gate 		ret = DDI_FAILURE;
210*7c478bd9Sstevel@tonic-gate 	}
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 	/* notify other targets */
213*7c478bd9Sstevel@tonic-gate 	if (ret == DDI_SUCCESS) {
214*7c478bd9Sstevel@tonic-gate 		s1394_cmp_notify_reg_change(hal, reg, target);
215*7c478bd9Sstevel@tonic-gate 	}
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_cmp_cas_exit, S1394_TNF_SL_CMP_STACK, "");
218*7c478bd9Sstevel@tonic-gate 	return (ret);
219*7c478bd9Sstevel@tonic-gate }
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate static void
s1394_cmp_init(s1394_hal_t * hal)222*7c478bd9Sstevel@tonic-gate s1394_cmp_init(s1394_hal_t *hal)
223*7c478bd9Sstevel@tonic-gate {
224*7c478bd9Sstevel@tonic-gate 	s1394_cmp_hal_t *cmp = &hal->hal_cmp;
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 	rw_init(&cmp->cmp_ompr_rwlock, NULL, RW_DRIVER, NULL);
227*7c478bd9Sstevel@tonic-gate 	rw_init(&cmp->cmp_impr_rwlock, NULL, RW_DRIVER, NULL);
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 	cmp->cmp_ompr_val = IEC61883_CMP_OMPR_INIT_VAL;
230*7c478bd9Sstevel@tonic-gate 	cmp->cmp_impr_val = IEC61883_CMP_IMPR_INIT_VAL;
231*7c478bd9Sstevel@tonic-gate }
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate static void
s1394_cmp_fini(s1394_hal_t * hal)234*7c478bd9Sstevel@tonic-gate s1394_cmp_fini(s1394_hal_t *hal)
235*7c478bd9Sstevel@tonic-gate {
236*7c478bd9Sstevel@tonic-gate 	s1394_cmp_hal_t *cmp = &hal->hal_cmp;
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate 	rw_destroy(&cmp->cmp_ompr_rwlock);
239*7c478bd9Sstevel@tonic-gate 	rw_destroy(&cmp->cmp_impr_rwlock);
240*7c478bd9Sstevel@tonic-gate }
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate /*
243*7c478bd9Sstevel@tonic-gate  * iMPR/oMPR read/lock requests
244*7c478bd9Sstevel@tonic-gate  */
245*7c478bd9Sstevel@tonic-gate static void
s1394_cmp_ompr_recv_read_request(cmd1394_cmd_t * req)246*7c478bd9Sstevel@tonic-gate s1394_cmp_ompr_recv_read_request(cmd1394_cmd_t *req)
247*7c478bd9Sstevel@tonic-gate {
248*7c478bd9Sstevel@tonic-gate 	s1394_hal_t	*hal = req->cmd_callback_arg;
249*7c478bd9Sstevel@tonic-gate 	s1394_cmp_hal_t *cmp = &hal->hal_cmp;
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_cmp_ompr_recv_read_request_enter,
252*7c478bd9Sstevel@tonic-gate 	    S1394_TNF_SL_CMP_STACK, "");
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate 	if (req->cmd_type != CMD1394_ASYNCH_RD_QUAD) {
255*7c478bd9Sstevel@tonic-gate 		req->cmd_result = IEEE1394_RESP_TYPE_ERROR;
256*7c478bd9Sstevel@tonic-gate 	} else {
257*7c478bd9Sstevel@tonic-gate 		rw_enter(&cmp->cmp_ompr_rwlock, RW_READER);
258*7c478bd9Sstevel@tonic-gate 		req->cmd_u.q.quadlet_data = cmp->cmp_ompr_val;
259*7c478bd9Sstevel@tonic-gate 		rw_exit(&cmp->cmp_ompr_rwlock);
260*7c478bd9Sstevel@tonic-gate 		req->cmd_result = IEEE1394_RESP_COMPLETE;
261*7c478bd9Sstevel@tonic-gate 	}
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 	(void) s1394_send_response(hal, req);
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_cmp_ompr_recv_read_request_exit,
266*7c478bd9Sstevel@tonic-gate 	    S1394_TNF_SL_CMP_STACK, "");
267*7c478bd9Sstevel@tonic-gate }
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate static void
s1394_cmp_impr_recv_read_request(cmd1394_cmd_t * req)270*7c478bd9Sstevel@tonic-gate s1394_cmp_impr_recv_read_request(cmd1394_cmd_t *req)
271*7c478bd9Sstevel@tonic-gate {
272*7c478bd9Sstevel@tonic-gate 	s1394_hal_t	*hal = req->cmd_callback_arg;
273*7c478bd9Sstevel@tonic-gate 	s1394_cmp_hal_t *cmp = &hal->hal_cmp;
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_cmp_impr_recv_read_request_enter,
276*7c478bd9Sstevel@tonic-gate 	    S1394_TNF_SL_CMP_STACK, "");
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate 	if (req->cmd_type != CMD1394_ASYNCH_RD_QUAD) {
279*7c478bd9Sstevel@tonic-gate 		req->cmd_result = IEEE1394_RESP_TYPE_ERROR;
280*7c478bd9Sstevel@tonic-gate 	} else {
281*7c478bd9Sstevel@tonic-gate 		rw_enter(&cmp->cmp_impr_rwlock, RW_READER);
282*7c478bd9Sstevel@tonic-gate 		req->cmd_u.q.quadlet_data = cmp->cmp_impr_val;
283*7c478bd9Sstevel@tonic-gate 		rw_exit(&cmp->cmp_impr_rwlock);
284*7c478bd9Sstevel@tonic-gate 		req->cmd_result = IEEE1394_RESP_COMPLETE;
285*7c478bd9Sstevel@tonic-gate 	}
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 	(void) s1394_send_response(hal, req);
288*7c478bd9Sstevel@tonic-gate 
289*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_cmp_impr_recv_read_request_exit,
290*7c478bd9Sstevel@tonic-gate 	    S1394_TNF_SL_CMP_STACK, "");
291*7c478bd9Sstevel@tonic-gate }
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate static void
s1394_cmp_ompr_recv_lock_request(cmd1394_cmd_t * req)294*7c478bd9Sstevel@tonic-gate s1394_cmp_ompr_recv_lock_request(cmd1394_cmd_t *req)
295*7c478bd9Sstevel@tonic-gate {
296*7c478bd9Sstevel@tonic-gate 	s1394_hal_t	*hal = req->cmd_callback_arg;
297*7c478bd9Sstevel@tonic-gate 	s1394_cmp_hal_t *cmp = &hal->hal_cmp;
298*7c478bd9Sstevel@tonic-gate 	boolean_t	notify = B_TRUE;
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_cmp_ompr_recv_lock_request_enter,
301*7c478bd9Sstevel@tonic-gate 	    S1394_TNF_SL_CMP_STACK, "");
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate 	if ((req->cmd_type != CMD1394_ASYNCH_LOCK_32) ||
304*7c478bd9Sstevel@tonic-gate 	    (req->cmd_u.l32.lock_type != CMD1394_LOCK_COMPARE_SWAP)) {
305*7c478bd9Sstevel@tonic-gate 		req->cmd_result = IEEE1394_RESP_TYPE_ERROR;
306*7c478bd9Sstevel@tonic-gate 		notify = B_FALSE;
307*7c478bd9Sstevel@tonic-gate 	} else {
308*7c478bd9Sstevel@tonic-gate 		rw_enter(&cmp->cmp_ompr_rwlock, RW_WRITER);
309*7c478bd9Sstevel@tonic-gate 		req->cmd_u.l32.old_value = cmp->cmp_ompr_val;
310*7c478bd9Sstevel@tonic-gate 		if (cmp->cmp_ompr_val == req->cmd_u.l32.arg_value) {
311*7c478bd9Sstevel@tonic-gate 			/* write only allowed bits */
312*7c478bd9Sstevel@tonic-gate 			cmp->cmp_ompr_val = (req->cmd_u.l32.data_value &
313*7c478bd9Sstevel@tonic-gate 			    IEC61883_CMP_OMPR_LOCK_MASK) |
314*7c478bd9Sstevel@tonic-gate 			    (cmp->cmp_ompr_val & ~IEC61883_CMP_OMPR_LOCK_MASK);
315*7c478bd9Sstevel@tonic-gate 		}
316*7c478bd9Sstevel@tonic-gate 		rw_exit(&cmp->cmp_ompr_rwlock);
317*7c478bd9Sstevel@tonic-gate 		req->cmd_result = IEEE1394_RESP_COMPLETE;
318*7c478bd9Sstevel@tonic-gate 	}
319*7c478bd9Sstevel@tonic-gate 
320*7c478bd9Sstevel@tonic-gate 	(void) s1394_send_response(hal, req);
321*7c478bd9Sstevel@tonic-gate 
322*7c478bd9Sstevel@tonic-gate 	/* notify all targets */
323*7c478bd9Sstevel@tonic-gate 	if (notify) {
324*7c478bd9Sstevel@tonic-gate 		s1394_cmp_notify_reg_change(hal, T1394_CMP_OMPR, NULL);
325*7c478bd9Sstevel@tonic-gate 	}
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_cmp_ompr_recv_lock_request_exit,
328*7c478bd9Sstevel@tonic-gate 	    S1394_TNF_SL_CMP_STACK, "");
329*7c478bd9Sstevel@tonic-gate }
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate static void
s1394_cmp_impr_recv_lock_request(cmd1394_cmd_t * req)332*7c478bd9Sstevel@tonic-gate s1394_cmp_impr_recv_lock_request(cmd1394_cmd_t *req)
333*7c478bd9Sstevel@tonic-gate {
334*7c478bd9Sstevel@tonic-gate 	s1394_hal_t	*hal = req->cmd_callback_arg;
335*7c478bd9Sstevel@tonic-gate 	s1394_cmp_hal_t *cmp = &hal->hal_cmp;
336*7c478bd9Sstevel@tonic-gate 	boolean_t	notify = B_TRUE;
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_cmp_impr_recv_lock_request_enter,
339*7c478bd9Sstevel@tonic-gate 	    S1394_TNF_SL_CMP_STACK, "");
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 	if ((req->cmd_type != CMD1394_ASYNCH_LOCK_32) ||
342*7c478bd9Sstevel@tonic-gate 	    (req->cmd_u.l32.lock_type != CMD1394_LOCK_COMPARE_SWAP)) {
343*7c478bd9Sstevel@tonic-gate 		req->cmd_result = IEEE1394_RESP_TYPE_ERROR;
344*7c478bd9Sstevel@tonic-gate 		notify = B_FALSE;
345*7c478bd9Sstevel@tonic-gate 	} else {
346*7c478bd9Sstevel@tonic-gate 		rw_enter(&cmp->cmp_impr_rwlock, RW_WRITER);
347*7c478bd9Sstevel@tonic-gate 		req->cmd_u.l32.old_value = cmp->cmp_impr_val;
348*7c478bd9Sstevel@tonic-gate 		if (cmp->cmp_impr_val == req->cmd_u.l32.arg_value) {
349*7c478bd9Sstevel@tonic-gate 			/* write only allowed bits */
350*7c478bd9Sstevel@tonic-gate 			cmp->cmp_impr_val = (req->cmd_u.l32.data_value &
351*7c478bd9Sstevel@tonic-gate 			    IEC61883_CMP_IMPR_LOCK_MASK) |
352*7c478bd9Sstevel@tonic-gate 			    (cmp->cmp_impr_val & ~IEC61883_CMP_IMPR_LOCK_MASK);
353*7c478bd9Sstevel@tonic-gate 		}
354*7c478bd9Sstevel@tonic-gate 		rw_exit(&cmp->cmp_impr_rwlock);
355*7c478bd9Sstevel@tonic-gate 		req->cmd_result = IEEE1394_RESP_COMPLETE;
356*7c478bd9Sstevel@tonic-gate 	}
357*7c478bd9Sstevel@tonic-gate 
358*7c478bd9Sstevel@tonic-gate 	(void) s1394_send_response(hal, req);
359*7c478bd9Sstevel@tonic-gate 
360*7c478bd9Sstevel@tonic-gate 	/* notify all targets */
361*7c478bd9Sstevel@tonic-gate 	if (notify) {
362*7c478bd9Sstevel@tonic-gate 		s1394_cmp_notify_reg_change(hal, T1394_CMP_IMPR, NULL);
363*7c478bd9Sstevel@tonic-gate 	}
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_cmp_impr_recv_lock_request_exit,
366*7c478bd9Sstevel@tonic-gate 	    S1394_TNF_SL_CMP_STACK, "");
367*7c478bd9Sstevel@tonic-gate }
368*7c478bd9Sstevel@tonic-gate 
369*7c478bd9Sstevel@tonic-gate /*
370*7c478bd9Sstevel@tonic-gate  * Notify registered targets except 'self' about register value change
371*7c478bd9Sstevel@tonic-gate  */
372*7c478bd9Sstevel@tonic-gate static void
s1394_cmp_notify_reg_change(s1394_hal_t * hal,t1394_cmp_reg_t reg,s1394_target_t * self)373*7c478bd9Sstevel@tonic-gate s1394_cmp_notify_reg_change(s1394_hal_t *hal, t1394_cmp_reg_t reg,
374*7c478bd9Sstevel@tonic-gate     s1394_target_t *self)
375*7c478bd9Sstevel@tonic-gate {
376*7c478bd9Sstevel@tonic-gate 	s1394_target_t	*target;
377*7c478bd9Sstevel@tonic-gate 	s1394_fa_target_t *fat;
378*7c478bd9Sstevel@tonic-gate 	uint_t		saved_gen;
379*7c478bd9Sstevel@tonic-gate 	int		num_retries = 0;
380*7c478bd9Sstevel@tonic-gate 	void		(*cb)(opaque_t, t1394_cmp_reg_t);
381*7c478bd9Sstevel@tonic-gate 	opaque_t	arg;
382*7c478bd9Sstevel@tonic-gate 
383*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_cmp_notify_reg_change_enter,
384*7c478bd9Sstevel@tonic-gate 	    S1394_TNF_SL_CMP_STACK, "");
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate 	rw_enter(&hal->target_list_rwlock, RW_READER);
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate start:
389*7c478bd9Sstevel@tonic-gate 	target = hal->hal_fa[S1394_FA_TYPE_CMP].fal_head;
390*7c478bd9Sstevel@tonic-gate 
391*7c478bd9Sstevel@tonic-gate 	for (; target; target = fat->fat_next) {
392*7c478bd9Sstevel@tonic-gate 		fat = &target->target_fa[S1394_FA_TYPE_CMP];
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate 		/*
395*7c478bd9Sstevel@tonic-gate 		 * even if the target list changes when the lock is dropped,
396*7c478bd9Sstevel@tonic-gate 		 * comparing with self is safe because the target should
397*7c478bd9Sstevel@tonic-gate 		 * not unregister until all CMP operations are completed
398*7c478bd9Sstevel@tonic-gate 		 */
399*7c478bd9Sstevel@tonic-gate 		if (target == self) {
400*7c478bd9Sstevel@tonic-gate 			continue;
401*7c478bd9Sstevel@tonic-gate 		}
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate 		cb = fat->fat_u.cmp.cm_evts.cmp_reg_change;
404*7c478bd9Sstevel@tonic-gate 		if (cb == NULL) {
405*7c478bd9Sstevel@tonic-gate 			continue;
406*7c478bd9Sstevel@tonic-gate 		}
407*7c478bd9Sstevel@tonic-gate 		arg = fat->fat_u.cmp.cm_evts.cmp_arg;
408*7c478bd9Sstevel@tonic-gate 
409*7c478bd9Sstevel@tonic-gate 		saved_gen = s1394_fa_list_gen(hal, S1394_FA_TYPE_CMP);
410*7c478bd9Sstevel@tonic-gate 
411*7c478bd9Sstevel@tonic-gate 		rw_exit(&hal->target_list_rwlock);
412*7c478bd9Sstevel@tonic-gate 		cb(arg, reg);
413*7c478bd9Sstevel@tonic-gate 		rw_enter(&hal->target_list_rwlock, RW_READER);
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate 		/*
416*7c478bd9Sstevel@tonic-gate 		 * List could change while we dropped the lock. In such
417*7c478bd9Sstevel@tonic-gate 		 * case, start all over again, because missing a register
418*7c478bd9Sstevel@tonic-gate 		 * change can have more serious consequences for a
419*7c478bd9Sstevel@tonic-gate 		 * target than receiving same notification more than once
420*7c478bd9Sstevel@tonic-gate 		 */
421*7c478bd9Sstevel@tonic-gate 		if (saved_gen != s1394_fa_list_gen(hal, S1394_FA_TYPE_CMP)) {
422*7c478bd9Sstevel@tonic-gate 			TNF_PROBE_2(s1394_cmp_notify_reg_change_error,
423*7c478bd9Sstevel@tonic-gate 			    S1394_TNF_SL_CMP_ERROR, "",
424*7c478bd9Sstevel@tonic-gate 			    tnf_string, msg, "list gen changed",
425*7c478bd9Sstevel@tonic-gate 			    tnf_opaque, num_retries, num_retries);
426*7c478bd9Sstevel@tonic-gate 			if (++num_retries <= s1394_cmp_notify_retry_cnt) {
427*7c478bd9Sstevel@tonic-gate 				goto start;
428*7c478bd9Sstevel@tonic-gate 			} else {
429*7c478bd9Sstevel@tonic-gate 				break;
430*7c478bd9Sstevel@tonic-gate 			}
431*7c478bd9Sstevel@tonic-gate 		}
432*7c478bd9Sstevel@tonic-gate 	}
433*7c478bd9Sstevel@tonic-gate 
434*7c478bd9Sstevel@tonic-gate 	rw_exit(&hal->target_list_rwlock);
435*7c478bd9Sstevel@tonic-gate 
436*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_0_DEBUG(s1394_cmp_notify_reg_change_exit,
437*7c478bd9Sstevel@tonic-gate 	    S1394_TNF_SL_CMP_STACK, "");
438*7c478bd9Sstevel@tonic-gate }
439