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